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 * - during initialization agent enables ThreadStart and ThreadEnd events and starts new 36 * thread using JVMTI RunAgentThread 37 * - agent receives ThreadStart event for started thread 38 - agent receives ThreadEnd event for started thread and finishes work 39 */ 40 41 #define STARTED_THREAD_NAME "ThreadStartedByAgent" 42 43 static Options* options = NULL; 44 static const char* agentName; 45 46 static jvmtiEvent testEvents[] = {JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END}; 47 static const int testEventsNumber = 2; 48 49 volatile int threadStartReceived = 0; 50 volatile int threadWasExecuted = 0; 51 52 void JNICALL startedThreadFunction(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 53 char threadName[MAX_STRING_LENGTH]; 54 55 if (!nsk_jvmti_aod_getThreadName(jvmti, NULL, threadName)) { 56 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 57 return; 58 } 59 60 NSK_DISPLAY2("%s: thread started from agent is running (thread name: '%s')\n", agentName, threadName); 61 62 threadWasExecuted = 1; 63 } 64 65 int startNewThread(jvmtiEnv* jvmti, JNIEnv* jni) { 66 jthread thread; 67 68 if (!NSK_VERIFY((thread = nsk_jvmti_aod_createThreadWithName(jni, STARTED_THREAD_NAME)) != NULL)) 69 return NSK_FALSE; 70 71 if (!NSK_JVMTI_VERIFY(jvmti->RunAgentThread(thread, startedThreadFunction, NULL, JVMTI_THREAD_NORM_PRIORITY ))) { 72 return NSK_FALSE; 73 } 74 75 NSK_DISPLAY1("%s: new thread was started\n", agentName); 76 77 return NSK_TRUE; 78 } 79 80 void JNICALL threadEndHandler(jvmtiEnv *jvmti, 81 JNIEnv* jni, 82 jthread thread) { 83 char threadName[MAX_STRING_LENGTH]; 84 85 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 86 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 87 return; 88 } 89 90 NSK_DISPLAY2("%s: ThreadEnd event was received for thread '%s'\n", agentName, threadName); 91 92 if (!strcmp(STARTED_THREAD_NAME, threadName)) { 93 int success = 1; 94 95 if (!threadStartReceived) { 96 NSK_COMPLAIN1("%s: ThreadStart event wasn't received\n", agentName); 97 success = 0; 98 } 99 100 if (!threadWasExecuted) { 101 NSK_COMPLAIN1("%s: started thread haven't execute its code\n", agentName); 102 success = 0; 103 } 104 105 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni); 106 } 107 } 108 109 void JNICALL threadStartHandler(jvmtiEnv *jvmti, 110 JNIEnv* jni, 111 jthread thread) { 112 char threadName[MAX_STRING_LENGTH]; 113 114 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 115 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 116 return; 117 } 118 119 NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, threadName); 120 121 if (!strcmp(STARTED_THREAD_NAME, threadName)) { 122 threadStartReceived = 1; 123 } 124 } 125 126 #ifdef STATIC_BUILD 127 JNIEXPORT jint JNI_OnLoad_attach039Agent00(JavaVM *jvm, char *options, void *reserved) { 128 return JNI_VERSION_1_8; 129 } 130 #endif 131 132 JNIEXPORT jint JNICALL 133 #ifdef STATIC_BUILD 134 Agent_OnAttach_attach039Agent00(JavaVM *vm, char *optionsString, void *reserved) 135 #else 136 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 137 #endif 138 { 139 jvmtiEventCallbacks eventCallbacks; 140 jvmtiEnv* jvmti; 141 JNIEnv* jni; 142 143 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) 144 return JNI_ERR; 145 146 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 147 148 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) 149 return JNI_ERR; 150 151 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 152 return JNI_ERR; 153 154 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 155 eventCallbacks.ThreadEnd = threadEndHandler; 156 eventCallbacks.ThreadStart = threadStartHandler; 157 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) { 158 return JNI_ERR; 159 } 160 161 if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) { 162 return JNI_ERR; 163 } 164 165 NSK_DISPLAY1("%s: initialization was done\n", agentName); 166 167 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 168 return JNI_ERR; 169 170 NSK_DISPLAY1("%s: starting new thread\n", agentName); 171 172 if (!NSK_VERIFY(startNewThread(jvmti, jni))) 173 return JNI_ERR; 174 175 return JNI_OK; 176 } 177 178 }