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