mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 10:48:29 +00:00
Implement crash reporting for AARCH64
The ShowCrashReports() feature for aarch64 should work even better than the x86 crash reports. Thanks to the benefit of hindsight these reports should be rock solid reliable and beautiful to read. This change also improves the syscall polyfills for aarch64. Some of the sys_foo() functions have been removed, usually because they're legacy or downright footguns not worth building.
This commit is contained in:
parent
285e8a2348
commit
1f2a5a8fc1
42 changed files with 540 additions and 247 deletions
|
@ -16,8 +16,34 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
static const char *TryMonoRepoPath(const char *var, const char *path) {
|
||||
const char buf[PATH_MAX];
|
||||
if (getenv(var)) return 0;
|
||||
if (!isexecutable(path)) return 0;
|
||||
if (*path != '/') {
|
||||
if (getcwd(buf, sizeof(buf)) <= 0) return 0;
|
||||
strlcat(buf, "/", sizeof(buf));
|
||||
strlcat(buf, path, sizeof(buf));
|
||||
path = buf;
|
||||
}
|
||||
setenv(var, path, false);
|
||||
return getenv(var);
|
||||
}
|
||||
|
||||
const char *GetAddr2linePath(void) {
|
||||
return commandvenv("ADDR2LINE", "addr2line");
|
||||
const char *s = 0;
|
||||
#ifdef __x86_64__
|
||||
s = TryMonoRepoPath("ADDR2LINE",
|
||||
"o/third_party/gcc/bin/x86_64-linux-musl-addr2line");
|
||||
#elif defined(__aarch64__)
|
||||
s = TryMonoRepoPath("ADDR2LINE",
|
||||
"o/third_party/gcc/bin/aarch64-linux-musl-addr2line");
|
||||
#endif
|
||||
if (!s) s = commandvenv("ADDR2LINE", "addr2line");
|
||||
return s;
|
||||
}
|
||||
|
|
|
@ -117,13 +117,13 @@ static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
|
|||
if (sys_pipe2(pipefds, O_CLOEXEC) == -1) {
|
||||
return -1;
|
||||
}
|
||||
if ((pid = __sys_fork().ax) == -1) {
|
||||
if ((pid = sys_fork()) == -1) {
|
||||
sys_close(pipefds[0]);
|
||||
sys_close(pipefds[1]);
|
||||
return -1;
|
||||
}
|
||||
if (!pid) {
|
||||
sys_dup2(pipefds[1], 1);
|
||||
sys_dup2(pipefds[1], 1, 0);
|
||||
sys_execve(addr2line, argv, environ);
|
||||
_Exit(127);
|
||||
}
|
||||
|
|
|
@ -67,25 +67,33 @@ noinstrument noasan int PrintBacktraceUsingSymbols(int fd,
|
|||
break;
|
||||
}
|
||||
addr = frame->addr;
|
||||
if (!addr) break;
|
||||
if (addr == (intptr_t)_weaken(__gc)) {
|
||||
do {
|
||||
--gi;
|
||||
} while ((addr = garbage->p[gi].ret) == (intptr_t)_weaken(__gc));
|
||||
}
|
||||
/*
|
||||
* we subtract one to handle the case of noreturn functions with a
|
||||
* call instruction at the end, since %rip in such cases will point
|
||||
* to the start of the next function. generally %rip always points
|
||||
* to the byte after the instruction. one exception is in case like
|
||||
* __restore_rt where the kernel creates a stack frame that points
|
||||
* to the beginning of the function.
|
||||
*/
|
||||
if ((symbol = __get_symbol(st, addr - 1)) != -1 ||
|
||||
(symbol = __get_symbol(st, addr - 0)) != -1) {
|
||||
addend = addr - st->addr_base;
|
||||
addend -= st->symbols[symbol].x;
|
||||
if (addr) {
|
||||
if (
|
||||
#ifdef __x86_64__
|
||||
/*
|
||||
* we subtract one to handle the case of noreturn functions
|
||||
* with a call instruction at the end, since %rip in such
|
||||
* cases will point to the start of the next function.
|
||||
* generally %rip always points to the byte after the
|
||||
* instruction. one exception is in case like __restore_rt
|
||||
* where the kernel creates a stack frame that points to the
|
||||
* beginning of the function.
|
||||
*/
|
||||
(symbol = __get_symbol(st, addr - 1)) != -1 ||
|
||||
#endif
|
||||
(symbol = __get_symbol(st, addr)) != -1) {
|
||||
addend = addr - st->addr_base;
|
||||
addend -= st->symbols[symbol].x;
|
||||
} else {
|
||||
addend = 0;
|
||||
}
|
||||
} else {
|
||||
symbol = 0;
|
||||
addend = 0;
|
||||
}
|
||||
kprintf("%012lx %lx %s%+d\n", frame, addr, __get_symbol_name(st, symbol),
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_LOG_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_LOG_INTERNAL_H_
|
||||
#include "libc/calls/struct/siginfo.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
extern _Hide bool __nocolor;
|
||||
extern _Hide int kCrashSigs[8];
|
||||
extern _Hide bool _wantcrashreports;
|
||||
extern _Hide bool g_isrunningundermake;
|
||||
|
||||
void __start_fatal(const char *, int) _Hide;
|
||||
void __oncrash(int, struct siginfo *, struct ucontext *) relegated;
|
||||
void __restore_tty(void);
|
||||
void RestoreDefaultCrashSignalHandlers(void);
|
||||
void __oncrash_amd64(int, struct siginfo *, void *) relegated;
|
||||
void __oncrash_arm64(int, struct siginfo *, void *) relegated;
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -77,7 +77,8 @@ o/$(MODE)/libc/log/checkaligned.o \
|
|||
o/$(MODE)/libc/log/checkfail.o \
|
||||
o/$(MODE)/libc/log/checkfail_ndebug.o \
|
||||
o/$(MODE)/libc/log/restoretty.o \
|
||||
o/$(MODE)/libc/log/oncrash.o \
|
||||
o/$(MODE)/libc/log/oncrash_amd64.o \
|
||||
o/$(MODE)/libc/log/oncrash_arm64.o \
|
||||
o/$(MODE)/libc/log/onkill.o \
|
||||
o/$(MODE)/libc/log/startfatal.o \
|
||||
o/$(MODE)/libc/log/startfatal_ndebug.o \
|
||||
|
|
|
@ -46,16 +46,10 @@
|
|||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "third_party/libcxx/math.h"
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
||||
/**
|
||||
* @fileoverview Abnormal termination handling & GUI debugging.
|
||||
* @see libc/onkill.c
|
||||
*/
|
||||
|
||||
STATIC_YOINK("strerror_wr"); /* for kprintf %m */
|
||||
STATIC_YOINK("strsignal"); /* for kprintf %G */
|
||||
STATIC_YOINK("strerror_wr"); // for kprintf %m
|
||||
STATIC_YOINK("strsignal_r"); // for kprintf %G
|
||||
|
||||
static const char kGregOrder[17] forcealign(1) = {
|
||||
13, 11, 8, 14, 12, 9, 10, 15, 16, 0, 1, 2, 3, 4, 5, 6, 7,
|
||||
|
@ -69,8 +63,6 @@ static const char kGregNames[17][4] forcealign(1) = {
|
|||
static const char kCpuFlags[12] forcealign(1) = "CVPRAKZSTIDO";
|
||||
static const char kFpuExceptions[6] forcealign(1) = "IDZOUP";
|
||||
|
||||
int kCrashSigs[8];
|
||||
|
||||
relegated static void ShowFunctionCalls(ucontext_t *ctx) {
|
||||
struct StackFrame *bp;
|
||||
struct StackFrame goodframe;
|
||||
|
@ -298,11 +290,12 @@ static wontreturn relegated noinstrument void __minicrash(int sig,
|
|||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
|
||||
relegated void __oncrash_amd64(int sig, struct siginfo *si, void *arg) {
|
||||
int bZero;
|
||||
intptr_t rip;
|
||||
int me, owner;
|
||||
int gdbpid, err;
|
||||
ucontext_t *ctx = arg;
|
||||
static atomic_int once;
|
||||
static atomic_int once2;
|
||||
STRACE("__oncrash rip %x", ctx->uc_mcontext.rip);
|
||||
|
@ -321,7 +314,7 @@ relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
|
|||
} else if (__nocolor || g_isrunningundermake) {
|
||||
gdbpid = -1;
|
||||
} else if (!IsTiny() && IsLinux() && FindDebugBinary() && !__isworker) {
|
||||
RestoreDefaultCrashSignalHandlers();
|
||||
// RestoreDefaultCrashSignalHandlers();
|
||||
gdbpid = AttachDebugger(
|
||||
((sig == SIGTRAP || sig == SIGQUIT) &&
|
||||
(rip >= (intptr_t)&_base && rip < (intptr_t)&_etext))
|
277
libc/log/oncrash_arm64.c
Normal file
277
libc/log/oncrash_arm64.c
Normal file
|
@ -0,0 +1,277 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/rusage.internal.h"
|
||||
#include "libc/calls/struct/siginfo.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/struct/utsname.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nexgen32e/stackframe.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#ifdef __aarch64__
|
||||
|
||||
STATIC_YOINK("strerror_wr"); // for kprintf %m
|
||||
STATIC_YOINK("strsignal_r"); // for kprintf %G
|
||||
|
||||
#define RESET "\e[0m"
|
||||
#define STRONG "\e[30;101m"
|
||||
#define RED "\e[31;1m"
|
||||
#define GREEN "\e[32;1m"
|
||||
#define BLUE "\e[34;1m"
|
||||
#define YELLOW "\e[33;1m"
|
||||
#define MAGENTA "\e[35;1m"
|
||||
|
||||
struct Buffer {
|
||||
char *p;
|
||||
int n;
|
||||
int i;
|
||||
};
|
||||
|
||||
static void Append(struct Buffer *b, const char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
b->i += kvsnprintf(b->p + b->i, b->n - b->i, fmt, va);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
static const char *ColorRegister(int r) {
|
||||
if (__nocolor) return "";
|
||||
switch (r) {
|
||||
case 0: // arg / res
|
||||
case 1: // arg / res
|
||||
case 2: // arg / res
|
||||
case 3: // arg / res
|
||||
case 4: // arg / res
|
||||
case 5: // arg / res
|
||||
case 6: // arg / res
|
||||
case 7: // arg / res
|
||||
return GREEN;
|
||||
case 9: // volatile
|
||||
case 10: // volatile
|
||||
case 11: // volatile
|
||||
case 12: // volatile
|
||||
case 13: // volatile
|
||||
case 14: // volatile
|
||||
case 15: // volatile
|
||||
return BLUE;
|
||||
case 19: // saved
|
||||
case 20: // saved
|
||||
case 21: // saved
|
||||
case 22: // saved
|
||||
case 23: // saved
|
||||
case 24: // saved
|
||||
case 25: // saved
|
||||
case 26: // saved
|
||||
case 27: // saved
|
||||
case 28: // saved
|
||||
return MAGENTA;
|
||||
case 29: // frame pointer
|
||||
case 30: // return pointer
|
||||
case 31: // stack pointer
|
||||
return RED;
|
||||
default: // miscellaneous registers
|
||||
return YELLOW;
|
||||
}
|
||||
}
|
||||
|
||||
static bool AppendFileLine(struct Buffer *b, const char *addr2line,
|
||||
const char *debugbin, long addr) {
|
||||
ssize_t rc;
|
||||
char buf[128];
|
||||
int j, k, ws, pid, pfd[2];
|
||||
if (!debugbin || !*debugbin) return false;
|
||||
if (!addr2line || !*addr2line) return false;
|
||||
if (sys_pipe(pfd)) return false;
|
||||
ksnprintf(buf, sizeof(buf), "%lx", addr);
|
||||
if ((pid = vfork()) == -1) {
|
||||
sys_close(pfd[1]);
|
||||
sys_close(pfd[0]);
|
||||
return false;
|
||||
}
|
||||
if (!pid) {
|
||||
sys_close(pfd[0]);
|
||||
sys_dup2(pfd[1], 1, 0);
|
||||
sys_close(2);
|
||||
__sys_execve(addr2line,
|
||||
(char *const[]){addr2line, "-pifCe", debugbin, buf, 0},
|
||||
(char *const[]){0});
|
||||
_Exit(127);
|
||||
}
|
||||
sys_close(pfd[1]);
|
||||
// copy addr2line stdout to buffer. normally it is "file:line\n".
|
||||
// however additional lines can get created for inline functions.
|
||||
j = b->i;
|
||||
Append(b, "in ");
|
||||
k = b->i;
|
||||
while ((rc = sys_read(pfd[0], buf, sizeof(buf))) > 0) {
|
||||
Append(b, "%.*s", (int)rc, buf);
|
||||
}
|
||||
sys_close(pfd[0]);
|
||||
if (sys_wait4(pid, &ws, 0, 0) != -1 && !ws && b->p[k] != ':' &&
|
||||
b->p[k] != '?' && b->p[b->i - 1] == '\n') {
|
||||
--b->i; // chomp last newline
|
||||
return true;
|
||||
} else {
|
||||
b->i = j; // otherwise reset the buffer
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
relegated void __oncrash_arm64(int sig, struct siginfo *si, void *arg) {
|
||||
char buf[10000];
|
||||
static _Thread_local bool once;
|
||||
struct Buffer b[1] = {{buf, sizeof(buf)}};
|
||||
b->p[b->i++] = '\n';
|
||||
if (!once) {
|
||||
int i, j;
|
||||
const char *kind;
|
||||
const char *reset;
|
||||
const char *strong;
|
||||
ucontext_t *ctx = arg;
|
||||
char host[64] = "unknown";
|
||||
struct utsname names = {0};
|
||||
once = true;
|
||||
ftrace_enabled(-1);
|
||||
strace_enabled(-1);
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
|
||||
__restore_tty();
|
||||
uname(&names);
|
||||
gethostname(host, sizeof(host));
|
||||
reset = !__nocolor ? RESET : "";
|
||||
strong = !__nocolor ? STRONG : "";
|
||||
if (ctx && (ctx->uc_mcontext.sp & (GetStackSize() - 1)) <= GUARDSIZE) {
|
||||
kind = "Stack Overflow";
|
||||
} else {
|
||||
kind = GetSiCodeName(sig, si->si_code);
|
||||
}
|
||||
Append(b,
|
||||
"%serror%s: Uncaught %G (%s) on %s pid %d tid %d\n"
|
||||
" %s\n"
|
||||
" %m\n"
|
||||
" %s %s %s %s\n",
|
||||
strong, reset, sig, kind, host, getpid(), gettid(),
|
||||
program_invocation_name, names.sysname, names.version,
|
||||
names.nodename, names.release, "yo");
|
||||
if (ctx) {
|
||||
long pc;
|
||||
char line[256];
|
||||
int addend, symbol;
|
||||
const char *debugbin;
|
||||
const char *addr2line;
|
||||
struct StackFrame *fp;
|
||||
struct SymbolTable *st;
|
||||
st = GetSymbolTable();
|
||||
debugbin = FindDebugBinary();
|
||||
addr2line = GetAddr2linePath();
|
||||
|
||||
if (ctx->uc_mcontext.fault_address) {
|
||||
Append(b, " fault_address = %#lx\n", ctx->uc_mcontext.fault_address);
|
||||
}
|
||||
|
||||
// PRINT REGISTERS
|
||||
for (i = 0; i < 8; ++i) {
|
||||
Append(b, " ");
|
||||
for (j = 0; j < 4; ++j) {
|
||||
int r = 8 * j + i;
|
||||
if (j) Append(b, " ");
|
||||
Append(b, "%s%016lx%s %sr%d", ColorRegister(r),
|
||||
ctx->uc_mcontext.regs[r], reset, r == 8 || r == 9 ? " " : "",
|
||||
r);
|
||||
}
|
||||
Append(b, "\n");
|
||||
}
|
||||
|
||||
// PRINT CURRENT LOCATION
|
||||
pc = ctx->uc_mcontext.pc;
|
||||
Append(b, " %016lx sp %lx pc", ctx->uc_mcontext.sp, pc);
|
||||
if (pc && (symbol = __get_symbol(st, pc))) {
|
||||
addend = pc - st->addr_base;
|
||||
addend -= st->symbols[symbol].x;
|
||||
Append(b, " ");
|
||||
if (!AppendFileLine(b, addr2line, debugbin, pc)) {
|
||||
Append(b, "%s", __get_symbol_name(st, symbol));
|
||||
if (addend) Append(b, "%+d", addend);
|
||||
}
|
||||
}
|
||||
Append(b, "\n");
|
||||
|
||||
// PRINT LINKED LOCATION
|
||||
pc = ctx->uc_mcontext.regs[30];
|
||||
Append(b, " %016lx sp %lx lr", ctx->uc_mcontext.sp, pc);
|
||||
if (pc && (symbol = __get_symbol(st, pc))) {
|
||||
addend = pc - st->addr_base;
|
||||
addend -= st->symbols[symbol].x;
|
||||
Append(b, " ");
|
||||
if (!AppendFileLine(b, addr2line, debugbin, pc)) {
|
||||
Append(b, "%s", __get_symbol_name(st, symbol));
|
||||
if (addend) Append(b, "%+d", addend);
|
||||
}
|
||||
}
|
||||
Append(b, "\n");
|
||||
|
||||
// PRINT FRAME POINTERS
|
||||
fp = (struct StackFrame *)ctx->uc_mcontext.regs[29];
|
||||
for (i = 0; fp; fp = fp->next) {
|
||||
if (kisdangerous(fp)) {
|
||||
Append(b, " %016lx <dangerous fp>\n", fp);
|
||||
break;
|
||||
}
|
||||
if (++i == 100) {
|
||||
Append(b, " <truncated backtrace>\n");
|
||||
break;
|
||||
}
|
||||
if ((pc = fp->addr)) {
|
||||
if ((symbol = __get_symbol(st, pc))) {
|
||||
addend = pc - st->addr_base;
|
||||
addend -= st->symbols[symbol].x;
|
||||
} else {
|
||||
addend = 0;
|
||||
}
|
||||
} else {
|
||||
symbol = 0;
|
||||
addend = 0;
|
||||
}
|
||||
Append(b, " %016lx fp %lx lr ", fp, pc);
|
||||
if (!AppendFileLine(b, addr2line, debugbin, pc)) {
|
||||
Append(b, "%s", __get_symbol_name(st, symbol));
|
||||
if (addend) Append(b, "%+d", addend);
|
||||
}
|
||||
Append(b, "\n");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Append(b, "got %G while crashing!\n");
|
||||
}
|
||||
sys_write(2, b->p, MIN(b->i, b->n));
|
||||
_Exit(128 + sig);
|
||||
}
|
||||
|
||||
#endif /* __aarch64__ */
|
|
@ -1,103 +0,0 @@
|
|||
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
||||
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/macros.internal.h"
|
||||
|
||||
// These function names make it a bit more obvious which signal
|
||||
// caused the crash, particularly in the GDB GUI. They're coded
|
||||
// into an array to pinch pennies on code size registering them.
|
||||
|
||||
__oncrash_thunks:
|
||||
|
||||
// <SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c, internal.h
|
||||
|
||||
.org 11*0
|
||||
__oncrash_sigquit:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigquit,globl
|
||||
|
||||
.org 11*1
|
||||
__oncrash_sigfpe:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigfpe,globl
|
||||
|
||||
.org 11*2
|
||||
__oncrash_sigill:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigill,globl
|
||||
|
||||
.org 11*3
|
||||
__oncrash_sigsegv:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigsegv,globl
|
||||
|
||||
.org 11*4
|
||||
__oncrash_sigtrap:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigtrap,globl
|
||||
|
||||
.org 11*5
|
||||
__oncrash_sigabrt:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigabrt,globl
|
||||
|
||||
.org 11*6
|
||||
__oncrash_sigbus:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigbus,globl
|
||||
|
||||
.org 11*7
|
||||
__oncrash_sigurg:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
call __oncrash
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn __oncrash_sigurg,globl
|
||||
|
||||
// </SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c, internal.h
|
||||
|
||||
.endobj __oncrash_thunks,globl
|
|
@ -25,6 +25,8 @@
|
|||
#include "libc/log/internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -34,8 +36,6 @@
|
|||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/ss.h"
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
||||
STATIC_YOINK("zipos"); // for symtab
|
||||
STATIC_YOINK("__die"); // for backtracing
|
||||
STATIC_YOINK("ShowBacktrace"); // for backtracing
|
||||
|
@ -44,44 +44,80 @@ STATIC_YOINK("PrintBacktraceUsingSymbols"); // for backtracing
|
|||
STATIC_YOINK("malloc_inspect_all"); // for asan memory origin
|
||||
STATIC_YOINK("GetSymbolByAddr"); // for asan memory origin
|
||||
|
||||
static struct sigaction g_oldcrashacts[8];
|
||||
extern const unsigned char __oncrash_thunks[8][11];
|
||||
struct CrashHandler {
|
||||
int sig;
|
||||
struct sigaction old;
|
||||
};
|
||||
|
||||
static void InstallCrashHandlers(int extraflags) {
|
||||
int e;
|
||||
size_t i;
|
||||
struct sigaction sa;
|
||||
bzero(&sa, sizeof(sa));
|
||||
sigfillset(&sa.sa_mask);
|
||||
sa.sa_flags = SA_SIGINFO | SA_NODEFER | extraflags;
|
||||
for (i = 0; i < ARRAYLEN(kCrashSigs); ++i) {
|
||||
sigdelset(&sa.sa_mask, kCrashSigs[i]);
|
||||
}
|
||||
for (i = 0; i < ARRAYLEN(kCrashSigs); ++i) {
|
||||
if (kCrashSigs[i]) {
|
||||
sa.sa_sigaction = (sigaction_f)__oncrash_thunks[i];
|
||||
e = errno;
|
||||
sigaction(kCrashSigs[i], &sa, &g_oldcrashacts[i]);
|
||||
errno = e;
|
||||
}
|
||||
}
|
||||
static inline void __oncrash(int sig, struct siginfo *si, void *arg) {
|
||||
#ifdef __x86_64__
|
||||
__oncrash_amd64(sig, si, arg);
|
||||
#elif defined(__aarch64__)
|
||||
__oncrash_arm64(sig, si, arg);
|
||||
#else
|
||||
abort();
|
||||
#endif
|
||||
}
|
||||
|
||||
relegated void RestoreDefaultCrashSignalHandlers(void) {
|
||||
static void __got_sigquit(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
static void __got_sigfpe(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
static void __got_sigill(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
static void __got_sigsegv(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
static void __got_sigtrap(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
static void __got_sigabrt(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
static void __got_sigbus(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
static void __got_sigurg(int sig, struct siginfo *si, void *arg) {
|
||||
__oncrash(sig, si, arg);
|
||||
}
|
||||
|
||||
static void RemoveCrashHandler(void *arg) {
|
||||
int e;
|
||||
size_t i;
|
||||
sigset_t ss;
|
||||
struct CrashHandler *ch = arg;
|
||||
strace_enabled(-1);
|
||||
sigemptyset(&ss);
|
||||
sigprocmask(SIG_SETMASK, &ss, NULL);
|
||||
for (i = 0; i < ARRAYLEN(kCrashSigs); ++i) {
|
||||
if (kCrashSigs[i]) {
|
||||
e = errno;
|
||||
sigaction(kCrashSigs[i], &g_oldcrashacts[i], NULL);
|
||||
errno = e;
|
||||
e = errno;
|
||||
sigaction(ch->sig, &ch->old, NULL);
|
||||
errno = e;
|
||||
free(ch);
|
||||
strace_enabled(+1);
|
||||
}
|
||||
|
||||
static void InstallCrashHandler(int sig, sigaction_f thunk, int extraflags) {
|
||||
int e;
|
||||
struct sigaction sa;
|
||||
struct CrashHandler *ch;
|
||||
e = errno;
|
||||
if ((ch = malloc(sizeof(*ch)))) {
|
||||
ch->sig = sig;
|
||||
sa.sa_sigaction = thunk;
|
||||
sigfillset(&sa.sa_mask);
|
||||
sigdelset(&sa.sa_mask, SIGQUIT);
|
||||
sigdelset(&sa.sa_mask, SIGFPE);
|
||||
sigdelset(&sa.sa_mask, SIGILL);
|
||||
sigdelset(&sa.sa_mask, SIGSEGV);
|
||||
sigdelset(&sa.sa_mask, SIGTRAP);
|
||||
sigdelset(&sa.sa_mask, SIGABRT);
|
||||
sigdelset(&sa.sa_mask, SIGBUS);
|
||||
sigdelset(&sa.sa_mask, SIGURG);
|
||||
sa.sa_flags = SA_SIGINFO | SA_NODEFER | extraflags;
|
||||
if (!sigaction(sig, &sa, &ch->old)) {
|
||||
__cxa_atexit(RemoveCrashHandler, ch, 0);
|
||||
}
|
||||
}
|
||||
strace_enabled(+1);
|
||||
errno = e;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,33 +136,25 @@ relegated void RestoreDefaultCrashSignalHandlers(void) {
|
|||
* useful, for example, if a program is caught in an infinite loop.
|
||||
*/
|
||||
void ShowCrashReports(void) {
|
||||
int ef = 0;
|
||||
struct sigaltstack ss;
|
||||
_wantcrashreports = true;
|
||||
/* <SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c */
|
||||
kCrashSigs[0] = SIGQUIT; /* ctrl+\ aka ctrl+break */
|
||||
kCrashSigs[1] = SIGFPE; /* 1 / 0 */
|
||||
kCrashSigs[2] = SIGILL; /* illegal instruction */
|
||||
kCrashSigs[3] = SIGSEGV; /* bad memory access */
|
||||
kCrashSigs[4] = SIGTRAP; /* bad system call */
|
||||
kCrashSigs[5] = SIGABRT; /* abort() called */
|
||||
kCrashSigs[6] = SIGBUS; /* misaligned, noncanonical ptr, etc. */
|
||||
kCrashSigs[7] = SIGURG; /* placeholder */
|
||||
/* </SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c */
|
||||
if (!IsWindows()) {
|
||||
ef = SA_ONSTACK;
|
||||
ss.ss_flags = 0;
|
||||
ss.ss_size = GetStackSize();
|
||||
// FreeBSD sigaltstack() will EFAULT if we use MAP_STACK here
|
||||
// OpenBSD sigaltstack() auto-applies MAP_STACK to the memory
|
||||
_npassert((ss.ss_sp = _mapanon(GetStackSize())));
|
||||
_npassert(!sigaltstack(&ss, 0));
|
||||
InstallCrashHandlers(SA_ONSTACK);
|
||||
} else {
|
||||
InstallCrashHandlers(0);
|
||||
}
|
||||
InstallCrashHandler(SIGQUIT, __got_sigquit, ef); // ctrl+\ aka ctrl+break
|
||||
InstallCrashHandler(SIGFPE, __got_sigfpe, ef); // 1 / 0
|
||||
InstallCrashHandler(SIGILL, __got_sigill, ef); // illegal instruction
|
||||
InstallCrashHandler(SIGSEGV, __got_sigsegv, ef); // bad memory access
|
||||
InstallCrashHandler(SIGTRAP, __got_sigtrap, ef); // bad system call
|
||||
InstallCrashHandler(SIGABRT, __got_sigabrt, ef); // abort() called
|
||||
InstallCrashHandler(SIGBUS, __got_sigbus, ef); // misalign, mmap i/o failed
|
||||
InstallCrashHandler(SIGURG, __got_sigurg, ef); // placeholder
|
||||
GetSymbolTable();
|
||||
}
|
||||
|
||||
#else
|
||||
void ShowCrashReports(void) {
|
||||
}
|
||||
#endif /* __x86_64__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue