< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/sampling/SP05/sp05t002/sp05t002.cpp

Print this page
rev 52100 : 8212082: Remove the NSK_CPP_STUB macros for remaining vmTestbase/jvmti/[sS]*
Summary:
Reviewed-by:


 126         if (!clean()) {
 127             nsk_jvmti_setFailStatus();
 128             return;
 129         }
 130     }
 131 
 132     /* resume debugee after last sync */
 133     if (!nsk_jvmti_resumeSync())
 134         return;
 135 }
 136 
 137 /* ============================================================================= */
 138 
 139 /**
 140  * Enable or disable tested events.
 141  */
 142 static int enableEvents(jvmtiEventMode enable) {
 143     int i;
 144 
 145     for (i = 0; i < EVENTS_COUNT; i++) {
 146         if (!NSK_JVMTI_VERIFY(
 147                 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, enable,
 148                                                 eventsList[i], NULL))) {
 149             nsk_jvmti_setFailStatus();
 150             return NSK_FALSE;
 151         }
 152     }
 153     return NSK_TRUE;
 154 }
 155 
 156 /**
 157  * Prepare data:
 158  *    - get threads array from static field
 159  *    - get each thread from array
 160  *    - make global refs
 161  *    - enable events
 162  */
 163 static int prepare() {
 164     jclass debugeeClass = NULL;
 165     jfieldID threadsFieldID = NULL;
 166     jobjectArray threadsArray = NULL;
 167     jsize threadsArrayLength = 0;
 168     jsize i;
 169 
 170     /* find debugee class */
 171     if (!NSK_JNI_VERIFY(jni, (debugeeClass =
 172             NSK_CPP_STUB2(FindClass, jni, DEBUGEE_CLASS_NAME)) != NULL))
 173         return NSK_FALSE;
 174 
 175     /* find static field with threads array */
 176     if (!NSK_JNI_VERIFY(jni, (threadsFieldID =
 177             NSK_CPP_STUB4(GetStaticFieldID, jni, debugeeClass,
 178                                     THREADS_FIELD_NAME, THREADS_FIELD_SIG)) != NULL))
 179         return NSK_FALSE;
 180 
 181     /* get threads array from static field */
 182     if (!NSK_JNI_VERIFY(jni, (threadsArray = (jobjectArray)
 183             NSK_CPP_STUB3(GetStaticObjectField, jni, debugeeClass, threadsFieldID)) != NULL))
 184         return NSK_FALSE;
 185 
 186     /* check array length */
 187     if (!NSK_JNI_VERIFY(jni, (threadsArrayLength =
 188             NSK_CPP_STUB2(GetArrayLength, jni, threadsArray)) == THREADS_COUNT))
 189         return NSK_FALSE;
 190 
 191     /* get each thread from array */
 192     for (i = 0; i < THREADS_COUNT; i++) {
 193         if (!NSK_JNI_VERIFY(jni, (threadsList[i] = (jthread)
 194                 NSK_CPP_STUB3(GetObjectArrayElement, jni, threadsArray, i)) != NULL))
 195             return NSK_FALSE;
 196     }
 197 
 198     /* make global references to threads */
 199     for (i = 0; i < THREADS_COUNT; i++) {
 200         if (!NSK_JNI_VERIFY(jni, (threadsList[i] = (jthread)
 201                 NSK_CPP_STUB2(NewGlobalRef, jni, threadsList[i])) != NULL))
 202             return NSK_FALSE;
 203     }
 204 
 205     return enableEvents(JVMTI_ENABLE);
 206 }
 207 
 208 /**
 209  * Testcase: check thread's stack on event
 210  *
 211  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 212  */
 213 static int checkThread(jthread thread, int i, const char* kind) {
 214     jint framesCount = 0;
 215     jint stackDepth  = 0;
 216     jvmtiFrameInfo stackFrames[MAX_STACK_DEPTH];
 217 
 218     NSK_DISPLAY3("  thread #%d (%s): %p\n", i, threadsName[i], (void*)thread);
 219 
 220     /* get frames count */
 221     if (!NSK_JVMTI_VERIFY(
 222             NSK_CPP_STUB3(GetFrameCount, jvmti, thread, &framesCount))) {
 223         nsk_jvmti_setFailStatus();
 224         return NSK_TRUE;
 225     }
 226     NSK_DISPLAY1("    frames count: %d\n", (int)framesCount);
 227 
 228     /* get stack frames */
 229     if (!NSK_JVMTI_VERIFY(
 230             NSK_CPP_STUB6(GetStackTrace, jvmti, thread, 0, MAX_STACK_DEPTH,
 231                                                     stackFrames, &stackDepth))) {
 232         nsk_jvmti_setFailStatus();
 233         return NSK_TRUE;
 234     }
 235     NSK_DISPLAY1("    stack depth:  %d\n", (int)stackDepth);
 236 
 237     /* check against emptyness */
 238     if (framesCount != 0) {
 239         NSK_COMPLAIN5("Unexpected GetFramesCount() for %s thread #%d (%s):\n"
 240                                 "#   got frames: %d\n"
 241                                 "#   expected:   %d\n",
 242                                 kind, i, threadsName[i],
 243                                 framesCount, 0);
 244         nsk_jvmti_setFailStatus();
 245     }
 246     if (stackDepth != 0) {
 247         NSK_COMPLAIN5("Unexpected GetStackTrace() for %s thread #%d (%s):\n"
 248                                 "#   got frames: %d\n"
 249                                 "#   expected:   %d\n",
 250                                 kind, i, threadsName[i],
 251                                 stackDepth, 0);
 252         nsk_jvmti_setFailStatus();
 253     }
 254 
 255     /* test may continue */
 256     return NSK_TRUE;
 257 }
 258 
 259 /**
 260  * Clean data:
 261  *   - disable events
 262  *   - dispose global references to tested threads
 263  */
 264 static int clean() {
 265     int i;
 266 
 267     /* disable events */
 268     enableEvents(JVMTI_DISABLE);
 269 
 270     /* dispose global references to threads */
 271     for (i = 0; i < THREADS_COUNT; i++) {
 272         NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsList[i]));
 273     }
 274 
 275     return NSK_TRUE;
 276 }
 277 
 278 /* ============================================================================= */
 279 
 280 /**
 281  * THREAD_START callback:
 282  *   - check thread's stack on THREAD_START
 283  */
 284 JNIEXPORT void JNICALL
 285 callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 286     int i;
 287 
 288     /* check if thread is not NULL */
 289     if (!NSK_VERIFY(thread != NULL)) {
 290         nsk_jvmti_setFailStatus();
 291         return;
 292     }
 293 
 294     /* check if event is for tested thread */
 295     for (i = 0; i < THREADS_COUNT; i++) {
 296         if (NSK_CPP_STUB3(IsSameObject, jni, threadsList[i], thread)) {
 297                 NSK_DISPLAY0("SUCCESS: expected THREAD_START event\n");
 298             eventsStart++;
 299 
 300             /* check thread on event */
 301             checkThread(thread, i, "starting");
 302             break;
 303         }
 304     }
 305 }
 306 
 307 /**
 308  * THREAD_END callback:
 309  *   - check thread's stack on THREAD_END
 310  */
 311 JNIEXPORT void JNICALL
 312 callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 313     int i;
 314 
 315     /* check if thread is not NULL */
 316     if (!NSK_VERIFY(thread != NULL)) {
 317         nsk_jvmti_setFailStatus();
 318         return;
 319     }
 320 
 321     /* check if event is for tested thread */
 322     for (i = 0; i < THREADS_COUNT; i++) {
 323         if (NSK_CPP_STUB3(IsSameObject, jni, threadsList[i], thread)) {
 324                 NSK_DISPLAY0("SUCCESS: expected THREAD_START event\n");
 325             eventsEnd++;
 326 
 327             /* check thread on event */
 328             checkThread(thread, i, "finishing");
 329             break;
 330         }
 331     }
 332 }
 333 
 334 /* ============================================================================= */
 335 
 336 static volatile int testedThreadRunning = NSK_FALSE;
 337 static volatile int testedThreadShouldFinish = NSK_FALSE;
 338 
 339 /** Native running method in tested thread. */
 340 JNIEXPORT void JNICALL
 341 Java_nsk_jvmti_scenarios_sampling_SP05_sp05t002ThreadRunningNative_run(JNIEnv* jni,
 342                                                                             jobject obj) {
 343     volatile int i = 0, n = 1000;


 386 }
 387 #endif
 388 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 389     /* init framework and parse options */
 390     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 391         return JNI_ERR;
 392 
 393     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 394 
 395     /* create JVMTI environment */
 396     if (!NSK_VERIFY((jvmti =
 397             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 398         return JNI_ERR;
 399 
 400     /* set events callbacks */
 401     {
 402         jvmtiEventCallbacks eventCallbacks;
 403         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 404         eventCallbacks.ThreadStart = callbackThreadStart;
 405         eventCallbacks.ThreadEnd = callbackThreadEnd;
 406         if (!NSK_JVMTI_VERIFY(
 407                 NSK_CPP_STUB3(SetEventCallbacks, jvmti,
 408                                     &eventCallbacks, sizeof(eventCallbacks))))
 409             return JNI_ERR;
 410     }
 411 
 412     /* register agent proc and arg */
 413     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 414         return JNI_ERR;
 415 
 416     return JNI_OK;
 417 }
 418 
 419 /* ============================================================================= */
 420 
 421 }


 126         if (!clean()) {
 127             nsk_jvmti_setFailStatus();
 128             return;
 129         }
 130     }
 131 
 132     /* resume debugee after last sync */
 133     if (!nsk_jvmti_resumeSync())
 134         return;
 135 }
 136 
 137 /* ============================================================================= */
 138 
 139 /**
 140  * Enable or disable tested events.
 141  */
 142 static int enableEvents(jvmtiEventMode enable) {
 143     int i;
 144 
 145     for (i = 0; i < EVENTS_COUNT; i++) {
 146         if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(enable, eventsList[i], NULL))) {


 147             nsk_jvmti_setFailStatus();
 148             return NSK_FALSE;
 149         }
 150     }
 151     return NSK_TRUE;
 152 }
 153 
 154 /**
 155  * Prepare data:
 156  *    - get threads array from static field
 157  *    - get each thread from array
 158  *    - make global refs
 159  *    - enable events
 160  */
 161 static int prepare() {
 162     jclass debugeeClass = NULL;
 163     jfieldID threadsFieldID = NULL;
 164     jobjectArray threadsArray = NULL;
 165     jsize threadsArrayLength = 0;
 166     jsize i;
 167 
 168     /* find debugee class */
 169     if (!NSK_JNI_VERIFY(jni, (debugeeClass = jni->FindClass(DEBUGEE_CLASS_NAME)) != NULL))

 170         return NSK_FALSE;
 171 
 172     /* find static field with threads array */
 173     if (!NSK_JNI_VERIFY(jni, (threadsFieldID =
 174             jni->GetStaticFieldID(debugeeClass, THREADS_FIELD_NAME, THREADS_FIELD_SIG)) != NULL))

 175         return NSK_FALSE;
 176 
 177     /* get threads array from static field */
 178     if (!NSK_JNI_VERIFY(jni, (threadsArray = (jobjectArray)
 179             jni->GetStaticObjectField(debugeeClass, threadsFieldID)) != NULL))
 180         return NSK_FALSE;
 181 
 182     /* check array length */
 183     if (!NSK_JNI_VERIFY(jni, (threadsArrayLength =
 184             jni->GetArrayLength(threadsArray)) == THREADS_COUNT))
 185         return NSK_FALSE;
 186 
 187     /* get each thread from array */
 188     for (i = 0; i < THREADS_COUNT; i++) {
 189         if (!NSK_JNI_VERIFY(jni, (threadsList[i] = (jthread)
 190                 jni->GetObjectArrayElement(threadsArray, i)) != NULL))
 191             return NSK_FALSE;
 192     }
 193 
 194     /* make global references to threads */
 195     for (i = 0; i < THREADS_COUNT; i++) {
 196         if (!NSK_JNI_VERIFY(jni, (threadsList[i] = (jthread)
 197                 jni->NewGlobalRef(threadsList[i])) != NULL))
 198             return NSK_FALSE;
 199     }
 200 
 201     return enableEvents(JVMTI_ENABLE);
 202 }
 203 
 204 /**
 205  * Testcase: check thread's stack on event
 206  *
 207  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 208  */
 209 static int checkThread(jthread thread, int i, const char* kind) {
 210     jint framesCount = 0;
 211     jint stackDepth  = 0;
 212     jvmtiFrameInfo stackFrames[MAX_STACK_DEPTH];
 213 
 214     NSK_DISPLAY3("  thread #%d (%s): %p\n", i, threadsName[i], (void*)thread);
 215 
 216     /* get frames count */
 217     if (!NSK_JVMTI_VERIFY(jvmti->GetFrameCount(thread, &framesCount))) {

 218         nsk_jvmti_setFailStatus();
 219         return NSK_TRUE;
 220     }
 221     NSK_DISPLAY1("    frames count: %d\n", (int)framesCount);
 222 
 223     /* get stack frames */
 224     if (!NSK_JVMTI_VERIFY(
 225             jvmti->GetStackTrace(thread, 0, MAX_STACK_DEPTH, stackFrames, &stackDepth))) {

 226         nsk_jvmti_setFailStatus();
 227         return NSK_TRUE;
 228     }
 229     NSK_DISPLAY1("    stack depth:  %d\n", (int)stackDepth);
 230 
 231     /* check against emptyness */
 232     if (framesCount != 0) {
 233         NSK_COMPLAIN5("Unexpected GetFramesCount() for %s thread #%d (%s):\n"
 234                                 "#   got frames: %d\n"
 235                                 "#   expected:   %d\n",
 236                                 kind, i, threadsName[i],
 237                                 framesCount, 0);
 238         nsk_jvmti_setFailStatus();
 239     }
 240     if (stackDepth != 0) {
 241         NSK_COMPLAIN5("Unexpected GetStackTrace() for %s thread #%d (%s):\n"
 242                                 "#   got frames: %d\n"
 243                                 "#   expected:   %d\n",
 244                                 kind, i, threadsName[i],
 245                                 stackDepth, 0);
 246         nsk_jvmti_setFailStatus();
 247     }
 248 
 249     /* test may continue */
 250     return NSK_TRUE;
 251 }
 252 
 253 /**
 254  * Clean data:
 255  *   - disable events
 256  *   - dispose global references to tested threads
 257  */
 258 static int clean() {
 259     int i;
 260 
 261     /* disable events */
 262     enableEvents(JVMTI_DISABLE);
 263 
 264     /* dispose global references to threads */
 265     for (i = 0; i < THREADS_COUNT; i++) {
 266         NSK_TRACE(jni->DeleteGlobalRef(threadsList[i]));
 267     }
 268 
 269     return NSK_TRUE;
 270 }
 271 
 272 /* ============================================================================= */
 273 
 274 /**
 275  * THREAD_START callback:
 276  *   - check thread's stack on THREAD_START
 277  */
 278 JNIEXPORT void JNICALL
 279 callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 280     int i;
 281 
 282     /* check if thread is not NULL */
 283     if (!NSK_VERIFY(thread != NULL)) {
 284         nsk_jvmti_setFailStatus();
 285         return;
 286     }
 287 
 288     /* check if event is for tested thread */
 289     for (i = 0; i < THREADS_COUNT; i++) {
 290         if (jni->IsSameObject(threadsList[i], thread)) {
 291                 NSK_DISPLAY0("SUCCESS: expected THREAD_START event\n");
 292             eventsStart++;
 293 
 294             /* check thread on event */
 295             checkThread(thread, i, "starting");
 296             break;
 297         }
 298     }
 299 }
 300 
 301 /**
 302  * THREAD_END callback:
 303  *   - check thread's stack on THREAD_END
 304  */
 305 JNIEXPORT void JNICALL
 306 callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 307     int i;
 308 
 309     /* check if thread is not NULL */
 310     if (!NSK_VERIFY(thread != NULL)) {
 311         nsk_jvmti_setFailStatus();
 312         return;
 313     }
 314 
 315     /* check if event is for tested thread */
 316     for (i = 0; i < THREADS_COUNT; i++) {
 317         if (jni->IsSameObject(threadsList[i], thread)) {
 318                 NSK_DISPLAY0("SUCCESS: expected THREAD_START event\n");
 319             eventsEnd++;
 320 
 321             /* check thread on event */
 322             checkThread(thread, i, "finishing");
 323             break;
 324         }
 325     }
 326 }
 327 
 328 /* ============================================================================= */
 329 
 330 static volatile int testedThreadRunning = NSK_FALSE;
 331 static volatile int testedThreadShouldFinish = NSK_FALSE;
 332 
 333 /** Native running method in tested thread. */
 334 JNIEXPORT void JNICALL
 335 Java_nsk_jvmti_scenarios_sampling_SP05_sp05t002ThreadRunningNative_run(JNIEnv* jni,
 336                                                                             jobject obj) {
 337     volatile int i = 0, n = 1000;


 380 }
 381 #endif
 382 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 383     /* init framework and parse options */
 384     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 385         return JNI_ERR;
 386 
 387     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 388 
 389     /* create JVMTI environment */
 390     if (!NSK_VERIFY((jvmti =
 391             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 392         return JNI_ERR;
 393 
 394     /* set events callbacks */
 395     {
 396         jvmtiEventCallbacks eventCallbacks;
 397         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 398         eventCallbacks.ThreadStart = callbackThreadStart;
 399         eventCallbacks.ThreadEnd = callbackThreadEnd;
 400         if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))


 401             return JNI_ERR;
 402     }
 403 
 404     /* register agent proc and arg */
 405     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 406         return JNI_ERR;
 407 
 408     return JNI_OK;
 409 }
 410 
 411 /* ============================================================================= */
 412 
 413 }
< prev index next >