1 /*
   2  * Copyright (c) 2007, 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 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 #include <jni.h>
  27 #include <jvmti.h>
  28 #include <aod.h>
  29 #include <jvmti_aod.h>
  30 
  31 extern "C" {
  32 
  33 /*
  34  * Expected agent work scenario:
  35  *  - receive MonitorContentedEnter event for thread ThreadGeneratingEvents
  36  *  - receive MonitorContentedEntered event for thread ThreadGeneratingEvents and finish work
  37  */
  38 
  39 #define THREAD_GENERATING_EVENTS_NAME "ThreadGeneratingEvents"
  40 
  41 static Options* options = NULL;
  42 static const char* agentName;
  43 
  44 static jvmtiEvent testEvents[] = {JVMTI_EVENT_MONITOR_CONTENDED_ENTER, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED};
  45 static const int testEventsNumber = 2;
  46 
  47 static volatile int monitorEnter = 0;
  48 
  49 static int success = 1;
  50 
  51 void JNICALL monitorContentedEnterHandler(jvmtiEnv * jvmti,
  52         JNIEnv * jni,
  53         jthread thread,
  54         jobject object) {
  55     char threadName[MAX_STRING_LENGTH];
  56 
  57     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
  58         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  59         return;
  60     }
  61 
  62     NSK_DISPLAY2("%s: MonitorContentedEnter event received for thread '%s'\n", agentName, threadName);
  63 
  64     if (!strcmp(threadName, THREAD_GENERATING_EVENTS_NAME)) {
  65         monitorEnter = 1;
  66     }
  67 }
  68 
  69 void JNICALL monitorContentedEnteredHandler(
  70         jvmtiEnv * jvmti,
  71         JNIEnv * jni,
  72         jthread thread,
  73         jobject object) {
  74     char threadName[MAX_STRING_LENGTH];
  75 
  76     if(!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
  77         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  78         return;
  79     }
  80 
  81     NSK_DISPLAY2("%s: MonitorContentedEntered event received for thread '%s'\n", agentName, threadName);
  82 
  83     if (!strcmp(threadName, THREAD_GENERATING_EVENTS_NAME)) {
  84         int success = 1;
  85 
  86         if (!monitorEnter) {
  87             success = 0;
  88             NSK_COMPLAIN2("%s: MonitorContentedEnter event wasn't received for thread %s\n", agentName, threadName);
  89         }
  90 
  91         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
  92     }
  93 }
  94 
  95 #ifdef STATIC_BUILD
  96 JNIEXPORT jint JNI_OnLoad_attach008Agent00(JavaVM *jvm, char *options, void *reserved) {
  97     return JNI_VERSION_1_8;
  98 }
  99 #endif
 100 
 101 JNIEXPORT jint JNICALL
 102 #ifdef STATIC_BUILD
 103 Agent_OnAttach_attach008Agent00(JavaVM *vm, char *optionsString, void *reserved)
 104 #else
 105 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 106 #endif
 107 {
 108     jvmtiEventCallbacks eventCallbacks;
 109     jvmtiCapabilities caps;
 110     jvmtiEnv* jvmti = NULL;
 111     JNIEnv* jni = NULL;
 112 
 113     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 114         return JNI_ERR;
 115 
 116     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 117 
 118     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 119         return NSK_FALSE;
 120 
 121     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 122         return JNI_ERR;
 123 
 124     memset(&caps, 0, sizeof(caps));
 125     caps.can_generate_monitor_events = 1;
 126     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)) ) {
 127         return JNI_ERR;
 128     }
 129 
 130     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 131     eventCallbacks.MonitorContendedEntered = monitorContentedEnteredHandler;
 132     eventCallbacks.MonitorContendedEnter = monitorContentedEnterHandler;
 133     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 134         return JNI_ERR;
 135     }
 136 
 137     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 138         return JNI_ERR;
 139     }
 140 
 141     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 142 
 143     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 144         return JNI_ERR;
 145 
 146     return JNI_OK;
 147 }
 148 
 149 }