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