1 /*
   2  * Copyright (c) 2004, 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 <stdlib.h>
  25 #include <string.h>
  26 #include "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 #define PASSED 0
  31 #define STATUS_FAILED 2
  32 
  33 extern "C" {
  34 
  35 /* ========================================================================== */
  36 
  37 /* scaffold objects */
  38 static jlong timeout = 0;
  39 
  40 /* event counts */
  41 static int MonitorContendedEnterEventsCount = 0;
  42 static int MonitorContendedEnteredEventsCount = 0;
  43 static int MonitorWaitEventsCount = 0;
  44 static int MonitorWaitedEventsCount = 0;
  45 
  46 /* ========================================================================== */
  47 
  48 /** callback functions **/
  49 
  50 static void JNICALL
  51 MonitorContendedEnter(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
  52         jthread thread, jobject object) {
  53     jvmtiThreadInfo info;
  54 
  55     MonitorContendedEnterEventsCount++;
  56 
  57     /* get thread information */
  58     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadInfo, jvmti_env,
  59             thread, &info))) {
  60         nsk_jvmti_setFailStatus();
  61         return;
  62     }
  63     NSK_DISPLAY2("MonitorContendedEnter event: thread=\"%s\", object=0x%p\n",
  64         info.name, object);
  65 }
  66 
  67 static void JNICALL
  68 MonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
  69         jthread thread, jobject object) {
  70     jvmtiThreadInfo info;
  71 
  72     MonitorContendedEnteredEventsCount++;
  73 
  74     /* get thread information */
  75     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadInfo, jvmti_env,
  76             thread, &info))) {
  77         nsk_jvmti_setFailStatus();
  78         return;
  79     }
  80     NSK_DISPLAY2("MonitorContendedEntered event: thread=\"%s\", object=0x%p\n",
  81         info.name, object);
  82 }
  83 
  84 static void JNICALL
  85 MonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
  86         jthread thread, jobject object, jlong timeout) {
  87     jvmtiThreadInfo info;
  88 
  89     MonitorWaitEventsCount++;
  90 
  91     /* get thread information */
  92     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadInfo, jvmti_env,
  93             thread, &info))) {
  94         nsk_jvmti_setFailStatus();
  95         return;
  96     }
  97     NSK_DISPLAY2("MonitorWait event: thread=\"%s\", object=0x%p\n",
  98         info.name, object);
  99 }
 100 
 101 static void JNICALL
 102 MonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
 103         jthread thread, jobject object, jboolean timed_out) {
 104     jvmtiThreadInfo info;
 105 
 106     MonitorWaitedEventsCount++;
 107 
 108     /* get thread information */
 109     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadInfo, jvmti_env,
 110             thread, &info))) {
 111         nsk_jvmti_setFailStatus();
 112         return;
 113     }
 114     NSK_DISPLAY2("MonitorWaited event: thread=\"%s\", object=0x%p\n",
 115         info.name, object);
 116 }
 117 
 118 /* ========================================================================== */
 119 
 120 /** Agent algorithm. */
 121 static void JNICALL
 122 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 123 
 124     if (!nsk_jvmti_waitForSync(timeout))
 125         return;
 126 
 127     /* resume debugee and wait for sync */
 128     if (!nsk_jvmti_resumeSync())
 129         return;
 130     if (!nsk_jvmti_waitForSync(timeout))
 131         return;
 132 
 133     NSK_DISPLAY1("MonitorContendedEnter events received: %d\n",
 134         MonitorContendedEnterEventsCount);
 135     if (!NSK_VERIFY(MonitorContendedEnterEventsCount != 0))
 136         nsk_jvmti_setFailStatus();
 137 
 138     NSK_DISPLAY1("MonitorContendedEntered events received: %d\n",
 139         MonitorContendedEnteredEventsCount);
 140     if (!NSK_VERIFY(MonitorContendedEnteredEventsCount != 0))
 141         nsk_jvmti_setFailStatus();
 142 
 143     NSK_DISPLAY1("MonitorWait events received: %d\n",
 144         MonitorWaitEventsCount);
 145     if (!NSK_VERIFY(MonitorWaitEventsCount != 0))
 146         nsk_jvmti_setFailStatus();
 147 
 148     NSK_DISPLAY1("MonitorWaited events received: %d\n",
 149         MonitorWaitedEventsCount);
 150     if (!NSK_VERIFY(MonitorWaitedEventsCount != 0))
 151         nsk_jvmti_setFailStatus();
 152 
 153     if (!nsk_jvmti_resumeSync())
 154         return;
 155 }
 156 
 157 /* ========================================================================== */
 158 
 159 /** Agent library initialization. */
 160 #ifdef STATIC_BUILD
 161 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t008(JavaVM *jvm, char *options, void *reserved) {
 162     return Agent_Initialize(jvm, options, reserved);
 163 }
 164 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t008(JavaVM *jvm, char *options, void *reserved) {
 165     return Agent_Initialize(jvm, options, reserved);
 166 }
 167 JNIEXPORT jint JNI_OnLoad_ma10t008(JavaVM *jvm, char *options, void *reserved) {
 168     return JNI_VERSION_1_8;
 169 }
 170 #endif
 171 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 172     jvmtiEnv* jvmti = NULL;
 173     jvmtiCapabilities caps;
 174     jvmtiEventCallbacks callbacks;
 175 
 176     NSK_DISPLAY0("Agent_OnLoad\n");
 177 
 178     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 179         return JNI_ERR;
 180 
 181     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 182 
 183     if (!NSK_VERIFY((jvmti =
 184             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 185         return JNI_ERR;
 186 
 187     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 188         return JNI_ERR;
 189 
 190     memset(&caps, 0, sizeof(caps));
 191     caps.can_generate_monitor_events = 1;
 192     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 193         return JNI_ERR;
 194     }
 195 
 196     memset(&callbacks, 0, sizeof(callbacks));
 197     callbacks.MonitorContendedEnter = &MonitorContendedEnter;
 198     callbacks.MonitorContendedEntered = &MonitorContendedEntered;
 199     callbacks.MonitorWait = &MonitorWait;
 200     callbacks.MonitorWaited = &MonitorWaited;
 201     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 202         return JNI_ERR;
 203 
 204     /* enable events */
 205     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 206             jvmti, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, NULL)))
 207         return JNI_ERR;
 208     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 209             jvmti, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL)))
 210         return JNI_ERR;
 211     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 212             jvmti, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAIT, NULL)))
 213         return JNI_ERR;
 214     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 215             jvmti, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAITED, NULL)))
 216         return JNI_ERR;
 217 
 218     return JNI_OK;
 219 }
 220 
 221 /* ========================================================================== */
 222 
 223 }