Skip to content

Commit

Permalink
#914: debug: fix stacktrace symbols when dladdr fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lifflander committed Jul 8, 2020
1 parent c565a68 commit f504d95
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cmake/check_system_functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ check_function_exists(sysinfo vt_has_sysinfo)

set(CMAKE_REQUIRED_INCLUDES "mach/mach.h")
check_function_exists(mach_task_self vt_has_mach_task_self)

find_program(vt_has_addrtoline addr2line)
find_program(vt_has_atos atos)
2 changes: 2 additions & 0 deletions cmake_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@
#cmakedefine vt_has_unistd_h
#cmakedefine vt_has_inttypes_h
#cmakedefine vt_has_sysconf
#cmakedefine vt_has_addrtoline
#cmakedefine vt_has_ados
80 changes: 71 additions & 9 deletions src/vt/configs/error/stack_out.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

#include "fmt/format.h"

#include <vt/cmake_config.h>
#include "vt/configs/error/stack_out.h"
#include "vt/configs/arguments/args.h"

#include <cstdlib>
#include <vector>
Expand Down Expand Up @@ -95,16 +97,76 @@ DumpStackType dumpStack(int skip) {
std::free(demangled);
} else {

tuple.emplace_back(
std::forward_as_tuple(
static_cast<int>(2 + sizeof(void*) * 2), callstack[i], symbols[i], 0
)
);
bool found_sym = false;

auto const& t = tuple.back();
str = fmt::format(
"{:10} {} {} {}\n", i, std::get<0>(t), std::get<1>(t), std::get<2>(t)
);
char* addr = nullptr;
char* p = symbols[i];
while (p and *p != '\0') {
if (*p == '(' and *(p+1) == '+') {
addr = p+2;
break;
}
p++;
}

while (p and *p != '\0' and *p != ')') p++;
*p = '\0';

#if defined(vt_has_popen) && (defined(vt_has_addrtoline) || defined(vt_has_atos))
if (addr) {
#if defined(vt_has_addrtoline)
auto cmd = fmt::format(
"addr2line -f -i -C -s -p -e {} {}",
arguments::ArgConfig::argv_prog_name, addr
);
#elif defined(vt_has_atos)
auto cmd = fmt::format(
"atos -o {} {}",
arguments::ArgConfig::argv_prog_name, addr
);
#endif
FILE* f = popen(cmd.c_str(), "r");
if (f) {
std::string sym;
sym.resize(16384);

fgets(&sym[0], 16384, f);

for (std::size_t j = 0; j < 16384; j++) {
if (sym[j] == '\n') {
sym[j] = '\0';
}
}

tuple.emplace_back(
std::forward_as_tuple(
static_cast<int>(2 + sizeof(void*) * 2), callstack[i], sym, 0
)
);
auto const& t = tuple.back();
str = fmt::format(
"{:<4} {:<4} {:<15} {} + {}\n",
i, std::get<0>(t), std::get<1>(t), std::get<2>(t), std::get<3>(t)
);
found_sym = true;

pclose(f);
}
}
# endif

if (not found_sym) {
tuple.emplace_back(
std::forward_as_tuple(
static_cast<int>(2 + sizeof(void*) * 2), callstack[i], symbols[i], 0
)
);

auto const& t = tuple.back();
str = fmt::format(
"{:10} {} {} {}\n", i, std::get<0>(t), std::get<1>(t), std::get<2>(t)
);
}
}

trace_buf << str;
Expand Down

0 comments on commit f504d95

Please sign in to comment.