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 CLASS_NAME "nsk/jvmti/scenarios/events/EM02/em02t010" 43 #define FIELD_ACC_NAME "testedField_acc" 44 #define FIELD_MOD_NAME "testedField_mod" 45 #define JVMTI_EVENT_COUNT (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1) 46 #define NUMBER_OF_INVOCATIONS 1000 47 48 static int eventCount[JVMTI_EVENT_COUNT]; 49 static int newEventCount[JVMTI_EVENT_COUNT]; 50 51 /* ============================================================================= */ 52 53 static void 54 showEventStatistics(int step) { 55 int i; 56 const char* str; 57 int *currentCounts = (step == 1) ? &eventCount[0] : &newEventCount[0]; 58 59 NSK_DISPLAY0("\n"); 60 NSK_DISPLAY1("Event statistics for %d step:\n", step); 61 NSK_DISPLAY0("-----------------------------\n"); 62 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 63 if (currentCounts[i] > 0) { 64 str = TranslateEvent((jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL)); 65 NSK_DISPLAY2("%-40s %7d\n", str, currentCounts[i]); 66 } 67 } 68 } 69 70 /* ========================================================================== */ 71 72 int checkEvents(int step) { 73 int i; 74 jvmtiEvent curr; 75 int result = NSK_TRUE; 76 int *currentCounts; 77 int isExpected = 0; 78 79 switch (step) { 80 case 1: 81 currentCounts = &eventCount[0]; 82 break; 83 84 case 2: 85 case 3: 86 currentCounts = &newEventCount[0]; 87 break; 88 89 default: 90 NSK_COMPLAIN1("Unexpected step no: %d\n", step); 91 return NSK_FALSE; 92 } 93 94 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 95 96 curr = (jvmtiEvent) (i + JVMTI_MIN_EVENT_TYPE_VAL); 97 98 switch (step) { 99 case 1: 100 isExpected = ((curr == JVMTI_EVENT_VM_INIT) 101 || (curr == JVMTI_EVENT_FIELD_MODIFICATION) 102 || (curr == JVMTI_EVENT_FIELD_ACCESS)); 103 break; 104 105 case 2: 106 isExpected = ((curr == JVMTI_EVENT_FIELD_MODIFICATION) 107 || (curr == JVMTI_EVENT_FIELD_ACCESS)); 108 break; 109 110 case 3: 111 isExpected = (curr == JVMTI_EVENT_VM_DEATH); 112 break; 113 } 114 115 if (isExpected) { 116 if (curr == JVMTI_EVENT_FIELD_MODIFICATION || 117 curr == JVMTI_EVENT_FIELD_ACCESS) { 118 if (currentCounts[i] != NUMBER_OF_INVOCATIONS) { 119 NSK_COMPLAIN3("Unexpected number of %s events %d, expected value is %d\n", 120 TranslateEvent(curr), 121 currentCounts[i], 122 NUMBER_OF_INVOCATIONS); 123 result = NSK_FALSE; 124 } 125 } else { 126 if (currentCounts[i] < 1) { 127 NSK_COMPLAIN2("Unexpected events number %7d for %s\n\texpected value must be greater than 1\n", 128 currentCounts[i], 129 TranslateEvent(curr)); 130 result = NSK_FALSE; 131 } 132 } 133 134 } else { 135 136 if (currentCounts[i] > 0) { 137 NSK_COMPLAIN2("Unexpected event %s was sent %d times\n", 138 TranslateEvent(curr), 139 currentCounts[i]); 140 result = NSK_FALSE; 141 } 142 } 143 } 144 145 return result; 146 } 147 148 static void 149 changeCount(jvmtiEvent event, int *currentCounts) { 150 151 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock))) 152 nsk_jvmti_setFailStatus(); 153 154 currentCounts[event - JVMTI_MIN_EVENT_TYPE_VAL]++; 155 156 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock))) 157 nsk_jvmti_setFailStatus(); 158 159 } 160 161 /* ============================================================================= */ 162 163 /* callbacks */ 164 JNIEXPORT void JNICALL 165 cbVMInit(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread) { 166 changeCount(JVMTI_EVENT_VM_INIT, &eventCount[0]); 167 } 168 169 JNIEXPORT void JNICALL 170 cbVMDeath(jvmtiEnv* jvmti, JNIEnv* jni_env) { 171 changeCount(JVMTI_EVENT_VM_DEATH, &newEventCount[0]); 172 showEventStatistics(STEP_NUMBER); 173 if (!checkEvents(STEP_NUMBER)) 174 nsk_jvmti_setFailStatus(); 175 176 if (!NSK_JVMTI_VERIFY( 177 NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock))) 178 nsk_jvmti_setFailStatus(); 179 180 } 181 182 void JNICALL 183 cbException(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 184 jmethodID method, jlocation location, jobject exception, 185 jmethodID catch_method, jlocation catch_location) { 186 187 changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]); 188 } 189 190 void JNICALL 191 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 192 jmethodID method, jlocation location, jobject exception) { 193 194 changeCount(JVMTI_EVENT_EXCEPTION_CATCH, &eventCount[0]); 195 } 196 197 void JNICALL 198 cbSingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 199 jmethodID method, jlocation location) { 200 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 221 changeCount(JVMTI_EVENT_FIELD_ACCESS, &eventCount[0]); 222 } 223 224 void JNICALL 225 cbFieldModification(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 226 jmethodID method, jlocation location, jclass field_klass, 227 jobject object, jfieldID field, char signature_type, 228 jvalue new_value) { 229 230 changeCount(JVMTI_EVENT_FIELD_MODIFICATION, &eventCount[0]); 231 } 232 233 void JNICALL 234 cbNewFieldAccess(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 235 jmethodID method, jlocation location, jclass field_klass, 236 jobject object, jfieldID field) { 237 238 changeCount(JVMTI_EVENT_FIELD_ACCESS, &newEventCount[0]); 239 } 240 241 void JNICALL 242 cbNewFieldModification(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 243 jmethodID method, jlocation location, jclass field_klass, 244 jobject object, jfieldID field, char signature_type, 245 jvalue new_value) { 246 247 changeCount(JVMTI_EVENT_FIELD_MODIFICATION, &newEventCount[0]); 248 } 249 250 void JNICALL 251 cbMethodEntry(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 252 jmethodID method) { 253 254 changeCount(JVMTI_EVENT_METHOD_ENTRY, &eventCount[0]); 255 } 256 257 void JNICALL 258 cbMethodExit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 259 jmethodID method, jboolean was_popped_by_exception, 260 jvalue return_value) { 261 262 changeCount(JVMTI_EVENT_METHOD_EXIT, &eventCount[0]); 263 } 264 265 void JNICALL 266 cbNativeMethodBind(jvmtiEnv *jvmti_env, JNIEnv* jni_env,jthread thread, 267 jmethodID method, void* address, void** new_address_ptr) { 268 changeCount(JVMTI_EVENT_NATIVE_METHOD_BIND, &eventCount[0]); 269 } 270 271 void JNICALL 272 cbMonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 273 jobject object, jlong tout) { 274 275 changeCount(JVMTI_EVENT_MONITOR_WAIT, &eventCount[0]); 276 } 277 278 void JNICALL 279 cbMonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 280 jobject object, jboolean timed_out) { 281 282 changeCount(JVMTI_EVENT_MONITOR_WAITED, &eventCount[0]); 283 } 284 285 JNIEXPORT void JNICALL 286 cbMonitorContendedEnter(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, 287 jobject object) { 288 289 changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, &eventCount[0]); 290 } 291 292 void JNICALL 293 cbMonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 294 jobject object) { 295 296 changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, &eventCount[0]); 297 } 298 299 void JNICALL 300 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size, 301 const void* code_addr, jint map_length, 302 const jvmtiAddrLocationMap* map, const void* compile_info) { 303 changeCount(JVMTI_EVENT_COMPILED_METHOD_LOAD, &eventCount[0]); 304 } 305 306 void JNICALL 307 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method, 308 const void* code_addr) { 309 changeCount(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, &eventCount[0]); 310 } 311 312 void JNICALL 313 cbGarbageCollectionStart(jvmtiEnv *jvmti_env) { 314 changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_START, &eventCount[0]); 315 } 316 317 void JNICALL 318 cbGarbageCollectionFinish(jvmtiEnv *jvmti_env) { 319 changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, &eventCount[0]); 320 } 321 322 void JNICALL 323 cbObjectFree(jvmtiEnv *jvmti_env, jlong tag) { 324 325 changeCount(JVMTI_EVENT_OBJECT_FREE, &eventCount[0]); 326 } 327 328 void JNICALL 329 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 330 jobject object, jclass object_klass, jlong size) { 331 332 changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]); 333 } 334 335 /* ============================================================================= */ 336 337 static int enableEvent(jvmtiEvent event) { 338 339 if (nsk_jvmti_isOptionalEvent(event) 340 && (event != JVMTI_EVENT_FIELD_MODIFICATION) 341 && (event != JVMTI_EVENT_FIELD_ACCESS)) { 342 if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, 343 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, 344 JVMTI_ENABLE, event, NULL))) { 345 NSK_COMPLAIN1("Unexpected error enabling %s\n", 346 TranslateEvent(event)); 347 return NSK_FALSE; 348 } 349 } else { 350 if (!NSK_JVMTI_VERIFY( 351 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, 352 JVMTI_ENABLE, event, NULL))) { 353 NSK_COMPLAIN1("Unexpected error enabling %s\n", 354 TranslateEvent(event)); 355 return NSK_FALSE; 356 } 357 } 358 359 return NSK_TRUE; 360 } 361 362 /** 363 * Enable or disable tested events. 364 */ 365 static int enableEventList() { 366 367 int i, result; 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 switch (step) { 400 case 1: 401 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 402 eventCount[i] = 0; 403 } 404 405 eventCallbacks.VMInit = cbVMInit; 406 eventCallbacks.Exception = cbException; 407 eventCallbacks.ExceptionCatch = cbExceptionCatch; 408 eventCallbacks.SingleStep = cbSingleStep; 409 eventCallbacks.FramePop = cbFramePop; 410 eventCallbacks.Breakpoint = cbBreakpoint; 411 eventCallbacks.FieldAccess = cbFieldAccess; 412 eventCallbacks.FieldModification = cbFieldModification; 413 eventCallbacks.MethodEntry = cbMethodEntry; 414 eventCallbacks.MethodExit = cbMethodExit; 415 eventCallbacks.NativeMethodBind = cbNativeMethodBind; 416 eventCallbacks.CompiledMethodLoad = cbCompiledMethodLoad; 417 eventCallbacks.CompiledMethodUnload = cbCompiledMethodUnload; 418 eventCallbacks.MonitorWait = cbMonitorWait; 419 eventCallbacks.MonitorWaited = cbMonitorWaited; 420 eventCallbacks.MonitorContendedEnter = cbMonitorContendedEnter; 421 eventCallbacks.MonitorContendedEntered = cbMonitorContendedEntered; 422 eventCallbacks.GarbageCollectionStart = cbGarbageCollectionStart; 423 eventCallbacks.GarbageCollectionFinish = cbGarbageCollectionFinish; 424 eventCallbacks.ObjectFree = cbObjectFree; 425 eventCallbacks.VMObjectAlloc = cbVMObjectAlloc; 426 break; 427 428 case 2: 429 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 430 newEventCount[i] = 0; 431 } 432 433 eventCallbacks.FieldAccess = cbNewFieldAccess; 434 eventCallbacks.FieldModification = cbNewFieldModification; 435 break; 436 437 case 3: 438 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 439 newEventCount[i] = 0; 440 } 441 442 eventCallbacks.VMDeath = cbVMDeath; 443 break; 444 445 } 446 if (!NSK_JVMTI_VERIFY( 447 NSK_CPP_STUB3(SetEventCallbacks, jvmti, 448 &eventCallbacks, 449 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 jfieldID field_accID, field_modID; 463 jclass cls; 464 465 466 if (!nsk_jvmti_waitForSync(timeout)) 467 return; 468 469 if (!NSK_JNI_VERIFY(agentJNI, (cls = 470 NSK_CPP_STUB2(FindClass, agentJNI, CLASS_NAME)) != NULL)) 471 return; 472 473 if (!NSK_JNI_VERIFY(agentJNI, (field_accID = 474 NSK_CPP_STUB4(GetStaticFieldID, agentJNI, cls, FIELD_ACC_NAME, 475 "I")) != NULL)) 476 return; 477 478 if (!NSK_JNI_VERIFY(agentJNI, (field_modID = 479 NSK_CPP_STUB4(GetStaticFieldID, agentJNI, cls, FIELD_MOD_NAME, 480 "I")) != NULL)) 481 return; 482 483 if (!NSK_JVMTI_VERIFY( 484 NSK_CPP_STUB3(SetFieldModificationWatch, jvmti, cls, field_modID))) 485 return; 486 487 if (!NSK_JVMTI_VERIFY( 488 NSK_CPP_STUB3(SetFieldAccessWatch, jvmti, cls, field_accID))) 489 return; 490 491 if (!nsk_jvmti_resumeSync()) 492 return; 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_em02t010(JavaVM *jvm, char *options, void *reserved) { 520 return Agent_Initialize(jvm, options, reserved); 521 } 522 JNIEXPORT jint JNICALL Agent_OnAttach_em02t010(JavaVM *jvm, char *options, void *reserved) { 523 return Agent_Initialize(jvm, options, reserved); 524 } 525 JNIEXPORT jint JNI_OnLoad_em02t010(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( 540 NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) { 541 nsk_jvmti_setFailStatus(); 542 return JNI_ERR; 543 } 544 545 { 546 jvmtiCapabilities caps; 547 memset(&caps, 0, sizeof(caps)); 548 549 caps.can_generate_field_modification_events = 1; 550 caps.can_generate_field_access_events = 1; 551 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) 552 return JNI_ERR; 553 } 554 555 if (!setCallBacks(1)) { 556 return JNI_ERR; 557 } 558 559 if (!enableEventList()) { 560 return JNI_ERR; 561 } 562 563 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 564 return JNI_ERR; 565 566 return JNI_OK; 567 } 568 569 /* ============================================================================= */ 570 571 572 }