forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunwind_with_execinfo.cpp
More file actions
33 lines (28 loc) · 1.14 KB
/
Copy pathunwind_with_execinfo.cpp
File metadata and controls
33 lines (28 loc) · 1.14 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
#ifdef CPPTRACE_UNWIND_WITH_EXECINFO
#include "unwind.hpp"
#include "../platform/common.hpp"
#include "../platform/utils.hpp"
#include <algorithm>
#include <cstddef>
#include <vector>
#include <execinfo.h>
namespace cpptrace {
namespace detail {
CPPTRACE_FORCE_NO_INLINE
std::vector<std::uintptr_t> capture_frames(std::size_t skip, std::size_t max_depth) {
skip++;
std::vector<void*> addrs(std::min(hard_max_frames, skip + max_depth), nullptr);
const int n_frames = backtrace(addrs.data(), static_cast<int>(addrs.size())); // thread safe
// I hate the copy here but it's the only way that isn't UB
std::vector<std::uintptr_t> frames(n_frames - skip, 0);
for(int i = skip; 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 - skip] = reinterpret_cast<std::uintptr_t>(addrs[i]) - 1;
}
return frames;
}
}
}
#endif