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 JNIEnv* jni = NULL; 36 static jvmtiEnv *jvmti = NULL; 37 static jlong timeout = 0; 38 39 /* constants */ 40 #define THREADS_COUNT 6 41 #define EVENTS_COUNT 2 42 #define MAX_NAME_LENGTH 100 43 #define MAX_STACK_SIZE 100 44 45 /* tested events */ 46 static jvmtiEvent eventsList[EVENTS_COUNT] = { 47 JVMTI_EVENT_COMPILED_METHOD_LOAD, 48 JVMTI_EVENT_COMPILED_METHOD_UNLOAD 49 }; 50 51 /* thread description structure */ 52 typedef struct { 53 char threadName[MAX_NAME_LENGTH]; 54 char methodName[MAX_NAME_LENGTH]; 55 char methodSig[MAX_NAME_LENGTH]; 56 jthread thread; 57 jclass cls; 58 jmethodID method; 59 jlocation location; 60 int methodCompiled; 61 } ThreadDesc; 62 63 /* descriptions of tested threads */ 64 static ThreadDesc threadsDesc[THREADS_COUNT] = { 65 {"threadRunning", "testedMethod", "(ZI)V", NULL, NULL, NULL, NSK_JVMTI_INVALID_JLOCATION, NSK_FALSE}, 66 {"threadEntering", "testedMethod", "(ZI)V", NULL, NULL, NULL, NSK_JVMTI_INVALID_JLOCATION, NSK_FALSE}, 67 {"threadWaiting", "testedMethod", "(ZI)V", NULL, NULL, NULL, NSK_JVMTI_INVALID_JLOCATION, NSK_FALSE}, 68 {"threadSleeping", "testedMethod", "(ZI)V", NULL, NULL, NULL, NSK_JVMTI_INVALID_JLOCATION, NSK_FALSE}, 69 {"threadRunningInterrupted", "testedMethod", "(ZI)V", NULL, NULL, NULL, NSK_JVMTI_INVALID_JLOCATION, NSK_FALSE}, 70 {"threadRunningNative", "testedMethod", "(ZI)V", NULL, NULL, NULL, NSK_JVMTI_INVALID_JLOCATION, NSK_FALSE} 71 }; 72 73 /* indexes of known threads */ 74 static const int interruptedThreadIndex = THREADS_COUNT - 2; 75 static const int nativeThreadIndex = THREADS_COUNT - 1; 76 77 /* ============================================================================= */ 78 79 /* testcase(s) */ 80 static int prepare(); 81 static int generateEvents(); 82 static int checkThreads(int suspended, const char* kind); 83 static int suspendThreadsIndividually(int suspend); 84 static int clean(); 85 86 /* ============================================================================= */ 87 88 /** Agent algorithm. */ 89 static void JNICALL 90 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) { 91 jni = agentJNI; 92 93 NSK_DISPLAY0("Wait for debuggee to become ready\n"); 94 if (!nsk_jvmti_waitForSync(timeout)) 95 return; 96 97 { 98 NSK_DISPLAY0("Prepare data\n"); 99 if (!prepare()) { 100 nsk_jvmti_setFailStatus(); 101 return; 102 } 103 104 NSK_DISPLAY0("Generate missed events\n"); 105 if (!generateEvents()) 106 return; 107 108 NSK_DISPLAY0("Testcase #1: check stack frames of not suspended threads\n"); 109 if (!checkThreads(NSK_FALSE, "not suspended")) 110 return; 111 112 NSK_DISPLAY0("Suspend each thread\n"); 113 if (!suspendThreadsIndividually(NSK_TRUE)) 114 return; 115 116 NSK_DISPLAY0("Testcase #2: check stack frames of suspended threads\n"); 117 if (!checkThreads(NSK_TRUE, "suspended")) 118 return; 119 120 NSK_DISPLAY0("Resume each thread\n"); 121 if (!suspendThreadsIndividually(NSK_FALSE)) 122 return; 123 124 NSK_DISPLAY0("Testcase #3: check stack frames of resumed threads\n"); 125 if (!checkThreads(NSK_FALSE, "resumed")) 126 return; 127 128 NSK_DISPLAY0("Clean data\n"); 129 if (!clean()) { 130 nsk_jvmti_setFailStatus(); 131 return; 132 } 133 } 134 135 NSK_DISPLAY0("Let debuggee to finish\n"); 136 if (!nsk_jvmti_resumeSync()) 137 return; 138 } 139 140 /* ============================================================================= */ 141 142 /** 143 * Generate missed events (COMPILED_METHOD_LOAD only). 144 */ 145 static int generateEvents() { 146 if (!NSK_JVMTI_VERIFY( 147 NSK_CPP_STUB2(GenerateEvents, jvmti, JVMTI_EVENT_COMPILED_METHOD_LOAD))) { 148 nsk_jvmti_setFailStatus(); 149 return NSK_FALSE; 150 } 151 return NSK_TRUE; 152 } 153 154 /** 155 * Prepare data. 156 * - clean threads list 157 * - get all live threads 158 * - get threads name 159 * - find tested threads 160 * - make global refs 161 * - enable events 162 */ 163 static int prepare() { 164 jthread *allThreadsList = NULL; 165 jint allThreadsCount = 0; 166 int found = 0; 167 int i; 168 169 NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT); 170 171 /* clean threads list */ 172 for (i = 0; i < THREADS_COUNT; i++) { 173 threadsDesc[i].thread = (jthread)NULL; 174 threadsDesc[i].method = (jmethodID)NULL; 175 threadsDesc[i].location = NSK_JVMTI_INVALID_JLOCATION; 176 threadsDesc[i].methodCompiled = NSK_FALSE; 177 } 178 179 /* get all live threads */ 180 if (!NSK_JVMTI_VERIFY( 181 NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList))) 182 return NSK_FALSE; 183 184 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL)) 185 return NSK_FALSE; 186 187 /* find tested threads */ 188 for (i = 0; i < allThreadsCount; i++) { 189 jvmtiThreadInfo threadInfo; 190 191 if (!NSK_VERIFY(allThreadsList[i] != NULL)) 192 return NSK_FALSE; 193 194 if (!NSK_JVMTI_VERIFY( 195 NSK_CPP_STUB3(GetThreadInfo, jvmti, allThreadsList[i], &threadInfo))) 196 return NSK_FALSE; 197 198 if (threadInfo.name != NULL) { 199 int j; 200 201 for (j = 0; j < THREADS_COUNT; j++) { 202 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) { 203 threadsDesc[j].thread = allThreadsList[i]; 204 NSK_DISPLAY3(" thread #%d (%s): 0x%p\n", 205 j, threadInfo.name, (void*)threadsDesc[j].thread); 206 } 207 } 208 } 209 } 210 211 /* deallocate all threads list */ 212 if (!NSK_JVMTI_VERIFY( 213 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList))) 214 return NSK_FALSE; 215 216 /* check if all tested threads found */ 217 found = 0; 218 for (i = 0; i < THREADS_COUNT; i++) { 219 if (threadsDesc[i].thread == NULL) { 220 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName); 221 } else { 222 found++; 223 } 224 } 225 226 if (found < THREADS_COUNT) 227 return NSK_FALSE; 228 229 /* get threads class and frame method */ 230 NSK_DISPLAY0("Find tested methods:\n"); 231 for (i = 0; i < THREADS_COUNT; i++) { 232 233 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = 234 NSK_CPP_STUB2(GetObjectClass, jni, threadsDesc[i].thread)) != NULL)) 235 return NSK_FALSE; 236 237 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method = 238 NSK_CPP_STUB4(GetMethodID, jni, threadsDesc[i].cls, 239 threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL)) 240 return NSK_FALSE; 241 242 NSK_DISPLAY4(" thread #%d (%s): 0x%p (%s)\n", 243 i, threadsDesc[i].threadName, 244 (void*)threadsDesc[i].method, 245 threadsDesc[i].methodName); 246 } 247 248 /* make global refs */ 249 for (i = 0; i < THREADS_COUNT; i++) { 250 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread) 251 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].thread)) != NULL)) 252 return NSK_FALSE; 253 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass) 254 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].cls)) != NULL)) 255 return NSK_FALSE; 256 } 257 258 NSK_DISPLAY0("Enable tested events\n"); 259 if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL)) 260 return NSK_FALSE; 261 262 return NSK_TRUE; 263 } 264 265 /** 266 * Suspend or resume tested threads. 267 */ 268 static int suspendThreadsIndividually(int suspend) { 269 int i; 270 271 for (i = 0; i < THREADS_COUNT; i++) { 272 if (suspend) { 273 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsDesc[i].threadName); 274 if (!NSK_JVMTI_VERIFY( 275 NSK_CPP_STUB2(SuspendThread, jvmti, threadsDesc[i].thread))) 276 nsk_jvmti_setFailStatus(); 277 } else { 278 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsDesc[i].threadName); 279 if (!NSK_JVMTI_VERIFY( 280 NSK_CPP_STUB2(ResumeThread, jvmti, threadsDesc[i].thread))) 281 nsk_jvmti_setFailStatus(); 282 } 283 } 284 return NSK_TRUE; 285 } 286 287 /** 288 * Testcase: check tested threads. 289 * - call GetFrameCount() and getStackTrace() 290 * - for suspended thread compare number of stack frames returned 291 * - find stack frames with expected methodID 292 * 293 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break. 294 */ 295 static int checkThreads(int suspended, const char* kind0) { 296 char kind[256] = ""; 297 int i; 298 299 /* check each thread */ 300 for (i = 0; i < THREADS_COUNT; i++) { 301 jint frameCount = 0; 302 jint frameStackSize = 0; 303 jvmtiFrameInfo frameStack[MAX_STACK_SIZE]; 304 int found = 0; 305 int j; 306 307 /* make proper kind */ 308 strcpy(kind, threadsDesc[i].methodCompiled ? "compiled " : "not compiled "); 309 strcat(kind, kind0); 310 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsDesc[i].threadName); 311 312 /* get frame count */ 313 if (!NSK_JVMTI_VERIFY( 314 NSK_CPP_STUB3(GetFrameCount, jvmti, 315 threadsDesc[i].thread, &frameCount))) { 316 nsk_jvmti_setFailStatus(); 317 return NSK_TRUE; 318 } 319 320 NSK_DISPLAY1(" frameCount: %d\n", (int)frameCount); 321 322 /* get stack trace */ 323 if (!NSK_JVMTI_VERIFY( 324 NSK_CPP_STUB6(GetStackTrace, jvmti, threadsDesc[i].thread, 325 0, MAX_STACK_SIZE, frameStack, &frameStackSize))) { 326 nsk_jvmti_setFailStatus(); 327 return NSK_TRUE; 328 } 329 330 NSK_DISPLAY1(" stack depth: %d\n", (int)frameStackSize); 331 332 /* Only check for suspended threads: running threads might have different 333 frames between stack grabbing calls. */ 334 if (suspended && (frameStackSize != frameCount)) { 335 NSK_COMPLAIN5("Different frames count for %s thread #%d (%s):\n" 336 "# getStackTrace(): %d\n" 337 "# getFrameCount(): %d\n", 338 kind, i, threadsDesc[i].threadName, 339 (int)frameStackSize, (int)frameCount); 340 nsk_jvmti_setFailStatus(); 341 } 342 343 /* find method on the stack */ 344 found = 0; 345 for (j = 0; j < frameStackSize; j++) { 346 NSK_DISPLAY3(" %d: methodID: 0x%p, location: %ld\n", 347 j, (void*)frameStack[j].method, 348 (long)frameStack[j].location); 349 /* check frame method */ 350 if (frameStack[j].method == NULL) { 351 NSK_COMPLAIN3("NULL methodID in stack for %s thread #%d (%s)\n", 352 kind, i, threadsDesc[i].threadName); 353 nsk_jvmti_setFailStatus(); 354 } else if (frameStack[j].method == threadsDesc[i].method) { 355 found++; 356 NSK_DISPLAY1(" found expected method: %s\n", 357 (void*)threadsDesc[i].methodName); 358 } 359 } 360 361 /* check if expected method found */ 362 if (found != 1) { 363 NSK_COMPLAIN5("Unexpected method frames on stack for %s thread #%d (%s):\n" 364 "# found frames: %d\n" 365 "# expected: %d\n", 366 kind, i, threadsDesc[i].threadName, 367 found, 1); 368 nsk_jvmti_setFailStatus(); 369 } 370 } 371 372 /* test may continue */ 373 return NSK_TRUE; 374 } 375 376 /** 377 * Clean data. 378 * - disable events 379 * - dispose global references to tested threads 380 */ 381 static int clean() { 382 int i; 383 384 NSK_DISPLAY0("Disable events\n"); 385 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL)) 386 return NSK_FALSE; 387 388 NSK_DISPLAY0("Dispose global references to threads\n"); 389 for (i = 0; i < THREADS_COUNT; i++) { 390 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].thread)); 391 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].cls)); 392 } 393 394 return NSK_TRUE; 395 } 396 397 /* ============================================================================= */ 398 399 /** 400 * COMPILED_METHOD_LOAD callback. 401 * - turn on flag that method is compiled 402 */ 403 JNIEXPORT void JNICALL 404 callbackCompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method, 405 jint code_size, const void* code_addr, 406 jint map_length, const jvmtiAddrLocationMap* map, 407 const void* compile_info) { 408 int i; 409 410 /* check if event is for tested method and turn flag on */ 411 for (i = 0; i < THREADS_COUNT; i++) { 412 if (threadsDesc[i].method == method) { 413 threadsDesc[i].methodCompiled = NSK_TRUE; 414 415 NSK_DISPLAY2(" COMPILED_METHOD_LOAD for method #%d (%s):\n", 416 i, threadsDesc[i].methodName); 417 NSK_DISPLAY1(" methodID: 0x%p\n", 418 (void*)threadsDesc[i].method); 419 NSK_DISPLAY1(" code_size: %d\n", 420 (int)code_size); 421 NSK_DISPLAY1(" map_length: %d\n", 422 (int)map_length); 423 break; 424 } 425 } 426 } 427 428 /** 429 * COMPILED_METHOD_UNLOAD callback. 430 * - turn off flag that method is compiled 431 */ 432 JNIEXPORT void JNICALL 433 callbackCompiledMethodUnload(jvmtiEnv* jvmti, jmethodID method, 434 const void* code_addr) { 435 int i; 436 437 /* check if event is for tested method and turn flag off */ 438 for (i = 0; i < THREADS_COUNT; i++) { 439 if (threadsDesc[i].method == method) { 440 threadsDesc[i].methodCompiled = NSK_FALSE; 441 442 NSK_DISPLAY2(" COMPILED_METHOD_UNLOAD for method #%d (%s):\n", 443 i, threadsDesc[i].methodName); 444 NSK_DISPLAY1(" methodID: 0x%p\n", 445 (void*)threadsDesc[i].method); 446 break; 447 } 448 } 449 } 450 451 /* ============================================================================= */ 452 453 volatile int testedThreadReady = NSK_FALSE; 454 volatile int testedThreadShouldFinish = NSK_FALSE; 455 456 /** Native running method in tested thread. */ 457 JNIEXPORT void JNICALL 458 Java_nsk_jvmti_scenarios_sampling_SP06_sp06t002ThreadRunningNative_testedMethod(JNIEnv* jni, 459 jobject obj, 460 jboolean simulate, 461 jint i) { 462 if (!simulate) { 463 volatile int k = 0, n = 1000; 464 465 /* run in a continous loop */ 466 testedThreadReady = NSK_TRUE; 467 while (!testedThreadShouldFinish) { 468 if (n <= 0) 469 n = 1000; 470 if (k >= n) 471 k = 0; 472 k++; 473 } 474 } 475 } 476 477 /* Wait for native method is running. */ 478 JNIEXPORT jboolean JNICALL 479 Java_nsk_jvmti_scenarios_sampling_SP06_sp06t002ThreadRunningNative_checkReady(JNIEnv* jni, 480 jobject obj) { 481 while (!testedThreadReady) { 482 nsk_jvmti_sleep(1000); 483 } 484 return testedThreadReady ? JNI_TRUE : JNI_FALSE; 485 } 486 487 /** Let native method to finish. */ 488 JNIEXPORT void JNICALL 489 Java_nsk_jvmti_scenarios_sampling_SP06_sp06t002ThreadRunningNative_letFinish(JNIEnv* jni, 490 jobject obj) { 491 testedThreadShouldFinish = NSK_TRUE; 492 } 493 494 /* ============================================================================= */ 495 496 /** Agent library initialization. */ 497 #ifdef STATIC_BUILD 498 JNIEXPORT jint JNICALL Agent_OnLoad_sp06t002(JavaVM *jvm, char *options, void *reserved) { 499 return Agent_Initialize(jvm, options, reserved); 500 } 501 JNIEXPORT jint JNICALL Agent_OnAttach_sp06t002(JavaVM *jvm, char *options, void *reserved) { 502 return Agent_Initialize(jvm, options, reserved); 503 } 504 JNIEXPORT jint JNI_OnLoad_sp06t002(JavaVM *jvm, char *options, void *reserved) { 505 return JNI_VERSION_1_8; 506 } 507 #endif 508 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 509 510 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 511 return JNI_ERR; 512 513 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 514 515 if (!NSK_VERIFY((jvmti = 516 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 517 return JNI_ERR; 518 519 { 520 jvmtiCapabilities caps; 521 memset(&caps, 0, sizeof(caps)); 522 caps.can_suspend = 1; 523 caps.can_generate_compiled_method_load_events = 1; 524 if (!NSK_JVMTI_VERIFY( 525 NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) 526 return JNI_ERR; 527 } 528 529 { 530 jvmtiEventCallbacks eventCallbacks; 531 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 532 eventCallbacks.CompiledMethodLoad = callbackCompiledMethodLoad; 533 eventCallbacks.CompiledMethodUnload = callbackCompiledMethodUnload; 534 if (!NSK_JVMTI_VERIFY( 535 NSK_CPP_STUB3(SetEventCallbacks, jvmti, 536 &eventCallbacks, sizeof(eventCallbacks)))) 537 return JNI_ERR; 538 } 539 540 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 541 return JNI_ERR; 542 543 return JNI_OK; 544 } 545 546 /* ============================================================================= */ 547 548 }