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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/assert.h"
|
|
|
|
#include "libc/atomic.h"
|
2022-04-18 07:01:26 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2021-08-13 18:18:25 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/calls/struct/utsname.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2023-07-10 11:29:46 +00:00
|
|
|
#include "libc/calls/ucontext.h"
|
2022-10-13 20:44:41 +00:00
|
|
|
#include "libc/dce.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/errno.h"
|
2022-07-11 12:55:17 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2023-09-10 15:12:43 +00:00
|
|
|
#include "libc/intrin/describebacktrace.internal.h"
|
2023-09-02 03:49:13 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-09-04 04:10:37 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/log/backtrace.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"
|
2023-07-08 15:44:46 +00:00
|
|
|
#include "libc/math.h"
|
2020-09-03 12:44:37 +00:00
|
|
|
#include "libc/nexgen32e/stackframe.h"
|
2022-03-23 13:31:55 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/runtime/pc.internal.h"
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-11-02 13:49:42 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
2022-07-11 12:55:17 +00:00
|
|
|
#include "libc/str/str.h"
|
2023-07-26 20:54:49 +00:00
|
|
|
#include "libc/sysv/consts/auxv.h"
|
2023-07-10 11:29:46 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2022-11-05 01:19:05 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2023-05-09 19:14:57 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2023-07-26 20:54:49 +00:00
|
|
|
__static_yoink("strerror_wr"); // for kprintf %m
|
|
|
|
__static_yoink("strsignal_r"); // for kprintf %G
|
2022-03-16 20:33:13 +00:00
|
|
|
|
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-06-15 14:18:57 +00:00
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
relegated static void ShowFunctionCalls(ucontext_t *ctx) {
|
2020-06-15 14:18:57 +00:00
|
|
|
struct StackFrame *bp;
|
|
|
|
struct StackFrame goodframe;
|
2021-10-15 02:36:49 +00:00
|
|
|
if (!ctx->uc_mcontext.rip) {
|
2022-04-24 16:59:22 +00:00
|
|
|
kprintf("%s is NULL can't show backtrace\n", "RIP");
|
2021-10-15 02:36:49 +00:00
|
|
|
} else {
|
2020-06-15 14:18:57 +00:00
|
|
|
goodframe.next = (struct StackFrame *)ctx->uc_mcontext.rbp;
|
|
|
|
goodframe.addr = ctx->uc_mcontext.rip;
|
|
|
|
bp = &goodframe;
|
2021-10-14 00:27:13 +00:00
|
|
|
ShowBacktrace(2, 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) {
|
2021-10-14 00:27:13 +00:00
|
|
|
if (b) {
|
2022-07-11 12:55:17 +00:00
|
|
|
p = stpcpy(p, s);
|
2021-10-14 00:27:13 +00:00
|
|
|
} else {
|
|
|
|
*p = 0;
|
|
|
|
}
|
2021-02-26 02:30:17 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
relegated static char *DescribeCpuFlags(char *p, int flags, int x87sw,
|
|
|
|
int mxcsr) {
|
2020-10-11 04:18:53 +00:00
|
|
|
unsigned i;
|
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");
|
2021-09-28 05:58:51 +00:00
|
|
|
return p;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 12:55:17 +00:00
|
|
|
static char *HexCpy(char p[hasatleast 17], uint64_t x, uint8_t k) {
|
|
|
|
while (k > 0) *p++ = "0123456789abcdef"[(x >> (k -= 4)) & 15];
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-11-02 05:36:03 +00:00
|
|
|
relegated static char *ShowGeneralRegisters(char *p, ucontext_t *ctx) {
|
2021-09-28 05:58:51 +00:00
|
|
|
int64_t x;
|
|
|
|
const char *s;
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t i, j, k;
|
|
|
|
long double st;
|
2022-11-02 05:36:03 +00:00
|
|
|
*p++ = '\n';
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0, j = 0, k = 0; i < ARRAYLEN(kGregNames); ++i) {
|
2021-09-28 05:58:51 +00:00
|
|
|
if (j > 0) *p++ = ' ';
|
|
|
|
if (!(s = kGregNames[(unsigned)kGregOrder[i]])[2]) *p++ = ' ';
|
2022-07-11 12:55:17 +00:00
|
|
|
p = stpcpy(p, s), *p++ = ' ';
|
|
|
|
p = HexCpy(p, ctx->uc_mcontext.gregs[(unsigned)kGregOrder[i]], 64);
|
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));
|
2022-11-02 13:49:42 +00:00
|
|
|
p = stpcpy(p, " ST(");
|
|
|
|
p = FormatUint64(p, k++);
|
|
|
|
p = stpcpy(p, ") ");
|
|
|
|
if (signbit(st)) {
|
|
|
|
st = -st;
|
|
|
|
*p++ = '-';
|
|
|
|
}
|
|
|
|
if (isnan(st)) {
|
|
|
|
p = stpcpy(p, "nan");
|
|
|
|
} else if (isinf(st)) {
|
|
|
|
p = stpcpy(p, "inf");
|
|
|
|
} else {
|
|
|
|
if (st > 999.999) st = 999.999;
|
|
|
|
x = st * 1000;
|
|
|
|
p = FormatUint64(p, x / 1000), *p++ = '.';
|
|
|
|
p = FormatUint64(p, x % 1000);
|
|
|
|
}
|
2021-01-25 21:08:05 +00:00
|
|
|
}
|
2022-11-02 05:36:03 +00:00
|
|
|
*p++ = '\n';
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
DescribeCpuFlags(
|
2022-04-20 16:56:53 +00:00
|
|
|
p, ctx->uc_mcontext.eflags,
|
2021-02-26 02:30:17 +00:00
|
|
|
ctx->uc_mcontext.fpregs ? ctx->uc_mcontext.fpregs->swd : 0,
|
|
|
|
ctx->uc_mcontext.fpregs ? ctx->uc_mcontext.fpregs->mxcsr : 0);
|
2022-11-02 05:36:03 +00:00
|
|
|
*p++ = '\n';
|
|
|
|
return p;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 05:36:03 +00:00
|
|
|
relegated static char *ShowSseRegisters(char *p, 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) {
|
2022-11-02 05:36:03 +00:00
|
|
|
*p++ = '\n';
|
2021-01-25 21:08:05 +00:00
|
|
|
for (i = 0; i < 8; ++i) {
|
2022-11-02 05:36:03 +00:00
|
|
|
*p++ = 'X';
|
|
|
|
*p++ = 'M';
|
|
|
|
*p++ = 'M';
|
2021-09-28 05:58:51 +00:00
|
|
|
if (i >= 10) {
|
|
|
|
*p++ = i / 10 + '0';
|
|
|
|
*p++ = i % 10 + '0';
|
|
|
|
} else {
|
|
|
|
*p++ = i + '0';
|
|
|
|
*p++ = ' ';
|
|
|
|
}
|
|
|
|
*p++ = ' ';
|
2022-07-11 12:55:17 +00:00
|
|
|
p = HexCpy(p, ctx->uc_mcontext.fpregs->xmm[i + 0].u64[1], 64);
|
|
|
|
p = HexCpy(p, ctx->uc_mcontext.fpregs->xmm[i + 0].u64[0], 64);
|
|
|
|
p = stpcpy(p, " XMM");
|
2021-09-28 05:58:51 +00:00
|
|
|
if (i + 8 >= 10) {
|
|
|
|
*p++ = (i + 8) / 10 + '0';
|
|
|
|
*p++ = (i + 8) % 10 + '0';
|
|
|
|
} else {
|
|
|
|
*p++ = (i + 8) + '0';
|
|
|
|
*p++ = ' ';
|
|
|
|
}
|
|
|
|
*p++ = ' ';
|
2022-07-11 12:55:17 +00:00
|
|
|
p = HexCpy(p, ctx->uc_mcontext.fpregs->xmm[i + 8].u64[1], 64);
|
|
|
|
p = HexCpy(p, ctx->uc_mcontext.fpregs->xmm[i + 8].u64[0], 64);
|
2022-11-02 05:36:03 +00:00
|
|
|
*p++ = '\n';
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-02 05:36:03 +00:00
|
|
|
return p;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 04:04:44 +00:00
|
|
|
void ShowCrashReportHook(int, int, int, struct siginfo *, ucontext_t *);
|
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
2021-09-28 05:58:51 +00:00
|
|
|
ucontext_t *ctx) {
|
2020-11-09 23:41:11 +00:00
|
|
|
int i;
|
2023-09-12 04:34:53 +00:00
|
|
|
char *p;
|
2021-10-14 00:27:13 +00:00
|
|
|
char host[64];
|
2023-09-12 04:34:53 +00:00
|
|
|
char buf[3000];
|
2020-06-15 14:18:57 +00:00
|
|
|
struct utsname names;
|
2022-09-13 06:10:38 +00:00
|
|
|
if (_weaken(ShowCrashReportHook)) {
|
2021-10-14 00:27:13 +00:00
|
|
|
ShowCrashReportHook(2, err, sig, si, ctx);
|
2021-09-13 04:04:44 +00:00
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
names.sysname[0] = 0;
|
|
|
|
names.release[0] = 0;
|
|
|
|
names.version[0] = 0;
|
|
|
|
names.nodename[0] = 0;
|
2022-07-11 12:55:17 +00:00
|
|
|
stpcpy(host, "unknown");
|
2022-04-18 15:54:42 +00:00
|
|
|
gethostname(host, sizeof(host));
|
|
|
|
uname(&names);
|
2022-03-16 20:33:13 +00:00
|
|
|
errno = err;
|
2022-11-02 05:36:03 +00:00
|
|
|
// TODO(jart): Buffer the WHOLE crash report with backtrace for atomic write.
|
2023-09-12 04:34:53 +00:00
|
|
|
p = buf;
|
2022-11-02 05:36:03 +00:00
|
|
|
p += ksnprintf(
|
2023-09-12 04:34:53 +00:00
|
|
|
p, 10000,
|
2022-11-02 05:36:03 +00:00
|
|
|
"\n%serror%s: Uncaught %G (%s) on %s pid %d tid %d\n"
|
|
|
|
" %s\n"
|
2023-06-18 07:55:09 +00:00
|
|
|
" %s\n"
|
2022-11-02 05:36:03 +00:00
|
|
|
" %s %s %s %s\n",
|
|
|
|
!__nocolor ? "\e[30;101m" : "", !__nocolor ? "\e[0m" : "", sig,
|
2023-07-26 20:54:49 +00:00
|
|
|
(ctx &&
|
|
|
|
(ctx->uc_mcontext.rsp >= GetStaticStackAddr(0) &&
|
|
|
|
ctx->uc_mcontext.rsp <= GetStaticStackAddr(0) + getauxval(AT_PAGESZ)))
|
2022-11-02 05:36:03 +00:00
|
|
|
? "Stack Overflow"
|
2023-09-02 03:49:13 +00:00
|
|
|
: DescribeSiCode(sig, si->si_code),
|
2023-07-02 10:50:29 +00:00
|
|
|
host, getpid(), gettid(), program_invocation_name, strerror(err),
|
|
|
|
names.sysname, names.version, names.nodename, names.release);
|
2020-06-15 14:18:57 +00:00
|
|
|
if (ctx) {
|
2022-11-02 05:36:03 +00:00
|
|
|
p = ShowGeneralRegisters(p, ctx);
|
|
|
|
p = ShowSseRegisters(p, ctx);
|
|
|
|
*p++ = '\n';
|
2023-08-19 13:41:06 +00:00
|
|
|
klog(buf, p - buf);
|
2022-10-09 06:54:05 +00:00
|
|
|
ShowFunctionCalls(ctx);
|
2022-11-02 05:36:03 +00:00
|
|
|
} else {
|
|
|
|
*p++ = '\n';
|
2023-08-19 13:41:06 +00:00
|
|
|
klog(buf, p - buf);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-24 16:59:22 +00:00
|
|
|
kprintf("\n");
|
2022-10-13 20:44:41 +00:00
|
|
|
if (!IsWindows()) __print_maps();
|
2021-10-14 00:27:13 +00:00
|
|
|
/* PrintSystemMappings(2); */
|
2021-09-28 05:58:51 +00:00
|
|
|
if (__argv) {
|
|
|
|
for (i = 0; i < __argc; ++i) {
|
|
|
|
if (!__argv[i]) continue;
|
2022-11-02 05:36:03 +00:00
|
|
|
if (IsAsan() && !__asan_is_valid_str(__argv[i])) continue;
|
2022-03-16 20:33:13 +00:00
|
|
|
kprintf("%s ", __argv[i]);
|
2021-09-28 05:58:51 +00:00
|
|
|
}
|
2020-11-09 23:41:11 +00:00
|
|
|
}
|
2022-04-24 16:59:22 +00:00
|
|
|
kprintf("\n");
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 15:12:43 +00:00
|
|
|
static relegated wontreturn void RaiseCrash(int sig) {
|
|
|
|
sigset_t ss;
|
|
|
|
sigfillset(&ss);
|
|
|
|
sigdelset(&ss, sig);
|
|
|
|
sigprocmask(SIG_SETMASK, &ss, 0);
|
|
|
|
signal(sig, SIG_DFL);
|
|
|
|
kill(getpid(), sig);
|
|
|
|
_Exit(128 + sig);
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 12:47:54 +00:00
|
|
|
relegated void __oncrash_amd64(int sig, struct siginfo *si, void *arg) {
|
2022-05-14 11:33:58 +00:00
|
|
|
int gdbpid, err;
|
2023-05-12 12:47:54 +00:00
|
|
|
ucontext_t *ctx = arg;
|
2023-09-10 15:12:43 +00:00
|
|
|
|
|
|
|
// print vital error nubers reliably
|
|
|
|
// the surface are of code this calls is small and audited
|
|
|
|
kprintf(
|
|
|
|
"\r\n\e[1;31m__oncrash %G %s pid %d tid %d rip %x bt %s\e[0m\n", sig,
|
|
|
|
program_invocation_short_name, getpid(), sys_gettid(),
|
|
|
|
ctx ? ctx->uc_mcontext.rip : 0,
|
|
|
|
DescribeBacktrace(ctx ? (struct StackFrame *)ctx->uc_mcontext.rbp
|
|
|
|
: (struct StackFrame *)__builtin_frame_address(0)));
|
|
|
|
|
|
|
|
// print friendlier detailed crash report less reliably
|
|
|
|
// we're in a broken runtime state and so much can go wrong
|
2022-11-02 05:36:03 +00:00
|
|
|
ftrace_enabled(-1);
|
|
|
|
strace_enabled(-1);
|
2023-09-10 15:12:43 +00:00
|
|
|
err = errno;
|
|
|
|
if ((gdbpid = IsDebuggerPresent(true))) {
|
|
|
|
DebugBreak();
|
|
|
|
}
|
|
|
|
if (!(gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT))) {
|
|
|
|
__restore_tty();
|
|
|
|
ShowCrashReport(err, sig, si, ctx);
|
|
|
|
RaiseCrash(sig);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-09 19:14:57 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|