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(jvmti->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD))) { 147 nsk_jvmti_setFailStatus(); 148 return NSK_FALSE; 149 } 150 return NSK_TRUE; 151 } 152 153 /** 154 * Prepare data. 155 * - clean threads list 156 * - get all live threads 157 * - get threads name 158 * - find tested threads 159 * - make global refs 160 * - enable events 161 */ 162 static int prepare() { 163 jthread *allThreadsList = NULL; 164 jint allThreadsCount = 0; 165 int found = 0; 166 int i; 167 168 NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT); 169 170 /* clean threads list */ 171 for (i = 0; i < THREADS_COUNT; i++) { 172 threadsDesc[i].thread = (jthread)NULL; 173 threadsDesc[i].method = (jmethodID)NULL; 174 threadsDesc[i].location = NSK_JVMTI_INVALID_JLOCATION; 175 threadsDesc[i].methodCompiled = NSK_FALSE; 176 } 177 178 /* get all live threads */ 179 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&allThreadsCount, &allThreadsList))) 180 return NSK_FALSE; 181 182 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL)) 183 return NSK_FALSE; 184 185 /* find tested threads */ 186 for (i = 0; i < allThreadsCount; i++) { 187 jvmtiThreadInfo threadInfo; 188 189 if (!NSK_VERIFY(allThreadsList[i] != NULL)) 190 return NSK_FALSE; 191 192 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(allThreadsList[i], &threadInfo))) 193 return NSK_FALSE; 194 195 if (threadInfo.name != NULL) { 196 int j; 197 198 for (j = 0; j < THREADS_COUNT; j++) { 199 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) { 200 threadsDesc[j].thread = allThreadsList[i]; 201 NSK_DISPLAY3(" thread #%d (%s): 0x%p\n", 202 j, threadInfo.name, (void*)threadsDesc[j].thread); 203 } 204 } 205 } 206 } 207 208 /* deallocate all threads list */ 209 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)allThreadsList))) 210 return NSK_FALSE; 211 212 /* check if all tested threads found */ 213 found = 0; 214 for (i = 0; i < THREADS_COUNT; i++) { 215 if (threadsDesc[i].thread == NULL) { 216 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName); 217 } else { 218 found++; 219 } 220 } 221 222 if (found < THREADS_COUNT) 223 return NSK_FALSE; 224 225 /* get threads class and frame method */ 226 NSK_DISPLAY0("Find tested methods:\n"); 227 for (i = 0; i < THREADS_COUNT; i++) { 228 229 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = 230 jni->GetObjectClass(threadsDesc[i].thread)) != NULL)) 231 return NSK_FALSE; 232 233 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method = 234 jni->GetMethodID(threadsDesc[i].cls, threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL)) 235 return NSK_FALSE; 236 237 NSK_DISPLAY4(" thread #%d (%s): 0x%p (%s)\n", 238 i, threadsDesc[i].threadName, 239 (void*)threadsDesc[i].method, 240 threadsDesc[i].methodName); 241 } 242 243 /* make global refs */ 244 for (i = 0; i < THREADS_COUNT; i++) { 245 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread) 246 jni->NewGlobalRef(threadsDesc[i].thread)) != NULL)) 247 return NSK_FALSE; 248 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass) 249 jni->NewGlobalRef(threadsDesc[i].cls)) != NULL)) 250 return NSK_FALSE; 251 } 252 253 NSK_DISPLAY0("Enable tested events\n"); 254 if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL)) 255 return NSK_FALSE; 256 257 return NSK_TRUE; 258 } 259 260 /** 261 * Suspend or resume tested threads. 262 */ 263 static int suspendThreadsIndividually(int suspend) { 264 int i; 265 266 for (i = 0; i < THREADS_COUNT; i++) { 267 if (suspend) { 268 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsDesc[i].threadName); 269 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(threadsDesc[i].thread))) 270 nsk_jvmti_setFailStatus(); 271 } else { 272 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsDesc[i].threadName); 273 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(threadsDesc[i].thread))) 274 nsk_jvmti_setFailStatus(); 275 } 276 } 277 return NSK_TRUE; 278 } 279 280 /** 281 * Testcase: check tested threads. 282 * - call GetFrameCount() and then GetStackTrace() 283 * - for each stack frame of common depth GetFrameLocation() 284 * - compare frame ifno returned by GetFrameLocation() and GetStackTrace() 285 * - find expected frame for tested method 286 * 287 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break. 288 */ 289 static int checkThreads(int suspended, const char* kind0) { 290 char kind[256] = ""; 291 int i; 292 293 /* check each thread */ 294 for (i = 0; i < THREADS_COUNT; i++) { 295 jint frameCount = 0; 296 jint frameStackSize = 0; 297 jvmtiFrameInfo frameStack[MAX_STACK_SIZE]; 298 int commonDepth = 0; 299 int found = 0; 300 int j; 301 302 /* make proper kind */ 303 strcpy(kind, threadsDesc[i].methodCompiled ? "compiled " : "not compiled "); 304 strcat(kind, kind0); 305 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsDesc[i].threadName); 306 307 /* get frame count */ 308 if (!NSK_JVMTI_VERIFY(jvmti->GetFrameCount(threadsDesc[i].thread, &frameCount))) { 309 nsk_jvmti_setFailStatus(); 310 return NSK_TRUE; 311 } 312 NSK_DISPLAY1(" frameCount: %d\n", (int)frameCount); 313 314 /* get stack trace */ 315 if (!NSK_JVMTI_VERIFY( 316 jvmti->GetStackTrace(threadsDesc[i].thread, 0, MAX_STACK_SIZE, frameStack, &frameStackSize))) { 317 nsk_jvmti_setFailStatus(); 318 return NSK_TRUE; 319 } 320 NSK_DISPLAY1(" stack depth: %d\n", (int)frameStackSize); 321 322 commonDepth = (frameCount < frameStackSize ? frameCount : frameStackSize); 323 NSK_DISPLAY1(" common: %d\n", (int)commonDepth); 324 325 /* check first commonDepth frames and find expected method there */ 326 found = 0; 327 for (j = 0; j < commonDepth; j++) { 328 jmethodID qMethod = (jmethodID)NULL; 329 jlocation qLocation = NSK_JVMTI_INVALID_JLOCATION; 330 331 NSK_DISPLAY3(" %d frame: method: 0x%p, location: %ld\n", 332 j, (void*)frameStack[j].method, 333 (long)frameStack[j].location); 334 /* query frame location */ 335 if (!NSK_JVMTI_VERIFY( 336 jvmti->GetFrameLocation(threadsDesc[i].thread, j, &qMethod, &qLocation))) { 337 nsk_jvmti_setFailStatus(); 338 continue; 339 } 340 341 NSK_DISPLAY2(" queried: method: 0x%p, location: %ld\n", 342 (void*)qMethod, (long)qLocation); 343 344 /* check frame equalaty */ 345 if (frameStack[j].method != qMethod) { 346 NSK_COMPLAIN6("Different method in stack frame #%d for %s thread #%d (%s):\n" 347 "# GetStackTrace(): 0x%p\n" 348 "# GetFrameLocation(): 0x%p\n", 349 j, kind, i, threadsDesc[i].threadName, 350 (void*)frameStack[j].method, (void*)qMethod); 351 nsk_jvmti_setFailStatus(); 352 } 353 if (frameStack[j].location != qLocation) { 354 NSK_COMPLAIN6("Different location in stack frame #%d for %s thread #%d (%s):\n" 355 "# GetStackTrace(): %ld\n" 356 "# GetFrameLocation(): %ld\n", 357 j, kind, i, threadsDesc[i].threadName, 358 (long)frameStack[j].location, (long)qLocation); 359 nsk_jvmti_setFailStatus(); 360 } 361 362 /* find expected method */ 363 if (frameStack[j].method == threadsDesc[i].method) { 364 found++; 365 NSK_DISPLAY1(" found expected method: %s\n", 366 threadsDesc[i].methodName); 367 } 368 } 369 370 /* check if expected method frame found */ 371 if (found <= 0) { 372 NSK_COMPLAIN3("No expected method frame for %s thread #%d (%s)\n", 373 kind, i, threadsDesc[i].threadName); 374 nsk_jvmti_setFailStatus(); 375 } 376 } 377 378 /* test may continue */ 379 return NSK_TRUE; 380 } 381 382 /** 383 * Clean data. 384 * - disable events 385 * - dispose global references to tested threads 386 */ 387 static int clean() { 388 int i; 389 390 NSK_DISPLAY0("Disable events\n"); 391 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL)) 392 return NSK_FALSE; 393 394 NSK_DISPLAY0("Dispose global references to threads\n"); 395 for (i = 0; i < THREADS_COUNT; i++) { 396 NSK_TRACE(jni->DeleteGlobalRef(threadsDesc[i].thread)); 397 NSK_TRACE(jni->DeleteGlobalRef(threadsDesc[i].cls)); 398 } 399 400 return NSK_TRUE; 401 } 402 403 /* ============================================================================= */ 404 405 /** 406 * COMPILED_METHOD_LOAD callback. 407 * - turn on flag that method is compiled 408 */ 409 JNIEXPORT void JNICALL 410 callbackCompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method, 411 jint code_size, const void* code_addr, 412 jint map_length, const jvmtiAddrLocationMap* map, 413 const void* compile_info) { 414 int i; 415 416 /* check if event is for tested method and turn flag on */ 417 for (i = 0; i < THREADS_COUNT; i++) { 418 if (threadsDesc[i].method == method) { 419 threadsDesc[i].methodCompiled = NSK_TRUE; 420 421 NSK_DISPLAY2(" COMPILED_METHOD_LOAD for method #%d (%s):\n", 422 i, threadsDesc[i].methodName); 423 NSK_DISPLAY1(" methodID: 0x%p\n", 424 (void*)threadsDesc[i].method); 425 NSK_DISPLAY1(" code_size: %d\n", 426 (int)code_size); 427 NSK_DISPLAY1(" map_length: %d\n", 428 (int)map_length); 429 break; 430 } 431 } 432 } 433 434 /** 435 * COMPILED_METHOD_UNLOAD callback. 436 * - turn off flag that method is compiled 437 */ 438 JNIEXPORT void JNICALL 439 callbackCompiledMethodUnload(jvmtiEnv* jvmti, jmethodID method, 440 const void* code_addr) { 441 int i; 442 443 /* check if event is for tested method and turn flag off */ 444 for (i = 0; i < THREADS_COUNT; i++) { 445 if (threadsDesc[i].method == method) { 446 threadsDesc[i].methodCompiled = NSK_FALSE; 447 448 NSK_DISPLAY2(" COMPILED_METHOD_UNLOAD for method #%d (%s):\n", 449 i, threadsDesc[i].methodName); 450 NSK_DISPLAY1(" methodID: 0x%p\n", 451 (void*)threadsDesc[i].method); 452 break; 453 } 454 } 455 } 456 457 /* ============================================================================= */ 458 459 volatile int testedThreadReady = NSK_FALSE; 460 volatile int testedThreadShouldFinish = NSK_FALSE; 461 462 /** Native running method in tested thread */ 463 JNIEXPORT void JNICALL 464 Java_nsk_jvmti_scenarios_sampling_SP06_sp06t003ThreadRunningNative_testedMethod(JNIEnv* jni, 465 jobject obj, 466 jboolean simulate, 467 jint i) { 468 if (!simulate) { 469 volatile int k = 0, n = 1000; 470 471 /* run in a continous loop */ 472 testedThreadReady = NSK_TRUE; 473 while (!testedThreadShouldFinish) { 474 if (n <= 0) 475 n = 1000; 476 if (k >= n) 477 k = 0; 478 k++; 479 } 480 } 481 } 482 483 /** Wait for native method is running. */ 484 JNIEXPORT jboolean JNICALL 485 Java_nsk_jvmti_scenarios_sampling_SP06_sp06t003ThreadRunningNative_checkReady(JNIEnv* jni, 486 jobject obj) { 487 while (!testedThreadReady) { 488 nsk_jvmti_sleep(1000); 489 } 490 return testedThreadReady ? JNI_TRUE : JNI_FALSE; 491 } 492 493 /** Let native method to finish. */ 494 JNIEXPORT void JNICALL 495 Java_nsk_jvmti_scenarios_sampling_SP06_sp06t003ThreadRunningNative_letFinish(JNIEnv* jni, 496 jobject obj) { 497 testedThreadShouldFinish = NSK_TRUE; 498 } 499 500 /* ============================================================================= */ 501 502 /** Agent library initialization. */ 503 #ifdef STATIC_BUILD 504 JNIEXPORT jint JNICALL Agent_OnLoad_sp06t003(JavaVM *jvm, char *options, void *reserved) { 505 return Agent_Initialize(jvm, options, reserved); 506 } 507 JNIEXPORT jint JNICALL Agent_OnAttach_sp06t003(JavaVM *jvm, char *options, void *reserved) { 508 return Agent_Initialize(jvm, options, reserved); 509 } 510 JNIEXPORT jint JNI_OnLoad_sp06t003(JavaVM *jvm, char *options, void *reserved) { 511 return JNI_VERSION_1_8; 512 } 513 #endif 514 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 515 516 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 517 return JNI_ERR; 518 519 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 520 521 if (!NSK_VERIFY((jvmti = 522 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 523 return JNI_ERR; 524 525 { 526 jvmtiCapabilities caps; 527 memset(&caps, 0, sizeof(caps)); 528 caps.can_suspend = 1; 529 caps.can_generate_compiled_method_load_events = 1; 530 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) 531 return JNI_ERR; 532 } 533 534 { 535 jvmtiEventCallbacks eventCallbacks; 536 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 537 eventCallbacks.CompiledMethodLoad = callbackCompiledMethodLoad; 538 eventCallbacks.CompiledMethodUnload = callbackCompiledMethodUnload; 539 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) 540 return JNI_ERR; 541 } 542 543 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 544 return JNI_ERR; 545 546 return JNI_OK; 547 } 548 549 /* ============================================================================= */ 550 551 }