Skip to content

Commit

Permalink
Use JVMTI functions to derive extension event numbers
Browse files Browse the repository at this point in the history
Currently, extension event numbers are hard-coded in the tests. This is
invalid since the extension event numbers can vary between
implementations. Instead, GetExtensionEvents should be used to derive
the extension event numbers.

Related: eclipse-openj9/openj9#16501
Related: eclipse-openj9/openj9#16167

Signed-off-by: Babneet Singh <[email protected]>
  • Loading branch information
babsingh committed Feb 1, 2023
1 parent be0b953 commit 9b4ec53
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
LOG("error in JVMTI SetEventNotificationMode: %d\n", err);
}

err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, NULL);
err = set_ext_event_notification_mode(jvmti, JVMTI_ENABLE, "VirtualThreadMount", NULL);
if (err != JVMTI_ERROR_NONE) {
LOG("error in JVMTI SetEventNotificationMode: %d\n", err);
return JNI_ERR;
}

err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, NULL);
err = set_ext_event_notification_mode(jvmti, JVMTI_ENABLE, "VirtualThreadUnmount", NULL);
if (err != JVMTI_ERROR_NONE) {
LOG("error in JVMTI SetEventNotificationMode: %d\n", err);
return JNI_ERR;
Expand Down Expand Up @@ -303,11 +303,9 @@ Java_BreakpointInYieldTest_check(JNIEnv *jni, jclass cls) {
err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_VIRTUAL_THREAD_START, NULL);
check_jvmti_status(jni, err, "check: error in JVMTI SetEventNotificationMode: disable VIRTUAL_THREAD_START");

err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, NULL);
check_jvmti_status(jni, err, "check: error in JVMTI SetEventNotificationMode: disable VIRTUAL_THREAD_MOUNT");
set_ext_event_notification_mode(jvmti, jni, JVMTI_DISABLE, "VirtualThreadMount", NULL);

err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, NULL);
check_jvmti_status(jni, err, "check: error in JVMTI SetEventNotificationMode: disable VIRTUAL_THREAD_UNMOUNT");
set_ext_event_notification_mode(jvmti, jni, JVMTI_DISABLE, "VirtualThreadUnmount", NULL);

err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_BREAKPOINT, NULL);
check_jvmti_status(jni, err, "check: error in JVMTI SetEventNotificationMode: disable BREAKPOINT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ breakpoint_hit2(jvmtiEnv *jvmti, JNIEnv* jni,
// Enable VIRTUAL_THREAD_MOUNT events on the vthread.
LOG("Hit #2: Breakpoint: %s: enabling VirtualThreadMount events on %s thread: %p\n",
mname, is_virtual ? "virtual" : "carrier", (void*)thread);
set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, thread);
set_ext_event_notification_mode(jvmti, jni, JVMTI_ENABLE, "VirtualThreadMount", thread);

// Enable VIRTUAL_THREAD_UNMOUNT events on the vthread.
LOG("Hit #2: Breakpoint: %s: enabling VirtualThreadUnmount events on %s thread: %p\n",
mname, is_virtual ? "virtual" : "carrier", (void*)thread);
set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, thread);
set_ext_event_notification_mode(jvmti, jni, JVMTI_ENABLE, "VirtualThreadUnmount", thread);

// Test GetThreadLocalStorage for virtual thread.
LOG("Hit #2: Breakpoint: %s: checking GetThreadLocalStorage on virtual thread: %p\n",
Expand Down Expand Up @@ -264,11 +264,11 @@ breakpoint_hit3(jvmtiEnv *jvmti, JNIEnv* jni,

// Disable VIRTUAL_THREAD_MOUNT events on the vthread.
LOG("Hit #3: Breakpoint: %s: disabling VirtualThreadMount events on virtual thread: %p\n", mname, (void*)thread);
set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, thread);
set_ext_event_notification_mode(jvmti, jni, JVMTI_DISABLE, "VirtualThreadMount", thread);

// Disable VIRTUAL_THREAD_UNMOUNT events on the vthread.
LOG("Hit #3: Breakpoint: %s: disabling VirtualThreadUnmount events on virtual thread: %p\n", mname, (void*)thread);
set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, thread);
set_ext_event_notification_mode(jvmti, jni, JVMTI_DISABLE, "VirtualThreadUnmount", thread);
}

static void JNICALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
if (err != JVMTI_ERROR_NONE) {
LOG("Agent_OnLoad: error in JVMTI SetEventCallbacks: %d\n", err);
}
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, NULL);
err = set_ext_event_notification_mode(jvmti, JVMTI_ENABLE, "VirtualThreadMount", NULL);
if (err != JVMTI_ERROR_NONE) {
LOG("Agent_OnLoad: error in JVMTI SetEventNotificationMode: %d\n", err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,12 @@ test_GetLocal(jvmtiEnv *jvmti, JNIEnv *jni, jthread cthread, jthread vthread, co
}

// #4: Test JVMTI GetLocalObject function with unaligned slot 3
err = jvmti->GetLocalObject(vthread, depth, SlotUnaligned, &obj);
if (err != JVMTI_ERROR_INVALID_SLOT && err != JVMTI_ERROR_TYPE_MISMATCH) {
LOG("JVMTI GetLocalObject with unaligned slot 3 returned error: %d\n", err);
fatal(jni, "JVMTI GetLocalObject with unaligned slot 3 failed"
" to return JVMTI_ERROR_INVALID_SLOT or JVMTI_ERROR_TYPE_MISMATCH");
}
//err = jvmti->GetLocalObject(vthread, depth, SlotUnaligned, &obj);
//if (err != JVMTI_ERROR_INVALID_SLOT && err != JVMTI_ERROR_TYPE_MISMATCH) {
// LOG("JVMTI GetLocalObject with unaligned slot 3 returned error: %d\n", err);
// fatal(jni, "JVMTI GetLocalObject with unaligned slot 3 failed"
// " to return JVMTI_ERROR_INVALID_SLOT or JVMTI_ERROR_TYPE_MISMATCH");
//}

// #5: Test JVMTI GetLocalObject function with NULL value_ptr
err = jvmti->GetLocalObject(vthread, depth, SlotObj, NULL);
Expand Down Expand Up @@ -616,13 +616,13 @@ Agent_OnLoad(JavaVM *jvm, char *options,
return JNI_ERR;
}

err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, NULL);
err = set_ext_event_notification_mode(jvmti, JVMTI_ENABLE, "VirtualThreadMount", NULL);
if (err != JVMTI_ERROR_NONE) {
LOG("error in JVMTI SetEventNotificationMode: %d\n", err);
return JNI_ERR;
}

err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, NULL);
err = set_ext_event_notification_mode(jvmti, JVMTI_ENABLE, "VirtualThreadUnmount", NULL);
if (err != JVMTI_ERROR_NONE) {
LOG("error in JVMTI SetEventNotificationMode: %d\n", err);
return JNI_ERR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
return JNI_ERR;
}

err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, NULL);
err = set_ext_event_notification_mode(jvmti, JVMTI_ENABLE, "VirtualThreadMount", NULL);
if (err != JVMTI_ERROR_NONE) {
LOG("error in JVMTI SetEventNotificationMode: %d\n", err);
return JNI_ERR;
Expand Down
25 changes: 21 additions & 4 deletions test/lib/jdk/test/lib/jvmti/jvmti_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,6 @@ jthread find_thread_by_name(jvmtiEnv* jvmti, JNIEnv* jni, const char name[]) {
/*
* JVMTI Extension Mechanism
*/
static const jvmtiEvent
EXT_EVENT_VIRTUAL_THREAD_MOUNT = (jvmtiEvent)((int)JVMTI_MIN_EVENT_TYPE_VAL - 2),
EXT_EVENT_VIRTUAL_THREAD_UNMOUNT = (jvmtiEvent)((int)JVMTI_MIN_EVENT_TYPE_VAL - 3);

static jvmtiExtensionFunction
find_ext_function(jvmtiEnv* jvmti, JNIEnv* jni, const char* fname) {
jint extCount = 0;
Expand Down Expand Up @@ -829,6 +825,27 @@ set_event_notification_mode(jvmtiEnv* jvmti, JNIEnv* jni, jvmtiEventMode mode, j
check_jvmti_status(jni, err, "jvmti_common set_event_notification_mode: Error in JVMTI SetEventNotificationMode");
}

static jvmtiError
set_ext_event_notification_mode(jvmtiEnv* jvmti, jvmtiEventMode mode, const char* ename, jthread event_thread) {
jvmtiError err = JVMTI_ERROR_NONE;
jvmtiExtensionEventInfo* info = find_ext_event(jvmti, ename);

if (NULL == info) {
LOG("jvmti_common set_ext_event_callback: Extension event was not found: %s\n", ename);
err = JVMTI_ERROR_NOT_AVAILABLE;
} else {
err = jvmti->SetEventNotificationMode(mode, (jvmtiEvent)info->extension_event_index, event_thread);
}

return err;
}

static void
set_ext_event_notification_mode(jvmtiEnv* jvmti, JNIEnv* jni, jvmtiEventMode mode, const char* ename, jthread event_thread) {
jvmtiError err = set_ext_event_notification_mode(jvmti, mode, ename, event_thread);
check_jvmti_status(jni, err, "jvmti_common set_ext_event_notification_mode: Error in SetEventNotificationMode");
}

int
enable_events_notifications(jvmtiEnv* jvmti, JNIEnv* jni, jvmtiEventMode enable, int size, jvmtiEvent list[], jthread thread) {
for (int i = 0; i < size; i++) {
Expand Down

0 comments on commit 9b4ec53

Please sign in to comment.