Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach008/attach008Agent00.cpp
40951 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22#include <stdio.h>23#include <stdlib.h>24#include <string.h>25#include <jni.h>26#include <jvmti.h>27#include <aod.h>28#include <jvmti_aod.h>2930extern "C" {3132/*33* Expected agent work scenario:34* - receive MonitorContentedEnter event for thread ThreadGeneratingEvents35* - receive MonitorContentedEntered event for thread ThreadGeneratingEvents and finish work36*/3738#define THREAD_GENERATING_EVENTS_NAME "ThreadGeneratingEvents"3940static Options* options = NULL;41static const char* agentName;4243static jvmtiEvent testEvents[] = { JVMTI_EVENT_MONITOR_CONTENDED_ENTER, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED };44static const int testEventsNumber = 2;4546static volatile int monitorEnter = 0;4748static int success = 1;4950void JNICALL monitorContentedEnterHandler(jvmtiEnv * jvmti,51JNIEnv * jni,52jthread thread,53jobject object) {54char threadName[MAX_STRING_LENGTH];5556if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {57nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);58return;59}6061NSK_DISPLAY2("%s: MonitorContentedEnter event received for thread '%s'\n", agentName, threadName);6263if (!strcmp(threadName, THREAD_GENERATING_EVENTS_NAME)) {64monitorEnter = 1;65}66}6768void JNICALL monitorContentedEnteredHandler(69jvmtiEnv * jvmti,70JNIEnv * jni,71jthread thread,72jobject object) {73char threadName[MAX_STRING_LENGTH];7475if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {76nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);77return;78}7980NSK_DISPLAY2("%s: MonitorContentedEntered event received for thread '%s'\n", agentName, threadName);8182if (!strcmp(threadName, THREAD_GENERATING_EVENTS_NAME)) {83int success = 1;8485if (!monitorEnter) {86success = 0;87NSK_COMPLAIN2("%s: MonitorContentedEnter event wasn't received for thread %s\n", agentName, threadName);88}8990nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);91}92}9394#ifdef STATIC_BUILD95JNIEXPORT jint JNI_OnLoad_attach008Agent00(JavaVM *jvm, char *options, void *reserved) {96return JNI_VERSION_1_8;97}98#endif99100JNIEXPORT jint JNICALL101#ifdef STATIC_BUILD102Agent_OnAttach_attach008Agent00(JavaVM *vm, char *optionsString, void *reserved)103#else104Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)105#endif106{107jvmtiEventCallbacks eventCallbacks;108jvmtiCapabilities caps;109jvmtiEnv* jvmti = NULL;110JNIEnv* jni = NULL;111112options = (Options*) nsk_aod_createOptions(optionsString);113if (!NSK_VERIFY(options != NULL))114return JNI_ERR;115116agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);117118jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);119if (jni == NULL)120return NSK_FALSE;121122jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);123if (!NSK_VERIFY(jvmti != NULL))124return JNI_ERR;125126memset(&caps, 0, sizeof(caps));127caps.can_generate_monitor_events = 1;128if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {129return JNI_ERR;130}131132memset(&eventCallbacks,0, sizeof(eventCallbacks));133eventCallbacks.MonitorContendedEntered = monitorContentedEnteredHandler;134eventCallbacks.MonitorContendedEnter = monitorContentedEnterHandler;135if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {136return JNI_ERR;137}138139if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {140return JNI_ERR;141}142143NSK_DISPLAY1("%s: initialization was done\n", agentName);144145if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))146return JNI_ERR;147148return JNI_OK;149}150151}152153154