diff options
Diffstat (limited to 'vespamalloc/src/vespamalloc/util/callstack.cpp')
-rw-r--r-- | vespamalloc/src/vespamalloc/util/callstack.cpp | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/vespamalloc/src/vespamalloc/util/callstack.cpp b/vespamalloc/src/vespamalloc/util/callstack.cpp index b8449c89a72..56b634bca33 100644 --- a/vespamalloc/src/vespamalloc/util/callstack.cpp +++ b/vespamalloc/src/vespamalloc/util/callstack.cpp @@ -1,39 +1,59 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <dlfcn.h> -#include <ctype.h> +#include <cctype> #include <vespamalloc/util/callstack.h> +#include <string> +#include <cxxabi.h> namespace vespamalloc { -const char * -dlAddr(const void * func) { - static const char * _unknown = "UNKNOWN"; - const char * funcName = _unknown; +namespace { + +std::string +demangle(const char *native) { + int status = 0; + size_t size = 0; + char *unmangled = abi::__cxa_demangle(native, nullptr, &size, &status); + if (unmangled == nullptr) { + return ""; // Demangling failed for some reason. TODO return `native` instead? + } + std::string result(unmangled); + free(unmangled); + return result; +} + + +std::string +dlAddr(const void *func) { + static std::string _unknown = "UNKNOWN"; Dl_info info; int ret = dladdr(func, &info); if (ret != 0) { - funcName = info.dli_sname; + return demangle(info.dli_sname); } - return funcName; + return _unknown; +} + } namespace { void verifyAndCopy(const void *addr, char *v, size_t sz) { size_t pos(0); - const char *sym = dlAddr(addr); - for (; sym && (sym[pos] != '\0') && (pos < sz - 1); pos++) { + std::string sym = dlAddr(addr); + for (; (pos < sym.size()) && (pos < sz - 1); pos++) { char c(sym[pos]); v[pos] = isprint(c) ? c : '.'; } v[pos] = '\0'; } + } void StackReturnEntry::info(FILE * os) const { - static char tmp[0x400]; + char tmp[0x400]; verifyAndCopy(_return, tmp, sizeof(tmp)); fprintf(os, "%s(%p)", tmp, _return); } @@ -41,8 +61,8 @@ StackReturnEntry::info(FILE * os) const asciistream & operator << (asciistream & os, const StackReturnEntry & v) { - static char tmp[0x100]; - static char t[0x200]; + char tmp[0x100]; + char t[0x200]; verifyAndCopy(v._return, tmp, sizeof(tmp)); snprintf(t, sizeof(t), "%s(%p)", tmp, v._return); return os << t; |