37 /* scaffold objects */
38 static jlong timeout = 0;
39
40 /* test objects */
41 static jthread thread = NULL;
42 static jmethodID method = NULL;
43 static int BreakpointEventsCount = 0;
44 static int FramePopEventsCount = 0;
45
46 /* ========================================================================== */
47
48 /** callback functions **/
49
50 static void JNICALL
51 Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
52 jthread thread, jmethodID method, jlocation location) {
53 char *name = NULL;
54 char *signature = NULL;
55
56 BreakpointEventsCount++;
57 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
58 jvmti_env, method, &name, &signature, NULL))) {
59 nsk_jvmti_setFailStatus();
60 }
61
62 NSK_DISPLAY2("Breakpoint event: %s%s\n", name, signature);
63
64 if (name != NULL)
65 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name);
66 if (signature != NULL)
67 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
68
69 switch(BreakpointEventsCount) {
70 case 1:
71 NSK_DISPLAY0("Testcase #1: FramePop in both agents\n");
72 break;
73
74 case 2:
75 NSK_DISPLAY0("Testcase #2: w/o NotifyFramePop in 2nd agent\n");
76 break;
77
78 case 3:
79 NSK_DISPLAY0("Testcase #3: FramePop disabled in 2nd agent\n");
80 break;
81
82 default:
83 NSK_COMPLAIN0("Should no reach here");
84 nsk_jvmti_setFailStatus();
85 break;
86 }
87
88 if (!NSK_JVMTI_VERIFY(
89 NSK_CPP_STUB3(NotifyFramePop, jvmti_env, thread, 0))) {
90 nsk_jvmti_setFailStatus();
91 }
92 }
93
94 static void JNICALL
95 FramePop(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
96 jthread thread, jmethodID method,
97 jboolean wasPopedByException) {
98 char *name = NULL;
99 char *signature = NULL;
100
101 FramePopEventsCount++;
102 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
103 jvmti_env, method, &name, &signature, NULL))) {
104 nsk_jvmti_setFailStatus();
105 return;
106 }
107
108 NSK_DISPLAY2("FramePop event: %s%s\n", name, signature);
109
110 if (name != NULL)
111 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name);
112 if (signature != NULL)
113 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
114 }
115
116 /* ========================================================================== */
117
118 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
119 const char* THREAD_NAME = "Debuggee Thread";
120 jvmtiThreadInfo info;
121 jthread *threads = NULL;
122 jclass klass = NULL;
123 jint threads_count = 0;
124 int i;
125
126 NSK_DISPLAY0("Prepare: find tested thread\n");
127
128 /* get all live threads */
129 if (!NSK_JVMTI_VERIFY(
130 NSK_CPP_STUB3(GetAllThreads, jvmti, &threads_count, &threads)))
131 return NSK_FALSE;
132
133 if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
134 return NSK_FALSE;
135
136 /* find tested thread */
137 for (i = 0; i < threads_count; i++) {
138 if (!NSK_VERIFY(threads[i] != NULL))
139 return NSK_FALSE;
140
141 /* get thread information */
142 if (!NSK_JVMTI_VERIFY(
143 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info)))
144 return NSK_FALSE;
145
146 NSK_DISPLAY3(" thread #%d (%s): %p\n", i, info.name, threads[i]);
147
148 /* find by name */
149 if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
150 thread = threads[i];
151 }
152
153 if (info.name != NULL) {
154 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(
155 Deallocate, jvmti, (unsigned char*)info.name)))
156 return NSK_FALSE;
157 }
158 }
159
160 /* deallocate threads list */
161 if (!NSK_JVMTI_VERIFY(
162 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads)))
163 return NSK_FALSE;
164
165 if (thread == NULL) {
166 NSK_COMPLAIN0("Debuggee thread not found");
167 return NSK_FALSE;
168 }
169
170 if (!NSK_JNI_VERIFY(jni, (thread =
171 NSK_CPP_STUB2(NewGlobalRef, jni, thread)) != NULL))
172 return NSK_FALSE;
173
174 /* get tested thread class */
175 if (!NSK_JNI_VERIFY(jni, (klass =
176 NSK_CPP_STUB2(GetObjectClass, jni, thread)) != NULL))
177 return NSK_FALSE;
178
179 /* get tested thread method 'checkPoint' */
180 if (!NSK_JNI_VERIFY(jni, (method = NSK_CPP_STUB4(
181 GetMethodID, jni, klass, "checkPoint", "()V")) != NULL))
182 return NSK_FALSE;
183
184 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetBreakpoint, jvmti, method, 0)))
185 return NSK_FALSE;
186
187 /* enable events */
188 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
189 jvmti, JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
190 return NSK_FALSE;
191 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
192 jvmti, JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, NULL)))
193 return NSK_FALSE;
194
195 return NSK_TRUE;
196 }
197
198 /* ========================================================================== */
199
200 /** Agent algorithm. */
201 static void JNICALL
202 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
203
204 if (!nsk_jvmti_waitForSync(timeout))
205 return;
206
207 if (!prepare(jvmti, jni)) {
208 nsk_jvmti_setFailStatus();
209 return;
210 }
211
212 /* resume debugee and wait for sync */
213 if (!nsk_jvmti_resumeSync())
214 return;
215 if (!nsk_jvmti_waitForSync(timeout))
216 return;
217
218 if (FramePopEventsCount == 0) {
219 NSK_COMPLAIN0("No FramePop events\n");
220 nsk_jvmti_setFailStatus();
221 } else if (FramePopEventsCount != 3) {
222 NSK_COMPLAIN1("Some of FramePop events were missed: %d\n",
223 FramePopEventsCount);
224 nsk_jvmti_setFailStatus();
225 }
226
227 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
228 jvmti, JVMTI_DISABLE, JVMTI_EVENT_FRAME_POP, NULL)))
229 nsk_jvmti_setFailStatus();
230
231 NSK_TRACE(NSK_CPP_STUB3(ClearBreakpoint, jvmti, method, 0));
232 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, thread));
233
234 if (!nsk_jvmti_resumeSync())
235 return;
236 }
237
238 /* ========================================================================== */
239
240 /** Agent library initialization. */
241 #ifdef STATIC_BUILD
242 JNIEXPORT jint JNICALL Agent_OnLoad_ma05t001(JavaVM *jvm, char *options, void *reserved) {
243 return Agent_Initialize(jvm, options, reserved);
244 }
245 JNIEXPORT jint JNICALL Agent_OnAttach_ma05t001(JavaVM *jvm, char *options, void *reserved) {
246 return Agent_Initialize(jvm, options, reserved);
247 }
248 JNIEXPORT jint JNI_OnLoad_ma05t001(JavaVM *jvm, char *options, void *reserved) {
249 return JNI_VERSION_1_8;
250 }
251 #endif
252 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
253 jvmtiEnv* jvmti = NULL;
254 jvmtiEventCallbacks callbacks;
255 jvmtiCapabilities caps;
256
257 NSK_DISPLAY0("Agent_OnLoad\n");
258
259 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
260 return JNI_ERR;
261
262 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
263
264 if (!NSK_VERIFY((jvmti =
265 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
266 return JNI_ERR;
267
268 memset(&caps, 0, sizeof(caps));
269 caps.can_generate_breakpoint_events = 1;
270 caps.can_generate_frame_pop_events = 1;
271 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)))
272 return JNI_ERR;
273
274 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
275 return JNI_ERR;
276
277 memset(&callbacks, 0, sizeof(callbacks));
278 callbacks.Breakpoint = &Breakpoint;
279 callbacks.FramePop = &FramePop;
280 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
281 return JNI_ERR;
282
283 return JNI_OK;
284 }
285
286 /* ========================================================================== */
287
288 }
|
37 /* scaffold objects */
38 static jlong timeout = 0;
39
40 /* test objects */
41 static jthread thread = NULL;
42 static jmethodID method = NULL;
43 static int BreakpointEventsCount = 0;
44 static int FramePopEventsCount = 0;
45
46 /* ========================================================================== */
47
48 /** callback functions **/
49
50 static void JNICALL
51 Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
52 jthread thread, jmethodID method, jlocation location) {
53 char *name = NULL;
54 char *signature = NULL;
55
56 BreakpointEventsCount++;
57 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
58 nsk_jvmti_setFailStatus();
59 }
60
61 NSK_DISPLAY2("Breakpoint event: %s%s\n", name, signature);
62
63 if (name != NULL)
64 jvmti_env->Deallocate((unsigned char*)name);
65 if (signature != NULL)
66 jvmti_env->Deallocate((unsigned char*)signature);
67
68 switch(BreakpointEventsCount) {
69 case 1:
70 NSK_DISPLAY0("Testcase #1: FramePop in both agents\n");
71 break;
72
73 case 2:
74 NSK_DISPLAY0("Testcase #2: w/o NotifyFramePop in 2nd agent\n");
75 break;
76
77 case 3:
78 NSK_DISPLAY0("Testcase #3: FramePop disabled in 2nd agent\n");
79 break;
80
81 default:
82 NSK_COMPLAIN0("Should no reach here");
83 nsk_jvmti_setFailStatus();
84 break;
85 }
86
87 if (!NSK_JVMTI_VERIFY(jvmti_env->NotifyFramePop(thread, 0))) {
88 nsk_jvmti_setFailStatus();
89 }
90 }
91
92 static void JNICALL
93 FramePop(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
94 jthread thread, jmethodID method,
95 jboolean wasPopedByException) {
96 char *name = NULL;
97 char *signature = NULL;
98
99 FramePopEventsCount++;
100 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
101 nsk_jvmti_setFailStatus();
102 return;
103 }
104
105 NSK_DISPLAY2("FramePop event: %s%s\n", name, signature);
106
107 if (name != NULL)
108 jvmti_env->Deallocate((unsigned char*)name);
109 if (signature != NULL)
110 jvmti_env->Deallocate((unsigned char*)signature);
111 }
112
113 /* ========================================================================== */
114
115 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
116 const char* THREAD_NAME = "Debuggee Thread";
117 jvmtiThreadInfo info;
118 jthread *threads = NULL;
119 jclass klass = NULL;
120 jint threads_count = 0;
121 int i;
122
123 NSK_DISPLAY0("Prepare: find tested thread\n");
124
125 /* get all live threads */
126 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threads_count, &threads)))
127 return NSK_FALSE;
128
129 if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
130 return NSK_FALSE;
131
132 /* find tested thread */
133 for (i = 0; i < threads_count; i++) {
134 if (!NSK_VERIFY(threads[i] != NULL))
135 return NSK_FALSE;
136
137 /* get thread information */
138 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))
139 return NSK_FALSE;
140
141 NSK_DISPLAY3(" thread #%d (%s): %p\n", i, info.name, threads[i]);
142
143 /* find by name */
144 if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
145 thread = threads[i];
146 }
147
148 if (info.name != NULL) {
149 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)info.name)))
150 return NSK_FALSE;
151 }
152 }
153
154 /* deallocate threads list */
155 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))
156 return NSK_FALSE;
157
158 if (thread == NULL) {
159 NSK_COMPLAIN0("Debuggee thread not found");
160 return NSK_FALSE;
161 }
162
163 if (!NSK_JNI_VERIFY(jni, (thread = jni->NewGlobalRef(thread)) != NULL))
164 return NSK_FALSE;
165
166 /* get tested thread class */
167 if (!NSK_JNI_VERIFY(jni, (klass = jni->GetObjectClass(thread)) != NULL))
168 return NSK_FALSE;
169
170 /* get tested thread method 'checkPoint' */
171 if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "checkPoint", "()V")) != NULL))
172 return NSK_FALSE;
173
174 if (!NSK_JVMTI_VERIFY(jvmti->SetBreakpoint(method, 0)))
175 return NSK_FALSE;
176
177 /* enable events */
178 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
179 return NSK_FALSE;
180 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, NULL)))
181 return NSK_FALSE;
182
183 return NSK_TRUE;
184 }
185
186 /* ========================================================================== */
187
188 /** Agent algorithm. */
189 static void JNICALL
190 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
191
192 if (!nsk_jvmti_waitForSync(timeout))
193 return;
194
195 if (!prepare(jvmti, jni)) {
196 nsk_jvmti_setFailStatus();
197 return;
198 }
199
200 /* resume debugee and wait for sync */
201 if (!nsk_jvmti_resumeSync())
202 return;
203 if (!nsk_jvmti_waitForSync(timeout))
204 return;
205
206 if (FramePopEventsCount == 0) {
207 NSK_COMPLAIN0("No FramePop events\n");
208 nsk_jvmti_setFailStatus();
209 } else if (FramePopEventsCount != 3) {
210 NSK_COMPLAIN1("Some of FramePop events were missed: %d\n",
211 FramePopEventsCount);
212 nsk_jvmti_setFailStatus();
213 }
214
215 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_FRAME_POP, NULL)))
216 nsk_jvmti_setFailStatus();
217
218 NSK_TRACE(jvmti->ClearBreakpoint(method, 0));
219 NSK_TRACE(jni->DeleteGlobalRef(thread));
220
221 if (!nsk_jvmti_resumeSync())
222 return;
223 }
224
225 /* ========================================================================== */
226
227 /** Agent library initialization. */
228 #ifdef STATIC_BUILD
229 JNIEXPORT jint JNICALL Agent_OnLoad_ma05t001(JavaVM *jvm, char *options, void *reserved) {
230 return Agent_Initialize(jvm, options, reserved);
231 }
232 JNIEXPORT jint JNICALL Agent_OnAttach_ma05t001(JavaVM *jvm, char *options, void *reserved) {
233 return Agent_Initialize(jvm, options, reserved);
234 }
235 JNIEXPORT jint JNI_OnLoad_ma05t001(JavaVM *jvm, char *options, void *reserved) {
236 return JNI_VERSION_1_8;
237 }
238 #endif
239 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
240 jvmtiEnv* jvmti = NULL;
241 jvmtiEventCallbacks callbacks;
242 jvmtiCapabilities caps;
243
244 NSK_DISPLAY0("Agent_OnLoad\n");
245
246 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
247 return JNI_ERR;
248
249 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
250
251 if (!NSK_VERIFY((jvmti =
252 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
253 return JNI_ERR;
254
255 memset(&caps, 0, sizeof(caps));
256 caps.can_generate_breakpoint_events = 1;
257 caps.can_generate_frame_pop_events = 1;
258 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
259 return JNI_ERR;
260
261 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
262 return JNI_ERR;
263
264 memset(&callbacks, 0, sizeof(callbacks));
265 callbacks.Breakpoint = &Breakpoint;
266 callbacks.FramePop = &FramePop;
267 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
268 return JNI_ERR;
269
270 return JNI_OK;
271 }
272
273 /* ========================================================================== */
274
275 }
|