agent/src/os/solaris/proc/saproc.cpp

Mon, 09 Mar 2009 13:28:46 -0700

author
xdono
date
Mon, 09 Mar 2009 13:28:46 -0700
changeset 1014
0fbdb4381b99
parent 964
8db2b3e46c38
child 1327
a94af87c3357
permissions
-rw-r--r--

6814575: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 03/09
Reviewed-by: katleman, tbell, ohair

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

mercurial