Skip to content

Commit

Permalink
stacktrace: Handle dladdr function failure
Browse files Browse the repository at this point in the history
dladdr function could be failed.
There could be only three cases.
Followings are the way to print for each cases.
Consider symbol name as A, symbol offset as B, shared object name as C
and shared object offset as D

Case 1: Success
  A +B (C +D)
Case 2: Failed to get symbol but succeed to get shared object
  (C +D)
Case 3: Failed to get symbol as well as shared object
  unknown

Signed-off-by: Bojun Seo <bojun.seo.0@gmail.com>
  • Loading branch information
Bojun-Seo committed Apr 25, 2023
1 parent f17e22b commit f0f4f81
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/stacktrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,25 @@ static void print_backtrace_symbol(int count, void *addr)
char *symbol;
int offset;
int status;

// dladdr() translates address to symbolic info.
dladdr(addr, &dlip);
int dl_ret;
int len = SYMBOL_MAXLEN;

#if __SIZEOF_LONG__ == 4
pr_out("%2d [%#10lx] ", count, (unsigned long)addr);
#else
pr_out("%2d [%#14lx] ", count, (unsigned long)addr);
#endif
// dladdr() translates address to symbolic info.
dl_ret = dladdr(addr, &dlip);
if (dl_ret == 0) {
pr_out("?\n");
return;
}

symbol = abi::__cxa_demangle(dlip.dli_sname, nullptr, nullptr, &status);

if (status == -2 && !symbol)
symbol = strdup(dlip.dli_sname);

if (symbol) {
int len = SYMBOL_MAXLEN;
if (dlip.dli_sname != NULL && dlip.dli_saddr != NULL) {
symbol = abi::__cxa_demangle(dlip.dli_sname, nullptr, nullptr, &status);
if (status != 0)
symbol = strdup(dlip.dli_sname);

if (strlen(symbol) > len) {
symbol[len - 3] = '.';
Expand Down

0 comments on commit f0f4f81

Please sign in to comment.