From 52dba9e60ea7712da45d889159acc0a30f2fbf82 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 15 Jan 2020 05:04:51 +0300 Subject: DbgHelp: add more enum_* methods --- src/dbghelp.cpp | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/dbghelp.cpp b/src/dbghelp.cpp index 26eff79..8268673 100644 --- a/src/dbghelp.cpp +++ b/src/dbghelp.cpp @@ -45,6 +45,27 @@ Address gen_next_offline_base(std::size_t pdb_size) { return base; } +ModuleInfo get_module_info(HANDLE id, Address offline_base) { + ModuleInfo info; + + if (!SymGetModuleInfo64(id, offline_base, &static_cast(info))) + throw error::windows(GetLastError()); + + return info; +} + +struct ModuleEnumerator { + HANDLE id; + DbgHelp::OnModule callback; +}; + +BOOL CALLBACK enum_modules_callback(PCSTR, DWORD64 offline_base, PVOID raw_enumerator_ptr) { + const auto enumerator_ptr = reinterpret_cast(raw_enumerator_ptr); + const auto& enumerator = *enumerator_ptr; + enumerator.callback(get_module_info(enumerator.id, offline_base)); + return TRUE; +} + BOOL CALLBACK enum_symbols_callback(SYMBOL_INFO* info, ULONG, VOID* raw_callback_ptr) { const auto callback_ptr = reinterpret_cast(raw_callback_ptr); const auto& callback = *callback_ptr; @@ -90,16 +111,13 @@ ModuleInfo DbgHelp::load_pdb(const std::string& path) const { if (!offline_base) throw error::windows(GetLastError()); - return get_module_info(offline_base); + return get_module_info(id, offline_base); } -ModuleInfo DbgHelp::get_module_info(Address offline_base) const { - ModuleInfo info; - - if (!SymGetModuleInfo64(id, offline_base, &static_cast(info))) +void DbgHelp::enum_modules(const OnModule& callback) const { + ModuleEnumerator enumerator{id, callback}; + if (!SymEnumerateModules64(id, &enum_modules_callback, &enumerator)) throw error::windows(GetLastError()); - - return info; } void DbgHelp::enum_symbols(const ModuleInfo& module, const OnSymbol& callback) const { @@ -111,6 +129,13 @@ void DbgHelp::enum_symbols(const ModuleInfo& module, const OnSymbol& callback) c throw error::windows(GetLastError()); } +void DbgHelp::enum_symbols(const OnSymbol& callback) const { + static constexpr auto all_symbols = "*!*"; + if (!SymEnumSymbols( + id, 0, all_symbols, &enum_symbols_callback, const_cast(&callback))) + throw error::windows(GetLastError()); +} + SymbolInfo DbgHelp::resolve_symbol(Address offline) const { Address displacement = 0; SymbolInfo symbol; -- cgit v1.2.3