|
| 1 | +#ifdef CPPTRACE_UNWIND_WITH_RTLVIRTUALUNWIND |
| 2 | + |
| 3 | +#include <cpptrace/basic.hpp> |
| 4 | +#include "unwind/unwind.hpp" |
| 5 | +#include "utils/common.hpp" |
| 6 | +#include "utils/utils.hpp" |
| 7 | + |
| 8 | +#include <cstddef> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#ifndef WIN32_LEAN_AND_MEAN |
| 12 | + #define WIN32_LEAN_AND_MEAN |
| 13 | +#endif |
| 14 | +#include <windows.h> |
| 15 | + |
| 16 | +#if !defined(_M_X64) && !defined(__x86_64__) && !defined(_M_ARM64) && !defined(__aarch64__) |
| 17 | + #error "Cpptrace: RtlVirtualUnwind is only supported on 64-bit Windows" |
| 18 | +#endif |
| 19 | + |
| 20 | +namespace { |
| 21 | +DWORD64 context_pc(CONTEXT& context) { |
| 22 | + #if defined(_M_X64) || defined(__x86_64__) |
| 23 | + return context.Rip; |
| 24 | + #elif defined(_M_ARM64) || defined(__aarch64__) |
| 25 | + return context.Pc; |
| 26 | + #endif |
| 27 | +} |
| 28 | +} |
| 29 | + |
| 30 | +CPPTRACE_BEGIN_NAMESPACE |
| 31 | +namespace detail { |
| 32 | + CPPTRACE_FORCE_NO_INLINE |
| 33 | + std::vector<frame_ptr> capture_frames( |
| 34 | + std::size_t skip, |
| 35 | + std::size_t max_depth, |
| 36 | + EXCEPTION_POINTERS* exception_pointers |
| 37 | + ) { |
| 38 | + CONTEXT context; |
| 39 | + ZeroMemory(&context, sizeof(CONTEXT)); |
| 40 | + if(exception_pointers) { |
| 41 | + context = *exception_pointers->ContextRecord; |
| 42 | + } else { |
| 43 | + skip++; // we're unwinding from the capture_frames frame, skip it |
| 44 | + RtlCaptureContext(&context); |
| 45 | + } |
| 46 | + |
| 47 | + std::vector<frame_ptr> trace; |
| 48 | + while(trace.size() < max_depth) { |
| 49 | + DWORD64 image_base; |
| 50 | + PRUNTIME_FUNCTION function_entry = RtlLookupFunctionEntry(context_pc(context), &image_base, NULL); |
| 51 | + if(!function_entry) { |
| 52 | + break; |
| 53 | + } |
| 54 | + if(skip) { |
| 55 | + skip--; |
| 56 | + } else { |
| 57 | + // Same adjustment as StackWalk64 |
| 58 | + trace.push_back(to_frame_ptr(context_pc(context)) - 1); |
| 59 | + } |
| 60 | + #if defined(_M_X64) || defined(__x86_64__) |
| 61 | + PVOID handler_data; |
| 62 | + DWORD64 establisher_frame; |
| 63 | + RtlVirtualUnwind( |
| 64 | + UNW_FLAG_NHANDLER, |
| 65 | + image_base, |
| 66 | + context_pc(context), |
| 67 | + function_entry, |
| 68 | + &context, |
| 69 | + &handler_data, |
| 70 | + &establisher_frame, |
| 71 | + NULL |
| 72 | + ); |
| 73 | + #elif defined(_M_ARM64) || defined(__aarch64__) |
| 74 | + BOOLEAN in_function; |
| 75 | + FRAME_POINTERS establisher_frame; |
| 76 | + RtlVirtualUnwind( |
| 77 | + image_base, |
| 78 | + context_pc(context), |
| 79 | + function_entry, |
| 80 | + &context, |
| 81 | + &in_function, |
| 82 | + &establisher_frame, |
| 83 | + NULL |
| 84 | + ); |
| 85 | + #endif |
| 86 | + if(context_pc(context) == 0) { |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + return trace; |
| 91 | + } |
| 92 | + |
| 93 | + CPPTRACE_FORCE_NO_INLINE |
| 94 | + std::size_t safe_capture_frames(frame_ptr*, std::size_t, std::size_t, std::size_t) { |
| 95 | + // Can't safe trace with RtlVirtualUnwind |
| 96 | + return 0; |
| 97 | + } |
| 98 | + |
| 99 | + bool has_safe_unwind() { |
| 100 | + return false; |
| 101 | + } |
| 102 | +} |
| 103 | +CPPTRACE_END_NAMESPACE |
| 104 | + |
| 105 | +#endif |
0 commit comments