]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/stacktrace/detail/collect_unwind.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / stacktrace / detail / collect_unwind.ipp
1 // Copyright Antony Polukhin, 2016-2017.
2 //
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)
6
7 #ifndef BOOST_STACKTRACE_DETAIL_COLLECT_UNWIND_IPP
8 #define BOOST_STACKTRACE_DETAIL_COLLECT_UNWIND_IPP
9
10 #include <boost/config.hpp>
11 #ifdef BOOST_HAS_PRAGMA_ONCE
12 # pragma once
13 #endif
14
15 #include <boost/stacktrace/safe_dump_to.hpp>
16
17 #include <unwind.h>
18 #include <cstdio>
19
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`."
22 #endif
23
24 namespace boost { namespace stacktrace { namespace detail {
25
26 struct unwind_state {
27 std::size_t frames_to_skip;
28 native_frame_ptr_t* current;
29 native_frame_ptr_t* end;
30 };
31
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;
39 }
40
41 *state->current = reinterpret_cast<native_frame_ptr_t>(
42 _Unwind_GetIP(context)
43 );
44
45 ++state->current;
46 if (!*(state->current - 1) || state->current == state->end) {
47 return ::_URC_END_OF_STACK;
48 }
49 return ::_URC_NO_REASON;
50 }
51
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) {
55 return frames_count;
56 }
57
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;
61
62 if (frames_count && out_frames[frames_count - 1] == 0) {
63 -- frames_count;
64 }
65
66 return frames_count;
67 }
68
69
70 }}} // namespace boost::stacktrace::detail
71
72 #endif // BOOST_STACKTRACE_DETAIL_COLLECT_UNWIND_IPP