48 JNIEXPORT jint JNICALL
49 Java_VThreadEventTest_threadEndCount(JNIEnv* jni, jclass clazz) {
50 return thread_end_cnt;
51 }
52
53 JNIEXPORT jint JNICALL
54 Java_VThreadEventTest_threadMountCount(JNIEnv* jni, jclass clazz) {
55 return thread_mount_cnt;
56 }
57
58 JNIEXPORT jint JNICALL
59 Java_VThreadEventTest_threadUnmountCount(JNIEnv* jni, jclass clazz) {
60 return thread_unmount_cnt;
61 }
62
63 JNIEXPORT jint JNICALL
64 Agent_OnAttach(JavaVM *vm, char *options, void *reserved) {
65 jvmtiEventCallbacks callbacks;
66 jvmtiCapabilities caps;
67 jvmtiError err;
68
69 LOG("Agent_OnAttach started\n");
70 if (vm->GetEnv(reinterpret_cast<void **>(&jvmti), JVMTI_VERSION) != JNI_OK || !jvmti) {
71 LOG("Could not initialize JVMTI env\n");
72 return JNI_ERR;
73 }
74 memset(&caps, 0, sizeof(caps));
75 caps.can_support_virtual_threads = 1;
76 check_jvmti_error(jvmti->AddCapabilities(&caps), "AddCapabilities");
77
78 memset(&callbacks, 0, sizeof(callbacks));
79 callbacks.VirtualThreadEnd = &VirtualThreadEnd;
80
81 err = jvmti->SetEventCallbacks(&callbacks, (jint)sizeof(callbacks));
82 check_jvmti_error(err, "SetEventCallbacks");
83
84 err = set_ext_event_callback(jvmti, "VirtualThreadMount", VirtualThreadMount);
85 check_jvmti_error(err, "SetExtEventCallback for VirtualThreadMount");
86
87 err = set_ext_event_callback(jvmti, "VirtualThreadUnmount", VirtualThreadUnmount);
88 check_jvmti_error(err, "SetExtEventCallback for VirtualThreadUnmount");
89
90 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VIRTUAL_THREAD_END, nullptr);
91 check_jvmti_error(err, "SetEventNotificationMode for VirtualThreadEnd");
92
93 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, nullptr);
94 check_jvmti_error(err, "SetEventNotificationMode for VirtualThreadMount");
95
96 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, nullptr);
97 check_jvmti_error(err, "SetEventNotificationMode for VirtualThreadUnmount");
98
99 LOG("vthread events enabled\n");
100 return JVMTI_ERROR_NONE;
101 }
102
103 } // extern "C"
104
|
48 JNIEXPORT jint JNICALL
49 Java_VThreadEventTest_threadEndCount(JNIEnv* jni, jclass clazz) {
50 return thread_end_cnt;
51 }
52
53 JNIEXPORT jint JNICALL
54 Java_VThreadEventTest_threadMountCount(JNIEnv* jni, jclass clazz) {
55 return thread_mount_cnt;
56 }
57
58 JNIEXPORT jint JNICALL
59 Java_VThreadEventTest_threadUnmountCount(JNIEnv* jni, jclass clazz) {
60 return thread_unmount_cnt;
61 }
62
63 JNIEXPORT jint JNICALL
64 Agent_OnAttach(JavaVM *vm, char *options, void *reserved) {
65 jvmtiEventCallbacks callbacks;
66 jvmtiCapabilities caps;
67 jvmtiError err;
68 JNIEnv *env;
69 jsize nVMs;
70 jint res;
71 jclass clazz;
72 jmethodID mid;
73
74 LOG("Agent_OnAttach started\n");
75 if (vm->GetEnv(reinterpret_cast<void **>(&jvmti), JVMTI_VERSION) != JNI_OK || !jvmti) {
76 LOG("Could not initialize JVMTI env\n");
77 return JNI_ERR;
78 }
79 memset(&caps, 0, sizeof(caps));
80 caps.can_support_virtual_threads = 1;
81 check_jvmti_error(jvmti->AddCapabilities(&caps), "AddCapabilities");
82
83 memset(&callbacks, 0, sizeof(callbacks));
84 callbacks.VirtualThreadEnd = &VirtualThreadEnd;
85
86 err = jvmti->SetEventCallbacks(&callbacks, (jint)sizeof(callbacks));
87 check_jvmti_error(err, "SetEventCallbacks");
88
89 err = set_ext_event_callback(jvmti, "VirtualThreadMount", VirtualThreadMount);
90 check_jvmti_error(err, "SetExtEventCallback for VirtualThreadMount");
91
92 err = set_ext_event_callback(jvmti, "VirtualThreadUnmount", VirtualThreadUnmount);
93 check_jvmti_error(err, "SetExtEventCallback for VirtualThreadUnmount");
94
95 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VIRTUAL_THREAD_END, nullptr);
96 check_jvmti_error(err, "SetEventNotificationMode for VirtualThreadEnd");
97
98 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, nullptr);
99 check_jvmti_error(err, "SetEventNotificationMode for VirtualThreadMount");
100
101 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, nullptr);
102 check_jvmti_error(err, "SetEventNotificationMode for VirtualThreadUnmount");
103
104 LOG("vthread events enabled\n");
105
106 // call VThreadEventTest.agentStarted to notify test that agent has started
107
108 res = JNI_GetCreatedJavaVMs(&vm, 1, &nVMs);
109 if (res != JNI_OK) {
110 LOG("JNI_GetCreatedJavaVMs failed: %d\n", res);
111 return JNI_ERR;
112 }
113
114 res = vm->GetEnv((void **) &env, JNI_VERSION_21);
115 if (res != JNI_OK) {
116 LOG("GetEnv failed: %d\n", res);
117 return JNI_ERR;
118 }
119
120 clazz = env->FindClass("VThreadEventTest");
121 if (clazz == NULL) {
122 LOG("FindClass failed\n");
123 return JNI_ERR;
124 }
125
126 mid = env->GetStaticMethodID(clazz, "agentStarted", "()V");
127 if (mid == NULL) {
128 LOG("GetStaticMethodID failed\n");
129 return JNI_ERR;
130 }
131
132 env->CallStaticVoidMethod(clazz, mid);
133 if (env->ExceptionOccurred()) {
134 LOG("CallStaticVoidMethod failed\n");
135 return JNI_ERR;
136 }
137
138 LOG("Agent_OnAttach done\n");
139
140 return JVMTI_ERROR_NONE;
141 }
142
143 } // extern "C"
144
|