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   char err_buf[200];
 217   struct ps_prochandle* ph;
 218   if ( (ph = Pgrab(jpid, err_buf, sizeof(err_buf))) == NULL) {
 219     char msg[230];
 220     snprintf(msg, sizeof(msg), "Can't attach to the process: %s", err_buf);
 221     THROW_NEW_DEBUGGER_EXCEPTION(msg);
 222   }
 223   (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
 224   fillThreadsAndLoadObjects(env, this_obj, ph);
 225 }
 226 
 227 /*
 228  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 229  * Method:    attach0
 230  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 231  */
 232 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
 233   (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) {
 234   const char *execName_cstr;
 235   const char *coreName_cstr;
 236   jboolean isCopy;
 237   struct ps_prochandle* ph;
 238 
 239   execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
 240   CHECK_EXCEPTION;
 241   coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
 242   CHECK_EXCEPTION;
 243 
 244   verifyBitness(env, execName_cstr);
 245   CHECK_EXCEPTION;
 246 
 247   if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
 248     (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
 249     (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
 250     THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
 251   }
 252   (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
 253   (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
 254   (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
 255   fillThreadsAndLoadObjects(env, this_obj, ph);
 256 }
 257 
 258 /*
 259  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 260  * Method:    detach0
 261  * Signature: ()V
 262  */
 263 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_detach0
 264   (JNIEnv *env, jobject this_obj) {
 265   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 266   if (ph != NULL) {
 267      Prelease(ph);
 268   }
 269 }
 270 
 271 /*
 272  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 273  * Method:    lookupByName0
 274  * Signature: (Ljava/lang/String;Ljava/lang/String;)J
 275  */
 276 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByName0
 277   (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
 278   const char *objectName_cstr, *symbolName_cstr;
 279   jlong addr;
 280   jboolean isCopy;
 281   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 282 
 283   objectName_cstr = NULL;
 284   if (objectName != NULL) {
 285     objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
 286     CHECK_EXCEPTION_(0);
 287   }
 288   symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
 289   CHECK_EXCEPTION_(0);
 290 
 291   addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
 292 
 293   if (objectName_cstr != NULL) {
 294     (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
 295   }
 296   (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
 297   return addr;
 298 }
 299 
 300 /*
 301  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 302  * Method:    lookupByAddress0
 303  * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
 304  */
 305 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByAddress0
 306   (JNIEnv *env, jobject this_obj, jlong addr) {
 307   uintptr_t offset;
 308   const char* sym = NULL;
 309 
 310   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 311   sym = symbol_for_pc(ph, (uintptr_t) addr, &offset);
 312   if (sym == NULL) return 0;
 313   return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID,
 314                           (*env)->NewStringUTF(env, sym), (jlong)offset);
 315 }
 316 
 317 /*
 318  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 319  * Method:    readBytesFromProcess0
 320  * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
 321  */
 322 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_readBytesFromProcess0
 323   (JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) {
 324 
 325   jboolean isCopy;
 326   jbyteArray array;
 327   jbyte *bufPtr;
 328   ps_err_e err;
 329 
 330   array = (*env)->NewByteArray(env, numBytes);
 331   CHECK_EXCEPTION_(0);
 332   bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
 333   CHECK_EXCEPTION_(0);
 334 
 335   err = ps_pdread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes);
 336   (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
 337   return (err == PS_OK)? array : 0;
 338 }
 339 
 340 #if defined(i386) || defined(amd64) || defined(sparc) || defined(sparcv9) || defined(aarch64)
 341 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getThreadIntegerRegisterSet0
 342   (JNIEnv *env, jobject this_obj, jint lwp_id) {
 343 
 344   struct user_regs_struct gregs;
 345   jboolean isCopy;
 346   jlongArray array;
 347   jlong *regs;
 348   int i;
 349 
 350   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 351   if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
 352      THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0);
 353   }
 354 
 355 #undef NPRGREG
 356 #ifdef i386
 357 #define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG
 358 #endif
 359 #ifdef amd64
 360 #define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
 361 #endif
 362 #ifdef aarch64
 363 #define NPRGREG sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_NPRGREG
 364 #endif
 365 #if defined(sparc) || defined(sparcv9)
 366 #define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG
 367 #endif
 368 
 369   array = (*env)->NewLongArray(env, NPRGREG);
 370   CHECK_EXCEPTION_(0);
 371   regs = (*env)->GetLongArrayElements(env, array, &isCopy);
 372 
 373 #undef REG_INDEX
 374 
 375 #ifdef i386
 376 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg
 377 
 378   regs[REG_INDEX(GS)]  = (uintptr_t) gregs.xgs;
 379   regs[REG_INDEX(FS)]  = (uintptr_t) gregs.xfs;
 380   regs[REG_INDEX(ES)]  = (uintptr_t) gregs.xes;
 381   regs[REG_INDEX(DS)]  = (uintptr_t) gregs.xds;
 382   regs[REG_INDEX(EDI)] = (uintptr_t) gregs.edi;
 383   regs[REG_INDEX(ESI)] = (uintptr_t) gregs.esi;
 384   regs[REG_INDEX(FP)] = (uintptr_t) gregs.ebp;
 385   regs[REG_INDEX(SP)] = (uintptr_t) gregs.esp;
 386   regs[REG_INDEX(EBX)] = (uintptr_t) gregs.ebx;
 387   regs[REG_INDEX(EDX)] = (uintptr_t) gregs.edx;
 388   regs[REG_INDEX(ECX)] = (uintptr_t) gregs.ecx;
 389   regs[REG_INDEX(EAX)] = (uintptr_t) gregs.eax;
 390   regs[REG_INDEX(PC)] = (uintptr_t) gregs.eip;
 391   regs[REG_INDEX(CS)]  = (uintptr_t) gregs.xcs;
 392   regs[REG_INDEX(SS)]  = (uintptr_t) gregs.xss;
 393 
 394 #endif /* i386 */
 395 
 396 #ifdef amd64
 397 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
 398 
 399   regs[REG_INDEX(R15)] = gregs.r15;
 400   regs[REG_INDEX(R14)] = gregs.r14;
 401   regs[REG_INDEX(R13)] = gregs.r13;
 402   regs[REG_INDEX(R12)] = gregs.r12;
 403   regs[REG_INDEX(RBP)] = gregs.rbp;
 404   regs[REG_INDEX(RBX)] = gregs.rbx;
 405   regs[REG_INDEX(R11)] = gregs.r11;
 406   regs[REG_INDEX(R10)] = gregs.r10;
 407   regs[REG_INDEX(R9)] = gregs.r9;
 408   regs[REG_INDEX(R8)] = gregs.r8;
 409   regs[REG_INDEX(RAX)] = gregs.rax;
 410   regs[REG_INDEX(RCX)] = gregs.rcx;
 411   regs[REG_INDEX(RDX)] = gregs.rdx;
 412   regs[REG_INDEX(RSI)] = gregs.rsi;
 413   regs[REG_INDEX(RDI)] = gregs.rdi;
 414   regs[REG_INDEX(RIP)] = gregs.rip;
 415   regs[REG_INDEX(CS)] = gregs.cs;
 416   regs[REG_INDEX(RSP)] = gregs.rsp;
 417   regs[REG_INDEX(SS)] = gregs.ss;
 418   regs[REG_INDEX(FSBASE)] = gregs.fs_base;
 419   regs[REG_INDEX(GSBASE)] = gregs.gs_base;
 420   regs[REG_INDEX(DS)] = gregs.ds;
 421   regs[REG_INDEX(ES)] = gregs.es;
 422   regs[REG_INDEX(FS)] = gregs.fs;
 423   regs[REG_INDEX(GS)] = gregs.gs;
 424 
 425 #endif /* amd64 */
 426 
 427 #if defined(sparc) || defined(sparcv9)
 428 
 429 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg
 430 
 431 #ifdef _LP64
 432   regs[REG_INDEX(R_PSR)] = gregs.tstate;
 433   regs[REG_INDEX(R_PC)]  = gregs.tpc;
 434   regs[REG_INDEX(R_nPC)] = gregs.tnpc;
 435   regs[REG_INDEX(R_Y)]   = gregs.y;
 436 #else
 437   regs[REG_INDEX(R_PSR)] = gregs.psr;
 438   regs[REG_INDEX(R_PC)]  = gregs.pc;
 439   regs[REG_INDEX(R_nPC)] = gregs.npc;
 440   regs[REG_INDEX(R_Y)]   = gregs.y;
 441 #endif
 442   regs[REG_INDEX(R_G0)]  =            0 ;
 443   regs[REG_INDEX(R_G1)]  = gregs.u_regs[0];
 444   regs[REG_INDEX(R_G2)]  = gregs.u_regs[1];
 445   regs[REG_INDEX(R_G3)]  = gregs.u_regs[2];
 446   regs[REG_INDEX(R_G4)]  = gregs.u_regs[3];
 447   regs[REG_INDEX(R_G5)]  = gregs.u_regs[4];
 448   regs[REG_INDEX(R_G6)]  = gregs.u_regs[5];
 449   regs[REG_INDEX(R_G7)]  = gregs.u_regs[6];
 450   regs[REG_INDEX(R_O0)]  = gregs.u_regs[7];
 451   regs[REG_INDEX(R_O1)]  = gregs.u_regs[8];
 452   regs[REG_INDEX(R_O2)]  = gregs.u_regs[ 9];
 453   regs[REG_INDEX(R_O3)]  = gregs.u_regs[10];
 454   regs[REG_INDEX(R_O4)]  = gregs.u_regs[11];
 455   regs[REG_INDEX(R_O5)]  = gregs.u_regs[12];
 456   regs[REG_INDEX(R_O6)]  = gregs.u_regs[13];
 457   regs[REG_INDEX(R_O7)]  = gregs.u_regs[14];
 458 #endif /* sparc */
 459 
 460 #if defined(aarch64)
 461 
 462 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_##reg
 463 
 464   {
 465     int i;
 466     for (i = 0; i < 31; i++)
 467       regs[i] = gregs.regs[i];
 468     regs[REG_INDEX(SP)] = gregs.sp;
 469     regs[REG_INDEX(PC)] = gregs.pc;
 470   }
 471 #endif /* aarch64 */
 472 
 473 
 474   (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
 475   return array;
 476 }
 477 #endif