aboutsummaryrefslogblamecommitdiffstatshomepage
path: root/utils/name2addr.cpp
blob: 4b83757d7b60ebbeccb56303658a298e78fc9492 (plain) (tree)
1
2
3
4
5
6
7
8
9





                                                            
                      
                        
 
                                    



                                    


                 
           
 

                                         
                                                                            
                                              
 






                                                                                                   
 

                                                                 

     




                                          
                                                         




                                                                            
                                                                    

                                       
                                            


     



                                  
                                       
 
             
                                       
                                                          



                                    
                                       










                                                    
                                       




                      
// Copyright (c) 2017 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "PDB repository" project.
// For details, see https://github.com/egor-tensin/pdb-repo.
// Distributed under the MIT License.

#include "command_line.hpp"
#include "pdb/all.hpp"
#include "pdb_descr.hpp"

#include <boost/nowide/iostream.hpp>
#include <boost/program_options.hpp>

#include <exception>
#include <iostream>
#include <string>
#include <vector>

namespace {

class Name2Addr : public SettingsParser {
public:
    explicit Name2Addr(int argc, char** argv) : SettingsParser{argc, argv} {
        namespace po = boost::program_options;

        visible.add_options()(
            "pdb", po::value<std::vector<PDB>>(&pdbs)->value_name("ADDR,PATH"), "load a PDB file");
        hidden.add_options()("name",
                             po::value<std::vector<std::string>>(&names)->value_name("NAME"),
                             "add a name to resolve");
        positional.add("name", -1);
    }

    const char* get_short_description() const override {
        return "[-h|--help] [--pdb ADDR,PATH]... [--] [NAME]...";
    }

    std::vector<PDB> pdbs;
    std::vector<std::string> names;
};

void dump_error(const std::exception& e) {
    boost::nowide::cerr << "error: " << e.what() << '\n';
}

void resolve_symbol(const pdb::Repo& repo, const std::string& name) {
    try {
        const auto address = repo.resolve_symbol(name).get_online_address();
        boost::nowide::cout << pdb::format_address(address) << '\n';
    } catch (const std::exception& e) {
        dump_error(e);
        boost::nowide::cout << name << '\n';
    }
}

} // namespace

int main(int argc, char* argv[]) {
    try {
        Name2Addr settings{argc, argv};

        try {
            settings.parse(argc, argv);
        } catch (const boost::program_options::error& e) {
            settings.usage_error(e);
            return 1;
        }

        if (settings.exit_with_usage) {
            settings.usage();
            return 0;
        }

        pdb::Repo repo;

        for (const auto& pdb : settings.pdbs)
            repo.add_pdb(pdb.online_base, pdb.path);

        for (const auto& name : settings.names)
            resolve_symbol(repo, name);
    } catch (const std::exception& e) {
        dump_error(e);
        return 1;
    }
    return 0;
}