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