Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetLocalVariable/libGetLocalVars.cpp
41153 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"2627#ifdef __cplusplus28extern "C" {29#endif3031#define STATUS_PASSED 032#define STATUS_FAILED 233#define TranslateError(err) "JVMTI error"3435static jint result = STATUS_PASSED;36static jvmtiEnv *jvmti = NULL;3738#define DECL_TEST_FUNC(type, Type) \39static void \40test_##type(jthread thr, int depth, int slot, const char* exp_type) { \41j##type val; \42jvmtiError err = jvmti->GetLocal##Type(thr, depth, slot, &val); \43\44printf(" GetLocal%s: %s (%d)\n", #Type, TranslateError(err), err); \45if (err != JVMTI_ERROR_NONE) { \46printf(" FAIL: GetLocal%s failed to get value from a local %s\n", #Type, exp_type); \47result = STATUS_FAILED; \48} else { \49printf(" GetLocal%s got value from a local %s as expected\n", #Type, exp_type); \50} \51}5253#define DECL_TEST_INV_SLOT_FUNC(type, Type) \54static void \55test_##type##_inv_slot(jthread thr, int depth, int slot, const char* exp_type) { \56j##type val; \57jvmtiError err = jvmti->GetLocal##Type(thr, depth, slot, &val); \58\59printf(" GetLocal%s: %s (%d)\n", #Type, TranslateError(err), err); \60if (err != JVMTI_ERROR_INVALID_SLOT) { \61printf(" FAIL: GetLocal%s failed to return JVMTI_ERROR_INVALID_SLOT for local %s\n", #Type, exp_type); \62result = STATUS_FAILED; \63} else { \64printf(" GetLocal%s returned JVMTI_ERROR_INVALID_SLOT for local %s as expected\n", #Type, exp_type); \65} \66}6768#define DECL_TEST_TYPE_MISMATCH_FUNC(type, Type) \69static void \70test_##type##_type_mismatch(jthread thr, int depth, int slot, const char* exp_type) { \71j##type val; \72jvmtiError err = jvmti->GetLocal##Type(thr, depth, slot, &val); \73\74printf(" GetLocal%s: %s (%d)\n", #Type, TranslateError(err), err); \75if (err != JVMTI_ERROR_TYPE_MISMATCH) { \76printf(" FAIL: GetLocal%s failed to return JVMTI_ERROR_TYPE_MISMATCH for local %s\n", #Type, exp_type); \77result = STATUS_FAILED; \78} else { \79printf(" GetLocal%s returned JVMTI_ERROR_TYPE_MISMATCH for local %s as expected\n", #Type, exp_type); \80} \81}8283DECL_TEST_FUNC(int, Int);84DECL_TEST_FUNC(float, Float);85DECL_TEST_FUNC(long, Long);86DECL_TEST_FUNC(double, Double);87DECL_TEST_FUNC(object, Object);8889DECL_TEST_INV_SLOT_FUNC(int, Int);90DECL_TEST_INV_SLOT_FUNC(float, Float);91DECL_TEST_INV_SLOT_FUNC(long, Long);92DECL_TEST_INV_SLOT_FUNC(double, Double);93DECL_TEST_INV_SLOT_FUNC(object, Object);9495DECL_TEST_TYPE_MISMATCH_FUNC(int, Int);96DECL_TEST_TYPE_MISMATCH_FUNC(float, Float);97DECL_TEST_TYPE_MISMATCH_FUNC(long, Long);98DECL_TEST_TYPE_MISMATCH_FUNC(double, Double);99DECL_TEST_TYPE_MISMATCH_FUNC(object, Object);100101static void102test_local_byte(jthread thr, int depth, int slot) {103printf("\n test_local_byte: BEGIN\n\n");104105test_int(thr, depth, slot, "byte");106test_long_inv_slot(thr, depth, slot, "byte");107test_float(thr, depth, slot, "byte");108test_double_inv_slot(thr, depth, slot, "byte");109test_object_type_mismatch(thr, depth, slot, "byte");110111printf("\n test_local_byte: END\n\n");112}113114static void115test_local_object(jthread thr, int depth, int slot) {116printf("\n test_local_object: BEGIN\n\n");117118test_int_type_mismatch(thr, depth, slot, "object");119test_long_type_mismatch(thr, depth, slot, "object");120test_float_type_mismatch(thr, depth, slot, "object");121test_double_type_mismatch(thr, depth, slot, "object");122test_object(thr, depth, slot, "object");123124printf("\n test_local_object: END\n\n");125}126127static void128test_local_double(jthread thr, int depth, int slot) {129printf("\n test_local_double: BEGIN\n\n");130131test_int(thr, depth, slot, "double");132test_long(thr, depth, slot, "double");133test_float(thr, depth, slot, "double");134test_double(thr, depth, slot, "double");135test_object_type_mismatch(thr, depth, slot, "double");136137printf("\n test_local_double: END\n\n");138}139140static void141test_local_integer(jthread thr, int depth, int slot) {142printf("\n test_local_integer: BEGIN\n\n");143144test_int(thr, depth, slot, "int");145test_float(thr, depth, slot, "int");146test_object_type_mismatch(thr, depth, slot, "double");147148printf("\n test_local_integer: END\n\n");149}150151static void152test_local_invalid(jthread thr, int depth, int slot) {153printf("\n test_local_invalid: BEGIN\n\n");154155test_int_inv_slot(thr, depth, slot, "invalid");156test_long_inv_slot(thr, depth, slot, "invalid");157test_float_inv_slot(thr, depth, slot, "invalid");158test_double_inv_slot(thr, depth, slot, "invalid");159160printf("\n test_local_invalid: END\n\n");161}162163jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {164jint res;165jvmtiError err;166static jvmtiCapabilities caps;167168res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_9);169if (res != JNI_OK || jvmti == NULL) {170printf("Wrong result of a valid call to GetEnv!\n");171return JNI_ERR;172}173caps.can_access_local_variables = 1;174175err = jvmti->AddCapabilities(&caps);176if (err != JVMTI_ERROR_NONE) {177printf("AddCapabilities: unexpected error: %s (%d)\n", TranslateError(err), err);178return JNI_ERR;179}180err = jvmti->GetCapabilities(&caps);181if (err != JVMTI_ERROR_NONE) {182printf("GetCapabilities: unexpected error: %s (%d)\n", TranslateError(err), err);183return JNI_ERR;184}185if (!caps.can_access_local_variables) {186printf("Warning: Access to local variables is not implemented\n");187return JNI_ERR;188}189return JNI_OK;190}191192JNIEXPORT jint JNICALL193Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {194return Agent_Initialize(jvm, options, reserved);195}196197JNIEXPORT jint JNICALL198Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {199return Agent_Initialize(jvm, options, reserved);200}201202JNIEXPORT void JNICALL203Java_GetLocalVars_testLocals(JNIEnv *env, jclass cls, jobject thread) {204/*205* We test the JVMTI GetLocal<Type> for locals of the method:206*207* int staticMeth(byte byteArg, Object objArg, double dblArg, int intArg) {208* testLocals(Thread.currentThread());209* {210* int intLoc = 9999;211* intArg = intLoc;212* }213* return intArg;214* }215*/216static const char* METHOD_NAME = "staticMeth";217static const char* METHOD_SIGN = "(BLjava/lang/Object;DI)I";218static const int Depth = 1;219static const int ByteSlot = 0;220static const int ObjSlot = 1;221static const int DblSlot = 2;222static const int IntSlot = 4;223static const int InvalidSlot = 5;224225jmethodID mid = NULL;226227if (jvmti == NULL) {228printf("JVMTI client was not properly loaded!\n");229result = STATUS_FAILED;230return;231}232233mid = env->GetStaticMethodID(cls, METHOD_NAME, METHOD_SIGN);234if (mid == NULL) {235printf("Cannot find Method ID for %s%s\n", METHOD_NAME, METHOD_SIGN);236result = STATUS_FAILED;237return;238}239240test_local_byte(thread, Depth, ByteSlot);241test_local_object(thread, Depth, ObjSlot);242test_local_double(thread, Depth, DblSlot);243test_local_integer(thread, Depth, IntSlot);244test_local_invalid(thread, Depth, InvalidSlot);245}246247JNIEXPORT jint JNICALL248Java_GetLocalVars_getStatus(JNIEnv *env, jclass cls) {249return result;250}251252#ifdef __cplusplus253}254#endif255256257