Add sigaction and ucontext support to Mac crashlog

This commit is contained in:
Jonathan G Rennison 2019-04-23 18:04:08 +01:00
parent 73c8715eb6
commit 61a4ecf078
2 changed files with 135 additions and 9 deletions

View File

@ -1908,6 +1908,31 @@ EOL
fi
fi
fi
log 2 "executing $cc_host $CFLAGS $CFLAGS_ENV $LDFLAGS $LDFLAGS_ENV $STATIC_FLAGS -o tmp.config.ucontext -x c++ - -ldl"
"$cc_host" $CFLAGS $CFLAGS_ENV $LDFLAGS $LDFLAGS_ENV $STATIC_FLAGS -o tmp.config.ucontext -x c++ - -ldl 2> /dev/null << EOL
#include <sys/ucontext.h>
int main() {
ucontext_t context;
#if defined(__x86_64__)
void *ptr = (void *) context.uc_mcontext->__ss.__rip;
#elif defined(__i386)
void *ptr = (void *) context.uc_mcontext->__ss.__rip;
#else
#error Unknown arch
#endif
return 0;
}
EOL
ret=$?
rm -f tmp.config.ucontext
log 2 " exit code $ret"
if [ $ret -ne 0 ]; then
log 1 "checking ucontext... no"
else
log 1 "checking ucontext... found"
CFLAGS="$CFLAGS -DWITH_UCONTEXT"
fi
fi
if [ "$os" = "HAIKU" ]; then

View File

@ -22,6 +22,9 @@
#include <mach-o/arch.h>
#include <dlfcn.h>
#include <cxxabi.h>
#ifdef WITH_UCONTEXT
#include <sys/ucontext.h>
#endif
#include "../../safeguards.h"
@ -104,6 +107,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 +137,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,
message == NULL ? "<none>" : message
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
@ -226,6 +251,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);
@ -239,6 +265,12 @@ class CrashLogOSX : public CrashLog {
args.push_back("-o");
args.push_back(IsNonMainThread() ? "bt all" : "bt");
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)) {
buffer = buffer_orig;
@ -255,6 +287,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 +346,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. */
@ -327,7 +423,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++) {
@ -342,7 +438,7 @@ void CDECL HandleCrash(int signum)
abort();
}
CrashLogOSX log(signum);
CrashLogOSX log(signum, si, context);
log.MakeCrashLog();
log.DisplayCrashDialog();
@ -353,6 +449,11 @@ void CDECL HandleCrash(int signum)
/* static */ void CrashLog::InitialiseCrashLog()
{
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;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = HandleCrash;
sigaction(*i, &sa, nullptr);
}
}