From 56d9b6dff34c7f934786aa996d94c9f5d7b0c0c9 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 5 Dec 2008 20:12:48 +0000 Subject: [PATCH] Demangle and pretty-print symbols in internal backtraces. Patch by Wesley Peck, with a few fixes by me. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60605 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/System/Unix/Signals.inc | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/System/Unix/Signals.inc b/lib/System/Unix/Signals.inc index 4e0e6d22fd9..abb6a336377 100644 --- a/lib/System/Unix/Signals.inc +++ b/lib/System/Unix/Signals.inc @@ -25,6 +25,10 @@ #if HAVE_SYS_STAT_H #include #endif +#if HAVE_DLFCN_H && __GNUG__ +#include +#include +#endif using namespace llvm; namespace { @@ -69,8 +73,48 @@ static void PrintStackTrace() { // Use backtrace() to output a backtrace on Linux systems with glibc. int depth = backtrace(StackTrace, static_cast(array_lengthof(StackTrace))); +#if HAVE_DLFCN_H && __GNUG__ + int width = 0; + for (int i = 0; i < depth; ++i) { + Dl_info dlinfo; + dladdr(StackTrace[i], &dlinfo); + char* name = strrchr(dlinfo.dli_fname, '/'); + + int nwidth; + if (name == NULL) nwidth = strlen(dlinfo.dli_fname); + else nwidth = strlen(name) - 1; + + if (nwidth > width) width = nwidth; + } + + for (int i = 0; i < depth; ++i) { + Dl_info dlinfo; + dladdr(StackTrace[i], &dlinfo); + + fprintf(stderr, "%-3d", i); + + char* name = strrchr(dlinfo.dli_fname, '/'); + if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname); + else fprintf(stderr, " %-*s", width, name+1); + + fprintf(stderr, " %#0*x", (int)(sizeof(void*) * 2) + 2, StackTrace[i]); + + if (dlinfo.dli_sname != NULL) { + int res; + fputc(' ', stderr); + char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res); + if (d == NULL) fputs(dlinfo.dli_sname, stderr); + else fputs(d, stderr); + free(d); + + fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr); + } + fputc('\n', stderr); + } +#else backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); #endif +#endif } // SignalHandler - The signal handler that runs... -- 2.34.1