mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-25 12:00:31 +00:00
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this puzzle was porting our POSIX threads cancelation support, since that works differently on ARM64 XNU vs. AMD64. Our semaphore support on Apple Silicon is also superior now compared to AMD64, thanks to the grand central dispatch library which lets *NSYNC locks go faster. - The Cosmopolitan runtime is now more stable, particularly on Windows. To do this, thread local storage is mandatory at all runtime levels, and the innermost packages of the C library is no longer being built using ASAN. TLS is being bootstrapped with a 128-byte TIB during the process startup phase, and then later on the runtime re-allocates it either statically or dynamically to support code using _Thread_local. fork() and execve() now do a better job cooperating with threads. We can now check how much stack memory is left in the process or thread when functions like kprintf() / execve() etc. call alloca(), so that ENOMEM can be raised, reduce a buffer size, or just print a warning. - POSIX signal emulation is now implemented the same way kernels do it with pthread_kill() and raise(). Any thread can interrupt any other thread, regardless of what it's doing. If it's blocked on read/write then the killer thread will cancel its i/o operation so that EINTR can be returned in the mark thread immediately. If it's doing a tight CPU bound operation, then that's also interrupted by the signal delivery. Signal delivery works now by suspending a thread and pushing context data structures onto its stack, and redirecting its execution to a trampoline function, which calls SetThreadContext(GetCurrentThread()) when it's done. - We're now doing a better job managing locks and handles. On NetBSD we now close semaphore file descriptors in forked children. Semaphores on Windows can now be canceled immediately, which means mutexes/condition variables will now go faster. Apple Silicon semaphores can be canceled too. We're now using Apple's pthread_yield() funciton. Apple _nocancel syscalls are now used on XNU when appropriate to ensure pthread_cancel requests aren't lost. The MbedTLS library has been updated to support POSIX thread cancelations. See tool/build/runitd.c for an example of how it can be used for production multi-threaded tls servers. Handles on Windows now leak less often across processes. All i/o operations on Windows are now overlapped, which means file pointers can no longer be inherited across dup() and fork() for the time being. - We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4() which means, for example, that posix_spawn() now goes 3x faster. POSIX spawn is also now more correct. Like Musl, it's now able to report the failure code of execve() via a pipe although our approach favors using shared memory to do that on systems that have a true vfork() function. - We now spawn a thread to deliver SIGALRM to threads when setitimer() is used. This enables the most precise wakeups the OS makes possible. - The Cosmopolitan runtime now uses less memory. On NetBSD for example, it turned out the kernel would actually commit the PT_GNU_STACK size which caused RSS to be 6mb for every process. Now it's down to ~4kb. On Apple Silicon, we reduce the mandatory upstream thread size to the smallest possible size to reduce the memory overhead of Cosmo threads. The examples directory has a program called greenbean which can spawn a web server on Linux with 10,000 worker threads and have the memory usage of the process be ~77mb. The 1024 byte overhead of POSIX-style thread-local storage is now optional; it won't be allocated until the pthread_setspecific/getspecific functions are called. On Windows, the threads that get spawned which are internal to the libc implementation use reserve rather than commit memory, which shaves a few hundred kb. - sigaltstack() is now supported on Windows, however it's currently not able to be used to handle stack overflows, since crash signals are still generated by WIN32. However the crash handler will still switch to the alt stack, which is helpful in environments with tiny threads. - Test binaries are now smaller. Many of the mandatory dependencies of the test runner have been removed. This ensures many programs can do a better job only linking the the thing they're testing. This caused the test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb - long double is no longer used in the implementation details of libc, except in the APIs that define it. The old code that used long double for time (instead of struct timespec) has now been thoroughly removed. - ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing backtraces itself, it'll just print a command you can run on the shell using our new `cosmoaddr2line` program to view the backtrace. - Crash report signal handling now works in a much better way. Instead of terminating the process, it now relies on SA_RESETHAND so that the default SIG_IGN behavior can terminate the process if necessary. - Our pledge() functionality has now been fully ported to AARCH64 Linux.
This commit is contained in:
parent
c4eb838516
commit
ec480f5aa0
638 changed files with 7925 additions and 8282 deletions
|
@ -16,35 +16,48 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/limits.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) {
|
||||
#ifdef __x86_64__
|
||||
#define ADDR2LINE "o/third_party/gcc/bin/x86_64-linux-musl-addr2line"
|
||||
#elif defined(__aarch64__)
|
||||
#define ADDR2LINE "o/third_party/gcc/bin/aarch64-linux-musl-addr2line"
|
||||
#endif
|
||||
|
||||
static struct {
|
||||
atomic_uint once;
|
||||
char *res;
|
||||
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;
|
||||
} g_addr2line;
|
||||
|
||||
const void GetAddr2linePathInit(void) {
|
||||
int e = errno;
|
||||
const char *path;
|
||||
if (!(path = getenv("ADDR2LINE"))) {
|
||||
path = ADDR2LINE;
|
||||
}
|
||||
setenv(var, path, false);
|
||||
return getenv(var);
|
||||
char *buf = g_addr2line.buf;
|
||||
if (isexecutable(path)) {
|
||||
if (*path != '/' && getcwd(buf, PATH_MAX)) {
|
||||
strlcat(buf, "/", PATH_MAX);
|
||||
}
|
||||
strlcat(buf, path, PATH_MAX);
|
||||
}
|
||||
if (*buf) {
|
||||
g_addr2line.res = buf;
|
||||
} else {
|
||||
g_addr2line.res = commandv("addr2line", buf, PATH_MAX);
|
||||
}
|
||||
errno = e;
|
||||
}
|
||||
|
||||
const char *GetAddr2linePath(void) {
|
||||
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;
|
||||
cosmo_once(&g_addr2line.once, GetAddr2linePathInit);
|
||||
return g_addr2line.res;
|
||||
}
|
||||
|
|
|
@ -60,9 +60,9 @@ static void AppendUnit(struct State *s, int64_t x, const char *t) {
|
|||
* Generates process resource usage report.
|
||||
*/
|
||||
void AppendResourceReport(char **b, struct rusage *ru, const char *nl) {
|
||||
double ticks;
|
||||
struct State s;
|
||||
long utime, stime;
|
||||
long double ticks;
|
||||
struct State *st = &s;
|
||||
s.b = b;
|
||||
s.nl = nl;
|
||||
|
@ -75,10 +75,10 @@ void AppendResourceReport(char **b, struct rusage *ru, const char *nl) {
|
|||
appends(b, "needed ");
|
||||
AppendInt(st, utime + stime);
|
||||
appends(b, "us cpu (");
|
||||
AppendInt(st, (long double)stime / (utime + stime) * 100);
|
||||
AppendInt(st, (double)stime / (utime + stime) * 100);
|
||||
appends(b, "% kernel)");
|
||||
AppendNl(st);
|
||||
ticks = ceill((long double)(utime + stime) / (1000000.L / CLK_TCK));
|
||||
ticks = ceill((double)(utime + stime) / (1000000.L / CLK_TCK));
|
||||
if (ru->ru_idrss) {
|
||||
AppendMetric(st, "needed ", lroundl(ru->ru_idrss / ticks),
|
||||
"kb private on average");
|
||||
|
@ -96,8 +96,8 @@ void AppendResourceReport(char **b, struct rusage *ru, const char *nl) {
|
|||
appends(b, "caused ");
|
||||
AppendInt(st, ru->ru_minflt + ru->ru_majflt);
|
||||
appends(b, " page faults (");
|
||||
AppendInt(
|
||||
st, (long double)ru->ru_minflt / (ru->ru_minflt + ru->ru_majflt) * 100);
|
||||
AppendInt(st,
|
||||
(double)ru->ru_minflt / (ru->ru_minflt + ru->ru_majflt) * 100);
|
||||
appends(b, "% memcpy)");
|
||||
AppendNl(st);
|
||||
}
|
||||
|
@ -108,8 +108,7 @@ void AppendResourceReport(char **b, struct rusage *ru, const char *nl) {
|
|||
appendw(b, READ16LE("es"));
|
||||
}
|
||||
appendw(b, READ16LE(" ("));
|
||||
AppendInt(st,
|
||||
(long double)ru->ru_nvcsw / (ru->ru_nvcsw + ru->ru_nivcsw) * 100);
|
||||
AppendInt(st, (double)ru->ru_nvcsw / (ru->ru_nvcsw + ru->ru_nivcsw) * 100);
|
||||
appends(b, "% consensual)");
|
||||
AppendNl(st);
|
||||
}
|
||||
|
@ -129,7 +128,7 @@ void AppendResourceReport(char **b, struct rusage *ru, const char *nl) {
|
|||
AppendNl(st);
|
||||
}
|
||||
if (ru->ru_nsignals) {
|
||||
appends(b, "received ");
|
||||
appends(b, "delivered ");
|
||||
AppendUnit(st, ru->ru_nsignals, "signal");
|
||||
AppendNl(st);
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ dontinstrument dontasan int PrintBacktraceUsingSymbols(
|
|||
}
|
||||
addr = frame->addr;
|
||||
#ifdef __x86_64__
|
||||
if (addr == (intptr_t)_weaken(__gc)) {
|
||||
if (gi && addr == (intptr_t)_weaken(__gc)) {
|
||||
do {
|
||||
--gi;
|
||||
} while ((addr = garbage->p[gi].ret) == (intptr_t)_weaken(__gc));
|
||||
|
|
|
@ -17,36 +17,40 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/describebacktrace.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/backtrace.internal.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
#if SupportsMetal()
|
||||
__static_yoink("_idt");
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Aborts process after printing a backtrace.
|
||||
* Exits process with crash report.
|
||||
*
|
||||
* The `cosmoaddr2line` command may be copied and pasted into the shell
|
||||
* to obtain further details such as function calls and source lines in
|
||||
* the backtrace. Unlike abort() this function doesn't depend on signal
|
||||
* handling infrastructure. If tcsetattr() was called earlier to change
|
||||
* terminal settings, then they'll be restored automatically. Your exit
|
||||
* handlers won't be called. The `KPRINTF_LOG` environment variable may
|
||||
* configure the output location of these reports, defaulting to stderr
|
||||
* which is duplicated at startup, in case the program closes the file.
|
||||
*
|
||||
* @see __minicrash() for signal handlers, e.g. handling abort()
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
relegated dontasan wontreturn void __die(void) {
|
||||
|
||||
// print vital error nubers reliably
|
||||
// the surface are of code this calls is small and audited
|
||||
kprintf("\r\n\e[1;31m__die %s pid %d tid %d bt %s\e[0m\n",
|
||||
program_invocation_short_name, getpid(), sys_gettid(),
|
||||
DescribeBacktrace(__builtin_frame_address(0)));
|
||||
|
||||
// print much friendlier backtrace less reliably
|
||||
// we're in a broken runtime state and so much can go wrong
|
||||
relegated wontreturn void __die(void) {
|
||||
char host[128];
|
||||
__restore_tty();
|
||||
ShowBacktrace(2, __builtin_frame_address(0));
|
||||
strcpy(host, "unknown");
|
||||
gethostname(host, sizeof(host));
|
||||
kprintf("%serror: %s on %s pid %d tid %d has perished%s\n"
|
||||
" cosmoaddr2line %s%s %s\n",
|
||||
__nocolor ? "" : "\e[1;31m", program_invocation_short_name, host,
|
||||
getpid(), gettid(), __nocolor ? "" : "\e[0m", __argv[0],
|
||||
endswith(__argv[0], ".com") ? ".dbg" : "",
|
||||
DescribeBacktrace(__builtin_frame_address(0)));
|
||||
_Exit(77);
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
#define COSMOPOLITAN_LIBC_LOG_GDB_H_
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/rusage.h"
|
||||
#include "libc/calls/wait4.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/proc/proc.internal.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
#include "libc/sysv/consts/w.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
|
|
@ -10,9 +10,7 @@ extern bool g_isrunningundermake;
|
|||
|
||||
void __start_fatal(const char *, int);
|
||||
void __restore_tty(void);
|
||||
void RestoreDefaultCrashSignalHandlers(void);
|
||||
void __oncrash_amd64(int, struct siginfo *, void *) relegated;
|
||||
void __oncrash_arm64(int, struct siginfo *, void *) relegated;
|
||||
void __oncrash(int, struct siginfo *, void *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -26,7 +26,10 @@
|
|||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/memtrack.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
__static_yoink("GetSymbolByAddr");
|
||||
|
||||
|
@ -82,10 +85,17 @@ static dontasan bool HasLeaks(void) {
|
|||
dontasan void CheckForMemoryLeaks(void) {
|
||||
struct mallinfo mi;
|
||||
if (!IsAsan()) return; // we need traces to exclude leaky
|
||||
if (!GetSymbolTable()) {
|
||||
kprintf("CheckForMemoryLeaks() needs the symbol table\n");
|
||||
return;
|
||||
}
|
||||
if (!_cmpxchg(&once, false, true)) {
|
||||
kprintf("CheckForMemoryLeaks() may only be called once\n");
|
||||
exit(1);
|
||||
exit(0);
|
||||
}
|
||||
_pthread_unwind(_pthread_self());
|
||||
_pthread_unkey(__get_tls());
|
||||
_pthread_ungarbage();
|
||||
__cxa_finalize(0);
|
||||
STRACE("checking for memory leaks% m");
|
||||
if (!IsAsan()) {
|
||||
|
|
|
@ -32,11 +32,13 @@ LIBC_LOG_A_DIRECTDEPS = \
|
|||
LIBC_NEXGEN32E \
|
||||
LIBC_NT_KERNEL32 \
|
||||
LIBC_NT_NTDLL \
|
||||
LIBC_PROC \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_SYSV \
|
||||
LIBC_SYSV_CALLS \
|
||||
LIBC_THREAD \
|
||||
LIBC_TIME \
|
||||
LIBC_TINYMATH \
|
||||
THIRD_PARTY_COMPILER_RT \
|
||||
|
@ -54,10 +56,14 @@ $(LIBC_LOG_A).pkg: \
|
|||
$(LIBC_LOG_A_OBJS) \
|
||||
$(foreach x,$(LIBC_LOG_A_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/libc/log/backtrace2.o \
|
||||
o/$(MODE)/libc/log/backtrace3.o: private \
|
||||
CFLAGS += \
|
||||
-fno-sanitize=all
|
||||
# offer assurances about the stack safety of cosmo libc
|
||||
$(LIBC_LOG_A_OBJS): private COPTS += -Wframe-larger-than=4096 -Walloca-larger-than=4096
|
||||
|
||||
$(LIBC_RUNTIME_A_OBJS): private \
|
||||
COPTS += \
|
||||
-fno-sanitize=all \
|
||||
-Wframe-larger-than=4096 \
|
||||
-Walloca-larger-than=4096
|
||||
|
||||
o/$(MODE)/libc/log/checkfail.o: private \
|
||||
CFLAGS += \
|
||||
|
@ -67,22 +73,6 @@ o/$(MODE)/libc/log/watch.o: private \
|
|||
CFLAGS += \
|
||||
-ffreestanding
|
||||
|
||||
o/$(MODE)/libc/log/watch.o \
|
||||
o/$(MODE)/libc/log/attachdebugger.o \
|
||||
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_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 \
|
||||
o/$(MODE)/libc/log/ubsan.o \
|
||||
o/$(MODE)/libc/log/die.o: private \
|
||||
CFLAGS += \
|
||||
$(NO_MAGIC)
|
||||
|
||||
LIBC_LOG_LIBS = $(foreach x,$(LIBC_LOG_ARTIFACTS),$($(x)))
|
||||
LIBC_LOG_SRCS = $(foreach x,$(LIBC_LOG_ARTIFACTS),$($(x)_SRCS))
|
||||
LIBC_LOG_HDRS = $(foreach x,$(LIBC_LOG_ARTIFACTS),$($(x)_HDRS))
|
||||
|
|
70
libc/log/minicrash.c
Normal file
70
libc/log/minicrash.c
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*-*- 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/siginfo.h"
|
||||
#include "libc/calls/struct/ucontext.internal.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/describebacktrace.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Prints miniature crash report.
|
||||
*
|
||||
* This function may be called from a signal handler to print vital
|
||||
* information about the cause of a crash. Only vital number values
|
||||
* shall be printed. The `cosmoaddr2line` command may be copied and
|
||||
* pasted into the shell to obtain further details such as function
|
||||
* calls and source lines in the backtrace.
|
||||
*
|
||||
* This function may be used as a sigaction handler, so long as the
|
||||
* `SA_RESETHAND` flag is used. Using `SA_ONSTACK` is also a lovely
|
||||
* feature since sigaltstack() is needed to report stack overflows.
|
||||
*
|
||||
* This implementation is designed to be:
|
||||
*
|
||||
* 1. Reliable under broken runtime states
|
||||
* 2. Require only a few kB of stack
|
||||
* 3. Have minimal binary footprint
|
||||
*
|
||||
* @see __die() for crashing from normal code without aborting
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
relegated dontinstrument void __minicrash(int sig, siginfo_t *si, void *arg) {
|
||||
char host[128];
|
||||
ucontext_t *ctx = arg;
|
||||
strcpy(host, "unknown");
|
||||
gethostname(host, sizeof(host));
|
||||
kprintf(
|
||||
"%serror: %s on %s pid %d tid %d got %G%s code %d addr %p%s\n"
|
||||
"cosmoaddr2line %s%s %lx %s\n",
|
||||
__nocolor ? "" : "\e[1;31m", program_invocation_short_name, host,
|
||||
getpid(), gettid(), sig,
|
||||
__is_stack_overflow(si, ctx) ? " (stack overflow)" : "", si->si_code,
|
||||
si->si_addr, __nocolor ? "" : "\e[0m", __argv[0],
|
||||
endswith(__argv[0], ".com") ? ".dbg" : "", ctx ? ctx->uc_mcontext.PC : 0,
|
||||
DescribeBacktrace(ctx ? (struct StackFrame *)ctx->uc_mcontext.BP
|
||||
: (struct StackFrame *)__builtin_frame_address(0)));
|
||||
}
|
|
@ -18,10 +18,13 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/blockcancel.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/siginfo.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/struct/ucontext.internal.h"
|
||||
#include "libc/calls/struct/utsname.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
|
@ -41,10 +44,12 @@
|
|||
#include "libc/log/log.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/math.h"
|
||||
#include "libc/mem/alloca.h"
|
||||
#include "libc/nexgen32e/stackframe.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/pc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
|
@ -56,6 +61,8 @@
|
|||
__static_yoink("strerror_wr"); // for kprintf %m
|
||||
__static_yoink("strsignal_r"); // for kprintf %G
|
||||
|
||||
#define STACK_ERROR "error: not enough room on stack to print crash report\n"
|
||||
|
||||
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,16 +76,15 @@ static const char kCpuFlags[12] forcealign(1) = "CVPRAKZSTIDO";
|
|||
static const char kFpuExceptions[6] forcealign(1) = "IDZOUP";
|
||||
|
||||
relegated static void ShowFunctionCalls(ucontext_t *ctx) {
|
||||
struct StackFrame *bp;
|
||||
struct StackFrame goodframe;
|
||||
if (!ctx->uc_mcontext.rip) {
|
||||
kprintf("%s is NULL can't show backtrace\n", "RIP");
|
||||
} else {
|
||||
goodframe.next = (struct StackFrame *)ctx->uc_mcontext.rbp;
|
||||
goodframe.addr = ctx->uc_mcontext.rip;
|
||||
bp = &goodframe;
|
||||
ShowBacktrace(2, bp);
|
||||
}
|
||||
kprintf(
|
||||
"cosmoaddr2line %s%s %lx %s\n\n", __argv[0],
|
||||
endswith(__argv[0], ".com") ? ".dbg" : "", ctx ? ctx->uc_mcontext.PC : 0,
|
||||
DescribeBacktrace(ctx ? (struct StackFrame *)ctx->uc_mcontext.BP
|
||||
: (struct StackFrame *)__builtin_frame_address(0)));
|
||||
ShowBacktrace(2, &(struct StackFrame){
|
||||
.next = (struct StackFrame *)ctx->uc_mcontext.rbp,
|
||||
.addr = ctx->uc_mcontext.rip,
|
||||
});
|
||||
}
|
||||
|
||||
relegated static char *AddFlag(char *p, int b, const char *s) {
|
||||
|
@ -90,8 +96,8 @@ relegated static char *AddFlag(char *p, int b, const char *s) {
|
|||
return p;
|
||||
}
|
||||
|
||||
relegated static char *DescribeCpuFlags(char *p, int flags, int x87sw,
|
||||
int mxcsr) {
|
||||
relegated static dontinline char *DescribeCpuFlags(char *p, int flags,
|
||||
int x87sw, int mxcsr) {
|
||||
unsigned i;
|
||||
for (i = 0; i < ARRAYLEN(kCpuFlags); ++i) {
|
||||
if (flags & 1) {
|
||||
|
@ -123,38 +129,16 @@ static char *HexCpy(char p[hasatleast 17], uint64_t x, uint8_t k) {
|
|||
}
|
||||
|
||||
relegated static char *ShowGeneralRegisters(char *p, ucontext_t *ctx) {
|
||||
int64_t x;
|
||||
size_t i, j;
|
||||
const char *s;
|
||||
size_t i, j, k;
|
||||
long double st;
|
||||
*p++ = '\n';
|
||||
for (i = 0, j = 0, k = 0; i < ARRAYLEN(kGregNames); ++i) {
|
||||
for (i = 0, j = 0; i < ARRAYLEN(kGregNames); ++i) {
|
||||
if (j > 0) *p++ = ' ';
|
||||
if (!(s = kGregNames[(unsigned)kGregOrder[i]])[2]) *p++ = ' ';
|
||||
p = stpcpy(p, s), *p++ = ' ';
|
||||
p = HexCpy(p, ctx->uc_mcontext.gregs[(unsigned)kGregOrder[i]], 64);
|
||||
if (++j == 3) {
|
||||
j = 0;
|
||||
if (ctx->uc_mcontext.fpregs) {
|
||||
memcpy(&st, (char *)&ctx->uc_mcontext.fpregs->st[k], sizeof(st));
|
||||
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);
|
||||
}
|
||||
}
|
||||
*p++ = '\n';
|
||||
}
|
||||
}
|
||||
|
@ -203,12 +187,22 @@ relegated static char *ShowSseRegisters(char *p, ucontext_t *ctx) {
|
|||
|
||||
void ShowCrashReportHook(int, int, int, struct siginfo *, ucontext_t *);
|
||||
|
||||
relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
||||
ucontext_t *ctx) {
|
||||
static relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
||||
ucontext_t *ctx) {
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
long size = __get_safe_size(8192, 4096);
|
||||
if (size < 6000) {
|
||||
klog(STACK_ERROR, sizeof(STACK_ERROR) - 1);
|
||||
__minicrash(sig, si, ctx);
|
||||
return;
|
||||
}
|
||||
char *buf = alloca(size);
|
||||
CheckLargeStackAllocation(buf, size);
|
||||
#pragma GCC pop_options
|
||||
int i;
|
||||
char *p;
|
||||
char *p = buf;
|
||||
char host[64];
|
||||
char buf[3000];
|
||||
struct utsname names;
|
||||
if (_weaken(ShowCrashReportHook)) {
|
||||
ShowCrashReportHook(2, err, sig, si, ctx);
|
||||
|
@ -222,21 +216,18 @@ relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
|||
uname(&names);
|
||||
errno = err;
|
||||
// TODO(jart): Buffer the WHOLE crash report with backtrace for atomic write.
|
||||
p = buf;
|
||||
p += ksnprintf(
|
||||
p, 10000,
|
||||
"\n%serror%s: Uncaught %G (%s) on %s pid %d tid %d\n"
|
||||
" %s\n"
|
||||
" %s\n"
|
||||
" %s %s %s %s\n",
|
||||
!__nocolor ? "\e[30;101m" : "", !__nocolor ? "\e[0m" : "", sig,
|
||||
(ctx &&
|
||||
(ctx->uc_mcontext.rsp >= GetStaticStackAddr(0) &&
|
||||
ctx->uc_mcontext.rsp <= GetStaticStackAddr(0) + getauxval(AT_PAGESZ)))
|
||||
? "Stack Overflow"
|
||||
: DescribeSiCode(sig, si->si_code),
|
||||
host, getpid(), gettid(), program_invocation_name, strerror(err),
|
||||
names.sysname, names.version, names.nodename, names.release);
|
||||
p +=
|
||||
ksnprintf(p, 4000,
|
||||
"\n%serror%s: Uncaught %G (%s) at %p on %s pid %d tid %d\n"
|
||||
" %s\n"
|
||||
" %s\n"
|
||||
" %s %s %s %s\n",
|
||||
!__nocolor ? "\e[30;101m" : "", !__nocolor ? "\e[0m" : "", sig,
|
||||
__is_stack_overflow(si, ctx) ? "\e[7mStack Overflow\e[0m"
|
||||
: DescribeSiCode(sig, si->si_code),
|
||||
si->si_addr, host, getpid(), gettid(), program_invocation_name,
|
||||
strerror(err), names.sysname, names.version, names.nodename,
|
||||
names.release);
|
||||
if (ctx) {
|
||||
p = ShowGeneralRegisters(p, ctx);
|
||||
p = ShowSseRegisters(p, ctx);
|
||||
|
@ -248,45 +239,21 @@ relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
|||
klog(buf, p - buf);
|
||||
}
|
||||
kprintf("\n");
|
||||
if (!IsWindows()) __print_maps();
|
||||
if (!IsWindows()) {
|
||||
__print_maps();
|
||||
}
|
||||
/* PrintSystemMappings(2); */
|
||||
if (__argv) {
|
||||
for (i = 0; i < __argc; ++i) {
|
||||
if (!__argv[i]) continue;
|
||||
if (IsAsan() && !__asan_is_valid_str(__argv[i])) continue;
|
||||
kprintf("%s ", __argv[i]);
|
||||
}
|
||||
}
|
||||
kprintf("\n");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
relegated void __oncrash_amd64(int sig, struct siginfo *si, void *arg) {
|
||||
int gdbpid, err;
|
||||
relegated void __oncrash(int sig, struct siginfo *si, void *arg) {
|
||||
ucontext_t *ctx = arg;
|
||||
|
||||
// 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
|
||||
ftrace_enabled(-1);
|
||||
strace_enabled(-1);
|
||||
int gdbpid, err;
|
||||
err = errno;
|
||||
if ((gdbpid = IsDebuggerPresent(true))) {
|
||||
DebugBreak();
|
||||
|
@ -294,7 +261,6 @@ relegated void __oncrash_amd64(int sig, struct siginfo *si, void *arg) {
|
|||
if (!(gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT))) {
|
||||
__restore_tty();
|
||||
ShowCrashReport(err, sig, si, ctx);
|
||||
RaiseCrash(sig);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "ape/sections.internal.h"
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/blockcancel.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/aarch64.internal.h"
|
||||
#include "libc/calls/struct/rusage.internal.h"
|
||||
|
@ -25,10 +26,12 @@
|
|||
#include "libc/calls/struct/siginfo.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/struct/ucontext.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/describebacktrace.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/internal.h"
|
||||
|
@ -36,6 +39,7 @@
|
|||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/stackframe.h"
|
||||
#include "libc/runtime/memtrack.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
|
@ -49,6 +53,8 @@
|
|||
__static_yoink("strerror_wr"); // for kprintf %m
|
||||
__static_yoink("strsignal_r"); // for kprintf %G
|
||||
|
||||
#define STACK_ERROR "error: not enough room on stack to print crash report\n"
|
||||
|
||||
#define RESET "\e[0m"
|
||||
#define BOLD "\e[1m"
|
||||
#define STRONG "\e[30;101m"
|
||||
|
@ -75,16 +81,6 @@ static relegated void Append(struct Buffer *b, const char *fmt, ...) {
|
|||
va_end(va);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static relegated const char *ColorRegister(int r) {
|
||||
if (__nocolor) return "";
|
||||
switch (r) {
|
||||
|
@ -144,7 +140,7 @@ static relegated bool AppendFileLine(struct Buffer *b, const char *addr2line,
|
|||
sys_close(pfd[0]);
|
||||
sys_dup2(pfd[1], 1, 0);
|
||||
sys_close(2);
|
||||
__sys_execve(
|
||||
sys_execve(
|
||||
addr2line,
|
||||
(char *const[]){(char *)addr2line, "-pifCe", (char *)debugbin, buf, 0},
|
||||
(char *const[]){0});
|
||||
|
@ -189,98 +185,129 @@ static relegated char *GetSymbolName(struct SymbolTable *st, int symbol,
|
|||
return s;
|
||||
}
|
||||
|
||||
relegated void __oncrash_arm64(int sig, struct siginfo *si, void *arg) {
|
||||
char buf[10000];
|
||||
ucontext_t *ctx = arg;
|
||||
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;
|
||||
char host[64] = "unknown";
|
||||
struct utsname names = {0};
|
||||
once = true;
|
||||
ftrace_enabled(-1);
|
||||
strace_enabled(-1);
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
|
||||
static relegated void __oncrash_impl(int sig, struct siginfo *si,
|
||||
ucontext_t *ctx) {
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
long size = __get_safe_size(10000, 4096);
|
||||
if (size < 80) {
|
||||
// almost certainly guaranteed to succeed
|
||||
klog(STACK_ERROR, sizeof(STACK_ERROR) - 1);
|
||||
__restore_tty();
|
||||
uname(&names);
|
||||
gethostname(host, sizeof(host));
|
||||
reset = !__nocolor ? RESET : "";
|
||||
strong = !__nocolor ? STRONG : "";
|
||||
if (ctx &&
|
||||
(ctx->uc_mcontext.sp & (GetStackSize() - 1)) <= getauxval(AT_PAGESZ)) {
|
||||
kind = "Stack Overflow";
|
||||
} else {
|
||||
kind = DescribeSiCode(sig, si->si_code);
|
||||
}
|
||||
Append(b,
|
||||
"%serror%s: Uncaught %G (%s) on %s pid %d tid %d\n"
|
||||
"%s\n",
|
||||
strong, reset, sig, kind, host, getpid(), gettid(),
|
||||
program_invocation_name);
|
||||
if (errno) {
|
||||
Append(b, " %m\n");
|
||||
}
|
||||
Append(b, " %s %s %s %s\n", names.sysname, names.version, names.nodename,
|
||||
names.release);
|
||||
if (ctx) {
|
||||
long pc;
|
||||
char *mem = 0;
|
||||
size_t memsz = 0;
|
||||
int addend, symbol;
|
||||
const char *debugbin;
|
||||
const char *addr2line;
|
||||
struct StackFrame *fp;
|
||||
struct SymbolTable *st;
|
||||
struct fpsimd_context *vc;
|
||||
st = GetSymbolTable();
|
||||
debugbin = FindDebugBinary();
|
||||
addr2line = GetAddr2linePath();
|
||||
__minicrash(sig, si, ctx);
|
||||
return;
|
||||
}
|
||||
char *buf = alloca(size);
|
||||
CheckLargeStackAllocation(buf, size);
|
||||
#pragma GCC pop_options
|
||||
int i, j;
|
||||
const char *kind;
|
||||
const char *reset;
|
||||
const char *strong;
|
||||
char host[64] = "unknown";
|
||||
struct utsname names = {0};
|
||||
struct Buffer b[1] = {{buf, size}};
|
||||
b->p[b->i++] = '\n';
|
||||
ftrace_enabled(-1);
|
||||
strace_enabled(-1);
|
||||
__restore_tty();
|
||||
uname(&names);
|
||||
gethostname(host, sizeof(host));
|
||||
reset = !__nocolor ? RESET : "";
|
||||
strong = !__nocolor ? STRONG : "";
|
||||
if (__is_stack_overflow(si, ctx)) {
|
||||
kind = "\e[7mStack Overflow\e[0m";
|
||||
} else {
|
||||
kind = DescribeSiCode(sig, si->si_code);
|
||||
}
|
||||
Append(b, "%serror%s: Uncaught %G (%s) on %s pid %d tid %d\n", strong, reset,
|
||||
sig, kind, host, getpid(), gettid());
|
||||
if (program_invocation_name) {
|
||||
Append(b, " %s\n", program_invocation_name);
|
||||
}
|
||||
if (errno) {
|
||||
Append(b, " %s\n", strerror(errno));
|
||||
}
|
||||
Append(b, " %s %s %s %s\n", names.sysname, names.version, names.nodename,
|
||||
names.release);
|
||||
if (ctx) {
|
||||
long pc;
|
||||
char *mem = 0;
|
||||
size_t memsz = 0;
|
||||
int addend, symbol;
|
||||
const char *debugbin;
|
||||
const char *addr2line;
|
||||
struct StackFrame *fp;
|
||||
struct SymbolTable *st;
|
||||
struct fpsimd_context *vc;
|
||||
st = GetSymbolTable();
|
||||
debugbin = FindDebugBinary();
|
||||
addr2line = GetAddr2linePath();
|
||||
|
||||
if (ctx->uc_mcontext.fault_address) {
|
||||
Append(b, " fault_address = %#lx\n", ctx->uc_mcontext.fault_address);
|
||||
if (sig == SIGFPE || //
|
||||
sig == SIGILL || //
|
||||
sig == SIGBUS || //
|
||||
sig == SIGSEGV || //
|
||||
sig == SIGTRAP) {
|
||||
Append(b, " faulting address is %016lx\n", si->si_addr);
|
||||
}
|
||||
|
||||
// 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 x%d%s", ColorRegister(r),
|
||||
ctx->uc_mcontext.regs[r], reset, r, r == 8 || r == 9 ? " " : "");
|
||||
}
|
||||
Append(b, "\n");
|
||||
}
|
||||
|
||||
// PRINT REGISTERS
|
||||
for (i = 0; i < 8; ++i) {
|
||||
// PRINT VECTORS
|
||||
vc = (struct fpsimd_context *)ctx->uc_mcontext.__reserved;
|
||||
if (vc->head.magic == FPSIMD_MAGIC) {
|
||||
int n = 16;
|
||||
while (n && !vc->vregs[n - 1] && !vc->vregs[n - 2]) n -= 2;
|
||||
for (i = 0; i * 2 < n; ++i) {
|
||||
Append(b, " ");
|
||||
for (j = 0; j < 4; ++j) {
|
||||
int r = 8 * j + i;
|
||||
for (j = 0; j < 2; ++j) {
|
||||
int r = j + 2 * i;
|
||||
if (j) Append(b, " ");
|
||||
Append(b, "%s%016lx%s x%d%s", ColorRegister(r),
|
||||
ctx->uc_mcontext.regs[r], reset, r,
|
||||
r == 8 || r == 9 ? " " : "");
|
||||
Append(b, "%016lx ..%s %016lx v%d%s", (long)(vc->vregs[r] >> 64),
|
||||
!j ? "" : ".", (long)vc->vregs[r], r, r < 10 ? " " : "");
|
||||
}
|
||||
Append(b, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
// PRINT VECTORS
|
||||
vc = (struct fpsimd_context *)ctx->uc_mcontext.__reserved;
|
||||
if (vc->head.magic == FPSIMD_MAGIC) {
|
||||
int n = 16;
|
||||
while (n && !vc->vregs[n - 1] && !vc->vregs[n - 2]) n -= 2;
|
||||
for (i = 0; i * 2 < n; ++i) {
|
||||
Append(b, " ");
|
||||
for (j = 0; j < 2; ++j) {
|
||||
int r = j + 2 * i;
|
||||
if (j) Append(b, " ");
|
||||
Append(b, "%016lx ..%s %016lx v%d%s", (long)(vc->vregs[r] >> 64),
|
||||
!j ? "" : ".", (long)vc->vregs[r], r, r < 10 ? " " : "");
|
||||
}
|
||||
Append(b, "\n");
|
||||
}
|
||||
// PRINT CURRENT LOCATION
|
||||
//
|
||||
// We can get the address of the currently executing function by
|
||||
// simply examining the program counter.
|
||||
pc = ctx->uc_mcontext.pc;
|
||||
Append(b, " %016lx sp %lx pc", ctx->uc_mcontext.sp, pc);
|
||||
if (pc && st && (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", GetSymbolName(st, symbol, &mem, &memsz));
|
||||
if (addend) Append(b, "%+d", addend);
|
||||
}
|
||||
}
|
||||
Append(b, "\n");
|
||||
|
||||
// PRINT CURRENT LOCATION
|
||||
//
|
||||
// We can get the address of the currently executing function by
|
||||
// simply examining the program counter.
|
||||
pc = ctx->uc_mcontext.pc;
|
||||
Append(b, " %016lx sp %lx pc", ctx->uc_mcontext.sp, pc);
|
||||
// PRINT LINKED LOCATION
|
||||
//
|
||||
// The x30 register can usually tell us the address of the parent
|
||||
// function. This can help us determine the caller in cases where
|
||||
// stack frames aren't being generated by the compiler; but if we
|
||||
// have stack frames, then we need to ensure this won't duplicate
|
||||
// the first element of the frame pointer backtrace below.
|
||||
fp = (struct StackFrame *)ctx->uc_mcontext.regs[29];
|
||||
if (IsCode((pc = ctx->uc_mcontext.regs[30]))) {
|
||||
Append(b, " %016lx sp %lx lr", ctx->uc_mcontext.sp, pc);
|
||||
if (pc && st && (symbol = __get_symbol(st, pc))) {
|
||||
addend = pc - st->addr_base;
|
||||
addend -= st->symbols[symbol].x;
|
||||
|
@ -291,75 +318,56 @@ relegated void __oncrash_arm64(int sig, struct siginfo *si, void *arg) {
|
|||
}
|
||||
}
|
||||
Append(b, "\n");
|
||||
if (fp && !kisdangerous(fp) && pc == fp->addr) {
|
||||
fp = fp->next;
|
||||
}
|
||||
}
|
||||
|
||||
// PRINT LINKED LOCATION
|
||||
//
|
||||
// The x30 register can usually tell us the address of the parent
|
||||
// function. This can help us determine the caller in cases where
|
||||
// stack frames aren't being generated by the compiler; but if we
|
||||
// have stack frames, then we need to ensure this won't duplicate
|
||||
// the first element of the frame pointer backtrace below.
|
||||
fp = (struct StackFrame *)ctx->uc_mcontext.regs[29];
|
||||
if (IsCode((pc = ctx->uc_mcontext.regs[30]))) {
|
||||
Append(b, " %016lx sp %lx lr", ctx->uc_mcontext.sp, pc);
|
||||
if (pc && st && (symbol = __get_symbol(st, pc))) {
|
||||
// PRINT FRAME POINTERS
|
||||
//
|
||||
// The prologues and epilogues of non-leaf functions should save
|
||||
// the frame pointer (x29) and return address (x30) to the stack
|
||||
// and then set x29 to sp, which is the address of the new frame
|
||||
// effectively creating a daisy chain letting us trace back into
|
||||
// the origin of execution, e.g. _start(), or sys_clone_linux().
|
||||
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 (st && (pc = fp->addr)) {
|
||||
if ((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", GetSymbolName(st, symbol, &mem, &memsz));
|
||||
if (addend) Append(b, "%+d", addend);
|
||||
}
|
||||
}
|
||||
Append(b, "\n");
|
||||
if (fp && !kisdangerous(fp) && pc == fp->addr) {
|
||||
fp = fp->next;
|
||||
}
|
||||
}
|
||||
|
||||
// PRINT FRAME POINTERS
|
||||
//
|
||||
// The prologues and epilogues of non-leaf functions should save
|
||||
// the frame pointer (x29) and return address (x30) to the stack
|
||||
// and then set x29 to sp, which is the address of the new frame
|
||||
// effectively creating a daisy chain letting us trace back into
|
||||
// the origin of execution, e.g. _start(), or sys_clone_linux().
|
||||
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 (st && (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) && st) {
|
||||
Append(b, "%s", GetSymbolName(st, symbol, &mem, &memsz));
|
||||
if (addend) Append(b, "%+d", addend);
|
||||
}
|
||||
Append(b, "\n");
|
||||
} else {
|
||||
symbol = 0;
|
||||
addend = 0;
|
||||
}
|
||||
free(mem);
|
||||
Append(b, " %016lx fp %lx lr ", fp, pc);
|
||||
if (!AppendFileLine(b, addr2line, debugbin, pc) && st) {
|
||||
Append(b, "%s", GetSymbolName(st, symbol, &mem, &memsz));
|
||||
if (addend) Append(b, "%+d", addend);
|
||||
}
|
||||
Append(b, "\n");
|
||||
}
|
||||
} else {
|
||||
Append(b, "got %G while crashing! pc %lx lr %lx\n", sig,
|
||||
ctx->uc_mcontext.pc, ctx->uc_mcontext.regs[30]);
|
||||
free(mem);
|
||||
}
|
||||
b->p[b->n - 1] = '\n';
|
||||
sys_write(2, b->p, MIN(b->i, b->n));
|
||||
__print_maps();
|
||||
RaiseCrash(sig);
|
||||
}
|
||||
|
||||
relegated void __oncrash(int sig, struct siginfo *si, void *arg) {
|
||||
ucontext_t *ctx = arg;
|
||||
BLOCK_CANCELLATIONS;
|
||||
__oncrash_impl(sig, si, ctx);
|
||||
ALLOW_CANCELLATIONS;
|
||||
}
|
||||
|
||||
#endif /* __aarch64__ */
|
||||
|
|
|
@ -17,26 +17,18 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/sigaltstack.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/intrin/leaky.internal.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/internal.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/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/sa.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/ss.h"
|
||||
|
||||
#ifndef TINY
|
||||
__static_yoink("zipos"); // for symtab
|
||||
__static_yoink("__die"); // for backtracing
|
||||
__static_yoink("ShowBacktrace"); // for backtracing
|
||||
|
@ -44,82 +36,26 @@ __static_yoink("GetSymbolTable"); // for backtracing
|
|||
__static_yoink("PrintBacktraceUsingSymbols"); // for backtracing
|
||||
__static_yoink("malloc_inspect_all"); // for asan memory origin
|
||||
__static_yoink("GetSymbolByAddr"); // for asan memory origin
|
||||
|
||||
struct CrashHandler {
|
||||
int sig;
|
||||
struct sigaction old;
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
static void __got_sigquit(int sig, struct siginfo *si, void *arg) {
|
||||
write(2, "^\\", 2);
|
||||
__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;
|
||||
struct CrashHandler *ch = arg;
|
||||
strace_enabled(-1);
|
||||
e = errno;
|
||||
sigaction(ch->sig, &ch->old, NULL);
|
||||
errno = e;
|
||||
free(ch);
|
||||
strace_enabled(+1);
|
||||
}
|
||||
|
||||
static void InstallCrashHandler(int sig, sigaction_f thunk) {
|
||||
int e;
|
||||
static void InstallCrashHandler(int sig, int flags) {
|
||||
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_ONSTACK;
|
||||
if (!sigaction(sig, &sa, &ch->old)) {
|
||||
__cxa_atexit(RemoveCrashHandler, ch, 0);
|
||||
}
|
||||
}
|
||||
errno = e;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sigaddset(&sa.sa_mask, SIGQUIT);
|
||||
sigaddset(&sa.sa_mask, SIGFPE);
|
||||
sigaddset(&sa.sa_mask, SIGILL);
|
||||
sigaddset(&sa.sa_mask, SIGSEGV);
|
||||
sigaddset(&sa.sa_mask, SIGTRAP);
|
||||
sigaddset(&sa.sa_mask, SIGBUS);
|
||||
sigaddset(&sa.sa_mask, SIGABRT);
|
||||
sa.sa_flags = SA_SIGINFO | flags;
|
||||
#ifdef TINY
|
||||
sa.sa_sigaction = __minicrash;
|
||||
#else
|
||||
GetSymbolTable();
|
||||
sa.sa_sigaction = __oncrash;
|
||||
#endif
|
||||
unassert(!sigaction(sig, &sa, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -138,21 +74,24 @@ static void InstallCrashHandler(int sig, sigaction_f thunk) {
|
|||
* useful, for example, if a program is caught in an infinite loop.
|
||||
*/
|
||||
void ShowCrashReports(void) {
|
||||
if (!IsWindows()) {
|
||||
struct sigaltstack ss;
|
||||
ss.ss_flags = 0;
|
||||
ss.ss_size = SIGSTKSZ;
|
||||
ss.ss_sp = malloc(SIGSTKSZ);
|
||||
sigaltstack(&ss, 0);
|
||||
__cxa_atexit(free, ss.ss_sp, 0);
|
||||
}
|
||||
InstallCrashHandler(SIGQUIT, __got_sigquit); // ctrl+\ aka ctrl+break
|
||||
InstallCrashHandler(SIGFPE, __got_sigfpe); // 1 / 0
|
||||
InstallCrashHandler(SIGILL, __got_sigill); // illegal instruction
|
||||
InstallCrashHandler(SIGSEGV, __got_sigsegv); // bad memory access
|
||||
InstallCrashHandler(SIGTRAP, __got_sigtrap); // bad system call
|
||||
InstallCrashHandler(SIGBUS, __got_sigbus); // misalign, mmap i/o failed
|
||||
InstallCrashHandler(SIGURG, __got_sigurg); // placeholder
|
||||
struct sigaltstack ss;
|
||||
static char crashstack[65536];
|
||||
ss.ss_flags = 0;
|
||||
ss.ss_size = sizeof(crashstack);
|
||||
ss.ss_sp = crashstack;
|
||||
unassert(!sigaltstack(&ss, 0));
|
||||
InstallCrashHandler(SIGQUIT, 0);
|
||||
#ifdef __x86_64__
|
||||
InstallCrashHandler(SIGTRAP, 0);
|
||||
#else
|
||||
InstallCrashHandler(SIGTRAP, SA_RESETHAND);
|
||||
#endif
|
||||
InstallCrashHandler(SIGFPE, SA_RESETHAND);
|
||||
InstallCrashHandler(SIGILL, SA_RESETHAND);
|
||||
InstallCrashHandler(SIGBUS, SA_RESETHAND);
|
||||
InstallCrashHandler(SIGABRT, SA_RESETHAND);
|
||||
InstallCrashHandler(SIGSEGV, SA_RESETHAND | SA_ONSTACK);
|
||||
_wantcrashreports = true;
|
||||
GetSymbolTable();
|
||||
}
|
||||
|
||||
IGNORE_LEAKS(ShowCrashReports)
|
||||
|
|
|
@ -136,7 +136,6 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
|
|||
"exiting due to aforementioned error (host %s pid %d tid %d)\n",
|
||||
buf32, getpid(), gettid());
|
||||
__die();
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
ALLOW_CANCELLATIONS;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue