diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-03 11:45:38 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-03 11:45:38 +0200 |
commit | 6569a6dceec10c77578184ce68c26e20ba27fa39 (patch) | |
tree | 775d831ecbca4a3e1c3e4cfb523b0233fd98b17a /synchronizer/main.cpp | |
parent | 672bbe1f10be056c1170bc227e16060edeff2481 (diff) | |
download | sink-6569a6dceec10c77578184ce68c26e20ba27fa39.tar.gz sink-6569a6dceec10c77578184ce68c26e20ba27fa39.zip |
Print a demangled stacktrace
Diffstat (limited to 'synchronizer/main.cpp')
-rw-r--r-- | synchronizer/main.cpp | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/synchronizer/main.cpp b/synchronizer/main.cpp index 14a8250..4e04acb 100644 --- a/synchronizer/main.cpp +++ b/synchronizer/main.cpp | |||
@@ -25,6 +25,10 @@ | |||
25 | #include <csignal> | 25 | #include <csignal> |
26 | #include <iostream> | 26 | #include <iostream> |
27 | #include <cstdlib> | 27 | #include <cstdlib> |
28 | #include <cxxabi.h> | ||
29 | #include <dlfcn.h> | ||
30 | #include <ostream> | ||
31 | #include <sstream> | ||
28 | 32 | ||
29 | #include "listener.h" | 33 | #include "listener.h" |
30 | #include "log.h" | 34 | #include "log.h" |
@@ -35,26 +39,43 @@ | |||
35 | 39 | ||
36 | Listener *listener = nullptr; | 40 | Listener *listener = nullptr; |
37 | 41 | ||
42 | //Print a demangled stacktrace | ||
38 | void printStacktrace() | 43 | void printStacktrace() |
39 | { | 44 | { |
40 | QString s; | 45 | int skip = 1; |
41 | void *trace[256]; | 46 | void *callstack[128]; |
42 | int n = backtrace(trace, 256); | 47 | const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]); |
43 | if (n) { | 48 | char buf[1024]; |
44 | char **strings = backtrace_symbols(trace, n); | 49 | int nFrames = backtrace(callstack, nMaxFrames); |
45 | 50 | char **symbols = backtrace_symbols(callstack, nFrames); | |
46 | s = QLatin1String("[\n"); | 51 | |
47 | 52 | std::ostringstream trace_buf; | |
48 | for (int i = 0; i < n; ++i) { | 53 | for (int i = skip; i < nFrames; i++) { |
49 | s += QString::number(i) + QLatin1String(": ") + QLatin1String(strings[i]) + QLatin1String("\n"); | 54 | // printf("%s\n", symbols[i]); |
50 | } | 55 | Dl_info info; |
51 | s += QLatin1String("]\n"); | 56 | if (dladdr(callstack[i], &info) && info.dli_sname) { |
52 | std::fprintf(stderr, "Backtrace: %s\n", s.toLatin1().data()); | 57 | char *demangled = NULL; |
53 | 58 | int status = -1; | |
54 | if (strings) { | 59 | if (info.dli_sname[0] == '_') { |
55 | free(strings); | 60 | demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status); |
56 | } | 61 | } |
62 | snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n", | ||
63 | i, int(2 + sizeof(void*) * 2), callstack[i], | ||
64 | status == 0 ? demangled : | ||
65 | info.dli_sname == 0 ? symbols[i] : info.dli_sname, | ||
66 | (char *)callstack[i] - (char *)info.dli_saddr); | ||
67 | free(demangled); | ||
68 | } else { | ||
69 | snprintf(buf, sizeof(buf), "%-3d %*p %s\n", | ||
70 | i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]); | ||
71 | } | ||
72 | trace_buf << buf; | ||
73 | } | ||
74 | free(symbols); | ||
75 | if (nFrames == nMaxFrames) { | ||
76 | trace_buf << "[truncated]\n"; | ||
57 | } | 77 | } |
78 | std::cerr << trace_buf.str(); | ||
58 | } | 79 | } |
59 | 80 | ||
60 | int sCounter = 0; | 81 | int sCounter = 0; |