Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach037/attach037Agent00.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_WAIT, JVMTI_EVENT_MONITOR_WAITED };44static const int testEventsNumber = 2;4546volatile int monitorWaitReceived = 0;4748void JNICALL monitorWaitHandler(jvmtiEnv *jvmti,49JNIEnv* jni,50jthread thread,51jobject object,52jlong timeout) {53char threadName[MAX_STRING_LENGTH];5455if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {56nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);57return;58}5960NSK_DISPLAY2("%s: MonitorWait event was received for thread '%s'\n", agentName, threadName);6162if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) {63monitorWaitReceived = 1;64}65}6667void JNICALL monitorWaitedHandler(jvmtiEnv *jvmti,68JNIEnv* jni,69jthread thread,70jobject object,71jboolean timed_out) {72char threadName[MAX_STRING_LENGTH];7374if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {75nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);76return;77}7879NSK_DISPLAY2("%s: MonitorWaited event was received for thread '%s'\n", agentName, threadName);8081if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) {82int success = 1;8384if (!monitorWaitReceived) {85success = 0;86NSK_COMPLAIN2("%s: MonitorWait event wasn't received for thread '%s'\n", agentName, threadName);87}8889nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);90}91}9293#ifdef STATIC_BUILD94JNIEXPORT jint JNI_OnLoad_attach037Agent00(JavaVM *jvm, char *options, void *reserved) {95return JNI_VERSION_1_8;96}97#endif9899JNIEXPORT jint JNICALL100#ifdef STATIC_BUILD101Agent_OnAttach_attach037Agent00(JavaVM *vm, char *optionsString, void *reserved)102#else103Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)104#endif105{106jvmtiEventCallbacks eventCallbacks;107jvmtiCapabilities caps;108jvmtiEnv* jvmti;109JNIEnv* jni;110111options = (Options*) nsk_aod_createOptions(optionsString);112if (!NSK_VERIFY(options != NULL))113return JNI_ERR;114115agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);116117jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);118if (jni == NULL)119return JNI_ERR;120121jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);122if (!NSK_VERIFY(jvmti != NULL))123return JNI_ERR;124125memset(&caps, 0, sizeof(caps));126caps.can_generate_monitor_events = 1;127if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {128return JNI_ERR;129}130131memset(&eventCallbacks,0, sizeof(eventCallbacks));132eventCallbacks.MonitorWaited = monitorWaitedHandler;133eventCallbacks.MonitorWait = monitorWaitHandler;134if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {135return JNI_ERR;136}137138if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {139return JNI_ERR;140}141142NSK_DISPLAY1("%s: initialization was done\n", agentName);143144if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))145return JNI_ERR;146147return JNI_OK;148}149150}151152153