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 * Expected agent work scenario: 35 * - receive ClassLoad event for class FirstLoadedClass, from handler for this event 36 * disable ClassLoad events for all threads except thread loading FirstLoadedClass 37 * (after these ClassLoad events should come only from this thread) 38 * - receive ClassLoad event for class LastLoadedClass and finish work 39 */ 40 41 static Options* options = NULL; 42 static const char* agentName; 43 44 #define FIRST_LOADED_CLASS "Lnsk/jvmti/AttachOnDemand/attach009/FirstLoadedClass;" 45 #define LAST_LOADED_CLASS "Lnsk/jvmti/AttachOnDemand/attach009/LastLoadedClass;" 46 47 static int disabledForOthers = 0; 48 49 static volatile int success = 1; 50 51 void JNICALL 52 classLoadHandler(jvmtiEnv *jvmti, 53 JNIEnv* jni, 54 jthread thread, 55 jclass klass) { 56 static char mainThreadName[MAX_STRING_LENGTH]; 57 char loadedClassName[MAX_STRING_LENGTH]; 58 char threadName[MAX_STRING_LENGTH]; 59 60 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 61 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni); 62 return; 63 } 64 65 if (!nsk_jvmti_aod_getClassName(jvmti, klass, loadedClassName)) { 66 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni); 67 return; 68 } 69 70 NSK_DISPLAY2("Class '%s' was loaded by thread '%s'\n", loadedClassName, threadName); 71 72 /* 73 * When class FIRST_LOADED_CLASS was loaded try to disable events for all threads 74 * except main target application thread 75 */ 76 if (strcmp(loadedClassName, FIRST_LOADED_CLASS) == 0) { 77 strcpy(mainThreadName, threadName); 78 79 if (!nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD)) { 80 NSK_COMPLAIN0("Failed to disable events\n"); 81 nsk_aod_agentFinished(jni, agentName, 0); 82 return; 83 } 84 85 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, thread))) { 86 NSK_COMPLAIN1("Failed to enable events for thread '%s'\n", mainThreadName); 87 nsk_aod_agentFinished(jni, agentName, 0); 88 return; 89 } 90 91 NSK_DISPLAY1("ClassLoad events are enabled only for thread '%s'", mainThreadName); 92 93 disabledForOthers = 1; 94 95 return; 96 } 97 98 if (disabledForOthers) { 99 if (strcmp(threadName, mainThreadName) != 0) { 100 success = 0; 101 NSK_COMPLAIN1("ClassLoad event was erroneously generated for thread '%s'\n", threadName); 102 } 103 } 104 105 /* 106 * Stop agent when LAST_LOADED_CLASS was loaded 107 */ 108 if (strcmp(loadedClassName, LAST_LOADED_CLASS) == 0) { 109 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, success, jvmti, jni); 110 } 111 } 112 113 114 #ifdef STATIC_BUILD 115 JNIEXPORT jint JNI_OnLoad_attach009Agent00(JavaVM *jvm, char *options, void *reserved) { 116 return JNI_VERSION_1_8; 117 } 118 #endif 119 120 JNIEXPORT jint JNICALL 121 #ifdef STATIC_BUILD 122 Agent_OnAttach_attach009Agent00(JavaVM *vm, char *optionsString, void *reserved) 123 #else 124 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 125 #endif 126 { 127 jvmtiEnv* jvmti; 128 jvmtiEventCallbacks eventCallbacks; 129 JNIEnv* jni; 130 131 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) 132 return JNI_ERR; 133 134 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 135 136 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) 137 return NSK_FALSE; 138 139 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 140 return JNI_ERR; 141 142 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 143 eventCallbacks.ClassLoad = classLoadHandler; 144 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) { 145 return JNI_ERR; 146 } 147 148 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) { 149 return JNI_ERR; 150 } 151 152 NSK_DISPLAY1("%s: initialization was done\n", agentName); 153 154 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 155 return JNI_ERR; 156 157 return JNI_OK; 158 } 159 160 }