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