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