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:
Justine Tunney 2024-06-01 19:57:32 -07:00
parent dcd626edf8
commit 165c6b37e2
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
13 changed files with 727 additions and 284 deletions

View file

@ -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;