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 "agent_common.h" 28 #include "JVMTITools.h" 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #ifndef JNI_ENV_ARG 35 36 #ifdef __cplusplus 37 #define JNI_ENV_ARG(x, y) y 38 #define JNI_ENV_PTR(x) x 39 #else 40 #define JNI_ENV_ARG(x,y) x, y 41 #define JNI_ENV_PTR(x) (*x) 42 #endif 43 44 #endif 45 46 #define PASSED 0 47 #define STATUS_FAILED 2 48 49 #define RETURN_FAILED errCode = STATUS_FAILED; fflush(0); return 50 51 static jvmtiEnv *jvmti = NULL; 52 static jvmtiCapabilities caps; 53 static jvmtiEventCallbacks callbacks; 54 static jint errCode = PASSED; 55 static jboolean printdump = JNI_TRUE; 56 57 static jmethodID midRun = NULL; 58 static jmethodID midCountDownString = NULL; 59 static jmethodID midCheckPoint = NULL; 60 61 static jint framesExpected = 0; 62 static jint framesCount = 0; 63 static jint methodExitEventCount = 0; 64 65 static const char *cls_exp = "Lnsk/jvmti/unit/ForceEarlyReturn/earlyretstr$earlyretThread;"; 66 67 static jstring str_exp = NULL; 68 static const char *sig_exp = "(I)Ljava/lang/String;"; 69 static const char *name_exp = "countDownString"; 70 71 static const char *argName = "nestingCount"; 72 73 void check(jvmtiEnv *jvmti_env, jthread thr, jmethodID mid, 74 jlocation loc, jint frame_no) { 75 jvmtiError err; 76 jclass cls; 77 jlocation loc_exp = (frame_no == 0) ? 0x15 : 0xd; 78 char *sigClass, *name, *sig, *generic; 79 jvmtiLocalVariableEntry *table = NULL; 80 jint entryCount = 0; 81 jint argValue; 82 jint j; 83 84 err = jvmti_env->GetMethodDeclaringClass(mid, &cls); 85 if (err != JVMTI_ERROR_NONE) { 86 printf("(GetMethodDeclaringClass#%d) unexpected error: %s (%d)\n", 87 frame_no, TranslateError(err), err); 88 RETURN_FAILED; 89 } 90 91 err = jvmti_env->GetClassSignature(cls, &sigClass, &generic); 92 if (err != JVMTI_ERROR_NONE) { 93 printf("(GetClassSignature#%d) unexpected error: %s (%d)\n", 94 frame_no, TranslateError(err), err); 95 RETURN_FAILED; 96 } 97 98 err = jvmti_env->GetMethodName(mid, &name, &sig, &generic); 99 if (err != JVMTI_ERROR_NONE) { 100 printf("(GetMethodName#%d) unexpected error: %s (%d)\n", 101 frame_no, TranslateError(err), err); 102 RETURN_FAILED; 103 } 104 105 /* Get Local Variable Table to be able to get the argument value 106 * from current method frame and compare it with the expected value 107 */ 108 err = jvmti_env->GetLocalVariableTable(mid, &entryCount, &table); 109 if (err != JVMTI_ERROR_NONE) { 110 printf("(GetLocalVariableTable#%d) unexpected error: %s (%d)\n", 111 frame_no, TranslateError(err), err); 112 RETURN_FAILED; 113 } 114 if (table != NULL) { 115 for (j = 0; j < entryCount; j++) { 116 if (strcmp(table[j].name, argName) == 0) { 117 err = jvmti_env->GetLocalInt(thr, 0, 118 table[j].slot, &argValue); 119 if (err != JVMTI_ERROR_NONE) { 120 printf("(GetLocalInt#%d) unexpected error: %s (%d)\n", 121 frame_no, TranslateError(err), err); 122 RETURN_FAILED; 123 } 124 } 125 } 126 } 127 128 if (printdump == JNI_TRUE) { 129 printf("\n>>> step %d: \"%s.%s%s\"\n", frame_no, sigClass, name, sig); 130 printf(">>> location: 0x%x%08x", (jint)(loc >> 32), (jint)loc); 131 printf(", arg value: %d\n", argValue); 132 } 133 134 if (sigClass == NULL || strcmp(sigClass, cls_exp) != 0) { 135 printf("(step %d) Wrong class sig: \"%s\",\n", frame_no, sigClass); 136 printf(" expected: \"%s\"\n", cls_exp); 137 RETURN_FAILED; 138 } 139 if (name == NULL || strcmp(name, name_exp) != 0) { 140 printf("(step %d) wrong method name: \"%s\",", frame_no, name); 141 printf(" expected: \"%s\"\n", name_exp); 142 RETURN_FAILED; 143 } 144 if (sig == NULL || strcmp(sig, sig_exp) != 0) { 145 printf("(step %d) wrong method sig: \"%s\",", frame_no, sig); 146 printf(" expected: \"%s\"\n", sig_exp); 147 RETURN_FAILED; 148 } 149 if (loc != loc_exp) { 150 printf("(step %d) wrong location: 0x%x%08x,", 151 frame_no, (jint)(loc >> 32), (jint)loc); 152 printf(" expected: 0x%x\n", (jint)loc_exp); 153 RETURN_FAILED; 154 } 155 if (argValue != frame_no) { 156 printf("(step %d) wrong argument value: %d,", frame_no, argValue); 157 printf(" expected: %d\n", frame_no); 158 RETURN_FAILED; 159 } 160 161 if (sigClass != NULL) { 162 jvmti_env->Deallocate((unsigned char*)sigClass); 163 } 164 if (name != NULL) { 165 jvmti_env->Deallocate((unsigned char*)name); 166 } 167 if (sig != NULL) { 168 jvmti_env->Deallocate((unsigned char*)sig); 169 } 170 if (table != NULL) { 171 for (j = 0; j < entryCount; j++) { 172 jvmti_env->Deallocate((unsigned char*)(table[j].name)); 173 jvmti_env->Deallocate((unsigned char*)(table[j].signature)); 174 } 175 jvmti_env->Deallocate((unsigned char*)table); 176 } 177 if (methodExitEventCount != (framesCount + 1)) { 178 printf("(step %d) wrong methodExitEventCount: %d,", 179 frame_no, methodExitEventCount); 180 printf(" expected: %d\n", framesCount + 1); 181 RETURN_FAILED; 182 } 183 fflush(0); 184 } 185 186 void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env, 187 jthread thread, jmethodID method, jlocation location) { 188 jvmtiError err; 189 190 if (midCheckPoint != method) { 191 printf("bp: don't know where we get called from"); 192 RETURN_FAILED; 193 } 194 195 if (printdump == JNI_TRUE) { 196 printf(">>> breakpoint in checkPoint\n"); 197 } 198 199 err = jvmti_env->ClearBreakpoint(midCheckPoint, 0); 200 if (err != JVMTI_ERROR_NONE) { 201 printf("(ClearBreakpoint) unexpected error: %s (%d)\n", 202 TranslateError(err), err); 203 RETURN_FAILED; 204 } 205 206 err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, 207 JVMTI_EVENT_SINGLE_STEP, thread); 208 if (err != JVMTI_ERROR_NONE) { 209 printf("Cannot enable single step events: %s (%d)\n", 210 TranslateError(err), err); 211 RETURN_FAILED; 212 } 213 214 err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, 215 JVMTI_EVENT_METHOD_EXIT, thread); 216 if (err != JVMTI_ERROR_NONE) { 217 printf("Cannot enable method exit events: %s (%d)\n", 218 TranslateError(err), err); 219 RETURN_FAILED; 220 } 221 222 err = jvmti_env->ForceEarlyReturnVoid(thread); 223 if (err != JVMTI_ERROR_NONE) { 224 printf("(ForceEarlyReturnVoid) unexpected error: %s (%d)\n", 225 TranslateError(err), err); 226 RETURN_FAILED; 227 } 228 fflush(0); 229 } 230 231 void JNICALL SingleStep(jvmtiEnv *jvmti_env, JNIEnv *env, 232 jthread thread, jmethodID method, jlocation location) { 233 jvmtiError err; 234 235 if (method == midRun) { 236 if (printdump == JNI_TRUE) { 237 printf(">>> returned early %d frames till method \"run()\"\n", 238 framesCount); 239 } 240 241 err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, 242 JVMTI_EVENT_SINGLE_STEP, thread); 243 if (err != JVMTI_ERROR_NONE) { 244 printf("Cannot disable single step events: %s (%d)\n", 245 TranslateError(err), err); 246 RETURN_FAILED; 247 } 248 err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, 249 JVMTI_EVENT_METHOD_EXIT, thread); 250 if (err != JVMTI_ERROR_NONE) { 251 printf("Cannot disable method exit events: %s (%d)\n", 252 TranslateError(err), err); 253 RETURN_FAILED; 254 } 255 } else { 256 check(jvmti_env, thread, method, location, framesCount); 257 framesCount++; 258 err = jvmti_env->ForceEarlyReturnObject(thread, str_exp); 259 if (err != JVMTI_ERROR_NONE) { 260 printf("(ForceEarlyReturnObject) unexpected error: %s (%d)\n", 261 TranslateError(err), err); 262 RETURN_FAILED; 263 } 264 } 265 fflush(0); 266 } 267 268 void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, 269 jmethodID method, jboolean was_popped_by_exception, jvalue value) { 270 const char* ret_str; 271 const char* exp_str; 272 jstring ret_val = (jstring) value.l; 273 274 methodExitEventCount++; 275 printf("MethodExit event: methodExitEventCount=%d\n", methodExitEventCount); 276 if (method == midRun || method == midCheckPoint) { 277 return; 278 } 279 if (method == midCountDownString) { 280 ret_str = JNI_ENV_PTR(env)->GetStringUTFChars(JNI_ENV_ARG(env, ret_val), 0); 281 exp_str = JNI_ENV_PTR(env)->GetStringUTFChars(JNI_ENV_ARG(env, str_exp), 0); 282 printf("Expected string: \"%s\"\n", exp_str); 283 printf("Returned string: \"%s\"\n", ret_str); 284 if (was_popped_by_exception) { 285 printf("Method was_popped_by_exception unexpectedly\n"); 286 errCode = STATUS_FAILED; 287 } 288 } 289 fflush(0); 290 } 291 292 #ifdef STATIC_BUILD 293 JNIEXPORT jint JNICALL Agent_OnLoad_earlyretstr(JavaVM *jvm, char *options, void *reserved) { 294 return Agent_Initialize(jvm, options, reserved); 295 } 296 JNIEXPORT jint JNICALL Agent_OnAttach_earlyretstr(JavaVM *jvm, char *options, void *reserved) { 297 return Agent_Initialize(jvm, options, reserved); 298 } 299 JNIEXPORT jint JNI_OnLoad_earlyretstr(JavaVM *jvm, char *options, void *reserved) { 300 return JNI_VERSION_1_8; 301 } 302 #endif 303 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 304 jvmtiError err; 305 jint res; 306 307 if (options != NULL && strcmp(options, "printdump") == 0) { 308 printf("Printdump is turned on!\n"); 309 310 printdump = JNI_TRUE; 311 } 312 313 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), 314 JVMTI_VERSION_1_1); 315 if (res != JNI_OK || jvmti == NULL) { 316 printf("Wrong error code of a valid call to GetEnv!\n"); 317 return JNI_ERR; 318 } 319 320 err = jvmti->GetPotentialCapabilities(&caps); 321 if (err != JVMTI_ERROR_NONE) { 322 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n", 323 TranslateError(err), err); 324 return JNI_ERR; 325 } 326 327 err = jvmti->AddCapabilities(&caps); 328 if (err != JVMTI_ERROR_NONE) { 329 printf("(AddCapabilities) unexpected error: %s (%d)\n", 330 TranslateError(err), err); 331 return JNI_ERR; 332 } 333 334 err = jvmti->GetCapabilities(&caps); 335 if (err != JVMTI_ERROR_NONE) { 336 printf("(GetCapabilities) unexpected error: %s (%d)\n", 337 TranslateError(err), err); 338 return JNI_ERR; 339 } 340 341 if (!caps.can_force_early_return) { 342 printf("Warning: ForceEarlyReturn is not implemented\n"); 343 } 344 345 if (caps.can_generate_breakpoint_events && 346 caps.can_generate_method_exit_events && 347 caps.can_generate_single_step_events) 348 { 349 callbacks.Breakpoint = &Breakpoint; 350 callbacks.SingleStep = &SingleStep; 351 callbacks.MethodExit = &MethodExit; 352 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); 353 if (err != JVMTI_ERROR_NONE) { 354 printf("(SetEventCallbacks) unexpected error: %s (%d)\n", 355 TranslateError(err), err); 356 return JNI_ERR; 357 } 358 } else { 359 printf("Warning: Breakpoint or SingleStep event are not implemented\n"); 360 } 361 362 return JNI_OK; 363 } 364 365 JNIEXPORT void JNICALL 366 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretstr_getReady( 367 JNIEnv *env, jclass c, jclass cls, jint depth, jstring expected_str) { 368 jvmtiError err; 369 370 if (jvmti == NULL) { 371 printf("JVMTI client was not properly loaded!\n"); 372 RETURN_FAILED; 373 } 374 375 if (!caps.can_force_early_return || 376 !caps.can_generate_breakpoint_events || 377 !caps.can_generate_method_exit_events || 378 !caps.can_generate_single_step_events) { 379 return; 380 } 381 382 midRun = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls), 383 "run", "()V"); 384 if (midRun == NULL) { 385 printf("Cannot find Method ID for method run\n"); 386 RETURN_FAILED; 387 } 388 389 midCheckPoint = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls), 390 "checkPoint", "()V"); 391 if (midCheckPoint == NULL) { 392 printf("Cannot find Method ID for method checkPoint\n"); 393 RETURN_FAILED; 394 } 395 396 midCountDownString = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls), 397 "countDownString", sig_exp); 398 if (midCountDownString == NULL) { 399 printf("Cannot find Method ID for method countDownString\n"); 400 RETURN_FAILED; 401 } 402 403 err = jvmti->SetBreakpoint(midCheckPoint, 0); 404 if (err != JVMTI_ERROR_NONE) { 405 printf("(SetBreakpoint) unexpected error: %s (%d)\n", 406 TranslateError(err), err); 407 RETURN_FAILED; 408 } 409 410 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, 411 JVMTI_EVENT_BREAKPOINT, NULL); 412 if (err != JVMTI_ERROR_NONE) { 413 printf("Failed to enable BREAKPOINT event: %s (%d)\n", 414 TranslateError(err), err); 415 RETURN_FAILED; 416 } else { 417 str_exp = (jstring) JNI_ENV_PTR(env)->NewGlobalRef(JNI_ENV_ARG(env, expected_str)); 418 framesExpected = depth; 419 } 420 } 421 422 JNIEXPORT jint JNICALL 423 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretstr_check(JNIEnv *env, jclass cls) { 424 if (framesCount != framesExpected) { 425 printf("Wrong number of returned early frames: %d, expected: %d\n", 426 framesCount, framesExpected); 427 errCode = STATUS_FAILED; 428 } 429 fflush(0); 430 return errCode; 431 } 432 433 #ifdef __cplusplus 434 } 435 #endif