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(jvmti_env->GetThreadInfo(thread, &info))) {
  59         nsk_jvmti_setFailStatus();
  60         return;
  61     }
  62     NSK_DISPLAY2("MonitorContendedEnter event: thread=\"%s\", object=0x%p\n",
  63         info.name, object);
  64 }
  65 
  66 static void JNICALL
  67 MonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
  68         jthread thread, jobject object) {
  69     jvmtiThreadInfo info;
  70 
  71     MonitorContendedEnteredEventsCount++;
  72 
  73     /* get thread information */
  74     if (!NSK_JVMTI_VERIFY(jvmti_env->GetThreadInfo(thread, &info))) {
  75         nsk_jvmti_setFailStatus();
  76         return;
  77     }
  78     NSK_DISPLAY2("MonitorContendedEntered event: thread=\"%s\", object=0x%p\n",
  79         info.name, object);
  80 }
  81 
  82 static void JNICALL
  83 MonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
  84         jthread thread, jobject object, jlong timeout) {
  85     jvmtiThreadInfo info;
  86 
  87     MonitorWaitEventsCount++;
  88 
  89     /* get thread information */
  90     if (!NSK_JVMTI_VERIFY(jvmti_env->GetThreadInfo(thread, &info))) {
  91         nsk_jvmti_setFailStatus();
  92         return;
  93     }
  94     NSK_DISPLAY2("MonitorWait event: thread=\"%s\", object=0x%p\n",
  95         info.name, object);
  96 }
  97 
  98 static void JNICALL
  99 MonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
 100         jthread thread, jobject object, jboolean timed_out) {
 101     jvmtiThreadInfo info;
 102 
 103     MonitorWaitedEventsCount++;
 104 
 105     /* get thread information */
 106     if (!NSK_JVMTI_VERIFY(jvmti_env->GetThreadInfo(thread, &info))) {
 107         nsk_jvmti_setFailStatus();
 108         return;
 109     }
 110     NSK_DISPLAY2("MonitorWaited event: thread=\"%s\", object=0x%p\n",
 111         info.name, object);
 112 }
 113 
 114 /* ========================================================================== */
 115 
 116 /** Agent algorithm. */
 117 static void JNICALL
 118 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 119 
 120     if (!nsk_jvmti_waitForSync(timeout))
 121         return;
 122 
 123     /* resume debugee and wait for sync */
 124     if (!nsk_jvmti_resumeSync())
 125         return;
 126     if (!nsk_jvmti_waitForSync(timeout))
 127         return;
 128 
 129     NSK_DISPLAY1("MonitorContendedEnter events received: %d\n",
 130         MonitorContendedEnterEventsCount);
 131     if (!NSK_VERIFY(MonitorContendedEnterEventsCount == 0))
 132         nsk_jvmti_setFailStatus();
 133 
 134     NSK_DISPLAY1("MonitorContendedEntered events received: %d\n",
 135         MonitorContendedEnteredEventsCount);
 136     if (!NSK_VERIFY(MonitorContendedEnteredEventsCount == 0))
 137         nsk_jvmti_setFailStatus();
 138 
 139     NSK_DISPLAY1("MonitorWait events received: %d\n",
 140         MonitorWaitEventsCount);
 141     if (!NSK_VERIFY(MonitorWaitEventsCount == 0))
 142         nsk_jvmti_setFailStatus();
 143 
 144     NSK_DISPLAY1("MonitorWaited events received: %d\n",
 145         MonitorWaitedEventsCount);
 146     if (!NSK_VERIFY(MonitorWaitedEventsCount == 0))
 147         nsk_jvmti_setFailStatus();
 148 
 149     if (!nsk_jvmti_resumeSync())
 150         return;
 151 }
 152 
 153 /* ========================================================================== */
 154 
 155 /** Agent library initialization. */
 156 #ifdef STATIC_BUILD
 157 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t008a(JavaVM *jvm, char *options, void *reserved) {
 158     return Agent_Initialize(jvm, options, reserved);
 159 }
 160 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t008a(JavaVM *jvm, char *options, void *reserved) {
 161     return Agent_Initialize(jvm, options, reserved);
 162 }
 163 JNIEXPORT jint JNI_OnLoad_ma10t008a(JavaVM *jvm, char *options, void *reserved) {
 164     return JNI_VERSION_1_8;
 165 }
 166 #endif
 167 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 168     jvmtiEnv* jvmti = NULL;
 169     jvmtiCapabilities caps;
 170     jvmtiEventCallbacks callbacks;
 171 
 172     NSK_DISPLAY0("Agent_OnLoad\n");
 173 
 174     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 175         return JNI_ERR;
 176 
 177     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 178 
 179     if (!NSK_VERIFY((jvmti =
 180             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 181         return JNI_ERR;
 182 
 183     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 184         return JNI_ERR;
 185 
 186     memset(&caps, 0, sizeof(caps));
 187     caps.can_generate_monitor_events = 1;
 188     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 189         return JNI_ERR;
 190     }
 191 
 192     memset(&callbacks, 0, sizeof(callbacks));
 193     callbacks.MonitorContendedEnter = &MonitorContendedEnter;
 194     callbacks.MonitorContendedEntered = &MonitorContendedEntered;
 195     callbacks.MonitorWait = &MonitorWait;
 196     callbacks.MonitorWaited = &MonitorWaited;
 197     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 198         return JNI_ERR;
 199 
 200     return JNI_OK;
 201 }
 202 
 203 /* ========================================================================== */
 204 
 205 }