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
|
// 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 "pdb/all.hpp"
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
namespace pdb
{
namespace
{
template <typename Value>
const Module& guess_module(
const std::map<Address, Value>& modules,
Address pivot)
{
if (modules.empty())
throw std::range_error{"there're no modules to choose from"};
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{"couldn't choose a module"};
--it;
return it->second;
}
return it->second;
}
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();
}
}
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 = file::query_id(path);
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& raw)
{
callback(symbol_from_buffer(module, raw));
});
}
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));
}
Symbol Repo::symbol_from_buffer(const SymbolInfo& raw) const
{
const auto offline_base = raw.get_offline_base();
const auto it = offline_bases.find(offline_base);
if (it == offline_bases.cend())
throw std::runtime_error{"symbol's module hasn't been loaded (shouldn't happen)"};
return symbol_from_buffer(it->second, raw);
}
Symbol Repo::symbol_from_buffer(const Module& module, const SymbolInfo& raw)
{
return {module.translate_offline_address(raw.get_offline_address()), raw};
}
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);
}
}
|