Skip to content

Commit

Permalink
Use GetExtensionEvents 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 2, 2023
1 parent 409e6dd commit db6f8df
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#include <string.h>
#include "jvmti.h"
Expand Down Expand Up @@ -250,13 +255,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,10 +308,10 @@ 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);
err = set_ext_event_notification_mode(jvmti, JVMTI_DISABLE, "VirtualThreadMount", NULL);
check_jvmti_status(jni, err, "check: error in JVMTI SetEventNotificationMode: disable VIRTUAL_THREAD_MOUNT");

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

err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_BREAKPOINT, NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#include <string.h>
#include "jvmti.h"
Expand Down Expand Up @@ -215,12 +220,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 +269,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 @@ -20,6 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#include <string.h>
#include "jvmti.h"
Expand Down Expand Up @@ -395,7 +400,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 @@ -20,6 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#include <string.h>
#include "jvmti.h"
Expand Down Expand Up @@ -620,13 +625,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 @@ -20,6 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#include <string.h>
#include <atomic>
Expand Down Expand Up @@ -187,7 +192,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 @@ -20,6 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#ifndef JVMTI_COMMON_H
#define JVMTI_COMMON_H
Expand Down Expand Up @@ -779,10 +784,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 @@ -880,6 +881,22 @@ 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) {
jvmtiExtensionEventInfo* info = find_ext_event(jvmti, ename);
if (NULL == info) {
LOG("jvmti_common set_ext_event_notification_mode: Extension event was not found: %s\n", ename);
return JVMTI_ERROR_NOT_AVAILABLE;
}
return jvmti->SetEventNotificationMode(mode, (jvmtiEvent)info->extension_event_index, event_thread);
}

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 db6f8df

Please sign in to comment.