1 /* 2 * Copyright (c) 2007, 2018, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <jni.h> 27 #include <jvmti.h> 28 #include <aod.h> 29 #include <jvmti_aod.h> 30 31 extern "C" { 32 33 /* 34 * In this test the same agent library is attached 3 times, but with 35 * different options. In such scenario functions nsk_jvmti_aod_addMultiagentsOptions and 36 * nsk_jvmti_aod_getMultiagentsOptions are used. 37 * 38 * Each agent from ClassLoad event handler tries to redefine class ClassToRedefine and 39 * finishes work. 40 */ 41 42 #define REDEFINED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach046/ClassToRedefine;" 43 #define REDEFINED_CLASS_FILE_NAME "nsk/jvmti/AttachOnDemand/attach046/ClassToRedefine" 44 45 void JNICALL classLoadHandler( 46 jvmtiEnv *jvmti, 47 JNIEnv* jni, 48 jthread thread, 49 jclass klass) { 50 char className[MAX_STRING_LENGTH]; 51 const char* agentName; 52 Options* options; 53 54 options = nsk_jvmti_aod_getMultiagentsOptions(jvmti); 55 if (!NSK_VERIFY(options != NULL)) { 56 NSK_COMPLAIN0("Failed to get agents's options\n"); 57 nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD); 58 // can't call nsk_aod_agentFinished because of without options can't get agent's name 59 return; 60 } 61 62 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 63 64 if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) { 65 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni); 66 return; 67 } 68 69 NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className); 70 71 if (!strcmp(className, REDEFINED_CLASS_NAME)) { 72 73 NSK_DISPLAY1("%s: redefining class\n", agentName); 74 75 if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, klass, REDEFINED_CLASS_FILE_NAME))) { 76 NSK_COMPLAIN1("%s: failed to redefine class\n", agentName); 77 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni); 78 } else { 79 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 1, jvmti, jni); 80 } 81 } 82 } 83 84 #ifdef STATIC_BUILD 85 JNIEXPORT jint JNI_OnLoad_attach046Agent00(JavaVM *jvm, char *options, void *reserved) { 86 return JNI_VERSION_1_8; 87 } 88 #endif 89 90 JNIEXPORT jint JNICALL 91 #ifdef STATIC_BUILD 92 Agent_OnAttach_attach046Agent00(JavaVM *vm, char *optionsString, void *reserved) 93 #else 94 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 95 #endif 96 { 97 jvmtiEventCallbacks eventCallbacks; 98 jvmtiCapabilities caps; 99 jvmtiEnv* jvmti = NULL; 100 JNIEnv* jni = NULL; 101 Options* options; 102 const char* agentName; 103 104 options = (Options*) nsk_aod_createOptions(optionsString); 105 if (!NSK_VERIFY(options != NULL)) 106 return JNI_ERR; 107 108 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 109 110 jni = (JNIEnv*) nsk_aod_createJNIEnv(vm); 111 if (jni == NULL) 112 return NSK_FALSE; 113 114 jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved); 115 if (!NSK_VERIFY(jvmti != NULL)) 116 return JNI_ERR; 117 118 memset(&caps, 0, sizeof(caps)); 119 caps.can_redefine_classes = 1; 120 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) { 121 return JNI_ERR; 122 } 123 124 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 125 eventCallbacks.ClassLoad = classLoadHandler; 126 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) { 127 return JNI_ERR; 128 } 129 130 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) { 131 return JNI_ERR; 132 } 133 134 if (!NSK_VERIFY(nsk_jvmti_aod_addMultiagentsOptions(jvmti, options))) { 135 return JNI_ERR; 136 } 137 138 NSK_DISPLAY1("%s: initialization was done\n", agentName); 139 140 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 141 return JNI_ERR; 142 143 return JNI_OK; 144 } 145 146 }