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 MonitorContentedEnter event for thread ThreadGeneratingEvents 36 * - receive MonitorContentedEntered 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_MONITOR_WAIT, JVMTI_EVENT_MONITOR_WAITED}; 45 static const int testEventsNumber = 2; 46 47 volatile int monitorWaitReceived = 0; 48 49 void JNICALL monitorWaitHandler(jvmtiEnv *jvmti, 50 JNIEnv* jni, 51 jthread thread, 52 jobject object, 53 jlong timeout) { 54 char threadName[MAX_STRING_LENGTH]; 55 56 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 57 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 58 return; 59 } 60 61 NSK_DISPLAY2("%s: MonitorWait event was received for thread '%s'\n", agentName, threadName); 62 63 if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) { 64 monitorWaitReceived = 1; 65 } 66 } 67 68 void JNICALL monitorWaitedHandler(jvmtiEnv *jvmti, 69 JNIEnv* jni, 70 jthread thread, 71 jobject object, 72 jboolean timed_out) { 73 char threadName[MAX_STRING_LENGTH]; 74 75 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) { 76 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 77 return; 78 } 79 80 NSK_DISPLAY2("%s: MonitorWaited event was received for thread '%s'\n", agentName, threadName); 81 82 if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) { 83 int success = 1; 84 85 if (!monitorWaitReceived) { 86 success = 0; 87 NSK_COMPLAIN2("%s: MonitorWait event wasn't received for thread '%s'\n", agentName, threadName); 88 } 89 90 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni); 91 } 92 } 93 94 #ifdef STATIC_BUILD 95 JNIEXPORT jint JNI_OnLoad_attach037Agent00(JavaVM *jvm, char *options, void *reserved) { 96 return JNI_VERSION_1_8; 97 } 98 #endif 99 100 JNIEXPORT jint JNICALL 101 #ifdef STATIC_BUILD 102 Agent_OnAttach_attach037Agent00(JavaVM *vm, char *optionsString, void *reserved) 103 #else 104 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 105 #endif 106 { 107 jvmtiEventCallbacks eventCallbacks; 108 jvmtiCapabilities caps; 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 memset(&caps, 0, sizeof(caps)); 124 caps.can_generate_monitor_events = 1; 125 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) { 126 return JNI_ERR; 127 } 128 129 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 130 eventCallbacks.MonitorWaited = monitorWaitedHandler; 131 eventCallbacks.MonitorWait = monitorWaitHandler; 132 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) { 133 return JNI_ERR; 134 } 135 136 if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) { 137 return JNI_ERR; 138 } 139 140 NSK_DISPLAY1("%s: initialization was done\n", agentName); 141 142 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 143 return JNI_ERR; 144 145 return JNI_OK; 146 } 147 148 }