2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/sigbits.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/calls/struct/siginfo.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/struct/utsname.h"
|
|
|
|
#include "libc/calls/ucontext.h"
|
|
|
|
#include "libc/dce.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/errno.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/fmt/fmt.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/log/backtrace.internal.h"
|
|
|
|
#include "libc/log/color.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/log/gdb.h"
|
|
|
|
#include "libc/log/internal.h"
|
|
|
|
#include "libc/log/log.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-09-03 12:44:37 +00:00
|
|
|
#include "libc/nexgen32e/stackframe.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/runtime/memtrack.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/runtime/pc.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/auxv.h"
|
2020-10-11 04:18:53 +00:00
|
|
|
#include "libc/sysv/consts/fileno.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview Abnormal termination handling & GUI debugging.
|
|
|
|
* @see libc/onkill.c
|
|
|
|
*/
|
|
|
|
|
2020-12-05 20:20:41 +00:00
|
|
|
static const char kGregOrder[17] forcealign(1) = {
|
2020-08-25 11:23:25 +00:00
|
|
|
13, 11, 8, 14, 12, 9, 10, 15, 16, 0, 1, 2, 3, 4, 5, 6, 7,
|
|
|
|
};
|
|
|
|
|
2020-12-05 20:20:41 +00:00
|
|
|
static const char kGregNames[17][4] forcealign(1) = {
|
2020-06-15 14:18:57 +00:00
|
|
|
"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", "RDI",
|
2020-08-25 11:23:25 +00:00
|
|
|
"RSI", "RBP", "RBX", "RDX", "RAX", "RCX", "RSP", "RIP",
|
|
|
|
};
|
|
|
|
|
2021-02-26 02:30:17 +00:00
|
|
|
static const char kCpuFlags[12] forcealign(1) = "CVPRAKZSTIDO";
|
|
|
|
static const char kFpuExceptions[6] forcealign(1) = "IDZOUP";
|
2020-12-05 20:20:41 +00:00
|
|
|
static const char kCrashSigNames[8][5] forcealign(1) = {
|
2020-10-27 10:39:46 +00:00
|
|
|
"QUIT", "FPE", "ILL", "SEGV", "TRAP", "ABRT", "BUS"};
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2020-12-27 15:02:35 +00:00
|
|
|
hidden int kCrashSigs[8];
|
|
|
|
hidden struct sigaction g_oldcrashacts[8];
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
relegated static const char *TinyStrSignal(int sig) {
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < ARRAYLEN(kCrashSigs); ++i) {
|
|
|
|
if (kCrashSigs[i] && sig == kCrashSigs[i]) {
|
|
|
|
return kCrashSigNames[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2020-10-11 04:18:53 +00:00
|
|
|
relegated static void ShowFunctionCalls(int fd, ucontext_t *ctx) {
|
2020-06-15 14:18:57 +00:00
|
|
|
struct StackFrame *bp;
|
|
|
|
struct StackFrame goodframe;
|
2020-10-11 04:18:53 +00:00
|
|
|
write(fd, "\r\n", 2);
|
2020-06-15 14:18:57 +00:00
|
|
|
if (ctx && ctx->uc_mcontext.rip && ctx->uc_mcontext.rbp) {
|
|
|
|
goodframe.next = (struct StackFrame *)ctx->uc_mcontext.rbp;
|
|
|
|
goodframe.addr = ctx->uc_mcontext.rip;
|
|
|
|
bp = &goodframe;
|
2020-10-11 04:18:53 +00:00
|
|
|
ShowBacktrace(fd, bp);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 02:30:17 +00:00
|
|
|
relegated static char *AddFlag(char *p, int b, const char *s) {
|
|
|
|
if (b) p = stpcpy(p, s);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
relegated static void DescribeCpuFlags(int fd, int flags, int x87sw,
|
|
|
|
int mxcsr) {
|
2020-10-11 04:18:53 +00:00
|
|
|
unsigned i;
|
|
|
|
char buf[64], *p;
|
|
|
|
p = buf;
|
2021-02-26 02:30:17 +00:00
|
|
|
for (i = 0; i < ARRAYLEN(kCpuFlags); ++i) {
|
2020-10-11 04:18:53 +00:00
|
|
|
if (flags & 1) {
|
|
|
|
*p++ = ' ';
|
2021-02-26 02:30:17 +00:00
|
|
|
*p++ = kCpuFlags[i];
|
2020-10-11 04:18:53 +00:00
|
|
|
*p++ = 'F';
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2020-10-11 04:18:53 +00:00
|
|
|
flags >>= 1;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-02-26 02:30:17 +00:00
|
|
|
for (i = 0; i < ARRAYLEN(kFpuExceptions); ++i) {
|
|
|
|
if ((x87sw | mxcsr) & (1 << i)) {
|
|
|
|
*p++ = ' ';
|
|
|
|
*p++ = kFpuExceptions[i];
|
|
|
|
*p++ = 'E';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = AddFlag(p, x87sw & FPU_SF, " SF");
|
|
|
|
p = AddFlag(p, x87sw & FPU_C0, " C0");
|
|
|
|
p = AddFlag(p, x87sw & FPU_C1, " C1");
|
|
|
|
p = AddFlag(p, x87sw & FPU_C2, " C2");
|
|
|
|
p = AddFlag(p, x87sw & FPU_C3, " C3");
|
2020-10-11 04:18:53 +00:00
|
|
|
write(fd, buf, p - buf);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 04:18:53 +00:00
|
|
|
relegated static void ShowGeneralRegisters(int fd, ucontext_t *ctx) {
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t i, j, k;
|
|
|
|
long double st;
|
2020-10-11 04:18:53 +00:00
|
|
|
write(fd, "\r\n", 2);
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0, j = 0, k = 0; i < ARRAYLEN(kGregNames); ++i) {
|
2020-10-11 04:18:53 +00:00
|
|
|
if (j > 0) write(fd, " ", 1);
|
2021-02-26 02:30:17 +00:00
|
|
|
dprintf(fd, "%-3s %016lx", kGregNames[(unsigned)kGregOrder[i]],
|
|
|
|
ctx->uc_mcontext.gregs[(unsigned)kGregOrder[i]]);
|
2020-06-15 14:18:57 +00:00
|
|
|
if (++j == 3) {
|
|
|
|
j = 0;
|
2021-01-25 21:08:05 +00:00
|
|
|
if (ctx->uc_mcontext.fpregs) {
|
|
|
|
memcpy(&st, (char *)&ctx->uc_mcontext.fpregs->st[k], sizeof(st));
|
|
|
|
} else {
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
}
|
2021-02-26 02:30:17 +00:00
|
|
|
dprintf(fd, " %s(%zu) %Lf", "ST", k, st);
|
2020-06-15 14:18:57 +00:00
|
|
|
++k;
|
2020-10-11 04:18:53 +00:00
|
|
|
write(fd, "\r\n", 2);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 02:30:17 +00:00
|
|
|
DescribeCpuFlags(
|
|
|
|
fd, ctx->uc_mcontext.gregs[REG_EFL],
|
|
|
|
ctx->uc_mcontext.fpregs ? ctx->uc_mcontext.fpregs->swd : 0,
|
|
|
|
ctx->uc_mcontext.fpregs ? ctx->uc_mcontext.fpregs->mxcsr : 0);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 04:18:53 +00:00
|
|
|
relegated static void ShowSseRegisters(int fd, ucontext_t *ctx) {
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t i;
|
2021-01-25 21:08:05 +00:00
|
|
|
if (ctx->uc_mcontext.fpregs) {
|
|
|
|
write(fd, "\r\n\r\n", 4);
|
|
|
|
for (i = 0; i < 8; ++i) {
|
2021-02-26 02:30:17 +00:00
|
|
|
dprintf(fd, "%s%-2zu %016lx%016lx %s%-2d %016lx%016lx\r\n", "XMM", i + 0,
|
|
|
|
ctx->uc_mcontext.fpregs->xmm[i + 0].u64[1],
|
|
|
|
ctx->uc_mcontext.fpregs->xmm[i + 0].u64[0], "XMM", i + 8,
|
|
|
|
ctx->uc_mcontext.fpregs->xmm[i + 8].u64[1],
|
|
|
|
ctx->uc_mcontext.fpregs->xmm[i + 8].u64[0]);
|
2021-01-25 21:08:05 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
relegated static void ShowMemoryMappings(int outfd) {
|
|
|
|
ssize_t rc;
|
|
|
|
int c, infd;
|
|
|
|
char buf[64];
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!IsTiny()) {
|
2020-08-25 11:23:25 +00:00
|
|
|
PrintMemoryIntervals(outfd, &_mmi);
|
|
|
|
if ((infd = open("/proc/self/maps", O_RDONLY)) != -1) {
|
|
|
|
while ((rc = read(infd, buf, sizeof(buf))) > 0) {
|
|
|
|
write(outfd, buf, rc);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 11:23:25 +00:00
|
|
|
close(infd);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 04:18:53 +00:00
|
|
|
relegated static void ShowCrashReport(int err, int fd, int sig,
|
2020-06-15 14:18:57 +00:00
|
|
|
ucontext_t *ctx) {
|
2020-11-09 23:41:11 +00:00
|
|
|
int i;
|
2021-01-25 21:08:05 +00:00
|
|
|
char hostname[64];
|
2020-06-15 14:18:57 +00:00
|
|
|
struct utsname names;
|
2021-01-25 21:08:05 +00:00
|
|
|
strcpy(hostname, "unknown");
|
|
|
|
gethostname(hostname, sizeof(hostname));
|
2021-02-26 02:30:17 +00:00
|
|
|
dprintf(fd, "\r\n%serror%s: Uncaught SIG%s on %s\r\n %s\r\n %s\r\n", RED2,
|
|
|
|
RESET, TinyStrSignal(sig), hostname, getauxval(AT_EXECFN),
|
|
|
|
strerror(err));
|
2020-06-15 14:18:57 +00:00
|
|
|
if (uname(&names) != -1) {
|
2021-02-26 02:30:17 +00:00
|
|
|
dprintf(fd, " %s %s %s %s\r\n", names.sysname, names.nodename,
|
|
|
|
names.release, names.version);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2020-10-11 04:18:53 +00:00
|
|
|
ShowFunctionCalls(fd, ctx);
|
2020-06-15 14:18:57 +00:00
|
|
|
if (ctx) {
|
2020-10-11 04:18:53 +00:00
|
|
|
ShowGeneralRegisters(fd, ctx);
|
|
|
|
ShowSseRegisters(fd, ctx);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2020-10-11 04:18:53 +00:00
|
|
|
write(fd, "\r\n", 2);
|
|
|
|
ShowMemoryMappings(fd);
|
2020-11-09 23:41:11 +00:00
|
|
|
write(fd, "\r\n", 2);
|
2021-02-24 12:00:38 +00:00
|
|
|
for (i = 0; i < __argc; ++i) {
|
|
|
|
write(fd, __argv[i], strlen(__argv[i]));
|
2021-02-01 11:33:13 +00:00
|
|
|
write(fd, " ", 1);
|
2020-11-09 23:41:11 +00:00
|
|
|
}
|
2021-02-01 11:33:13 +00:00
|
|
|
write(fd, "\r\n", 2);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
relegated static void RestoreDefaultCrashSignalHandlers(void) {
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t i;
|
|
|
|
sigset_t ss;
|
|
|
|
sigemptyset(&ss);
|
|
|
|
sigprocmask(SIG_SETMASK, &ss, NULL);
|
|
|
|
for (i = 0; i < ARRAYLEN(kCrashSigs); ++i) {
|
|
|
|
if (kCrashSigs[i]) sigaction(kCrashSigs[i], &g_oldcrashacts[i], NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crashes in a developer-friendly human-centric way.
|
|
|
|
*
|
|
|
|
* We first try to launch GDB if it's an interactive development
|
|
|
|
* session. Otherwise we show a really good crash report, sort of like
|
|
|
|
* Python, that includes filenames and line numbers. Many editors, e.g.
|
|
|
|
* Emacs, will even recognize its syntax for quick hopping to the
|
|
|
|
* failing line. That's only possible if the the .com.dbg file is in the
|
|
|
|
* same folder. If the concomitant debug binary can't be found, we
|
|
|
|
* simply print addresses which may be cross-referenced using objdump.
|
|
|
|
*
|
|
|
|
* This function never returns, except for traps w/ human supervision.
|
|
|
|
*/
|
2020-10-27 10:39:46 +00:00
|
|
|
relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
|
2020-06-15 14:18:57 +00:00
|
|
|
intptr_t rip;
|
|
|
|
int gdbpid, err;
|
|
|
|
static bool once;
|
|
|
|
err = errno;
|
2021-02-28 06:35:50 +00:00
|
|
|
if (once) _exit(119);
|
2020-06-15 14:18:57 +00:00
|
|
|
once = true;
|
|
|
|
rip = ctx ? ctx->uc_mcontext.rip : 0;
|
|
|
|
if ((gdbpid = IsDebuggerPresent(true))) {
|
|
|
|
DebugBreak();
|
2021-02-01 11:33:13 +00:00
|
|
|
} else if (IsTerminalInarticulate() || IsRunningUnderMake()) {
|
2020-06-15 14:18:57 +00:00
|
|
|
gdbpid = -1;
|
2021-02-01 11:33:13 +00:00
|
|
|
} else if (FindDebugBinary()) {
|
2020-08-25 11:23:25 +00:00
|
|
|
RestoreDefaultCrashSignalHandlers();
|
2020-06-15 14:18:57 +00:00
|
|
|
gdbpid =
|
|
|
|
attachdebugger(((sig == SIGTRAP || sig == SIGQUIT) &&
|
|
|
|
(rip >= (intptr_t)&_base && rip < (intptr_t)&_etext))
|
|
|
|
? rip
|
|
|
|
: 0);
|
|
|
|
}
|
|
|
|
if (gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT)) return;
|
2020-10-11 04:18:53 +00:00
|
|
|
ShowCrashReport(err, STDERR_FILENO, sig, ctx);
|
|
|
|
exit(128 + sig);
|
2020-06-15 14:18:57 +00:00
|
|
|
unreachable;
|
|
|
|
}
|