]> sourceware.org Git - systemtap.git/blob - dwflpp.cxx
buildrun.cxx: adapt to kernel 5.4+
[systemtap.git] / dwflpp.cxx
1 // C++ interface to dwfl
2 // Copyright (C) 2005-2019 Red Hat Inc.
3 // Copyright (C) 2005-2007 Intel Corporation.
4 // Copyright (C) 2008 James.Bottomley@HansenPartnership.com
5 //
6 // This file is part of systemtap, and is free software. You can
7 // redistribute it and/or modify it under the terms of the GNU General
8 // Public License (GPL); either version 2, or (at your option) any
9 // later version.
10
11 #include "dwflpp.h"
12 #include "config.h"
13 #include <cxxabi.h>
14 #include "staptree.h"
15 #include "elaborate.h"
16 #include "tapsets.h"
17 #include "task_finder.h"
18 #include "translate.h"
19 #include "session.h"
20 #include "util.h"
21 #include "buildrun.h"
22 #include "dwarf_wrappers.h"
23 #include "hash.h"
24 #include "rpm_finder.h"
25 #include "setupdwfl.h"
26 #include "loc2stap.h"
27
28 #include <cstdlib>
29 #include <algorithm>
30 #include <deque>
31 #include <iostream>
32 #include <map>
33 #include <set>
34 #include <sstream>
35 #include <stdexcept>
36 #include <vector>
37 #include <cstdarg>
38 #include <cassert>
39 #include <iomanip>
40 #include <cerrno>
41
42 extern "C" {
43 #include <fcntl.h>
44 #include <elfutils/libdwfl.h>
45 #include <elfutils/libdw.h>
46 #include <dwarf.h>
47 #include <elf.h>
48 #include <regex.h>
49 #include <glob.h>
50 #include <fnmatch.h>
51 #include <stdio.h>
52 #include <sys/types.h>
53
54 #define __STDC_FORMAT_MACROS
55 #include <inttypes.h>
56 }
57
58 // Older glibc elf.h don't know about this new constant.
59 #ifndef STB_GNU_UNIQUE
60 #define STB_GNU_UNIQUE 10
61 #endif
62
63
64 // debug flag to compare to the uncached version from libdw
65 // #define DEBUG_DWFLPP_GETSCOPES 1
66
67
68 using namespace std;
69 using namespace __gnu_cxx;
70
71
72 static string TOK_KERNEL("kernel");
73
74
75 dwflpp::dwflpp(systemtap_session & session, const string& name, bool kernel_p):
76 sess(session), module(NULL), module_bias(0), mod_info(NULL),
77 module_start(0), module_end(0), cu(NULL), dwfl(NULL),
78 module_dwarf(NULL), function(NULL), blacklist_func(), blacklist_func_ret(),
79 blacklist_file(), blacklist_enabled(false)
80 {
81 if (kernel_p)
82 setup_kernel(name, session);
83 else
84 {
85 vector<string> modules;
86 modules.push_back(name);
87 setup_user(modules);
88 }
89 }
90
91 dwflpp::dwflpp(systemtap_session & session, const vector<string>& names,
92 bool kernel_p):
93 sess(session), module(NULL), module_bias(0), mod_info(NULL),
94 module_start(0), module_end(0), cu(NULL), dwfl(NULL),
95 module_dwarf(NULL), function(NULL), blacklist_enabled(false)
96 {
97 if (kernel_p)
98 setup_kernel(names);
99 else
100 setup_user(names);
101 }
102
103 dwflpp::~dwflpp()
104 {
105 delete_map(module_cu_cache);
106 delete_map(cu_function_cache);
107 delete_map(mod_function_cache);
108 delete_map(cu_inl_function_cache);
109 delete_map(cu_call_sites_cache);
110 delete_map(global_alias_cache);
111 delete_map(cu_die_parent_cache);
112
113 for (auto i = cu_lines_cache.begin(); i != cu_lines_cache.end(); ++i)
114 delete_map(*i->second);
115 delete_map(cu_lines_cache);
116
117 delete_map(cu_entry_pc_cache);
118
119 if (dwfl)
120 dwfl_end(dwfl);
121 // NB: don't "delete mod_info;", as that may be shared
122 // between dwflpp instances, and are stored in
123 // session.module_cache[] anyway.
124 }
125
126
127 module_cache::~module_cache ()
128 {
129 delete_map(cache);
130 }
131
132
133 void
134 dwflpp::get_module_dwarf(bool required, bool report)
135 {
136 module_dwarf = dwfl_module_getdwarf(module, &module_bias);
137 mod_info->dwarf_status = (module_dwarf ? info_present : info_absent);
138 if (!module_dwarf && report)
139 {
140 string msg = _("cannot find ");
141 if (module_name == "")
142 msg += "kernel";
143 else
144 msg += string("module ") + module_name;
145 msg += " debuginfo";
146
147 int i = dwfl_errno();
148 if (i)
149 msg += string(": ") + dwfl_errmsg (i);
150
151 msg += " [man warning::debuginfo]";
152
153 /* add module_name to list to find rpm */
154 find_debug_rpms(sess, module_name.c_str());
155
156 if (required)
157 throw SEMANTIC_ERROR (msg);
158 else
159 sess.print_warning(msg);
160 }
161 }
162
163
164 void
165 dwflpp::focus_on_module(Dwfl_Module * m, module_info * mi)
166 {
167 module = m;
168 mod_info = mi;
169 if (m)
170 {
171 module_name = dwfl_module_info(module, NULL, &module_start, &module_end,
172 NULL, NULL, NULL, NULL) ?: "module";
173 }
174 else
175 {
176 assert(mi && mi->name && mi->name == TOK_KERNEL);
177 module_name = mi->name;
178 module_start = 0;
179 module_end = 0;
180 module_bias = mi->bias;
181 }
182
183 // Reset existing pointers and names
184
185 module_dwarf = NULL;
186
187 cu = NULL;
188
189 function_name.clear();
190 function = NULL;
191 }
192
193
194 void
195 dwflpp::focus_on_cu(Dwarf_Die * c)
196 {
197 assert(c);
198 assert(module);
199
200 cu = c;
201
202 // Reset existing pointers and names
203 function_name.clear();
204 function = NULL;
205 }
206
207
208 string
209 dwflpp::cu_name(void)
210 {
211 return dwarf_diename(cu) ?: "<unknown source>";
212 }
213
214
215 void
216 dwflpp::focus_on_function(Dwarf_Die * f)
217 {
218 assert(f);
219 assert(module);
220 assert(cu);
221
222 function = f;
223 function_name = dwarf_diename(function) ?: "function";
224 }
225
226
227 /* Return the Dwarf_Die for the given address in the current module.
228 * The address should be in the module address address space (this
229 * function will take care of any dw bias).
230 */
231 Dwarf_Die *
232 dwflpp::query_cu_containing_address(Dwarf_Addr a)
233 {
234 Dwarf_Addr bias;
235 assert(dwfl);
236 assert(module);
237 get_module_dwarf();
238
239 Dwarf_Die* cudie = dwfl_module_addrdie(module, a, &bias);
240 assert(bias == module_bias);
241 return cudie;
242 }
243
244
245 bool
246 dwflpp::module_name_matches(const string& pattern)
247 {
248 bool t = (fnmatch(pattern.c_str(), module_name.c_str(), 0) == 0);
249 if (t && sess.verbose>3)
250 clog << _F("pattern '%s' matches module '%s'\n",
251 pattern.c_str(), module_name.c_str());
252 if (!t && sess.verbose>4)
253 clog << _F("pattern '%s' does not match module '%s'\n",
254 pattern.c_str(), module_name.c_str());
255
256 return t;
257 }
258
259
260 bool
261 dwflpp::name_has_wildcard (const string& pattern)
262 {
263 return (pattern.find('*') != string::npos ||
264 pattern.find('?') != string::npos ||
265 pattern.find('[') != string::npos);
266 }
267
268
269 bool
270 dwflpp::module_name_final_match(const string& pattern)
271 {
272 // Assume module_name_matches(). Can there be any more matches?
273 // Not unless the pattern is a wildcard, since module names are
274 // presumed unique.
275 return !name_has_wildcard(pattern);
276 }
277
278
279 bool
280 dwflpp::function_name_matches_pattern(const string& name, const string& pattern)
281 {
282 bool t = (fnmatch(pattern.c_str(), name.c_str(), 0) == 0);
283 if (t && sess.verbose>3)
284 clog << _F("pattern '%s' matches function '%s'\n", pattern.c_str(), name.c_str());
285 return t;
286 }
287
288
289 bool
290 dwflpp::function_name_matches(const string& pattern)
291 {
292 assert(function);
293 return function_name_matches_pattern(function_name, pattern);
294 }
295
296
297 bool
298 dwflpp::function_scope_matches(const vector<string>& scopes)
299 {
300 // walk up the containing scopes
301 Dwarf_Die* die = function;
302 for (int i = scopes.size() - 1; i >= 0; --i)
303 {
304 die = get_parent_scope(die);
305
306 // check if this scope matches, and prepend it if so
307 // NB: a NULL die is the global scope, compared as ""
308 string name = dwarf_diename(die) ?: "";
309 if (name_has_wildcard(scopes[i]) ?
310 function_name_matches_pattern(name, scopes[i]) :
311 name == scopes[i])
312 function_name = name + "::" + function_name;
313 else
314 return false;
315
316 // make sure there's no more if we're at the global scope
317 if (!die && i > 0)
318 return false;
319 }
320 return true;
321 }
322
323
324 void
325 dwflpp::setup_kernel(const string& name, systemtap_session & s, bool debuginfo_needed)
326 {
327 if (! sess.module_cache)
328 sess.module_cache = new module_cache ();
329
330 unsigned offline_search_matches = 0;
331 dwfl = setup_dwfl_kernel(name, &offline_search_matches, sess);
332
333 if (offline_search_matches < 1)
334 {
335 if (debuginfo_needed) {
336 // Suggest a likely kernel dir to find debuginfo rpm for
337 string dir = string(sess.sysroot + "/lib/modules/" + sess.kernel_release );
338 find_debug_rpms(sess, dir.c_str());
339 }
340 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
341 sess.architecture.c_str(), sess.kernel_build_tree.c_str()));
342 }
343
344 if (dwfl != NULL)
345 {
346 ptrdiff_t off = 0;
347 do
348 {
349 assert_no_interrupts();
350 off = dwfl_getmodules (dwfl, &add_module_build_id_to_hash, &s, off);
351 }
352 while (off > 0);
353 DWFL_ASSERT("dwfl_getmodules", off == 0);
354 }
355
356 build_kernel_blacklist();
357 }
358
359 void
360 dwflpp::setup_kernel(const vector<string> &names, bool debuginfo_needed)
361 {
362 if (! sess.module_cache)
363 sess.module_cache = new module_cache ();
364
365 unsigned offline_search_matches = 0;
366 set<string> offline_search_names(names.begin(), names.end());
367 dwfl = setup_dwfl_kernel(offline_search_names,
368 &offline_search_matches,
369 sess);
370
371 if (offline_search_matches < offline_search_names.size())
372 {
373 if (debuginfo_needed) {
374 // Suggest a likely kernel dir to find debuginfo rpm for
375 string dir = string(sess.sysroot + "/lib/modules/" + sess.kernel_release );
376 find_debug_rpms(sess, dir.c_str());
377 }
378 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
379 sess.architecture.c_str(), sess.kernel_build_tree.c_str()));
380 }
381
382 build_kernel_blacklist();
383 }
384
385
386 void
387 dwflpp::setup_user(const vector<string>& modules, bool debuginfo_needed)
388 {
389 if (! sess.module_cache)
390 sess.module_cache = new module_cache ();
391
392 auto it = modules.begin();
393 dwfl = setup_dwfl_user(it, modules.end(), debuginfo_needed, sess);
394 if (debuginfo_needed && it != modules.end())
395 DWFL_ASSERT (string(_F("missing process %s %s debuginfo",
396 (*it).c_str(), sess.architecture.c_str())),
397 dwfl);
398
399 build_user_blacklist();
400 }
401
402 template<> void
403 dwflpp::iterate_over_modules<void>(int (*callback)(Dwfl_Module*,
404 void**,
405 const char*,
406 Dwarf_Addr,
407 void*),
408 void *data)
409 {
410 dwfl_getmodules (dwfl, callback, data, 0);
411
412 // Don't complain if we exited dwfl_getmodules early.
413 // This could be a $target variable error that will be
414 // reported soon anyway.
415 // DWFL_ASSERT("dwfl_getmodules", off == 0);
416
417 // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the
418 // "kernel" module is not reported in the loop above. However, we
419 // may be able to make do with symbol table data.
420 }
421
422
423 template<> void
424 dwflpp::iterate_over_cus<void>(int (*callback)(Dwarf_Die*, void*),
425 void *data,
426 bool want_types)
427 {
428 get_module_dwarf(false);
429 Dwarf *dw = module_dwarf;
430 if (!dw) return;
431
432 vector<Dwarf_Die>* v = module_cu_cache[dw];
433 if (v == 0)
434 {
435 v = new vector<Dwarf_Die>;
436 module_cu_cache[dw] = v;
437
438 Dwarf_Off off = 0;
439 size_t cuhl;
440 Dwarf_Off noff;
441 while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
442 {
443 assert_no_interrupts();
444 Dwarf_Die die_mem;
445 Dwarf_Die *die;
446 die = dwarf_offdie (dw, off + cuhl, &die_mem);
447 /* Skip partial units. */
448 if (dwarf_tag (die) == DW_TAG_compile_unit)
449 v->push_back (*die); /* copy */
450 off = noff;
451 }
452 }
453
454 if (want_types && module_tus_read.find(dw) == module_tus_read.end())
455 {
456 // Process type units.
457 Dwarf_Off off = 0;
458 size_t cuhl;
459 Dwarf_Off noff;
460 uint64_t type_signature;
461 while (dwarf_next_unit (dw, off, &noff, &cuhl, NULL, NULL, NULL, NULL,
462 &type_signature, NULL) == 0)
463 {
464 assert_no_interrupts();
465 Dwarf_Die die_mem;
466 Dwarf_Die *die;
467 die = dwarf_offdie_types (dw, off + cuhl, &die_mem);
468 /* Skip partial units. */
469 if (dwarf_tag (die) == DW_TAG_type_unit)
470 v->push_back (*die); /* copy */
471 off = noff;
472 }
473 module_tus_read.insert(dw);
474 }
475
476 for (auto i = v->begin(); i != v->end(); ++i)
477 {
478 int rc = (*callback)(&*i, data);
479 assert_no_interrupts();
480 if (rc != DWARF_CB_OK)
481 break;
482 }
483 }
484
485
486 bool
487 dwflpp::func_is_inline()
488 {
489 assert (function);
490 return dwarf_func_inline (function) != 0;
491 }
492
493
494 bool
495 dwflpp::func_is_exported()
496 {
497 const char *name = dwarf_linkage_name (function) ?: dwarf_diename (function);
498
499 assert (function);
500
501 int syms = dwfl_module_getsymtab (module);
502 DWFL_ASSERT (_("Getting symbols"), syms >= 0);
503
504 for (int i = 0; i < syms; i++)
505 {
506 GElf_Sym sym;
507 GElf_Word shndxp;
508 const char *symname = dwfl_module_getsym(module, i, &sym, &shndxp);
509 if (symname
510 && strcmp (name, symname) == 0)
511 {
512 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC
513 && (GELF_ST_BIND(sym.st_info) == STB_GLOBAL
514 || GELF_ST_BIND(sym.st_info) == STB_WEAK
515 || GELF_ST_BIND(sym.st_info) == STB_GNU_UNIQUE))
516 return true;
517 else
518 return false;
519 }
520 }
521 return false;
522 }
523
524 void
525 dwflpp::cache_inline_instances (Dwarf_Die* die)
526 {
527 // If this is an inline instance, link it back to its origin
528 Dwarf_Die origin;
529 if (dwarf_tag(die) == DW_TAG_inlined_subroutine &&
530 dwarf_attr_die(die, DW_AT_abstract_origin, &origin))
531 {
532 vector<Dwarf_Die>*& v = cu_inl_function_cache[origin.addr];
533 if (!v)
534 v = new vector<Dwarf_Die>;
535 v->push_back(*die);
536 }
537
538 // Recurse through other scopes that may contain inlines
539 Dwarf_Die child, import;
540 if (dwarf_child(die, &child) == 0)
541 do
542 {
543 switch (dwarf_tag (&child))
544 {
545 // tags that could contain inlines
546 case DW_TAG_compile_unit:
547 case DW_TAG_module:
548 case DW_TAG_lexical_block:
549 case DW_TAG_with_stmt:
550 case DW_TAG_catch_block:
551 case DW_TAG_try_block:
552 case DW_TAG_entry_point:
553 case DW_TAG_inlined_subroutine:
554 case DW_TAG_subprogram:
555 cache_inline_instances(&child);
556 break;
557
558 // imported dies should be followed
559 case DW_TAG_imported_unit:
560 if (dwarf_attr_die(&child, DW_AT_import, &import))
561 cache_inline_instances(&import);
562 break;
563
564 // nothing to do for other tags
565 default:
566 break;
567 }
568 }
569 while (dwarf_siblingof(&child, &child) == 0);
570 }
571
572
573 template<> void
574 dwflpp::iterate_over_inline_instances<void>(int (*callback)(Dwarf_Die*, void*),
575 void *data)
576 {
577 assert (function);
578 assert (func_is_inline ());
579
580 if (cu_inl_function_cache_done.insert(cu->addr).second)
581 cache_inline_instances(cu);
582
583 vector<Dwarf_Die>* v = cu_inl_function_cache[function->addr];
584 if (!v)
585 return;
586
587 for (auto i = v->begin(); i != v->end(); ++i)
588 {
589 int rc = (*callback)(&*i, data);
590 assert_no_interrupts();
591 if (rc != DWARF_CB_OK)
592 break;
593 }
594 }
595
596
597 void dwflpp::cache_call_sites (Dwarf_Die* die, Dwarf_Die *function)
598 {
599 Dwarf_Die origin;
600 if (dwarf_tag(die) == DW_TAG_GNU_call_site &&
601 dwarf_attr_die(die, DW_AT_abstract_origin, &origin))
602 {
603 vector<call_site_cache_t>*& v = cu_call_sites_cache[origin.addr];
604 if (!v)
605 v = new vector<call_site_cache_t>;
606 call_site_cache_t c (*die, *function);
607 v->push_back(c);
608 }
609
610 Dwarf_Die child;
611 if (dwarf_child(die, &child) == 0)
612 do
613 {
614 switch (dwarf_tag (&child))
615 {
616 // tags that could contain call sites
617 case DW_TAG_compile_unit:
618 case DW_TAG_module:
619 case DW_TAG_lexical_block:
620 case DW_TAG_with_stmt:
621 case DW_TAG_catch_block:
622 case DW_TAG_try_block:
623 case DW_TAG_entry_point:
624 case DW_TAG_GNU_call_site:
625 cache_call_sites(&child, function);
626 break;
627
628 case DW_TAG_subprogram:
629 case DW_TAG_inlined_subroutine:
630 cache_call_sites(&child, &child);
631
632 // nothing to do for other tags
633 default:
634 break;
635 }
636 }
637 while (dwarf_siblingof(&child, &child) == 0);
638 }
639
640 template<> void
641 dwflpp::iterate_over_call_sites<void> (int (*callback)(Dwarf_Die*, Dwarf_Die*, void*),
642 void *data)
643 {
644 assert (cu);
645 assert (function);
646
647 if (cu_call_sites_cache_done.insert(cu->addr).second)
648 cache_call_sites(cu, NULL);
649
650 vector<call_site_cache_t>* v = cu_call_sites_cache[function->addr];
651 if (!v)
652 return;
653
654 for (auto i = v->begin(); i != v->end(); ++i)
655 {
656 int rc = (*callback)(&i->first, &i->second, data);
657 if (rc != DWARF_CB_OK)
658 break;
659 }
660 }
661
662
663 void
664 dwflpp::cache_die_parents(cu_die_parent_cache_t* parents, Dwarf_Die* die)
665 {
666 // Record and recurse through DIEs we care about
667 Dwarf_Die child, import;
668 if (dwarf_child(die, &child) == 0)
669 do
670 {
671 switch (dwarf_tag (&child))
672 {
673 // normal tags to recurse
674 case DW_TAG_compile_unit:
675 case DW_TAG_module:
676 case DW_TAG_lexical_block:
677 case DW_TAG_with_stmt:
678 case DW_TAG_catch_block:
679 case DW_TAG_try_block:
680 case DW_TAG_entry_point:
681 case DW_TAG_inlined_subroutine:
682 case DW_TAG_subprogram:
683 case DW_TAG_namespace:
684 case DW_TAG_class_type:
685 case DW_TAG_structure_type:
686 parents->insert(make_pair(child.addr, *die));
687 cache_die_parents(parents, &child);
688 break;
689
690 // record only, nothing to recurse
691 case DW_TAG_label:
692 parents->insert(make_pair(child.addr, *die));
693 break;
694
695 // imported dies should be followed
696 case DW_TAG_imported_unit:
697 if (dwarf_attr_die(&child, DW_AT_import, &import))
698 {
699 parents->insert(make_pair(import.addr, *die));
700 cache_die_parents(parents, &import);
701 }
702 break;
703
704 // nothing to do for other tags
705 default:
706 break;
707 }
708 }
709 while (dwarf_siblingof(&child, &child) == 0);
710 }
711
712
713 cu_die_parent_cache_t*
714 dwflpp::get_die_parents()
715 {
716 assert (cu);
717
718 cu_die_parent_cache_t *& parents = cu_die_parent_cache[cu->addr];
719 if (!parents)
720 {
721 parents = new cu_die_parent_cache_t;
722 cache_die_parents(parents, cu);
723 if (sess.verbose > 4)
724 clog << _F("die parent cache %s:%s size %zu", module_name.c_str(),
725 cu_name().c_str(), parents->size()) << endl;
726 }
727 return parents;
728 }
729
730
731 vector<Dwarf_Die>
732 dwflpp::getscopes_die(Dwarf_Die* die)
733 {
734 cu_die_parent_cache_t *parents = get_die_parents();
735
736 vector<Dwarf_Die> scopes;
737 Dwarf_Die *scope = die;
738 auto it = parents->end();
739 do
740 {
741 scopes.push_back(*scope);
742 it = parents->find(scope->addr);
743 scope = &it->second;
744 }
745 while (it != parents->end());
746
747 #ifdef DEBUG_DWFLPP_GETSCOPES
748 Dwarf_Die *dscopes = NULL;
749 int nscopes = dwarf_getscopes_die(die, &dscopes);
750
751 assert(nscopes == (int)scopes.size());
752 for (unsigned i = 0; i < scopes.size(); ++i)
753 assert(scopes[i].addr == dscopes[i].addr);
754 free(dscopes);
755 #endif
756
757 return scopes;
758 }
759
760
761 std::vector<Dwarf_Die>
762 dwflpp::getscopes(Dwarf_Die* die)
763 {
764 cu_die_parent_cache_t *parents = get_die_parents();
765
766 vector<Dwarf_Die> scopes;
767
768 Dwarf_Die origin;
769 Dwarf_Die *scope = die;
770 auto it = parents->end();
771 do
772 {
773 scopes.push_back(*scope);
774 if (dwarf_tag(scope) == DW_TAG_inlined_subroutine &&
775 dwarf_attr_die(scope, DW_AT_abstract_origin, &origin))
776 scope = &origin;
777
778 it = parents->find(scope->addr);
779 scope = &it->second;
780 }
781 while (it != parents->end());
782
783 #ifdef DEBUG_DWFLPP_GETSCOPES
784 // there isn't an exact libdw equivalent, but if dwarf_getscopes on the
785 // entrypc returns the same first die, then all the scopes should match
786 Dwarf_Addr pc;
787 if (die_entrypc(die, &pc))
788 {
789 Dwarf_Die *dscopes = NULL;
790 int nscopes = dwarf_getscopes(cu, pc, &dscopes);
791 if (nscopes > 0 && dscopes[0].addr == die->addr)
792 {
793 assert(nscopes == (int)scopes.size());
794 for (unsigned i = 0; i < scopes.size(); ++i)
795 assert(scopes[i].addr == dscopes[i].addr);
796 }
797 free(dscopes);
798 }
799 #endif
800
801 return scopes;
802 }
803
804
805 std::vector<Dwarf_Die>
806 dwflpp::getscopes(Dwarf_Addr pc)
807 {
808 // The die_parent_cache doesn't help us without knowing where the pc is
809 // contained, so we have to do this one the old fashioned way.
810
811 assert (cu);
812
813 vector<Dwarf_Die> scopes;
814
815 Dwarf_Die* dwarf_scopes;
816 int nscopes = dwarf_getscopes(cu, pc, &dwarf_scopes);
817 if (nscopes > 0)
818 {
819 scopes.assign(dwarf_scopes, dwarf_scopes + nscopes);
820 free(dwarf_scopes);
821 }
822
823 #ifdef DEBUG_DWFLPP_GETSCOPES
824 // check that getscopes on the starting die gets the same result
825 if (!scopes.empty())
826 {
827 vector<Dwarf_Die> other = getscopes(&scopes[0]);
828 assert(scopes.size() == other.size());
829 for (unsigned i = 0; i < scopes.size(); ++i)
830 assert(scopes[i].addr == other[i].addr);
831 }
832 #endif
833
834 return scopes;
835 }
836
837
838 Dwarf_Die*
839 dwflpp::get_parent_scope(Dwarf_Die* die)
840 {
841 Dwarf_Die specification;
842 if (dwarf_attr_die(die, DW_AT_specification, &specification))
843 die = &specification;
844
845 cu_die_parent_cache_t *parents = get_die_parents();
846 auto it = parents->find(die->addr);
847 while (it != parents->end())
848 {
849 Dwarf_Die* scope = &it->second;
850 switch (dwarf_tag (scope))
851 {
852 case DW_TAG_namespace:
853 case DW_TAG_class_type:
854 case DW_TAG_structure_type:
855 return scope;
856
857 default:
858 break;
859 }
860 it = parents->find(scope->addr);
861 }
862 return NULL;
863 }
864
865 static const char*
866 cache_type_prefix(Dwarf_Die* type)
867 {
868 switch (dwarf_tag(type))
869 {
870 case DW_TAG_enumeration_type:
871 return "enum ";
872 case DW_TAG_structure_type:
873 case DW_TAG_class_type:
874 // treating struct/class as equals
875 return "struct ";
876 case DW_TAG_union_type:
877 return "union ";
878 }
879 return "";
880 }
881
882 /* GCC might generate a struct/class without DW_AT_declaration,
883 but that only contains members which have DW_AT_declaration
884 set. We aren't interested in those. PR14434 (GCC bug #54181). */
885 static bool
886 has_only_decl_members (Dwarf_Die *die)
887 {
888 Dwarf_Die child, import;
889 if (dwarf_child(die, &child) != 0)
890 return false; /* no members */
891
892 do
893 {
894 if (! dwarf_hasattr(&child, DW_AT_declaration))
895 return false; /* real member found. */
896 int tag = dwarf_tag(&child);
897 if ((tag == DW_TAG_namespace
898 || tag == DW_TAG_structure_type
899 || tag == DW_TAG_class_type)
900 && ! has_only_decl_members (&child))
901 return false; /* real grand child member found. */
902
903 // Unlikely to ever happen, but if there is an imported unit
904 // then check its children as if they are children of this DIE.
905 if (tag == DW_TAG_imported_unit
906 && dwarf_attr_die(&child, DW_AT_import, &import)
907 && ! has_only_decl_members (&import))
908 return false;
909 }
910 while (dwarf_siblingof(&child, &child) == 0);
911
912 return true; /* Tried all children and grandchildren. */
913 }
914
915 int
916 dwflpp::global_alias_caching_callback(Dwarf_Die *die, bool has_inner_types,
917 const string& prefix, cu_type_cache_t *cache)
918 {
919 const char *name = dwarf_diename(die);
920
921 if (!name || dwarf_hasattr(die, DW_AT_declaration)
922 || has_only_decl_members(die))
923 return DWARF_CB_OK;
924
925 int tag = dwarf_tag(die);
926 if (has_inner_types && (tag == DW_TAG_namespace
927 || tag == DW_TAG_structure_type
928 || tag == DW_TAG_class_type))
929 iterate_over_types(die, has_inner_types, prefix + name + "::",
930 global_alias_caching_callback, cache);
931
932 if (tag != DW_TAG_namespace)
933 {
934 string type_name = prefix + cache_type_prefix(die) + name;
935 if (cache->find(type_name) == cache->end())
936 (*cache)[type_name] = *die;
937 }
938
939 return DWARF_CB_OK;
940 }
941
942 int
943 dwflpp::global_alias_caching_callback_cus(Dwarf_Die *die, dwflpp *dw)
944 {
945 mod_cu_type_cache_t *global_alias_cache;
946 global_alias_cache = &dw->global_alias_cache;
947
948 cu_type_cache_t *v = (*global_alias_cache)[die->addr];
949 if (v != 0)
950 return DWARF_CB_OK;
951
952 v = new cu_type_cache_t;
953 (*global_alias_cache)[die->addr] = v;
954 iterate_over_globals(die, global_alias_caching_callback, v);
955
956 return DWARF_CB_OK;
957 }
958
959 Dwarf_Die *
960 dwflpp::declaration_resolve_other_cus(const string& name)
961 {
962 iterate_over_cus(global_alias_caching_callback_cus, this, true);
963 for (auto i = global_alias_cache.begin();
964 i != global_alias_cache.end(); ++i)
965 {
966 cu_type_cache_t *v = (*i).second;
967 if (v->find(name) != v->end())
968 return & ((*v)[name]);
969 }
970
971 return NULL;
972 }
973
974 Dwarf_Die *
975 dwflpp::declaration_resolve(const string& name)
976 {
977 cu_type_cache_t *v = global_alias_cache[cu->addr];
978 if (v == 0) // need to build the cache, just once per encountered module/cu
979 {
980 v = new cu_type_cache_t;
981 global_alias_cache[cu->addr] = v;
982 iterate_over_globals(cu, global_alias_caching_callback, v);
983 if (sess.verbose > 4)
984 clog << _F("global alias cache %s:%s size %zu", module_name.c_str(),
985 cu_name().c_str(), v->size()) << endl;
986 }
987
988 // XXX: it may be desirable to search other modules' declarations
989 // too, in case a module/shared-library processes a
990 // forward-declared pointer type only, where the actual definition
991 // may only be in vmlinux or the application.
992
993 if (v->find(name) == v->end())
994 return declaration_resolve_other_cus(name);
995
996 return & ((*v)[name]);
997 }
998
999 Dwarf_Die *
1000 dwflpp::declaration_resolve(Dwarf_Die *type)
1001 {
1002 const char* name = dwarf_diename(type);
1003 if (!name)
1004 return NULL;
1005
1006 string type_name = cache_type_prefix(type) + string(name);
1007 return declaration_resolve(type_name);
1008 }
1009
1010
1011 int
1012 dwflpp::cu_function_caching_callback (Dwarf_Die* func, cu_function_cache_t *v)
1013 {
1014 const char *name = dwarf_diename(func);
1015 if (!name)
1016 return DWARF_CB_OK;
1017
1018 v->insert(make_pair(name, *func));
1019 return DWARF_CB_OK;
1020 }
1021
1022
1023 int
1024 dwflpp::mod_function_caching_callback (Dwarf_Die* cu, cu_function_cache_t *v)
1025 {
1026 // need to cast callback to func which accepts void*
1027 dwarf_getfuncs (cu, (int (*)(Dwarf_Die*, void*))cu_function_caching_callback,
1028 v, 0);
1029 return DWARF_CB_OK;
1030 }
1031
1032
1033 template<> int
1034 dwflpp::iterate_over_functions<void>(int (*callback)(Dwarf_Die*, void*),
1035 void *data, const string& function)
1036 {
1037 int rc = DWARF_CB_OK;
1038 assert (module);
1039 assert (cu);
1040
1041 cu_function_cache_t *v = cu_function_cache[cu->addr];
1042 if (v == 0)
1043 {
1044 v = new cu_function_cache_t;
1045 cu_function_cache[cu->addr] = v;
1046 // need to cast callback to func which accepts void*
1047 dwarf_getfuncs (cu, (int (*)(Dwarf_Die*, void*))cu_function_caching_callback,
1048 v, 0);
1049 if (sess.verbose > 4)
1050 clog << _F("function cache %s:%s size %zu", module_name.c_str(),
1051 cu_name().c_str(), v->size()) << endl;
1052 mod_info->update_symtab(v);
1053 }
1054
1055 auto range = v->equal_range(function);
1056 if (range.first != range.second)
1057 {
1058 for (auto it = range.first; it != range.second; ++it)
1059 {
1060 Dwarf_Die& die = it->second;
1061 if (sess.verbose > 4)
1062 clog << _F("function cache %s:%s hit %s", module_name.c_str(),
1063 cu_name().c_str(), function.c_str()) << endl;
1064 rc = (*callback)(& die, data);
1065 if (rc != DWARF_CB_OK) break;
1066 }
1067 }
1068 else if (startswith(function, "_Z"))
1069 {
1070 // C++ names are mangled starting with a "_Z" prefix. Most of the time
1071 // we can discover the mangled name from a die's MIPS_linkage_name
1072 // attribute, so we read that to match against the user's function
1073 // pattern. Note that this isn't perfect, as not all will have that
1074 // attribute (notably ctors and dtors), but we do what we can...
1075 for (auto it = v->begin(); it != v->end(); ++it)
1076 {
1077 if (pending_interrupts) return DWARF_CB_ABORT;
1078 Dwarf_Die& die = it->second;
1079 const char* linkage_name = NULL;
1080 if ((linkage_name = dwarf_linkage_name (&die))
1081 && function_name_matches_pattern (linkage_name, function))
1082 {
1083 if (sess.verbose > 4)
1084 clog << _F("function cache %s:%s match %s vs %s", module_name.c_str(),
1085 cu_name().c_str(), linkage_name, function.c_str()) << endl;
1086
1087 rc = (*callback)(& die, data);
1088 if (rc != DWARF_CB_OK) break;
1089 }
1090 }
1091 }
1092 else if (name_has_wildcard (function))
1093 {
1094 for (auto it = v->begin(); it != v->end(); ++it)
1095 {
1096 if (pending_interrupts) return DWARF_CB_ABORT;
1097 const string& func_name = it->first;
1098 Dwarf_Die& die = it->second;
1099 if (function_name_matches_pattern (func_name, function))
1100 {
1101 if (sess.verbose > 4)
1102 clog << _F("function cache %s:%s match %s vs %s", module_name.c_str(),
1103 cu_name().c_str(), func_name.c_str(), function.c_str()) << endl;
1104
1105 rc = (*callback)(& die, data);
1106 if (rc != DWARF_CB_OK) break;
1107 }
1108 }
1109 }
1110 else // not a linkage name or wildcard and no match in this CU
1111 {
1112 // do nothing
1113 }
1114 return rc;
1115 }
1116
1117
1118 template<> int
1119 dwflpp::iterate_single_function<void>(int (*callback)(Dwarf_Die*, void*),
1120 void *data, const string& function)
1121 {
1122 int rc = DWARF_CB_OK;
1123 assert (module);
1124
1125 get_module_dwarf(false);
1126 if (!module_dwarf)
1127 return rc;
1128
1129 cu_function_cache_t *v = mod_function_cache[module_dwarf];
1130 if (v == 0)
1131 {
1132 v = new cu_function_cache_t;
1133 mod_function_cache[module_dwarf] = v;
1134 iterate_over_cus (mod_function_caching_callback, v, false);
1135 if (sess.verbose > 4)
1136 clog << _F("module function cache %s size %zu", module_name.c_str(),
1137 v->size()) << endl;
1138 mod_info->update_symtab(v);
1139 }
1140
1141 auto range = v->equal_range(function);
1142 if (range.first != range.second)
1143 {
1144 for (auto it = range.first; it != range.second; ++it)
1145 {
1146 Dwarf_Die cu_mem;
1147 Dwarf_Die& die = it->second;
1148 if (sess.verbose > 4)
1149 clog << _F("module function cache %s hit %s", module_name.c_str(),
1150 function.c_str()) << endl;
1151
1152 // since we're iterating out of cu-context, we need each focus
1153 focus_on_cu(dwarf_diecu(&die, &cu_mem, NULL, NULL));
1154
1155 rc = (*callback)(& die, data);
1156 if (rc != DWARF_CB_OK) break;
1157 }
1158 }
1159
1160 // undo the focus_on_cu
1161 this->cu = NULL;
1162 this->function_name.clear();
1163 this->function = NULL;
1164
1165 return rc;
1166 }
1167
1168
1169 /* This basically only goes one level down from the compile unit so it
1170 * only picks up top level stuff (i.e. nothing in a lower scope) */
1171 template<> int
1172 dwflpp::iterate_over_globals<void>(Dwarf_Die *cu_die,
1173 int (*callback)(Dwarf_Die*,
1174 bool,
1175 const string&,
1176 void*),
1177 void *data)
1178 {
1179 assert (cu_die);
1180 assert (dwarf_tag(cu_die) == DW_TAG_compile_unit
1181 || dwarf_tag(cu_die) == DW_TAG_type_unit
1182 || dwarf_tag(cu_die) == DW_TAG_partial_unit);
1183
1184 // Ignore partial_unit, if they get imported by a real unit, then
1185 // iterate_over_types will traverse them.
1186 if (dwarf_tag(cu_die) == DW_TAG_partial_unit)
1187 return DWARF_CB_OK;
1188
1189 // If this is C++, recurse for any inner types
1190 bool has_inner_types = dwarf_srclang(cu_die) == DW_LANG_C_plus_plus;
1191
1192 return iterate_over_types(cu_die, has_inner_types, "", callback, data);
1193 }
1194
1195 template<> int
1196 dwflpp::iterate_over_types<void>(Dwarf_Die *top_die,
1197 bool has_inner_types,
1198 const string& prefix,
1199 int (* callback)(Dwarf_Die*,
1200 bool,
1201 const string&,
1202 void*),
1203 void *data)
1204 {
1205 int rc = DWARF_CB_OK;
1206 Dwarf_Die die, import;
1207
1208 assert (top_die);
1209
1210 if (dwarf_child(top_die, &die) != 0)
1211 return rc;
1212
1213 do
1214 /* We're only currently looking for named types,
1215 * although other types of declarations exist */
1216 switch (dwarf_tag(&die))
1217 {
1218 case DW_TAG_base_type:
1219 case DW_TAG_enumeration_type:
1220 case DW_TAG_structure_type:
1221 case DW_TAG_class_type:
1222 case DW_TAG_typedef:
1223 case DW_TAG_union_type:
1224 case DW_TAG_namespace:
1225 rc = (*callback)(&die, has_inner_types, prefix, data);
1226 break;
1227
1228 case DW_TAG_imported_unit:
1229 // Follow the imported_unit and iterate over its contents
1230 // (either a partial_unit or a full compile_unit), all its
1231 // children should be treated as if they appear in this place.
1232 if (dwarf_attr_die(&die, DW_AT_import, &import))
1233 rc = iterate_over_types(&import, has_inner_types, prefix,
1234 callback, data);
1235 break;
1236 }
1237 while (rc == DWARF_CB_OK && dwarf_siblingof(&die, &die) == 0);
1238
1239 return rc;
1240 }
1241
1242
1243 /* For each notes section in the current module call 'callback', use
1244 * 'data' for the notes buffer and pass 'object' back in case
1245 * 'callback' is a method */
1246
1247 template<> int
1248 dwflpp::iterate_over_notes<void>(void *object, void (*callback)(void*,
1249 const string&,
1250 const string&,
1251 int,
1252 const char*,
1253 size_t))
1254 {
1255 Dwarf_Addr bias;
1256 // Note we really want the actual elf file, not the dwarf .debug file.
1257 // Older binutils had a bug where they mangled the SHT_NOTE type during
1258 // --keep-debug.
1259 Elf* elf = dwfl_module_getelf (module, &bias);
1260 size_t shstrndx;
1261 if (elf_getshdrstrndx (elf, &shstrndx))
1262 return elf_errno();
1263
1264 Elf_Scn *scn = NULL;
1265
1266 vector<Dwarf_Die> notes;
1267
1268 while ((scn = elf_nextscn (elf, scn)) != NULL)
1269 {
1270 GElf_Shdr shdr;
1271 if (gelf_getshdr (scn, &shdr) == NULL)
1272 continue;
1273 switch (shdr.sh_type)
1274 {
1275 case SHT_NOTE:
1276 if (!(shdr.sh_flags & SHF_ALLOC))
1277 {
1278 string scn_name = elf_strptr(elf, shstrndx, shdr.sh_name);
1279 Elf_Data *data = elf_getdata (scn, NULL);
1280 size_t next;
1281 GElf_Nhdr nhdr;
1282 size_t name_off;
1283 size_t desc_off;
1284 for (size_t offset = 0;
1285 (next = gelf_getnote (data, offset, &nhdr, &name_off, &desc_off)) > 0;
1286 offset = next)
1287 {
1288 const char *note_name_addr = (const char *)data->d_buf + name_off;
1289 const char *note_desc_addr = (const char *)data->d_buf + desc_off;
1290 string note_name = nhdr.n_namesz > 1 // n_namesz includes NULL
1291 ? string(note_name_addr, nhdr.n_namesz-1) : "";
1292 (*callback) (object, scn_name, note_name, nhdr.n_type,
1293 note_desc_addr, nhdr.n_descsz);
1294 }
1295 }
1296 break;
1297 }
1298 }
1299 return 0;
1300 }
1301
1302
1303 /* For each entry in the .dynamic section in the current module call 'callback'
1304 * returning 'object' in case 'callback' is a method */
1305
1306 template<> void
1307 dwflpp::iterate_over_libraries<void>(void (*callback)(void*, const char*),
1308 void *data)
1309 {
1310 std::set<std::string> added;
1311 string interpreter;
1312
1313 assert (this->module_name.length() != 0);
1314
1315 Dwarf_Addr bias;
1316 // We cannot use this: dwarf_getelf (dwfl_module_getdwarf (module, &bias))
1317 Elf *elf = dwfl_module_getelf (module, &bias);
1318 // elf_getphdrnum (elf, &phnum) is not available in all versions of elfutils
1319 // needs libelf from elfutils 0.144+
1320 for (int i = 0; ; i++)
1321 {
1322 GElf_Phdr mem;
1323 GElf_Phdr *phdr;
1324 phdr = gelf_getphdr (elf, i, &mem);
1325 if (phdr == NULL)
1326 break;
1327 if (phdr->p_type == PT_INTERP)
1328 {
1329 size_t maxsize;
1330 char *filedata = elf_rawfile (elf, &maxsize);
1331
1332 if (filedata != NULL && phdr->p_offset < maxsize)
1333 interpreter = (char*) (filedata + phdr->p_offset);
1334 break;
1335 }
1336 }
1337
1338 if (interpreter.length() == 0)
1339 return;
1340 // If it gets cumbersome to maintain this whitelist, we could just check for
1341 // startswith("/lib/ld") || startswith("/lib64/ld"), and trust that no admin
1342 // would install untrustworthy loaders in those paths.
1343 // See also http://sourceware.org/git/?p=glibc.git;a=blob;f=shlib-versions;hb=HEAD
1344 if (interpreter != "/lib/ld.so.1" // s390, ppc
1345 && interpreter != "/lib/ld64.so.1" // s390x, ppc64
1346 && interpreter != "/lib64/ld64.so.1"
1347 && interpreter != "/lib/ld-linux-ia64.so.2" // ia64
1348 && interpreter != "/usr/lib/ld-linux-x86-64.so.2" //
1349 && interpreter != "/emul/ia32-linux/lib/ld-linux.so.2"
1350 && interpreter != "/lib64/ld-linux-x86-64.so.2" // x8664
1351 && interpreter != "/lib/ld-linux.so.2" // x86
1352 && interpreter != "/lib/ld-linux.so.3" // arm
1353 && interpreter != "/lib/ld-linux-armhf.so.3" // arm
1354 && interpreter != "/lib/ld-linux-aarch64.so.1" // arm64
1355 && interpreter != "/lib64/ld64.so.2" // ppc64le
1356 )
1357 {
1358 sess.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s",
1359 module_name.c_str(), interpreter.c_str()));
1360 return;
1361 }
1362
1363 vector<string> ldd_command;
1364 ldd_command.push_back("/usr/bin/env");
1365 ldd_command.push_back("LD_TRACE_LOADED_OBJECTS=1");
1366 ldd_command.push_back("LD_WARN=yes");
1367 ldd_command.push_back("LD_BIND_NOW=yes");
1368 ldd_command.push_back(interpreter);
1369 ldd_command.push_back(module_name);
1370
1371 FILE *fp;
1372 int child_fd;
1373 pid_t child = stap_spawn_piped(sess.verbose, ldd_command, NULL, &child_fd);
1374 if (child <= 0 || !(fp = fdopen(child_fd, "r")))
1375 clog << _F("library iteration on %s failed: %s",
1376 module_name.c_str(), strerror(errno)) << endl;
1377 else
1378 {
1379 while (1)
1380 {
1381 char linebuf[256];
1382 char soname[256];
1383 char shlib[256];
1384 unsigned long int addr = 0;
1385
1386 char *line = fgets (linebuf, 256, fp);
1387 if (line == 0) break; // EOF or error
1388
1389 // Try soname => shlib (0xaddr)
1390 int nf = sscanf (line, "%255s => %255s (0x%lx)",
1391 soname, shlib, &addr);
1392 if (nf != 3 || shlib[0] != '/')
1393 {
1394 // Try shlib (0xaddr)
1395 nf = sscanf (line, " %255s (0x%lx)", shlib, &addr);
1396 if (nf != 2 || shlib[0] != '/')
1397 continue; // fewer than expected fields, or bad shlib.
1398 }
1399
1400 if (added.find (shlib) == added.end())
1401 {
1402 if (sess.verbose > 2)
1403 {
1404 clog << _F("Added -d '%s", shlib);
1405 if (nf == 3)
1406 clog << _F("' due to '%s'", soname);
1407 else
1408 clog << "'";
1409 clog << endl;
1410 }
1411 added.insert (shlib);
1412 }
1413 }
1414 if ((fclose(fp) || stap_waitpid(sess.verbose, child)))
1415 sess.print_warning("failed to read libraries from " + module_name + ": " + strerror(errno));
1416 }
1417
1418 for (auto it = added.begin(); it != added.end(); it++)
1419 {
1420 const string& modname = *it;
1421 (callback) (data, modname.c_str());
1422 }
1423 }
1424
1425
1426 /* For each plt section in the current module call 'callback', pass the plt entry
1427 * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */
1428
1429 template<> int
1430 dwflpp::iterate_over_plt<void>(void *object, void (*callback)(void*,
1431 const char*,
1432 size_t))
1433 {
1434 Dwarf_Addr load_addr;
1435 // Note we really want the actual elf file, not the dwarf .debug file.
1436 Elf* elf = dwfl_module_getelf (module, &load_addr);
1437 size_t shstrndx;
1438 assert (elf_getshdrstrndx (elf, &shstrndx) >= 0);
1439
1440 // Get the load address
1441 for (int i = 0; ; i++)
1442 {
1443 GElf_Phdr mem;
1444 GElf_Phdr *phdr;
1445 phdr = gelf_getphdr (elf, i, &mem);
1446 if (phdr == NULL)
1447 break;
1448 if (phdr->p_type == PT_LOAD)
1449 {
1450 load_addr = phdr->p_vaddr;
1451 break;
1452 }
1453 }
1454
1455 // Get the plt section header
1456 Elf_Scn *scn = NULL;
1457 GElf_Shdr *plt_shdr = NULL;
1458 GElf_Shdr plt_shdr_mem;
1459 while ((scn = elf_nextscn (elf, scn)))
1460 {
1461 GElf_Shdr *shdr = gelf_getshdr (scn, &plt_shdr_mem);
1462 assert (shdr != NULL);
1463 if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".plt") == 0)
1464 {
1465 plt_shdr = shdr;
1466 break;
1467 }
1468 }
1469 if (plt_shdr == NULL)
1470 return 0;
1471
1472 // Layout of the plt section
1473 int plt0_entry_size;
1474 int plt_entry_size;
1475 GElf_Ehdr ehdr_mem;
1476 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
1477 switch (em->e_machine)
1478 {
1479 case EM_386: plt0_entry_size = 16; plt_entry_size = 16; break;
1480 case EM_X86_64: plt0_entry_size = 16; plt_entry_size = 16; break;
1481 case EM_ARM: plt0_entry_size = 20; plt_entry_size = 12; break;
1482 case EM_AARCH64:plt0_entry_size = 32; plt_entry_size = 16; break;
1483 case EM_PPC64:
1484 case EM_S390:
1485 case EM_PPC:
1486 default:
1487 throw SEMANTIC_ERROR(".plt is not supported on this architecture");
1488 }
1489
1490 scn = NULL;
1491 while ((scn = elf_nextscn (elf, scn)))
1492 {
1493 GElf_Shdr shdr_mem;
1494 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1495 bool have_rela = false;
1496 bool have_rel = false;
1497
1498 if (shdr == NULL)
1499 continue;
1500 assert (shdr != NULL);
1501
1502 if ((have_rela = (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".rela.plt") == 0))
1503 || (have_rel = (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".rel.plt") == 0)))
1504 {
1505 /* Get the data of the section. */
1506 Elf_Data *data = elf_getdata (scn, NULL);
1507 assert (data != NULL);
1508 /* Get the symbol table information. */
1509 Elf_Scn *symscn = elf_getscn (elf, shdr->sh_link);
1510 GElf_Shdr symshdr_mem;
1511 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1512 assert (symshdr != NULL);
1513 Elf_Data *symdata = elf_getdata (symscn, NULL);
1514 assert (symdata != NULL);
1515
1516 unsigned int nsyms = shdr->sh_size / shdr->sh_entsize;
1517
1518 for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
1519 {
1520 GElf_Ehdr ehdr_mem;
1521 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
1522 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
1523
1524 GElf_Rela relamem;
1525 GElf_Rela *rela = NULL;
1526 GElf_Rel relmem;
1527 GElf_Rel *rel = NULL;
1528 if (have_rela)
1529 {
1530 rela = gelf_getrela (data, cnt, &relamem);
1531 assert (rela != NULL);
1532 }
1533 else if (have_rel)
1534 {
1535 rel = gelf_getrel (data, cnt, &relmem);
1536 assert (rel != NULL);
1537 }
1538 GElf_Sym symmem;
1539 Elf32_Word xndx;
1540 Elf_Data *xndxdata = NULL;
1541 GElf_Sym *sym =
1542 gelf_getsymshndx (symdata, xndxdata,
1543 GELF_R_SYM (have_rela ? rela->r_info : rel->r_info),
1544 &symmem, &xndx);
1545 assert (sym != NULL);
1546 Dwarf_Addr addr = plt_shdr->sh_offset + plt0_entry_size + cnt * plt_entry_size;
1547
1548 if (elf_strptr (elf, symshdr->sh_link, sym->st_name))
1549 (*callback) (object, elf_strptr (elf, symshdr->sh_link, sym->st_name), addr + load_addr);
1550 }
1551 break; // while scn
1552 }
1553 }
1554 return 0;
1555 }
1556
1557
1558 // Comparator function for sorting
1559 static bool
1560 compare_lines(Dwarf_Line* a, Dwarf_Line* b)
1561 {
1562 if (a == b)
1563 return false;
1564
1565 int lineno_a = DWARF_LINENO(a);
1566 int lineno_b = DWARF_LINENO(b);
1567 if (lineno_a == lineno_b)
1568 return DWARF_LINEADDR(a) < DWARF_LINEADDR(b);
1569 return lineno_a < lineno_b;
1570 }
1571
1572 // Comparator object for searching Dwarf_Lines with a specific lineno when we
1573 // don't have a Dwarf_Line of our own to search for (hence why a or b is always
1574 // NULL).
1575 struct lineno_comparator {
1576 int lineno;
1577 lineno_comparator(int lineno): lineno(lineno) {}
1578 bool operator() (Dwarf_Line* a, Dwarf_Line* b)
1579 {
1580 assert(a || b);
1581 if (a)
1582 return DWARF_LINENO(a) < lineno;
1583 else
1584 return lineno < DWARF_LINENO(b);
1585 }
1586 };
1587
1588 // Returns a range of lines in between begin and end with wanted lineno. If
1589 // none exist, points to where it would have been.
1590 static lines_range_t
1591 lineno_equal_range(lines_t* v, int lineno)
1592 {
1593 lineno_comparator lc(lineno);
1594 return equal_range(v->begin(), v->end(), (Dwarf_Line*)NULL, lc);
1595 }
1596
1597 // Interface to CU lines cache sorted by lineno
1598 lines_t*
1599 dwflpp::get_cu_lines_sorted_by_lineno(const char *srcfile)
1600 {
1601 assert(cu);
1602
1603 srcfile_lines_cache_t *srcfile_lines = cu_lines_cache[cu];
1604 if (!srcfile_lines)
1605 {
1606 srcfile_lines = new srcfile_lines_cache_t();
1607 cu_lines_cache[cu] = srcfile_lines;
1608 }
1609
1610 lines_t *lines = (*srcfile_lines)[srcfile];
1611 if (!lines)
1612 {
1613 size_t nlines_cu = 0;
1614 Dwarf_Lines *lines_cu = NULL;
1615 DWARF_ASSERT("dwarf_getsrclines",
1616 dwarf_getsrclines(cu, &lines_cu, &nlines_cu));
1617
1618 lines = new lines_t();
1619 (*srcfile_lines)[srcfile] = lines;
1620
1621 for (size_t i = 0; i < nlines_cu; i++)
1622 {
1623 Dwarf_Line *line = dwarf_onesrcline(lines_cu, i);
1624 const char *linesrc = DWARF_LINESRC(line);
1625 if (strcmp(srcfile, linesrc))
1626 continue;
1627 lines->push_back(line);
1628 }
1629
1630 if (lines->size() > 1)
1631 sort(lines->begin(), lines->end(), compare_lines);
1632
1633 if (sess.verbose > 3)
1634 {
1635 clog << _F("found the following lines for %s:", srcfile) << endl;
1636 for (auto i = lines->begin(); i != lines->end(); ++i)
1637 cout << DWARF_LINENO(*i) << " = " << hex
1638 << DWARF_LINEADDR(*i) << dec << endl;
1639 }
1640 }
1641 return lines;
1642 }
1643
1644 static Dwarf_Line*
1645 get_func_first_line(Dwarf_Die *cu, base_func_info& func)
1646 {
1647 // dwarf_getsrc_die() uses binary search to find the Dwarf_Line, but will
1648 // return the wrong line if not found.
1649 Dwarf_Line *line = dwarf_getsrc_die(cu, func.entrypc);
1650 if (line && DWARF_LINEADDR(line) == func.entrypc)
1651 return line;
1652
1653 // Line not found (or line at wrong addr). We have to resort to a slower
1654 // linear method. We won't find an exact match (probably this is an inlined
1655 // instance), so just settle on the first Dwarf_Line with lowest addr which
1656 // falls in the die.
1657 size_t nlines = 0;
1658 Dwarf_Lines *lines = NULL;
1659 DWARF_ASSERT("dwarf_getsrclines",
1660 dwarf_getsrclines(cu, &lines, &nlines));
1661
1662 for (size_t i = 0; i < nlines; i++)
1663 {
1664 line = dwarf_onesrcline(lines, i);
1665 if (dwarf_haspc(&func.die, DWARF_LINEADDR(line)))
1666 return line;
1667 }
1668 return NULL;
1669 }
1670
1671 static lines_t
1672 collect_lines_in_die(lines_range_t range, Dwarf_Die *die)
1673 {
1674 lines_t lines_in_die;
1675 for (auto line = range.first; line != range.second; ++line)
1676 if (dwarf_haspc(die, DWARF_LINEADDR(*line)))
1677 lines_in_die.push_back(*line);
1678 return lines_in_die;
1679 }
1680
1681 static void
1682 add_matching_lines_in_func(Dwarf_Die *cu,
1683 lines_t *cu_lines,
1684 base_func_info& func,
1685 lines_t& matching_lines)
1686 {
1687 Dwarf_Line *start_line = get_func_first_line(cu, func);
1688 if (!start_line)
1689 return;
1690
1691 for (int lineno = DWARF_LINENO(start_line);;)
1692 {
1693 lines_range_t range = lineno_equal_range(cu_lines, lineno);
1694
1695 // We consider the lineno still part of the die if at least one of them
1696 // falls in the die.
1697 lines_t lines_in_die = collect_lines_in_die(range, &func.die);
1698 if (lines_in_die.empty())
1699 break;
1700
1701 // Just pick the first LR even if there are more than one. Since the lines
1702 // are sorted by lineno and then addr, the first one is the one with the
1703 // lowest addr.
1704 matching_lines.push_back(lines_in_die[0]);
1705
1706 // break out if there are no more lines, otherwise, go to the next lineno
1707 if (range.second == cu_lines->end())
1708 break;
1709 lineno = DWARF_LINENO(*range.second);
1710 }
1711 }
1712
1713 void
1714 dwflpp::collect_all_lines(char const * srcfile,
1715 base_func_info_map_t& funcs,
1716 lines_t& matching_lines)
1717 {
1718 // This is where we handle WILDCARD lineno types.
1719 lines_t *cu_lines = get_cu_lines_sorted_by_lineno(srcfile);
1720 for (auto func = funcs.begin(); func != funcs.end(); ++func)
1721 add_matching_lines_in_func(cu, cu_lines, *func, matching_lines);
1722 }
1723
1724
1725 static void
1726 add_matching_line_in_die(lines_t *cu_lines,
1727 lines_t& matching_lines,
1728 Dwarf_Die *die, int lineno)
1729 {
1730 lines_range_t lineno_range = lineno_equal_range(cu_lines, lineno);
1731 lines_t lines_in_die = collect_lines_in_die(lineno_range, die);
1732 if (lines_in_die.empty())
1733 return;
1734
1735 // Even if there are more than 1 LRs, just pick the first one. Since the lines
1736 // are sorted by lineno and then addr, the first one is the one with the
1737 // lowest addr. This is similar to what GDB does.
1738 matching_lines.push_back(lines_in_die[0]);
1739 }
1740
1741 void
1742 dwflpp::collect_lines_for_single_lineno(char const * srcfile,
1743 int lineno,
1744 bool is_relative,
1745 base_func_info_map_t& funcs,
1746 lines_t& matching_lines)
1747 {
1748 /* Here, we handle ABSOLUTE and RELATIVE lineno types. Relative line numbers
1749 * are a bit special. The issue is that functions (esp. inlined ones) may not
1750 * even have a LR corresponding to the first valid line of code. So, applying
1751 * an offset to the 'first' LR found in the DIE can be quite imprecise.
1752 * Instead, we use decl_line, which although does not necessarily have a
1753 * LR associated with it (it can sometimes still happen esp. if the code is
1754 * written in OTB-style), it serves as an anchor on which we can apply the
1755 * offset to yield a lineno that will not change with compiler optimization.
1756 * It also has the added benefit of being consistent with the lineno
1757 * printed by e.g. stap -l kernel.function("vfs_read"), so users can be
1758 * confident from what lineno we adjust.
1759 */
1760 lines_t *cu_lines = get_cu_lines_sorted_by_lineno(srcfile);
1761 for (auto func = funcs.begin(); func != funcs.end(); ++func)
1762 add_matching_line_in_die(cu_lines, matching_lines, &func->die,
1763 is_relative ? lineno + func->decl_line
1764 : lineno);
1765 }
1766
1767 static bool
1768 functions_have_lineno(base_func_info_map_t& funcs,
1769 lines_t *lines, int lineno)
1770 {
1771 lines_range_t lineno_range = lineno_equal_range(lines, lineno);
1772 if (lineno_range.first == lineno_range.second)
1773 return false; // no LRs at this lineno
1774
1775 for (auto func = funcs.begin(); func != funcs.end(); ++func)
1776 if (!collect_lines_in_die(lineno_range, &func->die).empty())
1777 return true;
1778
1779 return false;
1780 }
1781
1782 // returns pair of valid linenos surrounding target lineno
1783 pair<int,int>
1784 dwflpp::get_nearest_linenos(char const * srcfile,
1785 int lineno,
1786 base_func_info_map_t& funcs)
1787 {
1788 assert(cu);
1789 lines_t *cu_lines = get_cu_lines_sorted_by_lineno(srcfile);
1790
1791 // Look around lineno for linenos with LRs.
1792 pair<int,int> nearest_linenos = make_pair(-1, -1);
1793 for (size_t i = 1; i < 6; ++i)
1794 {
1795 if (nearest_linenos.first == -1 && functions_have_lineno(funcs, cu_lines, lineno-i))
1796 nearest_linenos.first = lineno - i;
1797 if (nearest_linenos.second == -1 && functions_have_lineno(funcs, cu_lines, lineno+i))
1798 nearest_linenos.second = lineno + i;
1799 }
1800
1801 return nearest_linenos;
1802 }
1803
1804 // returns nearest valid lineno to target lineno
1805 int
1806 dwflpp::get_nearest_lineno(char const * srcfile,
1807 int lineno,
1808 base_func_info_map_t& funcs)
1809 {
1810 assert(cu);
1811 pair<int,int> nearest_linenos = get_nearest_linenos(srcfile, lineno, funcs);
1812
1813 if (nearest_linenos.first > 0
1814 && nearest_linenos.second > 0)
1815 {
1816 // pick the nearest line number (break tie to upper)
1817 if (lineno - nearest_linenos.first < nearest_linenos.second - lineno)
1818 return nearest_linenos.first;
1819 else
1820 return nearest_linenos.second;
1821 }
1822 else if (nearest_linenos.first > 0)
1823 return nearest_linenos.first;
1824 else if (nearest_linenos.second > 0)
1825 return nearest_linenos.second;
1826 else
1827 return -1;
1828 }
1829
1830 void
1831 dwflpp::suggest_alternative_linenos(char const * srcfile,
1832 int lineno,
1833 base_func_info_map_t& funcs)
1834 {
1835 assert(cu);
1836 pair<int,int> nearest_linenos = get_nearest_linenos(srcfile, lineno, funcs);
1837
1838 stringstream advice;
1839 advice << _F("no line records for %s:%d [man error::dwarf]", srcfile, lineno);
1840
1841 if (nearest_linenos.first > 0 || nearest_linenos.second > 0)
1842 {
1843 //TRANSLATORS: Here we are trying to advise what source file
1844 //TRANSLATORS: to attempt.
1845 advice << _(" (try ");
1846 if (nearest_linenos.first > 0)
1847 advice << ":" << nearest_linenos.first;
1848 if (nearest_linenos.first > 0 && nearest_linenos.second > 0)
1849 advice << _(" or ");
1850 if (nearest_linenos.second > 0)
1851 advice << ":" << nearest_linenos.second;
1852 advice << ")";
1853 }
1854 throw SEMANTIC_ERROR (advice.str());
1855 }
1856
1857 static base_func_info_map_t
1858 get_funcs_in_srcfile(base_func_info_map_t& funcs,
1859 const char * srcfile)
1860 {
1861 base_func_info_map_t matching_funcs;
1862 for (auto func = funcs.begin(); func != funcs.end(); ++func)
1863 if (func->decl_file == string(srcfile))
1864 matching_funcs.push_back(*func);
1865 return matching_funcs;
1866 }
1867
1868 template<> void
1869 dwflpp::iterate_over_srcfile_lines<void>(char const * srcfile,
1870 const vector<int>& linenos,
1871 enum lineno_t lineno_type,
1872 base_func_info_map_t& funcs,
1873 void (* callback) (Dwarf_Addr,
1874 int, void*),
1875 bool has_nearest,
1876 void *data)
1877 {
1878 /* Matching line records (LRs) to user-provided linenos is trickier than it
1879 * seems. The fate of all functions is one of three possibilities:
1880 * 1. it's a normal function, with a subprogram DIE and a bona fide lowpc
1881 * and highpc attribute.
1882 * 2. it's an inlined function (one/multiple inlined_subroutine DIE, with one
1883 * abstract_origin DIE)
1884 * 3. it's both a normal function and an inlined function. For example, if
1885 * the funtion has been inlined only in some places, we'll have a DIE for
1886 * the normal subprogram DIE as well as inlined_subroutine DIEs.
1887 *
1888 * Multiple LRs for the same lineno but different addresses can simply happen
1889 * due to the function appearing in multiple forms. E.g. a function inlined
1890 * in two spots can yield two sets of LRs for its linenos at the different
1891 * addresses where it is inlined.
1892 * This is why the collect_* functions used here try to match up LRs back
1893 * to their originating DIEs. For example, in the function
1894 * collect_lines_for_single_lineno(), we filter first by DIE so that a lineno
1895 * corresponding to multiple addrs in multiple inlined_subroutine DIEs yields
1896 * a probe for each of them.
1897 */
1898 assert(cu);
1899
1900 // only work on the functions found in the current srcfile
1901 base_func_info_map_t current_funcs = get_funcs_in_srcfile(funcs, srcfile);
1902 if (current_funcs.empty())
1903 return;
1904
1905 // collect lines
1906 lines_t matching_lines;
1907 if (lineno_type == ABSOLUTE)
1908 collect_lines_for_single_lineno(srcfile, linenos[0], false, /* is_relative */
1909 current_funcs, matching_lines);
1910 else if (lineno_type == RELATIVE)
1911 collect_lines_for_single_lineno(srcfile, linenos[0], true, /* is_relative */
1912 current_funcs, matching_lines);
1913 else if (lineno_type == WILDCARD)
1914 collect_all_lines(srcfile, current_funcs, matching_lines);
1915 else if (lineno_type == ENUMERATED)
1916 {
1917 set<int> collected_linenos;
1918 for (auto it = linenos.begin(); it != linenos.end(); it++)
1919 {
1920 // have we already collected this lineno?
1921 if (collected_linenos.find(*it) != collected_linenos.end())
1922 continue;
1923
1924 // remember end iterator so we can tell if things were found later
1925 auto itend = matching_lines.end();
1926
1927 collect_lines_for_single_lineno(srcfile, *it, false, /* is_relative */
1928 current_funcs, matching_lines);
1929 // add to set if we found LRs
1930 if (itend != matching_lines.end())
1931 collected_linenos.insert(*it);
1932
1933 // if we didn't find anything and .nearest is given, then try nearest
1934 if (itend == matching_lines.end() && has_nearest)
1935 {
1936 int nearest_lineno = get_nearest_lineno(srcfile, *it,
1937 current_funcs);
1938 if (nearest_lineno <= 0) // no valid nearest linenos
1939 continue;
1940
1941 bool new_lineno = collected_linenos.insert(nearest_lineno).second;
1942 if (new_lineno)
1943 collect_lines_for_single_lineno(srcfile, nearest_lineno,
1944 false, /* is_relative */
1945 current_funcs, matching_lines);
1946 }
1947 }
1948 }
1949
1950 // should we try to collect the nearest lines if we didn't collect everything
1951 // on first try? (ABSOLUTE and RELATIVE only: ENUMERATED handles it already
1952 // and WILDCARD doesn't need it)
1953 if (matching_lines.empty() && has_nearest && (lineno_type == ABSOLUTE ||
1954 lineno_type == RELATIVE))
1955 {
1956 int lineno = linenos[0];
1957 if (lineno_type == RELATIVE)
1958 // just pick the first function and make it relative to that
1959 lineno += current_funcs[0].decl_line;
1960
1961 int nearest_lineno = get_nearest_lineno(srcfile, lineno, current_funcs);
1962 if (nearest_lineno > 0)
1963 collect_lines_for_single_lineno(srcfile, nearest_lineno,
1964 false, /* is_relative */
1965 current_funcs, matching_lines);
1966 }
1967
1968 // call back with matching lines
1969 if (!matching_lines.empty())
1970 {
1971 set<Dwarf_Addr> probed_addrs;
1972 for (auto line = matching_lines.begin();
1973 line != matching_lines.end(); ++line)
1974 {
1975 int lineno = DWARF_LINENO(*line);
1976 Dwarf_Addr addr = DWARF_LINEADDR(*line);
1977 bool is_new_addr = probed_addrs.insert(addr).second;
1978 if (is_new_addr)
1979 callback(addr, lineno, data);
1980 }
1981 }
1982 // No LRs found at the wanted lineno. So let's suggest other ones if user was
1983 // targeting a specific lineno (ABSOLUTE or RELATIVE).
1984 else if (lineno_type == ABSOLUTE ||
1985 lineno_type == RELATIVE)
1986 {
1987 int lineno = linenos[0];
1988 if (lineno_type == RELATIVE)
1989 // just pick the first function and make it relative to that
1990 lineno += current_funcs[0].decl_line;
1991
1992 suggest_alternative_linenos(srcfile, lineno, current_funcs);
1993 }
1994 else if (lineno_type == WILDCARD)
1995 {
1996 // PR23760: It's not an error to come across a srcfile that has
1997 // no line records, if we're just happening across it as a
1998 // wildcard case. Only best effort matches are expected here.
1999 //
2000 // throw SEMANTIC_ERROR(_F("no line records for %s [man error::dwarf]", srcfile));
2001 }
2002 }
2003
2004
2005 template<> void
2006 dwflpp::iterate_over_labels<void>(Dwarf_Die *begin_die,
2007 const string& sym,
2008 const base_func_info& function,
2009 const vector<int>& linenos,
2010 enum lineno_t lineno_type,
2011 void *data,
2012 void (* callback)(const base_func_info&,
2013 const char*,
2014 const char*,
2015 int,
2016 Dwarf_Die*,
2017 Dwarf_Addr,
2018 void*))
2019 {
2020 get_module_dwarf();
2021
2022 Dwarf_Die die, import;
2023 const char *name;
2024 int res = dwarf_child (begin_die, &die);
2025 if (res != 0)
2026 return; // die without children, bail out.
2027
2028 do
2029 {
2030 switch (dwarf_tag(&die))
2031 {
2032 case DW_TAG_label:
2033 name = dwarf_diename (&die);
2034 if (name &&
2035 (name == sym
2036 || (name_has_wildcard(sym)
2037 && function_name_matches_pattern (name, sym))))
2038 {
2039 // Don't try to be smart. Just drop no addr labels.
2040 Dwarf_Addr stmt_addr;
2041 if (dwarf_lowpc (&die, &stmt_addr) == 0)
2042 {
2043 // Get the file/line number for this label
2044 int dline;
2045 const char *file = dwarf_decl_file (&die) ?: "<unknown source>";
2046 dwarf_decl_line (&die, &dline);
2047
2048 vector<Dwarf_Die> scopes = getscopes_die(&die);
2049 if (scopes.size() > 1)
2050 {
2051 Dwarf_Die scope;
2052 if (!inner_die_containing_pc(scopes[1], stmt_addr, scope))
2053 {
2054 sess.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not "
2055 "contained by its scope '%s' (dieoffset: %s) -- bad"
2056 " debuginfo? [man error::dwarf]", name, lex_cast_hex(stmt_addr).c_str(),
2057 lex_cast_hex(dwarf_dieoffset(&die)).c_str(),
2058 (dwarf_diename(&scope) ?: "<unknown>"),
2059 lex_cast_hex(dwarf_dieoffset(&scope)).c_str()));
2060 }
2061
2062 bool matches_lineno;
2063 if (lineno_type == ABSOLUTE)
2064 matches_lineno = dline == linenos[0];
2065 else if (lineno_type == RELATIVE)
2066 matches_lineno = dline == linenos[0] + function.decl_line;
2067 else if (lineno_type == ENUMERATED)
2068 matches_lineno = (binary_search(linenos.begin(), linenos.end(), dline));
2069 else // WILDCARD
2070 matches_lineno = true;
2071
2072 if (matches_lineno)
2073 callback(function, name, file, dline,
2074 &scope, stmt_addr, data);
2075 }
2076 }
2077 }
2078 break;
2079
2080 case DW_TAG_subprogram:
2081 case DW_TAG_inlined_subroutine:
2082 // Stay within our filtered function
2083 break;
2084
2085 case DW_TAG_imported_unit:
2086 // Iterate over the children of the imported unit as if they
2087 // were inserted in place.
2088 if (dwarf_attr_die(&die, DW_AT_import, &import))
2089 iterate_over_labels (&import, sym, function, linenos,
2090 lineno_type, data, callback);
2091 break;
2092
2093 default:
2094 if (dwarf_haschildren (&die))
2095 iterate_over_labels (&die, sym, function, linenos,
2096 lineno_type, data, callback);
2097 break;
2098 }
2099 }
2100 while (dwarf_siblingof (&die, &die) == 0);
2101 }
2102
2103 // Mini 'query-like' struct to help us navigate callbacks during
2104 // external function resolution
2105 struct external_function_query {
2106 dwflpp* dw;
2107 const string name;
2108 Dwarf_Die die;
2109 Dwarf_Addr addr;
2110 bool resolved;
2111 external_function_query(dwflpp* dw, const string& name):
2112 dw(dw), name(name), die(), addr(0), resolved(false) {}
2113 };
2114
2115 int
2116 dwflpp::external_function_cu_callback (Dwarf_Die* cu, external_function_query *efq)
2117 {
2118 efq->dw->focus_on_cu(cu);
2119 return efq->dw->iterate_over_functions(external_function_func_callback,
2120 efq, efq->name);
2121 }
2122
2123 int
2124 dwflpp::external_function_func_callback (Dwarf_Die* func, external_function_query *efq)
2125 {
2126 Dwarf_Attribute external;
2127 Dwarf_Addr func_addr;
2128 if (dwarf_attr_integrate(func, DW_AT_external, &external) != NULL &&
2129 dwarf_lowpc(func, &func_addr) == 0)
2130 {
2131 efq->die = *func;
2132 efq->addr = func_addr;
2133 efq->resolved = true;
2134 return DWARF_CB_ABORT; // we found it so stop here
2135 }
2136 return DWARF_CB_OK;
2137 }
2138
2139 template<> void
2140 dwflpp::iterate_over_callees<void>(Dwarf_Die *begin_die,
2141 const string& sym,
2142 int64_t recursion_depth,
2143 void *data,
2144 void (* callback)(base_func_info&,
2145 base_func_info&,
2146 stack<Dwarf_Addr>*,
2147 void*),
2148 base_func_info& caller,
2149 stack<Dwarf_Addr> *callers)
2150 {
2151 get_module_dwarf();
2152
2153 Dwarf_Die die, import;
2154
2155 // DIE of abstract_origin found in die
2156 Dwarf_Die origin;
2157
2158 // callee's entry pc (= where we'll probe)
2159 Dwarf_Addr func_addr;
2160
2161 // caller's unwind pc during call (to match against bt for filtering)
2162 Dwarf_Addr caller_uw_addr;
2163
2164 Dwarf_Attribute attr;
2165
2166 base_func_info callee;
2167 if (dwarf_child(begin_die, &die) != 0)
2168 return; // die without children, bail out.
2169
2170 bool free_callers = false;
2171 if (callers == NULL) /* first call */
2172 {
2173 callers = new stack<Dwarf_Addr>();
2174 free_callers = true;
2175 }
2176
2177 do
2178 {
2179 bool inlined = false;
2180 switch (dwarf_tag(&die))
2181 {
2182 case DW_TAG_inlined_subroutine:
2183 inlined = true;
2184 /* FALLTHROUGH */ /* thanks mjw */
2185 case DW_TAG_GNU_call_site:
2186 callee.name = dwarf_diename(&die) ?: "";
2187 if (callee.name.empty())
2188 continue;
2189 if (callee.name != sym)
2190 {
2191 if (!name_has_wildcard(sym))
2192 continue;
2193 if (!function_name_matches_pattern(callee.name, sym))
2194 continue;
2195 }
2196
2197 /* In both cases (call sites and inlines), we want the
2198 * abstract_origin. The difference is that in inlines, the addr is
2199 * in the die itself, whereas for call sites, the addr is in the
2200 * abstract_origin's die.
2201 * Note that in the case of inlines, we're only calling back
2202 * for that inline instance, not all. This is what we want, since
2203 * it will only be triggered when 'called' from the target func,
2204 * which is something we have to emulate for non-inlined funcs
2205 * (which is the purpose of the caller_uw_addr below) */
2206 if (dwarf_attr_die(&die, DW_AT_abstract_origin, &origin) == NULL)
2207 continue;
2208
2209 // the low_pc of the die in either cases is the pc that would
2210 // show up in a backtrace (inlines are a special case in which
2211 // the die's low_pc is also the abstract_origin's low_pc = the
2212 // 'start' of the inline instance)
2213 if (dwarf_lowpc(&die, &caller_uw_addr) != 0)
2214 continue;
2215
2216 if (inlined)
2217 func_addr = caller_uw_addr;
2218 else if (dwarf_lowpc(&origin, &func_addr) != 0)
2219 {
2220 // function doesn't have a low_pc, is it external?
2221 if (dwarf_attr_integrate(&origin, DW_AT_external,
2222 &attr) != NULL)
2223 {
2224 // let's iterate over the CUs and find it. NB: it's
2225 // possible we could have also done this by creating a
2226 // probe point with .exported tacked on and rerunning it
2227 // through derive_probe(). But since we're already on the
2228 // dwflpp side of things, and we already have access to
2229 // everything we need, let's try to be self-sufficient.
2230
2231 // remember old focus
2232 Dwarf_Die *old_cu = cu;
2233
2234 external_function_query efq(this, dwarf_linkage_name(&origin) ?: callee.name);
2235 iterate_over_cus(external_function_cu_callback, &efq, false);
2236
2237 // restore focus
2238 cu = old_cu;
2239
2240 if (!efq.resolved) // did we resolve it?
2241 continue;
2242
2243 func_addr = efq.addr;
2244 origin = efq.die;
2245 }
2246 // non-external function without low_pc, jump ship
2247 else continue;
2248 }
2249
2250 // We now have the addr to probe in func_addr, and the DIE
2251 // from which to obtain file/line info in origin
2252
2253 // Get the file/line number for this callee
2254 callee.decl_file = dwarf_decl_file (&origin) ?: "<unknown source>";
2255 dwarf_decl_line (&origin, &callee.decl_line);
2256
2257 // add as a caller to match against
2258 if (!inlined)
2259 callers->push(caller_uw_addr);
2260
2261 callee.die = inlined ? die : origin;
2262 callee.entrypc = func_addr;
2263 callback(callee, caller, callers, data);
2264
2265 // If it's a tail call, print a warning that it may not be caught
2266 if (!inlined
2267 && dwarf_attr_integrate(&die, DW_AT_GNU_tail_call, &attr) != NULL)
2268 sess.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: "
2269 ".callee probe may not fire. Try placing the probe "
2270 "directly on the callee function instead.",
2271 callee.name.to_string().c_str(),
2272 caller.name.to_string().c_str()));
2273
2274 // For .callees(N) probes, we recurse on this callee. Note that we
2275 // pass the callee we just found as the caller arg for this recursion,
2276 // since it (the callee we just found) will be the caller of whatever
2277 // callees found inside this recursion.
2278 if (recursion_depth > 1)
2279 iterate_over_callees(inlined ? &die : &origin,
2280 sym, recursion_depth-1, data,
2281 callback, callee, callers);
2282
2283 if (!inlined)
2284 callers->pop();
2285 break;
2286
2287 case DW_TAG_subprogram:
2288 break; // don't leave our filtered func
2289
2290 case DW_TAG_imported_unit:
2291 // Iterate over the children of the imported unit as if they
2292 // were inserted in place.
2293 if (dwarf_attr_die(&die, DW_AT_import, &import))
2294 // NB: we pass the same caller arg into it
2295 iterate_over_callees (&import, sym, recursion_depth, data,
2296 callback, caller, callers);
2297 break;
2298
2299 default:
2300 if (dwarf_haschildren (&die))
2301 // NB: we pass the same caller arg into it
2302 iterate_over_callees (&die, sym, recursion_depth, data,
2303 callback, caller, callers);
2304 break;
2305 }
2306 }
2307 while (dwarf_siblingof (&die, &die) == 0);
2308
2309 if (free_callers && callers != NULL)
2310 delete callers;
2311 }
2312
2313
2314 void
2315 dwflpp::collect_srcfiles_matching (string const & pattern,
2316 set<string> & filtered_srcfiles)
2317 {
2318 assert (module);
2319 assert (cu);
2320
2321 size_t nfiles;
2322 Dwarf_Files *srcfiles;
2323
2324 // PR 5049: implicit * in front of given path pattern.
2325 // NB: fnmatch() is used without FNM_PATHNAME.
2326 string prefixed_pattern = string("*/") + pattern;
2327
2328 DWARF_ASSERT ("dwarf_getsrcfiles",
2329 dwarf_getsrcfiles (cu, &srcfiles, &nfiles));
2330 {
2331 for (size_t i = 0; i < nfiles; ++i)
2332 {
2333 char const * fname = dwarf_filesrc (srcfiles, i, NULL, NULL);
2334 if (fnmatch (pattern.c_str(), fname, 0) == 0 ||
2335 fnmatch (prefixed_pattern.c_str(), fname, 0) == 0)
2336 {
2337 filtered_srcfiles.insert (fname);
2338 if (sess.verbose>2)
2339 clog << _F("selected source file '%s'\n", fname);
2340 }
2341 }
2342 }
2343 }
2344
2345
2346 void
2347 dwflpp::resolve_prologue_endings (func_info_map_t & funcs)
2348 {
2349 // When a program is compiled with no optimization, GCC does no variable
2350 // tracking, which means that location info is actually only really valid
2351 // after the prologue, even though GCC reports it as valid during. So we need
2352 // to find the prologue ends to get accurate info. This may or may not be the
2353 // first address that has a source line distinct from the function
2354 // declaration's.
2355
2356 assert(module);
2357 assert(cu);
2358
2359 size_t nlines = 0;
2360 Dwarf_Lines *lines = NULL;
2361
2362 /* trouble cases:
2363 malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue
2364 sys_get?id in kernel/timer.c no-prologue
2365 sys_exit_group tail-recursive
2366 {do_,}sys_open extra-long-prologue (gcc 3.4)
2367 cpu_to_logical_apicid NULL-decl_file
2368 */
2369
2370 // Fetch all srcline records, sorted by address. No need to free lines, it's a
2371 // direct pointer to the CU's cached lines.
2372 if (dwarf_getsrclines(cu, &lines, &nlines) != 0
2373 || lines == NULL || nlines == 0)
2374 {
2375 if (sess.verbose > 2)
2376 clog << _F("aborting prologue search: no source lines found for cu '%s'\n",
2377 cu_name().c_str());
2378 return;
2379 }
2380
2381 // Dump them into our own array for easier searching. They should already be
2382 // sorted by addr, but we doublecheck that here. We want to keep the indices
2383 // between lines and addrs the same.
2384 vector<Dwarf_Addr> addrs;
2385 for (size_t i = 0; i < nlines; i++)
2386 {
2387 Dwarf_Line* line = dwarf_onesrcline(lines, i);
2388 Dwarf_Addr addr = DWARF_LINEADDR(line);
2389 if (!addrs.empty() && addr < addrs.back())
2390 throw SEMANTIC_ERROR(_("lines from dwarf_getsrclines() not sorted"));
2391 addrs.push_back(addr);
2392 }
2393 // We normally ignore a function's decl_line, since it is associated with the
2394 // line at which the identifier appears in the declaration, and has no
2395 // meaningful relation to the lineno associated with the entrypc (which is
2396 // normally the lineno of '{', which could occur at the same line as the
2397 // declaration, or lower down).
2398 // However, if the CU was compiled using GCC < 4.4, then the decl_line
2399 // actually represents the lineno of '{' as well, in which case if the lineno
2400 // associated with the entrypc is != to the decl_line, it means the compiler
2401 // scraped/optimized off some of the beginning of the function and the safest
2402 // thing we can do is consider it naked.
2403 bool consider_decl_line = false;
2404 {
2405 string prod, vers;
2406 if (is_gcc_producer(cu, prod, vers)
2407 && strverscmp(vers.c_str(), "4.4.0") < 0)
2408 consider_decl_line = true;
2409 }
2410
2411 for(auto it = funcs.begin(); it != funcs.end(); it++)
2412 {
2413 Dwarf_Addr entrypc = it->entrypc;
2414
2415 unsigned entrypc_srcline_idx = 0;
2416 Dwarf_Line *entrypc_srcline = NULL;
2417 {
2418 auto it_addr = lower_bound(addrs.cbegin(), addrs.cend(), entrypc);
2419 if (it_addr != addrs.cend() && *it_addr == entrypc)
2420 {
2421 entrypc_srcline_idx = it_addr - addrs.cbegin();
2422 entrypc_srcline = dwarf_onesrcline(lines, entrypc_srcline_idx);
2423 }
2424 }
2425
2426 if (!entrypc_srcline)
2427 {
2428 if (sess.verbose > 2)
2429 clog << _F("missing entrypc dwarf line record for function '%s'\n",
2430 it->name.to_string().c_str());
2431 // This is probably an inlined function. We'll end up using
2432 // its lowpc as a probe address.
2433 continue;
2434 }
2435
2436 if (entrypc == 0)
2437 {
2438 if (sess.verbose > 2)
2439 clog << _F("null entrypc dwarf line record for function '%s'\n",
2440 it->name.to_string().c_str());
2441 // This is probably an inlined function. We'll skip this instance;
2442 // it is messed up.
2443 continue;
2444 }
2445
2446 if (sess.verbose>2)
2447 clog << _F("searching for prologue of function '%s' %#" PRIx64
2448 "@%s:%d\n", it->name.to_string().c_str(), entrypc,
2449 it->decl_file.to_string().c_str(), it->decl_line);
2450
2451 // For each function, we look for the prologue-end marker (e.g. clang
2452 // outputs one). If there is no explicit marker (e.g. GCC does not), we
2453 // accept a bigger or equal lineno as a prologue end (this catches GCC's
2454 // 0-line advances).
2455
2456 // We may have to skip a few because some old compilers plop
2457 // in dummy line records for longer prologues. If we go too
2458 // far (outside function), we take the previous one. Or, it may
2459 // be the first one, if the function had no prologue, and thus
2460 // the entrypc maps to a statement in the body rather than the
2461 // declaration.
2462
2463 int entrypc_srcline_lineno = DWARF_LINENO(entrypc_srcline);
2464 unsigned postprologue_srcline_idx = entrypc_srcline_idx;
2465 Dwarf_Line *postprologue_srcline = entrypc_srcline;
2466
2467 while (postprologue_srcline_idx < nlines)
2468 {
2469 postprologue_srcline = dwarf_onesrcline(lines,
2470 postprologue_srcline_idx);
2471 Dwarf_Addr lineaddr = DWARF_LINEADDR(postprologue_srcline);
2472 const char* linesrc = DWARF_LINESRC(postprologue_srcline);
2473 int lineno = DWARF_LINENO(postprologue_srcline);
2474 bool lineprologue_end = DWARF_LINEPROLOGUEEND(postprologue_srcline);
2475
2476 if (sess.verbose>2)
2477 clog << _F("checking line record %#" PRIx64 "@%s:%d%s\n", lineaddr,
2478 linesrc, lineno, lineprologue_end ? " (marked)" : "");
2479
2480 // have we passed the function?
2481 if (dwarf_haspc (& it->die, lineaddr) != 1)
2482 break;
2483 // is there an explicit prologue_end marker?
2484 if (lineprologue_end)
2485 break;
2486 // is it a different file?
2487 if (it->decl_file != string(linesrc))
2488 break;
2489 // OK, it's the same file, but is it a different line?
2490 if (lineno != entrypc_srcline_lineno)
2491 break;
2492 // Same file and line, is this a second line record (e.g. 0-line advance)?
2493 if (postprologue_srcline_idx != entrypc_srcline_idx)
2494 break;
2495 // This is the first iteration. Is decl_line meaningful and is the
2496 // lineno past the decl_line?
2497 if (consider_decl_line && lineno != it->decl_line)
2498 break;
2499
2500 // Let's try the next srcline.
2501 postprologue_srcline_idx ++;
2502
2503 } // loop over srclines
2504
2505
2506 Dwarf_Addr postprologue_addr = DWARF_LINEADDR(postprologue_srcline);
2507 if (dwarf_haspc (& it->die, postprologue_addr) != 1)
2508 {
2509 // pick addr of previous line record
2510 Dwarf_Line *lr = dwarf_onesrcline(lines, postprologue_srcline_idx-1);
2511 postprologue_addr = DWARF_LINEADDR(lr);
2512 }
2513
2514 it->prologue_end = postprologue_addr;
2515
2516 if (sess.verbose>2)
2517 {
2518 clog << _F("prologue found function '%s'", it->name.to_string().c_str());
2519 // Add a little classification datum
2520 //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free)
2521 if (postprologue_addr == entrypc)
2522 clog << _(" (naked)");
2523 //TRANSLATORS: Here we're adding some classification datum (ie we went over)
2524 if (dwarf_haspc (& it->die,
2525 DWARF_LINEADDR(postprologue_srcline)) != 1)
2526 clog << _(" (tail-call?)");
2527 //TRANSLATORS: Here we're adding some classification datum (ie it was marked)
2528 if (DWARF_LINEPROLOGUEEND(postprologue_srcline))
2529 clog << _(" (marked)");
2530
2531 clog << " = 0x" << hex << postprologue_addr << dec << "\n";
2532 }
2533
2534 } // loop over functions
2535 }
2536
2537
2538 bool
2539 dwflpp::function_entrypc (Dwarf_Addr * addr)
2540 {
2541 assert (function);
2542
2543 // assign default value
2544 *addr = 0;
2545
2546 // PR10574: reject 0, which tends to be eliminated COMDAT
2547 if (dwarf_entrypc (function, addr) == 0 && *addr != 0)
2548 return true;
2549
2550 /* Assume the entry pc is the base address, or (if zero)
2551 the first address of the ranges covering this DIE. */
2552 Dwarf_Addr start = 0, end;
2553 if (dwarf_ranges (function, 0, addr, &start, &end) >= 0)
2554 {
2555 if (*addr == 0)
2556 *addr = start;
2557
2558 return *addr != 0;
2559 }
2560
2561 return false;
2562 }
2563
2564
2565 bool
2566 dwflpp::die_entrypc (Dwarf_Die * die, Dwarf_Addr * addr)
2567 {
2568 int rc = 0;
2569 string lookup_method;
2570
2571 * addr = 0;
2572
2573 lookup_method = "dwarf_entrypc";
2574 rc = dwarf_entrypc (die, addr);
2575
2576 if (rc)
2577 {
2578 lookup_method = "dwarf_ranges";
2579
2580 Dwarf_Addr base;
2581 Dwarf_Addr begin;
2582 Dwarf_Addr end;
2583 ptrdiff_t offset = dwarf_ranges (die, 0, &base, &begin, &end);
2584 if (offset < 0) rc = -1;
2585 else if (offset > 0)
2586 {
2587 * addr = begin;
2588 rc = 0;
2589
2590 // Now we need to check that there are no more ranges
2591 // associated with this function, which could conceivably
2592 // happen if a function is inlined, then pieces of it are
2593 // split amongst different conditional branches. It's not
2594 // obvious which of them to favour. As a heuristic, we
2595 // pick the beginning of the first range, and ignore the
2596 // others (but with a warning).
2597
2598 unsigned extra = 0;
2599 while ((offset = dwarf_ranges (die, offset, &base, &begin, &end)) > 0)
2600 extra ++;
2601 if (extra)
2602 lookup_method += _F(", ignored %s more", lex_cast(extra).c_str());
2603 }
2604 }
2605
2606 // PR10574: reject subprograms where the entrypc address turns out
2607 // to be 0, since they tend to correspond to duplicate-eliminated
2608 // COMDAT copies of C++ functions.
2609 if (rc == 0 && *addr == 0)
2610 {
2611 lookup_method += _(" (skip comdat)");
2612 rc = 1;
2613 }
2614
2615 if (sess.verbose > 2)
2616 clog << _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64 " (rc %d)", lookup_method.c_str(),
2617 lex_cast_hex(dwarf_dieoffset(die)).c_str(), *addr, rc) << endl;
2618
2619 return (rc == 0);
2620 }
2621
2622
2623 void
2624 dwflpp::function_die (Dwarf_Die *d)
2625 {
2626 assert (function);
2627 *d = *function;
2628 }
2629
2630
2631 void
2632 dwflpp::function_file (char const ** c)
2633 {
2634 assert (function);
2635 assert (c);
2636 *c = dwarf_decl_file (function);
2637 if (*c == NULL)
2638 {
2639 // The line table might know.
2640 Dwarf_Addr pc;
2641 if (dwarf_lowpc(function, &pc) == 0)
2642 *c = pc_line (pc, NULL, NULL);
2643
2644 if (*c == NULL)
2645 *c = "<unknown source>";
2646 }
2647 }
2648
2649
2650 void
2651 dwflpp::function_line (int *linep)
2652 {
2653 assert (function);
2654 if (dwarf_decl_line (function, linep) != 0)
2655 {
2656 // The line table might know.
2657 Dwarf_Addr pc;
2658 if (dwarf_lowpc(function, &pc) == 0)
2659 pc_line (pc, linep, NULL);
2660 }
2661 }
2662
2663
2664 bool
2665 dwflpp::die_has_pc (Dwarf_Die & die, Dwarf_Addr pc)
2666 {
2667 int res = dwarf_haspc (&die, pc);
2668 // dwarf_ranges will return -1 if a function die has no DW_AT_ranges
2669 // if (res == -1)
2670 // DWARF_ASSERT ("dwarf_haspc", res);
2671 return res == 1;
2672 }
2673
2674
2675 bool
2676 dwflpp::inner_die_containing_pc(Dwarf_Die& scope, Dwarf_Addr addr,
2677 Dwarf_Die& result)
2678 {
2679 result = scope;
2680
2681 // Sometimes we're in a bad scope to begin with -- just let it be. This can
2682 // happen for example if the compiler outputs a label PC that's just outside
2683 // the lexical scope. We can't really do anything about that, but variables
2684 // will probably not be accessible in this case.
2685 if (!die_has_pc(scope, addr))
2686 return false;
2687
2688 Dwarf_Die child, import;
2689 int rc = dwarf_child(&result, &child);
2690 while (rc == 0)
2691 {
2692 switch (dwarf_tag (&child))
2693 {
2694 case DW_TAG_imported_unit:
2695 // The children of the imported unit need to be treated as if
2696 // they are inserted here. So look inside and set result if
2697 // found.
2698 if (dwarf_attr_die(&child, DW_AT_import, &import))
2699 {
2700 Dwarf_Die import_result;
2701 if (inner_die_containing_pc(import, addr, import_result))
2702 {
2703 result = import_result;
2704 return true;
2705 }
2706 }
2707 break;
2708
2709 // lexical tags to recurse within the same starting scope
2710 // NB: this intentionally doesn't cross into inlines!
2711 case DW_TAG_lexical_block:
2712 case DW_TAG_with_stmt:
2713 case DW_TAG_catch_block:
2714 case DW_TAG_try_block:
2715 case DW_TAG_entry_point:
2716 if (die_has_pc(child, addr))
2717 {
2718 result = child;
2719 rc = dwarf_child(&result, &child);
2720 continue;
2721 }
2722 }
2723 rc = dwarf_siblingof(&child, &child);
2724 }
2725 return true;
2726 }
2727
2728 void
2729 dwflpp::get_locals(vector<Dwarf_Die>& scopes, set<string>& locals)
2730 {
2731 // XXX Shouldn't this be walking up to outer scopes too?
2732
2733 get_locals_die(scopes[0], locals);
2734 }
2735
2736 void
2737 dwflpp::get_locals_die(Dwarf_Die& die, set<string>& locals)
2738 {
2739 // Try to get the first child of die.
2740 Dwarf_Die child, import;
2741 if (dwarf_child (&die, &child) == 0)
2742 {
2743 do
2744 {
2745 const char *name;
2746 // Output each sibling's name (that is a variable or
2747 // parameter) to 'o'.
2748 switch (dwarf_tag (&child))
2749 {
2750 case DW_TAG_variable:
2751 case DW_TAG_formal_parameter:
2752 name = dwarf_diename (&child);
2753 if (name)
2754 locals.insert(string("$") + name);
2755 break;
2756 case DW_TAG_imported_unit:
2757 // Treat the imported unit children as if they are
2758 // children of the given DIE.
2759 if (dwarf_attr_die(&child, DW_AT_import, &import))
2760 get_locals_die (import, locals);
2761 break;
2762 default:
2763 break;
2764 }
2765 }
2766 while (dwarf_siblingof (&child, &child) == 0);
2767 }
2768 }
2769
2770
2771 Dwarf_Attribute *
2772 dwflpp::find_variable_and_frame_base (vector<Dwarf_Die>& scopes,
2773 Dwarf_Addr pc,
2774 string const & local,
2775 const target_symbol *e,
2776 Dwarf_Die *vardie,
2777 Dwarf_Attribute *fb_attr_mem,
2778 Dwarf_Die *funcdie)
2779 {
2780 Dwarf_Die *scope_die = &scopes[0];
2781 Dwarf_Attribute *fb_attr = NULL;
2782
2783 assert (cu);
2784
2785 int declaring_scope = dwarf_getscopevar (&scopes[0], scopes.size(),
2786 local.c_str(),
2787 0, NULL, 0, 0,
2788 vardie);
2789 if (declaring_scope < 0)
2790 {
2791 set<string> locals;
2792 get_locals(scopes, locals);
2793 string sugs = levenshtein_suggest(local, locals); // probably not that many, so no limit
2794 if (pc)
2795 throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)",
2796 local.c_str(),
2797 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str(),
2798 module_name.c_str(),
2799 lex_cast_hex(pc).c_str(),
2800 (scope_die == NULL) ? "" : _("in"),
2801 (dwarf_diename(scope_die) ?: "<unknown>"),
2802 (dwarf_diename(cu) ?: "<unknown>"),
2803 (sugs.empty()
2804 ? (_("<no alternatives>"))
2805 : (_("alternatives: ") + sugs + ")")).c_str()),
2806 e->tok);
2807 else
2808 throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)",
2809 local.c_str(),
2810 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str(),
2811 module_name.c_str(),
2812 (scope_die == NULL) ? "" : _("in"),
2813 (dwarf_diename(scope_die) ?: "<unknown>"),
2814 cu_name().c_str(),
2815 (sugs.empty()
2816 ? (_("<no alternatives>"))
2817 : (_("alternatives: ") + sugs + ")")).c_str()),
2818 e->tok);
2819 }
2820
2821 *funcdie = scopes[declaring_scope];
2822
2823 /* Some GCC versions would output duplicate external variables, one
2824 without a location attribute. If so, try to find the other if it
2825 exists in the same scope. See GCC PR51410. */
2826 Dwarf_Attribute attr_mem;
2827 if (dwarf_attr_integrate (vardie, DW_AT_const_value, &attr_mem) == NULL
2828 && dwarf_attr_integrate (vardie, DW_AT_location, &attr_mem) == NULL
2829 && dwarf_attr_integrate (vardie, DW_AT_external, &attr_mem) != NULL
2830 && dwarf_tag(&scopes[declaring_scope]) == DW_TAG_compile_unit)
2831 {
2832 Dwarf_Die orig_vardie = *vardie;
2833 bool alt_found = false;
2834 if (dwarf_child(&scopes[declaring_scope], vardie) == 0)
2835 do
2836 {
2837 // Note, not handling DW_TAG_imported_unit, assuming GCC
2838 // version is recent enough to not need this workaround if
2839 // we would see an imported unit.
2840 if (dwarf_tag (vardie) == DW_TAG_variable
2841 && strcmp (dwarf_diename (vardie), local.c_str ()) == 0
2842 && (dwarf_attr_integrate (vardie, DW_AT_external, &attr_mem)
2843 != NULL)
2844 && ((dwarf_attr_integrate (vardie, DW_AT_const_value, &attr_mem)
2845 != NULL)
2846 || (dwarf_attr_integrate (vardie, DW_AT_location, &attr_mem)
2847 != NULL)))
2848 alt_found = true;
2849 }
2850 while (!alt_found && dwarf_siblingof(vardie, vardie) == 0);
2851
2852 if (! alt_found)
2853 *vardie = orig_vardie;
2854 }
2855
2856 // Global vars don't need (cannot use) frame base in location descriptor.
2857 if (pc == 0)
2858 return NULL;
2859
2860 /* We start out walking the "lexical scopes" as returned by
2861 * as returned by dwarf_getscopes for the address, starting with the
2862 * declaring_scope that the variable was found in.
2863 */
2864 vector<Dwarf_Die> physcopes, *fbscopes = &scopes;
2865 for (size_t inner = declaring_scope;
2866 inner < fbscopes->size() && fb_attr == NULL;
2867 ++inner)
2868 {
2869 Dwarf_Die& scope = (*fbscopes)[inner];
2870 switch (dwarf_tag (&scope))
2871 {
2872 default:
2873 continue;
2874 case DW_TAG_subprogram:
2875 case DW_TAG_entry_point:
2876 fb_attr = dwarf_attr_integrate (&scope,
2877 DW_AT_frame_base,
2878 fb_attr_mem);
2879 break;
2880 case DW_TAG_inlined_subroutine:
2881 /* Unless we already are going through the "pyshical die tree",
2882 * we now need to start walking the die tree where this
2883 * subroutine is inlined to find the appropriate frame base. */
2884 if (declaring_scope != -1)
2885 {
2886 physcopes = getscopes_die(&scope);
2887 if (physcopes.empty())
2888 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
2889 local.c_str()), e->tok);
2890 fbscopes = &physcopes;
2891 inner = 0; // zero is current scope, for look will increase.
2892 declaring_scope = -1;
2893 }
2894 break;
2895 }
2896 }
2897
2898 return fb_attr;
2899 }
2900
2901 /* Returns a human readable string with suggested locations where a
2902 DIE attribute is valid. */
2903 static string
2904 suggested_locations_string(Dwarf_Attribute *attr)
2905 {
2906 string locsstr;
2907 if (attr == NULL)
2908 locsstr = "<no alternatives for NULL attribute>";
2909 else
2910 {
2911 #if _ELFUTILS_PREREQ (0, 158)
2912 Dwarf_Op *expr;
2913 size_t exprlen;
2914 Dwarf_Addr base, start, end;
2915 ptrdiff_t off = 0;
2916
2917 off = dwarf_getlocations (attr, off, &base,
2918 &start, &end,
2919 &expr, &exprlen);
2920 if (off > 0)
2921 {
2922 locsstr = _("alternative locations: ");
2923
2924 while (off > 0)
2925 {
2926 locsstr += "[";
2927 locsstr += lex_cast_hex(start);
2928 locsstr += ",";
2929 locsstr += lex_cast_hex(end);
2930 locsstr += "]";
2931
2932 off = dwarf_getlocations (attr, off, &base,
2933 &start, &end,
2934 &expr, &exprlen);
2935 if (off > 0)
2936 locsstr += ", ";
2937 }
2938 }
2939 else if (off == 0)
2940 locsstr = _("<no alternative locations>");
2941 else
2942 locsstr = _F("<error getting alternative locations: %s>",
2943 dwarf_errmsg(-1));
2944 #else
2945 locsstr = "<cannot suggest any alternative locations, elfutils too old>";
2946 #endif /* _ELFUTILS_PREREQ (0, 158) */
2947 }
2948
2949 return locsstr;
2950 }
2951
2952 /* Produce a human readable name for a DIE. */
2953 static string
2954 die_name_string (Dwarf_Die *die)
2955 {
2956 string res;
2957 const char *name = dwarf_linkage_name(die);
2958 if (name == NULL)
2959 name = dwarf_diename (die);
2960
2961 size_t demangle_buffer_len = 0;
2962 char *demangle_buffer = NULL;
2963 if (name != NULL && name[0] == '_' && name[1] == 'Z')
2964 {
2965 int status = -1;
2966 char *dsymname = abi::__cxa_demangle (name, demangle_buffer,
2967 &demangle_buffer_len, &status);
2968 if (status == 0)
2969 name = demangle_buffer = dsymname;
2970 }
2971 if (name != NULL)
2972 res = name;
2973 else
2974 res = _("<unknown");
2975 free (demangle_buffer);
2976
2977 return res;
2978 }
2979
2980 /* Returns a source file name, line and column information based on the
2981 pc and the current cu. */
2982 const char *
2983 dwflpp::pc_line (Dwarf_Addr pc, int *lineno, int *colno)
2984 {
2985 if (pc != 0)
2986 {
2987 Dwarf_Line *line = dwarf_getsrc_die (cu, pc);
2988 if (line != NULL)
2989 {
2990 if (lineno != NULL)
2991 dwarf_lineno (line, lineno);
2992 if (colno != NULL)
2993 dwarf_linecol (line, colno);
2994 return dwarf_linesrc (line, NULL, NULL);
2995 }
2996 }
2997
2998 return NULL;
2999 }
3000
3001 /* Returns a source line and column string based on the inlined DIE
3002 or based on the pc if DIE is NULL. */
3003 string
3004 dwflpp::pc_die_line_string (Dwarf_Addr pc, Dwarf_Die *die)
3005 {
3006 string linestr;
3007
3008 int lineno, col;
3009 const char *src = NULL;
3010 lineno = col = -1;
3011
3012 if (die == NULL)
3013 src = pc_line (pc, &lineno, &col);
3014 else
3015 {
3016 Dwarf_Files *files;
3017 if (dwarf_getsrcfiles (cu, &files, NULL) == 0)
3018 {
3019 Dwarf_Attribute attr;
3020 Dwarf_Word val;
3021 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_file, &attr),
3022 &val) == 0)
3023 {
3024 src = dwarf_filesrc (files, val, NULL, NULL);
3025 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_line,
3026 &attr), &val) == 0)
3027 {
3028 lineno = val;
3029 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_column,
3030 &attr), &val) == 0)
3031 col = val;
3032 }
3033 }
3034 }
3035 else
3036 src = pc_line (pc, &lineno, &col);
3037 }
3038
3039 if (src != NULL)
3040 {
3041 linestr += src;
3042 if (lineno > 0)
3043 {
3044 linestr += ":" + lex_cast(lineno);
3045 if (col > 0)
3046 linestr += ":" + lex_cast(col);
3047 }
3048 }
3049 else
3050 linestr += _("unknown source");
3051
3052 return linestr;
3053 }
3054
3055 /* Returns a human readable DIE offset for use in error messages.
3056 Includes DIE offset and DWARF file used. */
3057 string
3058 dwflpp::die_location_as_string(Dwarf_Die *die)
3059 {
3060 string locstr;
3061
3062 /* DIE offset */
3063 locstr += _("dieoffset: ");
3064 locstr += lex_cast_hex(dwarf_dieoffset(die));
3065
3066 /* DWARF file */
3067 const char *mainfile, *debugfile;
3068 locstr += _(" from ");
3069 if (dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &mainfile,
3070 &debugfile) == NULL
3071 || (mainfile == NULL && debugfile == NULL))
3072 {
3073 locstr += _("unknown debug file for ");
3074 locstr += module_name;
3075 }
3076 else
3077 {
3078 if (debugfile != NULL)
3079 locstr += debugfile;
3080 else
3081 locstr += mainfile;
3082 }
3083
3084 return locstr;
3085 }
3086
3087 /* Returns a human readable (inlined) function and source file/line location
3088 for a pc location. */
3089 string
3090 dwflpp::pc_location_as_function_string(Dwarf_Addr pc)
3091 {
3092 string locstr;
3093 locstr = _("function: ");
3094
3095 /* Find the first function-like DIE with a name in scope. */
3096 Dwarf_Die funcdie_mem;
3097 Dwarf_Die *funcdie = NULL;
3098 string funcname = "";
3099 Dwarf_Die *scopes = NULL;
3100 int nscopes = dwarf_getscopes (cu, pc, &scopes);
3101 for (int i = 0; funcname == "" && i < nscopes; i++)
3102 {
3103 Dwarf_Die *scope = &scopes[i];
3104 int tag = dwarf_tag (scope);
3105 if (tag == DW_TAG_subprogram
3106 || tag == DW_TAG_inlined_subroutine
3107 || tag == DW_TAG_entry_point)
3108 funcname = die_name_string (scope);
3109 if (funcname != "")
3110 {
3111 funcdie_mem = *scope;
3112 funcdie = &funcdie_mem;
3113 }
3114 }
3115 free (scopes);
3116
3117 /* source location */
3118 if (funcname == "")
3119 locstr += _("<unknown> at ") + pc_die_line_string (pc, NULL);
3120 else
3121 {
3122 int nscopes = dwarf_getscopes_die (funcdie, &scopes);
3123 if (nscopes > 0)
3124 {
3125 /* scopes[0] == funcdie, the lowest level, for which we already have
3126 the name. This is the actual source location where it
3127 happened. */
3128 locstr += funcname;
3129 locstr += _(" at ");
3130 locstr += pc_die_line_string (pc, NULL);
3131
3132 /* last_scope is the source location where the next inlined frame/function
3133 call was done. */
3134 Dwarf_Die *last_scope = &scopes[0];
3135 for (int i = 1; i < nscopes; i++)
3136 {
3137 Dwarf_Die *scope = &scopes[i];
3138 int tag = dwarf_tag (scope);
3139 if (tag != DW_TAG_inlined_subroutine
3140 && tag != DW_TAG_entry_point
3141 && tag != DW_TAG_subprogram)
3142 continue;
3143
3144 locstr += _(" inlined by ");
3145 locstr += die_name_string (scope);
3146 locstr += _(" at ");
3147 locstr += pc_die_line_string (pc, last_scope);
3148
3149 /* Found the "top-level" in which everything was inlined. */
3150 if (tag == DW_TAG_subprogram)
3151 break;
3152
3153 last_scope = scope;
3154 }
3155 }
3156 else
3157 {
3158 locstr += funcname;
3159 locstr += _(" at ");
3160 locstr += pc_die_line_string (pc, NULL);
3161 }
3162 free (scopes);
3163 }
3164
3165 return locstr;
3166 }
3167
3168 struct location *
3169 dwflpp::translate_location(location_context *ctx,
3170 Dwarf_Attribute *attr, Dwarf_Die *die,
3171 Dwarf_Addr pc, Dwarf_Attribute *fb_attr,
3172 const target_symbol *e, location *input)
3173 {
3174
3175 /* DW_AT_data_member_location, can be either constant offsets
3176 (struct member fields), or full blown location expressions. */
3177
3178 /* There is no location expression, but a constant value instead. */
3179 if (dwarf_whatattr (attr) == DW_AT_const_value)
3180 return ctx->translate_constant (attr);
3181
3182 Dwarf_Op *expr;
3183 size_t len;
3184
3185 /* PR9768: formerly, we added pc+module_bias here. However, that bias value
3186 is not present in the pc value by the time we get it, so adding it would
3187 result in false negatives of variable reachibility. In other instances
3188 further below, the c_translate_FOO functions, the module_bias value used
3189 to be passed in, but instead should now be zero for the same reason. */
3190
3191 retry:
3192 switch (dwarf_getlocation_addr (attr, pc /*+ module_bias*/, &expr, &len, 1))
3193 {
3194 case 1: /* Should always happen. */
3195 if (len > 0)
3196 break;
3197 /* Fall through. */
3198
3199 case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */
3200 {
3201 Dwarf_Addr pc2 = pr15123_retry_addr (pc, die);
3202 if (pc2 != 0) {
3203 pc = pc2;
3204 goto retry;
3205 }
3206 }
3207
3208 /* FALLTHROUGH */
3209 {
3210 string msg = _F("not accessible at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc).c_str());
3211 semantic_error err(ERR_SRC, msg, e->tok);
3212 err.details.push_back(die_location_as_string(die));
3213 err.details.push_back(pc_location_as_function_string(pc));
3214 err.details.push_back(suggested_locations_string(attr));
3215 throw err;
3216 }
3217
3218 default: /* Shouldn't happen. */
3219 case -1:
3220 {
3221 string msg = _F("dwarf_getlocation_addr failed at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc).c_str());
3222 semantic_error err(ERR_SRC, msg, e->tok);
3223 string dwarf_err = _F("dwarf_error: %s", dwarf_errmsg(-1));
3224 err.details.push_back(dwarf_err);
3225 err.details.push_back(die_location_as_string(die));
3226 err.details.push_back(pc_location_as_function_string(pc));
3227 err.details.push_back(suggested_locations_string(attr));
3228 throw err;
3229 }
3230 }
3231
3232 Dwarf_Op *cfa_ops = NULL;
3233 // pc is in the dw address space of the current module, which is what
3234 // c_translate_location expects. get_cfa_ops wants the global dwfl address.
3235 // cfa_ops only make sense for locals.
3236 if (pc)
3237 {
3238 Dwarf_Addr addr = pc + module_bias;
3239 cfa_ops = get_cfa_ops (addr);
3240 }
3241
3242 // ??? Reset these afterward.
3243 ctx->cfa_ops = cfa_ops;
3244 ctx->fb_attr = fb_attr;
3245 ctx->pc = pc;
3246 ctx->dw = this;
3247
3248 return ctx->translate_location (expr, len, input);
3249 }
3250
3251
3252 void
3253 dwflpp::get_members(Dwarf_Die *typedie, set<string>& members, set<string> &dupes)
3254 {
3255 const int typetag = dwarf_tag (typedie);
3256
3257 /* compile and partial unit included for recursion through
3258 imported_unit below. */
3259 if (typetag != DW_TAG_structure_type &&
3260 typetag != DW_TAG_class_type &&
3261 typetag != DW_TAG_union_type &&
3262 typetag != DW_TAG_compile_unit &&
3263 typetag != DW_TAG_partial_unit)
3264 {
3265 throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union",
3266 dwarf_type_name(typedie).c_str()));
3267 }
3268
3269 // Try to get the first child of vardie.
3270 Dwarf_Die die_mem, import;
3271 Dwarf_Die *die = &die_mem;
3272 switch (dwarf_child (typedie, die))
3273 {
3274 case 1: // No children.
3275 throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie).c_str()));
3276
3277 case -1: // Error.
3278 default: // Shouldn't happen.
3279 throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie).c_str(),
3280 dwarf_errmsg(-1)));
3281
3282 case 0: // Success.
3283 break;
3284 }
3285
3286 // Add each sibling's name to members set
3287 do
3288 {
3289 int tag = dwarf_tag(die);
3290
3291 /* The children of an imported_unit should be treated as members too. */
3292 if (tag == DW_TAG_imported_unit
3293 && dwarf_attr_die(die, DW_AT_import, &import))
3294 get_members(&import, members, dupes);
3295
3296 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3297 continue;
3298
3299 const char *member = dwarf_diename (die) ;
3300
3301 if ( tag == DW_TAG_member && member != NULL )
3302 {
3303 // Only output if this is new, to avoid inheritance dupes.
3304 if (dupes.insert(member).second)
3305 members.insert(member);
3306 }
3307 else
3308 {
3309 Dwarf_Die temp_die;
3310 if (!dwarf_attr_die (die, DW_AT_type, &temp_die))
3311 {
3312 string source = dwarf_decl_file(die) ?: "<unknown source>";
3313 int line = -1;
3314 dwarf_decl_line(die, &line);
3315 throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous "
3316 "member at %s:%d", source.c_str(), line));
3317 }
3318
3319 get_members(&temp_die, members, dupes);
3320 }
3321
3322 }
3323 while (dwarf_siblingof (die, die) == 0);
3324 }
3325
3326
3327 bool
3328 dwflpp::find_struct_member(const target_symbol::component& c,
3329 Dwarf_Die *parentdie,
3330 Dwarf_Die *memberdie,
3331 vector<Dwarf_Die>& dies,
3332 vector<Dwarf_Attribute>& locs)
3333 {
3334 Dwarf_Attribute attr;
3335 Dwarf_Die die;
3336
3337 /* With inheritance, a subclass may mask member names of parent classes, so
3338 * our search among the inheritance tree must be breadth-first rather than
3339 * depth-first (recursive). The parentdie is still our starting point. */
3340 deque<Dwarf_Die> inheritees(1, *parentdie);
3341 for (; !inheritees.empty(); inheritees.pop_front())
3342 {
3343 switch (dwarf_child (&inheritees.front(), &die))
3344 {
3345 case 0: /* First child found. */
3346 break;
3347 case 1: /* No children. */
3348 continue;
3349 case -1: /* Error. */
3350 default: /* Shouldn't happen */
3351 throw SEMANTIC_ERROR (dwarf_type_name(&inheritees.front()) + ": "
3352 + string (dwarf_errmsg (-1)),
3353 c.tok);
3354 }
3355
3356 do
3357 {
3358 int tag = dwarf_tag(&die);
3359 /* recurse into imported units as if they are anonymoust structs */
3360 Dwarf_Die import;
3361 if (tag == DW_TAG_imported_unit
3362 && dwarf_attr_die(&die, DW_AT_import, &import)
3363 && find_struct_member(c, &import, memberdie, dies, locs))
3364 goto success;
3365
3366 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3367 continue;
3368
3369 const char *name = dwarf_diename(&die);
3370 if (tag == DW_TAG_inheritance)
3371 {
3372 /* Remember inheritee for breadth-first search. */
3373 Dwarf_Die inheritee;
3374 if (dwarf_attr_die (&die, DW_AT_type, &inheritee))
3375 inheritees.push_back(inheritee);
3376 }
3377 else if (name == NULL)
3378 {
3379 /* Need to recurse for anonymous structs/unions. */
3380 Dwarf_Die subdie;
3381 if (dwarf_attr_die (&die, DW_AT_type, &subdie) &&
3382 find_struct_member(c, &subdie, memberdie, dies, locs))
3383 goto success;
3384 }
3385 else if (name == c.member)
3386 {
3387 *memberdie = die;
3388 goto success;
3389 }
3390 }
3391 while (dwarf_siblingof (&die, &die) == 0);
3392 }
3393
3394 return false;
3395
3396 success:
3397 /* As we unwind the recursion, we need to build the chain of
3398 * locations that got to the final answer. */
3399 if (dwarf_attr_integrate (&die, DW_AT_data_member_location, &attr))
3400 {
3401 dies.insert(dies.begin(), die);
3402 locs.insert(locs.begin(), attr);
3403 }
3404
3405 /* Union members don't usually have a location,
3406 * but just use the containing union's location. */
3407 else if (dwarf_tag(parentdie) != DW_TAG_union_type)
3408 throw SEMANTIC_ERROR (_F("no location for field '%s':%s",
3409 c.member.c_str(), dwarf_errmsg(-1)), c.tok);
3410
3411 return true;
3412 }
3413
3414
3415 static inline void
3416 dwarf_die_type (Dwarf_Die *die, Dwarf_Die *typedie_mem, const token *tok=NULL)
3417 {
3418 if (!dwarf_attr_die (die, DW_AT_type, typedie_mem))
3419 throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok);
3420 }
3421
3422
3423 void
3424 dwflpp::translate_components(location_context *ctx,
3425 Dwarf_Addr pc,
3426 const target_symbol *e,
3427 Dwarf_Die *vardie,
3428 Dwarf_Die *typedie,
3429 bool lvalue,
3430 unsigned first)
3431 {
3432 unsigned i = first;
3433 while (i < e->components.size())
3434 {
3435 const target_symbol::component& c = e->components[i];
3436
3437 switch (dwarf_tag (typedie))
3438 {
3439 case DW_TAG_typedef:
3440 case DW_TAG_const_type:
3441 case DW_TAG_volatile_type:
3442 case DW_TAG_restrict_type:
3443 /* Just iterate on the referent type. */
3444 dwarf_die_type (typedie, typedie, c.tok);
3445 break;
3446
3447 case DW_TAG_reference_type:
3448 case DW_TAG_rvalue_reference_type:
3449 translate_pointer (*ctx, typedie, lvalue);
3450 dwarf_die_type (typedie, typedie, c.tok);
3451 break;
3452
3453 case DW_TAG_pointer_type:
3454 /* A pointer with no type is a void* -- can't dereference it. */
3455 if (!dwarf_hasattr_integrate (typedie, DW_AT_type))
3456 throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c).c_str(),
3457 dwarf_type_name(typedie).c_str()), c.tok);
3458
3459 if (!ctx->locations.empty())
3460 translate_pointer (*ctx, typedie, lvalue);
3461 if (c.type != target_symbol::comp_literal_array_index &&
3462 c.type != target_symbol::comp_expression_array_index)
3463 {
3464 dwarf_die_type (typedie, typedie, c.tok);
3465 break;
3466 }
3467 /* Fallthrough */
3468 /* else an array access */
3469
3470 case DW_TAG_array_type:
3471 if (c.type == target_symbol::comp_literal_array_index)
3472 {
3473 if (c.num_index != 0 && !ctx->locations.empty())
3474 ctx->translate_array(typedie, ctx->locations.back(),
3475 new literal_number(c.num_index));
3476 }
3477 else if (c.type == target_symbol::comp_expression_array_index)
3478 {
3479 if (!ctx->locations.empty())
3480 ctx->translate_array(typedie, ctx->locations.back(),
3481 c.expr_index);
3482 }
3483 else
3484 throw SEMANTIC_ERROR (_F("invalid access '%s' for array type",
3485 lex_cast(c).c_str()), c.tok);
3486
3487 dwarf_die_type (typedie, typedie, c.tok);
3488 *vardie = *typedie;
3489 ++i;
3490 break;
3491
3492 case DW_TAG_structure_type:
3493 case DW_TAG_union_type:
3494 case DW_TAG_class_type:
3495 if (c.type != target_symbol::comp_struct_member)
3496 throw SEMANTIC_ERROR (_F("invalid access '%s' for %s",
3497 lex_cast(c).c_str(), dwarf_type_name(typedie).c_str()));
3498
3499 if (dwarf_hasattr(typedie, DW_AT_declaration))
3500 {
3501 Dwarf_Die *tmpdie = declaration_resolve(typedie);
3502 if (tmpdie == NULL)
3503 throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie).c_str()), c.tok);
3504 *typedie = *tmpdie;
3505 }
3506
3507 {
3508 vector<Dwarf_Die> dies;
3509 vector<Dwarf_Attribute> locs;
3510 if (!find_struct_member(c, typedie, vardie, dies, locs))
3511 {
3512 /* Add a file:line hint for anonymous types */
3513 string source;
3514 if (!dwarf_hasattr_integrate(typedie, DW_AT_name))
3515 {
3516 int line;
3517 const char *file = dwarf_decl_file(typedie);
3518 if (file && dwarf_decl_line(typedie, &line) == 0)
3519 source = " (" + string(file) + ":"
3520 + lex_cast(line) + ")";
3521 }
3522
3523 set<string> members, member_dupes;
3524 get_members(typedie, members, member_dupes);
3525 string sugs = levenshtein_suggest(c.member, members);
3526 if (!sugs.empty())
3527 sugs = " (alternatives: " + sugs + ")";
3528 throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c.member.c_str(),
3529 dwarf_type_name(typedie).c_str(), source.c_str(),
3530 sugs.c_str()), c.tok);
3531 }
3532
3533 if (!ctx->locations.empty())
3534 {
3535 location *n = ctx->locations.back();
3536 for (unsigned j = 0; j < locs.size(); ++j)
3537 n = translate_location (ctx, &locs[j], &dies[j],
3538 pc, NULL, e, n);
3539 ctx->locations.push_back(n);
3540 }
3541 }
3542
3543 dwarf_die_type (vardie, typedie, c.tok);
3544 ++i;
3545 break;
3546
3547 case DW_TAG_enumeration_type:
3548 case DW_TAG_base_type:
3549 throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c).c_str(),
3550 dwarf_type_name(typedie).c_str()), c.tok);
3551 break;
3552
3553 case -1:
3554 throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c.tok);
3555 break;
3556
3557 default:
3558 throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie).c_str(),
3559 lex_cast(dwarf_tag(typedie)).c_str()), c.tok);
3560 break;
3561 }
3562 }
3563 }
3564
3565
3566 void
3567 dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die *typedie,
3568 Dwarf_Die *innerdie,
3569 const target_symbol *e)
3570 {
3571 int typetag = dwarf_tag (typedie);
3572 *innerdie = *typedie;
3573 while (typetag == DW_TAG_typedef ||
3574 typetag == DW_TAG_const_type ||
3575 typetag == DW_TAG_volatile_type ||
3576 typetag == DW_TAG_restrict_type)
3577 {
3578 if (!dwarf_attr_die (innerdie, DW_AT_type, innerdie))
3579 throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e->tok);
3580 typetag = dwarf_tag (innerdie);
3581 }
3582 }
3583
3584 static void
3585 get_bitfield (const target_symbol *e, Dwarf_Die *die, Dwarf_Word byte_size,
3586 Dwarf_Word *bit_offset, Dwarf_Word *bit_size)
3587 {
3588 Dwarf_Attribute attr_mem;
3589 if (dwarf_attr_integrate (die, DW_AT_bit_offset, &attr_mem) == NULL
3590 || dwarf_formudata (&attr_mem, bit_offset) != 0
3591 || dwarf_attr_integrate (die, DW_AT_bit_size, &attr_mem) == NULL
3592 || dwarf_formudata (&attr_mem, bit_size) != 0)
3593 throw SEMANTIC_ERROR (_F("cannot get bit field parameters: %s",
3594 dwarf_errmsg(-1)), e->tok);
3595
3596 /* Convert the big-bit-endian numbers from Dwarf to little-endian.
3597 This means we can avoid having to propagate byte_size further. */
3598 *bit_offset = byte_size * 8 - *bit_offset - *bit_size;
3599 }
3600
3601 void
3602 dwflpp::translate_base_ref (location_context &ctx, Dwarf_Word byte_size,
3603 bool signed_p, bool lvalue_p)
3604 {
3605 location *loc = ctx.locations.back ();
3606
3607 restart:
3608 switch (loc->type)
3609 {
3610 case loc_value:
3611 // The existing program is the value.
3612 break;
3613
3614 case loc_address:
3615 {
3616 target_deref *d = new target_deref;
3617 d->tok = ctx.e->tok;
3618 d->addr = loc->program;
3619 d->size = byte_size;
3620 d->signed_p = signed_p;
3621 d->userspace_p = ctx.userspace_p;
3622
3623 loc = ctx.new_location(loc_value);
3624 loc->program = d;
3625 loc->byte_size = byte_size;
3626
3627 // ??? There is code in *translate.cxx to handle the sign-
3628 // (or implicit zero-) extension during the load. We might
3629 // be better off falling through to the shared code below.
3630 return;
3631 }
3632
3633 case loc_register:
3634 if (loc->offset != 0)
3635 throw SEMANTIC_ERROR (_("cannot handle offset into register"), ctx.e->tok);
3636 // The existing program is the value.
3637 break;
3638
3639 case loc_constant:
3640 {
3641 if (loc->byte_size < byte_size)
3642 throw SEMANTIC_ERROR (_("requested size larger than constant"),
3643 ctx.e->tok);
3644
3645 /* ??? Byte ordering. */
3646 int64_t val;
3647 switch (byte_size)
3648 {
3649 case 1:
3650 if (signed_p)
3651 val = *(int8_t *)loc->constant_block;
3652 else
3653 val = *(uint8_t *)loc->constant_block;
3654 break;
3655 case 2:
3656 if (signed_p)
3657 val = *(int16_t *)loc->constant_block;
3658 else
3659 val = *(uint16_t *)loc->constant_block;
3660 break;
3661 case 4:
3662 if (signed_p)
3663 val = *(int32_t *)loc->constant_block;
3664 else
3665 val = *(uint32_t *)loc->constant_block;
3666 break;
3667 case 8:
3668 val = *(int64_t *)loc->constant_block;
3669 break;
3670 default:
3671 throw SEMANTIC_ERROR (_("unhandled constant size"), ctx.e->tok);
3672 }
3673
3674 loc = ctx.new_location(loc_value);
3675 loc->program = new literal_number(val);
3676 loc->byte_size = byte_size;
3677 return;
3678 }
3679
3680 case loc_noncontiguous:
3681 loc = loc->pieces;
3682 if (loc && loc->byte_size <= byte_size)
3683 {
3684 ctx.locations.push_back(loc);
3685 goto restart;
3686 }
3687 throw SEMANTIC_ERROR (_("noncontiguous location for base fetch"),
3688 ctx.e->tok);
3689
3690 case loc_implicit_pointer:
3691 if (loc->offset != 0)
3692 throw SEMANTIC_ERROR (_("cannot handle offset into implicit pointer"),
3693 ctx.e->tok);
3694 loc = loc->target;
3695 if (loc)
3696 {
3697 ctx.locations.push_back(loc);
3698 goto restart;
3699 }
3700 throw SEMANTIC_ERROR (_("pointer optimized out"), ctx.e->tok);
3701
3702 case loc_unavailable:
3703 throw SEMANTIC_ERROR (_("location not available"), ctx.e->tok);
3704 default:
3705 abort();
3706 }
3707
3708 // Normalize LOC from the sign and width of the type to int64_t.
3709 // ??? We might require extending when assigning to a target_register.
3710 // Give this a try first...
3711 if (byte_size < 8 && !lvalue_p)
3712 {
3713 binary_expression *out = new binary_expression;
3714 out->tok = ctx.e->tok;
3715
3716 if (signed_p)
3717 {
3718 int shift = 64 - 8 * byte_size;
3719 binary_expression *shl = new binary_expression;
3720 shl->tok = ctx.e->tok;
3721 shl->op = "<<";
3722 shl->left = loc->program;
3723 shl->right = new literal_number(shift);
3724
3725 out->op = ">>";
3726 out->left = shl;
3727 out->right = new literal_number(shift);
3728 }
3729 else
3730 {
3731 out->op = "&";
3732 out->left = loc->program;
3733 out->right = new literal_number((1ull << (byte_size * 8)) - 1);
3734 }
3735
3736 loc = ctx.new_location(loc_value);
3737 loc->program = out;
3738 loc->byte_size = byte_size;
3739 }
3740 }
3741
3742 void
3743 dwflpp::translate_bitfield(location_context &ctx, Dwarf_Word byte_size,
3744 Dwarf_Word bit_offset, Dwarf_Word bit_size,
3745 bool signed_p)
3746 {
3747 location *loc = ctx.locations.back ();
3748
3749 target_bitfield *bf = new target_bitfield;
3750 bf->tok = ctx.e->tok;
3751 bf->base = loc->program;
3752 bf->offset = bit_offset;
3753 bf->size = bit_size;
3754 bf->signed_p = signed_p;
3755
3756 loc = ctx.new_location(loc_value);
3757 loc->program = bf;
3758 loc->byte_size = byte_size;
3759 ctx.locations.push_back(loc);
3760 }
3761
3762 // As usual, leave the result as the last location in ctx.
3763 void
3764 dwflpp::translate_final_fetch_or_store (location_context &ctx,
3765 Dwarf_Die *vardie,
3766 Dwarf_Die *start_typedie,
3767 bool lvalue,
3768 Dwarf_Die *typedie)
3769 {
3770 const target_symbol *e = ctx.e;
3771
3772 /* First boil away any qualifiers associated with the type DIE of
3773 the final location to be accessed. */
3774 resolve_unqualified_inner_typedie (start_typedie, typedie, e);
3775
3776 /* If we're looking for an address, then we can just provide what
3777 we computed to this point, without using a fetch/store. */
3778 if (ctx.e->addressof)
3779 {
3780 if (lvalue)
3781 throw SEMANTIC_ERROR (_("cannot write to member address"), e->tok);
3782
3783 if (dwarf_hasattr_integrate (vardie, DW_AT_bit_offset)
3784 || dwarf_hasattr_integrate (vardie, DW_AT_data_bit_offset))
3785 throw SEMANTIC_ERROR (_("cannot take the address of a bit field"),
3786 e->tok);
3787
3788 switch (ctx.locations.back()->type)
3789 {
3790 case loc_address:
3791 /* do nothing, we're done */
3792 break;
3793 case loc_register:
3794 throw SEMANTIC_ERROR (_("cannot take address of object in register"),
3795 e->tok);
3796 break;
3797 case loc_noncontiguous:
3798 throw SEMANTIC_ERROR (_("cannot take address of noncontiguous object"),
3799 e->tok);
3800 break;
3801 case loc_value:
3802 throw SEMANTIC_ERROR (_("cannot take address of computed value"),
3803 e->tok);
3804 break;
3805 case loc_constant:
3806 throw SEMANTIC_ERROR (_("cannot take address of constant value"),
3807 e->tok);
3808 break;
3809 case loc_unavailable:
3810 throw SEMANTIC_ERROR (_("cannot take address of unavailable value"),
3811 e->tok);
3812 break;
3813 case loc_implicit_pointer:
3814 throw SEMANTIC_ERROR (_("cannot take address of implicit pointer"),
3815 e->tok);
3816 break;
3817 default:
3818 abort ();
3819 break;
3820 }
3821 return;
3822 }
3823
3824 /* Then switch behavior depending on the type of fetch/store we
3825 want, and the type and pointer-ness of the final location. */
3826
3827 int typetag = dwarf_tag (typedie);
3828 switch (typetag)
3829 {
3830 default:
3831 throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s",
3832 lex_cast(typetag).c_str(),
3833 dwarf_type_name(typedie).c_str()), e->tok);
3834
3835 case DW_TAG_structure_type:
3836 case DW_TAG_class_type:
3837 case DW_TAG_union_type:
3838 {
3839 string type_name = dwarf_type_name(typedie);
3840 string decl_file = dwarf_decl_file(typedie) ?: "";
3841 int decl_line = 0;
3842 (void) dwarf_decl_line(typedie, &decl_line);
3843 string decl_source;
3844 // PR20423: assemble an error message at least as informative
3845 // as bad-member message in translate_components()
3846 if (decl_file[0] && decl_line > 0)
3847 decl_source = " (" + decl_file + ":" + lex_cast(decl_line) + ")";
3848 string a_member;
3849 try
3850 {
3851 set<string> members;
3852 set<string> dupes;
3853 get_members (typedie, members, dupes);
3854 if (members.begin() != members.end())
3855 {
3856 a_member = " such as '->" + (*members.begin()) + "'";
3857 }
3858 }
3859 catch (...)
3860 {
3861 // leave a_member empty
3862 }
3863 throw SEMANTIC_ERROR (_F("'%s'%s is being accessed instead of a member%s",
3864 type_name.c_str(),
3865 decl_source.c_str(),
3866 a_member.c_str()),
3867 (e->components.size() > 0 ?
3868 (e->components[e->components.size()-1].tok) :
3869 (e->tok)));
3870 }
3871
3872 case DW_TAG_base_type:
3873 case DW_TAG_enumeration_type:
3874 // Reject types we can't handle in systemtap
3875 {
3876 bool signed_p = false;
3877
3878 if (typetag == DW_TAG_base_type)
3879 {
3880 Dwarf_Attribute encoding_attr;
3881 Dwarf_Word encoding = (Dwarf_Word) -1;
3882
3883 dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding,
3884 &encoding_attr), &encoding);
3885 if (encoding == (Dwarf_Word) -1)
3886 {
3887 throw (SEMANTIC_ERROR
3888 (_F("unsupported type (mystery encoding %s for %s",
3889 lex_cast(encoding).c_str(),
3890 dwarf_type_name(typedie).c_str()), e->tok));
3891 }
3892
3893 if (encoding == DW_ATE_float
3894 || encoding == DW_ATE_complex_float
3895 /* XXX || many others? */)
3896 {
3897 throw (SEMANTIC_ERROR
3898 (_F("unsupported type (encoding %s) for %s",
3899 lex_cast(encoding).c_str(),
3900 dwarf_type_name(typedie).c_str()), e->tok));
3901 }
3902
3903 signed_p = (encoding == DW_ATE_signed
3904 || encoding == DW_ATE_signed_char);
3905 }
3906
3907 Dwarf_Attribute size_attr;
3908 Dwarf_Word byte_size;
3909 if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &size_attr) == NULL
3910 || dwarf_formudata (&size_attr, &byte_size) != 0)
3911 {
3912 throw (SEMANTIC_ERROR
3913 (_F("cannot get byte_size attribute for type %s: %s",
3914 dwarf_diename (typedie) ?: "<anonymous>",
3915 dwarf_errmsg (-1)), e->tok));
3916 }
3917
3918 translate_base_ref (ctx, byte_size, signed_p, lvalue);
3919
3920 if (dwarf_hasattr_integrate (vardie, DW_AT_bit_offset))
3921 {
3922 Dwarf_Word bit_offset = 0;
3923 Dwarf_Word bit_size = 0;
3924 get_bitfield (e, vardie, byte_size, &bit_offset, &bit_size);
3925 translate_bitfield (ctx, byte_size, bit_offset,
3926 bit_size, signed_p);
3927 }
3928 }
3929 break;
3930
3931 case DW_TAG_array_type:
3932 case DW_TAG_pointer_type:
3933 case DW_TAG_reference_type:
3934 case DW_TAG_rvalue_reference_type:
3935 if (lvalue)
3936 {
3937 if (typetag == DW_TAG_array_type)
3938 throw SEMANTIC_ERROR (_("cannot write to array address"), e->tok);
3939 if (typetag == DW_TAG_reference_type ||
3940 typetag == DW_TAG_rvalue_reference_type)
3941 throw SEMANTIC_ERROR (_("cannot write to reference"), e->tok);
3942 assert (typetag == DW_TAG_pointer_type);
3943 }
3944 if (typetag != DW_TAG_array_type)
3945 translate_pointer (ctx, typedie, lvalue);
3946 break;
3947 }
3948 }
3949
3950 void
3951 dwflpp::translate_pointer(location_context &ctx, Dwarf_Die *typedie,
3952 bool lvalue)
3953 {
3954 // ??? We also get DW_TAG_array_type here.
3955 // assert (dwarf_tag (typedie) == DW_TAG_pointer_type ||
3956 // dwarf_tag (typedie) == DW_TAG_reference_type ||
3957 // dwarf_tag (typedie) == DW_TAG_rvalue_reference_type);
3958
3959 location *loc = ctx.locations.back ();
3960 if (loc->type != loc_implicit_pointer)
3961 {
3962 Dwarf_Word byte_size;
3963 if (dwarf_aggregate_size (typedie, &byte_size) != 0)
3964 throw SEMANTIC_ERROR (_F("cannot get size for type %s: %s",
3965 dwarf_diename (typedie) ?: "<anonymous>",
3966 dwarf_errmsg (-1)), ctx.e->tok);
3967
3968 /* We know this is a pointer, therefore the signedness is irrelevant. */
3969 translate_base_ref (ctx, byte_size, false, lvalue);
3970 /* We're going to want to dereference this pointer. Therefore note
3971 that this is an address. */
3972 loc = ctx.locations.back ();
3973 loc->type = loc_address;
3974 }
3975 }
3976
3977 Dwarf_Addr
3978 dwflpp::vardie_from_symtable (Dwarf_Die *vardie, Dwarf_Addr *addr)
3979 {
3980 const char *name = dwarf_linkage_name (vardie) ?: dwarf_diename (vardie);
3981
3982 if (sess.verbose > 2)
3983 clog << _F("finding symtable address for %s\n", name);
3984
3985 *addr = 0;
3986 int syms = dwfl_module_getsymtab (module);
3987 DWFL_ASSERT (_("Getting symbols"), syms >= 0);
3988
3989 for (int i = 0; *addr == 0 && i < syms; i++)
3990 {
3991 GElf_Sym sym;
3992 GElf_Word shndxp;
3993 const char *symname = dwfl_module_getsym(module, i, &sym, &shndxp);
3994 if (symname
3995 && ! strcmp (name, symname)
3996 && sym.st_shndx != SHN_UNDEF
3997 && (GELF_ST_TYPE (sym.st_info) == STT_NOTYPE // PR13284
3998 || GELF_ST_TYPE (sym.st_info) == STT_OBJECT))
3999 *addr = sym.st_value;
4000 }
4001
4002 // Don't relocate for the kernel, or kernel modules we handle those
4003 // specially in emit_address.
4004 if (dwfl_module_relocations (module) == 1 && module_name != TOK_KERNEL)
4005 dwfl_module_relocate_address (module, addr);
4006
4007 if (sess.verbose > 2)
4008 clog << _F("found %s @%#" PRIx64 "\n", name, *addr);
4009
4010 return *addr;
4011 }
4012
4013 bool
4014 dwflpp::literal_stmt_for_local (location_context &ctx,
4015 vector<Dwarf_Die>& scopes,
4016 string const & local,
4017 const target_symbol *e,
4018 bool lvalue,
4019 Dwarf_Die *die_mem)
4020 {
4021 Dwarf_Die vardie, funcdie;
4022 Dwarf_Attribute fb_attr_mem, *fb_attr = NULL;
4023
4024 // NB: when addr_loc is used for a synthesized DW_OP_addr below, then it
4025 // needs to remain valid until express_as_string() has finished with it.
4026 Dwarf_Op addr_loc;
4027
4028 fb_attr = find_variable_and_frame_base (scopes, ctx.pc, local, e,
4029 &vardie, &fb_attr_mem,
4030 &funcdie);
4031
4032 if (sess.verbose>2)
4033 {
4034 if (ctx.pc)
4035 clog << _F("finding location for local '%s' near address %#" PRIx64
4036 ", module bias %#" PRIx64 "\n", local.c_str(), ctx.pc,
4037 module_bias);
4038 else
4039 clog << _F("finding location for global '%s' in CU '%s'\n",
4040 local.c_str(), cu_name().c_str());
4041 }
4042
4043 /* Given $foo->bar->baz[NN], translate the location of foo. */
4044 Dwarf_Attribute attr_mem;
4045 ctx.attr = &attr_mem;
4046 ctx.fb_attr = fb_attr;
4047 ctx.dw = this;
4048 ctx.function = &funcdie;
4049
4050 if (dwarf_attr_integrate (&vardie, DW_AT_const_value, &attr_mem) == NULL
4051 && dwarf_attr_integrate (&vardie, DW_AT_location, &attr_mem) == NULL)
4052 {
4053 memset(&addr_loc, 0, sizeof(Dwarf_Op));
4054 addr_loc.atom = DW_OP_addr;
4055 // If it is an external variable try the symbol table. PR10622.
4056 if (dwarf_attr_integrate (&vardie, DW_AT_external, &attr_mem) != NULL
4057 && vardie_from_symtable (&vardie, &addr_loc.number) != 0)
4058 {
4059 ctx.translate_location (&addr_loc, 1, NULL);
4060 }
4061 else
4062 {
4063 string msg = _F("failed to retrieve location attribute for '%s' [man error::dwarf]", local.c_str());
4064 semantic_error err(ERR_SRC, msg, e->tok);
4065 err.details.push_back(die_location_as_string(&vardie));
4066 err.details.push_back(pc_location_as_function_string(ctx.pc));
4067 throw err;
4068 }
4069 }
4070 else
4071 translate_location (&ctx, &attr_mem, &vardie, ctx.pc, fb_attr, e, NULL);
4072
4073 /* Translate the ->bar->baz[NN] parts. */
4074
4075 Dwarf_Die typedie;
4076 if (dwarf_attr_die (&vardie, DW_AT_type, &typedie) == NULL)
4077 {
4078 string msg = _F("failed to retrieve type attribute for '%s' [man error::dwarf]", local.c_str());
4079 semantic_error err(ERR_SRC, msg, e->tok);
4080 err.details.push_back(die_location_as_string(&vardie));
4081 err.details.push_back(pc_location_as_function_string(ctx.pc));
4082 throw err;
4083 }
4084
4085 translate_components (&ctx, ctx.pc, e, &vardie, &typedie, lvalue, 0);
4086
4087 /* Translate the assignment part, either
4088 x = $foo->bar->baz[NN]
4089 or
4090 $foo->bar->baz[NN] = x
4091 */
4092 translate_final_fetch_or_store (ctx, &vardie, &typedie, lvalue, die_mem);
4093 return true;
4094 }
4095
4096 struct location *
4097 dwflpp::translate_call_site_value (location_context *ctx,
4098 Dwarf_Attribute *attr,
4099 Dwarf_Die *die,
4100 Dwarf_Die *funcdie,
4101 Dwarf_Addr pc)
4102 {
4103 Dwarf_Attribute fb_attr_mem, *fb_attr = NULL;
4104
4105 vector<Dwarf_Die> scopes = getscopes (funcdie);
4106 vector<Dwarf_Die> physcopes, *fbscopes = &scopes;
4107 int declaring_scope = 0;
4108 for (size_t inner = declaring_scope;
4109 inner < fbscopes->size() && fb_attr == NULL;
4110 ++inner)
4111 {
4112 Dwarf_Die& scope = (*fbscopes)[inner];
4113 switch (dwarf_tag (&scope))
4114 {
4115 default:
4116 continue;
4117 case DW_TAG_subprogram:
4118 fb_attr = dwarf_attr_integrate (&scope,
4119 DW_AT_frame_base,
4120 &fb_attr_mem);
4121 break;
4122 case DW_TAG_inlined_subroutine:
4123 if (declaring_scope != -1)
4124 {
4125 physcopes = getscopes_die(&scope);
4126 if (physcopes.empty())
4127 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
4128 ctx->e->sym_name().c_str()), ctx->e->tok);
4129 fbscopes = &physcopes;
4130 inner = 0;
4131 declaring_scope = -1;
4132 }
4133 break;
4134 }
4135 }
4136
4137 return translate_location (ctx, attr, die, pc, fb_attr, ctx->e, NULL);
4138 }
4139
4140 Dwarf_Die*
4141 dwflpp::type_die_for_local (vector<Dwarf_Die>& scopes,
4142 Dwarf_Addr pc,
4143 string const & local,
4144 const target_symbol *e,
4145 Dwarf_Die *typedie,
4146 bool lvalue)
4147 {
4148 Dwarf_Die vardie, funcdie;
4149 Dwarf_Attribute attr_mem;
4150
4151 find_variable_and_frame_base (scopes, pc, local, e, &vardie, &attr_mem, &funcdie);
4152
4153 if (dwarf_attr_die (&vardie, DW_AT_type, typedie) == NULL)
4154 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local.c_str()), e->tok);
4155
4156 location_context ctx(const_cast<target_symbol *>(e));
4157 ctx.pc = pc;
4158 ctx.dw = this;
4159 translate_components (&ctx, pc, e, &vardie, typedie, lvalue);
4160 return typedie;
4161 }
4162
4163
4164 bool
4165 dwflpp::literal_stmt_for_return (location_context &ctx,
4166 Dwarf_Die *scope_die,
4167 const target_symbol *e,
4168 bool lvalue,
4169 Dwarf_Die *die_mem)
4170 {
4171 if (sess.verbose>2)
4172 clog << _F("literal_stmt_for_return: finding return value for %s (%s)\n",
4173 (dwarf_diename(scope_die) ?: "<unknown>"), (dwarf_diename(cu) ?: "<unknown>"));
4174
4175 /* Given $return->bar->baz[NN], translate the location of return. */
4176 const Dwarf_Op *locops;
4177 int nlocops = dwfl_module_return_value_location (module, scope_die,
4178 &locops);
4179 if (nlocops < 0)
4180 throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)",
4181 (dwarf_diename(scope_die) ?: "<unknown>"),
4182 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
4183 // the function has no return value (e.g. "void" in C)
4184 else if (nlocops == 0)
4185 throw SEMANTIC_ERROR(_F("function %s (%s) has no return value",
4186 (dwarf_diename(scope_die) ?: "<unknown>"),
4187 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
4188
4189 ctx.translate_location (locops, nlocops, NULL);
4190
4191 /* Translate the ->bar->baz[NN] parts. */
4192
4193 Dwarf_Die vardie = *scope_die, typedie;
4194 if (dwarf_attr_die (&vardie, DW_AT_type, &typedie) == NULL)
4195 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4196 (dwarf_diename(&vardie) ?: "<unknown>"),
4197 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
4198
4199 translate_components (&ctx, ctx.pc, e, &vardie, &typedie, lvalue);
4200
4201 /* Translate the assignment part, either
4202 x = $return->bar->baz[NN]
4203 or
4204 $return->bar->baz[NN] = x
4205 */
4206 translate_final_fetch_or_store (ctx, &vardie, &typedie, lvalue, die_mem);
4207 return true;
4208 }
4209
4210 Dwarf_Die*
4211 dwflpp::type_die_for_return (Dwarf_Die *scope_die,
4212 Dwarf_Addr pc,
4213 const target_symbol *e,
4214 Dwarf_Die *typedie,
4215 bool lvalue)
4216 {
4217 Dwarf_Die vardie = *scope_die;
4218 if (dwarf_attr_die (&vardie, DW_AT_type, typedie) == NULL)
4219 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4220 (dwarf_diename(&vardie) ?: "<unknown>"),
4221 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
4222
4223 translate_components (NULL, pc, e, &vardie, typedie, lvalue);
4224 return typedie;
4225 }
4226
4227
4228 bool
4229 dwflpp::literal_stmt_for_pointer (location_context &ctx,
4230 Dwarf_Die *start_typedie,
4231 const target_symbol *e,
4232 bool lvalue,
4233 Dwarf_Die *die_mem)
4234 {
4235 if (sess.verbose>2)
4236 clog << _F("literal_stmt_for_pointer: finding value for %s (%s)\n",
4237 dwarf_type_name(start_typedie).c_str(), (dwarf_diename(cu) ?: "<unknown>"));
4238
4239 assert(ctx.pointer != NULL);
4240 location *tail = ctx.translate_argument (ctx.pointer);
4241
4242 /* Translate the ->bar->baz[NN] parts. */
4243
4244 unsigned first = 0;
4245 Dwarf_Die typedie = *start_typedie, vardie = typedie;
4246
4247 /* As a special case when typedie is not an array or pointer, we can
4248 * allow array indexing on STAP_ARG_pointer instead (since we do
4249 * know the pointee type and can determine its size). PR11556. */
4250 const target_symbol::component* c =
4251 e->components.empty() ? NULL : &e->components[0];
4252 if (c && (c->type == target_symbol::comp_literal_array_index ||
4253 c->type == target_symbol::comp_expression_array_index))
4254 {
4255 resolve_unqualified_inner_typedie (&typedie, &typedie, e);
4256 int typetag = dwarf_tag (&typedie);
4257 if (typetag != DW_TAG_pointer_type &&
4258 typetag != DW_TAG_array_type)
4259 {
4260 if (c->type == target_symbol::comp_literal_array_index)
4261 tail = ctx.translate_array_pointer (&typedie, tail,
4262 new literal_number (c->num_index));
4263 else
4264 tail = ctx.translate_array_pointer (&typedie, tail,
4265 ctx.indicies[0]);
4266 ++first;
4267 }
4268 }
4269
4270 /* Now translate the rest normally. */
4271 translate_components (&ctx, 0, e, &vardie, &typedie, lvalue, first);
4272
4273 /* Translate the assignment part, either
4274 x = (STAP_ARG_pointer)->bar->baz[NN]
4275 or
4276 (STAP_ARG_pointer)->bar->baz[NN] = x
4277 */
4278 translate_final_fetch_or_store (ctx, &vardie, &typedie, lvalue, die_mem);
4279 return true;
4280 }
4281
4282 Dwarf_Die*
4283 dwflpp::type_die_for_pointer (Dwarf_Die *start_typedie,
4284 const target_symbol *e,
4285 Dwarf_Die *typedie,
4286 bool lvalue)
4287 {
4288 unsigned first = 0;
4289 *typedie = *start_typedie;
4290 Dwarf_Die vardie = *typedie;
4291
4292 /* Handle the same PR11556 case as above. */
4293 const target_symbol::component* c =
4294 e->components.empty() ? NULL : &e->components[0];
4295 if (c && (c->type == target_symbol::comp_literal_array_index ||
4296 c->type == target_symbol::comp_expression_array_index))
4297 {
4298 resolve_unqualified_inner_typedie (typedie, typedie, e);
4299 int typetag = dwarf_tag (typedie);
4300 if (typetag != DW_TAG_pointer_type &&
4301 typetag != DW_TAG_array_type)
4302 ++first;
4303 }
4304
4305 location_context ctx(const_cast<target_symbol *>(e));
4306 ctx.dw = this;
4307 translate_components (&ctx, 0, e, &vardie, typedie, lvalue, first);
4308 return typedie;
4309 }
4310
4311
4312 static bool
4313 in_kprobes_function(systemtap_session& sess, Dwarf_Addr addr)
4314 {
4315 if (sess.sym_kprobes_text_start != 0 && sess.sym_kprobes_text_end != 0)
4316 {
4317 // If the probe point address is anywhere in the __kprobes
4318 // address range, we can't use this probe point.
4319 if (addr >= sess.sym_kprobes_text_start && addr < sess.sym_kprobes_text_end)
4320 return true;
4321 }
4322 return false;
4323 }
4324
4325
4326 enum dwflpp::blacklisted_type
4327 dwflpp::blacklisted_p(interned_string funcname,
4328 interned_string filename,
4329 int,
4330 interned_string module,
4331 Dwarf_Addr addr,
4332 bool has_return)
4333 {
4334 if (!blacklist_enabled)
4335 return dwflpp::blacklisted_none;
4336
4337 enum dwflpp::blacklisted_type blacklisted = dwflpp::blacklisted_none;
4338
4339 // check against section blacklist
4340 string section = get_blacklist_section(addr);
4341
4342 // PR6503: modules don't need special init/exit treatment
4343 if (module == TOK_KERNEL && !regexec (&blacklist_section, section.c_str(), 0, NULL, 0))
4344 blacklisted = dwflpp::blacklisted_section;
4345
4346 // Check for function marked '__kprobes'.
4347 else if (module == TOK_KERNEL && in_kprobes_function(sess, addr))
4348 blacklisted = dwflpp::blacklisted_kprobes;
4349
4350 // Check probe point against function blacklist
4351 else if (!regexec(&blacklist_func, funcname.to_string().c_str(), 0, NULL, 0))
4352 blacklisted = dwflpp::blacklisted_function;
4353
4354 // Check probe point against function return blacklist
4355 else if (has_return && !regexec(&blacklist_func_ret, funcname.to_string().c_str(), 0, NULL, 0))
4356 blacklisted = dwflpp::blacklisted_function_return;
4357
4358 // Check probe point against file blacklist
4359 else if (!regexec(&blacklist_file, filename.to_string().c_str(), 0, NULL, 0))
4360 blacklisted = dwflpp::blacklisted_file;
4361
4362 if (blacklisted)
4363 {
4364 if (sess.verbose>1)
4365 clog << _(" - blacklisted");
4366 if (sess.guru_mode)
4367 {
4368 blacklisted = dwflpp::blacklisted_none;
4369 if (sess.verbose>1)
4370 clog << _(" but not skipped (guru mode enabled)");
4371 }
4372 }
4373
4374 // This probe point is not blacklisted.
4375 return blacklisted;
4376 }
4377
4378
4379 void
4380 dwflpp::build_kernel_blacklist()
4381 {
4382 // We build up the regexps in these strings
4383
4384 // Add ^ anchors at the front; $ will be added just before regcomp.
4385
4386 string blfn = "^(";
4387 string blfn_ret = "^(";
4388 string blfile = "^(";
4389 string blsection = "^(";
4390
4391 blsection += "\\.init\\."; // first alternative, no "|"
4392 blsection += "|\\.exit\\.";
4393 blsection += "|\\.devinit\\.";
4394 blsection += "|\\.devexit\\.";
4395 blsection += "|\\.cpuinit\\.";
4396 blsection += "|\\.cpuexit\\.";
4397 blsection += "|\\.meminit\\.";
4398 blsection += "|\\.memexit\\.";
4399
4400 /* NOTE all include/asm .h blfile patterns might need "full path"
4401 so prefix those with '.*' - see PR13108 and PR13112. */
4402 blfile += "kernel/kprobes\\.c"; // first alternative, no "|"
4403 blfile += "|arch/.*/kernel/kprobes\\.c";
4404 blfile += "|.*/include/asm/io\\.h";
4405 blfile += "|.*/include/asm/io-defs\\.h";
4406 blfile += "|.*/include/asm/io_64\\.h";
4407 blfile += "|.*/include/asm/bitops\\.h";
4408 blfile += "|drivers/ide/ide-iops\\.c";
4409 // paravirt ops
4410 blfile += "|arch/.*/kernel/paravirt\\.c";
4411 blfile += "|.*/include/asm/paravirt\\.h";
4412
4413 // XXX: it would be nice if these blacklisted functions were pulled
4414 // in dynamically, instead of being statically defined here.
4415 // Perhaps it could be populated from script files. A "noprobe
4416 // kernel.function("...")" construct might do the trick.
4417
4418 // Most of these are marked __kprobes in newer kernels. We list
4419 // them here (anyway) so the translator can block them on older
4420 // kernels that don't have the __kprobes function decorator. This
4421 // also allows detection of problems at translate- rather than
4422 // run-time.
4423
4424 blfn += "atomic_notifier_call_chain"; // first blfn; no "|"
4425 blfn += "|default_do_nmi";
4426 blfn += "|__die";
4427 blfn += "|die_nmi";
4428 blfn += "|do_debug";
4429 blfn += "|do_general_protection";
4430 blfn += "|do_int3";
4431 blfn += "|do_IRQ";
4432 blfn += "|do_page_fault";
4433 blfn += "|do_sparc64_fault";
4434 blfn += "|do_trap";
4435 blfn += "|dummy_nmi_callback";
4436 blfn += "|flush_icache_range";
4437 blfn += "|ia64_bad_break";
4438 blfn += "|ia64_do_page_fault";
4439 blfn += "|ia64_fault";
4440 blfn += "|io_check_error";
4441 blfn += "|mem_parity_error";
4442 blfn += "|nmi_watchdog_tick";
4443 blfn += "|notifier_call_chain";
4444 blfn += "|oops_begin";
4445 blfn += "|oops_end";
4446 blfn += "|program_check_exception";
4447 blfn += "|single_step_exception";
4448 blfn += "|sync_regs";
4449 blfn += "|unhandled_fault";
4450 blfn += "|unknown_nmi_error";
4451 blfn += "|xen_[gs]et_debugreg";
4452 blfn += "|xen_irq_.*";
4453 blfn += "|xen_.*_fl_direct.*";
4454 blfn += "|check_events";
4455 blfn += "|xen_adjust_exception_frame";
4456 blfn += "|xen_iret.*";
4457 blfn += "|xen_sysret64.*";
4458 blfn += "|test_ti_thread_flag.*";
4459 blfn += "|inat_get_opcode_attribute";
4460 blfn += "|system_call_after_swapgs";
4461 blfn += "|HYPERVISOR_[gs]et_debugreg";
4462 blfn += "|HYPERVISOR_event_channel_op";
4463 blfn += "|hash_64";
4464 blfn += "|hash_ptr";
4465 blfn += "|native_set_pte";
4466
4467 // Lots of locks
4468 blfn += "|.*raw_.*_lock.*";
4469 blfn += "|.*raw_.*_unlock.*";
4470 blfn += "|.*raw_.*_trylock.*";
4471 blfn += "|.*read_lock.*";
4472 blfn += "|.*read_unlock.*";
4473 blfn += "|.*read_trylock.*";
4474 blfn += "|.*write_lock.*";
4475 blfn += "|.*write_unlock.*";
4476 blfn += "|.*write_trylock.*";
4477 blfn += "|.*write_seqlock.*";
4478 blfn += "|.*write_sequnlock.*";
4479 blfn += "|.*spin_lock.*";
4480 blfn += "|.*spin_unlock.*";
4481 blfn += "|.*spin_trylock.*";
4482 blfn += "|.*spin_is_locked.*";
4483 blfn += "|rwsem_.*lock.*";
4484 blfn += "|.*mutex_.*lock.*";
4485
4486 // atomic functions
4487 blfn += "|atomic_.*";
4488 blfn += "|atomic64_.*";
4489
4490 // few other problematic cases
4491 blfn += "|get_bh";
4492 blfn += "|put_bh";
4493
4494 // Experimental
4495 blfn += "|.*apic.*|.*APIC.*";
4496 blfn += "|.*softirq.*";
4497 blfn += "|.*IRQ.*";
4498 blfn += "|.*_intr.*";
4499 blfn += "|__delay";
4500 blfn += "|.*kernel_text.*";
4501 blfn += "|get_current";
4502 blfn += "|current_.*";
4503 blfn += "|.*exception_tables.*";
4504 blfn += "|.*setup_rt_frame.*";
4505
4506 // PR 5759, CONFIG_PREEMPT kernels
4507 blfn += "|.*preempt_count.*";
4508 blfn += "|preempt_schedule";
4509
4510 // These functions don't return, so return probes would never be recovered
4511 blfn_ret += "do_exit"; // no "|"
4512 blfn_ret += "|sys_exit";
4513 blfn_ret += "|sys_exit_group";
4514
4515 // __switch_to changes "current" on x86_64 and i686, so return probes
4516 // would cause kernel panic, and it is marked as "__kprobes" on x86_64
4517 if (sess.architecture == "x86_64")
4518 blfn += "|__switch_to";
4519 if (sess.architecture == "i686")
4520 blfn_ret += "|__switch_to";
4521
4522 // RHEL6 pre-beta 2.6.32-19.el6
4523 blfn += "|special_mapping_.*";
4524 blfn += "|.*_pte_.*"; // or "|smaps_pte_range";
4525 blfile += "|fs/seq_file\\.c";
4526
4527 blfn += ")$";
4528 blfn_ret += ")$";
4529 blfile += ")$";
4530 blsection += ")"; // NB: no $, sections match just the beginning
4531
4532 if (sess.verbose > 2)
4533 {
4534 clog << _("blacklist regexps:") << endl;
4535 clog << "blfn: " << blfn << endl;
4536 clog << "blfn_ret: " << blfn_ret << endl;
4537 clog << "blfile: " << blfile << endl;
4538 clog << "blsection: " << blsection << endl;
4539 }
4540
4541 int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED);
4542 if (rc) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4543 rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED);
4544 if (rc) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4545 rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED);
4546 if (rc) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4547 rc = regcomp (& blacklist_section, blsection.c_str(), REG_NOSUB|REG_EXTENDED);
4548 if (rc) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4549
4550 blacklist_enabled = true;
4551 }
4552
4553
4554 void
4555 dwflpp::build_user_blacklist()
4556 {
4557 // We build up the regexps in these strings
4558
4559 // Add ^ anchors at the front; $ will be added just before regcomp.
4560
4561 string blfn = "^(";
4562 string blfn_ret = "^(";
4563 string blfile = "^(";
4564 string blsection = "^(";
4565
4566 // Non-matching placeholders until we have real things to match
4567 blfn += ".^";
4568 blfile += ".^";
4569 blsection += ".^";
4570
4571 // These functions don't use the normal function-entry ABI, so can't be .return probed safely
4572 blfn_ret += "_start";
4573
4574 blfn += ")$";
4575 blfn_ret += ")$";
4576 blfile += ")$";
4577 blsection += ")"; // NB: no $, sections match just the beginning
4578
4579 if (sess.verbose > 2)
4580 {
4581 clog << _("blacklist regexps:") << endl;
4582 clog << "blfn: " << blfn << endl;
4583 clog << "blfn_ret: " << blfn_ret << endl;
4584 clog << "blfile: " << blfile << endl;
4585 clog << "blsection: " << blsection << endl;
4586 }
4587
4588 int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED);
4589 if (rc) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4590 rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED);
4591 if (rc) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4592 rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED);
4593 if (rc) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4594 rc = regcomp (& blacklist_section, blsection.c_str(), REG_NOSUB|REG_EXTENDED);
4595 if (rc) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4596
4597 blacklist_enabled = true;
4598 }
4599
4600
4601 string
4602 dwflpp::get_blacklist_section(Dwarf_Addr addr)
4603 {
4604 string blacklist_section;
4605 Dwarf_Addr bias;
4606 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
4607 // because dwfl_module_getelf can force costly section relocations
4608 // we don't really need, while either will do for this purpose.
4609 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (module, &bias))
4610 ?: dwfl_module_getelf (module, &bias));
4611
4612 Dwarf_Addr offset = addr - bias;
4613 if (elf)
4614 {
4615 Elf_Scn* scn = 0;
4616 size_t shstrndx;
4617 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
4618 while ((scn = elf_nextscn (elf, scn)) != NULL)
4619 {
4620 GElf_Shdr shdr_mem;
4621 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4622 if (! shdr)
4623 continue; // XXX error?
4624
4625 if (!(shdr->sh_flags & SHF_ALLOC))
4626 continue;
4627
4628 GElf_Addr start = shdr->sh_addr;
4629 GElf_Addr end = start + shdr->sh_size;
4630 if (! (offset >= start && offset < end))
4631 continue;
4632
4633 blacklist_section = elf_strptr (elf, shstrndx, shdr->sh_name);
4634 break;
4635 }
4636 }
4637 return blacklist_section;
4638 }
4639
4640
4641 /* Find the section named 'section_name' in the current module
4642 * returning the section header using 'shdr_mem' */
4643
4644 GElf_Shdr *
4645 dwflpp::get_section(string section_name, GElf_Shdr *shdr_mem, Elf **elf_ret)
4646 {
4647 GElf_Shdr *shdr = NULL;
4648 Elf* elf;
4649 Dwarf_Addr bias;
4650 size_t shstrndx;
4651
4652 // Explicitly look in the main elf file first.
4653 elf = dwfl_module_getelf (module, &bias);
4654 Elf_Scn *probe_scn = NULL;
4655
4656 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
4657
4658 bool have_section = false;
4659
4660 while ((probe_scn = elf_nextscn (elf, probe_scn)))
4661 {
4662 shdr = gelf_getshdr (probe_scn, shdr_mem);
4663 assert (shdr != NULL);
4664
4665 if (elf_strptr (elf, shstrndx, shdr->sh_name) == section_name)
4666 {
4667 have_section = true;
4668 break;
4669 }
4670 }
4671
4672 // Older versions may put the section in the debuginfo dwarf file,
4673 // so check if it actually exists, if not take a look in the debuginfo file
4674 if (! have_section || (have_section && shdr->sh_type == SHT_NOBITS))
4675 {
4676 elf = dwarf_getelf (dwfl_module_getdwarf (module, &bias));
4677 if (! elf)
4678 return NULL;
4679 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
4680 probe_scn = NULL;
4681 while ((probe_scn = elf_nextscn (elf, probe_scn)))
4682 {
4683 shdr = gelf_getshdr (probe_scn, shdr_mem);
4684 if (elf_strptr (elf, shstrndx, shdr->sh_name) == section_name)
4685 {
4686 have_section = true;
4687 break;
4688 }
4689 }
4690 }
4691
4692 if (!have_section)
4693 return NULL;
4694
4695 if (elf_ret)
4696 *elf_ret = elf;
4697 return shdr;
4698 }
4699
4700
4701 Dwarf_Addr
4702 dwflpp::relocate_address(Dwarf_Addr dw_addr, interned_string& reloc_section)
4703 {
4704 // PR10273
4705 // libdw address, so adjust for bias gotten from dwfl_module_getdwarf
4706 Dwarf_Addr reloc_addr = dw_addr + module_bias;
4707 if (!module)
4708 {
4709 assert(module_name == TOK_KERNEL);
4710 reloc_section = "";
4711 }
4712 else if (dwfl_module_relocations (module) > 0)
4713 {
4714 // This is a relocatable module; libdwfl already knows its
4715 // sections, so we can relativize addr.
4716 int idx = dwfl_module_relocate_address (module, &reloc_addr);
4717 const char* r_s = dwfl_module_relocation_info (module, idx, NULL);
4718 if (r_s)
4719 reloc_section = r_s;
4720
4721 if (reloc_section == "" && dwfl_module_relocations (module) == 1)
4722 reloc_section = ".dynamic";
4723 }
4724 else
4725 reloc_section = ".absolute";
4726 return reloc_addr;
4727 }
4728
4729 /* Returns the call frame address operations for the given program counter
4730 * in the libdw address space.
4731 */
4732 Dwarf_Op *
4733 dwflpp::get_cfa_ops (Dwarf_Addr pc)
4734 {
4735 Dwarf_Op *cfa_ops = NULL;
4736
4737 if (sess.verbose > 2)
4738 clog << "get_cfa_ops @0x" << hex << pc << dec
4739 << ", module_start @0x" << hex << module_start << dec << endl;
4740
4741 // Try debug_frame first, then fall back on eh_frame.
4742 size_t cfa_nops = 0;
4743 Dwarf_Addr bias = 0;
4744 Dwarf_Frame *frame = NULL;
4745 Dwarf_CFI *cfi = dwfl_module_dwarf_cfi (module, &bias);
4746 if (cfi != NULL)
4747 {
4748 if (sess.verbose > 3)
4749 clog << "got dwarf cfi bias: 0x" << hex << bias << dec << endl;
4750 if (dwarf_cfi_addrframe (cfi, pc - bias, &frame) == 0)
4751 dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops);
4752 else if (sess.verbose > 3)
4753 clog << "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl;
4754 }
4755 else if (sess.verbose > 3)
4756 clog << "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl;
4757
4758 if (cfa_ops == NULL)
4759 {
4760 cfi = dwfl_module_eh_cfi (module, &bias);
4761 if (cfi != NULL)
4762 {
4763 if (sess.verbose > 3)
4764 clog << "got eh cfi bias: 0x" << hex << bias << dec << endl;
4765 Dwarf_Frame *frame = NULL;
4766 if (dwarf_cfi_addrframe (cfi, pc - bias, &frame) == 0)
4767 dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops);
4768 else if (sess.verbose > 3)
4769 clog << "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl;
4770 }
4771 else if (sess.verbose > 3)
4772 clog << "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl;
4773
4774 }
4775
4776 if (sess.verbose > 2)
4777 {
4778 if (cfa_ops == NULL)
4779 clog << _("not found cfa") << endl;
4780 else
4781 {
4782 Dwarf_Addr frame_start, frame_end;
4783 bool frame_signalp;
4784 int info = dwarf_frame_info (frame, &frame_start, &frame_end,
4785 &frame_signalp);
4786 clog << _F("found cfa, info: %d [start: %#" PRIx64 ", end: %#" PRIx64
4787 ", nops: %zu", info, frame_start, frame_end, cfa_nops) << endl;
4788 }
4789 }
4790
4791 return cfa_ops;
4792 }
4793
4794 int
4795 dwflpp::add_module_build_id_to_hash (Dwfl_Module *m,
4796 void **userdata __attribute__ ((unused)),
4797 const char *name,
4798 Dwarf_Addr,
4799 void *arg)
4800 {
4801 string modname = name;
4802 systemtap_session * s = (systemtap_session *)arg;
4803 if (pending_interrupts)
4804 return DWARF_CB_ABORT;
4805
4806 // Extract the build ID
4807 const unsigned char *bits;
4808 GElf_Addr vaddr;
4809 int bits_length = dwfl_module_build_id(m, &bits, &vaddr);
4810 if(bits_length > 0)
4811 {
4812 // Convert the binary bits to a hex string
4813 string hex = hex_dump(bits, bits_length);
4814
4815 // Store the build ID in the session
4816 s->build_ids.push_back(hex);
4817 }
4818
4819 return DWARF_CB_OK;
4820 }
4821
4822
4823 static int
4824 cu_entry_pc_caching_callback (Dwarf_Die *func, pair<dwflpp&, entry_pc_cache_t&> *data)
4825 {
4826 auto& dw = data->first;
4827 auto& cache = data->second;
4828
4829 Dwarf_Addr entry_pc;
4830 if (dw.die_entrypc (func, &entry_pc))
4831 cache.insert (entry_pc);
4832 return DWARF_CB_OK;
4833 }
4834
4835 bool
4836 dwflpp::check_cu_entry_pc (Dwarf_Die *cu, Dwarf_Addr pc)
4837 {
4838 auto& entry_pcs = cu_entry_pc_cache[cu->addr];
4839 if (!entry_pcs)
4840 {
4841 save_and_restore<Dwarf_Die*> saved_cu(&this->cu, cu);
4842 entry_pcs = new entry_pc_cache_t;
4843 pair<dwflpp&, entry_pc_cache_t&> data (*this, *entry_pcs);
4844 int rc = iterate_over_functions (cu_entry_pc_caching_callback, &data, "*");
4845 if (rc != DWARF_CB_OK)
4846 return false;
4847 }
4848
4849 return entry_pcs->count(pc) != 0;
4850 }
4851
4852
4853 // Perform PR15123 heuristic for given variable at given address.
4854 // Return alternate pc address to do location-list lookup at, or 0 if
4855 // inapplicable.
4856 //
4857 Dwarf_Addr
4858 dwflpp::pr15123_retry_addr (Dwarf_Addr pc, Dwarf_Die* die)
4859 {
4860 // For PR15123, we'd like to detect the situation where the
4861 // incoming PC may point to a couple-of-byte instruction
4862 // sequence that gcc emits for CFLAGS=-mfentry, and where
4863 // context variables are in fact available throughout, *but* due
4864 // to the bug, the dwarf debuginfo location-list only starts a
4865 // few instructions later. Prologue searching does not resolve
4866 // this as a line-record is in place at the -mfentry prologue.
4867 //
4868 // Detecting this is complicated because ...
4869 // - we only want to do this if -mfentry was actually used
4870 // - if <pc> points to the a function entry point
4871 // - if the architecture is familiar enough that we can have a
4872 // hard-coded constant to skip over the prologue.
4873 //
4874 // Otherwise, we could give a false-positive - return corrupted
4875 // data.
4876 //
4877 // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches
4878 // was used. Without it, set the PR15123_ASSUME_MFENTRY environment
4879 // variable to override the -mfentry test.
4880
4881 if (getenv ("PR15123_DISABLE"))
4882 return 0;
4883
4884 Dwarf_Die cudie;
4885 dwarf_diecu (die, &cudie, NULL, NULL);
4886
4887 if (!getenv ("PR15123_ASSUME_MFENTRY")) {
4888 string producer, version;
4889 if (!is_gcc_producer(&cudie, producer, version))
4890 return 0;
4891 if (producer.find("-mfentry") == string::npos)
4892 return 0;
4893 }
4894
4895 // Determine if this pc maps to the beginning of a
4896 // real function (not some inlined doppelganger. This
4897 // is made tricker by this->function may not be
4898 // pointing at the right DIE (say e.g. stap encountered
4899 // the inlined copy first, so was focus_on_function'd).
4900 if (!check_cu_entry_pc (&cudie, pc))
4901 return 0; // (will fail on retry, so we won't loop more than once)
4902
4903 if (sess.architecture == "i386" ||
4904 sess.architecture == "x86_64") {
4905 /* pull the trigger */
4906 if (sess.verbose > 2)
4907 clog << _("retrying variable location-list lookup at address pc+5\n");
4908 return pc + 5;
4909 }
4910
4911 return 0;
4912 }
4913
4914 bool
4915 dwflpp::has_gnu_debugdata ()
4916 {
4917 Dwarf_Addr load_addr;
4918 // Note we really want the actual elf file, not the dwarf .debug file.
4919 Elf* elf = dwfl_module_getelf (module, &load_addr);
4920 size_t shstrndx;
4921 assert (elf_getshdrstrndx (elf, &shstrndx) >= 0);
4922
4923 // Get the gnu_debugdata section header
4924 Elf_Scn *scn = NULL;
4925 GElf_Shdr *gnu_debugdata_shdr = NULL;
4926 GElf_Shdr gnu_debugdata_shdr_mem;
4927 while ((scn = elf_nextscn (elf, scn)))
4928 {
4929 gnu_debugdata_shdr = gelf_getshdr (scn, &gnu_debugdata_shdr_mem);
4930 assert (gnu_debugdata_shdr != NULL);
4931 if (strcmp (elf_strptr (elf, shstrndx, gnu_debugdata_shdr->sh_name), ".gnu_debugdata") == 0)
4932 return true;
4933 }
4934 return false;
4935 }
4936
4937 // If not GCC, return false. Otherwise, return true and set vers.
4938 bool
4939 dwflpp::is_gcc_producer(Dwarf_Die *cudie, string& producer, string& version)
4940 {
4941 Dwarf_Attribute producer_attr;
4942 if (!dwarf_attr_integrate(cudie, DW_AT_producer, &producer_attr))
4943 return false;
4944
4945 // GNU {C|C++|...} x.x.x YYYYMMDD ...
4946 const char *cproducer = dwarf_formstring(&producer_attr);
4947 if (!cproducer)
4948 return false;
4949 producer = cproducer;
4950
4951 vector<string> tokens;
4952 tokenize(producer, tokens);
4953
4954 if (tokens.size() < 3
4955 || tokens[0] != "GNU"
4956 || tokens[2].find_first_not_of(".0123456789") != string::npos)
4957 return false;
4958
4959 version = tokens[2];
4960 return true;
4961 }
4962
4963 static bool
4964 die_has_loclist(Dwarf_Die *begin_die)
4965 {
4966 Dwarf_Die die;
4967 Dwarf_Attribute loc;
4968
4969 if (dwarf_child(begin_die, &die) != 0)
4970 return false;
4971
4972 do
4973 {
4974 switch (dwarf_tag(&die))
4975 {
4976 case DW_TAG_formal_parameter:
4977 case DW_TAG_variable:
4978 if (dwarf_attr_integrate(&die, DW_AT_location, &loc)
4979 && dwarf_whatform(&loc) == DW_FORM_sec_offset)
4980 return true;
4981 break;
4982 default:
4983 if (dwarf_haschildren (&die))
4984 if (die_has_loclist(&die))
4985 return true;
4986 break;
4987 }
4988 }
4989 while (dwarf_siblingof (&die, &die) == 0);
4990
4991 return false;
4992 }
4993
4994 bool
4995 dwflpp::has_valid_locs ()
4996 {
4997 assert(cu);
4998
4999 // The current CU has valid location info (implying we do not need to skip the
5000 // prologue) if
5001 // - it was compiled with -O2 -g (in which case, GCC outputs proper location
5002 // info for the prologue), and
5003 // - it was compiled by GCC >= 4.5 (previous versions could have had invalid
5004 // debug info in the prologue, see GDB's PR13777)
5005 // Note that clang behaves similarly to GCC here: non-optimized code does not
5006 // have location lists, while optimized code does. In the check below, even if
5007 // the producer is not GCC, we assume that it is valid to do the loclist check
5008 // afterwards (which it is for clang).
5009
5010 string prod, vers;
5011 if (is_gcc_producer(cu, prod, vers)
5012 && strverscmp(vers.c_str(), "4.5") < 0)
5013 return false;
5014
5015 // We determine if the current CU has been optimized with -O2 -g by looking
5016 // for any data objects whose DW_AT_location is a location list. This is also
5017 // how GDB determines whether to skip the prologue or not. See GDB's PR12573
5018 // and also RHBZ612253#c6.
5019 if (!die_has_loclist(cu))
5020 return false;
5021
5022 if (sess.verbose > 2)
5023 clog << _F("CU '%s' in module '%s' has valid locs",
5024 cu_name().c_str(), module_name.c_str()) << endl;
5025
5026 return true;
5027 }
5028
5029 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.262952 seconds and 5 git commands to generate.