Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClearFieldAccessWatch/clrfldw002/clrfldw002.cpp
40951 views
/*1* Copyright (c) 2003, 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*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"26#include "agent_common.h"27#include "JVMTITools.h"2829extern "C" {303132#define PASSED 033#define STATUS_FAILED 23435static jvmtiEnv *jvmti;36static jvmtiEventCallbacks callbacks;37static jvmtiCapabilities caps;38static jint result = PASSED;3940void JNICALL FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *env,41jthread thd, jmethodID mid, jlocation loc,42jclass field_klass, jobject obj, jfieldID field) {43}4445#ifdef STATIC_BUILD46JNIEXPORT jint JNICALL Agent_OnLoad_clrfldw002(JavaVM *jvm, char *options, void *reserved) {47return Agent_Initialize(jvm, options, reserved);48}49JNIEXPORT jint JNICALL Agent_OnAttach_clrfldw002(JavaVM *jvm, char *options, void *reserved) {50return Agent_Initialize(jvm, options, reserved);51}52JNIEXPORT jint JNI_OnLoad_clrfldw002(JavaVM *jvm, char *options, void *reserved) {53return JNI_VERSION_1_8;54}55#endif56jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {57jint res;58jvmtiError err;5960res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);61if (res != JNI_OK || jvmti == NULL) {62printf("Wrong result of a valid call to GetEnv !\n");63return JNI_ERR;64}656667err = jvmti->GetPotentialCapabilities(&caps);68if (err != JVMTI_ERROR_NONE) {69printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",70TranslateError(err), err);71return JNI_ERR;72}7374err = jvmti->AddCapabilities(&caps);75if (err != JVMTI_ERROR_NONE) {76printf("(AddCapabilities) unexpected error: %s (%d)\n",77TranslateError(err), err);78return JNI_ERR;79}8081err = jvmti->GetCapabilities(&caps);82if (err != JVMTI_ERROR_NONE) {83printf("(GetCapabilities) unexpected error: %s (%d)\n",84TranslateError(err), err);85return JNI_ERR;86}8788if (caps.can_generate_field_access_events) {89callbacks.FieldAccess = &FieldAccess;90err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));91if (err != JVMTI_ERROR_NONE) {92printf("(SetEventCallbacks) unexpected error: %s (%d)\n",93TranslateError(err), err);94return JNI_ERR;95}9697err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,98JVMTI_EVENT_FIELD_ACCESS, NULL);99if (err != JVMTI_ERROR_NONE) {100printf("Failed to enable JVMTI_EVENT_FIELD_ACCESS: %s (%d)\n",101TranslateError(err), err);102return JNI_ERR;103}104} else {105printf("Warning: FieldAccess watch is not implemented\n");106}107108return JNI_OK;109}110111JNIEXPORT void JNICALL112Java_nsk_jvmti_ClearFieldAccessWatch_clrfldw002_check(JNIEnv *env, jclass cls) {113jvmtiError err;114jfieldID fid1, fid2;115116fid1 = env->GetStaticFieldID(cls, "fld1", "I");117fid2 = env->GetStaticFieldID(cls, "fld2", "I");118119if (!caps.can_generate_field_access_events) {120err = jvmti->ClearFieldAccessWatch(cls, fid1);121if (err != JVMTI_ERROR_MUST_POSSESS_CAPABILITY) {122result = STATUS_FAILED;123printf("Failed to return MUST_POSSESS_CAPABILITY: %s (%d)\n",124TranslateError(err), err);125}126} else {127err = jvmti->ClearFieldAccessWatch(NULL, fid2);128if (err != JVMTI_ERROR_INVALID_CLASS) {129result = STATUS_FAILED;130printf("Failed to return JVMTI_ERROR_INVALID_CLASS: %s (%d)\n",131TranslateError(err), err);132}133134err = jvmti->ClearFieldAccessWatch(cls, NULL);135if (err != JVMTI_ERROR_INVALID_FIELDID) {136result = STATUS_FAILED;137printf("Failed to return INVALID_FIELDID: %s (%d)\n",138TranslateError(err), err);139}140141err = jvmti->ClearFieldAccessWatch(cls, fid2);142if (err != JVMTI_ERROR_NOT_FOUND) {143result = STATUS_FAILED;144printf("Failed to return NOT_FOUND: %s (%d)\n",145TranslateError(err), err);146}147}148}149150JNIEXPORT jint JNICALL151Java_nsk_jvmti_ClearFieldAccessWatch_clrfldw002_getRes(JNIEnv *env, jclass cls) {152return result;153}154155}156157158