Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/FieldAccess/fieldacc004/fieldacc004.cpp
40951 views
/*1* Copyright (c) 2003, 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*/2223#include <stdio.h>24#include <string.h>25#include <inttypes.h>26#include "jvmti.h"27#include "agent_common.h"28#include "JVMTITools.h"2930extern "C" {313233#define PASSED 034#define STATUS_FAILED 23536typedef struct {37jfieldID fid;38char *m_cls;39char *m_name;40char *m_sig;41jlocation loc;42char *f_cls;43char *f_name;44char *f_sig;45jboolean is_static;46} writable_watch_info;4748typedef struct {49jfieldID fid;50const char *m_cls;51const char *m_name;52const char *m_sig;53jlocation loc;54const char *f_cls;55const char *f_name;56const char *f_sig;57jboolean is_static;58} watch_info;5960static jvmtiEnv *jvmti;61static jvmtiEventCallbacks callbacks;62static jvmtiCapabilities caps;63static jint result = PASSED;64static jboolean printdump = JNI_FALSE;65static int eventsExpected = 0;66static int eventsCount = 0;67static watch_info watches[] = {68{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc004a;", "run", "()I", 2,69"Lnsk/jvmti/FieldAccess/fieldacc004a;", "interfaceObject",70"Ljava/lang/Object;", JNI_TRUE },71{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc004a;", "run", "()I", 12,72"Lnsk/jvmti/FieldAccess/fieldacc004a;", "interfaceArrInt",73"[I", JNI_TRUE }74};7576void JNICALL FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *env,77jthread thr, jmethodID method,78jlocation location, jclass field_klass, jobject obj, jfieldID field) {79jvmtiError err;80jclass cls;81writable_watch_info watch;82char *generic;83size_t i;8485eventsCount++;86if (printdump == JNI_TRUE) {87printf(">>> retrieving access watch info ...\n");88}89watch.fid = field;90watch.loc = location;91watch.is_static = (obj == NULL) ? JNI_TRUE : JNI_FALSE;92err = jvmti_env->GetMethodDeclaringClass(method, &cls);93if (err != JVMTI_ERROR_NONE) {94printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",95TranslateError(err), err);96result = STATUS_FAILED;97return;98}99err = jvmti_env->GetClassSignature(cls,100&watch.m_cls, &generic);101if (err != JVMTI_ERROR_NONE) {102printf("(GetClassSignature) unexpected error: %s (%d)\n",103TranslateError(err), err);104result = STATUS_FAILED;105return;106}107err = jvmti_env->GetMethodName(method,108&watch.m_name, &watch.m_sig, &generic);109if (err != JVMTI_ERROR_NONE) {110printf("(GetMethodName) unexpected error: %s (%d)\n",111TranslateError(err), err);112result = STATUS_FAILED;113return;114}115err = jvmti_env->GetClassSignature(field_klass,116&watch.f_cls, &generic);117if (err != JVMTI_ERROR_NONE) {118printf("(GetClassSignature) unexpected error: %s (%d)\n",119TranslateError(err), err);120result = STATUS_FAILED;121return;122}123err = jvmti_env->GetFieldName(field_klass, field,124&watch.f_name, &watch.f_sig, &generic);125if (err != JVMTI_ERROR_NONE) {126printf("(GetFieldName) unexpected error: %s (%d)\n",127TranslateError(err), err);128result = STATUS_FAILED;129return;130}131if (printdump == JNI_TRUE) {132printf(">>> class: \"%s\"\n", watch.m_cls);133printf(">>> method: \"%s%s\"\n", watch.m_name, watch.m_sig);134printf(">>> location: 0x%x%08x\n",135(jint)(watch.loc >> 32), (jint)watch.loc);136printf(">>> field cls: \"%s\"\n", watch.f_cls);137printf(">>> field: \"%s:%s\"\n", watch.f_name, watch.f_sig);138printf(">>> object: 0x%p\n", obj);139printf(">>> ... done\n");140}141for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {142if (watch.fid == watches[i].fid) {143if (watch.m_cls == NULL ||144strcmp(watch.m_cls, watches[i].m_cls) != 0) {145printf("(watch#%" PRIuPTR ") wrong class: \"%s\", expected: \"%s\"\n",146i, watch.m_cls, watches[i].m_cls);147result = STATUS_FAILED;148}149if (watch.m_name == NULL ||150strcmp(watch.m_name, watches[i].m_name) != 0) {151printf("(watch#%" PRIuPTR ") wrong method name: \"%s\"",152i, watch.m_name);153printf(", expected: \"%s\"\n", watches[i].m_name);154result = STATUS_FAILED;155}156if (watch.m_sig == NULL ||157strcmp(watch.m_sig, watches[i].m_sig) != 0) {158printf("(watch#%" PRIuPTR ") wrong method sig: \"%s\"",159i, watch.m_sig);160printf(", expected: \"%s\"\n", watches[i].m_sig);161result = STATUS_FAILED;162}163if (watch.loc != watches[i].loc) {164printf("(watch#%" PRIuPTR ") wrong location: 0x%x%08x",165i, (jint)(watch.loc >> 32), (jint)watch.loc);166printf(", expected: 0x%x%08x\n",167(jint)(watches[i].loc >> 32), (jint)watches[i].loc);168result = STATUS_FAILED;169}170if (watch.f_name == NULL ||171strcmp(watch.f_name, watches[i].f_name) != 0) {172printf("(watch#%" PRIuPTR ") wrong field name: \"%s\"",173i, watch.f_name);174printf(", expected: \"%s\"\n", watches[i].f_name);175result = STATUS_FAILED;176}177if (watch.f_sig == NULL ||178strcmp(watch.f_sig, watches[i].f_sig) != 0) {179printf("(watch#%" PRIuPTR ") wrong field sig: \"%s\"",180i, watch.f_sig);181printf(", expected: \"%s\"\n", watches[i].f_sig);182result = STATUS_FAILED;183}184if (watch.is_static != watches[i].is_static) {185printf("(watch#%" PRIuPTR ") wrong field type: %s", i,186(watch.is_static == JNI_TRUE) ? "static" : "instance");187printf(", expected: %s\n",188(watches[i].is_static == JNI_TRUE) ? "static" : "instance");189result = STATUS_FAILED;190}191return;192}193}194printf("Unexpected field access catched: 0x%p\n", watch.fid);195result = STATUS_FAILED;196}197198#ifdef STATIC_BUILD199JNIEXPORT jint JNICALL Agent_OnLoad_fieldacc004(JavaVM *jvm, char *options, void *reserved) {200return Agent_Initialize(jvm, options, reserved);201}202JNIEXPORT jint JNICALL Agent_OnAttach_fieldacc004(JavaVM *jvm, char *options, void *reserved) {203return Agent_Initialize(jvm, options, reserved);204}205JNIEXPORT jint JNI_OnLoad_fieldacc004(JavaVM *jvm, char *options, void *reserved) {206return JNI_VERSION_1_8;207}208#endif209jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {210jvmtiError err;211jint res;212213if (options != NULL && strcmp(options, "printdump") == 0) {214printdump = JNI_TRUE;215}216217res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);218if (res != JNI_OK || jvmti == NULL) {219printf("Wrong result of a valid call to GetEnv!\n");220return JNI_ERR;221}222223err = jvmti->GetPotentialCapabilities(&caps);224if (err != JVMTI_ERROR_NONE) {225printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",226TranslateError(err), err);227return JNI_ERR;228}229230err = jvmti->AddCapabilities(&caps);231if (err != JVMTI_ERROR_NONE) {232printf("(AddCapabilities) unexpected error: %s (%d)\n",233TranslateError(err), err);234return JNI_ERR;235}236237err = jvmti->GetCapabilities(&caps);238if (err != JVMTI_ERROR_NONE) {239printf("(GetCapabilities) unexpected error: %s (%d)\n",240TranslateError(err), err);241return JNI_ERR;242}243244if (caps.can_generate_field_access_events) {245callbacks.FieldAccess = &FieldAccess;246err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));247if (err != JVMTI_ERROR_NONE) {248printf("(SetEventCallbacks) unexpected error: %s (%d)\n",249TranslateError(err), err);250return JNI_ERR;251}252253err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,254JVMTI_EVENT_FIELD_ACCESS, NULL);255if (err != JVMTI_ERROR_NONE) {256printf("Failed to enable JVMTI_EVENT_FIELD_ACCESS: %s (%d)\n",257TranslateError(err), err);258return JNI_ERR;259}260} else {261printf("Warning: FieldAccess watch is not implemented\n");262}263264return JNI_OK;265}266267JNIEXPORT void JNICALL268Java_nsk_jvmti_FieldAccess_fieldacc004_getReady(JNIEnv *env, jclass klass) {269jvmtiError err;270jclass cls;271size_t i;272273if (!caps.can_generate_field_access_events) {274return;275}276277if (printdump == JNI_TRUE) {278printf(">>> setting field access watches ...\n");279}280for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {281cls = env->FindClass(watches[i].f_cls);282if (cls == NULL) {283printf("Cannot find %s class!\n", watches[i].f_cls);284result = STATUS_FAILED;285return;286}287if (watches[i].is_static == JNI_TRUE) {288watches[i].fid = env->GetStaticFieldID(289cls, watches[i].f_name, watches[i].f_sig);290} else {291watches[i].fid = env->GetFieldID(292cls, watches[i].f_name, watches[i].f_sig);293}294if (watches[i].fid == NULL) {295printf("Cannot get field ID for \"%s:%s\"\n",296watches[i].f_name, watches[i].f_sig);297result = STATUS_FAILED;298return;299}300err = jvmti->SetFieldAccessWatch(cls, watches[i].fid);301if (err == JVMTI_ERROR_NONE) {302eventsExpected++;303} else {304printf("(SetFieldAccessWatch#%" PRIuPTR ") unexpected error: %s (%d)\n",305i, TranslateError(err), err);306result = STATUS_FAILED;307}308}309if (printdump == JNI_TRUE) {310printf(">>> ... done\n");311}312}313314JNIEXPORT jint JNICALL315Java_nsk_jvmti_FieldAccess_fieldacc004_check(JNIEnv *env, jclass cls) {316if (eventsCount != eventsExpected) {317printf("Wrong number of field access events: %d, expected: %d\n",318eventsCount, eventsExpected);319result = STATUS_FAILED;320}321return result;322}323324}325326327