1 /* 2 * Copyright (c) 2004, 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 <stdlib.h> 25 #include <string.h> 26 #include "jni_tools.h" 27 #include "agent_common.h" 28 #include "jvmti_tools.h" 29 30 #define PASSED 0 31 #define STATUS_FAILED 2 32 33 extern "C" { 34 35 /* ========================================================================== */ 36 37 /* scaffold objects */ 38 static jlong timeout = 0; 39 40 /* test objects */ 41 static jobject threadDeath = NULL; 42 static jthread runningThread = NULL; 43 static jthread waitingThread = NULL; 44 static jthread sleepingThread = NULL; 45 46 /* ========================================================================== */ 47 48 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) { 49 const char* RUNNING_THREAD_NAME = "DebuggeeRunningThread"; 50 const char* WAITING_THREAD_NAME = "DebuggeeWaitingThread"; 51 const char* SLEEPING_THREAD_NAME = "DebuggeeSleepingThread"; 52 const char* THREAD_DEATH_CLASS_NAME = "java/lang/ThreadDeath"; 53 const char* THREAD_DEATH_CTOR_NAME = "<init>"; 54 const char* THREAD_DEATH_CTOR_SIGNATURE = "()V"; 55 jvmtiThreadInfo info; 56 jthread *threads = NULL; 57 jint threads_count = 0; 58 jclass cls = NULL; 59 jmethodID ctor = NULL; 60 int i; 61 62 NSK_DISPLAY0("Prepare: find tested threads\n"); 63 64 /* get all live threads */ 65 if (!NSK_JVMTI_VERIFY( 66 NSK_CPP_STUB3(GetAllThreads, jvmti, &threads_count, &threads))) 67 return NSK_FALSE; 68 69 if (!NSK_VERIFY(threads_count > 0 && threads != NULL)) 70 return NSK_FALSE; 71 72 /* find tested thread */ 73 for (i = 0; i < threads_count; i++) { 74 if (!NSK_VERIFY(threads[i] != NULL)) 75 return NSK_FALSE; 76 77 /* get thread information */ 78 if (!NSK_JVMTI_VERIFY( 79 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info))) 80 return NSK_FALSE; 81 82 NSK_DISPLAY3(" thread #%d (%s): %p\n", i, info.name, threads[i]); 83 84 /* find by name */ 85 if (info.name != NULL) { 86 if (strcmp(info.name, RUNNING_THREAD_NAME) == 0) { 87 runningThread = threads[i]; 88 } else if (strcmp(info.name, WAITING_THREAD_NAME) == 0) { 89 waitingThread = threads[i]; 90 } else if (strcmp(info.name, SLEEPING_THREAD_NAME) == 0) { 91 sleepingThread = threads[i]; 92 } 93 } 94 } 95 96 if (!NSK_JVMTI_VERIFY( 97 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads))) 98 return NSK_FALSE; 99 100 NSK_DISPLAY0("Prepare: create new instance of ThreadDeath exception\n"); 101 102 if (!NSK_JNI_VERIFY(jni, (cls = 103 NSK_CPP_STUB2(FindClass, jni, THREAD_DEATH_CLASS_NAME)) != NULL)) 104 return NSK_FALSE; 105 106 if (!NSK_JNI_VERIFY(jni, (ctor = 107 NSK_CPP_STUB4(GetMethodID, jni, cls, 108 THREAD_DEATH_CTOR_NAME, THREAD_DEATH_CTOR_SIGNATURE)) != NULL)) 109 return NSK_FALSE; 110 111 if (!NSK_JNI_VERIFY(jni, (threadDeath = 112 NSK_CPP_STUB3(NewObject, jni, cls, ctor)) != NULL)) 113 return NSK_FALSE; 114 115 return NSK_TRUE; 116 } 117 118 /* ========================================================================== */ 119 120 /** Agent algorithm. */ 121 static void JNICALL 122 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 123 124 if (!nsk_jvmti_waitForSync(timeout)) 125 return; 126 127 if (!prepare(jvmti, jni)) { 128 nsk_jvmti_setFailStatus(); 129 return; 130 } 131 132 NSK_DISPLAY0("Testcase #1: call StopThread for runningThread\n"); 133 if (!NSK_VERIFY(runningThread != NULL)) { 134 nsk_jvmti_setFailStatus(); 135 } else { 136 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(StopThread, 137 jvmti, runningThread, threadDeath))) 138 nsk_jvmti_setFailStatus(); 139 } 140 141 NSK_DISPLAY0("Testcase #2: call StopThread for waitingThread\n"); 142 if (!NSK_VERIFY(waitingThread != NULL)) { 143 nsk_jvmti_setFailStatus(); 144 } else { 145 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(StopThread, 146 jvmti, waitingThread, threadDeath))) 147 nsk_jvmti_setFailStatus(); 148 } 149 150 NSK_DISPLAY0("Testcase #3: call StopThread for sleepingThread\n"); 151 if (!NSK_VERIFY(sleepingThread != NULL)) { 152 nsk_jvmti_setFailStatus(); 153 } else { 154 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(StopThread, 155 jvmti, sleepingThread, threadDeath))) 156 nsk_jvmti_setFailStatus(); 157 } 158 159 if (!nsk_jvmti_resumeSync()) 160 return; 161 } 162 163 /* ========================================================================== */ 164 165 /** Agent library initialization. */ 166 #ifdef STATIC_BUILD 167 JNIEXPORT jint JNICALL Agent_OnLoad_stopthrd007(JavaVM *jvm, char *options, void *reserved) { 168 return Agent_Initialize(jvm, options, reserved); 169 } 170 JNIEXPORT jint JNICALL Agent_OnAttach_stopthrd007(JavaVM *jvm, char *options, void *reserved) { 171 return Agent_Initialize(jvm, options, reserved); 172 } 173 JNIEXPORT jint JNI_OnLoad_stopthrd007(JavaVM *jvm, char *options, void *reserved) { 174 return JNI_VERSION_1_8; 175 } 176 #endif 177 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 178 jvmtiEnv* jvmti = NULL; 179 jvmtiCapabilities caps; 180 181 NSK_DISPLAY0("Agent_OnLoad\n"); 182 183 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 184 return JNI_ERR; 185 186 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 187 188 if (!NSK_VERIFY((jvmti = 189 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 190 return JNI_ERR; 191 192 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 193 return JNI_ERR; 194 195 memset(&caps, 0, sizeof(caps)); 196 caps.can_signal_thread = 1; 197 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) { 198 return JNI_ERR; 199 } 200 201 return JNI_OK; 202 } 203 204 /* ========================================================================== */ 205 206 }