aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/addr2name.cpp
blob: b2f0a51e1cbf4f1b9f3000ee035c2abf5402dbf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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_descr.hpp"

#include "pdb/all.hpp"

#include <boost/program_options.hpp>

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

namespace
{
    class Addr2Name : public SettingsParser
    {
    public:
        explicit Addr2Name(const std::string& argv0)
            : SettingsParser{argv0, build_options(), build_args()}
        { }

        bool exit_with_usage() const { return help_flag; }

        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;

    private:
        Options build_options()
        {
            namespace program_options = boost::program_options;
            Options descr{"options"};
            descr.add_options()
                ("help,h",
                    program_options::bool_switch(&help_flag),
                    "show this message and exit")
                ("pdb",
                    program_options::value<std::vector<PDB>>(&pdbs)
                        ->value_name("ADDR,PATH"),
                    "load a PDB file")
                ("address",
                    program_options::value<std::vector<pdb::Address>>(&addresses)
                        ->value_name("ADDR"),
                    "add an address to resolve")
                ("lines,l",
                    program_options::bool_switch(&lines),
                    "try to resolve source files & line numbers");
            return descr;
        }

        static Arguments build_args()
        {
            Arguments descr;
            descr.add("address", -1);
            return descr;
        }

        bool help_flag = 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();
    }

    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();
    }

    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
    {
        const Addr2Name settings{argv[0]};

        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& address : settings.addresses)
            resolve_symbol(repo, address, settings.lines);
    }
    catch (const std::exception& e)
    {
        dump_error(e);
        return 1;
    }
    return 0;
}