8311541: JavaThread::print_jni_stack doesn't support native stacks on all platforms

Reviewed-by: pchilanomate, shade, stuefe
This commit is contained in:
David Holmes 2023-07-18 22:04:37 +00:00
parent e31df3a774
commit c2f421b8bf
5 changed files with 50 additions and 8 deletions

View File

@ -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

View File

@ -1710,9 +1710,15 @@ void JavaThread::print_jni_stack() {
tty->print_cr("Unable to print native stack - out of memory");
return;
}
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);
}

View File

@ -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>");
}
}

View File

@ -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
*/

View File

@ -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"
@ -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;
@ -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);
@ -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;
@ -125,4 +157,5 @@ Java_TestNativeStack_triggerJNIStackTrace
fprintf(stderr, "TEST ERROR: pthread_join failed: %s (%d)\n", strerror(res), res);
exit(1);
}
#endif
}