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_COLLECT_UNWIND_IPP
8 #define BOOST_STACKTRACE_DETAIL_COLLECT_UNWIND_IPP
10 #include <boost/config.hpp>
11 #ifdef BOOST_HAS_PRAGMA_ONCE
15 #include <boost/stacktrace/safe_dump_to.hpp>
20 #if !defined(_GNU_SOURCE) && !defined(BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED) && !defined(BOOST_WINDOWS)
21 #error "Boost.Stacktrace requires `_Unwind_Backtrace` function. Define `_GNU_SOURCE` macro or `BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED` if _Unwind_Backtrace is available without `_GNU_SOURCE`."
24 namespace boost { namespace stacktrace { namespace detail {
27 std::size_t frames_to_skip;
28 native_frame_ptr_t* current;
29 native_frame_ptr_t* end;
32 inline _Unwind_Reason_Code unwind_callback(::_Unwind_Context* context, void* arg) {
33 // Note: do not write `::_Unwind_GetIP` because it is a macro on some platforms.
34 // Use `_Unwind_GetIP` instead!
35 unwind_state* const state = static_cast<unwind_state*>(arg);
36 if (state->frames_to_skip) {
37 --state->frames_to_skip;
38 return _Unwind_GetIP(context) ? ::_URC_NO_REASON : ::_URC_END_OF_STACK;
41 *state->current = reinterpret_cast<native_frame_ptr_t>(
42 _Unwind_GetIP(context)
46 if (!*(state->current - 1) || state->current == state->end) {
47 return ::_URC_END_OF_STACK;
49 return ::_URC_NO_REASON;
52 std::size_t this_thread_frames::collect(native_frame_ptr_t* out_frames, std::size_t max_frames_count, std::size_t skip) BOOST_NOEXCEPT {
53 std::size_t frames_count = 0;
54 if (!max_frames_count) {
58 boost::stacktrace::detail::unwind_state state = { skip + 1, out_frames, out_frames + max_frames_count };
59 ::_Unwind_Backtrace(&boost::stacktrace::detail::unwind_callback, &state);
60 frames_count = state.current - out_frames;
62 if (frames_count && out_frames[frames_count - 1] == 0) {
70 }}} // namespace boost::stacktrace::detail
72 #endif // BOOST_STACKTRACE_DETAIL_COLLECT_UNWIND_IPP