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 ClassLoad 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 classLoadHandler( 48 jvmtiEnv *jvmti, 49 JNIEnv* jni, 50 jthread thread, 51 jclass klass) { 52 char className[MAX_STRING_LENGTH]; 53 int success = 1; 54 55 if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) { 56 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni); 57 return; 58 } 59 60 if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) { 61 62 eventsCounter++; 63 64 NSK_DISPLAY3("%s: ClassLoad event received for class '%s (eventsCounter: %d)'\n", agentName, className, eventsCounter); 65 66 if (eventsCounter == EXPECTED_EVENTS_NUMBER) { 67 NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter); 68 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, success, jvmti, jni); 69 } 70 71 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) { 72 success = 0; 73 } 74 } else { 75 success = 0; 76 } 77 78 if (!success) { 79 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni); 80 } 81 } 82 83 #ifdef STATIC_BUILD 84 JNIEXPORT jint JNI_OnLoad_attach045Agent00(JavaVM *jvm, char *options, void *reserved) { 85 return JNI_VERSION_1_8; 86 } 87 #endif 88 89 JNIEXPORT jint JNICALL 90 #ifdef STATIC_BUILD 91 Agent_OnAttach_attach045Agent00(JavaVM *vm, char *optionsString, void *reserved) 92 #else 93 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 94 #endif 95 { 96 jvmtiEventCallbacks eventCallbacks; 97 jvmtiEnv* jvmti; 98 JNIEnv* jni; 99 100 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) 101 return JNI_ERR; 102 103 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 104 105 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) 106 return JNI_ERR; 107 108 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 109 return JNI_ERR; 110 111 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent00-eventsCounterMonitor", &eventsCounterMonitor))) { 112 return JNI_ERR; 113 } 114 115 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 116 eventCallbacks.ClassLoad = classLoadHandler; 117 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) { 118 return JNI_ERR; 119 } 120 121 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) { 122 return JNI_ERR; 123 } 124 125 NSK_DISPLAY1("%s: initialization was done\n", agentName); 126 127 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 128 return JNI_ERR; 129 130 return JNI_OK; 131 } 132 133 134 }