| 1 |
#ifdef CPPTRACE_UNWIND_WITH_WINAPI |
| 2 |
|
| 3 |
#include <cpptrace/basic.hpp> |
| 4 |
#include "unwind/unwind.hpp" |
| 5 |
#include "utils/common.hpp" |
| 6 |
#include "utils/utils.hpp" |
| 7 |
|
| 8 |
#include <algorithm> |
| 9 |
#include <cstdint> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#ifndef WIN32_LEAN_AND_MEAN |
| 13 |
#define WIN32_LEAN_AND_MEAN |
| 14 |
#endif |
| 15 |
#include <windows.h> |
| 16 |
|
| 17 |
// Fucking windows headers |
| 18 |
#ifdef min |
| 19 |
#undef min |
| 20 |
#endif |
| 21 |
|
| 22 |
CPPTRACE_BEGIN_NAMESPACE |
| 23 |
namespace detail { |
| 24 |
CPPTRACE_FORCE_NO_INLINE |
| 25 |
std::vector<frame_ptr> capture_frames(std::size_t skip, std::size_t max_depth) { |
| 26 |
std::vector<void*> addrs(skip + std::min(hard_max_frames, max_depth), nullptr); |
| 27 |
std::size_t n_frames = CaptureStackBackTrace( |
| 28 |
static_cast<ULONG>(skip + 1), |
| 29 |
static_cast<ULONG>(addrs.size()), |
| 30 |
addrs.data(), |
| 31 |
NULL |
| 32 |
); |
| 33 |
// I hate the copy here but it's the only way that isn't UB |
| 34 |
std::vector<frame_ptr> frames(n_frames, 0); |
| 35 |
for(std::size_t i = 0; i < n_frames; i++) { |
| 36 |
// On x86/x64/arm, as far as I can tell, the frame return address is always one after the call |
| 37 |
// So we just decrement to get the pc back inside the `call` / `bl` |
| 38 |
// This is done with _Unwind too but conditionally based on info from _Unwind_GetIPInfo. |
| 39 |
frames[i] = reinterpret_cast<frame_ptr>(addrs[i]) - 1; |
| 40 |
} |
| 41 |
return frames; |
| 42 |
} |
| 43 |
|
| 44 |
CPPTRACE_FORCE_NO_INLINE |
| 45 |
std::size_t safe_capture_frames(frame_ptr*, std::size_t, std::size_t, std::size_t) { |
| 46 |
// Can't safe trace with winapi |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
bool has_safe_unwind() { |
| 51 |
return false; |
| 52 |
} |
| 53 |
} |
| 54 |
CPPTRACE_END_NAMESPACE |
| 55 |
|
| 56 |
#endif |