< prev index next >

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

Print this page

 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 <stdio.h>
 25 #include <string.h>
 26 #include <jvmti.h>
 27 #include "jvmti_common.hpp"
 28 #include "jvmti_thread.hpp"
 29 
 30 
 31 extern "C" {
 32 


 33 /* scaffold objects */
 34 static JNIEnv *jni = nullptr;
 35 static jvmtiEnv *jvmti = nullptr;
 36 static jlong timeout = 0;
 37 
 38 /* test objects */
 39 static jthread expected_thread = nullptr;
 40 static jobject expected_object = nullptr;
 41 static volatile int eventsCount = 0;
 42 


 43 void JNICALL
 44 MonitorWaited(jvmtiEnv *jvmti, JNIEnv *jni, jthread thr, jobject obj, jboolean timed_out) {
 45 
 46   LOG("MonitorWaited event:\n\tthread: %p, object: %p, timed_out: %s\n",
 47       thr, obj, (timed_out == JNI_TRUE) ? "true" : "false");
 48 
 49   print_thread_info(jvmti, jni, thr);
 50 
 51   if (expected_thread == nullptr) {
 52     jni->FatalError("expected_thread is null.");
 53   }
 54 
 55   if (expected_object == nullptr) {
 56     jni->FatalError("expected_object is null.");
 57   }
 58 
 59   /* check if event is for tested thread and for tested object */
 60   if (jni->IsSameObject(expected_thread, thr) &&
 61       jni->IsSameObject(expected_object, obj)) {
 62     eventsCount++;
 63     if (timed_out == JNI_TRUE) {
 64       COMPLAIN("Unexpected timed_out value: true\n");
 65       set_agent_fail_status();
 66     }
 67   }




 68 }
 69 
 70 /* ========================================================================== */
 71 



































 72 static int prepare() {
 73   /* enable MonitorWait event */
 74   jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAITED, nullptr);
 75   if (err != JVMTI_ERROR_NONE) {
 76     LOG("Prepare: 11\n");
 77     return JNI_FALSE;
 78   }
 79   return JNI_TRUE;
 80 }
 81 
 82 static int clean() {
 83   /* disable MonitorWaited event */
 84   jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_WAITED, nullptr);
 85   if (err != JVMTI_ERROR_NONE) {
 86     set_agent_fail_status();
 87   }
 88   return JNI_TRUE;
 89 }
 90 
 91 static void JNICALL
 92 agentProc(jvmtiEnv *jvmti, JNIEnv *agentJNI, void *arg) {
 93   jni = agentJNI;
 94 
 95   /* wait for initial sync */
 96   if (!agent_wait_for_sync(timeout))
 97     return;
 98 
 99   if (!prepare()) {
100     set_agent_fail_status();
101     return;
102   }
103 
104   /* clear events count */
105   eventsCount = 0;
106 
107   /* resume debugee to catch MonitorWaited event */
108   if (!((agent_resume_sync() == JNI_TRUE) && (agent_wait_for_sync(timeout) == JNI_TRUE))) {
109     return;
110   }
111 
112   LOG("Number of MonitorWaited events: %d\n", eventsCount);
113 

 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 <stdio.h>
 25 #include <string.h>
 26 #include <jvmti.h>
 27 #include "jvmti_common.hpp"
 28 #include "jvmti_thread.hpp"
 29 
 30 
 31 extern "C" {
 32 
 33 const int MAX_COUNT = 50;
 34 
 35 /* scaffold objects */

 36 static jvmtiEnv *jvmti = nullptr;
 37 static jlong timeout = 0;
 38 
 39 /* test objects */
 40 static jthread expected_thread = nullptr;
 41 static jobject expected_object = nullptr;
 42 static volatile int eventsCount = 0;
 43 
 44 static void check_stack_trace(JNIEnv* env, jthread thr);
 45 
 46 void JNICALL
 47 MonitorWaited(jvmtiEnv *jvmti, JNIEnv *jni, jthread thr, jobject obj, jboolean timed_out) {
 48 
 49   LOG("MonitorWaited event:\n\tthread: %p, object: %p, timed_out: %s\n",
 50       thr, obj, (timed_out == JNI_TRUE) ? "true" : "false");
 51 
 52   print_thread_info(jvmti, jni, thr);
 53 
 54   if (expected_thread == nullptr) {
 55     jni->FatalError("expected_thread is null.");
 56   }
 57 
 58   if (expected_object == nullptr) {
 59     jni->FatalError("expected_object is null.");
 60   }
 61 
 62   /* check if event is for tested thread and for tested object */
 63   if (jni->IsSameObject(expected_thread, thr) &&
 64       jni->IsSameObject(expected_object, obj)) {
 65     eventsCount++;
 66     if (timed_out == JNI_TRUE) {
 67       COMPLAIN("Unexpected timed_out value: true\n");
 68       set_agent_fail_status();
 69     }
 70   }
 71 
 72   if (jni->IsVirtualThread(thr)) {
 73     check_stack_trace(jni, thr);
 74   }
 75 }
 76 
 77 /* ========================================================================== */
 78 
 79 static void check_stack_trace(JNIEnv* jni, jthread thr) {
 80   jvmtiError err;
 81   jint count = 0;
 82   jint skipped = 0;
 83 
 84   print_stack_trace(jvmti, jni, nullptr);
 85 
 86   jvmtiFrameInfo frameInfo[MAX_COUNT];
 87 
 88   err = jvmti->GetStackTrace(thr, 0, MAX_COUNT, frameInfo, &count);
 89   check_jvmti_status(jni, err, "event handler: error in JVMTI GetStackTrace call");
 90 
 91   const int expected_count = 8;
 92   const char* expected_methods[expected_count] = {"wait0", "wait", "run", "runWith", "run", "run", "enter0", "enter"};
 93 
 94   if (count != expected_count) {
 95     LOG("Expected 8 methods in the stack but found %d", count);
 96     jni->FatalError("Unexpected method count");
 97   }
 98 
 99   for (int idx = 0; idx < count; idx++) {
100     jclass declaringClass = nullptr;
101     char *clasSignature = nullptr;
102     char *methodName = nullptr;
103 
104     err = jvmti->GetMethodName(frameInfo[idx].method, &methodName, nullptr, nullptr);
105     check_jvmti_status(jni, err, "event handler: error in JVMTI GetMethodName call");
106 
107     if (strcmp(methodName, expected_methods[idx]) != 0) {
108       LOG("Expected method %s but found %s", expected_methods[idx], methodName);
109       jni->FatalError("Unexpected method found");
110     }
111   }
112 }
113 
114 static int prepare() {
115   /* enable MonitorWait event */
116   jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAITED, nullptr);
117   if (err != JVMTI_ERROR_NONE) {
118     LOG("Prepare: 11\n");
119     return JNI_FALSE;
120   }
121   return JNI_TRUE;
122 }
123 
124 static int clean() {
125   /* disable MonitorWaited event */
126   jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_WAITED, nullptr);
127   if (err != JVMTI_ERROR_NONE) {
128     set_agent_fail_status();
129   }
130   return JNI_TRUE;
131 }
132 
133 static void JNICALL
134 agentProc(jvmtiEnv *jvmti, JNIEnv *agentJNI, void *arg) {

135 
136   /* wait for initial sync */
137   if (!agent_wait_for_sync(timeout))
138     return;
139 
140   if (!prepare()) {
141     set_agent_fail_status();
142     return;
143   }
144 
145   /* clear events count */
146   eventsCount = 0;
147 
148   /* resume debugee to catch MonitorWaited event */
149   if (!((agent_resume_sync() == JNI_TRUE) && (agent_wait_for_sync(timeout) == JNI_TRUE))) {
150     return;
151   }
152 
153   LOG("Number of MonitorWaited events: %d\n", eventsCount);
154 
< prev index next >