annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/SetFieldAccessWatch/setfldw004/setfldw004.cpp @ 53610:9cfc8b0c45fd

8214149: Move out assignments when not using NSK*VERIFY macros Summary: Move out the assignments from ifs Reviewed-by: sspitsyn, cjplummer
author jcbeyler
date Wed, 21 Nov 2018 10:46:45 -0800
parents 1ebe04845112
children 7edc56620d42
rev   line source
iignatyev@50844 1 /*
iignatyev@50844 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
iignatyev@50844 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
iignatyev@50844 4 *
iignatyev@50844 5 * This code is free software; you can redistribute it and/or modify it
iignatyev@50844 6 * under the terms of the GNU General Public License version 2 only, as
iignatyev@50844 7 * published by the Free Software Foundation.
iignatyev@50844 8 *
iignatyev@50844 9 * This code is distributed in the hope that it will be useful, but WITHOUT
iignatyev@50844 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
iignatyev@50844 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
iignatyev@50844 12 * version 2 for more details (a copy is included in the LICENSE file that
iignatyev@50844 13 * accompanied this code).
iignatyev@50844 14 *
iignatyev@50844 15 * You should have received a copy of the GNU General Public License version
iignatyev@50844 16 * 2 along with this work; if not, write to the Free Software Foundation,
iignatyev@50844 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
iignatyev@50844 18 *
iignatyev@50844 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
iignatyev@50844 20 * or visit www.oracle.com if you need additional information or have any
iignatyev@50844 21 * questions.
iignatyev@50844 22 */
iignatyev@50844 23
iignatyev@50844 24 #include <stdio.h>
iignatyev@50844 25 #include <string.h>
iignatyev@52294 26 #include <inttypes.h>
iignatyev@50844 27 #include "jvmti.h"
iignatyev@50844 28 #include "agent_common.h"
iignatyev@50844 29 #include "JVMTITools.h"
iignatyev@50844 30
iignatyev@50844 31 extern "C" {
iignatyev@50844 32
iignatyev@50844 33
iignatyev@50844 34 #define PASSED 0
iignatyev@50844 35 #define STATUS_FAILED 2
iignatyev@50844 36
iignatyev@50844 37 static jvmtiEnv *jvmti;
iignatyev@50844 38 static jvmtiEventCallbacks callbacks;
iignatyev@50844 39 static jvmtiCapabilities caps;
iignatyev@50844 40 static jint result = PASSED;
iignatyev@50844 41 static jboolean printdump = JNI_FALSE;
iignatyev@50844 42 static jfieldID actual_fid = NULL;
iignatyev@50844 43 static jfieldID fids[4] = {NULL, NULL, NULL, NULL};
iignatyev@50844 44
iignatyev@50844 45 void JNICALL FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *env,
iignatyev@50844 46 jthread thr, jmethodID method,
iignatyev@50844 47 jlocation location, jclass field_klass, jobject obj, jfieldID field) {
iignatyev@50844 48 if (printdump == JNI_TRUE) {
iignatyev@50844 49 printf(">>> FieldAccess, field: 0x%p\n", field);
iignatyev@50844 50 }
iignatyev@50844 51 actual_fid = field;
iignatyev@50844 52 }
iignatyev@50844 53
iignatyev@50844 54 #ifdef STATIC_BUILD
iignatyev@50844 55 JNIEXPORT jint JNICALL Agent_OnLoad_setfldw004(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 56 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 57 }
iignatyev@50844 58 JNIEXPORT jint JNICALL Agent_OnAttach_setfldw004(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 59 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 60 }
iignatyev@50844 61 JNIEXPORT jint JNI_OnLoad_setfldw004(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 62 return JNI_VERSION_1_8;
iignatyev@50844 63 }
iignatyev@50844 64 #endif
iignatyev@50844 65 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 66 jint res;
iignatyev@50844 67 jvmtiError err;
iignatyev@50844 68
iignatyev@50844 69 if (options != NULL && strcmp(options, "printdump") == 0) {
iignatyev@50844 70 printdump = JNI_TRUE;
iignatyev@50844 71 }
iignatyev@50844 72
jcbeyler@52515 73 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
iignatyev@50844 74 if (res != JNI_OK || jvmti == NULL) {
iignatyev@50844 75 printf("Wrong result of a valid call to GetEnv !\n");
iignatyev@50844 76 return JNI_ERR;
iignatyev@50844 77 }
iignatyev@50844 78
jcbeyler@53610 79 err = jvmti->GetCapabilities(&caps);
jcbeyler@53610 80 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 81 printf("(GetCapabilities) unexpected error: %s (%d)\n",
iignatyev@50844 82 TranslateError(err), err);
iignatyev@50844 83 return JNI_ERR;
iignatyev@50844 84 }
iignatyev@50844 85
iignatyev@52294 86 err = jvmti->GetPotentialCapabilities(&caps);
iignatyev@50844 87 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 88 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
iignatyev@50844 89 TranslateError(err), err);
iignatyev@50844 90 return JNI_ERR;
iignatyev@50844 91 }
iignatyev@50844 92
iignatyev@52294 93 err = jvmti->AddCapabilities(&caps);
iignatyev@50844 94 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 95 printf("(AddCapabilities) unexpected error: %s (%d)\n",
iignatyev@50844 96 TranslateError(err), err);
iignatyev@50844 97 return JNI_ERR;
iignatyev@50844 98 }
iignatyev@50844 99
iignatyev@52294 100 err = jvmti->GetCapabilities(&caps);
iignatyev@50844 101 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 102 printf("(GetCapabilities) unexpected error: %s (%d)\n",
iignatyev@50844 103 TranslateError(err), err);
iignatyev@50844 104 return JNI_ERR;
iignatyev@50844 105 }
iignatyev@50844 106
iignatyev@50844 107 if (caps.can_generate_field_access_events) {
iignatyev@50844 108 callbacks.FieldAccess = &FieldAccess;
iignatyev@52294 109 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
iignatyev@50844 110 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 111 printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
iignatyev@50844 112 TranslateError(err), err);
iignatyev@50844 113 return JNI_ERR;
iignatyev@50844 114 }
iignatyev@50844 115 } else {
iignatyev@50844 116 printf("Warning: FieldAccess watch is not implemented\n");
iignatyev@50844 117 }
iignatyev@50844 118
iignatyev@50844 119 return JNI_OK;
iignatyev@50844 120 }
iignatyev@50844 121
iignatyev@50844 122 JNIEXPORT void JNICALL
iignatyev@50844 123 Java_nsk_jvmti_SetFieldAccessWatch_setfldw004_getReady(JNIEnv *env, jclass cls) {
iignatyev@50844 124 jvmtiError err;
iignatyev@50844 125 size_t i;
iignatyev@50844 126
iignatyev@50844 127 if (!caps.can_generate_field_access_events) {
iignatyev@50844 128 return;
iignatyev@50844 129 }
iignatyev@50844 130
jcbeyler@52515 131 fids[0] = env->GetStaticFieldID(cls, "fld0", "I");
jcbeyler@52515 132 fids[1] = env->GetStaticFieldID(cls, "fld1", "I");
jcbeyler@52515 133 fids[2] = env->GetFieldID(cls, "fld2", "I");
jcbeyler@52515 134 fids[3] = env->GetFieldID(cls, "fld3", "I");
iignatyev@50844 135 for (i = 0; i < sizeof(fids) / sizeof(jfieldID); i++) {
iignatyev@50844 136 if (fids[i] == NULL) {
iignatyev@50844 137 printf("Unable to set field access watch on fld%" PRIuPTR ", fieldID=0", i);
iignatyev@50844 138 } else {
iignatyev@50844 139 if (printdump == JNI_TRUE) {
iignatyev@50844 140 printf(">>> setting access watch on fld%" PRIuPTR, i);
iignatyev@50844 141 printf(", FieldID=0x%p\n", fids[i]);
iignatyev@50844 142 }
iignatyev@52294 143 err = jvmti->SetFieldAccessWatch(cls, fids[i]);
iignatyev@50844 144 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 145 printf("(SetFieldAccessWatch#%" PRIuPTR ") unexpected error: %s (%d)\n",
iignatyev@50844 146 i, TranslateError(err), err);
iignatyev@50844 147 result = STATUS_FAILED;
iignatyev@50844 148 }
iignatyev@50844 149 }
iignatyev@50844 150 }
iignatyev@52294 151 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
iignatyev@50844 152 JVMTI_EVENT_FIELD_ACCESS, NULL);
iignatyev@50844 153 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 154 printf("(SetEventNotificationMode) unexpected error: %s (%d)\n",
iignatyev@50844 155 TranslateError(err), err);
iignatyev@50844 156 result = STATUS_FAILED;
iignatyev@50844 157 }
iignatyev@50844 158 }
iignatyev@50844 159
iignatyev@50844 160 JNIEXPORT void JNICALL
iignatyev@50844 161 Java_nsk_jvmti_SetFieldAccessWatch_setfldw004_check(JNIEnv *env,
iignatyev@50844 162 jclass cls, jint ind) {
iignatyev@50844 163 if (printdump == JNI_TRUE) {
iignatyev@50844 164 printf(">>> checking on fld%d\n", ind);
iignatyev@50844 165 }
iignatyev@50844 166 if (actual_fid != fids[ind]) {
iignatyev@50844 167 result = STATUS_FAILED;
iignatyev@50844 168 printf("Field %d: thrown field ID expected=0x%p, actual=0x%p\n",
iignatyev@50844 169 ind, fids[ind], actual_fid);
iignatyev@50844 170 }
iignatyev@50844 171 actual_fid = NULL;
iignatyev@50844 172 }
iignatyev@50844 173
iignatyev@50844 174 JNIEXPORT jint JNICALL
iignatyev@50844 175 Java_nsk_jvmti_SetFieldAccessWatch_setfldw004_getRes(JNIEnv *env, jclass cls) {
iignatyev@50844 176 return result;
iignatyev@50844 177 }
iignatyev@50844 178
iignatyev@50844 179 }