|
| 1 | +// Eggs.Stacktrace |
| 2 | +// |
| 3 | +// Copyright (c) 2026 Agustin Berge |
| 4 | +// |
| 5 | +// Distributed under the Boost Software License, Version 1.0. |
| 6 | +// See accompanying file LICENSE.txt or copy at |
| 7 | +// http://www.boost.org/LICENSE_1_0.txt |
| 8 | + |
| 9 | +#include <eggs/stacktrace.hpp> |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | +#include <cstddef> |
| 13 | +#include <cstdint> |
| 14 | +#include <limits> |
| 15 | +#include <mutex> |
| 16 | +#include <string> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "detail/backend.hpp" |
| 20 | + |
| 21 | +#define WIN32_LEAN_AND_MEAN |
| 22 | +#define NOMINMAX |
| 23 | +#include <windows.h> |
| 24 | +// <dbghelp.h> requires <windows.h> to be included first. |
| 25 | +#include <dbghelp.h> |
| 26 | + |
| 27 | +namespace eggs { |
| 28 | +namespace detail { |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +// DbgHelp is not thread-safe; serialize the Sym* calls made by this backend. |
| 33 | +// DbgHelp state is process-global, so this does not protect against Sym* |
| 34 | +// calls made elsewhere in the process. |
| 35 | +std::mutex& dbg_mutex() noexcept |
| 36 | +{ |
| 37 | + static std::mutex m; |
| 38 | + return m; |
| 39 | +} |
| 40 | + |
| 41 | +// One-time initialization, must run before any other Sym* call. |
| 42 | +bool dbg_init() noexcept |
| 43 | +{ |
| 44 | + static bool const ok = []() noexcept -> bool { |
| 45 | + ::SymSetOptions( |
| 46 | + SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES |
| 47 | + ); |
| 48 | + if (::SymInitialize(::GetCurrentProcess(), nullptr, TRUE) != FALSE) |
| 49 | + return true; |
| 50 | + // Already initialized by some other module in the process. |
| 51 | + return ::GetLastError() == ERROR_INVALID_PARAMETER; |
| 52 | + }(); |
| 53 | + return ok; |
| 54 | +} |
| 55 | + |
| 56 | +// Sym* lookups only see modules known at the last enumeration; retry once |
| 57 | +// after a refresh to pick up modules loaded since. |
| 58 | +template <typename F> |
| 59 | +auto with_module_refresh(F const& f) noexcept -> decltype(f()) |
| 60 | +{ |
| 61 | + auto result = f(); |
| 62 | + if (!result) { |
| 63 | + ::SymRefreshModuleList(::GetCurrentProcess()); |
| 64 | + result = f(); |
| 65 | + } |
| 66 | + return result; |
| 67 | +} |
| 68 | + |
| 69 | +// Captured addresses are return addresses; subtract 1 to point into the |
| 70 | +// call instruction itself. |
| 71 | +IMAGEHLP_LINE64 get_line_info(void* address) noexcept |
| 72 | +{ |
| 73 | + IMAGEHLP_LINE64 line = {}; |
| 74 | + line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| 75 | + if (!address || !dbg_init()) return line; |
| 76 | + |
| 77 | + DWORD displacement = 0; |
| 78 | + DWORD64 const addr = reinterpret_cast<DWORD64>(address) - 1; |
| 79 | + |
| 80 | + std::lock_guard<std::mutex> const lock(dbg_mutex()); |
| 81 | + bool const found = with_module_refresh([&]() noexcept { |
| 82 | + return ::SymGetLineFromAddr64( |
| 83 | + ::GetCurrentProcess(), addr, &displacement, &line |
| 84 | + ) != FALSE; |
| 85 | + }); |
| 86 | + if (!found) return IMAGEHLP_LINE64{}; |
| 87 | + return line; |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace |
| 91 | + |
| 92 | +void capture( |
| 93 | + std::vector<stacktrace_entry>& frames, std::size_t skip, |
| 94 | + std::size_t max_depth |
| 95 | +) noexcept |
| 96 | +{ |
| 97 | + if (max_depth == 0) return; |
| 98 | + |
| 99 | + // +1 to skip this capture() frame itself in the backtrace. |
| 100 | + constexpr std::size_t overhead = 1; |
| 101 | + if (skip > |
| 102 | + static_cast<std::size_t>(std::numeric_limits<ULONG>::max()) - overhead) |
| 103 | + return; |
| 104 | + ULONG const first = static_cast<ULONG>(skip + overhead); |
| 105 | + |
| 106 | + constexpr std::size_t kInitial = 128; |
| 107 | + // The captured count is reported as USHORT. |
| 108 | + constexpr std::size_t kHardLimit = std::numeric_limits<USHORT>::max(); |
| 109 | + |
| 110 | + void* stack_buf[kInitial]; |
| 111 | + void** buf = stack_buf; |
| 112 | + std::size_t cap = kInitial; |
| 113 | + USHORT got = |
| 114 | + ::CaptureStackBackTrace(first, static_cast<ULONG>(cap), buf, nullptr); |
| 115 | + |
| 116 | + std::vector<void*> heap_buf; |
| 117 | + try { |
| 118 | + while (static_cast<std::size_t>(got) == cap && cap < kHardLimit) { |
| 119 | + if (static_cast<std::size_t>(got) >= max_depth) break; |
| 120 | + |
| 121 | + cap = std::min(cap * 2, kHardLimit); |
| 122 | + heap_buf.resize(cap); |
| 123 | + buf = heap_buf.data(); |
| 124 | + got = ::CaptureStackBackTrace( |
| 125 | + first, static_cast<ULONG>(cap), buf, nullptr |
| 126 | + ); |
| 127 | + } |
| 128 | + } catch (...) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + std::size_t const take = std::min(static_cast<std::size_t>(got), max_depth); |
| 133 | + try { |
| 134 | + for (std::size_t i = 0; i < take; ++i) |
| 135 | + frames.push_back(capture_helper::make(buf[i])); |
| 136 | + } catch (...) { |
| 137 | + // Keep the frames captured so far. |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +std::string symbolize_description(void* address) |
| 142 | +{ |
| 143 | + if (!address || !dbg_init()) return {}; |
| 144 | + |
| 145 | + // SYMBOL_INFO::Name is a trailing array; make room for kMaxNameLen chars. |
| 146 | + constexpr ULONG kMaxNameLen = 256; |
| 147 | + |
| 148 | + struct |
| 149 | + { |
| 150 | + SYMBOL_INFO sym; |
| 151 | + char overflow[kMaxNameLen - 1]; |
| 152 | + } buf = {}; |
| 153 | + |
| 154 | + buf.sym.SizeOfStruct = sizeof(SYMBOL_INFO); |
| 155 | + buf.sym.MaxNameLen = kMaxNameLen; |
| 156 | + |
| 157 | + std::lock_guard<std::mutex> const lock(dbg_mutex()); |
| 158 | + DWORD64 const addr = reinterpret_cast<DWORD64>(address) - 1; |
| 159 | + bool const found = with_module_refresh([&]() noexcept { |
| 160 | + return ::SymFromAddr(::GetCurrentProcess(), addr, nullptr, &buf.sym) != |
| 161 | + FALSE; |
| 162 | + }); |
| 163 | + if (!found) return {}; |
| 164 | + return buf.sym.Name; |
| 165 | +} |
| 166 | + |
| 167 | +std::string symbolize_source_file(void* address) |
| 168 | +{ |
| 169 | + IMAGEHLP_LINE64 const line = get_line_info(address); |
| 170 | + return line.FileName ? std::string(line.FileName) : std::string{}; |
| 171 | +} |
| 172 | + |
| 173 | +std::uint_least32_t symbolize_source_line(void* address) |
| 174 | +{ |
| 175 | + IMAGEHLP_LINE64 const line = get_line_info(address); |
| 176 | + return static_cast<std::uint_least32_t>(line.LineNumber); |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace detail |
| 180 | +} // namespace eggs |
0 commit comments