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
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);
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;
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
|
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 extern "C" {
32
33
34 #define PASSED 0
35 #define STATUS_FAILED 2
36
37 #define RETURN_FAILED errCode = STATUS_FAILED; fflush(0); return
38
39 static jvmtiEnv *jvmti = NULL;
40 static jvmtiCapabilities caps;
41 static jvmtiEventCallbacks callbacks;
42 static jint errCode = PASSED;
43 static jboolean printdump = JNI_TRUE;
44
45 static jmethodID midRun = NULL;
46 static jmethodID midCountDownLong = NULL;
47 static jmethodID midCheckPoint = NULL;
48
49 static jint framesExpected = 0;
50 static jint framesCount = 0;
51 static jint methodExitEventCount = 0;
52
283 JNIEXPORT jint JNICALL Agent_OnLoad_earlyretlong(JavaVM *jvm, char *options, void *reserved) {
284 return Agent_Initialize(jvm, options, reserved);
285 }
286 JNIEXPORT jint JNICALL Agent_OnAttach_earlyretlong(JavaVM *jvm, char *options, void *reserved) {
287 return Agent_Initialize(jvm, options, reserved);
288 }
289 JNIEXPORT jint JNI_OnLoad_earlyretlong(JavaVM *jvm, char *options, void *reserved) {
290 return JNI_VERSION_1_8;
291 }
292 #endif
293 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
294 jvmtiError err;
295 jint res;
296
297 if (options != NULL && strcmp(options, "printdump") == 0) {
298 printf("Printdump is turned on!\n");
299
300 printdump = JNI_TRUE;
301 }
302
303 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
304 if (res != JNI_OK || jvmti == NULL) {
305 printf("Wrong error code from a valid call to GetEnv!\n");
306 return JNI_ERR;
307 }
308
309 err = jvmti->GetPotentialCapabilities(&caps);
310 if (err != JVMTI_ERROR_NONE) {
311 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
312 TranslateError(err), err);
313 return JNI_ERR;
314 }
315
316 err = jvmti->AddCapabilities(&caps);
317 if (err != JVMTI_ERROR_NONE) {
318 printf("(AddCapabilities) unexpected error: %s (%d)\n",
319 TranslateError(err), err);
320 return JNI_ERR;
321 }
322
323 err = jvmti->GetCapabilities(&caps);
352 }
353
354 JNIEXPORT void JNICALL
355 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretlong_getReady(
356 JNIEnv *env, jclass c, jclass cls, jint depth, jlong retval_base) {
357 jvmtiError err;
358
359 val_exp = retval_base;
360 if (jvmti == NULL) {
361 printf("JVMTI client was not properly loaded!\n");
362 RETURN_FAILED;
363 }
364
365 if (!caps.can_force_early_return ||
366 !caps.can_generate_breakpoint_events ||
367 !caps.can_generate_method_exit_events ||
368 !caps.can_generate_single_step_events) {
369 return;
370 }
371
372 midRun = env->GetMethodID(cls, "run", "()V");
373 if (midRun == NULL) {
374 printf("Cannot find Method ID for method run\n");
375 RETURN_FAILED;
376 }
377
378 midCheckPoint = env->GetMethodID(cls, "checkPoint", "()V");
379 if (midCheckPoint == NULL) {
380 printf("Cannot find Method ID for method checkPoint\n");
381 RETURN_FAILED;
382 }
383
384 midCountDownLong = env->GetMethodID(cls, "countDownLong", "(I)J");
385 if (midCountDownLong == NULL) {
386 printf("Cannot find Method ID for method countDownLong\n");
387 RETURN_FAILED;
388 }
389
390 err = jvmti->SetBreakpoint(midCheckPoint, 0);
391 if (err != JVMTI_ERROR_NONE) {
392 printf("(SetBreakpoint) unexpected error: %s (%d)\n",
393 TranslateError(err), err);
394 RETURN_FAILED;
395 }
396
397 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
398 JVMTI_EVENT_BREAKPOINT, NULL);
399 if (err != JVMTI_ERROR_NONE) {
400 printf("Failed to enable BREAKPOINT event: %s (%d)\n",
401 TranslateError(err), err);
402 RETURN_FAILED;
403 } else {
404 framesExpected = depth;
410 JNIEnv *env, jclass cls, jlong val) {
411 jint *iptr = (jint *) &val;
412
413 printf("\n>>> Returned value: dec: %" LL "d, hex: %#x %#x\n",
414 val, iptr[0], iptr[1]);
415 fflush(0);
416 return;
417 }
418
419 JNIEXPORT jint JNICALL
420 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretlong_check(JNIEnv *env, jclass cls) {
421 if (framesCount != framesExpected) {
422 printf("Wrong number of returned early frames: %d, expected: %d\n",
423 framesCount, framesExpected);
424 errCode = STATUS_FAILED;
425 }
426 fflush(0);
427 return errCode;
428 }
429
430 }
|