1 /* 2 * Copyright (c) 2003, 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 /* 25 */ 26 27 #include <stdio.h> 28 #include <string.h> 29 #include "jvmti.h" 30 #include "jni_tools.h" 31 #include "agent_common.h" 32 #include "JVMTITools.h" 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 #ifndef JNI_ENV_ARG 39 40 #ifdef __cplusplus 41 #define JNI_ENV_ARG(x, y) y 42 #define JNI_ENV_PTR(x) x 43 #else 44 #define JNI_ENV_ARG(x,y) x, y 45 #define JNI_ENV_PTR(x) (*x) 46 #endif 47 48 #endif 49 50 #define PASSED 0 51 #define STATUS_FAILED 2 52 53 static jvmtiEnv *jvmti = NULL; 54 static jvmtiCapabilities caps; 55 static jint result = PASSED; 56 static jboolean printdump = JNI_FALSE; 57 static jmethodID mid = NULL; 58 59 void print_LocalVariableEntry(jvmtiLocalVariableEntry *lvt_elem) { 60 printf("\n Var name: %s, slot: %d", lvt_elem->name, lvt_elem->slot); 61 printf(", start_bci: %" LL "d", lvt_elem->start_location); 62 printf(", end_bci: %" LL "d", lvt_elem->start_location + lvt_elem->length); 63 printf(", signature: %s\n", lvt_elem->signature); 64 } 65 66 #ifdef STATIC_BUILD 67 JNIEXPORT jint JNICALL Agent_OnLoad_getlocal004(JavaVM *jvm, char *options, void *reserved) { 68 return Agent_Initialize(jvm, options, reserved); 69 } 70 JNIEXPORT jint JNICALL Agent_OnAttach_getlocal004(JavaVM *jvm, char *options, void *reserved) { 71 return Agent_Initialize(jvm, options, reserved); 72 } 73 JNIEXPORT jint JNI_OnLoad_getlocal004(JavaVM *jvm, char *options, void *reserved) { 74 return JNI_VERSION_1_8; 75 } 76 #endif 77 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 78 jint res; 79 jvmtiError err; 80 81 if (options != NULL && strcmp(options, "printdump") == 0) { 82 printdump = JNI_TRUE; 83 } 84 85 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), 86 JVMTI_VERSION_1_1); 87 if (res != JNI_OK || jvmti == NULL) { 88 printf("Wrong result of a valid call to GetEnv!\n"); 89 return JNI_ERR; 90 } 91 92 err = jvmti->GetPotentialCapabilities(&caps); 93 if (err != JVMTI_ERROR_NONE) { 94 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n", 95 TranslateError(err), err); 96 return JNI_ERR; 97 } 98 99 err = jvmti->AddCapabilities(&caps); 100 if (err != JVMTI_ERROR_NONE) { 101 printf("(AddCapabilities) unexpected error: %s (%d)\n", 102 TranslateError(err), err); 103 return JNI_ERR; 104 } 105 106 err = jvmti->GetCapabilities(&caps); 107 if (err != JVMTI_ERROR_NONE) { 108 printf("(GetCapabilities) unexpected error: %s (%d)\n", 109 TranslateError(err), err); 110 return JNI_ERR; 111 } 112 113 if (!caps.can_access_local_variables) { 114 printf("Warning: Access to local variables is not implemented\n"); 115 } 116 117 return JNI_OK; 118 } 119 120 JNIEXPORT void JNICALL 121 Java_nsk_jvmti_unit_GetLocalVariable_getlocal004_getMeth(JNIEnv *env, jclass cls) { 122 if (jvmti == NULL) { 123 printf("JVMTI client was not properly loaded!\n"); 124 result = STATUS_FAILED; 125 return; 126 } 127 128 if (!caps.can_access_local_variables || 129 !caps.can_generate_method_exit_events) return; 130 131 mid = JNI_ENV_PTR(env)->GetStaticMethodID(JNI_ENV_ARG(env, cls), 132 "staticMeth", "(I)I"); 133 if (mid == NULL) { 134 printf("Cannot find Method ID for staticMeth\n"); 135 result = STATUS_FAILED; 136 return; 137 } 138 139 fflush(stdout); 140 } 141 142 void checkErrorCodeIn(jvmtiError err, jint slot) { 143 if (err != JVMTI_ERROR_NONE) { 144 printf(" FAILURE: JVMTI_ERROR_NONE is expected, slot: %d\n\n", slot); 145 result = STATUS_FAILED; 146 } else { 147 printf(" success: JVMTI_ERROR_NONE as expected, slot: %d\n\n", slot); 148 } 149 } 150 151 void checkErrorCodeOut(jvmtiError err, jint slot) { 152 if (err != JVMTI_ERROR_INVALID_SLOT) { 153 printf(" FAILURE: JVMTI_ERROR_INVALID_SLOT is expected, slot: %d\n\n", slot); 154 result = STATUS_FAILED; 155 } else { 156 printf(" success: JVMTI_ERROR_INVALID_SLOT as expected, slot: %d\n\n", slot); 157 } 158 } 159 160 #define CHECK_ERROR_CODE(scope_no, err, slot) \ 161 if (scope_no == 1) { \ 162 checkErrorCodeOut(err, slot); \ 163 } else { \ 164 checkErrorCodeIn(err, slot); \ 165 } 166 167 JNIEXPORT void JNICALL 168 Java_nsk_jvmti_unit_GetLocalVariable_getlocal004_checkLoc(JNIEnv *env, 169 jclass cls, jthread thr, jint scope_no) { 170 jvmtiError err = JVMTI_ERROR_NONE; 171 jint slot = 0; 172 jint locInt = 0; 173 jlong locLong = 0L; 174 jdouble locDouble = 0.0f; 175 176 if (jvmti == NULL) { 177 return; 178 } 179 printf("\n ----------------- checkLoc: %d -----------------\n\n", scope_no); 180 181 /* Check for slots which has to be available in general */ 182 for (slot = 3; slot < 5; slot++) { 183 err = jvmti->GetLocalInt(thr, 1, slot, &locInt); 184 printf(" GetLocalInt: %s (%d)\n", TranslateError(err), err); 185 CHECK_ERROR_CODE(scope_no, err, slot); 186 187 if (err == JVMTI_ERROR_NONE) { 188 printf(" slot%d: %d\n", slot, locInt); 189 } 190 191 err = jvmti->GetLocalLong(thr, 1, slot, &locLong); 192 printf(" GetLocalLong: %s (%d)\n", TranslateError(err), err); 193 CHECK_ERROR_CODE(scope_no, err, slot); 194 195 err = jvmti->GetLocalDouble(thr, 1, slot, &locDouble); 196 printf(" GetLocalDouble: %s (%d)\n", TranslateError(err), err); 197 CHECK_ERROR_CODE(scope_no, err, slot); 198 } 199 200 /* Slot 5 is special: it's not for 64 bit values! */ 201 slot = 5; { 202 err = jvmti->GetLocalInt(thr, 1, slot, &locInt); 203 printf(" GetLocalInt: %s (%d)\n", TranslateError(err), err); 204 CHECK_ERROR_CODE(scope_no, err, slot); 205 206 if (err == JVMTI_ERROR_NONE) { 207 printf(" slot%d: %d\n", slot, locInt); 208 } 209 210 err = jvmti->GetLocalLong(thr, 1, slot, &locLong); 211 printf(" GetLocalLong: %s (%d)\n", TranslateError(err), err); 212 checkErrorCodeOut(err, slot); 213 214 err = jvmti->GetLocalDouble(thr, 1, slot, &locDouble); 215 printf(" GetLocalDouble: %s (%d)\n", TranslateError(err), err); 216 checkErrorCodeOut(err, slot); 217 } 218 219 /* Check for slots which has to be unavailable in general */ 220 for (slot = 6; slot < 8; slot++) { 221 err = jvmti->GetLocalInt(thr, 1, slot, &locInt); 222 printf(" GetLocalInt: %s (%d)\n", TranslateError(err), err); 223 checkErrorCodeOut(err, slot); 224 225 if (err == JVMTI_ERROR_NONE) { 226 printf(" slot%d: %d\n", slot, locInt); 227 } 228 229 err = jvmti->GetLocalLong(thr, 1, slot, &locLong); 230 printf(" GetLocalLong: %s (%d)\n", TranslateError(err), err); 231 checkErrorCodeOut(err, slot); 232 233 err = jvmti->GetLocalDouble(thr, 1, slot, &locDouble); 234 printf(" GetLocalDouble: %s (%d)\n", TranslateError(err), err); 235 checkErrorCodeOut(err, slot); 236 } 237 238 fflush(stdout); 239 } 240 241 JNIEXPORT jint JNICALL 242 Java_nsk_jvmti_unit_GetLocalVariable_getlocal004_getRes(JNIEnv *env, jclass cls) { 243 return result; 244 } 245 246 #ifdef __cplusplus 247 } 248 #endif