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 static jfieldID p_ps_prochandle_ID = 0;
  53 static jfieldID threadList_ID = 0;
  54 static jfieldID loadObjectList_ID = 0;
  55 
  56 static jmethodID createClosestSymbol_ID = 0;
  57 static jmethodID createLoadObject_ID = 0;
  58 static jmethodID getThreadForThreadId_ID = 0;
  59 static jmethodID listAdd_ID = 0;
  60 
  61 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
  62 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
  63 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; }
  64 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
  65 
  66 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
  67   (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
  68 }
  69 
  70 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) {
  71   jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID);
  72   return (struct ps_prochandle*)(intptr_t)ptr;
  73 }
  74 
  75 /*
  76  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
  77  * Method:    init0
  78  * Signature: ()V
  79  */
  80 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_init0
  81   (JNIEnv *env, jclass cls) {
  82   jclass listClass;
  83 
  84   if (init_libproc(getenv("LIBSAPROC_DEBUG") != NULL) != true) {
  85      THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc");
  86   }
  87 
  88   // fields we use
  89   p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J");
  90   CHECK_EXCEPTION;
  91   threadList_ID = (*env)->GetFieldID(env, cls, "threadList", "Ljava/util/List;");
  92   CHECK_EXCEPTION;
  93   loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;");
  94   CHECK_EXCEPTION;
  95 
  96   // methods we use
  97   createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol",
  98                     "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
  99   CHECK_EXCEPTION;
 100   createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject",
 101                     "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;");
 102   CHECK_EXCEPTION;
 103   getThreadForThreadId_ID = (*env)->GetMethodID(env, cls, "getThreadForThreadId",
 104                                                      "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;");
 105   CHECK_EXCEPTION;
 106   // java.util.List method we call
 107   listClass = (*env)->FindClass(env, "java/util/List");
 108   CHECK_EXCEPTION;
 109   listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z");
 110   CHECK_EXCEPTION;
 111 }
 112 
 113 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getAddressSize
 114   (JNIEnv *env, jclass cls)
 115 {
 116 #ifdef _LP64
 117  return 8;
 118 #else
 119  return 4;
 120 #endif
 121 
 122 }
 123 
 124 
 125 static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
 126   int n = 0, i = 0;
 127 
 128   // add threads
 129   n = get_num_threads(ph);
 130   for (i = 0; i < n; i++) {
 131     jobject thread;
 132     jobject threadList;
 133     lwpid_t lwpid;
 134 
 135     lwpid = get_lwp_id(ph, i);
 136     thread = (*env)->CallObjectMethod(env, this_obj, getThreadForThreadId_ID,
 137                                       (jlong)lwpid);
 138     CHECK_EXCEPTION;
 139     threadList = (*env)->GetObjectField(env, this_obj, threadList_ID);
 140     CHECK_EXCEPTION;
 141     (*env)->CallBooleanMethod(env, threadList, listAdd_ID, thread);
 142     CHECK_EXCEPTION;
 143   }
 144 
 145   // add load objects
 146   n = get_num_libs(ph);
 147   for (i = 0; i < n; i++) {
 148      uintptr_t base;
 149      const char* name;
 150      jobject loadObject;
 151      jobject loadObjectList;
 152 
 153      base = get_lib_base(ph, i);
 154      name = get_lib_name(ph, i);
 155      loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID,
 156                                    (*env)->NewStringUTF(env, name), (jlong)0, (jlong)base);
 157      CHECK_EXCEPTION;
 158      loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID);
 159      CHECK_EXCEPTION;
 160      (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
 161      CHECK_EXCEPTION;
 162   }
 163 }
 164 
 165 
 166 /*
 167  * Verify that a named ELF binary file (core or executable) has the same
 168  * bitness as ourselves.
 169  * Throw an exception if there is a mismatch or other problem.
 170  *
 171  * If we proceed using a mismatched debugger/debuggee, the best to hope
 172  * for is a missing symbol, the worst is a crash searching for debug symbols.
 173  */
 174 void verifyBitness(JNIEnv *env, const char *binaryName) {
 175   int fd = open(binaryName, O_RDONLY);
 176   if (fd < 0) {
 177     THROW_NEW_DEBUGGER_EXCEPTION("cannot open binary file");
 178   }
 179   unsigned char elf_ident[EI_NIDENT];
 180   int i = read(fd, &elf_ident, sizeof(elf_ident));
 181   close(fd);
 182 
 183   if (i < 0) {
 184     THROW_NEW_DEBUGGER_EXCEPTION("cannot read binary file");
 185   }
 186 #ifndef _LP64
 187   if (elf_ident[EI_CLASS] == ELFCLASS64) {
 188     THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use 64-bit java for debugger");
 189   }
 190 #else
 191   if (elf_ident[EI_CLASS] != ELFCLASS64) {
 192     THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger");
 193   }
 194 #endif
 195 }
 196 
 197 
 198 /*
 199  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 200  * Method:    attach0
 201  * Signature: (I)V
 202  */
 203 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__I
 204   (JNIEnv *env, jobject this_obj, jint jpid) {
 205 
 206   // For bitness checking, locate binary at /proc/jpid/exe
 207   char buf[PATH_MAX];
 208   snprintf((char *) &buf, PATH_MAX, "/proc/%d/exe", jpid);
 209   verifyBitness(env, (char *) &buf);
 210   CHECK_EXCEPTION;
 211 
 212   char err_buf[200];
 213   struct ps_prochandle* ph;
 214   if ( (ph = Pgrab(jpid, err_buf, sizeof(err_buf))) == NULL) {
 215     char msg[230];
 216     snprintf(msg, sizeof(msg), "Can't attach to the process: %s", err_buf);
 217     THROW_NEW_DEBUGGER_EXCEPTION(msg);
 218   }
 219   (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
 220   fillThreadsAndLoadObjects(env, this_obj, ph);
 221 }
 222 
 223 /*
 224  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 225  * Method:    attach0
 226  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 227  */
 228 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
 229   (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) {
 230   const char *execName_cstr;
 231   const char *coreName_cstr;
 232   jboolean isCopy;
 233   struct ps_prochandle* ph;
 234 
 235   execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
 236   CHECK_EXCEPTION;
 237   coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
 238   CHECK_EXCEPTION;
 239 
 240   verifyBitness(env, execName_cstr);
 241   CHECK_EXCEPTION;
 242 
 243   if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
 244     (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
 245     (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
 246     THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
 247   }
 248   (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
 249   (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
 250   (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
 251   fillThreadsAndLoadObjects(env, this_obj, ph);
 252 }
 253 
 254 /*
 255  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 256  * Method:    detach0
 257  * Signature: ()V
 258  */
 259 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_detach0
 260   (JNIEnv *env, jobject this_obj) {
 261   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 262   if (ph != NULL) {
 263      Prelease(ph);
 264   }
 265 }
 266 
 267 /*
 268  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 269  * Method:    lookupByName0
 270  * Signature: (Ljava/lang/String;Ljava/lang/String;)J
 271  */
 272 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByName0
 273   (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
 274   const char *objectName_cstr, *symbolName_cstr;
 275   jlong addr;
 276   jboolean isCopy;
 277   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 278 
 279   objectName_cstr = NULL;
 280   if (objectName != NULL) {
 281     objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
 282     CHECK_EXCEPTION_(0);
 283   }
 284   symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
 285   CHECK_EXCEPTION_(0);
 286 
 287   addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
 288 
 289   if (objectName_cstr != NULL) {
 290     (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
 291   }
 292   (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
 293   return addr;
 294 }
 295 
 296 /*
 297  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 298  * Method:    lookupByAddress0
 299  * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
 300  */
 301 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByAddress0
 302   (JNIEnv *env, jobject this_obj, jlong addr) {
 303   uintptr_t offset;
 304   const char* sym = NULL;
 305 
 306   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 307   sym = symbol_for_pc(ph, (uintptr_t) addr, &offset);
 308   if (sym == NULL) return 0;
 309   return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID,
 310                           (*env)->NewStringUTF(env, sym), (jlong)offset);
 311 }
 312 
 313 /*
 314  * Class:     sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
 315  * Method:    readBytesFromProcess0
 316  * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
 317  */
 318 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_readBytesFromProcess0
 319   (JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) {
 320 
 321   jboolean isCopy;
 322   jbyteArray array;
 323   jbyte *bufPtr;
 324   ps_err_e err;
 325 
 326   array = (*env)->NewByteArray(env, numBytes);
 327   CHECK_EXCEPTION_(0);
 328   bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
 329   CHECK_EXCEPTION_(0);
 330 
 331   err = ps_pdread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes);
 332   (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
 333   return (err == PS_OK)? array : 0;
 334 }
 335 
 336 #if defined(i386) || defined(amd64) || defined(sparc) || defined(sparcv9)
 337 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getThreadIntegerRegisterSet0
 338   (JNIEnv *env, jobject this_obj, jint lwp_id) {
 339 
 340   struct user_regs_struct gregs;
 341   jboolean isCopy;
 342   jlongArray array;
 343   jlong *regs;
 344   int i;
 345 
 346   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
 347   if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
 348      THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0);
 349   }
 350 
 351 #undef NPRGREG
 352 #ifdef i386
 353 #define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG
 354 #endif
 355 #ifdef amd64
 356 #define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
 357 #endif



 358 #if defined(sparc) || defined(sparcv9)
 359 #define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG
 360 #endif
 361 
 362   array = (*env)->NewLongArray(env, NPRGREG);
 363   CHECK_EXCEPTION_(0);
 364   regs = (*env)->GetLongArrayElements(env, array, &isCopy);
 365 
 366 #undef REG_INDEX
 367 
 368 #ifdef i386
 369 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg
 370 
 371   regs[REG_INDEX(GS)]  = (uintptr_t) gregs.xgs;
 372   regs[REG_INDEX(FS)]  = (uintptr_t) gregs.xfs;
 373   regs[REG_INDEX(ES)]  = (uintptr_t) gregs.xes;
 374   regs[REG_INDEX(DS)]  = (uintptr_t) gregs.xds;
 375   regs[REG_INDEX(EDI)] = (uintptr_t) gregs.edi;
 376   regs[REG_INDEX(ESI)] = (uintptr_t) gregs.esi;
 377   regs[REG_INDEX(FP)] = (uintptr_t) gregs.ebp;
 378   regs[REG_INDEX(SP)] = (uintptr_t) gregs.esp;
 379   regs[REG_INDEX(EBX)] = (uintptr_t) gregs.ebx;
 380   regs[REG_INDEX(EDX)] = (uintptr_t) gregs.edx;
 381   regs[REG_INDEX(ECX)] = (uintptr_t) gregs.ecx;
 382   regs[REG_INDEX(EAX)] = (uintptr_t) gregs.eax;
 383   regs[REG_INDEX(PC)] = (uintptr_t) gregs.eip;
 384   regs[REG_INDEX(CS)]  = (uintptr_t) gregs.xcs;
 385   regs[REG_INDEX(SS)]  = (uintptr_t) gregs.xss;
 386 
 387 #endif /* i386 */
 388 
 389 #ifdef amd64
 390 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
 391 
 392   regs[REG_INDEX(R15)] = gregs.r15;
 393   regs[REG_INDEX(R14)] = gregs.r14;
 394   regs[REG_INDEX(R13)] = gregs.r13;
 395   regs[REG_INDEX(R12)] = gregs.r12;
 396   regs[REG_INDEX(RBP)] = gregs.rbp;
 397   regs[REG_INDEX(RBX)] = gregs.rbx;
 398   regs[REG_INDEX(R11)] = gregs.r11;
 399   regs[REG_INDEX(R10)] = gregs.r10;
 400   regs[REG_INDEX(R9)] = gregs.r9;
 401   regs[REG_INDEX(R8)] = gregs.r8;
 402   regs[REG_INDEX(RAX)] = gregs.rax;
 403   regs[REG_INDEX(RCX)] = gregs.rcx;
 404   regs[REG_INDEX(RDX)] = gregs.rdx;
 405   regs[REG_INDEX(RSI)] = gregs.rsi;
 406   regs[REG_INDEX(RDI)] = gregs.rdi;
 407   regs[REG_INDEX(RIP)] = gregs.rip;
 408   regs[REG_INDEX(CS)] = gregs.cs;
 409   regs[REG_INDEX(RSP)] = gregs.rsp;
 410   regs[REG_INDEX(SS)] = gregs.ss;
 411   regs[REG_INDEX(FSBASE)] = gregs.fs_base;
 412   regs[REG_INDEX(GSBASE)] = gregs.gs_base;
 413   regs[REG_INDEX(DS)] = gregs.ds;
 414   regs[REG_INDEX(ES)] = gregs.es;
 415   regs[REG_INDEX(FS)] = gregs.fs;
 416   regs[REG_INDEX(GS)] = gregs.gs;
 417 
 418 #endif /* amd64 */
 419 
 420 #if defined(sparc) || defined(sparcv9)
 421 
 422 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg
 423 
 424 #ifdef _LP64
 425   regs[REG_INDEX(R_PSR)] = gregs.tstate;
 426   regs[REG_INDEX(R_PC)]  = gregs.tpc;
 427   regs[REG_INDEX(R_nPC)] = gregs.tnpc;
 428   regs[REG_INDEX(R_Y)]   = gregs.y;
 429 #else
 430   regs[REG_INDEX(R_PSR)] = gregs.psr;
 431   regs[REG_INDEX(R_PC)]  = gregs.pc;
 432   regs[REG_INDEX(R_nPC)] = gregs.npc;
 433   regs[REG_INDEX(R_Y)]   = gregs.y;
 434 #endif
 435   regs[REG_INDEX(R_G0)]  =            0 ;
 436   regs[REG_INDEX(R_G1)]  = gregs.u_regs[0];
 437   regs[REG_INDEX(R_G2)]  = gregs.u_regs[1];
 438   regs[REG_INDEX(R_G3)]  = gregs.u_regs[2];
 439   regs[REG_INDEX(R_G4)]  = gregs.u_regs[3];
 440   regs[REG_INDEX(R_G5)]  = gregs.u_regs[4];
 441   regs[REG_INDEX(R_G6)]  = gregs.u_regs[5];
 442   regs[REG_INDEX(R_G7)]  = gregs.u_regs[6];
 443   regs[REG_INDEX(R_O0)]  = gregs.u_regs[7];
 444   regs[REG_INDEX(R_O1)]  = gregs.u_regs[8];
 445   regs[REG_INDEX(R_O2)]  = gregs.u_regs[ 9];
 446   regs[REG_INDEX(R_O3)]  = gregs.u_regs[10];
 447   regs[REG_INDEX(R_O4)]  = gregs.u_regs[11];
 448   regs[REG_INDEX(R_O5)]  = gregs.u_regs[12];
 449   regs[REG_INDEX(R_O6)]  = gregs.u_regs[13];
 450   regs[REG_INDEX(R_O7)]  = gregs.u_regs[14];
 451 #endif /* sparc */













 452 
 453 
 454   (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
 455   return array;
 456 }
 457 #endif
--- EOF ---