118 return;
119
120 NSK_DISPLAY0("Clean data\n");
121 if (!clean()) {
122 nsk_jvmti_setFailStatus();
123 return;
124 }
125 }
126
127 NSK_DISPLAY0("Let debuggee to finish\n");
128 if (!nsk_jvmti_resumeSync())
129 return;
130 }
131
132 /* ============================================================================= */
133
134 /**
135 * Generate missed events (COMPILED_METHOD_LOAD only).
136 */
137 static int generateEvents() {
138 if (!NSK_JVMTI_VERIFY(
139 NSK_CPP_STUB2(GenerateEvents, jvmti, JVMTI_EVENT_COMPILED_METHOD_LOAD))) {
140 nsk_jvmti_setFailStatus();
141 return NSK_FALSE;
142 }
143 return NSK_TRUE;
144 }
145
146 /**
147 * Prepare data.
148 * - clean threads list
149 * - get all live threads
150 * - get threads name
151 * - find tested threads
152 * - make global refs
153 * - enable events
154 */
155 static int prepare() {
156 jthread *allThreadsList = NULL;
157 jint allThreadsCount = 0;
158 int found = 0;
159 int i;
160
161 NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT);
162
163 /* clean threads list */
164 for (i = 0; i < THREADS_COUNT; i++) {
165 threadsDesc[i].thread = (jthread)NULL;
166 threadsDesc[i].method = (jmethodID)NULL;
167 threadsDesc[i].methodCompiled = NSK_FALSE;
168 }
169
170 /* get all live threads */
171 if (!NSK_JVMTI_VERIFY(
172 NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList)))
173 return NSK_FALSE;
174
175 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
176 return NSK_FALSE;
177
178 /* find tested threads */
179 for (i = 0; i < allThreadsCount; i++) {
180 jvmtiThreadInfo threadInfo;
181
182 if (!NSK_VERIFY(allThreadsList[i] != NULL))
183 return NSK_FALSE;
184
185 if (!NSK_JVMTI_VERIFY(
186 NSK_CPP_STUB3(GetThreadInfo, jvmti, allThreadsList[i], &threadInfo)))
187 return NSK_FALSE;
188
189 if (threadInfo.name != NULL) {
190 int j;
191
192 for (j = 0; j < THREADS_COUNT; j++) {
193 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) {
194 threadsDesc[j].thread = allThreadsList[i];
195 NSK_DISPLAY3(" thread #%d (%s): 0x%p\n",
196 j, threadInfo.name, (void*)threadsDesc[j].thread);
197 }
198 }
199 }
200 }
201
202 /* deallocate all threads list */
203 if (!NSK_JVMTI_VERIFY(
204 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList)))
205 return NSK_FALSE;
206
207 /* check if all tested threads found */
208 found = 0;
209 for (i = 0; i < THREADS_COUNT; i++) {
210 if (threadsDesc[i].thread == NULL) {
211 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName);
212 } else {
213 found++;
214 }
215 }
216
217 if (found < THREADS_COUNT)
218 return NSK_FALSE;
219
220 /* get threads class and frame method */
221 NSK_DISPLAY0("Find tested methods:\n");
222 for (i = 0; i < THREADS_COUNT; i++) {
223
224 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls =
225 NSK_CPP_STUB2(GetObjectClass, jni, threadsDesc[i].thread)) != NULL))
226 return NSK_FALSE;
227
228 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method =
229 NSK_CPP_STUB4(GetMethodID, jni, threadsDesc[i].cls,
230 threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL))
231 return NSK_FALSE;
232
233 NSK_DISPLAY4(" thread #%d (%s): 0x%p (%s)\n",
234 i, threadsDesc[i].threadName,
235 (void*)threadsDesc[i].method,
236 threadsDesc[i].methodName);
237 }
238
239 /* make global refs */
240 for (i = 0; i < THREADS_COUNT; i++) {
241 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread)
242 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].thread)) != NULL))
243 return NSK_FALSE;
244 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass)
245 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].cls)) != NULL))
246 return NSK_FALSE;
247 }
248
249 NSK_DISPLAY0("Enable tested events\n");
250 if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL))
251 return NSK_FALSE;
252
253 return NSK_TRUE;
254 }
255
256 /**
257 * Suspend or resume tested threads.
258 */
259 static int suspendThreadsIndividually(int suspend) {
260 int i;
261
262 for (i = 0; i < THREADS_COUNT; i++) {
263 if (suspend) {
264 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsDesc[i].threadName);
265 if (!NSK_JVMTI_VERIFY(
266 NSK_CPP_STUB2(SuspendThread, jvmti, threadsDesc[i].thread)))
267 nsk_jvmti_setFailStatus();
268 } else {
269 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsDesc[i].threadName);
270 if (!NSK_JVMTI_VERIFY(
271 NSK_CPP_STUB2(ResumeThread, jvmti, threadsDesc[i].thread)))
272 nsk_jvmti_setFailStatus();
273 }
274 }
275 return NSK_TRUE;
276 }
277
278 /**
279 * Testcase: check tested threads.
280 * - invoke getFrameCount() for each thread
281 * - check if frameCount is not less than minimal stack depth
282 * - invoke getStackTrace() for each thread
283 * - check if stack depth is equal to frameCount
284 *
285 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
286 */
287 static int checkSuspendedThreads() {
288 char kind[256] = "";
289 int i;
290
291 /* check each thread */
292 for (i = 0; i < THREADS_COUNT; i++) {
293 jint frameCount = 0;
294 jint frameStackSize = 0;
295 jvmtiFrameInfo frameStack[MAX_STACK_SIZE];
296 int found = 0;
297
298 /* make proper kind */
299 strcpy(kind, threadsDesc[i].methodCompiled ? "compiled " : "not compiled ");
300 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsDesc[i].threadName);
301
302 /* get frame count */
303 if (!NSK_JVMTI_VERIFY(
304 NSK_CPP_STUB3(GetFrameCount, jvmti,
305 threadsDesc[i].thread, &frameCount))) {
306 nsk_jvmti_setFailStatus();
307 return NSK_TRUE;
308 }
309
310 NSK_DISPLAY1(" frameCount: %d\n", (int)frameCount);
311
312 /* get stack trace */
313 if (!NSK_JVMTI_VERIFY(
314 NSK_CPP_STUB6(GetStackTrace, jvmti, threadsDesc[i].thread,
315 0, MAX_STACK_SIZE, frameStack, &frameStackSize))) {
316 nsk_jvmti_setFailStatus();
317 return NSK_TRUE;
318 }
319
320 NSK_DISPLAY1(" stack depth: %d\n", (int)frameStackSize);
321
322 /* check frame count */
323 if (frameCount < threadsDesc[i].minDepth) {
324 NSK_COMPLAIN5("Too few frameCount of %s thread #%d (%s):\n"
325 "# got frameCount: %d\n"
326 "# expected minimum: %d\n",
327 kind, i, threadsDesc[i].threadName,
328 (int)frameCount, threadsDesc[i].minDepth);
329 nsk_jvmti_setFailStatus();
330 }
331
332 if (frameStackSize != frameCount) {
333 NSK_COMPLAIN5("Different frames count for %s thread #%d (%s):\n"
334 "# getStackTrace(): %d\n"
335 "# getFrameCount(): %d\n",
341 }
342
343 /* test may continue */
344 return NSK_TRUE;
345 }
346
347 /**
348 * Clean data.
349 * - disable events
350 * - dispose global references to tested threads
351 */
352 static int clean() {
353 int i;
354
355 NSK_DISPLAY0("Disable events\n");
356 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL))
357 return NSK_FALSE;
358
359 NSK_DISPLAY0("Dispose global references to threads\n");
360 for (i = 0; i < THREADS_COUNT; i++) {
361 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].thread));
362 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].cls));
363 }
364
365 return NSK_TRUE;
366 }
367
368 /* ============================================================================= */
369
370 /**
371 * COMPILED_METHOD_LOAD callback.
372 * - turn on flag that method is compiled
373 */
374 JNIEXPORT void JNICALL
375 callbackCompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method,
376 jint code_size, const void* code_addr,
377 jint map_length, const jvmtiAddrLocationMap* map,
378 const void* compile_info) {
379 int i;
380
381 /* check if event is for tested method and turn flag on */
382 for (i = 0; i < THREADS_COUNT; i++) {
475 JNIEXPORT jint JNI_OnLoad_sp06t001(JavaVM *jvm, char *options, void *reserved) {
476 return JNI_VERSION_1_8;
477 }
478 #endif
479 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
480
481 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
482 return JNI_ERR;
483
484 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
485
486 if (!NSK_VERIFY((jvmti =
487 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
488 return JNI_ERR;
489
490 {
491 jvmtiCapabilities caps;
492 memset(&caps, 0, sizeof(caps));
493 caps.can_suspend = 1;
494 caps.can_generate_compiled_method_load_events = 1;
495 if (!NSK_JVMTI_VERIFY(
496 NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)))
497 return JNI_ERR;
498 }
499
500 {
501 jvmtiEventCallbacks eventCallbacks;
502 memset(&eventCallbacks, 0, sizeof(eventCallbacks));
503 eventCallbacks.CompiledMethodLoad = callbackCompiledMethodLoad;
504 eventCallbacks.CompiledMethodUnload = callbackCompiledMethodUnload;
505 if (!NSK_JVMTI_VERIFY(
506 NSK_CPP_STUB3(SetEventCallbacks, jvmti,
507 &eventCallbacks, sizeof(eventCallbacks))))
508 return JNI_ERR;
509 }
510
511 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
512 return JNI_ERR;
513
514 return JNI_OK;
515 }
516
517 /* ============================================================================= */
518
519 }
|
118 return;
119
120 NSK_DISPLAY0("Clean data\n");
121 if (!clean()) {
122 nsk_jvmti_setFailStatus();
123 return;
124 }
125 }
126
127 NSK_DISPLAY0("Let debuggee to finish\n");
128 if (!nsk_jvmti_resumeSync())
129 return;
130 }
131
132 /* ============================================================================= */
133
134 /**
135 * Generate missed events (COMPILED_METHOD_LOAD only).
136 */
137 static int generateEvents() {
138 if (!NSK_JVMTI_VERIFY(jvmti->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD))) {
139 nsk_jvmti_setFailStatus();
140 return NSK_FALSE;
141 }
142 return NSK_TRUE;
143 }
144
145 /**
146 * Prepare data.
147 * - clean threads list
148 * - get all live threads
149 * - get threads name
150 * - find tested threads
151 * - make global refs
152 * - enable events
153 */
154 static int prepare() {
155 jthread *allThreadsList = NULL;
156 jint allThreadsCount = 0;
157 int found = 0;
158 int i;
159
160 NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT);
161
162 /* clean threads list */
163 for (i = 0; i < THREADS_COUNT; i++) {
164 threadsDesc[i].thread = (jthread)NULL;
165 threadsDesc[i].method = (jmethodID)NULL;
166 threadsDesc[i].methodCompiled = NSK_FALSE;
167 }
168
169 /* get all live threads */
170 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&allThreadsCount, &allThreadsList)))
171 return NSK_FALSE;
172
173 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
174 return NSK_FALSE;
175
176 /* find tested threads */
177 for (i = 0; i < allThreadsCount; i++) {
178 jvmtiThreadInfo threadInfo;
179
180 if (!NSK_VERIFY(allThreadsList[i] != NULL))
181 return NSK_FALSE;
182
183 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(allThreadsList[i], &threadInfo)))
184 return NSK_FALSE;
185
186 if (threadInfo.name != NULL) {
187 int j;
188
189 for (j = 0; j < THREADS_COUNT; j++) {
190 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) {
191 threadsDesc[j].thread = allThreadsList[i];
192 NSK_DISPLAY3(" thread #%d (%s): 0x%p\n",
193 j, threadInfo.name, (void*)threadsDesc[j].thread);
194 }
195 }
196 }
197 }
198
199 /* deallocate all threads list */
200 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)allThreadsList)))
201 return NSK_FALSE;
202
203 /* check if all tested threads found */
204 found = 0;
205 for (i = 0; i < THREADS_COUNT; i++) {
206 if (threadsDesc[i].thread == NULL) {
207 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName);
208 } else {
209 found++;
210 }
211 }
212
213 if (found < THREADS_COUNT)
214 return NSK_FALSE;
215
216 /* get threads class and frame method */
217 NSK_DISPLAY0("Find tested methods:\n");
218 for (i = 0; i < THREADS_COUNT; i++) {
219
220 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls =
221 jni->GetObjectClass(threadsDesc[i].thread)) != NULL))
222 return NSK_FALSE;
223
224 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method =
225 jni->GetMethodID(threadsDesc[i].cls, threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL))
226 return NSK_FALSE;
227
228 NSK_DISPLAY4(" thread #%d (%s): 0x%p (%s)\n",
229 i, threadsDesc[i].threadName,
230 (void*)threadsDesc[i].method,
231 threadsDesc[i].methodName);
232 }
233
234 /* make global refs */
235 for (i = 0; i < THREADS_COUNT; i++) {
236 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread)
237 jni->NewGlobalRef(threadsDesc[i].thread)) != NULL))
238 return NSK_FALSE;
239 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass)
240 jni->NewGlobalRef(threadsDesc[i].cls)) != NULL))
241 return NSK_FALSE;
242 }
243
244 NSK_DISPLAY0("Enable tested events\n");
245 if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL))
246 return NSK_FALSE;
247
248 return NSK_TRUE;
249 }
250
251 /**
252 * Suspend or resume tested threads.
253 */
254 static int suspendThreadsIndividually(int suspend) {
255 int i;
256
257 for (i = 0; i < THREADS_COUNT; i++) {
258 if (suspend) {
259 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsDesc[i].threadName);
260 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(threadsDesc[i].thread)))
261 nsk_jvmti_setFailStatus();
262 } else {
263 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsDesc[i].threadName);
264 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(threadsDesc[i].thread)))
265 nsk_jvmti_setFailStatus();
266 }
267 }
268 return NSK_TRUE;
269 }
270
271 /**
272 * Testcase: check tested threads.
273 * - invoke getFrameCount() for each thread
274 * - check if frameCount is not less than minimal stack depth
275 * - invoke getStackTrace() for each thread
276 * - check if stack depth is equal to frameCount
277 *
278 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
279 */
280 static int checkSuspendedThreads() {
281 char kind[256] = "";
282 int i;
283
284 /* check each thread */
285 for (i = 0; i < THREADS_COUNT; i++) {
286 jint frameCount = 0;
287 jint frameStackSize = 0;
288 jvmtiFrameInfo frameStack[MAX_STACK_SIZE];
289 int found = 0;
290
291 /* make proper kind */
292 strcpy(kind, threadsDesc[i].methodCompiled ? "compiled " : "not compiled ");
293 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsDesc[i].threadName);
294
295 /* get frame count */
296 if (!NSK_JVMTI_VERIFY(jvmti->GetFrameCount(threadsDesc[i].thread, &frameCount))) {
297 nsk_jvmti_setFailStatus();
298 return NSK_TRUE;
299 }
300
301 NSK_DISPLAY1(" frameCount: %d\n", (int)frameCount);
302
303 /* get stack trace */
304 if (!NSK_JVMTI_VERIFY(
305 jvmti->GetStackTrace(threadsDesc[i].thread, 0, MAX_STACK_SIZE, frameStack, &frameStackSize))) {
306 nsk_jvmti_setFailStatus();
307 return NSK_TRUE;
308 }
309
310 NSK_DISPLAY1(" stack depth: %d\n", (int)frameStackSize);
311
312 /* check frame count */
313 if (frameCount < threadsDesc[i].minDepth) {
314 NSK_COMPLAIN5("Too few frameCount of %s thread #%d (%s):\n"
315 "# got frameCount: %d\n"
316 "# expected minimum: %d\n",
317 kind, i, threadsDesc[i].threadName,
318 (int)frameCount, threadsDesc[i].minDepth);
319 nsk_jvmti_setFailStatus();
320 }
321
322 if (frameStackSize != frameCount) {
323 NSK_COMPLAIN5("Different frames count for %s thread #%d (%s):\n"
324 "# getStackTrace(): %d\n"
325 "# getFrameCount(): %d\n",
331 }
332
333 /* test may continue */
334 return NSK_TRUE;
335 }
336
337 /**
338 * Clean data.
339 * - disable events
340 * - dispose global references to tested threads
341 */
342 static int clean() {
343 int i;
344
345 NSK_DISPLAY0("Disable events\n");
346 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL))
347 return NSK_FALSE;
348
349 NSK_DISPLAY0("Dispose global references to threads\n");
350 for (i = 0; i < THREADS_COUNT; i++) {
351 NSK_TRACE(jni->DeleteGlobalRef(threadsDesc[i].thread));
352 NSK_TRACE(jni->DeleteGlobalRef(threadsDesc[i].cls));
353 }
354
355 return NSK_TRUE;
356 }
357
358 /* ============================================================================= */
359
360 /**
361 * COMPILED_METHOD_LOAD callback.
362 * - turn on flag that method is compiled
363 */
364 JNIEXPORT void JNICALL
365 callbackCompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method,
366 jint code_size, const void* code_addr,
367 jint map_length, const jvmtiAddrLocationMap* map,
368 const void* compile_info) {
369 int i;
370
371 /* check if event is for tested method and turn flag on */
372 for (i = 0; i < THREADS_COUNT; i++) {
465 JNIEXPORT jint JNI_OnLoad_sp06t001(JavaVM *jvm, char *options, void *reserved) {
466 return JNI_VERSION_1_8;
467 }
468 #endif
469 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
470
471 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
472 return JNI_ERR;
473
474 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
475
476 if (!NSK_VERIFY((jvmti =
477 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
478 return JNI_ERR;
479
480 {
481 jvmtiCapabilities caps;
482 memset(&caps, 0, sizeof(caps));
483 caps.can_suspend = 1;
484 caps.can_generate_compiled_method_load_events = 1;
485 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
486 return JNI_ERR;
487 }
488
489 {
490 jvmtiEventCallbacks eventCallbacks;
491 memset(&eventCallbacks, 0, sizeof(eventCallbacks));
492 eventCallbacks.CompiledMethodLoad = callbackCompiledMethodLoad;
493 eventCallbacks.CompiledMethodUnload = callbackCompiledMethodUnload;
494 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))
495 return JNI_ERR;
496 }
497
498 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
499 return JNI_ERR;
500
501 return JNI_OK;
502 }
503
504 /* ============================================================================= */
505
506 }
|