Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach045/attach045Agent03.cpp
40955 views
/*1* Copyright (c) 2008, 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* Agent receives expected number of VMObjectAlloc events and finishes work34* (events should be provoked by target application)35*/3637#define EXPECTED_EVENTS_NUMBER 5003839static Options* options = NULL;40static const char* agentName;4142static jvmtiEvent testEvents[] = { JVMTI_EVENT_VM_OBJECT_ALLOC };43static const int testEventsNumber = 1;4445static jrawMonitorID eventsCounterMonitor;4647static volatile int eventsCounter = 0;4849void JNICALL50VMObjectAllocHandler(jvmtiEnv *jvmti,51JNIEnv* jni,52jthread thread,53jobject object,54jclass object_klass,55jlong size) {56char threadName[MAX_STRING_LENGTH];57char className[MAX_STRING_LENGTH];58int success = 1;5960if (!nsk_jvmti_aod_getClassName(jvmti, object_klass, className)) {61nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);62return;63}6465if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {66nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);67return;68}6970if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {7172eventsCounter++;7374NSK_DISPLAY4("%s: VMObjectAlloc received in thread '%s' for instance of '%s' (eventsCounter: %d)\n",75agentName, threadName, className, eventsCounter);7677if ((eventsCounter % 10) == 0) {78NSK_DISPLAY1("%s: force garbage collection\n", agentName);7980if (!NSK_JVMTI_VERIFY(jvmti->ForceGarbageCollection()))81success = 0;82}8384if (eventsCounter == EXPECTED_EVENTS_NUMBER) {85NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);8687nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);88}8990if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {91success = 0;92}93} else {94success = 0;95}9697if (!success) {98nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);99}100}101102#ifdef STATIC_BUILD103JNIEXPORT jint JNI_OnLoad_attach045Agent03(JavaVM *jvm, char *options, void *reserved) {104return JNI_VERSION_1_8;105}106#endif107108JNIEXPORT jint JNICALL109#ifdef STATIC_BUILD110Agent_OnAttach_attach045Agent03(JavaVM *vm, char *optionsString, void *reserved)111#else112Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)113#endif114{115jvmtiEventCallbacks eventCallbacks;116jvmtiEnv* jvmti;117jvmtiCapabilities caps;118JNIEnv* jni;119120options = (Options*) nsk_aod_createOptions(optionsString);121if (!NSK_VERIFY(options != NULL))122return JNI_ERR;123124agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);125126jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);127if (jni == NULL)128return JNI_ERR;129130jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);131if (!NSK_VERIFY(jvmti != NULL))132return JNI_ERR;133134if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent03-eventsCounterMonitor", &eventsCounterMonitor))) {135return JNI_ERR;136}137138memset(&caps, 0, sizeof(caps));139caps.can_generate_vm_object_alloc_events = 1;140if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {141return JNI_ERR;142}143144memset(&eventCallbacks,0, sizeof(eventCallbacks));145eventCallbacks.VMObjectAlloc = VMObjectAllocHandler;146if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {147return JNI_ERR;148}149150if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {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}161162163}164165166