Path: blob/master/src/java.instrument/share/classes/java/lang/instrument/package-info.java
41159 views
/*1* Copyright (c) 2003, 2020, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Copyright 2003 Wily Technology, Inc.27*/2829/**30* Provides services that allow Java programming language agents to instrument31* programs running on the JVM. The mechanism for instrumentation is modification32* of the byte-codes of methods.33*34* <p> An agent is deployed as a JAR file. An attribute in the JAR file manifest35* specifies the agent class which will be loaded to start the agent. Agents can36* be started in several ways:37*38* <ol>39* <li><p> For implementations that support a command-line interface, an agent40* can be started by specifying an option on the command-line. </p></li>41*42* <li><p> An implementation may support a mechanism to start agents some time43* after the VM has started. For example, an implementation may provide a44* mechanism that allows a tool to <i>attach</i> to a running application, and45* initiate the loading of the tool's agent into the running application. </p></li>46*47* <li><p> An agent may be packaged with an application in an executable JAR48* file.</p></li>49* </ol>50*51* <p> Agents can transform classes in arbitrary ways at load time, transform52* modules, or transform the bytecode of methods of already loaded classes.53* Developers or administrators that deploy agents, deploy applications that54* package an agent with the application, or use tools that load agents into a55* running application, are responsible for verifying the trustworthiness of each56* agent including the content and structure of the agent JAR file.57*58* <p> The three ways to start an agent are described below.59*60* <h2>Starting an Agent from the Command-Line Interface</h2>61*62* <p> Where an implementation provides a means to start agents from the63* command-line interface, an agent is started by adding the following option64* to the command-line:65*66* <blockquote>{@code67* -javaagent:<jarpath>[=<options>]68* }</blockquote>69*70* where <i>{@code <jarpath>}</i> is the path to the agent JAR file and71* <i>{@code <options>}</i> is the agent options.72*73* <p> The manifest of the agent JAR file must contain the attribute {@code74* Premain-Class} in its main manifest. The value of this attribute is the75* name of the <i>agent class</i>. The agent class must implement a public76* static {@code premain} method similar in principle to the {@code main}77* application entry point. After the Java Virtual Machine (JVM) has78* initialized, the {@code premain} method will be called, then the real79* application {@code main} method. The {@code premain} method must return80* in order for the startup to proceed.81*82* <p> The {@code premain} method has one of two possible signatures. The83* JVM first attempts to invoke the following method on the agent class:84*85* <blockquote>{@code86* public static void premain(String agentArgs, Instrumentation inst)87* }</blockquote>88*89* <p> If the agent class does not implement this method then the JVM will90* attempt to invoke:91* <blockquote>{@code92* public static void premain(String agentArgs)93* }</blockquote>9495* <p> The agent class may also have an {@code agentmain} method for use when96* the agent is started after VM startup (see below). When the agent is started97* using a command-line option, the {@code agentmain} method is not invoked.98*99* <p> Each agent is passed its agent options via the {@code agentArgs} parameter.100* The agent options are passed as a single string, any additional parsing101* should be performed by the agent itself.102*103* <p> If the agent cannot be started (for example, because the agent class104* cannot be loaded, or because the agent class does not have an appropriate105* {@code premain} method), the JVM will abort. If a {@code premain} method106* throws an uncaught exception, the JVM will abort.107*108* <p> An implementation is not required to provide a way to start agents109* from the command-line interface. When it does, then it supports the110* {@code -javaagent} option as specified above. The {@code -javaagent} option111* may be used multiple times on the same command-line, thus starting multiple112* agents. The {@code premain} methods will be called in the order that the113* agents are specified on the command line. More than one agent may use the114* same <i>{@code <jarpath>}</i>.115*116* <p> There are no modeling restrictions on what the agent {@code premain}117* method may do. Anything application {@code main} can do, including creating118* threads, is legal from {@code premain}.119*120*121* <h2>Starting an Agent After VM Startup</h2>122*123* <p> An implementation may provide a mechanism to start agents sometime after124* the the VM has started. The details as to how this is initiated are125* implementation specific but typically the application has already started and126* its {@code main} method has already been invoked. In cases where an127* implementation supports the starting of agents after the VM has started the128* following applies:129*130* <ol>131*132* <li><p> The manifest of the agent JAR must contain the attribute {@code133* Agent-Class} in its main manfiest. The value of this attribute is the name134* of the <i>agent class</i>. </p></li>135*136* <li><p> The agent class must implement a public static {@code agentmain}137* method. </p></li>138*139* </ol>140*141* <p> The {@code agentmain} method has one of two possible signatures. The JVM142* first attempts to invoke the following method on the agent class:143*144* <blockquote>{@code145* public static void agentmain(String agentArgs, Instrumentation inst)146* }</blockquote>147*148* <p> If the agent class does not implement this method then the JVM will149* attempt to invoke:150*151* <blockquote>{@code152* public static void agentmain(String agentArgs)153* }</blockquote>154*155* <p> The agent class may also have a {@code premain} method for use when the156* agent is started using a command-line option. When the agent is started after157* VM startup the {@code premain} method is not invoked.158*159* <p> The agent is passed its agent options via the {@code agentArgs}160* parameter. The agent options are passed as a single string, any additional161* parsing should be performed by the agent itself.162*163* <p> The {@code agentmain} method should do any necessary initialization164* required to start the agent. When startup is complete the method should165* return. If the agent cannot be started (for example, because the agent class166* cannot be loaded, or because the agent class does not have a conformant167* {@code agentmain} method), the JVM will not abort. If the {@code agentmain}168* method throws an uncaught exception it will be ignored (but may be logged169* by the JVM for troubleshooting purposes).170*171*172* <h2>Including an Agent in an Executable JAR file</h2>173*174* <p> The JAR File Specification defines manifest attributes for standalone175* applications that are packaged as <em>executable JAR files</em>. If an176* implementation supports a mechanism to start an application as an executable177* JAR then the main manifest may include the {@code Launcher-Agent-Class}178* attribute to specify the class name of an agent to start before the application179* {@code main} method is invoked. The Java virtual machine attempts to180* invoke the following method on the agent class:181*182* <blockquote>{@code183* public static void agentmain(String agentArgs, Instrumentation inst)184* }</blockquote>185*186* <p> If the agent class does not implement this method then the JVM will187* attempt to invoke:188*189* <blockquote>{@code190* public static void agentmain(String agentArgs)191* }</blockquote>192*193* <p> The value of the {@code agentArgs} parameter is always the empty string.194*195* <p> The {@code agentmain} method should do any necessary initialization196* required to start the agent and return. If the agent cannot be started, for197* example the agent class cannot be loaded, the agent class does not define a198* conformant {@code agentmain} method, or the {@code agentmain} method throws199* an uncaught exception or error, the JVM will abort.200*201*202* <h2> Loading agent classes and the modules/classes available to the agent203* class </h2>204*205* <p> Classes loaded from the agent JAR file are loaded by the206* {@linkplain ClassLoader#getSystemClassLoader() system class loader} and are207* members of the system class loader's {@linkplain ClassLoader#getUnnamedModule()208* unnamed module}. The system class loader typically defines the class containing209* the application {@code main} method too.210*211* <p> The classes visible to the agent class are the classes visible to the system212* class loader and minimally include:213*214* <ul>215*216* <li><p> The classes in packages exported by the modules in the {@linkplain217* ModuleLayer#boot() boot layer}. Whether the boot layer contains all platform218* modules or not will depend on the initial module or how the application was219* started. </p></li>220*221* <li><p> The classes that can be defined by the system class loader (typically222* the class path) to be members of its unnamed module. </p></li>223*224* <li><p> Any classes that the agent arranges to be defined by the bootstrap225* class loader to be members of its unnamed module. </p></li>226*227* </ul>228*229* <p> If agent classes need to link to classes in platform (or other) modules230* that are not in the boot layer then the application may need to be started in231* a way that ensures that these modules are in the boot layer. In the JDK232* implementation for example, the {@code --add-modules} command line option can233* be used to add modules to the set of root modules to resolve at startup. </p>234*235* <p> Supporting classes that the agent arranges to be loaded by the bootstrap236* class loader (by means of {@link Instrumentation#appendToBootstrapClassLoaderSearch237* appendToBootstrapClassLoaderSearch} or the {@code Boot-Class-Path} attribute238* specified below), must link only to classes defined to the bootstrap class loader.239* There is no guarantee that all platform classes can be defined by the boot240* class loader.241*242* <p> If a custom system class loader is configured (by means of the system property243* {@code java.system.class.loader} as specified in the {@link244* ClassLoader#getSystemClassLoader() getSystemClassLoader} method) then it must245* define the {@code appendToClassPathForInstrumentation} method as specified in246* {@link Instrumentation#appendToSystemClassLoaderSearch appendToSystemClassLoaderSearch}.247* In other words, a custom system class loader must support the mechanism to248* add an agent JAR file to the system class loader search.249*250* <h2>Manifest Attributes</h2>251*252* <p> The following manifest attributes are defined for an agent JAR file:253*254* <blockquote><dl>255*256* <dt>{@code Premain-Class}</dt>257* <dd> When an agent is specified at JVM launch time this attribute specifies258* the agent class. That is, the class containing the {@code premain} method.259* When an agent is specified at JVM launch time this attribute is required. If260* the attribute is not present the JVM will abort. Note: this is a class name,261* not a file name or path. </dd>262*263* <dt>{@code Agent-Class}</dt>264* <dd> If an implementation supports a mechanism to start agents sometime after265* the VM has started then this attribute specifies the agent class. That is,266* the class containing the {@code agentmain} method. This attribute is required267* if it is not present the agent will not be started. Note: this is a class name,268* not a file name or path. </dd>269*270* <dt>{@code Launcher-Agent-Class}</dt>271* <dd> If an implementation supports a mechanism to start an application as an272* executable JAR then the main manifest may include this attribute to specify273* the class name of an agent to start before the application {@code main}274* method is invoked. </dd>275*276* <dt>{@code Boot-Class-Path}</dt>277* <dd> A list of paths to be searched by the bootstrap class loader. Paths278* represent directories or libraries (commonly referred to as JAR or zip279* libraries on many platforms). These paths are searched by the bootstrap class280* loader after the platform specific mechanisms of locating a class have failed.281* Paths are searched in the order listed. Paths in the list are separated by one282* or more spaces. A path takes the syntax of the path component of a hierarchical283* URI. The path is absolute if it begins with a slash character ('/'), otherwise284* it is relative. A relative path is resolved against the absolute path of the285* agent JAR file. Malformed and non-existent paths are ignored. When an agent is286* started sometime after the VM has started then paths that do not represent a287* JAR file are ignored. This attribute is optional. </dd>288*289* <dt>{@code Can-Redefine-Classes}</dt>290* <dd> Boolean ({@code true} or {@code false}, case irrelevant). Is the ability291* to redefine classes needed by this agent. Values other than {@code true} are292* considered {@code false}. This attribute is optional, the default is {@code293* false}. </dd>294*295* <dt>{@code Can-Retransform-Classes}</dt>296* <dd> Boolean ({@code true} or {@code false}, case irrelevant). Is the ability297* to retransform classes needed by this agent. Values other than {@code true}298* are considered {@code false}. This attribute is optional, the default is299* {@code false}. </dd>300*301* <dt>{@code Can-Set-Native-Method-Prefix}</dt>302* <dd> Boolean ({@code true} or {@code false}, case irrelevant). Is the ability303* to set native method prefix needed by this agent. Values other than {@code304* true} are considered {@code false}. This attribute is optional, the default305* is {@code false}. </dd>306*307* </dl></blockquote>308*309* <p> An agent JAR file may have both the {@code Premain-Class} and {@code310* Agent-Class} attributes present in the manifest. When the agent is started311* on the command-line using the {@code -javaagent} option then the {@code312* Premain-Class} attribute specifies the name of the agent class and the {@code313* Agent-Class} attribute is ignored. Similarly, if the agent is started sometime314* after the VM has started, then the {@code Agent-Class} attribute specifies315* the name of the agent class (the value of {@code Premain-Class} attribute is316* ignored).317*318*319* <h2>Instrumenting code in modules</h2>320*321* <p> As an aid to agents that deploy supporting classes on the search path of322* the bootstrap class loader, or the search path of the class loader that loads323* the main agent class, the Java virtual machine arranges for the module of324* transformed classes to read the unnamed module of both class loaders.325*326* @since 1.5327* @revised 1.6328* @revised 9329*/330331package java.lang.instrument;332333334