Debian Codegraph @

src/platform/dbghelp_utils.cpp

raw
1 #include "platform/platform.hpp"
2
3 #if IS_WINDOWS
4
5 #include "platform/dbghelp_utils.hpp"
6
7 #if defined(CPPTRACE_UNWIND_WITH_DBGHELP) \
8 || defined(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP) \
9 || defined(CPPTRACE_DEMANGLE_WITH_WINAPI)
10
11 #include "utils/error.hpp"
12 #include "utils/microfmt.hpp"
13 #include "utils/utils.hpp"
14
15 #include <unordered_map>
16
17 #ifndef WIN32_LEAN_AND_MEAN
18 #define WIN32_LEAN_AND_MEAN
19 #endif
20 #include <windows.h>
21 #include <dbghelp.h>
22
23 CPPTRACE_BEGIN_NAMESPACE
24 namespace detail {
25 dbghelp_syminit_info::dbghelp_syminit_info(void* handle, bool should_sym_cleanup, bool should_close_handle)
26 : handle(handle), should_sym_cleanup(should_sym_cleanup), should_close_handle(should_close_handle) {}
27
28 dbghelp_syminit_info::~dbghelp_syminit_info() {
29 release();
30 }
31
32 void dbghelp_syminit_info::release() {
33 if(!handle) {
34 return;
35 }
36 if(should_sym_cleanup) {
37 if(!SymCleanup(handle)) {
38 throw internal_error("SymCleanup failed with code {}\n", GetLastError());
39 }
40 }
41 if(should_close_handle) {
42 if(!CloseHandle(handle)) {
43 throw internal_error("CloseHandle failed with code {}\n", GetLastError());
44 }
45 }
46 }
47
48 dbghelp_syminit_info dbghelp_syminit_info::make_not_owned(void* handle) {
49 return dbghelp_syminit_info(handle, false, false);
50 }
51
52 dbghelp_syminit_info dbghelp_syminit_info::make_owned(void* handle, bool should_close_handle) {
53 return dbghelp_syminit_info(handle, true, should_close_handle);
54 }
55
56 dbghelp_syminit_info::dbghelp_syminit_info(dbghelp_syminit_info&& other) {
57 handle = exchange(other.handle, nullptr);
58 should_sym_cleanup = other.should_sym_cleanup;
59 should_close_handle = other.should_close_handle;
60 }
61
62 dbghelp_syminit_info& dbghelp_syminit_info::operator=(dbghelp_syminit_info&& other) {
63 release();
64 handle = exchange(other.handle, nullptr);
65 should_sym_cleanup = other.should_sym_cleanup;
66 should_close_handle = other.should_close_handle;
67 return *this;
68 }
69
70 void* dbghelp_syminit_info::get_process_handle() const {
71 return handle;
72 }
73
74 dbghelp_syminit_info dbghelp_syminit_info::make_non_owning_view() const {
75 return make_not_owned(handle);
76 }
77
78 std::unordered_map<HANDLE, dbghelp_syminit_info>& get_syminit_cache() {
79 static std::unordered_map<HANDLE, dbghelp_syminit_info> syminit_cache;
80 return syminit_cache;
81 }
82
83 dbghelp_syminit_info ensure_syminit() {
84 auto lock = get_dbghelp_lock(); // locking around the entire access of the cache unordered_map
85 HANDLE proc = GetCurrentProcess();
86 if(get_cache_mode() == cache_mode::prioritize_speed) {
87 auto& syminit_cache = get_syminit_cache();
88 auto it = syminit_cache.find(proc);
89 if(it != syminit_cache.end()) {
90 return it->second.make_non_owning_view();
91 }
92 }
93
94 auto duplicated_handle = raii_wrap<void*>(nullptr, [] (void* handle) {
95 if(handle) {
96 if(!CloseHandle(handle)) {
97 throw internal_error("CloseHandle failed with code {}\n", GetLastError());
98 }
99 }
100 });
101 // https://github.com/jeremy-rifkin/cpptrace/issues/204
102 // https://github.com/jeremy-rifkin/cpptrace/pull/206
103 // https://learn.microsoft.com/en-us/windows/win32/debug/initializing-the-symbol-handler
104 // Apparently duplicating the process handle is the idiomatic thing to do and this avoids issues of
105 // SymInitialize being called twice.
106 // DuplicateHandle requires the PROCESS_DUP_HANDLE access right. If for some reason DuplicateHandle we fall back
107 // to calling SymInitialize on the process handle.
108 optional<DWORD> maybe_duplicate_handle_error_code;
109 if(!DuplicateHandle(proc, proc, proc, &duplicated_handle.get(), 0, FALSE, DUPLICATE_SAME_ACCESS)) {
110 maybe_duplicate_handle_error_code = GetLastError();
111 }
112 if(!SymInitialize(maybe_duplicate_handle_error_code ? proc : duplicated_handle.get(), NULL, TRUE)) {
113 if(maybe_duplicate_handle_error_code) {
114 throw internal_error(
115 "SymInitialize failed with error code {} after DuplicateHandle failed with error code {}",
116 GetLastError(),
117 maybe_duplicate_handle_error_code.unwrap()
118 );
119 } else {
120 throw internal_error("SymInitialize failed with error code {}", GetLastError());
121 }
122 }
123
124 auto info = dbghelp_syminit_info::make_owned(
125 maybe_duplicate_handle_error_code ? proc : exchange(duplicated_handle.get(), nullptr),
126 !maybe_duplicate_handle_error_code
127 );
128 // either cache and return a view or return the owning wrapper
129 if(get_cache_mode() == cache_mode::prioritize_speed) {
130 auto& syminit_cache = get_syminit_cache();
131 auto pair = syminit_cache.insert({proc, std::move(info)});
132 VERIFY(pair.second);
133 return pair.first->second.make_non_owning_view();
134 } else {
135 return info;
136 }
137 }
138
139 std::unique_lock<std::recursive_mutex> get_dbghelp_lock() {
140 static std::recursive_mutex mutex;
141 return std::unique_lock<std::recursive_mutex>{mutex};
142 }
143 }
144 CPPTRACE_END_NAMESPACE
145
146 #endif
147
148 #endif