1 // Copyright Antony Polukhin, 2016-2017.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_STACKTRACE_DETAIL_LIBBACKTRACE_IMPLS_HPP
8 #define BOOST_STACKTRACE_DETAIL_LIBBACKTRACE_IMPLS_HPP
10 #include <boost/config.hpp>
11 #ifdef BOOST_HAS_PRAGMA_ONCE
15 #include <boost/stacktrace/detail/to_hex_array.hpp>
16 #include <boost/stacktrace/detail/location_from_symbol.hpp>
17 #include <boost/core/demangle.hpp>
18 #include <boost/lexical_cast.hpp>
20 #include <backtrace.h>
22 namespace boost { namespace stacktrace { namespace detail {
26 std::string* function;
27 std::string* filename;
31 inline int libbacktrace_full_callback(void *data, uintptr_t /*pc*/, const char *filename, int lineno, const char *function) {
32 pc_data& d = *static_cast<pc_data*>(data);
33 if (d.filename && filename) {
34 *d.filename = filename;
36 if (d.function && function) {
37 *d.function = function;
43 inline void libbacktrace_error_callback(void* /*data*/, const char* /*msg*/, int /*errnum*/) BOOST_NOEXCEPT {
44 // Do nothing, just return.
48 inline ::backtrace_state* construct_state(const program_location& prog_location) BOOST_NOEXCEPT {
49 // Currently `backtrace_create_state` can not detect file name on Windows https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82543
50 // That's why we provide a `prog_location` here.
51 return ::backtrace_create_state(
52 prog_location.name(), 0 /*thread-safe*/, boost::stacktrace::detail::libbacktrace_error_callback, 0
55 // TODO: this does not seem to work well when this function is in .so:
56 // Not async-signal-safe, so this method is not called from async-safe functions.
58 // This function is not async signal safe because:
59 // * Dynamic initialization of a block-scope variable with static storage duration could lock a mutex
60 // * No guarantees on `backtrace_create_state` function.
62 // [dcl.inline]: A static local variable in an inline function with external linkage always refers to the same object.
65 static ::backtrace_state* state = ::backtrace_create_state(
66 0, 1 , boost::stacktrace::detail::libbacktrace_error_callback, 0
73 struct to_string_using_backtrace {
75 boost::stacktrace::detail::program_location prog_location;
76 ::backtrace_state* state;
80 void prepare_function_name(const void* addr) {
81 boost::stacktrace::detail::pc_data data = {&res, &filename, 0};
85 reinterpret_cast<uintptr_t>(addr),
86 boost::stacktrace::detail::libbacktrace_full_callback,
87 boost::stacktrace::detail::libbacktrace_error_callback,
94 bool prepare_source_location(const void* /*addr*/) {
95 if (filename.empty() || !line) {
102 res += boost::lexical_cast<boost::array<char, 40> >(line).data();
106 to_string_using_backtrace() BOOST_NOEXCEPT {
107 state = boost::stacktrace::detail::construct_state(prog_location);
111 template <class Base> class to_string_impl_base;
112 typedef to_string_impl_base<to_string_using_backtrace> to_string_impl;
114 inline std::string name_impl(const void* addr) {
117 boost::stacktrace::detail::program_location prog_location;
118 ::backtrace_state* state = boost::stacktrace::detail::construct_state(prog_location);
120 boost::stacktrace::detail::pc_data data = {&res, 0, 0};
124 reinterpret_cast<uintptr_t>(addr),
125 boost::stacktrace::detail::libbacktrace_full_callback,
126 boost::stacktrace::detail::libbacktrace_error_callback,
131 res = boost::core::demangle(res.c_str());
137 } // namespace detail
139 std::string frame::source_file() const {
142 boost::stacktrace::detail::program_location prog_location;
143 ::backtrace_state* state = boost::stacktrace::detail::construct_state(prog_location);
145 boost::stacktrace::detail::pc_data data = {0, &res, 0};
149 reinterpret_cast<uintptr_t>(addr_),
150 boost::stacktrace::detail::libbacktrace_full_callback,
151 boost::stacktrace::detail::libbacktrace_error_callback,
159 std::size_t frame::source_line() const {
160 boost::stacktrace::detail::program_location prog_location;
161 ::backtrace_state* state = boost::stacktrace::detail::construct_state(prog_location);
163 boost::stacktrace::detail::pc_data data = {0, 0, 0};
167 reinterpret_cast<uintptr_t>(addr_),
168 boost::stacktrace::detail::libbacktrace_full_callback,
169 boost::stacktrace::detail::libbacktrace_error_callback,
178 }} // namespace boost::stacktrace
180 #endif // BOOST_STACKTRACE_DETAIL_LIBBACKTRACE_IMPLS_HPP