forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunwind_with_unwind.cpp
More file actions
75 lines (65 loc) · 2.02 KB
/
unwind_with_unwind.cpp
File metadata and controls
75 lines (65 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifdef CPPTRACE_UNWIND_WITH_UNWIND
#include "unwind/unwind.hpp"
#include "utils/common.hpp"
#include "utils/error.hpp"
#include "utils/utils.hpp"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <vector>
#include <unwind.h>
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
struct unwind_state {
std::size_t skip;
std::size_t max_depth;
std::vector<frame_ptr>& vec;
};
_Unwind_Reason_Code unwind_callback(_Unwind_Context* context, void* arg) {
unwind_state& state = *static_cast<unwind_state*>(arg);
if(state.skip) {
state.skip--;
if(_Unwind_GetIP(context) == frame_ptr(0)) {
return _URC_END_OF_STACK;
} else {
return _URC_NO_REASON;
}
}
ASSERT(
state.vec.size() < state.max_depth,
"Somehow cpptrace::detail::unwind_callback is being called beyond the max_depth"
);
int is_before_instruction = 0;
frame_ptr ip = _Unwind_GetIPInfo(context, &is_before_instruction);
if(!is_before_instruction && ip != frame_ptr(0)) {
ip--;
}
if (ip == frame_ptr(0)) {
return _URC_END_OF_STACK;
} else {
state.vec.push_back(ip);
if(state.vec.size() >= state.max_depth) {
return _URC_END_OF_STACK;
} else {
return _URC_NO_REASON;
}
}
}
CPPTRACE_FORCE_NO_INLINE
std::vector<frame_ptr> capture_frames(std::size_t skip, std::size_t max_depth) {
std::vector<frame_ptr> frames;
unwind_state state{skip + 1, max_depth, frames};
_Unwind_Backtrace(unwind_callback, &state); // presumably thread-safe
return frames;
}
CPPTRACE_FORCE_NO_INLINE
std::size_t safe_capture_frames(frame_ptr*, std::size_t, std::size_t, std::size_t) {
// Can't safe trace with _Unwind
return 0;
}
bool has_safe_unwind() {
return false;
}
}
CPPTRACE_END_NAMESPACE
#endif