backtrace.cpp
Go to the documentation of this file.
/*
* Copyright © 2014 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#include "backtrace.h"
#include <cxxabi.h>
#include <execinfo.h>
namespace bt = core::posix::backtrace;
namespace impl
{
std::tuple<std::string, bool> demangle(const std::string& symbol)
{
int status = 1;
auto result = abi::__cxa_demangle(symbol.c_str(),
nullptr,
nullptr,
&status);
if (!result || status != 0)
{
return std::make_tuple(std::string(), false);
}
std::string s{result};
::free(result);
return std::make_tuple(s, true);
}
struct Frame : public bt::Frame
{
struct Symbol : public bt::Frame::Symbol
{
Symbol(const char* symbol) : raw_(symbol)
{
auto first = raw_.find_first_of("(");
auto last = raw_.find_last_of(")");
if (first != std::string::npos && last != std::string::npos)
{
auto mangled_symbol = raw_.substr(first+1,
(last-1) - (first+1));
auto plus = mangled_symbol.find_first_of("+");
if (plus != std::string::npos)
mangled_symbol.erase(plus);
std::tie(demangled_, is_cxx_) = demangle(mangled_symbol);
if (!is_cxx_)
demangled_ = raw_;
}
}
bool is_cxx() const
{
return is_cxx_;
}
std::string demangled() const
{
return demangled_;
}
std::string raw() const
{
return raw_;
}
std::string raw_;
std::string demangled_;
bool is_cxx_ = false;
};
std::size_t depth_;
void* frame_pointer_;
Symbol symbol_;
Frame(std::size_t depth, void* frame_pointer, const char* symbol)
: depth_(depth),
frame_pointer_(frame_pointer),
symbol_(symbol)
{
}
std::size_t depth() const
{
return depth_;
}
virtual void* frame_pointer() const
{
return frame_pointer_;
}
const Symbol& symbol() const
{
return symbol_;
}
};
}
std::shared_ptr<bt::Frame::Symbol> bt::Frame::Symbol::for_testing_from_raw_symbol(const char* symbol)
{
return std::shared_ptr<bt::Frame::Symbol>(new impl::Frame::Symbol(symbol));
}
void bt::visit_with_handler(const bt::FrameHandler& handler)
{
static const unsigned int max_frames=64;
void *frames[max_frames];
auto frame_count = ::backtrace(frames, max_frames);
auto symbols = ::backtrace_symbols(frames, frame_count);
struct Scope
{
Scope(char** symbols) : symbols(symbols)
{
}
~Scope()
{
::free(symbols);
}
char** symbols = nullptr;
} scope{symbols};
for (int i = 0; i < frame_count; i++)
{
impl::Frame frame(i, frames[i], symbols[i]);
if (!handler(frame))
return;
}
}
backtrace.h
core::posix::backtrace::Frame::Symbol
The Symbol class models the symbolic representation of a frame pointer.
Definition backtrace.h:44
core::posix::backtrace::Frame::Symbol::for_testing_from_raw_symbol
static std::shared_ptr< Symbol > for_testing_from_raw_symbol(const char *symbol)
Definition backtrace.cpp:120
core::posix::backtrace::Frame
The Frame class models an individual frame of a backtrace.
Definition backtrace.h:38
core::posix::backtrace::Frame::symbol
virtual const Symbol & symbol() const =0
symbol returns the symbolic representation of this frame.
core::posix::backtrace
Definition backtrace.h:33
core::posix::backtrace::visit_with_handler
void visit_with_handler(const FrameHandler &handler)
visit_with_handler iterates the backtrace of the calling program, invoking the handler for every fram...
Definition backtrace.cpp:125
core::posix::backtrace::FrameHandler
std::function< bool(const Frame &frame)> FrameHandler
FrameHandler is the functor invoked for every frame of a backtrace.
Definition backtrace.h:106
impl
Definition backtrace.cpp:28
impl::demangle
std::tuple< std::string, bool > demangle(const std::string &symbol)
Definition backtrace.cpp:29
impl::Frame::Symbol
Definition backtrace.cpp:51
impl::Frame::Symbol::is_cxx
bool is_cxx() const
is_cxx checks whether the symbol refers to a mangled C++ symbol.
Definition backtrace.cpp:72
impl::Frame::Symbol::is_cxx_
bool is_cxx_
Definition backtrace.cpp:89
impl::Frame::Symbol::raw
std::string raw() const
raw The raw symbolic representation of a frame pointer.
Definition backtrace.cpp:82
impl::Frame::Symbol::demangled
std::string demangled() const
demangled returns the demangled C++ symbol name or raw.
Definition backtrace.cpp:77
impl::Frame::Symbol::raw_
std::string raw_
Definition backtrace.cpp:87
impl::Frame::Symbol::Symbol
Symbol(const char *symbol)
Definition backtrace.cpp:52
impl::Frame::Symbol::demangled_
std::string demangled_
Definition backtrace.cpp:88
impl::Frame
Definition backtrace.cpp:49
impl::Frame::depth_
std::size_t depth_
Definition backtrace.cpp:92
impl::Frame::symbol
const Symbol & symbol() const
symbol returns the symbolic representation of this frame.
Definition backtrace.cpp:113
impl::Frame::Frame
Frame(std::size_t depth, void *frame_pointer, const char *symbol)
Definition backtrace.cpp:96
impl::Frame::symbol_
Symbol symbol_
Definition backtrace.cpp:94
impl::Frame::depth
std::size_t depth() const
depth returns the depth of this frame in the overall backtrace.
Definition backtrace.cpp:103
impl::Frame::frame_pointer_
void * frame_pointer_
Definition backtrace.cpp:93
impl::Frame::frame_pointer
virtual void * frame_pointer() const
frame_pointer returns the the raw frame pointer of this frame.
Definition backtrace.cpp:108