Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach022/attach022Agent00.cpp
40951 views
/*1* Copyright (c) 2007, 2020, 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>29#include "ExceptionCheckingJniEnv.hpp"3031extern "C" {3233#define OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach022/ClassForAllocationEventsTest;"3435static jvmtiEnv* jvmti;36static Options* options = NULL;37static const char* agentName;3839static jvmtiEvent testEvents[] = { JVMTI_EVENT_OBJECT_FREE, JVMTI_EVENT_VM_OBJECT_ALLOC };40static const int testEventsNumber = 2;4142static volatile int taggedObjectsCounter = 0;43static volatile int freedObjectsCounter = 0;4445static jrawMonitorID objectTagMonitor;46static jrawMonitorID objectFreeMonitor;4748volatile int success = 1;4950volatile int agentFinished;5152void shutdownAgent(JNIEnv* jni) {53if (agentFinished)54return;5556if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))57success = 0;5859nsk_aod_agentFinished(jni, agentName, success);6061agentFinished = 1;62}6364JNIEXPORT jboolean JNICALL65Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent(JNIEnv * jni,66jclass klass, jint expectedTaggedObjectsCounter) {6768// Flush any pending ObjectFree events.69if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))70success = 0;7172if (taggedObjectsCounter != expectedTaggedObjectsCounter) {73success = 0;74NSK_COMPLAIN2("ERROR: unexpected taggedObjectsCounter: %d (expected value is %d)\n", taggedObjectsCounter, expectedTaggedObjectsCounter);75}7677if (taggedObjectsCounter != freedObjectsCounter) {78success = 0;79NSK_COMPLAIN2("ERROR: taggedObjectsCounter != freedObjectsCounter (taggedObjectsCounter: %d, freedObjectsCounter: %d)\n",80taggedObjectsCounter, freedObjectsCounter);81}8283shutdownAgent(jni);8485return success ? JNI_TRUE : JNI_FALSE;86}8788void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {89NSK_DISPLAY2("%s: ObjectFree event received (object tag: %ld)\n", agentName, tag);9091if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectFreeMonitor))) {92freedObjectsCounter++;9394if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectFreeMonitor))) {95success = 0;96}97} else {98success = 0;99}100}101102#define ATTACH022_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach022/attach022Target"103104void registerNativeMethods(JNIEnv* jni_env) {105ExceptionCheckingJniEnvPtr ec_jni(jni_env);106jclass appClass;107JNINativeMethod nativeMethods[] = {108{ (char*)"shutdownAgent", (char*)"(I)Z",109(void*) Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent } };110jint nativeMethodsNumber = 1;111112appClass = ec_jni->FindClass(ATTACH022_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL);113ec_jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL);114}115116void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,117JNIEnv * jni,118jthread thread,119jobject object,120jclass object_class,121jlong size) {122char className[MAX_STRING_LENGTH];123124if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {125success = 0;126shutdownAgent(jni);127return;128}129130NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);131132if (!strcmp(className, OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME)) {133if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectTagMonitor))) {134jlong tagValue = taggedObjectsCounter + 1;135136if (!NSK_JVMTI_VERIFY(jvmti->SetTag(object, tagValue))) {137NSK_COMPLAIN1("%s: failed to set tag\n", agentName);138success = 0;139} else {140NSK_DISPLAY2("%s: object was tagged (tag value: %ld)\n", agentName, tagValue);141taggedObjectsCounter++;142}143144if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectTagMonitor))) {145success = 0;146}147} else {148success = 0;149}150}151152if (!success) {153NSK_COMPLAIN1("%s: error happened during agent work, stop agent\n", agentName);154shutdownAgent(jni);155}156}157158#ifdef STATIC_BUILD159JNIEXPORT jint JNI_OnLoad_attach022Agent00(JavaVM *jvm, char *options, void *reserved) {160return JNI_VERSION_1_8;161}162#endif163164JNIEXPORT jint JNICALL165#ifdef STATIC_BUILD166Agent_OnAttach_attach022Agent00(JavaVM *vm, char *optionsString, void *reserved)167#else168Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)169#endif170{171jvmtiEventCallbacks eventCallbacks;172jvmtiCapabilities caps;173JNIEnv* jni;174175options = (Options*) nsk_aod_createOptions(optionsString);176if (!NSK_VERIFY(options != NULL))177return JNI_ERR;178179agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);180181jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);182if (jni == NULL)183return JNI_ERR;184185jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);186if (!NSK_VERIFY(jvmti != NULL))187return JNI_ERR;188189registerNativeMethods(jni);190191if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectTagMonitor", &objectTagMonitor))) {192return JNI_ERR;193}194195if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectFreeMonitor", &objectFreeMonitor))) {196return JNI_ERR;197}198199memset(&caps, 0, sizeof(caps));200caps.can_tag_objects = 1;201caps.can_generate_object_free_events = 1;202caps.can_generate_vm_object_alloc_events = 1;203if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {204return JNI_ERR;205}206207memset(&eventCallbacks,0, sizeof(eventCallbacks));208eventCallbacks.ObjectFree = objectFreeHandler;209eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;210if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {211return JNI_ERR;212}213214if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {215return JNI_ERR;216}217218NSK_DISPLAY1("%s: initialization was done\n", agentName);219220if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))221return JNI_ERR;222223return JNI_OK;224}225226}227228229