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 events 36 * - target application starts several threads 37 * - agent receives ThreadStart events and tries to find thread provoked this event in the array returned 38 * by JVMTI function GetAllThreads 39 * - when expected number of ThreadStart events is received agent finishes work 40 */ 41 42 #define TEST_THREADS_NUMBER 5 43 44 #define TEST_THREAD_NAME_PREFIX "attach040-TestThread-" 45 46 static jrawMonitorID threadsCounterMonitor; 47 48 static volatile int testThreadsCounter = 0; 49 50 static Options* options = NULL; 51 static const char* agentName; 52 53 void JNICALL threadStartHandler(jvmtiEnv *jvmti, 54 JNIEnv* jni, 55 jthread thread) { 56 int success = 1; 57 jint threadsCount = 0; 58 jthread * threads; 59 jint i; 60 char startedThreadName[MAX_STRING_LENGTH]; 61 62 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) { 63 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni); 64 return; 65 } 66 67 NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName); 68 69 if (NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetAllThreads, jvmti, &threadsCount, &threads))) { 70 int startedThreadWasFound = 0; 71 72 for (i = 0; i < threadsCount; i++) { 73 char threadName[MAX_STRING_LENGTH]; 74 75 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 76 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni); 77 return; 78 } 79 80 if (!strcmp(threadName, startedThreadName)) { 81 startedThreadWasFound = 1; 82 break; 83 } 84 } 85 86 if (!startedThreadWasFound) { 87 NSK_COMPLAIN2("%s: GetAllThreads didn't return information about thread '%s'\n", agentName, startedThreadName); 88 success = 0; 89 } 90 91 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads); 92 } else { 93 success = 0; 94 } 95 96 if (strstr(startedThreadName, TEST_THREAD_NAME_PREFIX)) { 97 if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2( 98 RawMonitorEnter, jvmti, threadsCounterMonitor))) { 99 100 testThreadsCounter++; 101 102 if (testThreadsCounter == TEST_THREADS_NUMBER) { 103 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni); 104 } 105 106 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, threadsCounterMonitor))) { 107 success = 0; 108 } 109 } else { 110 success = 0; 111 } 112 } 113 114 if (!success) { 115 NSK_COMPLAIN1("%s: unexpected error during agent work, stop agent\n", agentName); 116 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni); 117 } 118 } 119 120 #ifdef STATIC_BUILD 121 JNIEXPORT jint JNI_OnLoad_attach040Agent00(JavaVM *jvm, char *options, void *reserved) { 122 return JNI_VERSION_1_8; 123 } 124 #endif 125 126 JNIEXPORT jint JNICALL 127 #ifdef STATIC_BUILD 128 Agent_OnAttach_attach040Agent00(JavaVM *vm, char *optionsString, void *reserved) 129 #else 130 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 131 #endif 132 { 133 jvmtiEventCallbacks eventCallbacks; 134 jvmtiEnv* jvmti; 135 JNIEnv* jni; 136 137 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) 138 return JNI_ERR; 139 140 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 141 142 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) 143 return JNI_ERR; 144 145 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 146 return JNI_ERR; 147 148 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "threadsCounterMonitor", &threadsCounterMonitor))) { 149 return JNI_ERR; 150 } 151 152 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 153 eventCallbacks.ThreadStart = threadStartHandler; 154 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) { 155 return JNI_ERR; 156 } 157 158 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) { 159 return JNI_ERR; 160 } 161 162 NSK_DISPLAY1("%s: initialization was done\n", agentName); 163 164 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 165 return JNI_ERR; 166 167 return JNI_OK; 168 } 169 170 }