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 jvmtiEventCallbacks callbacks; 43 static jint result = PASSED; 44 static jboolean printdump = JNI_FALSE; 45 static jmethodID mid = NULL; 46 static jvmtiLocalVariableEntry *table = NULL; 47 static jint entryCount = 0; 48 static jint methodExitCnt = -1; 49 50 void print_LocalVariableEntry(jvmtiLocalVariableEntry *lvt_elem) { 51 printf("\n Var name: %s, slot: %d", lvt_elem->name, lvt_elem->slot); 52 printf(", start_bci: %" LL "d", lvt_elem->start_location); 53 printf(", end_bci: %" LL "d", lvt_elem->start_location + lvt_elem->length); 54 printf(", signature: %s\n", lvt_elem->signature); 55 } 56 57 void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env, 58 jthread thr, jmethodID method, 59 jboolean was_poped_by_exception, jvalue return_value) { 60 jvmtiError err; 61 jint i; 62 jmethodID frame_method; 63 jlocation location; 64 jint intVal; 65 jfloat floatVal; 66 jdouble doubleVal; 67 jobject obj; 68 69 if (mid == method) { 70 71 err = jvmti_env->GetFrameLocation(thr, 0, 72 &frame_method, &location); 73 if (err != JVMTI_ERROR_NONE) { 74 printf("\t failure: %s (%d)\n", TranslateError(err), err); 75 result = STATUS_FAILED; 76 return; 77 } 78 if (frame_method != method) { 79 printf("\t failure: GetFrameLocation returned wrong jmethodID\n"); 80 result = STATUS_FAILED; 81 return; 82 } 83 84 printf("\n MethodExit: BEGIN %d, Current frame bci: %" LL "d\n\n", 85 ++methodExitCnt, location); 86 for (i = 0; i < entryCount; i++) { 87 if (table[i].start_location > location || 88 table[i].start_location + table[i].length < location) { 89 continue; /* The local variable is not visible */ 90 } 91 print_LocalVariableEntry(&table[i]); 92 93 err = jvmti->GetLocalInt(thr, 0, table[i].slot, &intVal); 94 printf(" GetLocalInt: %s (%d)\n", TranslateError(err), err); 95 if (err != JVMTI_ERROR_NONE && table[i].signature[0] == 'I') { 96 result = STATUS_FAILED; 97 } 98 99 err = jvmti->GetLocalFloat(thr, 0, table[i].slot, &floatVal); 100 printf(" GetLocalFloat: %s (%d)\n", TranslateError(err), err); 101 if (err != JVMTI_ERROR_NONE && table[i].signature[0] == 'F') { 102 result = STATUS_FAILED; 103 } 104 105 err = jvmti->GetLocalDouble(thr, 0, table[i].slot, &doubleVal); 106 printf(" GetLocalDouble: %s (%d)\n", TranslateError(err), err); 107 if (err != JVMTI_ERROR_NONE && table[i].signature[0] == 'D') { 108 result = STATUS_FAILED; 109 } 110 111 err = jvmti->GetLocalObject(thr, 0, table[i].slot, &obj); 112 printf(" GetLocalObject: %s (%d)\n", TranslateError(err), err); 113 if (err != JVMTI_ERROR_NONE && table[i].signature[0] == 'L') { 114 result = STATUS_FAILED; 115 } 116 } 117 printf("\n MethodExit: END %d\n\n", methodExitCnt); 118 fflush(stdout); 119 } 120 } 121 122 #ifdef STATIC_BUILD 123 JNIEXPORT jint JNICALL Agent_OnLoad_getlocal003(JavaVM *jvm, char *options, void *reserved) { 124 return Agent_Initialize(jvm, options, reserved); 125 } 126 JNIEXPORT jint JNICALL Agent_OnAttach_getlocal003(JavaVM *jvm, char *options, void *reserved) { 127 return Agent_Initialize(jvm, options, reserved); 128 } 129 JNIEXPORT jint JNI_OnLoad_getlocal003(JavaVM *jvm, char *options, void *reserved) { 130 return JNI_VERSION_1_8; 131 } 132 #endif 133 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 134 jint res; 135 jvmtiError err; 136 137 if (options != NULL && strcmp(options, "printdump") == 0) { 138 printdump = JNI_TRUE; 139 } 140 141 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1); 142 if (res != JNI_OK || jvmti == NULL) { 143 printf("Wrong result of a valid call to GetEnv!\n"); 144 return JNI_ERR; 145 } 146 147 err = jvmti->GetPotentialCapabilities(&caps); 148 if (err != JVMTI_ERROR_NONE) { 149 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n", 150 TranslateError(err), err); 151 return JNI_ERR; 152 } 153 154 err = jvmti->AddCapabilities(&caps); 155 if (err != JVMTI_ERROR_NONE) { 156 printf("(AddCapabilities) unexpected error: %s (%d)\n", 157 TranslateError(err), err); 158 return JNI_ERR; 159 } 160 161 err = jvmti->GetCapabilities(&caps); 162 if (err != JVMTI_ERROR_NONE) { 163 printf("(GetCapabilities) unexpected error: %s (%d)\n", 164 TranslateError(err), err); 165 return JNI_ERR; 166 } 167 168 if (!caps.can_access_local_variables) { 169 printf("Warning: Access to local variables is not implemented\n"); 170 } else if (caps.can_generate_method_exit_events) { 171 callbacks.MethodExit = &MethodExit; 172 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); 173 if (err != JVMTI_ERROR_NONE) { 174 printf("(SetEventCallbacks) unexpected error: %s (%d)\n", 175 TranslateError(err), err); 176 return JNI_ERR; 177 } 178 } else { 179 printf("Warning: MethodExit event is not implemented\n"); 180 } 181 182 return JNI_OK; 183 } 184 185 JNIEXPORT void JNICALL 186 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_getMeth(JNIEnv *env, jclass cls) { 187 jvmtiError err; 188 189 if (jvmti == NULL) { 190 printf("JVMTI client was not properly loaded!\n"); 191 result = STATUS_FAILED; 192 return; 193 } 194 195 if (!caps.can_access_local_variables || 196 !caps.can_generate_method_exit_events) return; 197 198 mid = env->GetStaticMethodID(cls, "staticMeth", "(I)I"); 199 if (mid == NULL) { 200 printf("Cannot find Method ID for staticMeth\n"); 201 result = STATUS_FAILED; 202 return; 203 } 204 205 err = jvmti->GetLocalVariableTable(mid, &entryCount, &table); 206 if (err != JVMTI_ERROR_NONE) { 207 printf("(GetLocalVariableTable) unexpected error: %s (%d)\n", 208 TranslateError(err), err); 209 result = STATUS_FAILED; 210 return; 211 } 212 213 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, 214 JVMTI_EVENT_METHOD_EXIT, NULL); 215 if (err != JVMTI_ERROR_NONE) { 216 printf("Failed to enable metod exit event: %s (%d)\n", 217 TranslateError(err), err); 218 result = STATUS_FAILED; 219 } 220 fflush(stdout); 221 } 222 223 JNIEXPORT void JNICALL 224 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_checkLoc(JNIEnv *env, 225 jclass cls, jthread thr) { 226 jvmtiError err; 227 jvmtiLocalVariableEntry *table; 228 jint entryCount; 229 jmethodID mid; 230 jint locVar; 231 jint i, j; 232 int overlap = 0; 233 234 if (jvmti == NULL) { 235 return; 236 } 237 238 mid = env->GetStaticMethodID(cls, "staticMeth", "(I)I"); 239 if (mid == NULL) { 240 printf("Cannot find Method ID for staticMeth\n"); 241 result = STATUS_FAILED; 242 return; 243 } 244 245 err = jvmti->GetLocalVariableTable(mid, &entryCount, &table); 246 if (err != JVMTI_ERROR_NONE) { 247 printf("(GetLocalVariableTable) unexpected error: %s (%d)\n", 248 TranslateError(err), err); 249 result = STATUS_FAILED; 250 return; 251 } 252 253 for (i = 0; i < entryCount; i++) { 254 print_LocalVariableEntry(&table[i]); 255 256 err = jvmti->GetLocalInt(thr, 1, table[i].slot, &locVar); 257 258 printf(" GetLocalInt: %s (%d)\n", TranslateError(err), err); 259 if (strcmp(table[i].name, "intArg") == 0) { 260 if (err != JVMTI_ERROR_NONE) { 261 printf(" failure: JVMTI_ERROR_NONE is expected\n"); 262 result = STATUS_FAILED; 263 } 264 } 265 else if (strcmp(table[i].name, "pi") == 0) { 266 if (err != JVMTI_ERROR_TYPE_MISMATCH) { 267 printf(" failure: JVMTI_ERROR_TYPE_MISMATCH is expected\n"); 268 result = STATUS_FAILED; 269 } 270 } else { 271 if (err != JVMTI_ERROR_INVALID_SLOT) { 272 printf(" failure: JVMTI_ERROR_INVALID_SLOT is expected\n"); 273 result = STATUS_FAILED; 274 } 275 } 276 if (table[i].slot != 2) { 277 continue; 278 } 279 280 for (j = 0; j < entryCount; j++) { 281 /* We do cross checks between all variables having slot #2. 282 * No overlapping between location ranges are allowed. 283 */ 284 if (table[j].slot != 2 || i == j) { 285 continue; 286 } 287 if (table[i].start_location > table[j].start_location + table[j].length || 288 table[j].start_location > table[i].start_location + table[i].length 289 ) { 290 continue; /* Everything is Ok */ 291 } 292 293 printf(" failure: locations of vars with slot #2 are overlaped:\n"); 294 print_LocalVariableEntry(&table[i]); 295 print_LocalVariableEntry(&table[j]); 296 overlap++; 297 result = STATUS_FAILED; 298 } 299 } 300 if (!overlap) { 301 printf("\n Succes: locations of vars with slot #2 are NOT overlaped\n\n"); 302 } 303 fflush(stdout); 304 } 305 306 JNIEXPORT jint JNICALL 307 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_getRes(JNIEnv *env, jclass cls) { 308 return result; 309 } 310 311 }