|
|
|
@ -22,6 +22,12 @@
|
|
|
|
|
#include <mach-o/arch.h>
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
#include <cxxabi.h>
|
|
|
|
|
#ifdef WITH_UCONTEXT
|
|
|
|
|
#include <sys/ucontext.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT)
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
|
|
|
|
@ -104,6 +110,10 @@ static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, c
|
|
|
|
|
class CrashLogOSX : public CrashLog {
|
|
|
|
|
/** Signal that has been thrown. */
|
|
|
|
|
int signum;
|
|
|
|
|
siginfo_t *si;
|
|
|
|
|
void *context;
|
|
|
|
|
bool signal_instruction_ptr_valid;
|
|
|
|
|
void *signal_instruction_ptr;
|
|
|
|
|
|
|
|
|
|
char filename_log[MAX_PATH]; ///< Path of crash.log
|
|
|
|
|
char filename_save[MAX_PATH]; ///< Path of crash.sav
|
|
|
|
@ -130,14 +140,32 @@ class CrashLogOSX : public CrashLog {
|
|
|
|
|
|
|
|
|
|
char *LogError(char *buffer, const char *last, const char *message) const override
|
|
|
|
|
{
|
|
|
|
|
return buffer + seprintf(buffer, last,
|
|
|
|
|
buffer += seprintf(buffer, last,
|
|
|
|
|
"Crash reason:\n"
|
|
|
|
|
" Signal: %s (%d)\n"
|
|
|
|
|
" Message: %s\n\n",
|
|
|
|
|
" Signal: %s (%d)\n",
|
|
|
|
|
strsignal(this->signum),
|
|
|
|
|
this->signum,
|
|
|
|
|
this->signum
|
|
|
|
|
);
|
|
|
|
|
if (this->si) {
|
|
|
|
|
buffer += seprintf(buffer, last,
|
|
|
|
|
" si_code: %d\n",
|
|
|
|
|
this->si->si_code);
|
|
|
|
|
if (this->signum != SIGABRT) {
|
|
|
|
|
buffer += seprintf(buffer, last,
|
|
|
|
|
" Fault address: %p\n",
|
|
|
|
|
this->si->si_addr);
|
|
|
|
|
if (this->signal_instruction_ptr_valid) {
|
|
|
|
|
buffer += seprintf(buffer, last,
|
|
|
|
|
" Instruction address: %p\n",
|
|
|
|
|
this->signal_instruction_ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buffer += seprintf(buffer, last,
|
|
|
|
|
" Message: %s\n\n",
|
|
|
|
|
message == nullptr ? "<none>" : message
|
|
|
|
|
);
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *LogStacktrace(char *buffer, const char *last) const override
|
|
|
|
@ -149,22 +177,9 @@ class CrashLogOSX : public CrashLog {
|
|
|
|
|
buffer += seprintf(buffer, last, "\nStacktrace:\n");
|
|
|
|
|
|
|
|
|
|
void **frame;
|
|
|
|
|
#if defined(__ppc__) || defined(__ppc64__)
|
|
|
|
|
/* Apple says __builtin_frame_address can be broken on PPC. */
|
|
|
|
|
__asm__ volatile("mr %0, r1" : "=r" (frame));
|
|
|
|
|
#else
|
|
|
|
|
frame = (void **)__builtin_frame_address(0);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
for (int i = 0; frame != nullptr && i < MAX_STACK_FRAMES; i++) {
|
|
|
|
|
/* Get IP for current stack frame. */
|
|
|
|
|
#if defined(__ppc__) || defined(__ppc64__)
|
|
|
|
|
void *ip = frame[2];
|
|
|
|
|
#else
|
|
|
|
|
void *ip = frame[1];
|
|
|
|
|
#endif
|
|
|
|
|
if (ip == nullptr) break;
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
auto print_frame = [&](void *ip) {
|
|
|
|
|
/* Print running index. */
|
|
|
|
|
buffer += seprintf(buffer, last, " [%02d]", i);
|
|
|
|
|
|
|
|
|
@ -196,6 +211,49 @@ class CrashLogOSX : public CrashLog {
|
|
|
|
|
free(func_name);
|
|
|
|
|
}
|
|
|
|
|
buffer += seprintf(buffer, last, "\n");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#if defined(__ppc__) || defined(__ppc64__)
|
|
|
|
|
/* Apple says __builtin_frame_address can be broken on PPC. */
|
|
|
|
|
__asm__ volatile("mr %0, r1" : "=r" (frame));
|
|
|
|
|
#elif defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT) && (defined(__x86_64__) || defined(__i386))
|
|
|
|
|
if (this->signal_instruction_ptr_valid) {
|
|
|
|
|
print_frame(this->signal_instruction_ptr);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
|
char *stacktop = (char *) pthread_get_stackaddr_np(self);
|
|
|
|
|
char *stackbot = stacktop - pthread_get_stacksize_np(self);
|
|
|
|
|
stacktop -= 2 * sizeof(void *);
|
|
|
|
|
|
|
|
|
|
ucontext_t *ucontext = static_cast<ucontext_t *>(context);
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
void **bp = (void **) ucontext->uc_mcontext->__ss.__rbp;
|
|
|
|
|
void **sp = (void **) ucontext->uc_mcontext->__ss.__rsp;
|
|
|
|
|
#else
|
|
|
|
|
void **bp = (void **) ucontext->uc_mcontext->__ss.__ebp;
|
|
|
|
|
void **sp = (void **) ucontext->uc_mcontext->__ss.__esp;
|
|
|
|
|
#endif
|
|
|
|
|
if (IS_ALIGNED(bp) && reinterpret_cast<uintptr_t>(bp) >= reinterpret_cast<uintptr_t>(sp) && reinterpret_cast<uintptr_t>(bp) >= reinterpret_cast<uintptr_t>(stackbot) && reinterpret_cast<uintptr_t>(bp) <= reinterpret_cast<uintptr_t>(stacktop)) {
|
|
|
|
|
frame = bp;
|
|
|
|
|
} else {
|
|
|
|
|
frame = nullptr;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
frame = (void **)__builtin_frame_address(0);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
for (; frame != nullptr && i < MAX_STACK_FRAMES; i++) {
|
|
|
|
|
/* Get IP for current stack frame. */
|
|
|
|
|
#if defined(__ppc__) || defined(__ppc64__)
|
|
|
|
|
void *ip = frame[2];
|
|
|
|
|
#else
|
|
|
|
|
void *ip = frame[1];
|
|
|
|
|
#endif
|
|
|
|
|
if (ip == nullptr) break;
|
|
|
|
|
|
|
|
|
|
print_frame(ip);
|
|
|
|
|
|
|
|
|
|
/* Get address of next stack frame. */
|
|
|
|
|
void **next = (void **)frame[0];
|
|
|
|
@ -226,6 +284,7 @@ class CrashLogOSX : public CrashLog {
|
|
|
|
|
buffer += seprintf(buffer, last, "LLDB info:\n");
|
|
|
|
|
|
|
|
|
|
char pid_buffer[16];
|
|
|
|
|
char disasm_buffer[64];
|
|
|
|
|
|
|
|
|
|
seprintf(pid_buffer, lastof(pid_buffer), "%d", pid);
|
|
|
|
|
|
|
|
|
@ -237,7 +296,13 @@ class CrashLogOSX : public CrashLog {
|
|
|
|
|
args.push_back("--batch");
|
|
|
|
|
|
|
|
|
|
args.push_back("-o");
|
|
|
|
|
args.push_back(IsNonMainThread() ? "bt all" : "bt");
|
|
|
|
|
args.push_back(IsNonMainThread() ? "bt all" : "bt 100");
|
|
|
|
|
|
|
|
|
|
if (this->GetMessage() == nullptr && this->signal_instruction_ptr_valid) {
|
|
|
|
|
seprintf(disasm_buffer, lastof(disasm_buffer), "disassemble -b -F intel -c 1 -s %p", this->signal_instruction_ptr);
|
|
|
|
|
args.push_back("-o");
|
|
|
|
|
args.push_back(disasm_buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args.push_back(nullptr);
|
|
|
|
|
if (!ExecReadStdout("lldb", const_cast<char* const*>(&(args[0])), buffer, last)) {
|
|
|
|
@ -255,6 +320,57 @@ class CrashLogOSX : public CrashLog {
|
|
|
|
|
{
|
|
|
|
|
buffer = LogLldbInfo(buffer, last);
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_UCONTEXT
|
|
|
|
|
ucontext_t *ucontext = static_cast<ucontext_t *>(context);
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
const auto &gregs = ucontext->uc_mcontext->__ss;
|
|
|
|
|
buffer += seprintf(buffer, last,
|
|
|
|
|
"Registers:\n"
|
|
|
|
|
" rax: %#16llx rbx: %#16llx rcx: %#16llx rdx: %#16llx\n"
|
|
|
|
|
" rsi: %#16llx rdi: %#16llx rbp: %#16llx rsp: %#16llx\n"
|
|
|
|
|
" r8: %#16llx r9: %#16llx r10: %#16llx r11: %#16llx\n"
|
|
|
|
|
" r12: %#16llx r13: %#16llx r14: %#16llx r15: %#16llx\n"
|
|
|
|
|
" rip: %#16llx rflags: %#8llx\n\n",
|
|
|
|
|
gregs.__rax,
|
|
|
|
|
gregs.__rbx,
|
|
|
|
|
gregs.__rcx,
|
|
|
|
|
gregs.__rdx,
|
|
|
|
|
gregs.__rsi,
|
|
|
|
|
gregs.__rdi,
|
|
|
|
|
gregs.__rbp,
|
|
|
|
|
gregs.__rsp,
|
|
|
|
|
gregs.__r8,
|
|
|
|
|
gregs.__r9,
|
|
|
|
|
gregs.__r10,
|
|
|
|
|
gregs.__r11,
|
|
|
|
|
gregs.__r12,
|
|
|
|
|
gregs.__r13,
|
|
|
|
|
gregs.__r14,
|
|
|
|
|
gregs.__r15,
|
|
|
|
|
gregs.__rip,
|
|
|
|
|
gregs.__rflags
|
|
|
|
|
);
|
|
|
|
|
#elif defined(__i386)
|
|
|
|
|
const auto &gregs = ucontext->uc_mcontext->__ss;
|
|
|
|
|
buffer += seprintf(buffer, last,
|
|
|
|
|
"Registers:\n"
|
|
|
|
|
" eax: %#8x ebx: %#8x ecx: %#8x edx: %#8x\n"
|
|
|
|
|
" esi: %#8x edi: %#8x ebp: %#8x esp: %#8x\n"
|
|
|
|
|
" eip: %#8x eflags: %#8x\n\n",
|
|
|
|
|
gregs.__eax,
|
|
|
|
|
gregs.__ebx,
|
|
|
|
|
gregs.__ecx,
|
|
|
|
|
gregs.__edx,
|
|
|
|
|
gregs.__esi,
|
|
|
|
|
gregs.__edi,
|
|
|
|
|
gregs.__ebp,
|
|
|
|
|
gregs.__esp,
|
|
|
|
|
gregs.__eip,
|
|
|
|
|
gregs.__eflags
|
|
|
|
|
);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -263,11 +379,24 @@ public:
|
|
|
|
|
* A crash log is always generated by signal.
|
|
|
|
|
* @param signum the signal that was caused by the crash.
|
|
|
|
|
*/
|
|
|
|
|
CrashLogOSX(int signum) : signum(signum)
|
|
|
|
|
CrashLogOSX(int signum, siginfo_t *si, void *context) : signum(signum), si(si), context(context)
|
|
|
|
|
{
|
|
|
|
|
filename_log[0] = '\0';
|
|
|
|
|
filename_save[0] = '\0';
|
|
|
|
|
filename_screenshot[0] = '\0';
|
|
|
|
|
|
|
|
|
|
this->signal_instruction_ptr_valid = false;
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_UCONTEXT
|
|
|
|
|
ucontext_t *ucontext = static_cast<ucontext_t *>(context);
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
this->signal_instruction_ptr = (void *) ucontext->uc_mcontext->__ss.__rip;
|
|
|
|
|
this->signal_instruction_ptr_valid = true;
|
|
|
|
|
#elif defined(__i386)
|
|
|
|
|
this->signal_instruction_ptr = (void *) ucontext->uc_mcontext->__ss.__eip;
|
|
|
|
|
this->signal_instruction_ptr_valid = true;
|
|
|
|
|
#endif
|
|
|
|
|
#endif /* WITH_UCONTEXT */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Generate the crash log. */
|
|
|
|
@ -328,7 +457,7 @@ static const int _signals_to_handle[] = { SIGSEGV, SIGABRT, SIGFPE, SIGBUS, SIGI
|
|
|
|
|
* @note Not static so it shows up in the backtrace.
|
|
|
|
|
* @param signum the signal that caused us to crash.
|
|
|
|
|
*/
|
|
|
|
|
void CDECL HandleCrash(int signum)
|
|
|
|
|
void CDECL HandleCrash(int signum, siginfo_t *si, void *context)
|
|
|
|
|
{
|
|
|
|
|
/* Disable all handling of signals by us, so we don't go into infinite loops. */
|
|
|
|
|
for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) {
|
|
|
|
@ -343,7 +472,7 @@ void CDECL HandleCrash(int signum)
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CrashLogOSX log(signum);
|
|
|
|
|
CrashLogOSX log(signum, si, context);
|
|
|
|
|
log.MakeCrashLog();
|
|
|
|
|
log.DisplayCrashDialog();
|
|
|
|
|
|
|
|
|
@ -353,7 +482,23 @@ void CDECL HandleCrash(int signum)
|
|
|
|
|
|
|
|
|
|
/* static */ void CrashLog::InitialiseCrashLog()
|
|
|
|
|
{
|
|
|
|
|
#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT)
|
|
|
|
|
const size_t stack_size = max<size_t>(SIGSTKSZ, 512*1024);
|
|
|
|
|
stack_t ss;
|
|
|
|
|
ss.ss_sp = CallocT<byte>(stack_size);
|
|
|
|
|
ss.ss_size = stack_size;
|
|
|
|
|
ss.ss_flags = 0;
|
|
|
|
|
sigaltstack(&ss, nullptr);
|
|
|
|
|
#endif
|
|
|
|
|
for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) {
|
|
|
|
|
signal(*i, HandleCrash);
|
|
|
|
|
struct sigaction sa;
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
|
sa.sa_flags = SA_SIGINFO | SA_RESTART;
|
|
|
|
|
#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT)
|
|
|
|
|
sa.sa_flags |= SA_ONSTACK;
|
|
|
|
|
#endif
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
sa.sa_sigaction = HandleCrash;
|
|
|
|
|
sigaction(*i, &sa, nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|