1 // C++ interface to dwfl
2 // Copyright (C) 2005-2019 Red Hat Inc.
3 // Copyright (C) 2005-2007 Intel Corporation.
4 // Copyright (C) 2008 James.Bottomley@HansenPartnership.com
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
15 #include "elaborate.h"
17 #include "task_finder.h"
18 #include "translate.h"
22 #include "dwarf_wrappers.h"
24 #include "rpm_finder.h"
25 #include "setupdwfl.h"
44 #include <elfutils/libdwfl.h>
45 #include <elfutils/libdw.h>
52 #include <sys/types.h>
54 #define __STDC_FORMAT_MACROS
58 // Older glibc elf.h don't know about this new constant.
59 #ifndef STB_GNU_UNIQUE
60 #define STB_GNU_UNIQUE 10
64 // debug flag to compare to the uncached version from libdw
65 // #define DEBUG_DWFLPP_GETSCOPES 1
69 using namespace __gnu_cxx
;
72 static string
TOK_KERNEL("kernel");
75 dwflpp::dwflpp(systemtap_session
& session
, const string
& name
, bool kernel_p
):
76 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
77 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
78 module_dwarf(NULL
), function(NULL
), blacklist_func(), blacklist_func_ret(),
79 blacklist_file(), blacklist_enabled(false)
82 setup_kernel(name
, session
);
85 vector
<string
> modules
;
86 modules
.push_back(name
);
91 dwflpp::dwflpp(systemtap_session
& session
, const vector
<string
>& names
,
93 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
94 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
95 module_dwarf(NULL
), function(NULL
), blacklist_enabled(false)
105 delete_map(module_cu_cache
);
106 delete_map(cu_function_cache
);
107 delete_map(mod_function_cache
);
108 delete_map(cu_inl_function_cache
);
109 delete_map(cu_call_sites_cache
);
110 delete_map(global_alias_cache
);
111 delete_map(cu_die_parent_cache
);
113 for (auto i
= cu_lines_cache
.begin(); i
!= cu_lines_cache
.end(); ++i
)
114 delete_map(*i
->second
);
115 delete_map(cu_lines_cache
);
117 delete_map(cu_entry_pc_cache
);
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.
127 module_cache::~module_cache ()
134 dwflpp::get_module_dwarf(bool required
, bool report
)
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
)
140 string msg
= _("cannot find ");
141 if (module_name
== "")
144 msg
+= string("module ") + module_name
;
147 int i
= dwfl_errno();
149 msg
+= string(": ") + dwfl_errmsg (i
);
151 msg
+= " [man warning::debuginfo]";
153 /* add module_name to list to find rpm */
154 find_debug_rpms(sess
, module_name
.c_str());
157 throw SEMANTIC_ERROR (msg
);
159 sess
.print_warning(msg
);
165 dwflpp::focus_on_module(Dwfl_Module
* m
, module_info
* mi
)
171 module_name
= dwfl_module_info(module
, NULL
, &module_start
, &module_end
,
172 NULL
, NULL
, NULL
, NULL
) ?: "module";
176 assert(mi
&& mi
->name
&& mi
->name
== TOK_KERNEL
);
177 module_name
= mi
->name
;
180 module_bias
= mi
->bias
;
183 // Reset existing pointers and names
189 function_name
.clear();
195 dwflpp::focus_on_cu(Dwarf_Die
* c
)
202 // Reset existing pointers and names
203 function_name
.clear();
209 dwflpp::cu_name(void)
211 return dwarf_diename(cu
) ?: "<unknown source>";
216 dwflpp::focus_on_function(Dwarf_Die
* f
)
223 function_name
= dwarf_diename(function
) ?: "function";
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).
232 dwflpp::query_cu_containing_address(Dwarf_Addr a
)
239 Dwarf_Die
* cudie
= dwfl_module_addrdie(module
, a
, &bias
);
240 assert(bias
== module_bias
);
246 dwflpp::module_name_matches(const string
& pattern
)
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());
261 dwflpp::name_has_wildcard (const string
& pattern
)
263 return (pattern
.find('*') != string::npos
||
264 pattern
.find('?') != string::npos
||
265 pattern
.find('[') != string::npos
);
270 dwflpp::module_name_final_match(const string
& pattern
)
272 // Assume module_name_matches(). Can there be any more matches?
273 // Not unless the pattern is a wildcard, since module names are
275 return !name_has_wildcard(pattern
);
280 dwflpp::function_name_matches_pattern(const string
& name
, const string
& pattern
)
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());
290 dwflpp::function_name_matches(const string
& pattern
)
293 return function_name_matches_pattern(function_name
, pattern
);
298 dwflpp::function_scope_matches(const vector
<string
>& scopes
)
300 // walk up the containing scopes
301 Dwarf_Die
* die
= function
;
302 for (int i
= scopes
.size() - 1; i
>= 0; --i
)
304 die
= get_parent_scope(die
);
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
]) :
312 function_name
= name
+ "::" + function_name
;
316 // make sure there's no more if we're at the global scope
325 dwflpp::setup_kernel(const string
& name
, systemtap_session
& s
, bool debuginfo_needed
)
327 if (! sess
.module_cache
)
328 sess
.module_cache
= new module_cache ();
330 unsigned offline_search_matches
= 0;
331 dwfl
= setup_dwfl_kernel(name
, &offline_search_matches
, sess
);
333 if (offline_search_matches
< 1)
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());
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()));
349 assert_no_interrupts();
350 off
= dwfl_getmodules (dwfl
, &add_module_build_id_to_hash
, &s
, off
);
353 DWFL_ASSERT("dwfl_getmodules", off
== 0);
356 build_kernel_blacklist();
360 dwflpp::setup_kernel(const vector
<string
> &names
, bool debuginfo_needed
)
362 if (! sess
.module_cache
)
363 sess
.module_cache
= new module_cache ();
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
,
371 if (offline_search_matches
< offline_search_names
.size())
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());
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()));
382 build_kernel_blacklist();
387 dwflpp::setup_user(const vector
<string
>& modules
, bool debuginfo_needed
)
389 if (! sess
.module_cache
)
390 sess
.module_cache
= new module_cache ();
392 auto it
= modules
.begin();
393 dwfl
= setup_dwfl_user(it
, modules
.end(), debuginfo_needed
, sess
);
394 if (debuginfo_needed
&& it
!= modules
.end())
395 DWFL_ASSERT (string(_F("missing process %s %s debuginfo",
396 (*it
).c_str(), sess
.architecture
.c_str())),
399 build_user_blacklist();
403 dwflpp::iterate_over_modules
<void>(int (*callback
)(Dwfl_Module
*,
410 dwfl_getmodules (dwfl
, callback
, data
, 0);
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);
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.
424 dwflpp::iterate_over_cus
<void>(int (*callback
)(Dwarf_Die
*, void*),
428 get_module_dwarf(false);
429 Dwarf
*dw
= module_dwarf
;
432 vector
<Dwarf_Die
>* v
= module_cu_cache
[dw
];
435 v
= new vector
<Dwarf_Die
>;
436 module_cu_cache
[dw
] = v
;
441 while (dwarf_nextcu (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) == 0)
443 assert_no_interrupts();
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 */
454 if (want_types
&& module_tus_read
.find(dw
) == module_tus_read
.end())
456 // Process type units.
460 uint64_t type_signature
;
461 while (dwarf_next_unit (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
, NULL
,
462 &type_signature
, NULL
) == 0)
464 assert_no_interrupts();
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 */
473 module_tus_read
.insert(dw
);
476 for (auto i
= v
->begin(); i
!= v
->end(); ++i
)
478 int rc
= (*callback
)(&*i
, data
);
479 assert_no_interrupts();
480 if (rc
!= DWARF_CB_OK
)
487 dwflpp::func_is_inline()
490 return dwarf_func_inline (function
) != 0;
495 dwflpp::func_is_exported()
497 const char *name
= dwarf_linkage_name (function
) ?: dwarf_diename (function
);
501 int syms
= dwfl_module_getsymtab (module
);
502 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
504 for (int i
= 0; i
< syms
; i
++)
508 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
510 && strcmp (name
, symname
) == 0)
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
))
525 dwflpp::cache_inline_instances (Dwarf_Die
* die
)
527 // If this is an inline instance, link it back to its origin
529 if (dwarf_tag(die
) == DW_TAG_inlined_subroutine
&&
530 dwarf_attr_die(die
, DW_AT_abstract_origin
, &origin
))
532 vector
<Dwarf_Die
>*& v
= cu_inl_function_cache
[origin
.addr
];
534 v
= new vector
<Dwarf_Die
>;
538 // Recurse through other scopes that may contain inlines
539 Dwarf_Die child
, import
;
540 if (dwarf_child(die
, &child
) == 0)
543 switch (dwarf_tag (&child
))
545 // tags that could contain inlines
546 case DW_TAG_compile_unit
:
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
);
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
);
564 // nothing to do for other tags
569 while (dwarf_siblingof(&child
, &child
) == 0);
574 dwflpp::iterate_over_inline_instances
<void>(int (*callback
)(Dwarf_Die
*, void*),
578 assert (func_is_inline ());
580 if (cu_inl_function_cache_done
.insert(cu
->addr
).second
)
581 cache_inline_instances(cu
);
583 vector
<Dwarf_Die
>* v
= cu_inl_function_cache
[function
->addr
];
587 for (auto i
= v
->begin(); i
!= v
->end(); ++i
)
589 int rc
= (*callback
)(&*i
, data
);
590 assert_no_interrupts();
591 if (rc
!= DWARF_CB_OK
)
597 void dwflpp::cache_call_sites (Dwarf_Die
* die
, Dwarf_Die
*function
)
600 if (dwarf_tag(die
) == DW_TAG_GNU_call_site
&&
601 dwarf_attr_die(die
, DW_AT_abstract_origin
, &origin
))
603 vector
<call_site_cache_t
>*& v
= cu_call_sites_cache
[origin
.addr
];
605 v
= new vector
<call_site_cache_t
>;
606 call_site_cache_t
c (*die
, *function
);
611 if (dwarf_child(die
, &child
) == 0)
614 switch (dwarf_tag (&child
))
616 // tags that could contain call sites
617 case DW_TAG_compile_unit
:
619 case DW_TAG_lexical_block
:
620 case DW_TAG_with_stmt
:
621 case DW_TAG_catch_block
:
622 case DW_TAG_try_block
:
623 case DW_TAG_entry_point
:
624 case DW_TAG_GNU_call_site
:
625 cache_call_sites(&child
, function
);
628 case DW_TAG_subprogram
:
629 case DW_TAG_inlined_subroutine
:
630 cache_call_sites(&child
, &child
);
632 // nothing to do for other tags
637 while (dwarf_siblingof(&child
, &child
) == 0);
641 dwflpp::iterate_over_call_sites
<void> (int (*callback
)(Dwarf_Die
*, Dwarf_Die
*, void*),
647 if (cu_call_sites_cache_done
.insert(cu
->addr
).second
)
648 cache_call_sites(cu
, NULL
);
650 vector
<call_site_cache_t
>* v
= cu_call_sites_cache
[function
->addr
];
654 for (auto i
= v
->begin(); i
!= v
->end(); ++i
)
656 int rc
= (*callback
)(&i
->first
, &i
->second
, data
);
657 if (rc
!= DWARF_CB_OK
)
664 dwflpp::cache_die_parents(cu_die_parent_cache_t
* parents
, Dwarf_Die
* die
)
666 // Record and recurse through DIEs we care about
667 Dwarf_Die child
, import
;
668 if (dwarf_child(die
, &child
) == 0)
671 switch (dwarf_tag (&child
))
673 // normal tags to recurse
674 case DW_TAG_compile_unit
:
676 case DW_TAG_lexical_block
:
677 case DW_TAG_with_stmt
:
678 case DW_TAG_catch_block
:
679 case DW_TAG_try_block
:
680 case DW_TAG_entry_point
:
681 case DW_TAG_inlined_subroutine
:
682 case DW_TAG_subprogram
:
683 case DW_TAG_namespace
:
684 case DW_TAG_class_type
:
685 case DW_TAG_structure_type
:
686 parents
->insert(make_pair(child
.addr
, *die
));
687 cache_die_parents(parents
, &child
);
690 // record only, nothing to recurse
692 parents
->insert(make_pair(child
.addr
, *die
));
695 // imported dies should be followed
696 case DW_TAG_imported_unit
:
697 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
699 parents
->insert(make_pair(import
.addr
, *die
));
700 cache_die_parents(parents
, &import
);
704 // nothing to do for other tags
709 while (dwarf_siblingof(&child
, &child
) == 0);
713 cu_die_parent_cache_t
*
714 dwflpp::get_die_parents()
718 cu_die_parent_cache_t
*& parents
= cu_die_parent_cache
[cu
->addr
];
721 parents
= new cu_die_parent_cache_t
;
722 cache_die_parents(parents
, cu
);
723 if (sess
.verbose
> 4)
724 clog
<< _F("die parent cache %s:%s size %zu", module_name
.c_str(),
725 cu_name().c_str(), parents
->size()) << endl
;
732 dwflpp::getscopes_die(Dwarf_Die
* die
)
734 cu_die_parent_cache_t
*parents
= get_die_parents();
736 vector
<Dwarf_Die
> scopes
;
737 Dwarf_Die
*scope
= die
;
738 auto it
= parents
->end();
741 scopes
.push_back(*scope
);
742 it
= parents
->find(scope
->addr
);
745 while (it
!= parents
->end());
747 #ifdef DEBUG_DWFLPP_GETSCOPES
748 Dwarf_Die
*dscopes
= NULL
;
749 int nscopes
= dwarf_getscopes_die(die
, &dscopes
);
751 assert(nscopes
== (int)scopes
.size());
752 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
753 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
761 std::vector
<Dwarf_Die
>
762 dwflpp::getscopes(Dwarf_Die
* die
)
764 cu_die_parent_cache_t
*parents
= get_die_parents();
766 vector
<Dwarf_Die
> scopes
;
769 Dwarf_Die
*scope
= die
;
770 auto it
= parents
->end();
773 scopes
.push_back(*scope
);
774 if (dwarf_tag(scope
) == DW_TAG_inlined_subroutine
&&
775 dwarf_attr_die(scope
, DW_AT_abstract_origin
, &origin
))
778 it
= parents
->find(scope
->addr
);
781 while (it
!= parents
->end());
783 #ifdef DEBUG_DWFLPP_GETSCOPES
784 // there isn't an exact libdw equivalent, but if dwarf_getscopes on the
785 // entrypc returns the same first die, then all the scopes should match
787 if (die_entrypc(die
, &pc
))
789 Dwarf_Die
*dscopes
= NULL
;
790 int nscopes
= dwarf_getscopes(cu
, pc
, &dscopes
);
791 if (nscopes
> 0 && dscopes
[0].addr
== die
->addr
)
793 assert(nscopes
== (int)scopes
.size());
794 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
795 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
805 std::vector
<Dwarf_Die
>
806 dwflpp::getscopes(Dwarf_Addr pc
)
808 // The die_parent_cache doesn't help us without knowing where the pc is
809 // contained, so we have to do this one the old fashioned way.
813 vector
<Dwarf_Die
> scopes
;
815 Dwarf_Die
* dwarf_scopes
;
816 int nscopes
= dwarf_getscopes(cu
, pc
, &dwarf_scopes
);
819 scopes
.assign(dwarf_scopes
, dwarf_scopes
+ nscopes
);
823 #ifdef DEBUG_DWFLPP_GETSCOPES
824 // check that getscopes on the starting die gets the same result
827 vector
<Dwarf_Die
> other
= getscopes(&scopes
[0]);
828 assert(scopes
.size() == other
.size());
829 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
830 assert(scopes
[i
].addr
== other
[i
].addr
);
839 dwflpp::get_parent_scope(Dwarf_Die
* die
)
841 Dwarf_Die specification
;
842 if (dwarf_attr_die(die
, DW_AT_specification
, &specification
))
843 die
= &specification
;
845 cu_die_parent_cache_t
*parents
= get_die_parents();
846 auto it
= parents
->find(die
->addr
);
847 while (it
!= parents
->end())
849 Dwarf_Die
* scope
= &it
->second
;
850 switch (dwarf_tag (scope
))
852 case DW_TAG_namespace
:
853 case DW_TAG_class_type
:
854 case DW_TAG_structure_type
:
860 it
= parents
->find(scope
->addr
);
866 cache_type_prefix(Dwarf_Die
* type
)
868 switch (dwarf_tag(type
))
870 case DW_TAG_enumeration_type
:
872 case DW_TAG_structure_type
:
873 case DW_TAG_class_type
:
874 // treating struct/class as equals
876 case DW_TAG_union_type
:
882 /* GCC might generate a struct/class without DW_AT_declaration,
883 but that only contains members which have DW_AT_declaration
884 set. We aren't interested in those. PR14434 (GCC bug #54181). */
886 has_only_decl_members (Dwarf_Die
*die
)
888 Dwarf_Die child
, import
;
889 if (dwarf_child(die
, &child
) != 0)
890 return false; /* no members */
894 if (! dwarf_hasattr(&child
, DW_AT_declaration
))
895 return false; /* real member found. */
896 int tag
= dwarf_tag(&child
);
897 if ((tag
== DW_TAG_namespace
898 || tag
== DW_TAG_structure_type
899 || tag
== DW_TAG_class_type
)
900 && ! has_only_decl_members (&child
))
901 return false; /* real grand child member found. */
903 // Unlikely to ever happen, but if there is an imported unit
904 // then check its children as if they are children of this DIE.
905 if (tag
== DW_TAG_imported_unit
906 && dwarf_attr_die(&child
, DW_AT_import
, &import
)
907 && ! has_only_decl_members (&import
))
910 while (dwarf_siblingof(&child
, &child
) == 0);
912 return true; /* Tried all children and grandchildren. */
916 dwflpp::global_alias_caching_callback(Dwarf_Die
*die
, bool has_inner_types
,
917 const string
& prefix
, cu_type_cache_t
*cache
)
919 const char *name
= dwarf_diename(die
);
921 if (!name
|| dwarf_hasattr(die
, DW_AT_declaration
)
922 || has_only_decl_members(die
))
925 int tag
= dwarf_tag(die
);
926 if (has_inner_types
&& (tag
== DW_TAG_namespace
927 || tag
== DW_TAG_structure_type
928 || tag
== DW_TAG_class_type
))
929 iterate_over_types(die
, has_inner_types
, prefix
+ name
+ "::",
930 global_alias_caching_callback
, cache
);
932 if (tag
!= DW_TAG_namespace
)
934 string type_name
= prefix
+ cache_type_prefix(die
) + name
;
935 if (cache
->find(type_name
) == cache
->end())
936 (*cache
)[type_name
] = *die
;
943 dwflpp::global_alias_caching_callback_cus(Dwarf_Die
*die
, dwflpp
*dw
)
945 mod_cu_type_cache_t
*global_alias_cache
;
946 global_alias_cache
= &dw
->global_alias_cache
;
948 cu_type_cache_t
*v
= (*global_alias_cache
)[die
->addr
];
952 v
= new cu_type_cache_t
;
953 (*global_alias_cache
)[die
->addr
] = v
;
954 iterate_over_globals(die
, global_alias_caching_callback
, v
);
960 dwflpp::declaration_resolve_other_cus(const string
& name
)
962 iterate_over_cus(global_alias_caching_callback_cus
, this, true);
963 for (auto i
= global_alias_cache
.begin();
964 i
!= global_alias_cache
.end(); ++i
)
966 cu_type_cache_t
*v
= (*i
).second
;
967 if (v
->find(name
) != v
->end())
968 return & ((*v
)[name
]);
975 dwflpp::declaration_resolve(const string
& name
)
977 cu_type_cache_t
*v
= global_alias_cache
[cu
->addr
];
978 if (v
== 0) // need to build the cache, just once per encountered module/cu
980 v
= new cu_type_cache_t
;
981 global_alias_cache
[cu
->addr
] = v
;
982 iterate_over_globals(cu
, global_alias_caching_callback
, v
);
983 if (sess
.verbose
> 4)
984 clog
<< _F("global alias cache %s:%s size %zu", module_name
.c_str(),
985 cu_name().c_str(), v
->size()) << endl
;
988 // XXX: it may be desirable to search other modules' declarations
989 // too, in case a module/shared-library processes a
990 // forward-declared pointer type only, where the actual definition
991 // may only be in vmlinux or the application.
993 if (v
->find(name
) == v
->end())
994 return declaration_resolve_other_cus(name
);
996 return & ((*v
)[name
]);
1000 dwflpp::declaration_resolve(Dwarf_Die
*type
)
1002 const char* name
= dwarf_diename(type
);
1006 string type_name
= cache_type_prefix(type
) + string(name
);
1007 return declaration_resolve(type_name
);
1012 dwflpp::cu_function_caching_callback (Dwarf_Die
* func
, cu_function_cache_t
*v
)
1014 const char *name
= dwarf_diename(func
);
1018 v
->insert(make_pair(name
, *func
));
1024 dwflpp::mod_function_caching_callback (Dwarf_Die
* cu
, cu_function_cache_t
*v
)
1026 // need to cast callback to func which accepts void*
1027 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
1034 dwflpp::iterate_over_functions
<void>(int (*callback
)(Dwarf_Die
*, void*),
1035 void *data
, const string
& function
)
1037 int rc
= DWARF_CB_OK
;
1041 cu_function_cache_t
*v
= cu_function_cache
[cu
->addr
];
1044 v
= new cu_function_cache_t
;
1045 cu_function_cache
[cu
->addr
] = v
;
1046 // need to cast callback to func which accepts void*
1047 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
1049 if (sess
.verbose
> 4)
1050 clog
<< _F("function cache %s:%s size %zu", module_name
.c_str(),
1051 cu_name().c_str(), v
->size()) << endl
;
1052 mod_info
->update_symtab(v
);
1055 auto range
= v
->equal_range(function
);
1056 if (range
.first
!= range
.second
)
1058 for (auto it
= range
.first
; it
!= range
.second
; ++it
)
1060 Dwarf_Die
& die
= it
->second
;
1061 if (sess
.verbose
> 4)
1062 clog
<< _F("function cache %s:%s hit %s", module_name
.c_str(),
1063 cu_name().c_str(), function
.c_str()) << endl
;
1064 rc
= (*callback
)(& die
, data
);
1065 if (rc
!= DWARF_CB_OK
) break;
1068 else if (startswith(function
, "_Z"))
1070 // C++ names are mangled starting with a "_Z" prefix. Most of the time
1071 // we can discover the mangled name from a die's MIPS_linkage_name
1072 // attribute, so we read that to match against the user's function
1073 // pattern. Note that this isn't perfect, as not all will have that
1074 // attribute (notably ctors and dtors), but we do what we can...
1075 for (auto it
= v
->begin(); it
!= v
->end(); ++it
)
1077 if (pending_interrupts
) return DWARF_CB_ABORT
;
1078 Dwarf_Die
& die
= it
->second
;
1079 const char* linkage_name
= NULL
;
1080 if ((linkage_name
= dwarf_linkage_name (&die
))
1081 && function_name_matches_pattern (linkage_name
, function
))
1083 if (sess
.verbose
> 4)
1084 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1085 cu_name().c_str(), linkage_name
, function
.c_str()) << endl
;
1087 rc
= (*callback
)(& die
, data
);
1088 if (rc
!= DWARF_CB_OK
) break;
1092 else if (name_has_wildcard (function
))
1094 for (auto it
= v
->begin(); it
!= v
->end(); ++it
)
1096 if (pending_interrupts
) return DWARF_CB_ABORT
;
1097 const string
& func_name
= it
->first
;
1098 Dwarf_Die
& die
= it
->second
;
1099 if (function_name_matches_pattern (func_name
, function
))
1101 if (sess
.verbose
> 4)
1102 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1103 cu_name().c_str(), func_name
.c_str(), function
.c_str()) << endl
;
1105 rc
= (*callback
)(& die
, data
);
1106 if (rc
!= DWARF_CB_OK
) break;
1110 else // not a linkage name or wildcard and no match in this CU
1119 dwflpp::iterate_single_function
<void>(int (*callback
)(Dwarf_Die
*, void*),
1120 void *data
, const string
& function
)
1122 int rc
= DWARF_CB_OK
;
1125 get_module_dwarf(false);
1129 cu_function_cache_t
*v
= mod_function_cache
[module_dwarf
];
1132 v
= new cu_function_cache_t
;
1133 mod_function_cache
[module_dwarf
] = v
;
1134 iterate_over_cus (mod_function_caching_callback
, v
, false);
1135 if (sess
.verbose
> 4)
1136 clog
<< _F("module function cache %s size %zu", module_name
.c_str(),
1138 mod_info
->update_symtab(v
);
1141 auto range
= v
->equal_range(function
);
1142 if (range
.first
!= range
.second
)
1144 for (auto it
= range
.first
; it
!= range
.second
; ++it
)
1147 Dwarf_Die
& die
= it
->second
;
1148 if (sess
.verbose
> 4)
1149 clog
<< _F("module function cache %s hit %s", module_name
.c_str(),
1150 function
.c_str()) << endl
;
1152 // since we're iterating out of cu-context, we need each focus
1153 focus_on_cu(dwarf_diecu(&die
, &cu_mem
, NULL
, NULL
));
1155 rc
= (*callback
)(& die
, data
);
1156 if (rc
!= DWARF_CB_OK
) break;
1160 // undo the focus_on_cu
1162 this->function_name
.clear();
1163 this->function
= NULL
;
1169 /* This basically only goes one level down from the compile unit so it
1170 * only picks up top level stuff (i.e. nothing in a lower scope) */
1172 dwflpp::iterate_over_globals
<void>(Dwarf_Die
*cu_die
,
1173 int (*callback
)(Dwarf_Die
*,
1180 assert (dwarf_tag(cu_die
) == DW_TAG_compile_unit
1181 || dwarf_tag(cu_die
) == DW_TAG_type_unit
1182 || dwarf_tag(cu_die
) == DW_TAG_partial_unit
);
1184 // Ignore partial_unit, if they get imported by a real unit, then
1185 // iterate_over_types will traverse them.
1186 if (dwarf_tag(cu_die
) == DW_TAG_partial_unit
)
1189 // If this is C++, recurse for any inner types
1190 bool has_inner_types
= dwarf_srclang(cu_die
) == DW_LANG_C_plus_plus
;
1192 return iterate_over_types(cu_die
, has_inner_types
, "", callback
, data
);
1196 dwflpp::iterate_over_types
<void>(Dwarf_Die
*top_die
,
1197 bool has_inner_types
,
1198 const string
& prefix
,
1199 int (* callback
)(Dwarf_Die
*,
1205 int rc
= DWARF_CB_OK
;
1206 Dwarf_Die die
, import
;
1210 if (dwarf_child(top_die
, &die
) != 0)
1214 /* We're only currently looking for named types,
1215 * although other types of declarations exist */
1216 switch (dwarf_tag(&die
))
1218 case DW_TAG_base_type
:
1219 case DW_TAG_enumeration_type
:
1220 case DW_TAG_structure_type
:
1221 case DW_TAG_class_type
:
1222 case DW_TAG_typedef
:
1223 case DW_TAG_union_type
:
1224 case DW_TAG_namespace
:
1225 rc
= (*callback
)(&die
, has_inner_types
, prefix
, data
);
1228 case DW_TAG_imported_unit
:
1229 // Follow the imported_unit and iterate over its contents
1230 // (either a partial_unit or a full compile_unit), all its
1231 // children should be treated as if they appear in this place.
1232 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
1233 rc
= iterate_over_types(&import
, has_inner_types
, prefix
,
1237 while (rc
== DWARF_CB_OK
&& dwarf_siblingof(&die
, &die
) == 0);
1243 /* For each notes section in the current module call 'callback', use
1244 * 'data' for the notes buffer and pass 'object' back in case
1245 * 'callback' is a method */
1248 dwflpp::iterate_over_notes
<void>(void *object
, void (*callback
)(void*,
1256 // Note we really want the actual elf file, not the dwarf .debug file.
1257 // Older binutils had a bug where they mangled the SHT_NOTE type during
1259 Elf
* elf
= dwfl_module_getelf (module
, &bias
);
1261 if (elf_getshdrstrndx (elf
, &shstrndx
))
1264 Elf_Scn
*scn
= NULL
;
1266 vector
<Dwarf_Die
> notes
;
1268 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
1271 if (gelf_getshdr (scn
, &shdr
) == NULL
)
1273 switch (shdr
.sh_type
)
1276 if (!(shdr
.sh_flags
& SHF_ALLOC
))
1278 string scn_name
= elf_strptr(elf
, shstrndx
, shdr
.sh_name
);
1279 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1284 for (size_t offset
= 0;
1285 (next
= gelf_getnote (data
, offset
, &nhdr
, &name_off
, &desc_off
)) > 0;
1288 const char *note_name_addr
= (const char *)data
->d_buf
+ name_off
;
1289 const char *note_desc_addr
= (const char *)data
->d_buf
+ desc_off
;
1290 string note_name
= nhdr
.n_namesz
> 1 // n_namesz includes NULL
1291 ? string(note_name_addr
, nhdr
.n_namesz
-1) : "";
1292 (*callback
) (object
, scn_name
, note_name
, nhdr
.n_type
,
1293 note_desc_addr
, nhdr
.n_descsz
);
1303 /* For each entry in the .dynamic section in the current module call 'callback'
1304 * returning 'object' in case 'callback' is a method */
1307 dwflpp::iterate_over_libraries
<void>(void (*callback
)(void*, const char*),
1310 std::set
<std::string
> added
;
1313 assert (this->module_name
.length() != 0);
1316 // We cannot use this: dwarf_getelf (dwfl_module_getdwarf (module, &bias))
1317 Elf
*elf
= dwfl_module_getelf (module
, &bias
);
1318 // elf_getphdrnum (elf, &phnum) is not available in all versions of elfutils
1319 // needs libelf from elfutils 0.144+
1320 for (int i
= 0; ; i
++)
1324 phdr
= gelf_getphdr (elf
, i
, &mem
);
1327 if (phdr
->p_type
== PT_INTERP
)
1330 char *filedata
= elf_rawfile (elf
, &maxsize
);
1332 if (filedata
!= NULL
&& phdr
->p_offset
< maxsize
)
1333 interpreter
= (char*) (filedata
+ phdr
->p_offset
);
1338 if (interpreter
.length() == 0)
1340 // If it gets cumbersome to maintain this whitelist, we could just check for
1341 // startswith("/lib/ld") || startswith("/lib64/ld"), and trust that no admin
1342 // would install untrustworthy loaders in those paths.
1343 // See also http://sourceware.org/git/?p=glibc.git;a=blob;f=shlib-versions;hb=HEAD
1344 if (interpreter
!= "/lib/ld.so.1" // s390, ppc
1345 && interpreter
!= "/lib/ld64.so.1" // s390x, ppc64
1346 && interpreter
!= "/lib64/ld64.so.1"
1347 && interpreter
!= "/lib/ld-linux-ia64.so.2" // ia64
1348 && interpreter
!= "/usr/lib/ld-linux-x86-64.so.2" //
1349 && interpreter
!= "/emul/ia32-linux/lib/ld-linux.so.2"
1350 && interpreter
!= "/lib64/ld-linux-x86-64.so.2" // x8664
1351 && interpreter
!= "/lib/ld-linux.so.2" // x86
1352 && interpreter
!= "/lib/ld-linux.so.3" // arm
1353 && interpreter
!= "/lib/ld-linux-armhf.so.3" // arm
1354 && interpreter
!= "/lib/ld-linux-aarch64.so.1" // arm64
1355 && interpreter
!= "/lib64/ld64.so.2" // ppc64le
1358 sess
.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s",
1359 module_name
.c_str(), interpreter
.c_str()));
1363 vector
<string
> ldd_command
;
1364 ldd_command
.push_back("/usr/bin/env");
1365 ldd_command
.push_back("LD_TRACE_LOADED_OBJECTS=1");
1366 ldd_command
.push_back("LD_WARN=yes");
1367 ldd_command
.push_back("LD_BIND_NOW=yes");
1368 ldd_command
.push_back(interpreter
);
1369 ldd_command
.push_back(module_name
);
1373 pid_t child
= stap_spawn_piped(sess
.verbose
, ldd_command
, NULL
, &child_fd
);
1374 if (child
<= 0 || !(fp
= fdopen(child_fd
, "r")))
1375 clog
<< _F("library iteration on %s failed: %s",
1376 module_name
.c_str(), strerror(errno
)) << endl
;
1384 unsigned long int addr
= 0;
1386 char *line
= fgets (linebuf
, 256, fp
);
1387 if (line
== 0) break; // EOF or error
1389 // Try soname => shlib (0xaddr)
1390 int nf
= sscanf (line
, "%255s => %255s (0x%lx)",
1391 soname
, shlib
, &addr
);
1392 if (nf
!= 3 || shlib
[0] != '/')
1394 // Try shlib (0xaddr)
1395 nf
= sscanf (line
, " %255s (0x%lx)", shlib
, &addr
);
1396 if (nf
!= 2 || shlib
[0] != '/')
1397 continue; // fewer than expected fields, or bad shlib.
1400 if (added
.find (shlib
) == added
.end())
1402 if (sess
.verbose
> 2)
1404 clog
<< _F("Added -d '%s", shlib
);
1406 clog
<< _F("' due to '%s'", soname
);
1411 added
.insert (shlib
);
1414 if ((fclose(fp
) || stap_waitpid(sess
.verbose
, child
)))
1415 sess
.print_warning("failed to read libraries from " + module_name
+ ": " + strerror(errno
));
1418 for (auto it
= added
.begin(); it
!= added
.end(); it
++)
1420 const string
& modname
= *it
;
1421 (callback
) (data
, modname
.c_str());
1426 /* For each plt section in the current module call 'callback', pass the plt entry
1427 * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */
1430 dwflpp::iterate_over_plt
<void>(void *object
, void (*callback
)(void*,
1434 Dwarf_Addr load_addr
;
1435 // Note we really want the actual elf file, not the dwarf .debug file.
1436 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
1438 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
1440 // Get the load address
1441 for (int i
= 0; ; i
++)
1445 phdr
= gelf_getphdr (elf
, i
, &mem
);
1448 if (phdr
->p_type
== PT_LOAD
)
1450 load_addr
= phdr
->p_vaddr
;
1455 // Get the plt section header
1456 Elf_Scn
*scn
= NULL
;
1457 GElf_Shdr
*plt_shdr
= NULL
;
1458 GElf_Shdr plt_shdr_mem
;
1459 while ((scn
= elf_nextscn (elf
, scn
)))
1461 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &plt_shdr_mem
);
1462 assert (shdr
!= NULL
);
1463 if (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".plt") == 0)
1469 if (plt_shdr
== NULL
)
1472 // Layout of the plt section
1473 int plt0_entry_size
;
1476 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1477 switch (em
->e_machine
)
1479 case EM_386
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1480 case EM_X86_64
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1481 case EM_ARM
: plt0_entry_size
= 20; plt_entry_size
= 12; break;
1482 case EM_AARCH64
:plt0_entry_size
= 32; plt_entry_size
= 16; break;
1487 throw SEMANTIC_ERROR(".plt is not supported on this architecture");
1491 while ((scn
= elf_nextscn (elf
, scn
)))
1494 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
1495 bool have_rela
= false;
1496 bool have_rel
= false;
1500 assert (shdr
!= NULL
);
1502 if ((have_rela
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rela.plt") == 0))
1503 || (have_rel
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rel.plt") == 0)))
1505 /* Get the data of the section. */
1506 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1507 assert (data
!= NULL
);
1508 /* Get the symbol table information. */
1509 Elf_Scn
*symscn
= elf_getscn (elf
, shdr
->sh_link
);
1510 GElf_Shdr symshdr_mem
;
1511 GElf_Shdr
*symshdr
= gelf_getshdr (symscn
, &symshdr_mem
);
1512 assert (symshdr
!= NULL
);
1513 Elf_Data
*symdata
= elf_getdata (symscn
, NULL
);
1514 assert (symdata
!= NULL
);
1516 unsigned int nsyms
= shdr
->sh_size
/ shdr
->sh_entsize
;
1518 for (unsigned int cnt
= 0; cnt
< nsyms
; ++cnt
)
1521 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1522 if (em
== 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
1525 GElf_Rela
*rela
= NULL
;
1527 GElf_Rel
*rel
= NULL
;
1530 rela
= gelf_getrela (data
, cnt
, &relamem
);
1531 assert (rela
!= NULL
);
1535 rel
= gelf_getrel (data
, cnt
, &relmem
);
1536 assert (rel
!= NULL
);
1540 Elf_Data
*xndxdata
= NULL
;
1542 gelf_getsymshndx (symdata
, xndxdata
,
1543 GELF_R_SYM (have_rela
? rela
->r_info
: rel
->r_info
),
1545 assert (sym
!= NULL
);
1546 Dwarf_Addr addr
= plt_shdr
->sh_offset
+ plt0_entry_size
+ cnt
* plt_entry_size
;
1548 if (elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
))
1549 (*callback
) (object
, elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
), addr
+ load_addr
);
1558 // Comparator function for sorting
1560 compare_lines(Dwarf_Line
* a
, Dwarf_Line
* b
)
1565 int lineno_a
= DWARF_LINENO(a
);
1566 int lineno_b
= DWARF_LINENO(b
);
1567 if (lineno_a
== lineno_b
)
1568 return DWARF_LINEADDR(a
) < DWARF_LINEADDR(b
);
1569 return lineno_a
< lineno_b
;
1572 // Comparator object for searching Dwarf_Lines with a specific lineno when we
1573 // don't have a Dwarf_Line of our own to search for (hence why a or b is always
1575 struct lineno_comparator
{
1577 lineno_comparator(int lineno
): lineno(lineno
) {}
1578 bool operator() (Dwarf_Line
* a
, Dwarf_Line
* b
)
1582 return DWARF_LINENO(a
) < lineno
;
1584 return lineno
< DWARF_LINENO(b
);
1588 // Returns a range of lines in between begin and end with wanted lineno. If
1589 // none exist, points to where it would have been.
1590 static lines_range_t
1591 lineno_equal_range(lines_t
* v
, int lineno
)
1593 lineno_comparator
lc(lineno
);
1594 return equal_range(v
->begin(), v
->end(), (Dwarf_Line
*)NULL
, lc
);
1597 // Interface to CU lines cache sorted by lineno
1599 dwflpp::get_cu_lines_sorted_by_lineno(const char *srcfile
)
1603 srcfile_lines_cache_t
*srcfile_lines
= cu_lines_cache
[cu
];
1606 srcfile_lines
= new srcfile_lines_cache_t();
1607 cu_lines_cache
[cu
] = srcfile_lines
;
1610 lines_t
*lines
= (*srcfile_lines
)[srcfile
];
1613 size_t nlines_cu
= 0;
1614 Dwarf_Lines
*lines_cu
= NULL
;
1615 DWARF_ASSERT("dwarf_getsrclines",
1616 dwarf_getsrclines(cu
, &lines_cu
, &nlines_cu
));
1618 lines
= new lines_t();
1619 (*srcfile_lines
)[srcfile
] = lines
;
1621 for (size_t i
= 0; i
< nlines_cu
; i
++)
1623 Dwarf_Line
*line
= dwarf_onesrcline(lines_cu
, i
);
1624 const char *linesrc
= DWARF_LINESRC(line
);
1625 if (strcmp(srcfile
, linesrc
))
1627 lines
->push_back(line
);
1630 if (lines
->size() > 1)
1631 sort(lines
->begin(), lines
->end(), compare_lines
);
1633 if (sess
.verbose
> 3)
1635 clog
<< _F("found the following lines for %s:", srcfile
) << endl
;
1636 for (auto i
= lines
->begin(); i
!= lines
->end(); ++i
)
1637 cout
<< DWARF_LINENO(*i
) << " = " << hex
1638 << DWARF_LINEADDR(*i
) << dec
<< endl
;
1645 get_func_first_line(Dwarf_Die
*cu
, base_func_info
& func
)
1647 // dwarf_getsrc_die() uses binary search to find the Dwarf_Line, but will
1648 // return the wrong line if not found.
1649 Dwarf_Line
*line
= dwarf_getsrc_die(cu
, func
.entrypc
);
1650 if (line
&& DWARF_LINEADDR(line
) == func
.entrypc
)
1653 // Line not found (or line at wrong addr). We have to resort to a slower
1654 // linear method. We won't find an exact match (probably this is an inlined
1655 // instance), so just settle on the first Dwarf_Line with lowest addr which
1656 // falls in the die.
1658 Dwarf_Lines
*lines
= NULL
;
1659 DWARF_ASSERT("dwarf_getsrclines",
1660 dwarf_getsrclines(cu
, &lines
, &nlines
));
1662 for (size_t i
= 0; i
< nlines
; i
++)
1664 line
= dwarf_onesrcline(lines
, i
);
1665 if (dwarf_haspc(&func
.die
, DWARF_LINEADDR(line
)))
1672 collect_lines_in_die(lines_range_t range
, Dwarf_Die
*die
)
1674 lines_t lines_in_die
;
1675 for (auto line
= range
.first
; line
!= range
.second
; ++line
)
1676 if (dwarf_haspc(die
, DWARF_LINEADDR(*line
)))
1677 lines_in_die
.push_back(*line
);
1678 return lines_in_die
;
1682 add_matching_lines_in_func(Dwarf_Die
*cu
,
1684 base_func_info
& func
,
1685 lines_t
& matching_lines
)
1687 Dwarf_Line
*start_line
= get_func_first_line(cu
, func
);
1691 for (int lineno
= DWARF_LINENO(start_line
);;)
1693 lines_range_t range
= lineno_equal_range(cu_lines
, lineno
);
1695 // We consider the lineno still part of the die if at least one of them
1696 // falls in the die.
1697 lines_t lines_in_die
= collect_lines_in_die(range
, &func
.die
);
1698 if (lines_in_die
.empty())
1701 // Just pick the first LR even if there are more than one. Since the lines
1702 // are sorted by lineno and then addr, the first one is the one with the
1704 matching_lines
.push_back(lines_in_die
[0]);
1706 // break out if there are no more lines, otherwise, go to the next lineno
1707 if (range
.second
== cu_lines
->end())
1709 lineno
= DWARF_LINENO(*range
.second
);
1714 dwflpp::collect_all_lines(char const * srcfile
,
1715 base_func_info_map_t
& funcs
,
1716 lines_t
& matching_lines
)
1718 // This is where we handle WILDCARD lineno types.
1719 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1720 for (auto func
= funcs
.begin(); func
!= funcs
.end(); ++func
)
1721 add_matching_lines_in_func(cu
, cu_lines
, *func
, matching_lines
);
1726 add_matching_line_in_die(lines_t
*cu_lines
,
1727 lines_t
& matching_lines
,
1728 Dwarf_Die
*die
, int lineno
)
1730 lines_range_t lineno_range
= lineno_equal_range(cu_lines
, lineno
);
1731 lines_t lines_in_die
= collect_lines_in_die(lineno_range
, die
);
1732 if (lines_in_die
.empty())
1735 // Even if there are more than 1 LRs, just pick the first one. Since the lines
1736 // are sorted by lineno and then addr, the first one is the one with the
1737 // lowest addr. This is similar to what GDB does.
1738 matching_lines
.push_back(lines_in_die
[0]);
1742 dwflpp::collect_lines_for_single_lineno(char const * srcfile
,
1745 base_func_info_map_t
& funcs
,
1746 lines_t
& matching_lines
)
1748 /* Here, we handle ABSOLUTE and RELATIVE lineno types. Relative line numbers
1749 * are a bit special. The issue is that functions (esp. inlined ones) may not
1750 * even have a LR corresponding to the first valid line of code. So, applying
1751 * an offset to the 'first' LR found in the DIE can be quite imprecise.
1752 * Instead, we use decl_line, which although does not necessarily have a
1753 * LR associated with it (it can sometimes still happen esp. if the code is
1754 * written in OTB-style), it serves as an anchor on which we can apply the
1755 * offset to yield a lineno that will not change with compiler optimization.
1756 * It also has the added benefit of being consistent with the lineno
1757 * printed by e.g. stap -l kernel.function("vfs_read"), so users can be
1758 * confident from what lineno we adjust.
1760 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1761 for (auto func
= funcs
.begin(); func
!= funcs
.end(); ++func
)
1762 add_matching_line_in_die(cu_lines
, matching_lines
, &func
->die
,
1763 is_relative
? lineno
+ func
->decl_line
1768 functions_have_lineno(base_func_info_map_t
& funcs
,
1769 lines_t
*lines
, int lineno
)
1771 lines_range_t lineno_range
= lineno_equal_range(lines
, lineno
);
1772 if (lineno_range
.first
== lineno_range
.second
)
1773 return false; // no LRs at this lineno
1775 for (auto func
= funcs
.begin(); func
!= funcs
.end(); ++func
)
1776 if (!collect_lines_in_die(lineno_range
, &func
->die
).empty())
1782 // returns pair of valid linenos surrounding target lineno
1784 dwflpp::get_nearest_linenos(char const * srcfile
,
1786 base_func_info_map_t
& funcs
)
1789 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1791 // Look around lineno for linenos with LRs.
1792 pair
<int,int> nearest_linenos
= make_pair(-1, -1);
1793 for (size_t i
= 1; i
< 6; ++i
)
1795 if (nearest_linenos
.first
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
-i
))
1796 nearest_linenos
.first
= lineno
- i
;
1797 if (nearest_linenos
.second
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
+i
))
1798 nearest_linenos
.second
= lineno
+ i
;
1801 return nearest_linenos
;
1804 // returns nearest valid lineno to target lineno
1806 dwflpp::get_nearest_lineno(char const * srcfile
,
1808 base_func_info_map_t
& funcs
)
1811 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1813 if (nearest_linenos
.first
> 0
1814 && nearest_linenos
.second
> 0)
1816 // pick the nearest line number (break tie to upper)
1817 if (lineno
- nearest_linenos
.first
< nearest_linenos
.second
- lineno
)
1818 return nearest_linenos
.first
;
1820 return nearest_linenos
.second
;
1822 else if (nearest_linenos
.first
> 0)
1823 return nearest_linenos
.first
;
1824 else if (nearest_linenos
.second
> 0)
1825 return nearest_linenos
.second
;
1831 dwflpp::suggest_alternative_linenos(char const * srcfile
,
1833 base_func_info_map_t
& funcs
)
1836 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1838 stringstream advice
;
1839 advice
<< _F("no line records for %s:%d [man error::dwarf]", srcfile
, lineno
);
1841 if (nearest_linenos
.first
> 0 || nearest_linenos
.second
> 0)
1843 //TRANSLATORS: Here we are trying to advise what source file
1844 //TRANSLATORS: to attempt.
1845 advice
<< _(" (try ");
1846 if (nearest_linenos
.first
> 0)
1847 advice
<< ":" << nearest_linenos
.first
;
1848 if (nearest_linenos
.first
> 0 && nearest_linenos
.second
> 0)
1849 advice
<< _(" or ");
1850 if (nearest_linenos
.second
> 0)
1851 advice
<< ":" << nearest_linenos
.second
;
1854 throw SEMANTIC_ERROR (advice
.str());
1857 static base_func_info_map_t
1858 get_funcs_in_srcfile(base_func_info_map_t
& funcs
,
1859 const char * srcfile
)
1861 base_func_info_map_t matching_funcs
;
1862 for (auto func
= funcs
.begin(); func
!= funcs
.end(); ++func
)
1863 if (func
->decl_file
== string(srcfile
))
1864 matching_funcs
.push_back(*func
);
1865 return matching_funcs
;
1869 dwflpp::iterate_over_srcfile_lines
<void>(char const * srcfile
,
1870 const vector
<int>& linenos
,
1871 enum lineno_t lineno_type
,
1872 base_func_info_map_t
& funcs
,
1873 void (* callback
) (Dwarf_Addr
,
1878 /* Matching line records (LRs) to user-provided linenos is trickier than it
1879 * seems. The fate of all functions is one of three possibilities:
1880 * 1. it's a normal function, with a subprogram DIE and a bona fide lowpc
1881 * and highpc attribute.
1882 * 2. it's an inlined function (one/multiple inlined_subroutine DIE, with one
1883 * abstract_origin DIE)
1884 * 3. it's both a normal function and an inlined function. For example, if
1885 * the funtion has been inlined only in some places, we'll have a DIE for
1886 * the normal subprogram DIE as well as inlined_subroutine DIEs.
1888 * Multiple LRs for the same lineno but different addresses can simply happen
1889 * due to the function appearing in multiple forms. E.g. a function inlined
1890 * in two spots can yield two sets of LRs for its linenos at the different
1891 * addresses where it is inlined.
1892 * This is why the collect_* functions used here try to match up LRs back
1893 * to their originating DIEs. For example, in the function
1894 * collect_lines_for_single_lineno(), we filter first by DIE so that a lineno
1895 * corresponding to multiple addrs in multiple inlined_subroutine DIEs yields
1896 * a probe for each of them.
1900 // only work on the functions found in the current srcfile
1901 base_func_info_map_t current_funcs
= get_funcs_in_srcfile(funcs
, srcfile
);
1902 if (current_funcs
.empty())
1906 lines_t matching_lines
;
1907 if (lineno_type
== ABSOLUTE
)
1908 collect_lines_for_single_lineno(srcfile
, linenos
[0], false, /* is_relative */
1909 current_funcs
, matching_lines
);
1910 else if (lineno_type
== RELATIVE
)
1911 collect_lines_for_single_lineno(srcfile
, linenos
[0], true, /* is_relative */
1912 current_funcs
, matching_lines
);
1913 else if (lineno_type
== WILDCARD
)
1914 collect_all_lines(srcfile
, current_funcs
, matching_lines
);
1915 else if (lineno_type
== ENUMERATED
)
1917 set
<int> collected_linenos
;
1918 for (auto it
= linenos
.begin(); it
!= linenos
.end(); it
++)
1920 // have we already collected this lineno?
1921 if (collected_linenos
.find(*it
) != collected_linenos
.end())
1924 // remember end iterator so we can tell if things were found later
1925 auto itend
= matching_lines
.end();
1927 collect_lines_for_single_lineno(srcfile
, *it
, false, /* is_relative */
1928 current_funcs
, matching_lines
);
1929 // add to set if we found LRs
1930 if (itend
!= matching_lines
.end())
1931 collected_linenos
.insert(*it
);
1933 // if we didn't find anything and .nearest is given, then try nearest
1934 if (itend
== matching_lines
.end() && has_nearest
)
1936 int nearest_lineno
= get_nearest_lineno(srcfile
, *it
,
1938 if (nearest_lineno
<= 0) // no valid nearest linenos
1941 bool new_lineno
= collected_linenos
.insert(nearest_lineno
).second
;
1943 collect_lines_for_single_lineno(srcfile
, nearest_lineno
,
1944 false, /* is_relative */
1945 current_funcs
, matching_lines
);
1950 // should we try to collect the nearest lines if we didn't collect everything
1951 // on first try? (ABSOLUTE and RELATIVE only: ENUMERATED handles it already
1952 // and WILDCARD doesn't need it)
1953 if (matching_lines
.empty() && has_nearest
&& (lineno_type
== ABSOLUTE
||
1954 lineno_type
== RELATIVE
))
1956 int lineno
= linenos
[0];
1957 if (lineno_type
== RELATIVE
)
1958 // just pick the first function and make it relative to that
1959 lineno
+= current_funcs
[0].decl_line
;
1961 int nearest_lineno
= get_nearest_lineno(srcfile
, lineno
, current_funcs
);
1962 if (nearest_lineno
> 0)
1963 collect_lines_for_single_lineno(srcfile
, nearest_lineno
,
1964 false, /* is_relative */
1965 current_funcs
, matching_lines
);
1968 // call back with matching lines
1969 if (!matching_lines
.empty())
1971 set
<Dwarf_Addr
> probed_addrs
;
1972 for (auto line
= matching_lines
.begin();
1973 line
!= matching_lines
.end(); ++line
)
1975 int lineno
= DWARF_LINENO(*line
);
1976 Dwarf_Addr addr
= DWARF_LINEADDR(*line
);
1977 bool is_new_addr
= probed_addrs
.insert(addr
).second
;
1979 callback(addr
, lineno
, data
);
1982 // No LRs found at the wanted lineno. So let's suggest other ones if user was
1983 // targeting a specific lineno (ABSOLUTE or RELATIVE).
1984 else if (lineno_type
== ABSOLUTE
||
1985 lineno_type
== RELATIVE
)
1987 int lineno
= linenos
[0];
1988 if (lineno_type
== RELATIVE
)
1989 // just pick the first function and make it relative to that
1990 lineno
+= current_funcs
[0].decl_line
;
1992 suggest_alternative_linenos(srcfile
, lineno
, current_funcs
);
1994 else if (lineno_type
== WILDCARD
)
1996 // PR23760: It's not an error to come across a srcfile that has
1997 // no line records, if we're just happening across it as a
1998 // wildcard case. Only best effort matches are expected here.
2000 // throw SEMANTIC_ERROR(_F("no line records for %s [man error::dwarf]", srcfile));
2006 dwflpp::iterate_over_labels
<void>(Dwarf_Die
*begin_die
,
2008 const base_func_info
& function
,
2009 const vector
<int>& linenos
,
2010 enum lineno_t lineno_type
,
2012 void (* callback
)(const base_func_info
&,
2022 Dwarf_Die die
, import
;
2024 int res
= dwarf_child (begin_die
, &die
);
2026 return; // die without children, bail out.
2030 switch (dwarf_tag(&die
))
2033 name
= dwarf_diename (&die
);
2036 || (name_has_wildcard(sym
)
2037 && function_name_matches_pattern (name
, sym
))))
2039 // Don't try to be smart. Just drop no addr labels.
2040 Dwarf_Addr stmt_addr
;
2041 if (dwarf_lowpc (&die
, &stmt_addr
) == 0)
2043 // Get the file/line number for this label
2045 const char *file
= dwarf_decl_file (&die
) ?: "<unknown source>";
2046 dwarf_decl_line (&die
, &dline
);
2048 vector
<Dwarf_Die
> scopes
= getscopes_die(&die
);
2049 if (scopes
.size() > 1)
2052 if (!inner_die_containing_pc(scopes
[1], stmt_addr
, scope
))
2054 sess
.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not "
2055 "contained by its scope '%s' (dieoffset: %s) -- bad"
2056 " debuginfo? [man error::dwarf]", name
, lex_cast_hex(stmt_addr
).c_str(),
2057 lex_cast_hex(dwarf_dieoffset(&die
)).c_str(),
2058 (dwarf_diename(&scope
) ?: "<unknown>"),
2059 lex_cast_hex(dwarf_dieoffset(&scope
)).c_str()));
2062 bool matches_lineno
;
2063 if (lineno_type
== ABSOLUTE
)
2064 matches_lineno
= dline
== linenos
[0];
2065 else if (lineno_type
== RELATIVE
)
2066 matches_lineno
= dline
== linenos
[0] + function
.decl_line
;
2067 else if (lineno_type
== ENUMERATED
)
2068 matches_lineno
= (binary_search(linenos
.begin(), linenos
.end(), dline
));
2070 matches_lineno
= true;
2073 callback(function
, name
, file
, dline
,
2074 &scope
, stmt_addr
, data
);
2080 case DW_TAG_subprogram
:
2081 case DW_TAG_inlined_subroutine
:
2082 // Stay within our filtered function
2085 case DW_TAG_imported_unit
:
2086 // Iterate over the children of the imported unit as if they
2087 // were inserted in place.
2088 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2089 iterate_over_labels (&import
, sym
, function
, linenos
,
2090 lineno_type
, data
, callback
);
2094 if (dwarf_haschildren (&die
))
2095 iterate_over_labels (&die
, sym
, function
, linenos
,
2096 lineno_type
, data
, callback
);
2100 while (dwarf_siblingof (&die
, &die
) == 0);
2103 // Mini 'query-like' struct to help us navigate callbacks during
2104 // external function resolution
2105 struct external_function_query
{
2111 external_function_query(dwflpp
* dw
, const string
& name
):
2112 dw(dw
), name(name
), die(), addr(0), resolved(false) {}
2116 dwflpp::external_function_cu_callback (Dwarf_Die
* cu
, external_function_query
*efq
)
2118 efq
->dw
->focus_on_cu(cu
);
2119 return efq
->dw
->iterate_over_functions(external_function_func_callback
,
2124 dwflpp::external_function_func_callback (Dwarf_Die
* func
, external_function_query
*efq
)
2126 Dwarf_Attribute external
;
2127 Dwarf_Addr func_addr
;
2128 if (dwarf_attr_integrate(func
, DW_AT_external
, &external
) != NULL
&&
2129 dwarf_lowpc(func
, &func_addr
) == 0)
2132 efq
->addr
= func_addr
;
2133 efq
->resolved
= true;
2134 return DWARF_CB_ABORT
; // we found it so stop here
2140 dwflpp::iterate_over_callees
<void>(Dwarf_Die
*begin_die
,
2142 int64_t recursion_depth
,
2144 void (* callback
)(base_func_info
&,
2148 base_func_info
& caller
,
2149 stack
<Dwarf_Addr
> *callers
)
2153 Dwarf_Die die
, import
;
2155 // DIE of abstract_origin found in die
2158 // callee's entry pc (= where we'll probe)
2159 Dwarf_Addr func_addr
;
2161 // caller's unwind pc during call (to match against bt for filtering)
2162 Dwarf_Addr caller_uw_addr
;
2164 Dwarf_Attribute attr
;
2166 base_func_info callee
;
2167 if (dwarf_child(begin_die
, &die
) != 0)
2168 return; // die without children, bail out.
2170 bool free_callers
= false;
2171 if (callers
== NULL
) /* first call */
2173 callers
= new stack
<Dwarf_Addr
>();
2174 free_callers
= true;
2179 bool inlined
= false;
2180 switch (dwarf_tag(&die
))
2182 case DW_TAG_inlined_subroutine
:
2184 /* FALLTHROUGH */ /* thanks mjw */
2185 case DW_TAG_GNU_call_site
:
2186 callee
.name
= dwarf_diename(&die
) ?: "";
2187 if (callee
.name
.empty())
2189 if (callee
.name
!= sym
)
2191 if (!name_has_wildcard(sym
))
2193 if (!function_name_matches_pattern(callee
.name
, sym
))
2197 /* In both cases (call sites and inlines), we want the
2198 * abstract_origin. The difference is that in inlines, the addr is
2199 * in the die itself, whereas for call sites, the addr is in the
2200 * abstract_origin's die.
2201 * Note that in the case of inlines, we're only calling back
2202 * for that inline instance, not all. This is what we want, since
2203 * it will only be triggered when 'called' from the target func,
2204 * which is something we have to emulate for non-inlined funcs
2205 * (which is the purpose of the caller_uw_addr below) */
2206 if (dwarf_attr_die(&die
, DW_AT_abstract_origin
, &origin
) == NULL
)
2209 // the low_pc of the die in either cases is the pc that would
2210 // show up in a backtrace (inlines are a special case in which
2211 // the die's low_pc is also the abstract_origin's low_pc = the
2212 // 'start' of the inline instance)
2213 if (dwarf_lowpc(&die
, &caller_uw_addr
) != 0)
2217 func_addr
= caller_uw_addr
;
2218 else if (dwarf_lowpc(&origin
, &func_addr
) != 0)
2220 // function doesn't have a low_pc, is it external?
2221 if (dwarf_attr_integrate(&origin
, DW_AT_external
,
2224 // let's iterate over the CUs and find it. NB: it's
2225 // possible we could have also done this by creating a
2226 // probe point with .exported tacked on and rerunning it
2227 // through derive_probe(). But since we're already on the
2228 // dwflpp side of things, and we already have access to
2229 // everything we need, let's try to be self-sufficient.
2231 // remember old focus
2232 Dwarf_Die
*old_cu
= cu
;
2234 external_function_query
efq(this, dwarf_linkage_name(&origin
) ?: callee
.name
);
2235 iterate_over_cus(external_function_cu_callback
, &efq
, false);
2240 if (!efq
.resolved
) // did we resolve it?
2243 func_addr
= efq
.addr
;
2246 // non-external function without low_pc, jump ship
2250 // We now have the addr to probe in func_addr, and the DIE
2251 // from which to obtain file/line info in origin
2253 // Get the file/line number for this callee
2254 callee
.decl_file
= dwarf_decl_file (&origin
) ?: "<unknown source>";
2255 dwarf_decl_line (&origin
, &callee
.decl_line
);
2257 // add as a caller to match against
2259 callers
->push(caller_uw_addr
);
2261 callee
.die
= inlined
? die
: origin
;
2262 callee
.entrypc
= func_addr
;
2263 callback(callee
, caller
, callers
, data
);
2265 // If it's a tail call, print a warning that it may not be caught
2267 && dwarf_attr_integrate(&die
, DW_AT_GNU_tail_call
, &attr
) != NULL
)
2268 sess
.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: "
2269 ".callee probe may not fire. Try placing the probe "
2270 "directly on the callee function instead.",
2271 callee
.name
.to_string().c_str(),
2272 caller
.name
.to_string().c_str()));
2274 // For .callees(N) probes, we recurse on this callee. Note that we
2275 // pass the callee we just found as the caller arg for this recursion,
2276 // since it (the callee we just found) will be the caller of whatever
2277 // callees found inside this recursion.
2278 if (recursion_depth
> 1)
2279 iterate_over_callees(inlined
? &die
: &origin
,
2280 sym
, recursion_depth
-1, data
,
2281 callback
, callee
, callers
);
2287 case DW_TAG_subprogram
:
2288 break; // don't leave our filtered func
2290 case DW_TAG_imported_unit
:
2291 // Iterate over the children of the imported unit as if they
2292 // were inserted in place.
2293 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2294 // NB: we pass the same caller arg into it
2295 iterate_over_callees (&import
, sym
, recursion_depth
, data
,
2296 callback
, caller
, callers
);
2300 if (dwarf_haschildren (&die
))
2301 // NB: we pass the same caller arg into it
2302 iterate_over_callees (&die
, sym
, recursion_depth
, data
,
2303 callback
, caller
, callers
);
2307 while (dwarf_siblingof (&die
, &die
) == 0);
2309 if (free_callers
&& callers
!= NULL
)
2315 dwflpp::collect_srcfiles_matching (string
const & pattern
,
2316 set
<string
> & filtered_srcfiles
)
2322 Dwarf_Files
*srcfiles
;
2324 // PR 5049: implicit * in front of given path pattern.
2325 // NB: fnmatch() is used without FNM_PATHNAME.
2326 string prefixed_pattern
= string("*/") + pattern
;
2328 DWARF_ASSERT ("dwarf_getsrcfiles",
2329 dwarf_getsrcfiles (cu
, &srcfiles
, &nfiles
));
2331 for (size_t i
= 0; i
< nfiles
; ++i
)
2333 char const * fname
= dwarf_filesrc (srcfiles
, i
, NULL
, NULL
);
2334 if (fnmatch (pattern
.c_str(), fname
, 0) == 0 ||
2335 fnmatch (prefixed_pattern
.c_str(), fname
, 0) == 0)
2337 filtered_srcfiles
.insert (fname
);
2339 clog
<< _F("selected source file '%s'\n", fname
);
2347 dwflpp::resolve_prologue_endings (func_info_map_t
& funcs
)
2349 // When a program is compiled with no optimization, GCC does no variable
2350 // tracking, which means that location info is actually only really valid
2351 // after the prologue, even though GCC reports it as valid during. So we need
2352 // to find the prologue ends to get accurate info. This may or may not be the
2353 // first address that has a source line distinct from the function
2360 Dwarf_Lines
*lines
= NULL
;
2363 malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue
2364 sys_get?id in kernel/timer.c no-prologue
2365 sys_exit_group tail-recursive
2366 {do_,}sys_open extra-long-prologue (gcc 3.4)
2367 cpu_to_logical_apicid NULL-decl_file
2370 // Fetch all srcline records, sorted by address. No need to free lines, it's a
2371 // direct pointer to the CU's cached lines.
2372 if (dwarf_getsrclines(cu
, &lines
, &nlines
) != 0
2373 || lines
== NULL
|| nlines
== 0)
2375 if (sess
.verbose
> 2)
2376 clog
<< _F("aborting prologue search: no source lines found for cu '%s'\n",
2381 // Dump them into our own array for easier searching. They should already be
2382 // sorted by addr, but we doublecheck that here. We want to keep the indices
2383 // between lines and addrs the same.
2384 vector
<Dwarf_Addr
> addrs
;
2385 for (size_t i
= 0; i
< nlines
; i
++)
2387 Dwarf_Line
* line
= dwarf_onesrcline(lines
, i
);
2388 Dwarf_Addr addr
= DWARF_LINEADDR(line
);
2389 if (!addrs
.empty() && addr
< addrs
.back())
2390 throw SEMANTIC_ERROR(_("lines from dwarf_getsrclines() not sorted"));
2391 addrs
.push_back(addr
);
2393 // We normally ignore a function's decl_line, since it is associated with the
2394 // line at which the identifier appears in the declaration, and has no
2395 // meaningful relation to the lineno associated with the entrypc (which is
2396 // normally the lineno of '{', which could occur at the same line as the
2397 // declaration, or lower down).
2398 // However, if the CU was compiled using GCC < 4.4, then the decl_line
2399 // actually represents the lineno of '{' as well, in which case if the lineno
2400 // associated with the entrypc is != to the decl_line, it means the compiler
2401 // scraped/optimized off some of the beginning of the function and the safest
2402 // thing we can do is consider it naked.
2403 bool consider_decl_line
= false;
2406 if (is_gcc_producer(cu
, prod
, vers
)
2407 && strverscmp(vers
.c_str(), "4.4.0") < 0)
2408 consider_decl_line
= true;
2411 for(auto it
= funcs
.begin(); it
!= funcs
.end(); it
++)
2413 Dwarf_Addr entrypc
= it
->entrypc
;
2415 unsigned entrypc_srcline_idx
= 0;
2416 Dwarf_Line
*entrypc_srcline
= NULL
;
2418 auto it_addr
= lower_bound(addrs
.cbegin(), addrs
.cend(), entrypc
);
2419 if (it_addr
!= addrs
.cend() && *it_addr
== entrypc
)
2421 entrypc_srcline_idx
= it_addr
- addrs
.cbegin();
2422 entrypc_srcline
= dwarf_onesrcline(lines
, entrypc_srcline_idx
);
2426 if (!entrypc_srcline
)
2428 if (sess
.verbose
> 2)
2429 clog
<< _F("missing entrypc dwarf line record for function '%s'\n",
2430 it
->name
.to_string().c_str());
2431 // This is probably an inlined function. We'll end up using
2432 // its lowpc as a probe address.
2438 if (sess
.verbose
> 2)
2439 clog
<< _F("null entrypc dwarf line record for function '%s'\n",
2440 it
->name
.to_string().c_str());
2441 // This is probably an inlined function. We'll skip this instance;
2447 clog
<< _F("searching for prologue of function '%s' %#" PRIx64
2448 "@%s:%d\n", it
->name
.to_string().c_str(), entrypc
,
2449 it
->decl_file
.to_string().c_str(), it
->decl_line
);
2451 // For each function, we look for the prologue-end marker (e.g. clang
2452 // outputs one). If there is no explicit marker (e.g. GCC does not), we
2453 // accept a bigger or equal lineno as a prologue end (this catches GCC's
2454 // 0-line advances).
2456 // We may have to skip a few because some old compilers plop
2457 // in dummy line records for longer prologues. If we go too
2458 // far (outside function), we take the previous one. Or, it may
2459 // be the first one, if the function had no prologue, and thus
2460 // the entrypc maps to a statement in the body rather than the
2463 int entrypc_srcline_lineno
= DWARF_LINENO(entrypc_srcline
);
2464 unsigned postprologue_srcline_idx
= entrypc_srcline_idx
;
2465 Dwarf_Line
*postprologue_srcline
= entrypc_srcline
;
2467 while (postprologue_srcline_idx
< nlines
)
2469 postprologue_srcline
= dwarf_onesrcline(lines
,
2470 postprologue_srcline_idx
);
2471 Dwarf_Addr lineaddr
= DWARF_LINEADDR(postprologue_srcline
);
2472 const char* linesrc
= DWARF_LINESRC(postprologue_srcline
);
2473 int lineno
= DWARF_LINENO(postprologue_srcline
);
2474 bool lineprologue_end
= DWARF_LINEPROLOGUEEND(postprologue_srcline
);
2477 clog
<< _F("checking line record %#" PRIx64
"@%s:%d%s\n", lineaddr
,
2478 linesrc
, lineno
, lineprologue_end
? " (marked)" : "");
2480 // have we passed the function?
2481 if (dwarf_haspc (& it
->die
, lineaddr
) != 1)
2483 // is there an explicit prologue_end marker?
2484 if (lineprologue_end
)
2486 // is it a different file?
2487 if (it
->decl_file
!= string(linesrc
))
2489 // OK, it's the same file, but is it a different line?
2490 if (lineno
!= entrypc_srcline_lineno
)
2492 // Same file and line, is this a second line record (e.g. 0-line advance)?
2493 if (postprologue_srcline_idx
!= entrypc_srcline_idx
)
2495 // This is the first iteration. Is decl_line meaningful and is the
2496 // lineno past the decl_line?
2497 if (consider_decl_line
&& lineno
!= it
->decl_line
)
2500 // Let's try the next srcline.
2501 postprologue_srcline_idx
++;
2503 } // loop over srclines
2506 Dwarf_Addr postprologue_addr
= DWARF_LINEADDR(postprologue_srcline
);
2507 if (dwarf_haspc (& it
->die
, postprologue_addr
) != 1)
2509 // pick addr of previous line record
2510 Dwarf_Line
*lr
= dwarf_onesrcline(lines
, postprologue_srcline_idx
-1);
2511 postprologue_addr
= DWARF_LINEADDR(lr
);
2514 it
->prologue_end
= postprologue_addr
;
2518 clog
<< _F("prologue found function '%s'", it
->name
.to_string().c_str());
2519 // Add a little classification datum
2520 //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free)
2521 if (postprologue_addr
== entrypc
)
2522 clog
<< _(" (naked)");
2523 //TRANSLATORS: Here we're adding some classification datum (ie we went over)
2524 if (dwarf_haspc (& it
->die
,
2525 DWARF_LINEADDR(postprologue_srcline
)) != 1)
2526 clog
<< _(" (tail-call?)");
2527 //TRANSLATORS: Here we're adding some classification datum (ie it was marked)
2528 if (DWARF_LINEPROLOGUEEND(postprologue_srcline
))
2529 clog
<< _(" (marked)");
2531 clog
<< " = 0x" << hex
<< postprologue_addr
<< dec
<< "\n";
2534 } // loop over functions
2539 dwflpp::function_entrypc (Dwarf_Addr
* addr
)
2543 // assign default value
2546 // PR10574: reject 0, which tends to be eliminated COMDAT
2547 if (dwarf_entrypc (function
, addr
) == 0 && *addr
!= 0)
2550 /* Assume the entry pc is the base address, or (if zero)
2551 the first address of the ranges covering this DIE. */
2552 Dwarf_Addr start
= 0, end
;
2553 if (dwarf_ranges (function
, 0, addr
, &start
, &end
) >= 0)
2566 dwflpp::die_entrypc (Dwarf_Die
* die
, Dwarf_Addr
* addr
)
2569 string lookup_method
;
2573 lookup_method
= "dwarf_entrypc";
2574 rc
= dwarf_entrypc (die
, addr
);
2578 lookup_method
= "dwarf_ranges";
2583 ptrdiff_t offset
= dwarf_ranges (die
, 0, &base
, &begin
, &end
);
2584 if (offset
< 0) rc
= -1;
2585 else if (offset
> 0)
2590 // Now we need to check that there are no more ranges
2591 // associated with this function, which could conceivably
2592 // happen if a function is inlined, then pieces of it are
2593 // split amongst different conditional branches. It's not
2594 // obvious which of them to favour. As a heuristic, we
2595 // pick the beginning of the first range, and ignore the
2596 // others (but with a warning).
2599 while ((offset
= dwarf_ranges (die
, offset
, &base
, &begin
, &end
)) > 0)
2602 lookup_method
+= _F(", ignored %s more", lex_cast(extra
).c_str());
2606 // PR10574: reject subprograms where the entrypc address turns out
2607 // to be 0, since they tend to correspond to duplicate-eliminated
2608 // COMDAT copies of C++ functions.
2609 if (rc
== 0 && *addr
== 0)
2611 lookup_method
+= _(" (skip comdat)");
2615 if (sess
.verbose
> 2)
2616 clog
<< _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64
" (rc %d)", lookup_method
.c_str(),
2617 lex_cast_hex(dwarf_dieoffset(die
)).c_str(), *addr
, rc
) << endl
;
2624 dwflpp::function_die (Dwarf_Die
*d
)
2632 dwflpp::function_file (char const ** c
)
2636 *c
= dwarf_decl_file (function
);
2639 // The line table might know.
2641 if (dwarf_lowpc(function
, &pc
) == 0)
2642 *c
= pc_line (pc
, NULL
, NULL
);
2645 *c
= "<unknown source>";
2651 dwflpp::function_line (int *linep
)
2654 if (dwarf_decl_line (function
, linep
) != 0)
2656 // The line table might know.
2658 if (dwarf_lowpc(function
, &pc
) == 0)
2659 pc_line (pc
, linep
, NULL
);
2665 dwflpp::die_has_pc (Dwarf_Die
& die
, Dwarf_Addr pc
)
2667 int res
= dwarf_haspc (&die
, pc
);
2668 // dwarf_ranges will return -1 if a function die has no DW_AT_ranges
2670 // DWARF_ASSERT ("dwarf_haspc", res);
2676 dwflpp::inner_die_containing_pc(Dwarf_Die
& scope
, Dwarf_Addr addr
,
2681 // Sometimes we're in a bad scope to begin with -- just let it be. This can
2682 // happen for example if the compiler outputs a label PC that's just outside
2683 // the lexical scope. We can't really do anything about that, but variables
2684 // will probably not be accessible in this case.
2685 if (!die_has_pc(scope
, addr
))
2688 Dwarf_Die child
, import
;
2689 int rc
= dwarf_child(&result
, &child
);
2692 switch (dwarf_tag (&child
))
2694 case DW_TAG_imported_unit
:
2695 // The children of the imported unit need to be treated as if
2696 // they are inserted here. So look inside and set result if
2698 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2700 Dwarf_Die import_result
;
2701 if (inner_die_containing_pc(import
, addr
, import_result
))
2703 result
= import_result
;
2709 // lexical tags to recurse within the same starting scope
2710 // NB: this intentionally doesn't cross into inlines!
2711 case DW_TAG_lexical_block
:
2712 case DW_TAG_with_stmt
:
2713 case DW_TAG_catch_block
:
2714 case DW_TAG_try_block
:
2715 case DW_TAG_entry_point
:
2716 if (die_has_pc(child
, addr
))
2719 rc
= dwarf_child(&result
, &child
);
2723 rc
= dwarf_siblingof(&child
, &child
);
2729 dwflpp::get_locals(vector
<Dwarf_Die
>& scopes
, set
<string
>& locals
)
2731 // XXX Shouldn't this be walking up to outer scopes too?
2733 get_locals_die(scopes
[0], locals
);
2737 dwflpp::get_locals_die(Dwarf_Die
& die
, set
<string
>& locals
)
2739 // Try to get the first child of die.
2740 Dwarf_Die child
, import
;
2741 if (dwarf_child (&die
, &child
) == 0)
2746 // Output each sibling's name (that is a variable or
2747 // parameter) to 'o'.
2748 switch (dwarf_tag (&child
))
2750 case DW_TAG_variable
:
2751 case DW_TAG_formal_parameter
:
2752 name
= dwarf_diename (&child
);
2754 locals
.insert(string("$") + name
);
2756 case DW_TAG_imported_unit
:
2757 // Treat the imported unit children as if they are
2758 // children of the given DIE.
2759 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2760 get_locals_die (import
, locals
);
2766 while (dwarf_siblingof (&child
, &child
) == 0);
2772 dwflpp::find_variable_and_frame_base (vector
<Dwarf_Die
>& scopes
,
2774 string
const & local
,
2775 const target_symbol
*e
,
2777 Dwarf_Attribute
*fb_attr_mem
,
2780 Dwarf_Die
*scope_die
= &scopes
[0];
2781 Dwarf_Attribute
*fb_attr
= NULL
;
2785 int declaring_scope
= dwarf_getscopevar (&scopes
[0], scopes
.size(),
2789 if (declaring_scope
< 0)
2792 get_locals(scopes
, locals
);
2793 string sugs
= levenshtein_suggest(local
, locals
); // probably not that many, so no limit
2795 throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)",
2797 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2798 module_name
.c_str(),
2799 lex_cast_hex(pc
).c_str(),
2800 (scope_die
== NULL
) ? "" : _("in"),
2801 (dwarf_diename(scope_die
) ?: "<unknown>"),
2802 (dwarf_diename(cu
) ?: "<unknown>"),
2804 ? (_("<no alternatives>"))
2805 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2808 throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)",
2810 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2811 module_name
.c_str(),
2812 (scope_die
== NULL
) ? "" : _("in"),
2813 (dwarf_diename(scope_die
) ?: "<unknown>"),
2816 ? (_("<no alternatives>"))
2817 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2821 *funcdie
= scopes
[declaring_scope
];
2823 /* Some GCC versions would output duplicate external variables, one
2824 without a location attribute. If so, try to find the other if it
2825 exists in the same scope. See GCC PR51410. */
2826 Dwarf_Attribute attr_mem
;
2827 if (dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
) == NULL
2828 && dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
) == NULL
2829 && dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
) != NULL
2830 && dwarf_tag(&scopes
[declaring_scope
]) == DW_TAG_compile_unit
)
2832 Dwarf_Die orig_vardie
= *vardie
;
2833 bool alt_found
= false;
2834 if (dwarf_child(&scopes
[declaring_scope
], vardie
) == 0)
2837 // Note, not handling DW_TAG_imported_unit, assuming GCC
2838 // version is recent enough to not need this workaround if
2839 // we would see an imported unit.
2840 if (dwarf_tag (vardie
) == DW_TAG_variable
2841 && strcmp (dwarf_diename (vardie
), local
.c_str ()) == 0
2842 && (dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
)
2844 && ((dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
)
2846 || (dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
)
2850 while (!alt_found
&& dwarf_siblingof(vardie
, vardie
) == 0);
2853 *vardie
= orig_vardie
;
2856 // Global vars don't need (cannot use) frame base in location descriptor.
2860 /* We start out walking the "lexical scopes" as returned by
2861 * as returned by dwarf_getscopes for the address, starting with the
2862 * declaring_scope that the variable was found in.
2864 vector
<Dwarf_Die
> physcopes
, *fbscopes
= &scopes
;
2865 for (size_t inner
= declaring_scope
;
2866 inner
< fbscopes
->size() && fb_attr
== NULL
;
2869 Dwarf_Die
& scope
= (*fbscopes
)[inner
];
2870 switch (dwarf_tag (&scope
))
2874 case DW_TAG_subprogram
:
2875 case DW_TAG_entry_point
:
2876 fb_attr
= dwarf_attr_integrate (&scope
,
2880 case DW_TAG_inlined_subroutine
:
2881 /* Unless we already are going through the "pyshical die tree",
2882 * we now need to start walking the die tree where this
2883 * subroutine is inlined to find the appropriate frame base. */
2884 if (declaring_scope
!= -1)
2886 physcopes
= getscopes_die(&scope
);
2887 if (physcopes
.empty())
2888 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
2889 local
.c_str()), e
->tok
);
2890 fbscopes
= &physcopes
;
2891 inner
= 0; // zero is current scope, for look will increase.
2892 declaring_scope
= -1;
2901 /* Returns a human readable string with suggested locations where a
2902 DIE attribute is valid. */
2904 suggested_locations_string(Dwarf_Attribute
*attr
)
2908 locsstr
= "<no alternatives for NULL attribute>";
2911 #if _ELFUTILS_PREREQ (0, 158)
2914 Dwarf_Addr base
, start
, end
;
2917 off
= dwarf_getlocations (attr
, off
, &base
,
2922 locsstr
= _("alternative locations: ");
2927 locsstr
+= lex_cast_hex(start
);
2929 locsstr
+= lex_cast_hex(end
);
2932 off
= dwarf_getlocations (attr
, off
, &base
,
2940 locsstr
= _("<no alternative locations>");
2942 locsstr
= _F("<error getting alternative locations: %s>",
2945 locsstr
= "<cannot suggest any alternative locations, elfutils too old>";
2946 #endif /* _ELFUTILS_PREREQ (0, 158) */
2952 /* Produce a human readable name for a DIE. */
2954 die_name_string (Dwarf_Die
*die
)
2957 const char *name
= dwarf_linkage_name(die
);
2959 name
= dwarf_diename (die
);
2961 size_t demangle_buffer_len
= 0;
2962 char *demangle_buffer
= NULL
;
2963 if (name
!= NULL
&& name
[0] == '_' && name
[1] == 'Z')
2966 char *dsymname
= abi::__cxa_demangle (name
, demangle_buffer
,
2967 &demangle_buffer_len
, &status
);
2969 name
= demangle_buffer
= dsymname
;
2974 res
= _("<unknown");
2975 free (demangle_buffer
);
2980 /* Returns a source file name, line and column information based on the
2981 pc and the current cu. */
2983 dwflpp::pc_line (Dwarf_Addr pc
, int *lineno
, int *colno
)
2987 Dwarf_Line
*line
= dwarf_getsrc_die (cu
, pc
);
2991 dwarf_lineno (line
, lineno
);
2993 dwarf_linecol (line
, colno
);
2994 return dwarf_linesrc (line
, NULL
, NULL
);
3001 /* Returns a source line and column string based on the inlined DIE
3002 or based on the pc if DIE is NULL. */
3004 dwflpp::pc_die_line_string (Dwarf_Addr pc
, Dwarf_Die
*die
)
3009 const char *src
= NULL
;
3013 src
= pc_line (pc
, &lineno
, &col
);
3017 if (dwarf_getsrcfiles (cu
, &files
, NULL
) == 0)
3019 Dwarf_Attribute attr
;
3021 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_file
, &attr
),
3024 src
= dwarf_filesrc (files
, val
, NULL
, NULL
);
3025 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_line
,
3029 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_column
,
3036 src
= pc_line (pc
, &lineno
, &col
);
3044 linestr
+= ":" + lex_cast(lineno
);
3046 linestr
+= ":" + lex_cast(col
);
3050 linestr
+= _("unknown source");
3055 /* Returns a human readable DIE offset for use in error messages.
3056 Includes DIE offset and DWARF file used. */
3058 dwflpp::die_location_as_string(Dwarf_Die
*die
)
3063 locstr
+= _("dieoffset: ");
3064 locstr
+= lex_cast_hex(dwarf_dieoffset(die
));
3067 const char *mainfile
, *debugfile
;
3068 locstr
+= _(" from ");
3069 if (dwfl_module_info (module
, NULL
, NULL
, NULL
, NULL
, NULL
, &mainfile
,
3071 || (mainfile
== NULL
&& debugfile
== NULL
))
3073 locstr
+= _("unknown debug file for ");
3074 locstr
+= module_name
;
3078 if (debugfile
!= NULL
)
3079 locstr
+= debugfile
;
3087 /* Returns a human readable (inlined) function and source file/line location
3088 for a pc location. */
3090 dwflpp::pc_location_as_function_string(Dwarf_Addr pc
)
3093 locstr
= _("function: ");
3095 /* Find the first function-like DIE with a name in scope. */
3096 Dwarf_Die funcdie_mem
;
3097 Dwarf_Die
*funcdie
= NULL
;
3098 string funcname
= "";
3099 Dwarf_Die
*scopes
= NULL
;
3100 int nscopes
= dwarf_getscopes (cu
, pc
, &scopes
);
3101 for (int i
= 0; funcname
== "" && i
< nscopes
; i
++)
3103 Dwarf_Die
*scope
= &scopes
[i
];
3104 int tag
= dwarf_tag (scope
);
3105 if (tag
== DW_TAG_subprogram
3106 || tag
== DW_TAG_inlined_subroutine
3107 || tag
== DW_TAG_entry_point
)
3108 funcname
= die_name_string (scope
);
3111 funcdie_mem
= *scope
;
3112 funcdie
= &funcdie_mem
;
3117 /* source location */
3119 locstr
+= _("<unknown> at ") + pc_die_line_string (pc
, NULL
);
3122 int nscopes
= dwarf_getscopes_die (funcdie
, &scopes
);
3125 /* scopes[0] == funcdie, the lowest level, for which we already have
3126 the name. This is the actual source location where it
3129 locstr
+= _(" at ");
3130 locstr
+= pc_die_line_string (pc
, NULL
);
3132 /* last_scope is the source location where the next inlined frame/function
3134 Dwarf_Die
*last_scope
= &scopes
[0];
3135 for (int i
= 1; i
< nscopes
; i
++)
3137 Dwarf_Die
*scope
= &scopes
[i
];
3138 int tag
= dwarf_tag (scope
);
3139 if (tag
!= DW_TAG_inlined_subroutine
3140 && tag
!= DW_TAG_entry_point
3141 && tag
!= DW_TAG_subprogram
)
3144 locstr
+= _(" inlined by ");
3145 locstr
+= die_name_string (scope
);
3146 locstr
+= _(" at ");
3147 locstr
+= pc_die_line_string (pc
, last_scope
);
3149 /* Found the "top-level" in which everything was inlined. */
3150 if (tag
== DW_TAG_subprogram
)
3159 locstr
+= _(" at ");
3160 locstr
+= pc_die_line_string (pc
, NULL
);
3169 dwflpp::translate_location(location_context
*ctx
,
3170 Dwarf_Attribute
*attr
, Dwarf_Die
*die
,
3171 Dwarf_Addr pc
, Dwarf_Attribute
*fb_attr
,
3172 const target_symbol
*e
, location
*input
)
3175 /* DW_AT_data_member_location, can be either constant offsets
3176 (struct member fields), or full blown location expressions. */
3178 /* There is no location expression, but a constant value instead. */
3179 if (dwarf_whatattr (attr
) == DW_AT_const_value
)
3180 return ctx
->translate_constant (attr
);
3185 /* PR9768: formerly, we added pc+module_bias here. However, that bias value
3186 is not present in the pc value by the time we get it, so adding it would
3187 result in false negatives of variable reachibility. In other instances
3188 further below, the c_translate_FOO functions, the module_bias value used
3189 to be passed in, but instead should now be zero for the same reason. */
3192 switch (dwarf_getlocation_addr (attr
, pc
/*+ module_bias*/, &expr
, &len
, 1))
3194 case 1: /* Should always happen. */
3199 case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */
3201 Dwarf_Addr pc2
= pr15123_retry_addr (pc
, die
);
3210 string msg
= _F("not accessible at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3211 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3212 err
.details
.push_back(die_location_as_string(die
));
3213 err
.details
.push_back(pc_location_as_function_string(pc
));
3214 err
.details
.push_back(suggested_locations_string(attr
));
3218 default: /* Shouldn't happen. */
3221 string msg
= _F("dwarf_getlocation_addr failed at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3222 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3223 string dwarf_err
= _F("dwarf_error: %s", dwarf_errmsg(-1));
3224 err
.details
.push_back(dwarf_err
);
3225 err
.details
.push_back(die_location_as_string(die
));
3226 err
.details
.push_back(pc_location_as_function_string(pc
));
3227 err
.details
.push_back(suggested_locations_string(attr
));
3232 Dwarf_Op
*cfa_ops
= NULL
;
3233 // pc is in the dw address space of the current module, which is what
3234 // c_translate_location expects. get_cfa_ops wants the global dwfl address.
3235 // cfa_ops only make sense for locals.
3238 Dwarf_Addr addr
= pc
+ module_bias
;
3239 cfa_ops
= get_cfa_ops (addr
);
3242 // ??? Reset these afterward.
3243 ctx
->cfa_ops
= cfa_ops
;
3244 ctx
->fb_attr
= fb_attr
;
3248 return ctx
->translate_location (expr
, len
, input
);
3253 dwflpp::get_members(Dwarf_Die
*typedie
, set
<string
>& members
, set
<string
> &dupes
)
3255 const int typetag
= dwarf_tag (typedie
);
3257 /* compile and partial unit included for recursion through
3258 imported_unit below. */
3259 if (typetag
!= DW_TAG_structure_type
&&
3260 typetag
!= DW_TAG_class_type
&&
3261 typetag
!= DW_TAG_union_type
&&
3262 typetag
!= DW_TAG_compile_unit
&&
3263 typetag
!= DW_TAG_partial_unit
)
3265 throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union",
3266 dwarf_type_name(typedie
).c_str()));
3269 // Try to get the first child of vardie.
3270 Dwarf_Die die_mem
, import
;
3271 Dwarf_Die
*die
= &die_mem
;
3272 switch (dwarf_child (typedie
, die
))
3274 case 1: // No children.
3275 throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie
).c_str()));
3278 default: // Shouldn't happen.
3279 throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie
).c_str(),
3286 // Add each sibling's name to members set
3289 int tag
= dwarf_tag(die
);
3291 /* The children of an imported_unit should be treated as members too. */
3292 if (tag
== DW_TAG_imported_unit
3293 && dwarf_attr_die(die
, DW_AT_import
, &import
))
3294 get_members(&import
, members
, dupes
);
3296 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3299 const char *member
= dwarf_diename (die
) ;
3301 if ( tag
== DW_TAG_member
&& member
!= NULL
)
3303 // Only output if this is new, to avoid inheritance dupes.
3304 if (dupes
.insert(member
).second
)
3305 members
.insert(member
);
3310 if (!dwarf_attr_die (die
, DW_AT_type
, &temp_die
))
3312 string source
= dwarf_decl_file(die
) ?: "<unknown source>";
3314 dwarf_decl_line(die
, &line
);
3315 throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous "
3316 "member at %s:%d", source
.c_str(), line
));
3319 get_members(&temp_die
, members
, dupes
);
3323 while (dwarf_siblingof (die
, die
) == 0);
3328 dwflpp::find_struct_member(const target_symbol::component
& c
,
3329 Dwarf_Die
*parentdie
,
3330 Dwarf_Die
*memberdie
,
3331 vector
<Dwarf_Die
>& dies
,
3332 vector
<Dwarf_Attribute
>& locs
)
3334 Dwarf_Attribute attr
;
3337 /* With inheritance, a subclass may mask member names of parent classes, so
3338 * our search among the inheritance tree must be breadth-first rather than
3339 * depth-first (recursive). The parentdie is still our starting point. */
3340 deque
<Dwarf_Die
> inheritees(1, *parentdie
);
3341 for (; !inheritees
.empty(); inheritees
.pop_front())
3343 switch (dwarf_child (&inheritees
.front(), &die
))
3345 case 0: /* First child found. */
3347 case 1: /* No children. */
3349 case -1: /* Error. */
3350 default: /* Shouldn't happen */
3351 throw SEMANTIC_ERROR (dwarf_type_name(&inheritees
.front()) + ": "
3352 + string (dwarf_errmsg (-1)),
3358 int tag
= dwarf_tag(&die
);
3359 /* recurse into imported units as if they are anonymoust structs */
3361 if (tag
== DW_TAG_imported_unit
3362 && dwarf_attr_die(&die
, DW_AT_import
, &import
)
3363 && find_struct_member(c
, &import
, memberdie
, dies
, locs
))
3366 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3369 const char *name
= dwarf_diename(&die
);
3370 if (tag
== DW_TAG_inheritance
)
3372 /* Remember inheritee for breadth-first search. */
3373 Dwarf_Die inheritee
;
3374 if (dwarf_attr_die (&die
, DW_AT_type
, &inheritee
))
3375 inheritees
.push_back(inheritee
);
3377 else if (name
== NULL
)
3379 /* Need to recurse for anonymous structs/unions. */
3381 if (dwarf_attr_die (&die
, DW_AT_type
, &subdie
) &&
3382 find_struct_member(c
, &subdie
, memberdie
, dies
, locs
))
3385 else if (name
== c
.member
)
3391 while (dwarf_siblingof (&die
, &die
) == 0);
3397 /* As we unwind the recursion, we need to build the chain of
3398 * locations that got to the final answer. */
3399 if (dwarf_attr_integrate (&die
, DW_AT_data_member_location
, &attr
))
3401 dies
.insert(dies
.begin(), die
);
3402 locs
.insert(locs
.begin(), attr
);
3405 /* Union members don't usually have a location,
3406 * but just use the containing union's location. */
3407 else if (dwarf_tag(parentdie
) != DW_TAG_union_type
)
3408 throw SEMANTIC_ERROR (_F("no location for field '%s':%s",
3409 c
.member
.c_str(), dwarf_errmsg(-1)), c
.tok
);
3416 dwarf_die_type (Dwarf_Die
*die
, Dwarf_Die
*typedie_mem
, const token
*tok
=NULL
)
3418 if (!dwarf_attr_die (die
, DW_AT_type
, typedie_mem
))
3419 throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok
);
3424 dwflpp::translate_components(location_context
*ctx
,
3426 const target_symbol
*e
,
3433 while (i
< e
->components
.size())
3435 const target_symbol::component
& c
= e
->components
[i
];
3437 switch (dwarf_tag (typedie
))
3439 case DW_TAG_typedef
:
3440 case DW_TAG_const_type
:
3441 case DW_TAG_volatile_type
:
3442 case DW_TAG_restrict_type
:
3443 /* Just iterate on the referent type. */
3444 dwarf_die_type (typedie
, typedie
, c
.tok
);
3447 case DW_TAG_reference_type
:
3448 case DW_TAG_rvalue_reference_type
:
3449 translate_pointer (*ctx
, typedie
, lvalue
);
3450 dwarf_die_type (typedie
, typedie
, c
.tok
);
3453 case DW_TAG_pointer_type
:
3454 /* A pointer with no type is a void* -- can't dereference it. */
3455 if (!dwarf_hasattr_integrate (typedie
, DW_AT_type
))
3456 throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c
).c_str(),
3457 dwarf_type_name(typedie
).c_str()), c
.tok
);
3459 if (!ctx
->locations
.empty())
3460 translate_pointer (*ctx
, typedie
, lvalue
);
3461 if (c
.type
!= target_symbol::comp_literal_array_index
&&
3462 c
.type
!= target_symbol::comp_expression_array_index
)
3464 dwarf_die_type (typedie
, typedie
, c
.tok
);
3468 /* else an array access */
3470 case DW_TAG_array_type
:
3471 if (c
.type
== target_symbol::comp_literal_array_index
)
3473 if (c
.num_index
!= 0 && !ctx
->locations
.empty())
3474 ctx
->translate_array(typedie
, ctx
->locations
.back(),
3475 new literal_number(c
.num_index
));
3477 else if (c
.type
== target_symbol::comp_expression_array_index
)
3479 if (!ctx
->locations
.empty())
3480 ctx
->translate_array(typedie
, ctx
->locations
.back(),
3484 throw SEMANTIC_ERROR (_F("invalid access '%s' for array type",
3485 lex_cast(c
).c_str()), c
.tok
);
3487 dwarf_die_type (typedie
, typedie
, c
.tok
);
3492 case DW_TAG_structure_type
:
3493 case DW_TAG_union_type
:
3494 case DW_TAG_class_type
:
3495 if (c
.type
!= target_symbol::comp_struct_member
)
3496 throw SEMANTIC_ERROR (_F("invalid access '%s' for %s",
3497 lex_cast(c
).c_str(), dwarf_type_name(typedie
).c_str()));
3499 if (dwarf_hasattr(typedie
, DW_AT_declaration
))
3501 Dwarf_Die
*tmpdie
= declaration_resolve(typedie
);
3503 throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie
).c_str()), c
.tok
);
3508 vector
<Dwarf_Die
> dies
;
3509 vector
<Dwarf_Attribute
> locs
;
3510 if (!find_struct_member(c
, typedie
, vardie
, dies
, locs
))
3512 /* Add a file:line hint for anonymous types */
3514 if (!dwarf_hasattr_integrate(typedie
, DW_AT_name
))
3517 const char *file
= dwarf_decl_file(typedie
);
3518 if (file
&& dwarf_decl_line(typedie
, &line
) == 0)
3519 source
= " (" + string(file
) + ":"
3520 + lex_cast(line
) + ")";
3523 set
<string
> members
, member_dupes
;
3524 get_members(typedie
, members
, member_dupes
);
3525 string sugs
= levenshtein_suggest(c
.member
, members
);
3527 sugs
= " (alternatives: " + sugs
+ ")";
3528 throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c
.member
.c_str(),
3529 dwarf_type_name(typedie
).c_str(), source
.c_str(),
3530 sugs
.c_str()), c
.tok
);
3533 if (!ctx
->locations
.empty())
3535 location
*n
= ctx
->locations
.back();
3536 for (unsigned j
= 0; j
< locs
.size(); ++j
)
3537 n
= translate_location (ctx
, &locs
[j
], &dies
[j
],
3539 ctx
->locations
.push_back(n
);
3543 dwarf_die_type (vardie
, typedie
, c
.tok
);
3547 case DW_TAG_enumeration_type
:
3548 case DW_TAG_base_type
:
3549 throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c
).c_str(),
3550 dwarf_type_name(typedie
).c_str()), c
.tok
);
3554 throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c
.tok
);
3558 throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie
).c_str(),
3559 lex_cast(dwarf_tag(typedie
)).c_str()), c
.tok
);
3567 dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die
*typedie
,
3568 Dwarf_Die
*innerdie
,
3569 const target_symbol
*e
)
3571 int typetag
= dwarf_tag (typedie
);
3572 *innerdie
= *typedie
;
3573 while (typetag
== DW_TAG_typedef
||
3574 typetag
== DW_TAG_const_type
||
3575 typetag
== DW_TAG_volatile_type
||
3576 typetag
== DW_TAG_restrict_type
)
3578 if (!dwarf_attr_die (innerdie
, DW_AT_type
, innerdie
))
3579 throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e
->tok
);
3580 typetag
= dwarf_tag (innerdie
);
3585 get_bitfield (const target_symbol
*e
, Dwarf_Die
*die
, Dwarf_Word byte_size
,
3586 Dwarf_Word
*bit_offset
, Dwarf_Word
*bit_size
)
3588 Dwarf_Attribute attr_mem
;
3589 if (dwarf_attr_integrate (die
, DW_AT_bit_offset
, &attr_mem
) == NULL
3590 || dwarf_formudata (&attr_mem
, bit_offset
) != 0
3591 || dwarf_attr_integrate (die
, DW_AT_bit_size
, &attr_mem
) == NULL
3592 || dwarf_formudata (&attr_mem
, bit_size
) != 0)
3593 throw SEMANTIC_ERROR (_F("cannot get bit field parameters: %s",
3594 dwarf_errmsg(-1)), e
->tok
);
3596 /* Convert the big-bit-endian numbers from Dwarf to little-endian.
3597 This means we can avoid having to propagate byte_size further. */
3598 *bit_offset
= byte_size
* 8 - *bit_offset
- *bit_size
;
3602 dwflpp::translate_base_ref (location_context
&ctx
, Dwarf_Word byte_size
,
3603 bool signed_p
, bool lvalue_p
)
3605 location
*loc
= ctx
.locations
.back ();
3611 // The existing program is the value.
3616 target_deref
*d
= new target_deref
;
3617 d
->tok
= ctx
.e
->tok
;
3618 d
->addr
= loc
->program
;
3619 d
->size
= byte_size
;
3620 d
->signed_p
= signed_p
;
3621 d
->userspace_p
= ctx
.userspace_p
;
3623 loc
= ctx
.new_location(loc_value
);
3625 loc
->byte_size
= byte_size
;
3627 // ??? There is code in *translate.cxx to handle the sign-
3628 // (or implicit zero-) extension during the load. We might
3629 // be better off falling through to the shared code below.
3634 if (loc
->offset
!= 0)
3635 throw SEMANTIC_ERROR (_("cannot handle offset into register"), ctx
.e
->tok
);
3636 // The existing program is the value.
3641 if (loc
->byte_size
< byte_size
)
3642 throw SEMANTIC_ERROR (_("requested size larger than constant"),
3645 /* ??? Byte ordering. */
3651 val
= *(int8_t *)loc
->constant_block
;
3653 val
= *(uint8_t *)loc
->constant_block
;
3657 val
= *(int16_t *)loc
->constant_block
;
3659 val
= *(uint16_t *)loc
->constant_block
;
3663 val
= *(int32_t *)loc
->constant_block
;
3665 val
= *(uint32_t *)loc
->constant_block
;
3668 val
= *(int64_t *)loc
->constant_block
;
3671 throw SEMANTIC_ERROR (_("unhandled constant size"), ctx
.e
->tok
);
3674 loc
= ctx
.new_location(loc_value
);
3675 loc
->program
= new literal_number(val
);
3676 loc
->byte_size
= byte_size
;
3680 case loc_noncontiguous
:
3682 if (loc
&& loc
->byte_size
<= byte_size
)
3684 ctx
.locations
.push_back(loc
);
3687 throw SEMANTIC_ERROR (_("noncontiguous location for base fetch"),
3690 case loc_implicit_pointer
:
3691 if (loc
->offset
!= 0)
3692 throw SEMANTIC_ERROR (_("cannot handle offset into implicit pointer"),
3697 ctx
.locations
.push_back(loc
);
3700 throw SEMANTIC_ERROR (_("pointer optimized out"), ctx
.e
->tok
);
3702 case loc_unavailable
:
3703 throw SEMANTIC_ERROR (_("location not available"), ctx
.e
->tok
);
3708 // Normalize LOC from the sign and width of the type to int64_t.
3709 // ??? We might require extending when assigning to a target_register.
3710 // Give this a try first...
3711 if (byte_size
< 8 && !lvalue_p
)
3713 binary_expression
*out
= new binary_expression
;
3714 out
->tok
= ctx
.e
->tok
;
3718 int shift
= 64 - 8 * byte_size
;
3719 binary_expression
*shl
= new binary_expression
;
3720 shl
->tok
= ctx
.e
->tok
;
3722 shl
->left
= loc
->program
;
3723 shl
->right
= new literal_number(shift
);
3727 out
->right
= new literal_number(shift
);
3732 out
->left
= loc
->program
;
3733 out
->right
= new literal_number((1ull << (byte_size
* 8)) - 1);
3736 loc
= ctx
.new_location(loc_value
);
3738 loc
->byte_size
= byte_size
;
3743 dwflpp::translate_bitfield(location_context
&ctx
, Dwarf_Word byte_size
,
3744 Dwarf_Word bit_offset
, Dwarf_Word bit_size
,
3747 location
*loc
= ctx
.locations
.back ();
3749 target_bitfield
*bf
= new target_bitfield
;
3750 bf
->tok
= ctx
.e
->tok
;
3751 bf
->base
= loc
->program
;
3752 bf
->offset
= bit_offset
;
3753 bf
->size
= bit_size
;
3754 bf
->signed_p
= signed_p
;
3756 loc
= ctx
.new_location(loc_value
);
3758 loc
->byte_size
= byte_size
;
3759 ctx
.locations
.push_back(loc
);
3762 // As usual, leave the result as the last location in ctx.
3764 dwflpp::translate_final_fetch_or_store (location_context
&ctx
,
3766 Dwarf_Die
*start_typedie
,
3770 const target_symbol
*e
= ctx
.e
;
3772 /* First boil away any qualifiers associated with the type DIE of
3773 the final location to be accessed. */
3774 resolve_unqualified_inner_typedie (start_typedie
, typedie
, e
);
3776 /* If we're looking for an address, then we can just provide what
3777 we computed to this point, without using a fetch/store. */
3778 if (ctx
.e
->addressof
)
3781 throw SEMANTIC_ERROR (_("cannot write to member address"), e
->tok
);
3783 if (dwarf_hasattr_integrate (vardie
, DW_AT_bit_offset
)
3784 || dwarf_hasattr_integrate (vardie
, DW_AT_data_bit_offset
))
3785 throw SEMANTIC_ERROR (_("cannot take the address of a bit field"),
3788 switch (ctx
.locations
.back()->type
)
3791 /* do nothing, we're done */
3794 throw SEMANTIC_ERROR (_("cannot take address of object in register"),
3797 case loc_noncontiguous
:
3798 throw SEMANTIC_ERROR (_("cannot take address of noncontiguous object"),
3802 throw SEMANTIC_ERROR (_("cannot take address of computed value"),
3806 throw SEMANTIC_ERROR (_("cannot take address of constant value"),
3809 case loc_unavailable
:
3810 throw SEMANTIC_ERROR (_("cannot take address of unavailable value"),
3813 case loc_implicit_pointer
:
3814 throw SEMANTIC_ERROR (_("cannot take address of implicit pointer"),
3824 /* Then switch behavior depending on the type of fetch/store we
3825 want, and the type and pointer-ness of the final location. */
3827 int typetag
= dwarf_tag (typedie
);
3831 throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s",
3832 lex_cast(typetag
).c_str(),
3833 dwarf_type_name(typedie
).c_str()), e
->tok
);
3835 case DW_TAG_structure_type
:
3836 case DW_TAG_class_type
:
3837 case DW_TAG_union_type
:
3839 string type_name
= dwarf_type_name(typedie
);
3840 string decl_file
= dwarf_decl_file(typedie
) ?: "";
3842 (void) dwarf_decl_line(typedie
, &decl_line
);
3844 // PR20423: assemble an error message at least as informative
3845 // as bad-member message in translate_components()
3846 if (decl_file
[0] && decl_line
> 0)
3847 decl_source
= " (" + decl_file
+ ":" + lex_cast(decl_line
) + ")";
3851 set
<string
> members
;
3853 get_members (typedie
, members
, dupes
);
3854 if (members
.begin() != members
.end())
3856 a_member
= " such as '->" + (*members
.begin()) + "'";
3861 // leave a_member empty
3863 throw SEMANTIC_ERROR (_F("'%s'%s is being accessed instead of a member%s",
3865 decl_source
.c_str(),
3867 (e
->components
.size() > 0 ?
3868 (e
->components
[e
->components
.size()-1].tok
) :
3872 case DW_TAG_base_type
:
3873 case DW_TAG_enumeration_type
:
3874 // Reject types we can't handle in systemtap
3876 bool signed_p
= false;
3878 if (typetag
== DW_TAG_base_type
)
3880 Dwarf_Attribute encoding_attr
;
3881 Dwarf_Word encoding
= (Dwarf_Word
) -1;
3883 dwarf_formudata (dwarf_attr_integrate (typedie
, DW_AT_encoding
,
3884 &encoding_attr
), &encoding
);
3885 if (encoding
== (Dwarf_Word
) -1)
3887 throw (SEMANTIC_ERROR
3888 (_F("unsupported type (mystery encoding %s for %s",
3889 lex_cast(encoding
).c_str(),
3890 dwarf_type_name(typedie
).c_str()), e
->tok
));
3893 if (encoding
== DW_ATE_float
3894 || encoding
== DW_ATE_complex_float
3895 /* XXX || many others? */)
3897 throw (SEMANTIC_ERROR
3898 (_F("unsupported type (encoding %s) for %s",
3899 lex_cast(encoding
).c_str(),
3900 dwarf_type_name(typedie
).c_str()), e
->tok
));
3903 signed_p
= (encoding
== DW_ATE_signed
3904 || encoding
== DW_ATE_signed_char
);
3907 Dwarf_Attribute size_attr
;
3908 Dwarf_Word byte_size
;
3909 if (dwarf_attr_integrate (typedie
, DW_AT_byte_size
, &size_attr
) == NULL
3910 || dwarf_formudata (&size_attr
, &byte_size
) != 0)
3912 throw (SEMANTIC_ERROR
3913 (_F("cannot get byte_size attribute for type %s: %s",
3914 dwarf_diename (typedie
) ?: "<anonymous>",
3915 dwarf_errmsg (-1)), e
->tok
));
3918 translate_base_ref (ctx
, byte_size
, signed_p
, lvalue
);
3920 if (dwarf_hasattr_integrate (vardie
, DW_AT_bit_offset
))
3922 Dwarf_Word bit_offset
= 0;
3923 Dwarf_Word bit_size
= 0;
3924 get_bitfield (e
, vardie
, byte_size
, &bit_offset
, &bit_size
);
3925 translate_bitfield (ctx
, byte_size
, bit_offset
,
3926 bit_size
, signed_p
);
3931 case DW_TAG_array_type
:
3932 case DW_TAG_pointer_type
:
3933 case DW_TAG_reference_type
:
3934 case DW_TAG_rvalue_reference_type
:
3937 if (typetag
== DW_TAG_array_type
)
3938 throw SEMANTIC_ERROR (_("cannot write to array address"), e
->tok
);
3939 if (typetag
== DW_TAG_reference_type
||
3940 typetag
== DW_TAG_rvalue_reference_type
)
3941 throw SEMANTIC_ERROR (_("cannot write to reference"), e
->tok
);
3942 assert (typetag
== DW_TAG_pointer_type
);
3944 if (typetag
!= DW_TAG_array_type
)
3945 translate_pointer (ctx
, typedie
, lvalue
);
3951 dwflpp::translate_pointer(location_context
&ctx
, Dwarf_Die
*typedie
,
3954 // ??? We also get DW_TAG_array_type here.
3955 // assert (dwarf_tag (typedie) == DW_TAG_pointer_type ||
3956 // dwarf_tag (typedie) == DW_TAG_reference_type ||
3957 // dwarf_tag (typedie) == DW_TAG_rvalue_reference_type);
3959 location
*loc
= ctx
.locations
.back ();
3960 if (loc
->type
!= loc_implicit_pointer
)
3962 Dwarf_Word byte_size
;
3963 if (dwarf_aggregate_size (typedie
, &byte_size
) != 0)
3964 throw SEMANTIC_ERROR (_F("cannot get size for type %s: %s",
3965 dwarf_diename (typedie
) ?: "<anonymous>",
3966 dwarf_errmsg (-1)), ctx
.e
->tok
);
3968 /* We know this is a pointer, therefore the signedness is irrelevant. */
3969 translate_base_ref (ctx
, byte_size
, false, lvalue
);
3970 /* We're going to want to dereference this pointer. Therefore note
3971 that this is an address. */
3972 loc
= ctx
.locations
.back ();
3973 loc
->type
= loc_address
;
3978 dwflpp::vardie_from_symtable (Dwarf_Die
*vardie
, Dwarf_Addr
*addr
)
3980 const char *name
= dwarf_linkage_name (vardie
) ?: dwarf_diename (vardie
);
3982 if (sess
.verbose
> 2)
3983 clog
<< _F("finding symtable address for %s\n", name
);
3986 int syms
= dwfl_module_getsymtab (module
);
3987 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
3989 for (int i
= 0; *addr
== 0 && i
< syms
; i
++)
3993 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
3995 && ! strcmp (name
, symname
)
3996 && sym
.st_shndx
!= SHN_UNDEF
3997 && (GELF_ST_TYPE (sym
.st_info
) == STT_NOTYPE
// PR13284
3998 || GELF_ST_TYPE (sym
.st_info
) == STT_OBJECT
))
3999 *addr
= sym
.st_value
;
4002 // Don't relocate for the kernel, or kernel modules we handle those
4003 // specially in emit_address.
4004 if (dwfl_module_relocations (module
) == 1 && module_name
!= TOK_KERNEL
)
4005 dwfl_module_relocate_address (module
, addr
);
4007 if (sess
.verbose
> 2)
4008 clog
<< _F("found %s @%#" PRIx64
"\n", name
, *addr
);
4014 dwflpp::literal_stmt_for_local (location_context
&ctx
,
4015 vector
<Dwarf_Die
>& scopes
,
4016 string
const & local
,
4017 const target_symbol
*e
,
4021 Dwarf_Die vardie
, funcdie
;
4022 Dwarf_Attribute fb_attr_mem
, *fb_attr
= NULL
;
4024 // NB: when addr_loc is used for a synthesized DW_OP_addr below, then it
4025 // needs to remain valid until express_as_string() has finished with it.
4028 fb_attr
= find_variable_and_frame_base (scopes
, ctx
.pc
, local
, e
,
4029 &vardie
, &fb_attr_mem
,
4035 clog
<< _F("finding location for local '%s' near address %#" PRIx64
4036 ", module bias %#" PRIx64
"\n", local
.c_str(), ctx
.pc
,
4039 clog
<< _F("finding location for global '%s' in CU '%s'\n",
4040 local
.c_str(), cu_name().c_str());
4043 /* Given $foo->bar->baz[NN], translate the location of foo. */
4044 Dwarf_Attribute attr_mem
;
4045 ctx
.attr
= &attr_mem
;
4046 ctx
.fb_attr
= fb_attr
;
4048 ctx
.function
= &funcdie
;
4050 if (dwarf_attr_integrate (&vardie
, DW_AT_const_value
, &attr_mem
) == NULL
4051 && dwarf_attr_integrate (&vardie
, DW_AT_location
, &attr_mem
) == NULL
)
4053 memset(&addr_loc
, 0, sizeof(Dwarf_Op
));
4054 addr_loc
.atom
= DW_OP_addr
;
4055 // If it is an external variable try the symbol table. PR10622.
4056 if (dwarf_attr_integrate (&vardie
, DW_AT_external
, &attr_mem
) != NULL
4057 && vardie_from_symtable (&vardie
, &addr_loc
.number
) != 0)
4059 ctx
.translate_location (&addr_loc
, 1, NULL
);
4063 string msg
= _F("failed to retrieve location attribute for '%s' [man error::dwarf]", local
.c_str());
4064 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
4065 err
.details
.push_back(die_location_as_string(&vardie
));
4066 err
.details
.push_back(pc_location_as_function_string(ctx
.pc
));
4071 translate_location (&ctx
, &attr_mem
, &vardie
, ctx
.pc
, fb_attr
, e
, NULL
);
4073 /* Translate the ->bar->baz[NN] parts. */
4076 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
4078 string msg
= _F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str());
4079 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
4080 err
.details
.push_back(die_location_as_string(&vardie
));
4081 err
.details
.push_back(pc_location_as_function_string(ctx
.pc
));
4085 translate_components (&ctx
, ctx
.pc
, e
, &vardie
, &typedie
, lvalue
, 0);
4087 /* Translate the assignment part, either
4088 x = $foo->bar->baz[NN]
4090 $foo->bar->baz[NN] = x
4092 translate_final_fetch_or_store (ctx
, &vardie
, &typedie
, lvalue
, die_mem
);
4097 dwflpp::translate_call_site_value (location_context
*ctx
,
4098 Dwarf_Attribute
*attr
,
4103 Dwarf_Attribute fb_attr_mem
, *fb_attr
= NULL
;
4105 vector
<Dwarf_Die
> scopes
= getscopes (funcdie
);
4106 vector
<Dwarf_Die
> physcopes
, *fbscopes
= &scopes
;
4107 int declaring_scope
= 0;
4108 for (size_t inner
= declaring_scope
;
4109 inner
< fbscopes
->size() && fb_attr
== NULL
;
4112 Dwarf_Die
& scope
= (*fbscopes
)[inner
];
4113 switch (dwarf_tag (&scope
))
4117 case DW_TAG_subprogram
:
4118 fb_attr
= dwarf_attr_integrate (&scope
,
4122 case DW_TAG_inlined_subroutine
:
4123 if (declaring_scope
!= -1)
4125 physcopes
= getscopes_die(&scope
);
4126 if (physcopes
.empty())
4127 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
4128 ctx
->e
->sym_name().c_str()), ctx
->e
->tok
);
4129 fbscopes
= &physcopes
;
4131 declaring_scope
= -1;
4137 return translate_location (ctx
, attr
, die
, pc
, fb_attr
, ctx
->e
, NULL
);
4141 dwflpp::type_die_for_local (vector
<Dwarf_Die
>& scopes
,
4143 string
const & local
,
4144 const target_symbol
*e
,
4148 Dwarf_Die vardie
, funcdie
;
4149 Dwarf_Attribute attr_mem
;
4151 find_variable_and_frame_base (scopes
, pc
, local
, e
, &vardie
, &attr_mem
, &funcdie
);
4153 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
4154 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str()), e
->tok
);
4156 location_context
ctx(const_cast<target_symbol
*>(e
));
4159 translate_components (&ctx
, pc
, e
, &vardie
, typedie
, lvalue
);
4165 dwflpp::literal_stmt_for_return (location_context
&ctx
,
4166 Dwarf_Die
*scope_die
,
4167 const target_symbol
*e
,
4172 clog
<< _F("literal_stmt_for_return: finding return value for %s (%s)\n",
4173 (dwarf_diename(scope_die
) ?: "<unknown>"), (dwarf_diename(cu
) ?: "<unknown>"));
4175 /* Given $return->bar->baz[NN], translate the location of return. */
4176 const Dwarf_Op
*locops
;
4177 int nlocops
= dwfl_module_return_value_location (module
, scope_die
,
4180 throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)",
4181 (dwarf_diename(scope_die
) ?: "<unknown>"),
4182 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4183 // the function has no return value (e.g. "void" in C)
4184 else if (nlocops
== 0)
4185 throw SEMANTIC_ERROR(_F("function %s (%s) has no return value",
4186 (dwarf_diename(scope_die
) ?: "<unknown>"),
4187 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4189 ctx
.translate_location (locops
, nlocops
, NULL
);
4191 /* Translate the ->bar->baz[NN] parts. */
4193 Dwarf_Die vardie
= *scope_die
, typedie
;
4194 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
4195 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4196 (dwarf_diename(&vardie
) ?: "<unknown>"),
4197 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4199 translate_components (&ctx
, ctx
.pc
, e
, &vardie
, &typedie
, lvalue
);
4201 /* Translate the assignment part, either
4202 x = $return->bar->baz[NN]
4204 $return->bar->baz[NN] = x
4206 translate_final_fetch_or_store (ctx
, &vardie
, &typedie
, lvalue
, die_mem
);
4211 dwflpp::type_die_for_return (Dwarf_Die
*scope_die
,
4213 const target_symbol
*e
,
4217 Dwarf_Die vardie
= *scope_die
;
4218 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
4219 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4220 (dwarf_diename(&vardie
) ?: "<unknown>"),
4221 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4223 translate_components (NULL
, pc
, e
, &vardie
, typedie
, lvalue
);
4229 dwflpp::literal_stmt_for_pointer (location_context
&ctx
,
4230 Dwarf_Die
*start_typedie
,
4231 const target_symbol
*e
,
4236 clog
<< _F("literal_stmt_for_pointer: finding value for %s (%s)\n",
4237 dwarf_type_name(start_typedie
).c_str(), (dwarf_diename(cu
) ?: "<unknown>"));
4239 assert(ctx
.pointer
!= NULL
);
4240 location
*tail
= ctx
.translate_argument (ctx
.pointer
);
4242 /* Translate the ->bar->baz[NN] parts. */
4245 Dwarf_Die typedie
= *start_typedie
, vardie
= typedie
;
4247 /* As a special case when typedie is not an array or pointer, we can
4248 * allow array indexing on STAP_ARG_pointer instead (since we do
4249 * know the pointee type and can determine its size). PR11556. */
4250 const target_symbol::component
* c
=
4251 e
->components
.empty() ? NULL
: &e
->components
[0];
4252 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
4253 c
->type
== target_symbol::comp_expression_array_index
))
4255 resolve_unqualified_inner_typedie (&typedie
, &typedie
, e
);
4256 int typetag
= dwarf_tag (&typedie
);
4257 if (typetag
!= DW_TAG_pointer_type
&&
4258 typetag
!= DW_TAG_array_type
)
4260 if (c
->type
== target_symbol::comp_literal_array_index
)
4261 tail
= ctx
.translate_array_pointer (&typedie
, tail
,
4262 new literal_number (c
->num_index
));
4264 tail
= ctx
.translate_array_pointer (&typedie
, tail
,
4270 /* Now translate the rest normally. */
4271 translate_components (&ctx
, 0, e
, &vardie
, &typedie
, lvalue
, first
);
4273 /* Translate the assignment part, either
4274 x = (STAP_ARG_pointer)->bar->baz[NN]
4276 (STAP_ARG_pointer)->bar->baz[NN] = x
4278 translate_final_fetch_or_store (ctx
, &vardie
, &typedie
, lvalue
, die_mem
);
4283 dwflpp::type_die_for_pointer (Dwarf_Die
*start_typedie
,
4284 const target_symbol
*e
,
4289 *typedie
= *start_typedie
;
4290 Dwarf_Die vardie
= *typedie
;
4292 /* Handle the same PR11556 case as above. */
4293 const target_symbol::component
* c
=
4294 e
->components
.empty() ? NULL
: &e
->components
[0];
4295 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
4296 c
->type
== target_symbol::comp_expression_array_index
))
4298 resolve_unqualified_inner_typedie (typedie
, typedie
, e
);
4299 int typetag
= dwarf_tag (typedie
);
4300 if (typetag
!= DW_TAG_pointer_type
&&
4301 typetag
!= DW_TAG_array_type
)
4305 location_context
ctx(const_cast<target_symbol
*>(e
));
4307 translate_components (&ctx
, 0, e
, &vardie
, typedie
, lvalue
, first
);
4313 in_kprobes_function(systemtap_session
& sess
, Dwarf_Addr addr
)
4315 if (sess
.sym_kprobes_text_start
!= 0 && sess
.sym_kprobes_text_end
!= 0)
4317 // If the probe point address is anywhere in the __kprobes
4318 // address range, we can't use this probe point.
4319 if (addr
>= sess
.sym_kprobes_text_start
&& addr
< sess
.sym_kprobes_text_end
)
4326 enum dwflpp::blacklisted_type
4327 dwflpp::blacklisted_p(interned_string funcname
,
4328 interned_string filename
,
4330 interned_string module
,
4334 if (!blacklist_enabled
)
4335 return dwflpp::blacklisted_none
;
4337 enum dwflpp::blacklisted_type blacklisted
= dwflpp::blacklisted_none
;
4339 // check against section blacklist
4340 string section
= get_blacklist_section(addr
);
4342 // PR6503: modules don't need special init/exit treatment
4343 if (module
== TOK_KERNEL
&& !regexec (&blacklist_section
, section
.c_str(), 0, NULL
, 0))
4344 blacklisted
= dwflpp::blacklisted_section
;
4346 // Check for function marked '__kprobes'.
4347 else if (module
== TOK_KERNEL
&& in_kprobes_function(sess
, addr
))
4348 blacklisted
= dwflpp::blacklisted_kprobes
;
4350 // Check probe point against function blacklist
4351 else if (!regexec(&blacklist_func
, funcname
.to_string().c_str(), 0, NULL
, 0))
4352 blacklisted
= dwflpp::blacklisted_function
;
4354 // Check probe point against function return blacklist
4355 else if (has_return
&& !regexec(&blacklist_func_ret
, funcname
.to_string().c_str(), 0, NULL
, 0))
4356 blacklisted
= dwflpp::blacklisted_function_return
;
4358 // Check probe point against file blacklist
4359 else if (!regexec(&blacklist_file
, filename
.to_string().c_str(), 0, NULL
, 0))
4360 blacklisted
= dwflpp::blacklisted_file
;
4365 clog
<< _(" - blacklisted");
4368 blacklisted
= dwflpp::blacklisted_none
;
4370 clog
<< _(" but not skipped (guru mode enabled)");
4374 // This probe point is not blacklisted.
4380 dwflpp::build_kernel_blacklist()
4382 // We build up the regexps in these strings
4384 // Add ^ anchors at the front; $ will be added just before regcomp.
4387 string blfn_ret
= "^(";
4388 string blfile
= "^(";
4389 string blsection
= "^(";
4391 blsection
+= "\\.init\\."; // first alternative, no "|"
4392 blsection
+= "|\\.exit\\.";
4393 blsection
+= "|\\.devinit\\.";
4394 blsection
+= "|\\.devexit\\.";
4395 blsection
+= "|\\.cpuinit\\.";
4396 blsection
+= "|\\.cpuexit\\.";
4397 blsection
+= "|\\.meminit\\.";
4398 blsection
+= "|\\.memexit\\.";
4400 /* NOTE all include/asm .h blfile patterns might need "full path"
4401 so prefix those with '.*' - see PR13108 and PR13112. */
4402 blfile
+= "kernel/kprobes\\.c"; // first alternative, no "|"
4403 blfile
+= "|arch/.*/kernel/kprobes\\.c";
4404 blfile
+= "|.*/include/asm/io\\.h";
4405 blfile
+= "|.*/include/asm/io-defs\\.h";
4406 blfile
+= "|.*/include/asm/io_64\\.h";
4407 blfile
+= "|.*/include/asm/bitops\\.h";
4408 blfile
+= "|drivers/ide/ide-iops\\.c";
4410 blfile
+= "|arch/.*/kernel/paravirt\\.c";
4411 blfile
+= "|.*/include/asm/paravirt\\.h";
4413 // XXX: it would be nice if these blacklisted functions were pulled
4414 // in dynamically, instead of being statically defined here.
4415 // Perhaps it could be populated from script files. A "noprobe
4416 // kernel.function("...")" construct might do the trick.
4418 // Most of these are marked __kprobes in newer kernels. We list
4419 // them here (anyway) so the translator can block them on older
4420 // kernels that don't have the __kprobes function decorator. This
4421 // also allows detection of problems at translate- rather than
4424 blfn
+= "atomic_notifier_call_chain"; // first blfn; no "|"
4425 blfn
+= "|default_do_nmi";
4428 blfn
+= "|do_debug";
4429 blfn
+= "|do_general_protection";
4432 blfn
+= "|do_page_fault";
4433 blfn
+= "|do_sparc64_fault";
4435 blfn
+= "|dummy_nmi_callback";
4436 blfn
+= "|flush_icache_range";
4437 blfn
+= "|ia64_bad_break";
4438 blfn
+= "|ia64_do_page_fault";
4439 blfn
+= "|ia64_fault";
4440 blfn
+= "|io_check_error";
4441 blfn
+= "|mem_parity_error";
4442 blfn
+= "|nmi_watchdog_tick";
4443 blfn
+= "|notifier_call_chain";
4444 blfn
+= "|oops_begin";
4445 blfn
+= "|oops_end";
4446 blfn
+= "|program_check_exception";
4447 blfn
+= "|single_step_exception";
4448 blfn
+= "|sync_regs";
4449 blfn
+= "|unhandled_fault";
4450 blfn
+= "|unknown_nmi_error";
4451 blfn
+= "|xen_[gs]et_debugreg";
4452 blfn
+= "|xen_irq_.*";
4453 blfn
+= "|xen_.*_fl_direct.*";
4454 blfn
+= "|check_events";
4455 blfn
+= "|xen_adjust_exception_frame";
4456 blfn
+= "|xen_iret.*";
4457 blfn
+= "|xen_sysret64.*";
4458 blfn
+= "|test_ti_thread_flag.*";
4459 blfn
+= "|inat_get_opcode_attribute";
4460 blfn
+= "|system_call_after_swapgs";
4461 blfn
+= "|HYPERVISOR_[gs]et_debugreg";
4462 blfn
+= "|HYPERVISOR_event_channel_op";
4464 blfn
+= "|hash_ptr";
4465 blfn
+= "|native_set_pte";
4468 blfn
+= "|.*raw_.*_lock.*";
4469 blfn
+= "|.*raw_.*_unlock.*";
4470 blfn
+= "|.*raw_.*_trylock.*";
4471 blfn
+= "|.*read_lock.*";
4472 blfn
+= "|.*read_unlock.*";
4473 blfn
+= "|.*read_trylock.*";
4474 blfn
+= "|.*write_lock.*";
4475 blfn
+= "|.*write_unlock.*";
4476 blfn
+= "|.*write_trylock.*";
4477 blfn
+= "|.*write_seqlock.*";
4478 blfn
+= "|.*write_sequnlock.*";
4479 blfn
+= "|.*spin_lock.*";
4480 blfn
+= "|.*spin_unlock.*";
4481 blfn
+= "|.*spin_trylock.*";
4482 blfn
+= "|.*spin_is_locked.*";
4483 blfn
+= "|rwsem_.*lock.*";
4484 blfn
+= "|.*mutex_.*lock.*";
4487 blfn
+= "|atomic_.*";
4488 blfn
+= "|atomic64_.*";
4490 // few other problematic cases
4495 blfn
+= "|.*apic.*|.*APIC.*";
4496 blfn
+= "|.*softirq.*";
4498 blfn
+= "|.*_intr.*";
4500 blfn
+= "|.*kernel_text.*";
4501 blfn
+= "|get_current";
4502 blfn
+= "|current_.*";
4503 blfn
+= "|.*exception_tables.*";
4504 blfn
+= "|.*setup_rt_frame.*";
4506 // PR 5759, CONFIG_PREEMPT kernels
4507 blfn
+= "|.*preempt_count.*";
4508 blfn
+= "|preempt_schedule";
4510 // These functions don't return, so return probes would never be recovered
4511 blfn_ret
+= "do_exit"; // no "|"
4512 blfn_ret
+= "|sys_exit";
4513 blfn_ret
+= "|sys_exit_group";
4515 // __switch_to changes "current" on x86_64 and i686, so return probes
4516 // would cause kernel panic, and it is marked as "__kprobes" on x86_64
4517 if (sess
.architecture
== "x86_64")
4518 blfn
+= "|__switch_to";
4519 if (sess
.architecture
== "i686")
4520 blfn_ret
+= "|__switch_to";
4522 // RHEL6 pre-beta 2.6.32-19.el6
4523 blfn
+= "|special_mapping_.*";
4524 blfn
+= "|.*_pte_.*"; // or "|smaps_pte_range";
4525 blfile
+= "|fs/seq_file\\.c";
4530 blsection
+= ")"; // NB: no $, sections match just the beginning
4532 if (sess
.verbose
> 2)
4534 clog
<< _("blacklist regexps:") << endl
;
4535 clog
<< "blfn: " << blfn
<< endl
;
4536 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4537 clog
<< "blfile: " << blfile
<< endl
;
4538 clog
<< "blsection: " << blsection
<< endl
;
4541 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4542 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4543 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4544 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4545 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4546 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4547 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4548 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4550 blacklist_enabled
= true;
4555 dwflpp::build_user_blacklist()
4557 // We build up the regexps in these strings
4559 // Add ^ anchors at the front; $ will be added just before regcomp.
4562 string blfn_ret
= "^(";
4563 string blfile
= "^(";
4564 string blsection
= "^(";
4566 // Non-matching placeholders until we have real things to match
4571 // These functions don't use the normal function-entry ABI, so can't be .return probed safely
4572 blfn_ret
+= "_start";
4577 blsection
+= ")"; // NB: no $, sections match just the beginning
4579 if (sess
.verbose
> 2)
4581 clog
<< _("blacklist regexps:") << endl
;
4582 clog
<< "blfn: " << blfn
<< endl
;
4583 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4584 clog
<< "blfile: " << blfile
<< endl
;
4585 clog
<< "blsection: " << blsection
<< endl
;
4588 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4589 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4590 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4591 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4592 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4593 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4594 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4595 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4597 blacklist_enabled
= true;
4602 dwflpp::get_blacklist_section(Dwarf_Addr addr
)
4604 string blacklist_section
;
4606 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
4607 // because dwfl_module_getelf can force costly section relocations
4608 // we don't really need, while either will do for this purpose.
4609 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (module
, &bias
))
4610 ?: dwfl_module_getelf (module
, &bias
));
4612 Dwarf_Addr offset
= addr
- bias
;
4617 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4618 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
4621 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
4623 continue; // XXX error?
4625 if (!(shdr
->sh_flags
& SHF_ALLOC
))
4628 GElf_Addr start
= shdr
->sh_addr
;
4629 GElf_Addr end
= start
+ shdr
->sh_size
;
4630 if (! (offset
>= start
&& offset
< end
))
4633 blacklist_section
= elf_strptr (elf
, shstrndx
, shdr
->sh_name
);
4637 return blacklist_section
;
4641 /* Find the section named 'section_name' in the current module
4642 * returning the section header using 'shdr_mem' */
4645 dwflpp::get_section(string section_name
, GElf_Shdr
*shdr_mem
, Elf
**elf_ret
)
4647 GElf_Shdr
*shdr
= NULL
;
4652 // Explicitly look in the main elf file first.
4653 elf
= dwfl_module_getelf (module
, &bias
);
4654 Elf_Scn
*probe_scn
= NULL
;
4656 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4658 bool have_section
= false;
4660 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4662 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4663 assert (shdr
!= NULL
);
4665 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4667 have_section
= true;
4672 // Older versions may put the section in the debuginfo dwarf file,
4673 // so check if it actually exists, if not take a look in the debuginfo file
4674 if (! have_section
|| (have_section
&& shdr
->sh_type
== SHT_NOBITS
))
4676 elf
= dwarf_getelf (dwfl_module_getdwarf (module
, &bias
));
4679 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4681 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4683 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4684 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4686 have_section
= true;
4702 dwflpp::relocate_address(Dwarf_Addr dw_addr
, interned_string
& reloc_section
)
4705 // libdw address, so adjust for bias gotten from dwfl_module_getdwarf
4706 Dwarf_Addr reloc_addr
= dw_addr
+ module_bias
;
4709 assert(module_name
== TOK_KERNEL
);
4712 else if (dwfl_module_relocations (module
) > 0)
4714 // This is a relocatable module; libdwfl already knows its
4715 // sections, so we can relativize addr.
4716 int idx
= dwfl_module_relocate_address (module
, &reloc_addr
);
4717 const char* r_s
= dwfl_module_relocation_info (module
, idx
, NULL
);
4719 reloc_section
= r_s
;
4721 if (reloc_section
== "" && dwfl_module_relocations (module
) == 1)
4722 reloc_section
= ".dynamic";
4725 reloc_section
= ".absolute";
4729 /* Returns the call frame address operations for the given program counter
4730 * in the libdw address space.
4733 dwflpp::get_cfa_ops (Dwarf_Addr pc
)
4735 Dwarf_Op
*cfa_ops
= NULL
;
4737 if (sess
.verbose
> 2)
4738 clog
<< "get_cfa_ops @0x" << hex
<< pc
<< dec
4739 << ", module_start @0x" << hex
<< module_start
<< dec
<< endl
;
4741 // Try debug_frame first, then fall back on eh_frame.
4742 size_t cfa_nops
= 0;
4743 Dwarf_Addr bias
= 0;
4744 Dwarf_Frame
*frame
= NULL
;
4745 Dwarf_CFI
*cfi
= dwfl_module_dwarf_cfi (module
, &bias
);
4748 if (sess
.verbose
> 3)
4749 clog
<< "got dwarf cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4750 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4751 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4752 else if (sess
.verbose
> 3)
4753 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4755 else if (sess
.verbose
> 3)
4756 clog
<< "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl
;
4758 if (cfa_ops
== NULL
)
4760 cfi
= dwfl_module_eh_cfi (module
, &bias
);
4763 if (sess
.verbose
> 3)
4764 clog
<< "got eh cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4765 Dwarf_Frame
*frame
= NULL
;
4766 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4767 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4768 else if (sess
.verbose
> 3)
4769 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4771 else if (sess
.verbose
> 3)
4772 clog
<< "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl
;
4776 if (sess
.verbose
> 2)
4778 if (cfa_ops
== NULL
)
4779 clog
<< _("not found cfa") << endl
;
4782 Dwarf_Addr frame_start
, frame_end
;
4784 int info
= dwarf_frame_info (frame
, &frame_start
, &frame_end
,
4786 clog
<< _F("found cfa, info: %d [start: %#" PRIx64
", end: %#" PRIx64
4787 ", nops: %zu", info
, frame_start
, frame_end
, cfa_nops
) << endl
;
4795 dwflpp::add_module_build_id_to_hash (Dwfl_Module
*m
,
4796 void **userdata
__attribute__ ((unused
)),
4801 string modname
= name
;
4802 systemtap_session
* s
= (systemtap_session
*)arg
;
4803 if (pending_interrupts
)
4804 return DWARF_CB_ABORT
;
4806 // Extract the build ID
4807 const unsigned char *bits
;
4809 int bits_length
= dwfl_module_build_id(m
, &bits
, &vaddr
);
4812 // Convert the binary bits to a hex string
4813 string hex
= hex_dump(bits
, bits_length
);
4815 // Store the build ID in the session
4816 s
->build_ids
.push_back(hex
);
4824 cu_entry_pc_caching_callback (Dwarf_Die
*func
, pair
<dwflpp
&, entry_pc_cache_t
&> *data
)
4826 auto& dw
= data
->first
;
4827 auto& cache
= data
->second
;
4829 Dwarf_Addr entry_pc
;
4830 if (dw
.die_entrypc (func
, &entry_pc
))
4831 cache
.insert (entry_pc
);
4836 dwflpp::check_cu_entry_pc (Dwarf_Die
*cu
, Dwarf_Addr pc
)
4838 auto& entry_pcs
= cu_entry_pc_cache
[cu
->addr
];
4841 save_and_restore
<Dwarf_Die
*> saved_cu(&this->cu
, cu
);
4842 entry_pcs
= new entry_pc_cache_t
;
4843 pair
<dwflpp
&, entry_pc_cache_t
&> data (*this, *entry_pcs
);
4844 int rc
= iterate_over_functions (cu_entry_pc_caching_callback
, &data
, "*");
4845 if (rc
!= DWARF_CB_OK
)
4849 return entry_pcs
->count(pc
) != 0;
4853 // Perform PR15123 heuristic for given variable at given address.
4854 // Return alternate pc address to do location-list lookup at, or 0 if
4858 dwflpp::pr15123_retry_addr (Dwarf_Addr pc
, Dwarf_Die
* die
)
4860 // For PR15123, we'd like to detect the situation where the
4861 // incoming PC may point to a couple-of-byte instruction
4862 // sequence that gcc emits for CFLAGS=-mfentry, and where
4863 // context variables are in fact available throughout, *but* due
4864 // to the bug, the dwarf debuginfo location-list only starts a
4865 // few instructions later. Prologue searching does not resolve
4866 // this as a line-record is in place at the -mfentry prologue.
4868 // Detecting this is complicated because ...
4869 // - we only want to do this if -mfentry was actually used
4870 // - if <pc> points to the a function entry point
4871 // - if the architecture is familiar enough that we can have a
4872 // hard-coded constant to skip over the prologue.
4874 // Otherwise, we could give a false-positive - return corrupted
4877 // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches
4878 // was used. Without it, set the PR15123_ASSUME_MFENTRY environment
4879 // variable to override the -mfentry test.
4881 if (getenv ("PR15123_DISABLE"))
4885 dwarf_diecu (die
, &cudie
, NULL
, NULL
);
4887 if (!getenv ("PR15123_ASSUME_MFENTRY")) {
4888 string producer
, version
;
4889 if (!is_gcc_producer(&cudie
, producer
, version
))
4891 if (producer
.find("-mfentry") == string::npos
)
4895 // Determine if this pc maps to the beginning of a
4896 // real function (not some inlined doppelganger. This
4897 // is made tricker by this->function may not be
4898 // pointing at the right DIE (say e.g. stap encountered
4899 // the inlined copy first, so was focus_on_function'd).
4900 if (!check_cu_entry_pc (&cudie
, pc
))
4901 return 0; // (will fail on retry, so we won't loop more than once)
4903 if (sess
.architecture
== "i386" ||
4904 sess
.architecture
== "x86_64") {
4905 /* pull the trigger */
4906 if (sess
.verbose
> 2)
4907 clog
<< _("retrying variable location-list lookup at address pc+5\n");
4915 dwflpp::has_gnu_debugdata ()
4917 Dwarf_Addr load_addr
;
4918 // Note we really want the actual elf file, not the dwarf .debug file.
4919 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
4921 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
4923 // Get the gnu_debugdata section header
4924 Elf_Scn
*scn
= NULL
;
4925 GElf_Shdr
*gnu_debugdata_shdr
= NULL
;
4926 GElf_Shdr gnu_debugdata_shdr_mem
;
4927 while ((scn
= elf_nextscn (elf
, scn
)))
4929 gnu_debugdata_shdr
= gelf_getshdr (scn
, &gnu_debugdata_shdr_mem
);
4930 assert (gnu_debugdata_shdr
!= NULL
);
4931 if (strcmp (elf_strptr (elf
, shstrndx
, gnu_debugdata_shdr
->sh_name
), ".gnu_debugdata") == 0)
4937 // If not GCC, return false. Otherwise, return true and set vers.
4939 dwflpp::is_gcc_producer(Dwarf_Die
*cudie
, string
& producer
, string
& version
)
4941 Dwarf_Attribute producer_attr
;
4942 if (!dwarf_attr_integrate(cudie
, DW_AT_producer
, &producer_attr
))
4945 // GNU {C|C++|...} x.x.x YYYYMMDD ...
4946 const char *cproducer
= dwarf_formstring(&producer_attr
);
4949 producer
= cproducer
;
4951 vector
<string
> tokens
;
4952 tokenize(producer
, tokens
);
4954 if (tokens
.size() < 3
4955 || tokens
[0] != "GNU"
4956 || tokens
[2].find_first_not_of(".0123456789") != string::npos
)
4959 version
= tokens
[2];
4964 die_has_loclist(Dwarf_Die
*begin_die
)
4967 Dwarf_Attribute loc
;
4969 if (dwarf_child(begin_die
, &die
) != 0)
4974 switch (dwarf_tag(&die
))
4976 case DW_TAG_formal_parameter
:
4977 case DW_TAG_variable
:
4978 if (dwarf_attr_integrate(&die
, DW_AT_location
, &loc
)
4979 && dwarf_whatform(&loc
) == DW_FORM_sec_offset
)
4983 if (dwarf_haschildren (&die
))
4984 if (die_has_loclist(&die
))
4989 while (dwarf_siblingof (&die
, &die
) == 0);
4995 dwflpp::has_valid_locs ()
4999 // The current CU has valid location info (implying we do not need to skip the
5001 // - it was compiled with -O2 -g (in which case, GCC outputs proper location
5002 // info for the prologue), and
5003 // - it was compiled by GCC >= 4.5 (previous versions could have had invalid
5004 // debug info in the prologue, see GDB's PR13777)
5005 // Note that clang behaves similarly to GCC here: non-optimized code does not
5006 // have location lists, while optimized code does. In the check below, even if
5007 // the producer is not GCC, we assume that it is valid to do the loclist check
5008 // afterwards (which it is for clang).
5011 if (is_gcc_producer(cu
, prod
, vers
)
5012 && strverscmp(vers
.c_str(), "4.5") < 0)
5015 // We determine if the current CU has been optimized with -O2 -g by looking
5016 // for any data objects whose DW_AT_location is a location list. This is also
5017 // how GDB determines whether to skip the prologue or not. See GDB's PR12573
5018 // and also RHBZ612253#c6.
5019 if (!die_has_loclist(cu
))
5022 if (sess
.verbose
> 2)
5023 clog
<< _F("CU '%s' in module '%s' has valid locs",
5024 cu_name().c_str(), module_name
.c_str()) << endl
;
5029 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */