< prev index next >

test/hotspot/jtreg/serviceability/jvmti/events/MonitorWaited/monitorwaited01/libmonitorwaited01.cpp

Print this page
*** 28,20 ***
  #include "jvmti_thread.hpp"
  
  
  extern "C" {
  
  /* scaffold objects */
- static JNIEnv *jni = nullptr;
  static jvmtiEnv *jvmti = nullptr;
  static jlong timeout = 0;
  
  /* test objects */
  static jthread expected_thread = nullptr;
  static jobject expected_object = nullptr;
  static volatile int eventsCount = 0;
  
  void JNICALL
  MonitorWaited(jvmtiEnv *jvmti, JNIEnv *jni, jthread thr, jobject obj, jboolean timed_out) {
  
    LOG("MonitorWaited event:\n\tthread: %p, object: %p, timed_out: %s\n",
        thr, obj, (timed_out == JNI_TRUE) ? "true" : "false");
--- 28,23 ---
  #include "jvmti_thread.hpp"
  
  
  extern "C" {
  
+ const int MAX_COUNT = 50;
+ 
  /* scaffold objects */
  static jvmtiEnv *jvmti = nullptr;
  static jlong timeout = 0;
  
  /* test objects */
  static jthread expected_thread = nullptr;
  static jobject expected_object = nullptr;
  static volatile int eventsCount = 0;
  
+ static void check_stack_trace(JNIEnv* env, jthread thr);
+ 
  void JNICALL
  MonitorWaited(jvmtiEnv *jvmti, JNIEnv *jni, jthread thr, jobject obj, jboolean timed_out) {
  
    LOG("MonitorWaited event:\n\tthread: %p, object: %p, timed_out: %s\n",
        thr, obj, (timed_out == JNI_TRUE) ? "true" : "false");

*** 63,14 ***
--- 66,53 ---
      if (timed_out == JNI_TRUE) {
        COMPLAIN("Unexpected timed_out value: true\n");
        set_agent_fail_status();
      }
    }
+ 
+   if (jni->IsVirtualThread(thr)) {
+     check_stack_trace(jni, thr);
+   }
  }
  
  /* ========================================================================== */
  
+ static void check_stack_trace(JNIEnv* jni, jthread thr) {
+   jvmtiError err;
+   jint count = 0;
+   jint skipped = 0;
+ 
+   print_stack_trace(jvmti, jni, nullptr);
+ 
+   jvmtiFrameInfo frameInfo[MAX_COUNT];
+ 
+   err = jvmti->GetStackTrace(thr, 0, MAX_COUNT, frameInfo, &count);
+   check_jvmti_status(jni, err, "event handler: error in JVMTI GetStackTrace call");
+ 
+   const int expected_count = 8;
+   const char* expected_methods[expected_count] = {"wait0", "wait", "run", "runWith", "run", "run", "enter0", "enter"};
+ 
+   if (count != expected_count) {
+     LOG("Expected 8 methods in the stack but found %d", count);
+     jni->FatalError("Unexpected method count");
+   }
+ 
+   for (int idx = 0; idx < count; idx++) {
+     jclass declaringClass = nullptr;
+     char *clasSignature = nullptr;
+     char *methodName = nullptr;
+ 
+     err = jvmti->GetMethodName(frameInfo[idx].method, &methodName, nullptr, nullptr);
+     check_jvmti_status(jni, err, "event handler: error in JVMTI GetMethodName call");
+ 
+     if (strcmp(methodName, expected_methods[idx]) != 0) {
+       LOG("Expected method %s but found %s", expected_methods[idx], methodName);
+       jni->FatalError("Unexpected method found");
+     }
+   }
+ }
+ 
  static int prepare() {
    /* enable MonitorWait event */
    jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAITED, nullptr);
    if (err != JVMTI_ERROR_NONE) {
      LOG("Prepare: 11\n");

*** 88,11 ***
    return JNI_TRUE;
  }
  
  static void JNICALL
  agentProc(jvmtiEnv *jvmti, JNIEnv *agentJNI, void *arg) {
-   jni = agentJNI;
  
    /* wait for initial sync */
    if (!agent_wait_for_sync(timeout))
      return;
  
--- 130,10 ---
< prev index next >