Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach009/attach009Agent00.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 ClassLoad event for class FirstLoadedClass, from handler for this event35* disable ClassLoad events for all threads except thread loading FirstLoadedClass36* (after these ClassLoad events should come only from this thread)37* - receive ClassLoad event for class LastLoadedClass and finish work38*/3940static Options* options = NULL;41static const char* agentName;4243#define FIRST_LOADED_CLASS "Lnsk/jvmti/AttachOnDemand/attach009/FirstLoadedClass;"44#define LAST_LOADED_CLASS "Lnsk/jvmti/AttachOnDemand/attach009/LastLoadedClass;"4546static int disabledForOthers = 0;4748static volatile int success = 1;4950void JNICALL51classLoadHandler(jvmtiEnv *jvmti,52JNIEnv* jni,53jthread thread,54jclass klass) {55static char mainThreadName[MAX_STRING_LENGTH];56char loadedClassName[MAX_STRING_LENGTH];57char threadName[MAX_STRING_LENGTH];5859if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {60nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);61return;62}6364if (!nsk_jvmti_aod_getClassName(jvmti, klass, loadedClassName)) {65nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);66return;67}6869NSK_DISPLAY2("Class '%s' was loaded by thread '%s'\n", loadedClassName, threadName);7071/*72* When class FIRST_LOADED_CLASS was loaded try to disable events for all threads73* except main target application thread74*/75if (strcmp(loadedClassName, FIRST_LOADED_CLASS) == 0) {76strcpy(mainThreadName, threadName);7778if (!nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD)) {79NSK_COMPLAIN0("Failed to disable events\n");80nsk_aod_agentFinished(jni, agentName, 0);81return;82}8384if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, thread))) {85NSK_COMPLAIN1("Failed to enable events for thread '%s'\n", mainThreadName);86nsk_aod_agentFinished(jni, agentName, 0);87return;88}8990NSK_DISPLAY1("ClassLoad events are enabled only for thread '%s'", mainThreadName);9192disabledForOthers = 1;9394return;95}9697if (disabledForOthers) {98if (strcmp(threadName, mainThreadName) != 0) {99success = 0;100NSK_COMPLAIN1("ClassLoad event was erroneously generated for thread '%s'\n", threadName);101}102}103104/*105* Stop agent when LAST_LOADED_CLASS was loaded106*/107if (strcmp(loadedClassName, LAST_LOADED_CLASS) == 0) {108nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, success, jvmti, jni);109}110}111112113#ifdef STATIC_BUILD114JNIEXPORT jint JNI_OnLoad_attach009Agent00(JavaVM *jvm, char *options, void *reserved) {115return JNI_VERSION_1_8;116}117#endif118119JNIEXPORT jint JNICALL120#ifdef STATIC_BUILD121Agent_OnAttach_attach009Agent00(JavaVM *vm, char *optionsString, void *reserved)122#else123Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)124#endif125{126jvmtiEnv* jvmti;127jvmtiEventCallbacks eventCallbacks;128JNIEnv* jni;129130options = (Options*) nsk_aod_createOptions(optionsString);131if (!NSK_VERIFY(options != NULL))132return JNI_ERR;133134agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);135136jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);137if (jni == NULL)138return NSK_FALSE;139140jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);141if (!NSK_VERIFY(jvmti != NULL))142return JNI_ERR;143144memset(&eventCallbacks,0, sizeof(eventCallbacks));145eventCallbacks.ClassLoad = classLoadHandler;146if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {147return JNI_ERR;148}149150if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {151return JNI_ERR;152}153154NSK_DISPLAY1("%s: initialization was done\n", agentName);155156if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))157return JNI_ERR;158159return JNI_OK;160}161162}163164165