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