annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/unit/ForceEarlyReturn/earlyretbase/earlyretbase.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 f6005102c6ef
rev   line source
iignatyev@50844 1 /*
iignatyev@50844 2 * Copyright (c) 2007, 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@50844 26 #include "jvmti.h"
iignatyev@50844 27 #include "agent_common.h"
iignatyev@50844 28 #include "JVMTITools.h"
iignatyev@50844 29
iignatyev@50844 30 extern "C" {
iignatyev@50844 31
iignatyev@50844 32
iignatyev@50844 33 #define STATUS_FAILED 2
iignatyev@50844 34 #define PASSED 0
iignatyev@50844 35
iignatyev@50844 36 #define RETURN_FAILED errCode = STATUS_FAILED; fflush(0); return errCode
iignatyev@50844 37
iignatyev@50844 38 static jvmtiEnv *jvmti = NULL;
iignatyev@50844 39 static jvmtiCapabilities caps;
iignatyev@50844 40 static jvmtiEventCallbacks callbacks;
iignatyev@50844 41
iignatyev@50844 42 /* MethodExit and PopFrame events expected */
iignatyev@50844 43 static int meth_exit_exp_events = 0;
iignatyev@50844 44 static int pop_frame_exp_events = 0;
iignatyev@50844 45
iignatyev@50844 46 /* MethodExit and PopFrame events generated */
iignatyev@50844 47 static int meth_exit_gen_events = 0;
iignatyev@50844 48 static int pop_frame_gen_events = 0;
iignatyev@50844 49
iignatyev@50844 50 static int errCode = PASSED;
iignatyev@50844 51
iignatyev@50844 52 static const char *sig_exp = "()J";
iignatyev@50844 53 static const char *name_exp = "activeMethod";
iignatyev@50844 54 static jmethodID midActiveMethod = NULL;
iignatyev@50844 55
iignatyev@50844 56 void JNICALL
iignatyev@50844 57 MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr,
iignatyev@50844 58 jmethodID method, jboolean was_poped_by_exc, jvalue return_value) {
iignatyev@50844 59
iignatyev@50844 60 if (method == midActiveMethod) {
iignatyev@50844 61 printf("#### MethodExit event occurred ####\n");
iignatyev@50844 62 fflush(0);
iignatyev@50844 63 meth_exit_gen_events++;
iignatyev@50844 64 }
iignatyev@50844 65 }
iignatyev@50844 66
iignatyev@50844 67 void JNICALL
iignatyev@50844 68 FramePop(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread,
iignatyev@50844 69 jmethodID method, jboolean wasPopedByException) {
iignatyev@50844 70
iignatyev@50844 71 if (method == midActiveMethod) {
iignatyev@50844 72 printf("#### FramePop event occurred ####\n");
iignatyev@50844 73 fflush(0);
iignatyev@50844 74 pop_frame_gen_events++;
iignatyev@50844 75 }
iignatyev@50844 76 }
iignatyev@50844 77
iignatyev@50844 78 JNIEXPORT jint JNICALL
iignatyev@50844 79 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretbase_suspThread(JNIEnv *env,
iignatyev@50844 80 jclass cls, jobject earlyretThr) {
iignatyev@50844 81 jvmtiError err;
iignatyev@50844 82
iignatyev@50844 83 if (!caps.can_force_early_return || !caps.can_suspend) {
iignatyev@50844 84 return PASSED;
iignatyev@50844 85 }
iignatyev@50844 86
iignatyev@50844 87 printf(">>>>>>>> Invoke SuspendThread()\n");
jcbeyler@53610 88 err = jvmti->SuspendThread(earlyretThr);
jcbeyler@53610 89 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 90 printf("%s: Failed to call SuspendThread(): error=%d: %s\n",
iignatyev@50844 91 __FILE__, err, TranslateError(err));
iignatyev@50844 92 return JNI_ERR;
iignatyev@50844 93 }
iignatyev@50844 94 printf("<<<<<<<< SuspendThread() is successfully done\n");
iignatyev@50844 95 fflush(0);
iignatyev@50844 96 return PASSED;
iignatyev@50844 97 }
iignatyev@50844 98
iignatyev@50844 99 JNIEXPORT jint JNICALL
iignatyev@50844 100 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretbase_resThread(JNIEnv *env,
iignatyev@50844 101 jclass cls, jobject earlyretThr) {
iignatyev@50844 102 jvmtiError err;
iignatyev@50844 103
iignatyev@50844 104 if (!caps.can_force_early_return || !caps.can_suspend) {
iignatyev@50844 105 return PASSED;
iignatyev@50844 106 }
iignatyev@50844 107
iignatyev@50844 108 printf(">>>>>>>> Invoke ResumeThread()\n");
jcbeyler@53610 109 err = jvmti->ResumeThread(earlyretThr);
jcbeyler@53610 110 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 111 printf("%s: Failed to call ResumeThread(): error=%d: %s\n",
iignatyev@50844 112 __FILE__, err, TranslateError(err));
iignatyev@50844 113 return JNI_ERR;
iignatyev@50844 114 }
iignatyev@50844 115 printf("<<<<<<<< ResumeThread() is successfully done\n");
iignatyev@50844 116 fflush(0);
iignatyev@50844 117 return PASSED;
iignatyev@50844 118 }
iignatyev@50844 119
iignatyev@50844 120 JNIEXPORT jint JNICALL
iignatyev@50844 121 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretbase_doForceEarlyReturn(JNIEnv *env,
iignatyev@50844 122 jclass cls, jclass targCls, jthread earlyretThr, jlong valToRet) {
iignatyev@50844 123 jvmtiError err;
iignatyev@50844 124
iignatyev@50844 125 if (!caps.can_force_early_return || !caps.can_suspend) {
iignatyev@50844 126 return PASSED;
iignatyev@50844 127 }
iignatyev@50844 128
iignatyev@50844 129 /* Turn on the JVMTI MetodExit and PopFrame events to check
iignatyev@50844 130 * that ForceEarlyReturn correctly generates them */
iignatyev@50844 131
iignatyev@52294 132 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
iignatyev@50844 133 JVMTI_EVENT_METHOD_EXIT, NULL);
iignatyev@50844 134 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 135 printf("Failed to enable METHOD_EXIT event: %s (%d)\n",
iignatyev@50844 136 TranslateError(err), err);
iignatyev@50844 137 RETURN_FAILED;
iignatyev@50844 138 } else {
iignatyev@50844 139 meth_exit_exp_events++;
iignatyev@50844 140 }
iignatyev@50844 141
iignatyev@52294 142 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
iignatyev@50844 143 JVMTI_EVENT_FRAME_POP, NULL);
iignatyev@50844 144 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 145 printf("Failed to enable FRAME_POP event: %s (%d)\n",
iignatyev@50844 146 TranslateError(err), err);
iignatyev@50844 147 RETURN_FAILED;
iignatyev@50844 148 }
iignatyev@50844 149
iignatyev@52294 150 err = jvmti->NotifyFramePop(earlyretThr, 0);
iignatyev@50844 151 if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY &&
iignatyev@50844 152 !caps.can_generate_frame_pop_events) {
iignatyev@50844 153 /* Ok, it's expected */
iignatyev@50844 154 } else if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 155 printf("(NotifyFramePop) unexpected error: %s (%d)\n",
iignatyev@50844 156 TranslateError(err), err);
iignatyev@50844 157 RETURN_FAILED;
iignatyev@50844 158 } else {
iignatyev@50844 159 pop_frame_exp_events++;
iignatyev@50844 160 }
iignatyev@50844 161 printf(">>>>>>>> Invoke ForceEarlyReturn()\n");
iignatyev@50844 162
iignatyev@50844 163 printf("Before call to GetMethodID(%s, %s)\n", name_exp, sig_exp);
jcbeyler@52515 164 midActiveMethod = env->GetMethodID(targCls, name_exp, sig_exp);
iignatyev@50844 165 if (midActiveMethod == NULL) {
iignatyev@50844 166 printf("Cannot find Method ID for method %s\n", name_exp);
iignatyev@50844 167 RETURN_FAILED;
iignatyev@50844 168 }
iignatyev@50844 169 printf("After call to GetMethodID(%s, %s)\n", name_exp, sig_exp);
iignatyev@50844 170
jcbeyler@53610 171 err = jvmti->ForceEarlyReturnLong(earlyretThr, valToRet);
jcbeyler@53610 172 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 173 printf("TEST FAILED: the function ForceEarlyReturn()"
iignatyev@50844 174 " returned the error %d: %s\n",
iignatyev@50844 175 err, TranslateError(err));
iignatyev@50844 176 printf("\tFor more info about this error see the JVMTI spec.\n");
iignatyev@50844 177 RETURN_FAILED;
iignatyev@50844 178 }
iignatyev@50844 179 printf("Check #1 PASSED: ForceEarlyReturn() is successfully done\n");
iignatyev@50844 180 fflush(0);
iignatyev@50844 181
iignatyev@50844 182 return(errCode);
iignatyev@50844 183 }
iignatyev@50844 184
iignatyev@50844 185 #ifdef STATIC_BUILD
iignatyev@50844 186 JNIEXPORT jint JNICALL Agent_OnLoad_earlyretbase(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 187 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 188 }
iignatyev@50844 189 JNIEXPORT jint JNICALL Agent_OnAttach_earlyretbase(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 190 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 191 }
iignatyev@50844 192 JNIEXPORT jint JNI_OnLoad_earlyretbase(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 193 return JNI_VERSION_1_8;
iignatyev@50844 194 }
iignatyev@50844 195 #endif
iignatyev@50844 196 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 197 jint res;
iignatyev@50844 198 jvmtiError err;
iignatyev@50844 199
jcbeyler@52515 200 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
iignatyev@50844 201 if (res != JNI_OK || jvmti == NULL) {
iignatyev@50844 202 printf("Wrong error code from a valid call to GetEnv!\n");
iignatyev@50844 203 return JNI_ERR;
iignatyev@50844 204 }
iignatyev@50844 205
iignatyev@52294 206 err = jvmti->GetPotentialCapabilities(&caps);
iignatyev@50844 207 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 208 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
iignatyev@50844 209 TranslateError(err), err);
iignatyev@50844 210 return JNI_ERR;
iignatyev@50844 211 }
iignatyev@50844 212
iignatyev@52294 213 err = jvmti->AddCapabilities(&caps);
iignatyev@50844 214 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 215 printf("(AddCapabilities) unexpected error: %s (%d)\n",
iignatyev@50844 216 TranslateError(err), err);
iignatyev@50844 217 return JNI_ERR;
iignatyev@50844 218 }
iignatyev@50844 219
iignatyev@52294 220 err = jvmti->GetCapabilities(&caps);
iignatyev@50844 221 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 222 printf("(GetCapabilities) unexpected error: %s (%d)\n",
iignatyev@50844 223 TranslateError(err), err);
iignatyev@50844 224 return JNI_ERR;
iignatyev@50844 225 }
iignatyev@50844 226
iignatyev@50844 227 if (!caps.can_force_early_return) {
iignatyev@50844 228 printf("Warning: ForceEarlyReturn is not implemented\n");
iignatyev@50844 229 return JNI_OK;
iignatyev@50844 230 }
iignatyev@50844 231
iignatyev@50844 232 if (!caps.can_suspend) {
iignatyev@50844 233 printf("Warning: suspend/resume is not implemented\n");
iignatyev@50844 234 return JNI_OK;
iignatyev@50844 235 }
iignatyev@50844 236
iignatyev@50844 237 if (caps.can_generate_frame_pop_events && caps.can_generate_method_exit_events) {
iignatyev@50844 238 callbacks.MethodExit = &MethodExit;
iignatyev@50844 239 callbacks.FramePop = &FramePop;
iignatyev@52294 240 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
iignatyev@50844 241 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 242 printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
iignatyev@50844 243 TranslateError(err), err);
iignatyev@50844 244 return JNI_ERR;
iignatyev@50844 245 }
iignatyev@50844 246 } else {
iignatyev@50844 247 printf("Warning: FramePop or MethodExit event is not implemented\n");
iignatyev@50844 248 }
iignatyev@50844 249 return JNI_OK;
iignatyev@50844 250 }
iignatyev@50844 251
iignatyev@50844 252 JNIEXPORT jint JNICALL
iignatyev@50844 253 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretbase_check(JNIEnv *env, jclass cls) {
iignatyev@50844 254
iignatyev@50844 255 printf("JVMTI PopFrame events: expected: %d, generated: %d\n",
iignatyev@50844 256 meth_exit_exp_events, meth_exit_gen_events);
iignatyev@50844 257
iignatyev@50844 258 printf("JVMTI MethodExit events: expected: %d, generated: %d\n",
iignatyev@50844 259 pop_frame_exp_events, pop_frame_gen_events);
iignatyev@50844 260
iignatyev@50844 261 /* Check if the JVMTI events were generated correctly by ForceEarlyReturn */
iignatyev@50844 262 if (meth_exit_exp_events != meth_exit_gen_events ||
iignatyev@50844 263 pop_frame_exp_events != pop_frame_gen_events) {
iignatyev@50844 264 printf("TEST FAILED: JVMTI MethodExit or PopFrame events "
iignatyev@50844 265 "generated incorrectly\n");
iignatyev@50844 266 errCode = STATUS_FAILED;
iignatyev@50844 267 } else {
iignatyev@50844 268 printf("Check #2 PASSED: JVMTI MethodExit and PopFrame "
iignatyev@50844 269 "events generated correctly\n");
iignatyev@50844 270 errCode = PASSED;
iignatyev@50844 271 }
iignatyev@50844 272 fflush(0);
iignatyev@50844 273 return errCode;
iignatyev@50844 274 }
iignatyev@50844 275
iignatyev@50844 276 }