提交 8458636d 编写于 作者: M minqi

8003348: SA can not read core file on OS

Summary: Macosx uses Mach-O file format for binary files, not ELF format. Currently SA works on core files on other platforms, t his change enables SA work on core file generated on Darwin.
Reviewed-by: sla, sspitsyn
Contributed-by: yumin.qi@oracle.com
上级 cdc18c13
/*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -40,12 +40,34 @@
#import <errno.h>
#import <sys/types.h>
#import <sys/ptrace.h>
#include "libproc_impl.h"
jboolean debug = JNI_FALSE;
#define UNSUPPORTED_ARCH "Unsupported architecture!"
#if defined(x86_64) && !defined(amd64)
#define amd64 1
#endif
#if amd64
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
#else
#error UNSUPPORTED_ARCH
#endif
static jfieldID symbolicatorID = 0; // set in _init0
static jfieldID taskID = 0; // set in _init0
static jfieldID p_ps_prochandle_ID = 0;
static jfieldID loadObjectList_ID = 0;
static jmethodID listAdd_ID = 0;
static jmethodID createClosestSymbol_ID = 0;
static jmethodID createLoadObject_ID = 0;
static jmethodID getJavaThreadsInfo_ID = 0;
// indicator if thread id (lwpid_t) was set
static bool _threads_filled = false;
static void putSymbolicator(JNIEnv *env, jobject this_obj, id symbolicator) {
(*env)->SetLongField(env, this_obj, symbolicatorID, (jlong)(intptr_t)symbolicator);
}
......@@ -76,6 +98,11 @@ static void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
(*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
}
static struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) {
jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID);
return (struct ps_prochandle*)(intptr_t)ptr;
}
#if defined(__i386__)
#define hsdb_thread_state_t x86_thread_state32_t
#define hsdb_float_state_t x86_float_state32_t
......@@ -91,7 +118,7 @@ static void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
#define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
#define HSDB_FLOAT_STATE_COUNT x86_FLOAT_STATE64_COUNT
#else
#error "Unsupported architecture"
#error UNSUPPORTED_ARCH
#endif
/*
......@@ -104,6 +131,66 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv *env, jclass cls
symbolicatorID = (*env)->GetFieldID(env, cls, "symbolicator", "J");
taskID = (*env)->GetFieldID(env, cls, "task", "J");
CHECK_EXCEPTION;
// for core file
p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J");
CHECK_EXCEPTION;
loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;");
CHECK_EXCEPTION;
// methods we use
createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol",
"(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
CHECK_EXCEPTION;
createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject",
"(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;");
CHECK_EXCEPTION;
// java.util.List method we call
jclass listClass = (*env)->FindClass(env, "java/util/List");
CHECK_EXCEPTION;
listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z");
CHECK_EXCEPTION;
getJavaThreadsInfo_ID = (*env)->GetMethodID(env, cls, "getJavaThreadsInfo",
"()[J");
CHECK_EXCEPTION;
init_libproc(getenv("LIBSAPROC_DEBUG") != NULL);
}
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getAddressSize
(JNIEnv *env, jclass cls)
{
#ifdef _LP64
return 8;
#else
#error UNSUPPORTED_ARCH
#endif
}
/** called by Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0 */
jlong lookupByNameIncore(
JNIEnv *env, struct ps_prochandle *ph, jobject this_obj, jstring objectName, jstring symbolName)
{
const char *objectName_cstr, *symbolName_cstr;
jlong addr;
jboolean isCopy;
objectName_cstr = NULL;
if (objectName != NULL) {
objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
CHECK_EXCEPTION_(0);
}
symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
CHECK_EXCEPTION_(0);
print_debug("look for %s \n", symbolName_cstr);
addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
if (objectName_cstr != NULL) {
(*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
}
(*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
return addr;
}
/*
......@@ -116,14 +203,17 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0(
JNIEnv *env, jobject this_obj,
jstring objectName, jstring symbolName)
{
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
if (ph->core != NULL) {
return lookupByNameIncore(env, ph, this_obj, objectName, symbolName);
}
jlong address = 0;
JNF_COCOA_ENTER(env);
NSString *symbolNameString = JNFJavaToNSString(env, symbolName);
if (debug) {
printf("lookupInProcess called for %s\n", [symbolNameString UTF8String]);
}
print_debug("lookupInProcess called for %s\n", [symbolNameString UTF8String]);
id symbolicator = getSymbolicator(env, this_obj);
if (symbolicator != nil) {
......@@ -131,14 +221,48 @@ JNF_COCOA_ENTER(env);
address = (jlong) dynamicCall(symbolicator, @selector(addressForSymbol:), symbolNameString);
}
if (debug) {
printf("address of symbol %s = %llx\n", [symbolNameString UTF8String], address);
}
print_debug("address of symbol %s = %llx\n", [symbolNameString UTF8String], address);
JNF_COCOA_EXIT(env);
return address;
}
/*
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
* Method: lookupByAddress0
* Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
*/
JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByAddress0
(JNIEnv *env, jobject this_obj, jlong addr) {
uintptr_t offset;
const char* sym = NULL;
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
sym = symbol_for_pc(ph, (uintptr_t) addr, &offset);
if (sym == NULL) return 0;
return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID,
(*env)->NewStringUTF(env, sym), (jlong)offset);
}
/** called from Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0 */
jbyteArray readBytesFromCore(
JNIEnv *env, struct ps_prochandle *ph, jobject this_obj, jlong addr, jlong numBytes)
{
jboolean isCopy;
jbyteArray array;
jbyte *bufPtr;
ps_err_e err;
array = (*env)->NewByteArray(env, numBytes);
CHECK_EXCEPTION_(0);
bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
CHECK_EXCEPTION_(0);
err = ps_pread(ph, (psaddr_t) (uintptr_t)addr, bufPtr, numBytes);
(*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
return (err == PS_OK)? array : 0;
}
/*
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
* Method: readBytesFromProcess0
......@@ -149,12 +273,15 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
JNIEnv *env, jobject this_obj,
jlong addr, jlong numBytes)
{
if (debug) printf("readBytesFromProcess called. addr = %llx numBytes = %lld\n", addr, numBytes);
print_debug("readBytesFromProcess called. addr = %llx numBytes = %lld\n", addr, numBytes);
// must allocate storage instead of using former parameter buf
jboolean isCopy;
jbyteArray array;
jbyte *bufPtr;
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
if (ph->core != NULL) {
return readBytesFromCore(env, ph, this_obj, addr, numBytes);
}
array = (*env)->NewByteArray(env, numBytes);
CHECK_EXCEPTION_(0);
......@@ -189,7 +316,7 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
// assume all failures are unmapped pages
}
if (debug) fprintf(stderr, "%ld pages\n", pageCount);
print_debug("%ld pages\n", pageCount);
remaining = numBytes;
......@@ -207,7 +334,7 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
}
if (mapped[i]) {
if (debug) fprintf(stderr, "page %d mapped (len %ld start %ld)\n", i, len, start);
print_debug("page %d mapped (len %ld start %ld)\n", i, len, start);
(*env)->SetByteArrayRegion(env, array, 0, len, ((jbyte *) pages[i] + start));
vm_deallocate(mach_task_self(), pages[i], vm_page_size);
}
......@@ -220,6 +347,115 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
return array;
}
/** Only used for core file reading, set thread_id for threads which is got after core file parsed.
* Thread context is available in Mach-O core file but thread id is not. We can get thread id
* from Threads which store all java threads information when they are created. Here we can identify
* them as java threads by checking if a thread's rsp or rbp within a java thread's stack.
* Note Macosx uses unique_thread_id which is different from other platforms though printed ids
* are still pthread id. Function BsdDebuggerLocal.getJavaThreadsInfo returns an array of long
* integers to host all java threads' id, stack_start, stack_end as:
* [uid0, stack_start0, stack_end0, uid1, stack_start1, stack_end1, ...]
*
* The work cannot be done at init0 since Threads is not available yet(VM not initialized yet).
* This function should be called only once if succeeded
*/
bool fill_java_threads(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
int n = 0, i = 0, j;
struct reg regs;
jlongArray thrinfos = (*env)->CallObjectMethod(env, this_obj, getJavaThreadsInfo_ID);
CHECK_EXCEPTION_(false);
int len = (int)(*env)->GetArrayLength(env, thrinfos);
uint64_t* cinfos = (uint64_t *)(*env)->GetLongArrayElements(env, thrinfos, NULL);
CHECK_EXCEPTION_(false);
n = get_num_threads(ph);
print_debug("fill_java_threads called, num_of_thread = %d\n", n);
for (i = 0; i < n; i++) {
if (!get_nth_lwp_regs(ph, i, &regs)) {
print_debug("Could not get regs of thread %d, already set!\n", i);
return false;
}
for (j = 0; j < len; j += 3) {
lwpid_t uid = cinfos[j];
uint64_t beg = cinfos[j + 1];
uint64_t end = cinfos[j + 2];
if ((regs.r_rsp < end && regs.r_rsp >= beg) ||
(regs.r_rbp < end && regs.r_rbp >= beg)) {
set_lwp_id(ph, i, uid);
break;
}
}
}
(*env)->ReleaseLongArrayElements(env, thrinfos, (jlong*)cinfos, 0);
CHECK_EXCEPTION_(false);
return true;
}
/* For core file only, called from
* Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0
*/
jlongArray getThreadIntegerRegisterSetFromCore(JNIEnv *env, jobject this_obj, long lwp_id) {
if (!_threads_filled) {
if (!fill_java_threads(env, this_obj, get_proc_handle(env, this_obj))) {
throw_new_debugger_exception(env, "Failed to fill in threads");
return 0;
} else {
_threads_filled = true;
}
}
struct reg gregs;
jboolean isCopy;
jlongArray array;
jlong *regs;
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0);
}
#undef NPRGREG
#undef REG_INDEX
#if amd64
#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
array = (*env)->NewLongArray(env, NPRGREG);
CHECK_EXCEPTION_(0);
regs = (*env)->GetLongArrayElements(env, array, &isCopy);
regs[REG_INDEX(R15)] = gregs.r_r15;
regs[REG_INDEX(R14)] = gregs.r_r14;
regs[REG_INDEX(R13)] = gregs.r_r13;
regs[REG_INDEX(R12)] = gregs.r_r12;
regs[REG_INDEX(RBP)] = gregs.r_rbp;
regs[REG_INDEX(RBX)] = gregs.r_rbx;
regs[REG_INDEX(R11)] = gregs.r_r11;
regs[REG_INDEX(R10)] = gregs.r_r10;
regs[REG_INDEX(R9)] = gregs.r_r9;
regs[REG_INDEX(R8)] = gregs.r_r8;
regs[REG_INDEX(RAX)] = gregs.r_rax;
regs[REG_INDEX(RCX)] = gregs.r_rcx;
regs[REG_INDEX(RDX)] = gregs.r_rdx;
regs[REG_INDEX(RSI)] = gregs.r_rsi;
regs[REG_INDEX(RDI)] = gregs.r_rdi;
regs[REG_INDEX(RIP)] = gregs.r_rip;
regs[REG_INDEX(CS)] = gregs.r_cs;
regs[REG_INDEX(RSP)] = gregs.r_rsp;
regs[REG_INDEX(SS)] = gregs.r_ss;
regs[REG_INDEX(FSBASE)] = 0;
regs[REG_INDEX(GSBASE)] = 0;
regs[REG_INDEX(DS)] = gregs.r_ds;
regs[REG_INDEX(ES)] = gregs.r_es;
regs[REG_INDEX(FS)] = gregs.r_fs;
regs[REG_INDEX(GS)] = gregs.r_gs;
regs[REG_INDEX(TRAPNO)] = gregs.r_trapno;
regs[REG_INDEX(RFL)] = gregs.r_rflags;
#endif /* amd64 */
(*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
return array;
}
/*
* Lookup the thread_t that corresponds to the given thread_id.
......@@ -232,9 +468,7 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
*/
thread_t
lookupThreadFromThreadId(task_t task, jlong thread_id) {
if (debug) {
printf("lookupThreadFromThreadId thread_id=0x%llx\n", thread_id);
}
print_debug("lookupThreadFromThreadId thread_id=0x%llx\n", thread_id);
thread_array_t thread_list = NULL;
mach_msg_type_number_t thread_list_count = 0;
......@@ -244,9 +478,7 @@ lookupThreadFromThreadId(task_t task, jlong thread_id) {
// get the list of all the send rights
kern_return_t result = task_threads(task, &thread_list, &thread_list_count);
if (result != KERN_SUCCESS) {
if (debug) {
printf("task_threads returned 0x%x\n", result);
}
print_debug("task_threads returned 0x%x\n", result);
return 0;
}
......@@ -257,9 +489,7 @@ lookupThreadFromThreadId(task_t task, jlong thread_id) {
// get the THREAD_IDENTIFIER_INFO for the send right
result = thread_info(thread_list[i], THREAD_IDENTIFIER_INFO, (thread_info_t) &m_ident_info, &count);
if (result != KERN_SUCCESS) {
if (debug) {
printf("thread_info returned 0x%x\n", result);
}
print_debug("thread_info returned 0x%x\n", result);
break;
}
......@@ -288,15 +518,17 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(
JNIEnv *env, jobject this_obj,
jlong thread_id)
{
if (debug)
printf("getThreadRegisterSet0 called\n");
print_debug("getThreadRegisterSet0 called\n");
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
if (ph->core != NULL) {
return getThreadIntegerRegisterSetFromCore(env, this_obj, thread_id);
}
kern_return_t result;
thread_t tid;
mach_msg_type_number_t count = HSDB_THREAD_STATE_COUNT;
hsdb_thread_state_t state;
unsigned int *r;
int i;
jlongArray registerArray;
jlong *primitiveArray;
task_t gTask = getTask(env, this_obj);
......@@ -306,97 +538,56 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(
result = thread_get_state(tid, HSDB_THREAD_STATE, (thread_state_t)&state, &count);
if (result != KERN_SUCCESS) {
if (debug)
printf("getregs: thread_get_state(%d) failed (%d)\n", tid, result);
print_error("getregs: thread_get_state(%d) failed (%d)\n", tid, result);
return NULL;
}
// 40 32-bit registers on ppc, 16 on x86.
// Output order is the same as the order in the ppc_thread_state/i386_thread_state struct.
#if defined(__i386__)
r = (unsigned int *)&state;
registerArray = (*env)->NewLongArray(env, 8);
primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
primitiveArray[0] = r[0]; // eax
primitiveArray[1] = r[2]; // ecx
primitiveArray[2] = r[3]; // edx
primitiveArray[3] = r[1]; // ebx
primitiveArray[4] = r[7]; // esp
primitiveArray[5] = r[6]; // ebp
primitiveArray[6] = r[5]; // esi
primitiveArray[7] = r[4]; // edi
(*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
#elif defined(__x86_64__)
/* From AMD64ThreadContext.java
public static final int R15 = 0;
public static final int R14 = 1;
public static final int R13 = 2;
public static final int R12 = 3;
public static final int R11 = 4;
public static final int R10 = 5;
public static final int R9 = 6;
public static final int R8 = 7;
public static final int RDI = 8;
public static final int RSI = 9;
public static final int RBP = 10;
public static final int RBX = 11;
public static final int RDX = 12;
public static final int RCX = 13;
public static final int RAX = 14;
public static final int TRAPNO = 15;
public static final int ERR = 16;
public static final int RIP = 17;
public static final int CS = 18;
public static final int RFL = 19;
public static final int RSP = 20;
public static final int SS = 21;
public static final int FS = 22;
public static final int GS = 23;
public static final int ES = 24;
public static final int DS = 25;
public static final int FSBASE = 26;
public static final int GSBASE = 27;
*/
#if amd64
#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
#undef REG_INDEX
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
// 64 bit
if (debug) printf("Getting threads for a 64-bit process\n");
registerArray = (*env)->NewLongArray(env, 28);
print_debug("Getting threads for a 64-bit process\n");
registerArray = (*env)->NewLongArray(env, NPRGREG);
CHECK_EXCEPTION_(0);
primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
primitiveArray[0] = state.__r15;
primitiveArray[1] = state.__r14;
primitiveArray[2] = state.__r13;
primitiveArray[3] = state.__r12;
primitiveArray[4] = state.__r11;
primitiveArray[5] = state.__r10;
primitiveArray[6] = state.__r9;
primitiveArray[7] = state.__r8;
primitiveArray[8] = state.__rdi;
primitiveArray[9] = state.__rsi;
primitiveArray[10] = state.__rbp;
primitiveArray[11] = state.__rbx;
primitiveArray[12] = state.__rdx;
primitiveArray[13] = state.__rcx;
primitiveArray[14] = state.__rax;
primitiveArray[15] = 0; // trapno ?
primitiveArray[16] = 0; // err ?
primitiveArray[17] = state.__rip;
primitiveArray[18] = state.__cs;
primitiveArray[19] = state.__rflags;
primitiveArray[20] = state.__rsp;
primitiveArray[21] = 0; // We don't have SS
primitiveArray[22] = state.__fs;
primitiveArray[23] = state.__gs;
primitiveArray[24] = 0;
primitiveArray[25] = 0;
primitiveArray[26] = 0;
primitiveArray[27] = 0;
if (debug) printf("set registers\n");
primitiveArray[REG_INDEX(R15)] = state.__r15;
primitiveArray[REG_INDEX(R14)] = state.__r14;
primitiveArray[REG_INDEX(R13)] = state.__r13;
primitiveArray[REG_INDEX(R12)] = state.__r12;
primitiveArray[REG_INDEX(R11)] = state.__r11;
primitiveArray[REG_INDEX(R10)] = state.__r10;
primitiveArray[REG_INDEX(R9)] = state.__r9;
primitiveArray[REG_INDEX(R8)] = state.__r8;
primitiveArray[REG_INDEX(RDI)] = state.__rdi;
primitiveArray[REG_INDEX(RSI)] = state.__rsi;
primitiveArray[REG_INDEX(RBP)] = state.__rbp;
primitiveArray[REG_INDEX(RBX)] = state.__rbx;
primitiveArray[REG_INDEX(RDX)] = state.__rdx;
primitiveArray[REG_INDEX(RCX)] = state.__rcx;
primitiveArray[REG_INDEX(RAX)] = state.__rax;
primitiveArray[REG_INDEX(TRAPNO)] = 0; // trapno, not used
primitiveArray[REG_INDEX(ERR)] = 0; // err, not used
primitiveArray[REG_INDEX(RIP)] = state.__rip;
primitiveArray[REG_INDEX(CS)] = state.__cs;
primitiveArray[REG_INDEX(RFL)] = state.__rflags;
primitiveArray[REG_INDEX(RSP)] = state.__rsp;
primitiveArray[REG_INDEX(SS)] = 0; // We don't have SS
primitiveArray[REG_INDEX(FS)] = state.__fs;
primitiveArray[REG_INDEX(GS)] = state.__gs;
primitiveArray[REG_INDEX(ES)] = 0;
primitiveArray[REG_INDEX(DS)] = 0;
primitiveArray[REG_INDEX(FSBASE)] = 0;
primitiveArray[REG_INDEX(GSBASE)] = 0;
print_debug("set registers\n");
(*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
#else
#error Unsupported architecture
#endif
#error UNSUPPORTED_ARCH
#endif /* amd64 */
return registerArray;
}
......@@ -410,8 +601,7 @@ JNIEXPORT jint JNICALL
Java_sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal_translateTID0(
JNIEnv *env, jobject this_obj, jint tid)
{
if (debug)
printf("translateTID0 called on tid = 0x%x\n", (int)tid);
print_debug("translateTID0 called on tid = 0x%x\n", (int)tid);
kern_return_t result;
thread_t foreign_tid, usable_tid;
......@@ -426,8 +616,7 @@ Java_sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal_translateTID0(
if (result != KERN_SUCCESS)
return -1;
if (debug)
printf("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
print_debug("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
return (jint) usable_tid;
}
......@@ -437,7 +626,7 @@ static bool ptrace_continue(pid_t pid, int signal) {
// pass the signal to the process so we don't swallow it
int res;
if ((res = ptrace(PT_CONTINUE, pid, (caddr_t)1, signal)) < 0) {
fprintf(stderr, "attach: ptrace(PT_CONTINUE, %d) failed with %d\n", pid, res);
print_error("attach: ptrace(PT_CONTINUE, %d) failed with %d\n", pid, res);
return false;
}
return true;
......@@ -461,11 +650,11 @@ static bool ptrace_waitpid(pid_t pid) {
return true;
}
if (!ptrace_continue(pid, WSTOPSIG(status))) {
fprintf(stderr, "attach: Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
print_error("attach: Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
return false;
}
} else {
fprintf(stderr, "attach: waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
print_error("attach: waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
return false;
}
} else {
......@@ -474,13 +663,13 @@ static bool ptrace_waitpid(pid_t pid) {
continue;
break;
case ECHILD:
fprintf(stderr, "attach: waitpid() failed. Child process pid (%d) does not exist \n", pid);
print_error("attach: waitpid() failed. Child process pid (%d) does not exist \n", pid);
break;
case EINVAL:
fprintf(stderr, "attach: waitpid() failed. Invalid options argument.\n");
print_error("attach: waitpid() failed. Invalid options argument.\n");
break;
default:
fprintf(stderr, "attach: waitpid() failed. Unexpected error %d\n",errno);
print_error("attach: waitpid() failed. Unexpected error %d\n",errno);
break;
}
return false;
......@@ -492,7 +681,7 @@ static bool ptrace_waitpid(pid_t pid) {
static bool ptrace_attach(pid_t pid) {
int res;
if ((res = ptrace(PT_ATTACH, pid, 0, 0)) < 0) {
fprintf(stderr, "ptrace(PT_ATTACH, %d) failed with %d\n", pid, res);
print_error("ptrace(PT_ATTACH, %d) failed with %d\n", pid, res);
return false;
} else {
return ptrace_waitpid(pid);
......@@ -508,19 +697,15 @@ JNIEXPORT void JNICALL
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(
JNIEnv *env, jobject this_obj, jint jpid)
{
print_debug("attach0 called for jpid=%d\n", (int)jpid);
JNF_COCOA_ENTER(env);
if (getenv("JAVA_SAPROC_DEBUG") != NULL)
debug = JNI_TRUE;
else
debug = JNI_FALSE;
if (debug) printf("attach0 called for jpid=%d\n", (int)jpid);
// get the task from the pid
kern_return_t result;
task_t gTask = 0;
result = task_for_pid(mach_task_self(), jpid, &gTask);
if (result != KERN_SUCCESS) {
fprintf(stderr, "attach: task_for_pid(%d) failed (%d)\n", (int)jpid, result);
print_error("attach: task_for_pid(%d) failed (%d)\n", (int)jpid, result);
THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
}
putTask(env, this_obj, gTask);
......@@ -550,6 +735,63 @@ JNF_COCOA_ENTER(env);
JNF_COCOA_EXIT(env);
}
/** For core file,
called from Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 */
static void fillLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
int n = 0, i = 0;
// add load objects
n = get_num_libs(ph);
for (i = 0; i < n; i++) {
uintptr_t base;
const char* name;
jobject loadObject;
jobject loadObjectList;
base = get_lib_base(ph, i);
name = get_lib_name(ph, i);
loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID,
(*env)->NewStringUTF(env, name), (jlong)0, (jlong)base);
CHECK_EXCEPTION;
loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID);
CHECK_EXCEPTION;
(*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
CHECK_EXCEPTION;
}
}
/*
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
* Method: attach0
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2(
JNIEnv *env, jobject this_obj, jstring execName, jstring coreName)
{
const char *execName_cstr;
const char *coreName_cstr;
jboolean isCopy;
struct ps_prochandle* ph;
execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
CHECK_EXCEPTION;
coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
CHECK_EXCEPTION;
print_debug("attach: %s %s\n", execName_cstr, coreName_cstr);
if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
(*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
(*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
}
(*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
(*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
(*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
fillLoadObjects(env, this_obj, ph);
}
/*
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
* Method: detach0
......@@ -559,9 +801,13 @@ JNIEXPORT void JNICALL
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(
JNIEnv *env, jobject this_obj)
{
print_debug("detach0 called\n");
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
if (ph != NULL && ph->core != NULL) {
Prelease(ph);
return;
}
JNF_COCOA_ENTER(env);
if (debug) printf("detach0 called\n");
task_t gTask = getTask(env, this_obj);
// detach from the ptraced process causing it to resume execution
......@@ -569,12 +815,12 @@ JNF_COCOA_ENTER(env);
kern_return_t k_res;
k_res = pid_for_task(gTask, &pid);
if (k_res != KERN_SUCCESS) {
fprintf(stderr, "detach: pid_for_task(%d) failed (%d)\n", pid, k_res);
print_error("detach: pid_for_task(%d) failed (%d)\n", pid, k_res);
}
else {
int res = ptrace(PT_DETACH, pid, 0, 0);
if (res < 0) {
fprintf(stderr, "detach: ptrace(PT_DETACH, %d) failed (%d)\n", pid, res);
print_error("detach: ptrace(PT_DETACH, %d) failed (%d)\n", pid, res);
}
}
......@@ -585,170 +831,3 @@ JNF_COCOA_ENTER(env);
}
JNF_COCOA_EXIT(env);
}
/*
* Class: sun_jvm_hotspot_asm_Disassembler
* Method: load_library
* Signature: (Ljava/lang/String;)L
*/
JNIEXPORT jlong JNICALL
Java_sun_jvm_hotspot_asm_Disassembler_load_1library(
JNIEnv * env,
jclass disclass,
jstring jrepath_s,
jstring libname_s)
{
uintptr_t func = 0;
const char* error_message = NULL;
const char* java_home;
jboolean isCopy;
uintptr_t *handle = NULL;
const char * jrepath = (*env)->GetStringUTFChars(env, jrepath_s, &isCopy); // like $JAVA_HOME/jre/lib/sparc/
const char * libname = (*env)->GetStringUTFChars(env, libname_s, &isCopy);
char buffer[128];
/* Load the hsdis library */
void* hsdis_handle;
hsdis_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
if (hsdis_handle == NULL) {
snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
hsdis_handle = dlopen(buffer, RTLD_LAZY | RTLD_GLOBAL);
}
if (hsdis_handle != NULL) {
func = (uintptr_t)dlsym(hsdis_handle, "decode_instructions_virtual");
}
if (func == 0) {
error_message = dlerror();
fprintf(stderr, "%s\n", error_message);
}
(*env)->ReleaseStringUTFChars(env, libname_s, libname);
(*env)->ReleaseStringUTFChars(env, jrepath_s, jrepath);
if (func == 0) {
/* Couldn't find entry point. error_message should contain some
* platform dependent error message.
*/
THROW_NEW_DEBUGGER_EXCEPTION_(error_message, (jlong)func);
}
return (jlong)func;
}
/* signature of decode_instructions_virtual from hsdis.h */
typedef void* (*decode_func)(uintptr_t start_va, uintptr_t end_va,
unsigned char* start, uintptr_t length,
void* (*event_callback)(void*, const char*, void*),
void* event_stream,
int (*printf_callback)(void*, const char*, ...),
void* printf_stream,
const char* options);
/* container for call back state when decoding instructions */
typedef struct {
JNIEnv* env;
jobject dis;
jobject visitor;
jmethodID handle_event;
jmethodID raw_print;
char buffer[4096];
} decode_env;
/* event callback binding to Disassembler.handleEvent */
static void* event_to_env(void* env_pv, const char* event, void* arg) {
decode_env* denv = (decode_env*)env_pv;
JNIEnv* env = denv->env;
jstring event_string = (*env)->NewStringUTF(env, event);
jlong result = (*env)->CallLongMethod(env, denv->dis, denv->handle_event, denv->visitor,
event_string, (jlong) (uintptr_t)arg);
/* ignore exceptions for now */
CHECK_EXCEPTION_CLEAR_((void *)0);
return (void*)(uintptr_t)result;
}
/* printing callback binding to Disassembler.rawPrint */
static int printf_to_env(void* env_pv, const char* format, ...) {
jstring output;
va_list ap;
int cnt;
decode_env* denv = (decode_env*)env_pv;
JNIEnv* env = denv->env;
size_t flen = strlen(format);
const char* raw = NULL;
if (flen == 0) return 0;
if (flen < 2 ||
strchr(format, '%') == NULL) {
raw = format;
} else if (format[0] == '%' && format[1] == '%' &&
strchr(format+2, '%') == NULL) {
// happens a lot on machines with names like %foo
flen--;
raw = format+1;
}
if (raw != NULL) {
jstring output = (*env)->NewStringUTF(env, raw);
(*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
CHECK_EXCEPTION_CLEAR;
return (int) flen;
}
va_start(ap, format);
cnt = vsnprintf(denv->buffer, sizeof(denv->buffer), format, ap);
va_end(ap);
output = (*env)->NewStringUTF(env, denv->buffer);
(*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
CHECK_EXCEPTION_CLEAR;
return cnt;
}
/*
* Class: sun_jvm_hotspot_asm_Disassembler
* Method: decode
* Signature: (Lsun/jvm/hotspot/asm/InstructionVisitor;J[BLjava/lang/String;J)V
*/
JNIEXPORT void JNICALL
Java_sun_jvm_hotspot_asm_Disassembler_decode(
JNIEnv * env,
jobject dis,
jobject visitor,
jlong startPc,
jbyteArray code,
jstring options_s,
jlong decode_instructions_virtual)
{
jboolean isCopy;
jbyte* start = (*env)->GetByteArrayElements(env, code, &isCopy);
jbyte* end = start + (*env)->GetArrayLength(env, code);
const char * options = (*env)->GetStringUTFChars(env, options_s, &isCopy);
jclass disclass = (*env)->GetObjectClass(env, dis);
decode_env denv;
denv.env = env;
denv.dis = dis;
denv.visitor = visitor;
/* find Disassembler.handleEvent callback */
denv.handle_event = (*env)->GetMethodID(env, disclass, "handleEvent",
"(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;J)J");
CHECK_EXCEPTION_CLEAR_VOID
/* find Disassembler.rawPrint callback */
denv.raw_print = (*env)->GetMethodID(env, disclass, "rawPrint",
"(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;)V");
CHECK_EXCEPTION_CLEAR_VOID
/* decode the buffer */
(*(decode_func)(uintptr_t)decode_instructions_virtual)(startPc,
startPc + end - start,
(unsigned char*)start,
end - start,
&event_to_env, (void*) &denv,
&printf_to_env, (void*) &denv,
options);
/* cleanup */
(*env)->ReleaseByteArrayElements(env, code, start, JNI_ABORT);
(*env)->ReleaseStringUTFChars(env, options_s, options);
}
#
# Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
......@@ -22,34 +22,60 @@
#
#
ARCH := $(shell if ([ `uname -m` = "ia64" ]) ; then echo ia64 ; elif ([ `uname -m` = "amd64" ]) ; then echo amd64; elif ([ `uname -m` = "sparc64" ]) ; then echo sparc; else echo i386 ; fi )
ARCH := $(shell if ([ `uname -m` = "ia64" ]) ; then echo ia64 ; elif ([ `uname -m` = "amd64" ]) ; then echo amd64; elif ([ `uname -m` = "x86_64" ]) ; then echo amd64; elif ([ `uname -m` = "sparc64" ]) ; then echo sparc; else echo i386 ; fi )
OS := $(shell uname -s)
GCC = gcc
JAVAH = ${JAVA_HOME}/bin/javah
ifneq ($(OS), Darwin)
SOURCES = salibelf.c \
symtab.c \
libproc_impl.c \
ps_proc.c \
ps_core.c \
BsdDebuggerLocal.c
OBJS = $(SOURCES:.c=.o)
OBJSPLUS = $(OBJS) sadis.o
LIBSA = $(ARCH)/libsaproc.so
INCLUDES = -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(shell uname -s | tr "[:upper:]" "[:lower:]")
LIBS = -lutil -lthread_db
else
SOURCES = symtab.c \
libproc_impl.c \
ps_core.c
OBJS = $(SOURCES:.c=.o)
OBJSPLUS = MacosxDebuggerLocal.o sadis.o $(OBJS)
EXTINCLUDE = -I/System/Library/Frameworks/JavaVM.framework/Headers -I.
EXTCFLAGS = -m64 -D__APPLE__ -framework JavaNativeFoundation
FOUNDATIONFLAGS = -framework Foundation -F/System/Library/Frameworks/JavaVM.framework/Frameworks -framework JavaNativeFoundation -framework Security -framework CoreFoundation
LIBSA = $(ARCH)/libsaproc.dylib
endif # Darwin
LIBS = -lutil -lthread_db
INCLUDES = -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(shell uname -s | tr "[:upper:]" "[:lower:]") $(EXTINCLUDE)
CFLAGS = -c -fPIC -g -Wall -D_ALLBSD_SOURCE -D_GNU_SOURCE -D$(ARCH) $(INCLUDES) $(EXTCFLAGS)
CFLAGS = -c -fPIC -g -Wall -D_ALLBSD_SOURCE -D_GNU_SOURCE -D$(ARCH) $(INCLUDES)
LIBSA = $(ARCH)/libsaproc.so
all: $(LIBSA)
BsdDebuggerLocal.o: BsdDebuggerLocal.c
$(JAVAH) -jni -classpath ../../../../../build/bsd-i586/hotspot/outputdir/bsd_i486_compiler2/generated/saclasses \
MacosxDebuggerLocal.o: MacosxDebuggerLocal.m
echo "OS="$(OS)
$(JAVAH) -jni -classpath ../../../build/classes \
sun.jvm.hotspot.debugger.x86.X86ThreadContext \
sun.jvm.hotspot.debugger.amd64.AMD64ThreadContext
$(GCC) $(CFLAGS) $(FOUNDATIONFLAGS) $<
sadis.o: ../../share/native/sadis.c
$(JAVAH) -jni -classpath ../../../build/classes \
sun.jvm.hotspot.asm.Disassembler
$(GCC) $(CFLAGS) $<
.c.obj:
......@@ -59,9 +85,9 @@ ifndef LDNOMAP
LFLAGS_LIBSA = -Xlinker --version-script=mapfile
endif
$(LIBSA): $(OBJS) mapfile
$(LIBSA): $(OBJSPLUS) mapfile
if [ ! -d $(ARCH) ] ; then mkdir $(ARCH) ; fi
$(GCC) -shared $(LFLAGS_LIBSA) -o $(LIBSA) $(OBJS) $(LIBS)
$(GCC) -shared $(LFLAGS_LIBSA) -o $(LIBSA) $(FOUNDATIONFLAGS) $(OBJSPLUS) $(LIBS) $(SALIBS)
test.o: $(LIBSA) test.c
$(GCC) -c -o test.o -g -D_GNU_SOURCE -D$(ARCH) $(INCLUDES) test.c
......@@ -71,7 +97,6 @@ test: test.o
clean:
rm -f $(LIBSA)
rm -f $(OBJS)
rm -f *.o
rm -f test.o
-rmdir $(ARCH)
/*
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,9 +27,38 @@
#include <unistd.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#ifdef __APPLE__
typedef enum ps_err_e {
PS_OK, PS_ERR, PS_BADPID, PS_BADLID,
PS_BADADDR, PS_NOSYM, PS_NOFREGS
} ps_err_e;
#ifndef psaddr_t
#define psaddr_t uintptr_t
#endif
#ifndef bool
typedef int bool;
#define true 1
#define false 0
#endif // bool
#ifndef lwpid_t
#define lwpid_t uintptr_t
#endif
#include <mach/thread_status.h>
#else // __APPLE__
#include <elf.h>
#include <link.h>
#include <machine/reg.h>
#include <proc_service.h>
#if defined(sparc) || defined(sparcv9)
/*
If _LP64 is defined ptrace.h should be taken from /usr/include/asm-sparc64
......@@ -44,6 +73,14 @@
#endif //sparc or sparcv9
// This C bool type must be int for compatibility with BSD calls and
// it would be a mistake to equivalence it to C++ bool on many platforms
typedef int bool;
#define true 1
#define false 0
#endif // __APPLE__
/************************************************************************************
0. This is very minimal subset of Solaris libproc just enough for current application.
......@@ -72,13 +109,7 @@ combination of ptrace and /proc calls.
*************************************************************************************/
// This C bool type must be int for compatibility with BSD calls and
// it would be a mistake to equivalence it to C++ bool on many platforms
typedef int bool;
#define true 1
#define false 0
struct reg;
struct ps_prochandle;
// attach to a process
......
......@@ -21,12 +21,6 @@
* questions.
*
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <thread_db.h>
#include "libproc_impl.h"
static const char* alt_root = NULL;
......@@ -34,6 +28,10 @@ static int alt_root_len = -1;
#define SA_ALTROOT "SA_ALTROOT"
off_t ltell(int fd) {
return lseek(fd, 0, SEEK_CUR);
}
static void init_alt_root() {
if (alt_root_len == -1) {
alt_root = getenv(SA_ALTROOT);
......@@ -50,10 +48,6 @@ int pathmap_open(const char* name) {
char alt_path[PATH_MAX + 1];
init_alt_root();
fd = open(name, O_RDONLY);
if (fd >= 0) {
return fd;
}
if (alt_root_len > 0) {
strcpy(alt_path, alt_root);
......@@ -73,8 +67,12 @@ int pathmap_open(const char* name) {
return fd;
}
}
} else {
fd = open(name, O_RDONLY);
if (fd >= 0) {
return fd;
}
}
return -1;
}
......@@ -103,24 +101,81 @@ bool is_debug() {
return _libsaproc_debug;
}
#ifdef __APPLE__
// get arch offset in file
bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset) {
struct fat_header fatheader;
struct fat_arch fatarch;
off_t img_start = 0;
off_t pos = ltell(fd);
if (read(fd, (void *)&fatheader, sizeof(struct fat_header)) != sizeof(struct fat_header)) {
return false;
}
if (fatheader.magic == FAT_CIGAM) {
int i;
for (i = 0; i < ntohl(fatheader.nfat_arch); i++) {
if (read(fd, (void *)&fatarch, sizeof(struct fat_arch)) != sizeof(struct fat_arch)) {
return false;
}
if (ntohl(fatarch.cputype) == cputype) {
print_debug("fat offset=%x\n", ntohl(fatarch.offset));
img_start = ntohl(fatarch.offset);
break;
}
}
if (img_start == 0) {
return false;
}
}
lseek(fd, pos, SEEK_SET);
*offset = img_start;
return true;
}
bool is_macho_file(int fd) {
mach_header_64 fhdr;
off_t x86_64_off;
if (fd < 0) {
print_debug("Invalid file handle passed to is_macho_file\n");
return false;
}
off_t pos = ltell(fd);
// check fat header
if (!get_arch_off(fd, CPU_TYPE_X86_64, &x86_64_off)) {
print_debug("failed to get fat header\n");
return false;
}
lseek(fd, x86_64_off, SEEK_SET);
if (read(fd, (void *)&fhdr, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
return false;
}
lseek(fd, pos, SEEK_SET); // restore
print_debug("fhdr.magic %x\n", fhdr.magic);
return (fhdr.magic == MH_MAGIC_64 || fhdr.magic == MH_CIGAM_64);
}
#endif //__APPLE__
// initialize libproc
bool init_libproc(bool debug) {
// init debug mode
_libsaproc_debug = debug;
#ifndef __APPLE__
// initialize the thread_db library
if (td_init() != TD_OK) {
print_debug("libthread_db's td_init failed\n");
return false;
}
#endif // __APPLE__
return true;
}
static void destroy_lib_info(struct ps_prochandle* ph) {
void destroy_lib_info(struct ps_prochandle* ph) {
lib_info* lib = ph->libs;
while (lib) {
lib_info *next = lib->next;
lib_info* next = lib->next;
if (lib->symtab) {
destroy_symtab(lib->symtab);
}
......@@ -129,17 +184,15 @@ static void destroy_lib_info(struct ps_prochandle* ph) {
}
}
static void destroy_thread_info(struct ps_prochandle* ph) {
thread_info* thr = ph->threads;
void destroy_thread_info(struct ps_prochandle* ph) {
sa_thread_info* thr = ph->threads;
while (thr) {
thread_info *next = thr->next;
sa_thread_info* n = thr->next;
free(thr);
thr = next;
thr = n;
}
}
// ps_prochandle cleanup
// ps_prochandle cleanup
void Prelease(struct ps_prochandle* ph) {
// do the "derived class" clean-up first
......@@ -155,6 +208,7 @@ lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
lib_info* newlib;
print_debug("add_lib_info_fd %s\n", libname);
if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
print_debug("can't allocate memory for lib_info\n");
......@@ -174,6 +228,15 @@ lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
newlib->fd = fd;
}
#ifdef __APPLE__
// check whether we have got an Macho file.
if (is_macho_file(newlib->fd) == false) {
close(newlib->fd);
free(newlib);
print_debug("not a mach-o file\n");
return NULL;
}
#else
// check whether we have got an ELF file. /proc/<pid>/map
// gives out all file mappings and not just shared objects
if (is_elf_file(newlib->fd) == false) {
......@@ -181,17 +244,17 @@ lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
free(newlib);
return NULL;
}
#endif // __APPLE__
newlib->symtab = build_symtab(newlib->fd);
if (newlib->symtab == NULL) {
print_debug("symbol table build failed for %s\n", newlib->name);
}
else {
} else {
print_debug("built symbol table for %s\n", newlib->name);
}
// even if symbol table building fails, we add the lib_info.
// This is because we may need to read from the ELF file for core file
// This is because we may need to read from the ELF file or MachO file for core file
// address read functionality. lookup_symbol checks for NULL symtab.
if (ph->libs) {
ph->lib_tail->next = newlib;
......@@ -200,7 +263,6 @@ lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
ph->libs = ph->lib_tail = newlib;
}
ph->num_libs++;
return newlib;
}
......@@ -228,7 +290,6 @@ uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,
return (uintptr_t) NULL;
}
const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
const char* res = NULL;
lib_info* lib = ph->libs;
......@@ -243,9 +304,9 @@ const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* p
}
// add a thread to ps_prochandle
thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
thread_info* newthr;
if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
sa_thread_info* newthr;
if ( (newthr = (sa_thread_info*) calloc(1, sizeof(sa_thread_info))) == NULL) {
print_debug("can't allocate memory for thread_info\n");
return NULL;
}
......@@ -261,7 +322,7 @@ thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwp
return newthr;
}
#ifndef __APPLE__
// struct used for client data from thread_db callback
struct thread_db_client_data {
struct ps_prochandle* ph;
......@@ -314,6 +375,7 @@ bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
return true;
}
#endif // __APPLE__
// get number of threads
int get_num_threads(struct ps_prochandle* ph) {
......@@ -323,7 +385,7 @@ int get_num_threads(struct ps_prochandle* ph) {
// get lwp_id of n'th thread
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
int count = 0;
thread_info* thr = ph->threads;
sa_thread_info* thr = ph->threads;
while (thr) {
if (count == index) {
return thr->lwp_id;
......@@ -331,9 +393,45 @@ lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
count++;
thr = thr->next;
}
return -1;
return 0;
}
#ifdef __APPLE__
// set lwp_id of n'th thread
bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid) {
int count = 0;
sa_thread_info* thr = ph->threads;
while (thr) {
if (count == index) {
thr->lwp_id = lwpid;
return true;
}
count++;
thr = thr->next;
}
return false;
}
// get regs of n-th thread, only used in fillThreads the first time called
bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs) {
int count = 0;
sa_thread_info* thr = ph->threads;
while (thr) {
if (count == index) {
break;
}
count++;
thr = thr->next;
}
if (thr != NULL) {
memcpy(regs, &thr->regs, sizeof(struct reg));
return true;
}
return false;
}
#endif // __APPLE__
// get regs for a given lwp
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs) {
return ph->ops->get_lwp_regs(ph, lwp_id, regs);
......@@ -425,6 +523,7 @@ ps_plog (const char *format, ...)
va_end(alist);
}
#ifndef __APPLE__
// ------------------------------------------------------------------------
// Functions below this point are not yet implemented. They are here only
// to make the linker happy.
......@@ -458,3 +557,4 @@ ps_err_e ps_pcontinue(struct ps_prochandle *ph) {
print_debug("ps_pcontinue not implemented\n");
return PS_OK;
}
#endif // __APPLE__
......@@ -30,6 +30,60 @@
#include "libproc.h"
#include "symtab.h"
#ifdef __APPLE__
#include <inttypes.h> // for PRIx64, 32, ...
#include <pthread.h>
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <mach-o/fat.h>
#ifndef register_t
#define register_t uint64_t
#endif
/*** registers copied from bsd/amd64 */
typedef struct reg {
register_t r_r15;
register_t r_r14;
register_t r_r13;
register_t r_r12;
register_t r_r11;
register_t r_r10;
register_t r_r9;
register_t r_r8;
register_t r_rdi;
register_t r_rsi;
register_t r_rbp;
register_t r_rbx;
register_t r_rdx;
register_t r_rcx;
register_t r_rax;
uint32_t r_trapno; // not used
uint16_t r_fs;
uint16_t r_gs;
uint32_t r_err; // not used
uint16_t r_es; // not used
uint16_t r_ds; // not used
register_t r_rip;
register_t r_cs;
register_t r_rflags;
register_t r_rsp;
register_t r_ss; // not used
} reg;
// convenient defs
typedef struct mach_header_64 mach_header_64;
typedef struct load_command load_command;
typedef struct segment_command_64 segment_command_64;
typedef struct thread_command thread_command;
typedef struct dylib_command dylib_command;
typedef struct symtab_command symtab_command;
typedef struct nlist_64 nlist_64;
#else
#include <thread_db.h>
#include "salibelf.h"
#endif // __APPLE__
// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h
#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)
......@@ -44,12 +98,12 @@ typedef struct lib_info {
} lib_info;
// list of threads
typedef struct thread_info {
lwpid_t lwp_id;
pthread_t pthread_id; // not used cores, always -1
typedef struct sa_thread_info {
lwpid_t lwp_id; // same as pthread_t
pthread_t pthread_id; //
struct reg regs; // not for process, core uses for caching regset
struct thread_info* next;
} thread_info;
struct sa_thread_info* next;
} sa_thread_info;
// list of virtual memory maps
typedef struct map_info {
......@@ -91,6 +145,7 @@ struct core_data {
// part of the class sharing workaround
map_info* class_share_maps;// class share maps in a linked list
map_info** map_array; // sorted (by vaddr) array of map_info pointers
char exec_path[4096]; // file name java
};
struct ps_prochandle {
......@@ -100,12 +155,11 @@ struct ps_prochandle {
lib_info* libs; // head of lib list
lib_info* lib_tail; // tail of lib list - to append at the end
int num_threads;
thread_info* threads; // head of thread list
sa_thread_info* threads; // head of thread list
struct core_data* core; // data only used for core dumps, NULL for process
};
int pathmap_open(const char* name);
void print_debug(const char* format,...);
void print_error(const char* format,...);
bool is_debug();
......@@ -122,10 +176,45 @@ lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
uintptr_t base);
// adds a new thread to threads list, returns NULL on failure
thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
// a test for ELF signature without using libelf
#ifdef __APPLE__
// a test for Mach-O signature
bool is_macho_file(int fd);
// skip fat head to get image start offset of cpu_type_t
// return false if any error happens, else value in offset.
bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset);
#else
bool is_elf_file(int fd);
#endif // __APPLE__
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid);
bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs);
// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
// of the load object object_name in the target process identified by ph.
// It returns the symbol's value as an address in the target process in
// *sym_addr.
ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
const char *sym_name, psaddr_t *sym_addr);
// read "size" bytes info "buf" from address "addr"
ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,
void *buf, size_t size);
// write "size" bytes of data to debuggee at address "addr"
ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,
const void *buf, size_t size);
// fill in ptrace_lwpinfo for lid
ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo);
// needed for when libthread_db is compiled with TD_DEBUG defined
void ps_plog (const char *format, ...);
// untility, tells the position in file
off_t ltell(int fd);
#endif //_LIBPROC_IMPL_H_
/*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -28,10 +28,11 @@
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <elf.h>
#include <link.h>
#include "libproc_impl.h"
#include "salibelf.h"
#ifdef __APPLE__
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
#endif
// This file has the libproc implementation to read core files.
// For live processes, refer to ps_proc.c. Portions of this is adapted
......@@ -41,9 +42,8 @@
// ps_prochandle cleanup helper functions
// close all file descriptors
static void close_elf_files(struct ps_prochandle* ph) {
static void close_files(struct ps_prochandle* ph) {
lib_info* lib = NULL;
// close core file descriptor
if (ph->core->core_fd >= 0)
close(ph->core->core_fd);
......@@ -64,7 +64,9 @@ static void close_elf_files(struct ps_prochandle* ph) {
lib = ph->libs;
while (lib) {
int fd = lib->fd;
if (fd >= 0 && fd != ph->core->exec_fd) close(fd);
if (fd >= 0 && fd != ph->core->exec_fd) {
close(fd);
}
lib = lib->next;
}
}
......@@ -94,7 +96,7 @@ static void destroy_map_info(struct ps_prochandle* ph) {
// ps_prochandle operations
static void core_release(struct ps_prochandle* ph) {
if (ph->core) {
close_elf_files(ph);
close_files(ph);
destroy_map_info(ph);
free(ph->core);
}
......@@ -154,19 +156,22 @@ static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr)
while (hi - lo > 1) {
mid = (lo + hi) / 2;
if (addr >= ph->core->map_array[mid]->vaddr)
if (addr >= ph->core->map_array[mid]->vaddr) {
lo = mid;
else
} else {
hi = mid;
}
}
if (addr < ph->core->map_array[hi]->vaddr)
if (addr < ph->core->map_array[hi]->vaddr) {
mp = ph->core->map_array[lo];
else
} else {
mp = ph->core->map_array[hi];
}
if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz)
if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
return (mp);
}
// Part of the class sharing workaround
......@@ -177,13 +182,11 @@ static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr)
// want to prefer class sharing data to data from core.
mp = ph->core->class_share_maps;
if (mp) {
print_debug("can't locate map_info at 0x%lx, trying class share maps\n",
addr);
print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr);
}
while (mp) {
if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
print_debug("located map_info at 0x%lx from class share maps\n",
addr);
print_debug("located map_info at 0x%lx from class share maps\n", addr);
return (mp);
}
mp = mp->next;
......@@ -250,7 +253,7 @@ static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pv
static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
uintptr_t uip;
if (ps_pread(ph, (psaddr_t) addr, &uip, sizeof(uip)) == PS_OK) {
if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
*pvalue = uip;
return true;
} else {
......@@ -264,35 +267,50 @@ static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, siz
char c = ' ';
while (c != '\0') {
if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK)
if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) {
return false;
if (i < size - 1)
}
if (i < size - 1) {
buf[i] = c;
else // smaller buffer
} else {
// smaller buffer
return false;
}
i++; addr++;
}
buf[i] = '\0';
return true;
}
#define USE_SHARED_SPACES_SYM "UseSharedSpaces"
#ifdef __APPLE__
#define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
// mangled name of Arguments::SharedArchivePath
#define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE"
#else
#define USE_SHARED_SPACES_SYM "UseSharedSpaces"
// mangled name of Arguments::SharedArchivePath
#define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
#endif // __APPLE_
static bool init_classsharing_workaround(struct ps_prochandle* ph) {
int m;
size_t n;
lib_info* lib = ph->libs;
while (lib != NULL) {
// we are iterating over shared objects from the core dump. look for
// libjvm[_g].so.
const char *jvm_name = 0;
#ifdef __APPLE__
if ((jvm_name = strstr(lib->name, "/libjvm.dylib")) != 0 ||
(jvm_name = strstr(lib->name, "/libjvm_g.dylib")) != 0)
#else
if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0 ||
(jvm_name = strstr(lib->name, "/libjvm_g.so")) != 0) {
(jvm_name = strstr(lib->name, "/libjvm_g.so")) != 0)
#endif // __APPLE__
{
char classes_jsa[PATH_MAX];
struct FileMapHeader header;
size_t n = 0;
int fd = -1, m = 0;
int fd = -1;
uintptr_t base = 0, useSharedSpacesAddr = 0;
uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
jboolean useSharedSpaces = 0;
......@@ -389,7 +407,6 @@ static bool init_classsharing_workaround(struct ps_prochandle* ph) {
return true;
}
//---------------------------------------------------------------------------
// functions to handle map_info
......@@ -400,8 +417,9 @@ static int core_cmp_mapping(const void *lhsp, const void *rhsp)
const map_info *lhs = *((const map_info **)lhsp);
const map_info *rhs = *((const map_info **)rhsp);
if (lhs->vaddr == rhs->vaddr)
if (lhs->vaddr == rhs->vaddr) {
return (0);
}
return (lhs->vaddr < rhs->vaddr ? -1 : 1);
}
......@@ -428,7 +446,9 @@ static bool sort_map_array(struct ps_prochandle* ph) {
}
// sort is called twice. If this is second time, clear map array
if (ph->core->map_array) free(ph->core->map_array);
if (ph->core->map_array) {
free(ph->core->map_array);
}
ph->core->map_array = array;
// sort the map_info array by base virtual address.
qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
......@@ -461,16 +481,18 @@ static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf,
off_t off;
int fd;
if (mp == NULL)
if (mp == NULL) {
break; /* No mapping for this address */
}
fd = mp->fd;
mapoff = addr - mp->vaddr;
len = MIN(resid, mp->memsz - mapoff);
off = mp->offset + mapoff;
if ((len = pread(fd, buf, len, off)) <= 0)
if ((len = pread(fd, buf, len, off)) <= 0) {
break;
}
resid -= len;
addr += len;
......@@ -507,8 +529,8 @@ static bool core_write_data(struct ps_prochandle* ph,
static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
struct reg* regs) {
// for core we have cached the lwp regs from NOTE section
thread_info* thr = ph->threads;
// for core we have cached the lwp regs after segment parsed
sa_thread_info* thr = ph->threads;
while (thr) {
if (thr->lwp_id == lwp_id) {
memcpy(regs, &thr->regs, sizeof(struct reg));
......@@ -519,7 +541,7 @@ static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
return false;
}
static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {
static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t id, void *info) {
print_debug("core_get_lwp_info not implemented\n");
return false;
}
......@@ -532,12 +554,451 @@ static ps_prochandle_ops core_ops = {
.get_lwp_info= core_get_lwp_info
};
// read regs and create thread from NT_PRSTATUS entries from core file
// from this point, mainly two blocks divided by def __APPLE__
// one for Macosx, the other for regular Bsd
#ifdef __APPLE__
void print_thread(sa_thread_info *threadinfo) {
print_debug("thread added: %d\n", threadinfo->lwp_id);
print_debug("registers:\n");
print_debug(" r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15);
print_debug(" r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14);
print_debug(" r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13);
print_debug(" r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12);
print_debug(" r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11);
print_debug(" r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10);
print_debug(" r_r9: 0x%" PRIx64 "\n", threadinfo->regs.r_r9);
print_debug(" r_r8: 0x%" PRIx64 "\n", threadinfo->regs.r_r8);
print_debug(" r_rdi: 0x%" PRIx64 "\n", threadinfo->regs.r_rdi);
print_debug(" r_rsi: 0x%" PRIx64 "\n", threadinfo->regs.r_rsi);
print_debug(" r_rbp: 0x%" PRIx64 "\n", threadinfo->regs.r_rbp);
print_debug(" r_rbx: 0x%" PRIx64 "\n", threadinfo->regs.r_rbx);
print_debug(" r_rdx: 0x%" PRIx64 "\n", threadinfo->regs.r_rdx);
print_debug(" r_rcx: 0x%" PRIx64 "\n", threadinfo->regs.r_rcx);
print_debug(" r_rax: 0x%" PRIx64 "\n", threadinfo->regs.r_rax);
print_debug(" r_fs: 0x%" PRIx32 "\n", threadinfo->regs.r_fs);
print_debug(" r_gs: 0x%" PRIx32 "\n", threadinfo->regs.r_gs);
print_debug(" r_rip 0x%" PRIx64 "\n", threadinfo->regs.r_rip);
print_debug(" r_cs: 0x%" PRIx64 "\n", threadinfo->regs.r_cs);
print_debug(" r_rsp: 0x%" PRIx64 "\n", threadinfo->regs.r_rsp);
print_debug(" r_rflags: 0x%" PRIx64 "\n", threadinfo->regs.r_rflags);
}
// read all segments64 commands from core file
// read all thread commands from core file
static bool read_core_segments(struct ps_prochandle* ph) {
int i = 0;
int num_threads = 0;
int fd = ph->core->core_fd;
off_t offset = 0;
mach_header_64 fhead;
load_command lcmd;
segment_command_64 segcmd;
// thread_command thrcmd;
lseek(fd, offset, SEEK_SET);
if(read(fd, (void *)&fhead, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
goto err;
}
print_debug("total commands: %d\n", fhead.ncmds);
offset += sizeof(mach_header_64);
for (i = 0; i < fhead.ncmds; i++) {
lseek(fd, offset, SEEK_SET);
if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
goto err;
}
offset += lcmd.cmdsize; // next command position
if (lcmd.cmd == LC_SEGMENT_64) {
lseek(fd, -sizeof(load_command), SEEK_CUR);
if (read(fd, (void *)&segcmd, sizeof(segment_command_64)) != sizeof(segment_command_64)) {
print_debug("failed to read LC_SEGMENT_64 i = %d!\n", i);
goto err;
}
if (add_map_info(ph, fd, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize) == NULL) {
print_debug("Failed to add map_info at i = %d\n", i);
goto err;
}
print_debug("segment added: %" PRIu64 " 0x%" PRIx64 " %d\n",
segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize);
} else if (lcmd.cmd == LC_THREAD || lcmd.cmd == LC_UNIXTHREAD) {
typedef struct thread_fc {
uint32_t flavor;
uint32_t count;
} thread_fc;
thread_fc fc;
uint32_t size = sizeof(load_command);
while (size < lcmd.cmdsize) {
if (read(fd, (void *)&fc, sizeof(thread_fc)) != sizeof(thread_fc)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(thread_fc);
if (fc.flavor == x86_THREAD_STATE) {
x86_thread_state_t thrstate;
if (read(fd, (void *)&thrstate, sizeof(x86_thread_state_t)) != sizeof(x86_thread_state_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(x86_thread_state_t);
// create thread info list, update lwp_id later
sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++);
if (newthr == NULL) {
printf("create thread_info failed\n");
goto err;
}
// note __DARWIN_UNIX03 depengs on other definitions
#if __DARWIN_UNIX03
#define get_register_v(regst, regname) \
regst.uts.ts64.__##regname
#else
#define get_register_v(regst, regname) \
regst.uts.ts64.##regname
#endif // __DARWIN_UNIX03
newthr->regs.r_rax = get_register_v(thrstate, rax);
newthr->regs.r_rbx = get_register_v(thrstate, rbx);
newthr->regs.r_rcx = get_register_v(thrstate, rcx);
newthr->regs.r_rdx = get_register_v(thrstate, rdx);
newthr->regs.r_rdi = get_register_v(thrstate, rdi);
newthr->regs.r_rsi = get_register_v(thrstate, rsi);
newthr->regs.r_rbp = get_register_v(thrstate, rbp);
newthr->regs.r_rsp = get_register_v(thrstate, rsp);
newthr->regs.r_r8 = get_register_v(thrstate, r8);
newthr->regs.r_r9 = get_register_v(thrstate, r9);
newthr->regs.r_r10 = get_register_v(thrstate, r10);
newthr->regs.r_r11 = get_register_v(thrstate, r11);
newthr->regs.r_r12 = get_register_v(thrstate, r12);
newthr->regs.r_r13 = get_register_v(thrstate, r13);
newthr->regs.r_r14 = get_register_v(thrstate, r14);
newthr->regs.r_r15 = get_register_v(thrstate, r15);
newthr->regs.r_rip = get_register_v(thrstate, rip);
newthr->regs.r_rflags = get_register_v(thrstate, rflags);
newthr->regs.r_cs = get_register_v(thrstate, cs);
newthr->regs.r_fs = get_register_v(thrstate, fs);
newthr->regs.r_gs = get_register_v(thrstate, gs);
print_thread(newthr);
} else if (fc.flavor == x86_FLOAT_STATE) {
x86_float_state_t flstate;
if (read(fd, (void *)&flstate, sizeof(x86_float_state_t)) != sizeof(x86_float_state_t)) {
print_debug("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(x86_float_state_t);
} else if (fc.flavor == x86_EXCEPTION_STATE) {
x86_exception_state_t excpstate;
if (read(fd, (void *)&excpstate, sizeof(x86_exception_state_t)) != sizeof(x86_exception_state_t)) {
printf("Reading flavor, count failed.\n");
goto err;
}
size += sizeof(x86_exception_state_t);
}
}
}
}
return true;
err:
return false;
}
/**local function **/
bool exists(const char *fname)
{
int fd;
if ((fd = open(fname, O_RDONLY)) > 0) {
close(fd);
return true;
}
return false;
}
// we check: 1. lib
// 2. lib/server
// 3. jre/lib
// 4. jre/lib/server
// from: 1. exe path
// 2. JAVA_HOME
// 3. DYLD_LIBRARY_PATH
static bool get_real_path(struct ps_prochandle* ph, char *rpath) {
/** check if they exist in JAVA ***/
char* execname = ph->core->exec_path;
char filepath[4096];
char* filename = strrchr(rpath, '/'); // like /libjvm.dylib
if (filename == NULL) {
return false;
}
char* posbin = strstr(execname, "/bin/java");
if (posbin != NULL) {
memcpy(filepath, execname, posbin - execname); // not include trailing '/'
filepath[posbin - execname] = '\0';
} else {
char* java_home = getenv("JAVA_HOME");
if (java_home != NULL) {
strcpy(filepath, java_home);
} else {
char* dyldpath = getenv("DYLD_LIBRARY_PATH");
char* dypath = strtok(dyldpath, ":");
while (dypath != NULL) {
strcpy(filepath, dypath);
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
dypath = strtok(dyldpath, ":");
}
// not found
return false;
}
}
// for exec and java_home, jdkpath now is filepath
size_t filepath_base_size = strlen(filepath);
// first try /lib/ and /lib/server
strcat(filepath, "/lib");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
char* pos = strstr(filepath, filename); // like /libjvm.dylib
*pos = '\0';
strcat(filepath, "/server");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
// then try /jre/lib/ and /jre/lib/server
filepath[filepath_base_size] = '\0';
strcat(filepath, "/jre/lib");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
pos = strstr(filepath, filename);
*pos = '\0';
strcat(filepath, "/server");
strcat(filepath, filename);
if (exists(filepath)) {
strcpy(rpath, filepath);
return true;
}
return false;
}
static bool read_shared_lib_info(struct ps_prochandle* ph) {
static int pagesize = 0;
int fd = ph->core->core_fd;
int i = 0, j;
uint32_t v;
mach_header_64 header; // used to check if a file header in segment
load_command lcmd;
dylib_command dylibcmd;
char name[BUF_SIZE]; // use to store name
if (pagesize == 0) {
pagesize = getpagesize();
print_debug("page size is %d\n", pagesize);
}
for (j = 0; j < ph->core->num_maps; j++) {
map_info *iter = ph->core->map_array[j]; // head
off_t fpos = iter->offset;
if (iter->fd != fd) {
// only search core file!
continue;
}
print_debug("map_info %d: vmaddr = 0x%016" PRIx64 " fileoff = %" PRIu64 " vmsize = %" PRIu64 "\n",
j, iter->vaddr, iter->offset, iter->memsz);
lseek(fd, fpos, SEEK_SET);
// we assume .dylib loaded at segment address --- which is true for JVM libraries
// multiple files may be loaded in one segment.
// if first word is not a magic word, means this segment does not contain lib file.
if (read(fd, (void *)&v, sizeof(uint32_t)) == sizeof(uint32_t)) {
if (v != MH_MAGIC_64) {
continue;
}
} else {
// may be encountered last map, which is not readable
continue;
}
while (ltell(fd) - iter->offset < iter->memsz) {
lseek(fd, fpos, SEEK_SET);
if (read(fd, (void *)&v, sizeof(uint32_t)) != sizeof(uint32_t)) {
break;
}
if (v != MH_MAGIC_64) {
fpos = (ltell(fd) + pagesize -1)/pagesize * pagesize;
continue;
}
lseek(fd, -sizeof(uint32_t), SEEK_CUR);
// this is the file begining to core file.
if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
goto err;
}
fpos = ltell(fd);
// found a mach-o file in this segment
for (i = 0; i < header.ncmds; i++) {
// read commands in this "file"
// LC_ID_DYLIB is the file itself for a .dylib
lseek(fd, fpos, SEEK_SET);
if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
return false; // error
}
fpos += lcmd.cmdsize; // next command position
// make sure still within seg size.
if (fpos - lcmd.cmdsize - iter->offset > iter->memsz) {
print_debug("Warning: out of segement limit: %ld \n", fpos - lcmd.cmdsize - iter->offset);
break; // no need to iterate all commands
}
if (lcmd.cmd == LC_ID_DYLIB) {
lseek(fd, -sizeof(load_command), SEEK_CUR);
if (read(fd, (void *)&dylibcmd, sizeof(dylib_command)) != sizeof(dylib_command)) {
return false;
}
/**** name stored at dylib_command.dylib.name.offset, is a C string */
lseek(fd, dylibcmd.dylib.name.offset - sizeof(dylib_command), SEEK_CUR);
int j = 0;
while (j < BUF_SIZE) {
read(fd, (void *)(name + j), sizeof(char));
if (name[j] == '\0') break;
j++;
}
print_debug("%s\n", name);
// changed name from @rpath/xxxx.dylib to real path
if (strrchr(name, '@')) {
get_real_path(ph, name);
print_debug("get_real_path returned: %s\n", name);
}
add_lib_info(ph, name, iter->vaddr);
break;
}
}
// done with the file, advanced to next page to search more files
fpos = (ltell(fd) + pagesize - 1) / pagesize * pagesize;
}
}
return true;
err:
return false;
}
bool read_macho64_header(int fd, mach_header_64* core_header) {
bool is_macho = false;
if (fd < 0) return false;
off_t pos = ltell(fd);
lseek(fd, 0, SEEK_SET);
if (read(fd, (void *)core_header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
is_macho = false;
} else {
is_macho = (core_header->magic == MH_MAGIC_64 || core_header->magic == MH_CIGAM_64);
}
lseek(fd, pos, SEEK_SET);
return is_macho;
}
// the one and only one exposed stuff from this file
struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
mach_header_64 core_header;
mach_header_64 exec_header;
struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
if (ph == NULL) {
print_debug("cant allocate ps_prochandle\n");
return NULL;
}
if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
free(ph);
print_debug("can't allocate ps_prochandle\n");
return NULL;
}
// initialize ph
ph->ops = &core_ops;
ph->core->core_fd = -1;
ph->core->exec_fd = -1;
ph->core->interp_fd = -1;
print_debug("exec: %s core: %s", exec_file, core_file);
strncpy(ph->core->exec_path, exec_file, sizeof(ph->core->exec_path));
// open the core file
if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
print_error("can't open core file\n");
goto err;
}
// read core file header
if (read_macho64_header(ph->core->core_fd, &core_header) != true || core_header.filetype != MH_CORE) {
print_debug("core file is not a valid Mach-O file\n");
goto err;
}
if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
print_error("can't open executable file\n");
goto err;
}
if (read_macho64_header(ph->core->exec_fd, &exec_header) != true ||
exec_header.filetype != MH_EXECUTE) {
print_error("executable file is not a valid Mach-O file\n");
goto err;
}
// process core file segments
if (read_core_segments(ph) != true) {
print_error("failed to read core segments\n");
goto err;
}
// allocate and sort maps into map_array, we need to do this
// here because read_shared_lib_info needs to read from debuggee
// address space
if (sort_map_array(ph) != true) {
print_error("failed to sort segment map array\n");
goto err;
}
if (read_shared_lib_info(ph) != true) {
print_error("failed to read libraries\n");
goto err;
}
// sort again because we have added more mappings from shared objects
if (sort_map_array(ph) != true) {
print_error("failed to sort segment map array\n");
goto err;
}
if (init_classsharing_workaround(ph) != true) {
print_error("failed to workaround classshareing\n");
goto err;
}
print_debug("Leave Pgrab_core\n");
return ph;
err:
Prelease(ph);
return NULL;
}
#else // __APPLE__ (none macosx)
// read regs and create thread from core file
static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
// we have to read prstatus_t from buf
// assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
prstatus_t* prstat = (prstatus_t*) buf;
thread_info* newthr;
sa_thread_info* newthr;
print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
// we set pthread_t to -1 for core dump
if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL)
......@@ -632,9 +1093,10 @@ static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
notep->n_type, notep->n_descsz);
if (notep->n_type == NT_PRSTATUS) {
if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true)
if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
return false;
}
}
p = descdata + ROUNDUP(notep->n_descsz, 4);
}
......@@ -681,7 +1143,9 @@ static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
switch (core_php->p_type) {
case PT_NOTE:
if (core_handle_note(ph, core_php) != true) goto err;
if (core_handle_note(ph, core_php) != true) {
goto err;
}
break;
case PT_LOAD: {
......@@ -800,7 +1264,6 @@ err:
return false;
}
#define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map)
#define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase)
#define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr)
......@@ -849,14 +1312,14 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) {
// read ld_base address from struct r_debug
// XXX: There is no r_ldbase member on BSD
/*
/*
if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
sizeof(uintptr_t)) != PS_OK) {
print_debug("can't read ld base address\n");
return false;
}
ph->core->ld_base_addr = ld_base_addr;
*/
*/
ph->core->ld_base_addr = 0;
print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
......@@ -947,7 +1410,7 @@ struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
if (ph == NULL) {
print_debug("can't allocate ps_prochandle\n");
print_debug("cant allocate ps_prochandle\n");
return NULL;
}
......@@ -963,6 +1426,8 @@ struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
ph->core->exec_fd = -1;
ph->core->interp_fd = -1;
print_debug("exec: %s core: %s", exec_file, core_file);
// open the core file
if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
print_debug("can't open core file\n");
......@@ -1014,9 +1479,12 @@ struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
if (init_classsharing_workaround(ph) != true)
goto err;
print_debug("Leave Pgrab_core\n");
return ph;
err:
Prelease(ph);
return NULL;
}
#endif // __APPLE__
/*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -28,32 +28,182 @@
#include <string.h>
#include <db.h>
#include <fcntl.h>
#include "libproc_impl.h"
#include "symtab.h"
#ifndef __APPLE__
#include "salibelf.h"
#endif // __APPLE__
// ----------------------------------------------------
// functions for symbol lookups
// ----------------------------------------------------
struct elf_section {
ELF_SHDR *c_shdr;
void *c_data;
};
struct elf_symbol {
char *name;
uintptr_t offset;
uintptr_t size;
};
typedef struct symtab_symbol {
char *name; // name like __ZThread_...
uintptr_t offset; // to loaded address
uintptr_t size; // size strlen
} symtab_symbol;
typedef struct symtab {
char *strs;
char *strs; // all symbols "__symbol1__'\0'__symbol2__...."
size_t num_symbols;
struct elf_symbol *symbols;
DB* hash_table;
symtab_symbol* symbols;
} symtab_t;
#ifdef __APPLE__
void build_search_table(symtab_t *symtab) {
int i;
for (i = 0; i < symtab->num_symbols; i++) {
DBT key, value;
key.data = symtab->symbols[i].name;
key.size = strlen(key.data) + 1;
value.data = &(symtab->symbols[i]);
value.size = sizeof(symtab_symbol);
(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);
// check result
if (is_debug()) {
DBT rkey, rvalue;
char* tmp = (char *)malloc(strlen(symtab->symbols[i].name) + 1);
strcpy(tmp, symtab->symbols[i].name);
rkey.data = tmp;
rkey.size = strlen(tmp) + 1;
(*symtab->hash_table->get)(symtab->hash_table, &rkey, &rvalue, 0);
// we may get a copy back so compare contents
symtab_symbol *res = (symtab_symbol *)rvalue.data;
if (strcmp(res->name, symtab->symbols[i].name) ||
res->offset != symtab->symbols[i].offset ||
res->size != symtab->symbols[i].size) {
print_debug("error to get hash_table value!\n");
}
free(tmp);
}
}
}
// read symbol table from given fd.
struct symtab* build_symtab(int fd) {
symtab_t* symtab = NULL;
int i;
mach_header_64 header;
off_t image_start;
if (!get_arch_off(fd, CPU_TYPE_X86_64, &image_start)) {
print_debug("failed in get fat header\n");
return NULL;
}
lseek(fd, image_start, SEEK_SET);
if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
print_debug("reading header failed!\n");
return NULL;
}
// header
if (header.magic != MH_MAGIC_64) {
print_debug("not a valid .dylib file\n");
return NULL;
}
load_command lcmd;
symtab_command symtabcmd;
nlist_64 lentry;
bool lcsymtab_exist = false;
long filepos = ltell(fd);
for (i = 0; i < header.ncmds; i++) {
lseek(fd, filepos, SEEK_SET);
if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
print_debug("read load_command failed for file\n");
return NULL;
}
filepos += lcmd.cmdsize; // next command position
if (lcmd.cmd == LC_SYMTAB) {
lseek(fd, -sizeof(load_command), SEEK_CUR);
lcsymtab_exist = true;
break;
}
}
if (!lcsymtab_exist) {
print_debug("No symtab command found!\n");
return NULL;
}
if (read(fd, (void *)&symtabcmd, sizeof(symtab_command)) != sizeof(symtab_command)) {
print_debug("read symtab_command failed for file");
return NULL;
}
symtab = (symtab_t *)malloc(sizeof(symtab_t));
if (symtab == NULL) {
print_debug("out of memory: allocating symtab\n");
return NULL;
}
// create hash table, we use berkeley db to
// manipulate the hash table.
symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL);
if (symtab->hash_table == NULL)
goto quit;
symtab->num_symbols = symtabcmd.nsyms;
symtab->symbols = (symtab_symbol *)malloc(sizeof(symtab_symbol) * symtab->num_symbols);
symtab->strs = (char *)malloc(sizeof(char) * symtabcmd.strsize);
if (symtab->symbols == NULL || symtab->strs == NULL) {
print_debug("out of memory: allocating symtab.symbol or symtab.strs\n");
goto quit;
}
lseek(fd, image_start + symtabcmd.symoff, SEEK_SET);
for (i = 0; i < symtab->num_symbols; i++) {
if (read(fd, (void *)&lentry, sizeof(nlist_64)) != sizeof(nlist_64)) {
print_debug("read nlist_64 failed at %i\n", i);
goto quit;
}
symtab->symbols[i].offset = lentry.n_value;
symtab->symbols[i].size = lentry.n_un.n_strx; // index
}
// string table
lseek(fd, image_start + symtabcmd.stroff, SEEK_SET);
int size = read(fd, (void *)(symtab->strs), symtabcmd.strsize * sizeof(char));
if (size != symtabcmd.strsize * sizeof(char)) {
print_debug("reading string table failed\n");
goto quit;
}
for (i = 0; i < symtab->num_symbols; i++) {
symtab->symbols[i].name = symtab->strs + symtab->symbols[i].size;
if (i > 0) {
// fix size
symtab->symbols[i - 1].size = symtab->symbols[i].size - symtab->symbols[i - 1].size;
print_debug("%s size = %d\n", symtab->symbols[i - 1].name, symtab->symbols[i - 1].size);
}
if (i == symtab->num_symbols - 1) {
// last index
symtab->symbols[i].size =
symtabcmd.strsize - symtab->symbols[i].size;
print_debug("%s size = %d\n", symtab->symbols[i].name, symtab->symbols[i].size);
}
}
// build a hashtable for fast query
build_search_table(symtab);
return symtab;
quit:
if (symtab) destroy_symtab(symtab);
return NULL;
}
#else // __APPLE__
struct elf_section {
ELF_SHDR *c_shdr;
void *c_data;
};
// read symbol table from given fd.
struct symtab* build_symtab(int fd) {
ELF_EHDR ehdr;
......@@ -176,7 +326,7 @@ struct symtab* build_symtab(int fd) {
key.data = sym_name;
key.size = strlen(sym_name) + 1;
value.data = &(symtab->symbols[j]);
value.size = sizeof(void *);
value.size = sizeof(symtab_symbol);
(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);
}
}
......@@ -201,30 +351,29 @@ quit:
return symtab;
}
void destroy_symtab(struct symtab* symtab) {
#endif // __APPLE__
void destroy_symtab(symtab_t* symtab) {
if (!symtab) return;
if (symtab->strs) free(symtab->strs);
if (symtab->symbols) free(symtab->symbols);
if (symtab->hash_table) {
(*symtab->hash_table->close)(symtab->hash_table);
}
free(symtab->strs);
free(symtab->symbols);
free(symtab);
}
uintptr_t search_symbol(struct symtab* symtab, uintptr_t base,
const char *sym_name, int *sym_size) {
uintptr_t search_symbol(struct symtab* symtab, uintptr_t base, const char *sym_name, int *sym_size) {
DBT key, value;
int ret;
// library does not have symbol table
if (!symtab || !symtab->hash_table)
if (!symtab || !symtab->hash_table) {
return 0;
}
key.data = (char*)(uintptr_t)sym_name;
key.size = strlen(sym_name) + 1;
ret = (*symtab->hash_table->get)(symtab->hash_table, &key, &value, 0);
if (ret == 0) {
struct elf_symbol *sym = value.data;
symtab_symbol *sym = value.data;
uintptr_t rslt = (uintptr_t) ((char*)base + sym->offset);
if (sym_size) *sym_size = sym->size;
return rslt;
......@@ -238,7 +387,7 @@ const char* nearest_symbol(struct symtab* symtab, uintptr_t offset,
int n = 0;
if (!symtab) return NULL;
for (; n < symtab->num_symbols; n++) {
struct elf_symbol* sym = &(symtab->symbols[n]);
symtab_symbol* sym = &(symtab->symbols[n]);
if (sym->name != NULL &&
offset >= sym->offset && offset < sym->offset + sym->size) {
if (poffset) *poffset = (offset - sym->offset);
......
/*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,11 +27,11 @@
#include <stdint.h>
// interface to manage ELF symbol tables
// interface to manage ELF or MachO symbol tables
struct symtab;
// build symbol table for a given ELF file descriptor
// build symbol table for a given ELF or MachO file escriptor
struct symtab* build_symtab(int fd);
// destroy the symbol table
......
/*
* Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -34,11 +34,18 @@ public class BsdVtblAccess extends BasicVtblAccess {
public BsdVtblAccess(SymbolLookup symbolLookup,
String[] dllNames) {
super(symbolLookup, dllNames);
if (symbolLookup.lookup("libjvm.so", "__vt_10JavaThread") != null ||
symbolLookup.lookup("libjvm_g.so", "__vt_10JavaThread") != null) {
boolean oldVT = false;
boolean isDarwin = dllNames[0].lastIndexOf(".dylib") != -1;
String vtJavaThread = isDarwin ? "_vt_10JavaThread" : "__vt_10JavaThread";
for (String dllName : dllNames) {
if (symbolLookup.lookup(dllName, vtJavaThread) != null) {
oldVT = true;
break;
}
}
if (oldVT) {
// old C++ ABI
vt = "__vt_";
vt = isDarwin ? "_vt_" : "__vt_";
} else {
// new C++ ABI
vt = "_ZTV";
......
......@@ -1517,7 +1517,7 @@ public class CommandProcessor {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
thread.printThreadIDOn(new PrintStream(bos));
if (all || bos.toString().equals(name)) {
out.println(bos.toString() + " = " + thread.getAddress());
out.println("Thread " + bos.toString() + " Address: " + thread.getAddress());
HTMLGenerator gen = new HTMLGenerator(false);
try {
out.println(gen.genHTMLForJavaStackTrace(thread));
......@@ -1546,7 +1546,7 @@ public class CommandProcessor {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
thread.printThreadIDOn(new PrintStream(bos));
if (all || bos.toString().equals(name)) {
out.println(bos.toString() + " = " + thread.getAddress());
out.println("Thread " + bos.toString() + " Address " + thread.getAddress());
if (!all) return;
}
}
......
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -311,6 +311,8 @@ public class HotSpotAgent {
setupDebuggerLinux();
} else if (os.equals("bsd")) {
setupDebuggerBsd();
} else if (os.equals("darwin")) {
setupDebuggerDarwin();
} else {
// Add support for more operating systems here
throw new DebuggerException("Operating system " + os + " not yet supported");
......@@ -370,6 +372,10 @@ public class HotSpotAgent {
db = new HotSpotTypeDataBase(machDesc,
new BsdVtblAccess(debugger, jvmLibNames),
debugger, jvmLibNames);
} else if (os.equals("darwin")) {
db = new HotSpotTypeDataBase(machDesc,
new BsdVtblAccess(debugger, jvmLibNames),
debugger, jvmLibNames);
} else {
throw new DebuggerException("OS \"" + os + "\" not yet supported (no VtblAccess yet)");
}
......@@ -459,6 +465,8 @@ public class HotSpotAgent {
setupJVMLibNamesLinux();
} else if (os.equals("bsd")) {
setupJVMLibNamesBsd();
} else if (os.equals("darwin")) {
setupJVMLibNamesDarwin();
} else {
throw new RuntimeException("Unknown OS type");
}
......@@ -567,6 +575,29 @@ public class HotSpotAgent {
jvmLibNames = new String[] { "libjvm.so", "libjvm_g.so" };
}
//
// Darwin
//
private void setupDebuggerDarwin() {
setupJVMLibNamesDarwin();
if (cpu.equals("amd64") || cpu.equals("x86_64")) {
machDesc = new MachineDescriptionAMD64();
} else {
throw new DebuggerException("Darwin only supported on x86_64. Current arch: " + cpu);
}
BsdDebuggerLocal dbg = new BsdDebuggerLocal(machDesc, !isServer);
debugger = dbg;
attachDebugger();
}
private void setupJVMLibNamesDarwin() {
jvmLibNames = new String[] { "libjvm.dylib", "libjvm_g.dylib" };
}
/** Convenience routine which should be called by per-platform
debugger setup. Should not be called when startupMode is
REMOTE_MODE. */
......
/*
* Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -31,6 +31,9 @@ import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.debugger.x86.*;
import sun.jvm.hotspot.debugger.cdbg.*;
import sun.jvm.hotspot.utilities.*;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.runtime.Threads;
import sun.jvm.hotspot.runtime.JavaThread;
import java.lang.reflect.*;
/** <P> An implementation of the JVMDebugger interface. The basic debug
......@@ -55,6 +58,7 @@ public class BsdDebuggerLocal extends DebuggerBase implements BsdDebugger {
private long symbolicator; // macosx symbolicator handle
private long task; // macosx task handle
private boolean isCore;
private boolean isDarwin; // variant for bsd
// CDebugger support
private BsdCDebugger cdbg;
......@@ -208,6 +212,7 @@ public class BsdDebuggerLocal extends DebuggerBase implements BsdDebugger {
}
}
isDarwin = getOS().equals("darwin");
workerThread = new BsdDebuggerLocalWorkerThread(this);
workerThread.start();
}
......@@ -240,8 +245,11 @@ public class BsdDebuggerLocal extends DebuggerBase implements BsdDebugger {
/* called from attach methods */
private void findABIVersion() throws DebuggerException {
if (lookupByName0("libjvm.so", "__vt_10JavaThread") != 0 ||
lookupByName0("libjvm_g.so", "__vt_10JavaThread") != 0) {
String libjvmName = isDarwin ? "libjvm.dylib" : "libjvm.so";
String libjvm_gName = isDarwin? "libjvm_g.dylib" : "libjvm_g.so";
String javaThreadVt = isDarwin ? "_vt_10JavaThread" : "__vt_10JavaThread";
if (lookupByName0(libjvmName, javaThreadVt) != 0 ||
lookupByName0(libjvm_gName, javaThreadVt) != 0) {
// old C++ ABI
useGCC32ABI = false;
} else {
......@@ -360,7 +368,8 @@ public class BsdDebuggerLocal extends DebuggerBase implements BsdDebugger {
}
if (isCore) {
long addr = lookupByName0(objectName, symbol);
// MacOSX symbol with "_" as leading
long addr = lookupByName0(objectName, isDarwin ? "_" + symbol : symbol);
return (addr == 0)? null : new BsdAddress(this, handleGCC32ABI(addr, symbol));
} else {
class LookupByNameTask implements WorkerThreadTask {
......@@ -403,12 +412,12 @@ public class BsdDebuggerLocal extends DebuggerBase implements BsdDebugger {
public ThreadProxy getThreadForIdentifierAddress(Address threadIdAddr, Address uniqueThreadIdAddr) {
return new BsdThread(this, threadIdAddr, uniqueThreadIdAddr);
}
@Override
public ThreadProxy getThreadForIdentifierAddress(Address addr) {
throw new RuntimeException("unimplemented");
}
/** From the ThreadAccess interface via Debugger and JVMDebugger */
public ThreadProxy getThreadForThreadId(long id) {
return new BsdThread(this, id);
......@@ -601,6 +610,33 @@ public class BsdDebuggerLocal extends DebuggerBase implements BsdDebugger {
throw new DebuggerException("Unimplemented");
}
/** this functions used for core file reading and called from native attach0,
it returns an array of long integers as
[thread_id, stack_start, stack_end, thread_id, stack_start, stack_end, ....] for
all java threads recorded in Threads. Also adds the ThreadProxy to threadList */
public long[] getJavaThreadsInfo() {
requireAttach();
Threads threads = VM.getVM().getThreads();
int len = threads.getNumberOfThreads();
long[] result = new long[len * 3]; // triple
JavaThread t = threads.first();
long beg, end;
int i = 0;
while (t != null) {
end = t.getStackBaseValue();
beg = end - t.getStackSize();
BsdThread bsdt = (BsdThread)t.getThreadProxy();
long uid = bsdt.getUniqueThreadId();
if (threadList != null) threadList.add(bsdt);
result[i] = uid;
result[i + 1] = beg;
result[i + 2] = end;
t = t.next();
i += 3;
}
return result;
}
static {
System.loadLibrary("saproc");
init0();
......
/*
* Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -44,7 +44,8 @@ class BsdThread implements ThreadProxy {
BsdThread(BsdDebugger debugger, long id) {
this.debugger = debugger;
this.thread_id = (int) id;
// use unique_thread_id to identify thread
this.unique_thread_id = id;
}
public boolean equals(Object obj) {
......@@ -52,7 +53,7 @@ class BsdThread implements ThreadProxy {
return false;
}
return (((BsdThread) obj).thread_id == thread_id);
return (((BsdThread) obj).unique_thread_id == unique_thread_id);
}
public int hashCode() {
......@@ -80,4 +81,9 @@ class BsdThread implements ThreadProxy {
throws IllegalThreadStateException, DebuggerException {
throw new DebuggerException("Unimplemented");
}
/** this is not interface function, used in core file to get unique thread id on Macosx*/
public long getUniqueThreadId() {
return unique_thread_id;
}
}
/*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -320,6 +320,10 @@ public class JavaThread extends Thread {
return stackBaseField.getValue(addr);
}
public long getStackBaseValue() {
return VM.getVM().getAddressValue(getStackBase());
}
public long getStackSize() {
return stackSizeField.getValue(addr);
}
......
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -42,6 +42,7 @@ import sun.jvm.hotspot.utilities.*;
public class Threads {
private static JavaThreadFactory threadFactory;
private static AddressField threadListField;
private static CIntegerField numOfThreadsField;
private static VirtualConstructor virtualConstructor;
private static JavaThreadPDAccess access;
......@@ -57,6 +58,7 @@ public class Threads {
Type type = db.lookupType("Threads");
threadListField = type.getAddressField("_thread_list");
numOfThreadsField = type.getCIntegerField("_number_of_threads");
// Instantiate appropriate platform-specific JavaThreadFactory
String os = VM.getVM().getOS();
......@@ -102,6 +104,10 @@ public class Threads {
} else if (cpu.equals("amd64") || cpu.equals("x86_64")) {
access = new BsdAMD64JavaThreadPDAccess();
}
} else if (os.equals("darwin")) {
if (cpu.equals("amd64") || cpu.equals("x86_64")) {
access = new BsdAMD64JavaThreadPDAccess();
}
}
if (access == null) {
......@@ -144,6 +150,10 @@ public class Threads {
return createJavaThreadWrapper(threadAddr);
}
public int getNumberOfThreads() {
return (int) numOfThreadsField.getValue();
}
/** Routine for instantiating appropriately-typed wrapper for a
JavaThread. Currently needs to be public for OopUtilities to
access it. */
......
/*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -32,6 +32,7 @@ import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.debugger.cdbg.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.utilities.PlatformInfo;
public class PStack extends Tool {
// in non-verbose mode, Method*s are not printed in java frames
......@@ -54,6 +55,11 @@ public class PStack extends Tool {
}
public void run(PrintStream out, Debugger dbg) {
if (PlatformInfo.getOS().equals("darwin")) {
out.println("Not available on Darwin");
return;
}
CDebugger cdbg = dbg.getCDebugger();
if (cdbg != null) {
ConcurrentLocksPrinter concLocksPrinter = null;
......
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -43,8 +43,8 @@ public class PlatformInfo {
return "bsd";
} else if (os.equals("OpenBSD")) {
return "bsd";
} else if (os.equals("Darwin") || os.contains("OS X")) {
return "bsd";
} else if (os.contains("Darwin") || os.contains("OS X")) {
return "darwin";
} else if (os.startsWith("Windows")) {
return "win32";
} else {
......
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -48,7 +48,10 @@
#include <string.h>
#include <dlfcn.h>
#ifndef __APPLE__
#include <link.h>
#endif
#endif
......@@ -109,9 +112,7 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIE
jstring libname_s) {
uintptr_t func = 0;
const char* error_message = NULL;
const char* java_home;
jboolean isCopy;
uintptr_t *handle = NULL;
const char * jrepath = (*env)->GetStringUTFChars(env, jrepath_s, &isCopy); // like $JAVA_HOME/jre/lib/sparc/
const char * libname = (*env)->GetStringUTFChars(env, libname_s, &isCopy);
......@@ -167,7 +168,8 @@ typedef void* (*decode_func)(uintptr_t start_va, uintptr_t end_va,
void* event_stream,
int (*printf_callback)(void*, const char*, ...),
void* printf_stream,
const char* options);
const char* options,
int newline);
/* container for call back state when decoding instructions */
typedef struct {
......@@ -281,7 +283,7 @@ JNIEXPORT void JNICALL Java_sun_jvm_hotspot_asm_Disassembler_decode(JNIEnv * env
end - start,
&event_to_env, (void*) &denv,
&printf_to_env, (void*) &denv,
options);
options, 0 /* newline */);
/* cleanup */
(*env)->ReleaseByteArrayElements(env, code, start, JNI_ABORT);
......
#
# Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
......@@ -24,7 +24,7 @@
# Rules to build serviceability agent library, used by vm.make
# libsaproc.so: serviceability agent
# libsaproc.so(dylib): serviceability agent
SAPROC = saproc
ifeq ($(OS_VENDOR), Darwin)
......@@ -37,7 +37,7 @@ AGENT_DIR = $(GAMMADIR)/agent
SASRCDIR = $(AGENT_DIR)/src/os/$(Platform_os_family)
NON_STUB_SASRCFILES = $(SASRCDIR)/salibelf.c \
BSD_NON_STUB_SASRCFILES = $(SASRCDIR)/salibelf.c \
$(SASRCDIR)/symtab.c \
$(SASRCDIR)/libproc_impl.c \
$(SASRCDIR)/ps_proc.c \
......@@ -45,13 +45,19 @@ NON_STUB_SASRCFILES = $(SASRCDIR)/salibelf.c \
$(SASRCDIR)/BsdDebuggerLocal.c \
$(AGENT_DIR)/src/share/native/sadis.c
DARWIN_NON_STUB_SASRCFILES = $(SASRCDIR)/symtab.c \
$(SASRCDIR)/libproc_impl.c \
$(SASRCDIR)/ps_core.c \
$(SASRCDIR)/MacosxDebuggerLocal.m \
$(AGENT_DIR)/src/share/native/sadis.c
ifeq ($(OS_VENDOR), FreeBSD)
SASRCFILES = $(NON_STUB_SASRCFILES)
SASRCFILES = $(BSD_NON_STUB_SASRCFILES)
SALIBS = -lutil -lthread_db
SAARCH = $(ARCHFLAG)
else
ifeq ($(OS_VENDOR), Darwin)
SASRCFILES = $(SASRCDIR)/MacosxDebuggerLocal.m
SASRCFILES = $(DARWIN_NON_STUB_SASRCFILES)
SALIBS = -g -framework Foundation -F/System/Library/Frameworks/JavaVM.framework/Frameworks -framework JavaNativeFoundation -framework Security -framework CoreFoundation
#objc compiler blows up on -march=i586, perhaps it should not be included in the macosx intel 32-bit C++ compiles?
SAARCH = $(subst -march=i586,,$(ARCHFLAG))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册