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 ThreadStart event for thread ThreadGeneratingEvents 36 * - receive ThreadEnd event for thread ThreadGeneratingEvents and finish work 37 */ 38 39 #define THREAD_GENERATING_EVENTS_NAME "ThreadGeneratingEvents" 40 41 static Options* options = NULL; 42 static const char* agentName; 43 44 static jvmtiEvent testEvents[] = {JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END}; 45 static const int testEventsNumber = 2; 46 47 static volatile int threadStartReceived = 0; 48 49 void JNICALL threadStartHandler(jvmtiEnv *jvmti, 50 JNIEnv* jni, 51 jthread thread) { 52 char threadName[MAX_STRING_LENGTH]; 53 54 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 55 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 56 return; 57 } 58 59 NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, threadName); 60 61 if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) { 62 threadStartReceived = 1; 63 } 64 } 65 66 67 void JNICALL threadEndHandler(jvmtiEnv *jvmti, 68 JNIEnv* jni, 69 jthread thread) { 70 char threadName[MAX_STRING_LENGTH]; 71 72 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 73 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 74 return; 75 } 76 77 NSK_DISPLAY2("%s: ThreadEnd event was received for thread '%s'\n", agentName, threadName); 78 79 if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) { 80 int success = 1; 81 82 if (!threadStartReceived) { 83 NSK_COMPLAIN1("%s: ThreadStart event wasn't received\n", agentName); 84 success = 0; 85 } 86 87 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni); 88 } 89 } 90 91 #ifdef STATIC_BUILD 92 JNIEXPORT jint JNI_OnLoad_attach038Agent00(JavaVM *jvm, char *options, void *reserved) { 93 return JNI_VERSION_1_8; 94 } 95 #endif 96 97 JNIEXPORT jint JNICALL 98 #ifdef STATIC_BUILD 99 Agent_OnAttach_attach038Agent00(JavaVM *vm, char *optionsString, void *reserved) 100 #else 101 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 102 #endif 103 { 104 jvmtiEventCallbacks eventCallbacks; 105 jvmtiEnv* jvmti; 106 JNIEnv* jni; 107 108 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) 109 return JNI_ERR; 110 111 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 112 113 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) 114 return JNI_ERR; 115 116 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 117 return JNI_ERR; 118 119 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 120 eventCallbacks.ThreadEnd = threadEndHandler; 121 eventCallbacks.ThreadStart = threadStartHandler; 122 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) { 123 return JNI_ERR; 124 } 125 126 if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) { 127 return JNI_ERR; 128 } 129 130 NSK_DISPLAY1("%s: initialization was done\n", agentName); 131 132 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 133 return JNI_ERR; 134 135 return JNI_OK; 136 } 137 138 }