1 /*
  2  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 #include <string.h>
 25 #include "jvmti.h"
 26 #include "jvmti_common.h"
 27 
 28 extern "C" {
 29 
 30 static jvmtiEnv *jvmti = NULL;
 31 static jthread exp_thread = NULL;
 32 static jrawMonitorID event_mon = NULL;
 33 static int vthread_mounted_count = 0;
 34 static int vthread_unmounted_count = 0;
 35 static int breakpoint_count = 0;
 36 static int method_entry_count = 0;
 37 static int method_exit_count = 0;
 38 static int frame_pop_count = 0;
 39 static int brkptBreakpointHit = 0;
 40 static jboolean received_method_exit_event = JNI_FALSE;
 41 static jboolean passed = JNI_TRUE;
 42 
 43 static jmethodID *test_methods = NULL;
 44 jint test_method_count = 0;
 45 jclass test_class = NULL;
 46 
 47 static void
 48 print_frame_event_info(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jmethodID method,
 49                        const char* event_name, int event_count) {
 50   char* tname = get_thread_name(jvmti, jni, thread);
 51   char* cname = get_method_class_name(jvmti, jni, method);
 52   char* mname = get_method_name(jvmti, jni, method);
 53 
 54   LOG("\n%s #%d: method: %s::%s, thread: %s\n",
 55          event_name, event_count, cname, mname, tname);
 56 
 57   if (strcmp(event_name, "SingleStep") != 0) {
 58     print_stack_trace(jvmti, jni, thread);
 59   }
 60   deallocate(jvmti, jni, (void*)tname);
 61   deallocate(jvmti, jni, (void*)cname);
 62   deallocate(jvmti, jni, (void*)mname);
 63 }
 64 
 65 static void
 66 print_cont_event_info(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jint frames_cnt, const char* event_name) {
 67   char* tname = get_thread_name(jvmti, jni, thread);
 68 
 69   LOG("%s: thread: %s, frames: %d\n\n", event_name, tname, frames_cnt);
 70 
 71   print_stack_trace(jvmti, jni, thread);
 72 
 73   deallocate(jvmti, jni, (void*)tname);
 74 }
 75 
 76 static void
 77 set_or_clear_breakpoint(JNIEnv *jni, jboolean set, const char *methodName,
 78                      jclass klass, jmethodID methods[], int method_count)
 79 {
 80   jlocation location = (jlocation)0L;
 81   jmethodID method = NULL;
 82   jvmtiError err;
 83 
 84   // Find the jmethodID of the specified method
 85   while (--method_count >= 0) {
 86     jmethodID meth = methods[method_count];
 87     char* mname = get_method_name(jvmti, jni, meth);
 88 
 89     if (strcmp(mname, methodName) == 0) {
 90       // LOG("setupBreakpoint: found method %s() to %s a breakpoint\n", mname, set ? "set" : "clear");
 91       method = meth;
 92     }
 93     deallocate(jvmti, jni, (void*)mname);
 94   }
 95   if (method == NULL) {
 96       LOG("setupBreakpoint: not found method %s() to %s a breakpoint\n",
 97              methodName, set ? "set" : "clear");
 98       jni->FatalError("Error in setupBreakpoint: not found method");
 99   }
100 
101   if (set) {
102       err = jvmti->SetBreakpoint(method, location);
103   } else {
104       err = jvmti->ClearBreakpoint(method, location);
105   }
106   check_jvmti_status(jni, err, "setupBreakpoint: error in JVMTI SetBreakpoint");
107 
108 }
109 
110 static void
111 set_breakpoint(JNIEnv *jni, const char *methodName,
112               jclass klass, jmethodID methods[], int method_count)
113 {
114   set_or_clear_breakpoint(jni, JNI_TRUE, methodName, klass, methods, method_count);
115 }
116 
117 static void
118 clear_breakpoint(JNIEnv *jni, const char *methodName,
119                 jclass klass, jmethodID methods[], int method_count)
120 {
121   set_or_clear_breakpoint(jni, JNI_FALSE, methodName, klass, methods, method_count);
122 }
123 
124 static long tls_data = 0;
125 
126 static void
127 breakpoint_hit1(jvmtiEnv *jvmti, JNIEnv* jni,
128                 jthread thread, jthread cthread,
129                 jboolean is_virtual, char* mname) {
130   char* tname = get_thread_name(jvmti, jni, cthread);
131   jthread vthread = NULL;
132   jvmtiError err;
133 
134   // Test GetVirtualThread for carrier thread.
135   LOG("Hit #1: Breakpoint: %s: checking GetVirtualThread on carrier thread: %p, %s\n",
136          mname, (void*)cthread, tname);
137 
138   vthread = get_virtual_thread(jvmti, jni, cthread);
139 
140   if (jni->IsSameObject(thread, vthread) != JNI_TRUE) {
141     passed = JNI_FALSE;
142     LOG("FAILED: GetVirtualThread for carrier thread returned wrong vthread\n\n");
143   } else {
144     LOG("GetVirtualThread for carrier thread %p returned expected virtual thread: %p\n\n",
145            (void*)cthread, (void*)vthread);
146   }
147 
148   // Test GetThreadLocalStorage for carrier thread.
149   LOG("Hit #1: Breakpoint: %s: checking GetThreadLocalStorage on carrier thread: %p\n",
150          mname, (void*)cthread);
151   err = jvmti->GetThreadLocalStorage(cthread, (void**)&tls_data);
152   check_jvmti_status(jni, err, "Breakpoint: error in JVMTI GetThreadLocalStorage");
153 
154   if (tls_data != 111) {
155     passed = JNI_FALSE;
156     LOG("FAILED: GetThreadLocalStorage for carrier thread returned value: %d, expected 111\n\n", (int)tls_data);
157   } else {
158     LOG("GetThreadLocalStorage for carrier thread returned value %d as expected\n\n", (int)tls_data);
159   }
160   {
161     jmethodID method = NULL;
162     jlocation loc = 0L;
163     char* mname1 = NULL;
164     char* cname1 = NULL;
165 
166     err = jvmti->GetFrameLocation(cthread, 0, &method, &loc);
167     check_jvmti_status(jni, err, "Breakpoint: error in JVMTI GetFrameLocation");
168 
169     mname1 = get_method_name(jvmti, jni, method);
170     cname1 = get_method_class_name(jvmti, jni, method);
171 
172     // Enable METHOD_EXIT events on the cthread. We should not get one.
173     LOG("Hit #1: Breakpoint: %s: enabling MethodExit events on carrier thread: %p\n",
174            mname, (void*)cthread);
175     set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, cthread);
176 
177     // Setup NotifyFramePop on the cthread.
178     LOG("Hit #1: Breakpoint: %s: enabling FramePop event for method: %s::%s on carrier thread: %p\n",
179            mname, cname1, mname1, (void*)cthread);
180     err = jvmti->NotifyFramePop(cthread, 0);
181     check_jvmti_status(jni, err, "Breakpoint: error in JVMTI NotifyFramePop");
182 
183     // Print stack trace of cthread.
184     LOG("Hit #1: Breakpoint: %s: Stack Trace of carrier thread: %p\n",
185            mname, (void*)cthread);
186     print_stack_trace(jvmti, jni, cthread);
187   }
188   deallocate(jvmti, jni, (void*)tname);
189 }
190 
191 static void
192 breakpoint_hit2(jvmtiEnv *jvmti, JNIEnv* jni,
193                 jthread thread, jthread cthread,
194                 jboolean is_virtual, char* mname) {
195   jvmtiError err;
196 
197   // Verify that we did not get a METHOD_EXIT events when enabled on the cthread.
198   if (received_method_exit_event) {
199     passed = JNI_FALSE;
200     received_method_exit_event = JNI_FALSE;
201     LOG("FAILED: got METHOD_EXIT event on the cthread: %p\n", cthread);
202   }
203 
204   // Disable METHOD_EXIT events on the cthread.
205   LOG("Hit #2: Breakpoint: %s: disabling MethodExit events on carrier thread: %p\n",
206           mname, (void*)cthread);
207   set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, JVMTI_EVENT_METHOD_EXIT, cthread);
208 
209   // Enable METHOD_EXIT events on the vthread. We should get one.
210   LOG("Hit #2: Breakpoint: %s: enabling MethodExit events on %s thread: %p\n",
211           mname, is_virtual ? "virtual" : "carrier", (void*)thread);
212   set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread);
213 
214   // Enable VIRTUAL_THREAD_MOUNT events on the vthread.
215   LOG("Hit #2: Breakpoint: %s: enabling VirtualThreadMount events on %s thread: %p\n",
216           mname, is_virtual ? "virtual" : "carrier", (void*)thread);
217   set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, thread);
218 
219   // Enable VIRTUAL_THREAD_UNMOUNT events on the vthread.
220   LOG("Hit #2: Breakpoint: %s: enabling VirtualThreadUnmount events on %s thread: %p\n",
221           mname, is_virtual ? "virtual" : "carrier", (void*)thread);
222   set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, thread);
223 
224   // Test GetThreadLocalStorage for virtual thread.
225   LOG("Hit #2: Breakpoint: %s: checking GetThreadLocalStorage on virtual thread: %p\n",
226          mname, (void*)thread);
227   err = jvmti->GetThreadLocalStorage(thread, (void**)&tls_data);
228   check_jvmti_status(jni, err, "Breakpoint: error in JVMTI GetThreadLocalStorage");
229 
230   if (tls_data != 222) {
231     passed = JNI_FALSE;
232     LOG("FAILED: GetThreadLocalStorage for virtual thread returned value: %d, expected 222\n\n", (int)tls_data);
233   } else {
234     LOG("GetThreadLocalStorage for virtual thread returned value %d as expected\n\n", (int)tls_data);
235   }
236 }
237 
238 static void
239 breakpoint_hit3(jvmtiEnv *jvmti, JNIEnv* jni,
240                 jthread thread, jthread cthread,
241                 jboolean is_virtual, char* mname) {
242   jvmtiError err;
243 
244   // Verify that we got a METHOD_EXIT when enabled on the vthread.
245   if (!received_method_exit_event) {
246     LOG("FAILED: did not get METHOD_EXIT event on the vthread: %p\n", (void*)thread);
247     passed = JNI_FALSE;
248   }
249 
250   // Disable breakpoint events.
251   clear_breakpoint(jni, "brkpt", test_class, test_methods, test_method_count);
252   set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, JVMTI_EVENT_BREAKPOINT, NULL);
253 
254   // Disable METHOD_EXIT events on the vthread.
255   LOG("Hit #3: Breakpoint: %s: disabling MethodExit events on virtual thread: %p\n", mname, (void*)thread);
256   set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, JVMTI_EVENT_METHOD_EXIT, thread);
257 
258   // Setup NotifyFramePop on the vthread.
259   LOG("Hit #3: Breakpoint: %s: enabling FramePop event for method: %s on virtual thread: %p\n",
260          mname, mname, (void*)thread);
261   err = jvmti->NotifyFramePop(thread, 0);
262   check_jvmti_status(jni, err, "Breakpoint: error in JVMTI NotifyFramePop");
263 
264   // Disable VIRTUAL_THREAD_MOUNT events on the vthread.
265   LOG("Hit #3: Breakpoint: %s: disabling VirtualThreadMount events on virtual thread: %p\n", mname, (void*)thread);
266   set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, EXT_EVENT_VIRTUAL_THREAD_MOUNT, thread);
267 
268   // Disable VIRTUAL_THREAD_UNMOUNT events on the vthread.
269   LOG("Hit #3: Breakpoint: %s: disabling VirtualThreadUnmount events on virtual thread: %p\n", mname, (void*)thread);
270   set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, thread);
271 }
272 
273 static void JNICALL
274 Breakpoint(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread,
275            jmethodID method, jlocation location) {
276   jthread cthread = NULL;
277   char* mname = get_method_name(jvmti, jni, method);
278   jboolean is_virtual = jni->IsVirtualThread(thread);
279 
280   if (strcmp(mname, "brkpt") != 0) {
281     LOG("FAILED: got  unexpected breakpoint in method %s()\n", mname);
282     passed = JNI_FALSE;
283     deallocate(jvmti, jni, (void*)mname);
284     return;
285   }
286 
287   RawMonitorLocker rml(jvmti, jni, event_mon);
288 
289   brkptBreakpointHit++;
290   print_frame_event_info(jvmti, jni, thread, method,
291                          "Breakpoint", ++breakpoint_count);
292 
293   cthread = get_carrier_thread(jvmti, jni, thread);
294 
295   if (brkptBreakpointHit == 1) { // 1st MethodExitTest.brkpt() breakpoint
296     breakpoint_hit1(jvmti, jni, thread, cthread, is_virtual, mname);
297 
298   } else if (brkptBreakpointHit == 2) { // 2nd MethodExitTest.brkpt breakpoint
299     breakpoint_hit2(jvmti, jni, thread, cthread, is_virtual, mname);
300 
301   } else if (brkptBreakpointHit == 3) { // 3rd MethodExitTest.brkpt breakpoint
302     breakpoint_hit3(jvmti, jni, thread, cthread, is_virtual, mname);
303 
304   } else {
305     LOG("FAILED: Breakpoint: too many brkpt breakpoints.\n");
306     passed = JNI_FALSE;
307   }
308 
309   deallocate(jvmti, jni, (void*)mname);
310 }
311 
312 static void JNICALL
313 MethodEntry(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jmethodID method) {
314   char* mname = get_method_name(jvmti, jni, method);
315 
316   RawMonitorLocker rml(jvmti, jni, event_mon);
317   method_entry_count++;
318 
319   LOG("Hit #%d: MethodEntry #%d: method: %s, thread: %p\n",
320          brkptBreakpointHit, method_entry_count,  mname, (void*)thread);
321 
322   // print_frame_event_info(jvmti, jni, thread, method, "MethodEntry", method_entry_count);
323 
324 
325   deallocate(jvmti, jni, (void*)mname);
326 }
327 
328 static void JNICALL
329 MethodExit(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jmethodID method,
330            jboolean was_popped_by_exception, jvalue return_value) {
331   char* mname = get_method_name(jvmti, jni, method);
332 
333   RawMonitorLocker rml(jvmti, jni, event_mon);
334   method_exit_count++;
335 
336   if (brkptBreakpointHit == 1) {
337     received_method_exit_event = JNI_TRUE; // set it for any method as it is not expected
338   }
339 
340   // print_frame_event_info(jvmti, jni, thread, method, "MethodExit", method_exit_count);
341   if (strstr(mname, "brkpt") != NULL) { // event IS in the "brkpt" method
342     LOG("Hit #%d: MethodExit #%d: method: %s on thread: %p\n",
343            brkptBreakpointHit, method_exit_count, mname, (void*)thread);
344     received_method_exit_event = JNI_TRUE; // set it for brkpt method only if brkptBreakpointHit > 1
345 
346     set_event_notification_mode(jvmti, jni, JVMTI_DISABLE, JVMTI_EVENT_METHOD_EXIT, thread);
347   }
348 
349   deallocate(jvmti, jni, (void*)mname);
350 }
351 
352 static void JNICALL
353 FramePop(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jmethodID method,
354          jboolean was_popped_by_exception) {
355   char* mname = get_method_name(jvmti, jni, method);
356 
357   RawMonitorLocker rml(jvmti, jni, event_mon);
358   frame_pop_count++;
359 
360   LOG("\nHit #%d: FramePop #%d: method: %s on thread: %p\n",
361          brkptBreakpointHit, frame_pop_count, mname, (void*)thread);
362 
363   print_frame_event_info(jvmti, jni, thread, method, "FramePop", frame_pop_count);
364 
365 
366   deallocate(jvmti, jni, (void*)mname);
367 }
368 
369 static void JNICALL
370 ThreadStart(jvmtiEnv *jvmti, JNIEnv* jni, jthread cthread) {
371   char* tname = get_thread_name(jvmti, jni, cthread);
372   long loc_tls_data = 0;
373   jvmtiError err;
374 
375   RawMonitorLocker rml(jvmti, jni, event_mon);
376 
377   LOG("\nThreadStart: cthread: %p, name: %s\n", (void*)cthread, tname);
378 
379   // Test SetThreadLocalStorage for carrier thread.
380   err = jvmti->SetThreadLocalStorage(cthread, (void*)111);
381   check_jvmti_status(jni, err, "ThreadStart: error in JVMTI SetThreadLocalStorage");
382 
383   // Test GetThreadLocalStorage for carrier thread.
384   err = jvmti->GetThreadLocalStorage(cthread, (void**)&loc_tls_data);
385   check_jvmti_status(jni, err, "ThreadStart: error in JVMTI GetThreadLocalStorage");
386 
387   if (loc_tls_data != 111) {
388     passed = JNI_FALSE;
389     LOG("ThreadStart: FAILED: GetThreadLocalStorage for carrier thread returned value: %d, expected 111\n\n", (int)loc_tls_data);
390   } else {
391     LOG("ThreadStart: GetThreadLocalStorage for carrier thread returned value %d as expected\n\n", (int)loc_tls_data);
392   }
393 
394   deallocate(jvmti, jni, (void*)tname);
395 }
396 
397 static void JNICALL
398 VirtualThreadStart(jvmtiEnv *jvmti, JNIEnv* jni, jthread vthread) {
399   char* tname = get_thread_name(jvmti, jni, vthread);
400   jvmtiError err;
401   jboolean is_virtual = jni->IsVirtualThread(vthread);
402   const char* virt = is_virtual == JNI_TRUE ? "virtual" : "carrier";
403 
404   RawMonitorLocker rml(jvmti, jni, event_mon);
405 
406   LOG("\nVirtualThreadStart: %s thread: %p, name: %s\n", virt, (void*)vthread, tname);
407 
408   // Test SetThreadLocalStorage for virtual thread.
409   err = jvmti->SetThreadLocalStorage(vthread, (void*)222);
410   check_jvmti_status(jni, err, "VirtualThreadMount: error in JVMTI SetThreadLocalStorage");
411 
412 
413   deallocate(jvmti, jni, (void*)tname);
414 }
415 
416 // Parameters: (jvmtiEnv *jvmti, JNIEnv* jni, jthread thread)
417 static void JNICALL
418 VirtualThreadMount(jvmtiEnv *jvmti, ...) {
419   jmethodID method = NULL;
420   jlocation loc = 0L;
421   char* mname = NULL;
422   char* cname = NULL;
423   jvmtiError err;
424 
425   va_list ap;
426   JNIEnv* jni = NULL;
427   jthread thread = NULL;
428 
429   va_start(ap, jvmti);
430   jni = va_arg(ap, JNIEnv*);
431   thread = va_arg(ap, jthread);
432   va_end(ap);
433 
434   RawMonitorLocker rml(jvmti, jni, event_mon);
435 
436   err = jvmti->GetFrameLocation(thread, 0, &method, &loc);
437   check_jvmti_status(jni, err, "VirtualThreadMount: error in JVMTI GetFrameLocation");
438 
439   mname = get_method_name(jvmti, jni, method);
440   cname = get_method_class_name(jvmti, jni, method);
441 
442   LOG("\nHit #%d: VirtualThreadMount #%d: enabling FramePop for method: %s::%s on virtual thread: %p\n",
443          brkptBreakpointHit, ++vthread_mounted_count, cname, mname, (void*)thread);
444 
445   err = jvmti->NotifyFramePop(thread, 0);
446   check_jvmti_status(jni, err, "VirtualThreadMount: error in JVMTI NotifyFramePop");
447 
448   print_frame_event_info(jvmti, jni, thread, method, "VirtualThreadMount", vthread_mounted_count);
449 
450   // Test SetThreadLocalStorage for virtual thread.
451   err = jvmti->SetThreadLocalStorage(thread, (void*)222);
452   check_jvmti_status(jni, err, "VirtualThreadMount: error in JVMTI SetThreadLocalStorage");
453 
454 
455   deallocate(jvmti, jni, (void*)mname);
456   deallocate(jvmti, jni, (void*)cname);
457 }
458 
459 // Parameters: (jvmtiEnv *jvmti, JNIEnv* jni, jthread thread)
460 static void JNICALL
461 VirtualThreadUnmount(jvmtiEnv *jvmti, ...) {
462   jmethodID method = NULL;
463   jlocation loc = 0L;
464   char* mname = NULL;
465   char* cname = NULL;
466   jvmtiError err;
467 
468   va_list ap;
469   JNIEnv* jni = NULL;
470   jthread thread = NULL;
471 
472   va_start(ap, jvmti);
473   jni = va_arg(ap, JNIEnv*);
474   thread = va_arg(ap, jthread);
475   va_end(ap);
476 
477   RawMonitorLocker rml(jvmti, jni, event_mon);
478 
479   err = jvmti->GetFrameLocation(thread, 0, &method, &loc);
480   check_jvmti_status(jni, err, "VirtualThreadUnmount: error in JVMTI GetFrameLocation");
481 
482   mname = get_method_name(jvmti, jni, method);
483   cname = get_method_class_name(jvmti, jni, method);
484 
485   LOG("\nHit #%d: VirtualThreadUnmount #%d: enabling FramePop for method: %s::%s on virtual thread: %p\n",
486          brkptBreakpointHit, ++vthread_unmounted_count, cname, mname, (void*)thread);
487 
488   err = jvmti->NotifyFramePop(thread, 0);
489   check_jvmti_status(jni, err, "VirtualThreadUnmount: error in JVMTI NotifyFramePop");
490 
491   print_frame_event_info(jvmti, jni, thread, method, "VirtualThreadUnmount", vthread_unmounted_count);
492 
493 
494   deallocate(jvmti, jni, (void*)mname);
495   deallocate(jvmti, jni, (void*)cname);
496 }
497 
498 JNIEXPORT jint JNICALL
499 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
500   jvmtiEventCallbacks callbacks;
501   jvmtiCapabilities caps;
502   jvmtiError err;
503 
504   LOG("Agent_OnLoad started\n");
505 
506   if (jvm->GetEnv((void **)(&jvmti), JVMTI_VERSION) != JNI_OK) {
507     LOG("Error: GetEnv call for JVMTI_VERSION failed\n");
508     return JNI_ERR;
509   }
510   memset(&callbacks, 0, sizeof(callbacks));
511   callbacks.Breakpoint  = &Breakpoint;
512   callbacks.FramePop    = &FramePop;
513   callbacks.MethodEntry = &MethodEntry;
514   callbacks.MethodExit  = &MethodExit;
515   callbacks.ThreadStart = &ThreadStart;
516   callbacks.VirtualThreadStart = &VirtualThreadStart;
517 
518   err = set_ext_event_callback(jvmti, "VirtualThreadMount", VirtualThreadMount);
519   if (err != JVMTI_ERROR_NONE) {
520     LOG("Agent_OnLoad: Error in JVMTI SetExtEventCallback for VirtualThreadMount: %s(%d)\n",
521            TranslateError(err), err);
522     return JNI_ERR;
523   }
524   err = set_ext_event_callback(jvmti, "VirtualThreadUnmount", VirtualThreadUnmount);
525   if (err != JVMTI_ERROR_NONE) {
526     LOG("Agent_OnLoad: Error in JVMTI SetExtEventCallback for VirtualThreadUnmount: %s(%d)\n",
527            TranslateError(err), err);
528     return JNI_ERR;
529   }
530 
531   memset(&caps, 0, sizeof(caps));
532   caps.can_generate_breakpoint_events = 1;
533   caps.can_generate_frame_pop_events = 1;
534   caps.can_generate_method_entry_events = 1;
535   caps.can_generate_method_exit_events = 1;
536   caps.can_support_virtual_threads = 1;
537 
538   err = jvmti->AddCapabilities(&caps);
539   if (err != JVMTI_ERROR_NONE) {
540     LOG("Agent_OnLoad: Error in JVMTI AddCapabilities: %d\n", err);
541     return JNI_ERR;
542   }
543 
544   err = jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));
545   if (err != JVMTI_ERROR_NONE) {
546     LOG("Agent_OnLoad: Error in JVMTI SetEventCallbacks: %d\n", err);
547     return JNI_ERR;
548   }
549 
550   set_event_notification_mode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, NULL);
551   set_event_notification_mode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL);
552   set_event_notification_mode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VIRTUAL_THREAD_START, NULL);
553 
554   event_mon = create_raw_monitor(jvmti, "Events Monitor");
555 
556   LOG("Agent_OnLoad finished\n");
557 
558 
559   return JNI_OK;
560 }
561 
562 JNIEXPORT void JNICALL
563 Java_MethodExitTest_enableEvents(JNIEnv *jni, jclass klass, jthread thread, jclass testKlass) {
564   jvmtiError err;
565 
566   LOG("enableEvents: started\n");
567 
568   test_class = (jclass)jni->NewGlobalRef(testKlass);
569   err = jvmti->GetClassMethods(testKlass, &test_method_count, &test_methods);
570   check_jvmti_status(jni, err, "enableEvents: error in JVMTI GetClassMethods for testKlass");
571 
572   set_breakpoint(jni, "brkpt", testKlass, test_methods, test_method_count);
573 
574   // Enable Breakpoint events globally
575   set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL);
576 
577   LOG("enableEvents: finished\n");
578 
579 }
580 
581 JNIEXPORT jboolean JNICALL
582 Java_MethodExitTest_check(JNIEnv *jni, jclass cls) {
583   LOG("\n");
584   LOG("check: started\n");
585 
586   LOG("check: vthread_mounted_count:   %d\n", vthread_mounted_count);
587   LOG("check: vthread_unmounted_count: %d\n", vthread_unmounted_count);
588   LOG("check: breakpoint_count:        %d\n", breakpoint_count);
589   LOG("check: method_exit_count:       %d\n", method_exit_count);
590   LOG("check: frame_pop_count:         %d\n", frame_pop_count);
591 
592   if (method_exit_count == 0) {
593     passed = JNI_FALSE;
594     LOG("FAILED: method_exit_count == 0\n");
595   }
596   if (frame_pop_count == 0) {
597     passed = JNI_FALSE;
598     LOG("FAILED: frame_pop_count == 0\n");
599   }
600 
601   LOG("check: finished\n");
602   LOG("\n");
603 
604 
605   return passed;
606 }
607 } // extern "C"