Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClearFieldModificationWatch/clrfmodw002/clrfmodw002.cpp
40955 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 FieldModification(jvmtiEnv *jvmti_env, JNIEnv *env,41jthread thd, jmethodID mid, jlocation loc,42jclass field_klass, jobject obj, jfieldID field,43char sig, jvalue new_value) {44}4546#ifdef STATIC_BUILD47JNIEXPORT jint JNICALL Agent_OnLoad_clrfmodw002(JavaVM *jvm, char *options, void *reserved) {48return Agent_Initialize(jvm, options, reserved);49}50JNIEXPORT jint JNICALL Agent_OnAttach_clrfmodw002(JavaVM *jvm, char *options, void *reserved) {51return Agent_Initialize(jvm, options, reserved);52}53JNIEXPORT jint JNI_OnLoad_clrfmodw002(JavaVM *jvm, char *options, void *reserved) {54return JNI_VERSION_1_8;55}56#endif57jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {58jint res;59jvmtiError err;6061res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);62if (res != JNI_OK || jvmti == NULL) {63printf("Wrong result of a valid call to GetEnv !\n");64return JNI_ERR;65}6667err = 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_modification_events) {89callbacks.FieldModification = &FieldModification;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_MODIFICATION, NULL);99if (err != JVMTI_ERROR_NONE) {100printf("Failed to enable JVMTI_EVENT_FIELD_MODIFICATION: %s (%d)\n",101TranslateError(err), err);102return JNI_ERR;103}104} else {105printf("Warning: FieldModification watch is not implemented\n");106}107108return JNI_OK;109}110111JNIEXPORT void JNICALL112Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw002_check(JNIEnv *env,113jclass cls) {114jvmtiError err;115jfieldID fid1, fid2;116117fid1 = env->GetStaticFieldID(cls, "fld1", "I");118fid2 = env->GetStaticFieldID(cls, "fld2", "I");119120if (!caps.can_generate_field_modification_events) {121printf("Warning: ClearFieldModificationWatch is not implemented\n");122err = jvmti->ClearFieldModificationWatch(cls, fid1);123if (err != JVMTI_ERROR_MUST_POSSESS_CAPABILITY) {124result = STATUS_FAILED;125printf("Failed to return MUST_POSSESS_CAPABILITY: %s (%d)\n",126TranslateError(err), err);127}128} else {129err = jvmti->ClearFieldModificationWatch(NULL, fid2);130if (err != JVMTI_ERROR_INVALID_CLASS) {131result = STATUS_FAILED;132printf("Failed to return JVMTI_ERROR_INVALID_CLASS: %s (%d)\n",133TranslateError(err), err);134}135136err = jvmti->ClearFieldModificationWatch(cls, NULL);137if (err != JVMTI_ERROR_INVALID_FIELDID) {138result = STATUS_FAILED;139printf("Failed to return INVALID_FIELDID: %s (%d)\n",140TranslateError(err), err);141}142143err = jvmti->ClearFieldModificationWatch(cls, fid2);144if (err != JVMTI_ERROR_NOT_FOUND) {145result = STATUS_FAILED;146printf("Failed to return NOT_FOUND: %s (%d)\n",147TranslateError(err), err);148}149}150}151152JNIEXPORT jint JNICALL153Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw002_getRes(JNIEnv *env,154jclass cls) {155return result;156}157158}159160161