Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach045/attach045Agent01.cpp
40955 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 ClassPrepare 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 classPrepareHandler(47jvmtiEnv *jvmti,48JNIEnv* jni,49jthread thread,50jclass klass) {51int success = 1;52char className[MAX_STRING_LENGTH];53jint loadedClassesCount;54jclass *loadedClasses;5556if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {57nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni);58return;59}6061if (!NSK_JVMTI_VERIFY(jvmti->GetLoadedClasses(&loadedClassesCount, &loadedClasses))) {62NSK_COMPLAIN1("%s: failed to get loaded classes\n", agentName);63nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni);64return;65}6667nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)loadedClasses);6869if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {7071eventsCounter++;7273NSK_DISPLAY3("%s: ClassPrepare event received for class '%s' (eventsCounter: %d)\n", agentName, className, eventsCounter);7475if (eventsCounter == EXPECTED_EVENTS_NUMBER) {76NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);7778nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, success, jvmti, jni);79}8081if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {82success = 0;83}84} else {85success = 0;86}8788if (!success) {89nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni);90}91}9293#ifdef STATIC_BUILD94JNIEXPORT jint JNI_OnLoad_attach045Agent01(JavaVM *jvm, char *options, void *reserved) {95return JNI_VERSION_1_8;96}97#endif9899JNIEXPORT jint JNICALL100#ifdef STATIC_BUILD101Agent_OnAttach_attach045Agent01(JavaVM *vm, char *optionsString, void *reserved)102#else103Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)104#endif105{106jvmtiEventCallbacks eventCallbacks;107jvmtiEnv* jvmti;108JNIEnv* jni;109110options = (Options*) nsk_aod_createOptions(optionsString);111if (!NSK_VERIFY(options != NULL))112return JNI_ERR;113114agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);115116jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);117if (jni == NULL)118return JNI_ERR;119120jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);121if (!NSK_VERIFY(jvmti != NULL))122return JNI_ERR;123124if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent01-eventsCounterMonitor", &eventsCounterMonitor))) {125return JNI_ERR;126}127128memset(&eventCallbacks,0, sizeof(eventCallbacks));129eventCallbacks.ClassPrepare = classPrepareHandler;130if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {131return JNI_ERR;132}133134if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_PREPARE))) {135return JNI_ERR;136}137138NSK_DISPLAY1("%s: initialization was done\n", agentName);139140if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))141return JNI_ERR;142143return JNI_OK;144}145146147}148149150