forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunwind_with_winapi.cpp
More file actions
56 lines (48 loc) · 1.6 KB
/
unwind_with_winapi.cpp
File metadata and controls
56 lines (48 loc) · 1.6 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
#ifdef CPPTRACE_UNWIND_WITH_WINAPI
#include <cpptrace/basic.hpp>
#include "unwind/unwind.hpp"
#include "utils/common.hpp"
#include "utils/utils.hpp"
#include <algorithm>
#include <cstdint>
#include <vector>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
// Fucking windows headers
#ifdef min
#undef min
#endif
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
CPPTRACE_FORCE_NO_INLINE
std::vector<frame_ptr> capture_frames(std::size_t skip, std::size_t max_depth) {
std::vector<void*> addrs(skip + std::min(hard_max_frames, max_depth), nullptr);
std::size_t n_frames = CaptureStackBackTrace(
static_cast<ULONG>(skip + 1),
static_cast<ULONG>(addrs.size()),
addrs.data(),
NULL
);
// I hate the copy here but it's the only way that isn't UB
std::vector<frame_ptr> frames(n_frames, 0);
for(std::size_t i = 0; i < n_frames; i++) {
// On x86/x64/arm, as far as I can tell, the frame return address is always one after the call
// So we just decrement to get the pc back inside the `call` / `bl`
// This is done with _Unwind too but conditionally based on info from _Unwind_GetIPInfo.
frames[i] = reinterpret_cast<frame_ptr>(addrs[i]) - 1;
}
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 winapi
return 0;
}
bool has_safe_unwind() {
return false;
}
}
CPPTRACE_END_NAMESPACE
#endif