From e162f2a21632e3f7b1c7ab9dd7b530beb1f2d4a8 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 29 May 2021 01:24:29 +0300 Subject: rename class Repo to PostMortem --- include/winapi/debug.hpp | 2 +- include/winapi/debug/post_mortem.hpp | 58 ++++++++++++ include/winapi/debug/repo.hpp | 58 ------------ src/post_mortem.cpp | 171 +++++++++++++++++++++++++++++++++++ src/repo.cpp | 171 ----------------------------------- utils/addr2name.cpp | 16 ++-- utils/name2addr.cpp | 10 +- 7 files changed, 244 insertions(+), 242 deletions(-) create mode 100644 include/winapi/debug/post_mortem.hpp delete mode 100644 include/winapi/debug/repo.hpp create mode 100644 src/post_mortem.cpp delete mode 100644 src/repo.cpp diff --git a/include/winapi/debug.hpp b/include/winapi/debug.hpp index dc42557..93b952f 100644 --- a/include/winapi/debug.hpp +++ b/include/winapi/debug.hpp @@ -9,5 +9,5 @@ #include "debug/call_stack.hpp" #include "debug/dbghelp.hpp" #include "debug/module.hpp" -#include "debug/repo.hpp" +#include "debug/post_mortem.hpp" #include "debug/symbol.hpp" diff --git a/include/winapi/debug/post_mortem.hpp b/include/winapi/debug/post_mortem.hpp new file mode 100644 index 0000000..bf4ad83 --- /dev/null +++ b/include/winapi/debug/post_mortem.hpp @@ -0,0 +1,58 @@ +// Copyright (c) 2017 Egor Tensin +// This file is part of the "winapi-debug" project. +// For details, see https://github.com/egor-tensin/winapi-debug. +// Distributed under the MIT License. + +#pragma once + +#include "address.hpp" +#include "dbghelp.hpp" +#include "module.hpp" +#include "symbol.hpp" + +#include + +#include +#include +#include +#include + +namespace winapi { + +class PostMortem { +public: + PostMortem() = default; + + Address add_pdb(Address online_base, const std::string& path); + + typedef std::function OnSymbol; + void enum_symbols(const OnSymbol&) const; + void enum_symbols(Address offline_base, const OnSymbol&) const; + void enum_symbols(const Module&, const OnSymbol&) const; + + Symbol resolve_symbol(Address) const; + Symbol resolve_symbol(const std::string&) const; + + LineInfo resolve_line(Address) const; + + const Module& module_with_online_base(Address) const; + const Module& module_with_offline_base(Address) const; + +private: + Symbol symbol_from_buffer(const SymbolInfo&) const; + static Symbol symbol_from_buffer(const Module&, const SymbolInfo&); + + const Module& module_from_online_address(Address) const; + const Module& module_from_offline_address(Address) const; + + Address address_offline_to_online(Address) const; + Address address_online_to_offline(Address) const; + + const DbgHelp dbghelp{DbgHelp::post_mortem()}; + + std::unordered_set file_ids; + std::map online_bases; + std::map offline_bases; +}; + +} // namespace winapi diff --git a/include/winapi/debug/repo.hpp b/include/winapi/debug/repo.hpp deleted file mode 100644 index 8ee7fde..0000000 --- a/include/winapi/debug/repo.hpp +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2017 Egor Tensin -// This file is part of the "winapi-debug" project. -// For details, see https://github.com/egor-tensin/winapi-debug. -// Distributed under the MIT License. - -#pragma once - -#include "address.hpp" -#include "dbghelp.hpp" -#include "module.hpp" -#include "symbol.hpp" - -#include - -#include -#include -#include -#include - -namespace winapi { - -class Repo { -public: - Repo() = default; - - Address add_pdb(Address online_base, const std::string& path); - - typedef std::function OnSymbol; - void enum_symbols(const OnSymbol&) const; - void enum_symbols(Address offline_base, const OnSymbol&) const; - void enum_symbols(const Module&, const OnSymbol&) const; - - Symbol resolve_symbol(Address) const; - Symbol resolve_symbol(const std::string&) const; - - LineInfo resolve_line(Address) const; - - const Module& module_with_online_base(Address) const; - const Module& module_with_offline_base(Address) const; - -private: - Symbol symbol_from_buffer(const SymbolInfo&) const; - static Symbol symbol_from_buffer(const Module&, const SymbolInfo&); - - const Module& module_from_online_address(Address) const; - const Module& module_from_offline_address(Address) const; - - Address address_offline_to_online(Address) const; - Address address_online_to_offline(Address) const; - - const DbgHelp dbghelp{DbgHelp::post_mortem()}; - - std::unordered_set file_ids; - std::map online_bases; - std::map offline_bases; -}; - -} // namespace winapi diff --git a/src/post_mortem.cpp b/src/post_mortem.cpp new file mode 100644 index 0000000..d365b02 --- /dev/null +++ b/src/post_mortem.cpp @@ -0,0 +1,171 @@ +// Copyright (c) 2017 Egor Tensin +// This file is part of the "winapi-debug" project. +// For details, see https://github.com/egor-tensin/winapi-debug. +// Distributed under the MIT License. + +#include +#include + +#include +#include +#include +#include +#include + +namespace winapi { +namespace { + +std::string pdb_already_loaded(Address online_base, const std::string& path) { + std::ostringstream oss; + oss << "module with online base address " << format_address(online_base) + << " has already been loaded: " << path; + return oss.str(); +} + +std::string pdb_already_loaded(const std::string& path) { + std::ostringstream oss; + oss << "module has already been loaded: " << path; + return oss.str(); +} + +std::string offline_base_already_used(Address base) { + std::ostringstream oss; + oss << "module with offline base address " << format_address(base) + << " has already been loaded (shouldn't happen)"; + return oss.str(); +} + +std::string module_not_found(Address base) { + std::ostringstream oss; + oss << "module with base address " << format_address(base) << " wasn't found"; + return oss.str(); +} + +std::string guess_module_no_modules(Address pivot) { + std::ostringstream oss; + oss << "couldn't select a module for address " << format_address(pivot) + << ": no modules have been loaded yet"; + return oss.str(); +} + +std::string guess_module_address_too_low(Address pivot) { + std::ostringstream oss; + oss << "couldn't select a module for address " << format_address(pivot) << ": it's too low"; + return oss.str(); +} + +template +const Module& lookup_module(const std::map& modules, Address base) { + const auto it = modules.find(base); + if (it == modules.cend()) + throw std::runtime_error{module_not_found(base)}; + return it->second; +} + +template +const Module& guess_module(const std::map& modules, Address pivot) { + if (modules.empty()) + throw std::range_error{guess_module_no_modules(pivot)}; + + auto it = modules.lower_bound(pivot); + + if (it == modules.cend()) { + --it; + return it->second; + } + + if (it->first > pivot) { + if (it == modules.cbegin()) + throw std::range_error{guess_module_address_too_low(pivot)}; + --it; + return it->second; + } + + return it->second; +} + +} // namespace + +Address PostMortem::add_pdb(Address online_base, const std::string& path) { + if (online_bases.find(online_base) != online_bases.cend()) + throw std::runtime_error{pdb_already_loaded(online_base, path)}; + + auto file_id = winapi::File::open_read_attributes(path).query_id(); + if (file_ids.find(file_id) != file_ids.cend()) + throw std::runtime_error{pdb_already_loaded(path)}; + + Module module{online_base, dbghelp.load_pdb(path)}; + const auto offline_base = module.get_offline_base(); + + if (offline_bases.find(offline_base) != offline_bases.cend()) + throw std::runtime_error{offline_base_already_used(offline_base)}; + + file_ids.emplace(std::move(file_id)); + const auto it = online_bases.emplace(online_base, std::move(module)); + offline_bases.emplace(offline_base, it.first->second); + + return offline_base; +} + +void PostMortem::enum_symbols(const OnSymbol& callback) const { + for (const auto& it : offline_bases) + enum_symbols(it.second, callback); +} + +void PostMortem::enum_symbols(Address offline_base, const OnSymbol& callback) const { + const auto it = offline_bases.find(offline_base); + if (it == offline_bases.cend()) + throw std::runtime_error{"unknown module"}; + enum_symbols(it->second, callback); +} + +void PostMortem::enum_symbols(const Module& module, const OnSymbol& callback) const { + dbghelp.enum_symbols( + module, [&](const SymbolInfo& impl) { callback(symbol_from_buffer(module, impl)); }); +} + +Symbol PostMortem::resolve_symbol(Address online) const { + return symbol_from_buffer(dbghelp.resolve_symbol(address_online_to_offline(online))); +} + +Symbol PostMortem::resolve_symbol(const std::string& name) const { + return symbol_from_buffer(dbghelp.resolve_symbol(name)); +} + +LineInfo PostMortem::resolve_line(Address online) const { + return dbghelp.resolve_line(address_online_to_offline(online)); +} + +const Module& PostMortem::module_with_online_base(Address base) const { + return lookup_module(online_bases, base); +} + +const Module& PostMortem::module_with_offline_base(Address base) const { + return lookup_module(offline_bases, base); +} + +Symbol PostMortem::symbol_from_buffer(const SymbolInfo& impl) const { + return symbol_from_buffer(module_with_offline_base(impl.get_offline_base()), impl); +} + +Symbol PostMortem::symbol_from_buffer(const Module& module, const SymbolInfo& impl) { + return {module.translate_offline_address(impl.get_offline_address()), impl}; +} + +Address PostMortem::address_online_to_offline(Address online) const { + return module_from_online_address(online).translate_online_address(online); +} + +Address PostMortem::address_offline_to_online(Address offline) const { + return module_from_offline_address(offline).translate_offline_address(offline); +} + +const Module& PostMortem::module_from_online_address(Address online) const { + return guess_module(online_bases, online); +} + +const Module& PostMortem::module_from_offline_address(Address offline) const { + return guess_module(offline_bases, offline); +} + +} // namespace winapi diff --git a/src/repo.cpp b/src/repo.cpp deleted file mode 100644 index 24d9e59..0000000 --- a/src/repo.cpp +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright (c) 2017 Egor Tensin -// This file is part of the "winapi-debug" project. -// For details, see https://github.com/egor-tensin/winapi-debug. -// Distributed under the MIT License. - -#include -#include - -#include -#include -#include -#include -#include - -namespace winapi { -namespace { - -std::string pdb_already_loaded(Address online_base, const std::string& path) { - std::ostringstream oss; - oss << "module with online base address " << format_address(online_base) - << " has already been loaded: " << path; - return oss.str(); -} - -std::string pdb_already_loaded(const std::string& path) { - std::ostringstream oss; - oss << "module has already been loaded: " << path; - return oss.str(); -} - -std::string offline_base_already_used(Address base) { - std::ostringstream oss; - oss << "module with offline base address " << format_address(base) - << " has already been loaded (shouldn't happen)"; - return oss.str(); -} - -std::string module_not_found(Address base) { - std::ostringstream oss; - oss << "module with base address " << format_address(base) << " wasn't found"; - return oss.str(); -} - -std::string guess_module_no_modules(Address pivot) { - std::ostringstream oss; - oss << "couldn't select a module for address " << format_address(pivot) - << ": no modules have been loaded yet"; - return oss.str(); -} - -std::string guess_module_address_too_low(Address pivot) { - std::ostringstream oss; - oss << "couldn't select a module for address " << format_address(pivot) << ": it's too low"; - return oss.str(); -} - -template -const Module& lookup_module(const std::map& modules, Address base) { - const auto it = modules.find(base); - if (it == modules.cend()) - throw std::runtime_error{module_not_found(base)}; - return it->second; -} - -template -const Module& guess_module(const std::map& modules, Address pivot) { - if (modules.empty()) - throw std::range_error{guess_module_no_modules(pivot)}; - - auto it = modules.lower_bound(pivot); - - if (it == modules.cend()) { - --it; - return it->second; - } - - if (it->first > pivot) { - if (it == modules.cbegin()) - throw std::range_error{guess_module_address_too_low(pivot)}; - --it; - return it->second; - } - - return it->second; -} - -} // namespace - -Address Repo::add_pdb(Address online_base, const std::string& path) { - if (online_bases.find(online_base) != online_bases.cend()) - throw std::runtime_error{pdb_already_loaded(online_base, path)}; - - auto file_id = winapi::File::open_read_attributes(path).query_id(); - if (file_ids.find(file_id) != file_ids.cend()) - throw std::runtime_error{pdb_already_loaded(path)}; - - Module module{online_base, dbghelp.load_pdb(path)}; - const auto offline_base = module.get_offline_base(); - - if (offline_bases.find(offline_base) != offline_bases.cend()) - throw std::runtime_error{offline_base_already_used(offline_base)}; - - file_ids.emplace(std::move(file_id)); - const auto it = online_bases.emplace(online_base, std::move(module)); - offline_bases.emplace(offline_base, it.first->second); - - return offline_base; -} - -void Repo::enum_symbols(const OnSymbol& callback) const { - for (const auto& it : offline_bases) - enum_symbols(it.second, callback); -} - -void Repo::enum_symbols(Address offline_base, const OnSymbol& callback) const { - const auto it = offline_bases.find(offline_base); - if (it == offline_bases.cend()) - throw std::runtime_error{"unknown module"}; - enum_symbols(it->second, callback); -} - -void Repo::enum_symbols(const Module& module, const OnSymbol& callback) const { - dbghelp.enum_symbols( - module, [&](const SymbolInfo& impl) { callback(symbol_from_buffer(module, impl)); }); -} - -Symbol Repo::resolve_symbol(Address online) const { - return symbol_from_buffer(dbghelp.resolve_symbol(address_online_to_offline(online))); -} - -Symbol Repo::resolve_symbol(const std::string& name) const { - return symbol_from_buffer(dbghelp.resolve_symbol(name)); -} - -LineInfo Repo::resolve_line(Address online) const { - return dbghelp.resolve_line(address_online_to_offline(online)); -} - -const Module& Repo::module_with_online_base(Address base) const { - return lookup_module(online_bases, base); -} - -const Module& Repo::module_with_offline_base(Address base) const { - return lookup_module(offline_bases, base); -} - -Symbol Repo::symbol_from_buffer(const SymbolInfo& impl) const { - return symbol_from_buffer(module_with_offline_base(impl.get_offline_base()), impl); -} - -Symbol Repo::symbol_from_buffer(const Module& module, const SymbolInfo& impl) { - return {module.translate_offline_address(impl.get_offline_address()), impl}; -} - -Address Repo::address_online_to_offline(Address online) const { - return module_from_online_address(online).translate_online_address(online); -} - -Address Repo::address_offline_to_online(Address offline) const { - return module_from_offline_address(offline).translate_offline_address(offline); -} - -const Module& Repo::module_from_online_address(Address online) const { - return guess_module(online_bases, online); -} - -const Module& Repo::module_from_offline_address(Address offline) const { - return guess_module(offline_bases, offline); -} - -} // namespace winapi diff --git a/utils/addr2name.cpp b/utils/addr2name.cpp index 611e02b..5a8cbc3 100644 --- a/utils/addr2name.cpp +++ b/utils/addr2name.cpp @@ -62,17 +62,19 @@ void dump_error(const std::exception& e) { std::cerr << "error: " << e.what() << '\n'; } -void resolve_symbol(const winapi::Repo& repo, winapi::Address address, bool lines = false) { +void resolve_symbol(const winapi::PostMortem& analysis, + winapi::Address address, + bool lines = false) { try { - const auto symbol = repo.resolve_symbol(address); - const auto& module = repo.module_with_offline_base(symbol.get_offline_base()); + const auto symbol = analysis.resolve_symbol(address); + const auto& module = analysis.module_with_offline_base(symbol.get_offline_base()); std::ostringstream msg; msg << format_symbol(module, symbol); if (lines) { try { - const auto line_info = repo.resolve_line(address); + const auto line_info = analysis.resolve_line(address); msg << ' ' << format_line_info(line_info); } catch (const std::exception& e) { dump_error(e); @@ -104,13 +106,13 @@ int main(int argc, char* argv[]) { return 0; } - winapi::Repo repo; + winapi::PostMortem analysis; for (const auto& pdb : settings.pdbs) - repo.add_pdb(pdb.online_base, pdb.path); + analysis.add_pdb(pdb.online_base, pdb.path); for (const auto& address : settings.addresses) - resolve_symbol(repo, address, settings.lines); + resolve_symbol(analysis, address, settings.lines); } catch (const std::exception& e) { dump_error(e); return 1; diff --git a/utils/name2addr.cpp b/utils/name2addr.cpp index db07c3a..c113a9a 100644 --- a/utils/name2addr.cpp +++ b/utils/name2addr.cpp @@ -42,9 +42,9 @@ void dump_error(const std::exception& e) { std::cerr << "error: " << e.what() << '\n'; } -void resolve_symbol(const winapi::Repo& repo, const std::string& name) { +void resolve_symbol(const winapi::PostMortem& analysis, const std::string& name) { try { - const auto address = repo.resolve_symbol(name).get_online_address(); + const auto address = analysis.resolve_symbol(name).get_online_address(); std::cout << winapi::format_address(address) << '\n'; } catch (const std::exception& e) { dump_error(e); @@ -70,13 +70,13 @@ int main(int argc, char* argv[]) { return 0; } - winapi::Repo repo; + winapi::PostMortem analysis; for (const auto& pdb : settings.pdbs) - repo.add_pdb(pdb.online_base, pdb.path); + analysis.add_pdb(pdb.online_base, pdb.path); for (const auto& name : settings.names) - resolve_symbol(repo, name); + resolve_symbol(analysis, name); } catch (const std::exception& e) { dump_error(e); return 1; -- cgit v1.2.3