libcruft-util/backtrace_execinfo.cpp
Danny Robson 9ca093d982 backtrace: remove dependency on addr2line
This wasn't functioning in any case so we may as well get rid of it.
2018-09-12 14:01:21 +10:00

59 lines
1.5 KiB
C++

/*
* 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 <danny@nerdcruft.net>
*/
#include "backtrace.hpp"
#include "debug.hpp"
#include "exe.hpp"
#include "io.hpp"
#include "cast.hpp"
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <execinfo.h>
#include <memory>
///////////////////////////////////////////////////////////////////////////////
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 <int> (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<char *, decltype(&std::free)>;
str_t names (
backtrace_symbols (
frames.data (),
cruft::cast::lossless <int> (frames.size ())
),
::free
);
for (unsigned int i = 0; i < frames.size (); ++i)
os << frames[i] << '\t' << names.get()[i] << '\n';
return os;
}