Skip to content

Commit 2e86715

Browse files
committed
DPL: improve backtrace handling
In case demangling does not work, still try with addr2line. In case that fails as well, print what we have from backtrace.
1 parent 83c51e1 commit 2e86715

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Framework/Foundation/src/BacktraceHelpers.cxx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void BacktraceHelpers::demangled_backtrace_symbols(void** stackTrace, unsigned i
3939
}
4040
#endif
4141

42+
// We skip the level 0, which is the actual logging function.
4243
for (size_t i = 1; i < stackDepth; i++) {
4344

4445
size_t sz = 64000; // 64K ought to be enough for our templates...
@@ -67,17 +68,20 @@ void BacktraceHelpers::demangled_backtrace_symbols(void** stackTrace, unsigned i
6768
bool tryAddr2Line = true;
6869
#endif
6970
if (begin && end) {
71+
// The first byte is a ' ' which we need to skip.
7072
*begin++ = '\0';
7173
*end = '\0';
7274
// found our mangled name, now in [begin, end)
7375

7476
int status;
7577
char* ret = abi::__cxa_demangle(begin, function, &sz, &status);
76-
if (ret) {
78+
if (status == 0) {
7779
// return value may be a realloc() of the input
7880
function = ret;
7981
dprintf(fd, " %s: %s\n", stackStrings[i], function);
8082
tryAddr2Line = false;
83+
} else {
84+
tryAddr2Line = true;
8185
}
8286
}
8387
if (tryAddr2Line) {
@@ -88,12 +92,12 @@ void BacktraceHelpers::demangled_backtrace_symbols(void** stackTrace, unsigned i
8892

8993
// Find c++filt from the environment
9094
// This is needed for platforms where we still need c++filt -r
91-
char const* cxxfilt = getenv("CXXFILT");
95+
static char const* cxxfilt = getenv("CXXFILT");
9296
if (cxxfilt == nullptr) {
9397
cxxfilt = "c++filt";
9498
}
9599
// Do the same for addr2line, just in case we wanted to pass some options
96-
char const* addr2line = getenv("ADDR2LINE");
100+
static char const* addr2line = getenv("ADDR2LINE");
97101
if (addr2line == nullptr) {
98102
addr2line = "addr2line";
99103
}
@@ -104,7 +108,10 @@ void BacktraceHelpers::demangled_backtrace_symbols(void** stackTrace, unsigned i
104108

105109
fp = popen(syscom, "r");
106110
if (fp == nullptr) {
107-
dprintf(fd, "-- no source could be retrieved --\n");
111+
// We could not run addr2line, print whatever we have.
112+
// Also free function, which is allocated at the beginning of the loop.
113+
dprintf(fd, "%s\n", begin);
114+
free(function);
108115
continue;
109116
}
110117

@@ -114,7 +121,7 @@ void BacktraceHelpers::demangled_backtrace_symbols(void** stackTrace, unsigned i
114121

115122
pclose(fp);
116123
} else {
117-
dprintf(fd, "-- no source avaliable --\n");
124+
dprintf(fd, "%s\n", begin);
118125
}
119126
}
120127
free(function);

0 commit comments

Comments
 (0)