Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach002a/attach002aAgent00.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* - during initialization agent enables events VMObjectAlloc35* - agent receives VMObjectAlloc event for instance of InterruptedException,36* redefines class InterruptedException and finishes work37*/3839#define REDEFINED_CLASS_NAME "Ljava/lang/InterruptedException;"40#define REDEFINED_CLASS_FILE_NAME "java/lang/InterruptedException"4142static Options* options = NULL;43static const char* agentName;4445static jvmtiEvent testEvents[] = { JVMTI_EVENT_VM_OBJECT_ALLOC };4647static const int testEventsNumber = 1;4849void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,50JNIEnv * jni,51jthread thread,52jobject object,53jclass object_class,54jlong size) {55char className[MAX_STRING_LENGTH];5657if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {58nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);59return;60}6162NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);6364if (!strcmp(className, REDEFINED_CLASS_NAME)) {65int success = 1;6667if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, object_class, REDEFINED_CLASS_FILE_NAME))) {68NSK_COMPLAIN1("%s: failed to redefine class\n", agentName);69success = 0;70}7172nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);73}74}7576#ifdef STATIC_BUILD77JNIEXPORT jint JNI_OnLoad_attach002aAgent00(JavaVM *jvm, char *options, void *reserved) {78return JNI_VERSION_1_8;79}80#endif8182JNIEXPORT jint JNICALL83#ifdef STATIC_BUILD84Agent_OnAttach_attach002aAgent00(JavaVM *vm, char *optionsString, void *reserved)85#else86Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)87#endif88{89jvmtiEventCallbacks eventCallbacks;90jvmtiCapabilities caps;91jvmtiEnv* jvmti = NULL;92JNIEnv* jni = NULL;9394options = (Options*) nsk_aod_createOptions(optionsString);95if (!NSK_VERIFY(options != NULL))96return JNI_ERR;9798agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);99100jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);101if (jni == NULL)102return NSK_FALSE;103104jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);105if (!NSK_VERIFY(jvmti != NULL))106return JNI_ERR;107108memset(&caps, 0, sizeof(caps));109caps.can_redefine_classes = 1;110caps.can_generate_vm_object_alloc_events = 1;111if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {112return JNI_ERR;113}114115memset(&eventCallbacks,0, sizeof(eventCallbacks));116eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;117if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {118return JNI_ERR;119}120121if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {122return JNI_ERR;123}124125NSK_DISPLAY1("%s: initialization was done\n", agentName);126127if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))128return JNI_ERR;129130return JNI_OK;131}132133}134135136