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 }
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) {
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
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
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
|
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(jvmti->SetTag(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 }
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(jvmti->RawMonitorEnter(syncLock)))
152 nsk_jvmti_setFailStatus();
153
154 currentCounts[event - JVMTI_MIN_EVENT_TYPE_VAL]++;
155
156 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(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(jvmti->DestroyRawMonitor(syncLock)))
177 nsk_jvmti_setFailStatus();
178
179 }
180
181 void JNICALL
182 cbException(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
183 jmethodID method, jlocation location, jobject exception,
184 jmethodID catch_method, jlocation catch_location) {
185 changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]);
186 }
187
188 void JNICALL
189 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
190 jmethodID method, jlocation location, jobject exception) {
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) {
311 if (tag < 1 || tag > OBJECT_NUMBER) {
312 NSK_COMPLAIN1("Unexpected tag value %lld\n", tag);
313 nsk_jvmti_setFailStatus();
314 }
315 }
316
317 void JNICALL
318 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
319 jobject object, jclass object_klass, jlong size) {
320
321 changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]);
322 }
323
324 /* ============================================================================= */
325
326 static int enableEvent(jvmtiEvent event) {
327
328 if (nsk_jvmti_isOptionalEvent(event)
329 && (event != JVMTI_EVENT_OBJECT_FREE)) {
330 if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
331 jvmti->SetEventNotificationMode(JVMTI_ENABLE, event, NULL))) {
332 NSK_COMPLAIN1("Unexpected error enabling %s\n",
333 TranslateEvent(event));
334 return NSK_FALSE;
335 }
336 } else {
337 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, event, NULL))) {
338 NSK_COMPLAIN1("Unexpected error enabling %s\n",
339 TranslateEvent(event));
340 return NSK_FALSE;
341 }
342 }
343
344 return NSK_TRUE;
345 }
346
347 /**
348 * Enable or disable tested events.
349 */
350 static int enableEventList() {
351
352 int i, result;
353
354 result = enableEvent(JVMTI_EVENT_VM_INIT);
355
356 result = result && enableEvent(JVMTI_EVENT_VM_DEATH);
357
410 eventCallbacks.VMObjectAlloc = cbVMObjectAlloc;
411 break;
412
413 case 2:
414 for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
415 newEventCount[i] = 0;
416 }
417
418 eventCallbacks.ObjectFree = cbNewObjectFree;
419 break;
420
421 case 3:
422 for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
423 newEventCount[i] = 0;
424 }
425
426 eventCallbacks.VMDeath = cbVMDeath;
427 break;
428
429 }
430 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))
431 return NSK_FALSE;
432
433 return NSK_TRUE;
434 }
435
436 /* ============================================================================= */
437
438 /** Agent algorithm. */
439 static void JNICALL
440 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
441
442 int i;
443
444 for (i = 1; i <= STEP_NUMBER; i++) {
445
446 if (!nsk_jvmti_waitForSync(timeout))
447 return;
448
449 NSK_DISPLAY0("Check received events\n");
450
471 JNIEXPORT jint JNICALL Agent_OnLoad_em02t006(JavaVM *jvm, char *options, void *reserved) {
472 return Agent_Initialize(jvm, options, reserved);
473 }
474 JNIEXPORT jint JNICALL Agent_OnAttach_em02t006(JavaVM *jvm, char *options, void *reserved) {
475 return Agent_Initialize(jvm, options, reserved);
476 }
477 JNIEXPORT jint JNI_OnLoad_em02t006(JavaVM *jvm, char *options, void *reserved) {
478 return JNI_VERSION_1_8;
479 }
480 #endif
481 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
482
483 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
484 return JNI_ERR;
485
486 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
487
488 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
489 return JNI_ERR;
490
491 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_syncLock", &syncLock))) {
492 nsk_jvmti_setFailStatus();
493 return JNI_ERR;
494 }
495
496 {
497 jvmtiCapabilities caps;
498 memset(&caps, 0, sizeof(caps));
499
500 caps.can_tag_objects = 1;
501 caps.can_generate_object_free_events = 1;
502 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
503 return JNI_ERR;
504 }
505
506 if (!setCallBacks(1)) {
507 return JNI_ERR;
508 }
509
510 if (!enableEventList()) {
511 return JNI_ERR;
512 }
513
514 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
515 return JNI_ERR;
516
517 return JNI_OK;
518 }
519
520 /* ============================================================================= */
521
522
|