Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach020/attach020Agent00.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* - from Agent_OnAttach function start auxiliary thread waiting on 'gcFinishMonitor'35* - receive GarbageCollectionStart event36* - receive GarbageCollectionFinish event, notify 'gcFinishMonitor'37* - notified auxiliary thread calls function 'nsk_aod_agentFinished' and agent completes work38* (such schema is used because of agent can't call nsk_aod_agentFinished from GarbageCollectionFinish handler,39* nsk_aod_agentFinished calls JNI functions and it is prohibited in GarbageCollectionFinish handler)40*41*/4243static Options* options = NULL;44static const char* agentName;4546static jvmtiEvent testEvents[] = { JVMTI_EVENT_GARBAGE_COLLECTION_START, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH };47static const int testEventsNumber = 2;4849static jrawMonitorID gcFinishMonitor;5051static volatile int gcStartEventReceived = 0;5253static volatile int gcFinishEventReceived = 0;5455static volatile int success = 1;5657void JNICALL garbageCollectionStartHandler(jvmtiEnv *jvmti) {58NSK_DISPLAY1("%s: GC start event received\n", agentName);5960gcStartEventReceived = 1;61}6263void JNICALL garbageCollectionFinishHandler(jvmtiEnv *jvmti) {64int auxiliaryThreadNotified = 0;6566NSK_DISPLAY1("%s: GC finish event received\n", agentName);6768if (!gcStartEventReceived) {69NSK_COMPLAIN1("%s: GC start event wasn't received before GC finish event\n", agentName);70success = 0;71}7273if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(gcFinishMonitor))) {7475gcFinishEventReceived = 1;7677if (NSK_JVMTI_VERIFY(jvmti->RawMonitorNotify(gcFinishMonitor))) {78auxiliaryThreadNotified = 1;79}8081if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(gcFinishMonitor))) {82success = 0;83}84} else {85success = 0;86}8788if (!auxiliaryThreadNotified) {89NSK_COMPLAIN1("%s: Error happenned during auxiliary thread notification, test may hang\n", agentName);90}91}9293void JNICALL auxiliaryThreadFunction(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {94NSK_DISPLAY1("%s: auxiliary thread is running\n", agentName);9596if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(gcFinishMonitor))) {9798if (!gcFinishEventReceived) {99if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorWait(gcFinishMonitor, 0))) {100success = 0;101}102}103104if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(gcFinishMonitor))) {105success = 0;106}107} else {108success = 0;109}110111nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);112}113114int startAuxiliaryThread(jvmtiEnv* jvmti, JNIEnv* jni) {115jthread thread;116117thread = nsk_jvmti_aod_createThread(jni);118if (!NSK_VERIFY(thread != NULL))119return NSK_FALSE;120121if (!NSK_JVMTI_VERIFY(jvmti->RunAgentThread(thread, auxiliaryThreadFunction, NULL, JVMTI_THREAD_NORM_PRIORITY))) {122return NSK_FALSE;123}124125NSK_DISPLAY1("%s: auxiliary thread was started\n", agentName);126127return NSK_TRUE;128}129130#ifdef STATIC_BUILD131JNIEXPORT jint JNI_OnLoad_attach020Agent00(JavaVM *jvm, char *options, void *reserved) {132return JNI_VERSION_1_8;133}134#endif135136JNIEXPORT jint JNICALL137#ifdef STATIC_BUILD138Agent_OnAttach_attach020Agent00(JavaVM *vm, char *optionsString, void *reserved)139#else140Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)141#endif142{143jvmtiEnv* jvmti;144jvmtiEventCallbacks eventCallbacks;145jvmtiCapabilities caps;146JNIEnv* jni;147148options = (Options*) nsk_aod_createOptions(optionsString);149if (!NSK_VERIFY(options != NULL))150return JNI_ERR;151152agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);153154jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);155if (jni == NULL)156return JNI_ERR;157158jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);159if (!NSK_VERIFY(jvmti != NULL))160return JNI_ERR;161162if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("GCFinishMonitor", &gcFinishMonitor))) {163return JNI_ERR;164}165166if (!NSK_VERIFY(startAuxiliaryThread(jvmti, jni)))167return JNI_ERR;168169memset(&caps, 0, sizeof(caps));170caps.can_generate_garbage_collection_events = 1;171if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {172return JNI_ERR;173}174175memset(&eventCallbacks,0, sizeof(eventCallbacks));176eventCallbacks.GarbageCollectionStart = garbageCollectionStartHandler;177eventCallbacks.GarbageCollectionFinish = garbageCollectionFinishHandler;178if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {179return JNI_ERR;180}181182if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {183return JNI_ERR;184}185186NSK_DISPLAY1("%s: initialization was done\n", agentName);187188if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))189return JNI_ERR;190191return JNI_OK;192}193}194195196