source: trunk/openjdk/jdk/src/share/instrument/JPLISAgent.h

Last change on this file was 278, checked in by dmik, 14 years ago

trunk: Merged in openjdk6 b22 from branches/vendor/oracle.

File size: 10.6 KB
Line 
1/*
2 * Copyright (c) 2003, 2007, 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/*
27 * Copyright 2003 Wily Technology, Inc.
28 */
29
30#ifndef _JPLISAGENT_H_
31#define _JPLISAGENT_H_
32
33#include <jni.h>
34#include <jvmti.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/*
41 * The JPLISAgent manages the initialization all of the Java programming language Agents.
42 * It also supports the native method bridge between the JPLIS and the JVMTI.
43 * It maintains a single JVMTI Env that all JPL agents share.
44 * It parses command line requests and creates individual Java agents.
45 */
46
47
48/*
49 * Forward definitions
50 */
51struct _JPLISAgent;
52
53typedef struct _JPLISAgent JPLISAgent;
54typedef struct _JPLISEnvironment JPLISEnvironment;
55
56
57/* constants for class names and methods names and such
58 these all must stay in sync with Java code & interfaces
59*/
60#define JPLIS_INSTRUMENTIMPL_CLASSNAME "sun/instrument/InstrumentationImpl"
61#define JPLIS_INSTRUMENTIMPL_CONSTRUCTOR_METHODNAME "<init>"
62#define JPLIS_INSTRUMENTIMPL_CONSTRUCTOR_METHODSIGNATURE "(JZZ)V"
63#define JPLIS_INSTRUMENTIMPL_PREMAININVOKER_METHODNAME "loadClassAndCallPremain"
64#define JPLIS_INSTRUMENTIMPL_PREMAININVOKER_METHODSIGNATURE "(Ljava/lang/String;Ljava/lang/String;)V"
65#define JPLIS_INSTRUMENTIMPL_AGENTMAININVOKER_METHODNAME "loadClassAndCallAgentmain"
66#define JPLIS_INSTRUMENTIMPL_AGENTMAININVOKER_METHODSIGNATURE "(Ljava/lang/String;Ljava/lang/String;)V"
67#define JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODNAME "transform"
68#define JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODSIGNATURE \
69 "(Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Class;Ljava/security/ProtectionDomain;[BZ)[B"
70
71
72/*
73 * Error messages
74 */
75#define JPLIS_ERRORMESSAGE_CANNOTSTART "processing of -javaagent failed"
76
77
78/*
79 * Our initialization errors
80 */
81typedef enum {
82 JPLIS_INIT_ERROR_NONE,
83 JPLIS_INIT_ERROR_CANNOT_CREATE_NATIVE_AGENT,
84 JPLIS_INIT_ERROR_FAILURE,
85 JPLIS_INIT_ERROR_ALLOCATION_FAILURE,
86 JPLIS_INIT_ERROR_AGENT_CLASS_NOT_SPECIFIED
87} JPLISInitializationError;
88
89
90struct _JPLISEnvironment {
91 jvmtiEnv * mJVMTIEnv; /* the JVM TI environment */
92 JPLISAgent * mAgent; /* corresponding agent */
93 jboolean mIsRetransformer; /* indicates if special environment */
94};
95
96struct _JPLISAgent {
97 JavaVM * mJVM; /* handle to the JVM */
98 JPLISEnvironment mNormalEnvironment; /* for every thing but retransform stuff */
99 JPLISEnvironment mRetransformEnvironment;/* for retransform stuff only */
100 jobject mInstrumentationImpl; /* handle to the Instrumentation instance */
101 jmethodID mPremainCaller; /* method on the InstrumentationImpl that does the premain stuff (cached to save lots of lookups) */
102 jmethodID mAgentmainCaller; /* method on the InstrumentationImpl for agents loaded via attach mechanism */
103 jmethodID mTransform; /* method on the InstrumentationImpl that does the class file transform */
104 jboolean mRedefineAvailable; /* cached answer to "does this agent support redefine" */
105 jboolean mRedefineAdded; /* indicates if can_redefine_classes capability has been added */
106 jboolean mNativeMethodPrefixAvailable; /* cached answer to "does this agent support prefixing" */
107 jboolean mNativeMethodPrefixAdded; /* indicates if can_set_native_method_prefix capability has been added */
108 char const * mAgentClassName; /* agent class name */
109 char const * mOptionsString; /* -javaagent options string */
110};
111
112/*
113 * JVMTI event handlers
114 */
115
116/* VMInit event handler. Installed during OnLoad, then removed during VMInit. */
117extern void JNICALL
118eventHandlerVMInit( jvmtiEnv * jvmtienv,
119 JNIEnv * jnienv,
120 jthread thread);
121
122/* ClassFileLoadHook event handler. Installed during VMInit, then left in place forever. */
123extern void JNICALL
124eventHandlerClassFileLoadHook( jvmtiEnv * jvmtienv,
125 JNIEnv * jnienv,
126 jclass class_being_redefined,
127 jobject loader,
128 const char* name,
129 jobject protectionDomain,
130 jint class_data_len,
131 const unsigned char* class_data,
132 jint* new_class_data_len,
133 unsigned char** new_class_data);
134
135/*
136 * Main entry points for the JPLIS JVMTI agent code
137 */
138
139/* looks up the environment instance. returns null if there isn't one */
140extern JPLISEnvironment *
141getJPLISEnvironment(jvmtiEnv * jvmtienv);
142
143/* Creates a new JPLIS agent.
144 * Returns error if the agent cannot be created and initialized.
145 * The JPLISAgent* pointed to by agent_ptr is set to the new broker,
146 * or NULL if an error has occurred.
147 */
148extern JPLISInitializationError
149createNewJPLISAgent(JavaVM * vm, JPLISAgent **agent_ptr);
150
151/* Adds can_redefine_classes capability */
152extern void
153addRedefineClassesCapability(JPLISAgent * agent);
154
155/* Add the can_set_native_method_prefix capability */
156extern void
157addNativeMethodPrefixCapability(JPLISAgent * agent);
158
159/* Add the can_maintain_original_method_order capability (for testing) */
160extern void
161addOriginalMethodOrderCapability(JPLISAgent * agent);
162
163
164/* Our JPLIS agent is paralleled by a Java InstrumentationImpl instance.
165 * This routine uses JNI to create and initialized the Java instance.
166 * Returns true if it succeeds, false otherwise.
167 */
168extern jboolean
169createInstrumentationImpl( JNIEnv * jnienv,
170 JPLISAgent * agent);
171
172
173/* during OnLoad phase (command line parsing)
174 * record the parameters of -javaagent
175 */
176extern JPLISInitializationError
177recordCommandLineData( JPLISAgent * agent,
178 const char * agentClass,
179 const char * optionsString );
180
181/* Swaps the start phase event handlers out and the live phase event handlers in.
182 * Also used in attach to enabled live phase event handlers.
183 * Returns true if it succeeds, false otherwise.
184 */
185extern jboolean
186setLivePhaseEventHandlers( JPLISAgent * agent);
187
188/* Loads the Java agent according to the already processed command line. For each,
189 * loads the Java agent class, then calls the premain method.
190 * Returns true if all Java agent classes are loaded and all premain methods complete with no exceptions,
191 * false otherwise.
192 */
193extern jboolean
194startJavaAgent( JPLISAgent * agent,
195 JNIEnv * jnienv,
196 const char * classname,
197 const char * optionsString,
198 jmethodID agentMainMethod);
199
200
201/* during VMInit processing
202 * this is how the invocation engine (callback wrapper) tells us to start up all the javaagents
203 */
204extern jboolean
205processJavaStart( JPLISAgent * agent,
206 JNIEnv * jnienv);
207
208/* on an ongoing basis,
209 * this is how the invocation engine (callback wrapper) tells us to process a class file
210 */
211extern void
212transformClassFile( JPLISAgent * agent,
213 JNIEnv * jnienv,
214 jobject loader,
215 const char* name,
216 jclass classBeingRedefined,
217 jobject protectionDomain,
218 jint class_data_len,
219 const unsigned char* class_data,
220 jint* new_class_data_len,
221 unsigned char** new_class_data,
222 jboolean is_retransformer);
223
224/* on an ongoing basis,
225 * Return the environment with the retransformation capability.
226 * Create it if it doesn't exist.
227 */
228extern jvmtiEnv *
229retransformableEnvironment(JPLISAgent * agent);
230
231/* on an ongoing basis,
232 * these are implementations of the Instrumentation services.
233 * Most are simple covers for JVMTI access services. These are the guts of the InstrumentationImpl
234 * native methods.
235 */
236extern jboolean
237isModifiableClass(JNIEnv * jnienv, JPLISAgent * agent, jclass clazz);
238
239extern jboolean
240isRetransformClassesSupported(JNIEnv * jnienv, JPLISAgent * agent);
241
242extern void
243setHasRetransformableTransformers(JNIEnv * jnienv, JPLISAgent * agent, jboolean has);
244
245extern void
246retransformClasses(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray classes);
247
248extern void
249redefineClasses(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray classDefinitions);
250
251extern jobjectArray
252getAllLoadedClasses(JNIEnv * jnienv, JPLISAgent * agent);
253
254extern jobjectArray
255getInitiatedClasses(JNIEnv * jnienv, JPLISAgent * agent, jobject classLoader);
256
257extern jlong
258getObjectSize(JNIEnv * jnienv, JPLISAgent * agent, jobject objectToSize);
259
260extern void
261appendToClassLoaderSearch(JNIEnv * jnienv, JPLISAgent * agent, jstring jarFile, jboolean isBootLoader);
262
263extern void
264setNativeMethodPrefixes(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray prefixArray,
265 jboolean isRetransformable);
266
267#define jvmti(a) a->mNormalEnvironment.mJVMTIEnv
268
269#ifdef __cplusplus
270} /* extern "C" */
271#endif /* __cplusplus */
272
273
274#endif
Note: See TracBrowser for help on using the repository browser.