Skip to content


Browse files Browse the repository at this point in the history
8278330: dump stack trace if the jvmti test nsk/jvmti/GetThreadState/…
…thrstat002 is failed with wrong thread state

Reviewed-by: amenkov, lmesnik
  • Loading branch information
Serguei Spitsyn committed Dec 8, 2021
1 parent b3faecf commit 3893f4f
Showing 1 changed file with 104 additions and 0 deletions.
Expand Up @@ -48,6 +48,107 @@ static jint state[] = {

#if 1 // support for debug tracing

#define LOG(...) \
{ \
printf(__VA_ARGS__); \
fflush(stdout); \


static void
check_jvmti_status(JNIEnv* jni, jvmtiError err, const char* msg) {
if (err != JVMTI_ERROR_NONE) {
LOG("check_jvmti_status: JVMTI function returned error: %s (%d)\n", TranslateError(err), err);

static void
deallocate(jvmtiEnv *jvmti, JNIEnv* jni, void* ptr) {
jvmtiError err;

err = jvmti->Deallocate((unsigned char*)ptr);
check_jvmti_status(jni, err, "deallocate: error in JVMTI Deallocate call");

static char*
get_method_class_name(jvmtiEnv *jvmti, JNIEnv* jni, jmethodID method) {
jclass klass = NULL;
char* cname = NULL;
char* result = NULL;
jvmtiError err;

err = jvmti->GetMethodDeclaringClass(method, &klass);
check_jvmti_status(jni, err, "get_method_class_name: error in JVMTI GetMethodDeclaringClass");

err = jvmti->GetClassSignature(klass, &cname, NULL);
check_jvmti_status(jni, err, "get_method_class_name: error in JVMTI GetClassSignature");

size_t len = strlen(cname) - 2; // get rid of leading 'L' and trailing ';'

err = jvmti->Allocate((jlong)(len + 1), (unsigned char**)&result);
check_jvmti_status(jni, err, "get_method_class_name: error in JVMTI Allocate");

strncpy(result, cname + 1, len); // skip leading 'L'
result[len] = '\0';
deallocate(jvmti, jni, (void*)cname);
return result;

static void
print_method(jvmtiEnv *jvmti, JNIEnv* jni, jmethodID method, jint depth) {
char* cname = NULL;
char* mname = NULL;
char* msign = NULL;
jvmtiError err;

cname = get_method_class_name(jvmti, jni, method);

err = jvmti->GetMethodName(method, &mname, &msign, NULL);
check_jvmti_status(jni, err, "print_method: error in JVMTI GetMethodName");

LOG("%2d: %s: %s%s\n", depth, cname, mname, msign);
deallocate(jvmti, jni, (void*)cname);
deallocate(jvmti, jni, (void*)mname);
deallocate(jvmti, jni, (void*)msign);

static char*
get_thread_name(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread) {
jvmtiThreadInfo thr_info;
jvmtiError err;

memset(&thr_info, 0, sizeof(thr_info));
err = jvmti->GetThreadInfo(thread, &thr_info);
check_jvmti_status(jni, err, "get_thread_name: error in JVMTI GetThreadInfo call");

return == NULL ? (char*)"<Unnamed thread>" :;

static void
print_stack_trace(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread) {
char* tname = get_thread_name(jvmti, jni, thread);
jint count = 0;
jvmtiError err;

err = jvmti->GetStackTrace(thread, 0, MAX_FRAME_COUNT_PRINT_STACK_TRACE, frames, &count);
check_jvmti_status(jni, err, "print_stack_trace: error in JVMTI GetStackTrace");

LOG("JVMTI Stack Trace for thread %s: frame count: %d\n", tname, count);
for (int depth = 0; depth < count; depth++) {
print_method(jvmti, jni, frames[depth].method, depth);
deallocate(jvmti, jni, (void*)tname);
#endif // support for debug tracing

void printStateFlags(jint flags) {
Expand Down Expand Up @@ -337,6 +438,9 @@ Java_nsk_jvmti_GetThreadState_thrstat002_checkStatus(JNIEnv *env, jclass cls,
TranslateState(state[statInd]), state[statInd]);
printf(" actual: %s (%d)\n",
TranslateState(thrState), thrState);
#ifdef DBG
print_stack_trace(jvmti, env, thr_ptr);
Expand Down

1 comment on commit 3893f4f

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.