diff options
-rw-r--r-- | include/pdb/address.hpp | 10 | ||||
-rw-r--r-- | include/pdb/module.hpp | 34 | ||||
-rw-r--r-- | src/repo.cpp | 18 | ||||
-rw-r--r-- | utils/addr2name.cpp | 12 | ||||
-rw-r--r-- | utils/name2addr.cpp | 10 |
5 files changed, 53 insertions, 31 deletions
diff --git a/include/pdb/address.hpp b/include/pdb/address.hpp index 53b25e8..33e0ac3 100644 --- a/include/pdb/address.hpp +++ b/include/pdb/address.hpp @@ -7,7 +7,17 @@ #include <Windows.h> +#include <sstream> +#include <string> + namespace pdb { typedef DWORD64 Address; + + inline std::string format_address(Address address) + { + std::ostringstream oss; + oss << std::hex << std::showbase << address; + return oss.str(); + } } diff --git a/include/pdb/module.hpp b/include/pdb/module.hpp index 118a53b..3e785c4 100644 --- a/include/pdb/module.hpp +++ b/include/pdb/module.hpp @@ -12,6 +12,8 @@ #include <cstring> +#include <sstream> +#include <stdexcept> #include <string> namespace pdb @@ -59,7 +61,39 @@ namespace pdb Address get_online_base() const { return online_base; } + Address translate_offline_address(Address offline) const + { + if (offline < get_offline_base()) + throw std::range_error{invalid_offline_address(offline)}; + return offline - get_offline_base() + get_online_base(); + } + + Address translate_online_address(Address online) const + { + if (online < get_online_base()) + throw std::range_error{invalid_online_address(online)}; + return online - get_online_base() + get_offline_base(); + } + private: + std::string invalid_offline_address(Address offline) const + { + std::ostringstream oss; + oss << "offline address " << format_address(offline) + << " doesn't belong to module " << get_name() + << " (base offline address " << format_address(get_offline_base()) << ')'; + return oss.str(); + } + + std::string invalid_online_address(Address online) const + { + std::ostringstream oss; + oss << "online address " << format_address(online) + << " doesn't belong to module " << get_name() + << " (base online address " << format_address(get_online_base()) << ')'; + return oss.str(); + } + const Address online_base; }; } diff --git a/src/repo.cpp b/src/repo.cpp index 0b28d5e..14a0933 100644 --- a/src/repo.cpp +++ b/src/repo.cpp @@ -40,16 +40,6 @@ namespace pdb return it->second; } - - Address address_online_to_offline(const Module& module, Address online) - { - return module.get_offline_base() + online - module.get_online_base(); - } - - Address address_offline_to_online(const Module& module, Address offline) - { - return module.get_online_base() + offline - module.get_offline_base(); - } } Address Repo::add_pdb(Address online_base, const std::string& path) @@ -103,17 +93,19 @@ namespace pdb Symbol Repo::symbol_from_buffer(const Module& module, const SymbolInfo& raw) const { - return {pdb::address_offline_to_online(module, raw.get_offline_address()), raw}; + return {module.translate_offline_address(raw.get_offline_address()), raw}; } Address Repo::address_online_to_offline(Address online) const { - return pdb::address_online_to_offline(module_from_online_address(online), online); + return module_from_online_address(online) + .translate_online_address(online); } Address Repo::address_offline_to_online(Address offline) const { - return pdb::address_offline_to_online(module_from_offline_address(offline), offline); + return module_from_offline_address(offline) + .translate_offline_address(offline); } const Module& Repo::module_from_online_address(Address online) const diff --git a/utils/addr2name.cpp b/utils/addr2name.cpp index 9719b60..a564e42 100644 --- a/utils/addr2name.cpp +++ b/utils/addr2name.cpp @@ -12,6 +12,7 @@ #include <exception> #include <iostream> +#include <sstream> #include <string> #include <vector> @@ -65,20 +66,13 @@ namespace bool help_flag = false; }; - std::string format_address(pdb::Address address) - { - std::ostringstream oss; - oss << std::showbase << std::hex << address; - return oss.str(); - } - std::string format_symbol(const pdb::Symbol& symbol) { std::ostringstream oss; oss << symbol.get_name(); const auto displacement = symbol.get_displacement(); if (displacement) - oss << '+' << format_address(displacement); + oss << '+' << pdb::format_address(displacement); return oss.str(); } @@ -96,7 +90,7 @@ namespace catch (const std::exception& e) { dump_error(e); - std::cout << format_address(address) << '\n'; + std::cout << pdb::format_address(address) << '\n'; } } } diff --git a/utils/name2addr.cpp b/utils/name2addr.cpp index 055b469..c6602c9 100644 --- a/utils/name2addr.cpp +++ b/utils/name2addr.cpp @@ -12,7 +12,6 @@ #include <exception> #include <iostream> -#include <sstream> #include <string> #include <vector> @@ -66,13 +65,6 @@ namespace bool help_flag = false; }; - std::string format_address(pdb::Address address) - { - std::ostringstream oss; - oss << std::showbase << std::hex << address; - return oss.str(); - } - void dump_error(const std::exception& e) { std::cerr << "error: " << e.what() << '\n'; @@ -83,7 +75,7 @@ namespace try { const auto address = repo.resolve_symbol(name).get_online_address(); - std::cout << format_address(address) << '\n'; + std::cout << pdb::format_address(address) << '\n'; } catch (const std::exception& e) { |