test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/events/EM02/em02t009/em02t009.cpp
changeset 51551 e409244ce72e
parent 50260 46c67f5e27c2
child 51774 79dc492c00ab
equal deleted inserted replaced
51550:a2f1923b3e16 51551:e409244ce72e
       
     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 #ifdef __cplusplus
       
    32 extern "C" {
       
    33 #endif
       
    34 
       
    35 /* ============================================================================= */
       
    36 
       
    37 /* scaffold objects */
       
    38 static jvmtiEnv *jvmti = NULL;
       
    39 static jlong timeout = 0;
       
    40 static jrawMonitorID syncLock = NULL;
       
    41 
       
    42 /* constant names */
       
    43 #define STEP_NUMBER 3
       
    44 #define NUMBER_OF_INVOCATIONS 1000
       
    45 #define JVMTI_EVENT_COUNT   (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1)
       
    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_METHOD_ENTRY)
       
   101                                 || (curr == JVMTI_EVENT_METHOD_EXIT));
       
   102                 break;
       
   103 
       
   104             case 2:
       
   105                 isExpected = ((curr == JVMTI_EVENT_METHOD_ENTRY)
       
   106                                 || (curr == JVMTI_EVENT_METHOD_EXIT));
       
   107                 break;
       
   108 
       
   109             case 3:
       
   110                 isExpected = (curr == JVMTI_EVENT_VM_DEATH);
       
   111                 break;
       
   112         }
       
   113 
       
   114         if (isExpected) {
       
   115             if (curr == JVMTI_EVENT_VM_INIT || curr == JVMTI_EVENT_VM_DEATH) {
       
   116                 if (currentCounts[i] < 1) {
       
   117                         NSK_COMPLAIN2("Unexpected events number %7d for %s\n\texpected value must be greater than 1\n",
       
   118                                             currentCounts[i],
       
   119                                             TranslateEvent(curr));
       
   120                     result = NSK_FALSE;
       
   121                 }
       
   122             } else {
       
   123                 if (currentCounts[i] != NUMBER_OF_INVOCATIONS) {
       
   124                     NSK_COMPLAIN3("Unexpected number of %s events %d, expected value is %d\n",
       
   125                                         TranslateEvent(curr),
       
   126                                         currentCounts[i],
       
   127                                         NUMBER_OF_INVOCATIONS);
       
   128                     result = NSK_FALSE;
       
   129                 }
       
   130             }
       
   131         } else {
       
   132 
       
   133             if (currentCounts[i] > 0) {
       
   134                 NSK_COMPLAIN2("Unexpected event %s was sent %d times\n",
       
   135                                     TranslateEvent(curr),
       
   136                                     currentCounts[i]);
       
   137                 result = NSK_FALSE;
       
   138             }
       
   139         }
       
   140     }
       
   141 
       
   142     return result;
       
   143 }
       
   144 
       
   145 static void
       
   146 changeCount(jvmtiEvent event, int *currentCounts) {
       
   147 
       
   148     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock)))
       
   149         nsk_jvmti_setFailStatus();
       
   150 
       
   151     currentCounts[event - JVMTI_MIN_EVENT_TYPE_VAL]++;
       
   152 
       
   153     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock)))
       
   154         nsk_jvmti_setFailStatus();
       
   155 
       
   156 }
       
   157 
       
   158 /* ============================================================================= */
       
   159 
       
   160 /* callbacks */
       
   161 JNIEXPORT void JNICALL
       
   162 cbVMInit(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread) {
       
   163     changeCount(JVMTI_EVENT_VM_INIT, &eventCount[0]);
       
   164 }
       
   165 
       
   166 JNIEXPORT void JNICALL
       
   167 cbVMDeath(jvmtiEnv* jvmti, JNIEnv* jni_env) {
       
   168     changeCount(JVMTI_EVENT_VM_DEATH, &newEventCount[0]);
       
   169     showEventStatistics(STEP_NUMBER);
       
   170     if (!checkEvents(STEP_NUMBER))
       
   171         nsk_jvmti_setFailStatus();
       
   172 
       
   173     if (!NSK_JVMTI_VERIFY(
       
   174             NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock)))
       
   175         nsk_jvmti_setFailStatus();
       
   176 
       
   177 }
       
   178 
       
   179 void JNICALL
       
   180 cbException(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   181                 jmethodID method, jlocation location, jobject exception,
       
   182                 jmethodID catch_method, jlocation catch_location) {
       
   183 
       
   184     changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]);
       
   185 }
       
   186 
       
   187 void JNICALL
       
   188 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   189                 jmethodID method, jlocation location, jobject exception) {
       
   190 
       
   191     changeCount(JVMTI_EVENT_EXCEPTION_CATCH, &eventCount[0]);
       
   192 }
       
   193 
       
   194 void JNICALL
       
   195 cbSingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   196                 jmethodID method, jlocation location) {
       
   197 
       
   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 handler1(jvmtiEnv *jvmti_env, jvmtiEvent event, jmethodID method) {
       
   229 
       
   230     char *name;
       
   231     char *sign;
       
   232     char *genc;
       
   233 
       
   234     if (!NSK_JVMTI_VERIFY(
       
   235             NSK_CPP_STUB5(
       
   236                 GetMethodName, jvmti_env, method, &name, &sign, &genc))) {
       
   237         nsk_jvmti_setFailStatus();
       
   238         return;
       
   239     }
       
   240 
       
   241     if (!strncmp(name,"javaMethod", 8)) {
       
   242         changeCount(event, &eventCount[0]);
       
   243     }
       
   244 
       
   245     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
       
   246             jvmti_env, (unsigned char*)name))) {
       
   247         nsk_jvmti_setFailStatus();
       
   248     }
       
   249     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
       
   250             jvmti_env, (unsigned char*)sign))) {
       
   251         nsk_jvmti_setFailStatus();
       
   252     }
       
   253     if (genc != NULL)
       
   254         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
       
   255                 jvmti_env, (unsigned char*)genc))) {
       
   256             nsk_jvmti_setFailStatus();
       
   257         }
       
   258 }
       
   259 
       
   260 void JNICALL
       
   261 cbMethodEntry(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   262                 jmethodID method) {
       
   263 
       
   264     handler1(jvmti_env, JVMTI_EVENT_METHOD_ENTRY, method);
       
   265 }
       
   266 
       
   267 void JNICALL
       
   268 cbMethodExit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   269                 jmethodID method, jboolean was_popped_by_exception,
       
   270                 jvalue return_value) {
       
   271 
       
   272     handler1(jvmti_env, JVMTI_EVENT_METHOD_EXIT, method);
       
   273 }
       
   274 
       
   275 void handler2(jvmtiEnv *jvmti_env, jvmtiEvent event, jmethodID method) {
       
   276 
       
   277     char *name;
       
   278     char *sign;
       
   279     char *genc;
       
   280 
       
   281     if (!NSK_JVMTI_VERIFY(
       
   282             NSK_CPP_STUB5(
       
   283                 GetMethodName, jvmti_env, method, &name, &sign, &genc))) {
       
   284         nsk_jvmti_setFailStatus();
       
   285         return;
       
   286     }
       
   287 
       
   288     if (!strncmp(name,"javaMethod", 8)) {
       
   289         changeCount(event, &newEventCount[0]);
       
   290     }
       
   291 
       
   292     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
       
   293             jvmti_env, (unsigned char*)name))) {
       
   294         nsk_jvmti_setFailStatus();
       
   295     }
       
   296     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
       
   297             jvmti_env, (unsigned char*)sign))) {
       
   298         nsk_jvmti_setFailStatus();
       
   299     }
       
   300     if (genc != NULL)
       
   301         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
       
   302                 jvmti_env, (unsigned char*)genc))) {
       
   303             nsk_jvmti_setFailStatus();
       
   304         }
       
   305 }
       
   306 
       
   307 void JNICALL
       
   308 cbNewMethodEntry(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   309                 jmethodID method) {
       
   310 
       
   311     handler2(jvmti_env, JVMTI_EVENT_METHOD_ENTRY, method);
       
   312 }
       
   313 
       
   314 void JNICALL
       
   315 cbNewMethodExit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   316                 jmethodID method, jboolean was_popped_by_exception,
       
   317                 jvalue return_value) {
       
   318 
       
   319     handler2(jvmti_env, JVMTI_EVENT_METHOD_EXIT, method);
       
   320 }
       
   321 
       
   322 void JNICALL
       
   323 cbNativeMethodBind(jvmtiEnv *jvmti_env, JNIEnv* jni_env,jthread thread,
       
   324                 jmethodID method, void* address, void** new_address_ptr) {
       
   325     changeCount(JVMTI_EVENT_NATIVE_METHOD_BIND, &eventCount[0]);
       
   326 }
       
   327 
       
   328 void JNICALL
       
   329 cbMonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   330                     jobject object, jlong tout) {
       
   331 
       
   332     changeCount(JVMTI_EVENT_MONITOR_WAIT, &eventCount[0]);
       
   333 }
       
   334 
       
   335 void JNICALL
       
   336 cbMonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   337                     jobject object, jboolean timed_out) {
       
   338 
       
   339     changeCount(JVMTI_EVENT_MONITOR_WAITED, &eventCount[0]);
       
   340 }
       
   341 
       
   342 JNIEXPORT void JNICALL
       
   343 cbMonitorContendedEnter(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread,
       
   344                             jobject object) {
       
   345 
       
   346     changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, &eventCount[0]);
       
   347 }
       
   348 
       
   349 void JNICALL
       
   350 cbMonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   351                             jobject object) {
       
   352 
       
   353     changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, &eventCount[0]);
       
   354 }
       
   355 
       
   356 void JNICALL
       
   357 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
       
   358                 const void* code_addr, jint map_length,
       
   359                 const jvmtiAddrLocationMap* map, const void* compile_info) {
       
   360     changeCount(JVMTI_EVENT_COMPILED_METHOD_LOAD, &eventCount[0]);
       
   361 }
       
   362 
       
   363 void JNICALL
       
   364 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
       
   365                 const void* code_addr) {
       
   366     changeCount(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, &eventCount[0]);
       
   367 }
       
   368 
       
   369 void JNICALL
       
   370 cbGarbageCollectionStart(jvmtiEnv *jvmti_env) {
       
   371     changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_START, &eventCount[0]);
       
   372 }
       
   373 
       
   374 void JNICALL
       
   375 cbGarbageCollectionFinish(jvmtiEnv *jvmti_env) {
       
   376     changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, &eventCount[0]);
       
   377 }
       
   378 
       
   379 void JNICALL
       
   380 cbObjectFree(jvmtiEnv *jvmti_env, jlong tag) {
       
   381 
       
   382     changeCount(JVMTI_EVENT_OBJECT_FREE, &eventCount[0]);
       
   383 }
       
   384 
       
   385 void JNICALL
       
   386 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
       
   387                     jobject object, jclass object_klass, jlong size) {
       
   388 
       
   389     changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]);
       
   390 }
       
   391 
       
   392 /* ============================================================================= */
       
   393 
       
   394 static int enableEvent(jvmtiEvent event) {
       
   395 
       
   396     if (nsk_jvmti_isOptionalEvent(event)
       
   397             && (event != JVMTI_EVENT_METHOD_ENTRY)
       
   398             && (event != JVMTI_EVENT_METHOD_EXIT)) {
       
   399         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
       
   400                 NSK_CPP_STUB4(SetEventNotificationMode, jvmti,
       
   401                     JVMTI_ENABLE, event, NULL))) {
       
   402             NSK_COMPLAIN1("Unexpected error enabling %s\n",
       
   403                 TranslateEvent(event));
       
   404             return NSK_FALSE;
       
   405         }
       
   406     } else {
       
   407         if (!NSK_JVMTI_VERIFY(
       
   408                 NSK_CPP_STUB4(SetEventNotificationMode, jvmti,
       
   409                     JVMTI_ENABLE, event, NULL))) {
       
   410             NSK_COMPLAIN1("Unexpected error enabling %s\n",
       
   411                 TranslateEvent(event));
       
   412             return NSK_FALSE;
       
   413         }
       
   414     }
       
   415 
       
   416     return NSK_TRUE;
       
   417 }
       
   418 
       
   419 /**
       
   420  * Enable or disable tested events.
       
   421  */
       
   422 static int enableEventList() {
       
   423 
       
   424     int i, result;
       
   425 
       
   426     result = enableEvent(JVMTI_EVENT_VM_INIT);
       
   427 
       
   428     result = result && enableEvent(JVMTI_EVENT_VM_DEATH);
       
   429 
       
   430     /* enabling optional events */
       
   431     for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
       
   432         jvmtiEvent event = (jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL);
       
   433 
       
   434         if (nsk_jvmti_isOptionalEvent(event))
       
   435             result = result && enableEvent(event);
       
   436     }
       
   437 
       
   438     if (result == NSK_FALSE) {
       
   439         nsk_jvmti_setFailStatus();
       
   440         return NSK_FALSE;
       
   441     }
       
   442 
       
   443     return NSK_TRUE;
       
   444 }
       
   445 
       
   446 /* ============================================================================= */
       
   447 
       
   448 static int
       
   449 setCallBacks(int step) {
       
   450 
       
   451     int i;
       
   452 
       
   453     jvmtiEventCallbacks eventCallbacks;
       
   454     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
       
   455 
       
   456     switch (step) {
       
   457         case 1:
       
   458             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
       
   459                 eventCount[i] = 0;
       
   460             }
       
   461 
       
   462             eventCallbacks.VMInit                    = cbVMInit;
       
   463             eventCallbacks.Exception                 = cbException;
       
   464             eventCallbacks.ExceptionCatch            = cbExceptionCatch;
       
   465             eventCallbacks.SingleStep                = cbSingleStep;
       
   466             eventCallbacks.FramePop                  = cbFramePop;
       
   467             eventCallbacks.Breakpoint                = cbBreakpoint;
       
   468             eventCallbacks.FieldAccess               = cbFieldAccess;
       
   469             eventCallbacks.FieldModification         = cbFieldModification;
       
   470             eventCallbacks.MethodEntry               = cbMethodEntry;
       
   471             eventCallbacks.MethodExit                = cbMethodExit;
       
   472             eventCallbacks.NativeMethodBind          = cbNativeMethodBind;
       
   473             eventCallbacks.CompiledMethodLoad        = cbCompiledMethodLoad;
       
   474             eventCallbacks.CompiledMethodUnload      = cbCompiledMethodUnload;
       
   475             eventCallbacks.MonitorWait               = cbMonitorWait;
       
   476             eventCallbacks.MonitorWaited             = cbMonitorWaited;
       
   477             eventCallbacks.MonitorContendedEnter     = cbMonitorContendedEnter;
       
   478             eventCallbacks.MonitorContendedEntered   = cbMonitorContendedEntered;
       
   479             eventCallbacks.GarbageCollectionStart    = cbGarbageCollectionStart;
       
   480             eventCallbacks.GarbageCollectionFinish   = cbGarbageCollectionFinish;
       
   481             eventCallbacks.ObjectFree                = cbObjectFree;
       
   482             eventCallbacks.VMObjectAlloc             = cbVMObjectAlloc;
       
   483             break;
       
   484 
       
   485         case 2:
       
   486             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
       
   487                 newEventCount[i] = 0;
       
   488             }
       
   489 
       
   490             eventCallbacks.MethodEntry               = cbNewMethodEntry;
       
   491             eventCallbacks.MethodExit                = cbNewMethodExit;
       
   492             break;
       
   493 
       
   494         case 3:
       
   495             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
       
   496                 newEventCount[i] = 0;
       
   497             }
       
   498 
       
   499             eventCallbacks.VMDeath                   = cbVMDeath;
       
   500             break;
       
   501 
       
   502     }
       
   503     if (!NSK_JVMTI_VERIFY(
       
   504             NSK_CPP_STUB3(SetEventCallbacks, jvmti,
       
   505                                 &eventCallbacks,
       
   506                                 sizeof(eventCallbacks))))
       
   507         return NSK_FALSE;
       
   508 
       
   509     return NSK_TRUE;
       
   510 }
       
   511 
       
   512 /* ============================================================================= */
       
   513 
       
   514 /** Agent algorithm. */
       
   515 static void JNICALL
       
   516 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
       
   517 
       
   518     int i;
       
   519 
       
   520     for (i = 1; i <= STEP_NUMBER; i++) {
       
   521 
       
   522         if (!nsk_jvmti_waitForSync(timeout))
       
   523             return;
       
   524 
       
   525         if (i < STEP_NUMBER) {
       
   526             showEventStatistics(i);
       
   527             if (!checkEvents(i))
       
   528                 nsk_jvmti_setFailStatus();
       
   529 
       
   530             if (!setCallBacks(i + 1)) {
       
   531                 return;
       
   532             }
       
   533         }
       
   534 
       
   535         if (!nsk_jvmti_resumeSync())
       
   536             return;
       
   537     }
       
   538 
       
   539 }
       
   540 
       
   541 /* ============================================================================= */
       
   542 
       
   543 /** Agent library initialization. */
       
   544 #ifdef STATIC_BUILD
       
   545 JNIEXPORT jint JNICALL Agent_OnLoad_em02t009(JavaVM *jvm, char *options, void *reserved) {
       
   546     return Agent_Initialize(jvm, options, reserved);
       
   547 }
       
   548 JNIEXPORT jint JNICALL Agent_OnAttach_em02t009(JavaVM *jvm, char *options, void *reserved) {
       
   549     return Agent_Initialize(jvm, options, reserved);
       
   550 }
       
   551 JNIEXPORT jint JNI_OnLoad_em02t009(JavaVM *jvm, char *options, void *reserved) {
       
   552     return JNI_VERSION_1_8;
       
   553 }
       
   554 #endif
       
   555 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
       
   556 
       
   557     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
       
   558         return JNI_ERR;
       
   559 
       
   560     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
       
   561 
       
   562     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
       
   563         return JNI_ERR;
       
   564 
       
   565     if (!NSK_JVMTI_VERIFY(
       
   566             NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) {
       
   567         nsk_jvmti_setFailStatus();
       
   568         return JNI_ERR;
       
   569     }
       
   570 
       
   571     {
       
   572         jvmtiCapabilities caps;
       
   573         memset(&caps, 0, sizeof(caps));
       
   574 
       
   575         caps.can_generate_method_entry_events = 1;
       
   576         caps.can_generate_method_exit_events = 1;
       
   577         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)))
       
   578             return JNI_ERR;
       
   579     }
       
   580 
       
   581     if (!setCallBacks(1)) {
       
   582         return JNI_ERR;
       
   583     }
       
   584 
       
   585     if (!enableEventList()) {
       
   586         return JNI_ERR;
       
   587     }
       
   588 
       
   589     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
       
   590         return JNI_ERR;
       
   591 
       
   592     return JNI_OK;
       
   593 }
       
   594 
       
   595 /* ============================================================================= */
       
   596 
       
   597 
       
   598 #ifdef __cplusplus
       
   599 }
       
   600 #endif