| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#include "dwarf.h" |
|
2
|
|
|
|
|
|
|
#include "dl.h" |
|
3
|
|
|
|
|
|
|
#include // PATH_MAX |
|
4
|
|
|
|
|
|
|
#include |
|
5
|
|
|
|
|
|
|
#include |
|
6
|
|
|
|
|
|
|
#include |
|
7
|
|
|
|
|
|
|
#include // for debug |
|
8
|
|
|
|
|
|
|
#include |
|
9
|
|
|
|
|
|
|
#include |
|
10
|
|
|
|
|
|
|
#include |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
13
|
|
|
|
|
|
|
#include |
|
14
|
|
|
|
|
|
|
#define PANDA_PATH_MAX MAX_PATH |
|
15
|
|
|
|
|
|
|
#else |
|
16
|
|
|
|
|
|
|
#define PANDA_PATH_MAX PATH_MAX |
|
17
|
|
|
|
|
|
|
#endif |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
static const constexpr uint32_t MASK_AO = 1 << 0; |
|
20
|
|
|
|
|
|
|
static const constexpr uint32_t MASK_SPEC = 1 << 1; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
namespace panda { namespace backtrace { |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
static BacktraceBackendSP dl_produce(const Backtrace& raw_traces); |
|
25
|
|
|
|
|
|
|
static BacktraceProducer dl_producer(dl_produce); |
|
26
|
|
|
|
|
|
|
|
|
27
|
17
|
|
|
|
|
|
static DwarfInfoMap load_dwarf_info(const SharedObjectMap& so_map) { |
|
28
|
17
|
|
|
|
|
|
DwarfInfoMap result; |
|
29
|
544
|
100
|
|
|
|
|
for (auto& so: so_map) { |
|
30
|
1054
|
50
|
|
|
|
|
auto info = std::make_unique(so); |
|
31
|
1054
|
50
|
|
|
|
|
string real_path(PANDA_PATH_MAX); |
|
32
|
|
|
|
|
|
|
Dwarf_Error error; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
FILE* file; |
|
35
|
527
|
50
|
|
|
|
|
file = fopen(so.name.c_str(), "rb"); |
|
|
|
50
|
|
|
|
|
|
|
36
|
2108
|
50
|
|
|
|
|
DwarfInfo::file_guard_t file_guard (file, [](auto* f){ if (f) {fclose(f); }}); |
|
|
|
50
|
|
|
|
|
|
|
37
|
527
|
50
|
|
|
|
|
int fd = file ? fileno(file) : 0; |
|
38
|
527
|
50
|
|
|
|
|
if (fd > 0) { |
|
39
|
527
|
50
|
|
|
|
|
auto err = dwarf_init_b(fd, DW_DLC_READ, DW_GROUPNUMBER_ANY, nullptr, &info->err_arg, &info->debug, &error); |
|
40
|
|
|
|
|
|
|
//std::cout << "loading '" << so.name << "', code = " << err << "\n"; |
|
41
|
527
|
50
|
|
|
|
|
if (err == DW_DLV_OK) { |
|
42
|
527
|
|
|
|
|
|
if (info->load(std::move(file_guard))) { |
|
43
|
|
|
|
|
|
|
//std::cout << "dwarf info initialized for " << so.name << "\n"; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
} else if (err == DW_DLV_ERROR) { |
|
46
|
|
|
|
|
|
|
//std::cout << "error initializing on " << so.name << " :: " << dwarf_errmsg(error) << "\n"; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
// use DwarfInfoMap independently whether the real file was loaded. So, if file is not found |
|
49
|
|
|
|
|
|
|
// we still can produce stack frame with address/offset and .so name |
|
50
|
|
|
|
|
|
|
} |
|
51
|
527
|
50
|
|
|
|
|
result.emplace(so.name, std::move(info)); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
17
|
|
|
|
|
|
return result; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
68
|
50
|
|
|
|
|
struct DwarfBackend: BacktraceBackend { |
|
57
|
|
|
|
|
|
|
const Backtrace& raw_traces; |
|
58
|
|
|
|
|
|
|
DwarfInfoMap info_map; |
|
59
|
|
|
|
|
|
|
|
|
60
|
34
|
|
|
|
|
|
DwarfBackend(const Backtrace& raw_traces_) noexcept: raw_traces{raw_traces_} { |
|
61
|
34
|
|
|
|
|
|
SharedObjectMap so_map; |
|
62
|
17
|
|
|
|
|
|
gather_info(so_map); |
|
63
|
17
|
|
|
|
|
|
info_map = load_dwarf_info(so_map); |
|
64
|
17
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
267
|
|
|
|
|
|
bool produce_frame(StackFrames& frames, size_t i) override { |
|
67
|
267
|
|
|
|
|
|
auto frame_ptr = raw_traces.buffer.at(i); |
|
68
|
267
|
|
|
|
|
|
auto ip_addr = reinterpret_cast(frame_ptr); |
|
69
|
3436
|
50
|
|
|
|
|
for(auto& it: info_map) { |
|
70
|
|
|
|
|
|
|
//std::cout << "resolving " << it.first << "\n"; |
|
71
|
3436
|
100
|
|
|
|
|
if (it.second->resolve(ip_addr, frames)) return true; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
0
|
|
|
|
|
|
return false; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
}; |
|
77
|
|
|
|
|
|
|
|
|
78
|
17
|
|
|
|
|
|
BacktraceBackendSP dl_produce(const Backtrace& raw_traces) { |
|
79
|
17
|
|
|
|
|
|
return new DwarfBackend(raw_traces); |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
6
|
|
|
|
|
|
void install_backtracer() { |
|
83
|
6
|
|
|
|
|
|
panda::Backtrace::install_producer(dl_producer); |
|
84
|
6
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
|
87
|
1054
|
|
|
|
|
|
DwarfInfo::~DwarfInfo() { |
|
88
|
527
|
50
|
|
|
|
|
if (debug) { |
|
89
|
527
|
|
|
|
|
|
CUs.clear(); // DIEs must be released before debug |
|
90
|
|
|
|
|
|
|
Dwarf_Error error; |
|
91
|
527
|
|
|
|
|
|
auto res = dwarf_finish(debug, &error); |
|
92
|
527
|
50
|
|
|
|
|
if (res != DW_DLV_OK) { |
|
93
|
527
|
|
|
|
|
|
fprintf(stderr, "dwarf_finish: %s\n", dwarf_errmsg(error)); |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
} |
|
96
|
527
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
527
|
|
|
|
|
|
bool DwarfInfo::load(file_guard_t&& guard_) noexcept { |
|
99
|
527
|
|
|
|
|
|
guard = std::move(guard_); |
|
100
|
|
|
|
|
|
|
Dwarf_Error error; |
|
101
|
527
|
|
|
|
|
|
for(int cu_number = 0;;++cu_number) { |
|
102
|
527
|
|
|
|
|
|
auto cu = dwarf::CUSP(new dwarf::CU(debug, cu_number)); |
|
103
|
|
|
|
|
|
|
|
|
104
|
527
|
|
|
|
|
|
auto res = dwarf_next_cu_header_d(debug, true, &cu->header_length, |
|
105
|
527
|
|
|
|
|
|
&cu->version_stamp, &cu->abbrev_offset, |
|
106
|
527
|
|
|
|
|
|
&cu->address_size, &cu->offset_size, |
|
107
|
527
|
|
|
|
|
|
&cu->extension_size, &cu->signature, |
|
108
|
527
|
|
|
|
|
|
&cu->typeoffset, nullptr, |
|
109
|
1054
|
|
|
|
|
|
&cu->header_type,&error); |
|
110
|
527
|
50
|
|
|
|
|
if (res != DW_DLV_OK) { break; } |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
/* The CU will have a single sibling, a cu_die. */ |
|
113
|
0
|
|
|
|
|
|
Dwarf_Die cu_die = nullptr; |
|
114
|
0
|
|
|
|
|
|
res = dwarf_siblingof_b(debug, nullptr, true, &cu_die, &error); |
|
115
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { break; } |
|
116
|
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
cu->cu_die = dwarf::DieSP(new dwarf::DieRC(cu_die, debug, nullptr)); |
|
118
|
527
|
50
|
|
|
|
|
CUs.emplace_back(std::move(cu)); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
527
|
|
|
|
|
|
return !CUs.empty(); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
|
124
|
3436
|
|
|
|
|
|
bool DwarfInfo::resolve(std::uint64_t ip, StackFrames &frames) noexcept { |
|
125
|
3436
|
100
|
|
|
|
|
if (ip < so_info.begin || ip >= so_info.end) { return false; } |
|
|
|
100
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
|
127
|
267
|
|
|
|
|
|
auto offset = so_info.get_offset(ip); |
|
128
|
|
|
|
|
|
|
/// std::cout << "resolving " << std::hex << ip << "/" << offset << " from " << so_info.name << ", CUs: " << CUs.size() << "\n"; |
|
129
|
|
|
|
|
|
|
|
|
130
|
267
|
50
|
|
|
|
|
for(auto it = CUs.begin(); it != CUs.end(); ++it){ |
|
131
|
|
|
|
|
|
|
//if (r.is_complete()) std::cout << "hit\n"; |
|
132
|
0
|
|
|
|
|
|
auto& cu = *it; |
|
133
|
0
|
|
|
|
|
|
auto r = cu->resolve(offset); |
|
134
|
0
|
0
|
|
|
|
|
if (r.is_complete()) { return r.get_frames(ip, so_info, frames); } |
|
|
|
0
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
// just fall-back to .so, address & offset |
|
138
|
3703
|
|
|
|
|
|
auto frame = StackframeSP(new Stackframe()); |
|
139
|
267
|
|
|
|
|
|
frame->library = so_info.name; |
|
140
|
267
|
|
|
|
|
|
frame->address = ip; |
|
141
|
267
|
|
|
|
|
|
frame->offset = offset; |
|
142
|
267
|
|
|
|
|
|
frames.emplace_back(std::move(frame)); |
|
143
|
267
|
|
|
|
|
|
return true; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
namespace dwarf { |
|
147
|
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
LookupResult::LookupResult(LookupResult&& other) { |
|
149
|
0
|
|
|
|
|
|
cu = std::move(other.cu); |
|
150
|
0
|
|
|
|
|
|
root = std::move(other.root); |
|
151
|
0
|
|
|
|
|
|
subprogram = std::move(other.subprogram); |
|
152
|
0
|
|
|
|
|
|
offset = std::move(other.offset); |
|
153
|
0
|
|
|
|
|
|
} |
|
154
|
0
|
0
|
|
|
|
|
bool LookupResult::is_complete() noexcept { return cu && subprogram; } |
|
|
|
0
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
bool LookupResult::get_frames(std::uint64_t ip, const SharedObjectInfo& so, StackFrames &frames) noexcept { |
|
157
|
|
|
|
|
|
|
//if (!is_complete()) { return frame; } |
|
158
|
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
auto push_frame = [&](const auto& details) { |
|
160
|
0
|
0
|
|
|
|
|
auto frame = StackframeSP(new Stackframe()); |
|
|
|
0
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
frame->address = ip; |
|
162
|
0
|
|
|
|
|
|
frame->offset = offset; |
|
163
|
0
|
0
|
|
|
|
|
frame->library = so.name; |
|
164
|
|
|
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
|
if (details.name) frame->name = details.name; |
|
|
|
0
|
|
|
|
|
|
|
166
|
0
|
0
|
|
|
|
|
if (details.line_no) frame->line_no = details.line_no; |
|
167
|
0
|
0
|
|
|
|
|
if (details.source) frame->file = details.source; |
|
|
|
0
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
|
169
|
0
|
0
|
|
|
|
|
frames.emplace_back(std::move(frame)); |
|
170
|
0
|
|
|
|
|
|
}; |
|
171
|
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
if (subprogram) { |
|
173
|
0
|
|
|
|
|
|
auto location = subprogram->refine_location(offset); |
|
174
|
0
|
|
|
|
|
|
auto details = location->refine_fn(*this); |
|
175
|
0
|
|
|
|
|
|
push_frame(details); |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
// printf("refined: %s at %lu, offset: %lu(%lx)\n", details.name.c_str(), details.line_no, offset, offset); |
|
178
|
0
|
0
|
|
|
|
|
while(!location->context.empty()) { |
|
179
|
0
|
|
|
|
|
|
auto outer = location->context.back(); |
|
180
|
0
|
|
|
|
|
|
location->context.pop_back(); |
|
181
|
0
|
|
|
|
|
|
auto details = outer->refine_fn(*this); |
|
182
|
0
|
|
|
|
|
|
push_frame(details); |
|
183
|
|
|
|
|
|
|
//printf("refined (outer): %s at %u\n", details.name.c_str(), details.line_no); |
|
184
|
|
|
|
|
|
|
} |
|
185
|
|
|
|
|
|
|
} else { |
|
186
|
0
|
|
|
|
|
|
push_frame(FunctionDetails()); |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
///std::cout << frame->name << " at " << frame->file << ":" << frame->line_no << ", o:" << frame->offset << "\n"; |
|
190
|
0
|
|
|
|
|
|
return true; |
|
191
|
|
|
|
|
|
|
} |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
DieRC::DieRC(Dwarf_Die die_, Dwarf_Debug debug_, DieSP parent_): die{die_}, debug{debug_}, parent{parent_} {} |
|
195
|
0
|
|
|
|
|
|
DieRC::~DieRC() { |
|
196
|
0
|
|
|
|
|
|
dwarf_dealloc(debug, die,DW_DLA_DIE); |
|
197
|
0
|
0
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
DieSP DieRC::resolve_ref(DieSP source, Dwarf_Half attr) noexcept { |
|
200
|
0
|
|
|
|
|
|
Dwarf_Die r = nullptr; |
|
201
|
|
|
|
|
|
|
Dwarf_Attribute attr_val; |
|
202
|
|
|
|
|
|
|
Dwarf_Error error; |
|
203
|
0
|
|
|
|
|
|
auto res = dwarf_attr(source->die, attr, &attr_val, &error); |
|
204
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
205
|
0
|
|
|
|
|
|
Dwarf_Off attr_offset = 0; |
|
206
|
0
|
|
|
|
|
|
res = dwarf_global_formref(attr_val,&attr_offset,&error); |
|
207
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
208
|
0
|
|
|
|
|
|
res = dwarf_offdie_b(debug, attr_offset, true, &r, &error); |
|
209
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { return new DieRC(r, source->debug, source); } |
|
210
|
|
|
|
|
|
|
} |
|
211
|
|
|
|
|
|
|
} |
|
212
|
0
|
|
|
|
|
|
return nullptr; |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
DieSP DieRC::discover(DieSP target) noexcept { |
|
216
|
0
|
|
|
|
|
|
auto p = parent; |
|
217
|
0
|
0
|
|
|
|
|
while (p->parent) { p = p->parent; } |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
/* no need to scan CU-siblings */ |
|
220
|
0
|
|
|
|
|
|
Dwarf_Die child_die = nullptr; |
|
221
|
|
|
|
|
|
|
Dwarf_Error error; |
|
222
|
0
|
|
|
|
|
|
auto res = dwarf_child(p->die, &child_die, &error); |
|
223
|
|
|
|
|
|
|
|
|
224
|
0
|
0
|
|
|
|
|
if(res == DW_DLV_OK) { |
|
225
|
0
|
|
|
|
|
|
DieSP child = new DieRC(child_die, debug, p); |
|
226
|
|
|
|
|
|
|
Dwarf_Off off; |
|
227
|
0
|
|
|
|
|
|
res = dwarf_dieoffset(target->die, &off, &error); |
|
228
|
0
|
0
|
|
|
|
|
assert(res == DW_DLV_OK); |
|
229
|
0
|
|
|
|
|
|
return discover(off, child); |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
|
std::abort(); |
|
233
|
|
|
|
|
|
|
} |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
using Queue = std::queue; |
|
236
|
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
void add_sibling_or_child(DieSP& node, Queue& queue) { |
|
238
|
|
|
|
|
|
|
Dwarf_Error error; |
|
239
|
|
|
|
|
|
|
Dwarf_Die child_die; |
|
240
|
|
|
|
|
|
|
|
|
241
|
0
|
0
|
|
|
|
|
int res = dwarf_siblingof_b(node->debug, node->die, true, &child_die, &error); |
|
242
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
243
|
0
|
0
|
|
|
|
|
DieSP child = new DieRC(child_die, node->debug, node->parent); |
|
|
|
0
|
|
|
|
|
|
|
244
|
0
|
0
|
|
|
|
|
queue.push(child); |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
|
|
247
|
0
|
0
|
|
|
|
|
res = dwarf_child(node->die, &child_die, &error); |
|
248
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
249
|
0
|
0
|
|
|
|
|
DieSP child = new DieRC(child_die, node->debug, node); |
|
|
|
0
|
|
|
|
|
|
|
250
|
0
|
0
|
|
|
|
|
queue.push(child); |
|
251
|
|
|
|
|
|
|
} |
|
252
|
0
|
|
|
|
|
|
} |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
template |
|
255
|
0
|
|
|
|
|
|
DieSP iterate(DieSP node, CheckFN&& fn) noexcept { |
|
256
|
|
|
|
|
|
|
Dwarf_Error error; |
|
257
|
|
|
|
|
|
|
Dwarf_Die child_die; |
|
258
|
|
|
|
|
|
|
int res; |
|
259
|
|
|
|
|
|
|
|
|
260
|
0
|
|
|
|
|
|
auto create_child = [&](DieSP& parent) -> DieSP { |
|
261
|
0
|
0
|
|
|
|
|
return new DieRC(child_die, node->debug, parent); |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
}; |
|
263
|
|
|
|
|
|
|
|
|
264
|
0
|
0
|
|
|
|
|
while (node) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
265
|
0
|
|
|
|
|
|
bool try_next = true; |
|
266
|
0
|
|
|
|
|
|
auto prev_node = node; |
|
267
|
0
|
0
|
|
|
|
|
while (try_next) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
268
|
0
|
|
|
|
|
|
Scan scan = fn(node); |
|
269
|
0
|
|
|
|
|
|
switch (scan) { |
|
270
|
0
|
|
|
|
|
|
case Scan::found: return node; |
|
271
|
0
|
|
|
|
|
|
case Scan::dead_end: try_next = false; node = prev_node; break; |
|
272
|
|
|
|
|
|
|
case Scan::not_found: { |
|
273
|
0
|
|
|
|
|
|
int res = dwarf_siblingof_b(node->debug, node->die, true, &child_die, &error); |
|
274
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { try_next = false; break; } |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
275
|
0
|
|
|
|
|
|
prev_node = node; |
|
276
|
0
|
|
|
|
|
|
node = create_child(node->parent); |
|
277
|
|
|
|
|
|
|
} |
|
278
|
|
|
|
|
|
|
} |
|
279
|
|
|
|
|
|
|
} |
|
280
|
|
|
|
|
|
|
|
|
281
|
0
|
|
|
|
|
|
res = dwarf_child(node->die, &child_die, &error); |
|
282
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { node = DieSP(); break; } |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
283
|
0
|
|
|
|
|
|
node = create_child(node); |
|
284
|
|
|
|
|
|
|
} |
|
285
|
|
|
|
|
|
|
/* dead end */ |
|
286
|
0
|
|
|
|
|
|
return node; |
|
287
|
|
|
|
|
|
|
} |
|
288
|
|
|
|
|
|
|
|
|
289
|
0
|
|
|
|
|
|
DieSP DieRC::discover(Dwarf_Off target_offset, DieSP node) noexcept { |
|
290
|
0
|
|
|
|
|
|
auto check = [&](DieSP& node) -> Scan { |
|
291
|
|
|
|
|
|
|
Dwarf_Error error; |
|
292
|
|
|
|
|
|
|
Dwarf_Off off; |
|
293
|
|
|
|
|
|
|
int res; |
|
294
|
0
|
0
|
|
|
|
|
res = dwarf_dieoffset(node->die, &off, &error); |
|
295
|
0
|
0
|
|
|
|
|
assert(res == DW_DLV_OK); |
|
296
|
|
|
|
|
|
|
|
|
297
|
0
|
0
|
|
|
|
|
if (off == target_offset) { return Scan::found; } |
|
298
|
0
|
0
|
|
|
|
|
else if (off > target_offset) { return Scan::dead_end; } |
|
299
|
0
|
|
|
|
|
|
else { return Scan::not_found; } |
|
300
|
0
|
|
|
|
|
|
}; |
|
301
|
|
|
|
|
|
|
|
|
302
|
0
|
|
|
|
|
|
return iterate(node, check); |
|
303
|
|
|
|
|
|
|
} |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
|
|
306
|
0
|
|
|
|
|
|
void DieRC::refine_fn_name(DieSP it, FunctionDetails& details) noexcept { |
|
307
|
0
|
0
|
|
|
|
|
if (!details.name) { |
|
308
|
|
|
|
|
|
|
Dwarf_Error error; |
|
309
|
|
|
|
|
|
|
Dwarf_Attribute attr_name; |
|
310
|
0
|
|
|
|
|
|
auto res = dwarf_attr(it->die, DW_AT_name, &attr_name, &error); |
|
311
|
|
|
|
|
|
|
|
|
312
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
313
|
0
|
0
|
|
|
|
|
iptr node = (it->die == die) ? iptr(this) : discover(it); |
|
|
|
0
|
|
|
|
|
|
|
314
|
0
|
|
|
|
|
|
auto fqn = node->gather_fqn(); |
|
315
|
0
|
|
|
|
|
|
details.name = fqn.full_name; |
|
316
|
0
|
|
|
|
|
|
details.name_die = fqn.source_die; |
|
317
|
0
|
|
|
|
|
|
return; |
|
318
|
|
|
|
|
|
|
} |
|
319
|
|
|
|
|
|
|
|
|
320
|
0
|
0
|
|
|
|
|
if (!(details.mask & MASK_SPEC)) { |
|
321
|
0
|
|
|
|
|
|
auto die_spec = resolve_ref(it, DW_AT_specification); |
|
322
|
0
|
0
|
|
|
|
|
if (die_spec) { |
|
323
|
0
|
|
|
|
|
|
details.mask = details.mask | MASK_AO; |
|
324
|
0
|
0
|
|
|
|
|
return refine_fn_spec(die_spec, details); |
|
325
|
|
|
|
|
|
|
} |
|
326
|
|
|
|
|
|
|
} |
|
327
|
|
|
|
|
|
|
|
|
328
|
0
|
0
|
|
|
|
|
if (!(details.mask & MASK_AO)) { |
|
329
|
0
|
|
|
|
|
|
auto die_ao = resolve_ref(it, DW_AT_abstract_origin); |
|
330
|
0
|
0
|
|
|
|
|
if (die_ao) { |
|
331
|
0
|
|
|
|
|
|
details.mask = details.mask | MASK_AO; |
|
332
|
0
|
|
|
|
|
|
refine_fn_name(die_ao, details); |
|
333
|
|
|
|
|
|
|
} |
|
334
|
|
|
|
|
|
|
} |
|
335
|
|
|
|
|
|
|
} |
|
336
|
|
|
|
|
|
|
} |
|
337
|
|
|
|
|
|
|
|
|
338
|
0
|
|
|
|
|
|
DieRC::FQN DieRC::gather_fqn() noexcept { |
|
339
|
|
|
|
|
|
|
Dwarf_Error error; |
|
340
|
0
|
|
|
|
|
|
DieSP source_die; |
|
341
|
|
|
|
|
|
|
|
|
342
|
0
|
|
|
|
|
|
auto try_record_source = [&](DieSP it) mutable { |
|
343
|
|
|
|
|
|
|
Dwarf_Bool has_source; |
|
344
|
0
|
0
|
|
|
|
|
if (!source_die) { |
|
345
|
0
|
0
|
|
|
|
|
int res = dwarf_hasattr(it->die, DW_AT_decl_file, &has_source, &error); |
|
346
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK && has_source) { |
|
|
|
0
|
|
|
|
|
|
|
347
|
0
|
0
|
|
|
|
|
source_die = it; |
|
348
|
|
|
|
|
|
|
} |
|
349
|
|
|
|
|
|
|
} |
|
350
|
0
|
|
|
|
|
|
}; |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
|
|
353
|
0
|
|
|
|
|
|
char* name = nullptr; |
|
354
|
0
|
|
|
|
|
|
auto res = dwarf_diename(die, &name, &error); |
|
355
|
0
|
0
|
|
|
|
|
assert(res == DW_DLV_OK); |
|
356
|
0
|
|
|
|
|
|
try_record_source(DieSP(this)); |
|
357
|
|
|
|
|
|
|
|
|
358
|
0
|
|
|
|
|
|
string r(name); |
|
359
|
|
|
|
|
|
|
|
|
360
|
0
|
|
|
|
|
|
auto p = parent; |
|
361
|
0
|
0
|
|
|
|
|
while(p) { |
|
362
|
0
|
|
|
|
|
|
Dwarf_Half tag = 0; |
|
363
|
0
|
|
|
|
|
|
res = dwarf_tag(p->die, &tag, &error); |
|
364
|
0
|
0
|
|
|
|
|
assert(res == DW_DLV_OK); |
|
365
|
0
|
0
|
|
|
|
|
if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type || tag == DW_TAG_namespace) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
Dwarf_Attribute attr_name; |
|
367
|
0
|
|
|
|
|
|
res = dwarf_attr(p->die, DW_AT_name, &attr_name, &error); |
|
368
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
369
|
|
|
|
|
|
|
char* prefix; |
|
370
|
0
|
|
|
|
|
|
dwarf_formstring(attr_name, &prefix, &error); |
|
371
|
0
|
0
|
|
|
|
|
assert(res == DW_DLV_OK); |
|
372
|
0
|
|
|
|
|
|
r = string(prefix) + "::" + r; |
|
373
|
0
|
|
|
|
|
|
try_record_source(p); |
|
374
|
|
|
|
|
|
|
} |
|
375
|
|
|
|
|
|
|
} |
|
376
|
0
|
|
|
|
|
|
p = p->parent; |
|
377
|
|
|
|
|
|
|
} |
|
378
|
|
|
|
|
|
|
|
|
379
|
0
|
|
|
|
|
|
return FQN{r, source_die}; |
|
380
|
|
|
|
|
|
|
} |
|
381
|
|
|
|
|
|
|
|
|
382
|
0
|
|
|
|
|
|
void DieRC::refine_fn_line(DieSP it, std::uint64_t offset, FunctionDetails& details) noexcept { |
|
383
|
|
|
|
|
|
|
/* currently it detects lines only in the current CU (compilation unit) */ |
|
384
|
|
|
|
|
|
|
using LineContextHolder = std::unique_ptr>; |
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
Dwarf_Error error; |
|
387
|
|
|
|
|
|
|
char* cu_name_raw; |
|
388
|
0
|
|
|
|
|
|
auto res = dwarf_die_text(it->die, DW_AT_name, &cu_name_raw, &error); |
|
389
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
390
|
0
|
|
|
|
|
|
string cu_name(cu_name_raw); |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
Dwarf_Unsigned line_version; |
|
393
|
|
|
|
|
|
|
Dwarf_Small table_type; |
|
394
|
|
|
|
|
|
|
Dwarf_Line_Context line_context; |
|
395
|
0
|
|
|
|
|
|
res = dwarf_srclines_b(it->die, &line_version, &table_type,&line_context,&error); |
|
396
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
397
|
0
|
0
|
|
|
|
|
LineContextHolder line_context_guard(&line_context, [](auto it){ dwarf_srclines_dealloc_b(*it); }); |
|
398
|
|
|
|
|
|
|
|
|
399
|
0
|
|
|
|
|
|
Dwarf_Signed base_index, end_index, cu_index = -1; |
|
400
|
|
|
|
|
|
|
Dwarf_Signed file_count; |
|
401
|
0
|
|
|
|
|
|
res = dwarf_srclines_files_indexes(line_context, &base_index,&file_count,&end_index, &error); |
|
402
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
403
|
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
//std::cout << "looking indices for " << cu_name << ", b = " << base_index << ", e = " << end_index << "\n"; |
|
405
|
|
|
|
|
|
|
|
|
406
|
0
|
0
|
|
|
|
|
for (Dwarf_Signed i = base_index; i < end_index; ++i) { |
|
407
|
|
|
|
|
|
|
Dwarf_Unsigned modtime; |
|
408
|
|
|
|
|
|
|
Dwarf_Unsigned flength; |
|
409
|
|
|
|
|
|
|
Dwarf_Unsigned dirindex; |
|
410
|
|
|
|
|
|
|
const char *source_name; |
|
411
|
|
|
|
|
|
|
|
|
412
|
0
|
|
|
|
|
|
res = dwarf_srclines_files_data(line_context, i, &source_name ,&dirindex, &modtime, &flength, &error); |
|
413
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
414
|
0
|
0
|
|
|
|
|
if (cu_name.find(source_name) != string::npos) { |
|
415
|
0
|
0
|
|
|
|
|
if (dirindex) { |
|
416
|
|
|
|
|
|
|
const char* dir_name; |
|
417
|
0
|
|
|
|
|
|
res = dwarf_srclines_include_dir_data(line_context, static_cast(dirindex), &dir_name, &error); |
|
418
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
419
|
|
|
|
|
|
|
|
|
420
|
0
|
0
|
|
|
|
|
if (cu_name.find(dir_name) != string::npos) { |
|
421
|
0
|
|
|
|
|
|
cu_index = i; |
|
422
|
0
|
|
|
|
|
|
break; |
|
423
|
|
|
|
|
|
|
} |
|
424
|
|
|
|
|
|
|
} else { |
|
425
|
|
|
|
|
|
|
/* no directory / current directory */ |
|
426
|
0
|
|
|
|
|
|
cu_index = i; |
|
427
|
0
|
|
|
|
|
|
break; |
|
428
|
|
|
|
|
|
|
} |
|
429
|
|
|
|
|
|
|
} |
|
430
|
|
|
|
|
|
|
} |
|
431
|
0
|
0
|
|
|
|
|
if (cu_index == -1) { return; } |
|
432
|
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
Dwarf_Line *linebuf; |
|
434
|
|
|
|
|
|
|
Dwarf_Signed linecount; |
|
435
|
0
|
|
|
|
|
|
res = dwarf_srclines_from_linecontext(line_context, &linebuf, &linecount, &error); |
|
436
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
437
|
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
|
|
439
|
0
|
|
|
|
|
|
bool found = false; |
|
440
|
0
|
|
|
|
|
|
Dwarf_Unsigned prev_lineno = 0; |
|
441
|
0
|
0
|
|
|
|
|
for(Dwarf_Signed i = 0; i < linecount; ++i) { |
|
442
|
0
|
|
|
|
|
|
Dwarf_Unsigned lineno = 0; |
|
443
|
0
|
|
|
|
|
|
Dwarf_Unsigned file_index = 0; |
|
444
|
0
|
|
|
|
|
|
Dwarf_Addr lineaddr = 0; |
|
445
|
|
|
|
|
|
|
|
|
446
|
0
|
|
|
|
|
|
res = dwarf_lineno(linebuf[i], &lineno, &error); |
|
447
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
448
|
0
|
|
|
|
|
|
res = dwarf_lineaddr(linebuf[i], &lineaddr, &error); |
|
449
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
450
|
0
|
|
|
|
|
|
res = dwarf_line_srcfileno(linebuf[i],&file_index, &error); |
|
451
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return; } |
|
452
|
0
|
0
|
|
|
|
|
if (file_index != static_cast(cu_index)) { continue; } |
|
453
|
|
|
|
|
|
|
|
|
454
|
0
|
0
|
|
|
|
|
if (lineaddr >= offset) { found = true; break; } |
|
455
|
0
|
|
|
|
|
|
else { prev_lineno = lineno; } |
|
456
|
|
|
|
|
|
|
} |
|
457
|
|
|
|
|
|
|
|
|
458
|
0
|
0
|
|
|
|
|
if (found) { |
|
459
|
0
|
0
|
|
|
|
|
details.line_no = prev_lineno; |
|
460
|
|
|
|
|
|
|
} |
|
461
|
|
|
|
|
|
|
//std::cout << "refine_fn_line " << found << " :: " << lr.offset << " :: " << std::dec << prev_lineno << "\n"; |
|
462
|
|
|
|
|
|
|
} |
|
463
|
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
|
|
465
|
0
|
|
|
|
|
|
void DieRC::refine_fn_line_fallback(DieSP it, FunctionDetails& details) noexcept { |
|
466
|
0
|
0
|
|
|
|
|
if (!details.line_no) { |
|
467
|
|
|
|
|
|
|
Dwarf_Error error; |
|
468
|
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
Dwarf_Attribute attr_line; |
|
470
|
0
|
|
|
|
|
|
auto res = dwarf_attr(it->die, DW_AT_decl_line, &attr_line, &error); |
|
471
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
472
|
|
|
|
|
|
|
Dwarf_Unsigned line; |
|
473
|
0
|
|
|
|
|
|
res = dwarf_formudata(attr_line, &line, &error); |
|
474
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { details.line_no = line + 1; } |
|
475
|
|
|
|
|
|
|
} |
|
476
|
|
|
|
|
|
|
} |
|
477
|
0
|
|
|
|
|
|
} |
|
478
|
|
|
|
|
|
|
|
|
479
|
0
|
|
|
|
|
|
FunctionDetails DieRC::refine_fn(LookupResult& lr) noexcept { |
|
480
|
0
|
|
|
|
|
|
FunctionDetails r; |
|
481
|
|
|
|
|
|
|
|
|
482
|
0
|
|
|
|
|
|
refine_fn_name(DieSP(this), r); |
|
483
|
0
|
|
|
|
|
|
refine_fn_line(lr.cu, lr.offset, r); |
|
484
|
0
|
0
|
|
|
|
|
if (!r.line_no && r.name_die) refine_fn_line_fallback(r.name_die, r); |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
485
|
|
|
|
|
|
|
|
|
486
|
0
|
0
|
|
|
|
|
if (r.name_die) refine_fn_source(r.name_die, r, *lr.root); |
|
487
|
|
|
|
|
|
|
//printf("n = %s\n", r.name ? r.name.c_str() : "n/a"); |
|
488
|
|
|
|
|
|
|
|
|
489
|
0
|
|
|
|
|
|
return r; |
|
490
|
|
|
|
|
|
|
} |
|
491
|
|
|
|
|
|
|
|
|
492
|
0
|
|
|
|
|
|
void DieRC::refine_fn_source(DieSP it, FunctionDetails& details, CU& cu) noexcept { |
|
493
|
0
|
0
|
|
|
|
|
if (!details.source) { |
|
494
|
|
|
|
|
|
|
Dwarf_Error error; |
|
495
|
|
|
|
|
|
|
Dwarf_Attribute attr_file; |
|
496
|
0
|
|
|
|
|
|
auto res = dwarf_attr(it->die, DW_AT_decl_file, &attr_file, &error); |
|
497
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
498
|
|
|
|
|
|
|
Dwarf_Unsigned file_index; |
|
499
|
0
|
|
|
|
|
|
res = dwarf_formudata(attr_file, &file_index, &error); |
|
500
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK && file_index) { |
|
|
|
0
|
|
|
|
|
|
|
501
|
0
|
|
|
|
|
|
details.source = cu.get_source(file_index); |
|
502
|
|
|
|
|
|
|
} |
|
503
|
|
|
|
|
|
|
} |
|
504
|
|
|
|
|
|
|
} |
|
505
|
0
|
|
|
|
|
|
} |
|
506
|
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
|
|
508
|
0
|
|
|
|
|
|
void DieRC::refine_fn_ao(DieSP abstract_origin, FunctionDetails& details) noexcept { |
|
509
|
0
|
|
|
|
|
|
refine_fn_name(abstract_origin, details); |
|
510
|
0
|
0
|
|
|
|
|
if (!details.name && !(details.mask & MASK_SPEC)) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
511
|
0
|
|
|
|
|
|
auto die_spec = resolve_ref(abstract_origin, DW_AT_specification); |
|
512
|
0
|
0
|
|
|
|
|
if (die_spec) { |
|
513
|
0
|
|
|
|
|
|
details.mask = details.mask | MASK_SPEC; |
|
514
|
0
|
|
|
|
|
|
refine_fn_spec(die_spec, details); |
|
515
|
|
|
|
|
|
|
} |
|
516
|
|
|
|
|
|
|
} |
|
517
|
0
|
|
|
|
|
|
} |
|
518
|
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
|
|
520
|
0
|
|
|
|
|
|
void DieRC::refine_fn_spec(DieSP specification, FunctionDetails& details) noexcept { |
|
521
|
0
|
|
|
|
|
|
refine_fn_name(specification, details); |
|
522
|
0
|
|
|
|
|
|
refine_fn_line_fallback(specification, details); |
|
523
|
0
|
|
|
|
|
|
} |
|
524
|
|
|
|
|
|
|
|
|
525
|
0
|
|
|
|
|
|
DieSP DieRC::refine_location(uint64_t offset) noexcept { |
|
526
|
0
|
0
|
|
|
|
|
DieCollection context{{DieSP(this)}}; |
|
527
|
0
|
|
|
|
|
|
DieSP root = context.front(); |
|
528
|
|
|
|
|
|
|
|
|
529
|
0
|
|
|
|
|
|
Dwarf_Die child_die = nullptr; |
|
530
|
|
|
|
|
|
|
Dwarf_Error error; |
|
531
|
0
|
|
|
|
|
|
int res = dwarf_child(die, &child_die, &error); |
|
532
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
533
|
0
|
|
|
|
|
|
DieSP child(new DieRC(child_die, debug, root)); |
|
534
|
0
|
|
|
|
|
|
auto check = [&](DieSP& node) mutable { |
|
535
|
|
|
|
|
|
|
Dwarf_Error error; |
|
536
|
0
|
|
|
|
|
|
Dwarf_Half tag = 0; |
|
537
|
|
|
|
|
|
|
|
|
538
|
0
|
0
|
|
|
|
|
res = dwarf_tag(node->die, &tag, &error); |
|
539
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return Scan::dead_end; } |
|
540
|
|
|
|
|
|
|
|
|
541
|
0
|
0
|
|
|
|
|
if( tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) { |
|
|
|
0
|
|
|
|
|
|
|
542
|
0
|
|
|
|
|
|
switch(node->contains(offset)) { |
|
543
|
0
|
|
|
|
|
|
case Scan::dead_end: return Scan::dead_end; |
|
544
|
0
|
0
|
|
|
|
|
case Scan::found: context.push_back(node); break; |
|
545
|
0
|
|
|
|
|
|
default: break; |
|
546
|
|
|
|
|
|
|
} |
|
547
|
|
|
|
|
|
|
} |
|
548
|
|
|
|
|
|
|
/* scan everything */ |
|
549
|
0
|
|
|
|
|
|
return Scan::not_found; |
|
550
|
0
|
|
|
|
|
|
}; |
|
551
|
0
|
|
|
|
|
|
iterate(child, check); |
|
552
|
|
|
|
|
|
|
} |
|
553
|
|
|
|
|
|
|
|
|
554
|
0
|
0
|
|
|
|
|
while (context.size() > max_inline) context.pop_front(); |
|
555
|
|
|
|
|
|
|
|
|
556
|
0
|
|
|
|
|
|
auto best = context.back(); |
|
557
|
0
|
|
|
|
|
|
context.pop_back(); |
|
558
|
0
|
|
|
|
|
|
best->context = std::move(context); |
|
559
|
0
|
|
|
|
|
|
return best; |
|
560
|
|
|
|
|
|
|
} |
|
561
|
|
|
|
|
|
|
|
|
562
|
0
|
|
|
|
|
|
panda::optional DieRC::get_addr() noexcept { |
|
563
|
|
|
|
|
|
|
Dwarf_Error error; |
|
564
|
0
|
|
|
|
|
|
Dwarf_Addr low = 0; |
|
565
|
0
|
|
|
|
|
|
Dwarf_Addr high = 0; |
|
566
|
|
|
|
|
|
|
|
|
567
|
0
|
|
|
|
|
|
auto res = dwarf_lowpc(die,&low,&error); |
|
568
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
569
|
|
|
|
|
|
|
Dwarf_Form_Class formclass; |
|
570
|
0
|
|
|
|
|
|
Dwarf_Half form = 0; |
|
571
|
0
|
|
|
|
|
|
res = dwarf_highpc_b(die,&high,&form,&formclass,&error); |
|
572
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
573
|
0
|
0
|
|
|
|
|
if (formclass == DW_FORM_CLASS_CONSTANT) { high += low; } |
|
574
|
0
|
|
|
|
|
|
return panda::optional{HighLow{low, high}}; |
|
575
|
|
|
|
|
|
|
} |
|
576
|
|
|
|
|
|
|
} |
|
577
|
|
|
|
|
|
|
/* Cannot check ranges yet, we don't know the ranges base offset yet. */ |
|
578
|
0
|
|
|
|
|
|
return panda::optional(); |
|
579
|
|
|
|
|
|
|
} |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
|
|
582
|
0
|
|
|
|
|
|
Scan DieRC::contains(std::uint64_t offset) noexcept { |
|
583
|
0
|
|
|
|
|
|
auto addr = get_addr(); |
|
584
|
0
|
0
|
|
|
|
|
if (addr) { |
|
585
|
0
|
0
|
|
|
|
|
if ((addr->high >= offset) || (addr->low < offset)) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
586
|
0
|
|
|
|
|
|
return Scan::dead_end; |
|
587
|
|
|
|
|
|
|
} else { |
|
588
|
0
|
|
|
|
|
|
return Scan::found; |
|
589
|
|
|
|
|
|
|
} |
|
590
|
|
|
|
|
|
|
} else { |
|
591
|
|
|
|
|
|
|
Dwarf_Error error; |
|
592
|
|
|
|
|
|
|
Dwarf_Attribute attr; |
|
593
|
0
|
|
|
|
|
|
auto res = dwarf_attr(die, DW_AT_ranges, &attr, &error); |
|
594
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
595
|
|
|
|
|
|
|
Dwarf_Off ranges_offset; |
|
596
|
0
|
|
|
|
|
|
res = dwarf_global_formref(attr, &ranges_offset, &error); |
|
597
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
598
|
|
|
|
|
|
|
Dwarf_Ranges *ranges; |
|
599
|
|
|
|
|
|
|
Dwarf_Signed ranges_count; |
|
600
|
|
|
|
|
|
|
Dwarf_Unsigned byte_count; |
|
601
|
0
|
|
|
|
|
|
res = dwarf_get_ranges_a(debug, ranges_offset, die, &ranges, &ranges_count, &byte_count, &error); |
|
602
|
0
|
0
|
|
|
|
|
if (res == DW_DLV_OK) { |
|
603
|
0
|
|
|
|
|
|
Dwarf_Addr baseaddr = 0; |
|
604
|
0
|
0
|
|
|
|
|
for(int i = 0; i < ranges_count; ++i) { |
|
605
|
0
|
|
|
|
|
|
auto r = ranges[i]; |
|
606
|
0
|
|
|
|
|
|
switch (r.dwr_type) { |
|
607
|
0
|
|
|
|
|
|
case DW_RANGES_ADDRESS_SELECTION: baseaddr = r.dwr_addr2; break; |
|
608
|
|
|
|
|
|
|
case DW_RANGES_ENTRY: { |
|
609
|
0
|
|
|
|
|
|
auto low = r.dwr_addr1 + baseaddr; |
|
610
|
0
|
|
|
|
|
|
auto high = r.dwr_addr2 + baseaddr; |
|
611
|
0
|
0
|
|
|
|
|
auto matches = (low <= offset) && (high > offset); |
|
|
|
0
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
//std::cout << "l = " << low << ", h = " << high << ", attr = " << ranges_offset << ", o = " << offset << " " << (matches ? "Y" : "N") << "\n"; |
|
613
|
0
|
0
|
|
|
|
|
if (matches) {return Scan::found; } |
|
614
|
0
|
|
|
|
|
|
break; |
|
615
|
|
|
|
|
|
|
} |
|
616
|
0
|
|
|
|
|
|
default: break; |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
} |
|
619
|
|
|
|
|
|
|
} |
|
620
|
0
|
0
|
|
|
|
|
if (ranges_count > 0) { return Scan::dead_end; } |
|
621
|
|
|
|
|
|
|
} |
|
622
|
|
|
|
|
|
|
} |
|
623
|
|
|
|
|
|
|
} |
|
624
|
|
|
|
|
|
|
} |
|
625
|
0
|
|
|
|
|
|
return Scan::not_found; |
|
626
|
|
|
|
|
|
|
} |
|
627
|
|
|
|
|
|
|
|
|
628
|
1054
|
|
|
|
|
|
CU::CU(Dwarf_Debug debug_, int number_): debug{debug_}, number{number_} { |
|
629
|
527
|
|
|
|
|
|
std::memset(&signature, 0, sizeof(signature)); |
|
630
|
527
|
|
|
|
|
|
} |
|
631
|
|
|
|
|
|
|
|
|
632
|
1581
|
|
|
|
|
|
CU::~CU() { |
|
633
|
527
|
50
|
|
|
|
|
if (sources) { |
|
634
|
0
|
0
|
|
|
|
|
for(size_t i = 0; i < static_cast(sources_count); ++i) { |
|
635
|
0
|
|
|
|
|
|
dwarf_dealloc(debug, sources[i], DW_DLA_STRING); |
|
636
|
|
|
|
|
|
|
} |
|
637
|
0
|
|
|
|
|
|
dwarf_dealloc(debug, sources, DW_DLA_LIST); |
|
638
|
|
|
|
|
|
|
} |
|
639
|
1054
|
50
|
|
|
|
|
} |
|
640
|
|
|
|
|
|
|
|
|
641
|
0
|
|
|
|
|
|
LookupResult CU::resolve(std::uint64_t offset) noexcept { |
|
642
|
0
|
0
|
|
|
|
|
assert(cu_die); |
|
643
|
0
|
|
|
|
|
|
LookupResult lr(*this); |
|
644
|
0
|
|
|
|
|
|
resolve(offset, cu_die, lr); |
|
645
|
0
|
|
|
|
|
|
return lr; |
|
646
|
|
|
|
|
|
|
} |
|
647
|
|
|
|
|
|
|
|
|
648
|
0
|
|
|
|
|
|
void CU::resolve(std::uint64_t offset, DieSP &root, LookupResult& lr) noexcept { |
|
649
|
0
|
|
|
|
|
|
auto check = [&](DieSP& node) -> Scan { |
|
650
|
|
|
|
|
|
|
Dwarf_Error error; |
|
651
|
0
|
|
|
|
|
|
Dwarf_Half tag = 0; |
|
652
|
0
|
0
|
|
|
|
|
assert(node->die); |
|
653
|
0
|
0
|
|
|
|
|
auto res = dwarf_tag(node->die, &tag, &error); |
|
654
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { return Scan::dead_end; } |
|
655
|
|
|
|
|
|
|
|
|
656
|
0
|
0
|
|
|
|
|
if( tag == DW_TAG_subprogram || |
|
|
|
0
|
|
|
|
|
|
|
657
|
0
|
|
|
|
|
|
tag == DW_TAG_inlined_subroutine) { |
|
658
|
0
|
0
|
|
|
|
|
switch (node->contains(offset)) { |
|
659
|
|
|
|
|
|
|
case Scan::found: { |
|
660
|
0
|
0
|
|
|
|
|
lr.subprogram = node; |
|
661
|
0
|
|
|
|
|
|
lr.offset = offset; |
|
662
|
0
|
|
|
|
|
|
return Scan::found; |
|
663
|
|
|
|
|
|
|
} |
|
664
|
0
|
|
|
|
|
|
default: return Scan::not_found; |
|
665
|
|
|
|
|
|
|
} |
|
666
|
|
|
|
|
|
|
} |
|
667
|
0
|
0
|
|
|
|
|
else if(tag == DW_TAG_compile_unit) { |
|
668
|
0
|
|
|
|
|
|
Scan scan = node->contains(offset); |
|
669
|
0
|
0
|
|
|
|
|
switch (scan) { |
|
670
|
|
|
|
|
|
|
case Scan::found: { |
|
671
|
0
|
0
|
|
|
|
|
lr.cu = node; |
|
672
|
0
|
0
|
|
|
|
|
return lr.is_complete() ? Scan::found : Scan::not_found; |
|
673
|
|
|
|
|
|
|
} |
|
674
|
0
|
|
|
|
|
|
default: return scan; |
|
675
|
|
|
|
|
|
|
} |
|
676
|
|
|
|
|
|
|
} |
|
677
|
|
|
|
|
|
|
/* keep scaning */ |
|
678
|
0
|
|
|
|
|
|
return Scan::not_found; |
|
679
|
0
|
|
|
|
|
|
}; |
|
680
|
0
|
|
|
|
|
|
iterate(root, check); |
|
681
|
0
|
|
|
|
|
|
} |
|
682
|
|
|
|
|
|
|
|
|
683
|
0
|
|
|
|
|
|
string CU::get_source(size_t index) noexcept { |
|
684
|
0
|
0
|
|
|
|
|
if (!sources_count) { |
|
685
|
0
|
|
|
|
|
|
auto res = dwarf_srcfiles(cu_die->die, &sources, &sources_count, nullptr); |
|
686
|
0
|
0
|
|
|
|
|
if (res != DW_DLV_OK) { sources_count = -1; } |
|
687
|
|
|
|
|
|
|
} |
|
688
|
0
|
0
|
|
|
|
|
if (sources_count > 0 && index < static_cast(sources_count)) { |
|
|
|
0
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
/* "subtract 1 to index into srcfiles", see dwarf_line.c */ |
|
690
|
0
|
|
|
|
|
|
return string(sources[index - 1]); |
|
691
|
|
|
|
|
|
|
} |
|
692
|
0
|
|
|
|
|
|
return string{}; |
|
693
|
|
|
|
|
|
|
} |
|
694
|
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
|
|
696
|
24
|
50
|
|
|
|
|
}}} |
|
|
|
50
|
|
|
|
|
|