/* * 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); } /////////////////////////////////////////////////////////////////////////////// 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 using str_t = std::unique_ptr; 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] << '\n'; return os; }