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 <string.h> 25 #include "jvmti.h" 26 #include "agent_common.h" 27 #include "jni_tools.h" 28 #include "jvmti_tools.h" 29 #include "JVMTITools.h" 30 31 extern "C" { 32 33 /* ============================================================================= */ 34 35 /* scaffold objects */ 36 static jvmtiEnv *jvmti = NULL; 37 static jlong timeout = 0; 38 static jrawMonitorID syncLock = NULL; 39 40 /* constant names */ 41 #define STEP_NUMBER 3 42 #define OBJECT_NUMBER 100 43 #define JVMTI_EVENT_COUNT (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1) 44 45 static int eventCount[JVMTI_EVENT_COUNT]; 46 static int newEventCount[JVMTI_EVENT_COUNT]; 47 static int singleStepIdx = JVMTI_EVENT_SINGLE_STEP - JVMTI_MIN_EVENT_TYPE_VAL; 48 49 /* ============================================================================= */ 50 51 52 static void 53 showEventStatistics(int step) { 54 int i; 55 const char* str; 56 int *currentCounts = (step == 1) ? &eventCount[0] : &newEventCount[0]; 57 58 NSK_DISPLAY0("\n"); 59 NSK_DISPLAY1("Event statistics for %d step:\n", step); 60 NSK_DISPLAY0("-----------------------------\n"); 61 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 62 if (currentCounts[i] > 0) { 63 str = TranslateEvent((jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL)); 64 NSK_DISPLAY2("%-40s %7d\n", str, currentCounts[i]); 65 } 66 } 67 } 68 69 /* ========================================================================== */ 70 71 int checkEvents(int step) { 72 int i; 73 jvmtiEvent curr; 74 int result = NSK_TRUE; 75 int *currentCounts; 76 int isExpected = 0; 77 78 switch (step) { 79 case 1: 80 currentCounts = &eventCount[0]; 81 break; 82 83 case 2: 84 case 3: 85 currentCounts = &newEventCount[0]; 86 break; 87 88 default: 89 NSK_COMPLAIN1("Unexpected step no: %d\n", step); 90 return NSK_FALSE; 91 } 92 93 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 94 95 curr = (jvmtiEvent) (i + JVMTI_MIN_EVENT_TYPE_VAL); 96 97 switch (step) { 98 case 1: 99 isExpected = ((curr == JVMTI_EVENT_VM_INIT) 100 || (curr == JVMTI_EVENT_SINGLE_STEP)); 101 break; 102 103 case 2: 104 isExpected = (curr == JVMTI_EVENT_SINGLE_STEP); 105 break; 106 107 case 3: 108 isExpected = (curr == JVMTI_EVENT_VM_DEATH); 109 break; 110 } 111 112 if (isExpected) { 113 if (currentCounts[i] < 0) { 114 NSK_COMPLAIN2("Unexpected events number %7d for %s\n\texpected value must be greater than 1\n", 115 currentCounts[i], 116 TranslateEvent(curr)); 117 result = NSK_FALSE; 118 } 119 } else { 120 121 if (currentCounts[i] > 0) { 122 NSK_COMPLAIN2("Unexpected event %s was sent %d times\n", 123 TranslateEvent(curr), 124 currentCounts[i]); 125 result = NSK_FALSE; 126 } 127 } 128 } 129 130 return result; 131 } 132 133 static void 134 changeCount(jvmtiEvent event, int *currentCounts) { 135 136 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock))) 137 nsk_jvmti_setFailStatus(); 138 139 currentCounts[event - JVMTI_MIN_EVENT_TYPE_VAL]++; 140 141 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock))) 142 nsk_jvmti_setFailStatus(); 143 144 } 145 146 /* ============================================================================= */ 147 148 /* callbacks */ 149 JNIEXPORT void JNICALL 150 cbVMInit(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread) { 151 changeCount(JVMTI_EVENT_VM_INIT, &eventCount[0]); 152 } 153 154 JNIEXPORT void JNICALL 155 cbVMDeath(jvmtiEnv* jvmti, JNIEnv* jni_env) { 156 changeCount(JVMTI_EVENT_VM_DEATH, &newEventCount[0]); 157 showEventStatistics(STEP_NUMBER); 158 if (!checkEvents(STEP_NUMBER)) 159 nsk_jvmti_setFailStatus(); 160 161 if (!NSK_JVMTI_VERIFY( 162 NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock))) 163 nsk_jvmti_setFailStatus(); 164 165 } 166 167 void JNICALL 168 cbException(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 169 jmethodID method, jlocation location, jobject exception, 170 jmethodID catch_method, jlocation catch_location) { 171 changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]); 172 } 173 174 void JNICALL 175 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 176 jmethodID method, jlocation location, jobject exception) { 177 changeCount(JVMTI_EVENT_EXCEPTION_CATCH, &eventCount[0]); 178 } 179 180 void JNICALL 181 cbSingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 182 jmethodID method, jlocation location) { 183 char *name; 184 char *sign; 185 char *genc; 186 187 if (!NSK_JVMTI_VERIFY( 188 NSK_CPP_STUB5( 189 GetMethodName, jvmti_env, method, &name, &sign, &genc))) { 190 nsk_jvmti_setFailStatus(); 191 return; 192 } 193 194 if (!strncmp(name,"javaMethod", 8)) { 195 NSK_DISPLAY2("\tMethod: %s, location: %lld\n", 196 name, location); 197 changeCount(JVMTI_EVENT_SINGLE_STEP, &eventCount[0]); 198 } 199 200 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 201 jvmti_env, (unsigned char*)name))) { 202 nsk_jvmti_setFailStatus(); 203 } 204 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 205 jvmti_env, (unsigned char*)sign))) { 206 nsk_jvmti_setFailStatus(); 207 } 208 if (genc != NULL) 209 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 210 jvmti_env, (unsigned char*)genc))) { 211 nsk_jvmti_setFailStatus(); 212 } 213 } 214 215 void JNICALL 216 cbNewSingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 217 jmethodID method, jlocation location) { 218 char *name; 219 char *sign; 220 char *genc; 221 222 if (!NSK_JVMTI_VERIFY( 223 NSK_CPP_STUB5( 224 GetMethodName, jvmti_env, method, &name, &sign, &genc))) { 225 nsk_jvmti_setFailStatus(); 226 return; 227 } 228 229 if (!strncmp(name,"javaMethod", 8)) { 230 NSK_DISPLAY2("\tMethod: %s, location: %lld\n", 231 name, location); 232 changeCount(JVMTI_EVENT_SINGLE_STEP, &newEventCount[0]); 233 } 234 235 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 236 jvmti_env, (unsigned char*)name))) { 237 nsk_jvmti_setFailStatus(); 238 } 239 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 240 jvmti_env, (unsigned char*)sign))) { 241 nsk_jvmti_setFailStatus(); 242 } 243 if (genc != NULL) 244 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 245 jvmti_env, (unsigned char*)genc))) { 246 nsk_jvmti_setFailStatus(); 247 } 248 } 249 250 void JNICALL 251 cbFramePop(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 252 jmethodID method, jboolean was_popped_by_exception) { 253 changeCount(JVMTI_EVENT_FRAME_POP, &eventCount[0]); 254 } 255 256 void JNICALL 257 cbBreakpoint(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 258 jmethodID method, jlocation location) { 259 changeCount(JVMTI_EVENT_BREAKPOINT, &eventCount[0]); 260 } 261 262 void JNICALL 263 cbFieldAccess(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 264 jmethodID method, jlocation location, jclass field_klass, 265 jobject object, jfieldID field) { 266 changeCount(JVMTI_EVENT_FIELD_ACCESS, &eventCount[0]); 267 } 268 269 void JNICALL 270 cbFieldModification(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 271 jmethodID method, jlocation location, jclass field_klass, 272 jobject object, jfieldID field, char signature_type, 273 jvalue new_value) { 274 changeCount(JVMTI_EVENT_FIELD_MODIFICATION, &eventCount[0]); 275 } 276 277 void JNICALL 278 cbMethodEntry(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 279 jmethodID method) { 280 changeCount(JVMTI_EVENT_METHOD_ENTRY, &eventCount[0]); 281 } 282 283 void JNICALL 284 cbMethodExit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 285 jmethodID method, jboolean was_popped_by_exception, 286 jvalue return_value) { 287 changeCount(JVMTI_EVENT_METHOD_EXIT, &eventCount[0]); 288 } 289 290 void JNICALL 291 cbNativeMethodBind(jvmtiEnv *jvmti_env, JNIEnv* jni_env,jthread thread, 292 jmethodID method, void* address, void** new_address_ptr) { 293 changeCount(JVMTI_EVENT_NATIVE_METHOD_BIND, &eventCount[0]); 294 } 295 296 void JNICALL 297 cbMonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 298 jobject object, jlong tout) { 299 300 changeCount(JVMTI_EVENT_MONITOR_WAIT, &eventCount[0]); 301 } 302 303 void JNICALL 304 cbMonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 305 jobject object, jboolean timed_out) { 306 307 changeCount(JVMTI_EVENT_MONITOR_WAITED, &eventCount[0]); 308 } 309 310 JNIEXPORT void JNICALL 311 cbMonitorContendedEnter(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, 312 jobject object) { 313 314 changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, &eventCount[0]); 315 } 316 317 void JNICALL 318 cbMonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 319 jobject object) { 320 321 changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, &eventCount[0]); 322 } 323 324 void JNICALL 325 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size, 326 const void* code_addr, jint map_length, 327 const jvmtiAddrLocationMap* map, const void* compile_info) { 328 changeCount(JVMTI_EVENT_COMPILED_METHOD_LOAD, &eventCount[0]); 329 } 330 331 void JNICALL 332 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method, 333 const void* code_addr) { 334 changeCount(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, &eventCount[0]); 335 } 336 337 void JNICALL 338 cbGarbageCollectionStart(jvmtiEnv *jvmti_env) { 339 changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_START, &eventCount[0]); 340 } 341 342 void JNICALL 343 cbGarbageCollectionFinish(jvmtiEnv *jvmti_env) { 344 changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, &eventCount[0]); 345 } 346 347 void JNICALL 348 cbObjectFree(jvmtiEnv *jvmti_env, jlong tag) { 349 350 changeCount(JVMTI_EVENT_OBJECT_FREE, &eventCount[0]); 351 if (tag < 1 || tag > OBJECT_NUMBER) { 352 NSK_COMPLAIN1("Unexpected tag value %lld\n", tag); 353 nsk_jvmti_setFailStatus(); 354 } 355 } 356 357 void JNICALL 358 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 359 jobject object, jclass object_klass, jlong size) { 360 361 changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]); 362 } 363 364 /* ============================================================================= */ 365 366 static int enableEvent(jvmtiEvent event) { 367 368 if (nsk_jvmti_isOptionalEvent(event) 369 && (event != JVMTI_EVENT_SINGLE_STEP)) { 370 if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, 371 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, 372 JVMTI_ENABLE, event, NULL))) { 373 NSK_COMPLAIN1("Unexpected error enabling %s\n", 374 TranslateEvent(event)); 375 return NSK_FALSE; 376 } 377 } else { 378 if (!NSK_JVMTI_VERIFY( 379 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, 380 JVMTI_ENABLE, event, NULL))) { 381 NSK_COMPLAIN1("Unexpected error enabling %s\n", 382 TranslateEvent(event)); 383 return NSK_FALSE; 384 } 385 } 386 387 return NSK_TRUE; 388 } 389 390 /** 391 * Enable or disable tested events. 392 */ 393 static int enableEventList() { 394 395 int i, result; 396 397 result = enableEvent(JVMTI_EVENT_VM_INIT); 398 399 result = result && enableEvent(JVMTI_EVENT_VM_DEATH); 400 401 /* enabling optional events */ 402 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 403 jvmtiEvent event = (jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL); 404 405 if (nsk_jvmti_isOptionalEvent(event)) 406 result = result && enableEvent(event); 407 } 408 409 if (result == NSK_FALSE) { 410 nsk_jvmti_setFailStatus(); 411 return NSK_FALSE; 412 } 413 414 return NSK_TRUE; 415 } 416 417 /* ============================================================================= */ 418 419 static int 420 setCallBacks(int step) { 421 422 int i; 423 424 jvmtiEventCallbacks eventCallbacks; 425 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 426 427 switch (step) { 428 case 1: 429 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 430 eventCount[i] = 0; 431 } 432 433 eventCallbacks.VMInit = cbVMInit; 434 eventCallbacks.Exception = cbException; 435 eventCallbacks.ExceptionCatch = cbExceptionCatch; 436 eventCallbacks.SingleStep = cbSingleStep; 437 eventCallbacks.FramePop = cbFramePop; 438 eventCallbacks.Breakpoint = cbBreakpoint; 439 eventCallbacks.FieldAccess = cbFieldAccess; 440 eventCallbacks.FieldModification = cbFieldModification; 441 eventCallbacks.MethodEntry = cbMethodEntry; 442 eventCallbacks.MethodExit = cbMethodExit; 443 eventCallbacks.NativeMethodBind = cbNativeMethodBind; 444 eventCallbacks.CompiledMethodLoad = cbCompiledMethodLoad; 445 eventCallbacks.CompiledMethodUnload = cbCompiledMethodUnload; 446 eventCallbacks.MonitorWait = cbMonitorWait; 447 eventCallbacks.MonitorWaited = cbMonitorWaited; 448 eventCallbacks.MonitorContendedEnter = cbMonitorContendedEnter; 449 eventCallbacks.MonitorContendedEntered = cbMonitorContendedEntered; 450 eventCallbacks.GarbageCollectionStart = cbGarbageCollectionStart; 451 eventCallbacks.GarbageCollectionFinish = cbGarbageCollectionFinish; 452 eventCallbacks.ObjectFree = cbObjectFree; 453 eventCallbacks.VMObjectAlloc = cbVMObjectAlloc; 454 break; 455 456 case 2: 457 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 458 newEventCount[i] = 0; 459 } 460 461 eventCallbacks.SingleStep = cbNewSingleStep; 462 break; 463 464 case 3: 465 /* comparing number of JVMTI_EVENT_SINGLE_STEP events for steps*/ 466 if (eventCount[singleStepIdx] != newEventCount[singleStepIdx]) { 467 NSK_COMPLAIN2("Number of JVMTI_EVENT_SINGLE_STEP events must be the same\n\t1-st callback=%d; 2-nd callback=%d\n", 468 eventCount[singleStepIdx], 469 newEventCount[singleStepIdx]); 470 } 471 472 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 473 newEventCount[i] = 0; 474 } 475 476 eventCallbacks.VMDeath = cbVMDeath; 477 break; 478 479 } 480 if (!NSK_JVMTI_VERIFY( 481 NSK_CPP_STUB3(SetEventCallbacks, jvmti, 482 &eventCallbacks, 483 sizeof(eventCallbacks)))) 484 return NSK_FALSE; 485 486 return NSK_TRUE; 487 } 488 489 /* ============================================================================= */ 490 491 /** Agent algorithm. */ 492 static void JNICALL 493 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) { 494 495 int i; 496 497 for (i = 1; i <= STEP_NUMBER; i++) { 498 499 if (!nsk_jvmti_waitForSync(timeout)) 500 return; 501 502 NSK_DISPLAY0("Check received events\n"); 503 504 if (i < STEP_NUMBER) { 505 showEventStatistics(i); 506 if (!checkEvents(i)) 507 nsk_jvmti_setFailStatus(); 508 509 if (!setCallBacks(i + 1)) { 510 return; 511 } 512 } 513 514 if (!nsk_jvmti_resumeSync()) 515 return; 516 } 517 518 } 519 520 /* ============================================================================= */ 521 522 /** Agent library initialization. */ 523 #ifdef STATIC_BUILD 524 JNIEXPORT jint JNICALL Agent_OnLoad_em02t007(JavaVM *jvm, char *options, void *reserved) { 525 return Agent_Initialize(jvm, options, reserved); 526 } 527 JNIEXPORT jint JNICALL Agent_OnAttach_em02t007(JavaVM *jvm, char *options, void *reserved) { 528 return Agent_Initialize(jvm, options, reserved); 529 } 530 JNIEXPORT jint JNI_OnLoad_em02t007(JavaVM *jvm, char *options, void *reserved) { 531 return JNI_VERSION_1_8; 532 } 533 #endif 534 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 535 536 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 537 return JNI_ERR; 538 539 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 540 541 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 542 return JNI_ERR; 543 544 if (!NSK_JVMTI_VERIFY( 545 NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) { 546 nsk_jvmti_setFailStatus(); 547 return JNI_ERR; 548 } 549 550 { 551 jvmtiCapabilities caps; 552 memset(&caps, 0, sizeof(caps)); 553 554 caps.can_generate_single_step_events = 1; 555 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) 556 return JNI_ERR; 557 } 558 559 if (!setCallBacks(1)) { 560 return JNI_ERR; 561 } 562 563 if (!enableEventList()) { 564 return JNI_ERR; 565 } 566 567 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 568 return JNI_ERR; 569 570 return JNI_OK; 571 } 572 573 /* ============================================================================= */ 574 575 576 }