|
4 | 4 | // accompanying file LICENSE_1_0.txt or copy at
|
5 | 5 | // http://www.boost.org/LICENSE_1_0.txt)
|
6 | 6 |
|
| 7 | +#if defined(_MSC_VER) |
| 8 | + |
| 9 | +#include <boost/stacktrace/safe_dump_to.hpp> |
| 10 | +#include <windows.h> |
| 11 | + |
| 12 | +extern "C" void** __cdecl __current_exception(); // exported from vcruntime.dll |
| 13 | +#define _pCurrentException static_cast<PEXCEPTION_RECORD>(*__current_exception()) |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +constexpr std::size_t kStacktraceDumpSize = 4096; |
| 18 | + |
| 19 | +struct thrown_info { |
| 20 | + ULONG_PTR object; |
| 21 | + char* dump; |
| 22 | +}; |
| 23 | + |
| 24 | +struct exception_data { |
| 25 | + bool capture_stacktraces_at_throw = true; |
| 26 | + unsigned count = 0; |
| 27 | + thrown_info* info = nullptr; |
| 28 | + |
| 29 | + ~exception_data() noexcept { |
| 30 | + HANDLE hHeap = GetProcessHeap(); |
| 31 | + for (unsigned i = 0; i < count; ++i) { |
| 32 | + HeapFree(hHeap, 0, info[i].dump); |
| 33 | + } |
| 34 | + HeapFree(hHeap, 0, info); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +thread_local exception_data data; |
| 39 | + |
| 40 | +inline bool PER_IS_MSVC_EH(PEXCEPTION_RECORD p) noexcept { |
| 41 | + const DWORD EH_EXCEPTION_NUMBER = 0xE06D7363; |
| 42 | + const ULONG_PTR EH_MAGIC_NUMBER1 = 0x19930520; |
| 43 | + |
| 44 | + return p->ExceptionCode == EH_EXCEPTION_NUMBER && |
| 45 | + (p->NumberParameters == 3 || p->NumberParameters == 4) && |
| 46 | + p->ExceptionInformation[0] == EH_MAGIC_NUMBER1; |
| 47 | +} |
| 48 | + |
| 49 | +inline ULONG_PTR PER_PEXCEPTOBJ(PEXCEPTION_RECORD p) noexcept { |
| 50 | + return p->ExceptionInformation[1]; |
| 51 | +} |
| 52 | + |
| 53 | +unsigned current_cxx_exception_index() noexcept { |
| 54 | + if (PEXCEPTION_RECORD current_cxx_exception = _pCurrentException) { |
| 55 | + for (unsigned i = data.count; i > 0;) { |
| 56 | + --i; |
| 57 | + if (data.info[i].object == PER_PEXCEPTOBJ(current_cxx_exception)) { |
| 58 | + return i; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return data.count; |
| 63 | +} |
| 64 | + |
| 65 | +bool is_processing_rethrow(PEXCEPTION_RECORD p) noexcept { |
| 66 | + // Processing flow: |
| 67 | + // 0. throw; |
| 68 | + // 1. _CxxThrowException(pExceptionObject = nullptr) |
| 69 | + // 2. VEH & SEH (may throw new c++ exceptions!) |
| 70 | + // 3. __RethrowException(_pCurrentException) |
| 71 | + // 4. VEH |
| 72 | + if (PER_PEXCEPTOBJ(p) == 0) return true; |
| 73 | + PEXCEPTION_RECORD current_cxx_exception = _pCurrentException; |
| 74 | + if (current_cxx_exception == nullptr) return false; |
| 75 | + return PER_PEXCEPTOBJ(p) == PER_PEXCEPTOBJ(current_cxx_exception); |
| 76 | +} |
| 77 | + |
| 78 | +LONG NTAPI veh(PEXCEPTION_POINTERS p) { |
| 79 | + if (data.capture_stacktraces_at_throw && |
| 80 | + PER_IS_MSVC_EH(p->ExceptionRecord) && |
| 81 | + !is_processing_rethrow(p->ExceptionRecord)) { |
| 82 | + HANDLE hHeap = GetProcessHeap(); |
| 83 | + unsigned index = current_cxx_exception_index(); |
| 84 | + unsigned new_count = 1 + (index < data.count ? index + 1 : 0); |
| 85 | + |
| 86 | + for (unsigned i = new_count; i < data.count; ++i) { |
| 87 | + HeapFree(hHeap, 0, data.info[i].dump); |
| 88 | + data.info[i].dump = nullptr; |
| 89 | + } |
| 90 | + |
| 91 | + void* new_info; |
| 92 | + if (data.info) { |
| 93 | + new_info = HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, data.info, sizeof(thrown_info) * new_count); |
| 94 | + } else { |
| 95 | + new_info = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(thrown_info) * new_count); |
| 96 | + } |
| 97 | + if (new_info) { |
| 98 | + data.count = new_count; |
| 99 | + data.info = static_cast<thrown_info*>(new_info); |
| 100 | + data.info[data.count - 1].object = PER_PEXCEPTOBJ(p->ExceptionRecord); |
| 101 | + char*& dump_ptr = data.info[data.count - 1].dump; |
| 102 | + if (dump_ptr == nullptr) { |
| 103 | + dump_ptr = static_cast<char*>(HeapAlloc(hHeap, 0, kStacktraceDumpSize)); |
| 104 | + } |
| 105 | + if (dump_ptr != nullptr) { |
| 106 | + const std::size_t kSkip = 4; |
| 107 | + boost::stacktrace::safe_dump_to(kSkip, dump_ptr, kStacktraceDumpSize); |
| 108 | + } |
| 109 | + } else if (new_count <= data.count) { |
| 110 | + data.count = new_count - 1; |
| 111 | + HeapFree(hHeap, 0, data.info[data.count - 1].dump); |
| 112 | + data.info[data.count - 1].dump = nullptr; |
| 113 | + } |
| 114 | + } |
| 115 | + return EXCEPTION_CONTINUE_SEARCH; |
| 116 | +} |
| 117 | + |
| 118 | +struct veh_installer { |
| 119 | + PVOID h; |
| 120 | + veh_installer() noexcept : h(AddVectoredExceptionHandler(1, veh)) {} |
| 121 | + ~veh_installer() noexcept { RemoveVectoredExceptionHandler(h); } |
| 122 | +} installer; |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +extern "C" { |
| 127 | + |
| 128 | +BOOST_SYMBOL_EXPORT const char* boost_stacktrace_impl_current_exception_stacktrace() { |
| 129 | + unsigned index = current_cxx_exception_index(); |
| 130 | + return index < data.count ? data.info[index].dump : nullptr; |
| 131 | +} |
| 132 | + |
| 133 | +BOOST_SYMBOL_EXPORT bool* boost_stacktrace_impl_ref_capture_stacktraces_at_throw() { |
| 134 | + return &data.capture_stacktraces_at_throw; |
| 135 | +} |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +namespace boost { namespace stacktrace { namespace impl { |
| 140 | + |
| 141 | +BOOST_SYMBOL_EXPORT void assert_no_pending_traces() noexcept { |
| 142 | +} |
| 143 | + |
| 144 | +}}} // namespace boost::stacktrace::impl |
| 145 | + |
| 146 | +#else |
| 147 | + |
7 | 148 | #include "exception_headers.h"
|
8 | 149 |
|
9 | 150 | // At the moment the file is used only on POSIX. _Unwind_Backtrace may be
|
@@ -222,3 +363,4 @@ BOOST_SYMBOL_EXPORT void assert_no_pending_traces() noexcept {
|
222 | 363 |
|
223 | 364 | }}} // namespace boost::stacktrace::impl
|
224 | 365 |
|
| 366 | +#endif |
0 commit comments