Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SALauncherLoader.java
41159 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot;2526import java.io.*;27import java.net.*;28import java.util.*;29import java.security.*;3031/**32* SA uses native debugger back-end library - libsaproc.so on Unix platforms.33* Starting from 5.0, in Solaris & Linux JDK "libsaproc.so" is shipped with JDK34* and is kept jre/lib/cpu directory (where all other JDK platform libraries35* are kept). This implies that always that jre copy of libsaproc.so will be36* used and the copy of libsaproc.so built from SA sources here will not37* be used at all. We can override libsaproc.so using this class loader38* as System class loader using "java.system.class.loader" property. This39* class loader loads classes paths specified paths using the System property40* "java.class.path". Because, this class loader loads SA debugger classes41* (among other classes), JVM calls findLibrary override here. In this42* findLibrary, we first check the library in the directories specified through43* "sa.library.path" System property. This way updated/latest SA native library44* can be loaded instead of the one from JDK's jre/lib directory.45*/46public class SALauncherLoader extends URLClassLoader {4748/**49* Checks native libraries under directories specified using50* the System property "sa.library.path".51*/52public String findLibrary(String name) {53name = System.mapLibraryName(name);54for (int i = 0; i < libpaths.length; i++) {55File file = new File(new File(libpaths[i]), name);56if (file.exists()) {57return file.getAbsolutePath();58}59}60return null;61}6263public SALauncherLoader(ClassLoader parent) {64super(getClassPath(), parent);65String salibpath = System.getProperty("sa.library.path");66if (salibpath != null) {67libpaths = salibpath.split(File.pathSeparator);68} else {69libpaths = new String[0];70}71}7273/**74* Override loadClass so we can checkPackageAccess.75*/76public synchronized Class loadClass(String name, boolean resolve)77throws ClassNotFoundException {78int i = name.lastIndexOf('.');79if (i != -1) {80@SuppressWarnings("removal")81SecurityManager sm = System.getSecurityManager();82if (sm != null) {83sm.checkPackageAccess(name.substring(0, i));84}85}8687Class clazz = findLoadedClass(name);88if (clazz != null) return clazz;8990/*91* NOTE: Unlike 'usual' class loaders, we do *not* delegate to92* the parent loader first. We attempt to load the class93* ourselves first and use parent loader only if we can't load.94* This is because the parent of this loader is 'default'95* System loader that can 'see' all SA classes in classpath and96* so will load those if delegated. And if parent loads SA classes,97* then JVM won't call findNative override in this class.98*/99try {100return findClass(name);101} catch (ClassNotFoundException cnfe) {102return (super.loadClass(name, resolve));103}104}105106/**107* allow any classes loaded from classpath to exit the VM.108*/109protected PermissionCollection getPermissions(CodeSource codesource) {110PermissionCollection perms = super.getPermissions(codesource);111perms.add(new RuntimePermission("exitVM"));112return perms;113}114115//-- Internals only below this point116117private String[] libpaths;118119private static URL[] getClassPath() {120final String s = System.getProperty("java.class.path");121final File[] path = (s == null) ? new File[0] : getClassPath(s);122123return pathToURLs(path);124}125126private static URL[] pathToURLs(File[] path) {127URL[] urls = new URL[path.length];128for (int i = 0; i < path.length; i++) {129urls[i] = getFileURL(path[i]);130}131return urls;132}133134private static File[] getClassPath(String cp) {135String[] tmp = cp.split(File.pathSeparator);136File[] paths = new File[tmp.length];137for (int i = 0; i < paths.length; i++) {138paths[i] = new File(tmp[i].equals("")? "." : tmp[i]);139}140return paths;141}142143private static URL getFileURL(File file) {144try {145file = file.getCanonicalFile();146} catch (IOException e) {147e.printStackTrace();148}149150try {151return file.toURI().toURL();152} catch (MalformedURLException mue) {153throw new InternalError(mue.getMessage());154}155}156}157158159