File Coverage

src/panda/dwarf.cc
Criterion Covered Total %
statement 67 390 17.1
branch 30 350 8.5
condition n/a
subroutine n/a
pod n/a
total 97 740 13.1


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