Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach045/attach045Agent00.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* Agent receives expected number of ClassLoad events and finishes work34* (events should be provoked by target application)35*/3637#define EXPECTED_EVENTS_NUMBER 5003839static Options* options = NULL;40static const char* agentName;4142static jrawMonitorID eventsCounterMonitor;4344static volatile int eventsCounter = 0;4546void JNICALL classLoadHandler(47jvmtiEnv *jvmti,48JNIEnv* jni,49jthread thread,50jclass klass) {51char className[MAX_STRING_LENGTH];52int success = 1;5354if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {55nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);56return;57}5859if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {6061eventsCounter++;6263NSK_DISPLAY3("%s: ClassLoad event received for class '%s (eventsCounter: %d)'\n", agentName, className, eventsCounter);6465if (eventsCounter == EXPECTED_EVENTS_NUMBER) {66NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);67nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, success, jvmti, jni);68}6970if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {71success = 0;72}73} else {74success = 0;75}7677if (!success) {78nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);79}80}8182#ifdef STATIC_BUILD83JNIEXPORT jint JNI_OnLoad_attach045Agent00(JavaVM *jvm, char *options, void *reserved) {84return JNI_VERSION_1_8;85}86#endif8788JNIEXPORT jint JNICALL89#ifdef STATIC_BUILD90Agent_OnAttach_attach045Agent00(JavaVM *vm, char *optionsString, void *reserved)91#else92Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)93#endif94{95jvmtiEventCallbacks eventCallbacks;96jvmtiEnv* jvmti;97JNIEnv* jni;9899options = (Options*) nsk_aod_createOptions(optionsString);100if (!NSK_VERIFY(options != NULL))101return JNI_ERR;102103agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);104105jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);106if (jni == NULL)107return JNI_ERR;108109jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);110if (!NSK_VERIFY(jvmti != NULL))111return JNI_ERR;112113if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent00-eventsCounterMonitor", &eventsCounterMonitor))) {114return JNI_ERR;115}116117memset(&eventCallbacks,0, sizeof(eventCallbacks));118eventCallbacks.ClassLoad = classLoadHandler;119if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {120return JNI_ERR;121}122123if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {124return JNI_ERR;125}126127NSK_DISPLAY1("%s: initialization was done\n", agentName);128129if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))130return JNI_ERR;131132return JNI_OK;133}134135136}137138139