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 * - from Agent_OnAttach function start auxiliary thread waiting on 'gcFinishMonitor' 36 * - receive GarbageCollectionStart event 37 * - receive GarbageCollectionFinish event, notify 'gcFinishMonitor' 38 * - notified auxiliary thread calls function 'nsk_aod_agentFinished' and agent completes work 39 * (such schema is used because of agent can't call nsk_aod_agentFinished from GarbageCollectionFinish handler, 40 * nsk_aod_agentFinished calls JNI functions and it is prohibited in GarbageCollectionFinish handler) 41 * 42 */ 43 44 static Options* options = NULL; 45 static const char* agentName; 46 47 static jvmtiEvent testEvents[] = {JVMTI_EVENT_GARBAGE_COLLECTION_START, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH}; 48 static const int testEventsNumber = 2; 49 50 static jrawMonitorID gcFinishMonitor; 51 52 static volatile int gcStartEventReceived = 0; 53 54 static volatile int gcFinishEventReceived = 0; 55 56 static volatile int success = 1; 57 58 void JNICALL garbageCollectionStartHandler(jvmtiEnv *jvmti) { 59 NSK_DISPLAY1("%s: GC start event received\n", agentName); 60 61 gcStartEventReceived = 1; 62 } 63 64 void JNICALL garbageCollectionFinishHandler(jvmtiEnv *jvmti) { 65 int auxiliaryThreadNotified = 0; 66 67 NSK_DISPLAY1("%s: GC finish event received\n", agentName); 68 69 if (!gcStartEventReceived) { 70 NSK_COMPLAIN1("%s: GC start event wasn't received before GC finish event\n", agentName); 71 success = 0; 72 } 73 74 if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2( 75 RawMonitorEnter, jvmti, gcFinishMonitor))) { 76 77 gcFinishEventReceived = 1; 78 79 if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2( 80 RawMonitorNotify, jvmti, gcFinishMonitor))) { 81 auxiliaryThreadNotified = 1; 82 } 83 84 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, gcFinishMonitor))) { 85 success = 0; 86 } 87 } else { 88 success = 0; 89 } 90 91 if (!auxiliaryThreadNotified) { 92 NSK_COMPLAIN1("%s: Error happenned during auxiliary thread notification, test may hang\n", agentName); 93 } 94 } 95 96 void JNICALL auxiliaryThreadFunction(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 97 NSK_DISPLAY1("%s: auxiliary thread is running\n", agentName); 98 99 if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2( 100 RawMonitorEnter, jvmti, gcFinishMonitor))) { 101 102 if (!gcFinishEventReceived) { 103 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3( 104 RawMonitorWait, jvmti, gcFinishMonitor, 0))) { 105 success = 0; 106 } 107 } 108 109 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, gcFinishMonitor))) { 110 success = 0; 111 } 112 } else { 113 success = 0; 114 } 115 116 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni); 117 } 118 119 int startAuxiliaryThread(jvmtiEnv* jvmti, JNIEnv* jni) { 120 jthread thread; 121 122 if (!NSK_VERIFY((thread = nsk_jvmti_aod_createThread(jni)) != NULL)) 123 return NSK_FALSE; 124 125 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5( 126 RunAgentThread, jvmti, thread, auxiliaryThreadFunction, NULL, JVMTI_THREAD_NORM_PRIORITY ))) { 127 return NSK_FALSE; 128 } 129 130 NSK_DISPLAY1("%s: auxiliary thread was started\n", agentName); 131 132 return NSK_TRUE; 133 } 134 135 #ifdef STATIC_BUILD 136 JNIEXPORT jint JNI_OnLoad_attach020Agent00(JavaVM *jvm, char *options, void *reserved) { 137 return JNI_VERSION_1_8; 138 } 139 #endif 140 141 JNIEXPORT jint JNICALL 142 #ifdef STATIC_BUILD 143 Agent_OnAttach_attach020Agent00(JavaVM *vm, char *optionsString, void *reserved) 144 #else 145 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 146 #endif 147 { 148 jvmtiEnv* jvmti; 149 jvmtiEventCallbacks eventCallbacks; 150 jvmtiCapabilities caps; 151 JNIEnv* jni; 152 153 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) 154 return JNI_ERR; 155 156 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 157 158 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) 159 return JNI_ERR; 160 161 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 162 return JNI_ERR; 163 164 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "GCFinishMonitor", &gcFinishMonitor))) { 165 return JNI_ERR; 166 } 167 168 if (!NSK_VERIFY(startAuxiliaryThread(jvmti, jni))) 169 return JNI_ERR; 170 171 memset(&caps, 0, sizeof(caps)); 172 caps.can_generate_garbage_collection_events = 1; 173 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)) ) { 174 return JNI_ERR; 175 } 176 177 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 178 eventCallbacks.GarbageCollectionStart = garbageCollectionStartHandler; 179 eventCallbacks.GarbageCollectionFinish = garbageCollectionFinishHandler; 180 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) { 181 return JNI_ERR; 182 } 183 184 if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) { 185 return JNI_ERR; 186 } 187 188 NSK_DISPLAY1("%s: initialization was done\n", agentName); 189 190 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 191 return JNI_ERR; 192 193 return JNI_OK; 194 } 195 }