mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Add C++ demangling to privileged runtime
Cosmo will now print C++ symbols correctly in --ftrace logs and backtraces. Doing this required reducing the memory requirement of the __demangle() function by 3x. This was accomplished using 16-bit indices and 16-bit malloc granularity. That puts a limit on the longest symbol we can successfully decode, which I think would be around 6553 characters long, given a 65536-byte buffer
This commit is contained in:
parent
dcd626edf8
commit
165c6b37e2
13 changed files with 727 additions and 284 deletions
67
examples/demangle.c
Normal file
67
examples/demangle.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include <assert.h>
|
||||
#include <cxxabi.h>
|
||||
#include <errno.h>
|
||||
#include <stdckdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/time.h"
|
||||
|
||||
void nothing(void) {
|
||||
}
|
||||
|
||||
void (*barrier)(void) = nothing;
|
||||
|
||||
struct timespec now(void) {
|
||||
struct timespec ts;
|
||||
timespec_get(&ts, TIME_UTC);
|
||||
return ts;
|
||||
}
|
||||
|
||||
long long tonanos(struct timespec x) {
|
||||
return x.tv_sec * 1000000000ll + x.tv_nsec;
|
||||
}
|
||||
|
||||
struct timespec tub(struct timespec a, struct timespec b) {
|
||||
a.tv_sec -= b.tv_sec;
|
||||
if (a.tv_nsec < b.tv_nsec) {
|
||||
a.tv_nsec += 1000000000;
|
||||
a.tv_sec--;
|
||||
}
|
||||
a.tv_nsec -= b.tv_nsec;
|
||||
return a;
|
||||
}
|
||||
|
||||
#define BENCH(ITERATIONS, WORK_PER_RUN, CODE) \
|
||||
do { \
|
||||
struct timespec start = now(); \
|
||||
for (int i = 0; i < ITERATIONS; ++i) { \
|
||||
barrier(); \
|
||||
CODE; \
|
||||
} \
|
||||
long long work = WORK_PER_RUN * ITERATIONS; \
|
||||
double nanos = (tonanos(tub(now(), start)) + work - 1) / (double)work; \
|
||||
printf("%10g ns %2dx %s\n", nanos, ITERATIONS, #CODE); \
|
||||
} while (0)
|
||||
|
||||
char got[5632];
|
||||
char huge[262144];
|
||||
|
||||
__static_yoink("PrintBacktraceUsingSymbols");
|
||||
__static_yoink("GetSymbolTable");
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ShowCrashReports();
|
||||
const char *sym =
|
||||
"_ZN12_GLOBAL__N_116tinyBLAS_Q0_AVX2ILi0E10block_q4_010block_"
|
||||
"q8_0fE4gemmILi3ELi2ELi0EEEvllll";
|
||||
__demangle(got, sym, sizeof(got));
|
||||
printf("%s\n", got);
|
||||
BENCH(1000, 1, __demangle(got, sym, sizeof(got)));
|
||||
}
|
|
@ -1136,10 +1136,7 @@ int __asan_print_trace(void *p) {
|
|||
kprintf(" (shadow not mapped?!)");
|
||||
}
|
||||
for (i = 0; i < ARRAYLEN(e->bt.p) && e->bt.p[i]; ++i) {
|
||||
kprintf("\n%*lx %s", 12, e->bt.p[i],
|
||||
_weaken(GetSymbolByAddr)
|
||||
? _weaken(GetSymbolByAddr)(e->bt.p[i])
|
||||
: "please __static_yoink(\"GetSymbolByAddr\")");
|
||||
kprintf("\n%*lx %t", 12, e->bt.p[i], e->bt.p[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "ape/sections.internal.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/divmod10.internal.h"
|
||||
|
@ -457,6 +458,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
|
|||
const char *abet;
|
||||
signed char type;
|
||||
const char *s, *f;
|
||||
char cxxbuf[2048];
|
||||
struct CosmoTib *tib;
|
||||
unsigned long long x;
|
||||
unsigned i, j, m, rem, sign, hash, cols, prec;
|
||||
|
@ -804,10 +806,12 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
|
|||
x = va_arg(va, intptr_t);
|
||||
if (_weaken(__symtab) && *_weaken(__symtab) &&
|
||||
(idx = _weaken(__get_symbol)(0, x)) != -1) {
|
||||
if (p + 1 <= e)
|
||||
*p++ = '&';
|
||||
/* if (p + 1 <= e) */
|
||||
/* *p++ = '&'; */
|
||||
s = (*_weaken(__symtab))->name_base +
|
||||
(*_weaken(__symtab))->names[idx];
|
||||
if (__is_mangled(s) && __demangle(cxxbuf, s, sizeof(cxxbuf)) != -1)
|
||||
s = cxxbuf;
|
||||
goto FormatString;
|
||||
}
|
||||
base = 4;
|
||||
|
|
|
@ -51,10 +51,9 @@ dontinstrument dontasan int PrintBacktraceUsingSymbols(
|
|||
size_t gi;
|
||||
intptr_t addr;
|
||||
const char *name;
|
||||
char cxxbuf[3000];
|
||||
int i, symbol, addend;
|
||||
static char cxxbuf[8192];
|
||||
struct Garbages *garbage;
|
||||
static pthread_spinlock_t lock;
|
||||
const struct StackFrame *frame;
|
||||
(void)gi;
|
||||
if (!bp)
|
||||
|
@ -92,10 +91,8 @@ dontinstrument dontasan int PrintBacktraceUsingSymbols(
|
|||
addend = 0;
|
||||
}
|
||||
if ((name = __get_symbol_name(st, symbol)) && __is_mangled(name)) {
|
||||
pthread_spin_lock(&lock);
|
||||
__demangle(cxxbuf, name, sizeof(cxxbuf));
|
||||
kprintf("%012lx %lx %s%+d\n", frame, addr, cxxbuf, addend);
|
||||
pthread_spin_unlock(&lock);
|
||||
name = cxxbuf;
|
||||
} else {
|
||||
kprintf("%012lx %lx %s%+d\n", frame, addr, name, addend);
|
||||
|
|
|
@ -8,10 +8,10 @@ void *bsearch_r(const void *, const void *, size_t, size_t,
|
|||
int (*)(const void *, const void *, void *), void *)
|
||||
paramsnonnull((1, 2, 5)) nosideeffect;
|
||||
void djbsort(int32_t *, size_t) libcesque;
|
||||
void qsort3(void *, size_t, size_t, int (*)(const void *, const void *))
|
||||
paramsnonnull();
|
||||
void qsort(void *, size_t, size_t, int (*)(const void *, const void *))
|
||||
paramsnonnull();
|
||||
void qsort3(void *, size_t, size_t,
|
||||
int (*)(const void *, const void *)) libcesque paramsnonnull();
|
||||
void qsort(void *, size_t, size_t,
|
||||
int (*)(const void *, const void *)) libcesque paramsnonnull();
|
||||
void qsort_r(void *, size_t, size_t,
|
||||
int (*)(const void *, const void *, void *), void *)
|
||||
paramsnonnull((1, 4));
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
for i, c in enumerate("Asia/Kolkata"):
|
||||
print("buf[%d] = '%c';" % (i, c))
|
|
@ -21,6 +21,12 @@
|
|||
|
||||
void __funcs_on_quick_exit(void);
|
||||
|
||||
/**
|
||||
* Exits process faster.
|
||||
*
|
||||
* @param exitcode is masked with 255
|
||||
* @noreturn
|
||||
*/
|
||||
wontreturn void quick_exit(int code) {
|
||||
if (_weaken(__funcs_on_quick_exit))
|
||||
_weaken(__funcs_on_quick_exit)();
|
||||
|
|
|
@ -100,6 +100,10 @@ $(LIBC_STR_A_OBJS): private \
|
|||
-Wframe-larger-than=4096 \
|
||||
-Walloca-larger-than=4096
|
||||
|
||||
o/$(MODE)/libc/str/demangle.o: private \
|
||||
OVERRIDE_CFLAGS += \
|
||||
-ffreestanding
|
||||
|
||||
LIBC_STR_LIBS = $(foreach x,$(LIBC_STR_ARTIFACTS),$($(x)))
|
||||
LIBC_STR_SRCS = $(foreach x,$(LIBC_STR_ARTIFACTS),$($(x)_SRCS))
|
||||
LIBC_STR_HDRS = $(foreach x,$(LIBC_STR_ARTIFACTS),$($(x)_HDRS))
|
||||
|
|
|
@ -215,9 +215,9 @@ TEST(ksnprintf, testSymbols) {
|
|||
hassymbols = GetSymbolTable();
|
||||
ksnprintf(b[0], 32, "%t", strlen);
|
||||
if (hassymbols) {
|
||||
ASSERT_STREQ("&strlen", b[0]);
|
||||
ASSERT_STREQ("strlen", b[0]);
|
||||
} else {
|
||||
ksnprintf(b[1], 32, "&%lx", strlen);
|
||||
ksnprintf(b[1], 32, "%lx", strlen);
|
||||
ASSERT_STREQ(b[1], b[0]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ TEST_LIBCXX_DIRECTDEPS = \
|
|||
LIBC_CALLS \
|
||||
LIBC_INTRIN \
|
||||
LIBC_LOG \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_STDIO \
|
||||
|
@ -22,6 +23,8 @@ TEST_LIBCXX_DIRECTDEPS = \
|
|||
LIBC_THREAD \
|
||||
LIBC_TINYMATH \
|
||||
THIRD_PARTY_LIBCXX \
|
||||
THIRD_PARTY_LIBCXXABI \
|
||||
THIRD_PARTY_LIBUNWIND \
|
||||
THIRD_PARTY_OPENMP
|
||||
|
||||
TEST_LIBCXX_DEPS := \
|
||||
|
@ -39,7 +42,7 @@ o/$(MODE)/test/libcxx/%.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
$(TEST_LIBCXX_OBJS): private CCFLAGS += -fexceptions -frtti
|
||||
$(TEST_LIBCXX_OBJS): private OVERRIDE_CXXFLAGS += -fexceptions -frtti
|
||||
|
||||
o/$(MODE)/test/libcxx/openmp_test.o: private CXXFLAGS += -fopenmp
|
||||
o/$(MODE)/test/libcxx/openmp_test.runs: private QUOTA += -C100
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
/*-*-mode:c++;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8-*-│
|
||||
│ vi: set et ft=c++ ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ Copyright 2024 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 │
|
||||
|
@ -16,26 +16,65 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/mem/alg.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
||||
/**
|
||||
* Exits process faster.
|
||||
*
|
||||
* @param exitcode is masked with 255
|
||||
* @noreturn
|
||||
*/
|
||||
wontreturn void quick_exit(int exitcode) {
|
||||
const uintptr_t *p;
|
||||
STRACE("quick_exit(%d)", exitcode);
|
||||
if (_weaken(fflush)) {
|
||||
_weaken(fflush)(0);
|
||||
// this dontthrow keyword SHOULD break this test. it's probably passing
|
||||
// because we're currently using SjLj exceptions. the day we can change
|
||||
// things, remove `dontthrow` and this test will still be a useful help
|
||||
extern "C" dontthrow void qsort_(void *, size_t, size_t,
|
||||
int (*)(const void *,
|
||||
const void *)) asm("qsort");
|
||||
|
||||
struct Resource {
|
||||
char *p;
|
||||
Resource() {
|
||||
p = new char;
|
||||
}
|
||||
for (p = __fini_array_end; p > __fini_array_start;) {
|
||||
((void (*)(void))(*--p))();
|
||||
~Resource() {
|
||||
delete p;
|
||||
}
|
||||
_Exit(exitcode);
|
||||
};
|
||||
|
||||
void Poke(char *p) {
|
||||
*p = 1;
|
||||
}
|
||||
void (*pPoke)(char *) = Poke;
|
||||
|
||||
void Throw(void) {
|
||||
throw 42;
|
||||
}
|
||||
void (*pThrow)(void) = Throw;
|
||||
|
||||
int cmp(const void *x, const void *y) {
|
||||
Resource r;
|
||||
pPoke(r.p);
|
||||
if (*r.p)
|
||||
pThrow();
|
||||
exit(5);
|
||||
}
|
||||
|
||||
int A[3] = {3, 2, 1};
|
||||
int Work(void) {
|
||||
Resource r;
|
||||
pPoke(r.p);
|
||||
qsort_(A, 3, sizeof(int), cmp);
|
||||
return A[0];
|
||||
}
|
||||
int (*pWork)(void) = Work;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
Resource r;
|
||||
if (pWork() != 1)
|
||||
return 1;
|
||||
pPoke(r.p);
|
||||
if (r.p)
|
||||
return 2;
|
||||
} catch (int e) {
|
||||
if (e != 42)
|
||||
return 3;
|
||||
}
|
||||
CheckForMemoryLeaks();
|
||||
}
|
|
@ -45,7 +45,8 @@
|
|||
'("__builtin_va_list"))
|
||||
|
||||
(gcc-builtin-functions
|
||||
'("__builtin_va_start"
|
||||
'("__builtin_strlen"
|
||||
"__builtin_va_start"
|
||||
"__builtin_va_arg"
|
||||
"__builtin_va_end"
|
||||
"__builtin_add_overflow"
|
||||
|
|
Loading…
Reference in a new issue