2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-07-30 01:44:15 +00:00
|
|
|
#include "tool/build/lib/demangle.h"
|
2020-10-06 06:11:49 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-10-06 06:11:49 +00:00
|
|
|
#include "libc/calls/struct/iovec.h"
|
2022-08-19 00:41:32 +00:00
|
|
|
#include "libc/intrin/safemacros.h"
|
2023-09-06 10:54:42 +00:00
|
|
|
#include "libc/limits.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
struct CxxFilt {
|
|
|
|
int pid;
|
2021-01-25 21:08:05 +00:00
|
|
|
int reader;
|
|
|
|
int writer;
|
2020-09-28 08:13:56 +00:00
|
|
|
} g_cxxfilt;
|
|
|
|
|
|
|
|
void CloseCxxFilt(void) {
|
2021-01-25 21:08:05 +00:00
|
|
|
close(g_cxxfilt.reader);
|
|
|
|
close(g_cxxfilt.writer);
|
2020-09-28 08:13:56 +00:00
|
|
|
g_cxxfilt.pid = -1;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 08:13:56 +00:00
|
|
|
void SpawnCxxFilt(void) {
|
2021-01-25 21:08:05 +00:00
|
|
|
int pipefds[2][2];
|
2020-10-06 06:11:49 +00:00
|
|
|
const char *cxxfilt;
|
2022-04-28 16:42:36 +00:00
|
|
|
char path[PATH_MAX];
|
2021-01-25 21:08:05 +00:00
|
|
|
cxxfilt = firstnonnull(emptytonull(getenv("CXXFILT")), "c++filt");
|
2022-04-24 16:59:22 +00:00
|
|
|
if (commandv(cxxfilt, path, sizeof(path))) {
|
2021-01-25 21:08:05 +00:00
|
|
|
pipe2(pipefds[0], O_CLOEXEC);
|
|
|
|
pipe2(pipefds[1], O_CLOEXEC);
|
|
|
|
if (!(g_cxxfilt.pid = vfork())) {
|
|
|
|
dup2(pipefds[1][0], 0);
|
|
|
|
dup2(pipefds[0][1], 1);
|
2023-09-02 03:49:13 +00:00
|
|
|
execv(path, (char *const[]){(char *)cxxfilt, 0});
|
2021-01-25 21:08:05 +00:00
|
|
|
abort();
|
2020-09-28 08:13:56 +00:00
|
|
|
}
|
2021-01-25 21:08:05 +00:00
|
|
|
g_cxxfilt.reader = pipefds[0][0];
|
|
|
|
g_cxxfilt.writer = pipefds[1][1];
|
|
|
|
atexit(CloseCxxFilt);
|
2020-09-28 08:13:56 +00:00
|
|
|
} else {
|
2021-01-25 21:08:05 +00:00
|
|
|
g_cxxfilt.pid = -1;
|
2020-09-28 08:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 06:11:49 +00:00
|
|
|
char *CopySymbol(char *p, size_t pn, const char *s, size_t sn) {
|
|
|
|
size_t extra;
|
|
|
|
bool showdots, iscomplicated;
|
|
|
|
assert(pn >= 1 + 3 + 1 + 1);
|
|
|
|
iscomplicated = memchr(s, ' ', sn) || memchr(s, '(', sn);
|
|
|
|
extra = 1;
|
|
|
|
if (iscomplicated)
|
|
|
|
extra += 2;
|
|
|
|
if (sn + extra > pn) {
|
|
|
|
sn = pn - extra - 3;
|
|
|
|
showdots = true;
|
|
|
|
} else {
|
|
|
|
showdots = false;
|
|
|
|
}
|
|
|
|
if (iscomplicated)
|
|
|
|
*p++ = '"';
|
|
|
|
p = mempcpy(p, s, sn);
|
|
|
|
if (showdots)
|
|
|
|
p = stpcpy(p, "...");
|
|
|
|
if (iscomplicated)
|
|
|
|
*p++ = '"';
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *DemangleCxxFilt(char *p, size_t pn, const char *s, size_t sn) {
|
|
|
|
ssize_t rc;
|
|
|
|
size_t got;
|
|
|
|
struct iovec iov[2];
|
2023-07-30 01:44:15 +00:00
|
|
|
static char buf[4096];
|
2020-09-28 08:13:56 +00:00
|
|
|
if (!g_cxxfilt.pid)
|
|
|
|
SpawnCxxFilt();
|
|
|
|
if (g_cxxfilt.pid == -1)
|
|
|
|
return NULL;
|
2020-10-06 06:11:49 +00:00
|
|
|
buf[0] = '\n';
|
2023-09-02 03:49:13 +00:00
|
|
|
iov[0].iov_base = (void *)s;
|
2020-10-06 06:11:49 +00:00
|
|
|
iov[0].iov_len = sn;
|
|
|
|
iov[1].iov_base = buf;
|
|
|
|
iov[1].iov_len = 1;
|
2021-01-25 21:08:05 +00:00
|
|
|
writev(g_cxxfilt.writer, iov, ARRAYLEN(iov));
|
|
|
|
if ((rc = read(g_cxxfilt.reader, buf, sizeof(buf))) != -1) {
|
2020-10-06 06:11:49 +00:00
|
|
|
got = rc;
|
|
|
|
if (got >= 2 && buf[got - 1] == '\n') {
|
|
|
|
if (buf[got - 2] == '\r')
|
|
|
|
--got;
|
|
|
|
--got;
|
|
|
|
return CopySymbol(p, pn, buf, got);
|
|
|
|
}
|
2020-09-28 08:13:56 +00:00
|
|
|
}
|
2020-10-06 06:11:49 +00:00
|
|
|
CloseCxxFilt();
|
|
|
|
return NULL;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-28 08:13:56 +00:00
|
|
|
* Decrypts C++ symbol.
|
2020-06-15 14:18:57 +00:00
|
|
|
*
|
2020-09-28 08:13:56 +00:00
|
|
|
* Decoding these takes roughly the same lines of code as an entire
|
|
|
|
* x86_64 disassembler. That's just for the GNU encoding scheme. So
|
|
|
|
* what we'll do, is just offload this work to the c++filt program.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
2020-10-06 06:11:49 +00:00
|
|
|
char *Demangle(char *p, const char *symbol, size_t n) {
|
2020-09-28 08:13:56 +00:00
|
|
|
char *r;
|
2020-10-06 06:11:49 +00:00
|
|
|
size_t sn;
|
|
|
|
sn = strlen(symbol);
|
2023-08-14 03:31:27 +00:00
|
|
|
if (startswith(symbol, "_Z")) {
|
2020-10-06 06:11:49 +00:00
|
|
|
if ((r = DemangleCxxFilt(p, n, symbol, sn)))
|
|
|
|
return r;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2020-10-06 06:11:49 +00:00
|
|
|
return CopySymbol(p, n, symbol, sn);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|