Add sigaltstack support to MacOS crashlog

This commit is contained in:
Jonathan G Rennison 2019-04-23 19:14:02 +01:00
parent 145fee04fa
commit cc970aac70
2 changed files with 87 additions and 16 deletions

View File

@ -1956,6 +1956,33 @@ EOL
log 1 "checking ucontext... found" log 1 "checking ucontext... found"
CFLAGS="$CFLAGS -DWITH_UCONTEXT" CFLAGS="$CFLAGS -DWITH_UCONTEXT"
fi fi
log 2 "executing $cc_host $CFLAGS $CFLAGS_ENV $LDFLAGS $LDFLAGS_ENV $STATIC_FLAGS -o tmp.config.sigaltstack -x c++ - -ldl"
"$cc_host" $CFLAGS $CFLAGS_ENV $LDFLAGS $LDFLAGS_ENV $STATIC_FLAGS -o tmp.config.sigaltstack -x c++ - -ldl 2> /dev/null << EOL
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
int main() {
pthread_t self = pthread_self();
void *stacktop = pthread_get_stackaddr_np(self);
size_t stack_size = pthread_get_stacksize_np(self);
stack_t ss;
ss.ss_sp = calloc(SIGSTKSZ, 1);
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
sigaltstack(&ss, NULL);
return 0;
}
EOL
ret=$?
rm -f tmp.config.sigaltstack
log 2 " exit code $ret"
if [ $ret -ne 0 ]; then
log 1 "checking sigaltstack... no"
else
log 1 "checking sigaltstack... found"
CFLAGS="$CFLAGS -DWITH_SIGALTSTACK"
fi
fi fi
if [ "$os" = "HAIKU" ]; then if [ "$os" = "HAIKU" ]; then

View File

@ -25,6 +25,9 @@
#ifdef WITH_UCONTEXT #ifdef WITH_UCONTEXT
#include <sys/ucontext.h> #include <sys/ucontext.h>
#endif #endif
#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT)
#include <pthread.h>
#endif
#include "../../safeguards.h" #include "../../safeguards.h"
@ -174,22 +177,9 @@ class CrashLogOSX : public CrashLog {
buffer += seprintf(buffer, last, "\nStacktrace:\n"); buffer += seprintf(buffer, last, "\nStacktrace:\n");
void **frame; void **frame;
#if defined(__ppc__) || defined(__ppc64__) int i = 0;
/* 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 != NULL && 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 == NULL) break;
auto print_frame = [&](void *ip) {
/* Print running index. */ /* Print running index. */
buffer += seprintf(buffer, last, " [%02d]", i); buffer += seprintf(buffer, last, " [%02d]", i);
@ -221,6 +211,49 @@ class CrashLogOSX : public CrashLog {
free(func_name); free(func_name);
} }
buffer += seprintf(buffer, last, "\n"); 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. */ /* Get address of next stack frame. */
void **next = (void **)frame[0]; void **next = (void **)frame[0];
@ -263,7 +296,7 @@ class CrashLogOSX : public CrashLog {
args.push_back("--batch"); args.push_back("--batch");
args.push_back("-o"); 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) { 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); seprintf(disasm_buffer, lastof(disasm_buffer), "disassemble -b -F intel -c 1 -s %p", this->signal_instruction_ptr);
@ -448,10 +481,21 @@ void CDECL HandleCrash(int signum, siginfo_t *si, void *context)
/* static */ void CrashLog::InitialiseCrashLog() /* 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++) { for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) {
struct sigaction sa; struct sigaction sa;
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO | SA_RESTART; sa.sa_flags = SA_SIGINFO | SA_RESTART;
#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT)
sa.sa_flags |= SA_ONSTACK;
#endif
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_sigaction = HandleCrash; sa.sa_sigaction = HandleCrash;
sigaction(*i, &sa, nullptr); sigaction(*i, &sa, nullptr);