1 /* 2 * Copyright (c) 2003, 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 24 #include <string.h> 25 #include "jvmti.h" 26 #include "agent_common.h" 27 #include "jni_tools.h" 28 #include "jvmti_tools.h" 29 30 extern "C" { 31 32 /* ============================================================================= */ 33 34 /* scaffold objects */ 35 static jlong timeout = 0; 36 37 /* This is how long we verify that the thread has really suspended (in ms) */ 38 static jlong verificationTime = 5 * 1000; 39 40 /* constant names */ 41 #define THREAD_NAME "TestedThread" 42 43 /* constants */ 44 #define EVENTS_COUNT 1 45 46 /* events list */ 47 static jvmtiEvent eventsList[EVENTS_COUNT] = { 48 JVMTI_EVENT_THREAD_END 49 }; 50 51 static volatile int eventsReceived = 0; 52 static jthread testedThread = NULL; 53 54 /* ============================================================================= */ 55 56 /** Agent algorithm. */ 57 static void JNICALL 58 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 59 60 NSK_DISPLAY0("Wait for thread to start\n"); 61 if (!nsk_jvmti_waitForSync(timeout)) 62 return; 63 64 /* perform testing */ 65 { 66 NSK_DISPLAY1("Find thread: %s\n", THREAD_NAME); 67 if (!NSK_VERIFY((testedThread = 68 nsk_jvmti_threadByName(THREAD_NAME)) != NULL)) 69 return; 70 NSK_DISPLAY1(" ... found thread: %p\n", (void*)testedThread); 71 72 eventsReceived = 0; 73 NSK_DISPLAY1("Enable event: %s\n", "THREAD_END"); 74 if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL)) 75 return; 76 77 NSK_DISPLAY1("Suspend thread: %p\n", (void*)testedThread); 78 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(testedThread))) { 79 nsk_jvmti_setFailStatus(); 80 return; 81 } 82 83 NSK_DISPLAY0("Let thread to run and finish\n"); 84 if (!nsk_jvmti_resumeSync()) 85 return; 86 87 NSK_DISPLAY1("Check that THREAD_END event NOT received for timeout: %ld ms\n", (long)verificationTime); 88 { 89 jlong delta = 1000; 90 jlong time; 91 for (time = 0; time < verificationTime; time += delta) { 92 if (eventsReceived > 0) { 93 NSK_COMPLAIN0("Thread ran and finished after suspension\n"); 94 nsk_jvmti_setFailStatus(); 95 break; 96 } 97 nsk_jvmti_sleep(delta); 98 } 99 } 100 101 NSK_DISPLAY1("Disable event: %s\n", "THREAD_END"); 102 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL)) 103 return; 104 105 NSK_DISPLAY1("Resume thread: %p\n", (void*)testedThread); 106 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(testedThread))) { 107 nsk_jvmti_setFailStatus(); 108 } 109 110 NSK_DISPLAY0("Wait for thread to finish\n"); 111 if (!nsk_jvmti_waitForSync(timeout)) 112 return; 113 114 NSK_DISPLAY0("Delete thread reference\n"); 115 NSK_TRACE(jni->DeleteGlobalRef(testedThread)); 116 } 117 118 NSK_DISPLAY0("Let debugee to finish\n"); 119 if (!nsk_jvmti_resumeSync()) 120 return; 121 } 122 123 /* ============================================================================= */ 124 125 /** THREAD_END callback. */ 126 JNIEXPORT void JNICALL 127 callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) { 128 /* check if event is for tested thread */ 129 if (thread != NULL && 130 jni->IsSameObject(testedThread, thread)) { 131 NSK_DISPLAY1(" ... received THREAD_END event for tested thread: %p\n", (void*)thread); 132 eventsReceived++; 133 } else { 134 NSK_DISPLAY1(" ... received THREAD_END event for unknown thread: %p\n", (void*)thread); 135 } 136 } 137 138 /* ============================================================================= */ 139 140 /** Agent library initialization. */ 141 #ifdef STATIC_BUILD 142 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) { 143 return Agent_Initialize(jvm, options, reserved); 144 } 145 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrd002(JavaVM *jvm, char *options, void *reserved) { 146 return Agent_Initialize(jvm, options, reserved); 147 } 148 JNIEXPORT jint JNI_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) { 149 return JNI_VERSION_1_8; 150 } 151 #endif 152 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 153 jvmtiEnv* jvmti = NULL; 154 155 /* init framework and parse options */ 156 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 157 return JNI_ERR; 158 159 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 160 161 /* create JVMTI environment */ 162 if (!NSK_VERIFY((jvmti = 163 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 164 return JNI_ERR; 165 166 /* add specific capabilities for suspending thread */ 167 { 168 jvmtiCapabilities suspendCaps; 169 memset(&suspendCaps, 0, sizeof(suspendCaps)); 170 suspendCaps.can_suspend = 1; 171 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps))) 172 return JNI_ERR; 173 } 174 175 /* set callbacks for THREAD_END event */ 176 { 177 jvmtiEventCallbacks callbacks; 178 memset(&callbacks, 0, sizeof(callbacks)); 179 callbacks.ThreadEnd = callbackThreadEnd; 180 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)))) 181 return JNI_ERR; 182 } 183 184 /* register agent proc and arg */ 185 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 186 return JNI_ERR; 187 188 return JNI_OK; 189 } 190 191 /* ============================================================================= */ 192 193 }