1 <!-- |
|
2 Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
|
5 This code is free software; you can redistribute it and/or modify it |
|
6 under the terms of the GNU General Public License version 2 only, as |
|
7 published by the Free Software Foundation. Oracle designates this |
|
8 particular file as subject to the "Classpath" exception as provided |
|
9 by Oracle in the LICENSE file that accompanied this code. |
|
10 |
|
11 This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 version 2 for more details (a copy is included in the LICENSE file that |
|
15 accompanied this code). |
|
16 |
|
17 You should have received a copy of the GNU General Public License version |
|
18 2 along with this work; if not, write to the Free Software Foundation, |
|
19 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
|
21 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 or visit www.oracle.com if you need additional information or have any |
|
23 questions. |
|
24 --> |
|
25 |
|
26 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> |
|
27 <html> |
|
28 <head> |
|
29 <!-- |
|
30 |
|
31 Copyright 2003 Wily Technology, Inc. |
|
32 |
|
33 --> |
|
34 </head> |
|
35 |
|
36 <body bgcolor="white"> |
|
37 |
|
38 Provides services that allow Java programming language agents to instrument programs running on the JVM. |
|
39 The mechanism for instrumentation is modification of the byte-codes of methods. |
|
40 |
|
41 <h2>Package Specification</h2> |
|
42 |
|
43 <P> |
|
44 An agent is deployed as a JAR file. An attribute in the JAR file manifest specifies the |
|
45 agent class which will be loaded to start the agent. For implementations that support a command-line |
|
46 interface, an agent is started by specifying an option on the command-line. |
|
47 Implementations may also support a mechanism to start agents some time after the VM has |
|
48 started. For example, an implementation may provide a mechanism that allows a tool to |
|
49 <i>attach</i> to a running application, and initiate the loading of the tool's agent into |
|
50 the running application. The details as to how the load is initiated, is implementation |
|
51 dependent. |
|
52 |
|
53 <h3>Command-Line Interface</h3> |
|
54 |
|
55 <P> |
|
56 An implementation is not required to provide a way to start agents from the |
|
57 command-line interface. On implementations that do provide a way to start agents |
|
58 from the command-line interface, an agent is started by adding this option to |
|
59 the command-line: |
|
60 <blockquote> |
|
61 <code><b>-javaagent:</b></code><i>jarpath[</i><code><b>=</b></code><i>options]</i> |
|
62 </blockquote> |
|
63 <i>jarpath</i> is the path to the agent JAR file. |
|
64 <i>options</i> is the agent options. |
|
65 This switch may be used multiple times on the same command-line, |
|
66 thus creating multiple agents. |
|
67 More than one agent may use the same <i>jarpath</i>. |
|
68 An agent JAR file must conform to the JAR file specification. |
|
69 |
|
70 <P> |
|
71 The manifest of the agent JAR file must contain the attribute <code>Premain-Class</code>. The |
|
72 value of this attribute is the name of the <i>agent class</i>. The agent class must implement a |
|
73 public static <code>premain</code> method similar in principle to the <code>main</code> application |
|
74 entry point. After the Java Virtual Machine (JVM) has initialized, each <code>premain</code> method |
|
75 will be called in the order the agents were specified, then the real application |
|
76 <code>main</code> method will be called. |
|
77 Each <code>premain</code> method must return in order for the startup sequence to proceed. |
|
78 |
|
79 <P> |
|
80 The <code>premain</code> method has one of two possible signatures. The JVM first attempts to |
|
81 invoke the following method on the agent class: |
|
82 |
|
83 <blockquote> |
|
84 <code>public static void |
|
85 premain(String agentArgs, Instrumentation inst); |
|
86 </code> |
|
87 </blockquote> |
|
88 |
|
89 <P> |
|
90 If the agent class does not implement this method then the JVM will attempt to invoke: |
|
91 |
|
92 <blockquote> |
|
93 <code>public static void |
|
94 premain(String agentArgs); |
|
95 </code> |
|
96 </blockquote> |
|
97 |
|
98 <P> |
|
99 The agent class may also have an <code>agentmain</code> method for use when the agent is started |
|
100 after VM startup. When the agent is started using a command-line option, the <code>agentmain</code> |
|
101 method is not invoked. |
|
102 |
|
103 |
|
104 <P> |
|
105 The agent class will be loaded by the system class loader |
|
106 (see {@link java.lang.ClassLoader#getSystemClassLoader ClassLoader.getSystemClassLoader}). This is |
|
107 the class loader which typically loads the class containing the application <code>main</code> method. |
|
108 The system class loader must support a mechanism to add an agent JAR file to the system class path. |
|
109 If it is a custom system class loader then it must define the |
|
110 <code>appendToClassPathForInstrumentation</code> method as specified in |
|
111 {@link Instrumentation#appendToSystemClassLoaderSearch appendToSystemClassLoaderSearch}. |
|
112 The <code>premain</code> methods will be run under the same security and classloader |
|
113 rules as the application <code>main</code> method. |
|
114 There are no modeling restrictions on what the agent <code>premain</code> method may do. |
|
115 Anything application <code>main</code> can do, including creating threads, is legal from |
|
116 <code>premain</code>. |
|
117 |
|
118 |
|
119 <P> |
|
120 Each agent is passed its agent options via the <code>agentArgs</code> parameter. |
|
121 The agent options are passed as a single string, |
|
122 any additional parsing should be performed by the agent itself. |
|
123 |
|
124 <P> |
|
125 If the agent cannot be resolved |
|
126 (for example, because the agent class cannot be loaded, |
|
127 or because the agent class does not have an appropriate <code>premain</code> method), the JVM will abort. |
|
128 If a <code>premain</code> method throws an uncaught exception, the JVM will abort. |
|
129 |
|
130 |
|
131 <h3>Starting Agents After VM Startup</h3> |
|
132 |
|
133 <p> |
|
134 An implementation may provide a mechanism to start agents sometime after the |
|
135 the VM has started. The details as to how this is initiated are implementation |
|
136 specific but typically the application has already started and its <code> |
|
137 main</code> method has already been invoked. In cases where an implementation |
|
138 supports the starting of agents after the VM has started the following applies: |
|
139 |
|
140 <ol> |
|
141 <li><p>The manifest of the agent JAR must contain the attribute <code>Agent-Class</code>. |
|
142 The value of this attribute is the name of the <i>agent class</i>. </p></li> |
|
143 |
|
144 <li><p>The agent class must implement a public static <code>agentmain</code> method. </p></li> |
|
145 |
|
146 <li><p>The system class loader ( |
|
147 {@link java.lang.ClassLoader#getSystemClassLoader ClassLoader.getSystemClassLoader}) must |
|
148 support a mechanism to add an agent JAR file to the system class path. |
|
149 If it is a custom system class loader then it must define the |
|
150 <code>appendToClassPathForInstrumentation</code> method as specified in |
|
151 {@link Instrumentation#appendToSystemClassLoaderSearch appendToSystemClassLoaderSearch}.</li> |
|
152 </ol> |
|
153 |
|
154 <P> |
|
155 The agent JAR is appended to the system class path. This is the class loader that typically loads |
|
156 the class containing the application <code>main</code> method. The agent class is loaded and the |
|
157 JVM attempts to invoke the <code>agentmain</code> method. The JVM first attempts to invoke |
|
158 the following method on the agent class: |
|
159 |
|
160 <blockquote> |
|
161 <code>public static void |
|
162 agentmain(String agentArgs, Instrumentation inst); |
|
163 </code> |
|
164 </blockquote> |
|
165 |
|
166 <P> |
|
167 If the agent class does not implement this method then the JVM will attempt to invoke: |
|
168 |
|
169 <blockquote> |
|
170 <code>public static void |
|
171 agentmain(String agentArgs); |
|
172 </code> |
|
173 </blockquote> |
|
174 |
|
175 <P> |
|
176 The agent class may also have an <code>premain</code> method for use when the agent is started |
|
177 using a command-line option. When the agent is started after VM startup the <code>premain</code> |
|
178 method is not invoked. |
|
179 |
|
180 |
|
181 <P> |
|
182 The agent is passed its agent options via the <code>agentArgs</code> parameter. |
|
183 The agent options are passed as a single string, |
|
184 any additional parsing should be performed by the agent itself. |
|
185 |
|
186 <P> |
|
187 The <code>agentmain</code> method should do any necessary initialization |
|
188 required to start the agent. When startup is complete the method should |
|
189 return. If the agent cannot be started |
|
190 (for example, because the agent class cannot be loaded, |
|
191 or because the agent class does not have a conformant <code>agentmain</code> method), the JVM will |
|
192 not abort. If the <code>agentmain</code> method throws an uncaught exception it will be ignored. |
|
193 |
|
194 |
|
195 <h3>Deploying Agents in Executable JAR file</h3> |
|
196 |
|
197 The JAR File Specification defines manifest attributes for standalone applications that are |
|
198 bundled as <em>executable JAR files</em>. If an implementation supports a mechanism to start |
|
199 an application as an executable JAR then the main manifest may include the |
|
200 <code>Launcher-Agent-Class</code> attribute to specify the class name |
|
201 of an agent to start before the application <code>main</code> method is invoked. The Java |
|
202 virtual machine attempts to invoke the following method on the agent class: |
|
203 |
|
204 <blockquote> |
|
205 <code>public static void |
|
206 agentmain(String agentArgs, Instrumentation inst); |
|
207 </code> |
|
208 </blockquote> |
|
209 |
|
210 <P> |
|
211 If the agent class does not implement this method then the JVM will attempt to invoke: |
|
212 |
|
213 <blockquote> |
|
214 <code>public static void |
|
215 agentmain(String agentArgs); |
|
216 </code> |
|
217 </blockquote> |
|
218 |
|
219 <p> |
|
220 The value of the <code>agentArgs</code> parameter is always the empty string. |
|
221 |
|
222 <P> |
|
223 The <code>agentmain</code> method should do any necessary initialization |
|
224 required to start the agent and return. If the agent cannot be started, for |
|
225 example the agent class cannot be loaded, the agent class does not define a |
|
226 conformant <code>agentmain</code> method, or the <code>agentmain</code> method |
|
227 throws an uncaught exception or error, the JVM will abort. |
|
228 |
|
229 |
|
230 <h3>Visibility</h3> |
|
231 |
|
232 The types visible to the agent class are the types visible to the system class |
|
233 loader. They minimally include the types in packages exported by |
|
234 <a href="{@docRoot}/java.base-summary.html">java.base</a> and |
|
235 <a href="{@docRoot}/java.instrument-summary.html">java.instrument</a>. |
|
236 Whether all {@linkplain ClassLoader#getPlatformClassLoader() platform classes} |
|
237 are visible or not will depend on the initial module or application. |
|
238 |
|
239 <p> |
|
240 Supporting classes that the agent makes visible to the bootstrap class loader |
|
241 (by means of {@link Instrumentation#appendToBootstrapClassLoaderSearch |
|
242 appendToBootstrapClassLoaderSearch} or the <code>Boot-Class-Path</code> attribute |
|
243 specified below) can only link to types defined to the bootstrap class loader. |
|
244 There is no guarantee that all platform classes are visible to the boot class |
|
245 loader. |
|
246 |
|
247 |
|
248 <h3>Manifest Attributes</h3> |
|
249 |
|
250 The following manifest attributes are defined for an agent JAR file: |
|
251 <blockquote> |
|
252 <dl> |
|
253 <dt><code>Premain-Class</code></dt> |
|
254 <dd> |
|
255 When an agent is specified at JVM launch time this attribute |
|
256 specifies the agent class. |
|
257 That is, the class containing the <code>premain</code> method. |
|
258 When an agent is specified at JVM launch time this attribute |
|
259 is required. If the attribute is not present the JVM will abort. |
|
260 Note: this is a class name, not a file name or path. |
|
261 </dd> |
|
262 <dt><code>Agent-Class</code></dt> |
|
263 <dd> |
|
264 If an implementation supports a mechanism to start agents |
|
265 sometime after the VM has started then this attribute specifies |
|
266 the agent class. |
|
267 That is, the class containing the <code>agentmain</code> method. |
|
268 This attribute is required, if it is not present the agent |
|
269 will not be started. |
|
270 Note: this is a class name, not a file name or path. |
|
271 </dd> |
|
272 <dt><code>Launcher-Agent-Class</code></dt> |
|
273 <dd> |
|
274 If an implementation supports a mechanism to start an application |
|
275 as an executable JAR then the main manifest may include this |
|
276 attribute to specify the class name of an agent to start before the |
|
277 application <code>main</code> method is invoked. |
|
278 </dd> |
|
279 <dt><code>Boot-Class-Path</code></dt> |
|
280 <dd> |
|
281 A list of paths to be searched by the bootstrap class |
|
282 loader. Paths represent directories or libraries |
|
283 (commonly referred to as JAR or zip libraries on |
|
284 many platforms). |
|
285 These paths are searched by the |
|
286 bootstrap class loader after the platform specific |
|
287 mechanisms of locating a class have failed. |
|
288 Paths are searched in the order listed. |
|
289 Paths in the list are separated by one or more spaces. |
|
290 A path takes the syntax of the path component of a |
|
291 hierarchical URI. The path is |
|
292 absolute if it begins with a slash character ('/'), |
|
293 otherwise it is relative. A relative path is resolved |
|
294 against the absolute path of the agent JAR file. |
|
295 Malformed and non-existent paths are ignored. |
|
296 When an agent is started sometime after the VM has |
|
297 started then paths that do not represent a JAR file |
|
298 are ignored. |
|
299 This attribute is optional. |
|
300 </dd> |
|
301 <dt><code>Can-Redefine-Classes</code></dt> |
|
302 <dd> |
|
303 Boolean (<code>true</code> or <code>false</code>, case irrelevant). |
|
304 Is the ability to redefine classes |
|
305 needed by this agent. |
|
306 Values other than <code>true</code> are considered <code>false</code>. |
|
307 This attribute is optional, the default is <code>false</code>. |
|
308 </dd> |
|
309 <dt><code>Can-Retransform-Classes</code></dt> |
|
310 <dd> |
|
311 Boolean (<code>true</code> or <code>false</code>, case irrelevant). |
|
312 Is the ability to retransform classes |
|
313 needed by this agent. |
|
314 Values other than <code>true</code> are considered <code>false</code>. |
|
315 This attribute is optional, the default is <code>false</code>. |
|
316 </dd> |
|
317 <dt><code>Can-Set-Native-Method-Prefix</code></dt> |
|
318 <dd> |
|
319 Boolean (<code>true</code> or <code>false</code>, case irrelevant). |
|
320 Is the ability to set native method prefix needed by this agent. |
|
321 Values other than <code>true</code> are considered <code>false</code>. |
|
322 This attribute is optional, the default is <code>false</code>. |
|
323 </dd> |
|
324 </dl> |
|
325 </blockquote> |
|
326 |
|
327 <p> |
|
328 An agent JAR file may have both the <code>Premain-Class</code> and <code>Agent-Class</code> |
|
329 attributes present in the manifest. When the agent is started on the command-line using |
|
330 the <code>-javaagent</code> option then the <code>Premain-Class</code> attribute |
|
331 specifies the name of the agent class and the <code>Agent-Class</code> attribute is |
|
332 ignored. Similarly, if the agent is started sometime after the VM has started, then |
|
333 the <code>Agent-Class</code> attribute specifies the name of the agent class |
|
334 (the value of <code>Premain-Class</code> attribute is ignored). |
|
335 |
|
336 |
|
337 <h3>Instrumenting code in modules</h3> |
|
338 |
|
339 As an aid to agents that deploy supporting classes on the search path of the |
|
340 bootstrap class loader, or the search path of the class loader that loads |
|
341 the main agent class, the Java virtual machine arranges for the module of |
|
342 transformed classes to read the unnamed module of both class loaders. |
|
343 |
|
344 |
|
345 <h2>Related Documentation</h2> |
|
346 |
|
347 For tool documentation, please see: |
|
348 <ul> |
|
349 <li><a href="{@docRoot}/../technotes/tools/index.html">JDK Tools and Utilities</a> |
|
350 </ul> |
|
351 |
|
352 @since 1.5 |
|
353 @revised 1.6 |
|
354 |
|
355 </body> |
|
356 </html> |
|