Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach046/attach046Agent00.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* In this test the same agent library is attached 3 times, but with34* different options. In such scenario functions nsk_jvmti_aod_addMultiagentsOptions and35* nsk_jvmti_aod_getMultiagentsOptions are used.36*37* Each agent from ClassLoad event handler tries to redefine class ClassToRedefine and38* finishes work.39*/4041#define REDEFINED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach046/ClassToRedefine;"42#define REDEFINED_CLASS_FILE_NAME "nsk/jvmti/AttachOnDemand/attach046/ClassToRedefine"4344void JNICALL classLoadHandler(45jvmtiEnv *jvmti,46JNIEnv* jni,47jthread thread,48jclass klass) {49char className[MAX_STRING_LENGTH];50const char* agentName;51Options* options;5253options = nsk_jvmti_aod_getMultiagentsOptions(jvmti);54if (!NSK_VERIFY(options != NULL)) {55NSK_COMPLAIN0("Failed to get agents's options\n");56nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD);57// can't call nsk_aod_agentFinished because of without options can't get agent's name58return;59}6061agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);6263if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {64nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);65return;66}6768NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className);6970if (!strcmp(className, REDEFINED_CLASS_NAME)) {7172NSK_DISPLAY1("%s: redefining class\n", agentName);7374if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, klass, REDEFINED_CLASS_FILE_NAME))) {75NSK_COMPLAIN1("%s: failed to redefine class\n", agentName);76nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);77} else {78nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 1, jvmti, jni);79}80}81}8283#ifdef STATIC_BUILD84JNIEXPORT jint JNI_OnLoad_attach046Agent00(JavaVM *jvm, char *options, void *reserved) {85return JNI_VERSION_1_8;86}87#endif8889JNIEXPORT jint JNICALL90#ifdef STATIC_BUILD91Agent_OnAttach_attach046Agent00(JavaVM *vm, char *optionsString, void *reserved)92#else93Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)94#endif95{96jvmtiEventCallbacks eventCallbacks;97jvmtiCapabilities caps;98jvmtiEnv* jvmti = NULL;99JNIEnv* jni = NULL;100Options* options;101const char* agentName;102103options = (Options*) nsk_aod_createOptions(optionsString);104if (!NSK_VERIFY(options != NULL))105return JNI_ERR;106107agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);108109jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);110if (jni == NULL)111return NSK_FALSE;112113jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);114if (!NSK_VERIFY(jvmti != NULL))115return JNI_ERR;116117memset(&caps, 0, sizeof(caps));118caps.can_redefine_classes = 1;119if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {120return JNI_ERR;121}122123memset(&eventCallbacks,0, sizeof(eventCallbacks));124eventCallbacks.ClassLoad = classLoadHandler;125if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {126return JNI_ERR;127}128129if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {130return JNI_ERR;131}132133if (!NSK_VERIFY(nsk_jvmti_aod_addMultiagentsOptions(jvmti, options))) {134return JNI_ERR;135}136137NSK_DISPLAY1("%s: initialization was done\n", agentName);138139if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))140return JNI_ERR;141142return JNI_OK;143}144145}146147148