Add minor improvements and cleanup

This commit is contained in:
Justine Tunney 2020-10-27 03:39:46 -07:00
parent 9e3e985ae5
commit feed0d2b0e
163 changed files with 2286 additions and 2245 deletions

View file

@ -22,9 +22,11 @@
#include "libc/conv/itoa.h"
#include "libc/log/asan.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/mem/hook/hook.h"
#include "libc/runtime/directmap.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
@ -144,16 +146,23 @@ static const char *__asan_describe_access_poison(int c) {
static noreturn void __asan_die(const char *msg, size_t size) {
write(STDERR_FILENO, msg, size);
PrintBacktraceUsingSymbols(STDERR_FILENO, __builtin_frame_address(0),
GetSymbolTable());
DebugBreak();
_Exit(66);
die();
}
static char *__asan_report_start(char *p) {
bool ansi;
const char *term;
term = getenv("TERM");
ansi = !term || strcmp(term, "dumb") != 0;
if (ansi) p = stpcpy(p, "\r\e[J\e[1;91m");
p = stpcpy(p, "asan error");
if (ansi) p = stpcpy(p, "\e[0m");
return stpcpy(p, ": ");
}
static noreturn void __asan_report_deallocate_fault(void *addr, int c) {
char *p, ibuf[21], buf[256];
p = buf;
p = stpcpy(p, "error: ");
p = __asan_report_start(buf);
p = stpcpy(p, __asan_dscribe_free_poison(c));
p = stpcpy(p, " ");
p = mempcpy(p, ibuf, int64toarray_radix10(c, ibuf));
@ -166,8 +175,7 @@ static noreturn void __asan_report_deallocate_fault(void *addr, int c) {
static noreturn void __asan_report_memory_fault(uint8_t *addr, int size,
const char *kind) {
char *p, ibuf[21], buf[256];
p = buf;
p = stpcpy(p, "error: ");
p = __asan_report_start(buf);
p = stpcpy(p, __asan_describe_access_poison(*(char *)SHADOW((intptr_t)addr)));
p = stpcpy(p, " ");
p = mempcpy(p, ibuf, uint64toarray_radix10(size, ibuf));

View file

@ -19,17 +19,19 @@
*/
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/log/check.h"
#include "libc/log/color.h"
#include "libc/log/internal.h"
#include "libc/log/log.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"
STATIC_YOINK("ntoa");
STATIC_YOINK("stoa");
@ -46,12 +48,12 @@ relegated void __check_fail(const char *suffix, const char *opstr,
va_list va;
char sufbuf[8];
int lasterr = errno;
startfatal(file, line);
__start_fatal(file, line);
if (!memccpy(sufbuf, suffix, '\0', sizeof(sufbuf))) strcpy(sufbuf, "?");
strtoupper(sufbuf);
(fprintf)(stderr,
(dprintf)(STDERR_FILENO,
"check failed\r\n"
"\tCHECK_%s(%s, %s);\r\n"
"\t\t → %#lx (%s)\r\n"
@ -59,27 +61,26 @@ relegated void __check_fail(const char *suffix, const char *opstr,
sufbuf, wantstr, gotstr, want, wantstr, opstr, got, gotstr);
if (!isempty(fmt)) {
fputc('\t', stderr);
(dprintf)(STDERR_FILENO, "\t");
va_start(va, fmt);
(vfprintf)(stderr, fmt, va);
(vdprintf)(STDERR_FILENO, fmt, va);
va_end(va);
fputs("\r\n", stderr);
(dprintf)(STDERR_FILENO, "\r\n");
}
(fprintf)(stderr, "\t%s\r\n\t%s%s%s%s\r\n", strerror(lasterr), SUBTLE,
(dprintf)(STDERR_FILENO, "\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\r\n", g_argv[i], i < g_argc - 1 ? " \\" : "");
(dprintf)(STDERR_FILENO, "\t\t%s%s\r\n", g_argv[i],
i < g_argc - 1 ? " \\" : "");
}
if (!IsTiny() && lasterr == ENOMEM) {
(fprintf)(stderr, "\r\n");
fflush(stderr);
PrintMemoryIntervals(fileno(stderr), &_mmi);
(dprintf)(STDERR_FILENO, "\r\n");
PrintMemoryIntervals(STDERR_FILENO, &_mmi);
}
fflush(stderr);
die();
unreachable;
}

View file

@ -38,7 +38,7 @@ relegated void ___check_fail_ndebug(uint64_t want, uint64_t got,
char bx[21];
int lasterr;
lasterr = errno;
startfatal_ndebug();
__start_fatal_ndebug();
__print_string("check failed: 0x");
__print(bx, uint64toarray_radix16(want, bx));
__print_string(" ");

20
libc/log/color.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef COSMOPOLITAN_LIBC_LOG_COLOR_H_
#define COSMOPOLITAN_LIBC_LOG_COLOR_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define CLS (cancolor() ? "\r\e[J" : "")
#define RED (cancolor() ? "\e[30;101m" : "")
#define GREEN (cancolor() ? "\e[32m" : "")
#define UNBOLD (cancolor() ? "\e[22m" : "")
#define RED2 (cancolor() ? "\e[91;1m" : "")
#define BLUE1 (cancolor() ? "\e[94;49m" : "")
#define BLUE2 (cancolor() ? "\e[34m" : "")
#define RESET (cancolor() ? "\e[0m" : "")
#define SUBTLE (cancolor() ? "\e[35m" : "")
bool cancolor(void) nothrow nocallback nosideeffect;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LOG_COLOR_H_ */

View file

@ -1,33 +1,17 @@
#ifndef COSMOPOLITAN_LIBC_LOG_INTERNAL_H_
#define COSMOPOLITAN_LIBC_LOG_INTERNAL_H_
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/log/log.h"
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/ucontext.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define RED (cancolor() ? "\x1b[30;101m" : "")
#define UNBOLD (cancolor() ? "\x1b[22m" : "")
#define RED2 (cancolor() ? "\x1b[91;1m" : "")
#define BLUE1 (cancolor() ? "\x1b[94;49m" : "")
#define BLUE2 (cancolor() ? "\x1b[34m" : "")
#define RESET (cancolor() ? "\x1b[0m" : "")
#define SUBTLE (cancolor() ? "\x1b[35m" : "")
enum NtExceptionHandlerActions;
struct NtExceptionPointers;
extern int kCrashSigs[8];
extern struct sigaction g_oldcrashacts[8];
extern const char kCrashSigNames[8][5] aligned(1);
extern const char kGregNames[17][4] aligned(1);
extern const char kGregOrder[17] aligned(1);
void startfatal(const char *, int) hidden;
void startfatal_ndebug(void) hidden;
void oncrash(int, struct siginfo *, struct ucontext *) relegated;
enum NtExceptionHandlerActions wincrash$nt(
const struct NtExceptionPointers *) relegated;
void __start_fatal(const char *, int) hidden;
void __start_fatal_ndebug(void) hidden;
void __oncrash(int, struct siginfo *, struct ucontext *) relegated;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -39,7 +39,6 @@ void meminfo(int); /* shows malloc statistics &c. */
void memsummary(int); /* light version of same thing */
uint16_t getttycols(uint16_t);
int getttysize(int, struct winsize *) paramsnonnull();
bool cancolor(void) nothrow nocallback;
bool isterminalinarticulate(void) nosideeffect;
char *commandvenv(const char *, const char *) nodiscard;
const char *GetAddr2linePath(void);

View file

@ -32,7 +32,6 @@ LIBC_LOG_A_DIRECTDEPS = \
LIBC_CALLS_HEFTY \
LIBC_CONV \
LIBC_ELF \
LIBC_ESCAPE \
LIBC_FMT \
LIBC_TINYMATH \
LIBC_NEXGEN32E \

View file

@ -24,6 +24,7 @@
#include "libc/dce.h"
#include "libc/fmt/fmt.h"
#include "libc/log/backtrace.h"
#include "libc/log/color.h"
#include "libc/log/gdb.h"
#include "libc/log/internal.h"
#include "libc/log/log.h"
@ -49,18 +50,18 @@ STATIC_YOINK("stoa");
struct siginfo;
const char kGregOrder[17] aligned(1) = {
static const char kGregOrder[17] aligned(1) = {
13, 11, 8, 14, 12, 9, 10, 15, 16, 0, 1, 2, 3, 4, 5, 6, 7,
};
const char kGregNames[17][4] aligned(1) = {
static const char kGregNames[17][4] aligned(1) = {
"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", "RDI",
"RSI", "RBP", "RBX", "RDX", "RAX", "RCX", "RSP", "RIP",
};
const char kGodHatesFlags[12] aligned(1) = "CVPRAKZSTIDO";
const char kCrashSigNames[8][5] aligned(1) = {"QUIT", "FPE", "ILL", "SEGV",
"TRAP", "ABRT", "BUS"};
static const char kGodHatesFlags[12] aligned(1) = "CVPRAKZSTIDO";
static const char kCrashSigNames[8][5] aligned(1) = {
"QUIT", "FPE", "ILL", "SEGV", "TRAP", "ABRT", "BUS"};
int kCrashSigs[8];
struct sigaction g_oldcrashacts[8];
@ -191,7 +192,7 @@ relegated static void RestoreDefaultCrashSignalHandlers(void) {
*
* This function never returns, except for traps w/ human supervision.
*/
relegated void oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
intptr_t rip;
int gdbpid, err;
static bool once;

View file

@ -24,69 +24,69 @@
/ caused the crash, particularly in the GDB GUI. They're coded
/ into an array to pinch pennies on code size registering them.
kOncrashThunks:
__oncrash_thunks:
.org 11*0
oncrash_sigquit:
__oncrash_sigquit:
push %rbp
mov %rsp,%rbp
call oncrash
call __oncrash
pop %rbp
ret
.endfn oncrash_sigquit,globl
.endfn __oncrash_sigquit,globl
.org 11*1
oncrash_sigfpe:
__oncrash_sigfpe:
push %rbp
mov %rsp,%rbp
call oncrash
call __oncrash
pop %rbp
ret
.endfn oncrash_sigfpe,globl
.endfn __oncrash_sigfpe,globl
.org 11*2
oncrash_sigill:
__oncrash_sigill:
push %rbp
mov %rsp,%rbp
call oncrash
call __oncrash
pop %rbp
ret
.endfn oncrash_sigill,globl
.endfn __oncrash_sigill,globl
.org 11*3
oncrash_sigsegv:
__oncrash_sigsegv:
push %rbp
mov %rsp,%rbp
call oncrash
call __oncrash
pop %rbp
ret
.endfn oncrash_sigsegv,globl
.endfn __oncrash_sigsegv,globl
.org 11*4
oncrash_sigtrap:
__oncrash_sigtrap:
push %rbp
mov %rsp,%rbp
call oncrash
call __oncrash
pop %rbp
ret
.endfn oncrash_sigtrap,globl
.endfn __oncrash_sigtrap,globl
.org 11*5
oncrash_sigabrt:
__oncrash_sigabrt:
push %rbp
mov %rsp,%rbp
call oncrash
call __oncrash
pop %rbp
ret
.endfn oncrash_sigabrt,globl
.endfn __oncrash_sigabrt,globl
.org 11*6
oncrash_sigbus:
__oncrash_sigbus:
push %rbp
mov %rsp,%rbp
call oncrash
call __oncrash
pop %rbp
ret
.endfn oncrash_sigbus,globl
.endfn __oncrash_sigbus,globl
.endobj kOncrashThunks,globl
.endobj __oncrash_thunks,globl

View file

@ -19,6 +19,7 @@
*/
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/log/color.h"
#include "libc/log/internal.h"
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"

View file

@ -34,7 +34,7 @@
STATIC_YOINK("die");
STATIC_YOINK("__ubsan_abort");
extern const unsigned char kOncrashThunks[7][11];
extern const unsigned char __oncrash_thunks[7][11];
/**
* Installs crash signal handlers.
@ -73,7 +73,7 @@ void showcrashreports(void) {
}
for (i = 0; i < ARRAYLEN(kCrashSigs); ++i) {
if (kCrashSigs[i]) {
sa.sa_sigaction = (sigaction_f)kOncrashThunks[i];
sa.sa_sigaction = (sigaction_f)__oncrash_thunks[i];
sigaction(kCrashSigs[i], &sa, &g_oldcrashacts[i]);
}
}

View file

@ -17,18 +17,19 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/calls.h"
#include "libc/log/color.h"
#include "libc/log/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
/**
* Prints initial part of fatal message.
*
* @note this is support code for __check_fail(), __assert_fail(), etc.
* @see startfatal_ndebug()
* @see __start_fatal_ndebug()
*/
relegated void startfatal(const char *file, int line) {
fflush(stdout);
fprintf(stderr, "%s%s%s:%s:%d:%s%s: ", RED, "error", BLUE1, file, line,
program_invocation_short_name, RESET);
relegated void __start_fatal(const char *file, int line) {
dprintf(STDERR_FILENO, "%s%s%s%s:%s:%d:%s%s: ", CLS, RED, "error", BLUE1,
file, line, program_invocation_short_name, RESET);
}

View file

@ -17,6 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/log/color.h"
#include "libc/runtime/missioncritical.h"
#include "libc/stdio/stdio.h"
@ -24,11 +25,10 @@
* Prints initial part of fatal message.
*
* @note this is support code for __check_fail(), __assert_fail(), etc.
* @see startfatal()
* @see __start_fatal()
*/
relegated void startfatal_ndebug(void) {
fflush(stdout);
fflush(stderr);
relegated void __start_fatal_ndebug(void) {
if (cancolor()) __print_string("\r\e[J");
__print_string("error:");
__print_string(program_invocation_name);
__print_string(": ");

View file

@ -55,7 +55,7 @@ void __ubsan_abort(const struct UbsanSourceLocation *loc,
}
g_runstate |= RUNSTATE_BROKEN;
if (IsDebuggerPresent(false)) DebugBreak();
startfatal(loc->file, loc->line);
__start_fatal(loc->file, loc->line);
fprintf(stderr, "%s\r\n", description);
die();
unreachable;
@ -74,7 +74,7 @@ void __ubsan_handle_type_mismatch(struct UbsanTypeMismatchInfo *type_mismatch,
uintptr_t pointer) {
struct UbsanSourceLocation *loc = &type_mismatch->location;
const char *description;
const char *kind = indexdoublenulstring(kUbsanTypeCheckKinds,
const char *kind = IndexDoubleNulString(kUbsanTypeCheckKinds,
type_mismatch->type_check_kind);
if (pointer == 0) {
description = "null pointer access";

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/log/bsd.h"
#include "libc/log/color.h"
#include "libc/log/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/log/bsd.h"
#include "libc/log/color.h"
#include "libc/log/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"

View file

@ -32,6 +32,7 @@
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/time/struct/tm.h"
#include "libc/time/time.h"
@ -113,8 +114,8 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
va_end(va);
fputs("\r\n", f);
if (level == kLogFatal) {
startfatal(file, line);
(fprintf)(stderr, "fatal error see logfile\r\n");
__start_fatal(file, line);
(dprintf)(STDERR_FILENO, "fatal error see logfile\r\n");
die();
unreachable;
}

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/log/bsd.h"
#include "libc/log/color.h"
#include "libc/log/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/log/bsd.h"
#include "libc/log/color.h"
#include "libc/log/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"