src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SALauncherLoader.java
author jjg
Mon, 20 May 2019 10:57:57 -0700
changeset 54950 46ae54c3026d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8223663: Update links for tool guides Reviewed-by: alanb, erikj, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
/**
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
 * SA uses native debugger back-end library - libsaproc.so on Unix platforms.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
 * Starting from 5.0, in Solaris & Linux JDK "libsaproc.so" is shipped with JDK
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
 * and is kept jre/lib/cpu directory (where all other JDK platform libraries
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
 * are kept). This implies that always that jre copy of libsaproc.so will be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
 * used and the copy of libsaproc.so built from SA sources here will not
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
 * be used at all. We can override libsaproc.so using this class loader
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
 * as System class loader using "java.system.class.loader" property. This
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
 * class loader loads classes paths specified paths using the System property
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
 * "java.class.path". Because, this class loader loads SA debugger classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
 * (among other classes), JVM calls findLibrary override here. In this
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
 * findLibrary, we first check the library in the directories specified through
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
 * "sa.library.path" System property. This way updated/latest SA native library
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
 * can be loaded instead of the one from JDK's jre/lib directory.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
public class SALauncherLoader extends URLClassLoader {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    /**
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
     * Checks native libraries under directories specified using
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
     * the System property "sa.library.path".
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    public String findLibrary(String name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
        name = System.mapLibraryName(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
        for (int i = 0; i < libpaths.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
            File file = new File(new File(libpaths[i]), name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
            if (file.exists()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
                return file.getAbsolutePath();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
        return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    public SALauncherLoader(ClassLoader parent) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
        super(getClassPath(), parent);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        String salibpath = System.getProperty("sa.library.path");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
        if (salibpath != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
            libpaths = salibpath.split(File.pathSeparator);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
            libpaths = new String[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    /**
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
     * Override loadClass so we can checkPackageAccess.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    public synchronized Class loadClass(String name, boolean resolve)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
            throws ClassNotFoundException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
        int i = name.lastIndexOf('.');
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
        if (i != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
            SecurityManager sm = System.getSecurityManager();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
            if (sm != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
                sm.checkPackageAccess(name.substring(0, i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
        Class clazz = findLoadedClass(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
        if (clazz != null) return clazz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
         * NOTE: Unlike 'usual' class loaders, we do *not* delegate to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
         * the parent loader first. We attempt to load the class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
         * ourselves first and use parent loader only if we can't load.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
         * This is because the parent of this loader is 'default'
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
         * System loader that can 'see' all SA classes in classpath and
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
         * so will load those if delegated. And if parent loads SA classes,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
         * then JVM won't call findNative override in this class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
         */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
            return findClass(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
        } catch (ClassNotFoundException cnfe) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
            return (super.loadClass(name, resolve));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    /**
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
     * allow any classes loaded from classpath to exit the VM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    protected PermissionCollection getPermissions(CodeSource codesource) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
        PermissionCollection perms = super.getPermissions(codesource);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
        perms.add(new RuntimePermission("exitVM"));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        return perms;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    //-- Internals only below this point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    private String[] libpaths;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    private static URL[] getClassPath() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
        final String s = System.getProperty("java.class.path");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
        final File[] path = (s == null) ? new File[0] : getClassPath(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
        return pathToURLs(path);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    private static URL[] pathToURLs(File[] path) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
        URL[] urls = new URL[path.length];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
        for (int i = 0; i < path.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
            urls[i] = getFileURL(path[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        return urls;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    private static File[] getClassPath(String cp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
        String[] tmp = cp.split(File.pathSeparator);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
        File[] paths = new File[tmp.length];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
        for (int i = 0; i < paths.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
            paths[i] = new File(tmp[i].equals("")? "." : tmp[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
        return paths;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    private static URL getFileURL(File file) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
            file = file.getCanonicalFile();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
        } catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
            e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
        try {
578
862a85ed20db 6670684: 4/5 SA command universe did not print out CMS space information
dcubed
parents: 1
diff changeset
   151
            return file.toURI().toURL();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        } catch (MalformedURLException mue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
            throw new InternalError(mue.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
}