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