/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2010-2014 Danny Robson */ #include "backtrace.hpp" #include "debug.hpp" #include "exe.hpp" #include "io.hpp" #include "cast.hpp" #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////// debug::backtrace::backtrace (void): m_frames (DEFAULT_DEPTH) { size_t last; size_t size = m_frames.size (); while ((last = ::backtrace (&m_frames[0], cruft::cast::lossless (m_frames.size ()))) == size) m_frames.resize (size = m_frames.size () * 2); CHECK_GT (last, 0u); m_frames.resize (last); } /////////////////////////////////////////////////////////////////////////////// static std::string addr2line (const void *addr) { #if defined(ADDR2LINE) using pstream_t = std::unique_ptr; std::ostringstream cmd; cmd << ADDR2LINE << " -e " << cruft::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 = cruft::slurp (stream.get ()); return std::string (data.cbegin (), data.cend ()); #else return ""; #endif } /////////////////////////////////////////////////////////////////////////////// std::ostream& debug::operator <<(std::ostream &os, const debug::backtrace &rhs) { auto const &frames = rhs.frames (); // We don't use the array form of unique_ptr as clang fails on ambigious constructors typedef std::unique_ptr str_t; str_t names (backtrace_symbols (frames.data (), cruft::cast::lossless (frames.size ())), ::free); for (unsigned int i = 0; i < frames.size (); ++i) os << frames[i] << '\t' << names.get()[i] << '\t' << addr2line (frames[i]); return os; }