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