1 /* 2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include <stdio.h> 25 #include <string.h> 26 #include "jvmti.h" 27 #include "jni_tools.h" 28 #include "agent_common.h" 29 #include "JVMTITools.h" 30 31 #ifdef __cplusplus 32 extern "C" { 33 #endif 34 35 #ifndef JNI_ENV_ARG 36 37 #ifdef __cplusplus 38 #define JNI_ENV_ARG(x, y) y 39 #define JNI_ENV_PTR(x) x 40 #else 41 #define JNI_ENV_ARG(x,y) x, y 42 #define JNI_ENV_PTR(x) (*x) 43 #endif 44 45 #endif 46 47 #define PASSED 0 48 #define STATUS_FAILED 2 49 50 #define RETURN_FAILED errCode = STATUS_FAILED; fflush(0); return 51 52 static jvmtiEnv *jvmti = NULL; 53 static jvmtiCapabilities caps; 54 static jvmtiEventCallbacks callbacks; 55 static jint errCode = PASSED; 56 static jboolean printdump = JNI_TRUE; 57 58 static jmethodID midRun = NULL; 59 static jmethodID midCountDownLong = NULL; 60 static jmethodID midCheckPoint = NULL; 61 62 static jint framesExpected = 0; 63 static jint framesCount = 0; 64 static jint methodExitEventCount = 0; 65 66 static const char *cls_exp = "Lnsk/jvmti/unit/ForceEarlyReturn/earlyretlong$earlyretThread;"; 67 68 static jlong val_exp = 0L; 69 static const char *sig_exp = "(I)J"; 70 static const char *name_exp = "countDownLong"; 71 72 static const char *argName = "nestingCount"; 73 74 void check(jvmtiEnv *jvmti_env, jthread thr, jmethodID mid, 75 jlocation loc, jint frame_no) { 76 jvmtiError err; 77 jclass cls; 78 jlocation loc_exp = (frame_no == 0) ? 0x15 : 0xd; 79 char *sigClass, *name, *sig, *generic; 80 jvmtiLocalVariableEntry *table = NULL; 81 jint entryCount = 0; 82 jint argValue; 83 jint j; 84 85 err = jvmti_env->GetMethodDeclaringClass(mid, &cls); 86 if (err != JVMTI_ERROR_NONE) { 87 printf("(GetMethodDeclaringClass#%d) unexpected error: %s (%d)\n", 88 frame_no, TranslateError(err), err); 89 RETURN_FAILED; 90 } 91 92 err = jvmti_env->GetClassSignature(cls, &sigClass, &generic); 93 if (err != JVMTI_ERROR_NONE) { 94 printf("(GetClassSignature#%d) unexpected error: %s (%d)\n", 95 frame_no, TranslateError(err), err); 96 RETURN_FAILED; 97 } 98 99 err = jvmti_env->GetMethodName(mid, &name, &sig, &generic); 100 if (err != JVMTI_ERROR_NONE) { 101 printf("(GetMethodName#%d) unexpected error: %s (%d)\n", 102 frame_no, TranslateError(err), err); 103 RETURN_FAILED; 104 } 105 106 /* Get Local Variable Table to be able to get the argument value 107 * from current method frame and compare it with the expected value 108 */ 109 err = jvmti_env->GetLocalVariableTable(mid, &entryCount, &table); 110 if (err != JVMTI_ERROR_NONE) { 111 printf("(GetLocalVariableTable#%d) unexpected error: %s (%d)\n", 112 frame_no, TranslateError(err), err); 113 RETURN_FAILED; 114 } 115 if (table != NULL) { 116 for (j = 0; j < entryCount; j++) { 117 if (strcmp(table[j].name, argName) == 0) { 118 err = jvmti_env->GetLocalInt(thr, 0, 119 table[j].slot, &argValue); 120 if (err != JVMTI_ERROR_NONE) { 121 printf("(GetLocalInt#%d) unexpected error: %s (%d)\n", 122 frame_no, TranslateError(err), err); 123 RETURN_FAILED; 124 } 125 } 126 } 127 } 128 129 if (printdump == JNI_TRUE) { 130 printf("\n>>> step %d: \"%s.%s%s\"\n", frame_no, sigClass, name, sig); 131 printf(">>> location: %#x%08x", (jint)(loc >> 32), (jint)loc); 132 printf(", arg value: %d\n", argValue); 133 } 134 135 if (sigClass == NULL || strcmp(sigClass, cls_exp) != 0) { 136 printf("(step %d) Wrong class sig: \"%s\",\n", frame_no, sigClass); 137 printf(" expected: \"%s\"\n", cls_exp); 138 RETURN_FAILED; 139 } 140 if (name == NULL || strcmp(name, name_exp) != 0) { 141 printf("(step %d) wrong method name: \"%s\",", frame_no, name); 142 printf(" expected: \"%s\"\n", name_exp); 143 RETURN_FAILED; 144 } 145 if (sig == NULL || strcmp(sig, sig_exp) != 0) { 146 printf("(step %d) wrong method sig: \"%s\",", frame_no, sig); 147 printf(" expected: \"%s\"\n", sig_exp); 148 RETURN_FAILED; 149 } 150 if (loc != loc_exp) { 151 printf("(step %d) wrong location: %#x%08x,", 152 frame_no, (jint)(loc >> 32), (jint)loc); 153 printf(" expected: %#x\n", (jint)loc_exp); 154 RETURN_FAILED; 155 } 156 if (argValue != frame_no) { 157 printf("(step %d) wrong argument value: %d,", frame_no, argValue); 158 printf(" expected: %d\n", frame_no); 159 RETURN_FAILED; 160 } 161 162 if (sigClass != NULL) { 163 jvmti_env->Deallocate((unsigned char*)sigClass); 164 } 165 if (name != NULL) { 166 jvmti_env->Deallocate((unsigned char*)name); 167 } 168 if (sig != NULL) { 169 jvmti_env->Deallocate((unsigned char*)sig); 170 } 171 if (table != NULL) { 172 for (j = 0; j < entryCount; j++) { 173 jvmti_env->Deallocate((unsigned char*)(table[j].name)); 174 jvmti_env->Deallocate((unsigned char*)(table[j].signature)); 175 } 176 jvmti_env->Deallocate((unsigned char*)table); 177 } 178 if (methodExitEventCount != (framesCount + 1)) { 179 printf("(step %d) wrong methodExitEventCount: %d,", 180 frame_no, methodExitEventCount); 181 printf(" expected: %d\n", framesCount + 1); 182 RETURN_FAILED; 183 } 184 fflush(0); 185 } 186 187 void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env, 188 jthread thread, jmethodID method, jlocation location) { 189 jvmtiError err; 190 191 if (midCheckPoint != method) { 192 printf("bp: don't know where we get called from"); 193 RETURN_FAILED; 194 } 195 196 if (printdump == JNI_TRUE) { 197 printf(">>> breakpoint in checkPoint\n"); 198 } 199 200 err = jvmti_env->ClearBreakpoint(midCheckPoint, 0); 201 if (err != JVMTI_ERROR_NONE) { 202 printf("(ClearBreakpoint) unexpected error: %s (%d)\n", 203 TranslateError(err), err); 204 RETURN_FAILED; 205 } 206 207 err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, 208 JVMTI_EVENT_SINGLE_STEP, thread); 209 if (err != JVMTI_ERROR_NONE) { 210 printf("Cannot enable single step events: %s (%d)\n", 211 TranslateError(err), err); 212 RETURN_FAILED; 213 } 214 215 err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, 216 JVMTI_EVENT_METHOD_EXIT, thread); 217 if (err != JVMTI_ERROR_NONE) { 218 printf("Cannot enable method exit events: %s (%d)\n", 219 TranslateError(err), err); 220 RETURN_FAILED; 221 } 222 223 err = jvmti_env->ForceEarlyReturnVoid(thread); 224 if (err != JVMTI_ERROR_NONE) { 225 printf("(ForceEarlyReturn) unexpected error: %s (%d)\n", 226 TranslateError(err), err); 227 RETURN_FAILED; 228 } 229 fflush(0); 230 } 231 232 void JNICALL SingleStep(jvmtiEnv *jvmti_env, JNIEnv *env, 233 jthread thread, jmethodID method, jlocation location) { 234 jvmtiError err; 235 236 if (method == midRun) { 237 if (printdump == JNI_TRUE) { 238 printf(">>> returned early %d frames till method \"run()\"\n", 239 framesCount); 240 } 241 242 err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, 243 JVMTI_EVENT_SINGLE_STEP, thread); 244 if (err != JVMTI_ERROR_NONE) { 245 printf("Cannot disable single step events: %s (%d)\n", 246 TranslateError(err), err); 247 RETURN_FAILED; 248 } 249 err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, 250 JVMTI_EVENT_METHOD_EXIT, thread); 251 if (err != JVMTI_ERROR_NONE) { 252 printf("Cannot disable method exit events: %s (%d)\n", 253 TranslateError(err), err); 254 RETURN_FAILED; 255 } 256 } else if (method == midCountDownLong) { 257 check(jvmti_env, thread, method, location, framesCount); 258 framesCount++; 259 260 err = jvmti_env->ForceEarlyReturnLong(thread, ++val_exp); 261 if (err != JVMTI_ERROR_NONE) { 262 printf("(ForceEarlyReturn) unexpected error: %s (%d)\n", 263 TranslateError(err), err); 264 RETURN_FAILED; 265 } 266 } 267 fflush(0); 268 } 269 270 void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, 271 jmethodID method, jboolean was_popped_by_exception, jvalue value) { 272 jlong ret_val = value.j; 273 int * val_ptr = (int *) &ret_val; 274 int * exp_ptr = (int *) &val_exp; 275 276 methodExitEventCount++; 277 if (method == midCountDownLong) { 278 printf(">>> ForceEarlyReturnLong value: dec: %" LL "d, hex: %#x %#x\n", 279 ret_val, val_ptr[0], val_ptr[1]); 280 printf(">>> expected return value: dec: %" LL "d, hex: %#x %#x\n", 281 val_exp, exp_ptr[0], exp_ptr[1]); 282 283 if (ret_val != val_exp) { 284 printf("Wrong ForceEarlyReturnLong return value: %" LL "d\n", ret_val); 285 errCode = STATUS_FAILED; 286 } 287 if (was_popped_by_exception) { 288 printf("Method was_popped_by_exception unexpectedly\n"); 289 errCode = STATUS_FAILED; 290 } 291 } 292 fflush(0); 293 } 294 295 #ifdef STATIC_BUILD 296 JNIEXPORT jint JNICALL Agent_OnLoad_earlyretlong(JavaVM *jvm, char *options, void *reserved) { 297 return Agent_Initialize(jvm, options, reserved); 298 } 299 JNIEXPORT jint JNICALL Agent_OnAttach_earlyretlong(JavaVM *jvm, char *options, void *reserved) { 300 return Agent_Initialize(jvm, options, reserved); 301 } 302 JNIEXPORT jint JNI_OnLoad_earlyretlong(JavaVM *jvm, char *options, void *reserved) { 303 return JNI_VERSION_1_8; 304 } 305 #endif 306 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 307 jvmtiError err; 308 jint res; 309 310 if (options != NULL && strcmp(options, "printdump") == 0) { 311 printf("Printdump is turned on!\n"); 312 313 printdump = JNI_TRUE; 314 } 315 316 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), 317 JVMTI_VERSION_1_1); 318 if (res != JNI_OK || jvmti == NULL) { 319 printf("Wrong error code from a valid call to GetEnv!\n"); 320 return JNI_ERR; 321 } 322 323 err = jvmti->GetPotentialCapabilities(&caps); 324 if (err != JVMTI_ERROR_NONE) { 325 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n", 326 TranslateError(err), err); 327 return JNI_ERR; 328 } 329 330 err = jvmti->AddCapabilities(&caps); 331 if (err != JVMTI_ERROR_NONE) { 332 printf("(AddCapabilities) unexpected error: %s (%d)\n", 333 TranslateError(err), err); 334 return JNI_ERR; 335 } 336 337 err = jvmti->GetCapabilities(&caps); 338 if (err != JVMTI_ERROR_NONE) { 339 printf("(GetCapabilities) unexpected error: %s (%d)\n", 340 TranslateError(err), err); 341 return JNI_ERR; 342 } 343 344 if (!caps.can_force_early_return) { 345 printf("Warning: ForceEarlyReturn is not implemented\n"); 346 } 347 348 if (caps.can_generate_breakpoint_events && 349 caps.can_generate_method_exit_events && 350 caps.can_generate_single_step_events) 351 { 352 callbacks.Breakpoint = &Breakpoint; 353 callbacks.SingleStep = &SingleStep; 354 callbacks.MethodExit = &MethodExit; 355 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); 356 if (err != JVMTI_ERROR_NONE) { 357 printf("(SetEventCallbacks) unexpected error: %s (%d)\n", 358 TranslateError(err), err); 359 return JNI_ERR; 360 } 361 } else { 362 printf("Warning: Breakpoint or SingleStep event are not implemented\n"); 363 } 364 365 return JNI_OK; 366 } 367 368 JNIEXPORT void JNICALL 369 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretlong_getReady( 370 JNIEnv *env, jclass c, jclass cls, jint depth, jlong retval_base) { 371 jvmtiError err; 372 373 val_exp = retval_base; 374 if (jvmti == NULL) { 375 printf("JVMTI client was not properly loaded!\n"); 376 RETURN_FAILED; 377 } 378 379 if (!caps.can_force_early_return || 380 !caps.can_generate_breakpoint_events || 381 !caps.can_generate_method_exit_events || 382 !caps.can_generate_single_step_events) { 383 return; 384 } 385 386 midRun = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls), 387 "run", "()V"); 388 if (midRun == NULL) { 389 printf("Cannot find Method ID for method run\n"); 390 RETURN_FAILED; 391 } 392 393 midCheckPoint = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls), 394 "checkPoint", "()V"); 395 if (midCheckPoint == NULL) { 396 printf("Cannot find Method ID for method checkPoint\n"); 397 RETURN_FAILED; 398 } 399 400 midCountDownLong = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls), 401 "countDownLong", "(I)J"); 402 if (midCountDownLong == NULL) { 403 printf("Cannot find Method ID for method countDownLong\n"); 404 RETURN_FAILED; 405 } 406 407 err = jvmti->SetBreakpoint(midCheckPoint, 0); 408 if (err != JVMTI_ERROR_NONE) { 409 printf("(SetBreakpoint) unexpected error: %s (%d)\n", 410 TranslateError(err), err); 411 RETURN_FAILED; 412 } 413 414 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, 415 JVMTI_EVENT_BREAKPOINT, NULL); 416 if (err != JVMTI_ERROR_NONE) { 417 printf("Failed to enable BREAKPOINT event: %s (%d)\n", 418 TranslateError(err), err); 419 RETURN_FAILED; 420 } else { 421 framesExpected = depth; 422 } 423 } 424 425 JNIEXPORT void JNICALL 426 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretlong_printLong( 427 JNIEnv *env, jclass cls, jlong val) { 428 jint *iptr = (jint *) &val; 429 430 printf("\n>>> Returned value: dec: %" LL "d, hex: %#x %#x\n", 431 val, iptr[0], iptr[1]); 432 fflush(0); 433 return; 434 } 435 436 JNIEXPORT jint JNICALL 437 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretlong_check(JNIEnv *env, jclass cls) { 438 if (framesCount != framesExpected) { 439 printf("Wrong number of returned early frames: %d, expected: %d\n", 440 framesCount, framesExpected); 441 errCode = STATUS_FAILED; 442 } 443 fflush(0); 444 return errCode; 445 } 446 447 #ifdef __cplusplus 448 } 449 #endif