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
68 lines (60 loc) · 1.92 KB
/
Copy pathunwind_with_unwind.cpp
File metadata and controls
68 lines (60 loc) · 1.92 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
#ifdef CPPTRACE_UNWIND_WITH_UNWIND
#include "unwind.hpp"
#include "../platform/common.hpp"
#include "../platform/error.hpp"
#include "../platform/utils.hpp"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <vector>
#include <unwind.h>
namespace cpptrace {
namespace detail {
struct unwind_state {
std::size_t skip;
std::size_t count;
std::vector<std::uintptr_t>& 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) == std::uintptr_t(0)) {
return _URC_END_OF_STACK;
} else {
return _URC_NO_REASON;
}
}
VERIFY(
state.count < state.vec.size(),
"Somehow cpptrace::detail::unwind_callback is overflowing a vector"
);
int is_before_instruction = 0;
std::uintptr_t ip = _Unwind_GetIPInfo(context, &is_before_instruction);
if(!is_before_instruction && ip != std::uintptr_t(0)) {
ip--;
}
if (ip == std::uintptr_t(0)) {
return _URC_END_OF_STACK;
} else {
// TODO: push_back?...
state.vec[state.count++] = ip;
if(state.count == state.vec.size()) {
return _URC_END_OF_STACK;
} else {
return _URC_NO_REASON;
}
}
}
CPPTRACE_FORCE_NO_INLINE
std::vector<std::uintptr_t> capture_frames(std::size_t skip, std::size_t max_depth) {
std::vector<std::uintptr_t> frames(std::min(hard_max_frames, max_depth), 0);
unwind_state state{skip + 1, 0, frames};
_Unwind_Backtrace(unwind_callback, &state); // presumably thread-safe
frames.resize(state.count);
frames.shrink_to_fit();
return frames;
}
}
}
#endif