1 /* 2 * Copyright (c) 2002, 2013, 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 #include <jni.h> 26 #include "libproc.h" 27 28 #include <elf.h> 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <fcntl.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <limits.h> 35 36 #if defined(x86_64) && !defined(amd64) 37 #define amd64 1 38 #endif 39 40 #ifdef i386 41 #include "sun_jvm_hotspot_debugger_x86_X86ThreadContext.h" 42 #endif 43 44 #ifdef amd64 45 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h" 46 #endif 47 48 #if defined(sparc) || defined(sparcv9) 49 #include "sun_jvm_hotspot_debugger_sparc_SPARCThreadContext.h" 50 #endif 51 52 #ifdef aarch64 53 #include "sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext.h" 54 #endif 55 56 static jfieldID p_ps_prochandle_ID = 0; 57 static jfieldID threadList_ID = 0; 58 static jfieldID loadObjectList_ID = 0; 59 60 static jmethodID createClosestSymbol_ID = 0; 61 static jmethodID createLoadObject_ID = 0; 62 static jmethodID getThreadForThreadId_ID = 0; 63 static jmethodID listAdd_ID = 0; 64 65 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; } 66 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;} 67 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; } 68 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;} 69 70 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) { 71 (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg); 72 } 73 74 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) { 75 jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID); 76 return (struct ps_prochandle*)(intptr_t)ptr; 77 } 78 79 /* 80 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal 81 * Method: init0 82 * Signature: ()V 83 */ 84 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_init0 85 (JNIEnv *env, jclass cls) { 86 jclass listClass; 87 88 if (init_libproc(getenv("LIBSAPROC_DEBUG") != NULL) != true) { 89 THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc"); 90 } 91 92 // fields we use 93 p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J"); 94 CHECK_EXCEPTION; 95 threadList_ID = (*env)->GetFieldID(env, cls, "threadList", "Ljava/util/List;"); 96 CHECK_EXCEPTION; 97 loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;"); 98 CHECK_EXCEPTION; 99 100 // methods we use 101 createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol", 102 "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;"); 103 CHECK_EXCEPTION; 104 createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject", 105 "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;"); 106 CHECK_EXCEPTION; 107 getThreadForThreadId_ID = (*env)->GetMethodID(env, cls, "getThreadForThreadId", 108 "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;"); 109 CHECK_EXCEPTION; 110 // java.util.List method we call 111 listClass = (*env)->FindClass(env, "java/util/List"); 112 CHECK_EXCEPTION; 113 listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z"); 114 CHECK_EXCEPTION; 115 } 116 117 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getAddressSize 118 (JNIEnv *env, jclass cls) 119 { 120 #ifdef _LP64 121 return 8; 122 #else 123 return 4; 124 #endif 125 126 } 127 128 129 static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) { 130 int n = 0, i = 0; 131 132 // add threads 133 n = get_num_threads(ph); 134 for (i = 0; i < n; i++) { 135 jobject thread; 136 jobject threadList; 137 lwpid_t lwpid; 138 139 lwpid = get_lwp_id(ph, i); 140 thread = (*env)->CallObjectMethod(env, this_obj, getThreadForThreadId_ID, 141 (jlong)lwpid); 142 CHECK_EXCEPTION; 143 threadList = (*env)->GetObjectField(env, this_obj, threadList_ID); 144 CHECK_EXCEPTION; 145 (*env)->CallBooleanMethod(env, threadList, listAdd_ID, thread); 146 CHECK_EXCEPTION; 147 } 148 149 // add load objects 150 n = get_num_libs(ph); 151 for (i = 0; i < n; i++) { 152 uintptr_t base; 153 const char* name; 154 jobject loadObject; 155 jobject loadObjectList; 156 157 base = get_lib_base(ph, i); 158 name = get_lib_name(ph, i); 159 loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID, 160 (*env)->NewStringUTF(env, name), (jlong)0, (jlong)base); 161 CHECK_EXCEPTION; 162 loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID); 163 CHECK_EXCEPTION; 164 (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject); 165 CHECK_EXCEPTION; 166 } 167 } 168 169 170 /* 171 * Verify that a named ELF binary file (core or executable) has the same 172 * bitness as ourselves. 173 * Throw an exception if there is a mismatch or other problem. 174 * 175 * If we proceed using a mismatched debugger/debuggee, the best to hope 176 * for is a missing symbol, the worst is a crash searching for debug symbols. 177 */ 178 void verifyBitness(JNIEnv *env, const char *binaryName) { 179 int fd = open(binaryName, O_RDONLY); 180 if (fd < 0) { 181 THROW_NEW_DEBUGGER_EXCEPTION("cannot open binary file"); 182 } 183 unsigned char elf_ident[EI_NIDENT]; 184 int i = read(fd, &elf_ident, sizeof(elf_ident)); 185 close(fd); 186 187 if (i < 0) { 188 THROW_NEW_DEBUGGER_EXCEPTION("cannot read binary file"); 189 } 190 #ifndef _LP64 191 if (elf_ident[EI_CLASS] == ELFCLASS64) { 192 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use 64-bit java for debugger"); 193 } 194 #else 195 if (elf_ident[EI_CLASS] != ELFCLASS64) { 196 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger"); 197 } 198 #endif 199 } 200 201 202 /* 203 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal 204 * Method: attach0 205 * Signature: (I)V 206 */ 207 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__I 208 (JNIEnv *env, jobject this_obj, jint jpid) { 209 210 // For bitness checking, locate binary at /proc/jpid/exe 211 char buf[PATH_MAX]; 212 snprintf((char *) &buf, PATH_MAX, "/proc/%d/exe", jpid); 213 verifyBitness(env, (char *) &buf); 214 CHECK_EXCEPTION; 215 216 struct ps_prochandle* ph; 217 if ( (ph = Pgrab(jpid)) == NULL) { 218 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process"); 219 } 220 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph); 221 fillThreadsAndLoadObjects(env, this_obj, ph); 222 } 223 224 /* 225 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal 226 * Method: attach0 227 * Signature: (Ljava/lang/String;Ljava/lang/String;)V 228 */ 229 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 230 (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) { 231 const char *execName_cstr; 232 const char *coreName_cstr; 233 jboolean isCopy; 234 struct ps_prochandle* ph; 235 236 execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy); 237 CHECK_EXCEPTION; 238 coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy); 239 CHECK_EXCEPTION; 240 241 verifyBitness(env, execName_cstr); 242 CHECK_EXCEPTION; 243 244 if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) { 245 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr); 246 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr); 247 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file"); 248 } 249 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph); 250 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr); 251 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr); 252 fillThreadsAndLoadObjects(env, this_obj, ph); 253 } 254 255 /* 256 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal 257 * Method: detach0 258 * Signature: ()V 259 */ 260 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_detach0 261 (JNIEnv *env, jobject this_obj) { 262 struct ps_prochandle* ph = get_proc_handle(env, this_obj); 263 if (ph != NULL) { 264 Prelease(ph); 265 } 266 } 267 268 /* 269 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal 270 * Method: lookupByName0 271 * Signature: (Ljava/lang/String;Ljava/lang/String;)J 272 */ 273 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByName0 274 (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { 275 const char *objectName_cstr, *symbolName_cstr; 276 jlong addr; 277 jboolean isCopy; 278 struct ps_prochandle* ph = get_proc_handle(env, this_obj); 279 280 objectName_cstr = NULL; 281 if (objectName != NULL) { 282 objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy); 283 CHECK_EXCEPTION_(0); 284 } 285 symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy); 286 CHECK_EXCEPTION_(0); 287 288 addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr); 289 290 if (objectName_cstr != NULL) { 291 (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr); 292 } 293 (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr); 294 return addr; 295 } 296 297 /* 298 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal 299 * Method: lookupByAddress0 300 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol; 301 */ 302 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByAddress0 303 (JNIEnv *env, jobject this_obj, jlong addr) { 304 uintptr_t offset; 305 const char* sym = NULL; 306 307 struct ps_prochandle* ph = get_proc_handle(env, this_obj); 308 sym = symbol_for_pc(ph, (uintptr_t) addr, &offset); 309 if (sym == NULL) return 0; 310 return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID, 311 (*env)->NewStringUTF(env, sym), (jlong)offset); 312 } 313 314 /* 315 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal 316 * Method: readBytesFromProcess0 317 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult; 318 */ 319 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_readBytesFromProcess0 320 (JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) { 321 322 jboolean isCopy; 323 jbyteArray array; 324 jbyte *bufPtr; 325 ps_err_e err; 326 327 array = (*env)->NewByteArray(env, numBytes); 328 CHECK_EXCEPTION_(0); 329 bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy); 330 CHECK_EXCEPTION_(0); 331 332 err = ps_pdread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes); 333 (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0); 334 return (err == PS_OK)? array : 0; 335 } 336 337 #if defined(i386) || defined(amd64) || defined(sparc) || defined(sparcv9) || defined(aarch64) 338 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getThreadIntegerRegisterSet0 339 (JNIEnv *env, jobject this_obj, jint lwp_id) { 340 341 struct user_regs_struct gregs; 342 jboolean isCopy; 343 jlongArray array; 344 jlong *regs; 345 int i; 346 347 struct ps_prochandle* ph = get_proc_handle(env, this_obj); 348 if (get_lwp_regs(ph, lwp_id, &gregs) != true) { 349 THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0); 350 } 351 352 #undef NPRGREG 353 #ifdef i386 354 #define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG 355 #endif 356 #ifdef amd64 357 #define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG 358 #endif 359 #ifdef aarch64 360 #define NPRGREG sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_NPRGREG 361 #endif 362 #if defined(sparc) || defined(sparcv9) 363 #define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG 364 #endif 365 366 array = (*env)->NewLongArray(env, NPRGREG); 367 CHECK_EXCEPTION_(0); 368 regs = (*env)->GetLongArrayElements(env, array, &isCopy); 369 370 #undef REG_INDEX 371 372 #ifdef i386 373 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg 374 375 regs[REG_INDEX(GS)] = (uintptr_t) gregs.xgs; 376 regs[REG_INDEX(FS)] = (uintptr_t) gregs.xfs; 377 regs[REG_INDEX(ES)] = (uintptr_t) gregs.xes; 378 regs[REG_INDEX(DS)] = (uintptr_t) gregs.xds; 379 regs[REG_INDEX(EDI)] = (uintptr_t) gregs.edi; 380 regs[REG_INDEX(ESI)] = (uintptr_t) gregs.esi; 381 regs[REG_INDEX(FP)] = (uintptr_t) gregs.ebp; 382 regs[REG_INDEX(SP)] = (uintptr_t) gregs.esp; 383 regs[REG_INDEX(EBX)] = (uintptr_t) gregs.ebx; 384 regs[REG_INDEX(EDX)] = (uintptr_t) gregs.edx; 385 regs[REG_INDEX(ECX)] = (uintptr_t) gregs.ecx; 386 regs[REG_INDEX(EAX)] = (uintptr_t) gregs.eax; 387 regs[REG_INDEX(PC)] = (uintptr_t) gregs.eip; 388 regs[REG_INDEX(CS)] = (uintptr_t) gregs.xcs; 389 regs[REG_INDEX(SS)] = (uintptr_t) gregs.xss; 390 391 #endif /* i386 */ 392 393 #ifdef amd64 394 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg 395 396 regs[REG_INDEX(R15)] = gregs.r15; 397 regs[REG_INDEX(R14)] = gregs.r14; 398 regs[REG_INDEX(R13)] = gregs.r13; 399 regs[REG_INDEX(R12)] = gregs.r12; 400 regs[REG_INDEX(RBP)] = gregs.rbp; 401 regs[REG_INDEX(RBX)] = gregs.rbx; 402 regs[REG_INDEX(R11)] = gregs.r11; 403 regs[REG_INDEX(R10)] = gregs.r10; 404 regs[REG_INDEX(R9)] = gregs.r9; 405 regs[REG_INDEX(R8)] = gregs.r8; 406 regs[REG_INDEX(RAX)] = gregs.rax; 407 regs[REG_INDEX(RCX)] = gregs.rcx; 408 regs[REG_INDEX(RDX)] = gregs.rdx; 409 regs[REG_INDEX(RSI)] = gregs.rsi; 410 regs[REG_INDEX(RDI)] = gregs.rdi; 411 regs[REG_INDEX(RIP)] = gregs.rip; 412 regs[REG_INDEX(CS)] = gregs.cs; 413 regs[REG_INDEX(RSP)] = gregs.rsp; 414 regs[REG_INDEX(SS)] = gregs.ss; 415 regs[REG_INDEX(FSBASE)] = gregs.fs_base; 416 regs[REG_INDEX(GSBASE)] = gregs.gs_base; 417 regs[REG_INDEX(DS)] = gregs.ds; 418 regs[REG_INDEX(ES)] = gregs.es; 419 regs[REG_INDEX(FS)] = gregs.fs; 420 regs[REG_INDEX(GS)] = gregs.gs; 421 422 #endif /* amd64 */ 423 424 #if defined(sparc) || defined(sparcv9) 425 426 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg 427 428 #ifdef _LP64 429 regs[REG_INDEX(R_PSR)] = gregs.tstate; 430 regs[REG_INDEX(R_PC)] = gregs.tpc; 431 regs[REG_INDEX(R_nPC)] = gregs.tnpc; 432 regs[REG_INDEX(R_Y)] = gregs.y; 433 #else 434 regs[REG_INDEX(R_PSR)] = gregs.psr; 435 regs[REG_INDEX(R_PC)] = gregs.pc; 436 regs[REG_INDEX(R_nPC)] = gregs.npc; 437 regs[REG_INDEX(R_Y)] = gregs.y; 438 #endif 439 regs[REG_INDEX(R_G0)] = 0 ; 440 regs[REG_INDEX(R_G1)] = gregs.u_regs[0]; 441 regs[REG_INDEX(R_G2)] = gregs.u_regs[1]; 442 regs[REG_INDEX(R_G3)] = gregs.u_regs[2]; 443 regs[REG_INDEX(R_G4)] = gregs.u_regs[3]; 444 regs[REG_INDEX(R_G5)] = gregs.u_regs[4]; 445 regs[REG_INDEX(R_G6)] = gregs.u_regs[5]; 446 regs[REG_INDEX(R_G7)] = gregs.u_regs[6]; 447 regs[REG_INDEX(R_O0)] = gregs.u_regs[7]; 448 regs[REG_INDEX(R_O1)] = gregs.u_regs[8]; 449 regs[REG_INDEX(R_O2)] = gregs.u_regs[ 9]; 450 regs[REG_INDEX(R_O3)] = gregs.u_regs[10]; 451 regs[REG_INDEX(R_O4)] = gregs.u_regs[11]; 452 regs[REG_INDEX(R_O5)] = gregs.u_regs[12]; 453 regs[REG_INDEX(R_O6)] = gregs.u_regs[13]; 454 regs[REG_INDEX(R_O7)] = gregs.u_regs[14]; 455 #endif /* sparc */ 456 457 #if defined(aarch64) 458 459 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_##reg 460 461 { 462 int i; 463 for (i = 0; i < 31; i++) 464 regs[i] = gregs.regs[i]; 465 regs[REG_INDEX(SP)] = gregs.sp; 466 regs[REG_INDEX(PC)] = gregs.pc; 467 } 468 #endif /* aarch64 */ 469 470 471 (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT); 472 return array; 473 } 474 #endif