1 /*
  2  * Copyright (c) 2003, 2018, 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 <stdio.h>
 25 #include <string.h>
 26 #include "jni_tools.h"
 27 #include "agent_common.h"
 28 #include "jvmti_tools.h"
 29 
 30 extern "C" {
 31 
 32 /* ========================================================================== */
 33 
 34 /* scaffold objects */
 35 static JNIEnv* jni = NULL;
 36 static jvmtiEnv *jvmti = NULL;
 37 static jlong timeout = 0;
 38 
 39 /* test objects */
 40 static jthread thread = NULL;
 41 static jobject object = NULL;
 42 static volatile int eventsCount = 0;
 43 
 44 /* ========================================================================== */
 45 
 46 void JNICALL
 47 MonitorContendedEnter(jvmtiEnv *jvmti, JNIEnv* jni, jthread thr, jobject obj) {
 48 
 49     NSK_DISPLAY2("MonitorContendedEnter event:\n\tthread: %p, object: %p\n",
 50         thr, obj);
 51 
 52     if (!NSK_VERIFY(thread != NULL)) {
 53         nsk_jvmti_setFailStatus();
 54         return;
 55     }
 56 
 57     /* check if event is for tested thread and for tested object */
 58     if (jni->IsSameObject(thread, thr) &&
 59         jni->IsSameObject(object, obj))
 60             eventsCount++;
 61 }
 62 
 63 /* ========================================================================== */
 64 
 65 static int prepare() {
 66     const char* THREAD_NAME = "Debuggee Thread";
 67     jclass klass = NULL;
 68     jfieldID field = NULL;
 69     jvmtiThreadInfo info;
 70     jthread *threads = NULL;
 71     jint threads_count = 0;
 72     int i;
 73 
 74     NSK_DISPLAY0("Prepare: find tested thread\n");
 75 
 76     /* get all live threads */
 77     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threads_count, &threads)))
 78         return NSK_FALSE;
 79 
 80     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
 81         return NSK_FALSE;
 82 
 83     /* find tested thread */
 84     for (i = 0; i < threads_count; i++) {
 85         if (!NSK_VERIFY(threads[i] != NULL))
 86             return NSK_FALSE;
 87 
 88         /* get thread information */
 89         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))
 90             return NSK_FALSE;
 91 
 92         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
 93 
 94         /* find by name */
 95         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
 96             thread = threads[i];
 97         }
 98     }
 99 
100     /* deallocate threads list */
101     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))
102         return NSK_FALSE;
103 
104     if (thread == NULL) {
105         NSK_COMPLAIN0("Debuggee thread not found");
106         return NSK_FALSE;
107     }
108 
109     /* make thread accessable for a long time */
110     if (!NSK_JNI_VERIFY(jni, (thread = jni->NewGlobalRef(thread)) != NULL))
111         return NSK_FALSE;
112 
113     /* get tested thread class */
114     if (!NSK_JNI_VERIFY(jni, (klass = jni->GetObjectClass(thread)) != NULL))
115         return NSK_FALSE;
116 
117     /* get tested thread field 'endingMonitor' */
118     if (!NSK_JNI_VERIFY(jni, (field =
119             jni->GetFieldID(klass, "endingMonitor", "Ljava/lang/Object;")) != NULL))
120         return NSK_FALSE;
121 
122     /* get 'endingMonitor' object */
123     if (!NSK_JNI_VERIFY(jni, (object = jni->GetObjectField(thread, field)) != NULL))
124         return NSK_FALSE;
125 
126     /* make object accessable for a long time */
127     if (!NSK_JNI_VERIFY(jni, (object = jni->NewGlobalRef(object)) != NULL))
128         return NSK_FALSE;
129 
130     /* enable MonitorContendedEnter event */
131     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE,
132                                                           JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
133                                                           NULL)))
134         return NSK_FALSE;
135 
136     return NSK_TRUE;
137 }
138 
139 static int clean() {
140 
141     /* disable MonitorContendedEnter event */
142     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE,
143                                                           JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
144                                                           NULL)))
145         nsk_jvmti_setFailStatus();
146 
147     return NSK_TRUE;
148 }
149 
150 /* ========================================================================== */
151 
152 /* agent algorithm
153  */
154 static void JNICALL
155 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
156     jni = agentJNI;
157 
158     /* wait for initial sync */
159     if (!nsk_jvmti_waitForSync(timeout))
160         return;
161 
162     if (!prepare()) {
163         nsk_jvmti_setFailStatus();
164         return;
165     }
166 
167     /* clear events count */
168     eventsCount = 0;
169 
170     /* resume debugee to catch MonitorContendedEnter event */
171     if (!(NSK_VERIFY(nsk_jvmti_resumeSync()) &&
172           NSK_VERIFY(nsk_jvmti_waitForSync(timeout))))
173         return;
174 
175     NSK_DISPLAY1("Number of MonitorContendedEnter events: %d\n", eventsCount);
176 
177     if (eventsCount == 0) {
178         NSK_COMPLAIN0("No any MonitorContendedEnter event\n");
179         nsk_jvmti_setFailStatus();
180     }
181 
182     if (!clean()) {
183         nsk_jvmti_setFailStatus();
184         return;
185     }
186 
187     /* resume debugee after last sync */
188     if (!nsk_jvmti_resumeSync())
189         return;
190 }
191 
192 /* ========================================================================== */
193 
194 /* agent library initialization
195  */
196 #ifdef STATIC_BUILD
197 JNIEXPORT jint JNICALL Agent_OnLoad_mcontenter001(JavaVM *jvm, char *options, void *reserved) {
198     return Agent_Initialize(jvm, options, reserved);
199 }
200 JNIEXPORT jint JNICALL Agent_OnAttach_mcontenter001(JavaVM *jvm, char *options, void *reserved) {
201     return Agent_Initialize(jvm, options, reserved);
202 }
203 JNIEXPORT jint JNI_OnLoad_mcontenter001(JavaVM *jvm, char *options, void *reserved) {
204     return JNI_VERSION_1_8;
205 }
206 #endif
207 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
208     jvmtiCapabilities caps;
209     jvmtiEventCallbacks callbacks;
210 
211     /* init framework and parse options */
212     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
213         return JNI_ERR;
214 
215     timeout = nsk_jvmti_getWaitTime() * 60000;
216     NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
217 
218     /* create JVMTI environment */
219     if (!NSK_VERIFY((jvmti =
220             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
221         return JNI_ERR;
222 
223     if (!NSK_JVMTI_VERIFY(jvmti->GetPotentialCapabilities(&caps)))
224         return JNI_ERR;
225 
226     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
227         return JNI_ERR;
228 
229     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
230         return JNI_ERR;
231 
232     if (!NSK_VERIFY(caps.can_generate_monitor_events))
233         return JNI_ERR;
234 
235     memset(&callbacks, 0, sizeof(callbacks));
236     callbacks.MonitorContendedEnter = &MonitorContendedEnter;
237     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
238         return JNI_ERR;
239 
240     /* register agent proc and arg */
241     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
242         return JNI_ERR;
243 
244     return JNI_OK;
245 }
246 
247 JNIEXPORT jint JNICALL Java_nsk_jvmti_MonitorContendedEnter_mcontenter001_getEventCount
248   (JNIEnv *env, jobject obj)
249 {
250     return eventsCount;
251 }
252 
253 /* ========================================================================== */
254 
255 }