Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach014/attach014Agent00.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#define LOADED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach014/ClassToLoad;"3334/*35* Expected agent work scenario:36* - during initialization agent enables ClassLoad events37* - target application loads class 'ClassToLoad'38* - agent receives ClassLoad event for this class, calls DisposeEnvironment and finishes work39*/4041static Options* options = NULL;42static const char* agentName;4344void JNICALL45classLoadHandler(jvmtiEnv *jvmti,46JNIEnv* jni,47jthread thread,48jclass klass) {49char className[MAX_STRING_LENGTH];5051if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {52nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);53return;54}5556NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className);5758if (!strcmp(className, LOADED_CLASS_NAME)) {59int success = 1;6061if (!nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))62success = 0;6364if (!NSK_JVMTI_VERIFY(jvmti->DisposeEnvironment())) {65success = 0;66NSK_COMPLAIN1("%s: failed to dispose environment\n", agentName);67} else {68NSK_DISPLAY1("%s: jvmti env was disposed\n", agentName);69}7071nsk_aod_agentFinished(jni, agentName, success);72}73}747576#ifdef STATIC_BUILD77JNIEXPORT jint JNI_OnLoad_attach014Agent00(JavaVM *jvm, char *options, void *reserved) {78return JNI_VERSION_1_8;79}80#endif8182JNIEXPORT jint JNICALL83#ifdef STATIC_BUILD84Agent_OnAttach_attach014Agent00(JavaVM *vm, char *optionsString, void *reserved)85#else86Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)87#endif88{89jvmtiEventCallbacks eventCallbacks;90jvmtiEnv* jvmti = NULL;91JNIEnv* jni = NULL;9293options = (Options*) nsk_aod_createOptions(optionsString);94if (!NSK_VERIFY(options != NULL))95return JNI_ERR;9697agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);9899jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);100if (jni == NULL)101return NSK_FALSE;102103jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);104if (!NSK_VERIFY(jvmti != NULL))105return JNI_ERR;106107memset(&eventCallbacks,0, sizeof(eventCallbacks));108eventCallbacks.ClassLoad = classLoadHandler;109if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {110return JNI_ERR;111}112113if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {114return JNI_ERR;115}116117NSK_DISPLAY1("%s: initialization was done\n", agentName);118119if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))120return JNI_ERR;121122return JNI_OK;123}124125}126127128