1 /*
   2  * Copyright 2002-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "salibproc.h"
  26 #include "sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal.h"
  27 #include <thread_db.h>
  28 #include <strings.h>
  29 #include <limits.h>
  30 #include <demangle.h>
  31 #include <stdarg.h>
  32 #include <stdlib.h>
  33 #include <errno.h>
  34 
  35 #define CHECK_EXCEPTION_(value) if(env->ExceptionOccurred()) { return value; }
  36 #define CHECK_EXCEPTION if(env->ExceptionOccurred()) { return;}
  37 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throwNewDebuggerException(env, str); return value; }
  38 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throwNewDebuggerException(env, str); return;}
  39 
  40 #define SYMBOL_BUF_SIZE  256
  41 #define ERR_MSG_SIZE     (PATH_MAX + 256)
  42 
  43 // debug mode
  44 static int _libsaproc_debug = 0;
  45 
  46 static void print_debug(const char* format,...) {
  47   if (_libsaproc_debug) {
  48     va_list alist;
  49 
  50     va_start(alist, format);
  51     fputs("libsaproc DEBUG: ", stderr);
  52     vfprintf(stderr, format, alist);
  53     va_end(alist);
  54   }
  55 }
  56 
  57 struct Debugger {
  58     JNIEnv* env;
  59     jobject this_obj;
  60 };
  61 
  62 struct DebuggerWithObject : Debugger {
  63     jobject obj;
  64 };
  65 
  66 struct DebuggerWith2Objects : DebuggerWithObject {
  67     jobject obj2;
  68 };
  69 
  70 /*
  71 * Portions of user thread level detail gathering code is from pstack source
  72 * code. See pstack.c in Solaris 2.8 user commands source code.
  73 */
  74 
  75 static void throwNewDebuggerException(JNIEnv* env, const char* errMsg) {
  76   env->ThrowNew(env->FindClass("sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
  77 }
  78 
  79 // JNI ids for some fields, methods
  80 
  81 // libproc handler pointer
  82 static jfieldID p_ps_prochandle_ID = 0;
  83 
  84 // libthread.so dlopen handle, thread agent ptr and function pointers
  85 static jfieldID libthread_db_handle_ID   = 0;
  86 static jfieldID p_td_thragent_t_ID       = 0;
  87 static jfieldID p_td_init_ID             = 0;
  88 static jfieldID p_td_ta_new_ID           = 0;
  89 static jfieldID p_td_ta_delete_ID        = 0;
  90 static jfieldID p_td_ta_thr_iter_ID      = 0;
  91 static jfieldID p_td_thr_get_info_ID     = 0;
  92 static jfieldID p_td_ta_map_id2thr_ID    = 0;
  93 static jfieldID p_td_thr_getgregs_ID     = 0;
  94 
  95 // reg index fields
  96 static jfieldID pcRegIndex_ID            = 0;
  97 static jfieldID fpRegIndex_ID            = 0;
  98 
  99 // part of the class sharing workaround
 100 static jfieldID classes_jsa_fd_ID        = 0;
 101 static jfieldID p_file_map_header_ID     = 0;
 102 
 103 // method ids
 104 
 105 static jmethodID getThreadForThreadId_ID = 0;
 106 static jmethodID createSenderFrame_ID    = 0;
 107 static jmethodID createLoadObject_ID     = 0;
 108 static jmethodID createClosestSymbol_ID  = 0;
 109 static jmethodID listAdd_ID              = 0;
 110 
 111 /*
 112  * Functions we need from libthread_db
 113  */
 114 typedef td_err_e
 115         (*p_td_init_t)(void);
 116 typedef td_err_e
 117         (*p_td_ta_new_t)(void *, td_thragent_t **);
 118 typedef td_err_e
 119         (*p_td_ta_delete_t)(td_thragent_t *);
 120 typedef td_err_e
 121         (*p_td_ta_thr_iter_t)(const td_thragent_t *, td_thr_iter_f *, void *,
 122                 td_thr_state_e, int, sigset_t *, unsigned);
 123 typedef td_err_e
 124         (*p_td_thr_get_info_t)(const td_thrhandle_t *, td_thrinfo_t *);
 125 typedef td_err_e
 126         (*p_td_ta_map_id2thr_t)(const td_thragent_t *, thread_t,  td_thrhandle_t *);
 127 typedef td_err_e
 128         (*p_td_thr_getgregs_t)(const td_thrhandle_t *, prgregset_t);
 129 
 130 static void
 131 clear_libthread_db_ptrs(JNIEnv* env, jobject this_obj) {
 132   // release libthread_db agent, if we had created
 133   p_td_ta_delete_t p_td_ta_delete = 0;
 134   p_td_ta_delete = (p_td_ta_delete_t) env->GetLongField(this_obj, p_td_ta_delete_ID);
 135 
 136   td_thragent_t *p_td_thragent_t = 0;
 137   p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID);
 138   if (p_td_thragent_t != 0 && p_td_ta_delete != 0) {
 139      p_td_ta_delete(p_td_thragent_t);
 140   }
 141 
 142   // dlclose libthread_db.so
 143   void* libthread_db_handle = (void*) env->GetLongField(this_obj, libthread_db_handle_ID);
 144   if (libthread_db_handle != 0) {
 145     dlclose(libthread_db_handle);
 146   }
 147 
 148   env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)0);
 149   env->SetLongField(this_obj, p_td_init_ID, (jlong)0);
 150   env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)0);
 151   env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)0);
 152   env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)0);
 153   env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)0);
 154   env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)0);
 155   env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)0);
 156 }
 157 
 158 
 159 static void detach_internal(JNIEnv* env, jobject this_obj) {
 160   // clear libthread_db stuff
 161   clear_libthread_db_ptrs(env, this_obj);
 162 
 163   // release ptr to ps_prochandle
 164   jlong p_ps_prochandle;
 165   p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
 166   if (p_ps_prochandle != 0L) {
 167     Prelease((struct ps_prochandle*) p_ps_prochandle, PRELEASE_CLEAR);
 168   }
 169 
 170   // part of the class sharing workaround
 171   int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID);
 172   if (classes_jsa_fd != -1) {
 173     close(classes_jsa_fd);
 174     struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
 175     if (pheader != NULL) {
 176       free(pheader);
 177     }
 178   }
 179 }
 180 
 181 // Is it okay to ignore libthread_db failure? Set env var to ignore
 182 // libthread_db failure. You can still debug, but will miss threads
 183 // related functionality.
 184 static bool sa_ignore_threaddb = (getenv("SA_IGNORE_THREADDB") != 0);
 185 
 186 #define HANDLE_THREADDB_FAILURE(msg)          \
 187   if (sa_ignore_threaddb) {                   \
 188      printf("libsaproc WARNING: %s\n", msg);  \
 189      return;                                  \
 190   } else {                                    \
 191      THROW_NEW_DEBUGGER_EXCEPTION(msg);       \
 192   }
 193 
 194 #define HANDLE_THREADDB_FAILURE_(msg, ret)    \
 195   if (sa_ignore_threaddb) {                   \
 196      printf("libsaproc WARNING: %s\n", msg);  \
 197      return ret;                              \
 198   } else {                                    \
 199      THROW_NEW_DEBUGGER_EXCEPTION_(msg, ret); \
 200   }
 201 
 202 static const char * alt_root = NULL;
 203 static int alt_root_len = -1;
 204 
 205 #define SA_ALTROOT "SA_ALTROOT"
 206 
 207 static void init_alt_root() {
 208   if (alt_root_len == -1) {
 209     alt_root = getenv(SA_ALTROOT);
 210     if (alt_root)
 211       alt_root_len = strlen(alt_root);
 212     else
 213       alt_root_len = 0;
 214   }
 215 }
 216 
 217 // This function is a complete substitute for the open system call
 218 // since it's also used to override open calls from libproc to
 219 // implement as a pathmap style facility for the SA.  If libproc
 220 // starts using other interfaces then this might have to extended to
 221 // cover other calls.
 222 extern "C" int libsaproc_open(const char * name, int oflag, ...) {
 223   if (oflag == O_RDONLY) {
 224     init_alt_root();
 225 
 226     if (_libsaproc_debug) {
 227       printf("libsaproc DEBUG: libsaproc_open %s\n", name);
 228     }
 229 
 230     if (alt_root_len > 0) {
 231       int fd = -1;
 232       char alt_path[PATH_MAX+1];
 233 
 234       strcpy(alt_path, alt_root);
 235       strcat(alt_path, name);
 236       fd = open(alt_path, O_RDONLY);
 237       if (fd >= 0) {
 238         if (_libsaproc_debug) {
 239           printf("libsaproc DEBUG: libsaproc_open substituted %s\n", alt_path);
 240         }
 241         return fd;
 242       }
 243 
 244       if (strrchr(name, '/')) {
 245         strcpy(alt_path, alt_root);
 246         strcat(alt_path, strrchr(name, '/'));
 247         fd = open(alt_path, O_RDONLY);
 248         if (fd >= 0) {
 249           if (_libsaproc_debug) {
 250             printf("libsaproc DEBUG: libsaproc_open substituted %s\n", alt_path);
 251           }
 252           return fd;
 253         }
 254       }
 255     }
 256   }
 257 
 258   {
 259     mode_t mode;
 260     va_list ap;
 261     va_start(ap, oflag);
 262     mode = va_arg(ap, mode_t);
 263     va_end(ap);
 264 
 265     return open(name, oflag, mode);
 266   }
 267 }
 268 
 269 
 270 static void * pathmap_dlopen(const char * name, int mode) {
 271   init_alt_root();
 272 
 273   if (_libsaproc_debug) {
 274     printf("libsaproc DEBUG: pathmap_dlopen %s\n", name);
 275   }
 276 
 277   void * handle = NULL;
 278   if (alt_root_len > 0) {
 279     char alt_path[PATH_MAX+1];
 280     strcpy(alt_path, alt_root);
 281     strcat(alt_path, name);
 282     handle = dlopen(alt_path, mode);
 283     if (_libsaproc_debug && handle) {
 284       printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path);
 285     }
 286 
 287     if (handle == NULL && strrchr(name, '/')) {
 288       strcpy(alt_path, alt_root);
 289       strcat(alt_path, strrchr(name, '/'));
 290       handle = dlopen(alt_path, mode);
 291       if (_libsaproc_debug && handle) {
 292         printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path);
 293       }
 294     }
 295   }
 296   if (handle == NULL) {
 297     handle = dlopen(name, mode);
 298   }
 299   if (_libsaproc_debug) {
 300     printf("libsaproc DEBUG: pathmap_dlopen %s return 0x%x\n", name, handle);
 301   }
 302   return handle;
 303 }
 304 
 305 // libproc and libthread_db callback functions
 306 
 307 extern "C" {
 308 
 309 static int
 310 init_libthread_db_ptrs(void *cd, const prmap_t *pmp, const char *object_name) {
 311   Debugger* dbg = (Debugger*) cd;
 312   JNIEnv* env = dbg->env;
 313   jobject this_obj = dbg->this_obj;
 314   struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID);
 315 
 316   char *s1 = 0, *s2 = 0;
 317   char libthread_db[PATH_MAX];
 318 
 319   if (strstr(object_name, "/libthread.so.") == NULL)
 320      return (0);
 321 
 322   /*
 323    * We found a libthread.
 324    * dlopen() the matching libthread_db and get the thread agent handle.
 325    */
 326   if (Pstatus(ph)->pr_dmodel == PR_MODEL_NATIVE) {
 327      (void) strcpy(libthread_db, object_name);
 328      s1 = (char*) strstr(object_name, ".so.");
 329      s2 = (char*) strstr(libthread_db, ".so.");
 330      (void) strcpy(s2, "_db");
 331      s2 += 3;
 332      (void) strcpy(s2, s1);
 333   } else {
 334 #ifdef _LP64
 335      /*
 336       * The victim process is 32-bit, we are 64-bit.
 337       * We have to find the 64-bit version of libthread_db
 338       * that matches the victim's 32-bit version of libthread.
 339       */
 340      (void) strcpy(libthread_db, object_name);
 341      s1 = (char*) strstr(object_name, "/libthread.so.");
 342      s2 = (char*) strstr(libthread_db, "/libthread.so.");
 343      (void) strcpy(s2, "/64");
 344      s2 += 3;
 345      (void) strcpy(s2, s1);
 346      s1 = (char*) strstr(s1, ".so.");
 347      s2 = (char*) strstr(s2, ".so.");
 348      (void) strcpy(s2, "_db");
 349      s2 += 3;
 350      (void) strcpy(s2, s1);
 351 #else
 352      return (0);
 353 #endif  /* _LP64 */
 354   }
 355 
 356   void* libthread_db_handle = 0;
 357   if ((libthread_db_handle = pathmap_dlopen(libthread_db, RTLD_LAZY|RTLD_LOCAL)) == NULL) {
 358      char errMsg[PATH_MAX + 256];
 359      sprintf(errMsg, "Can't load %s!", libthread_db);
 360      HANDLE_THREADDB_FAILURE_(errMsg, 0);
 361   }
 362   env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)(uintptr_t)libthread_db_handle);
 363 
 364   void* tmpPtr = 0;
 365   tmpPtr = dlsym(libthread_db_handle, "td_init");
 366   if (tmpPtr == 0) {
 367      HANDLE_THREADDB_FAILURE_("dlsym failed on td_init!", 0);
 368   }
 369   env->SetLongField(this_obj, p_td_init_ID, (jlong)(uintptr_t) tmpPtr);
 370 
 371   tmpPtr =dlsym(libthread_db_handle, "td_ta_new");
 372   if (tmpPtr == 0) {
 373      HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_new!", 0);
 374   }
 375   env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)(uintptr_t) tmpPtr);
 376 
 377   tmpPtr = dlsym(libthread_db_handle, "td_ta_delete");
 378   if (tmpPtr == 0) {
 379      HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_delete!", 0);
 380   }
 381   env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)(uintptr_t) tmpPtr);
 382 
 383   tmpPtr = dlsym(libthread_db_handle, "td_ta_thr_iter");
 384   if (tmpPtr == 0) {
 385      HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_thr_iter!", 0);
 386   }
 387   env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)(uintptr_t) tmpPtr);
 388 
 389   tmpPtr = dlsym(libthread_db_handle, "td_thr_get_info");
 390   if (tmpPtr == 0) {
 391      HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_get_info!", 0);
 392   }
 393   env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)(uintptr_t) tmpPtr);
 394 
 395   tmpPtr = dlsym(libthread_db_handle, "td_ta_map_id2thr");
 396   if (tmpPtr == 0) {
 397      HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_map_id2thr!", 0);
 398   }
 399   env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)(uintptr_t) tmpPtr);
 400 
 401   tmpPtr = dlsym(libthread_db_handle, "td_thr_getgregs");
 402   if (tmpPtr == 0) {
 403      HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_getgregs!", 0);
 404   }
 405   env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)(uintptr_t) tmpPtr);
 406 
 407   return 1;
 408 }
 409 
 410 static int
 411 fill_thread_list(const td_thrhandle_t *p_td_thragent_t, void* cd) {
 412   DebuggerWithObject* dbgo = (DebuggerWithObject*) cd;
 413   JNIEnv* env = dbgo->env;
 414   jobject this_obj = dbgo->this_obj;
 415   jobject list = dbgo->obj;
 416 
 417   td_thrinfo_t thrinfo;
 418   p_td_thr_get_info_t p_td_thr_get_info = (p_td_thr_get_info_t) env->GetLongField(this_obj, p_td_thr_get_info_ID);
 419 
 420   if (p_td_thr_get_info(p_td_thragent_t, &thrinfo) != TD_OK)
 421     return (0);
 422 
 423   jobject threadProxy = env->CallObjectMethod(this_obj, getThreadForThreadId_ID, (jlong)(uintptr_t) thrinfo.ti_tid);
 424   CHECK_EXCEPTION_(1);
 425   env->CallBooleanMethod(list, listAdd_ID, threadProxy);
 426   CHECK_EXCEPTION_(1);
 427   return 0;
 428 }
 429 
 430 static int
 431 fill_load_object_list(void *cd, const prmap_t* pmp, const char* obj_name) {
 432 
 433   if (obj_name) {
 434      DebuggerWithObject* dbgo = (DebuggerWithObject*) cd;
 435      JNIEnv* env = dbgo->env;
 436      jobject this_obj = dbgo->this_obj;
 437      jobject list = dbgo->obj;
 438 
 439      jstring objectName = env->NewStringUTF(obj_name);
 440      CHECK_EXCEPTION_(1);
 441 
 442      jlong mapSize = (jlong) pmp->pr_size;
 443      jobject sharedObject = env->CallObjectMethod(this_obj, createLoadObject_ID,
 444                                   objectName, mapSize, (jlong)(uintptr_t)pmp->pr_vaddr);
 445      CHECK_EXCEPTION_(1);
 446      env->CallBooleanMethod(list, listAdd_ID, sharedObject);
 447      CHECK_EXCEPTION_(1);
 448   }
 449 
 450   return 0;
 451 }
 452 
 453 static int
 454 fill_cframe_list(void *cd, const prgregset_t regs, uint_t argc, const long *argv) {
 455   DebuggerWith2Objects* dbgo2 = (DebuggerWith2Objects*) cd;
 456   JNIEnv* env = dbgo2->env;
 457   jobject this_obj = dbgo2->this_obj;
 458   jobject curFrame = dbgo2->obj2;
 459 
 460   jint pcRegIndex = env->GetIntField(this_obj, pcRegIndex_ID);
 461   jint fpRegIndex = env->GetIntField(this_obj, fpRegIndex_ID);
 462 
 463   jlong pc = (jlong) (uintptr_t) regs[pcRegIndex];
 464   jlong fp = (jlong) (uintptr_t) regs[fpRegIndex];
 465 
 466   dbgo2->obj2 = env->CallObjectMethod(this_obj, createSenderFrame_ID,
 467                                     curFrame, pc, fp);
 468   CHECK_EXCEPTION_(1);
 469   if (dbgo2->obj == 0) {
 470      dbgo2->obj = dbgo2->obj2;
 471   }
 472   return 0;
 473 }
 474 
 475 // part of the class sharing workaround
 476 
 477 // FIXME: !!HACK ALERT!!
 478 
 479 // The format of sharing achive file header is needed to read shared heap
 480 // file mappings. For now, I am hard coding portion of FileMapHeader here.
 481 // Refer to filemap.hpp.
 482 
 483 // FileMapHeader describes the shared space data in the file to be
 484 // mapped.  This structure gets written to a file.  It is not a class, so
 485 // that the compilers don't add any compiler-private data to it.
 486 
 487 // Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp
 488 const int NUM_SHARED_MAPS = 4;
 489 
 490 // Refer to FileMapInfo::_current_version in filemap.hpp
 491 const int CURRENT_ARCHIVE_VERSION = 1;
 492 
 493 struct FileMapHeader {
 494  int   _magic;              // identify file type.
 495  int   _version;            // (from enum, above.)
 496  size_t _alignment;         // how shared archive should be aligned
 497 
 498 
 499  struct space_info {
 500    int    _file_offset;     // sizeof(this) rounded to vm page size
 501    char*  _base;            // copy-on-write base address
 502    size_t _capacity;        // for validity checking
 503    size_t _used;            // for setting space top on read
 504 
 505    bool   _read_only;       // read only space?
 506    bool   _allow_exec;      // executable code in space?
 507 
 508  } _space[NUM_SHARED_MAPS]; // was _space[CompactingPermGenGen::n_regions];
 509 
 510  // Ignore the rest of the FileMapHeader. We don't need those fields here.
 511 };
 512 
 513 static bool
 514 read_jboolean(struct ps_prochandle* ph, psaddr_t addr, jboolean* pvalue) {
 515   jboolean i;
 516   if (ps_pread(ph, addr, &i, sizeof(i)) == PS_OK) {
 517     *pvalue = i;
 518     return true;
 519   } else {
 520     return false;
 521   }
 522 }
 523 
 524 static bool
 525 read_pointer(struct ps_prochandle* ph, psaddr_t addr, uintptr_t* pvalue) {
 526   uintptr_t uip;
 527   if (ps_pread(ph, addr, &uip, sizeof(uip)) == PS_OK) {
 528     *pvalue = uip;
 529     return true;
 530   } else {
 531     return false;
 532   }
 533 }
 534 
 535 static bool
 536 read_string(struct ps_prochandle* ph, psaddr_t addr, char* buf, size_t size) {
 537   char ch = ' ';
 538   size_t i = 0;
 539 
 540   while (ch != '\0') {
 541     if (ps_pread(ph, addr, &ch, sizeof(ch)) != PS_OK)
 542       return false;
 543 
 544     if (i < size - 1) {
 545       buf[i] = ch;
 546     } else { // smaller buffer
 547       return false;
 548     }
 549 
 550     i++; addr++;
 551   }
 552 
 553   buf[i] = '\0';
 554   return true;
 555 }
 556 
 557 #define USE_SHARED_SPACES_SYM   "UseSharedSpaces"
 558 // mangled symbol name for Arguments::SharedArchivePath
 559 #define SHARED_ARCHIVE_PATH_SYM "__1cJArgumentsRSharedArchivePath_"
 560 
 561 static int
 562 init_classsharing_workaround(void *cd, const prmap_t* pmap, const char* obj_name) {
 563   Debugger* dbg = (Debugger*) cd;
 564   JNIEnv*   env = dbg->env;
 565   jobject this_obj = dbg->this_obj;
 566   const char* jvm_name = 0;
 567   if ((jvm_name = strstr(obj_name, "libjvm.so")) != NULL ||
 568       (jvm_name = strstr(obj_name, "libjvm_g.so")) != NULL) {
 569     jvm_name = obj_name;
 570   } else {
 571     return 0;
 572   }
 573 
 574   struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID);
 575 
 576   // initialize classes[_g].jsa file descriptor field.
 577   dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, -1);
 578 
 579   // check whether class sharing is on by reading variable "UseSharedSpaces"
 580   psaddr_t useSharedSpacesAddr = 0;
 581   ps_pglobal_lookup(ph, jvm_name, USE_SHARED_SPACES_SYM, &useSharedSpacesAddr);
 582   if (useSharedSpacesAddr == 0) {
 583     THROW_NEW_DEBUGGER_EXCEPTION_("can't find 'UseSharedSpaces' flag\n", 1);
 584   }
 585 
 586   // read the value of the flag "UseSharedSpaces"
 587   // Since hotspot types are not available to build this library. So
 588   // equivalent type "jboolean" is used to read the value of "UseSharedSpaces"
 589   // which is same as hotspot type "bool".
 590   jboolean value = 0;
 591   if (read_jboolean(ph, useSharedSpacesAddr, &value) != true) {
 592     THROW_NEW_DEBUGGER_EXCEPTION_("can't read 'UseSharedSpaces' flag", 1);
 593   } else if ((int)value == 0) {
 594     print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
 595     return 1;
 596   }
 597 
 598   char classes_jsa[PATH_MAX];
 599   psaddr_t sharedArchivePathAddrAddr = 0;
 600   ps_pglobal_lookup(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM, &sharedArchivePathAddrAddr);
 601   if (sharedArchivePathAddrAddr == 0) {
 602     print_debug("can't find symbol 'Arguments::SharedArchivePath'\n");
 603     THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1);
 604   }
 605 
 606   uintptr_t sharedArchivePathAddr = 0;
 607   if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) {
 608     print_debug("can't find read pointer 'Arguments::SharedArchivePath'\n");
 609     THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1);
 610   }
 611 
 612   if (read_string(ph, (psaddr_t)sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
 613     print_debug("can't find read 'Arguments::SharedArchivePath' value\n");
 614     THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1);
 615   }
 616 
 617   print_debug("looking for %s\n", classes_jsa);
 618 
 619   // open the classes[_g].jsa
 620   int fd = libsaproc_open(classes_jsa, O_RDONLY);
 621   if (fd < 0) {
 622     char errMsg[ERR_MSG_SIZE];
 623     sprintf(errMsg, "can't open shared archive file %s", classes_jsa);
 624     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 625   } else {
 626     print_debug("opened shared archive file %s\n", classes_jsa);
 627   }
 628 
 629   // parse classes[_g].jsa
 630   struct FileMapHeader* pheader = (struct FileMapHeader*) malloc(sizeof(struct FileMapHeader));
 631   if (pheader == NULL) {
 632     close(fd);
 633     THROW_NEW_DEBUGGER_EXCEPTION_("can't allocate memory for shared file map header", 1);
 634   }
 635 
 636   memset(pheader, 0, sizeof(struct FileMapHeader));
 637   // read FileMapHeader
 638   size_t n = read(fd, pheader, sizeof(struct FileMapHeader));
 639   if (n != sizeof(struct FileMapHeader)) {
 640     free(pheader);
 641     close(fd);
 642     char errMsg[ERR_MSG_SIZE];
 643     sprintf(errMsg, "unable to read shared archive file map header from %s", classes_jsa);
 644     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 645   }
 646 
 647   // check file magic
 648   if (pheader->_magic != 0xf00baba2) {
 649     free(pheader);
 650     close(fd);
 651     char errMsg[ERR_MSG_SIZE];
 652     sprintf(errMsg, "%s has bad shared archive magic 0x%x, expecting 0xf00baba2",
 653                    classes_jsa, pheader->_magic);
 654     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 655   }
 656 
 657   // check version
 658   if (pheader->_version != CURRENT_ARCHIVE_VERSION) {
 659     free(pheader);
 660     close(fd);
 661     char errMsg[ERR_MSG_SIZE];
 662     sprintf(errMsg, "%s has wrong shared archive version %d, expecting %d",
 663                    classes_jsa, pheader->_version, CURRENT_ARCHIVE_VERSION);
 664     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 665   }
 666 
 667   if (_libsaproc_debug) {
 668     for (int m = 0; m < NUM_SHARED_MAPS; m++) {
 669        print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n",
 670           pheader->_space[m]._file_offset, pheader->_space[m]._base,
 671           pheader->_space[m]._used, pheader->_space[m]._read_only);
 672     }
 673   }
 674 
 675   // FIXME: For now, omitting other checks such as VM version etc.
 676 
 677   // store class archive file fd and map header in debugger object fields
 678   dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, fd);
 679   dbg->env->SetLongField(this_obj, p_file_map_header_ID, (jlong)(uintptr_t) pheader);
 680   return 1;
 681 }
 682 
 683 } // extern "C"
 684 
 685 // error messages for proc_arg_grab failure codes. The messages are
 686 // modified versions of comments against corresponding #defines in
 687 // libproc.h.
 688 static const char* proc_arg_grab_errmsgs[] = {
 689                       "",
 690  /* G_NOPROC */       "No such process",
 691  /* G_NOCORE */       "No such core file",
 692  /* G_NOPROCORCORE */ "No such process or core",
 693  /* G_NOEXEC */       "Cannot locate executable file",
 694  /* G_ZOMB   */       "Zombie processs",
 695  /* G_PERM   */       "No permission to attach",
 696  /* G_BUSY   */       "Another process has already attached",
 697  /* G_SYS    */       "System process - can not attach",
 698  /* G_SELF   */       "Process is self - can't debug myself!",
 699  /* G_INTR   */       "Interrupt received while grabbing",
 700  /* G_LP64   */       "debuggee is 64 bit, use java -d64 for debugger",
 701  /* G_FORMAT */       "File is not an ELF format core file - corrupted core?",
 702  /* G_ELF    */       "Libelf error while parsing an ELF file",
 703  /* G_NOTE   */       "Required PT_NOTE Phdr not present - corrupted core?",
 704 };
 705 
 706 static void attach_internal(JNIEnv* env, jobject this_obj, jstring cmdLine, jboolean isProcess) {
 707   jboolean isCopy;
 708   int gcode;
 709   const char* cmdLine_cstr = env->GetStringUTFChars(cmdLine, &isCopy);
 710   CHECK_EXCEPTION;
 711 
 712   // some older versions of libproc.so crash when trying to attach 32 bit
 713   // debugger to 64 bit core file. check and throw error.
 714 #ifndef _LP64
 715   atoi(cmdLine_cstr);
 716   if (errno) {
 717      // core file
 718      int core_fd;
 719      if ((core_fd = open64(cmdLine_cstr, O_RDONLY)) >= 0) {
 720         Elf32_Ehdr e32;
 721         if (pread64(core_fd, &e32, sizeof (e32), 0) == sizeof (e32) &&
 722             memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0 &&
 723             e32.e_type == ET_CORE && e32.e_ident[EI_CLASS] == ELFCLASS64) {
 724               close(core_fd);
 725               THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use java -d64 for debugger");
 726         }
 727         close(core_fd);
 728      }
 729      // all other conditions are handled by libproc.so.
 730   }
 731 #endif
 732 
 733   // connect to process/core
 734   struct ps_prochandle* ph = proc_arg_grab(cmdLine_cstr, (isProcess? PR_ARG_PIDS : PR_ARG_CORES), PGRAB_FORCE, &gcode);
 735   env->ReleaseStringUTFChars(cmdLine, cmdLine_cstr);
 736   if (! ph) {
 737      if (gcode > 0 && gcode < sizeof(proc_arg_grab_errmsgs)/sizeof(const char*)) {
 738         char errMsg[ERR_MSG_SIZE];
 739         sprintf(errMsg, "Attach failed : %s", proc_arg_grab_errmsgs[gcode]);
 740         THROW_NEW_DEBUGGER_EXCEPTION(errMsg);
 741     } else {
 742         if (_libsaproc_debug && gcode == G_STRANGE) {
 743            perror("libsaproc DEBUG: ");
 744         }
 745         if (isProcess) {
 746            THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to process!");
 747         } else {
 748            THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to core file!");
 749         }
 750      }
 751   }
 752 
 753   // even though libproc.so supports 64 bit debugger and 32 bit debuggee, we don't
 754   // support such cross-bit-debugging. check for that combination and throw error.
 755 #ifdef _LP64
 756   int data_model;
 757   if (ps_pdmodel(ph, &data_model) != PS_OK) {
 758      Prelease(ph, PRELEASE_CLEAR);
 759      THROW_NEW_DEBUGGER_EXCEPTION("can't determine debuggee data model (ILP32? or LP64?)");
 760   }
 761   if (data_model == PR_MODEL_ILP32) {
 762      Prelease(ph, PRELEASE_CLEAR);
 763      THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger");
 764   }
 765 #endif
 766 
 767   env->SetLongField(this_obj, p_ps_prochandle_ID, (jlong)(uintptr_t)ph);
 768 
 769   Debugger dbg;
 770   dbg.env = env;
 771   dbg.this_obj = this_obj;
 772   jthrowable exception = 0;
 773   if (! isProcess) {
 774     /*
 775      * With class sharing, shared perm. gen heap is allocated in with MAP_SHARED|PROT_READ.
 776      * These pages are mapped from the file "classes[_g].jsa". MAP_SHARED pages are not dumped
 777      * in Solaris core.To read shared heap pages, we have to read classes[_g].jsa file.
 778      */
 779     Pobject_iter(ph, init_classsharing_workaround, &dbg);
 780     exception = env->ExceptionOccurred();
 781     if (exception) {
 782       env->ExceptionClear();
 783       detach_internal(env, this_obj);
 784       env->Throw(exception);
 785       return;
 786     }
 787   }
 788 
 789   /*
 790    * Iterate over the process mappings looking
 791    * for libthread and then dlopen the appropriate
 792    * libthread_db and get function pointers.
 793    */
 794   Pobject_iter(ph, init_libthread_db_ptrs, &dbg);
 795   exception = env->ExceptionOccurred();
 796   if (exception) {
 797     env->ExceptionClear();
 798     if (!sa_ignore_threaddb) {
 799       detach_internal(env, this_obj);
 800       env->Throw(exception);
 801     }
 802     return;
 803   }
 804 
 805   // init libthread_db and create thread_db agent
 806   p_td_init_t p_td_init = (p_td_init_t) env->GetLongField(this_obj, p_td_init_ID);
 807   if (p_td_init == 0) {
 808     if (!sa_ignore_threaddb) {
 809       detach_internal(env, this_obj);
 810     }
 811     HANDLE_THREADDB_FAILURE("Did not find libthread in target process/core!");
 812   }
 813 
 814   if (p_td_init() != TD_OK) {
 815     if (!sa_ignore_threaddb) {
 816       detach_internal(env, this_obj);
 817     }
 818     HANDLE_THREADDB_FAILURE("Can't initialize thread_db!");
 819   }
 820 
 821   p_td_ta_new_t p_td_ta_new = (p_td_ta_new_t) env->GetLongField(this_obj, p_td_ta_new_ID);
 822 
 823   td_thragent_t *p_td_thragent_t = 0;
 824   if (p_td_ta_new(ph, &p_td_thragent_t) != TD_OK) {
 825     if (!sa_ignore_threaddb) {
 826       detach_internal(env, this_obj);
 827     }
 828     HANDLE_THREADDB_FAILURE("Can't create thread_db agent!");
 829   }
 830   env->SetLongField(this_obj, p_td_thragent_t_ID, (jlong)(uintptr_t) p_td_thragent_t);
 831 
 832 }
 833 
 834 /*
 835  * Class:     sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 836  * Method:    attach0
 837  * Signature: (Ljava/lang/String;)V
 838  * Description: process detach
 839  */
 840 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2
 841   (JNIEnv *env, jobject this_obj, jstring pid) {
 842   attach_internal(env, this_obj, pid, JNI_TRUE);
 843 }
 844 
 845 /*
 846  * Class:     sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 847  * Method:    attach0
 848  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 849  * Description: core file detach
 850  */
 851 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
 852   (JNIEnv *env, jobject this_obj, jstring executable, jstring corefile) {
 853   // ignore executable file name, libproc.so can detect a.out name anyway.
 854   attach_internal(env, this_obj, corefile, JNI_FALSE);
 855 }
 856 
 857 
 858 /*
 859  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 860  * Method:      detach0
 861  * Signature:   ()V
 862  * Description: process/core file detach
 863  */
 864 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_detach0
 865   (JNIEnv *env, jobject this_obj) {
 866   detach_internal(env, this_obj);
 867 }
 868 
 869 /*
 870  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 871  * Method:      getRemoteProcessAddressSize0
 872  * Signature:   ()I
 873  * Description: get process/core address size
 874  */
 875 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getRemoteProcessAddressSize0
 876   (JNIEnv *env, jobject this_obj) {
 877   jlong p_ps_prochandle;
 878   p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
 879   int data_model = PR_MODEL_ILP32;
 880   ps_pdmodel((struct ps_prochandle*) p_ps_prochandle, &data_model);
 881   print_debug("debuggee is %d bit\n", data_model == PR_MODEL_ILP32? 32 : 64);
 882   return (jint) data_model == PR_MODEL_ILP32? 32 : 64;
 883 }
 884 
 885 /*
 886  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 887  * Method:      getPageSize0
 888  * Signature:   ()I
 889  * Description: get process/core page size
 890  */
 891 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getPageSize0
 892   (JNIEnv *env, jobject this_obj) {
 893 
 894 /*
 895   We are not yet attached to a java process or core file. getPageSize is called from
 896   the constructor of ProcDebuggerLocal. The following won't work!
 897 
 898     jlong p_ps_prochandle;
 899     p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
 900     CHECK_EXCEPTION_(-1);
 901     struct ps_prochandle* prochandle = (struct ps_prochandle*) p_ps_prochandle;
 902     return (Pstate(prochandle) == PS_DEAD) ? Pgetauxval(prochandle, AT_PAGESZ)
 903                                            : getpagesize();
 904 
 905   So even though core may have been generated with a different page size settings, for now
 906   call getpagesize.
 907 */
 908 
 909   return getpagesize();
 910 }
 911 
 912 /*
 913  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 914  * Method:      getThreadIntegerRegisterSet0
 915  * Signature:   (J)[J
 916  * Description: get gregset for a given thread specified by thread id
 917  */
 918 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getThreadIntegerRegisterSet0
 919   (JNIEnv *env, jobject this_obj, jlong tid) {
 920   // map the thread id to thread handle
 921   p_td_ta_map_id2thr_t p_td_ta_map_id2thr = (p_td_ta_map_id2thr_t) env->GetLongField(this_obj, p_td_ta_map_id2thr_ID);
 922 
 923   td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID);
 924   if (p_td_thragent_t == 0) {
 925      return 0;
 926   }
 927 
 928   td_thrhandle_t thr_handle;
 929   if (p_td_ta_map_id2thr(p_td_thragent_t, (thread_t) tid, &thr_handle) != TD_OK) {
 930      THROW_NEW_DEBUGGER_EXCEPTION_("can't map thread id to thread handle!", 0);
 931   }
 932 
 933   p_td_thr_getgregs_t p_td_thr_getgregs = (p_td_thr_getgregs_t) env->GetLongField(this_obj, p_td_thr_getgregs_ID);
 934   prgregset_t gregs;
 935   p_td_thr_getgregs(&thr_handle, gregs);
 936 
 937   jlongArray res = env->NewLongArray(NPRGREG);
 938   CHECK_EXCEPTION_(0);
 939   jboolean isCopy;
 940   jlong* ptr = env->GetLongArrayElements(res, &isCopy);
 941   for (int i = 0; i < NPRGREG; i++) {
 942     ptr[i] = (jlong) (uintptr_t) gregs[i];
 943   }
 944   env->ReleaseLongArrayElements(res, ptr, JNI_COMMIT);
 945   return res;
 946 }
 947 
 948 /*
 949  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 950  * Method:      fillThreadList0
 951  * Signature:   (Ljava/util/List;)V
 952  * Description: fills thread list of the debuggee process/core
 953  */
 954 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillThreadList0
 955   (JNIEnv *env, jobject this_obj, jobject list) {
 956 
 957   td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID);
 958   if (p_td_thragent_t == 0) {
 959      return;
 960   }
 961 
 962   p_td_ta_thr_iter_t p_td_ta_thr_iter = (p_td_ta_thr_iter_t) env->GetLongField(this_obj, p_td_ta_thr_iter_ID);
 963 
 964   DebuggerWithObject dbgo;
 965   dbgo.env = env;
 966   dbgo.this_obj = this_obj;
 967   dbgo.obj = list;
 968 
 969   p_td_ta_thr_iter(p_td_thragent_t, fill_thread_list, &dbgo,
 970                    TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
 971 }
 972 
 973 /*
 974  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 975  * Method:      fillCFrameList0
 976  * Signature:   ([J)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;
 977  * Description: fills CFrame list for a given thread
 978  */
 979 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillCFrameList0
 980   (JNIEnv *env, jobject this_obj, jlongArray regsArray) {
 981   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
 982 
 983   DebuggerWith2Objects dbgo2;
 984   dbgo2.env  = env;
 985   dbgo2.this_obj = this_obj;
 986   dbgo2.obj  = NULL;
 987   dbgo2.obj2 = NULL;
 988 
 989   jboolean isCopy;
 990   jlong* ptr = env->GetLongArrayElements(regsArray, &isCopy);
 991   CHECK_EXCEPTION_(0);
 992 
 993   prgregset_t gregs;
 994   for (int i = 0; i < NPRGREG; i++) {
 995      gregs[i] = (uintptr_t) ptr[i];
 996   }
 997 
 998   env->ReleaseLongArrayElements(regsArray, ptr, JNI_ABORT);
 999   CHECK_EXCEPTION_(0);
1000   Pstack_iter((struct ps_prochandle*) p_ps_prochandle, gregs, fill_cframe_list, &dbgo2);
1001   return dbgo2.obj;
1002 }
1003 
1004 /*
1005  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1006  * Method:      fillLoadObjectList0
1007  * Signature:   (Ljava/util/List;)V
1008  * Description: fills shared objects of the debuggee process/core
1009  */
1010 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillLoadObjectList0
1011   (JNIEnv *env, jobject this_obj, jobject list) {
1012   DebuggerWithObject dbgo;
1013   dbgo.env = env;
1014   dbgo.this_obj = this_obj;
1015   dbgo.obj = list;
1016 
1017   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1018   Pobject_iter((struct ps_prochandle*) p_ps_prochandle, fill_load_object_list, &dbgo);
1019 }
1020 
1021 /*
1022  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1023  * Method:      readBytesFromProcess0
1024  * Signature:   (JJ)[B
1025  * Description: read bytes from debuggee process/core
1026  */
1027 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0
1028   (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) {
1029 
1030   jbyteArray array = env->NewByteArray(numBytes);
1031   CHECK_EXCEPTION_(0);
1032   jboolean isCopy;
1033   jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy);
1034   CHECK_EXCEPTION_(0);
1035 
1036   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1037   ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle,
1038                        (psaddr_t)address, bufPtr, (size_t)numBytes);
1039 
1040   if (ret != PS_OK) {
1041     // part of the class sharing workaround. try shared heap area
1042     int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID);
1043     if (classes_jsa_fd != -1 && address != (jlong)0) {
1044       print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address);
1045 
1046       struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
1047       // walk through the shared mappings -- we just have 4 of them.
1048       // so, linear walking is okay.
1049       for (int m = 0; m < NUM_SHARED_MAPS; m++) {
1050 
1051         // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE
1052         // and hence will be read by libproc. Besides, the file copy may be
1053         // stale because the process might have modified those pages.
1054         if (pheader->_space[m]._read_only) {
1055           jlong baseAddress = (jlong) (uintptr_t) pheader->_space[m]._base;
1056           size_t usedSize = pheader->_space[m]._used;
1057           if (address >= baseAddress && address < (baseAddress + usedSize)) {
1058             // the given address falls in this shared heap area
1059             print_debug("found shared map at 0x%lx\n", (long) baseAddress);
1060 
1061 
1062             // If more data is asked than actually mapped from file, we need to zero fill
1063             // till the end-of-page boundary. But, java array new does that for us. we just
1064             // need to read as much as data available.
1065 
1066 #define MIN2(x, y) (((x) < (y))? (x) : (y))
1067 
1068             jlong diff = address - baseAddress;
1069             jlong bytesToRead = MIN2(numBytes, usedSize - diff);
1070             off_t offset = pheader->_space[m]._file_offset  + off_t(diff);
1071             ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset);
1072             if (bytesRead != bytesToRead) {
1073               env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT);
1074               print_debug("shared map read failed\n");
1075               return jbyteArray(0);
1076             } else {
1077               print_debug("shared map read succeeded\n");
1078               env->ReleaseByteArrayElements(array, bufPtr, 0);
1079               return array;
1080             }
1081           } // is in current map
1082         } // is read only map
1083       } // for shared maps
1084     } // classes_jsa_fd != -1
1085     env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT);
1086     return jbyteArray(0);
1087   } else {
1088     env->ReleaseByteArrayElements(array, bufPtr, 0);
1089     return array;
1090   }
1091 }
1092 
1093 /*
1094  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1095  * Method:      writeBytesToProcess0
1096  * Signature:   (JJ[B)V
1097  * Description: write bytes into debugger process
1098  */
1099 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_writeBytesToProcess0
1100   (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes, jbyteArray data) {
1101   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1102   jboolean isCopy;
1103   jbyte* ptr = env->GetByteArrayElements(data, &isCopy);
1104   CHECK_EXCEPTION;
1105 
1106   if (ps_pwrite((struct ps_prochandle*) p_ps_prochandle, address, ptr, numBytes) != PS_OK) {
1107      env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1108      THROW_NEW_DEBUGGER_EXCEPTION("Process write failed!");
1109   }
1110 
1111   env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1112 }
1113 
1114 /*
1115  * Class:     sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1116  * Method:    suspend0
1117  * Signature: ()V
1118  */
1119 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_suspend0
1120   (JNIEnv *env, jobject this_obj) {
1121   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1122   // for now don't check return value. revisit this again.
1123   Pstop((struct ps_prochandle*) p_ps_prochandle, 1000);
1124 }
1125 
1126 /*
1127  * Class:     sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1128  * Method:    resume0
1129  * Signature: ()V
1130  */
1131 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_resume0
1132   (JNIEnv *env, jobject this_obj) {
1133   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1134   // for now don't check return value. revisit this again.
1135   Psetrun((struct ps_prochandle*) p_ps_prochandle, 0, PRCFAULT|PRSTOP);
1136 }
1137 
1138 /*
1139   * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1140   * Method:      lookupByName0
1141   * Signature:   (Ljava/lang/String;Ljava/lang/String;)J
1142   * Description: symbol lookup by name
1143 */
1144 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByName0
1145    (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
1146    jlong p_ps_prochandle;
1147    p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1148 
1149    jboolean isCopy;
1150    const char* objectName_cstr = NULL;
1151    if (objectName != NULL) {
1152      objectName_cstr = env->GetStringUTFChars(objectName, &isCopy);
1153      CHECK_EXCEPTION_(0);
1154    } else {
1155      objectName_cstr = PR_OBJ_EVERY;
1156    }
1157 
1158    const char* symbolName_cstr = env->GetStringUTFChars(symbolName, &isCopy);
1159    CHECK_EXCEPTION_(0);
1160 
1161    psaddr_t symbol_addr = (psaddr_t) 0;
1162    ps_pglobal_lookup((struct ps_prochandle*) p_ps_prochandle,  objectName_cstr,
1163                     symbolName_cstr, &symbol_addr);
1164 
1165    if (symbol_addr == 0) {
1166       print_debug("lookup for %s in %s failed\n", symbolName_cstr, objectName_cstr);
1167    }
1168 
1169    if (objectName_cstr != PR_OBJ_EVERY) {
1170      env->ReleaseStringUTFChars(objectName, objectName_cstr);
1171    }
1172    env->ReleaseStringUTFChars(symbolName, symbolName_cstr);
1173    return (jlong) (uintptr_t) symbol_addr;
1174 }
1175 
1176 /*
1177  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1178  * Method:      lookupByAddress0
1179  * Signature:   (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
1180  * Description: lookup symbol name for a given address
1181  */
1182 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByAddress0
1183    (JNIEnv *env, jobject this_obj, jlong address) {
1184    jlong p_ps_prochandle;
1185    p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1186 
1187    char nameBuf[SYMBOL_BUF_SIZE + 1];
1188    GElf_Sym sym;
1189    int res = Plookup_by_addr((struct ps_prochandle*) p_ps_prochandle, (uintptr_t) address,
1190                                  nameBuf, sizeof(nameBuf), &sym);
1191    if (res != 0) { // failed
1192       return 0;
1193    }
1194 
1195    jstring resSym = env->NewStringUTF(nameBuf);
1196    CHECK_EXCEPTION_(0);
1197 
1198    return env->CallObjectMethod(this_obj, createClosestSymbol_ID, resSym, (address - sym.st_value));
1199 }
1200 
1201 /*
1202  * Class:     sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1203  * Method:    demangle0
1204  * Signature: (Ljava/lang/String;)Ljava/lang/String;
1205  */
1206 JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_demangle0
1207   (JNIEnv *env, jobject this_object, jstring name) {
1208   jboolean isCopy;
1209   const char* ptr = env->GetStringUTFChars(name, &isCopy);
1210   char  buf[2*SYMBOL_BUF_SIZE + 1];
1211   jstring res = 0;
1212   if (cplus_demangle((char*) ptr, buf, sizeof(buf)) != DEMANGLE_ESPACE) {
1213     res = env->NewStringUTF(buf);
1214   } else {
1215     res = name;
1216   }
1217   env->ReleaseStringUTFChars(name, ptr);
1218   return res;
1219 }
1220 
1221 /*
1222  * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
1223  * Method:      initIDs
1224  * Signature:   ()V
1225  * Description: get JNI ids for fields and methods of ProcDebuggerLocal class
1226  */
1227 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_initIDs
1228   (JNIEnv *env, jclass clazz) {
1229   _libsaproc_debug = getenv("LIBSAPROC_DEBUG") != NULL;
1230   if (_libsaproc_debug) {
1231      // propagate debug mode to libproc.so
1232      static const char* var = "LIBPROC_DEBUG=1";
1233      putenv((char*)var);
1234   }
1235 
1236   void* libproc_handle = dlopen("libproc.so", RTLD_LAZY | RTLD_GLOBAL);
1237   if (libproc_handle == 0)
1238      THROW_NEW_DEBUGGER_EXCEPTION("can't load libproc.so, if you are using Solaris 5.7 or below, copy libproc.so from 5.8!");
1239 
1240   p_ps_prochandle_ID = env->GetFieldID(clazz, "p_ps_prochandle", "J");
1241   CHECK_EXCEPTION;
1242 
1243   libthread_db_handle_ID = env->GetFieldID(clazz, "libthread_db_handle", "J");
1244   CHECK_EXCEPTION;
1245 
1246   p_td_thragent_t_ID = env->GetFieldID(clazz, "p_td_thragent_t", "J");
1247   CHECK_EXCEPTION;
1248 
1249   p_td_init_ID = env->GetFieldID(clazz, "p_td_init", "J");
1250   CHECK_EXCEPTION;
1251 
1252   p_td_ta_new_ID = env->GetFieldID(clazz, "p_td_ta_new", "J");
1253   CHECK_EXCEPTION;
1254 
1255   p_td_ta_delete_ID = env->GetFieldID(clazz, "p_td_ta_delete", "J");
1256   CHECK_EXCEPTION;
1257 
1258   p_td_ta_thr_iter_ID = env->GetFieldID(clazz, "p_td_ta_thr_iter", "J");
1259   CHECK_EXCEPTION;
1260 
1261   p_td_thr_get_info_ID = env->GetFieldID(clazz, "p_td_thr_get_info", "J");
1262   CHECK_EXCEPTION;
1263 
1264   p_td_ta_map_id2thr_ID = env->GetFieldID(clazz, "p_td_ta_map_id2thr", "J");
1265   CHECK_EXCEPTION;
1266 
1267   p_td_thr_getgregs_ID = env->GetFieldID(clazz, "p_td_thr_getgregs", "J");
1268   CHECK_EXCEPTION;
1269 
1270   getThreadForThreadId_ID = env->GetMethodID(clazz,
1271                             "getThreadForThreadId", "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;");
1272   CHECK_EXCEPTION;
1273 
1274   pcRegIndex_ID = env->GetFieldID(clazz, "pcRegIndex", "I");
1275   CHECK_EXCEPTION;
1276 
1277   fpRegIndex_ID = env->GetFieldID(clazz, "fpRegIndex", "I");
1278   CHECK_EXCEPTION;
1279 
1280   createSenderFrame_ID = env->GetMethodID(clazz,
1281                             "createSenderFrame", "(Lsun/jvm/hotspot/debugger/proc/ProcCFrame;JJ)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;");
1282   CHECK_EXCEPTION;
1283 
1284   createLoadObject_ID = env->GetMethodID(clazz,
1285                             "createLoadObject", "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;");
1286   CHECK_EXCEPTION;
1287 
1288   createClosestSymbol_ID = env->GetMethodID(clazz,
1289                             "createClosestSymbol", "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
1290   CHECK_EXCEPTION;
1291 
1292   listAdd_ID = env->GetMethodID(env->FindClass("java/util/List"), "add", "(Ljava/lang/Object;)Z");
1293   CHECK_EXCEPTION;
1294 
1295   // part of the class sharing workaround
1296   classes_jsa_fd_ID = env->GetFieldID(clazz, "classes_jsa_fd", "I");
1297   CHECK_EXCEPTION;
1298   p_file_map_header_ID = env->GetFieldID(clazz, "p_file_map_header", "J");
1299   CHECK_EXCEPTION;
1300 }