Debian Codegraph @

src/platform/dbghelp_utils.hpp

raw
1 #ifndef DBGHELP_UTILS_HPP
2 #define DBGHELP_UTILS_HPP
3
4 #if defined(CPPTRACE_UNWIND_WITH_DBGHELP) \
5 || defined(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP) \
6 || defined(CPPTRACE_DEMANGLE_WITH_WINAPI)
7
8 #include "utils/common.hpp"
9
10 #include <unordered_map>
11 #include <mutex>
12
13 CPPTRACE_BEGIN_NAMESPACE
14 namespace detail {
15 class dbghelp_syminit_info {
16 // `void*` is used to avoid including the (expensive) windows.h header here
17 void* handle = nullptr;
18 bool should_sym_cleanup; // true if cleanup is not managed by the syminit cache
19 bool should_close_handle; // true if cleanup is not managed by the syminit cache and the handle was duplicated
20 dbghelp_syminit_info(void* handle, bool should_sym_cleanup, bool should_close_handle);
21 public:
22 ~dbghelp_syminit_info();
23 void release();
24
25 NODISCARD static dbghelp_syminit_info make_not_owned(void* handle);
26 NODISCARD static dbghelp_syminit_info make_owned(void* handle, bool should_close_handle);
27
28 dbghelp_syminit_info(const dbghelp_syminit_info&) = delete;
29 dbghelp_syminit_info(dbghelp_syminit_info&&);
30 dbghelp_syminit_info& operator=(const dbghelp_syminit_info&) = delete;
31 dbghelp_syminit_info& operator=(dbghelp_syminit_info&&);
32
33 void* get_process_handle() const;
34 dbghelp_syminit_info make_non_owning_view() const;
35 };
36
37 // Ensure SymInitialize is called on the process. This function either
38 // - Finds that SymInitialize has been called for a handle to the current process already, in which case it returns
39 // a non-owning dbghelp_syminit_info instance holding the handle
40 // - Calls SymInitialize a handle to the current process, caches it, and returns a non-owning dbghelp_syminit_info
41 // - Calls SymInitialize and returns an owning dbghelp_syminit_info which will handle cleanup
42 dbghelp_syminit_info ensure_syminit();
43
44 NODISCARD std::unique_lock<std::recursive_mutex> get_dbghelp_lock();
45 }
46 CPPTRACE_END_NAMESPACE
47
48 #endif
49
50 #endif