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 <jni.h>
27 #include <jvmti.h>
28
29 #include "jvmti_common.hpp"
30 #include "jvmti_thread.hpp"
31
32
33 extern "C" {
34
35 /* ========================================================================== */
36
37 /* scaffold objects */
38 static JNIEnv *jni = nullptr;
39 static jvmtiEnv *jvmti = nullptr;
40 static jlong timeout = 0;
41
42 /* test objects */
43 static jthread expected_thread = nullptr;
44 static jobject expected_object = nullptr;
45 static volatile int eventsCount = 0;
46
47 /* ========================================================================== */
48
49 void JNICALL
50 MonitorContendedEnter(jvmtiEnv *jvmti, JNIEnv *jni, jthread thr, jobject obj) {
51
52 LOG("MonitorContendedEnter event:\n\tthread: %p, object: %p, expected object: %p\n",thr, obj, expected_object);
53
54 print_thread_info(jvmti, jni, thr);
55
56 if (expected_thread == nullptr) {
57 jni->FatalError("expected_thread is null.");
58 }
59
60 if (expected_object == nullptr) {
61 jni->FatalError("expected_object is null.");
62 }
63
64 /* check if event is for tested thread and for tested object */
65 if (jni->IsSameObject(expected_thread, thr) &&
66 jni->IsSameObject(expected_object, obj)) {
67 eventsCount++;
68 LOG("Increasing eventCount to %d\n", eventsCount);
69 }
70 }
71
72 /* ========================================================================== */
73
74 static int prepare() {
75 jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, nullptr);
76 if (err != JVMTI_ERROR_NONE) {
77 jni->FatalError("Error enabling JVMTI_EVENT_MONITOR_CONTENDED_ENTER.");
78 }
79 return JNI_TRUE;
80 }
81
82 static int clean() {
83 LOG("Disabling events\n");
84 /* disable MonitorContendedEnter event */
85 jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, nullptr);
86 if (err != JVMTI_ERROR_NONE) {
87 set_agent_fail_status();
88 }
89 return JNI_TRUE;
90 }
91
92 /* agent algorithm
93 */
94 static void JNICALL
95 agentProc(jvmtiEnv *jvmti, JNIEnv *agentJNI, void *arg) {
96 jni = agentJNI;
97
98 /* wait for initial sync */
99 if (!agent_wait_for_sync(timeout)) {
100 return;
101 }
102
103 if (!prepare()) {
104 set_agent_fail_status();
105 return;
106 }
107
108 /* clear events count */
109 eventsCount = 0;
110
111 /* resume debugee to catch MonitorContendedEnter event */
112 if (!((agent_resume_sync() == JNI_TRUE) && (agent_wait_for_sync(timeout) == JNI_TRUE))) {
113 return;
114 }
115 LOG("Number of MonitorContendedEnter events: %d\n", eventsCount);
116
117 if (eventsCount == 0) {
118 COMPLAIN("No any MonitorContendedEnter event\n");
119 set_agent_fail_status();
120 }
121
122 if (!clean()) {
123 set_agent_fail_status();
|
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 <jni.h>
27 #include <jvmti.h>
28
29 #include "jvmti_common.hpp"
30 #include "jvmti_thread.hpp"
31
32
33 extern "C" {
34
35 /* ========================================================================== */
36
37 /* scaffold objects */
38 static jvmtiEnv *jvmti = nullptr;
39 static jlong timeout = 0;
40
41 /* test objects */
42 static jthread expected_thread = nullptr;
43 static jobject expected_object = nullptr;
44 static volatile int eventsCount = 0;
45
46 /* ========================================================================== */
47
48 void JNICALL
49 MonitorContendedEnter(jvmtiEnv *jvmti, JNIEnv *jni, jthread thr, jobject obj) {
50
51 LOG("MonitorContendedEnter event:\n\tthread: %p, object: %p, expected object: %p\n",thr, obj, expected_object);
52
53 print_thread_info(jvmti, jni, thr);
54
55 if (expected_thread == nullptr) {
56 jni->FatalError("expected_thread is null.");
57 }
58
59 if (expected_object == nullptr) {
60 jni->FatalError("expected_object is null.");
61 }
62
63 /* check if event is for tested thread and for tested object */
64 if (jni->IsSameObject(expected_thread, thr) &&
65 jni->IsSameObject(expected_object, obj)) {
66 eventsCount++;
67 LOG("Increasing eventCount to %d\n", eventsCount);
68 }
69 }
70
71 /* ========================================================================== */
72
73 static int prepare(JNIEnv* jni) {
74 jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, nullptr);
75 if (err != JVMTI_ERROR_NONE) {
76 jni->FatalError("Error enabling JVMTI_EVENT_MONITOR_CONTENDED_ENTER.");
77 }
78 return JNI_TRUE;
79 }
80
81 static int clean() {
82 LOG("Disabling events\n");
83 /* disable MonitorContendedEnter event */
84 jvmtiError err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, nullptr);
85 if (err != JVMTI_ERROR_NONE) {
86 set_agent_fail_status();
87 }
88 return JNI_TRUE;
89 }
90
91 /* agent algorithm
92 */
93 static void JNICALL
94 agentProc(jvmtiEnv *jvmti, JNIEnv *jni, void *arg) {
95
96 /* wait for initial sync */
97 if (!agent_wait_for_sync(timeout)) {
98 return;
99 }
100
101 if (!prepare(jni)) {
102 set_agent_fail_status();
103 return;
104 }
105
106 /* clear events count */
107 eventsCount = 0;
108
109 /* resume debugee to catch MonitorContendedEnter event */
110 if (!((agent_resume_sync() == JNI_TRUE) && (agent_wait_for_sync(timeout) == JNI_TRUE))) {
111 return;
112 }
113 LOG("Number of MonitorContendedEnter events: %d\n", eventsCount);
114
115 if (eventsCount == 0) {
116 COMPLAIN("No any MonitorContendedEnter event\n");
117 set_agent_fail_status();
118 }
119
120 if (!clean()) {
121 set_agent_fail_status();
|