4570N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. 4570N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4570N/A * This code is free software; you can redistribute it and/or modify it 4570N/A * under the terms of the GNU General Public License version 2 only, as 4570N/A * published by the Free Software Foundation. Oracle designates this 4570N/A * particular file as subject to the "Classpath" exception as provided 4570N/A * by Oracle in the LICENSE file that accompanied this code. 4570N/A * This code is distributed in the hope that it will be useful, but WITHOUT 4570N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4570N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4570N/A * version 2 for more details (a copy is included in the LICENSE file that 4570N/A * You should have received a copy of the GNU General Public License version 4570N/A * 2 along with this work; if not, write to the Free Software Foundation, 4570N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4570N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4570N/A * Copyright 2003 Wily Technology, Inc. 4570N/A * This module contains the direct interface points with the JVMTI. 4570N/A * The OnLoad handler is here, along with the various event handlers. 4570N/A * Parse -javaagent tail, of the form name[=options], into name 4570N/A * and options. Returned values are heap allocated and options maybe 4570N/A * NULL. Returns 0 if parse succeeds, -1 if allocation fails. 4570N/A * Get the value of an attribute in an attribute list. Returns NULL 4570N/A * Parse any capability settings in the JAR manifest and 4570N/A * convert them to JVM TI capabilities. 4570N/A /* set redefineClasses capability */ 4570N/A /* create an environment which has the retransformClasses capability */ 4570N/A /* set setNativeMethodPrefix capability */ 4570N/A /* for retransformClasses testing, set capability to use original method order */ 4570N/A * This will be called once for every -javaagent on the command line. 4570N/A * Each call to Agent_OnLoad will create its own agent and agent data. 4570N/A * The argument tail string provided to Agent_OnLoad will be of form 4570N/A * <jarfile>[=<options>]. The tail string is split into the jarfile and 4570N/A * options components. The jarfile manifest is parsed and the value of the 4570N/A * Premain-Class attribute will become the agent's premain class. The jar 4570N/A * file is then added to the system class path, and if the Boot-Class-Path 4570N/A * attribute is present then all relative URLs in the value are processed 4570N/A * to create boot class path segments to append to the boot class path. 4570N/A * Parse <jarfile>[=options] into jarfile and options 4570N/A * Agent_OnLoad is specified to provide the agent options 4570N/A * argument tail in modified UTF8. However for 1.5.0 this is 4570N/A * actually in the platform encoding - see 5049313. 4570N/A * Open zip/jar file and parse archive. If can't be opened or 4570N/A * not a zip file return error. Also if Premain-Class attribute 4570N/A * isn't present we return an error. 4570N/A * The value of the Premain-Class attribute becomes the agent 4570N/A * class name. The manifest is in UTF8 so need to convert to 4570N/A * modified UTF8 (see JNI spec). * If the Boot-Class-Path attribute is specified then we process * each relative URL and add it to the bootclasspath. * Convert JAR attributes into agent capabilities * Track (record) the agent class name and options data fprintf(
stderr,
"java.lang.instrument/-javaagent: cannot create native agent.\n");
fprintf(
stderr,
"java.lang.instrument/-javaagent: initialization of native agent failed.\n");
fprintf(
stderr,
"java.lang.instrument/-javaagent: allocation failure.\n");
fprintf(
stderr,
"java.lang.instrument/-javaagent: unknown error\n");
* Agent_OnAttach returns a jint. 0/JNI_OK indicates success and non-0 * indicates an error. To allow the attach mechanism throw an * AgentInitializationException with a reasonable exception message we define * a few specific errors here. * This will be called once each time a tool attaches to the VM and loads * Need JNIEnv - guaranteed to be called from thread that is already * Parse <jarfile>[=options] into jarfile and options * Open the JAR file and parse the manifest fprintf(
stderr,
"Failed to find Agent-Class manifest attribute from %s\n",
* Add the jarfile to the system class path "- not supported by system class loader or configuration error!\n",
* The value of the Agent-Class attribute becomes the agent * class name. The manifest is in UTF8 so need to convert to * modified UTF8 (see JNI spec). * If the Boot-Class-Path attribute is specified then we process * each URL - in the live phase only JAR files will be added. * Convert JAR attributes into agent capabilities * Create the java.lang.instrument.Instrumentation instance * Turn on the ClassFileLoadHook. * We have two "stages" of callback support. * At OnLoad time, we install a VMInit handler. * When the VMInit handler runs, we remove the VMInit handler and install a * ClassFileLoadHook handler. /* process the premain calls on the all the JPL agents */ /* if we fail to start cleanly, bring down the JVM */ /* if something is internally inconsistent (no agent), just silently return without touching the buffer */ * URLs in Boot-Class-Path attributes are separated by one or more spaces. * This function splits the attribute value into a list of path segments. * The attribute value is in UTF8 but cannot contain NUL. Also non US-ASCII * characters must be escaped (URI syntax) so safe to iterate through the while (*c ==
' ') c++;
/* skip leading spaces */ if ((c >=
'0') && (c <=
'9'))
if ((c >=
'a') && (c <=
'f'))
if ((c >=
'A') && (c <=
'F'))
* Evaluates all escapes in s. Assumes that escapes are well-formed * syntactically, i.e., of the form %XX. * If the path does not require decoding the the original path is * returned. Otherwise the decoded path (heap allocated) is returned, * along with the length of the decoded path. Note that the return * string will not be null terminated after decoding. return (
char*)s;
/* no escapes, we are done */ return result;
// not null terminated. * Append the given jar file to the system class path. This should succeed in the * onload phase but may fail in the live phase if the system class loader doesn't * support appending to the class path. /* can be called from any phase */ "JAR file to system class path during the live phase!\n");
"AddToSystemClassLoaderSearch\n",
jvmtierr);
* res = func, free'ing the previous value of 'res' if function * This function takes the value of the Boot-Class-Path attribute, * splits it into the individual path segments, and then combines it * with the path to the jar file to create the path to be added * Each individual path segment starts out as a UTF8 string. Additionally * as the path is specified to use URI path syntax all non US-ASCII * characters are escaped. Once the URI path is decoded we get a UTF8 * string which must then be converted to the platform encoding (as it * will be combined with the platform path of the jar file). Once * converted it is then normalized (remove duplicate slashes, etc.). * If the resulting path is an absolute path (starts with a slash for * example) then the path will be added to the bootclasspath. Otherwise * if it's not absolute then we get the canoncial path of the agent jar * file and then resolve the path in the context of the base path of * Split the attribute value into the individual path segments * and process each in sequence for (i=0; i<
count; i++) {
* The path segment at this point is a pointer into the attribute * value. As it will go through a number of transformation (tossing away * the previous results as we go along) it make it easier if the path * starts out as a heap allocated string. * The attribute is specified to be a list of relative URIs so in theory * there could be a query component - if so, get rid of it. * Check for characters that are not allowed in the path component of fprintf(
stderr,
"WARNING: illegal character in Boot-Class-Path value: %s\n",
* Next decode any escaped characters. The result is a UTF8 string. * Convert to the platform encoding /* bogus value - exceeds maximum path size or unable to convert */ * Post-process the URI path - needed on Windows to transform * Normalize the path - no duplicate slashes (except UNCs on Windows), trailing * If the path is an absolute path then add to the bootclassloader * search path. Otherwise we get the canonical path of the agent jar * and then use its base path (directory) to resolve the given path * NOTE: JVMTI is specified to use modified UTF8 strings (like JNI). * In 1.5.0 the AddToBootstrapClassLoaderSearch takes a platform string /* print warning if boot class path not updated */ /* finished with the path */