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