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-05-19 23:57:49 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
|
|
|
#include "libc/calls/struct/sigaltstack.h"
|
2022-08-14 00:20:50 +00:00
|
|
|
#include "libc/errno.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/log/internal.h"
|
|
|
|
#include "libc/log/log.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/runtime/symbols.internal.h"
|
2022-08-11 07:15:29 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/sa.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/sysv/consts/ss.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-06-19 08:13:03 +00:00
|
|
|
STATIC_YOINK("__die"); // for backtracing
|
|
|
|
STATIC_YOINK("ShowBacktrace"); // for backtracing
|
|
|
|
STATIC_YOINK("GetSymbolTable"); // for backtracing
|
|
|
|
STATIC_YOINK("PrintBacktraceUsingSymbols"); // for backtracing
|
|
|
|
STATIC_YOINK("malloc_inspect_all"); // for asan memory origin
|
2022-09-13 06:10:38 +00:00
|
|
|
STATIC_YOINK("GetSymbolByAddr"); // for asan memory origin
|
2020-08-25 11:23:25 +00:00
|
|
|
|
2021-08-13 18:18:25 +00:00
|
|
|
extern const unsigned char __oncrash_thunks[8][11];
|
2022-05-19 23:57:49 +00:00
|
|
|
static struct sigaltstack g_oldsigaltstack;
|
2022-06-23 20:01:01 +00:00
|
|
|
static struct sigaction g_oldcrashacts[8];
|
2022-05-19 23:57:49 +00:00
|
|
|
|
|
|
|
static void InstallCrashHandlers(int extraflags) {
|
2022-07-08 13:29:24 +00:00
|
|
|
int e;
|
2022-05-19 23:57:49 +00:00
|
|
|
size_t i;
|
|
|
|
struct sigaction sa;
|
|
|
|
bzero(&sa, sizeof(sa));
|
|
|
|
sa.sa_flags = SA_SIGINFO | SA_NODEFER | extraflags;
|
|
|
|
sigfillset(&sa.sa_mask);
|
|
|
|
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];
|
2022-07-08 13:29:24 +00:00
|
|
|
e = errno;
|
2022-05-19 23:57:49 +00:00
|
|
|
sigaction(kCrashSigs[i], &sa, &g_oldcrashacts[i]);
|
2022-07-08 13:29:24 +00:00
|
|
|
errno = e;
|
2022-05-19 23:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
relegated void RestoreDefaultCrashSignalHandlers(void) {
|
2022-07-08 13:29:24 +00:00
|
|
|
int e;
|
2022-05-19 23:57:49 +00:00
|
|
|
size_t i;
|
|
|
|
sigset_t ss;
|
2022-07-09 06:06:46 +00:00
|
|
|
--__strace;
|
2022-05-19 23:57:49 +00:00
|
|
|
sigemptyset(&ss);
|
|
|
|
sigprocmask(SIG_SETMASK, &ss, NULL);
|
|
|
|
for (i = 0; i < ARRAYLEN(kCrashSigs); ++i) {
|
|
|
|
if (kCrashSigs[i]) {
|
2022-07-08 13:29:24 +00:00
|
|
|
e = errno;
|
2022-05-19 23:57:49 +00:00
|
|
|
sigaction(kCrashSigs[i], &g_oldcrashacts[i], NULL);
|
2022-07-08 13:29:24 +00:00
|
|
|
errno = e;
|
2022-05-19 23:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-09 06:06:46 +00:00
|
|
|
++__strace;
|
2022-05-19 23:57:49 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-05-18 23:41:29 +00:00
|
|
|
static void FreeSigAltStack(void *p) {
|
2022-05-19 23:57:49 +00:00
|
|
|
sigaltstack(&g_oldsigaltstack, 0);
|
|
|
|
munmap(p, GetStackSize());
|
2022-05-18 23:41:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Installs crash signal handlers.
|
|
|
|
*
|
|
|
|
* Normally, only functions calling die() will print backtraces. This
|
|
|
|
* function may be called at program startup to install handlers that
|
|
|
|
* will display similar information, for most types of crashes.
|
|
|
|
*
|
|
|
|
* - Backtraces
|
|
|
|
* - CPU state printout
|
|
|
|
* - Automatic debugger attachment
|
|
|
|
*
|
|
|
|
* Another trick this function enables, is you can press CTRL+\ to open
|
|
|
|
* the debugger GUI at any point while the program is running. It can be
|
|
|
|
* useful, for example, if a program is caught in an infinite loop.
|
|
|
|
*/
|
2021-08-18 21:21:30 +00:00
|
|
|
void ShowCrashReports(void) {
|
2022-05-23 22:52:26 +00:00
|
|
|
char *sp;
|
2021-10-14 00:27:13 +00:00
|
|
|
struct sigaltstack ss;
|
2022-09-11 18:02:07 +00:00
|
|
|
_wantcrashreports = true;
|
2021-08-13 18:18:25 +00:00
|
|
|
/* <SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c */
|
2020-06-15 14:18:57 +00:00
|
|
|
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. */
|
2022-08-07 23:18:33 +00:00
|
|
|
kCrashSigs[7] = SIGURG; /* placeholder */
|
2021-08-13 18:18:25 +00:00
|
|
|
/* </SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c */
|
2022-05-18 23:41:29 +00:00
|
|
|
if (!IsWindows()) {
|
|
|
|
bzero(&ss, sizeof(ss));
|
|
|
|
ss.ss_flags = 0;
|
2022-05-23 22:52:26 +00:00
|
|
|
ss.ss_size = GetStackSize();
|
2022-05-22 11:51:02 +00:00
|
|
|
// FreeBSD sigaltstack() will EFAULT if we use MAP_STACK here
|
|
|
|
// OpenBSD sigaltstack() auto-applies MAP_STACK to the memory
|
2022-09-09 23:54:28 +00:00
|
|
|
if ((sp = _mapanon(GetStackSize()))) {
|
2022-05-23 22:52:26 +00:00
|
|
|
ss.ss_sp = sp;
|
2022-05-19 23:57:49 +00:00
|
|
|
if (!sigaltstack(&ss, &g_oldsigaltstack)) {
|
2022-05-18 23:41:29 +00:00
|
|
|
__cxa_atexit(FreeSigAltStack, ss.ss_sp, 0);
|
|
|
|
} else {
|
2022-05-19 23:57:49 +00:00
|
|
|
munmap(ss.ss_sp, GetStackSize());
|
2022-05-18 23:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 23:57:49 +00:00
|
|
|
InstallCrashHandlers(SA_ONSTACK);
|
|
|
|
} else {
|
|
|
|
InstallCrashHandlers(0);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-16 17:40:23 +00:00
|
|
|
GetSymbolTable();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|