author | sspitsyn |
Mon, 12 Sep 2016 15:00:58 -0700 | |
changeset 40818 | 9c2079cba606 |
parent 36511 | 9d0388c6b336 |
child 42338 | a60f280f803c |
permissions | -rw-r--r-- |
1935
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
1 |
<!-- |
36511 | 2 |
Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. |
1935
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
3 |
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
4 |
|
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
5 |
This code is free software; you can redistribute it and/or modify it |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
6 |
under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
published by the Free Software Foundation. Oracle designates this |
1935
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
8 |
particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
by Oracle in the LICENSE file that accompanied this code. |
1935
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
10 |
|
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
11 |
This code is distributed in the hope that it will be useful, but WITHOUT |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
12 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
14 |
version 2 for more details (a copy is included in the LICENSE file that |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
15 |
accompanied this code). |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
16 |
|
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
17 |
You should have received a copy of the GNU General Public License version |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
18 |
2 along with this work; if not, write to the Free Software Foundation, |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
19 |
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
20 |
|
5506 | 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. |
|
1935
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
24 |
--> |
4010e03f8c1c
6719182: update legal notice in java/lang/instrument/package.html
darcy
parents:
2
diff
changeset
|
25 |
|
2 | 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> |
|
15306
4d1877e73daa
8006565: java.lang.instrument specification should make it clear that -javaagent is optional
alanb
parents:
5506
diff
changeset
|
56 |
An implementation is not required to provide a way to start agents from the |
4d1877e73daa
8006565: java.lang.instrument specification should make it clear that -javaagent is optional
alanb
parents:
5506
diff
changeset
|
57 |
command-line interface. On implementations that do provide a way to start agents |
4d1877e73daa
8006565: java.lang.instrument specification should make it clear that -javaagent is optional
alanb
parents:
5506
diff
changeset
|
58 |
from the command-line interface, an agent is started by adding this option to |
4d1877e73daa
8006565: java.lang.instrument specification should make it clear that -javaagent is optional
alanb
parents:
5506
diff
changeset
|
59 |
the command-line: |
2 | 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. |
|
40818
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
108 |
The system class loader must support a mechanism to add an agent JAR file to the system class path. |
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
109 |
If it is a custom system class loader then it must define the |
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
110 |
<code>appendToClassPathForInstrumentation</code> method as specified in |
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
111 |
{@link Instrumentation#appendToSystemClassLoaderSearch appendToSystemClassLoaderSearch}. |
2 | 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 <code>premain</code>. |
|
116 |
||
117 |
<P> |
|
118 |
Each agent is passed its agent options via the <code>agentArgs</code> parameter. |
|
119 |
The agent options are passed as a single string, |
|
120 |
any additional parsing should be performed by the agent itself. |
|
121 |
||
122 |
<P> |
|
123 |
If the agent cannot be resolved |
|
124 |
(for example, because the agent class cannot be loaded, |
|
125 |
or because the agent class does not have an appropriate <code>premain</code> method), the JVM will abort. |
|
126 |
If a <code>premain</code> method throws an uncaught exception, the JVM will abort. |
|
127 |
||
128 |
||
129 |
||
130 |
<h3>Starting Agents After VM Startup</h3> |
|
131 |
||
132 |
<p> |
|
133 |
An implementation may provide a mechanism to start agents sometime after the |
|
134 |
the VM has started. The details as to how this is initiated are implementation |
|
135 |
specific but typically the application has already started and its <code> |
|
136 |
main</code> method has already been invoked. In cases where an implementation |
|
137 |
supports the starting of agents after the VM has started the following applies: |
|
138 |
||
139 |
<ol> |
|
140 |
<li><p>The manifest of the agent JAR must contain the attribute <code>Agent-Class</code>. |
|
141 |
The value of this attribute is the name of the <i>agent class</i>. </p></li> |
|
142 |
||
143 |
<li><p>The agent class must implement a public static <code>agentmain</code> method. </p></li> |
|
144 |
||
145 |
<li><p>The system class loader ( |
|
146 |
{@link java.lang.ClassLoader#getSystemClassLoader ClassLoader.getSystemClassLoader}) must |
|
40818
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
147 |
support a mechanism to add an agent JAR file to the system class path. |
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
148 |
If it is a custom system class loader then it must define the |
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
149 |
<code>appendToClassPathForInstrumentation</code> method as specified in |
9c2079cba606
8160950: Agent JAR added to app class loader rather than system class loader when running with -Djava.system.class.loader
sspitsyn
parents:
36511
diff
changeset
|
150 |
{@link Instrumentation#appendToSystemClassLoaderSearch appendToSystemClassLoaderSearch}.</li> |
2 | 151 |
</ol> |
152 |
||
153 |
<P> |
|
154 |
The agent JAR is appended to the system class path. This is the class loader that typically loads |
|
155 |
the class containing the application <code>main</code> method. The agent class is loaded and the |
|
156 |
JVM attempts to invoke the <code>agentmain</code> method. The JVM first attempts to invoke |
|
157 |
the following method on the agent class: |
|
158 |
||
159 |
<blockquote> |
|
160 |
<code>public static void |
|
161 |
agentmain(String agentArgs, Instrumentation inst); |
|
162 |
</code> |
|
163 |
</blockquote> |
|
164 |
||
165 |
<P> |
|
166 |
If the agent class does not implement this method then the JVM will attempt to invoke: |
|
167 |
||
168 |
<blockquote> |
|
169 |
<code>public static void |
|
170 |
agentmain(String agentArgs); |
|
171 |
</code> |
|
172 |
</blockquote> |
|
173 |
||
174 |
<P> |
|
175 |
The agent class may also have an <code>premain</code> method for use when the agent is started |
|
176 |
using a command-line option. When the agent is started after VM startup the <code>premain</code> |
|
177 |
method is not invoked. |
|
178 |
||
179 |
||
180 |
<P> |
|
181 |
The agent is passed its agent options via the <code>agentArgs</code> parameter. |
|
182 |
The agent options are passed as a single string, |
|
183 |
any additional parsing should be performed by the agent itself. |
|
184 |
||
185 |
<P> |
|
186 |
The <code>agentmain</code> method should do any necessary initialization |
|
187 |
required to start the agent. When startup is complete the method should |
|
188 |
return. If the agent cannot be started |
|
189 |
(for example, because the agent class cannot be loaded, |
|
190 |
or because the agent class does not have a conformant <code>agentmain</code> method), the JVM will |
|
191 |
not abort. If the <code>agentmain</code> method throws an uncaught exception it will be ignored. |
|
192 |
||
193 |
||
194 |
||
195 |
<h3>Manifest Attributes</h3> |
|
196 |
The following manifest attributes are defined for an agent JAR file: |
|
197 |
<blockquote> |
|
198 |
<dl> |
|
199 |
<dt><code>Premain-Class</code></dt> |
|
200 |
<dd> |
|
201 |
When an agent is specified at JVM launch time this attribute |
|
202 |
specifies the agent class. |
|
203 |
That is, the class containing the <code>premain</code> method. |
|
204 |
When an agent is specified at JVM launch time this attribute |
|
205 |
is required. If the attribute is not present the JVM will abort. |
|
206 |
Note: this is a class name, not a file name or path. |
|
207 |
</dd> |
|
208 |
||
209 |
<dt><code>Agent-Class</code></dt> |
|
210 |
<dd> |
|
211 |
If an implementation supports a mechanism to start agents |
|
212 |
sometime after the VM has started then this attribute specifies |
|
213 |
the agent class. |
|
214 |
That is, the class containing the <code>agentmain</code> method. |
|
215 |
This attribute is required, if it is not present the agent |
|
216 |
will not be started. |
|
217 |
Note: this is a class name, not a file name or path. |
|
218 |
</dd> |
|
219 |
||
220 |
<dt><code>Boot-Class-Path</code></dt> |
|
221 |
<dd> |
|
222 |
A list of paths to be searched by the bootstrap class |
|
223 |
loader. Paths represent directories or libraries |
|
224 |
(commonly referred to as JAR or zip libraries on |
|
225 |
many platforms). |
|
226 |
These paths are searched by the |
|
227 |
bootstrap class loader after the platform specific |
|
228 |
mechanisms of locating a class have failed. |
|
229 |
Paths are searched in the order listed. |
|
230 |
Paths in the list are separated by one or more spaces. |
|
231 |
A path takes the syntax of the path component of a |
|
232 |
hierarchical URI. The path is |
|
233 |
absolute if it begins with a slash character ('/'), |
|
234 |
otherwise it is relative. A relative path is resolved |
|
235 |
against the absolute path of the agent JAR file. |
|
236 |
Malformed and non-existent paths are ignored. |
|
237 |
When an agent is started sometime after the VM has |
|
238 |
started then paths that do not represent a JAR file |
|
239 |
are ignored. |
|
240 |
This attribute is optional. |
|
241 |
</dd> |
|
242 |
<dt><code>Can-Redefine-Classes</code></dt> |
|
243 |
<dd> |
|
244 |
Boolean (<code>true</code> or <code>false</code>, case irrelevant). |
|
245 |
Is the ability to redefine classes |
|
246 |
needed by this agent. |
|
247 |
Values other than <code>true</code> are considered <code>false</code>. |
|
248 |
This attribute is optional, the default is <code>false</code>. |
|
249 |
</dd> |
|
250 |
<dt><code>Can-Retransform-Classes</code></dt> |
|
251 |
<dd> |
|
252 |
Boolean (<code>true</code> or <code>false</code>, case irrelevant). |
|
253 |
Is the ability to retransform classes |
|
254 |
needed by this agent. |
|
255 |
Values other than <code>true</code> are considered <code>false</code>. |
|
256 |
This attribute is optional, the default is <code>false</code>. |
|
257 |
</dd> |
|
258 |
<dt><code>Can-Set-Native-Method-Prefix</code></dt> |
|
259 |
<dd> |
|
260 |
Boolean (<code>true</code> or <code>false</code>, case irrelevant). |
|
261 |
Is the ability to set native method prefix needed by this agent. |
|
262 |
Values other than <code>true</code> are considered <code>false</code>. |
|
263 |
This attribute is optional, the default is <code>false</code>. |
|
264 |
</dd> |
|
265 |
</dl> |
|
266 |
</blockquote> |
|
267 |
||
268 |
<p> |
|
269 |
An agent JAR file may have both the <code>Premain-Class</code> and <code>Agent-Class</code> |
|
270 |
attributes present in the manifest. When the agent is started on the command-line using |
|
271 |
the <code>-javaagent</code> option then the <code>Premain-Class</code> attribute |
|
272 |
specifies the name of the agent class and the <code>Agent-Class</code> attribute is |
|
273 |
ignored. Similarly, if the agent is started sometime after the VM has started, then |
|
274 |
the <code>Agent-Class</code> attribute specifies the name of the agent class |
|
275 |
(the value of <code>Premain-Class</code> attribute is ignored). |
|
276 |
||
36511 | 277 |
|
278 |
<h3>Instrumenting code in modules</h3> |
|
279 |
||
280 |
Agents that instrument code in named modules may need to arrange for the |
|
281 |
modules to read other modules. If code is instrumented to invoke a method |
|
282 |
in a support class in another module, then the module of the instrumented |
|
283 |
code should read the module of the supporting class. Furthermore, the |
|
284 |
supporting class will only be accessible to the instrumented code if |
|
285 |
it is <code>public</code> and in a package that is exported by its module. |
|
286 |
Agents can use {@link Instrumentation#addModuleReads addModuleReads} to update |
|
287 |
a module to read another. |
|
288 |
<p> |
|
289 |
As an aid to agents that deploy supporting classes on the search path of the |
|
290 |
bootstrap class loader, or the search path of the class loader that loads |
|
291 |
the main agent class, the Java virtual machine arranges for the module of |
|
292 |
transformed classes to read the unnamed module of both class loaders. |
|
293 |
||
294 |
||
2 | 295 |
<h2>Related Documentation</h2> |
296 |
||
297 |
For tool documentation, please see: |
|
298 |
<ul> |
|
299 |
<li><a href="{@docRoot}/../technotes/tools/index.html">JDK Tools and Utilities</a> |
|
300 |
</ul> |
|
301 |
||
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24367
diff
changeset
|
302 |
@since 1.5 |
2 | 303 |
@revised 1.6 |
304 |
||
305 |
</body> |
|
306 |
</html> |