diff options
Diffstat (limited to 'utils/addr2name.cpp')
-rw-r--r-- | utils/addr2name.cpp | 163 |
1 files changed, 68 insertions, 95 deletions
diff --git a/utils/addr2name.cpp b/utils/addr2name.cpp index 87206f2..fe749b8 100644 --- a/utils/addr2name.cpp +++ b/utils/addr2name.cpp @@ -4,9 +4,8 @@ // Distributed under the MIT License. #include "command_line.hpp" -#include "pdb_descr.hpp" - #include "pdb/all.hpp" +#include "pdb_descr.hpp" #pragma warning(push, 0) #include <boost/program_options.hpp> @@ -18,113 +17,89 @@ #include <string> #include <vector> -namespace -{ - class Addr2Name : public SettingsParser - { - public: - explicit Addr2Name(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("pdb", - boost::program_options::value<std::vector<PDB>>(&pdbs) - ->value_name("ADDR,PATH"), - "load a PDB file") - ("lines,l", - boost::program_options::bool_switch(&lines), - "try to resolve source files & line numbers"); - hidden.add_options() - ("address", - boost::program_options::value<std::vector<pdb::Address>>(&addresses) - ->value_name("ADDR"), - "add an address to resolve"); - positional.add("address", -1); - } - - const char* get_short_description() const override - { - return "[-h|--help] [--pdb ADDR,PATH]... [--] [ADDR]..."; - } - - std::vector<PDB> pdbs; - std::vector<pdb::Address> addresses; - bool lines = false; - }; - - std::string format_symbol(const pdb::Module& module, const pdb::Symbol& symbol) - { - std::ostringstream oss; - oss << module.get_name() << '!' << symbol.get_name(); - const auto displacement = symbol.get_displacement(); - if (displacement) - oss << '+' << pdb::format_address(displacement); - return oss.str(); +namespace { + +class Addr2Name : public SettingsParser { +public: + explicit Addr2Name(const std::string& argv0) : SettingsParser{argv0} { + namespace po = boost::program_options; + + visible.add_options()( + "pdb", po::value<std::vector<PDB>>(&pdbs)->value_name("ADDR,PATH"), "load a PDB file"); + visible.add_options()( + "lines,l", po::bool_switch(&lines), "try to resolve source files & line numbers"); + hidden.add_options()("address", + po::value<std::vector<pdb::Address>>(&addresses)->value_name("ADDR"), + "add an address to resolve"); + positional.add("address", -1); } - std::string format_line_info(const pdb::LineInfo& line_info) - { - std::ostringstream oss; - oss << '[' << line_info.file_path << " @ " << line_info.line_number << ']'; - return oss.str(); + const char* get_short_description() const override { + return "[-h|--help] [--pdb ADDR,PATH]... [--] [ADDR]..."; } - void dump_error(const std::exception& e) - { - std::cerr << "error: " << e.what() << '\n'; - } + std::vector<PDB> pdbs; + std::vector<pdb::Address> addresses; + bool lines = false; +}; + +std::string format_symbol(const pdb::Module& module, const pdb::Symbol& symbol) { + std::ostringstream oss; + oss << module.get_name() << '!' << symbol.get_name(); + const auto displacement = symbol.get_displacement(); + if (displacement) + oss << '+' << pdb::format_address(displacement); + return oss.str(); +} - void resolve_symbol(const pdb::Repo& repo, pdb::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()); - - std::ostringstream msg; - msg << format_symbol(module, symbol); - - if (lines) - { - try - { - const auto line_info = repo.resolve_line(address); - msg << ' ' << format_line_info(line_info); - } - catch (const std::exception& e) - { - dump_error(e); - } - } +std::string format_line_info(const pdb::LineInfo& line_info) { + std::ostringstream oss; + oss << '[' << line_info.file_path << " @ " << line_info.line_number << ']'; + return oss.str(); +} - std::cout << msg.str() << '\n'; - } - catch (const std::exception& e) - { - dump_error(e); - std::cout << pdb::format_address(address) << '\n'; +void dump_error(const std::exception& e) { + std::cerr << "error: " << e.what() << '\n'; +} + +void resolve_symbol(const pdb::Repo& repo, pdb::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()); + + std::ostringstream msg; + msg << format_symbol(module, symbol); + + if (lines) { + try { + const auto line_info = repo.resolve_line(address); + msg << ' ' << format_line_info(line_info); + } catch (const std::exception& e) { + dump_error(e); + } } + + std::cout << msg.str() << '\n'; + } catch (const std::exception& e) { + dump_error(e); + std::cout << pdb::format_address(address) << '\n'; } } -int main(int argc, char* argv[]) -{ - try - { +} // namespace + +int main(int argc, char* argv[]) { + try { Addr2Name settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } @@ -136,9 +111,7 @@ int main(int argc, char* argv[]) for (const auto& address : settings.addresses) resolve_symbol(repo, address, settings.lines); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { dump_error(e); return 1; } |