backtrace: add addr2line fields in output

This commit is contained in:
Danny Robson 2015-10-29 17:52:48 +11:00
parent 620c2d3eb1
commit c33fd2601c
2 changed files with 37 additions and 4 deletions

View File

@ -17,9 +17,14 @@
#include "backtrace.hpp" #include "backtrace.hpp"
#include "debug.hpp" #include "./debug.hpp"
#include "./exe.hpp"
#include "./io.hpp"
#include "types/casts.hpp" #include "types/casts.hpp"
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <execinfo.h> #include <execinfo.h>
#include <algorithm> #include <algorithm>
@ -44,17 +49,42 @@ debug::backtrace::backtrace (void):
} }
//-----------------------------------------------------------------------------
static std::string
addr2line (const void *addr)
{
#if defined(ADDR2LINE)
using pstream_t = std::unique_ptr<FILE,decltype(&::pclose)>;
std::ostringstream cmd;
cmd << ADDR2LINE << " -e " << util::image_path () << ' ' << std::hex << addr;
pstream_t stream (
::popen (cmd.str ().c_str (), "r"),
::pclose
);
// inefficient to copy from vector to string, but it's not a high priority path
auto data = util::slurp (stream.get ());
return std::string (data.cbegin (), data.cend ());
#else
return "\n";
#endif
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
ostream& ostream&
debug::operator <<(ostream &os, const debug::backtrace &rhs) { debug::operator <<(ostream &os, const debug::backtrace &rhs) {
const auto frames = rhs.frames (); const auto frames = rhs.frames ();
// We don't use the array form of unique_ptr as clang fails on ambigious constructors // We don't use the array form of unique_ptr as clang fails on ambigious constructors
typedef unique_ptr<char *, decltype(&std::free)> unique_str; typedef unique_ptr<char *, decltype(&std::free)> str_t;
unique_str names (backtrace_symbols (frames.data (), frames.size ()), ::free); str_t names (backtrace_symbols (frames.data (), frames.size ()), ::free);
for (unsigned int i = 0; i < frames.size (); ++i) for (unsigned int i = 0; i < frames.size (); ++i)
os << frames[i] << "\t" << names.get()[i] << "\n"; os << frames[i] << '\t' << names.get()[i] << '\t' << addr2line (frames[i]);
return os; return os;
} }

View File

@ -95,6 +95,9 @@ PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.0])
AC_SUBST(ZLIB_CFLAGS) AC_SUBST(ZLIB_CFLAGS)
AC_SUBST(ZLIB_LIBS) AC_SUBST(ZLIB_LIBS)
AC_CHECK_TOOL([ADDR2LINE], [addr2line], [:])
AS_IF([test "x$ADDR2LINE" != "x:"], [AC_DEFINE_UNQUOTED([ADDR2LINE], ["$ADDR2LINE"], [addr2line tool name])])
############################################################################### ###############################################################################
## Performance and build optimisations ## Performance and build optimisations