duke@435: /* xdono@1014: * Copyright 2002-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "salibproc.h" duke@435: #include "sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal.h" duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: duke@435: #define CHECK_EXCEPTION_(value) if(env->ExceptionOccurred()) { return value; } duke@435: #define CHECK_EXCEPTION if(env->ExceptionOccurred()) { return;} duke@435: #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throwNewDebuggerException(env, str); return value; } duke@435: #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throwNewDebuggerException(env, str); return;} duke@435: duke@435: #define SYMBOL_BUF_SIZE 256 duke@435: #define ERR_MSG_SIZE (PATH_MAX + 256) duke@435: duke@435: // debug mode duke@435: static int _libsaproc_debug = 0; duke@435: duke@435: static void print_debug(const char* format,...) { duke@435: if (_libsaproc_debug) { duke@435: va_list alist; duke@435: duke@435: va_start(alist, format); duke@435: fputs("libsaproc DEBUG: ", stderr); duke@435: vfprintf(stderr, format, alist); duke@435: va_end(alist); duke@435: } duke@435: } duke@435: duke@435: struct Debugger { duke@435: JNIEnv* env; duke@435: jobject this_obj; duke@435: }; duke@435: duke@435: struct DebuggerWithObject : Debugger { duke@435: jobject obj; duke@435: }; duke@435: duke@435: struct DebuggerWith2Objects : DebuggerWithObject { duke@435: jobject obj2; duke@435: }; duke@435: duke@435: /* duke@435: * Portions of user thread level detail gathering code is from pstack source duke@435: * code. See pstack.c in Solaris 2.8 user commands source code. duke@435: */ duke@435: duke@435: static void throwNewDebuggerException(JNIEnv* env, const char* errMsg) { duke@435: env->ThrowNew(env->FindClass("sun/jvm/hotspot/debugger/DebuggerException"), errMsg); duke@435: } duke@435: duke@435: // JNI ids for some fields, methods duke@435: duke@435: // libproc handler pointer duke@435: static jfieldID p_ps_prochandle_ID = 0; duke@435: duke@435: // libthread.so dlopen handle, thread agent ptr and function pointers duke@435: static jfieldID libthread_db_handle_ID = 0; duke@435: static jfieldID p_td_thragent_t_ID = 0; duke@435: static jfieldID p_td_init_ID = 0; duke@435: static jfieldID p_td_ta_new_ID = 0; duke@435: static jfieldID p_td_ta_delete_ID = 0; duke@435: static jfieldID p_td_ta_thr_iter_ID = 0; duke@435: static jfieldID p_td_thr_get_info_ID = 0; duke@435: static jfieldID p_td_ta_map_id2thr_ID = 0; duke@435: static jfieldID p_td_thr_getgregs_ID = 0; duke@435: duke@435: // reg index fields duke@435: static jfieldID pcRegIndex_ID = 0; duke@435: static jfieldID fpRegIndex_ID = 0; duke@435: duke@435: // part of the class sharing workaround duke@435: static jfieldID classes_jsa_fd_ID = 0; duke@435: static jfieldID p_file_map_header_ID = 0; duke@435: duke@435: // method ids duke@435: duke@435: static jmethodID getThreadForThreadId_ID = 0; duke@435: static jmethodID createSenderFrame_ID = 0; duke@435: static jmethodID createLoadObject_ID = 0; duke@435: static jmethodID createClosestSymbol_ID = 0; duke@435: static jmethodID listAdd_ID = 0; duke@435: duke@435: /* duke@435: * Functions we need from libthread_db duke@435: */ duke@435: typedef td_err_e duke@435: (*p_td_init_t)(void); duke@435: typedef td_err_e duke@435: (*p_td_ta_new_t)(void *, td_thragent_t **); duke@435: typedef td_err_e duke@435: (*p_td_ta_delete_t)(td_thragent_t *); duke@435: typedef td_err_e duke@435: (*p_td_ta_thr_iter_t)(const td_thragent_t *, td_thr_iter_f *, void *, duke@435: td_thr_state_e, int, sigset_t *, unsigned); duke@435: typedef td_err_e duke@435: (*p_td_thr_get_info_t)(const td_thrhandle_t *, td_thrinfo_t *); duke@435: typedef td_err_e duke@435: (*p_td_ta_map_id2thr_t)(const td_thragent_t *, thread_t, td_thrhandle_t *); duke@435: typedef td_err_e duke@435: (*p_td_thr_getgregs_t)(const td_thrhandle_t *, prgregset_t); duke@435: duke@435: static void duke@435: clear_libthread_db_ptrs(JNIEnv* env, jobject this_obj) { duke@435: // release libthread_db agent, if we had created duke@435: p_td_ta_delete_t p_td_ta_delete = 0; duke@435: p_td_ta_delete = (p_td_ta_delete_t) env->GetLongField(this_obj, p_td_ta_delete_ID); duke@435: duke@435: td_thragent_t *p_td_thragent_t = 0; duke@435: p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); duke@435: if (p_td_thragent_t != 0 && p_td_ta_delete != 0) { duke@435: p_td_ta_delete(p_td_thragent_t); duke@435: } duke@435: duke@435: // dlclose libthread_db.so duke@435: void* libthread_db_handle = (void*) env->GetLongField(this_obj, libthread_db_handle_ID); duke@435: if (libthread_db_handle != 0) { duke@435: dlclose(libthread_db_handle); duke@435: } duke@435: duke@435: env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)0); duke@435: env->SetLongField(this_obj, p_td_init_ID, (jlong)0); duke@435: env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)0); duke@435: env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)0); duke@435: env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)0); duke@435: env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)0); duke@435: env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)0); duke@435: env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)0); duke@435: } duke@435: duke@435: duke@435: static void detach_internal(JNIEnv* env, jobject this_obj) { duke@435: // clear libthread_db stuff duke@435: clear_libthread_db_ptrs(env, this_obj); duke@435: duke@435: // release ptr to ps_prochandle duke@435: jlong p_ps_prochandle; duke@435: p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: if (p_ps_prochandle != 0L) { duke@435: Prelease((struct ps_prochandle*) p_ps_prochandle, PRELEASE_CLEAR); duke@435: } duke@435: duke@435: // part of the class sharing workaround duke@435: int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID); duke@435: if (classes_jsa_fd != -1) { duke@435: close(classes_jsa_fd); duke@435: struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID); duke@435: if (pheader != NULL) { duke@435: free(pheader); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Is it okay to ignore libthread_db failure? Set env var to ignore duke@435: // libthread_db failure. You can still debug, but will miss threads duke@435: // related functionality. duke@435: static bool sa_ignore_threaddb = (getenv("SA_IGNORE_THREADDB") != 0); duke@435: duke@435: #define HANDLE_THREADDB_FAILURE(msg) \ duke@435: if (sa_ignore_threaddb) { \ duke@435: printf("libsaproc WARNING: %s\n", msg); \ duke@435: return; \ duke@435: } else { \ duke@435: THROW_NEW_DEBUGGER_EXCEPTION(msg); \ duke@435: } duke@435: duke@435: #define HANDLE_THREADDB_FAILURE_(msg, ret) \ duke@435: if (sa_ignore_threaddb) { \ duke@435: printf("libsaproc WARNING: %s\n", msg); \ duke@435: return ret; \ duke@435: } else { \ duke@435: THROW_NEW_DEBUGGER_EXCEPTION_(msg, ret); \ duke@435: } duke@435: duke@435: static const char * alt_root = NULL; duke@435: static int alt_root_len = -1; duke@435: duke@435: #define SA_ALTROOT "SA_ALTROOT" duke@435: duke@435: static void init_alt_root() { duke@435: if (alt_root_len == -1) { duke@435: alt_root = getenv(SA_ALTROOT); duke@435: if (alt_root) duke@435: alt_root_len = strlen(alt_root); duke@435: else duke@435: alt_root_len = 0; duke@435: } duke@435: } duke@435: duke@435: static int find_file_hook(const char * name, int elf_checksum) { duke@435: init_alt_root(); duke@435: duke@435: if (_libsaproc_debug) { duke@435: printf("libsaproc DEBUG: find_file_hook %s 0x%x\n", name, elf_checksum); duke@435: } duke@435: duke@435: if (alt_root_len > 0) { duke@435: int fd = -1; duke@435: char alt_path[PATH_MAX+1]; duke@435: duke@435: strcpy(alt_path, alt_root); duke@435: strcat(alt_path, name); duke@435: fd = open(alt_path, O_RDONLY); duke@435: if (fd >= 0) { duke@435: if (_libsaproc_debug) { duke@435: printf("libsaproc DEBUG: find_file_hook substituted %s\n", alt_path); duke@435: } duke@435: return fd; duke@435: } duke@435: duke@435: if (strrchr(name, '/')) { duke@435: strcpy(alt_path, alt_root); duke@435: strcat(alt_path, strrchr(name, '/')); duke@435: fd = open(alt_path, O_RDONLY); duke@435: if (fd >= 0) { duke@435: if (_libsaproc_debug) { duke@435: printf("libsaproc DEBUG: find_file_hook substituted %s\n", alt_path); duke@435: } duke@435: return fd; duke@435: } duke@435: } duke@435: } duke@435: return -1; duke@435: } duke@435: duke@435: static int pathmap_open(const char* name) { duke@435: int fd = open(name, O_RDONLY); duke@435: if (fd < 0) { duke@435: fd = find_file_hook(name, 0); duke@435: } duke@435: return fd; duke@435: } duke@435: duke@435: static void * pathmap_dlopen(const char * name, int mode) { duke@435: init_alt_root(); duke@435: duke@435: if (_libsaproc_debug) { duke@435: printf("libsaproc DEBUG: pathmap_dlopen %s\n", name); duke@435: } duke@435: duke@435: void * handle = NULL; duke@435: if (alt_root_len > 0) { duke@435: char alt_path[PATH_MAX+1]; duke@435: strcpy(alt_path, alt_root); duke@435: strcat(alt_path, name); duke@435: handle = dlopen(alt_path, mode); duke@435: if (_libsaproc_debug && handle) { duke@435: printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path); duke@435: } duke@435: duke@435: if (handle == NULL && strrchr(name, '/')) { duke@435: strcpy(alt_path, alt_root); duke@435: strcat(alt_path, strrchr(name, '/')); duke@435: handle = dlopen(alt_path, mode); duke@435: if (_libsaproc_debug && handle) { duke@435: printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path); duke@435: } duke@435: } duke@435: } duke@435: if (handle == NULL) { duke@435: handle = dlopen(name, mode); duke@435: } duke@435: if (_libsaproc_debug) { duke@435: printf("libsaproc DEBUG: pathmap_dlopen %s return 0x%x\n", name, handle); duke@435: } duke@435: return handle; duke@435: } duke@435: duke@435: // libproc and libthread_db callback functions duke@435: duke@435: extern "C" { duke@435: duke@435: static int duke@435: init_libthread_db_ptrs(void *cd, const prmap_t *pmp, const char *object_name) { duke@435: Debugger* dbg = (Debugger*) cd; duke@435: JNIEnv* env = dbg->env; duke@435: jobject this_obj = dbg->this_obj; duke@435: struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: duke@435: char *s1 = 0, *s2 = 0; duke@435: char libthread_db[PATH_MAX]; duke@435: duke@435: if (strstr(object_name, "/libthread.so.") == NULL) duke@435: return (0); duke@435: duke@435: /* duke@435: * We found a libthread. duke@435: * dlopen() the matching libthread_db and get the thread agent handle. duke@435: */ duke@435: if (Pstatus(ph)->pr_dmodel == PR_MODEL_NATIVE) { duke@435: (void) strcpy(libthread_db, object_name); duke@435: s1 = (char*) strstr(object_name, ".so."); duke@435: s2 = (char*) strstr(libthread_db, ".so."); duke@435: (void) strcpy(s2, "_db"); duke@435: s2 += 3; duke@435: (void) strcpy(s2, s1); duke@435: } else { duke@435: #ifdef _LP64 duke@435: /* duke@435: * The victim process is 32-bit, we are 64-bit. duke@435: * We have to find the 64-bit version of libthread_db duke@435: * that matches the victim's 32-bit version of libthread. duke@435: */ duke@435: (void) strcpy(libthread_db, object_name); duke@435: s1 = (char*) strstr(object_name, "/libthread.so."); duke@435: s2 = (char*) strstr(libthread_db, "/libthread.so."); duke@435: (void) strcpy(s2, "/64"); duke@435: s2 += 3; duke@435: (void) strcpy(s2, s1); duke@435: s1 = (char*) strstr(s1, ".so."); duke@435: s2 = (char*) strstr(s2, ".so."); duke@435: (void) strcpy(s2, "_db"); duke@435: s2 += 3; duke@435: (void) strcpy(s2, s1); duke@435: #else duke@435: return (0); duke@435: #endif /* _LP64 */ duke@435: } duke@435: duke@435: void* libthread_db_handle = 0; duke@435: if ((libthread_db_handle = pathmap_dlopen(libthread_db, RTLD_LAZY|RTLD_LOCAL)) == NULL) { duke@435: char errMsg[PATH_MAX + 256]; duke@435: sprintf(errMsg, "Can't load %s!", libthread_db); duke@435: HANDLE_THREADDB_FAILURE_(errMsg, 0); duke@435: } duke@435: env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)(uintptr_t)libthread_db_handle); duke@435: duke@435: void* tmpPtr = 0; duke@435: tmpPtr = dlsym(libthread_db_handle, "td_init"); duke@435: if (tmpPtr == 0) { duke@435: HANDLE_THREADDB_FAILURE_("dlsym failed on td_init!", 0); duke@435: } duke@435: env->SetLongField(this_obj, p_td_init_ID, (jlong)(uintptr_t) tmpPtr); duke@435: duke@435: tmpPtr =dlsym(libthread_db_handle, "td_ta_new"); duke@435: if (tmpPtr == 0) { duke@435: HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_new!", 0); duke@435: } duke@435: env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)(uintptr_t) tmpPtr); duke@435: duke@435: tmpPtr = dlsym(libthread_db_handle, "td_ta_delete"); duke@435: if (tmpPtr == 0) { duke@435: HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_delete!", 0); duke@435: } duke@435: env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)(uintptr_t) tmpPtr); duke@435: duke@435: tmpPtr = dlsym(libthread_db_handle, "td_ta_thr_iter"); duke@435: if (tmpPtr == 0) { duke@435: HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_thr_iter!", 0); duke@435: } duke@435: env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)(uintptr_t) tmpPtr); duke@435: duke@435: tmpPtr = dlsym(libthread_db_handle, "td_thr_get_info"); duke@435: if (tmpPtr == 0) { duke@435: HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_get_info!", 0); duke@435: } duke@435: env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)(uintptr_t) tmpPtr); duke@435: duke@435: tmpPtr = dlsym(libthread_db_handle, "td_ta_map_id2thr"); duke@435: if (tmpPtr == 0) { duke@435: HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_map_id2thr!", 0); duke@435: } duke@435: env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)(uintptr_t) tmpPtr); duke@435: duke@435: tmpPtr = dlsym(libthread_db_handle, "td_thr_getgregs"); duke@435: if (tmpPtr == 0) { duke@435: HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_getgregs!", 0); duke@435: } duke@435: env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)(uintptr_t) tmpPtr); duke@435: duke@435: return 1; duke@435: } duke@435: duke@435: static int duke@435: fill_thread_list(const td_thrhandle_t *p_td_thragent_t, void* cd) { duke@435: DebuggerWithObject* dbgo = (DebuggerWithObject*) cd; duke@435: JNIEnv* env = dbgo->env; duke@435: jobject this_obj = dbgo->this_obj; duke@435: jobject list = dbgo->obj; duke@435: duke@435: td_thrinfo_t thrinfo; duke@435: 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); duke@435: duke@435: if (p_td_thr_get_info(p_td_thragent_t, &thrinfo) != TD_OK) duke@435: return (0); duke@435: duke@435: jobject threadProxy = env->CallObjectMethod(this_obj, getThreadForThreadId_ID, (jlong)(uintptr_t) thrinfo.ti_tid); duke@435: CHECK_EXCEPTION_(1); duke@435: env->CallBooleanMethod(list, listAdd_ID, threadProxy); duke@435: CHECK_EXCEPTION_(1); duke@435: return 0; duke@435: } duke@435: duke@435: static int duke@435: fill_load_object_list(void *cd, const prmap_t* pmp, const char* obj_name) { duke@435: duke@435: if (obj_name) { duke@435: DebuggerWithObject* dbgo = (DebuggerWithObject*) cd; duke@435: JNIEnv* env = dbgo->env; duke@435: jobject this_obj = dbgo->this_obj; duke@435: jobject list = dbgo->obj; duke@435: duke@435: jstring objectName = env->NewStringUTF(obj_name); duke@435: CHECK_EXCEPTION_(1); duke@435: duke@435: jlong mapSize = (jlong) pmp->pr_size; duke@435: jobject sharedObject = env->CallObjectMethod(this_obj, createLoadObject_ID, duke@435: objectName, mapSize, (jlong)(uintptr_t)pmp->pr_vaddr); duke@435: CHECK_EXCEPTION_(1); duke@435: env->CallBooleanMethod(list, listAdd_ID, sharedObject); duke@435: CHECK_EXCEPTION_(1); duke@435: } duke@435: duke@435: return 0; duke@435: } duke@435: duke@435: static int duke@435: fill_cframe_list(void *cd, const prgregset_t regs, uint_t argc, const long *argv) { duke@435: DebuggerWith2Objects* dbgo2 = (DebuggerWith2Objects*) cd; duke@435: JNIEnv* env = dbgo2->env; duke@435: jobject this_obj = dbgo2->this_obj; duke@435: jobject curFrame = dbgo2->obj2; duke@435: duke@435: jint pcRegIndex = env->GetIntField(this_obj, pcRegIndex_ID); duke@435: jint fpRegIndex = env->GetIntField(this_obj, fpRegIndex_ID); duke@435: duke@435: jlong pc = (jlong) (uintptr_t) regs[pcRegIndex]; duke@435: jlong fp = (jlong) (uintptr_t) regs[fpRegIndex]; duke@435: duke@435: dbgo2->obj2 = env->CallObjectMethod(this_obj, createSenderFrame_ID, duke@435: curFrame, pc, fp); duke@435: CHECK_EXCEPTION_(1); duke@435: if (dbgo2->obj == 0) { duke@435: dbgo2->obj = dbgo2->obj2; duke@435: } duke@435: return 0; duke@435: } duke@435: duke@435: // part of the class sharing workaround duke@435: duke@435: // FIXME: !!HACK ALERT!! duke@435: duke@435: // The format of sharing achive file header is needed to read shared heap duke@435: // file mappings. For now, I am hard coding portion of FileMapHeader here. duke@435: // Refer to filemap.hpp. duke@435: duke@435: // FileMapHeader describes the shared space data in the file to be duke@435: // mapped. This structure gets written to a file. It is not a class, so duke@435: // that the compilers don't add any compiler-private data to it. duke@435: duke@435: // Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp duke@435: const int NUM_SHARED_MAPS = 4; duke@435: duke@435: // Refer to FileMapInfo::_current_version in filemap.hpp duke@435: const int CURRENT_ARCHIVE_VERSION = 1; duke@435: duke@435: struct FileMapHeader { duke@435: int _magic; // identify file type. duke@435: int _version; // (from enum, above.) duke@435: size_t _alignment; // how shared archive should be aligned duke@435: duke@435: duke@435: struct space_info { duke@435: int _file_offset; // sizeof(this) rounded to vm page size duke@435: char* _base; // copy-on-write base address duke@435: size_t _capacity; // for validity checking duke@435: size_t _used; // for setting space top on read duke@435: duke@435: bool _read_only; // read only space? duke@435: bool _allow_exec; // executable code in space? duke@435: duke@435: } _space[NUM_SHARED_MAPS]; // was _space[CompactingPermGenGen::n_regions]; duke@435: duke@435: // Ignore the rest of the FileMapHeader. We don't need those fields here. duke@435: }; duke@435: duke@435: static bool swamyv@964: read_jboolean(struct ps_prochandle* ph, psaddr_t addr, jboolean* pvalue) { swamyv@964: jboolean i; duke@435: if (ps_pread(ph, addr, &i, sizeof(i)) == PS_OK) { duke@435: *pvalue = i; duke@435: return true; duke@435: } else { duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: static bool duke@435: read_pointer(struct ps_prochandle* ph, psaddr_t addr, uintptr_t* pvalue) { duke@435: uintptr_t uip; duke@435: if (ps_pread(ph, addr, &uip, sizeof(uip)) == PS_OK) { duke@435: *pvalue = uip; duke@435: return true; duke@435: } else { duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: static bool duke@435: read_string(struct ps_prochandle* ph, psaddr_t addr, char* buf, size_t size) { duke@435: char ch = ' '; duke@435: size_t i = 0; duke@435: duke@435: while (ch != '\0') { duke@435: if (ps_pread(ph, addr, &ch, sizeof(ch)) != PS_OK) duke@435: return false; duke@435: duke@435: if (i < size - 1) { duke@435: buf[i] = ch; duke@435: } else { // smaller buffer duke@435: return false; duke@435: } duke@435: duke@435: i++; addr++; duke@435: } duke@435: duke@435: buf[i] = '\0'; duke@435: return true; duke@435: } duke@435: duke@435: #define USE_SHARED_SPACES_SYM "UseSharedSpaces" duke@435: // mangled symbol name for Arguments::SharedArchivePath duke@435: #define SHARED_ARCHIVE_PATH_SYM "__1cJArgumentsRSharedArchivePath_" duke@435: duke@435: static int duke@435: init_classsharing_workaround(void *cd, const prmap_t* pmap, const char* obj_name) { duke@435: Debugger* dbg = (Debugger*) cd; duke@435: JNIEnv* env = dbg->env; duke@435: jobject this_obj = dbg->this_obj; duke@435: const char* jvm_name = 0; duke@435: if ((jvm_name = strstr(obj_name, "libjvm.so")) != NULL || duke@435: (jvm_name = strstr(obj_name, "libjvm_g.so")) != NULL) { duke@435: jvm_name = obj_name; duke@435: } else { duke@435: return 0; duke@435: } duke@435: duke@435: struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: duke@435: // initialize classes[_g].jsa file descriptor field. duke@435: dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, -1); duke@435: duke@435: // check whether class sharing is on by reading variable "UseSharedSpaces" duke@435: psaddr_t useSharedSpacesAddr = 0; duke@435: ps_pglobal_lookup(ph, jvm_name, USE_SHARED_SPACES_SYM, &useSharedSpacesAddr); duke@435: if (useSharedSpacesAddr == 0) { duke@435: THROW_NEW_DEBUGGER_EXCEPTION_("can't find 'UseSharedSpaces' flag\n", 1); duke@435: } duke@435: duke@435: // read the value of the flag "UseSharedSpaces" swamyv@964: // Since hotspot types are not available to build this library. So swamyv@964: // equivalent type "jboolean" is used to read the value of "UseSharedSpaces" swamyv@964: // which is same as hotspot type "bool". swamyv@964: jboolean value = 0; swamyv@964: if (read_jboolean(ph, useSharedSpacesAddr, &value) != true) { duke@435: THROW_NEW_DEBUGGER_EXCEPTION_("can't read 'UseSharedSpaces' flag", 1); swamyv@964: } else if ((int)value == 0) { duke@435: print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); duke@435: return 1; duke@435: } duke@435: duke@435: char classes_jsa[PATH_MAX]; duke@435: psaddr_t sharedArchivePathAddrAddr = 0; duke@435: ps_pglobal_lookup(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM, &sharedArchivePathAddrAddr); duke@435: if (sharedArchivePathAddrAddr == 0) { duke@435: print_debug("can't find symbol 'Arguments::SharedArchivePath'\n"); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); duke@435: } duke@435: duke@435: uintptr_t sharedArchivePathAddr = 0; duke@435: if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { duke@435: print_debug("can't find read pointer 'Arguments::SharedArchivePath'\n"); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); duke@435: } duke@435: duke@435: if (read_string(ph, (psaddr_t)sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { duke@435: print_debug("can't find read 'Arguments::SharedArchivePath' value\n"); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); duke@435: } duke@435: duke@435: print_debug("looking for %s\n", classes_jsa); duke@435: duke@435: // open the classes[_g].jsa duke@435: int fd = pathmap_open(classes_jsa); duke@435: if (fd < 0) { duke@435: char errMsg[ERR_MSG_SIZE]; duke@435: sprintf(errMsg, "can't open shared archive file %s", classes_jsa); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); duke@435: } else { duke@435: print_debug("opened shared archive file %s\n", classes_jsa); duke@435: } duke@435: duke@435: // parse classes[_g].jsa duke@435: struct FileMapHeader* pheader = (struct FileMapHeader*) malloc(sizeof(struct FileMapHeader)); duke@435: if (pheader == NULL) { duke@435: close(fd); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_("can't allocate memory for shared file map header", 1); duke@435: } duke@435: duke@435: memset(pheader, 0, sizeof(struct FileMapHeader)); duke@435: // read FileMapHeader duke@435: size_t n = read(fd, pheader, sizeof(struct FileMapHeader)); duke@435: if (n != sizeof(struct FileMapHeader)) { duke@435: free(pheader); duke@435: close(fd); duke@435: char errMsg[ERR_MSG_SIZE]; duke@435: sprintf(errMsg, "unable to read shared archive file map header from %s", classes_jsa); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); duke@435: } duke@435: duke@435: // check file magic duke@435: if (pheader->_magic != 0xf00baba2) { duke@435: free(pheader); duke@435: close(fd); duke@435: char errMsg[ERR_MSG_SIZE]; duke@435: sprintf(errMsg, "%s has bad shared archive magic 0x%x, expecting 0xf00baba2", duke@435: classes_jsa, pheader->_magic); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); duke@435: } duke@435: duke@435: // check version duke@435: if (pheader->_version != CURRENT_ARCHIVE_VERSION) { duke@435: free(pheader); duke@435: close(fd); duke@435: char errMsg[ERR_MSG_SIZE]; duke@435: sprintf(errMsg, "%s has wrong shared archive version %d, expecting %d", duke@435: classes_jsa, pheader->_version, CURRENT_ARCHIVE_VERSION); duke@435: THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); duke@435: } duke@435: duke@435: if (_libsaproc_debug) { duke@435: for (int m = 0; m < NUM_SHARED_MAPS; m++) { duke@435: print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n", duke@435: pheader->_space[m]._file_offset, pheader->_space[m]._base, duke@435: pheader->_space[m]._used, pheader->_space[m]._read_only); duke@435: } duke@435: } duke@435: duke@435: // FIXME: For now, omitting other checks such as VM version etc. duke@435: duke@435: // store class archive file fd and map header in debugger object fields duke@435: dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, fd); duke@435: dbg->env->SetLongField(this_obj, p_file_map_header_ID, (jlong)(uintptr_t) pheader); duke@435: return 1; duke@435: } duke@435: duke@435: } // extern "C" duke@435: duke@435: // error messages for proc_arg_grab failure codes. The messages are duke@435: // modified versions of comments against corresponding #defines in duke@435: // libproc.h. duke@435: static const char* proc_arg_grab_errmsgs[] = { duke@435: "", duke@435: /* G_NOPROC */ "No such process", duke@435: /* G_NOCORE */ "No such core file", duke@435: /* G_NOPROCORCORE */ "No such process or core", duke@435: /* G_NOEXEC */ "Cannot locate executable file", duke@435: /* G_ZOMB */ "Zombie processs", duke@435: /* G_PERM */ "No permission to attach", duke@435: /* G_BUSY */ "Another process has already attached", duke@435: /* G_SYS */ "System process - can not attach", duke@435: /* G_SELF */ "Process is self - can't debug myself!", duke@435: /* G_INTR */ "Interrupt received while grabbing", duke@435: /* G_LP64 */ "debuggee is 64 bit, use java -d64 for debugger", duke@435: /* G_FORMAT */ "File is not an ELF format core file - corrupted core?", duke@435: /* G_ELF */ "Libelf error while parsing an ELF file", duke@435: /* G_NOTE */ "Required PT_NOTE Phdr not present - corrupted core?", duke@435: }; duke@435: duke@435: static void attach_internal(JNIEnv* env, jobject this_obj, jstring cmdLine, jboolean isProcess) { duke@435: jboolean isCopy; duke@435: int gcode; duke@435: const char* cmdLine_cstr = env->GetStringUTFChars(cmdLine, &isCopy); duke@435: CHECK_EXCEPTION; duke@435: duke@435: // some older versions of libproc.so crash when trying to attach 32 bit duke@435: // debugger to 64 bit core file. check and throw error. duke@435: #ifndef _LP64 duke@435: atoi(cmdLine_cstr); duke@435: if (errno) { duke@435: // core file duke@435: int core_fd; duke@435: if ((core_fd = open64(cmdLine_cstr, O_RDONLY)) >= 0) { duke@435: Elf32_Ehdr e32; duke@435: if (pread64(core_fd, &e32, sizeof (e32), 0) == sizeof (e32) && duke@435: memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0 && duke@435: e32.e_type == ET_CORE && e32.e_ident[EI_CLASS] == ELFCLASS64) { duke@435: close(core_fd); duke@435: THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use java -d64 for debugger"); duke@435: } duke@435: close(core_fd); duke@435: } duke@435: // all other conditions are handled by libproc.so. duke@435: } duke@435: #endif duke@435: duke@435: // connect to process/core duke@435: struct ps_prochandle* ph = proc_arg_grab(cmdLine_cstr, (isProcess? PR_ARG_PIDS : PR_ARG_CORES), PGRAB_FORCE, &gcode); duke@435: env->ReleaseStringUTFChars(cmdLine, cmdLine_cstr); duke@435: if (! ph) { duke@435: if (gcode > 0 && gcode < sizeof(proc_arg_grab_errmsgs)/sizeof(const char*)) { duke@435: char errMsg[ERR_MSG_SIZE]; duke@435: sprintf(errMsg, "Attach failed : %s", proc_arg_grab_errmsgs[gcode]); duke@435: THROW_NEW_DEBUGGER_EXCEPTION(errMsg); duke@435: } else { duke@435: if (_libsaproc_debug && gcode == G_STRANGE) { duke@435: perror("libsaproc DEBUG: "); duke@435: } duke@435: if (isProcess) { duke@435: THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to process!"); duke@435: } else { duke@435: THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to core file!"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // even though libproc.so supports 64 bit debugger and 32 bit debuggee, we don't duke@435: // support such cross-bit-debugging. check for that combination and throw error. duke@435: #ifdef _LP64 duke@435: int data_model; duke@435: if (ps_pdmodel(ph, &data_model) != PS_OK) { duke@435: Prelease(ph, PRELEASE_CLEAR); duke@435: THROW_NEW_DEBUGGER_EXCEPTION("can't determine debuggee data model (ILP32? or LP64?)"); duke@435: } duke@435: if (data_model == PR_MODEL_ILP32) { duke@435: Prelease(ph, PRELEASE_CLEAR); duke@435: THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger"); duke@435: } duke@435: #endif duke@435: duke@435: env->SetLongField(this_obj, p_ps_prochandle_ID, (jlong)(uintptr_t)ph); duke@435: duke@435: Debugger dbg; duke@435: dbg.env = env; duke@435: dbg.this_obj = this_obj; duke@435: jthrowable exception = 0; duke@435: if (! isProcess) { duke@435: /* duke@435: * With class sharing, shared perm. gen heap is allocated in with MAP_SHARED|PROT_READ. duke@435: * These pages are mapped from the file "classes[_g].jsa". MAP_SHARED pages are not dumped duke@435: * in Solaris core.To read shared heap pages, we have to read classes[_g].jsa file. duke@435: */ duke@435: Pobject_iter(ph, init_classsharing_workaround, &dbg); duke@435: exception = env->ExceptionOccurred(); duke@435: if (exception) { duke@435: env->ExceptionClear(); duke@435: detach_internal(env, this_obj); duke@435: env->Throw(exception); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: /* duke@435: * Iterate over the process mappings looking duke@435: * for libthread and then dlopen the appropriate duke@435: * libthread_db and get function pointers. duke@435: */ duke@435: Pobject_iter(ph, init_libthread_db_ptrs, &dbg); duke@435: exception = env->ExceptionOccurred(); duke@435: if (exception) { duke@435: env->ExceptionClear(); duke@435: if (!sa_ignore_threaddb) { duke@435: detach_internal(env, this_obj); duke@435: env->Throw(exception); duke@435: } duke@435: return; duke@435: } duke@435: duke@435: // init libthread_db and create thread_db agent duke@435: p_td_init_t p_td_init = (p_td_init_t) env->GetLongField(this_obj, p_td_init_ID); duke@435: if (p_td_init == 0) { duke@435: if (!sa_ignore_threaddb) { duke@435: detach_internal(env, this_obj); duke@435: } duke@435: HANDLE_THREADDB_FAILURE("Did not find libthread in target process/core!"); duke@435: } duke@435: duke@435: if (p_td_init() != TD_OK) { duke@435: if (!sa_ignore_threaddb) { duke@435: detach_internal(env, this_obj); duke@435: } duke@435: HANDLE_THREADDB_FAILURE("Can't initialize thread_db!"); duke@435: } duke@435: duke@435: p_td_ta_new_t p_td_ta_new = (p_td_ta_new_t) env->GetLongField(this_obj, p_td_ta_new_ID); duke@435: duke@435: td_thragent_t *p_td_thragent_t = 0; duke@435: if (p_td_ta_new(ph, &p_td_thragent_t) != TD_OK) { duke@435: if (!sa_ignore_threaddb) { duke@435: detach_internal(env, this_obj); duke@435: } duke@435: HANDLE_THREADDB_FAILURE("Can't create thread_db agent!"); duke@435: } duke@435: env->SetLongField(this_obj, p_td_thragent_t_ID, (jlong)(uintptr_t) p_td_thragent_t); duke@435: duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: attach0 duke@435: * Signature: (Ljava/lang/String;)V duke@435: * Description: process detach duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2 duke@435: (JNIEnv *env, jobject this_obj, jstring pid) { duke@435: attach_internal(env, this_obj, pid, JNI_TRUE); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: attach0 duke@435: * Signature: (Ljava/lang/String;Ljava/lang/String;)V duke@435: * Description: core file detach duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 duke@435: (JNIEnv *env, jobject this_obj, jstring executable, jstring corefile) { duke@435: // ignore executable file name, libproc.so can detect a.out name anyway. duke@435: attach_internal(env, this_obj, corefile, JNI_FALSE); duke@435: } duke@435: duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: detach0 duke@435: * Signature: ()V duke@435: * Description: process/core file detach duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_detach0 duke@435: (JNIEnv *env, jobject this_obj) { duke@435: detach_internal(env, this_obj); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: getRemoteProcessAddressSize0 duke@435: * Signature: ()I duke@435: * Description: get process/core address size duke@435: */ duke@435: JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getRemoteProcessAddressSize0 duke@435: (JNIEnv *env, jobject this_obj) { duke@435: jlong p_ps_prochandle; duke@435: p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: int data_model = PR_MODEL_ILP32; duke@435: ps_pdmodel((struct ps_prochandle*) p_ps_prochandle, &data_model); duke@435: print_debug("debuggee is %d bit\n", data_model == PR_MODEL_ILP32? 32 : 64); duke@435: return (jint) data_model == PR_MODEL_ILP32? 32 : 64; duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: getPageSize0 duke@435: * Signature: ()I duke@435: * Description: get process/core page size duke@435: */ duke@435: JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getPageSize0 duke@435: (JNIEnv *env, jobject this_obj) { duke@435: duke@435: /* duke@435: We are not yet attached to a java process or core file. getPageSize is called from duke@435: the constructor of ProcDebuggerLocal. The following won't work! duke@435: duke@435: jlong p_ps_prochandle; duke@435: p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: CHECK_EXCEPTION_(-1); duke@435: struct ps_prochandle* prochandle = (struct ps_prochandle*) p_ps_prochandle; duke@435: return (Pstate(prochandle) == PS_DEAD) ? Pgetauxval(prochandle, AT_PAGESZ) duke@435: : getpagesize(); duke@435: duke@435: So even though core may have been generated with a different page size settings, for now duke@435: call getpagesize. duke@435: */ duke@435: duke@435: return getpagesize(); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: getThreadIntegerRegisterSet0 duke@435: * Signature: (J)[J duke@435: * Description: get gregset for a given thread specified by thread id duke@435: */ duke@435: JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getThreadIntegerRegisterSet0 duke@435: (JNIEnv *env, jobject this_obj, jlong tid) { duke@435: // map the thread id to thread handle duke@435: 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); duke@435: duke@435: td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); duke@435: if (p_td_thragent_t == 0) { duke@435: return 0; duke@435: } duke@435: duke@435: td_thrhandle_t thr_handle; duke@435: if (p_td_ta_map_id2thr(p_td_thragent_t, (thread_t) tid, &thr_handle) != TD_OK) { duke@435: THROW_NEW_DEBUGGER_EXCEPTION_("can't map thread id to thread handle!", 0); duke@435: } duke@435: duke@435: p_td_thr_getgregs_t p_td_thr_getgregs = (p_td_thr_getgregs_t) env->GetLongField(this_obj, p_td_thr_getgregs_ID); duke@435: prgregset_t gregs; duke@435: p_td_thr_getgregs(&thr_handle, gregs); duke@435: duke@435: jlongArray res = env->NewLongArray(NPRGREG); duke@435: CHECK_EXCEPTION_(0); duke@435: jboolean isCopy; duke@435: jlong* ptr = env->GetLongArrayElements(res, &isCopy); duke@435: for (int i = 0; i < NPRGREG; i++) { duke@435: ptr[i] = (jlong) (uintptr_t) gregs[i]; duke@435: } duke@435: env->ReleaseLongArrayElements(res, ptr, JNI_COMMIT); duke@435: return res; duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: fillThreadList0 duke@435: * Signature: (Ljava/util/List;)V duke@435: * Description: fills thread list of the debuggee process/core duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillThreadList0 duke@435: (JNIEnv *env, jobject this_obj, jobject list) { duke@435: duke@435: td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); duke@435: if (p_td_thragent_t == 0) { duke@435: return; duke@435: } duke@435: duke@435: 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); duke@435: duke@435: DebuggerWithObject dbgo; duke@435: dbgo.env = env; duke@435: dbgo.this_obj = this_obj; duke@435: dbgo.obj = list; duke@435: duke@435: p_td_ta_thr_iter(p_td_thragent_t, fill_thread_list, &dbgo, duke@435: TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: fillCFrameList0 duke@435: * Signature: ([J)Lsun/jvm/hotspot/debugger/proc/ProcCFrame; duke@435: * Description: fills CFrame list for a given thread duke@435: */ duke@435: JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillCFrameList0 duke@435: (JNIEnv *env, jobject this_obj, jlongArray regsArray) { duke@435: jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: duke@435: DebuggerWith2Objects dbgo2; duke@435: dbgo2.env = env; duke@435: dbgo2.this_obj = this_obj; duke@435: dbgo2.obj = NULL; duke@435: dbgo2.obj2 = NULL; duke@435: duke@435: jboolean isCopy; duke@435: jlong* ptr = env->GetLongArrayElements(regsArray, &isCopy); duke@435: CHECK_EXCEPTION_(0); duke@435: duke@435: prgregset_t gregs; duke@435: for (int i = 0; i < NPRGREG; i++) { duke@435: gregs[i] = (uintptr_t) ptr[i]; duke@435: } duke@435: duke@435: env->ReleaseLongArrayElements(regsArray, ptr, JNI_ABORT); duke@435: CHECK_EXCEPTION_(0); duke@435: Pstack_iter((struct ps_prochandle*) p_ps_prochandle, gregs, fill_cframe_list, &dbgo2); duke@435: return dbgo2.obj; duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: fillLoadObjectList0 duke@435: * Signature: (Ljava/util/List;)V duke@435: * Description: fills shared objects of the debuggee process/core duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillLoadObjectList0 duke@435: (JNIEnv *env, jobject this_obj, jobject list) { duke@435: DebuggerWithObject dbgo; duke@435: dbgo.env = env; duke@435: dbgo.this_obj = this_obj; duke@435: dbgo.obj = list; duke@435: duke@435: jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: Pobject_iter((struct ps_prochandle*) p_ps_prochandle, fill_load_object_list, &dbgo); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: readBytesFromProcess0 duke@435: * Signature: (JJ)[B duke@435: * Description: read bytes from debuggee process/core duke@435: */ duke@435: JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0 duke@435: (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) { duke@435: duke@435: jbyteArray array = env->NewByteArray(numBytes); duke@435: CHECK_EXCEPTION_(0); duke@435: jboolean isCopy; duke@435: jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy); duke@435: CHECK_EXCEPTION_(0); duke@435: duke@435: jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle, duke@435: (psaddr_t)address, bufPtr, (size_t)numBytes); duke@435: duke@435: if (ret != PS_OK) { duke@435: // part of the class sharing workaround. try shared heap area duke@435: int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID); duke@435: if (classes_jsa_fd != -1 && address != (jlong)0) { duke@435: print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address); duke@435: duke@435: struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID); duke@435: // walk through the shared mappings -- we just have 4 of them. duke@435: // so, linear walking is okay. duke@435: for (int m = 0; m < NUM_SHARED_MAPS; m++) { duke@435: duke@435: // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE duke@435: // and hence will be read by libproc. Besides, the file copy may be duke@435: // stale because the process might have modified those pages. duke@435: if (pheader->_space[m]._read_only) { duke@435: jlong baseAddress = (jlong) (uintptr_t) pheader->_space[m]._base; duke@435: size_t usedSize = pheader->_space[m]._used; duke@435: if (address >= baseAddress && address < (baseAddress + usedSize)) { duke@435: // the given address falls in this shared heap area duke@435: print_debug("found shared map at 0x%lx\n", (long) baseAddress); duke@435: duke@435: duke@435: // If more data is asked than actually mapped from file, we need to zero fill duke@435: // till the end-of-page boundary. But, java array new does that for us. we just duke@435: // need to read as much as data available. duke@435: duke@435: #define MIN2(x, y) (((x) < (y))? (x) : (y)) duke@435: duke@435: jlong diff = address - baseAddress; duke@435: jlong bytesToRead = MIN2(numBytes, usedSize - diff); duke@435: off_t offset = pheader->_space[m]._file_offset + off_t(diff); duke@435: ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset); duke@435: if (bytesRead != bytesToRead) { duke@435: env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); duke@435: print_debug("shared map read failed\n"); duke@435: return jbyteArray(0); duke@435: } else { duke@435: print_debug("shared map read succeeded\n"); duke@435: env->ReleaseByteArrayElements(array, bufPtr, 0); duke@435: return array; duke@435: } duke@435: } // is in current map duke@435: } // is read only map duke@435: } // for shared maps duke@435: } // classes_jsa_fd != -1 duke@435: env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); duke@435: return jbyteArray(0); duke@435: } else { duke@435: env->ReleaseByteArrayElements(array, bufPtr, 0); duke@435: return array; duke@435: } duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: writeBytesToProcess0 duke@435: * Signature: (JJ[B)V duke@435: * Description: write bytes into debugger process duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_writeBytesToProcess0 duke@435: (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes, jbyteArray data) { duke@435: jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: jboolean isCopy; duke@435: jbyte* ptr = env->GetByteArrayElements(data, &isCopy); duke@435: CHECK_EXCEPTION; duke@435: duke@435: if (ps_pwrite((struct ps_prochandle*) p_ps_prochandle, address, ptr, numBytes) != PS_OK) { duke@435: env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); duke@435: THROW_NEW_DEBUGGER_EXCEPTION("Process write failed!"); duke@435: } duke@435: duke@435: env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: suspend0 duke@435: * Signature: ()V duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_suspend0 duke@435: (JNIEnv *env, jobject this_obj) { duke@435: jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: // for now don't check return value. revisit this again. duke@435: Pstop((struct ps_prochandle*) p_ps_prochandle, 1000); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: resume0 duke@435: * Signature: ()V duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_resume0 duke@435: (JNIEnv *env, jobject this_obj) { duke@435: jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: // for now don't check return value. revisit this again. duke@435: Psetrun((struct ps_prochandle*) p_ps_prochandle, 0, PRCFAULT|PRSTOP); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: lookupByName0 duke@435: * Signature: (Ljava/lang/String;Ljava/lang/String;)J duke@435: * Description: symbol lookup by name duke@435: */ duke@435: JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByName0 duke@435: (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { duke@435: jlong p_ps_prochandle; duke@435: p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: duke@435: jboolean isCopy; duke@435: const char* objectName_cstr = NULL; duke@435: if (objectName != NULL) { duke@435: objectName_cstr = env->GetStringUTFChars(objectName, &isCopy); duke@435: CHECK_EXCEPTION_(0); duke@435: } else { duke@435: objectName_cstr = PR_OBJ_EVERY; duke@435: } duke@435: duke@435: const char* symbolName_cstr = env->GetStringUTFChars(symbolName, &isCopy); duke@435: CHECK_EXCEPTION_(0); duke@435: duke@435: psaddr_t symbol_addr = (psaddr_t) 0; duke@435: ps_pglobal_lookup((struct ps_prochandle*) p_ps_prochandle, objectName_cstr, duke@435: symbolName_cstr, &symbol_addr); duke@435: duke@435: if (symbol_addr == 0) { duke@435: print_debug("lookup for %s in %s failed\n", symbolName_cstr, objectName_cstr); duke@435: } duke@435: duke@435: if (objectName_cstr != PR_OBJ_EVERY) { duke@435: env->ReleaseStringUTFChars(objectName, objectName_cstr); duke@435: } duke@435: env->ReleaseStringUTFChars(symbolName, symbolName_cstr); duke@435: return (jlong) (uintptr_t) symbol_addr; duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: lookupByAddress0 duke@435: * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol; duke@435: * Description: lookup symbol name for a given address duke@435: */ duke@435: JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByAddress0 duke@435: (JNIEnv *env, jobject this_obj, jlong address) { duke@435: jlong p_ps_prochandle; duke@435: p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); duke@435: duke@435: char nameBuf[SYMBOL_BUF_SIZE + 1]; duke@435: GElf_Sym sym; duke@435: int res = Plookup_by_addr((struct ps_prochandle*) p_ps_prochandle, (uintptr_t) address, duke@435: nameBuf, sizeof(nameBuf), &sym); duke@435: if (res != 0) { // failed duke@435: return 0; duke@435: } duke@435: duke@435: jstring resSym = env->NewStringUTF(nameBuf); duke@435: CHECK_EXCEPTION_(0); duke@435: duke@435: return env->CallObjectMethod(this_obj, createClosestSymbol_ID, resSym, (address - sym.st_value)); duke@435: } duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: demangle0 duke@435: * Signature: (Ljava/lang/String;)Ljava/lang/String; duke@435: */ duke@435: JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_demangle0 duke@435: (JNIEnv *env, jobject this_object, jstring name) { duke@435: jboolean isCopy; duke@435: const char* ptr = env->GetStringUTFChars(name, &isCopy); duke@435: char buf[2*SYMBOL_BUF_SIZE + 1]; duke@435: jstring res = 0; duke@435: if (cplus_demangle((char*) ptr, buf, sizeof(buf)) != DEMANGLE_ESPACE) { duke@435: res = env->NewStringUTF(buf); duke@435: } else { duke@435: res = name; duke@435: } duke@435: env->ReleaseStringUTFChars(name, ptr); duke@435: return res; duke@435: } duke@435: duke@435: typedef int (*find_file_hook_t)(const char *, int elf_checksum); duke@435: duke@435: /* duke@435: * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal duke@435: * Method: initIDs duke@435: * Signature: ()V duke@435: * Description: get JNI ids for fields and methods of ProcDebuggerLocal class duke@435: */ duke@435: JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_initIDs duke@435: (JNIEnv *env, jclass clazz) { duke@435: _libsaproc_debug = getenv("LIBSAPROC_DEBUG") != NULL; duke@435: if (_libsaproc_debug) { duke@435: // propagate debug mode to libproc.so duke@435: static const char* var = "LIBPROC_DEBUG=1"; duke@435: putenv((char*)var); duke@435: } duke@435: duke@435: void* libproc_handle = dlopen("libproc.so", RTLD_LAZY | RTLD_GLOBAL); duke@435: if (libproc_handle == 0) duke@435: THROW_NEW_DEBUGGER_EXCEPTION("can't load libproc.so, if you are using Solaris 5.7 or below, copy libproc.so from 5.8!"); duke@435: duke@435: // If possible, set shared object find file hook. duke@435: void (*set_hook)(find_file_hook_t) = (void(*)(find_file_hook_t))dlsym(libproc_handle, "Pset_find_file_hook"); duke@435: if (set_hook) { duke@435: // we found find file hook symbol, set up our hook function. duke@435: set_hook(find_file_hook); duke@435: } else if (getenv(SA_ALTROOT)) { duke@435: printf("libsaproc WARNING: %s set, but can't set file hook. " \ duke@435: "Did you use right version of libproc.so?\n", SA_ALTROOT); duke@435: } duke@435: duke@435: p_ps_prochandle_ID = env->GetFieldID(clazz, "p_ps_prochandle", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: libthread_db_handle_ID = env->GetFieldID(clazz, "libthread_db_handle", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_thragent_t_ID = env->GetFieldID(clazz, "p_td_thragent_t", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_init_ID = env->GetFieldID(clazz, "p_td_init", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_ta_new_ID = env->GetFieldID(clazz, "p_td_ta_new", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_ta_delete_ID = env->GetFieldID(clazz, "p_td_ta_delete", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_ta_thr_iter_ID = env->GetFieldID(clazz, "p_td_ta_thr_iter", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_thr_get_info_ID = env->GetFieldID(clazz, "p_td_thr_get_info", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_ta_map_id2thr_ID = env->GetFieldID(clazz, "p_td_ta_map_id2thr", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: p_td_thr_getgregs_ID = env->GetFieldID(clazz, "p_td_thr_getgregs", "J"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: getThreadForThreadId_ID = env->GetMethodID(clazz, duke@435: "getThreadForThreadId", "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: pcRegIndex_ID = env->GetFieldID(clazz, "pcRegIndex", "I"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: fpRegIndex_ID = env->GetFieldID(clazz, "fpRegIndex", "I"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: createSenderFrame_ID = env->GetMethodID(clazz, duke@435: "createSenderFrame", "(Lsun/jvm/hotspot/debugger/proc/ProcCFrame;JJ)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: createLoadObject_ID = env->GetMethodID(clazz, duke@435: "createLoadObject", "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: createClosestSymbol_ID = env->GetMethodID(clazz, duke@435: "createClosestSymbol", "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: listAdd_ID = env->GetMethodID(env->FindClass("java/util/List"), "add", "(Ljava/lang/Object;)Z"); duke@435: CHECK_EXCEPTION; duke@435: duke@435: // part of the class sharing workaround duke@435: classes_jsa_fd_ID = env->GetFieldID(clazz, "classes_jsa_fd", "I"); duke@435: CHECK_EXCEPTION; duke@435: p_file_map_header_ID = env->GetFieldID(clazz, "p_file_map_header", "J"); duke@435: CHECK_EXCEPTION; duke@435: }