Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach015/attach015Agent01.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 ClassLoad event35* - agent waits events for 2 classes loaded by target application and36* finishes work37*/3839#define CLASS_NAME1 "Lnsk/jvmti/AttachOnDemand/attach015/ClassToLoad1;"40#define CLASS_NAME2 "Lnsk/jvmti/AttachOnDemand/attach015/ClassToLoad2;"4142static Options* options = NULL;43static const char* agentName;4445static int receivedEventsCount = 0;4647void JNICALL classLoadHandler(jvmtiEnv *jvmti,48JNIEnv* jni,49jthread thread,50jclass klass) {51char className[MAX_STRING_LENGTH];5253if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {54nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);55return;56}5758NSK_DISPLAY2("%s: class load event for class '%s'\n", agentName, className);5960if (!strcmp(className, CLASS_NAME1) || !strcmp(className, CLASS_NAME2)) {61receivedEventsCount++;6263if (receivedEventsCount == 2) {64nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 1, jvmti, jni);65}66}67}6869#ifdef STATIC_BUILD70JNIEXPORT jint JNI_OnLoad_attach015Agent01(JavaVM *jvm, char *options, void *reserved) {71return JNI_VERSION_1_8;72}73#endif7475JNIEXPORT jint JNICALL76#ifdef STATIC_BUILD77Agent_OnAttach_attach015Agent01(JavaVM *vm, char *optionsString, void *reserved)78#else79Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)80#endif81{82jvmtiEventCallbacks eventCallbacks;83jvmtiEnv* jvmti = NULL;84JNIEnv* jni = NULL;8586options = (Options*) nsk_aod_createOptions(optionsString);87if (!NSK_VERIFY(options != NULL))88return JNI_ERR;8990agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);9192jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);93if (jni == NULL)94return JNI_ERR;9596jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);97if (!NSK_VERIFY(jvmti != NULL))98return JNI_ERR;99100memset(&eventCallbacks,0, sizeof(eventCallbacks));101eventCallbacks.ClassLoad = classLoadHandler;102if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {103return JNI_ERR;104}105106if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {107return JNI_ERR;108}109110NSK_DISPLAY1("%s: initialization was done\n", agentName);111112if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))113return JNI_ERR;114115return JNI_OK;116}117}118119120