saproc.cpp revision 529
/*
* Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
#include "salibproc.h"
#include <thread_db.h>
#include <strings.h>
#include <limits.h>
#include <demangle.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throwNewDebuggerException(env, str); return value; }
#define SYMBOL_BUF_SIZE 256
// debug mode
static int _libsaproc_debug = 0;
static void print_debug(const char* format,...) {
if (_libsaproc_debug) {
}
}
struct Debugger {
};
struct DebuggerWithObject : Debugger {
};
struct DebuggerWith2Objects : DebuggerWithObject {
};
/*
* Portions of user thread level detail gathering code is from pstack source
* code. See pstack.c in Solaris 2.8 user commands source code.
*/
}
// JNI ids for some fields, methods
// libproc handler pointer
static jfieldID p_ps_prochandle_ID = 0;
// libthread.so dlopen handle, thread agent ptr and function pointers
static jfieldID libthread_db_handle_ID = 0;
static jfieldID p_td_thragent_t_ID = 0;
static jfieldID p_td_init_ID = 0;
static jfieldID p_td_ta_new_ID = 0;
static jfieldID p_td_ta_delete_ID = 0;
static jfieldID p_td_ta_thr_iter_ID = 0;
static jfieldID p_td_thr_get_info_ID = 0;
static jfieldID p_td_ta_map_id2thr_ID = 0;
static jfieldID p_td_thr_getgregs_ID = 0;
// reg index fields
static jfieldID pcRegIndex_ID = 0;
static jfieldID fpRegIndex_ID = 0;
// part of the class sharing workaround
static jfieldID classes_jsa_fd_ID = 0;
static jfieldID p_file_map_header_ID = 0;
// method ids
static jmethodID getThreadForThreadId_ID = 0;
static jmethodID createSenderFrame_ID = 0;
static jmethodID createLoadObject_ID = 0;
static jmethodID createClosestSymbol_ID = 0;
static jmethodID listAdd_ID = 0;
/*
* Functions we need from libthread_db
*/
typedef td_err_e
(*p_td_init_t)(void);
typedef td_err_e
(*p_td_ta_new_t)(void *, td_thragent_t **);
typedef td_err_e
(*p_td_ta_delete_t)(td_thragent_t *);
typedef td_err_e
td_thr_state_e, int, sigset_t *, unsigned);
typedef td_err_e
typedef td_err_e
typedef td_err_e
static void
// release libthread_db agent, if we had created
td_thragent_t *p_td_thragent_t = 0;
if (p_td_thragent_t != 0 && p_td_ta_delete != 0) {
}
// dlclose libthread_db.so
if (libthread_db_handle != 0) {
}
}
// clear libthread_db stuff
// release ptr to ps_prochandle
if (p_ps_prochandle != 0L) {
}
// part of the class sharing workaround
if (classes_jsa_fd != -1) {
struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
}
}
}
// Is it okay to ignore libthread_db failure? Set env var to ignore
// libthread_db failure. You can still debug, but will miss threads
// related functionality.
#define HANDLE_THREADDB_FAILURE(msg) \
if (sa_ignore_threaddb) { \
return; \
} else { \
}
if (sa_ignore_threaddb) { \
return ret; \
} else { \
}
static int alt_root_len = -1;
#define SA_ALTROOT "SA_ALTROOT"
static void init_alt_root() {
if (alt_root_len == -1) {
if (alt_root)
else
alt_root_len = 0;
}
}
if (_libsaproc_debug) {
}
if (alt_root_len > 0) {
int fd = -1;
if (fd >= 0) {
if (_libsaproc_debug) {
}
return fd;
}
if (fd >= 0) {
if (_libsaproc_debug) {
}
return fd;
}
}
}
return -1;
}
static int pathmap_open(const char* name) {
if (fd < 0) {
}
return fd;
}
if (_libsaproc_debug) {
}
if (alt_root_len > 0) {
if (_libsaproc_debug && handle) {
}
if (_libsaproc_debug && handle) {
}
}
}
}
if (_libsaproc_debug) {
}
return handle;
}
// libproc and libthread_db callback functions
extern "C" {
static int
char libthread_db[PATH_MAX];
return (0);
/*
* We found a libthread.
* dlopen() the matching libthread_db and get the thread agent handle.
*/
s2 += 3;
} else {
#ifdef _LP64
/*
* The victim process is 32-bit, we are 64-bit.
* We have to find the 64-bit version of libthread_db
* that matches the victim's 32-bit version of libthread.
*/
s2 += 3;
s2 += 3;
#else
return (0);
#endif /* _LP64 */
}
void* libthread_db_handle = 0;
}
void* tmpPtr = 0;
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_init!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_new!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_delete!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_thr_iter!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_get_info!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_map_id2thr!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_getgregs!", 0);
}
return 1;
}
static int
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);
return (0);
jobject threadProxy = env->CallObjectMethod(this_obj, getThreadForThreadId_ID, (jlong)(uintptr_t) thrinfo.ti_tid);
CHECK_EXCEPTION_(1);
CHECK_EXCEPTION_(1);
return 0;
}
static int
if (obj_name) {
CHECK_EXCEPTION_(1);
CHECK_EXCEPTION_(1);
CHECK_EXCEPTION_(1);
}
return 0;
}
static int
CHECK_EXCEPTION_(1);
}
return 0;
}
// part of the class sharing workaround
// FIXME: !!HACK ALERT!!
// The format of sharing achive file header is needed to read shared heap
// file mappings. For now, I am hard coding portion of FileMapHeader here.
// Refer to filemap.hpp.
// FileMapHeader describes the shared space data in the file to be
// mapped. This structure gets written to a file. It is not a class, so
// that the compilers don't add any compiler-private data to it.
// Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp
const int NUM_SHARED_MAPS = 4;
// Refer to FileMapInfo::_current_version in filemap.hpp
const int CURRENT_ARCHIVE_VERSION = 1;
struct FileMapHeader {
int _magic; // identify file type.
int _version; // (from enum, above.)
struct space_info {
int _file_offset; // sizeof(this) rounded to vm page size
char* _base; // copy-on-write base address
bool _read_only; // read only space?
bool _allow_exec; // executable code in space?
// Ignore the rest of the FileMapHeader. We don't need those fields here.
};
static bool
jboolean i;
*pvalue = i;
return true;
} else {
return false;
}
}
static bool
return true;
} else {
return false;
}
}
static bool
char ch = ' ';
size_t i = 0;
while (ch != '\0') {
return false;
if (i < size - 1) {
} else { // smaller buffer
return false;
}
i++; addr++;
}
buf[i] = '\0';
return true;
}
#define USE_SHARED_SPACES_SYM "UseSharedSpaces"
// mangled symbol name for Arguments::SharedArchivePath
#define SHARED_ARCHIVE_PATH_SYM "__1cJArgumentsRSharedArchivePath_"
static int
const char* jvm_name = 0;
} else {
return 0;
}
// initialize classes[_g].jsa file descriptor field.
// check whether class sharing is on by reading variable "UseSharedSpaces"
if (useSharedSpacesAddr == 0) {
}
// read the value of the flag "UseSharedSpaces"
// Since hotspot types are not available to build this library. So
// equivalent type "jboolean" is used to read the value of "UseSharedSpaces"
// which is same as hotspot type "bool".
} else if ((int)value == 0) {
print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
return 1;
}
char classes_jsa[PATH_MAX];
if (sharedArchivePathAddrAddr == 0) {
print_debug("can't find symbol 'Arguments::SharedArchivePath'\n");
}
print_debug("can't find read pointer 'Arguments::SharedArchivePath'\n");
}
print_debug("can't find read 'Arguments::SharedArchivePath' value\n");
}
// open the classes[_g].jsa
if (fd < 0) {
char errMsg[ERR_MSG_SIZE];
} else {
}
// parse classes[_g].jsa
}
// read FileMapHeader
if (n != sizeof(struct FileMapHeader)) {
char errMsg[ERR_MSG_SIZE];
}
// check file magic
char errMsg[ERR_MSG_SIZE];
}
// check version
char errMsg[ERR_MSG_SIZE];
}
if (_libsaproc_debug) {
for (int m = 0; m < NUM_SHARED_MAPS; m++) {
print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n",
}
}
// FIXME: For now, omitting other checks such as VM version etc.
// store class archive file fd and map header in debugger object fields
return 1;
}
} // extern "C"
// error messages for proc_arg_grab failure codes. The messages are
// modified versions of comments against corresponding #defines in
// libproc.h.
static const char* proc_arg_grab_errmsgs[] = {
"",
/* G_NOPROC */ "No such process",
/* G_NOCORE */ "No such core file",
/* G_NOPROCORCORE */ "No such process or core",
/* G_NOEXEC */ "Cannot locate executable file",
/* G_ZOMB */ "Zombie processs",
/* G_PERM */ "No permission to attach",
/* G_BUSY */ "Another process has already attached",
/* G_SYS */ "System process - can not attach",
/* G_SELF */ "Process is self - can't debug myself!",
/* G_INTR */ "Interrupt received while grabbing",
/* G_LP64 */ "debuggee is 64 bit, use java -d64 for debugger",
/* G_FORMAT */ "File is not an ELF format core file - corrupted core?",
/* G_ELF */ "Libelf error while parsing an ELF file",
/* G_NOTE */ "Required PT_NOTE Phdr not present - corrupted core?",
};
int gcode;
// some older versions of libproc.so crash when trying to attach 32 bit
// debugger to 64 bit core file. check and throw error.
#ifndef _LP64
if (errno) {
// core file
int core_fd;
THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use java -d64 for debugger");
}
}
// all other conditions are handled by libproc.so.
}
#endif
struct ps_prochandle* ph = proc_arg_grab(cmdLine_cstr, (isProcess? PR_ARG_PIDS : PR_ARG_CORES), PGRAB_FORCE, &gcode);
if (! ph) {
char errMsg[ERR_MSG_SIZE];
} else {
perror("libsaproc DEBUG: ");
}
if (isProcess) {
THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to process!");
} else {
THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to core file!");
}
}
}
// even though libproc.so supports 64 bit debugger and 32 bit debuggee, we don't
// support such cross-bit-debugging. check for that combination and throw error.
#ifdef _LP64
int data_model;
THROW_NEW_DEBUGGER_EXCEPTION("can't determine debuggee data model (ILP32? or LP64?)");
}
if (data_model == PR_MODEL_ILP32) {
THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger");
}
#endif
jthrowable exception = 0;
if (! isProcess) {
/*
* With class sharing, shared perm. gen heap is allocated in with MAP_SHARED|PROT_READ.
* These pages are mapped from the file "classes[_g].jsa". MAP_SHARED pages are not dumped
* in Solaris core.To read shared heap pages, we have to read classes[_g].jsa file.
*/
if (exception) {
env->ExceptionClear();
return;
}
}
/*
* Iterate over the process mappings looking
* for libthread and then dlopen the appropriate
* libthread_db and get function pointers.
*/
if (exception) {
env->ExceptionClear();
if (!sa_ignore_threaddb) {
}
return;
}
// init libthread_db and create thread_db agent
if (p_td_init == 0) {
if (!sa_ignore_threaddb) {
}
HANDLE_THREADDB_FAILURE("Did not find libthread in target process/core!");
}
if (!sa_ignore_threaddb) {
}
HANDLE_THREADDB_FAILURE("Can't initialize thread_db!");
}
td_thragent_t *p_td_thragent_t = 0;
if (!sa_ignore_threaddb) {
}
HANDLE_THREADDB_FAILURE("Can't create thread_db agent!");
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: attach0
* Description: process detach
*/
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: attach0
* Description: core file detach
*/
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
// ignore executable file name, libproc.so can detect a.out name anyway.
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: detach0
* Signature: ()V
*/
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: getRemoteProcessAddressSize0
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getRemoteProcessAddressSize0
int data_model = PR_MODEL_ILP32;
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: getPageSize0
* Signature: ()I
*/
/*
We are not yet attached to a java process or core file. getPageSize is called from
the constructor of ProcDebuggerLocal. The following won't work!
jlong p_ps_prochandle;
p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
CHECK_EXCEPTION_(-1);
struct ps_prochandle* prochandle = (struct ps_prochandle*) p_ps_prochandle;
return (Pstate(prochandle) == PS_DEAD) ? Pgetauxval(prochandle, AT_PAGESZ)
: getpagesize();
So even though core may have been generated with a different page size settings, for now
call getpagesize.
*/
return getpagesize();
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: getThreadIntegerRegisterSet0
* Signature: (J)[J
* Description: get gregset for a given thread specified by thread id
*/
JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getThreadIntegerRegisterSet0
// map the thread id to thread handle
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);
if (p_td_thragent_t == 0) {
return 0;
}
THROW_NEW_DEBUGGER_EXCEPTION_("can't map thread id to thread handle!", 0);
}
p_td_thr_getgregs_t p_td_thr_getgregs = (p_td_thr_getgregs_t) env->GetLongField(this_obj, p_td_thr_getgregs_ID);
CHECK_EXCEPTION_(0);
for (int i = 0; i < NPRGREG; i++) {
}
return res;
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: fillThreadList0
*/
if (p_td_thragent_t == 0) {
return;
}
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);
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: fillCFrameList0
* Description: fills CFrame list for a given thread
*/
CHECK_EXCEPTION_(0);
for (int i = 0; i < NPRGREG; i++) {
}
CHECK_EXCEPTION_(0);
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: fillLoadObjectList0
*/
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: readBytesFromProcess0
* Signature: (JJ)[B
*/
JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0
CHECK_EXCEPTION_(0);
CHECK_EXCEPTION_(0);
// part of the class sharing workaround. try shared heap area
struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
// walk through the shared mappings -- we just have 4 of them.
// so, linear walking is okay.
for (int m = 0; m < NUM_SHARED_MAPS; m++) {
// We can skip the non-read-only maps. These are mapped as MAP_PRIVATE
// and hence will be read by libproc. Besides, the file copy may be
// stale because the process might have modified those pages.
// the given address falls in this shared heap area
// If more data is asked than actually mapped from file, we need to zero fill
// till the end-of-page boundary. But, java array new does that for us. we just
// need to read as much as data available.
#define MIN2(x, y) (((x) < (y))? (x) : (y))
if (bytesRead != bytesToRead) {
print_debug("shared map read failed\n");
return jbyteArray(0);
} else {
print_debug("shared map read succeeded\n");
return array;
}
} // is in current map
} // is read only map
} // for shared maps
} // classes_jsa_fd != -1
return jbyteArray(0);
} else {
return array;
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: writeBytesToProcess0
* Signature: (JJ[B)V
* Description: write bytes into debugger process
*/
THROW_NEW_DEBUGGER_EXCEPTION("Process write failed!");
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: suspend0
* Signature: ()V
*/
// for now don't check return value. revisit this again.
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: resume0
* Signature: ()V
*/
// for now don't check return value. revisit this again.
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: lookupByName0
* Description: symbol lookup by name
*/
const char* objectName_cstr = NULL;
if (objectName != NULL) {
CHECK_EXCEPTION_(0);
} else {
}
CHECK_EXCEPTION_(0);
if (symbol_addr == 0) {
}
if (objectName_cstr != PR_OBJ_EVERY) {
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: lookupByAddress0
* Description: lookup symbol name for a given address
*/
if (res != 0) { // failed
return 0;
}
CHECK_EXCEPTION_(0);
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: demangle0
*/
} else {
}
return res;
}
typedef int (*find_file_hook_t)(const char *, int elf_checksum);
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: initIDs
* Signature: ()V
* Description: get JNI ids for fields and methods of ProcDebuggerLocal class
*/
if (_libsaproc_debug) {
// propagate debug mode to libproc.so
static const char* var = "LIBPROC_DEBUG=1";
}
if (libproc_handle == 0)
THROW_NEW_DEBUGGER_EXCEPTION("can't load libproc.so, if you are using Solaris 5.7 or below, copy libproc.so from 5.8!");
// If possible, set shared object find file hook.
void (*set_hook)(find_file_hook_t) = (void(*)(find_file_hook_t))dlsym(libproc_handle, "Pset_find_file_hook");
if (set_hook) {
// we found find file hook symbol, set up our hook function.
} else if (getenv(SA_ALTROOT)) {
printf("libsaproc WARNING: %s set, but can't set file hook. " \
"Did you use right version of libproc.so?\n", SA_ALTROOT);
}
"getThreadForThreadId", "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;");
"createSenderFrame", "(Lsun/jvm/hotspot/debugger/proc/ProcCFrame;JJ)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;");
// part of the class sharing workaround
}