From 2d2f082976c323909f6e97faa7fab2c831844c85 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 12 Nov 2024 16:32:51 +0100 Subject: [PATCH 1/2] Fix J9 JVM thread-bridge --- ddprof-lib/src/main/cpp/vmStructs.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ddprof-lib/src/main/cpp/vmStructs.cpp b/ddprof-lib/src/main/cpp/vmStructs.cpp index 27d8ccd29..e7cff9ad2 100644 --- a/ddprof-lib/src/main/cpp/vmStructs.cpp +++ b/ddprof-lib/src/main/cpp/vmStructs.cpp @@ -568,11 +568,15 @@ void VMStructs::initThreadBridge(JNIEnv *env) { // Get eetop field - a bridge from Java Thread to VMThread jclass thread_class = env->GetObjectClass(thread); - if ((_tid = env->GetFieldID(thread_class, "tid", "J")) == NULL || - (_eetop = env->GetFieldID(thread_class, "eetop", "J")) == NULL) { - // No such field - probably not a HotSpot JVM - env->ExceptionClear(); - + _tid = env->GetFieldID(thread_class, "tid", "J"); + env->ExceptionClear(); + _eetop = env->GetFieldID(thread_class, "eetop", "J"); + env->ExceptionClear(); + + if (_tid == NULL || _eetop == NULL || VM::isOpenJ9()) { + // In JDK 18 and earlier the eetop will be NULL + // In JDK 19 and later the eetop is an alias to 'threadRef' value + // - https://github.com/eclipse-openj9/openj9/blob/ecbfade39c5573ed48e19961708822b5c841b2e7/runtime/oti/vmconstantpool.xml#L239 void *j9thread = J9Ext::j9thread_self(); if (j9thread != NULL) { initTLS(j9thread); From f5db44fee8c99b1c4f585b5fcea5eaf93c790aa6 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 12 Nov 2024 17:49:50 +0100 Subject: [PATCH 2/2] Debug --- ddprof-lib/src/main/cpp/vmStructs.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ddprof-lib/src/main/cpp/vmStructs.cpp b/ddprof-lib/src/main/cpp/vmStructs.cpp index e7cff9ad2..8cca06ea0 100644 --- a/ddprof-lib/src/main/cpp/vmStructs.cpp +++ b/ddprof-lib/src/main/cpp/vmStructs.cpp @@ -16,6 +16,7 @@ */ #include "vmStructs.h" +#include "common.h" #include "j9Ext.h" #include "safeAccess.h" #include "spinLock.h" @@ -569,11 +570,12 @@ void VMStructs::initThreadBridge(JNIEnv *env) { // Get eetop field - a bridge from Java Thread to VMThread jclass thread_class = env->GetObjectClass(thread); _tid = env->GetFieldID(thread_class, "tid", "J"); - env->ExceptionClear(); _eetop = env->GetFieldID(thread_class, "eetop", "J"); - env->ExceptionClear(); if (_tid == NULL || _eetop == NULL || VM::isOpenJ9()) { + // clear any pending exceptions + TEST_LOG("J9 thread: tid=%p, eetop=%p", _tid, _eetop); + env->ExceptionClear(); // In JDK 18 and earlier the eetop will be NULL // In JDK 19 and later the eetop is an alias to 'threadRef' value // - https://github.com/eclipse-openj9/openj9/blob/ecbfade39c5573ed48e19961708822b5c841b2e7/runtime/oti/vmconstantpool.xml#L239