Make terminal ui binaries work well everywhere

Here's some screenshots of an emulator tui program that was compiled on
Linux, then scp'd it to Windows, Mac, and FreeBSD.

https://justine.storage.googleapis.com/blinkenlights-cmdexe.png
https://justine.storage.googleapis.com/blinkenlights-imac.png
https://justine.storage.googleapis.com/blinkenlights-freebsd.png
https://justine.storage.googleapis.com/blinkenlights-lisp.png

How is this even possible that we have a nontrivial ui binary that just
works on Mac, Windows, Linux, and BSD? Surely a first ever achievement.

Fixed many bugs. Bootstrapped John McCarthy's metacircular evaluator on
bare metal in half the size of Altair BASIC (about 2.5kb) and ran it in
emulator for fun and profit.
This commit is contained in:
Justine Tunney 2020-10-10 21:18:53 -07:00
parent 680daf1210
commit 9e3e985ae5
276 changed files with 7026 additions and 3790 deletions

View file

@ -17,25 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/assert.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/conv/conv.h"
#include "libc/conv/itoa.h"
#include "libc/log/asan.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/macros.h"
#include "libc/mem/hook/hook.h"
#include "libc/runtime/directmap.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
@ -154,9 +143,9 @@ static const char *__asan_describe_access_poison(int c) {
}
static noreturn void __asan_die(const char *msg, size_t size) {
__print(msg, size);
PrintBacktraceUsingSymbols(stderr, __builtin_frame_address(0),
getsymboltable());
write(STDERR_FILENO, msg, size);
PrintBacktraceUsingSymbols(STDERR_FILENO, __builtin_frame_address(0),
GetSymbolTable());
DebugBreak();
_Exit(66);
}
@ -170,7 +159,7 @@ static noreturn void __asan_report_deallocate_fault(void *addr, int c) {
p = mempcpy(p, ibuf, int64toarray_radix10(c, ibuf));
p = stpcpy(p, " at 0x");
p = mempcpy(p, ibuf, uint64toarray_fixed16((intptr_t)addr, ibuf, 48));
p = stpcpy(p, "\n");
p = stpcpy(p, "\r\n");
__asan_die(buf, p - buf);
}
@ -186,7 +175,7 @@ static noreturn void __asan_report_memory_fault(uint8_t *addr, int size,
p = stpcpy(p, kind);
p = stpcpy(p, " at 0x");
p = mempcpy(p, ibuf, uint64toarray_fixed16((intptr_t)addr, ibuf, 48));
p = stpcpy(p, "\n");
p = stpcpy(p, "\r\n");
__asan_die(buf, p - buf);
}
@ -209,13 +198,6 @@ static void __asan_morgue_flush(void) {
}
}
static bool __asan_is_mapped(void *p) {
int x, i;
x = (intptr_t)p >> 16;
i = FindMemoryInterval(&_mmi, x);
return i < _mmi.i && x >= _mmi.p[i].x && x <= _mmi.p[i].y;
}
static void *__asan_allocate(size_t align, size_t size, int underrun,
int overrun) {
char *p, *s;
@ -394,44 +376,62 @@ void __asan_install_malloc_hooks(void) {
HOOK(hook$malloc_usable_size, __asan_malloc_usable_size);
}
void __asan_map_shadow(void *addr, size_t size) {
int i, n, x;
char *a, *b;
static bool __asan_is_mapped(int x) {
int i = FindMemoryInterval(&_mmi, x);
return i < _mmi.i && x >= _mmi.p[i].x && x <= _mmi.p[i].y;
}
void __asan_map_shadow(void *p, size_t n) {
int i, x, a, b;
struct DirectMap sm;
a = (char *)ROUNDDOWN(SHADOW((intptr_t)addr), FRAMESIZE);
b = (char *)ROUNDDOWN(SHADOW((intptr_t)addr + size - 1), FRAMESIZE);
for (; a <= b; a += FRAMESIZE) {
a = SHADOW((uintptr_t)p) >> 16;
b = ROUNDUP(SHADOW(ROUNDUP((uintptr_t)p + n, 8)), 1 << 16) >> 16;
for (; a < b; ++a) {
if (!__asan_is_mapped(a)) {
sm = DirectMap(a, FRAMESIZE, PROT_READ | PROT_WRITE,
sm = DirectMap((void *)((uintptr_t)a << 16), 1 << 16,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (sm.addr == MAP_FAILED ||
TrackMemoryInterval(&_mmi, (intptr_t)a >> 16, (intptr_t)a >> 16,
sm.maphandle) == -1) {
TrackMemoryInterval(&_mmi, a, a, sm.maphandle) == -1) {
abort();
}
}
}
}
textstartup void __asan_init(int argc, char *argv[], char **envp,
intptr_t *auxv) {
int i;
static bool once;
register intptr_t rsp asm("rsp");
if (!once) {
__asan_map_shadow(_base, _end - _base);
__asan_map_shadow((void *)ROUNDDOWN(rsp, STACKSIZE), STACKSIZE);
for (i = 0; i < argc; ++i) __asan_map_shadow(argv[i], strlen(argv[i]));
for (; *envp; ++envp) __asan_map_shadow(*envp, strlen(*envp));
__asan_map_shadow(auxv, sizeof(intptr_t) * 2);
__asan_install_malloc_hooks();
once = true;
static char *__asan_get_stack_base(void) {
register uintptr_t rsp asm("rsp");
return (char *)ROUNDDOWN(ROUNDDOWN(rsp, STACKSIZE), FRAMESIZE);
}
static textstartup size_t __asan_get_auxv_size(intptr_t *auxv) {
unsigned i;
for (i = 0;; i += 2) {
if (!auxv[i]) break;
}
return (i + 2) * sizeof(intptr_t);
}
static textstartup void __asan_shadow_string_list(char **list) {
for (; *list; ++list) {
__asan_map_shadow(*list, strlen(*list) + 1);
}
}
textstartup void __asan_init(int argc, char **argv, char **envp,
intptr_t *auxv) {
static bool once;
if (once) return;
__asan_map_shadow(_base, _end - _base);
__asan_map_shadow(__asan_get_stack_base(), STACKSIZE);
__asan_shadow_string_list(argv);
__asan_shadow_string_list(envp);
__asan_map_shadow(auxv, __asan_get_auxv_size(auxv));
__asan_install_malloc_hooks();
}
static textstartup void __asan_ctor(void) {
/* __cxa_atexit(__asan_morgue_flush, NULL, NULL); */
getsymboltable();
__cxa_atexit(__asan_morgue_flush, NULL, NULL);
}
const void *const g_asan_ctor[] initarray = {__asan_ctor};

View file

@ -29,10 +29,13 @@
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/w.h"
#define RESTORE_TTY "\e[?1000;1002;1015;1006l\e[?25h"
/**
* Launches GDB debugger GUI for current process.
*
@ -50,17 +53,18 @@
* @note this is called via eponymous spinlock macro wrapper
*/
relegated int(attachdebugger)(intptr_t continuetoaddr) {
int ttyin, ttyout;
int ttyfd;
struct StackFrame *bp;
char pidstr[11], breakcmd[40];
const char *se, *elf, *gdb, *rewind, *layout;
if (IsGenuineCosmo() || !(gdb = GetGdbPath()) ||
(ttyin = ttyout = open(_PATH_TTY, O_RDWR, 0)) == -1) {
(ttyfd = open(_PATH_TTY, O_RDWR, 0)) == -1) {
return -1;
}
write(ttyfd, RESTORE_TTY, strlen(RESTORE_TTY));
snprintf(pidstr, sizeof(pidstr), "%u", getpid());
layout = "layout asm";
if ((elf = finddebugbinary())) {
if ((elf = FindDebugBinary())) {
se = "-se";
if (fileexists(__FILE__)) layout = "layout src";
} else {
@ -78,7 +82,7 @@ relegated int(attachdebugger)(intptr_t continuetoaddr) {
rewind = NULL;
breakcmd[0] = '\0';
}
return spawnve(0, (int[3]){ttyin, ttyout, STDERR_FILENO}, gdb,
return spawnve(0, (int[3]){ttyfd, ttyfd, STDERR_FILENO}, gdb,
(char *const[]){
"gdb", "--tui",
"-p", pidstr,

View file

@ -1,24 +0,0 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/log/backtrace.h"
void backtrace(FILE *f) {
showbacktrace(f, __builtin_frame_address(0));
}

View file

@ -2,12 +2,11 @@
#define COSMOPOLITAN_LIBC_LOG_BACKTRACE_H_
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
void showbacktrace(FILE *, const struct StackFrame *);
int PrintBacktraceUsingSymbols(FILE *, const struct StackFrame *,
void ShowBacktrace(int, const struct StackFrame *);
int PrintBacktraceUsingSymbols(int, const struct StackFrame *,
struct SymbolTable *);
COSMOPOLITAN_C_END_

View file

@ -31,14 +31,13 @@
#include "libc/nexgen32e/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#define kBacktraceMaxFrames 128
#define kBacktraceBufSize ((kBacktraceMaxFrames - 1) * (16 + 1))
static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
ssize_t got;
intptr_t addr;
size_t i, j, gi;
@ -47,7 +46,7 @@ static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
const struct StackFrame *frame;
const char *debugbin, *p1, *p2, *p3, *addr2line;
char buf[kBacktraceBufSize], *argv[kBacktraceMaxFrames];
if (!(debugbin = finddebugbinary()) || !(addr2line = GetAddr2linePath())) {
if (!(debugbin = FindDebugBinary()) || !(addr2line = GetAddr2linePath())) {
return -1;
}
i = 0;
@ -88,11 +87,11 @@ static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
strlen(" (discriminator ") - 1)) &&
(p3 = memchr(p2, '\n', got - (p2 - p1)))) {
if (p3 > p2 && p3[-1] == '\r') --p3;
fwrite(p1, 1, p2 - p1, f);
write(fd, p1, p2 - p1);
got -= p3 - p1;
p1 += p3 - p1;
} else {
fwrite(p1, 1, got, f);
write(fd, p1, got);
break;
}
}
@ -103,20 +102,21 @@ static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
return 0;
}
static int PrintBacktrace(FILE *f, const struct StackFrame *bp) {
static int PrintBacktrace(int fd, const struct StackFrame *bp) {
if (!IsTiny()) {
if (PrintBacktraceUsingAddr2line(f, bp) != -1) {
if (PrintBacktraceUsingAddr2line(fd, bp) != -1) {
return 0;
}
}
return PrintBacktraceUsingSymbols(f, bp, getsymboltable());
return PrintBacktraceUsingSymbols(fd, bp, GetSymbolTable());
}
void showbacktrace(FILE *f, const struct StackFrame *bp) {
void ShowBacktrace(int fd, const struct StackFrame *bp) {
static bool noreentry;
if (!bp) bp = __builtin_frame_address(0);
if (!noreentry) {
noreentry = true;
PrintBacktrace(f, bp);
PrintBacktrace(fd, bp);
noreentry = 0;
}
}

View file

@ -20,6 +20,7 @@
#include "libc/alg/bisectcarleft.h"
#include "libc/assert.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/conv/itoa.h"
#include "libc/fmt/fmt.h"
#include "libc/log/backtrace.h"
@ -28,26 +29,26 @@
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
/**
* Prints stack frames with symbols.
*
* PrintBacktraceUsingSymbols(stdout, NULL, getsymboltable());
* PrintBacktraceUsingSymbols(STDOUT_FILENO, NULL, GetSymbolTable());
*
* @param f is output stream
* @param bp is rbp which can be NULL to detect automatically
* @param st is open symbol table for current executable
* @return -1 w/ errno if error happened
*/
int PrintBacktraceUsingSymbols(FILE *f, const struct StackFrame *bp,
int PrintBacktraceUsingSymbols(int fd, const struct StackFrame *bp,
struct SymbolTable *st) {
char *p;
size_t gi;
intptr_t addr;
int64_t addend;
struct Garbages *garbage;
char *p, buf[256], ibuf[21];
char buf[256], ibuf[21];
const struct Symbol *symbol;
const struct StackFrame *frame;
if (!st) return -1;
@ -78,8 +79,11 @@ int PrintBacktraceUsingSymbols(FILE *f, const struct StackFrame *bp,
} else {
p = stpcpy(p, "UNKNOWN");
}
*p++ = '\r';
*p++ = '\n';
__print(buf, p - buf);
if (write(fd, buf, p - buf) == -1) {
return -1;
}
}
return 0;
}

View file

@ -1,43 +0,0 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/bisectcarleft.h"
#include "libc/assert.h"
#include "libc/runtime/symbols.h"
/**
* Finds symbol associated with address.
* @param symbol table pointer (null propagating)
* @return symbol address or NULL if not found
*/
const struct Symbol *bisectsymbol(struct SymbolTable *t,
intptr_t virtual_address,
int64_t *opt_out_addend) {
const struct Symbol *symbol = NULL;
int64_t addend = (intptr_t)virtual_address;
if (t && t->count) {
unsigned key = (unsigned)((intptr_t)virtual_address - t->addr_base - 1);
unsigned i = bisectcarleft((const int32_t(*)[2])t->symbols, t->count, key);
assert(0 <= i && i < t->count);
symbol = &t->symbols[i];
addend = (int64_t)key - (int64_t)symbol->addr_rva;
}
if (opt_out_addend) *opt_out_addend = addend;
return symbol;
}

View file

@ -10,7 +10,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define CHECK(X, ...) __CHK(eq, ==, true, "true", !!(X), #X, "" __VA_ARGS__)
#define CHECK(X, ...) __CHK(ne, !=, false, "false", !!(X), #X, "" __VA_ARGS__)
#define CHECK_EQ(Y, X, ...) __CHK(eq, ==, Y, #Y, X, #X, "" __VA_ARGS__)
#define CHECK_NE(Y, X, ...) __CHK(ne, !=, Y, #Y, X, #X, "" __VA_ARGS__)
#define CHECK_LE(Y, X, ...) __CHK(le, <=, Y, #Y, X, #X, "" __VA_ARGS__)
@ -19,7 +19,7 @@ COSMOPOLITAN_C_START_
#define CHECK_GT(Y, X, ...) __CHK(gt, >, Y, #Y, X, #X, "" __VA_ARGS__)
#define CHECK_NOTNULL(X, ...) __CHK(ne, !=, NULL, "NULL", X, #X, "" __VA_ARGS__)
#define DCHECK(X, ...) __DCHK(eq, ==, true, "true", !!(X), #X, "" __VA_ARGS__)
#define DCHECK(X, ...) __DCHK(ne, !=, false, "false", !!(X), #X, "" __VA_ARGS__)
#define DCHECK_EQ(Y, X, ...) __DCHK(eq, ==, Y, #Y, X, #X, "" __VA_ARGS__)
#define DCHECK_NE(Y, X, ...) __DCHK(ne, !=, Y, #Y, X, #X, "" __VA_ARGS__)
#define DCHECK_LE(Y, X, ...) __DCHK(le, <=, Y, #Y, X, #X, "" __VA_ARGS__)

View file

@ -29,7 +29,7 @@ STATIC_YOINK("stoa");
void __check_fail_aligned(unsigned bytes, uint64_t ptr) {
fflush(stderr);
if (!IsTiny()) memsummary(fileno(stderr));
(dprintf)(fileno(stderr), "%s%d%s%#p\n", "error: pointer not ", bytes,
(dprintf)(fileno(stderr), "%s%d%s%#p\r\n", "error: pointer not ", bytes,
"-byte aligned: ", ptr);
die();
}

View file

@ -52,10 +52,10 @@ relegated void __check_fail(const char *suffix, const char *opstr,
strtoupper(sufbuf);
(fprintf)(stderr,
"check failed\n"
"\tCHECK_%s(%s, %s);\n"
"\t\t → %#lx (%s)\n"
"\t\t%s %#lx (%s)\n",
"check failed\r\n"
"\tCHECK_%s(%s, %s);\r\n"
"\t\t → %#lx (%s)\r\n"
"\t\t%s %#lx (%s)\r\n",
sufbuf, wantstr, gotstr, want, wantstr, opstr, got, gotstr);
if (!isempty(fmt)) {
@ -63,18 +63,18 @@ relegated void __check_fail(const char *suffix, const char *opstr,
va_start(va, fmt);
(vfprintf)(stderr, fmt, va);
va_end(va);
fputc('\n', stderr);
fputs("\r\n", stderr);
}
(fprintf)(stderr, "\t%s\n\t%s%s%s%s\n", strerror(lasterr), SUBTLE,
(fprintf)(stderr, "\t%s\r\n\t%s%s%s%s\r\n", strerror(lasterr), SUBTLE,
getauxval(AT_EXECFN), g_argc > 1 ? " \\" : "", RESET);
for (i = 1; i < g_argc; ++i) {
(fprintf)(stderr, "\t\t%s%s\n", g_argv[i], i < g_argc - 1 ? " \\" : "");
(fprintf)(stderr, "\t\t%s%s\r\n", g_argv[i], i < g_argc - 1 ? " \\" : "");
}
if (!IsTiny() && lasterr == ENOMEM) {
(fprintf)(stderr, "\n");
(fprintf)(stderr, "\r\n");
fflush(stderr);
PrintMemoryIntervals(fileno(stderr), &_mmi);
}

View file

@ -47,5 +47,5 @@ relegated void ___check_fail_ndebug(uint64_t want, uint64_t got,
__print(bx, uint64toarray_radix16(got, bx));
__print_string(" (");
__print(bx, int64toarray_radix10(lasterr, bx));
__print_string(")\n");
__print_string(")\r\n");
}

View file

@ -18,10 +18,12 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/runtime/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/fileno.h"
/**
* Aborts process after printing details on its current state.
@ -33,7 +35,7 @@ relegated noreturn void die(void) {
if (!IsTiny()) {
g_runstate |= RUNSTATE_BROKEN;
if (IsDebuggerPresent(false)) DebugBreak();
backtrace(stderr);
ShowBacktrace(STDERR_FILENO, NULL);
}
exit(EXIT_FAILURE);
unreachable;

View file

@ -38,7 +38,7 @@ int(gdbexec)(const char *cmd) {
char pidstr[11], breakcmd[40];
if (!(gdb = GetGdbPath())) return -1;
snprintf(pidstr, sizeof(pidstr), "%u", getpid());
if ((elf = finddebugbinary())) {
if ((elf = FindDebugBinary())) {
se = "-se";
} else {
se = "-q";

View file

@ -1,32 +0,0 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/elf/elf.h"
#include "libc/runtime/symbols.h"
/**
* Finds starting address of symbol.
* @param symbol table pointer (null propagating)
* @return name string or null if not found
*/
const char *getsymbolname(struct SymbolTable *t, const struct Symbol *symbol) {
return (t && symbol)
? getelfstring(t->elf, t->elfsize, t->name_base, symbol->name_rva)
: NULL;
}

View file

@ -24,15 +24,15 @@
* Returns debug binary symbol table, as global singleton.
* @return symbol table, or NULL w/ errno on first call
*/
struct SymbolTable *getsymboltable(void) {
struct SymbolTable *GetSymbolTable(void) {
static bool once;
static struct SymbolTable *singleton;
const char *debugbin;
if (!once) {
once = true;
if ((debugbin = finddebugbinary()) &&
(singleton = opensymboltable(debugbin))) {
__cxa_atexit(closesymboltable, &singleton, NULL);
if ((debugbin = FindDebugBinary()) &&
(singleton = OpenSymbolTable(debugbin))) {
__cxa_atexit(CloseSymbolTable, &singleton, NULL);
}
}
return singleton;

View file

@ -33,7 +33,6 @@ typedef struct FILE FILE;
extern FILE *g_logfile;
void backtrace(FILE *) relegated; /* shows fn backtrace and args */
void perror(const char *) relegated; /* print the last system error */
void die(void) relegated noreturn; /* print backtrace and abort() */
void meminfo(int); /* shows malloc statistics &c. */

View file

@ -25,7 +25,7 @@ STATIC_YOINK("ntoa");
void malloc_stats(void) {
struct MallocStats res = dlmalloc_stats(g_dlmalloc);
(fprintf)(stderr, "max system bytes = %'10zu\n", res.maxfp);
(fprintf)(stderr, "system bytes = %'10zu\n", res.fp);
(fprintf)(stderr, "in use bytes = %'10zu\n", res.used);
(fprintf)(stderr, "max system bytes = %'10zu\r\n", res.maxfp);
(fprintf)(stderr, "system bytes = %'10zu\r\n", res.fp);
(fprintf)(stderr, "in use bytes = %'10zu\r\n", res.used);
}

View file

@ -26,7 +26,7 @@ STATIC_YOINK("ntoa");
STATIC_YOINK("stoa");
static void onmemchunk(void *start, void *end, size_t used_bytes, void *arg) {
(dprintf)(*(int *)arg, "%p - %p : %08zx / %08lx\n", start, end, used_bytes,
(dprintf)(*(int *)arg, "%p - %p : %08zx / %08lx\r\n", start, end, used_bytes,
(intptr_t)end - (intptr_t)start);
}
@ -35,7 +35,7 @@ static void onmemchunk(void *start, void *end, size_t used_bytes, void *arg) {
*/
void meminfo(int fd) {
memsummary(fd);
(dprintf)(fd, "%*s %*s %*s %*s\n", POINTER_XDIGITS, "start",
(dprintf)(fd, "%*s %*s %*s %*s\r\n", POINTER_XDIGITS, "start",
POINTER_XDIGITS, "end", 8, "used", 8, "size");
malloc_inspect_all(onmemchunk, &fd);
}

View file

@ -32,9 +32,9 @@
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
@ -49,17 +49,17 @@ STATIC_YOINK("stoa");
struct siginfo;
aligned(1) const char kGregOrder[17] = {
const char kGregOrder[17] aligned(1) = {
13, 11, 8, 14, 12, 9, 10, 15, 16, 0, 1, 2, 3, 4, 5, 6, 7,
};
aligned(1) const char kGregNames[17][4] = {
const char kGregNames[17][4] aligned(1) = {
"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", "RDI",
"RSI", "RBP", "RBX", "RDX", "RAX", "RCX", "RSP", "RIP",
};
aligned(1) const char kGodHatesFlags[12] = "CVPRAKZSTIDO";
aligned(1) const char kCrashSigNames[8][5] = {"QUIT", "FPE", "ILL", "SEGV",
const char kGodHatesFlags[12] aligned(1) = "CVPRAKZSTIDO";
const char kCrashSigNames[8][5] aligned(1) = {"QUIT", "FPE", "ILL", "SEGV",
"TRAP", "ABRT", "BUS"};
int kCrashSigs[8];
@ -75,60 +75,60 @@ relegated static const char *TinyStrSignal(int sig) {
return "???";
}
relegated static void ShowFunctionCalls(FILE *f, ucontext_t *ctx) {
relegated static void ShowFunctionCalls(int fd, ucontext_t *ctx) {
struct StackFrame *bp;
struct StackFrame goodframe;
fputc('\n', f);
write(fd, "\r\n", 2);
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;
showbacktrace(f, bp);
ShowBacktrace(fd, bp);
}
}
relegated static void DescribeCpuFlags(FILE *f, unsigned efl) {
size_t i;
relegated static void DescribeCpuFlags(int fd, unsigned flags) {
unsigned i;
char buf[64], *p;
p = buf;
for (i = 0; i < ARRAYLEN(kGodHatesFlags); ++i) {
if (efl & 1) {
fputc(' ', f);
fputc(kGodHatesFlags[i], f);
fputc('F', f);
if (flags & 1) {
*p++ = ' ';
*p++ = kGodHatesFlags[i];
*p++ = 'F';
}
efl >>= 1;
flags >>= 1;
}
(fprintf)(f, " %s%d\n", "IOPL", efl & 3);
p = stpcpy(p, " IOPL");
*p++ = '0' + (flags & 3);
write(fd, buf, p - buf);
}
relegated static void ShowGeneralRegisters(FILE *f, ucontext_t *ctx) {
relegated static void ShowGeneralRegisters(int fd, ucontext_t *ctx) {
size_t i, j, k;
long double st;
fputc('\n', f);
write(fd, "\r\n", 2);
for (i = 0, j = 0, k = 0; i < ARRAYLEN(kGregNames); ++i) {
if (j > 0) {
fputc(' ', f);
}
(fprintf)(f, "%-3s %016lx", kGregNames[(unsigned)kGregOrder[i]],
if (j > 0) write(fd, " ", 1);
(dprintf)(fd, "%-3s %016lx", kGregNames[(unsigned)kGregOrder[i]],
ctx->uc_mcontext.gregs[(unsigned)kGregOrder[i]]);
if (++j == 3) {
j = 0;
memcpy(&st, (char *)&ctx->fpustate.st[k], sizeof(st));
(fprintf)(f, " %s(%zu) %Lf", "ST", k, st);
(dprintf)(fd, " %s(%zu) %Lf", "ST", k, st);
++k;
fputc('\r', f);
fputc('\n', f);
write(fd, "\r\n", 2);
}
}
fflush(stderr);
DescribeCpuFlags(f, ctx->uc_mcontext.gregs[REG_EFL]);
DescribeCpuFlags(fd, ctx->uc_mcontext.gregs[REG_EFL]);
}
relegated static void ShowSseRegisters(FILE *f, ucontext_t *ctx) {
relegated static void ShowSseRegisters(int fd, ucontext_t *ctx) {
size_t i;
fputc('\n', f);
write(fd, "\r\n", 2);
for (i = 0; i < 8; ++i) {
(fprintf)(f, VEIL("r", "%s%-2zu %016lx%016lx %s%-2d %016lx%016lx\n"), "XMM",
i + 0, ctx->fpustate.xmm[i + 0].u64[0],
(dprintf)(fd, VEIL("r", "%s%-2zu %016lx%016lx %s%-2d %016lx%016lx\r\n"),
"XMM", i + 0, ctx->fpustate.xmm[i + 0].u64[0],
ctx->fpustate.xmm[i + 0].u64[1], "XMM", i + 8,
ctx->fpustate.xmm[i + 8].u64[0], ctx->fpustate.xmm[i + 8].u64[1]);
}
@ -149,23 +149,23 @@ relegated static void ShowMemoryMappings(int outfd) {
}
}
relegated static void ShowCrashReport(int err, FILE *f, int sig,
relegated static void ShowCrashReport(int err, int fd, int sig,
ucontext_t *ctx) {
struct utsname names;
(fprintf)(f, VEIL("r", "\n%serror%s: Uncaught SIG%s\n %s\n %s\n"), RED2,
RESET, TinyStrSignal(sig), getauxval(AT_EXECFN), strerror(err));
(dprintf)(fd, VEIL("r", "\r\n%serror%s: Uncaught SIG%s\r\n %s\r\n %s\r\n"),
RED2, RESET, TinyStrSignal(sig), getauxval(AT_EXECFN),
strerror(err));
if (uname(&names) != -1) {
(fprintf)(f, VEIL("r", " %s %s %s %s\n"), names.sysname, names.nodename,
(dprintf)(fd, VEIL("r", " %s %s %s %s\r\n"), names.sysname, names.nodename,
names.release, names.version);
}
ShowFunctionCalls(f, ctx);
ShowFunctionCalls(fd, ctx);
if (ctx) {
ShowGeneralRegisters(f, ctx);
ShowSseRegisters(f, ctx);
ShowGeneralRegisters(fd, ctx);
ShowSseRegisters(fd, ctx);
}
fputc('\n', f);
fflush(f);
ShowMemoryMappings(fileno(f));
write(fd, "\r\n", 2);
ShowMemoryMappings(fd);
}
relegated static void RestoreDefaultCrashSignalHandlers(void) {
@ -215,7 +215,7 @@ relegated void oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
: 0);
}
if (gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT)) return;
ShowCrashReport(err, stderr, sig, ctx);
quick_exit(128 + sig);
ShowCrashReport(err, STDERR_FILENO, sig, ctx);
exit(128 + sig);
unreachable;
}

View file

@ -17,12 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/log/internal.h"
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
void perror(const char *message) {
fprintf(stderr, "%s%s%s: %s: %m: %s\n", RED2, "error", RESET,
program_invocation_name, message);
fprintf(stderr, "%s%s%s: %s: %s: %s\r\n", RED2, "error", RESET,
program_invocation_name, strerror(errno), message);
}

View file

@ -56,7 +56,7 @@ void __ubsan_abort(const struct UbsanSourceLocation *loc,
g_runstate |= RUNSTATE_BROKEN;
if (IsDebuggerPresent(false)) DebugBreak();
startfatal(loc->file, loc->line);
fprintf(stderr, "%s\n", description);
fprintf(stderr, "%s\r\n", description);
die();
unreachable;
}
@ -86,7 +86,7 @@ void __ubsan_handle_type_mismatch(struct UbsanTypeMismatchInfo *type_mismatch,
type_mismatch->alignment);
} else {
description = __ubsan_buf;
snprintf(__ubsan_buf, sizeof(__ubsan_buf), "%s\n\t%s %s %p %s %s",
snprintf(__ubsan_buf, sizeof(__ubsan_buf), "%s\r\n\t%s %s %p %s %s",
"insufficient size", kind, "address", pointer,
"with insufficient space for object of type",
type_mismatch->type->name);

View file

@ -72,7 +72,7 @@ void vflogf_onfail(FILE *f) {
fseek(f, SEEK_SET, 0);
f->beg = f->end = 0;
clearerr(f);
(fprintf)(f, "performed emergency log truncation: %s\n", strerror(err));
(fprintf)(f, "performed emergency log truncation: %s\r\n", strerror(err));
}
}
@ -111,10 +111,10 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
}
(vfprintf)(f, fmt, va);
va_end(va);
fputc('\n', f);
fputs("\r\n", f);
if (level == kLogFatal) {
startfatal(file, line);
(fprintf)(stderr, "fatal error see logfile\n");
(fprintf)(stderr, "fatal error see logfile\r\n");
die();
unreachable;
}