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 /* constants */ 41 #define DEFAULT_THREADS_COUNT 10 42 43 static int threadsCount = 0; 44 45 /* ============================================================================= */ 46 47 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni, 48 const char name[], int foundCount, jthread foundThreads[]); 49 50 /** Agent algorithm. */ 51 static void JNICALL 52 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 53 54 NSK_DISPLAY0("Wait for threads to start\n"); 55 if (!nsk_jvmti_waitForSync(timeout)) 56 return; 57 58 /* perform testing */ 59 { 60 jthread* threads = NULL; 61 jvmtiError* results = NULL; 62 int i; 63 64 NSK_DISPLAY1("Allocate threads array: %d threads\n", threadsCount); 65 if (!NSK_JVMTI_VERIFY( 66 NSK_CPP_STUB3(Allocate, jvmti, (threadsCount * sizeof(jthread)), 67 (unsigned char**)&threads))) { 68 nsk_jvmti_setFailStatus(); 69 return; 70 } 71 NSK_DISPLAY1(" ... allocated array: %p\n", (void*)threads); 72 73 NSK_DISPLAY1("Allocate results array: %d threads\n", threadsCount); 74 if (!NSK_JVMTI_VERIFY( 75 NSK_CPP_STUB3(Allocate, jvmti, (threadsCount * sizeof(jvmtiError)), 76 (unsigned char**)&results))) { 77 nsk_jvmti_setFailStatus(); 78 return; 79 } 80 NSK_DISPLAY1(" ... allocated array: %p\n", (void*)threads); 81 82 NSK_DISPLAY1("Find threads: %d threads\n", threadsCount); 83 if (!NSK_VERIFY(fillThreadsByName(jvmti, jni, THREAD_NAME, threadsCount, threads))) 84 return; 85 86 NSK_DISPLAY0("Suspend threads list\n"); 87 if (!NSK_JVMTI_VERIFY( 88 NSK_CPP_STUB4(SuspendThreadList, jvmti, threadsCount, threads, results))) { 89 nsk_jvmti_setFailStatus(); 90 return; 91 } 92 93 NSK_DISPLAY0("Check threads results:\n"); 94 for (i = 0; i < threadsCount; i++) { 95 NSK_DISPLAY3(" ... thread #%d: %s (%d)\n", 96 i, TranslateError(results[i]), (int)results[i]); 97 if (!NSK_JVMTI_VERIFY(results[i])) 98 nsk_jvmti_setFailStatus(); 99 } 100 101 NSK_DISPLAY0("Let threads to run and finish\n"); 102 if (!nsk_jvmti_resumeSync()) 103 return; 104 105 NSK_DISPLAY0("Get state vector for each thread\n"); 106 for (i = 0; i < threadsCount; i++) { 107 jint state = 0; 108 109 NSK_DISPLAY2(" thread #%d (%p):\n", i, (void*)threads[i]); 110 if (!NSK_JVMTI_VERIFY( 111 NSK_CPP_STUB3(GetThreadState, jvmti, threads[i], &state))) { 112 nsk_jvmti_setFailStatus(); 113 } 114 NSK_DISPLAY2(" ... got state vector: %s (%d)\n", 115 TranslateState(state), (int)state); 116 117 if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) { 118 NSK_COMPLAIN3("SuspendThreadList() does not turn on flag SUSPENDED for thread #%i:\n" 119 "# state: %s (%d)\n", 120 i, TranslateState(state), (int)state); 121 nsk_jvmti_setFailStatus(); 122 } 123 } 124 125 NSK_DISPLAY0("Resume threads list\n"); 126 if (!NSK_JVMTI_VERIFY( 127 NSK_CPP_STUB4(ResumeThreadList, jvmti, threadsCount, threads, results))) { 128 nsk_jvmti_setFailStatus(); 129 return; 130 } 131 132 NSK_DISPLAY0("Wait for thread to finish\n"); 133 if (!nsk_jvmti_waitForSync(timeout)) 134 return; 135 136 NSK_DISPLAY0("Delete threads references\n"); 137 for (i = 0; i < threadsCount; i++) { 138 if (threads[i] != NULL) 139 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threads[i])); 140 } 141 142 NSK_DISPLAY1("Deallocate threads array: %p\n", (void*)threads); 143 if (!NSK_JVMTI_VERIFY( 144 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads))) { 145 nsk_jvmti_setFailStatus(); 146 } 147 148 NSK_DISPLAY1("Deallocate results array: %p\n", (void*)results); 149 if (!NSK_JVMTI_VERIFY( 150 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)results))) { 151 nsk_jvmti_setFailStatus(); 152 } 153 } 154 155 NSK_DISPLAY0("Let debugee to finish\n"); 156 if (!nsk_jvmti_resumeSync()) 157 return; 158 } 159 160 /* ============================================================================= */ 161 162 /** Find threads whose name starts with specified name prefix. */ 163 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni, 164 const char name[], int foundCount, jthread foundThreads[]) { 165 jint count = 0; 166 jthread* threads = NULL; 167 168 size_t len = strlen(name); 169 int found = 0; 170 int i; 171 172 for (i = 0; i < foundCount; i++) { 173 foundThreads[i] = NULL; 174 } 175 176 if (!NSK_JVMTI_VERIFY( 177 NSK_CPP_STUB3(GetAllThreads, jvmti, &count, &threads))) { 178 nsk_jvmti_setFailStatus(); 179 return NSK_FALSE; 180 } 181 182 found = 0; 183 for (i = 0; i < count; i++) { 184 jvmtiThreadInfo info; 185 186 if (!NSK_JVMTI_VERIFY( 187 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info))) { 188 nsk_jvmti_setFailStatus(); 189 break; 190 } 191 192 if (info.name != NULL && strncmp(name, info.name, len) == 0) { 193 NSK_DISPLAY3(" ... found thread #%d: %p (%s)\n", 194 found, threads[i], info.name); 195 if (found < foundCount) 196 foundThreads[found] = threads[i]; 197 found++; 198 } 199 200 } 201 202 if (!NSK_JVMTI_VERIFY( 203 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads))) { 204 nsk_jvmti_setFailStatus(); 205 return NSK_FALSE; 206 } 207 208 if (found != foundCount) { 209 NSK_COMPLAIN3("Unexpected number of tested threads found:\n" 210 "# name: %s\n" 211 "# found: %d\n" 212 "# expected: %d\n", 213 name, found, foundCount); 214 nsk_jvmti_setFailStatus(); 215 return NSK_FALSE; 216 } 217 218 NSK_DISPLAY1("Make global references for threads: %d threads\n", foundCount); 219 for (i = 0; i < foundCount; i++) { 220 if (!NSK_JNI_VERIFY(jni, (foundThreads[i] = (jthread) 221 NSK_CPP_STUB2(NewGlobalRef, jni, foundThreads[i])) != NULL)) { 222 nsk_jvmti_setFailStatus(); 223 return NSK_FALSE; 224 } 225 NSK_DISPLAY2(" ... thread #%d: %p\n", i, foundThreads[i]); 226 } 227 228 return NSK_TRUE; 229 } 230 231 /* ============================================================================= */ 232 233 /** Agent library initialization. */ 234 #ifdef STATIC_BUILD 235 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) { 236 return Agent_Initialize(jvm, options, reserved); 237 } 238 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) { 239 return Agent_Initialize(jvm, options, reserved); 240 } 241 JNIEXPORT jint JNI_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) { 242 return JNI_VERSION_1_8; 243 } 244 #endif 245 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 246 jvmtiEnv* jvmti = NULL; 247 248 /* init framework and parse options */ 249 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 250 return JNI_ERR; 251 252 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 253 254 /* get options */ 255 threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_COUNT); 256 if (!NSK_VERIFY(threadsCount > 0)) 257 return JNI_ERR; 258 259 /* create JVMTI environment */ 260 if (!NSK_VERIFY((jvmti = 261 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 262 return JNI_ERR; 263 264 /* add specific capabilities for suspending thread */ 265 { 266 jvmtiCapabilities suspendCaps; 267 memset(&suspendCaps, 0, sizeof(suspendCaps)); 268 suspendCaps.can_suspend = 1; 269 if (!NSK_JVMTI_VERIFY( 270 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps))) 271 return JNI_ERR; 272 } 273 274 /* register agent proc and arg */ 275 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 276 return JNI_ERR; 277 278 return JNI_OK; 279 } 280 281 /* ============================================================================= */ 282 283 }