Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach021/attach021Agent00.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/*34* Expected agent work scenario:35* - during initialization agent registers native methods used be target application and enables ObjectFree events36* - target application using native method and agent's jvmti environment tags object and provokes collection37* of this object38* - agent receives ObjectFree event for tagged object39* - target application using native method calls nsk_aod_agentFinished and agent finishes work40* (agent can't call nsk_aod_agentFinished from ObjectFree handler, nsk_aod_agentFinished calls41* JNI functions and it is prohibited in ObjectFree handler)42*43*/4445#define TAG_VALUE (jlong)77746#define ATTACH021_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach021/attach021Target"4748static jvmtiEnv* jvmti;4950static Options* options = NULL;51static const char* agentName;5253// agent should set success status from objectFreeHandler54volatile int success = 0;5556JNIEXPORT jboolean JNICALL57Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_setTagFor(JNIEnv * jni,58jclass klass, jobject obj) {59if (!NSK_JVMTI_VERIFY(jvmti->SetTag(obj, TAG_VALUE))) {60return JNI_FALSE;61}6263NSK_DISPLAY2("%s: object is tagged (tag: %ld)\n", agentName, TAG_VALUE);6465return JNI_TRUE;66}6768JNIEXPORT void JNICALL69Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_shutdownAgent(JNIEnv * jni,70jclass klass) {7172/* Flush any pending ObjectFree events, which will set global success variable to 173for any pending ObjectFree events. */74if (jvmti->SetEventNotificationMode(JVMTI_DISABLE,75JVMTI_EVENT_OBJECT_FREE,76NULL) != JVMTI_ERROR_NONE) {77success = 0;78}7980nsk_aod_agentFinished(jni, agentName, success);81}8283void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {84NSK_DISPLAY2("%s: object free event for object %ld\n", agentName, tag);8586if (tag != TAG_VALUE) {87success = 0;88NSK_COMPLAIN2("%s: unexpected tag value, expected is %ld\n", agentName, TAG_VALUE);89} else {90success = 1;91}9293/*94* Can't use JNI functions from ObjectFree event handler, in this test target application calls95* function nsk_aod_agentFinished96*/97}9899void registerNativeMethods(JNIEnv* jni_env) {100ExceptionCheckingJniEnvPtr ec_jni(jni_env);101jclass appClass;102JNINativeMethod nativeMethods[] = {103{ (char*) "setTagFor", (char*) "(Ljava/lang/Object;)Z", (void*) Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_setTagFor },104{ (char*) "shutdownAgent", (char*) "()V", (void*) Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_shutdownAgent } };105jint nativeMethodsNumber = 2;106107appClass = ec_jni->FindClass(ATTACH021_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL);108ec_jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL);109}110111#ifdef STATIC_BUILD112JNIEXPORT jint JNI_OnLoad_attach021Agent00(JavaVM *jvm, char *options, void *reserved) {113return JNI_VERSION_1_8;114}115#endif116117JNIEXPORT jint JNICALL118#ifdef STATIC_BUILD119Agent_OnAttach_attach021Agent00(JavaVM *vm, char *optionsString, void *reserved)120#else121Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)122#endif123{124jvmtiEventCallbacks eventCallbacks;125jvmtiCapabilities caps;126JNIEnv* jni;127128options = (Options*) nsk_aod_createOptions(optionsString);129if (!NSK_VERIFY(options != NULL))130return JNI_ERR;131132agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);133134jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);135if (jni == NULL)136return JNI_ERR;137138jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);139if (!NSK_VERIFY(jvmti != NULL))140return JNI_ERR;141142registerNativeMethods(jni);143144memset(&caps, 0, sizeof(caps));145caps.can_tag_objects = 1;146caps.can_generate_object_free_events = 1;147if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {148return JNI_ERR;149}150151memset(&eventCallbacks,0, sizeof(eventCallbacks));152eventCallbacks.ObjectFree = objectFreeHandler;153if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {154return JNI_ERR;155}156157if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_OBJECT_FREE))) {158return JNI_ERR;159}160161NSK_DISPLAY1("%s: initialization was done\n", agentName);162163if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))164return JNI_ERR;165166return JNI_OK;167}168169}170171172