Skip to content

Commit

Permalink
8311541: JavaThread::print_jni_stack doesn't support native stacks on…
Browse files Browse the repository at this point in the history
… all platforms

Reviewed-by: pchilanomate, shade, stuefe
  • Loading branch information
David Holmes committed Jul 18, 2023
1 parent e31df3a commit c2f421b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
4 changes: 3 additions & 1 deletion make/test/JtregNativeHotspot.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,10 @@ BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exesigtest := -ljvm

ifeq ($(call isTargetOs, windows), true)
BUILD_HOTSPOT_JTREG_EXECUTABLES_CFLAGS_exeFPRegs := -MT
BUILD_HOTSPOT_JTREG_EXCLUDE += exesigtest.c libterminatedThread.c libTestJNI.c libCompleteExit.c libTestPsig.c libnativeStack.c exeGetCreatedJavaVMs.c
BUILD_HOTSPOT_JTREG_EXCLUDE += exesigtest.c libterminatedThread.c libTestJNI.c libCompleteExit.c libTestPsig.c exeGetCreatedJavaVMs.c
BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libatExit := jvm.lib
BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libnativeStack := jvm.lib
BUILD_HOTSPOT_JTREG_LIBRARIES_COPY_DEBUG_SYMBOLS_libnativeStack := true
BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exedaemonDestroy := jvm.lib
else
BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exedaemonDestroy := -ljvm
Expand Down
12 changes: 9 additions & 3 deletions src/hotspot/share/runtime/javaThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1710,9 +1710,15 @@ void JavaThread::print_jni_stack() {
tty->print_cr("Unable to print native stack - out of memory");
return;
}
frame f = os::current_frame();
VMError::print_native_stack(tty, f, this, true /*print_source_info */,
-1 /* max stack */, buf, O_BUFLEN);
address lastpc = nullptr;
if (os::platform_print_native_stack(tty, nullptr, buf, O_BUFLEN, lastpc)) {
// We have printed the native stack in platform-specific code,
// so nothing else to do in this case.
} else {
frame f = os::current_frame();
VMError::print_native_stack(tty, f, this, true /*print_source_info */,
-1 /* max stack */, buf, O_BUFLEN);
}
} else {
print_active_stack_on(tty);
}
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/utilities/vmError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ void VMError::print_native_stack(outputStream* st, frame fr, Thread* t, bool pri
st->print_cr("...<more frames>...");
}

} else {
st->print_cr("Native frames: <unavailable>");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
/*
* @test
* @bug 8295974
* @requires os.family != "windows" & os.arch != "arm"
* @requires os.arch != "arm"
* @library /test/lib
* @summary Generate a JNI Fatal error, or a warning, in a launched VM and check
* the native stack is present as expected.
* @comment The native code only supports POSIX so no windows testing
* @run driver TestNativeStack
*/

Expand Down
37 changes: 35 additions & 2 deletions test/hotspot/jtreg/runtime/jni/nativeStack/libnativeStack.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32
#include <windows.h>
#include <process.h>
typedef unsigned int THREAD_ID;
#else
#include <pthread.h>
typedef pthread_t THREAD_ID;
#endif

#include <string.h>

#include "jni.h"
Expand Down Expand Up @@ -63,7 +71,12 @@ void generateError(JNIEnv *env) {
(*env)->FatalError(env, "Fatal error generated in test code");
}

static void * thread_start(void* unused) {
#ifdef _WIN32
unsigned __stdcall
#else
void *
#endif
thread_start(void* unused) {
JNIEnv *env;
int res;

Expand Down Expand Up @@ -94,13 +107,17 @@ static void * thread_start(void* unused) {

printf("Native thread terminating\n");

#ifndef _WIN32
return NULL;
#else
return 0;
#endif
}

JNIEXPORT void JNICALL
Java_TestNativeStack_triggerJNIStackTrace
(JNIEnv *env, jclass cls, jboolean warn) {
pthread_t thread;
THREAD_ID thread;
int res = (*env)->GetJavaVM(env, &jvm);
if (res != JNI_OK) {
fprintf(stderr, "Test ERROR. Can't extract JavaVM: %d\n", res);
Expand All @@ -109,6 +126,21 @@ Java_TestNativeStack_triggerJNIStackTrace

warning = warn;

#ifdef _WIN32
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, thread_start,
NULL, 0, &thread);
if (hThread == 0) {
fprintf(stderr, "TEST ERROR: _beginthreadex failed: %s\n", strerror(errno));
exit(1);
}
if (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0) {
fprintf(stderr, "TEST ERROR: WaitForSingleObject failed: %d\n", GetLastError());
exit(1);
}
CloseHandle(hThread);

#else

pthread_attr_t attr;
pthread_attr_init(&attr);
size_t stack_size = 0x100000;
Expand All @@ -125,4 +157,5 @@ Java_TestNativeStack_triggerJNIStackTrace
fprintf(stderr, "TEST ERROR: pthread_join failed: %s (%d)\n", strerror(res), res);
exit(1);
}
#endif
}

1 comment on commit c2f421b

@openjdk-notifier
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.