10static char *addr2line(
const char *image,
void *addr,
bool color_output) {
11 static char exename[4096] = {0};
12 static char result[4096] = {0};
14 if (exename[0] == 0) {
15 int ret = readlink(
"/proc/self/exe", exename, 4095);
24 if (pipe(pipefd) != 0) {
30 dup2(pipefd[1], STDOUT_FILENO);
31 dup2(pipefd[1], STDERR_FILENO);
32 if (execlp(
"addr2line",
"addr2line", exename,
"-f",
"-C",
"-e", image,
33 reinterpret_cast<void *
>(NULL)) == -1) {
39 const int line_max_length = 4096;
41 ssize_t len = read(pipefd[0], line, line_max_length);
48 if (waitpid(pid, NULL, 0) != pid) {
56 strcpy(line,
"\033[32;1m");
60 strcat(line,
"\033[0m");
66 if (*(strstr(line,
"\n") + 1) ==
'?') {
68 strcpy(strstr(line,
"\n") + 1, image);
113static std::string Backtrace(
int skip = 1) throw() {
114 void *callstack[128];
115 const int nMaxFrames =
sizeof(callstack) /
sizeof(callstack[0]);
117 int nFrames = backtrace(callstack, nMaxFrames);
118 char **symbols = backtrace_symbols(callstack, nFrames);
120 std::ostringstream trace_buf;
121 for (
int i = skip; i < nFrames - 2; i++) {
125 if (dladdr(callstack[i], &info) && info.dli_sname) {
126 char *demangled = NULL;
128 if (info.dli_sname[0] ==
'_')
129 demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
130 char *line = addr2line(info.dli_fname, callstack[i],
false);
132 snprintf(buf,
sizeof(buf),
"%5d %s + %zd %s\n",
134 status == 0 ? demangled
135 : info.dli_sname == 0 ? symbols[i]
137 (char *)callstack[i] - (char *)info.dli_saddr, line);
141 snprintf(buf,
sizeof(buf),
"%5d %s\n",
148 if (nFrames == nMaxFrames) trace_buf <<
"[truncated]\n";
149 return trace_buf.str();
152#include "Framework/Exception/Exception.h"
156void Exception::buildStackTrace() throw() {
stackTrace_ = Backtrace(2); }
std::string stackTrace_
The stack trace.
All classes in the ldmx-sw project use this namespace.