45 static const int testEventsNumber = 2;
46
47 static jrawMonitorID eventsCounterMonitor;
48
49 static volatile int eventsCounter = 0;
50
51 void eventHandler(jvmtiEnv *jvmti,
52 JNIEnv* jni,
53 jthread thread,
54 int threadStartEvent) {
55 char threadName[MAX_STRING_LENGTH];
56 int success = 1;
57 jint threadsCount = 0;
58 jthread * threads;
59
60 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
61 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
62 return;
63 }
64
65 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetAllThreads, jvmti, &threadsCount, &threads))) {
66 NSK_COMPLAIN1("%s: failed to get all threads\n", agentName);
67 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
68 return;
69 }
70
71 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
72
73 if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2(
74 RawMonitorEnter, jvmti, eventsCounterMonitor))) {
75
76 eventsCounter++;
77
78 if (threadStartEvent)
79 NSK_DISPLAY3("%s: ThreadStart event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
80 else
81 NSK_DISPLAY3("%s: ThreadEnd event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
82
83 if (eventsCounter == EXPECTED_EVENTS_NUMBER) {
84 NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);
85
86 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
87 }
88
89 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, eventsCounterMonitor))) {
90 success = 0;
91 }
92 } else {
93 success = 0;
94 }
95
96 if (!success) {
97 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
98 }
99 }
100
101 void JNICALL threadStartHandler(
102 jvmtiEnv *jvmti,
103 JNIEnv* jni,
104 jthread thread) {
105 eventHandler(jvmti, jni, thread, 1);
106 }
107
108 void JNICALL threadEndHandler(
109 jvmtiEnv *jvmti,
123 Agent_OnAttach_attach045Agent02(JavaVM *vm, char *optionsString, void *reserved)
124 #else
125 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
126 #endif
127 {
128 jvmtiEventCallbacks eventCallbacks;
129 jvmtiEnv* jvmti;
130 JNIEnv* jni;
131
132 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
133 return JNI_ERR;
134
135 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
136
137 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
138 return JNI_ERR;
139
140 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
141 return JNI_ERR;
142
143 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "attach045-agent02-eventsCounterMonitor", &eventsCounterMonitor))) {
144 return JNI_ERR;
145 }
146
147 memset(&eventCallbacks,0, sizeof(eventCallbacks));
148 eventCallbacks.ThreadStart = threadStartHandler;
149 eventCallbacks.ThreadEnd = threadEndHandler;
150 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) {
151 return JNI_ERR;
152 }
153
154 if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
155 return JNI_ERR;
156 }
157
158 NSK_DISPLAY1("%s: initialization was done\n", agentName);
159
160 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
161 return JNI_ERR;
162
163 return JNI_OK;
164 }
165
166
167 }
|
45 static const int testEventsNumber = 2;
46
47 static jrawMonitorID eventsCounterMonitor;
48
49 static volatile int eventsCounter = 0;
50
51 void eventHandler(jvmtiEnv *jvmti,
52 JNIEnv* jni,
53 jthread thread,
54 int threadStartEvent) {
55 char threadName[MAX_STRING_LENGTH];
56 int success = 1;
57 jint threadsCount = 0;
58 jthread * threads;
59
60 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
61 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
62 return;
63 }
64
65 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threadsCount, &threads))) {
66 NSK_COMPLAIN1("%s: failed to get all threads\n", agentName);
67 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
68 return;
69 }
70
71 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
72
73 if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {
74
75 eventsCounter++;
76
77 if (threadStartEvent)
78 NSK_DISPLAY3("%s: ThreadStart event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
79 else
80 NSK_DISPLAY3("%s: ThreadEnd event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
81
82 if (eventsCounter == EXPECTED_EVENTS_NUMBER) {
83 NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);
84
85 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
86 }
87
88 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {
89 success = 0;
90 }
91 } else {
92 success = 0;
93 }
94
95 if (!success) {
96 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
97 }
98 }
99
100 void JNICALL threadStartHandler(
101 jvmtiEnv *jvmti,
102 JNIEnv* jni,
103 jthread thread) {
104 eventHandler(jvmti, jni, thread, 1);
105 }
106
107 void JNICALL threadEndHandler(
108 jvmtiEnv *jvmti,
122 Agent_OnAttach_attach045Agent02(JavaVM *vm, char *optionsString, void *reserved)
123 #else
124 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
125 #endif
126 {
127 jvmtiEventCallbacks eventCallbacks;
128 jvmtiEnv* jvmti;
129 JNIEnv* jni;
130
131 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
132 return JNI_ERR;
133
134 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
135
136 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
137 return JNI_ERR;
138
139 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
140 return JNI_ERR;
141
142 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent02-eventsCounterMonitor", &eventsCounterMonitor))) {
143 return JNI_ERR;
144 }
145
146 memset(&eventCallbacks,0, sizeof(eventCallbacks));
147 eventCallbacks.ThreadStart = threadStartHandler;
148 eventCallbacks.ThreadEnd = threadEndHandler;
149 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
150 return JNI_ERR;
151 }
152
153 if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
154 return JNI_ERR;
155 }
156
157 NSK_DISPLAY1("%s: initialization was done\n", agentName);
158
159 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
160 return JNI_ERR;
161
162 return JNI_OK;
163 }
164
165
166 }
|