[sanitizer] demangling of libbacktrace symbolization
Jakub Jelinek
jakub at redhat.com
Fri Jan 10 02:23:04 PST 2014
Hi!
As written in http://gcc.gnu.org/ml/gcc-patches/2013-12/msg00964.html ,
I'd like to submit following small changes to the libbacktrace
symbolization. Apparently llvm-symbolizer symbolization demangles symbols
by default, libbacktrace symbolization didn't and this patch fixes that.
It is using cp-demangle for that which doesn't use
malloc/calloc/realloc/free when called from libsanitizer.
--- libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.h.jj 2013-12-05 12:04:28.000000000 +0100
+++ libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.h 2013-12-10 11:01:26.777371566 +0100
@@ -29,6 +29,8 @@ class LibbacktraceSymbolizer {
bool SymbolizeData(DataInfo *info);
+ const char *Demangle(const char *name);
+
private:
explicit LibbacktraceSymbolizer(void *state) : state_(state) {}
--- libsanitizer/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc.jj 2013-12-05 12:04:28.000000000 +0100
+++ libsanitizer/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc 2013-12-10 11:03:02.971876505 +0100
@@ -513,6 +513,11 @@ class POSIXSymbolizer : public Symbolize
SymbolizerScope sym_scope(this);
if (internal_symbolizer_ != 0)
return internal_symbolizer_->Demangle(name);
+ if (libbacktrace_symbolizer_ != 0) {
+ const char *demangled = libbacktrace_symbolizer_->Demangle(name);
+ if (demangled)
+ return demangled;
+ }
return DemangleCXXABI(name);
}
--- libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.cc.jj 2013-12-09 14:32:06.000000000 +0100
+++ libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.cc 2013-12-10 11:48:19.803830291 +0100
@@ -20,6 +20,10 @@
# include "backtrace-supported.h"
# if SANITIZER_POSIX && BACKTRACE_SUPPORTED && !BACKTRACE_USES_MALLOC
# include "backtrace.h"
+# if SANITIZER_CP_DEMANGLE
+# undef ARRAY_SIZE
+# include "demangle.h"
+# endif
# else
# define SANITIZER_LIBBACKTRACE 0
# endif
@@ -31,6 +35,60 @@ namespace __sanitizer {
namespace {
+#if SANITIZER_CP_DEMANGLE
+struct CplusV3DemangleData {
+ char *buf;
+ uptr size, allocated;
+};
+
+extern "C" {
+static void CplusV3DemangleCallback(const char *s, size_t l, void *vdata) {
+ CplusV3DemangleData *data = (CplusV3DemangleData *)vdata;
+ uptr needed = data->size + l + 1;
+ if (needed > data->allocated) {
+ data->allocated *= 2;
+ if (needed > data->allocated)
+ data->allocated = needed;
+ char *buf = (char *)InternalAlloc(data->allocated);
+ if (data->buf) {
+ internal_memcpy(buf, data->buf, data->size);
+ InternalFree(data->buf);
+ }
+ data->buf = buf;
+ }
+ internal_memcpy(data->buf + data->size, s, l);
+ data->buf[data->size + l] = '\0';
+ data->size += l;
+}
+} // extern "C"
+
+char *CplusV3Demangle(const char *name, bool always_alloc) {
+ CplusV3DemangleData data;
+ data.buf = 0;
+ data.size = 0;
+ data.allocated = 0;
+ if (cplus_demangle_v3_callback(name, DMGL_PARAMS | DMGL_ANSI,
+ CplusV3DemangleCallback, &data)) {
+ if (data.size + 64 > data.allocated)
+ return data.buf;
+ char *buf = internal_strdup(data.buf);
+ InternalFree(data.buf);
+ return buf;
+ }
+ if (data.buf)
+ InternalFree(data.buf);
+ if (always_alloc)
+ return internal_strdup(name);
+ return 0;
+}
+#else
+const char *CplusV3Demangle(const char *name, bool always_alloc) {
+ if (always_alloc)
+ return internal_strdup(name);
+ return 0;
+}
+#endif
+
struct SymbolizeCodeData {
AddressInfo *frames;
uptr n_frames;
@@ -49,7 +107,7 @@ static int SymbolizeCodePCInfoCallback(v
info->Clear();
info->FillAddressAndModuleInfo(addr, cdata->module_name,
cdata->module_offset);
- info->function = internal_strdup(function);
+ info->function = CplusV3Demangle(function, true);
if (filename)
info->file = internal_strdup(filename);
info->line = lineno;
@@ -67,7 +125,7 @@ static void SymbolizeCodeCallback(void *
info->Clear();
info->FillAddressAndModuleInfo(addr, cdata->module_name,
cdata->module_offset);
- info->function = internal_strdup(symname);
+ info->function = CplusV3Demangle(symname, true);
cdata->n_frames = 1;
}
}
@@ -76,7 +134,7 @@ static void SymbolizeDataCallback(void *
uintptr_t symval, uintptr_t symsize) {
DataInfo *info = (DataInfo *)vdata;
if (symname && symval) {
- info->name = internal_strdup(symname);
+ info->name = CplusV3Demangle(symname, true);
info->start = symval;
info->size = symsize;
}
@@ -121,6 +179,17 @@ bool LibbacktraceSymbolizer::SymbolizeDa
return true;
}
+const char *LibbacktraceSymbolizer::Demangle(const char *name) {
+#if SANITIZER_CP_DEMANGLE
+ const char *demangled = CplusV3Demangle(name, false);
+ if (demangled)
+ return demangled;
+ return name;
+#else
+ return 0;
+#endif
+}
+
#else // SANITIZER_LIBBACKTRACE
LibbacktraceSymbolizer *LibbacktraceSymbolizer::get(LowLevelAllocator *alloc) {
@@ -139,6 +208,10 @@ bool LibbacktraceSymbolizer::SymbolizeDa
return false;
}
+const char *LibbacktraceSymbolizer::Demangle(const char *name) {
+ return 0;
+}
+
#endif // SANITIZER_LIBBACKTRACE
} // namespace __sanitizer
Jakub
More information about the llvm-commits
mailing list