]> sourceware.org Git - systemtap.git/blob - dwflpp.cxx
Fixed systemtap-server.exp
[systemtap.git] / dwflpp.cxx
1 // C++ interface to dwfl
2 // Copyright (C) 2005-2014 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 )
1291 {
1292 sess.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s",
1293 module_name.c_str(), interpreter.c_str()));
1294 return;
1295 }
1296
1297 vector<string> ldd_command;
1298 ldd_command.push_back("/usr/bin/env");
1299 ldd_command.push_back("LD_TRACE_LOADED_OBJECTS=1");
1300 ldd_command.push_back("LD_WARN=yes");
1301 ldd_command.push_back("LD_BIND_NOW=yes");
1302 ldd_command.push_back(interpreter);
1303 ldd_command.push_back(module_name);
1304
1305 FILE *fp;
1306 int child_fd;
1307 pid_t child = stap_spawn_piped(sess.verbose, ldd_command, NULL, &child_fd);
1308 if (child <= 0 || !(fp = fdopen(child_fd, "r")))
1309 clog << _F("library iteration on %s failed: %s",
1310 module_name.c_str(), strerror(errno)) << endl;
1311 else
1312 {
1313 while (1) // this parsing loop borrowed from add_unwindsym_ldd
1314 {
1315 char linebuf[256];
1316 char *soname = 0;
1317 char *shlib = 0;
1318 unsigned long int addr = 0;
1319
1320 char *line = fgets (linebuf, 256, fp);
1321 if (line == 0) break; // EOF or error
1322
1323 #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)
1324 #define MS_FMT "%ms"
1325 #else
1326 #define MS_FMT "%as"
1327 #endif
1328 // Try soname => shlib (0xaddr)
1329 int nf = sscanf (line, MS_FMT " => " MS_FMT " (0x%lx)",
1330 &soname, &shlib, &addr);
1331 if (nf != 3 || shlib[0] != '/')
1332 {
1333 // Try shlib (0xaddr)
1334 nf = sscanf (line, " " MS_FMT " (0x%lx)", &shlib, &addr);
1335 if (nf != 2 || shlib[0] != '/')
1336 continue; // fewer than expected fields, or bad shlib.
1337 }
1338
1339 if (added.find (shlib) == added.end())
1340 {
1341 if (sess.verbose > 2)
1342 {
1343 clog << _F("Added -d '%s", shlib);
1344 if (nf == 3)
1345 clog << _F("' due to '%s'", soname);
1346 else
1347 clog << "'";
1348 clog << endl;
1349 }
1350 added.insert (shlib);
1351 }
1352
1353 free (soname);
1354 free (shlib);
1355 }
1356 if ((fclose(fp) || stap_waitpid(sess.verbose, child)))
1357 sess.print_warning("failed to read libraries from " + module_name + ": " + strerror(errno));
1358 }
1359
1360 for (std::set<std::string>::iterator it = added.begin();
1361 it != added.end();
1362 it++)
1363 {
1364 string modname = *it;
1365 (callback) (data, modname.c_str());
1366 }
1367 }
1368
1369
1370 /* For each plt section in the current module call 'callback', pass the plt entry
1371 * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */
1372
1373 template<> int
1374 dwflpp::iterate_over_plt<void>(void *object, void (*callback)(void*,
1375 const char*,
1376 size_t))
1377 {
1378 Dwarf_Addr load_addr;
1379 // Note we really want the actual elf file, not the dwarf .debug file.
1380 Elf* elf = dwfl_module_getelf (module, &load_addr);
1381 size_t shstrndx;
1382 assert (elf_getshdrstrndx (elf, &shstrndx) >= 0);
1383
1384 // Get the load address
1385 for (int i = 0; ; i++)
1386 {
1387 GElf_Phdr mem;
1388 GElf_Phdr *phdr;
1389 phdr = gelf_getphdr (elf, i, &mem);
1390 if (phdr == NULL)
1391 break;
1392 if (phdr->p_type == PT_LOAD)
1393 {
1394 load_addr = phdr->p_vaddr;
1395 break;
1396 }
1397 }
1398
1399 // Get the plt section header
1400 Elf_Scn *scn = NULL;
1401 GElf_Shdr *plt_shdr = NULL;
1402 GElf_Shdr plt_shdr_mem;
1403 while ((scn = elf_nextscn (elf, scn)))
1404 {
1405 GElf_Shdr *shdr = gelf_getshdr (scn, &plt_shdr_mem);
1406 assert (shdr != NULL);
1407 if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".plt") == 0)
1408 {
1409 plt_shdr = shdr;
1410 break;
1411 }
1412 }
1413 if (plt_shdr == NULL)
1414 return 0;
1415
1416 // Layout of the plt section
1417 int plt0_entry_size;
1418 int plt_entry_size;
1419 GElf_Ehdr ehdr_mem;
1420 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
1421 switch (em->e_machine)
1422 {
1423 case EM_386: plt0_entry_size = 16; plt_entry_size = 16; break;
1424 case EM_X86_64: plt0_entry_size = 16; plt_entry_size = 16; break;
1425 case EM_ARM: plt0_entry_size = 20; plt_entry_size = 12; break;
1426 case EM_AARCH64:plt0_entry_size = 32; plt_entry_size = 16; break;
1427 case EM_PPC64:
1428 case EM_S390:
1429 case EM_PPC:
1430 default:
1431 throw SEMANTIC_ERROR(".plt is not supported on this architecture");
1432 }
1433
1434 scn = NULL;
1435 while ((scn = elf_nextscn (elf, scn)))
1436 {
1437 GElf_Shdr shdr_mem;
1438 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1439 bool have_rela = false;
1440 bool have_rel = false;
1441
1442 if (shdr == NULL)
1443 continue;
1444 assert (shdr != NULL);
1445
1446 if ((have_rela = (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".rela.plt") == 0))
1447 || (have_rel = (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".rel.plt") == 0)))
1448 {
1449 /* Get the data of the section. */
1450 Elf_Data *data = elf_getdata (scn, NULL);
1451 assert (data != NULL);
1452 /* Get the symbol table information. */
1453 Elf_Scn *symscn = elf_getscn (elf, shdr->sh_link);
1454 GElf_Shdr symshdr_mem;
1455 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1456 assert (symshdr != NULL);
1457 Elf_Data *symdata = elf_getdata (symscn, NULL);
1458 assert (symdata != NULL);
1459
1460 unsigned int nsyms = shdr->sh_size / shdr->sh_entsize;
1461
1462 for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
1463 {
1464 GElf_Ehdr ehdr_mem;
1465 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
1466 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
1467
1468 GElf_Rela relamem;
1469 GElf_Rela *rela = NULL;
1470 GElf_Rel relmem;
1471 GElf_Rel *rel = NULL;
1472 if (have_rela)
1473 {
1474 rela = gelf_getrela (data, cnt, &relamem);
1475 assert (rela != NULL);
1476 }
1477 else if (have_rel)
1478 {
1479 rel = gelf_getrel (data, cnt, &relmem);
1480 assert (rel != NULL);
1481 }
1482 GElf_Sym symmem;
1483 Elf32_Word xndx;
1484 Elf_Data *xndxdata = NULL;
1485 GElf_Sym *sym =
1486 gelf_getsymshndx (symdata, xndxdata,
1487 GELF_R_SYM (have_rela ? rela->r_info : rel->r_info),
1488 &symmem, &xndx);
1489 assert (sym != NULL);
1490 Dwarf_Addr addr = plt_shdr->sh_offset + plt0_entry_size + cnt * plt_entry_size;
1491
1492 if (elf_strptr (elf, symshdr->sh_link, sym->st_name))
1493 (*callback) (object, elf_strptr (elf, symshdr->sh_link, sym->st_name), addr + load_addr);
1494 }
1495 break; // while scn
1496 }
1497 }
1498 return 0;
1499 }
1500
1501
1502 // Comparator function for sorting
1503 static bool
1504 compare_lines(Dwarf_Line* a, Dwarf_Line* b)
1505 {
1506 if (a == b)
1507 return false;
1508
1509 int lineno_a = DWARF_LINENO(a);
1510 int lineno_b = DWARF_LINENO(b);
1511 if (lineno_a == lineno_b)
1512 return DWARF_LINEADDR(a) < DWARF_LINEADDR(b);
1513 return lineno_a < lineno_b;
1514 }
1515
1516 // Comparator object for searching Dwarf_Lines with a specific lineno when we
1517 // don't have a Dwarf_Line of our own to search for (hence why a or b is always
1518 // NULL).
1519 struct lineno_comparator {
1520 int lineno;
1521 lineno_comparator(int lineno): lineno(lineno) {}
1522 bool operator() (Dwarf_Line* a, Dwarf_Line* b)
1523 {
1524 assert(a || b);
1525 if (a)
1526 return DWARF_LINENO(a) < lineno;
1527 else
1528 return lineno < DWARF_LINENO(b);
1529 }
1530 };
1531
1532 // Returns a range of lines in between begin and end with wanted lineno. If
1533 // none exist, points to where it would have been.
1534 static lines_range_t
1535 lineno_equal_range(lines_t* v, int lineno)
1536 {
1537 lineno_comparator lc(lineno);
1538 return equal_range(v->begin(), v->end(), (Dwarf_Line*)NULL, lc);
1539 }
1540
1541 // Interface to CU lines cache sorted by lineno
1542 lines_t*
1543 dwflpp::get_cu_lines_sorted_by_lineno(const char *srcfile)
1544 {
1545 assert(cu);
1546
1547 srcfile_lines_cache_t *srcfile_lines = cu_lines_cache[cu];
1548 if (!srcfile_lines)
1549 {
1550 srcfile_lines = new srcfile_lines_cache_t();
1551 cu_lines_cache[cu] = srcfile_lines;
1552 }
1553
1554 lines_t *lines = (*srcfile_lines)[srcfile];
1555 if (!lines)
1556 {
1557 size_t nlines_cu = 0;
1558 Dwarf_Lines *lines_cu = NULL;
1559 DWARF_ASSERT("dwarf_getsrclines",
1560 dwarf_getsrclines(cu, &lines_cu, &nlines_cu));
1561
1562 lines = new lines_t();
1563 (*srcfile_lines)[srcfile] = lines;
1564
1565 for (size_t i = 0; i < nlines_cu; i++)
1566 {
1567 Dwarf_Line *line = dwarf_onesrcline(lines_cu, i);
1568 const char *linesrc = DWARF_LINESRC(line);
1569 if (strcmp(srcfile, linesrc))
1570 continue;
1571 lines->push_back(line);
1572 }
1573
1574 if (lines->size() > 1)
1575 sort(lines->begin(), lines->end(), compare_lines);
1576
1577 if (sess.verbose > 3)
1578 {
1579 clog << _F("found the following lines for %s:", srcfile) << endl;
1580 lines_t::iterator i;
1581 for (i = lines->begin(); i != lines->end(); ++i)
1582 cout << DWARF_LINENO(*i) << " = " << hex
1583 << DWARF_LINEADDR(*i) << dec << endl;
1584 }
1585 }
1586 return lines;
1587 }
1588
1589 static Dwarf_Line*
1590 get_func_first_line(Dwarf_Die *cu, base_func_info& func)
1591 {
1592 // dwarf_getsrc_die() uses binary search to find the Dwarf_Line, but will
1593 // return the wrong line if not found.
1594 Dwarf_Line *line = dwarf_getsrc_die(cu, func.entrypc);
1595 if (line && DWARF_LINEADDR(line) == func.entrypc)
1596 return line;
1597
1598 // Line not found (or line at wrong addr). We have to resort to a slower
1599 // linear method. We won't find an exact match (probably this is an inlined
1600 // instance), so just settle on the first Dwarf_Line with lowest addr which
1601 // falls in the die.
1602 size_t nlines = 0;
1603 Dwarf_Lines *lines = NULL;
1604 DWARF_ASSERT("dwarf_getsrclines",
1605 dwarf_getsrclines(cu, &lines, &nlines));
1606
1607 for (size_t i = 0; i < nlines; i++)
1608 {
1609 line = dwarf_onesrcline(lines, i);
1610 if (dwarf_haspc(&func.die, DWARF_LINEADDR(line)))
1611 return line;
1612 }
1613 return NULL;
1614 }
1615
1616 static lines_t
1617 collect_lines_in_die(lines_range_t range, Dwarf_Die *die)
1618 {
1619 lines_t lines_in_die;
1620 for (lines_t::iterator line = range.first;
1621 line != range.second; ++line)
1622 if (dwarf_haspc(die, DWARF_LINEADDR(*line)))
1623 lines_in_die.push_back(*line);
1624 return lines_in_die;
1625 }
1626
1627 static void
1628 add_matching_lines_in_func(Dwarf_Die *cu,
1629 lines_t *cu_lines,
1630 base_func_info& func,
1631 lines_t& matching_lines)
1632 {
1633 Dwarf_Line *start_line = get_func_first_line(cu, func);
1634 if (!start_line)
1635 return;
1636
1637 for (int lineno = DWARF_LINENO(start_line);;)
1638 {
1639 lines_range_t range = lineno_equal_range(cu_lines, lineno);
1640
1641 // We consider the lineno still part of the die if at least one of them
1642 // falls in the die.
1643 lines_t lines_in_die = collect_lines_in_die(range, &func.die);
1644 if (lines_in_die.empty())
1645 break;
1646
1647 // Just pick the first LR even if there are more than one. Since the lines
1648 // are sorted by lineno and then addr, the first one is the one with the
1649 // lowest addr.
1650 matching_lines.push_back(lines_in_die[0]);
1651
1652 // break out if there are no more lines, otherwise, go to the next lineno
1653 if (range.second == cu_lines->end())
1654 break;
1655 lineno = DWARF_LINENO(*range.second);
1656 }
1657 }
1658
1659 void
1660 dwflpp::collect_all_lines(char const * srcfile,
1661 base_func_info_map_t& funcs,
1662 lines_t& matching_lines)
1663 {
1664 // This is where we handle WILDCARD lineno types.
1665 lines_t *cu_lines = get_cu_lines_sorted_by_lineno(srcfile);
1666 for (base_func_info_map_t::iterator func = funcs.begin();
1667 func != funcs.end(); ++func)
1668 add_matching_lines_in_func(cu, cu_lines, *func, matching_lines);
1669 }
1670
1671
1672 static void
1673 add_matching_line_in_die(lines_t *cu_lines,
1674 lines_t& matching_lines,
1675 Dwarf_Die *die, int lineno)
1676 {
1677 lines_range_t lineno_range = lineno_equal_range(cu_lines, lineno);
1678 lines_t lines_in_die = collect_lines_in_die(lineno_range, die);
1679 if (lines_in_die.empty())
1680 return;
1681
1682 // Even if there are more than 1 LRs, just pick the first one. Since the lines
1683 // are sorted by lineno and then addr, the first one is the one with the
1684 // lowest addr. This is similar to what GDB does.
1685 matching_lines.push_back(lines_in_die[0]);
1686 }
1687
1688 void
1689 dwflpp::collect_lines_for_single_lineno(char const * srcfile,
1690 int lineno,
1691 bool is_relative,
1692 base_func_info_map_t& funcs,
1693 lines_t& matching_lines)
1694 {
1695 /* Here, we handle ABSOLUTE and RELATIVE lineno types. Relative line numbers
1696 * are a bit special. The issue is that functions (esp. inlined ones) may not
1697 * even have a LR corresponding to the first valid line of code. So, applying
1698 * an offset to the 'first' LR found in the DIE can be quite imprecise.
1699 * Instead, we use decl_line, which although does not necessarily have a
1700 * LR associated with it (it can sometimes still happen esp. if the code is
1701 * written in OTB-style), it serves as an anchor on which we can apply the
1702 * offset to yield a lineno that will not change with compiler optimization.
1703 * It also has the added benefit of being consistent with the lineno
1704 * printed by e.g. stap -l kernel.function("vfs_read"), so users can be
1705 * confident from what lineno we adjust.
1706 */
1707 lines_t *cu_lines = get_cu_lines_sorted_by_lineno(srcfile);
1708 for (base_func_info_map_t::iterator func = funcs.begin();
1709 func != funcs.end(); ++func)
1710 add_matching_line_in_die(cu_lines, matching_lines, &func->die,
1711 is_relative ? lineno + func->decl_line
1712 : lineno);
1713 }
1714
1715 static bool
1716 functions_have_lineno(base_func_info_map_t& funcs,
1717 lines_t *lines, int lineno)
1718 {
1719 lines_range_t lineno_range = lineno_equal_range(lines, lineno);
1720 if (lineno_range.first == lineno_range.second)
1721 return false; // no LRs at this lineno
1722
1723 for (base_func_info_map_t::iterator func = funcs.begin();
1724 func != funcs.end(); ++func)
1725 if (!collect_lines_in_die(lineno_range, &func->die).empty())
1726 return true;
1727
1728 return false;
1729 }
1730
1731 // returns pair of valid linenos surrounding target lineno
1732 pair<int,int>
1733 dwflpp::get_nearest_linenos(char const * srcfile,
1734 int lineno,
1735 base_func_info_map_t& funcs)
1736 {
1737 assert(cu);
1738 lines_t *cu_lines = get_cu_lines_sorted_by_lineno(srcfile);
1739
1740 // Look around lineno for linenos with LRs.
1741 pair<int,int> nearest_linenos = make_pair(-1, -1);
1742 for (size_t i = 1; i < 6; ++i)
1743 {
1744 if (nearest_linenos.first == -1 && functions_have_lineno(funcs, cu_lines, lineno-i))
1745 nearest_linenos.first = lineno - i;
1746 if (nearest_linenos.second == -1 && functions_have_lineno(funcs, cu_lines, lineno+i))
1747 nearest_linenos.second = lineno + i;
1748 }
1749
1750 return nearest_linenos;
1751 }
1752
1753 // returns nearest valid lineno to target lineno
1754 int
1755 dwflpp::get_nearest_lineno(char const * srcfile,
1756 int lineno,
1757 base_func_info_map_t& funcs)
1758 {
1759 assert(cu);
1760 pair<int,int> nearest_linenos = get_nearest_linenos(srcfile, lineno, funcs);
1761
1762 if (nearest_linenos.first > 0
1763 && nearest_linenos.second > 0)
1764 {
1765 // pick the nearest line number (break tie to upper)
1766 if (lineno - nearest_linenos.first < nearest_linenos.second - lineno)
1767 return nearest_linenos.first;
1768 else
1769 return nearest_linenos.second;
1770 }
1771 else if (nearest_linenos.first > 0)
1772 return nearest_linenos.first;
1773 else if (nearest_linenos.second > 0)
1774 return nearest_linenos.second;
1775 else
1776 return -1;
1777 }
1778
1779 void
1780 dwflpp::suggest_alternative_linenos(char const * srcfile,
1781 int lineno,
1782 base_func_info_map_t& funcs)
1783 {
1784 assert(cu);
1785 pair<int,int> nearest_linenos = get_nearest_linenos(srcfile, lineno, funcs);
1786
1787 stringstream advice;
1788 advice << _F("no line records for %s:%d [man error::dwarf]", srcfile, lineno);
1789
1790 if (nearest_linenos.first > 0 || nearest_linenos.second > 0)
1791 {
1792 //TRANSLATORS: Here we are trying to advise what source file
1793 //TRANSLATORS: to attempt.
1794 advice << _(" (try ");
1795 if (nearest_linenos.first > 0)
1796 advice << ":" << nearest_linenos.first;
1797 if (nearest_linenos.first > 0 && nearest_linenos.second > 0)
1798 advice << _(" or ");
1799 if (nearest_linenos.second > 0)
1800 advice << ":" << nearest_linenos.second;
1801 advice << ")";
1802 }
1803 throw SEMANTIC_ERROR (advice.str());
1804 }
1805
1806 static base_func_info_map_t
1807 get_funcs_in_srcfile(base_func_info_map_t& funcs,
1808 const char * srcfile)
1809 {
1810 base_func_info_map_t matching_funcs;
1811 for (base_func_info_map_t::iterator func = funcs.begin();
1812 func != funcs.end(); ++func)
1813 if (strcmp(srcfile, func->decl_file) == 0)
1814 matching_funcs.push_back(*func);
1815 return matching_funcs;
1816 }
1817
1818 template<> void
1819 dwflpp::iterate_over_srcfile_lines<void>(char const * srcfile,
1820 const vector<int>& linenos,
1821 enum lineno_t lineno_type,
1822 base_func_info_map_t& funcs,
1823 void (* callback) (Dwarf_Addr,
1824 int, void*),
1825 bool has_nearest,
1826 void *data)
1827 {
1828 /* Matching line records (LRs) to user-provided linenos is trickier than it
1829 * seems. The fate of all functions is one of three possibilities:
1830 * 1. it's a normal function, with a subprogram DIE and a bona fide lowpc
1831 * and highpc attribute.
1832 * 2. it's an inlined function (one/multiple inlined_subroutine DIE, with one
1833 * abstract_origin DIE)
1834 * 3. it's both a normal function and an inlined function. For example, if
1835 * the funtion has been inlined only in some places, we'll have a DIE for
1836 * the normal subprogram DIE as well as inlined_subroutine DIEs.
1837 *
1838 * Multiple LRs for the same lineno but different addresses can simply happen
1839 * due to the function appearing in multiple forms. E.g. a function inlined
1840 * in two spots can yield two sets of LRs for its linenos at the different
1841 * addresses where it is inlined.
1842 * This is why the collect_* functions used here try to match up LRs back
1843 * to their originating DIEs. For example, in the function
1844 * collect_lines_for_single_lineno(), we filter first by DIE so that a lineno
1845 * corresponding to multiple addrs in multiple inlined_subroutine DIEs yields
1846 * a probe for each of them.
1847 */
1848 assert(cu);
1849
1850 // only work on the functions found in the current srcfile
1851 base_func_info_map_t current_funcs = get_funcs_in_srcfile(funcs, srcfile);
1852 if (current_funcs.empty())
1853 return;
1854
1855 // collect lines
1856 lines_t matching_lines;
1857 if (lineno_type == ABSOLUTE)
1858 collect_lines_for_single_lineno(srcfile, linenos[0], false, /* is_relative */
1859 current_funcs, matching_lines);
1860 else if (lineno_type == RELATIVE)
1861 collect_lines_for_single_lineno(srcfile, linenos[0], true, /* is_relative */
1862 current_funcs, matching_lines);
1863 else if (lineno_type == WILDCARD)
1864 collect_all_lines(srcfile, current_funcs, matching_lines);
1865 else if (lineno_type == ENUMERATED)
1866 for (vector<int>::const_iterator it = linenos.begin(); it != linenos.end(); it++)
1867 collect_lines_for_single_lineno(srcfile, *it, false, /* is_relative */
1868 current_funcs, matching_lines);
1869
1870 // should we try to collect the nearest line if we didn't collect everything on first try?
1871 if (matching_lines.empty() && has_nearest)
1872 {
1873 int lineno = linenos[0];
1874 if (lineno_type == RELATIVE)
1875 // just pick the first function and make it relative to that
1876 lineno += current_funcs[0].decl_line;
1877
1878 int nearest_lineno = get_nearest_lineno(srcfile, lineno, current_funcs);
1879 if (nearest_lineno > 0)
1880 collect_lines_for_single_lineno(srcfile, nearest_lineno, false, /* is_relative */
1881 current_funcs, matching_lines);
1882 }
1883
1884 // call back with matching lines
1885 if (!matching_lines.empty())
1886 {
1887 set<Dwarf_Addr> probed_addrs;
1888 for (lines_t::iterator line = matching_lines.begin();
1889 line != matching_lines.end(); ++line)
1890 {
1891 int lineno = DWARF_LINENO(*line);
1892 Dwarf_Addr addr = DWARF_LINEADDR(*line);
1893 bool is_new_addr = probed_addrs.insert(addr).second;
1894 if (is_new_addr)
1895 callback(addr, lineno, data);
1896 }
1897 }
1898 // No LRs found at the wanted lineno. So let's suggest other ones if user was
1899 // targeting a specific lineno (ABSOLUTE or RELATIVE).
1900 else if (lineno_type == ABSOLUTE ||
1901 lineno_type == RELATIVE)
1902 {
1903 int lineno = linenos[0];
1904 if (lineno_type == RELATIVE)
1905 // just pick the first function and make it relative to that
1906 lineno += current_funcs[0].decl_line;
1907
1908 suggest_alternative_linenos(srcfile, lineno, current_funcs);
1909 }
1910 }
1911
1912
1913 template<> void
1914 dwflpp::iterate_over_labels<void>(Dwarf_Die *begin_die,
1915 const string& sym,
1916 const base_func_info& function,
1917 const vector<int>& linenos,
1918 enum lineno_t lineno_type,
1919 void *data,
1920 void (* callback)(const base_func_info&,
1921 const char*,
1922 const char*,
1923 int,
1924 Dwarf_Die*,
1925 Dwarf_Addr,
1926 void*))
1927 {
1928 get_module_dwarf();
1929
1930 Dwarf_Die die, import;
1931 const char *name;
1932 int res = dwarf_child (begin_die, &die);
1933 if (res != 0)
1934 return; // die without children, bail out.
1935
1936 do
1937 {
1938 switch (dwarf_tag(&die))
1939 {
1940 case DW_TAG_label:
1941 name = dwarf_diename (&die);
1942 if (name &&
1943 (name == sym
1944 || (name_has_wildcard(sym)
1945 && function_name_matches_pattern (name, sym))))
1946 {
1947 // Don't try to be smart. Just drop no addr labels.
1948 Dwarf_Addr stmt_addr;
1949 if (dwarf_lowpc (&die, &stmt_addr) == 0)
1950 {
1951 // Get the file/line number for this label
1952 int dline;
1953 const char *file = dwarf_decl_file (&die) ?: "<unknown source>";
1954 dwarf_decl_line (&die, &dline);
1955
1956 vector<Dwarf_Die> scopes = getscopes_die(&die);
1957 if (scopes.size() > 1)
1958 {
1959 Dwarf_Die scope;
1960 if (!inner_die_containing_pc(scopes[1], stmt_addr, scope))
1961 {
1962 sess.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not "
1963 "contained by its scope '%s' (dieoffset: %s) -- bad"
1964 " debuginfo? [man error::dwarf]", name, lex_cast_hex(stmt_addr).c_str(),
1965 lex_cast_hex(dwarf_dieoffset(&die)).c_str(),
1966 (dwarf_diename(&scope) ?: "<unknown>"),
1967 lex_cast_hex(dwarf_dieoffset(&scope)).c_str()));
1968 }
1969
1970 bool matches_lineno;
1971 if (lineno_type == ABSOLUTE)
1972 matches_lineno = dline == linenos[0];
1973 else if (lineno_type == RELATIVE)
1974 matches_lineno = dline == linenos[0] + function.decl_line;
1975 else if (lineno_type == ENUMERATED)
1976 matches_lineno = (binary_search(linenos.begin(), linenos.end(), dline));
1977 else // WILDCARD
1978 matches_lineno = true;
1979
1980 if (matches_lineno)
1981 callback(function, name, file, dline,
1982 &scope, stmt_addr, data);
1983 }
1984 }
1985 }
1986 break;
1987
1988 case DW_TAG_subprogram:
1989 case DW_TAG_inlined_subroutine:
1990 // Stay within our filtered function
1991 break;
1992
1993 case DW_TAG_imported_unit:
1994 // Iterate over the children of the imported unit as if they
1995 // were inserted in place.
1996 if (dwarf_attr_die(&die, DW_AT_import, &import))
1997 iterate_over_labels (&import, sym, function, linenos,
1998 lineno_type, data, callback);
1999 break;
2000
2001 default:
2002 if (dwarf_haschildren (&die))
2003 iterate_over_labels (&die, sym, function, linenos,
2004 lineno_type, data, callback);
2005 break;
2006 }
2007 }
2008 while (dwarf_siblingof (&die, &die) == 0);
2009 }
2010
2011 // Mini 'query-like' struct to help us navigate callbacks during
2012 // external function resolution
2013 struct external_function_query {
2014 dwflpp* dw;
2015 const string name;
2016 Dwarf_Die die;
2017 Dwarf_Addr addr;
2018 bool resolved;
2019 external_function_query(dwflpp* dw, const string& name):
2020 dw(dw), name(name), addr(0), resolved(false) {}
2021 };
2022
2023 int
2024 dwflpp::external_function_cu_callback (Dwarf_Die* cu, external_function_query *efq)
2025 {
2026 efq->dw->focus_on_cu(cu);
2027 return efq->dw->iterate_over_functions(external_function_func_callback,
2028 efq, efq->name);
2029 }
2030
2031 int
2032 dwflpp::external_function_func_callback (Dwarf_Die* func, external_function_query *efq)
2033 {
2034 Dwarf_Attribute external;
2035 Dwarf_Addr func_addr;
2036 if (dwarf_attr_integrate(func, DW_AT_external, &external) != NULL &&
2037 dwarf_lowpc(func, &func_addr) == 0)
2038 {
2039 efq->die = *func;
2040 efq->addr = func_addr;
2041 efq->resolved = true;
2042 return DWARF_CB_ABORT; // we found it so stop here
2043 }
2044 return DWARF_CB_OK;
2045 }
2046
2047 template<> void
2048 dwflpp::iterate_over_callees<void>(Dwarf_Die *begin_die,
2049 const string& sym,
2050 long recursion_depth,
2051 void *data,
2052 void (* callback)(base_func_info&,
2053 base_func_info&,
2054 stack<Dwarf_Addr>*,
2055 void*),
2056 base_func_info& caller,
2057 stack<Dwarf_Addr> *callers)
2058 {
2059 get_module_dwarf();
2060
2061 Dwarf_Die die, import;
2062
2063 // DIE of abstract_origin found in die
2064 Dwarf_Die origin;
2065
2066 // callee's entry pc (= where we'll probe)
2067 Dwarf_Addr func_addr;
2068
2069 // caller's unwind pc during call (to match against bt for filtering)
2070 Dwarf_Addr caller_uw_addr;
2071
2072 Dwarf_Attribute attr;
2073
2074 base_func_info callee;
2075 if (dwarf_child(begin_die, &die) != 0)
2076 return; // die without children, bail out.
2077
2078 bool free_callers = false;
2079 if (callers == NULL) /* first call */
2080 {
2081 callers = new stack<Dwarf_Addr>();
2082 free_callers = true;
2083 }
2084
2085 do
2086 {
2087 bool inlined = false;
2088 switch (dwarf_tag(&die))
2089 {
2090 case DW_TAG_inlined_subroutine:
2091 inlined = true;
2092 case DW_TAG_GNU_call_site:
2093 callee.name = dwarf_diename(&die) ?: "";
2094 if (callee.name.empty())
2095 continue;
2096 if (callee.name != sym)
2097 {
2098 if (!name_has_wildcard(sym))
2099 continue;
2100 if (!function_name_matches_pattern(callee.name, sym))
2101 continue;
2102 }
2103
2104 /* In both cases (call sites and inlines), we want the
2105 * abstract_origin. The difference is that in inlines, the addr is
2106 * in the die itself, whereas for call sites, the addr is in the
2107 * abstract_origin's die.
2108 * Note that in the case of inlines, we're only calling back
2109 * for that inline instance, not all. This is what we want, since
2110 * it will only be triggered when 'called' from the target func,
2111 * which is something we have to emulate for non-inlined funcs
2112 * (which is the purpose of the caller_uw_addr below) */
2113 if (dwarf_attr_die(&die, DW_AT_abstract_origin, &origin) == NULL)
2114 continue;
2115
2116 // the low_pc of the die in either cases is the pc that would
2117 // show up in a backtrace (inlines are a special case in which
2118 // the die's low_pc is also the abstract_origin's low_pc = the
2119 // 'start' of the inline instance)
2120 if (dwarf_lowpc(&die, &caller_uw_addr) != 0)
2121 continue;
2122
2123 if (inlined)
2124 func_addr = caller_uw_addr;
2125 else if (dwarf_lowpc(&origin, &func_addr) != 0)
2126 {
2127 // function doesn't have a low_pc, is it external?
2128 if (dwarf_attr_integrate(&origin, DW_AT_external,
2129 &attr) != NULL)
2130 {
2131 // let's iterate over the CUs and find it. NB: it's
2132 // possible we could have also done this by creating a
2133 // probe point with .exported tacked on and rerunning it
2134 // through derive_probe(). But since we're already on the
2135 // dwflpp side of things, and we already have access to
2136 // everything we need, let's try to be self-sufficient.
2137
2138 // remember old focus
2139 Dwarf_Die *old_cu = cu;
2140
2141 external_function_query efq(this, dwarf_linkage_name(&origin) ?: callee.name);
2142 iterate_over_cus(external_function_cu_callback, &efq, false);
2143
2144 // restore focus
2145 cu = old_cu;
2146
2147 if (!efq.resolved) // did we resolve it?
2148 continue;
2149
2150 func_addr = efq.addr;
2151 origin = efq.die;
2152 }
2153 // non-external function without low_pc, jump ship
2154 else continue;
2155 }
2156
2157 // We now have the addr to probe in func_addr, and the DIE
2158 // from which to obtain file/line info in origin
2159
2160 // Get the file/line number for this callee
2161 callee.decl_file = dwarf_decl_file (&origin) ?: "<unknown source>";
2162 dwarf_decl_line (&origin, &callee.decl_line);
2163
2164 // add as a caller to match against
2165 if (!inlined)
2166 callers->push(caller_uw_addr);
2167
2168 callee.die = inlined ? die : origin;
2169 callee.entrypc = func_addr;
2170 callback(callee, caller, callers, data);
2171
2172 // If it's a tail call, print a warning that it may not be caught
2173 if (!inlined
2174 && dwarf_attr_integrate(&die, DW_AT_GNU_tail_call, &attr) != NULL)
2175 sess.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: "
2176 ".callee probe may not fire. Try placing the probe "
2177 "directly on the callee function instead.",
2178 callee.name.c_str(), caller.name.c_str()));
2179
2180 // For .callees(N) probes, we recurse on this callee. Note that we
2181 // pass the callee we just found as the caller arg for this recursion,
2182 // since it (the callee we just found) will be the caller of whatever
2183 // callees found inside this recursion.
2184 if (recursion_depth > 1)
2185 iterate_over_callees(inlined ? &die : &origin,
2186 sym, recursion_depth-1, data,
2187 callback, callee, callers);
2188
2189 if (!inlined)
2190 callers->pop();
2191 break;
2192
2193 case DW_TAG_subprogram:
2194 break; // don't leave our filtered func
2195
2196 case DW_TAG_imported_unit:
2197 // Iterate over the children of the imported unit as if they
2198 // were inserted in place.
2199 if (dwarf_attr_die(&die, DW_AT_import, &import))
2200 // NB: we pass the same caller arg into it
2201 iterate_over_callees (&import, sym, recursion_depth, data,
2202 callback, caller, callers);
2203 break;
2204
2205 default:
2206 if (dwarf_haschildren (&die))
2207 // NB: we pass the same caller arg into it
2208 iterate_over_callees (&die, sym, recursion_depth, data,
2209 callback, caller, callers);
2210 break;
2211 }
2212 }
2213 while (dwarf_siblingof (&die, &die) == 0);
2214
2215 if (free_callers && callers != NULL)
2216 delete callers;
2217 }
2218
2219
2220 void
2221 dwflpp::collect_srcfiles_matching (string const & pattern,
2222 set<string> & filtered_srcfiles)
2223 {
2224 assert (module);
2225 assert (cu);
2226
2227 size_t nfiles;
2228 Dwarf_Files *srcfiles;
2229
2230 // PR 5049: implicit * in front of given path pattern.
2231 // NB: fnmatch() is used without FNM_PATHNAME.
2232 string prefixed_pattern = string("*/") + pattern;
2233
2234 DWARF_ASSERT ("dwarf_getsrcfiles",
2235 dwarf_getsrcfiles (cu, &srcfiles, &nfiles));
2236 {
2237 for (size_t i = 0; i < nfiles; ++i)
2238 {
2239 char const * fname = dwarf_filesrc (srcfiles, i, NULL, NULL);
2240 if (fnmatch (pattern.c_str(), fname, 0) == 0 ||
2241 fnmatch (prefixed_pattern.c_str(), fname, 0) == 0)
2242 {
2243 filtered_srcfiles.insert (fname);
2244 if (sess.verbose>2)
2245 clog << _F("selected source file '%s'\n", fname);
2246 }
2247 }
2248 }
2249 }
2250
2251
2252 void
2253 dwflpp::resolve_prologue_endings (func_info_map_t & funcs)
2254 {
2255 // When a program is compiled with no optimization, GCC does no variable
2256 // tracking, which means that location info is actually only really valid
2257 // after the prologue, even though GCC reports it as valid during. So we need
2258 // to find the prologue ends to get accurate info. This may or may not be the
2259 // first address that has a source line distinct from the function
2260 // declaration's.
2261
2262 assert(module);
2263 assert(cu);
2264
2265 size_t nlines = 0;
2266 Dwarf_Lines *lines = NULL;
2267
2268 /* trouble cases:
2269 malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue
2270 sys_get?id in kernel/timer.c no-prologue
2271 sys_exit_group tail-recursive
2272 {do_,}sys_open extra-long-prologue (gcc 3.4)
2273 cpu_to_logical_apicid NULL-decl_file
2274 */
2275
2276 // Fetch all srcline records, sorted by address. No need to free lines, it's a
2277 // direct pointer to the CU's cached lines.
2278 if (dwarf_getsrclines(cu, &lines, &nlines) != 0
2279 || lines == NULL || nlines == 0)
2280 {
2281 if (sess.verbose > 2)
2282 clog << _F("aborting prologue search: no source lines found for cu '%s'\n",
2283 cu_name().c_str());
2284 return;
2285 }
2286
2287 // Dump them into our own array for easier searching. They should already be
2288 // sorted by addr, but we doublecheck that here. We want to keep the indices
2289 // between lines and addrs the same.
2290 vector<Dwarf_Addr> addrs;
2291 for (size_t i = 0; i < nlines; i++)
2292 {
2293 Dwarf_Line* line = dwarf_onesrcline(lines, i);
2294 Dwarf_Addr addr = DWARF_LINEADDR(line);
2295 if (!addrs.empty() && addr < addrs.back())
2296 throw SEMANTIC_ERROR(_("lines from dwarf_getsrclines() not sorted"));
2297 addrs.push_back(addr);
2298 }
2299 // We normally ignore a function's decl_line, since it is associated with the
2300 // line at which the identifier appears in the declaration, and has no
2301 // meaningful relation to the lineno associated with the entrypc (which is
2302 // normally the lineno of '{', which could occur at the same line as the
2303 // declaration, or lower down).
2304 // However, if the CU was compiled using GCC < 4.4, then the decl_line
2305 // actually represents the lineno of '{' as well, in which case if the lineno
2306 // associated with the entrypc is != to the decl_line, it means the compiler
2307 // scraped/optimized off some of the beginning of the function and the safest
2308 // thing we can do is consider it naked.
2309 bool consider_decl_line = false;
2310 {
2311 string prod, vers;
2312 if (is_gcc_producer(cu, prod, vers)
2313 && strverscmp(vers.c_str(), "4.4.0") < 0)
2314 consider_decl_line = true;
2315 }
2316
2317 for(func_info_map_t::iterator it = funcs.begin(); it != funcs.end(); it++)
2318 {
2319 #if 0 /* someday */
2320 Dwarf_Addr* bkpts = 0;
2321 int n = dwarf_entry_breakpoints (& it->die, & bkpts);
2322 // ...
2323 free (bkpts);
2324 #endif
2325
2326 Dwarf_Addr entrypc = it->entrypc;
2327 Dwarf_Addr highpc; // NB: highpc is exclusive: [entrypc,highpc)
2328 DWFL_ASSERT ("dwarf_highpc", dwarf_highpc (& it->die,
2329 & highpc));
2330
2331 if (it->decl_file == 0) it->decl_file = "";
2332
2333 unsigned entrypc_srcline_idx = 0;
2334 Dwarf_Line *entrypc_srcline = NULL;
2335 {
2336 vector<Dwarf_Addr>::const_iterator it_addr =
2337 lower_bound(addrs.begin(), addrs.end(), entrypc);
2338 if (it_addr != addrs.end() && *it_addr == entrypc)
2339 {
2340 entrypc_srcline_idx = it_addr - addrs.begin();
2341 entrypc_srcline = dwarf_onesrcline(lines, entrypc_srcline_idx);
2342 }
2343 }
2344
2345 if (!entrypc_srcline)
2346 {
2347 if (sess.verbose > 2)
2348 clog << _F("missing entrypc dwarf line record for function '%s'\n",
2349 it->name.c_str());
2350 // This is probably an inlined function. We'll end up using
2351 // its lowpc as a probe address.
2352 continue;
2353 }
2354
2355 if (entrypc == 0)
2356 {
2357 if (sess.verbose > 2)
2358 clog << _F("null entrypc dwarf line record for function '%s'\n",
2359 it->name.c_str());
2360 // This is probably an inlined function. We'll skip this instance;
2361 // it is messed up.
2362 continue;
2363 }
2364
2365 if (sess.verbose>2)
2366 clog << _F("searching for prologue of function '%s' %#" PRIx64 "-%#" PRIx64
2367 "@%s:%d\n", it->name.c_str(), entrypc, highpc, it->decl_file,
2368 it->decl_line);
2369
2370 // For each function, we look for the prologue-end marker (e.g. clang
2371 // outputs one). If there is no explicit marker (e.g. GCC does not), we
2372 // accept a bigger or equal lineno as a prologue end (this catches GCC's
2373 // 0-line advances).
2374
2375 // We may have to skip a few because some old compilers plop
2376 // in dummy line records for longer prologues. If we go too
2377 // far (addr >= highpc), we take the previous one. Or, it may
2378 // be the first one, if the function had no prologue, and thus
2379 // the entrypc maps to a statement in the body rather than the
2380 // declaration.
2381
2382 int entrypc_srcline_lineno = DWARF_LINENO(entrypc_srcline);
2383 unsigned postprologue_srcline_idx = entrypc_srcline_idx;
2384 Dwarf_Line *postprologue_srcline = entrypc_srcline;
2385
2386 while (postprologue_srcline_idx < nlines)
2387 {
2388 postprologue_srcline = dwarf_onesrcline(lines,
2389 postprologue_srcline_idx);
2390 Dwarf_Addr lineaddr = DWARF_LINEADDR(postprologue_srcline);
2391 const char* linesrc = DWARF_LINESRC(postprologue_srcline);
2392 int lineno = DWARF_LINENO(postprologue_srcline);
2393 bool lineprologue_end = DWARF_LINEPROLOGUEEND(postprologue_srcline);
2394
2395 if (sess.verbose>2)
2396 clog << _F("checking line record %#" PRIx64 "@%s:%d%s\n", lineaddr,
2397 linesrc, lineno, lineprologue_end ? " (marked)" : "");
2398
2399 // have we passed the function?
2400 if (lineaddr >= highpc)
2401 break;
2402 // is there an explicit prologue_end marker?
2403 if (lineprologue_end)
2404 break;
2405 // is it a different file?
2406 if (strcmp(linesrc, it->decl_file))
2407 break;
2408 // OK, it's the same file, but is it a different line?
2409 if (lineno != entrypc_srcline_lineno)
2410 break;
2411 // Same file and line, is this a second line record (e.g. 0-line advance)?
2412 if (postprologue_srcline_idx != entrypc_srcline_idx)
2413 break;
2414 // This is the first iteration. Is decl_line meaningful and is the
2415 // lineno past the decl_line?
2416 if (consider_decl_line && lineno != it->decl_line)
2417 break;
2418
2419 // Let's try the next srcline.
2420 postprologue_srcline_idx ++;
2421
2422 } // loop over srclines
2423
2424
2425 Dwarf_Addr postprologue_addr = DWARF_LINEADDR(postprologue_srcline);
2426 if (postprologue_addr >= highpc)
2427 {
2428 // pick addr of previous line record
2429 Dwarf_Line *lr = dwarf_onesrcline(lines, postprologue_srcline_idx-1);
2430 postprologue_addr = DWARF_LINEADDR(lr);
2431 }
2432
2433 it->prologue_end = postprologue_addr;
2434
2435 if (sess.verbose>2)
2436 {
2437 clog << _F("prologue found function '%s'", it->name.c_str());
2438 // Add a little classification datum
2439 //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free)
2440 if (postprologue_addr == entrypc)
2441 clog << _(" (naked)");
2442 //TRANSLATORS: Here we're adding some classification datum (ie we went over)
2443 if (DWARF_LINEADDR(postprologue_srcline) >= highpc)
2444 clog << _(" (tail-call?)");
2445 //TRANSLATORS: Here we're adding some classification datum (ie it was marked)
2446 if (DWARF_LINEPROLOGUEEND(postprologue_srcline))
2447 clog << _(" (marked)");
2448
2449 clog << " = 0x" << hex << postprologue_addr << dec << "\n";
2450 }
2451
2452 } // loop over functions
2453 }
2454
2455
2456 bool
2457 dwflpp::function_entrypc (Dwarf_Addr * addr)
2458 {
2459 assert (function);
2460 // PR10574: reject 0, which tends to be eliminated COMDAT
2461 return (dwarf_entrypc (function, addr) == 0 && *addr != 0);
2462 }
2463
2464
2465 bool
2466 dwflpp::die_entrypc (Dwarf_Die * die, Dwarf_Addr * addr)
2467 {
2468 int rc = 0;
2469 string lookup_method;
2470
2471 * addr = 0;
2472
2473 lookup_method = "dwarf_entrypc";
2474 rc = dwarf_entrypc (die, addr);
2475
2476 if (rc)
2477 {
2478 lookup_method = "dwarf_ranges";
2479
2480 Dwarf_Addr base;
2481 Dwarf_Addr begin;
2482 Dwarf_Addr end;
2483 ptrdiff_t offset = dwarf_ranges (die, 0, &base, &begin, &end);
2484 if (offset < 0) rc = -1;
2485 else if (offset > 0)
2486 {
2487 * addr = begin;
2488 rc = 0;
2489
2490 // Now we need to check that there are no more ranges
2491 // associated with this function, which could conceivably
2492 // happen if a function is inlined, then pieces of it are
2493 // split amongst different conditional branches. It's not
2494 // obvious which of them to favour. As a heuristic, we
2495 // pick the beginning of the first range, and ignore the
2496 // others (but with a warning).
2497
2498 unsigned extra = 0;
2499 while ((offset = dwarf_ranges (die, offset, &base, &begin, &end)) > 0)
2500 extra ++;
2501 if (extra)
2502 lookup_method += _F(", ignored %s more", lex_cast(extra).c_str());
2503 }
2504 }
2505
2506 // PR10574: reject subprograms where the entrypc address turns out
2507 // to be 0, since they tend to correspond to duplicate-eliminated
2508 // COMDAT copies of C++ functions.
2509 if (rc == 0 && *addr == 0)
2510 {
2511 lookup_method += _(" (skip comdat)");
2512 rc = 1;
2513 }
2514
2515 if (sess.verbose > 2)
2516 clog << _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64 " (rc %d)", lookup_method.c_str(),
2517 lex_cast_hex(dwarf_dieoffset(die)).c_str(), *addr, rc) << endl;
2518
2519 return (rc == 0);
2520 }
2521
2522
2523 void
2524 dwflpp::function_die (Dwarf_Die *d)
2525 {
2526 assert (function);
2527 *d = *function;
2528 }
2529
2530
2531 void
2532 dwflpp::function_file (char const ** c)
2533 {
2534 assert (function);
2535 assert (c);
2536 *c = dwarf_decl_file (function) ?: "<unknown source>";
2537 }
2538
2539
2540 void
2541 dwflpp::function_line (int *linep)
2542 {
2543 assert (function);
2544 dwarf_decl_line (function, linep);
2545 }
2546
2547
2548 bool
2549 dwflpp::die_has_pc (Dwarf_Die & die, Dwarf_Addr pc)
2550 {
2551 int res = dwarf_haspc (&die, pc);
2552 // dwarf_ranges will return -1 if a function die has no DW_AT_ranges
2553 // if (res == -1)
2554 // DWARF_ASSERT ("dwarf_haspc", res);
2555 return res == 1;
2556 }
2557
2558
2559 bool
2560 dwflpp::inner_die_containing_pc(Dwarf_Die& scope, Dwarf_Addr addr,
2561 Dwarf_Die& result)
2562 {
2563 result = scope;
2564
2565 // Sometimes we're in a bad scope to begin with -- just let it be. This can
2566 // happen for example if the compiler outputs a label PC that's just outside
2567 // the lexical scope. We can't really do anything about that, but variables
2568 // will probably not be accessible in this case.
2569 if (!die_has_pc(scope, addr))
2570 return false;
2571
2572 Dwarf_Die child, import;
2573 int rc = dwarf_child(&result, &child);
2574 while (rc == 0)
2575 {
2576 switch (dwarf_tag (&child))
2577 {
2578 case DW_TAG_imported_unit:
2579 // The children of the imported unit need to be treated as if
2580 // they are inserted here. So look inside and set result if
2581 // found.
2582 if (dwarf_attr_die(&child, DW_AT_import, &import))
2583 {
2584 Dwarf_Die import_result;
2585 if (inner_die_containing_pc(import, addr, import_result))
2586 {
2587 result = import_result;
2588 return true;
2589 }
2590 }
2591 break;
2592
2593 // lexical tags to recurse within the same starting scope
2594 // NB: this intentionally doesn't cross into inlines!
2595 case DW_TAG_lexical_block:
2596 case DW_TAG_with_stmt:
2597 case DW_TAG_catch_block:
2598 case DW_TAG_try_block:
2599 case DW_TAG_entry_point:
2600 if (die_has_pc(child, addr))
2601 {
2602 result = child;
2603 rc = dwarf_child(&result, &child);
2604 continue;
2605 }
2606 }
2607 rc = dwarf_siblingof(&child, &child);
2608 }
2609 return true;
2610 }
2611
2612
2613 void
2614 dwflpp::loc2c_error (void *, const char *fmt, ...)
2615 {
2616 const char *msg = "?";
2617 char *tmp = NULL;
2618 int rc;
2619 va_list ap;
2620 va_start (ap, fmt);
2621 rc = vasprintf (& tmp, fmt, ap);
2622 if (rc < 0)
2623 msg = "?";
2624 else
2625 msg = tmp;
2626 va_end (ap);
2627 throw SEMANTIC_ERROR (msg);
2628 }
2629
2630
2631 // This function generates code used for addressing computations of
2632 // target variables.
2633 void
2634 dwflpp::emit_address (struct obstack *pool, Dwarf_Addr address)
2635 {
2636 int n = dwfl_module_relocations (module);
2637 DWFL_ASSERT ("dwfl_module_relocations", n >= 0);
2638 Dwarf_Addr reloc_address = address;
2639 const char *secname = "";
2640 if (n > 1)
2641 {
2642 int i = dwfl_module_relocate_address (module, &reloc_address);
2643 DWFL_ASSERT ("dwfl_module_relocate_address", i >= 0);
2644 secname = dwfl_module_relocation_info (module, i, NULL);
2645 }
2646
2647 if (sess.verbose > 2)
2648 {
2649 clog << _F("emit dwarf addr %#" PRIx64 " => module %s section %s relocaddr %#" PRIx64,
2650 address, module_name.c_str (), (secname ?: "null"),
2651 reloc_address) << endl;
2652 }
2653
2654 if (n > 0 && !(n == 1 && secname == NULL))
2655 {
2656 DWFL_ASSERT ("dwfl_module_relocation_info", secname);
2657 if (n > 1 || secname[0] != '\0')
2658 {
2659 // This gives us the module name, and section name within the
2660 // module, for a kernel module (or other ET_REL module object).
2661 obstack_printf (pool, "({ unsigned long addr = 0; ");
2662 obstack_printf (pool, "addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64 "); ",
2663 module_name.c_str(), secname, reloc_address);
2664 obstack_printf (pool, "addr; })");
2665 }
2666 else if (n == 1 && module_name == TOK_KERNEL && secname[0] == '\0')
2667 {
2668 // elfutils' way of telling us that this is a relocatable kernel address, which we
2669 // need to treat the same way here as dwarf_query::add_probe_point does: _stext.
2670 address -= sess.sym_stext;
2671 secname = "_stext";
2672 // Note we "cache" the result here through a static because the
2673 // kernel will never move after being loaded (unlike modules and
2674 // user-space dynamic share libraries).
2675 obstack_printf (pool, "({ static unsigned long addr = 0; ");
2676 obstack_printf (pool, "if (addr==0) addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64 "); ",
2677 module_name.c_str(), secname, address); // PR10000 NB: not reloc_address
2678 obstack_printf (pool, "addr; })");
2679 }
2680 else
2681 {
2682 obstack_printf (pool, "/* pragma:vma */");
2683 obstack_printf (pool, "({ unsigned long addr = 0; ");
2684 obstack_printf (pool, "addr = _stp_umodule_relocate (\"%s\",%#" PRIx64 ", current); ",
2685 resolve_path(module_name.c_str()).c_str(), address);
2686 obstack_printf (pool, "addr; })");
2687 }
2688 }
2689 else
2690 obstack_printf (pool, "%#" PRIx64 "UL", address); // assume as constant
2691 }
2692
2693
2694 void
2695 dwflpp::loc2c_emit_address (void *arg, struct obstack *pool,
2696 Dwarf_Addr address)
2697 {
2698 static_cast<dwflpp *>(arg)->emit_address (pool, address);
2699 }
2700
2701
2702 void
2703 dwflpp::get_locals(vector<Dwarf_Die>& scopes, set<string>& locals)
2704 {
2705 // XXX Shouldn't this be walking up to outer scopes too?
2706
2707 get_locals_die(scopes[0], locals);
2708 }
2709
2710 void
2711 dwflpp::get_locals_die(Dwarf_Die& die, set<string>& locals)
2712 {
2713 // Try to get the first child of die.
2714 Dwarf_Die child, import;
2715 if (dwarf_child (&die, &child) == 0)
2716 {
2717 do
2718 {
2719 const char *name;
2720 // Output each sibling's name (that is a variable or
2721 // parameter) to 'o'.
2722 switch (dwarf_tag (&child))
2723 {
2724 case DW_TAG_variable:
2725 case DW_TAG_formal_parameter:
2726 name = dwarf_diename (&child);
2727 if (name)
2728 locals.insert(string("$") + name);
2729 break;
2730 case DW_TAG_imported_unit:
2731 // Treat the imported unit children as if they are
2732 // children of the given DIE.
2733 if (dwarf_attr_die(&child, DW_AT_import, &import))
2734 get_locals_die (import, locals);
2735 break;
2736 default:
2737 break;
2738 }
2739 }
2740 while (dwarf_siblingof (&child, &child) == 0);
2741 }
2742 }
2743
2744
2745 Dwarf_Attribute *
2746 dwflpp::find_variable_and_frame_base (vector<Dwarf_Die>& scopes,
2747 Dwarf_Addr pc,
2748 string const & local,
2749 const target_symbol *e,
2750 Dwarf_Die *vardie,
2751 Dwarf_Attribute *fb_attr_mem)
2752 {
2753 Dwarf_Die *scope_die = &scopes[0];
2754 Dwarf_Attribute *fb_attr = NULL;
2755
2756 assert (cu);
2757
2758 int declaring_scope = dwarf_getscopevar (&scopes[0], scopes.size(),
2759 local.c_str(),
2760 0, NULL, 0, 0,
2761 vardie);
2762 if (declaring_scope < 0)
2763 {
2764 set<string> locals;
2765 get_locals(scopes, locals);
2766 string sugs = levenshtein_suggest(local, locals); // probably not that many, so no limit
2767 if (pc)
2768 throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)",
2769 local.c_str(),
2770 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str(),
2771 module_name.c_str(),
2772 lex_cast_hex(pc).c_str(),
2773 (scope_die == NULL) ? "" : _("in"),
2774 (dwarf_diename(scope_die) ?: "<unknown>"),
2775 (dwarf_diename(cu) ?: "<unknown>"),
2776 (sugs.empty()
2777 ? (_("<no alternatives>"))
2778 : (_("alternatives: ") + sugs + ")")).c_str()),
2779 e->tok);
2780 else
2781 throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)",
2782 local.c_str(),
2783 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str(),
2784 module_name.c_str(),
2785 (scope_die == NULL) ? "" : _("in"),
2786 (dwarf_diename(scope_die) ?: "<unknown>"),
2787 cu_name().c_str(),
2788 (sugs.empty()
2789 ? (_("<no alternatives>"))
2790 : (_("alternatives: ") + sugs + ")")).c_str()),
2791 e->tok);
2792 }
2793
2794 /* Some GCC versions would output duplicate external variables, one
2795 without a location attribute. If so, try to find the other if it
2796 exists in the same scope. See GCC PR51410. */
2797 Dwarf_Attribute attr_mem;
2798 if (dwarf_attr_integrate (vardie, DW_AT_const_value, &attr_mem) == NULL
2799 && dwarf_attr_integrate (vardie, DW_AT_location, &attr_mem) == NULL
2800 && dwarf_attr_integrate (vardie, DW_AT_external, &attr_mem) != NULL
2801 && dwarf_tag(&scopes[declaring_scope]) == DW_TAG_compile_unit)
2802 {
2803 Dwarf_Die orig_vardie = *vardie;
2804 bool alt_found = false;
2805 if (dwarf_child(&scopes[declaring_scope], vardie) == 0)
2806 do
2807 {
2808 // Note, not handling DW_TAG_imported_unit, assuming GCC
2809 // version is recent enough to not need this workaround if
2810 // we would see an imported unit.
2811 if (dwarf_tag (vardie) == DW_TAG_variable
2812 && strcmp (dwarf_diename (vardie), local.c_str ()) == 0
2813 && (dwarf_attr_integrate (vardie, DW_AT_external, &attr_mem)
2814 != NULL)
2815 && ((dwarf_attr_integrate (vardie, DW_AT_const_value, &attr_mem)
2816 != NULL)
2817 || (dwarf_attr_integrate (vardie, DW_AT_location, &attr_mem)
2818 != NULL)))
2819 alt_found = true;
2820 }
2821 while (!alt_found && dwarf_siblingof(vardie, vardie) == 0);
2822
2823 if (! alt_found)
2824 *vardie = orig_vardie;
2825 }
2826
2827 // Global vars don't need (cannot use) frame base in location descriptor.
2828 if (pc == 0)
2829 return NULL;
2830
2831 /* We start out walking the "lexical scopes" as returned by
2832 * as returned by dwarf_getscopes for the address, starting with the
2833 * declaring_scope that the variable was found in.
2834 */
2835 vector<Dwarf_Die> physcopes, *fbscopes = &scopes;
2836 for (size_t inner = declaring_scope;
2837 inner < fbscopes->size() && fb_attr == NULL;
2838 ++inner)
2839 {
2840 Dwarf_Die& scope = (*fbscopes)[inner];
2841 switch (dwarf_tag (&scope))
2842 {
2843 default:
2844 continue;
2845 case DW_TAG_subprogram:
2846 case DW_TAG_entry_point:
2847 fb_attr = dwarf_attr_integrate (&scope,
2848 DW_AT_frame_base,
2849 fb_attr_mem);
2850 break;
2851 case DW_TAG_inlined_subroutine:
2852 /* Unless we already are going through the "pyshical die tree",
2853 * we now need to start walking the die tree where this
2854 * subroutine is inlined to find the appropriate frame base. */
2855 if (declaring_scope != -1)
2856 {
2857 physcopes = getscopes_die(&scope);
2858 if (physcopes.empty())
2859 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
2860 local.c_str()), e->tok);
2861 fbscopes = &physcopes;
2862 inner = 0; // zero is current scope, for look will increase.
2863 declaring_scope = -1;
2864 }
2865 break;
2866 }
2867 }
2868
2869 return fb_attr;
2870 }
2871
2872 /* Returns a human readable string with suggested locations where a
2873 DIE attribute is valid. */
2874 static string
2875 suggested_locations_string(Dwarf_Attribute *attr)
2876 {
2877 string locsstr;
2878 if (attr == NULL)
2879 locsstr = "<no alternatives for NULL attribute>";
2880 else
2881 {
2882 #if _ELFUTILS_PREREQ (0, 158)
2883 Dwarf_Op *expr;
2884 size_t exprlen;
2885 Dwarf_Addr base, start, end;
2886 ptrdiff_t off = 0;
2887
2888 off = dwarf_getlocations (attr, off, &base,
2889 &start, &end,
2890 &expr, &exprlen);
2891 if (off > 0)
2892 {
2893 locsstr = _("alternative locations: ");
2894
2895 while (off > 0)
2896 {
2897 locsstr += "[";
2898 locsstr += lex_cast_hex(start);
2899 locsstr += ",";
2900 locsstr += lex_cast_hex(end);
2901 locsstr += "]";
2902
2903 off = dwarf_getlocations (attr, off, &base,
2904 &start, &end,
2905 &expr, &exprlen);
2906 if (off > 0)
2907 locsstr += ", ";
2908 }
2909 }
2910 else if (off == 0)
2911 locsstr = _("<no alternative locations>");
2912 else
2913 locsstr = _F("<error getting alternative locations: %s>",
2914 dwarf_errmsg(-1));
2915 #else
2916 locsstr = "<cannot suggest any alternative locations, elfutils too old>";
2917 #endif /* _ELFUTILS_PREREQ (0, 158) */
2918 }
2919
2920 return locsstr;
2921 }
2922
2923 /* Produce a human readable name for a DIE. */
2924 static string
2925 die_name_string (Dwarf_Die *die)
2926 {
2927 string res;
2928 const char *name = dwarf_linkage_name(die);
2929 if (name == NULL)
2930 name = dwarf_diename (die);
2931
2932 size_t demangle_buffer_len = 0;
2933 char *demangle_buffer = NULL;
2934 if (name != NULL && name[0] == '_' && name[1] == 'Z')
2935 {
2936 int status = -1;
2937 char *dsymname = abi::__cxa_demangle (name, demangle_buffer,
2938 &demangle_buffer_len, &status);
2939 if (status == 0)
2940 name = demangle_buffer = dsymname;
2941 }
2942 if (name != NULL)
2943 res = name;
2944 else
2945 res = _("<unknown");
2946 free (demangle_buffer);
2947
2948 return res;
2949 }
2950
2951 /* Returns a source line and column string based on the inlined DIE
2952 or based on the pc if DIE is NULL. */
2953 string
2954 dwflpp::pc_die_line_string (Dwarf_Addr pc, Dwarf_Die *die)
2955 {
2956 string linestr;
2957
2958 int lineno, col;
2959 const char *src = NULL;
2960 lineno = col = -1;
2961
2962 if (die == NULL)
2963 {
2964 Dwarf_Line *line = dwarf_getsrc_die (cu, pc);
2965 if (line != NULL)
2966 {
2967 src = dwarf_linesrc (line, NULL, NULL);
2968 dwarf_lineno (line, &lineno);
2969 dwarf_linecol (line, &col);
2970 }
2971 }
2972 else
2973 {
2974 Dwarf_Files *files;
2975 if (dwarf_getsrcfiles (cu, &files, NULL) == 0)
2976 {
2977 Dwarf_Attribute attr;
2978 Dwarf_Word val;
2979 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_file, &attr),
2980 &val) == 0)
2981 {
2982 src = dwarf_filesrc (files, val, NULL, NULL);
2983 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_line,
2984 &attr), &val) == 0)
2985 {
2986 lineno = val;
2987 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_column,
2988 &attr), &val) == 0)
2989 col = val;
2990 }
2991 }
2992 }
2993 }
2994
2995 if (src != NULL)
2996 {
2997 linestr += src;
2998 if (lineno > 0)
2999 {
3000 linestr += ":" + lex_cast(lineno);
3001 if (col > 0)
3002 linestr += ":" + lex_cast(col);
3003 }
3004 }
3005 else
3006 linestr += _("unknown source");
3007
3008 return linestr;
3009 }
3010
3011 /* Returns a human readable DIE offset for use in error messages.
3012 Includes DIE offset and DWARF file used. */
3013 string
3014 dwflpp::die_location_as_string(Dwarf_Addr pc, Dwarf_Die *die)
3015 {
3016 string locstr;
3017
3018 /* DIE offset */
3019 locstr += _("dieoffset: ");
3020 locstr += lex_cast_hex(dwarf_dieoffset(die));
3021
3022 /* DWARF file */
3023 const char *debugfile;
3024 locstr += _(" from ");
3025 if (dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, NULL,
3026 &debugfile) == NULL || debugfile == NULL)
3027 {
3028 locstr += _("unknown debug file for ");
3029 locstr += module_name;
3030 }
3031 else
3032 locstr += debugfile;
3033
3034 return locstr;
3035 }
3036
3037 /* Returns a human readable (inlined) function and source file/line location
3038 for a DIE and pc location. */
3039 string
3040 dwflpp::die_location_as_function_string(Dwarf_Addr pc, Dwarf_Die *die)
3041 {
3042 string locstr;
3043 locstr = _("function: ");
3044
3045 /* Find the first function-like DIE with a name in scope. */
3046 Dwarf_Die funcdie_mem;
3047 Dwarf_Die *funcdie = NULL;
3048 string funcname = "";
3049 Dwarf_Die *scopes = NULL;
3050 int nscopes = dwarf_getscopes (cu, pc, &scopes);
3051 for (int i = 0; funcname == "" && i < nscopes; i++)
3052 {
3053 Dwarf_Die *scope = &scopes[i];
3054 int tag = dwarf_tag (scope);
3055 if (tag == DW_TAG_subprogram
3056 || tag == DW_TAG_inlined_subroutine
3057 || tag == DW_TAG_entry_point)
3058 funcname = die_name_string (scope);
3059 if (funcname != "")
3060 {
3061 funcdie_mem = *scope;
3062 funcdie = &funcdie_mem;
3063 }
3064 }
3065 free (scopes);
3066
3067 /* source location */
3068 if (funcname == "")
3069 locstr += _("<unknown> at ") + pc_die_line_string (pc, NULL);
3070 else
3071 {
3072 int nscopes = dwarf_getscopes_die (funcdie, &scopes);
3073 if (nscopes > 0)
3074 {
3075 /* scopes[0] == funcdie, the lowest level, for which we already have
3076 the name. This is the actual source location where it
3077 happened. */
3078 locstr += funcname;
3079 locstr += _(" at ");
3080 locstr += pc_die_line_string (pc, NULL);
3081
3082 /* last_scope is the source location where the next inlined frame/function
3083 call was done. */
3084 Dwarf_Die *last_scope = &scopes[0];
3085 for (int i = 1; i < nscopes; i++)
3086 {
3087 Dwarf_Die *scope = &scopes[i];
3088 int tag = dwarf_tag (scope);
3089 if (tag != DW_TAG_inlined_subroutine
3090 && tag != DW_TAG_entry_point
3091 && tag != DW_TAG_subprogram)
3092 continue;
3093
3094 locstr += _(" inlined by ");
3095 locstr += die_name_string (scope);
3096 locstr += _(" at ");
3097 locstr += pc_die_line_string (pc, last_scope);
3098
3099 /* Found the "top-level" in which everything was inlined. */
3100 if (tag == DW_TAG_subprogram)
3101 break;
3102
3103 last_scope = scope;
3104 }
3105 }
3106 else
3107 {
3108 locstr += funcname;
3109 locstr += _(" at ");
3110 locstr += pc_die_line_string (pc, NULL);
3111 }
3112 free (scopes);
3113 }
3114
3115 return locstr;
3116 }
3117
3118 struct location *
3119 dwflpp::translate_location(struct obstack *pool,
3120 Dwarf_Attribute *attr, Dwarf_Die *die,
3121 Dwarf_Addr pc,
3122 Dwarf_Attribute *fb_attr,
3123 struct location **tail,
3124 const target_symbol *e)
3125 {
3126
3127 /* DW_AT_data_member_location, can be either constant offsets
3128 (struct member fields), or full blown location expressions. */
3129
3130 /* There is no location expression, but a constant value instead. */
3131 if (dwarf_whatattr (attr) == DW_AT_const_value)
3132 {
3133 *tail = c_translate_constant (pool, &loc2c_error, this,
3134 &loc2c_emit_address, 0, pc, attr);
3135 return *tail;
3136 }
3137
3138 Dwarf_Op *expr;
3139 size_t len;
3140
3141 /* PR9768: formerly, we added pc+module_bias here. However, that bias value
3142 is not present in the pc value by the time we get it, so adding it would
3143 result in false negatives of variable reachibility. In other instances
3144 further below, the c_translate_FOO functions, the module_bias value used
3145 to be passed in, but instead should now be zero for the same reason. */
3146
3147 retry:
3148 switch (dwarf_getlocation_addr (attr, pc /*+ module_bias*/, &expr, &len, 1))
3149 {
3150 case 1: /* Should always happen. */
3151 if (len > 0)
3152 break;
3153 /* Fall through. */
3154
3155 case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */
3156 {
3157 Dwarf_Addr pc2 = pr15123_retry_addr (pc, die);
3158 if (pc2 != 0) {
3159 pc = pc2;
3160 goto retry;
3161 }
3162 }
3163
3164 /* FALLTHROUGH */
3165 {
3166 string msg = _F("not accessible at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc).c_str());
3167 semantic_error err(ERR_SRC, msg, e->tok);
3168 err.details.push_back(die_location_as_string(pc, die));
3169 err.details.push_back(die_location_as_function_string(pc, die));
3170 err.details.push_back(suggested_locations_string(attr));
3171 throw err;
3172 }
3173
3174 default: /* Shouldn't happen. */
3175 case -1:
3176 {
3177 string msg = _F("dwarf_getlocation_addr failed at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc).c_str());
3178 semantic_error err(ERR_SRC, msg, e->tok);
3179 string dwarf_err = _F("dwarf_error: %s", dwarf_errmsg(-1));
3180 err.details.push_back(dwarf_err);
3181 err.details.push_back(die_location_as_string(pc, die));
3182 err.details.push_back(die_location_as_function_string(pc, die));
3183 err.details.push_back(suggested_locations_string(attr));
3184 throw err;
3185 }
3186 }
3187
3188 Dwarf_Op *cfa_ops;
3189 // pc is in the dw address space of the current module, which is what
3190 // c_translate_location expects. get_cfa_ops wants the global dwfl address.
3191 // cfa_ops only make sense for locals.
3192 if (pc)
3193 {
3194 Dwarf_Addr addr = pc + module_bias;
3195 cfa_ops = get_cfa_ops (addr);
3196 }
3197 else
3198 cfa_ops = NULL;
3199
3200 return c_translate_location (pool, &loc2c_error, this,
3201 &loc2c_emit_address,
3202 1, 0 /* PR9768 */,
3203 pc, attr, expr, len, tail, fb_attr, cfa_ops);
3204 }
3205
3206
3207 void
3208 dwflpp::get_members(Dwarf_Die *typedie, set<string>& members, set<string> &dupes)
3209 {
3210 const int typetag = dwarf_tag (typedie);
3211
3212 /* compile and partial unit included for recursion through
3213 imported_unit below. */
3214 if (typetag != DW_TAG_structure_type &&
3215 typetag != DW_TAG_class_type &&
3216 typetag != DW_TAG_union_type &&
3217 typetag != DW_TAG_compile_unit &&
3218 typetag != DW_TAG_partial_unit)
3219 {
3220 throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union",
3221 dwarf_type_name(typedie).c_str()));
3222 }
3223
3224 // Try to get the first child of vardie.
3225 Dwarf_Die die_mem, import;
3226 Dwarf_Die *die = &die_mem;
3227 switch (dwarf_child (typedie, die))
3228 {
3229 case 1: // No children.
3230 throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie).c_str()));
3231
3232 case -1: // Error.
3233 default: // Shouldn't happen.
3234 throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie).c_str(),
3235 dwarf_errmsg(-1)));
3236
3237 case 0: // Success.
3238 break;
3239 }
3240
3241 // Add each sibling's name to members set
3242 do
3243 {
3244 int tag = dwarf_tag(die);
3245
3246 /* The children of an imported_unit should be treated as members too. */
3247 if (tag == DW_TAG_imported_unit
3248 && dwarf_attr_die(die, DW_AT_import, &import))
3249 get_members(&import, members, dupes);
3250
3251 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3252 continue;
3253
3254 const char *member = dwarf_diename (die) ;
3255
3256 if ( tag == DW_TAG_member && member != NULL )
3257 {
3258 // Only output if this is new, to avoid inheritance dupes.
3259 if (dupes.insert(member).second)
3260 members.insert(member);
3261 }
3262 else
3263 {
3264 Dwarf_Die temp_die;
3265 if (!dwarf_attr_die (die, DW_AT_type, &temp_die))
3266 {
3267 string source = dwarf_decl_file(die) ?: "<unknown source>";
3268 int line = -1;
3269 dwarf_decl_line(die, &line);
3270 throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous "
3271 "member at %s:%d", source.c_str(), line));
3272 }
3273
3274 get_members(&temp_die, members, dupes);
3275 }
3276
3277 }
3278 while (dwarf_siblingof (die, die) == 0);
3279 }
3280
3281
3282 bool
3283 dwflpp::find_struct_member(const target_symbol::component& c,
3284 Dwarf_Die *parentdie,
3285 Dwarf_Die *memberdie,
3286 vector<Dwarf_Die>& dies,
3287 vector<Dwarf_Attribute>& locs)
3288 {
3289 Dwarf_Attribute attr;
3290 Dwarf_Die die;
3291
3292 /* With inheritance, a subclass may mask member names of parent classes, so
3293 * our search among the inheritance tree must be breadth-first rather than
3294 * depth-first (recursive). The parentdie is still our starting point. */
3295 deque<Dwarf_Die> inheritees(1, *parentdie);
3296 for (; !inheritees.empty(); inheritees.pop_front())
3297 {
3298 switch (dwarf_child (&inheritees.front(), &die))
3299 {
3300 case 0: /* First child found. */
3301 break;
3302 case 1: /* No children. */
3303 continue;
3304 case -1: /* Error. */
3305 default: /* Shouldn't happen */
3306 throw SEMANTIC_ERROR (dwarf_type_name(&inheritees.front()) + ": "
3307 + string (dwarf_errmsg (-1)),
3308 c.tok);
3309 }
3310
3311 do
3312 {
3313 int tag = dwarf_tag(&die);
3314 /* recurse into imported units as if they are anonymoust structs */
3315 Dwarf_Die import;
3316 if (tag == DW_TAG_imported_unit
3317 && dwarf_attr_die(&die, DW_AT_import, &import)
3318 && find_struct_member(c, &import, memberdie, dies, locs))
3319 goto success;
3320
3321 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3322 continue;
3323
3324 const char *name = dwarf_diename(&die);
3325 if (tag == DW_TAG_inheritance)
3326 {
3327 /* Remember inheritee for breadth-first search. */
3328 Dwarf_Die inheritee;
3329 if (dwarf_attr_die (&die, DW_AT_type, &inheritee))
3330 inheritees.push_back(inheritee);
3331 }
3332 else if (name == NULL)
3333 {
3334 /* Need to recurse for anonymous structs/unions. */
3335 Dwarf_Die subdie;
3336 if (dwarf_attr_die (&die, DW_AT_type, &subdie) &&
3337 find_struct_member(c, &subdie, memberdie, dies, locs))
3338 goto success;
3339 }
3340 else if (name == c.member)
3341 {
3342 *memberdie = die;
3343 goto success;
3344 }
3345 }
3346 while (dwarf_siblingof (&die, &die) == 0);
3347 }
3348
3349 return false;
3350
3351 success:
3352 /* As we unwind the recursion, we need to build the chain of
3353 * locations that got to the final answer. */
3354 if (dwarf_attr_integrate (&die, DW_AT_data_member_location, &attr))
3355 {
3356 dies.insert(dies.begin(), die);
3357 locs.insert(locs.begin(), attr);
3358 }
3359
3360 /* Union members don't usually have a location,
3361 * but just use the containing union's location. */
3362 else if (dwarf_tag(parentdie) != DW_TAG_union_type)
3363 throw SEMANTIC_ERROR (_F("no location for field '%s':%s",
3364 c.member.c_str(), dwarf_errmsg(-1)), c.tok);
3365
3366 return true;
3367 }
3368
3369
3370 static inline void
3371 dwarf_die_type (Dwarf_Die *die, Dwarf_Die *typedie_mem, const token *tok=NULL)
3372 {
3373 if (!dwarf_attr_die (die, DW_AT_type, typedie_mem))
3374 throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok);
3375 }
3376
3377
3378 void
3379 dwflpp::translate_components(struct obstack *pool,
3380 struct location **tail,
3381 Dwarf_Addr pc,
3382 const target_symbol *e,
3383 Dwarf_Die *vardie,
3384 Dwarf_Die *typedie,
3385 unsigned first)
3386 {
3387 unsigned i = first;
3388 while (i < e->components.size())
3389 {
3390 const target_symbol::component& c = e->components[i];
3391
3392 /* XXX: This would be desirable, but we don't get the target_symbol token,
3393 and printing that gives us the file:line number too early anyway. */
3394 #if 0
3395 // Emit a marker to note which field is being access-attempted, to give
3396 // better error messages if deref() fails.
3397 string piece = string(...target_symbol token...) + string ("#") + lex_cast(components[i].second);
3398 obstack_printf (pool, "c->last_stmt = %s;", lex_cast_qstring(piece).c_str());
3399 #endif
3400
3401 switch (dwarf_tag (typedie))
3402 {
3403 case DW_TAG_typedef:
3404 case DW_TAG_const_type:
3405 case DW_TAG_volatile_type:
3406 case DW_TAG_restrict_type:
3407 /* Just iterate on the referent type. */
3408 dwarf_die_type (typedie, typedie, c.tok);
3409 break;
3410
3411 case DW_TAG_reference_type:
3412 case DW_TAG_rvalue_reference_type:
3413 if (pool)
3414 c_translate_pointer (pool, 1, 0 /* PR9768*/, typedie, tail);
3415 dwarf_die_type (typedie, typedie, c.tok);
3416 break;
3417
3418 case DW_TAG_pointer_type:
3419 /* A pointer with no type is a void* -- can't dereference it. */
3420 if (!dwarf_hasattr_integrate (typedie, DW_AT_type))
3421 throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c).c_str(),
3422 dwarf_type_name(typedie).c_str()), c.tok);
3423
3424 if (pool)
3425 c_translate_pointer (pool, 1, 0 /* PR9768*/, typedie, tail);
3426 if (c.type != target_symbol::comp_literal_array_index &&
3427 c.type != target_symbol::comp_expression_array_index)
3428 {
3429 dwarf_die_type (typedie, typedie, c.tok);
3430 break;
3431 }
3432 /* else fall through as an array access */
3433
3434 case DW_TAG_array_type:
3435 if (c.type == target_symbol::comp_literal_array_index)
3436 {
3437 if (pool)
3438 c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail,
3439 NULL, c.num_index);
3440 }
3441 else if (c.type == target_symbol::comp_expression_array_index)
3442 {
3443 string index = "STAP_ARG_index" + lex_cast(i);
3444 if (pool)
3445 c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail,
3446 index.c_str(), 0);
3447 }
3448 else
3449 throw SEMANTIC_ERROR (_F("invalid access '%s' for array type",
3450 lex_cast(c).c_str()), c.tok);
3451
3452 dwarf_die_type (typedie, typedie, c.tok);
3453 *vardie = *typedie;
3454 ++i;
3455 break;
3456
3457 case DW_TAG_structure_type:
3458 case DW_TAG_union_type:
3459 case DW_TAG_class_type:
3460 if (c.type != target_symbol::comp_struct_member)
3461 throw SEMANTIC_ERROR (_F("invalid access '%s' for %s",
3462 lex_cast(c).c_str(), dwarf_type_name(typedie).c_str()));
3463
3464 if (dwarf_hasattr(typedie, DW_AT_declaration))
3465 {
3466 Dwarf_Die *tmpdie = declaration_resolve(typedie);
3467 if (tmpdie == NULL)
3468 throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie).c_str()), c.tok);
3469 *typedie = *tmpdie;
3470 }
3471
3472 {
3473 vector<Dwarf_Die> dies;
3474 vector<Dwarf_Attribute> locs;
3475 if (!find_struct_member(c, typedie, vardie, dies, locs))
3476 {
3477 /* Add a file:line hint for anonymous types */
3478 string source;
3479 if (!dwarf_hasattr_integrate(typedie, DW_AT_name))
3480 {
3481 int line;
3482 const char *file = dwarf_decl_file(typedie);
3483 if (file && dwarf_decl_line(typedie, &line) == 0)
3484 source = " (" + string(file) + ":"
3485 + lex_cast(line) + ")";
3486 }
3487
3488 set<string> members, member_dupes;
3489 get_members(typedie, members, member_dupes);
3490 string sugs = levenshtein_suggest(c.member, members);
3491 if (!sugs.empty())
3492 sugs = " (alternatives: " + sugs + ")";
3493 throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c.member.c_str(),
3494 dwarf_type_name(typedie).c_str(), source.c_str(),
3495 sugs.c_str()), c.tok);
3496 }
3497
3498 for (unsigned j = 0; j < locs.size(); ++j)
3499 if (pool)
3500 translate_location (pool, &locs[j], &dies[j],
3501 pc, NULL, tail, e);
3502 }
3503
3504 dwarf_die_type (vardie, typedie, c.tok);
3505 ++i;
3506 break;
3507
3508 case DW_TAG_enumeration_type:
3509 case DW_TAG_base_type:
3510 throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c).c_str(),
3511 dwarf_type_name(typedie).c_str()), c.tok);
3512 break;
3513
3514 case -1:
3515 throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c.tok);
3516 break;
3517
3518 default:
3519 throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie).c_str(),
3520 lex_cast(dwarf_tag(typedie)).c_str()), c.tok);
3521 break;
3522 }
3523 }
3524 }
3525
3526
3527 void
3528 dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die *typedie,
3529 Dwarf_Die *innerdie,
3530 const target_symbol *e)
3531 {
3532 int typetag = dwarf_tag (typedie);
3533 *innerdie = *typedie;
3534 while (typetag == DW_TAG_typedef ||
3535 typetag == DW_TAG_const_type ||
3536 typetag == DW_TAG_volatile_type ||
3537 typetag == DW_TAG_restrict_type)
3538 {
3539 if (!dwarf_attr_die (innerdie, DW_AT_type, innerdie))
3540 throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e->tok);
3541 typetag = dwarf_tag (innerdie);
3542 }
3543 }
3544
3545
3546 void
3547 dwflpp::translate_final_fetch_or_store (struct obstack *pool,
3548 struct location **tail,
3549 Dwarf_Addr /*module_bias*/,
3550 Dwarf_Die *vardie,
3551 Dwarf_Die *start_typedie,
3552 bool lvalue,
3553 const target_symbol *e,
3554 string &,
3555 string & postlude,
3556 Dwarf_Die *typedie)
3557 {
3558 /* First boil away any qualifiers associated with the type DIE of
3559 the final location to be accessed. */
3560
3561 resolve_unqualified_inner_typedie (start_typedie, typedie, e);
3562
3563 /* If we're looking for an address, then we can just provide what
3564 we computed to this point, without using a fetch/store. */
3565 if (e->addressof)
3566 {
3567 if (lvalue)
3568 throw SEMANTIC_ERROR (_("cannot write to member address"), e->tok);
3569
3570 if (dwarf_hasattr_integrate (vardie, DW_AT_bit_offset))
3571 throw SEMANTIC_ERROR (_("cannot take address of bit-field"), e->tok);
3572
3573 c_translate_addressof (pool, 1, 0, vardie, typedie, tail, "STAP_RETVALUE");
3574 return;
3575 }
3576
3577 /* Then switch behavior depending on the type of fetch/store we
3578 want, and the type and pointer-ness of the final location. */
3579
3580 int typetag = dwarf_tag (typedie);
3581 switch (typetag)
3582 {
3583 default:
3584 throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s", lex_cast(typetag).c_str(),
3585 dwarf_type_name(typedie).c_str()), e->tok);
3586 break;
3587
3588 case DW_TAG_structure_type:
3589 case DW_TAG_class_type:
3590 case DW_TAG_union_type:
3591 throw SEMANTIC_ERROR (_F("'%s' is being accessed instead of a member",
3592 dwarf_type_name(typedie).c_str()), e->tok);
3593 break;
3594
3595 case DW_TAG_base_type:
3596
3597 // Reject types we can't handle in systemtap
3598 {
3599 Dwarf_Attribute encoding_attr;
3600 Dwarf_Word encoding = (Dwarf_Word) -1;
3601 dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding, &encoding_attr),
3602 & encoding);
3603 if (encoding == (Dwarf_Word) -1)
3604 {
3605 // clog << "bad type1 " << encoding << " diestr" << endl;
3606 throw SEMANTIC_ERROR (_F("unsupported type (mystery encoding %s for %s", lex_cast(encoding).c_str(),
3607 dwarf_type_name(typedie).c_str()), e->tok);
3608 }
3609
3610 if (encoding == DW_ATE_float
3611 || encoding == DW_ATE_complex_float
3612 /* XXX || many others? */)
3613 {
3614 // clog << "bad type " << encoding << " diestr" << endl;
3615 throw SEMANTIC_ERROR (_F("unsupported type (encoding %s) for %s", lex_cast(encoding).c_str(),
3616 dwarf_type_name(typedie).c_str()), e->tok);
3617 }
3618 }
3619 // Fallthrough. enumeration_types are always scalar.
3620 case DW_TAG_enumeration_type:
3621
3622 if (lvalue)
3623 c_translate_store (pool, 1, 0 /* PR9768 */, vardie, typedie, tail,
3624 "STAP_ARG_value");
3625 else
3626 c_translate_fetch (pool, 1, 0 /* PR9768 */, vardie, typedie, tail,
3627 "STAP_RETVALUE");
3628 break;
3629
3630 case DW_TAG_array_type:
3631 case DW_TAG_pointer_type:
3632 case DW_TAG_reference_type:
3633 case DW_TAG_rvalue_reference_type:
3634
3635 if (lvalue)
3636 {
3637 if (typetag == DW_TAG_array_type)
3638 throw SEMANTIC_ERROR (_("cannot write to array address"), e->tok);
3639 if (typetag == DW_TAG_reference_type ||
3640 typetag == DW_TAG_rvalue_reference_type)
3641 throw SEMANTIC_ERROR (_("cannot write to reference"), e->tok);
3642 assert (typetag == DW_TAG_pointer_type);
3643 c_translate_pointer_store (pool, 1, 0 /* PR9768 */, typedie, tail,
3644 "STAP_ARG_value");
3645 }
3646 else
3647 {
3648 // We have the pointer: cast it to an integral type via &(*(...))
3649
3650 // NB: per bug #1187, at one point char*-like types were
3651 // automagically converted here to systemtap string values.
3652 // For several reasons, this was taken back out, leaving
3653 // pointer-to-string "conversion" (copying) to tapset functions.
3654
3655 if (typetag == DW_TAG_array_type)
3656 c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail, NULL, 0);
3657 else
3658 c_translate_pointer (pool, 1, 0 /* PR9768 */, typedie, tail);
3659 c_translate_addressof (pool, 1, 0 /* PR9768 */, NULL, NULL, tail,
3660 "STAP_RETVALUE");
3661 }
3662 break;
3663 }
3664
3665 if (lvalue)
3666 postlude += " STAP_RETVALUE = STAP_ARG_value;\n";
3667 }
3668
3669
3670 string
3671 dwflpp::express_as_string (string prelude,
3672 string postlude,
3673 struct location *head)
3674 {
3675 size_t bufsz = 0;
3676 char *buf = 0; // NB: it would leak to pre-allocate a buffer here
3677 FILE *memstream = open_memstream (&buf, &bufsz);
3678 assert(memstream);
3679
3680 fprintf(memstream, "{\n");
3681 fprintf(memstream, "%s", prelude.c_str());
3682
3683 unsigned int stack_depth;
3684 bool deref = c_emit_location (memstream, head, 1, &stack_depth);
3685
3686 // Ensure that DWARF keeps loc2c to a "reasonable" stack size
3687 // 32 intptr_t leads to max 256 bytes on the stack
3688 if (stack_depth > 32)
3689 throw SEMANTIC_ERROR("oversized DWARF stack");
3690
3691 fprintf(memstream, "%s", postlude.c_str());
3692 fprintf(memstream, " goto out;\n");
3693
3694 // dummy use of deref_fault label, to disable warning if deref() not used
3695 fprintf(memstream, "if (0) goto deref_fault;\n");
3696
3697 // XXX: deref flag not reliable; emit fault label unconditionally
3698 (void) deref;
3699 fprintf(memstream,
3700 "deref_fault:\n"
3701 " goto out;\n");
3702 fprintf(memstream, "}\n");
3703
3704 fclose (memstream);
3705 string result(buf);
3706 free (buf);
3707 return result;
3708 }
3709
3710 Dwarf_Addr
3711 dwflpp::vardie_from_symtable (Dwarf_Die *vardie, Dwarf_Addr *addr)
3712 {
3713 const char *name = dwarf_linkage_name (vardie) ?: dwarf_diename (vardie);
3714
3715 if (sess.verbose > 2)
3716 clog << _F("finding symtable address for %s\n", name);
3717
3718 *addr = 0;
3719 int syms = dwfl_module_getsymtab (module);
3720 DWFL_ASSERT (_("Getting symbols"), syms >= 0);
3721
3722 for (int i = 0; *addr == 0 && i < syms; i++)
3723 {
3724 GElf_Sym sym;
3725 GElf_Word shndxp;
3726 const char *symname = dwfl_module_getsym(module, i, &sym, &shndxp);
3727 if (symname
3728 && ! strcmp (name, symname)
3729 && sym.st_shndx != SHN_UNDEF
3730 && (GELF_ST_TYPE (sym.st_info) == STT_NOTYPE // PR13284
3731 || GELF_ST_TYPE (sym.st_info) == STT_OBJECT))
3732 *addr = sym.st_value;
3733 }
3734
3735 // Don't relocate for the kernel, or kernel modules we handle those
3736 // specially in emit_address.
3737 if (dwfl_module_relocations (module) == 1 && module_name != TOK_KERNEL)
3738 dwfl_module_relocate_address (module, addr);
3739
3740 if (sess.verbose > 2)
3741 clog << _F("found %s @%#" PRIx64 "\n", name, *addr);
3742
3743 return *addr;
3744 }
3745
3746 string
3747 dwflpp::literal_stmt_for_local (vector<Dwarf_Die>& scopes,
3748 Dwarf_Addr pc,
3749 string const & local,
3750 const target_symbol *e,
3751 bool lvalue,
3752 Dwarf_Die *die_mem)
3753 {
3754 Dwarf_Die vardie;
3755 Dwarf_Attribute fb_attr_mem, *fb_attr = NULL;
3756
3757 fb_attr = find_variable_and_frame_base (scopes, pc, local, e,
3758 &vardie, &fb_attr_mem);
3759
3760 if (sess.verbose>2)
3761 {
3762 if (pc)
3763 clog << _F("finding location for local '%s' near address %#" PRIx64
3764 ", module bias %#" PRIx64 "\n", local.c_str(), pc,
3765 module_bias);
3766 else
3767 clog << _F("finding location for global '%s' in CU '%s'\n",
3768 local.c_str(), cu_name().c_str());
3769 }
3770
3771
3772 #define obstack_chunk_alloc malloc
3773 #define obstack_chunk_free free
3774
3775 struct obstack pool;
3776 obstack_init (&pool);
3777 struct location *tail = NULL;
3778
3779 /* Given $foo->bar->baz[NN], translate the location of foo. */
3780
3781 struct location *head;
3782
3783 Dwarf_Attribute attr_mem;
3784 if (dwarf_attr_integrate (&vardie, DW_AT_const_value, &attr_mem) == NULL
3785 && dwarf_attr_integrate (&vardie, DW_AT_location, &attr_mem) == NULL)
3786 {
3787 Dwarf_Op addr_loc;
3788 memset(&addr_loc, 0, sizeof(Dwarf_Op));
3789 addr_loc.atom = DW_OP_addr;
3790 // If it is an external variable try the symbol table. PR10622.
3791 if (dwarf_attr_integrate (&vardie, DW_AT_external, &attr_mem) != NULL
3792 && vardie_from_symtable (&vardie, &addr_loc.number) != 0)
3793 {
3794 head = c_translate_location (&pool, &loc2c_error, this,
3795 &loc2c_emit_address,
3796 1, 0, pc,
3797 NULL, &addr_loc, 1, &tail, NULL, NULL);
3798 }
3799 else
3800 {
3801 string msg = _F("failed to retrieve location attribute for '%s' [man error::dwarf]", local.c_str());
3802 semantic_error err(ERR_SRC, msg, e->tok);
3803 err.details.push_back(die_location_as_string(pc, &vardie));
3804 err.details.push_back(die_location_as_function_string(pc, &vardie));
3805 throw err;
3806 }
3807 }
3808 else
3809 head = translate_location (&pool, &attr_mem, &vardie, pc, fb_attr, &tail, e);
3810
3811 /* Translate the ->bar->baz[NN] parts. */
3812
3813 Dwarf_Die typedie;
3814 if (dwarf_attr_die (&vardie, DW_AT_type, &typedie) == NULL)
3815 {
3816 string msg = _F("failed to retrieve type attribute for '%s' [man error::dwarf]", local.c_str());
3817 semantic_error err(ERR_SRC, msg, e->tok);
3818 err.details.push_back(die_location_as_string(pc, &vardie));
3819 err.details.push_back(die_location_as_function_string(pc, &vardie));
3820 throw err;
3821 }
3822
3823 translate_components (&pool, &tail, pc, e, &vardie, &typedie);
3824
3825 /* Translate the assignment part, either
3826 x = $foo->bar->baz[NN]
3827 or
3828 $foo->bar->baz[NN] = x
3829 */
3830
3831 string prelude, postlude;
3832 translate_final_fetch_or_store (&pool, &tail, module_bias,
3833 &vardie, &typedie, lvalue, e,
3834 prelude, postlude, die_mem);
3835
3836 /* Write the translation to a string. */
3837 string result = express_as_string(prelude, postlude, head);
3838 obstack_free (&pool, 0);
3839 return result;
3840 }
3841
3842 Dwarf_Die*
3843 dwflpp::type_die_for_local (vector<Dwarf_Die>& scopes,
3844 Dwarf_Addr pc,
3845 string const & local,
3846 const target_symbol *e,
3847 Dwarf_Die *typedie)
3848 {
3849 Dwarf_Die vardie;
3850 Dwarf_Attribute attr_mem;
3851
3852 find_variable_and_frame_base (scopes, pc, local, e, &vardie, &attr_mem);
3853
3854 if (dwarf_attr_die (&vardie, DW_AT_type, typedie) == NULL)
3855 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local.c_str()), e->tok);
3856
3857 translate_components (NULL, NULL, pc, e, &vardie, typedie);
3858 return typedie;
3859 }
3860
3861
3862 string
3863 dwflpp::literal_stmt_for_return (Dwarf_Die *scope_die,
3864 Dwarf_Addr pc,
3865 const target_symbol *e,
3866 bool lvalue,
3867 Dwarf_Die *die_mem)
3868 {
3869 if (sess.verbose>2)
3870 clog << _F("literal_stmt_for_return: finding return value for %s (%s)\n",
3871 (dwarf_diename(scope_die) ?: "<unknown>"), (dwarf_diename(cu) ?: "<unknown>"));
3872
3873 struct obstack pool;
3874 obstack_init (&pool);
3875 struct location *tail = NULL;
3876
3877 /* Given $return->bar->baz[NN], translate the location of return. */
3878 const Dwarf_Op *locops;
3879 int nlocops = dwfl_module_return_value_location (module, scope_die,
3880 &locops);
3881 if (nlocops < 0)
3882 {
3883 throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)",
3884 (dwarf_diename(scope_die) ?: "<unknown>"),
3885 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
3886 }
3887 // the function has no return value (e.g. "void" in C)
3888 else if (nlocops == 0)
3889 {
3890 throw SEMANTIC_ERROR(_F("function %s (%s) has no return value",
3891 (dwarf_diename(scope_die) ?: "<unknown>"),
3892 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
3893 }
3894
3895 struct location *head = c_translate_location (&pool, &loc2c_error, this,
3896 &loc2c_emit_address,
3897 1, 0 /* PR9768 */,
3898 pc, NULL, locops, nlocops,
3899 &tail, NULL, NULL);
3900
3901 /* Translate the ->bar->baz[NN] parts. */
3902
3903 Dwarf_Die vardie = *scope_die, typedie;
3904 if (dwarf_attr_die (&vardie, DW_AT_type, &typedie) == NULL)
3905 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
3906 (dwarf_diename(&vardie) ?: "<unknown>"),
3907 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
3908
3909 translate_components (&pool, &tail, pc, e, &vardie, &typedie);
3910
3911 /* Translate the assignment part, either
3912 x = $return->bar->baz[NN]
3913 or
3914 $return->bar->baz[NN] = x
3915 */
3916
3917 string prelude, postlude;
3918 translate_final_fetch_or_store (&pool, &tail, module_bias,
3919 &vardie, &typedie, lvalue, e,
3920 prelude, postlude, die_mem);
3921
3922 /* Write the translation to a string. */
3923 string result = express_as_string(prelude, postlude, head);
3924 obstack_free (&pool, 0);
3925 return result;
3926 }
3927
3928 Dwarf_Die*
3929 dwflpp::type_die_for_return (Dwarf_Die *scope_die,
3930 Dwarf_Addr pc,
3931 const target_symbol *e,
3932 Dwarf_Die *typedie)
3933 {
3934 Dwarf_Die vardie = *scope_die;
3935 if (dwarf_attr_die (&vardie, DW_AT_type, typedie) == NULL)
3936 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
3937 (dwarf_diename(&vardie) ?: "<unknown>"),
3938 (dwarf_diename(cu) ?: "<unknown>")), e->tok);
3939
3940 translate_components (NULL, NULL, pc, e, &vardie, typedie);
3941 return typedie;
3942 }
3943
3944
3945 string
3946 dwflpp::literal_stmt_for_pointer (Dwarf_Die *start_typedie,
3947 const target_symbol *e,
3948 bool lvalue,
3949 Dwarf_Die *die_mem)
3950 {
3951 if (sess.verbose>2)
3952 clog << _F("literal_stmt_for_pointer: finding value for %s (%s)\n",
3953 dwarf_type_name(start_typedie).c_str(), (dwarf_diename(cu) ?: "<unknown>"));
3954
3955 struct obstack pool;
3956 obstack_init (&pool);
3957 struct location *head = c_translate_argument (&pool, &loc2c_error, this,
3958 &loc2c_emit_address,
3959 1, "STAP_ARG_pointer");
3960 struct location *tail = head;
3961
3962 /* Translate the ->bar->baz[NN] parts. */
3963
3964 unsigned first = 0;
3965 Dwarf_Die typedie = *start_typedie, vardie = typedie;
3966
3967 /* As a special case when typedie is not an array or pointer, we can
3968 * allow array indexing on STAP_ARG_pointer instead (since we do
3969 * know the pointee type and can determine its size). PR11556. */
3970 const target_symbol::component* c =
3971 e->components.empty() ? NULL : &e->components[0];
3972 if (c && (c->type == target_symbol::comp_literal_array_index ||
3973 c->type == target_symbol::comp_expression_array_index))
3974 {
3975 resolve_unqualified_inner_typedie (&typedie, &typedie, e);
3976 int typetag = dwarf_tag (&typedie);
3977 if (typetag != DW_TAG_pointer_type &&
3978 typetag != DW_TAG_array_type)
3979 {
3980 if (c->type == target_symbol::comp_literal_array_index)
3981 c_translate_array_pointer (&pool, 1, &typedie, &tail, NULL, c->num_index);
3982 else
3983 c_translate_array_pointer (&pool, 1, &typedie, &tail, "STAP_ARG_index0", 0);
3984 ++first;
3985 }
3986 }
3987
3988 /* Now translate the rest normally. */
3989
3990 translate_components (&pool, &tail, 0, e, &vardie, &typedie, first);
3991
3992 /* Translate the assignment part, either
3993 x = (STAP_ARG_pointer)->bar->baz[NN]
3994 or
3995 (STAP_ARG_pointer)->bar->baz[NN] = x
3996 */
3997
3998 string prelude, postlude;
3999 translate_final_fetch_or_store (&pool, &tail, module_bias,
4000 &vardie, &typedie, lvalue, e,
4001 prelude, postlude, die_mem);
4002
4003 /* Write the translation to a string. */
4004 string result = express_as_string(prelude, postlude, head);
4005 obstack_free (&pool, 0);
4006 return result;
4007 }
4008
4009 Dwarf_Die*
4010 dwflpp::type_die_for_pointer (Dwarf_Die *start_typedie,
4011 const target_symbol *e,
4012 Dwarf_Die *typedie)
4013 {
4014 unsigned first = 0;
4015 *typedie = *start_typedie;
4016 Dwarf_Die vardie = *typedie;
4017
4018 /* Handle the same PR11556 case as above. */
4019 const target_symbol::component* c =
4020 e->components.empty() ? NULL : &e->components[0];
4021 if (c && (c->type == target_symbol::comp_literal_array_index ||
4022 c->type == target_symbol::comp_expression_array_index))
4023 {
4024 resolve_unqualified_inner_typedie (typedie, typedie, e);
4025 int typetag = dwarf_tag (typedie);
4026 if (typetag != DW_TAG_pointer_type &&
4027 typetag != DW_TAG_array_type)
4028 ++first;
4029 }
4030
4031 translate_components (NULL, NULL, 0, e, &vardie, typedie, first);
4032 return typedie;
4033 }
4034
4035
4036 static bool
4037 in_kprobes_function(systemtap_session& sess, Dwarf_Addr addr)
4038 {
4039 if (sess.sym_kprobes_text_start != 0 && sess.sym_kprobes_text_end != 0)
4040 {
4041 // If the probe point address is anywhere in the __kprobes
4042 // address range, we can't use this probe point.
4043 if (addr >= sess.sym_kprobes_text_start && addr < sess.sym_kprobes_text_end)
4044 return true;
4045 }
4046 return false;
4047 }
4048
4049
4050 enum dwflpp::blacklisted_type
4051 dwflpp::blacklisted_p(const string& funcname,
4052 const string& filename,
4053 int,
4054 const string& module,
4055 Dwarf_Addr addr,
4056 bool has_return)
4057 {
4058 if (!blacklist_enabled)
4059 return dwflpp::blacklisted_none;
4060
4061 enum dwflpp::blacklisted_type blacklisted = dwflpp::blacklisted_none;
4062
4063 // check against section blacklist
4064 string section = get_blacklist_section(addr);
4065
4066 // PR6503: modules don't need special init/exit treatment
4067 if (module == TOK_KERNEL && !regexec (&blacklist_section, section.c_str(), 0, NULL, 0))
4068 blacklisted = dwflpp::blacklisted_section;
4069
4070 // Check for function marked '__kprobes'.
4071 else if (module == TOK_KERNEL && in_kprobes_function(sess, addr))
4072 blacklisted = dwflpp::blacklisted_kprobes;
4073
4074 // Check probe point against function blacklist
4075 else if (!regexec(&blacklist_func, funcname.c_str(), 0, NULL, 0))
4076 blacklisted = dwflpp::blacklisted_function;
4077
4078 // Check probe point against function return blacklist
4079 else if (has_return && !regexec(&blacklist_func_ret, funcname.c_str(), 0, NULL, 0))
4080 blacklisted = dwflpp::blacklisted_function_return;
4081
4082 // Check probe point against file blacklist
4083 else if (!regexec(&blacklist_file, filename.c_str(), 0, NULL, 0))
4084 blacklisted = dwflpp::blacklisted_file;
4085
4086 if (blacklisted)
4087 {
4088 if (sess.verbose>1)
4089 clog << _(" - blacklisted");
4090 if (sess.guru_mode)
4091 {
4092 blacklisted = dwflpp::blacklisted_none;
4093 if (sess.verbose>1)
4094 clog << _(" but not skipped (guru mode enabled)");
4095 }
4096 }
4097
4098 // This probe point is not blacklisted.
4099 return blacklisted;
4100 }
4101
4102
4103 void
4104 dwflpp::build_kernel_blacklist()
4105 {
4106 // We build up the regexps in these strings
4107
4108 // Add ^ anchors at the front; $ will be added just before regcomp.
4109
4110 string blfn = "^(";
4111 string blfn_ret = "^(";
4112 string blfile = "^(";
4113 string blsection = "^(";
4114
4115 blsection += "\\.init\\."; // first alternative, no "|"
4116 blsection += "|\\.exit\\.";
4117 blsection += "|\\.devinit\\.";
4118 blsection += "|\\.devexit\\.";
4119 blsection += "|\\.cpuinit\\.";
4120 blsection += "|\\.cpuexit\\.";
4121 blsection += "|\\.meminit\\.";
4122 blsection += "|\\.memexit\\.";
4123
4124 /* NOTE all include/asm .h blfile patterns might need "full path"
4125 so prefix those with '.*' - see PR13108 and PR13112. */
4126 blfile += "kernel/kprobes\\.c"; // first alternative, no "|"
4127 blfile += "|arch/.*/kernel/kprobes\\.c";
4128 blfile += "|.*/include/asm/io\\.h";
4129 blfile += "|.*/include/asm/io_64\\.h";
4130 blfile += "|.*/include/asm/bitops\\.h";
4131 blfile += "|drivers/ide/ide-iops\\.c";
4132 // paravirt ops
4133 blfile += "|arch/.*/kernel/paravirt\\.c";
4134 blfile += "|.*/include/asm/paravirt\\.h";
4135
4136 // XXX: it would be nice if these blacklisted functions were pulled
4137 // in dynamically, instead of being statically defined here.
4138 // Perhaps it could be populated from script files. A "noprobe
4139 // kernel.function("...")" construct might do the trick.
4140
4141 // Most of these are marked __kprobes in newer kernels. We list
4142 // them here (anyway) so the translator can block them on older
4143 // kernels that don't have the __kprobes function decorator. This
4144 // also allows detection of problems at translate- rather than
4145 // run-time.
4146
4147 blfn += "atomic_notifier_call_chain"; // first blfn; no "|"
4148 blfn += "|default_do_nmi";
4149 blfn += "|__die";
4150 blfn += "|die_nmi";
4151 blfn += "|do_debug";
4152 blfn += "|do_general_protection";
4153 blfn += "|do_int3";
4154 blfn += "|do_IRQ";
4155 blfn += "|do_page_fault";
4156 blfn += "|do_sparc64_fault";
4157 blfn += "|do_trap";
4158 blfn += "|dummy_nmi_callback";
4159 blfn += "|flush_icache_range";
4160 blfn += "|ia64_bad_break";
4161 blfn += "|ia64_do_page_fault";
4162 blfn += "|ia64_fault";
4163 blfn += "|io_check_error";
4164 blfn += "|mem_parity_error";
4165 blfn += "|nmi_watchdog_tick";
4166 blfn += "|notifier_call_chain";
4167 blfn += "|oops_begin";
4168 blfn += "|oops_end";
4169 blfn += "|program_check_exception";
4170 blfn += "|single_step_exception";
4171 blfn += "|sync_regs";
4172 blfn += "|unhandled_fault";
4173 blfn += "|unknown_nmi_error";
4174 blfn += "|xen_[gs]et_debugreg";
4175 blfn += "|xen_irq_.*";
4176 blfn += "|xen_.*_fl_direct.*";
4177 blfn += "|check_events";
4178 blfn += "|xen_adjust_exception_frame";
4179 blfn += "|xen_iret.*";
4180 blfn += "|xen_sysret64.*";
4181 blfn += "|test_ti_thread_flag.*";
4182 blfn += "|inat_get_opcode_attribute";
4183 blfn += "|system_call_after_swapgs";
4184 blfn += "|HYPERVISOR_[gs]et_debugreg";
4185 blfn += "|HYPERVISOR_event_channel_op";
4186 blfn += "|hash_64";
4187 blfn += "|hash_ptr";
4188 blfn += "|native_set_pte";
4189
4190 // Lots of locks
4191 blfn += "|.*raw_.*_lock.*";
4192 blfn += "|.*raw_.*_unlock.*";
4193 blfn += "|.*raw_.*_trylock.*";
4194 blfn += "|.*read_lock.*";
4195 blfn += "|.*read_unlock.*";
4196 blfn += "|.*read_trylock.*";
4197 blfn += "|.*write_lock.*";
4198 blfn += "|.*write_unlock.*";
4199 blfn += "|.*write_trylock.*";
4200 blfn += "|.*write_seqlock.*";
4201 blfn += "|.*write_sequnlock.*";
4202 blfn += "|.*spin_lock.*";
4203 blfn += "|.*spin_unlock.*";
4204 blfn += "|.*spin_trylock.*";
4205 blfn += "|.*spin_is_locked.*";
4206 blfn += "|rwsem_.*lock.*";
4207 blfn += "|.*mutex_.*lock.*";
4208 blfn += "|raw_.*";
4209
4210 // atomic functions
4211 blfn += "|atomic_.*";
4212 blfn += "|atomic64_.*";
4213
4214 // few other problematic cases
4215 blfn += "|get_bh";
4216 blfn += "|put_bh";
4217
4218 // Experimental
4219 blfn += "|.*apic.*|.*APIC.*";
4220 blfn += "|.*softirq.*";
4221 blfn += "|.*IRQ.*";
4222 blfn += "|.*_intr.*";
4223 blfn += "|__delay";
4224 blfn += "|.*kernel_text.*";
4225 blfn += "|get_current";
4226 blfn += "|current_.*";
4227 blfn += "|.*exception_tables.*";
4228 blfn += "|.*setup_rt_frame.*";
4229
4230 // PR 5759, CONFIG_PREEMPT kernels
4231 blfn += "|.*preempt_count.*";
4232 blfn += "|preempt_schedule";
4233
4234 // These functions don't return, so return probes would never be recovered
4235 blfn_ret += "do_exit"; // no "|"
4236 blfn_ret += "|sys_exit";
4237 blfn_ret += "|sys_exit_group";
4238
4239 // __switch_to changes "current" on x86_64 and i686, so return probes
4240 // would cause kernel panic, and it is marked as "__kprobes" on x86_64
4241 if (sess.architecture == "x86_64")
4242 blfn += "|__switch_to";
4243 if (sess.architecture == "i686")
4244 blfn_ret += "|__switch_to";
4245
4246 // RHEL6 pre-beta 2.6.32-19.el6
4247 blfn += "|special_mapping_.*";
4248 blfn += "|.*_pte_.*"; // or "|smaps_pte_range";
4249 blfile += "|fs/seq_file\\.c";
4250
4251 blfn += ")$";
4252 blfn_ret += ")$";
4253 blfile += ")$";
4254 blsection += ")"; // NB: no $, sections match just the beginning
4255
4256 if (sess.verbose > 2)
4257 {
4258 clog << _("blacklist regexps:") << endl;
4259 clog << "blfn: " << blfn << endl;
4260 clog << "blfn_ret: " << blfn_ret << endl;
4261 clog << "blfile: " << blfile << endl;
4262 clog << "blsection: " << blsection << endl;
4263 }
4264
4265 int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED);
4266 if (rc) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4267 rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED);
4268 if (rc) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4269 rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED);
4270 if (rc) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4271 rc = regcomp (& blacklist_section, blsection.c_str(), REG_NOSUB|REG_EXTENDED);
4272 if (rc) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4273
4274 blacklist_enabled = true;
4275 }
4276
4277
4278 void
4279 dwflpp::build_user_blacklist()
4280 {
4281 // We build up the regexps in these strings
4282
4283 // Add ^ anchors at the front; $ will be added just before regcomp.
4284
4285 string blfn = "^(";
4286 string blfn_ret = "^(";
4287 string blfile = "^(";
4288 string blsection = "^(";
4289
4290 // Non-matching placeholders until we have real things to match
4291 blfn += ".^";
4292 blfile += ".^";
4293 blsection += ".^";
4294
4295 // These functions don't use the normal function-entry ABI, so can't be .return probed safely
4296 blfn_ret += "_start";
4297
4298 blfn += ")$";
4299 blfn_ret += ")$";
4300 blfile += ")$";
4301 blsection += ")"; // NB: no $, sections match just the beginning
4302
4303 if (sess.verbose > 2)
4304 {
4305 clog << _("blacklist regexps:") << endl;
4306 clog << "blfn: " << blfn << endl;
4307 clog << "blfn_ret: " << blfn_ret << endl;
4308 clog << "blfile: " << blfile << endl;
4309 clog << "blsection: " << blsection << endl;
4310 }
4311
4312 int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED);
4313 if (rc) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4314 rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED);
4315 if (rc) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4316 rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED);
4317 if (rc) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4318 rc = regcomp (& blacklist_section, blsection.c_str(), REG_NOSUB|REG_EXTENDED);
4319 if (rc) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4320
4321 blacklist_enabled = true;
4322 }
4323
4324
4325 string
4326 dwflpp::get_blacklist_section(Dwarf_Addr addr)
4327 {
4328 string blacklist_section;
4329 Dwarf_Addr bias;
4330 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
4331 // because dwfl_module_getelf can force costly section relocations
4332 // we don't really need, while either will do for this purpose.
4333 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (module, &bias))
4334 ?: dwfl_module_getelf (module, &bias));
4335
4336 Dwarf_Addr offset = addr - bias;
4337 if (elf)
4338 {
4339 Elf_Scn* scn = 0;
4340 size_t shstrndx;
4341 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
4342 while ((scn = elf_nextscn (elf, scn)) != NULL)
4343 {
4344 GElf_Shdr shdr_mem;
4345 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4346 if (! shdr)
4347 continue; // XXX error?
4348
4349 if (!(shdr->sh_flags & SHF_ALLOC))
4350 continue;
4351
4352 GElf_Addr start = shdr->sh_addr;
4353 GElf_Addr end = start + shdr->sh_size;
4354 if (! (offset >= start && offset < end))
4355 continue;
4356
4357 blacklist_section = elf_strptr (elf, shstrndx, shdr->sh_name);
4358 break;
4359 }
4360 }
4361 return blacklist_section;
4362 }
4363
4364
4365 /* Find the section named 'section_name' in the current module
4366 * returning the section header using 'shdr_mem' */
4367
4368 GElf_Shdr *
4369 dwflpp::get_section(string section_name, GElf_Shdr *shdr_mem, Elf **elf_ret)
4370 {
4371 GElf_Shdr *shdr = NULL;
4372 Elf* elf;
4373 Dwarf_Addr bias;
4374 size_t shstrndx;
4375
4376 // Explicitly look in the main elf file first.
4377 elf = dwfl_module_getelf (module, &bias);
4378 Elf_Scn *probe_scn = NULL;
4379
4380 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
4381
4382 bool have_section = false;
4383
4384 while ((probe_scn = elf_nextscn (elf, probe_scn)))
4385 {
4386 shdr = gelf_getshdr (probe_scn, shdr_mem);
4387 assert (shdr != NULL);
4388
4389 if (elf_strptr (elf, shstrndx, shdr->sh_name) == section_name)
4390 {
4391 have_section = true;
4392 break;
4393 }
4394 }
4395
4396 // Older versions may put the section in the debuginfo dwarf file,
4397 // so check if it actually exists, if not take a look in the debuginfo file
4398 if (! have_section || (have_section && shdr->sh_type == SHT_NOBITS))
4399 {
4400 elf = dwarf_getelf (dwfl_module_getdwarf (module, &bias));
4401 if (! elf)
4402 return NULL;
4403 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
4404 probe_scn = NULL;
4405 while ((probe_scn = elf_nextscn (elf, probe_scn)))
4406 {
4407 shdr = gelf_getshdr (probe_scn, shdr_mem);
4408 if (elf_strptr (elf, shstrndx, shdr->sh_name) == section_name)
4409 {
4410 have_section = true;
4411 break;
4412 }
4413 }
4414 }
4415
4416 if (!have_section)
4417 return NULL;
4418
4419 if (elf_ret)
4420 *elf_ret = elf;
4421 return shdr;
4422 }
4423
4424
4425 Dwarf_Addr
4426 dwflpp::relocate_address(Dwarf_Addr dw_addr, string& reloc_section)
4427 {
4428 // PR10273
4429 // libdw address, so adjust for bias gotten from dwfl_module_getdwarf
4430 Dwarf_Addr reloc_addr = dw_addr + module_bias;
4431 if (!module)
4432 {
4433 assert(module_name == TOK_KERNEL);
4434 reloc_section = "";
4435 }
4436 else if (dwfl_module_relocations (module) > 0)
4437 {
4438 // This is a relocatable module; libdwfl already knows its
4439 // sections, so we can relativize addr.
4440 int idx = dwfl_module_relocate_address (module, &reloc_addr);
4441 const char* r_s = dwfl_module_relocation_info (module, idx, NULL);
4442 if (r_s)
4443 reloc_section = r_s;
4444
4445 if (reloc_section == "" && dwfl_module_relocations (module) == 1)
4446 reloc_section = ".dynamic";
4447 }
4448 else
4449 reloc_section = ".absolute";
4450 return reloc_addr;
4451 }
4452
4453 /* Returns the call frame address operations for the given program counter
4454 * in the libdw address space.
4455 */
4456 Dwarf_Op *
4457 dwflpp::get_cfa_ops (Dwarf_Addr pc)
4458 {
4459 Dwarf_Op *cfa_ops = NULL;
4460
4461 if (sess.verbose > 2)
4462 clog << "get_cfa_ops @0x" << hex << pc << dec
4463 << ", module_start @0x" << hex << module_start << dec << endl;
4464
4465 // Try debug_frame first, then fall back on eh_frame.
4466 size_t cfa_nops = 0;
4467 Dwarf_Addr bias = 0;
4468 Dwarf_Frame *frame = NULL;
4469 Dwarf_CFI *cfi = dwfl_module_dwarf_cfi (module, &bias);
4470 if (cfi != NULL)
4471 {
4472 if (sess.verbose > 3)
4473 clog << "got dwarf cfi bias: 0x" << hex << bias << dec << endl;
4474 if (dwarf_cfi_addrframe (cfi, pc - bias, &frame) == 0)
4475 dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops);
4476 else if (sess.verbose > 3)
4477 clog << "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl;
4478 }
4479 else if (sess.verbose > 3)
4480 clog << "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl;
4481
4482 if (cfa_ops == NULL)
4483 {
4484 cfi = dwfl_module_eh_cfi (module, &bias);
4485 if (cfi != NULL)
4486 {
4487 if (sess.verbose > 3)
4488 clog << "got eh cfi bias: 0x" << hex << bias << dec << endl;
4489 Dwarf_Frame *frame = NULL;
4490 if (dwarf_cfi_addrframe (cfi, pc - bias, &frame) == 0)
4491 dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops);
4492 else if (sess.verbose > 3)
4493 clog << "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl;
4494 }
4495 else if (sess.verbose > 3)
4496 clog << "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl;
4497
4498 }
4499
4500 if (sess.verbose > 2)
4501 {
4502 if (cfa_ops == NULL)
4503 clog << _("not found cfa") << endl;
4504 else
4505 {
4506 Dwarf_Addr frame_start, frame_end;
4507 bool frame_signalp;
4508 int info = dwarf_frame_info (frame, &frame_start, &frame_end,
4509 &frame_signalp);
4510 clog << _F("found cfa, info: %d [start: %#" PRIx64 ", end: %#" PRIx64
4511 ", nops: %zu", info, frame_start, frame_end, cfa_nops) << endl;
4512 }
4513 }
4514
4515 return cfa_ops;
4516 }
4517
4518 int
4519 dwflpp::add_module_build_id_to_hash (Dwfl_Module *m,
4520 void **userdata __attribute__ ((unused)),
4521 const char *name,
4522 Dwarf_Addr base,
4523 void *arg)
4524 {
4525 string modname = name;
4526 systemtap_session * s = (systemtap_session *)arg;
4527 if (pending_interrupts)
4528 return DWARF_CB_ABORT;
4529
4530 // Extract the build ID
4531 const unsigned char *bits;
4532 GElf_Addr vaddr;
4533 int bits_length = dwfl_module_build_id(m, &bits, &vaddr);
4534 if(bits_length > 0)
4535 {
4536 // Convert the binary bits to a hex string
4537 string hex = hex_dump(bits, bits_length);
4538
4539 // Store the build ID in the session
4540 s->build_ids.push_back(hex);
4541 }
4542
4543 return DWARF_CB_OK;
4544 }
4545
4546
4547
4548 // Perform PR15123 heuristic for given variable at given address.
4549 // Return alternate pc address to do location-list lookup at, or 0 if
4550 // inapplicable.
4551 //
4552 Dwarf_Addr
4553 dwflpp::pr15123_retry_addr (Dwarf_Addr pc, Dwarf_Die* die)
4554 {
4555 // For PR15123, we'd like to detect the situation where the
4556 // incoming PC may point to a couple-of-byte instruction
4557 // sequence that gcc emits for CFLAGS=-mfentry, and where
4558 // context variables are in fact available throughout, *but* due
4559 // to the bug, the dwarf debuginfo location-list only starts a
4560 // few instructions later. Prologue searching does not resolve
4561 // this as a line-record is in place at the -mfentry prologue.
4562 //
4563 // Detecting this is complicated because ...
4564 // - we only want to do this if -mfentry was actually used
4565 // - if <pc> points to the a function entry point
4566 // - if the architecture is familiar enough that we can have a
4567 // hard-coded constant to skip over the prologue.
4568 //
4569 // Otherwise, we could give a false-positive - return corrupted
4570 // data.
4571 //
4572 // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches
4573 // was used. Without it, set the PR15123_ASSUME_MFENTRY environment
4574 // variable to override the -mfentry test.
4575
4576 if (getenv ("PR15123_DISABLE"))
4577 return 0;
4578
4579 if (!getenv ("PR15123_ASSUME_MFENTRY")) {
4580 Dwarf_Die cudie;
4581 string producer, version;
4582 dwarf_diecu (die, &cudie, NULL, NULL);
4583 if (!is_gcc_producer(&cudie, producer, version))
4584 return 0;
4585 if (producer.find("-mfentry") == string::npos)
4586 return 0;
4587 }
4588
4589 // Determine if this pc maps to the beginning of a
4590 // real function (not some inlined doppelganger. This
4591 // is made tricker by this->function may not be
4592 // pointing at the right DIE (say e.g. stap encountered
4593 // the inlined copy first, so was focus_on_function'd).
4594 vector<Dwarf_Die> scopes = getscopes(pc);
4595 if (scopes.size() == 0)
4596 return 0;
4597
4598 Dwarf_Die outer_function_die = scopes[0];
4599 Dwarf_Addr entrypc;
4600 if (!die_entrypc(& outer_function_die, &entrypc) || entrypc != pc)
4601 return 0; // (will fail on retry, so we won't loop more than once)
4602
4603 if (sess.architecture == "i386" ||
4604 sess.architecture == "x86_64") {
4605 /* pull the trigger */
4606 if (sess.verbose > 2)
4607 clog << _("retrying variable location-list lookup at address pc+5\n");
4608 return pc + 5;
4609 }
4610
4611 return 0;
4612 }
4613
4614 bool
4615 dwflpp::has_gnu_debugdata ()
4616 {
4617 Dwarf_Addr load_addr;
4618 // Note we really want the actual elf file, not the dwarf .debug file.
4619 Elf* elf = dwfl_module_getelf (module, &load_addr);
4620 size_t shstrndx;
4621 assert (elf_getshdrstrndx (elf, &shstrndx) >= 0);
4622
4623 // Get the gnu_debugdata section header
4624 Elf_Scn *scn = NULL;
4625 GElf_Shdr *gnu_debugdata_shdr = NULL;
4626 GElf_Shdr gnu_debugdata_shdr_mem;
4627 while ((scn = elf_nextscn (elf, scn)))
4628 {
4629 gnu_debugdata_shdr = gelf_getshdr (scn, &gnu_debugdata_shdr_mem);
4630 assert (gnu_debugdata_shdr != NULL);
4631 if (strcmp (elf_strptr (elf, shstrndx, gnu_debugdata_shdr->sh_name), ".gnu_debugdata") == 0)
4632 return true;
4633 }
4634 return false;
4635 }
4636
4637 // If not GCC, return false. Otherwise, return true and set vers.
4638 bool
4639 dwflpp::is_gcc_producer(Dwarf_Die *cudie, string& producer, string& version)
4640 {
4641 Dwarf_Attribute producer_attr;
4642 if (!dwarf_attr_integrate(cudie, DW_AT_producer, &producer_attr))
4643 return false;
4644
4645 // GNU {C|C++|...} x.x.x YYYYMMDD ...
4646 const char *cproducer = dwarf_formstring(&producer_attr);
4647 if (!cproducer)
4648 return false;
4649 producer = cproducer;
4650
4651 vector<string> tokens;
4652 tokenize(producer, tokens);
4653
4654 if (tokens.size() < 3
4655 || tokens[0] != "GNU"
4656 || tokens[2].find_first_not_of(".0123456789") != string::npos)
4657 return false;
4658
4659 version = tokens[2];
4660 return true;
4661 }
4662
4663 static bool
4664 die_has_loclist(Dwarf_Die *begin_die)
4665 {
4666 Dwarf_Die die;
4667 Dwarf_Attribute loc;
4668
4669 if (dwarf_child(begin_die, &die) != 0)
4670 return false;
4671
4672 do
4673 {
4674 switch (dwarf_tag(&die))
4675 {
4676 case DW_TAG_formal_parameter:
4677 case DW_TAG_variable:
4678 if (dwarf_attr_integrate(&die, DW_AT_location, &loc)
4679 && dwarf_whatform(&loc) == DW_FORM_sec_offset)
4680 return true;
4681 break;
4682 default:
4683 if (dwarf_haschildren (&die))
4684 if (die_has_loclist(&die))
4685 return true;
4686 break;
4687 }
4688 }
4689 while (dwarf_siblingof (&die, &die) == 0);
4690
4691 return false;
4692 }
4693
4694 bool
4695 dwflpp::has_valid_locs ()
4696 {
4697 assert(cu);
4698
4699 // The current CU has valid location info (implying we do not need to skip the
4700 // prologue) if
4701 // - it was compiled with -O2 -g (in which case, GCC outputs proper location
4702 // info for the prologue), and
4703 // - it was compiled by GCC >= 4.5 (previous versions could have had invalid
4704 // debug info in the prologue, see GDB's PR13777)
4705 // Note that clang behaves similarly to GCC here: non-optimized code does not
4706 // have location lists, while optimized code does. In the check below, even if
4707 // the producer is not GCC, we assume that it is valid to do the loclist check
4708 // afterwards (which it is for clang).
4709
4710 string prod, vers;
4711 if (is_gcc_producer(cu, prod, vers)
4712 && strverscmp(vers.c_str(), "4.5") < 0)
4713 return false;
4714
4715 // We determine if the current CU has been optimized with -O2 -g by looking
4716 // for any data objects whose DW_AT_location is a location list. This is also
4717 // how GDB determines whether to skip the prologue or not. See GDB's PR12573
4718 // and also RHBZ612253#c6.
4719 if (!die_has_loclist(cu))
4720 return false;
4721
4722 if (sess.verbose > 2)
4723 clog << _F("CU '%s' in module '%s' has valid locs",
4724 cu_name().c_str(), module_name.c_str()) << endl;
4725
4726 return true;
4727 }
4728
4729 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.252362 seconds and 5 git commands to generate.