141 * - get threads name
142 * - find tested threads
143 * - make global refs
144 */
145 static int prepare() {
146 jthread *allThreadsList = NULL;
147 jint allThreadsCount = 0;
148 int found = 0;
149 int i;
150
151 NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT);
152
153 /* clean threads list */
154 for (i = 0; i < THREADS_COUNT; i++) {
155 threadsDesc[i].thread = (jthread)NULL;
156 threadsDesc[i].method = (jmethodID)NULL;
157 threadsDesc[i].location = NSK_JVMTI_INVALID_JLOCATION;
158 }
159
160 /* get all live threads */
161 if (!NSK_JVMTI_VERIFY(
162 NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList)))
163 return NSK_FALSE;
164
165 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
166 return NSK_FALSE;
167
168 /* find tested threads */
169 for (i = 0; i < allThreadsCount; i++) {
170 jvmtiThreadInfo threadInfo;
171
172 if (!NSK_VERIFY(allThreadsList[i] != NULL))
173 return NSK_FALSE;
174
175 /* get thread name (info) */
176 if (!NSK_JVMTI_VERIFY(
177 NSK_CPP_STUB3(GetThreadInfo, jvmti, allThreadsList[i], &threadInfo)))
178 return NSK_FALSE;
179
180 /* find by name */
181 if (threadInfo.name != NULL) {
182 int j;
183
184 for (j = 0; j < THREADS_COUNT; j++) {
185 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) {
186 threadsDesc[j].thread = allThreadsList[i];
187 NSK_DISPLAY3(" thread #%d (%s): %p\n",
188 j, threadInfo.name, (void*)threadsDesc[j].thread);
189 }
190 }
191 }
192 }
193
194 /* deallocate all threads list */
195 if (!NSK_JVMTI_VERIFY(
196 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList)))
197 return NSK_FALSE;
198
199 /* check if all tested threads found */
200 found = 0;
201 for (i = 0; i < THREADS_COUNT; i++) {
202 if (threadsDesc[i].thread == NULL) {
203 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName);
204 } else {
205 found++;
206 }
207 }
208
209 if (found < THREADS_COUNT)
210 return NSK_FALSE;
211
212 /* get threads class and frame method */
213 NSK_DISPLAY0("Find tested methods:\n");
214 for (i = 0; i < THREADS_COUNT; i++) {
215 /* get thread class */
216 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls =
217 NSK_CPP_STUB2(GetObjectClass, jni, threadsDesc[i].thread)) != NULL))
218 return NSK_FALSE;
219 /* get frame method */
220 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method =
221 NSK_CPP_STUB4(GetMethodID, jni, threadsDesc[i].cls,
222 threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL))
223 return NSK_FALSE;
224
225 NSK_DISPLAY4(" thread #%d (%s): %p (%s)\n",
226 i, threadsDesc[i].threadName,
227 (void*)threadsDesc[i].method,
228 threadsDesc[i].methodName);
229 }
230
231 /* make global refs */
232 for (i = 0; i < THREADS_COUNT; i++) {
233 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread)
234 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].thread)) != NULL))
235 return NSK_FALSE;
236 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass)
237 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].cls)) != NULL))
238 return NSK_FALSE;
239 }
240
241 return NSK_TRUE;
242 }
243
244 /**
245 * Suspend or resume tested threads.
246 */
247 static int suspendThreadsIndividually(int suspend) {
248 int i;
249
250 for (i = 0; i < THREADS_COUNT; i++) {
251 if (suspend) {
252 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsDesc[i].threadName);
253 if (!NSK_JVMTI_VERIFY(
254 NSK_CPP_STUB2(SuspendThread, jvmti, threadsDesc[i].thread)))
255 nsk_jvmti_setFailStatus();
256 } else {
257 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsDesc[i].threadName);
258 if (!NSK_JVMTI_VERIFY(
259 NSK_CPP_STUB2(ResumeThread, jvmti, threadsDesc[i].thread)))
260 nsk_jvmti_setFailStatus();
261 }
262 }
263 return NSK_TRUE;
264 }
265
266 /**
267 * Testcase: check tested threads
268 * - call GetFrameCount() and getStackTrace()
269 * - for suspended thread compare number of stack frames returned
270 * - find stack frames with expected methodID
271 *
272 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
273 */
274 static int checkThreads(int suspended, const char* kind) {
275 int i;
276
277 /* check each thread */
278 for (i = 0; i < THREADS_COUNT; i++) {
279 jint frameCount = 0;
280 jint frameStackSize = 0;
281 jvmtiFrameInfo frameStack[MAX_STACK_SIZE];
282 int found = 0;
283 int j;
284
285 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsDesc[i].threadName);
286
287 /* get frame count */
288 if (!NSK_JVMTI_VERIFY(
289 NSK_CPP_STUB3(GetFrameCount, jvmti,
290 threadsDesc[i].thread, &frameCount))) {
291 nsk_jvmti_setFailStatus();
292 return NSK_TRUE;
293 }
294
295 NSK_DISPLAY1(" frameCount: %d\n", (int)frameCount);
296
297 /* get stack trace */
298 if (!NSK_JVMTI_VERIFY(
299 NSK_CPP_STUB6(GetStackTrace, jvmti, threadsDesc[i].thread,
300 0, MAX_STACK_SIZE, frameStack, &frameStackSize))) {
301 nsk_jvmti_setFailStatus();
302 return NSK_TRUE;
303 }
304
305 NSK_DISPLAY1(" stack depth: %d\n", (int)frameStackSize);
306
307 /* Only check for suspended threads: running threads might have different
308 frames between stack grabbing calls. */
309 if (suspended && (frameStackSize != frameCount)) {
310 NSK_COMPLAIN5("Different frames count for %s thread #%d (%s):\n"
311 "# getStackTrace(): %d\n"
312 "# getFrameCount(): %d\n",
313 kind, i, threadsDesc[i].threadName,
314 (int)frameStackSize, (int)frameCount);
315 nsk_jvmti_setFailStatus();
316 }
317
318 /* find method on the stack */
319 found = 0;
320 for (j = 0; j < frameStackSize; j++) {
340 "# expected: %d\n",
341 kind, i, threadsDesc[i].threadName,
342 found, 1);
343 nsk_jvmti_setFailStatus();
344 }
345 }
346
347 /* test may continue */
348 return NSK_TRUE;
349 }
350
351 /**
352 * Clean data:
353 * - dispose global references to tested threads
354 */
355 static int clean() {
356 int i;
357
358 /* dispose global references to threads */
359 for (i = 0; i < THREADS_COUNT; i++) {
360 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].thread));
361 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].cls));
362 }
363
364 return NSK_TRUE;
365 }
366
367 /* ============================================================================= */
368
369 static volatile int testedThreadRunning = NSK_FALSE;
370 static volatile int testedThreadShouldFinish = NSK_FALSE;
371
372 /** Native running method in tested thread. */
373 JNIEXPORT void JNICALL
374 Java_nsk_jvmti_scenarios_sampling_SP02_sp02t002ThreadRunningNative_testedMethod(JNIEnv* jni,
375 jobject obj) {
376 volatile int i = 0, n = 1000;
377
378 /* run in a loop */
379 testedThreadRunning = NSK_TRUE;
380 while (!testedThreadShouldFinish) {
381 if (n <= 0)
419 }
420 #endif
421 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
422
423 /* init framework and parse options */
424 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
425 return JNI_ERR;
426
427 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
428
429 /* create JVMTI environment */
430 if (!NSK_VERIFY((jvmti =
431 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
432 return JNI_ERR;
433
434 /* add specific capabilities for suspending thread */
435 {
436 jvmtiCapabilities suspendCaps;
437 memset(&suspendCaps, 0, sizeof(suspendCaps));
438 suspendCaps.can_suspend = 1;
439 if (!NSK_JVMTI_VERIFY(
440 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
441 return JNI_ERR;
442 }
443
444 /* register agent proc and arg */
445 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
446 return JNI_ERR;
447
448 return JNI_OK;
449 }
450
451 /* ============================================================================= */
452
453 }
|
141 * - get threads name
142 * - find tested threads
143 * - make global refs
144 */
145 static int prepare() {
146 jthread *allThreadsList = NULL;
147 jint allThreadsCount = 0;
148 int found = 0;
149 int i;
150
151 NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT);
152
153 /* clean threads list */
154 for (i = 0; i < THREADS_COUNT; i++) {
155 threadsDesc[i].thread = (jthread)NULL;
156 threadsDesc[i].method = (jmethodID)NULL;
157 threadsDesc[i].location = NSK_JVMTI_INVALID_JLOCATION;
158 }
159
160 /* get all live threads */
161 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&allThreadsCount, &allThreadsList)))
162 return NSK_FALSE;
163
164 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
165 return NSK_FALSE;
166
167 /* find tested threads */
168 for (i = 0; i < allThreadsCount; i++) {
169 jvmtiThreadInfo threadInfo;
170
171 if (!NSK_VERIFY(allThreadsList[i] != NULL))
172 return NSK_FALSE;
173
174 /* get thread name (info) */
175 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(allThreadsList[i], &threadInfo)))
176 return NSK_FALSE;
177
178 /* find by name */
179 if (threadInfo.name != NULL) {
180 int j;
181
182 for (j = 0; j < THREADS_COUNT; j++) {
183 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) {
184 threadsDesc[j].thread = allThreadsList[i];
185 NSK_DISPLAY3(" thread #%d (%s): %p\n",
186 j, threadInfo.name, (void*)threadsDesc[j].thread);
187 }
188 }
189 }
190 }
191
192 /* deallocate all threads list */
193 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)allThreadsList)))
194 return NSK_FALSE;
195
196 /* check if all tested threads found */
197 found = 0;
198 for (i = 0; i < THREADS_COUNT; i++) {
199 if (threadsDesc[i].thread == NULL) {
200 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName);
201 } else {
202 found++;
203 }
204 }
205
206 if (found < THREADS_COUNT)
207 return NSK_FALSE;
208
209 /* get threads class and frame method */
210 NSK_DISPLAY0("Find tested methods:\n");
211 for (i = 0; i < THREADS_COUNT; i++) {
212 /* get thread class */
213 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls =
214 jni->GetObjectClass(threadsDesc[i].thread)) != NULL))
215 return NSK_FALSE;
216 /* get frame method */
217 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method =
218 jni->GetMethodID(threadsDesc[i].cls, threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL))
219 return NSK_FALSE;
220
221 NSK_DISPLAY4(" thread #%d (%s): %p (%s)\n",
222 i, threadsDesc[i].threadName,
223 (void*)threadsDesc[i].method,
224 threadsDesc[i].methodName);
225 }
226
227 /* make global refs */
228 for (i = 0; i < THREADS_COUNT; i++) {
229 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread)
230 jni->NewGlobalRef(threadsDesc[i].thread)) != NULL))
231 return NSK_FALSE;
232 if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass)
233 jni->NewGlobalRef(threadsDesc[i].cls)) != NULL))
234 return NSK_FALSE;
235 }
236
237 return NSK_TRUE;
238 }
239
240 /**
241 * Suspend or resume tested threads.
242 */
243 static int suspendThreadsIndividually(int suspend) {
244 int i;
245
246 for (i = 0; i < THREADS_COUNT; i++) {
247 if (suspend) {
248 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsDesc[i].threadName);
249 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(threadsDesc[i].thread)))
250 nsk_jvmti_setFailStatus();
251 } else {
252 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsDesc[i].threadName);
253 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(threadsDesc[i].thread)))
254 nsk_jvmti_setFailStatus();
255 }
256 }
257 return NSK_TRUE;
258 }
259
260 /**
261 * Testcase: check tested threads
262 * - call GetFrameCount() and getStackTrace()
263 * - for suspended thread compare number of stack frames returned
264 * - find stack frames with expected methodID
265 *
266 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
267 */
268 static int checkThreads(int suspended, const char* kind) {
269 int i;
270
271 /* check each thread */
272 for (i = 0; i < THREADS_COUNT; i++) {
273 jint frameCount = 0;
274 jint frameStackSize = 0;
275 jvmtiFrameInfo frameStack[MAX_STACK_SIZE];
276 int found = 0;
277 int j;
278
279 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsDesc[i].threadName);
280
281 /* get frame count */
282 if (!NSK_JVMTI_VERIFY(jvmti->GetFrameCount(threadsDesc[i].thread, &frameCount))) {
283 nsk_jvmti_setFailStatus();
284 return NSK_TRUE;
285 }
286
287 NSK_DISPLAY1(" frameCount: %d\n", (int)frameCount);
288
289 /* get stack trace */
290 if (!NSK_JVMTI_VERIFY(
291 jvmti->GetStackTrace(threadsDesc[i].thread, 0, MAX_STACK_SIZE, frameStack, &frameStackSize))) {
292 nsk_jvmti_setFailStatus();
293 return NSK_TRUE;
294 }
295
296 NSK_DISPLAY1(" stack depth: %d\n", (int)frameStackSize);
297
298 /* Only check for suspended threads: running threads might have different
299 frames between stack grabbing calls. */
300 if (suspended && (frameStackSize != frameCount)) {
301 NSK_COMPLAIN5("Different frames count for %s thread #%d (%s):\n"
302 "# getStackTrace(): %d\n"
303 "# getFrameCount(): %d\n",
304 kind, i, threadsDesc[i].threadName,
305 (int)frameStackSize, (int)frameCount);
306 nsk_jvmti_setFailStatus();
307 }
308
309 /* find method on the stack */
310 found = 0;
311 for (j = 0; j < frameStackSize; j++) {
331 "# expected: %d\n",
332 kind, i, threadsDesc[i].threadName,
333 found, 1);
334 nsk_jvmti_setFailStatus();
335 }
336 }
337
338 /* test may continue */
339 return NSK_TRUE;
340 }
341
342 /**
343 * Clean data:
344 * - dispose global references to tested threads
345 */
346 static int clean() {
347 int i;
348
349 /* dispose global references to threads */
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 static volatile int testedThreadRunning = NSK_FALSE;
361 static volatile int testedThreadShouldFinish = NSK_FALSE;
362
363 /** Native running method in tested thread. */
364 JNIEXPORT void JNICALL
365 Java_nsk_jvmti_scenarios_sampling_SP02_sp02t002ThreadRunningNative_testedMethod(JNIEnv* jni,
366 jobject obj) {
367 volatile int i = 0, n = 1000;
368
369 /* run in a loop */
370 testedThreadRunning = NSK_TRUE;
371 while (!testedThreadShouldFinish) {
372 if (n <= 0)
410 }
411 #endif
412 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
413
414 /* init framework and parse options */
415 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
416 return JNI_ERR;
417
418 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
419
420 /* create JVMTI environment */
421 if (!NSK_VERIFY((jvmti =
422 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
423 return JNI_ERR;
424
425 /* add specific capabilities for suspending thread */
426 {
427 jvmtiCapabilities suspendCaps;
428 memset(&suspendCaps, 0, sizeof(suspendCaps));
429 suspendCaps.can_suspend = 1;
430 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))
431 return JNI_ERR;
432 }
433
434 /* register agent proc and arg */
435 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
436 return JNI_ERR;
437
438 return JNI_OK;
439 }
440
441 /* ============================================================================= */
442
443 }
|