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