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 * Agent receives expected number of ClassPrepare events and finishes work 35 * (events should be provoked by target application) 36 */ 37 38 #define EXPECTED_EVENTS_NUMBER 500 39 40 static Options* options = NULL; 41 static const char* agentName; 42 43 static jrawMonitorID eventsCounterMonitor; 44 45 static volatile int eventsCounter = 0; 46 47 void JNICALL classPrepareHandler( 48 jvmtiEnv *jvmti, 49 JNIEnv* jni, 50 jthread thread, 51 jclass klass) { 52 int success = 1; 53 char className[MAX_STRING_LENGTH]; 54 jint loadedClassesCount; 55 jclass *loadedClasses; 56 57 if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) { 58 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni); 59 return; 60 } 61 62 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetLoadedClasses, jvmti, &loadedClassesCount, &loadedClasses))) { 63 NSK_COMPLAIN1("%s: failed to get loaded classes\n", agentName); 64 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni); 65 return; 66 } 67 68 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)loadedClasses); 69 70 if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2( 71 RawMonitorEnter, jvmti, eventsCounterMonitor))) { 72 73 eventsCounter++; 74 75 NSK_DISPLAY3("%s: ClassPrepare event received for class '%s' (eventsCounter: %d)\n", agentName, className, eventsCounter); 76 77 if (eventsCounter == EXPECTED_EVENTS_NUMBER) { 78 NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter); 79 80 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, success, jvmti, jni); 81 } 82 83 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, eventsCounterMonitor))) { 84 success = 0; 85 } 86 } else { 87 success = 0; 88 } 89 90 if (!success) { 91 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni); 92 } 93 } 94 95 #ifdef STATIC_BUILD 96 JNIEXPORT jint JNI_OnLoad_attach045Agent01(JavaVM *jvm, char *options, void *reserved) { 97 return JNI_VERSION_1_8; 98 } 99 #endif 100 101 JNIEXPORT jint JNICALL 102 #ifdef STATIC_BUILD 103 Agent_OnAttach_attach045Agent01(JavaVM *vm, char *optionsString, void *reserved) 104 #else 105 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 106 #endif 107 { 108 jvmtiEventCallbacks eventCallbacks; 109 jvmtiEnv* jvmti; 110 JNIEnv* jni; 111 112 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) 113 return JNI_ERR; 114 115 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 116 117 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) 118 return JNI_ERR; 119 120 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 121 return JNI_ERR; 122 123 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "attach045-agent01-eventsCounterMonitor", &eventsCounterMonitor))) { 124 return JNI_ERR; 125 } 126 127 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 128 eventCallbacks.ClassPrepare = classPrepareHandler; 129 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) { 130 return JNI_ERR; 131 } 132 133 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_PREPARE))) { 134 return JNI_ERR; 135 } 136 137 NSK_DISPLAY1("%s: initialization was done\n", agentName); 138 139 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 140 return JNI_ERR; 141 142 return JNI_OK; 143 } 144 145 146 }