1 /*
2 * Copyright (c) 2003, 2008, 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
124
125 /* for retransformClasses testing, set capability to use original method order */
126 if (getBooleanAttribute(attributes, "Can-Maintain-Original-Method-Order")) {
127 addOriginalMethodOrderCapability(agent);
128 }
129 }
130
131 /*
132 * This will be called once for every -javaagent on the command line.
133 * Each call to Agent_OnLoad will create its own agent and agent data.
134 *
135 * The argument tail string provided to Agent_OnLoad will be of form
136 * <jarfile>[=<options>]. The tail string is split into the jarfile and
137 * options components. The jarfile manifest is parsed and the value of the
138 * Premain-Class attribute will become the agent's premain class. The jar
139 * file is then added to the system class path, and if the Boot-Class-Path
140 * attribute is present then all relative URLs in the value are processed
141 * to create boot class path segments to append to the boot class path.
142 */
143 JNIEXPORT jint JNICALL
144 Agent_OnLoad(JavaVM *vm, char *tail, void * reserved) {
145 JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE;
146 jint result = JNI_OK;
147 JPLISAgent * agent = NULL;
148
149 initerror = createNewJPLISAgent(vm, &agent);
150 if ( initerror == JPLIS_INIT_ERROR_NONE ) {
151 int oldLen, newLen;
152 char * jarfile;
153 char * options;
154 jarAttribute* attributes;
155 char * premainClass;
156 char * bootClassPath;
157
158 /*
159 * Parse <jarfile>[=options] into jarfile and options
160 */
161 if (parseArgumentTail(tail, &jarfile, &options) != 0) {
162 fprintf(stderr, "-javaagent: memory allocation failure.\n");
163 return JNI_ERR;
164 }
273 break;
274 }
275 return result;
276 }
277
278 /*
279 * Agent_OnAttach returns a jint. 0/JNI_OK indicates success and non-0
280 * indicates an error. To allow the attach mechanism throw an
281 * AgentInitializationException with a reasonable exception message we define
282 * a few specific errors here.
283 */
284 #define AGENT_ERROR_BADJAR ((jint)100) /* Agent JAR not found or no Agent-Class attribute */
285 #define AGENT_ERROR_NOTONCP ((jint)101) /* Unable to add JAR file to system class path */
286 #define AGENT_ERROR_STARTFAIL ((jint)102) /* No agentmain method or agentmain failed */
287
288 /*
289 * This will be called once each time a tool attaches to the VM and loads
290 * the JPLIS library.
291 */
292 JNIEXPORT jint JNICALL
293 Agent_OnAttach(JavaVM* vm, char *args, void * reserved) {
294 JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE;
295 jint result = JNI_OK;
296 JPLISAgent * agent = NULL;
297 JNIEnv * jni_env = NULL;
298
299 /*
300 * Need JNIEnv - guaranteed to be called from thread that is already
301 * attached to VM
302 */
303 result = (*vm)->GetEnv(vm, (void**)&jni_env, JNI_VERSION_1_2);
304 jplis_assert(result==JNI_OK);
305
306 initerror = createNewJPLISAgent(vm, &agent);
307 if ( initerror == JPLIS_INIT_ERROR_NONE ) {
308 int oldLen, newLen;
309 char * jarfile;
310 char * options;
311 jarAttribute* attributes;
312 char * agentClass;
313 char * bootClassPath;
418
419 if (!success) {
420 fprintf(stderr, "Agent failed to start!\n");
421 result = AGENT_ERROR_STARTFAIL;
422 }
423
424 /*
425 * Clean-up
426 */
427 free(jarfile);
428 if (options != NULL) free(options);
429 free(agentClass);
430 freeAttributes(attributes);
431 }
432
433 return result;
434 }
435
436
437 JNIEXPORT void JNICALL
438 Agent_OnUnload(JavaVM *vm) {
439 }
440
441
442 /*
443 * JVMTI callback support
444 *
445 * We have two "stages" of callback support.
446 * At OnLoad time, we install a VMInit handler.
447 * When the VMInit handler runs, we remove the VMInit handler and install a
448 * ClassFileLoadHook handler.
449 */
450
451 void JNICALL
452 eventHandlerVMInit( jvmtiEnv * jvmtienv,
453 JNIEnv * jnienv,
454 jthread thread) {
455 JPLISEnvironment * environment = NULL;
456 jboolean success = JNI_FALSE;
457
458 environment = getJPLISEnvironment(jvmtienv);
|
1 /*
2 * Copyright (c) 2003, 2015, 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
124
125 /* for retransformClasses testing, set capability to use original method order */
126 if (getBooleanAttribute(attributes, "Can-Maintain-Original-Method-Order")) {
127 addOriginalMethodOrderCapability(agent);
128 }
129 }
130
131 /*
132 * This will be called once for every -javaagent on the command line.
133 * Each call to Agent_OnLoad will create its own agent and agent data.
134 *
135 * The argument tail string provided to Agent_OnLoad will be of form
136 * <jarfile>[=<options>]. The tail string is split into the jarfile and
137 * options components. The jarfile manifest is parsed and the value of the
138 * Premain-Class attribute will become the agent's premain class. The jar
139 * file is then added to the system class path, and if the Boot-Class-Path
140 * attribute is present then all relative URLs in the value are processed
141 * to create boot class path segments to append to the boot class path.
142 */
143 JNIEXPORT jint JNICALL
144 DEF_Agent_OnLoad(JavaVM *vm, char *tail, void * reserved) {
145 JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE;
146 jint result = JNI_OK;
147 JPLISAgent * agent = NULL;
148
149 initerror = createNewJPLISAgent(vm, &agent);
150 if ( initerror == JPLIS_INIT_ERROR_NONE ) {
151 int oldLen, newLen;
152 char * jarfile;
153 char * options;
154 jarAttribute* attributes;
155 char * premainClass;
156 char * bootClassPath;
157
158 /*
159 * Parse <jarfile>[=options] into jarfile and options
160 */
161 if (parseArgumentTail(tail, &jarfile, &options) != 0) {
162 fprintf(stderr, "-javaagent: memory allocation failure.\n");
163 return JNI_ERR;
164 }
273 break;
274 }
275 return result;
276 }
277
278 /*
279 * Agent_OnAttach returns a jint. 0/JNI_OK indicates success and non-0
280 * indicates an error. To allow the attach mechanism throw an
281 * AgentInitializationException with a reasonable exception message we define
282 * a few specific errors here.
283 */
284 #define AGENT_ERROR_BADJAR ((jint)100) /* Agent JAR not found or no Agent-Class attribute */
285 #define AGENT_ERROR_NOTONCP ((jint)101) /* Unable to add JAR file to system class path */
286 #define AGENT_ERROR_STARTFAIL ((jint)102) /* No agentmain method or agentmain failed */
287
288 /*
289 * This will be called once each time a tool attaches to the VM and loads
290 * the JPLIS library.
291 */
292 JNIEXPORT jint JNICALL
293 DEF_Agent_OnAttach(JavaVM* vm, char *args, void * reserved) {
294 JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE;
295 jint result = JNI_OK;
296 JPLISAgent * agent = NULL;
297 JNIEnv * jni_env = NULL;
298
299 /*
300 * Need JNIEnv - guaranteed to be called from thread that is already
301 * attached to VM
302 */
303 result = (*vm)->GetEnv(vm, (void**)&jni_env, JNI_VERSION_1_2);
304 jplis_assert(result==JNI_OK);
305
306 initerror = createNewJPLISAgent(vm, &agent);
307 if ( initerror == JPLIS_INIT_ERROR_NONE ) {
308 int oldLen, newLen;
309 char * jarfile;
310 char * options;
311 jarAttribute* attributes;
312 char * agentClass;
313 char * bootClassPath;
418
419 if (!success) {
420 fprintf(stderr, "Agent failed to start!\n");
421 result = AGENT_ERROR_STARTFAIL;
422 }
423
424 /*
425 * Clean-up
426 */
427 free(jarfile);
428 if (options != NULL) free(options);
429 free(agentClass);
430 freeAttributes(attributes);
431 }
432
433 return result;
434 }
435
436
437 JNIEXPORT void JNICALL
438 DEF_Agent_OnUnload(JavaVM *vm) {
439 }
440
441
442 /*
443 * JVMTI callback support
444 *
445 * We have two "stages" of callback support.
446 * At OnLoad time, we install a VMInit handler.
447 * When the VMInit handler runs, we remove the VMInit handler and install a
448 * ClassFileLoadHook handler.
449 */
450
451 void JNICALL
452 eventHandlerVMInit( jvmtiEnv * jvmtienv,
453 JNIEnv * jnienv,
454 jthread thread) {
455 JPLISEnvironment * environment = NULL;
456 jboolean success = JNI_FALSE;
457
458 environment = getJPLISEnvironment(jvmtienv);
|