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 /* constant names */ 38 #define THREAD_NAME "TestedThread" 39 40 /* ============================================================================= */ 41 42 /** Agent algorithm. */ 43 static void JNICALL 44 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 45 46 NSK_DISPLAY0("Wait for thread to start\n"); 47 if (!nsk_jvmti_waitForSync(timeout)) 48 return; 49 50 /* perform testing */ 51 { 52 jthread testedThread = NULL; 53 54 NSK_DISPLAY1("Find thread: %s\n", THREAD_NAME); 55 if (!NSK_VERIFY((testedThread = 56 nsk_jvmti_threadByName(THREAD_NAME)) != NULL)) 57 return; 58 NSK_DISPLAY1(" ... found thread: %p\n", (void*)testedThread); 59 60 NSK_DISPLAY1("Suspend thread: %p\n", (void*)testedThread); 61 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(testedThread))) { 62 nsk_jvmti_setFailStatus(); 63 return; 64 } 65 66 NSK_DISPLAY0("Let thread to run and finish\n"); 67 if (!nsk_jvmti_resumeSync()) 68 return; 69 70 NSK_DISPLAY1("Get state vector for thread: %p\n", (void*)testedThread); 71 { 72 jint state = 0; 73 74 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(testedThread, &state))) { 75 nsk_jvmti_setFailStatus(); 76 } 77 NSK_DISPLAY2(" ... got state vector: %s (%d)\n", 78 TranslateState(state), (int)state); 79 80 if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) { 81 NSK_COMPLAIN2("SuspendThread() does not turn on flag SUSPENDED:\n" 82 "# state: %s (%d)\n", 83 TranslateState(state), (int)state); 84 nsk_jvmti_setFailStatus(); 85 } 86 } 87 88 NSK_DISPLAY1("Resume thread: %p\n", (void*)testedThread); 89 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(testedThread))) { 90 nsk_jvmti_setFailStatus(); 91 } 92 93 NSK_DISPLAY0("Wait for thread to finish\n"); 94 if (!nsk_jvmti_waitForSync(timeout)) 95 return; 96 97 NSK_DISPLAY0("Delete thread reference\n"); 98 NSK_TRACE(jni->DeleteGlobalRef(testedThread)); 99 } 100 101 NSK_DISPLAY0("Let debugee to finish\n"); 102 if (!nsk_jvmti_resumeSync()) 103 return; 104 } 105 106 /* ============================================================================= */ 107 108 /** Agent library initialization. */ 109 #ifdef STATIC_BUILD 110 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrd001(JavaVM *jvm, char *options, void *reserved) { 111 return Agent_Initialize(jvm, options, reserved); 112 } 113 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrd001(JavaVM *jvm, char *options, void *reserved) { 114 return Agent_Initialize(jvm, options, reserved); 115 } 116 JNIEXPORT jint JNI_OnLoad_suspendthrd001(JavaVM *jvm, char *options, void *reserved) { 117 return JNI_VERSION_1_8; 118 } 119 #endif 120 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 121 jvmtiEnv* jvmti = NULL; 122 123 /* init framework and parse options */ 124 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 125 return JNI_ERR; 126 127 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 128 129 /* create JVMTI environment */ 130 if (!NSK_VERIFY((jvmti = 131 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 132 return JNI_ERR; 133 134 /* add specific capabilities for suspending thread */ 135 { 136 jvmtiCapabilities suspendCaps; 137 memset(&suspendCaps, 0, sizeof(suspendCaps)); 138 suspendCaps.can_suspend = 1; 139 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps))) 140 return JNI_ERR; 141 } 142 143 /* register agent proc and arg */ 144 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 145 return JNI_ERR; 146 147 return JNI_OK; 148 } 149 150 /* ============================================================================= */ 151 152 }