< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/events/EM06/em06t001/em06t001.cpp

Print this page
rev 52050 : [mq]: refactor


  43 #define EXPECTED_CLASS_NAME "nsk.jvmti.scenarios.events.EM06.em06t001a"
  44 #define CLASS_LOADER_COUNT_PARAM "classLoaderCount"
  45 
  46 static int classLoaderCount = 0;
  47 static int classloadEventCount = 0;
  48 static int classprepareEventCount = 0;
  49 
  50 /* ============================================================================= */
  51 
  52 /* callbacks */
  53 
  54 void
  55 handler(jvmtiEvent event, jvmtiEnv* jvmti, JNIEnv* jni_env,
  56                     jthread thread, jclass klass) {
  57 
  58     jmethodID methodID;
  59     jclass classObject;
  60     jstring jclassName;
  61     const char *className;
  62 
  63     if (!NSK_JNI_VERIFY(jni_env, (classObject =
  64             NSK_CPP_STUB2(GetObjectClass, jni_env, klass)) != NULL)) {
  65         nsk_jvmti_setFailStatus();
  66         return;
  67     }
  68 
  69     if (!NSK_JNI_VERIFY(jni_env, (methodID =
  70             NSK_CPP_STUB4(GetMethodID, jni_env, classObject,
  71                         "getName", "()Ljava/lang/String;")) != NULL)) {
  72         nsk_jvmti_setFailStatus();
  73         return;
  74     }
  75 
  76     jclassName = (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) NSK_CPP_STUB3(CallObjectMethod, jni_env, klass,
  77                         methodID);
  78 
  79     className = NSK_CPP_STUB3(GetStringUTFChars, jni_env, jclassName, 0);
  80 
  81     if ( className != NULL && (strcmp(className, EXPECTED_CLASS_NAME)==0) ) {
  82 
  83         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock)))
  84             nsk_jvmti_setFailStatus();
  85 
  86         switch (event) {
  87             case JVMTI_EVENT_CLASS_LOAD:
  88                 classloadEventCount++; break;
  89             case JVMTI_EVENT_CLASS_PREPARE:
  90                 classprepareEventCount++; break;
  91             default:
  92                 NSK_COMPLAIN1("Unexpected event %s", TranslateEvent(event));
  93                 nsk_jvmti_setFailStatus();
  94         }
  95 
  96         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock)))
  97             nsk_jvmti_setFailStatus();
  98 
  99     }
 100 
 101     NSK_CPP_STUB3(ReleaseStringUTFChars, jni_env, jclassName, className);
 102 }
 103 
 104 JNIEXPORT void JNICALL
 105 cbClassLoad(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
 106 
 107     handler(JVMTI_EVENT_CLASS_LOAD, jvmti, jni_env, thread, klass);
 108 }
 109 
 110 JNIEXPORT void JNICALL
 111 cbClassPrepare(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
 112 
 113     handler(JVMTI_EVENT_CLASS_PREPARE, jvmti, jni_env, thread, klass);
 114 }
 115 
 116 /* ============================================================================= */
 117 
 118 static int
 119 enableEvent(jvmtiEventMode enable, jvmtiEvent event) {
 120     if (!NSK_JVMTI_VERIFY(
 121             NSK_CPP_STUB4(SetEventNotificationMode, jvmti, enable,
 122                                             event, NULL))) {
 123         nsk_jvmti_setFailStatus();
 124         return NSK_FALSE;
 125     }
 126 
 127     return NSK_TRUE;
 128 }
 129 
 130 /* ============================================================================= */
 131 
 132 /**
 133  * Testcase: check tested events.
 134  *   - check if expected events received for each method
 135  *
 136  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 137  */
 138 int checkEvents() {
 139 
 140     int result = NSK_TRUE;
 141 
 142     if (classloadEventCount == classLoaderCount) {


 155     } else {
 156         NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_PREPARE events %d\n\texpected value %d\n",
 157                             classprepareEventCount,
 158                             classLoaderCount);
 159         result = NSK_FALSE;
 160     }
 161 
 162     return result;
 163 }
 164 
 165 /* ============================================================================= */
 166 
 167 static int
 168 setCallBacks() {
 169     jvmtiEventCallbacks eventCallbacks;
 170     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 171 
 172     eventCallbacks.ClassLoad    = cbClassLoad;
 173     eventCallbacks.ClassPrepare = cbClassPrepare;
 174 
 175     if (!NSK_JVMTI_VERIFY(
 176             NSK_CPP_STUB3(SetEventCallbacks, jvmti,
 177                                 &eventCallbacks,
 178                                 sizeof(eventCallbacks))))
 179         return NSK_FALSE;
 180 
 181     return NSK_TRUE;
 182 }
 183 
 184 /* ============================================================================= */
 185 
 186 /** Agent algorithm. */
 187 static void JNICALL
 188 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 189 
 190     if (!NSK_JVMTI_VERIFY(
 191             NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) {
 192         nsk_jvmti_setFailStatus();
 193         return;
 194     }
 195 
 196     jni = agentJNI;
 197 
 198     NSK_DISPLAY0("Wait for debuggee to become ready\n");
 199     if (!nsk_jvmti_waitForSync(timeout))
 200         return;
 201 
 202     if (!setCallBacks()) {
 203         return;
 204     }
 205 
 206     if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD)
 207             || !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE)) {
 208         NSK_COMPLAIN0("Events could not be enabled");
 209         nsk_jvmti_setFailStatus();
 210         return;
 211     }


 214     if (!nsk_jvmti_resumeSync())
 215         return;
 216 
 217     if (!nsk_jvmti_waitForSync(timeout))
 218         return;
 219 
 220     if (!checkEvents()) {
 221         nsk_jvmti_setFailStatus();
 222     }
 223 
 224     if (!enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_LOAD)
 225             || !enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_PREPARE)) {
 226         NSK_COMPLAIN0("Events could not be disabled");
 227         nsk_jvmti_setFailStatus();
 228     }
 229 
 230     NSK_DISPLAY0("Let debuggee to finish\n");
 231     if (!nsk_jvmti_resumeSync())
 232         return;
 233 
 234     if (!NSK_JVMTI_VERIFY(
 235             NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock)))
 236         nsk_jvmti_setFailStatus();
 237 
 238 }
 239 
 240 /* ============================================================================= */
 241 
 242 /** Agent library initialization. */
 243 #ifdef STATIC_BUILD
 244 JNIEXPORT jint JNICALL Agent_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
 245     return Agent_Initialize(jvm, options, reserved);
 246 }
 247 JNIEXPORT jint JNICALL Agent_OnAttach_em06t001(JavaVM *jvm, char *options, void *reserved) {
 248     return Agent_Initialize(jvm, options, reserved);
 249 }
 250 JNIEXPORT jint JNI_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
 251     return JNI_VERSION_1_8;
 252 }
 253 #endif
 254 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 255 


  43 #define EXPECTED_CLASS_NAME "nsk.jvmti.scenarios.events.EM06.em06t001a"
  44 #define CLASS_LOADER_COUNT_PARAM "classLoaderCount"
  45 
  46 static int classLoaderCount = 0;
  47 static int classloadEventCount = 0;
  48 static int classprepareEventCount = 0;
  49 
  50 /* ============================================================================= */
  51 
  52 /* callbacks */
  53 
  54 void
  55 handler(jvmtiEvent event, jvmtiEnv* jvmti, JNIEnv* jni_env,
  56                     jthread thread, jclass klass) {
  57 
  58     jmethodID methodID;
  59     jclass classObject;
  60     jstring jclassName;
  61     const char *className;
  62 
  63     if (!NSK_JNI_VERIFY(jni_env, (classObject = jni_env->GetObjectClass(klass)) != NULL)) {

  64         nsk_jvmti_setFailStatus();
  65         return;
  66     }
  67 
  68     if (!NSK_JNI_VERIFY(jni_env, (methodID =
  69             jni_env->GetMethodID(classObject, "getName", "()Ljava/lang/String;")) != NULL)) {

  70         nsk_jvmti_setFailStatus();
  71         return;
  72     }
  73 
  74     jclassName = (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) (jstring) jni_env->CallObjectMethod(klass, methodID);

  75 
  76     className = jni_env->GetStringUTFChars(jclassName, 0);
  77 
  78     if ( className != NULL && (strcmp(className, EXPECTED_CLASS_NAME)==0) ) {
  79 
  80         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
  81             nsk_jvmti_setFailStatus();
  82 
  83         switch (event) {
  84             case JVMTI_EVENT_CLASS_LOAD:
  85                 classloadEventCount++; break;
  86             case JVMTI_EVENT_CLASS_PREPARE:
  87                 classprepareEventCount++; break;
  88             default:
  89                 NSK_COMPLAIN1("Unexpected event %s", TranslateEvent(event));
  90                 nsk_jvmti_setFailStatus();
  91         }
  92 
  93         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
  94             nsk_jvmti_setFailStatus();
  95 
  96     }
  97 
  98     jni_env->ReleaseStringUTFChars(jclassName, className);
  99 }
 100 
 101 JNIEXPORT void JNICALL
 102 cbClassLoad(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
 103 
 104     handler(JVMTI_EVENT_CLASS_LOAD, jvmti, jni_env, thread, klass);
 105 }
 106 
 107 JNIEXPORT void JNICALL
 108 cbClassPrepare(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
 109 
 110     handler(JVMTI_EVENT_CLASS_PREPARE, jvmti, jni_env, thread, klass);
 111 }
 112 
 113 /* ============================================================================= */
 114 
 115 static int
 116 enableEvent(jvmtiEventMode enable, jvmtiEvent event) {
 117     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(enable, event, NULL))) {


 118         nsk_jvmti_setFailStatus();
 119         return NSK_FALSE;
 120     }
 121 
 122     return NSK_TRUE;
 123 }
 124 
 125 /* ============================================================================= */
 126 
 127 /**
 128  * Testcase: check tested events.
 129  *   - check if expected events received for each method
 130  *
 131  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 132  */
 133 int checkEvents() {
 134 
 135     int result = NSK_TRUE;
 136 
 137     if (classloadEventCount == classLoaderCount) {


 150     } else {
 151         NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_PREPARE events %d\n\texpected value %d\n",
 152                             classprepareEventCount,
 153                             classLoaderCount);
 154         result = NSK_FALSE;
 155     }
 156 
 157     return result;
 158 }
 159 
 160 /* ============================================================================= */
 161 
 162 static int
 163 setCallBacks() {
 164     jvmtiEventCallbacks eventCallbacks;
 165     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 166 
 167     eventCallbacks.ClassLoad    = cbClassLoad;
 168     eventCallbacks.ClassPrepare = cbClassPrepare;
 169 
 170     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))



 171         return NSK_FALSE;
 172 
 173     return NSK_TRUE;
 174 }
 175 
 176 /* ============================================================================= */
 177 
 178 /** Agent algorithm. */
 179 static void JNICALL
 180 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 181 
 182     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_syncLock", &syncLock))) {

 183         nsk_jvmti_setFailStatus();
 184         return;
 185     }
 186 
 187     jni = agentJNI;
 188 
 189     NSK_DISPLAY0("Wait for debuggee to become ready\n");
 190     if (!nsk_jvmti_waitForSync(timeout))
 191         return;
 192 
 193     if (!setCallBacks()) {
 194         return;
 195     }
 196 
 197     if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD)
 198             || !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE)) {
 199         NSK_COMPLAIN0("Events could not be enabled");
 200         nsk_jvmti_setFailStatus();
 201         return;
 202     }


 205     if (!nsk_jvmti_resumeSync())
 206         return;
 207 
 208     if (!nsk_jvmti_waitForSync(timeout))
 209         return;
 210 
 211     if (!checkEvents()) {
 212         nsk_jvmti_setFailStatus();
 213     }
 214 
 215     if (!enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_LOAD)
 216             || !enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_PREPARE)) {
 217         NSK_COMPLAIN0("Events could not be disabled");
 218         nsk_jvmti_setFailStatus();
 219     }
 220 
 221     NSK_DISPLAY0("Let debuggee to finish\n");
 222     if (!nsk_jvmti_resumeSync())
 223         return;
 224 
 225     if (!NSK_JVMTI_VERIFY(jvmti->DestroyRawMonitor(syncLock)))

 226         nsk_jvmti_setFailStatus();
 227 
 228 }
 229 
 230 /* ============================================================================= */
 231 
 232 /** Agent library initialization. */
 233 #ifdef STATIC_BUILD
 234 JNIEXPORT jint JNICALL Agent_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
 235     return Agent_Initialize(jvm, options, reserved);
 236 }
 237 JNIEXPORT jint JNICALL Agent_OnAttach_em06t001(JavaVM *jvm, char *options, void *reserved) {
 238     return Agent_Initialize(jvm, options, reserved);
 239 }
 240 JNIEXPORT jint JNI_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
 241     return JNI_VERSION_1_8;
 242 }
 243 #endif
 244 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 245 
< prev index next >