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 48 /* ============================================================================= */ 49 50 51 /* 52 * Class: nsk_jvmti_scenarios_events_EM02_em02t006 53 * Method: setTag 54 * Signature: (Ljava/lang/Object;J)Z 55 */ 56 JNIEXPORT jboolean JNICALL 57 Java_nsk_jvmti_scenarios_events_EM02_em02t006_setTag(JNIEnv *env, 58 jobject o, jobject object, jlong tag) { 59 60 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, object, tag))) { 61 NSK_COMPLAIN0("TEST FAILED: unable to set tag for a tested object\n"); 62 return NSK_FALSE; 63 } 64 return NSK_TRUE; 65 } 66 67 static void 68 showEventStatistics(int step) { 69 int i; 70 const char* str; 71 int *currentCounts = (step == 1) ? &eventCount[0] : &newEventCount[0]; 72 73 NSK_DISPLAY0("\n"); 74 NSK_DISPLAY1("Event statistics for %d step:\n", step); 75 NSK_DISPLAY0("-----------------------------\n"); 76 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 77 if (currentCounts[i] > 0) { 78 str = TranslateEvent((jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL)); 79 NSK_DISPLAY2("%-40s %7d\n", str, currentCounts[i]); 80 } 81 } 82 } 83 84 /* ========================================================================== */ 85 86 int checkEvents(int step) { 87 int i; 88 jvmtiEvent curr; 89 int result = NSK_TRUE; 90 int *currentCounts; 91 int isExpected = 0; 92 93 switch (step) { 94 case 1: 95 currentCounts = &eventCount[0]; 96 break; 97 98 case 2: 99 case 3: 100 currentCounts = &newEventCount[0]; 101 break; 102 103 default: 104 NSK_COMPLAIN1("Unexpected step no: %d\n", step); 105 return NSK_FALSE; 106 } 107 108 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 109 110 curr = (jvmtiEvent) (i + JVMTI_MIN_EVENT_TYPE_VAL); 111 112 switch (step) { 113 case 1: 114 isExpected = ((curr == JVMTI_EVENT_OBJECT_FREE) 115 || (curr == JVMTI_EVENT_VM_INIT)); 116 break; 117 118 case 2: 119 isExpected = (curr == JVMTI_EVENT_OBJECT_FREE); 120 break; 121 122 case 3: 123 isExpected = (curr == JVMTI_EVENT_VM_DEATH); 124 break; 125 } 126 127 if (isExpected) { 128 if (currentCounts[i] < 0 || currentCounts[i] > OBJECT_NUMBER) { 129 NSK_COMPLAIN2("Unexpected events number %7d for %s\n\texpected value must be greater than 1\n", 130 currentCounts[i], 131 TranslateEvent(curr)); 132 result = NSK_FALSE; 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 changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]); 187 } 188 189 void JNICALL 190 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 191 jmethodID method, jlocation location, jobject exception) { 192 changeCount(JVMTI_EVENT_EXCEPTION_CATCH, &eventCount[0]); 193 } 194 195 void JNICALL 196 cbSingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 197 jmethodID method, jlocation location) { 198 changeCount(JVMTI_EVENT_SINGLE_STEP, &eventCount[0]); 199 } 200 201 void JNICALL 202 cbFramePop(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 203 jmethodID method, jboolean was_popped_by_exception) { 204 changeCount(JVMTI_EVENT_FRAME_POP, &eventCount[0]); 205 } 206 207 void JNICALL 208 cbBreakpoint(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 209 jmethodID method, jlocation location) { 210 changeCount(JVMTI_EVENT_BREAKPOINT, &eventCount[0]); 211 } 212 213 void JNICALL 214 cbFieldAccess(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 215 jmethodID method, jlocation location, jclass field_klass, 216 jobject object, jfieldID field) { 217 changeCount(JVMTI_EVENT_FIELD_ACCESS, &eventCount[0]); 218 } 219 220 void JNICALL 221 cbFieldModification(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 222 jmethodID method, jlocation location, jclass field_klass, 223 jobject object, jfieldID field, char signature_type, 224 jvalue new_value) { 225 changeCount(JVMTI_EVENT_FIELD_MODIFICATION, &eventCount[0]); 226 } 227 228 void JNICALL 229 cbMethodEntry(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 230 jmethodID method) { 231 changeCount(JVMTI_EVENT_METHOD_ENTRY, &eventCount[0]); 232 } 233 234 void JNICALL 235 cbMethodExit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 236 jmethodID method, jboolean was_popped_by_exception, 237 jvalue return_value) { 238 changeCount(JVMTI_EVENT_METHOD_EXIT, &eventCount[0]); 239 } 240 241 void JNICALL 242 cbNativeMethodBind(jvmtiEnv *jvmti_env, JNIEnv* jni_env,jthread thread, 243 jmethodID method, void* address, void** new_address_ptr) { 244 changeCount(JVMTI_EVENT_NATIVE_METHOD_BIND, &eventCount[0]); 245 } 246 247 void JNICALL 248 cbMonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 249 jobject object, jlong tout) { 250 251 changeCount(JVMTI_EVENT_MONITOR_WAIT, &eventCount[0]); 252 } 253 254 void JNICALL 255 cbMonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 256 jobject object, jboolean timed_out) { 257 258 changeCount(JVMTI_EVENT_MONITOR_WAITED, &eventCount[0]); 259 } 260 261 JNIEXPORT void JNICALL 262 cbMonitorContendedEnter(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, 263 jobject object) { 264 265 changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, &eventCount[0]); 266 } 267 268 void JNICALL 269 cbMonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 270 jobject object) { 271 272 changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, &eventCount[0]); 273 } 274 275 void JNICALL 276 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size, 277 const void* code_addr, jint map_length, 278 const jvmtiAddrLocationMap* map, const void* compile_info) { 279 changeCount(JVMTI_EVENT_COMPILED_METHOD_LOAD, &eventCount[0]); 280 } 281 282 void JNICALL 283 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method, 284 const void* code_addr) { 285 changeCount(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, &eventCount[0]); 286 } 287 288 void JNICALL 289 cbGarbageCollectionStart(jvmtiEnv *jvmti_env) { 290 changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_START, &eventCount[0]); 291 } 292 293 void JNICALL 294 cbGarbageCollectionFinish(jvmtiEnv *jvmti_env) { 295 changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, &eventCount[0]); 296 } 297 298 void JNICALL 299 cbObjectFree(jvmtiEnv *jvmti_env, jlong tag) { 300 301 changeCount(JVMTI_EVENT_OBJECT_FREE, &eventCount[0]); 302 if (tag < 1 || tag > OBJECT_NUMBER) { 303 NSK_COMPLAIN1("Unexpected tag value %lld\n", tag); 304 nsk_jvmti_setFailStatus(); 305 } 306 } 307 308 void JNICALL 309 cbNewObjectFree(jvmtiEnv *jvmti_env, jlong tag) { 310 311 changeCount(JVMTI_EVENT_OBJECT_FREE, &newEventCount[0]); 312 if (tag < 1 || tag > OBJECT_NUMBER) { 313 NSK_COMPLAIN1("Unexpected tag value %lld\n", tag); 314 nsk_jvmti_setFailStatus(); 315 } 316 } 317 318 void JNICALL 319 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 320 jobject object, jclass object_klass, jlong size) { 321 322 changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]); 323 } 324 325 /* ============================================================================= */ 326 327 static int enableEvent(jvmtiEvent event) { 328 329 if (nsk_jvmti_isOptionalEvent(event) 330 && (event != JVMTI_EVENT_OBJECT_FREE)) { 331 if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, 332 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, 333 JVMTI_ENABLE, event, NULL))) { 334 NSK_COMPLAIN1("Unexpected error enabling %s\n", 335 TranslateEvent(event)); 336 return NSK_FALSE; 337 } 338 } else { 339 if (!NSK_JVMTI_VERIFY( 340 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, 341 JVMTI_ENABLE, event, NULL))) { 342 NSK_COMPLAIN1("Unexpected error enabling %s\n", 343 TranslateEvent(event)); 344 return NSK_FALSE; 345 } 346 } 347 348 return NSK_TRUE; 349 } 350 351 /** 352 * Enable or disable tested events. 353 */ 354 static int enableEventList() { 355 356 int i, result; 357 358 result = enableEvent(JVMTI_EVENT_VM_INIT); 359 360 result = result && enableEvent(JVMTI_EVENT_VM_DEATH); 361 362 /* enabling optional events */ 363 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 364 jvmtiEvent event = (jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL); 365 366 if (nsk_jvmti_isOptionalEvent(event)) 367 result = result && enableEvent(event); 368 } 369 370 if (result == NSK_FALSE) { 371 nsk_jvmti_setFailStatus(); 372 return NSK_FALSE; 373 } 374 375 return NSK_TRUE; 376 } 377 378 /* ============================================================================= */ 379 380 static int 381 setCallBacks(int step) { 382 383 int i; 384 385 jvmtiEventCallbacks eventCallbacks; 386 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 387 388 switch (step) { 389 case 1: 390 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 391 eventCount[i] = 0; 392 } 393 394 eventCallbacks.VMInit = cbVMInit; 395 eventCallbacks.Exception = cbException; 396 eventCallbacks.ExceptionCatch = cbExceptionCatch; 397 eventCallbacks.SingleStep = cbSingleStep; 398 eventCallbacks.FramePop = cbFramePop; 399 eventCallbacks.Breakpoint = cbBreakpoint; 400 eventCallbacks.FieldAccess = cbFieldAccess; 401 eventCallbacks.FieldModification = cbFieldModification; 402 eventCallbacks.MethodEntry = cbMethodEntry; 403 eventCallbacks.MethodExit = cbMethodExit; 404 eventCallbacks.NativeMethodBind = cbNativeMethodBind; 405 eventCallbacks.CompiledMethodLoad = cbCompiledMethodLoad; 406 eventCallbacks.CompiledMethodUnload = cbCompiledMethodUnload; 407 eventCallbacks.MonitorWait = cbMonitorWait; 408 eventCallbacks.MonitorWaited = cbMonitorWaited; 409 eventCallbacks.MonitorContendedEnter = cbMonitorContendedEnter; 410 eventCallbacks.MonitorContendedEntered = cbMonitorContendedEntered; 411 eventCallbacks.GarbageCollectionStart = cbGarbageCollectionStart; 412 eventCallbacks.GarbageCollectionFinish = cbGarbageCollectionFinish; 413 eventCallbacks.ObjectFree = cbObjectFree; 414 eventCallbacks.VMObjectAlloc = cbVMObjectAlloc; 415 break; 416 417 case 2: 418 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 419 newEventCount[i] = 0; 420 } 421 422 eventCallbacks.ObjectFree = cbNewObjectFree; 423 break; 424 425 case 3: 426 for (i = 0; i < JVMTI_EVENT_COUNT; i++) { 427 newEventCount[i] = 0; 428 } 429 430 eventCallbacks.VMDeath = cbVMDeath; 431 break; 432 433 } 434 if (!NSK_JVMTI_VERIFY( 435 NSK_CPP_STUB3(SetEventCallbacks, jvmti, 436 &eventCallbacks, 437 sizeof(eventCallbacks)))) 438 return NSK_FALSE; 439 440 return NSK_TRUE; 441 } 442 443 /* ============================================================================= */ 444 445 /** Agent algorithm. */ 446 static void JNICALL 447 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) { 448 449 int i; 450 451 for (i = 1; i <= STEP_NUMBER; i++) { 452 453 if (!nsk_jvmti_waitForSync(timeout)) 454 return; 455 456 NSK_DISPLAY0("Check received events\n"); 457 458 if (i < STEP_NUMBER) { 459 showEventStatistics(i); 460 if (!checkEvents(i)) 461 nsk_jvmti_setFailStatus(); 462 463 if (!setCallBacks(i + 1)) { 464 return; 465 } 466 } 467 468 if (!nsk_jvmti_resumeSync()) 469 return; 470 } 471 472 } 473 474 /* ============================================================================= */ 475 476 /** Agent library initialization. */ 477 #ifdef STATIC_BUILD 478 JNIEXPORT jint JNICALL Agent_OnLoad_em02t006(JavaVM *jvm, char *options, void *reserved) { 479 return Agent_Initialize(jvm, options, reserved); 480 } 481 JNIEXPORT jint JNICALL Agent_OnAttach_em02t006(JavaVM *jvm, char *options, void *reserved) { 482 return Agent_Initialize(jvm, options, reserved); 483 } 484 JNIEXPORT jint JNI_OnLoad_em02t006(JavaVM *jvm, char *options, void *reserved) { 485 return JNI_VERSION_1_8; 486 } 487 #endif 488 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 489 490 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 491 return JNI_ERR; 492 493 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 494 495 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 496 return JNI_ERR; 497 498 if (!NSK_JVMTI_VERIFY( 499 NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) { 500 nsk_jvmti_setFailStatus(); 501 return JNI_ERR; 502 } 503 504 { 505 jvmtiCapabilities caps; 506 memset(&caps, 0, sizeof(caps)); 507 508 caps.can_tag_objects = 1; 509 caps.can_generate_object_free_events = 1; 510 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) 511 return JNI_ERR; 512 } 513 514 if (!setCallBacks(1)) { 515 return JNI_ERR; 516 } 517 518 if (!enableEventList()) { 519 return JNI_ERR; 520 } 521 522 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 523 return JNI_ERR; 524 525 return JNI_OK; 526 } 527 528 /* ============================================================================= */ 529 530 531 }