1 // C++ interface to dwfl
2 // Copyright (C) 2005-2015 Red Hat Inc.
3 // Copyright (C) 2005-2007 Intel Corporation.
4 // Copyright (C) 2008 James.Bottomley@HansenPartnership.com
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"
23 #include "auto_free.h"
25 #include "rpm_finder.h"
26 #include "setupdwfl.h"
44 #include <elfutils/libdwfl.h>
45 #include <elfutils/libdw.h>
53 #include <sys/types.h>
56 #define __STDC_FORMAT_MACROS
60 // Older glibc elf.h don't know about this new constant.
61 #ifndef STB_GNU_UNIQUE
62 #define STB_GNU_UNIQUE 10
66 // debug flag to compare to the uncached version from libdw
67 // #define DEBUG_DWFLPP_GETSCOPES 1
71 using namespace __gnu_cxx
;
74 static string
TOK_KERNEL("kernel");
77 // RAII style tracker for obstack pool, used because of complex exception flows
78 #define obstack_chunk_alloc malloc
79 #define obstack_chunk_free free
80 struct obstack_tracker
83 obstack_tracker(struct obstack
*p
): p(p
) {
87 obstack_free (this->p
, 0);
92 dwflpp::dwflpp(systemtap_session
& session
, const string
& name
, bool kernel_p
):
93 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
94 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
95 module_dwarf(NULL
), function(NULL
), blacklist_func(), blacklist_func_ret(),
96 blacklist_file(), blacklist_enabled(false)
99 setup_kernel(name
, session
);
102 vector
<string
> modules
;
103 modules
.push_back(name
);
108 dwflpp::dwflpp(systemtap_session
& session
, const vector
<string
>& names
,
110 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
111 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
112 module_dwarf(NULL
), function(NULL
), blacklist_enabled(false)
122 delete_map(module_cu_cache
);
123 delete_map(cu_function_cache
);
124 delete_map(mod_function_cache
);
125 delete_map(cu_inl_function_cache
);
126 delete_map(global_alias_cache
);
127 delete_map(cu_die_parent_cache
);
129 cu_lines_cache_t::iterator i
;
130 for (i
= cu_lines_cache
.begin(); i
!= cu_lines_cache
.end(); ++i
)
131 delete_map(*i
->second
);
132 delete_map(cu_lines_cache
);
136 // NB: don't "delete mod_info;", as that may be shared
137 // between dwflpp instances, and are stored in
138 // session.module_cache[] anyway.
142 module_cache::~module_cache ()
149 dwflpp::get_module_dwarf(bool required
, bool report
)
151 module_dwarf
= dwfl_module_getdwarf(module
, &module_bias
);
152 mod_info
->dwarf_status
= (module_dwarf
? info_present
: info_absent
);
153 if (!module_dwarf
&& report
)
155 string msg
= _("cannot find ");
156 if (module_name
== "")
159 msg
+= string("module ") + module_name
;
162 int i
= dwfl_errno();
164 msg
+= string(": ") + dwfl_errmsg (i
);
166 msg
+= " [man warning::debuginfo]";
168 /* add module_name to list to find rpm */
169 find_debug_rpms(sess
, module_name
.c_str());
172 throw SEMANTIC_ERROR (msg
);
174 sess
.print_warning(msg
);
180 dwflpp::focus_on_module(Dwfl_Module
* m
, module_info
* mi
)
186 module_name
= dwfl_module_info(module
, NULL
, &module_start
, &module_end
,
187 NULL
, NULL
, NULL
, NULL
) ?: "module";
191 assert(mi
&& mi
->name
&& mi
->name
== TOK_KERNEL
);
192 module_name
= mi
->name
;
195 module_bias
= mi
->bias
;
198 // Reset existing pointers and names
204 function_name
.clear();
210 dwflpp::focus_on_cu(Dwarf_Die
* c
)
217 // Reset existing pointers and names
218 function_name
.clear();
224 dwflpp::cu_name(void)
226 return dwarf_diename(cu
) ?: "<unknown source>";
231 dwflpp::focus_on_function(Dwarf_Die
* f
)
238 function_name
= dwarf_diename(function
) ?: "function";
242 /* Return the Dwarf_Die for the given address in the current module.
243 * The address should be in the module address address space (this
244 * function will take care of any dw bias).
247 dwflpp::query_cu_containing_address(Dwarf_Addr a
)
254 Dwarf_Die
* cudie
= dwfl_module_addrdie(module
, a
, &bias
);
255 assert(bias
== module_bias
);
261 dwflpp::module_name_matches(const string
& pattern
)
263 bool t
= (fnmatch(pattern
.c_str(), module_name
.c_str(), 0) == 0);
264 if (t
&& sess
.verbose
>3)
265 clog
<< _F("pattern '%s' matches module '%s'\n",
266 pattern
.c_str(), module_name
.c_str());
267 if (!t
&& sess
.verbose
>4)
268 clog
<< _F("pattern '%s' does not match module '%s'\n",
269 pattern
.c_str(), module_name
.c_str());
276 dwflpp::name_has_wildcard (const string
& pattern
)
278 return (pattern
.find('*') != string::npos
||
279 pattern
.find('?') != string::npos
||
280 pattern
.find('[') != string::npos
);
285 dwflpp::module_name_final_match(const string
& pattern
)
287 // Assume module_name_matches(). Can there be any more matches?
288 // Not unless the pattern is a wildcard, since module names are
290 return !name_has_wildcard(pattern
);
295 dwflpp::function_name_matches_pattern(const string
& name
, const string
& pattern
)
297 bool t
= (fnmatch(pattern
.c_str(), name
.c_str(), 0) == 0);
298 if (t
&& sess
.verbose
>3)
299 clog
<< _F("pattern '%s' matches function '%s'\n", pattern
.c_str(), name
.c_str());
305 dwflpp::function_name_matches(const string
& pattern
)
308 return function_name_matches_pattern(function_name
, pattern
);
313 dwflpp::function_scope_matches(const vector
<string
>& scopes
)
315 // walk up the containing scopes
316 Dwarf_Die
* die
= function
;
317 for (int i
= scopes
.size() - 1; i
>= 0; --i
)
319 die
= get_parent_scope(die
);
321 // check if this scope matches, and prepend it if so
322 // NB: a NULL die is the global scope, compared as ""
323 string name
= dwarf_diename(die
) ?: "";
324 if (name_has_wildcard(scopes
[i
]) ?
325 function_name_matches_pattern(name
, scopes
[i
]) :
327 function_name
= name
+ "::" + function_name
;
331 // make sure there's no more if we're at the global scope
340 dwflpp::setup_kernel(const string
& name
, systemtap_session
& s
, bool debuginfo_needed
)
342 if (! sess
.module_cache
)
343 sess
.module_cache
= new module_cache ();
345 unsigned offline_search_matches
= 0;
346 dwfl
= setup_dwfl_kernel(name
, &offline_search_matches
, sess
);
348 if (offline_search_matches
< 1)
350 if (debuginfo_needed
) {
351 // Suggest a likely kernel dir to find debuginfo rpm for
352 string dir
= string(sess
.sysroot
+ "/lib/modules/" + sess
.kernel_release
);
353 find_debug_rpms(sess
, dir
.c_str());
355 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
356 sess
.architecture
.c_str(), sess
.kernel_build_tree
.c_str()));
364 assert_no_interrupts();
365 off
= dwfl_getmodules (dwfl
, &add_module_build_id_to_hash
, &s
, off
);
368 DWFL_ASSERT("dwfl_getmodules", off
== 0);
371 build_kernel_blacklist();
375 dwflpp::setup_kernel(const vector
<string
> &names
, bool debuginfo_needed
)
377 if (! sess
.module_cache
)
378 sess
.module_cache
= new module_cache ();
380 unsigned offline_search_matches
= 0;
381 set
<string
> offline_search_names(names
.begin(), names
.end());
382 dwfl
= setup_dwfl_kernel(offline_search_names
,
383 &offline_search_matches
,
386 if (offline_search_matches
< offline_search_names
.size())
388 if (debuginfo_needed
) {
389 // Suggest a likely kernel dir to find debuginfo rpm for
390 string dir
= string(sess
.sysroot
+ "/lib/modules/" + sess
.kernel_release
);
391 find_debug_rpms(sess
, dir
.c_str());
393 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
394 sess
.architecture
.c_str(), sess
.kernel_build_tree
.c_str()));
397 build_kernel_blacklist();
402 dwflpp::setup_user(const vector
<string
>& modules
, bool debuginfo_needed
)
404 if (! sess
.module_cache
)
405 sess
.module_cache
= new module_cache ();
407 vector
<string
>::const_iterator it
= modules
.begin();
408 dwfl
= setup_dwfl_user(it
, modules
.end(), debuginfo_needed
, sess
);
409 if (debuginfo_needed
&& it
!= modules
.end())
410 DWFL_ASSERT (string(_F("missing process %s %s debuginfo",
411 (*it
).c_str(), sess
.architecture
.c_str())),
414 build_user_blacklist();
418 dwflpp::iterate_over_modules
<void>(int (*callback
)(Dwfl_Module
*,
425 dwfl_getmodules (dwfl
, callback
, data
, 0);
427 // Don't complain if we exited dwfl_getmodules early.
428 // This could be a $target variable error that will be
429 // reported soon anyway.
430 // DWFL_ASSERT("dwfl_getmodules", off == 0);
432 // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the
433 // "kernel" module is not reported in the loop above. However, we
434 // may be able to make do with symbol table data.
439 dwflpp::iterate_over_cus
<void>(int (*callback
)(Dwarf_Die
*, void*),
443 get_module_dwarf(false);
444 Dwarf
*dw
= module_dwarf
;
447 vector
<Dwarf_Die
>* v
= module_cu_cache
[dw
];
450 v
= new vector
<Dwarf_Die
>;
451 module_cu_cache
[dw
] = v
;
456 while (dwarf_nextcu (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) == 0)
458 assert_no_interrupts();
461 die
= dwarf_offdie (dw
, off
+ cuhl
, &die_mem
);
462 /* Skip partial units. */
463 if (dwarf_tag (die
) == DW_TAG_compile_unit
)
464 v
->push_back (*die
); /* copy */
469 if (want_types
&& module_tus_read
.find(dw
) == module_tus_read
.end())
471 // Process type units.
475 uint64_t type_signature
;
476 while (dwarf_next_unit (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
, NULL
,
477 &type_signature
, NULL
) == 0)
479 assert_no_interrupts();
482 die
= dwarf_offdie_types (dw
, off
+ cuhl
, &die_mem
);
483 /* Skip partial units. */
484 if (dwarf_tag (die
) == DW_TAG_type_unit
)
485 v
->push_back (*die
); /* copy */
488 module_tus_read
.insert(dw
);
491 for (vector
<Dwarf_Die
>::iterator i
= v
->begin(); i
!= v
->end(); ++i
)
493 int rc
= (*callback
)(&*i
, data
);
494 assert_no_interrupts();
495 if (rc
!= DWARF_CB_OK
)
502 dwflpp::func_is_inline()
505 return dwarf_func_inline (function
) != 0;
510 dwflpp::func_is_exported()
512 const char *name
= dwarf_linkage_name (function
) ?: dwarf_diename (function
);
516 int syms
= dwfl_module_getsymtab (module
);
517 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
519 for (int i
= 0; i
< syms
; i
++)
523 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
525 && strcmp (name
, symname
) == 0)
527 if (GELF_ST_TYPE(sym
.st_info
) == STT_FUNC
528 && (GELF_ST_BIND(sym
.st_info
) == STB_GLOBAL
529 || GELF_ST_BIND(sym
.st_info
) == STB_WEAK
530 || GELF_ST_BIND(sym
.st_info
) == STB_GNU_UNIQUE
))
540 dwflpp::cache_inline_instances (Dwarf_Die
* die
)
542 // If this is an inline instance, link it back to its origin
544 if (dwarf_tag(die
) == DW_TAG_inlined_subroutine
&&
545 dwarf_attr_die(die
, DW_AT_abstract_origin
, &origin
))
547 vector
<Dwarf_Die
>*& v
= cu_inl_function_cache
[origin
.addr
];
549 v
= new vector
<Dwarf_Die
>;
553 // Recurse through other scopes that may contain inlines
554 Dwarf_Die child
, import
;
555 if (dwarf_child(die
, &child
) == 0)
558 switch (dwarf_tag (&child
))
560 // tags that could contain inlines
561 case DW_TAG_compile_unit
:
563 case DW_TAG_lexical_block
:
564 case DW_TAG_with_stmt
:
565 case DW_TAG_catch_block
:
566 case DW_TAG_try_block
:
567 case DW_TAG_entry_point
:
568 case DW_TAG_inlined_subroutine
:
569 case DW_TAG_subprogram
:
570 cache_inline_instances(&child
);
573 // imported dies should be followed
574 case DW_TAG_imported_unit
:
575 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
576 cache_inline_instances(&import
);
579 // nothing to do for other tags
584 while (dwarf_siblingof(&child
, &child
) == 0);
589 dwflpp::iterate_over_inline_instances
<void>(int (*callback
)(Dwarf_Die
*, void*),
593 assert (func_is_inline ());
595 if (cu_inl_function_cache_done
.insert(cu
->addr
).second
)
596 cache_inline_instances(cu
);
598 vector
<Dwarf_Die
>* v
= cu_inl_function_cache
[function
->addr
];
602 for (vector
<Dwarf_Die
>::iterator i
= v
->begin(); i
!= v
->end(); ++i
)
604 int rc
= (*callback
)(&*i
, data
);
605 assert_no_interrupts();
606 if (rc
!= DWARF_CB_OK
)
613 dwflpp::cache_die_parents(cu_die_parent_cache_t
* parents
, Dwarf_Die
* die
)
615 // Record and recurse through DIEs we care about
616 Dwarf_Die child
, import
;
617 if (dwarf_child(die
, &child
) == 0)
620 switch (dwarf_tag (&child
))
622 // normal tags to recurse
623 case DW_TAG_compile_unit
:
625 case DW_TAG_lexical_block
:
626 case DW_TAG_with_stmt
:
627 case DW_TAG_catch_block
:
628 case DW_TAG_try_block
:
629 case DW_TAG_entry_point
:
630 case DW_TAG_inlined_subroutine
:
631 case DW_TAG_subprogram
:
632 case DW_TAG_namespace
:
633 case DW_TAG_class_type
:
634 case DW_TAG_structure_type
:
635 parents
->insert(make_pair(child
.addr
, *die
));
636 cache_die_parents(parents
, &child
);
639 // record only, nothing to recurse
641 parents
->insert(make_pair(child
.addr
, *die
));
644 // imported dies should be followed
645 case DW_TAG_imported_unit
:
646 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
648 parents
->insert(make_pair(import
.addr
, *die
));
649 cache_die_parents(parents
, &import
);
653 // nothing to do for other tags
658 while (dwarf_siblingof(&child
, &child
) == 0);
662 cu_die_parent_cache_t
*
663 dwflpp::get_die_parents()
667 cu_die_parent_cache_t
*& parents
= cu_die_parent_cache
[cu
->addr
];
670 parents
= new cu_die_parent_cache_t
;
671 cache_die_parents(parents
, cu
);
672 if (sess
.verbose
> 4)
673 clog
<< _F("die parent cache %s:%s size %zu", module_name
.c_str(),
674 cu_name().c_str(), parents
->size()) << endl
;
681 dwflpp::getscopes_die(Dwarf_Die
* die
)
683 cu_die_parent_cache_t
*parents
= get_die_parents();
685 vector
<Dwarf_Die
> scopes
;
686 Dwarf_Die
*scope
= die
;
687 cu_die_parent_cache_t::iterator it
;
690 scopes
.push_back(*scope
);
691 it
= parents
->find(scope
->addr
);
694 while (it
!= parents
->end());
696 #ifdef DEBUG_DWFLPP_GETSCOPES
697 Dwarf_Die
*dscopes
= NULL
;
698 int nscopes
= dwarf_getscopes_die(die
, &dscopes
);
700 assert(nscopes
== (int)scopes
.size());
701 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
702 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
710 std::vector
<Dwarf_Die
>
711 dwflpp::getscopes(Dwarf_Die
* die
)
713 cu_die_parent_cache_t
*parents
= get_die_parents();
715 vector
<Dwarf_Die
> scopes
;
718 Dwarf_Die
*scope
= die
;
719 cu_die_parent_cache_t::iterator it
;
722 scopes
.push_back(*scope
);
723 if (dwarf_tag(scope
) == DW_TAG_inlined_subroutine
&&
724 dwarf_attr_die(scope
, DW_AT_abstract_origin
, &origin
))
727 it
= parents
->find(scope
->addr
);
730 while (it
!= parents
->end());
732 #ifdef DEBUG_DWFLPP_GETSCOPES
733 // there isn't an exact libdw equivalent, but if dwarf_getscopes on the
734 // entrypc returns the same first die, then all the scopes should match
736 if (die_entrypc(die
, &pc
))
738 Dwarf_Die
*dscopes
= NULL
;
739 int nscopes
= dwarf_getscopes(cu
, pc
, &dscopes
);
740 if (nscopes
> 0 && dscopes
[0].addr
== die
->addr
)
742 assert(nscopes
== (int)scopes
.size());
743 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
744 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
754 std::vector
<Dwarf_Die
>
755 dwflpp::getscopes(Dwarf_Addr pc
)
757 // The die_parent_cache doesn't help us without knowing where the pc is
758 // contained, so we have to do this one the old fashioned way.
762 vector
<Dwarf_Die
> scopes
;
764 Dwarf_Die
* dwarf_scopes
;
765 int nscopes
= dwarf_getscopes(cu
, pc
, &dwarf_scopes
);
768 scopes
.assign(dwarf_scopes
, dwarf_scopes
+ nscopes
);
772 #ifdef DEBUG_DWFLPP_GETSCOPES
773 // check that getscopes on the starting die gets the same result
776 vector
<Dwarf_Die
> other
= getscopes(&scopes
[0]);
777 assert(scopes
.size() == other
.size());
778 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
779 assert(scopes
[i
].addr
== other
[i
].addr
);
788 dwflpp::get_parent_scope(Dwarf_Die
* die
)
790 Dwarf_Die specification
;
791 if (dwarf_attr_die(die
, DW_AT_specification
, &specification
))
792 die
= &specification
;
794 cu_die_parent_cache_t
*parents
= get_die_parents();
795 cu_die_parent_cache_t::iterator it
= parents
->find(die
->addr
);
796 while (it
!= parents
->end())
798 Dwarf_Die
* scope
= &it
->second
;
799 switch (dwarf_tag (scope
))
801 case DW_TAG_namespace
:
802 case DW_TAG_class_type
:
803 case DW_TAG_structure_type
:
809 it
= parents
->find(scope
->addr
);
815 cache_type_prefix(Dwarf_Die
* type
)
817 switch (dwarf_tag(type
))
819 case DW_TAG_enumeration_type
:
821 case DW_TAG_structure_type
:
822 case DW_TAG_class_type
:
823 // treating struct/class as equals
825 case DW_TAG_union_type
:
831 /* GCC might generate a struct/class without DW_AT_declaration,
832 but that only contains members which have DW_AT_declaration
833 set. We aren't interested in those. PR14434 (GCC bug #54181). */
835 has_only_decl_members (Dwarf_Die
*die
)
837 Dwarf_Die child
, import
;
838 if (dwarf_child(die
, &child
) != 0)
839 return false; /* no members */
843 if (! dwarf_hasattr(&child
, DW_AT_declaration
))
844 return false; /* real member found. */
845 int tag
= dwarf_tag(&child
);
846 if ((tag
== DW_TAG_namespace
847 || tag
== DW_TAG_structure_type
848 || tag
== DW_TAG_class_type
)
849 && ! has_only_decl_members (&child
))
850 return false; /* real grand child member found. */
852 // Unlikely to ever happen, but if there is an imported unit
853 // then check its children as if they are children of this DIE.
854 if (tag
== DW_TAG_imported_unit
855 && dwarf_attr_die(&child
, DW_AT_import
, &import
)
856 && ! has_only_decl_members (&import
))
859 while (dwarf_siblingof(&child
, &child
) == 0);
861 return true; /* Tried all children and grandchildren. */
865 dwflpp::global_alias_caching_callback(Dwarf_Die
*die
, bool has_inner_types
,
866 const string
& prefix
, cu_type_cache_t
*cache
)
868 const char *name
= dwarf_diename(die
);
870 if (!name
|| dwarf_hasattr(die
, DW_AT_declaration
)
871 || has_only_decl_members(die
))
874 int tag
= dwarf_tag(die
);
875 if (has_inner_types
&& (tag
== DW_TAG_namespace
876 || tag
== DW_TAG_structure_type
877 || tag
== DW_TAG_class_type
))
878 iterate_over_types(die
, has_inner_types
, prefix
+ name
+ "::",
879 global_alias_caching_callback
, cache
);
881 if (tag
!= DW_TAG_namespace
)
883 string type_name
= prefix
+ cache_type_prefix(die
) + name
;
884 if (cache
->find(type_name
) == cache
->end())
885 (*cache
)[type_name
] = *die
;
892 dwflpp::global_alias_caching_callback_cus(Dwarf_Die
*die
, dwflpp
*dw
)
894 mod_cu_type_cache_t
*global_alias_cache
;
895 global_alias_cache
= &dw
->global_alias_cache
;
897 cu_type_cache_t
*v
= (*global_alias_cache
)[die
->addr
];
901 v
= new cu_type_cache_t
;
902 (*global_alias_cache
)[die
->addr
] = v
;
903 iterate_over_globals(die
, global_alias_caching_callback
, v
);
909 dwflpp::declaration_resolve_other_cus(const string
& name
)
911 iterate_over_cus(global_alias_caching_callback_cus
, this, true);
912 for (mod_cu_type_cache_t::iterator i
= global_alias_cache
.begin();
913 i
!= global_alias_cache
.end(); ++i
)
915 cu_type_cache_t
*v
= (*i
).second
;
916 if (v
->find(name
) != v
->end())
917 return & ((*v
)[name
]);
924 dwflpp::declaration_resolve(const string
& name
)
926 cu_type_cache_t
*v
= global_alias_cache
[cu
->addr
];
927 if (v
== 0) // need to build the cache, just once per encountered module/cu
929 v
= new cu_type_cache_t
;
930 global_alias_cache
[cu
->addr
] = v
;
931 iterate_over_globals(cu
, global_alias_caching_callback
, v
);
932 if (sess
.verbose
> 4)
933 clog
<< _F("global alias cache %s:%s size %zu", module_name
.c_str(),
934 cu_name().c_str(), v
->size()) << endl
;
937 // XXX: it may be desirable to search other modules' declarations
938 // too, in case a module/shared-library processes a
939 // forward-declared pointer type only, where the actual definition
940 // may only be in vmlinux or the application.
942 if (v
->find(name
) == v
->end())
943 return declaration_resolve_other_cus(name
);
945 return & ((*v
)[name
]);
949 dwflpp::declaration_resolve(Dwarf_Die
*type
)
951 const char* name
= dwarf_diename(type
);
955 string type_name
= cache_type_prefix(type
) + string(name
);
956 return declaration_resolve(type_name
);
961 dwflpp::cu_function_caching_callback (Dwarf_Die
* func
, cu_function_cache_t
*v
)
963 const char *name
= dwarf_diename(func
);
967 v
->insert(make_pair(name
, *func
));
973 dwflpp::mod_function_caching_callback (Dwarf_Die
* cu
, cu_function_cache_t
*v
)
975 // need to cast callback to func which accepts void*
976 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
983 dwflpp::iterate_over_functions
<void>(int (*callback
)(Dwarf_Die
*, void*),
984 void *data
, const string
& function
)
986 int rc
= DWARF_CB_OK
;
990 cu_function_cache_t
*v
= cu_function_cache
[cu
->addr
];
993 v
= new cu_function_cache_t
;
994 cu_function_cache
[cu
->addr
] = v
;
995 // need to cast callback to func which accepts void*
996 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
998 if (sess
.verbose
> 4)
999 clog
<< _F("function cache %s:%s size %zu", module_name
.c_str(),
1000 cu_name().c_str(), v
->size()) << endl
;
1001 mod_info
->update_symtab(v
);
1004 cu_function_cache_t::iterator it
;
1005 cu_function_cache_range_t range
= v
->equal_range(function
);
1006 if (range
.first
!= range
.second
)
1008 for (it
= range
.first
; it
!= range
.second
; ++it
)
1010 Dwarf_Die
& die
= it
->second
;
1011 if (sess
.verbose
> 4)
1012 clog
<< _F("function cache %s:%s hit %s", module_name
.c_str(),
1013 cu_name().c_str(), function
.c_str()) << endl
;
1014 rc
= (*callback
)(& die
, data
);
1015 if (rc
!= DWARF_CB_OK
) break;
1018 else if (startswith(function
, "_Z"))
1020 // C++ names are mangled starting with a "_Z" prefix. Most of the time
1021 // we can discover the mangled name from a die's MIPS_linkage_name
1022 // attribute, so we read that to match against the user's function
1023 // pattern. Note that this isn't perfect, as not all will have that
1024 // attribute (notably ctors and dtors), but we do what we can...
1025 for (it
= v
->begin(); it
!= v
->end(); ++it
)
1027 if (pending_interrupts
) return DWARF_CB_ABORT
;
1028 Dwarf_Die
& die
= it
->second
;
1029 const char* linkage_name
= NULL
;
1030 if ((linkage_name
= dwarf_linkage_name (&die
))
1031 && function_name_matches_pattern (linkage_name
, function
))
1033 if (sess
.verbose
> 4)
1034 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1035 cu_name().c_str(), linkage_name
, function
.c_str()) << endl
;
1037 rc
= (*callback
)(& die
, data
);
1038 if (rc
!= DWARF_CB_OK
) break;
1042 else if (name_has_wildcard (function
))
1044 for (it
= v
->begin(); it
!= v
->end(); ++it
)
1046 if (pending_interrupts
) return DWARF_CB_ABORT
;
1047 const string
& func_name
= it
->first
;
1048 Dwarf_Die
& die
= it
->second
;
1049 if (function_name_matches_pattern (func_name
, function
))
1051 if (sess
.verbose
> 4)
1052 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1053 cu_name().c_str(), func_name
.c_str(), function
.c_str()) << endl
;
1055 rc
= (*callback
)(& die
, data
);
1056 if (rc
!= DWARF_CB_OK
) break;
1060 else // not a linkage name or wildcard and no match in this CU
1069 dwflpp::iterate_single_function
<void>(int (*callback
)(Dwarf_Die
*, void*),
1070 void *data
, const string
& function
)
1072 int rc
= DWARF_CB_OK
;
1075 get_module_dwarf(false);
1079 cu_function_cache_t
*v
= mod_function_cache
[module_dwarf
];
1082 v
= new cu_function_cache_t
;
1083 mod_function_cache
[module_dwarf
] = v
;
1084 iterate_over_cus (mod_function_caching_callback
, v
, false);
1085 if (sess
.verbose
> 4)
1086 clog
<< _F("module function cache %s size %zu", module_name
.c_str(),
1088 mod_info
->update_symtab(v
);
1091 cu_function_cache_t::iterator it
;
1092 cu_function_cache_range_t range
= v
->equal_range(function
);
1093 if (range
.first
!= range
.second
)
1095 for (it
= range
.first
; it
!= range
.second
; ++it
)
1098 Dwarf_Die
& die
= it
->second
;
1099 if (sess
.verbose
> 4)
1100 clog
<< _F("module function cache %s hit %s", module_name
.c_str(),
1101 function
.c_str()) << endl
;
1103 // since we're iterating out of cu-context, we need each focus
1104 focus_on_cu(dwarf_diecu(&die
, &cu_mem
, NULL
, NULL
));
1106 rc
= (*callback
)(& die
, data
);
1107 if (rc
!= DWARF_CB_OK
) break;
1111 // undo the focus_on_cu
1113 this->function_name
.clear();
1114 this->function
= NULL
;
1120 /* This basically only goes one level down from the compile unit so it
1121 * only picks up top level stuff (i.e. nothing in a lower scope) */
1123 dwflpp::iterate_over_globals
<void>(Dwarf_Die
*cu_die
,
1124 int (*callback
)(Dwarf_Die
*,
1131 assert (dwarf_tag(cu_die
) == DW_TAG_compile_unit
1132 || dwarf_tag(cu_die
) == DW_TAG_type_unit
1133 || dwarf_tag(cu_die
) == DW_TAG_partial_unit
);
1135 // Ignore partial_unit, if they get imported by a real unit, then
1136 // iterate_over_types will traverse them.
1137 if (dwarf_tag(cu_die
) == DW_TAG_partial_unit
)
1140 // If this is C++, recurse for any inner types
1141 bool has_inner_types
= dwarf_srclang(cu_die
) == DW_LANG_C_plus_plus
;
1143 return iterate_over_types(cu_die
, has_inner_types
, "", callback
, data
);
1147 dwflpp::iterate_over_types
<void>(Dwarf_Die
*top_die
,
1148 bool has_inner_types
,
1149 const string
& prefix
,
1150 int (* callback
)(Dwarf_Die
*,
1156 int rc
= DWARF_CB_OK
;
1157 Dwarf_Die die
, import
;
1161 if (dwarf_child(top_die
, &die
) != 0)
1165 /* We're only currently looking for named types,
1166 * although other types of declarations exist */
1167 switch (dwarf_tag(&die
))
1169 case DW_TAG_base_type
:
1170 case DW_TAG_enumeration_type
:
1171 case DW_TAG_structure_type
:
1172 case DW_TAG_class_type
:
1173 case DW_TAG_typedef
:
1174 case DW_TAG_union_type
:
1175 case DW_TAG_namespace
:
1176 rc
= (*callback
)(&die
, has_inner_types
, prefix
, data
);
1179 case DW_TAG_imported_unit
:
1180 // Follow the imported_unit and iterate over its contents
1181 // (either a partial_unit or a full compile_unit), all its
1182 // children should be treated as if they appear in this place.
1183 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
1184 rc
= iterate_over_types(&import
, has_inner_types
, prefix
,
1188 while (rc
== DWARF_CB_OK
&& dwarf_siblingof(&die
, &die
) == 0);
1194 /* For each notes section in the current module call 'callback', use
1195 * 'data' for the notes buffer and pass 'object' back in case
1196 * 'callback' is a method */
1199 dwflpp::iterate_over_notes
<void>(void *object
, void (*callback
)(void*,
1207 // Note we really want the actual elf file, not the dwarf .debug file.
1208 // Older binutils had a bug where they mangled the SHT_NOTE type during
1210 Elf
* elf
= dwfl_module_getelf (module
, &bias
);
1212 if (elf_getshdrstrndx (elf
, &shstrndx
))
1215 Elf_Scn
*scn
= NULL
;
1217 vector
<Dwarf_Die
> notes
;
1219 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
1222 if (gelf_getshdr (scn
, &shdr
) == NULL
)
1224 switch (shdr
.sh_type
)
1227 if (!(shdr
.sh_flags
& SHF_ALLOC
))
1229 string scn_name
= elf_strptr(elf
, shstrndx
, shdr
.sh_name
);
1230 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1235 for (size_t offset
= 0;
1236 (next
= gelf_getnote (data
, offset
, &nhdr
, &name_off
, &desc_off
)) > 0;
1239 const char *note_name_addr
= (const char *)data
->d_buf
+ name_off
;
1240 const char *note_desc_addr
= (const char *)data
->d_buf
+ desc_off
;
1241 string note_name
= nhdr
.n_namesz
> 1 // n_namesz includes NULL
1242 ? string(note_name_addr
, nhdr
.n_namesz
-1) : "";
1243 (*callback
) (object
, scn_name
, note_name
, nhdr
.n_type
,
1244 note_desc_addr
, nhdr
.n_descsz
);
1254 /* For each entry in the .dynamic section in the current module call 'callback'
1255 * returning 'object' in case 'callback' is a method */
1258 dwflpp::iterate_over_libraries
<void>(void (*callback
)(void*, const char*),
1261 std::set
<std::string
> added
;
1264 assert (this->module_name
.length() != 0);
1267 // We cannot use this: dwarf_getelf (dwfl_module_getdwarf (module, &bias))
1268 Elf
*elf
= dwfl_module_getelf (module
, &bias
);
1269 // elf_getphdrnum (elf, &phnum) is not available in all versions of elfutils
1270 // needs libelf from elfutils 0.144+
1271 for (int i
= 0; ; i
++)
1275 phdr
= gelf_getphdr (elf
, i
, &mem
);
1278 if (phdr
->p_type
== PT_INTERP
)
1281 char *filedata
= elf_rawfile (elf
, &maxsize
);
1283 if (filedata
!= NULL
&& phdr
->p_offset
< maxsize
)
1284 interpreter
= (char*) (filedata
+ phdr
->p_offset
);
1289 if (interpreter
.length() == 0)
1291 // If it gets cumbersome to maintain this whitelist, we could just check for
1292 // startswith("/lib/ld") || startswith("/lib64/ld"), and trust that no admin
1293 // would install untrustworthy loaders in those paths.
1294 // See also http://sourceware.org/git/?p=glibc.git;a=blob;f=shlib-versions;hb=HEAD
1295 if (interpreter
!= "/lib/ld.so.1" // s390, ppc
1296 && interpreter
!= "/lib/ld64.so.1" // s390x, ppc64
1297 && interpreter
!= "/lib64/ld64.so.1"
1298 && interpreter
!= "/lib/ld-linux-ia64.so.2" // ia64
1299 && interpreter
!= "/emul/ia32-linux/lib/ld-linux.so.2"
1300 && interpreter
!= "/lib64/ld-linux-x86-64.so.2" // x8664
1301 && interpreter
!= "/lib/ld-linux.so.2" // x86
1302 && interpreter
!= "/lib/ld-linux.so.3" // arm
1303 && interpreter
!= "/lib/ld-linux-armhf.so.3" // arm
1304 && interpreter
!= "/lib/ld-linux-aarch64.so.1" // arm64
1305 && interpreter
!= "/lib64/ld64.so.2" // ppc64le
1308 sess
.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s",
1309 module_name
.c_str(), interpreter
.c_str()));
1313 vector
<string
> ldd_command
;
1314 ldd_command
.push_back("/usr/bin/env");
1315 ldd_command
.push_back("LD_TRACE_LOADED_OBJECTS=1");
1316 ldd_command
.push_back("LD_WARN=yes");
1317 ldd_command
.push_back("LD_BIND_NOW=yes");
1318 ldd_command
.push_back(interpreter
);
1319 ldd_command
.push_back(module_name
);
1323 pid_t child
= stap_spawn_piped(sess
.verbose
, ldd_command
, NULL
, &child_fd
);
1324 if (child
<= 0 || !(fp
= fdopen(child_fd
, "r")))
1325 clog
<< _F("library iteration on %s failed: %s",
1326 module_name
.c_str(), strerror(errno
)) << endl
;
1329 while (1) // this parsing loop borrowed from add_unwindsym_ldd
1334 unsigned long int addr
= 0;
1336 char *line
= fgets (linebuf
, 256, fp
);
1337 if (line
== 0) break; // EOF or error
1339 #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)
1340 #define MS_FMT "%ms"
1342 #define MS_FMT "%as"
1344 // Try soname => shlib (0xaddr)
1345 int nf
= sscanf (line
, MS_FMT
" => " MS_FMT
" (0x%lx)",
1346 &soname
, &shlib
, &addr
);
1347 if (nf
!= 3 || shlib
[0] != '/')
1349 // Try shlib (0xaddr)
1350 nf
= sscanf (line
, " " MS_FMT
" (0x%lx)", &shlib
, &addr
);
1351 if (nf
!= 2 || shlib
[0] != '/')
1352 continue; // fewer than expected fields, or bad shlib.
1355 if (added
.find (shlib
) == added
.end())
1357 if (sess
.verbose
> 2)
1359 clog
<< _F("Added -d '%s", shlib
);
1361 clog
<< _F("' due to '%s'", soname
);
1366 added
.insert (shlib
);
1372 if ((fclose(fp
) || stap_waitpid(sess
.verbose
, child
)))
1373 sess
.print_warning("failed to read libraries from " + module_name
+ ": " + strerror(errno
));
1376 for (std::set
<std::string
>::iterator it
= added
.begin();
1380 string modname
= *it
;
1381 (callback
) (data
, modname
.c_str());
1386 /* For each plt section in the current module call 'callback', pass the plt entry
1387 * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */
1390 dwflpp::iterate_over_plt
<void>(void *object
, void (*callback
)(void*,
1394 Dwarf_Addr load_addr
;
1395 // Note we really want the actual elf file, not the dwarf .debug file.
1396 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
1398 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
1400 // Get the load address
1401 for (int i
= 0; ; i
++)
1405 phdr
= gelf_getphdr (elf
, i
, &mem
);
1408 if (phdr
->p_type
== PT_LOAD
)
1410 load_addr
= phdr
->p_vaddr
;
1415 // Get the plt section header
1416 Elf_Scn
*scn
= NULL
;
1417 GElf_Shdr
*plt_shdr
= NULL
;
1418 GElf_Shdr plt_shdr_mem
;
1419 while ((scn
= elf_nextscn (elf
, scn
)))
1421 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &plt_shdr_mem
);
1422 assert (shdr
!= NULL
);
1423 if (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".plt") == 0)
1429 if (plt_shdr
== NULL
)
1432 // Layout of the plt section
1433 int plt0_entry_size
;
1436 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1437 switch (em
->e_machine
)
1439 case EM_386
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1440 case EM_X86_64
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1441 case EM_ARM
: plt0_entry_size
= 20; plt_entry_size
= 12; break;
1442 case EM_AARCH64
:plt0_entry_size
= 32; plt_entry_size
= 16; break;
1447 throw SEMANTIC_ERROR(".plt is not supported on this architecture");
1451 while ((scn
= elf_nextscn (elf
, scn
)))
1454 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
1455 bool have_rela
= false;
1456 bool have_rel
= false;
1460 assert (shdr
!= NULL
);
1462 if ((have_rela
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rela.plt") == 0))
1463 || (have_rel
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rel.plt") == 0)))
1465 /* Get the data of the section. */
1466 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1467 assert (data
!= NULL
);
1468 /* Get the symbol table information. */
1469 Elf_Scn
*symscn
= elf_getscn (elf
, shdr
->sh_link
);
1470 GElf_Shdr symshdr_mem
;
1471 GElf_Shdr
*symshdr
= gelf_getshdr (symscn
, &symshdr_mem
);
1472 assert (symshdr
!= NULL
);
1473 Elf_Data
*symdata
= elf_getdata (symscn
, NULL
);
1474 assert (symdata
!= NULL
);
1476 unsigned int nsyms
= shdr
->sh_size
/ shdr
->sh_entsize
;
1478 for (unsigned int cnt
= 0; cnt
< nsyms
; ++cnt
)
1481 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1482 if (em
== 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
1485 GElf_Rela
*rela
= NULL
;
1487 GElf_Rel
*rel
= NULL
;
1490 rela
= gelf_getrela (data
, cnt
, &relamem
);
1491 assert (rela
!= NULL
);
1495 rel
= gelf_getrel (data
, cnt
, &relmem
);
1496 assert (rel
!= NULL
);
1500 Elf_Data
*xndxdata
= NULL
;
1502 gelf_getsymshndx (symdata
, xndxdata
,
1503 GELF_R_SYM (have_rela
? rela
->r_info
: rel
->r_info
),
1505 assert (sym
!= NULL
);
1506 Dwarf_Addr addr
= plt_shdr
->sh_offset
+ plt0_entry_size
+ cnt
* plt_entry_size
;
1508 if (elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
))
1509 (*callback
) (object
, elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
), addr
+ load_addr
);
1518 // Comparator function for sorting
1520 compare_lines(Dwarf_Line
* a
, Dwarf_Line
* b
)
1525 int lineno_a
= DWARF_LINENO(a
);
1526 int lineno_b
= DWARF_LINENO(b
);
1527 if (lineno_a
== lineno_b
)
1528 return DWARF_LINEADDR(a
) < DWARF_LINEADDR(b
);
1529 return lineno_a
< lineno_b
;
1532 // Comparator object for searching Dwarf_Lines with a specific lineno when we
1533 // don't have a Dwarf_Line of our own to search for (hence why a or b is always
1535 struct lineno_comparator
{
1537 lineno_comparator(int lineno
): lineno(lineno
) {}
1538 bool operator() (Dwarf_Line
* a
, Dwarf_Line
* b
)
1542 return DWARF_LINENO(a
) < lineno
;
1544 return lineno
< DWARF_LINENO(b
);
1548 // Returns a range of lines in between begin and end with wanted lineno. If
1549 // none exist, points to where it would have been.
1550 static lines_range_t
1551 lineno_equal_range(lines_t
* v
, int lineno
)
1553 lineno_comparator
lc(lineno
);
1554 return equal_range(v
->begin(), v
->end(), (Dwarf_Line
*)NULL
, lc
);
1557 // Interface to CU lines cache sorted by lineno
1559 dwflpp::get_cu_lines_sorted_by_lineno(const char *srcfile
)
1563 srcfile_lines_cache_t
*srcfile_lines
= cu_lines_cache
[cu
];
1566 srcfile_lines
= new srcfile_lines_cache_t();
1567 cu_lines_cache
[cu
] = srcfile_lines
;
1570 lines_t
*lines
= (*srcfile_lines
)[srcfile
];
1573 size_t nlines_cu
= 0;
1574 Dwarf_Lines
*lines_cu
= NULL
;
1575 DWARF_ASSERT("dwarf_getsrclines",
1576 dwarf_getsrclines(cu
, &lines_cu
, &nlines_cu
));
1578 lines
= new lines_t();
1579 (*srcfile_lines
)[srcfile
] = lines
;
1581 for (size_t i
= 0; i
< nlines_cu
; i
++)
1583 Dwarf_Line
*line
= dwarf_onesrcline(lines_cu
, i
);
1584 const char *linesrc
= DWARF_LINESRC(line
);
1585 if (strcmp(srcfile
, linesrc
))
1587 lines
->push_back(line
);
1590 if (lines
->size() > 1)
1591 sort(lines
->begin(), lines
->end(), compare_lines
);
1593 if (sess
.verbose
> 3)
1595 clog
<< _F("found the following lines for %s:", srcfile
) << endl
;
1596 lines_t::iterator i
;
1597 for (i
= lines
->begin(); i
!= lines
->end(); ++i
)
1598 cout
<< DWARF_LINENO(*i
) << " = " << hex
1599 << DWARF_LINEADDR(*i
) << dec
<< endl
;
1606 get_func_first_line(Dwarf_Die
*cu
, base_func_info
& func
)
1608 // dwarf_getsrc_die() uses binary search to find the Dwarf_Line, but will
1609 // return the wrong line if not found.
1610 Dwarf_Line
*line
= dwarf_getsrc_die(cu
, func
.entrypc
);
1611 if (line
&& DWARF_LINEADDR(line
) == func
.entrypc
)
1614 // Line not found (or line at wrong addr). We have to resort to a slower
1615 // linear method. We won't find an exact match (probably this is an inlined
1616 // instance), so just settle on the first Dwarf_Line with lowest addr which
1617 // falls in the die.
1619 Dwarf_Lines
*lines
= NULL
;
1620 DWARF_ASSERT("dwarf_getsrclines",
1621 dwarf_getsrclines(cu
, &lines
, &nlines
));
1623 for (size_t i
= 0; i
< nlines
; i
++)
1625 line
= dwarf_onesrcline(lines
, i
);
1626 if (dwarf_haspc(&func
.die
, DWARF_LINEADDR(line
)))
1633 collect_lines_in_die(lines_range_t range
, Dwarf_Die
*die
)
1635 lines_t lines_in_die
;
1636 for (lines_t::iterator line
= range
.first
;
1637 line
!= range
.second
; ++line
)
1638 if (dwarf_haspc(die
, DWARF_LINEADDR(*line
)))
1639 lines_in_die
.push_back(*line
);
1640 return lines_in_die
;
1644 add_matching_lines_in_func(Dwarf_Die
*cu
,
1646 base_func_info
& func
,
1647 lines_t
& matching_lines
)
1649 Dwarf_Line
*start_line
= get_func_first_line(cu
, func
);
1653 for (int lineno
= DWARF_LINENO(start_line
);;)
1655 lines_range_t range
= lineno_equal_range(cu_lines
, lineno
);
1657 // We consider the lineno still part of the die if at least one of them
1658 // falls in the die.
1659 lines_t lines_in_die
= collect_lines_in_die(range
, &func
.die
);
1660 if (lines_in_die
.empty())
1663 // Just pick the first LR even if there are more than one. Since the lines
1664 // are sorted by lineno and then addr, the first one is the one with the
1666 matching_lines
.push_back(lines_in_die
[0]);
1668 // break out if there are no more lines, otherwise, go to the next lineno
1669 if (range
.second
== cu_lines
->end())
1671 lineno
= DWARF_LINENO(*range
.second
);
1676 dwflpp::collect_all_lines(char const * srcfile
,
1677 base_func_info_map_t
& funcs
,
1678 lines_t
& matching_lines
)
1680 // This is where we handle WILDCARD lineno types.
1681 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1682 for (base_func_info_map_t::iterator func
= funcs
.begin();
1683 func
!= funcs
.end(); ++func
)
1684 add_matching_lines_in_func(cu
, cu_lines
, *func
, matching_lines
);
1689 add_matching_line_in_die(lines_t
*cu_lines
,
1690 lines_t
& matching_lines
,
1691 Dwarf_Die
*die
, int lineno
)
1693 lines_range_t lineno_range
= lineno_equal_range(cu_lines
, lineno
);
1694 lines_t lines_in_die
= collect_lines_in_die(lineno_range
, die
);
1695 if (lines_in_die
.empty())
1698 // Even if there are more than 1 LRs, just pick the first one. Since the lines
1699 // are sorted by lineno and then addr, the first one is the one with the
1700 // lowest addr. This is similar to what GDB does.
1701 matching_lines
.push_back(lines_in_die
[0]);
1705 dwflpp::collect_lines_for_single_lineno(char const * srcfile
,
1708 base_func_info_map_t
& funcs
,
1709 lines_t
& matching_lines
)
1711 /* Here, we handle ABSOLUTE and RELATIVE lineno types. Relative line numbers
1712 * are a bit special. The issue is that functions (esp. inlined ones) may not
1713 * even have a LR corresponding to the first valid line of code. So, applying
1714 * an offset to the 'first' LR found in the DIE can be quite imprecise.
1715 * Instead, we use decl_line, which although does not necessarily have a
1716 * LR associated with it (it can sometimes still happen esp. if the code is
1717 * written in OTB-style), it serves as an anchor on which we can apply the
1718 * offset to yield a lineno that will not change with compiler optimization.
1719 * It also has the added benefit of being consistent with the lineno
1720 * printed by e.g. stap -l kernel.function("vfs_read"), so users can be
1721 * confident from what lineno we adjust.
1723 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1724 for (base_func_info_map_t::iterator func
= funcs
.begin();
1725 func
!= funcs
.end(); ++func
)
1726 add_matching_line_in_die(cu_lines
, matching_lines
, &func
->die
,
1727 is_relative
? lineno
+ func
->decl_line
1732 functions_have_lineno(base_func_info_map_t
& funcs
,
1733 lines_t
*lines
, int lineno
)
1735 lines_range_t lineno_range
= lineno_equal_range(lines
, lineno
);
1736 if (lineno_range
.first
== lineno_range
.second
)
1737 return false; // no LRs at this lineno
1739 for (base_func_info_map_t::iterator func
= funcs
.begin();
1740 func
!= funcs
.end(); ++func
)
1741 if (!collect_lines_in_die(lineno_range
, &func
->die
).empty())
1747 // returns pair of valid linenos surrounding target lineno
1749 dwflpp::get_nearest_linenos(char const * srcfile
,
1751 base_func_info_map_t
& funcs
)
1754 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1756 // Look around lineno for linenos with LRs.
1757 pair
<int,int> nearest_linenos
= make_pair(-1, -1);
1758 for (size_t i
= 1; i
< 6; ++i
)
1760 if (nearest_linenos
.first
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
-i
))
1761 nearest_linenos
.first
= lineno
- i
;
1762 if (nearest_linenos
.second
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
+i
))
1763 nearest_linenos
.second
= lineno
+ i
;
1766 return nearest_linenos
;
1769 // returns nearest valid lineno to target lineno
1771 dwflpp::get_nearest_lineno(char const * srcfile
,
1773 base_func_info_map_t
& funcs
)
1776 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1778 if (nearest_linenos
.first
> 0
1779 && nearest_linenos
.second
> 0)
1781 // pick the nearest line number (break tie to upper)
1782 if (lineno
- nearest_linenos
.first
< nearest_linenos
.second
- lineno
)
1783 return nearest_linenos
.first
;
1785 return nearest_linenos
.second
;
1787 else if (nearest_linenos
.first
> 0)
1788 return nearest_linenos
.first
;
1789 else if (nearest_linenos
.second
> 0)
1790 return nearest_linenos
.second
;
1796 dwflpp::suggest_alternative_linenos(char const * srcfile
,
1798 base_func_info_map_t
& funcs
)
1801 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1803 stringstream advice
;
1804 advice
<< _F("no line records for %s:%d [man error::dwarf]", srcfile
, lineno
);
1806 if (nearest_linenos
.first
> 0 || nearest_linenos
.second
> 0)
1808 //TRANSLATORS: Here we are trying to advise what source file
1809 //TRANSLATORS: to attempt.
1810 advice
<< _(" (try ");
1811 if (nearest_linenos
.first
> 0)
1812 advice
<< ":" << nearest_linenos
.first
;
1813 if (nearest_linenos
.first
> 0 && nearest_linenos
.second
> 0)
1814 advice
<< _(" or ");
1815 if (nearest_linenos
.second
> 0)
1816 advice
<< ":" << nearest_linenos
.second
;
1819 throw SEMANTIC_ERROR (advice
.str());
1822 static base_func_info_map_t
1823 get_funcs_in_srcfile(base_func_info_map_t
& funcs
,
1824 const char * srcfile
)
1826 base_func_info_map_t matching_funcs
;
1827 for (base_func_info_map_t::iterator func
= funcs
.begin();
1828 func
!= funcs
.end(); ++func
)
1829 if (func
->decl_file
== string(srcfile
))
1830 matching_funcs
.push_back(*func
);
1831 return matching_funcs
;
1835 dwflpp::iterate_over_srcfile_lines
<void>(char const * srcfile
,
1836 const vector
<int>& linenos
,
1837 enum lineno_t lineno_type
,
1838 base_func_info_map_t
& funcs
,
1839 void (* callback
) (Dwarf_Addr
,
1844 /* Matching line records (LRs) to user-provided linenos is trickier than it
1845 * seems. The fate of all functions is one of three possibilities:
1846 * 1. it's a normal function, with a subprogram DIE and a bona fide lowpc
1847 * and highpc attribute.
1848 * 2. it's an inlined function (one/multiple inlined_subroutine DIE, with one
1849 * abstract_origin DIE)
1850 * 3. it's both a normal function and an inlined function. For example, if
1851 * the funtion has been inlined only in some places, we'll have a DIE for
1852 * the normal subprogram DIE as well as inlined_subroutine DIEs.
1854 * Multiple LRs for the same lineno but different addresses can simply happen
1855 * due to the function appearing in multiple forms. E.g. a function inlined
1856 * in two spots can yield two sets of LRs for its linenos at the different
1857 * addresses where it is inlined.
1858 * This is why the collect_* functions used here try to match up LRs back
1859 * to their originating DIEs. For example, in the function
1860 * collect_lines_for_single_lineno(), we filter first by DIE so that a lineno
1861 * corresponding to multiple addrs in multiple inlined_subroutine DIEs yields
1862 * a probe for each of them.
1866 // only work on the functions found in the current srcfile
1867 base_func_info_map_t current_funcs
= get_funcs_in_srcfile(funcs
, srcfile
);
1868 if (current_funcs
.empty())
1872 lines_t matching_lines
;
1873 if (lineno_type
== ABSOLUTE
)
1874 collect_lines_for_single_lineno(srcfile
, linenos
[0], false, /* is_relative */
1875 current_funcs
, matching_lines
);
1876 else if (lineno_type
== RELATIVE
)
1877 collect_lines_for_single_lineno(srcfile
, linenos
[0], true, /* is_relative */
1878 current_funcs
, matching_lines
);
1879 else if (lineno_type
== WILDCARD
)
1880 collect_all_lines(srcfile
, current_funcs
, matching_lines
);
1881 else if (lineno_type
== ENUMERATED
)
1883 set
<int> collected_linenos
;
1884 for (vector
<int>::const_iterator it
= linenos
.begin();
1885 it
!= linenos
.end(); it
++)
1887 // have we already collected this lineno?
1888 if (collected_linenos
.find(*it
) != collected_linenos
.end())
1891 // remember end iterator so we can tell if things were found later
1892 lines_t::const_iterator itend
= matching_lines
.end();
1894 collect_lines_for_single_lineno(srcfile
, *it
, false, /* is_relative */
1895 current_funcs
, matching_lines
);
1896 // add to set if we found LRs
1897 if (itend
!= matching_lines
.end())
1898 collected_linenos
.insert(*it
);
1900 // if we didn't find anything and .nearest is given, then try nearest
1901 if (itend
== matching_lines
.end() && has_nearest
)
1903 int nearest_lineno
= get_nearest_lineno(srcfile
, *it
,
1905 if (nearest_lineno
<= 0) // no valid nearest linenos
1908 bool new_lineno
= collected_linenos
.insert(nearest_lineno
).second
;
1910 collect_lines_for_single_lineno(srcfile
, nearest_lineno
,
1911 false, /* is_relative */
1912 current_funcs
, matching_lines
);
1917 // should we try to collect the nearest lines if we didn't collect everything
1918 // on first try? (ABSOLUTE and RELATIVE only: ENUMERATED handles it already
1919 // and WILDCARD doesn't need it)
1920 if (matching_lines
.empty() && has_nearest
&& (lineno_type
== ABSOLUTE
||
1921 lineno_type
== RELATIVE
))
1923 int lineno
= linenos
[0];
1924 if (lineno_type
== RELATIVE
)
1925 // just pick the first function and make it relative to that
1926 lineno
+= current_funcs
[0].decl_line
;
1928 int nearest_lineno
= get_nearest_lineno(srcfile
, lineno
, current_funcs
);
1929 if (nearest_lineno
> 0)
1930 collect_lines_for_single_lineno(srcfile
, nearest_lineno
,
1931 false, /* is_relative */
1932 current_funcs
, matching_lines
);
1935 // call back with matching lines
1936 if (!matching_lines
.empty())
1938 set
<Dwarf_Addr
> probed_addrs
;
1939 for (lines_t::iterator line
= matching_lines
.begin();
1940 line
!= matching_lines
.end(); ++line
)
1942 int lineno
= DWARF_LINENO(*line
);
1943 Dwarf_Addr addr
= DWARF_LINEADDR(*line
);
1944 bool is_new_addr
= probed_addrs
.insert(addr
).second
;
1946 callback(addr
, lineno
, data
);
1949 // No LRs found at the wanted lineno. So let's suggest other ones if user was
1950 // targeting a specific lineno (ABSOLUTE or RELATIVE).
1951 else if (lineno_type
== ABSOLUTE
||
1952 lineno_type
== RELATIVE
)
1954 int lineno
= linenos
[0];
1955 if (lineno_type
== RELATIVE
)
1956 // just pick the first function and make it relative to that
1957 lineno
+= current_funcs
[0].decl_line
;
1959 suggest_alternative_linenos(srcfile
, lineno
, current_funcs
);
1965 dwflpp::iterate_over_labels
<void>(Dwarf_Die
*begin_die
,
1967 const base_func_info
& function
,
1968 const vector
<int>& linenos
,
1969 enum lineno_t lineno_type
,
1971 void (* callback
)(const base_func_info
&,
1981 Dwarf_Die die
, import
;
1983 int res
= dwarf_child (begin_die
, &die
);
1985 return; // die without children, bail out.
1989 switch (dwarf_tag(&die
))
1992 name
= dwarf_diename (&die
);
1995 || (name_has_wildcard(sym
)
1996 && function_name_matches_pattern (name
, sym
))))
1998 // Don't try to be smart. Just drop no addr labels.
1999 Dwarf_Addr stmt_addr
;
2000 if (dwarf_lowpc (&die
, &stmt_addr
) == 0)
2002 // Get the file/line number for this label
2004 const char *file
= dwarf_decl_file (&die
) ?: "<unknown source>";
2005 dwarf_decl_line (&die
, &dline
);
2007 vector
<Dwarf_Die
> scopes
= getscopes_die(&die
);
2008 if (scopes
.size() > 1)
2011 if (!inner_die_containing_pc(scopes
[1], stmt_addr
, scope
))
2013 sess
.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not "
2014 "contained by its scope '%s' (dieoffset: %s) -- bad"
2015 " debuginfo? [man error::dwarf]", name
, lex_cast_hex(stmt_addr
).c_str(),
2016 lex_cast_hex(dwarf_dieoffset(&die
)).c_str(),
2017 (dwarf_diename(&scope
) ?: "<unknown>"),
2018 lex_cast_hex(dwarf_dieoffset(&scope
)).c_str()));
2021 bool matches_lineno
;
2022 if (lineno_type
== ABSOLUTE
)
2023 matches_lineno
= dline
== linenos
[0];
2024 else if (lineno_type
== RELATIVE
)
2025 matches_lineno
= dline
== linenos
[0] + function
.decl_line
;
2026 else if (lineno_type
== ENUMERATED
)
2027 matches_lineno
= (binary_search(linenos
.begin(), linenos
.end(), dline
));
2029 matches_lineno
= true;
2032 callback(function
, name
, file
, dline
,
2033 &scope
, stmt_addr
, data
);
2039 case DW_TAG_subprogram
:
2040 case DW_TAG_inlined_subroutine
:
2041 // Stay within our filtered function
2044 case DW_TAG_imported_unit
:
2045 // Iterate over the children of the imported unit as if they
2046 // were inserted in place.
2047 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2048 iterate_over_labels (&import
, sym
, function
, linenos
,
2049 lineno_type
, data
, callback
);
2053 if (dwarf_haschildren (&die
))
2054 iterate_over_labels (&die
, sym
, function
, linenos
,
2055 lineno_type
, data
, callback
);
2059 while (dwarf_siblingof (&die
, &die
) == 0);
2062 // Mini 'query-like' struct to help us navigate callbacks during
2063 // external function resolution
2064 struct external_function_query
{
2070 external_function_query(dwflpp
* dw
, const string
& name
):
2071 dw(dw
), name(name
), die(), addr(0), resolved(false) {}
2075 dwflpp::external_function_cu_callback (Dwarf_Die
* cu
, external_function_query
*efq
)
2077 efq
->dw
->focus_on_cu(cu
);
2078 return efq
->dw
->iterate_over_functions(external_function_func_callback
,
2083 dwflpp::external_function_func_callback (Dwarf_Die
* func
, external_function_query
*efq
)
2085 Dwarf_Attribute external
;
2086 Dwarf_Addr func_addr
;
2087 if (dwarf_attr_integrate(func
, DW_AT_external
, &external
) != NULL
&&
2088 dwarf_lowpc(func
, &func_addr
) == 0)
2091 efq
->addr
= func_addr
;
2092 efq
->resolved
= true;
2093 return DWARF_CB_ABORT
; // we found it so stop here
2099 dwflpp::iterate_over_callees
<void>(Dwarf_Die
*begin_die
,
2101 int64_t recursion_depth
,
2103 void (* callback
)(base_func_info
&,
2107 base_func_info
& caller
,
2108 stack
<Dwarf_Addr
> *callers
)
2112 Dwarf_Die die
, import
;
2114 // DIE of abstract_origin found in die
2117 // callee's entry pc (= where we'll probe)
2118 Dwarf_Addr func_addr
;
2120 // caller's unwind pc during call (to match against bt for filtering)
2121 Dwarf_Addr caller_uw_addr
;
2123 Dwarf_Attribute attr
;
2125 base_func_info callee
;
2126 if (dwarf_child(begin_die
, &die
) != 0)
2127 return; // die without children, bail out.
2129 bool free_callers
= false;
2130 if (callers
== NULL
) /* first call */
2132 callers
= new stack
<Dwarf_Addr
>();
2133 free_callers
= true;
2138 bool inlined
= false;
2139 switch (dwarf_tag(&die
))
2141 case DW_TAG_inlined_subroutine
:
2143 /* FALLTHROUGH */ /* thanks mjw */
2144 case DW_TAG_GNU_call_site
:
2145 callee
.name
= dwarf_diename(&die
) ?: "";
2146 if (callee
.name
.empty())
2148 if (callee
.name
!= sym
)
2150 if (!name_has_wildcard(sym
))
2152 if (!function_name_matches_pattern(callee
.name
, sym
))
2156 /* In both cases (call sites and inlines), we want the
2157 * abstract_origin. The difference is that in inlines, the addr is
2158 * in the die itself, whereas for call sites, the addr is in the
2159 * abstract_origin's die.
2160 * Note that in the case of inlines, we're only calling back
2161 * for that inline instance, not all. This is what we want, since
2162 * it will only be triggered when 'called' from the target func,
2163 * which is something we have to emulate for non-inlined funcs
2164 * (which is the purpose of the caller_uw_addr below) */
2165 if (dwarf_attr_die(&die
, DW_AT_abstract_origin
, &origin
) == NULL
)
2168 // the low_pc of the die in either cases is the pc that would
2169 // show up in a backtrace (inlines are a special case in which
2170 // the die's low_pc is also the abstract_origin's low_pc = the
2171 // 'start' of the inline instance)
2172 if (dwarf_lowpc(&die
, &caller_uw_addr
) != 0)
2176 func_addr
= caller_uw_addr
;
2177 else if (dwarf_lowpc(&origin
, &func_addr
) != 0)
2179 // function doesn't have a low_pc, is it external?
2180 if (dwarf_attr_integrate(&origin
, DW_AT_external
,
2183 // let's iterate over the CUs and find it. NB: it's
2184 // possible we could have also done this by creating a
2185 // probe point with .exported tacked on and rerunning it
2186 // through derive_probe(). But since we're already on the
2187 // dwflpp side of things, and we already have access to
2188 // everything we need, let's try to be self-sufficient.
2190 // remember old focus
2191 Dwarf_Die
*old_cu
= cu
;
2193 external_function_query
efq(this, dwarf_linkage_name(&origin
) ?: callee
.name
);
2194 iterate_over_cus(external_function_cu_callback
, &efq
, false);
2199 if (!efq
.resolved
) // did we resolve it?
2202 func_addr
= efq
.addr
;
2205 // non-external function without low_pc, jump ship
2209 // We now have the addr to probe in func_addr, and the DIE
2210 // from which to obtain file/line info in origin
2212 // Get the file/line number for this callee
2213 callee
.decl_file
= dwarf_decl_file (&origin
) ?: "<unknown source>";
2214 dwarf_decl_line (&origin
, &callee
.decl_line
);
2216 // add as a caller to match against
2218 callers
->push(caller_uw_addr
);
2220 callee
.die
= inlined
? die
: origin
;
2221 callee
.entrypc
= func_addr
;
2222 callback(callee
, caller
, callers
, data
);
2224 // If it's a tail call, print a warning that it may not be caught
2226 && dwarf_attr_integrate(&die
, DW_AT_GNU_tail_call
, &attr
) != NULL
)
2227 sess
.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: "
2228 ".callee probe may not fire. Try placing the probe "
2229 "directly on the callee function instead.",
2230 callee
.name
.to_string().c_str(),
2231 caller
.name
.to_string().c_str()));
2233 // For .callees(N) probes, we recurse on this callee. Note that we
2234 // pass the callee we just found as the caller arg for this recursion,
2235 // since it (the callee we just found) will be the caller of whatever
2236 // callees found inside this recursion.
2237 if (recursion_depth
> 1)
2238 iterate_over_callees(inlined
? &die
: &origin
,
2239 sym
, recursion_depth
-1, data
,
2240 callback
, callee
, callers
);
2246 case DW_TAG_subprogram
:
2247 break; // don't leave our filtered func
2249 case DW_TAG_imported_unit
:
2250 // Iterate over the children of the imported unit as if they
2251 // were inserted in place.
2252 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2253 // NB: we pass the same caller arg into it
2254 iterate_over_callees (&import
, sym
, recursion_depth
, data
,
2255 callback
, caller
, callers
);
2259 if (dwarf_haschildren (&die
))
2260 // NB: we pass the same caller arg into it
2261 iterate_over_callees (&die
, sym
, recursion_depth
, data
,
2262 callback
, caller
, callers
);
2266 while (dwarf_siblingof (&die
, &die
) == 0);
2268 if (free_callers
&& callers
!= NULL
)
2274 dwflpp::collect_srcfiles_matching (string
const & pattern
,
2275 set
<string
> & filtered_srcfiles
)
2281 Dwarf_Files
*srcfiles
;
2283 // PR 5049: implicit * in front of given path pattern.
2284 // NB: fnmatch() is used without FNM_PATHNAME.
2285 string prefixed_pattern
= string("*/") + pattern
;
2287 DWARF_ASSERT ("dwarf_getsrcfiles",
2288 dwarf_getsrcfiles (cu
, &srcfiles
, &nfiles
));
2290 for (size_t i
= 0; i
< nfiles
; ++i
)
2292 char const * fname
= dwarf_filesrc (srcfiles
, i
, NULL
, NULL
);
2293 if (fnmatch (pattern
.c_str(), fname
, 0) == 0 ||
2294 fnmatch (prefixed_pattern
.c_str(), fname
, 0) == 0)
2296 filtered_srcfiles
.insert (fname
);
2298 clog
<< _F("selected source file '%s'\n", fname
);
2306 dwflpp::resolve_prologue_endings (func_info_map_t
& funcs
)
2308 // When a program is compiled with no optimization, GCC does no variable
2309 // tracking, which means that location info is actually only really valid
2310 // after the prologue, even though GCC reports it as valid during. So we need
2311 // to find the prologue ends to get accurate info. This may or may not be the
2312 // first address that has a source line distinct from the function
2319 Dwarf_Lines
*lines
= NULL
;
2322 malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue
2323 sys_get?id in kernel/timer.c no-prologue
2324 sys_exit_group tail-recursive
2325 {do_,}sys_open extra-long-prologue (gcc 3.4)
2326 cpu_to_logical_apicid NULL-decl_file
2329 // Fetch all srcline records, sorted by address. No need to free lines, it's a
2330 // direct pointer to the CU's cached lines.
2331 if (dwarf_getsrclines(cu
, &lines
, &nlines
) != 0
2332 || lines
== NULL
|| nlines
== 0)
2334 if (sess
.verbose
> 2)
2335 clog
<< _F("aborting prologue search: no source lines found for cu '%s'\n",
2340 // Dump them into our own array for easier searching. They should already be
2341 // sorted by addr, but we doublecheck that here. We want to keep the indices
2342 // between lines and addrs the same.
2343 vector
<Dwarf_Addr
> addrs
;
2344 for (size_t i
= 0; i
< nlines
; i
++)
2346 Dwarf_Line
* line
= dwarf_onesrcline(lines
, i
);
2347 Dwarf_Addr addr
= DWARF_LINEADDR(line
);
2348 if (!addrs
.empty() && addr
< addrs
.back())
2349 throw SEMANTIC_ERROR(_("lines from dwarf_getsrclines() not sorted"));
2350 addrs
.push_back(addr
);
2352 // We normally ignore a function's decl_line, since it is associated with the
2353 // line at which the identifier appears in the declaration, and has no
2354 // meaningful relation to the lineno associated with the entrypc (which is
2355 // normally the lineno of '{', which could occur at the same line as the
2356 // declaration, or lower down).
2357 // However, if the CU was compiled using GCC < 4.4, then the decl_line
2358 // actually represents the lineno of '{' as well, in which case if the lineno
2359 // associated with the entrypc is != to the decl_line, it means the compiler
2360 // scraped/optimized off some of the beginning of the function and the safest
2361 // thing we can do is consider it naked.
2362 bool consider_decl_line
= false;
2365 if (is_gcc_producer(cu
, prod
, vers
)
2366 && strverscmp(vers
.c_str(), "4.4.0") < 0)
2367 consider_decl_line
= true;
2370 for(func_info_map_t::iterator it
= funcs
.begin(); it
!= funcs
.end(); it
++)
2373 Dwarf_Addr
* bkpts
= 0;
2374 int n
= dwarf_entry_breakpoints (& it
->die
, & bkpts
);
2379 Dwarf_Addr entrypc
= it
->entrypc
;
2380 Dwarf_Addr highpc
; // NB: highpc is exclusive: [entrypc,highpc)
2381 DWFL_ASSERT ("dwarf_highpc", dwarf_highpc (& it
->die
,
2384 unsigned entrypc_srcline_idx
= 0;
2385 Dwarf_Line
*entrypc_srcline
= NULL
;
2387 vector
<Dwarf_Addr
>::const_iterator it_addr
=
2388 lower_bound(addrs
.begin(), addrs
.end(), entrypc
);
2389 if (it_addr
!= addrs
.end() && *it_addr
== entrypc
)
2391 entrypc_srcline_idx
= it_addr
- addrs
.begin();
2392 entrypc_srcline
= dwarf_onesrcline(lines
, entrypc_srcline_idx
);
2396 if (!entrypc_srcline
)
2398 if (sess
.verbose
> 2)
2399 clog
<< _F("missing entrypc dwarf line record for function '%s'\n",
2400 it
->name
.to_string().c_str());
2401 // This is probably an inlined function. We'll end up using
2402 // its lowpc as a probe address.
2408 if (sess
.verbose
> 2)
2409 clog
<< _F("null entrypc dwarf line record for function '%s'\n",
2410 it
->name
.to_string().c_str());
2411 // This is probably an inlined function. We'll skip this instance;
2417 clog
<< _F("searching for prologue of function '%s' %#" PRIx64
"-%#" PRIx64
2418 "@%s:%d\n", it
->name
.to_string().c_str(), entrypc
, highpc
,
2419 it
->decl_file
.to_string().c_str(), it
->decl_line
);
2421 // For each function, we look for the prologue-end marker (e.g. clang
2422 // outputs one). If there is no explicit marker (e.g. GCC does not), we
2423 // accept a bigger or equal lineno as a prologue end (this catches GCC's
2424 // 0-line advances).
2426 // We may have to skip a few because some old compilers plop
2427 // in dummy line records for longer prologues. If we go too
2428 // far (addr >= highpc), we take the previous one. Or, it may
2429 // be the first one, if the function had no prologue, and thus
2430 // the entrypc maps to a statement in the body rather than the
2433 int entrypc_srcline_lineno
= DWARF_LINENO(entrypc_srcline
);
2434 unsigned postprologue_srcline_idx
= entrypc_srcline_idx
;
2435 Dwarf_Line
*postprologue_srcline
= entrypc_srcline
;
2437 while (postprologue_srcline_idx
< nlines
)
2439 postprologue_srcline
= dwarf_onesrcline(lines
,
2440 postprologue_srcline_idx
);
2441 Dwarf_Addr lineaddr
= DWARF_LINEADDR(postprologue_srcline
);
2442 const char* linesrc
= DWARF_LINESRC(postprologue_srcline
);
2443 int lineno
= DWARF_LINENO(postprologue_srcline
);
2444 bool lineprologue_end
= DWARF_LINEPROLOGUEEND(postprologue_srcline
);
2447 clog
<< _F("checking line record %#" PRIx64
"@%s:%d%s\n", lineaddr
,
2448 linesrc
, lineno
, lineprologue_end
? " (marked)" : "");
2450 // have we passed the function?
2451 if (lineaddr
>= highpc
)
2453 // is there an explicit prologue_end marker?
2454 if (lineprologue_end
)
2456 // is it a different file?
2457 if (it
->decl_file
!= string(linesrc
))
2459 // OK, it's the same file, but is it a different line?
2460 if (lineno
!= entrypc_srcline_lineno
)
2462 // Same file and line, is this a second line record (e.g. 0-line advance)?
2463 if (postprologue_srcline_idx
!= entrypc_srcline_idx
)
2465 // This is the first iteration. Is decl_line meaningful and is the
2466 // lineno past the decl_line?
2467 if (consider_decl_line
&& lineno
!= it
->decl_line
)
2470 // Let's try the next srcline.
2471 postprologue_srcline_idx
++;
2473 } // loop over srclines
2476 Dwarf_Addr postprologue_addr
= DWARF_LINEADDR(postprologue_srcline
);
2477 if (postprologue_addr
>= highpc
)
2479 // pick addr of previous line record
2480 Dwarf_Line
*lr
= dwarf_onesrcline(lines
, postprologue_srcline_idx
-1);
2481 postprologue_addr
= DWARF_LINEADDR(lr
);
2484 it
->prologue_end
= postprologue_addr
;
2488 clog
<< _F("prologue found function '%s'", it
->name
.to_string().c_str());
2489 // Add a little classification datum
2490 //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free)
2491 if (postprologue_addr
== entrypc
)
2492 clog
<< _(" (naked)");
2493 //TRANSLATORS: Here we're adding some classification datum (ie we went over)
2494 if (DWARF_LINEADDR(postprologue_srcline
) >= highpc
)
2495 clog
<< _(" (tail-call?)");
2496 //TRANSLATORS: Here we're adding some classification datum (ie it was marked)
2497 if (DWARF_LINEPROLOGUEEND(postprologue_srcline
))
2498 clog
<< _(" (marked)");
2500 clog
<< " = 0x" << hex
<< postprologue_addr
<< dec
<< "\n";
2503 } // loop over functions
2508 dwflpp::function_entrypc (Dwarf_Addr
* addr
)
2511 // PR10574: reject 0, which tends to be eliminated COMDAT
2512 return (dwarf_entrypc (function
, addr
) == 0 && *addr
!= 0);
2517 dwflpp::die_entrypc (Dwarf_Die
* die
, Dwarf_Addr
* addr
)
2520 string lookup_method
;
2524 lookup_method
= "dwarf_entrypc";
2525 rc
= dwarf_entrypc (die
, addr
);
2529 lookup_method
= "dwarf_ranges";
2534 ptrdiff_t offset
= dwarf_ranges (die
, 0, &base
, &begin
, &end
);
2535 if (offset
< 0) rc
= -1;
2536 else if (offset
> 0)
2541 // Now we need to check that there are no more ranges
2542 // associated with this function, which could conceivably
2543 // happen if a function is inlined, then pieces of it are
2544 // split amongst different conditional branches. It's not
2545 // obvious which of them to favour. As a heuristic, we
2546 // pick the beginning of the first range, and ignore the
2547 // others (but with a warning).
2550 while ((offset
= dwarf_ranges (die
, offset
, &base
, &begin
, &end
)) > 0)
2553 lookup_method
+= _F(", ignored %s more", lex_cast(extra
).c_str());
2557 // PR10574: reject subprograms where the entrypc address turns out
2558 // to be 0, since they tend to correspond to duplicate-eliminated
2559 // COMDAT copies of C++ functions.
2560 if (rc
== 0 && *addr
== 0)
2562 lookup_method
+= _(" (skip comdat)");
2566 if (sess
.verbose
> 2)
2567 clog
<< _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64
" (rc %d)", lookup_method
.c_str(),
2568 lex_cast_hex(dwarf_dieoffset(die
)).c_str(), *addr
, rc
) << endl
;
2575 dwflpp::function_die (Dwarf_Die
*d
)
2583 dwflpp::function_file (char const ** c
)
2587 *c
= dwarf_decl_file (function
);
2590 // The line table might know.
2592 if (dwarf_lowpc(function
, &pc
) == 0)
2593 *c
= pc_line (pc
, NULL
, NULL
);
2596 *c
= "<unknown source>";
2602 dwflpp::function_line (int *linep
)
2605 if (dwarf_decl_line (function
, linep
) != 0)
2607 // The line table might know.
2609 if (dwarf_lowpc(function
, &pc
) == 0)
2610 pc_line (pc
, linep
, NULL
);
2616 dwflpp::die_has_pc (Dwarf_Die
& die
, Dwarf_Addr pc
)
2618 int res
= dwarf_haspc (&die
, pc
);
2619 // dwarf_ranges will return -1 if a function die has no DW_AT_ranges
2621 // DWARF_ASSERT ("dwarf_haspc", res);
2627 dwflpp::inner_die_containing_pc(Dwarf_Die
& scope
, Dwarf_Addr addr
,
2632 // Sometimes we're in a bad scope to begin with -- just let it be. This can
2633 // happen for example if the compiler outputs a label PC that's just outside
2634 // the lexical scope. We can't really do anything about that, but variables
2635 // will probably not be accessible in this case.
2636 if (!die_has_pc(scope
, addr
))
2639 Dwarf_Die child
, import
;
2640 int rc
= dwarf_child(&result
, &child
);
2643 switch (dwarf_tag (&child
))
2645 case DW_TAG_imported_unit
:
2646 // The children of the imported unit need to be treated as if
2647 // they are inserted here. So look inside and set result if
2649 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2651 Dwarf_Die import_result
;
2652 if (inner_die_containing_pc(import
, addr
, import_result
))
2654 result
= import_result
;
2660 // lexical tags to recurse within the same starting scope
2661 // NB: this intentionally doesn't cross into inlines!
2662 case DW_TAG_lexical_block
:
2663 case DW_TAG_with_stmt
:
2664 case DW_TAG_catch_block
:
2665 case DW_TAG_try_block
:
2666 case DW_TAG_entry_point
:
2667 if (die_has_pc(child
, addr
))
2670 rc
= dwarf_child(&result
, &child
);
2674 rc
= dwarf_siblingof(&child
, &child
);
2681 dwflpp::loc2c_error (void *arg
, const char *fmt
, ...)
2683 const char *msg
= "?";
2688 rc
= vasprintf (& tmp
, fmt
, ap
);
2695 dwflpp
*pp
= (dwflpp
*) arg
;
2696 semantic_error
err(ERR_SRC
, msg
);
2697 string what
= pp
->die_location_as_string(pp
->l2c_ctx
.pc
, pp
->l2c_ctx
.die
);
2698 string where
= pp
->die_location_as_function_string (pp
->l2c_ctx
.pc
,
2700 err
.details
.push_back(what
);
2701 err
.details
.push_back(where
);
2706 // This function generates code used for addressing computations of
2707 // target variables.
2709 dwflpp::emit_address (struct obstack
*pool
, Dwarf_Addr address
)
2711 int n
= dwfl_module_relocations (module
);
2712 DWFL_ASSERT ("dwfl_module_relocations", n
>= 0);
2713 Dwarf_Addr reloc_address
= address
;
2714 const char *secname
= "";
2717 int i
= dwfl_module_relocate_address (module
, &reloc_address
);
2718 DWFL_ASSERT ("dwfl_module_relocate_address", i
>= 0);
2719 secname
= dwfl_module_relocation_info (module
, i
, NULL
);
2722 if (sess
.verbose
> 2)
2724 clog
<< _F("emit dwarf addr %#" PRIx64
" => module %s section %s relocaddr %#" PRIx64
,
2725 address
, module_name
.c_str (), (secname
?: "null"),
2726 reloc_address
) << endl
;
2729 if (n
> 0 && !(n
== 1 && secname
== NULL
))
2731 DWFL_ASSERT ("dwfl_module_relocation_info", secname
);
2732 if (n
> 1 || secname
[0] != '\0')
2734 // This gives us the module name, and section name within the
2735 // module, for a kernel module (or other ET_REL module object).
2736 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2737 obstack_printf (pool
, "addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2738 module_name
.c_str(), secname
, reloc_address
);
2739 obstack_printf (pool
, "addr; })");
2741 else if (n
== 1 && module_name
== TOK_KERNEL
&& secname
[0] == '\0')
2743 // elfutils' way of telling us that this is a relocatable kernel address, which we
2744 // need to treat the same way here as dwarf_query::add_probe_point does: _stext.
2745 address
-= sess
.sym_stext
;
2747 // Note we "cache" the result here through a static because the
2748 // kernel will never move after being loaded (unlike modules and
2749 // user-space dynamic share libraries).
2750 obstack_printf (pool
, "({ static unsigned long addr = 0; ");
2751 obstack_printf (pool
, "if (addr==0) addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2752 module_name
.c_str(), secname
, address
); // PR10000 NB: not reloc_address
2753 obstack_printf (pool
, "addr; })");
2757 obstack_printf (pool
, "/* pragma:vma */");
2758 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2759 obstack_printf (pool
, "addr = _stp_umodule_relocate (\"%s\",%#" PRIx64
", current); ",
2760 resolve_path(module_name
.c_str()).c_str(), address
);
2761 obstack_printf (pool
, "addr; })");
2765 obstack_printf (pool
, "%#" PRIx64
"UL", address
); // assume as constant
2770 dwflpp::loc2c_emit_address (void *arg
, struct obstack
*pool
,
2773 static_cast<dwflpp
*>(arg
)->emit_address (pool
, address
);
2778 dwflpp::get_locals(vector
<Dwarf_Die
>& scopes
, set
<string
>& locals
)
2780 // XXX Shouldn't this be walking up to outer scopes too?
2782 get_locals_die(scopes
[0], locals
);
2786 dwflpp::get_locals_die(Dwarf_Die
& die
, set
<string
>& locals
)
2788 // Try to get the first child of die.
2789 Dwarf_Die child
, import
;
2790 if (dwarf_child (&die
, &child
) == 0)
2795 // Output each sibling's name (that is a variable or
2796 // parameter) to 'o'.
2797 switch (dwarf_tag (&child
))
2799 case DW_TAG_variable
:
2800 case DW_TAG_formal_parameter
:
2801 name
= dwarf_diename (&child
);
2803 locals
.insert(string("$") + name
);
2805 case DW_TAG_imported_unit
:
2806 // Treat the imported unit children as if they are
2807 // children of the given DIE.
2808 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2809 get_locals_die (import
, locals
);
2815 while (dwarf_siblingof (&child
, &child
) == 0);
2821 dwflpp::find_variable_and_frame_base (vector
<Dwarf_Die
>& scopes
,
2823 string
const & local
,
2824 const target_symbol
*e
,
2826 Dwarf_Attribute
*fb_attr_mem
)
2828 Dwarf_Die
*scope_die
= &scopes
[0];
2829 Dwarf_Attribute
*fb_attr
= NULL
;
2833 int declaring_scope
= dwarf_getscopevar (&scopes
[0], scopes
.size(),
2837 if (declaring_scope
< 0)
2840 get_locals(scopes
, locals
);
2841 string sugs
= levenshtein_suggest(local
, locals
); // probably not that many, so no limit
2843 throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)",
2845 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2846 module_name
.c_str(),
2847 lex_cast_hex(pc
).c_str(),
2848 (scope_die
== NULL
) ? "" : _("in"),
2849 (dwarf_diename(scope_die
) ?: "<unknown>"),
2850 (dwarf_diename(cu
) ?: "<unknown>"),
2852 ? (_("<no alternatives>"))
2853 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2856 throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)",
2858 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2859 module_name
.c_str(),
2860 (scope_die
== NULL
) ? "" : _("in"),
2861 (dwarf_diename(scope_die
) ?: "<unknown>"),
2864 ? (_("<no alternatives>"))
2865 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2869 /* Some GCC versions would output duplicate external variables, one
2870 without a location attribute. If so, try to find the other if it
2871 exists in the same scope. See GCC PR51410. */
2872 Dwarf_Attribute attr_mem
;
2873 if (dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
) == NULL
2874 && dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
) == NULL
2875 && dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
) != NULL
2876 && dwarf_tag(&scopes
[declaring_scope
]) == DW_TAG_compile_unit
)
2878 Dwarf_Die orig_vardie
= *vardie
;
2879 bool alt_found
= false;
2880 if (dwarf_child(&scopes
[declaring_scope
], vardie
) == 0)
2883 // Note, not handling DW_TAG_imported_unit, assuming GCC
2884 // version is recent enough to not need this workaround if
2885 // we would see an imported unit.
2886 if (dwarf_tag (vardie
) == DW_TAG_variable
2887 && strcmp (dwarf_diename (vardie
), local
.c_str ()) == 0
2888 && (dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
)
2890 && ((dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
)
2892 || (dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
)
2896 while (!alt_found
&& dwarf_siblingof(vardie
, vardie
) == 0);
2899 *vardie
= orig_vardie
;
2902 // Global vars don't need (cannot use) frame base in location descriptor.
2906 /* We start out walking the "lexical scopes" as returned by
2907 * as returned by dwarf_getscopes for the address, starting with the
2908 * declaring_scope that the variable was found in.
2910 vector
<Dwarf_Die
> physcopes
, *fbscopes
= &scopes
;
2911 for (size_t inner
= declaring_scope
;
2912 inner
< fbscopes
->size() && fb_attr
== NULL
;
2915 Dwarf_Die
& scope
= (*fbscopes
)[inner
];
2916 switch (dwarf_tag (&scope
))
2920 case DW_TAG_subprogram
:
2921 case DW_TAG_entry_point
:
2922 fb_attr
= dwarf_attr_integrate (&scope
,
2926 case DW_TAG_inlined_subroutine
:
2927 /* Unless we already are going through the "pyshical die tree",
2928 * we now need to start walking the die tree where this
2929 * subroutine is inlined to find the appropriate frame base. */
2930 if (declaring_scope
!= -1)
2932 physcopes
= getscopes_die(&scope
);
2933 if (physcopes
.empty())
2934 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
2935 local
.c_str()), e
->tok
);
2936 fbscopes
= &physcopes
;
2937 inner
= 0; // zero is current scope, for look will increase.
2938 declaring_scope
= -1;
2947 /* Returns a human readable string with suggested locations where a
2948 DIE attribute is valid. */
2950 suggested_locations_string(Dwarf_Attribute
*attr
)
2954 locsstr
= "<no alternatives for NULL attribute>";
2957 #if _ELFUTILS_PREREQ (0, 158)
2960 Dwarf_Addr base
, start
, end
;
2963 off
= dwarf_getlocations (attr
, off
, &base
,
2968 locsstr
= _("alternative locations: ");
2973 locsstr
+= lex_cast_hex(start
);
2975 locsstr
+= lex_cast_hex(end
);
2978 off
= dwarf_getlocations (attr
, off
, &base
,
2986 locsstr
= _("<no alternative locations>");
2988 locsstr
= _F("<error getting alternative locations: %s>",
2991 locsstr
= "<cannot suggest any alternative locations, elfutils too old>";
2992 #endif /* _ELFUTILS_PREREQ (0, 158) */
2998 /* Produce a human readable name for a DIE. */
3000 die_name_string (Dwarf_Die
*die
)
3003 const char *name
= dwarf_linkage_name(die
);
3005 name
= dwarf_diename (die
);
3007 size_t demangle_buffer_len
= 0;
3008 char *demangle_buffer
= NULL
;
3009 if (name
!= NULL
&& name
[0] == '_' && name
[1] == 'Z')
3012 char *dsymname
= abi::__cxa_demangle (name
, demangle_buffer
,
3013 &demangle_buffer_len
, &status
);
3015 name
= demangle_buffer
= dsymname
;
3020 res
= _("<unknown");
3021 free (demangle_buffer
);
3026 /* Returns a source file name, line and column information based on the
3027 pc and the current cu. */
3029 dwflpp::pc_line (Dwarf_Addr pc
, int *lineno
, int *colno
)
3033 Dwarf_Line
*line
= dwarf_getsrc_die (cu
, pc
);
3037 dwarf_lineno (line
, lineno
);
3039 dwarf_linecol (line
, colno
);
3040 return dwarf_linesrc (line
, NULL
, NULL
);
3047 /* Returns a source line and column string based on the inlined DIE
3048 or based on the pc if DIE is NULL. */
3050 dwflpp::pc_die_line_string (Dwarf_Addr pc
, Dwarf_Die
*die
)
3055 const char *src
= NULL
;
3059 src
= pc_line (pc
, &lineno
, &col
);
3063 if (dwarf_getsrcfiles (cu
, &files
, NULL
) == 0)
3065 Dwarf_Attribute attr
;
3067 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_file
, &attr
),
3070 src
= dwarf_filesrc (files
, val
, NULL
, NULL
);
3071 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_line
,
3075 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_column
,
3082 src
= pc_line (pc
, &lineno
, &col
);
3090 linestr
+= ":" + lex_cast(lineno
);
3092 linestr
+= ":" + lex_cast(col
);
3096 linestr
+= _("unknown source");
3101 /* Returns a human readable DIE offset for use in error messages.
3102 Includes DIE offset and DWARF file used. */
3104 dwflpp::die_location_as_string(Dwarf_Addr pc
, Dwarf_Die
*die
)
3109 locstr
+= _("dieoffset: ");
3110 locstr
+= lex_cast_hex(dwarf_dieoffset(die
));
3113 const char *debugfile
;
3114 locstr
+= _(" from ");
3115 if (dwfl_module_info (module
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
3116 &debugfile
) == NULL
|| debugfile
== NULL
)
3118 locstr
+= _("unknown debug file for ");
3119 locstr
+= module_name
;
3122 locstr
+= debugfile
;
3127 /* Returns a human readable (inlined) function and source file/line location
3128 for a DIE and pc location. */
3130 dwflpp::die_location_as_function_string(Dwarf_Addr pc
, Dwarf_Die
*die
)
3133 locstr
= _("function: ");
3135 /* Find the first function-like DIE with a name in scope. */
3136 Dwarf_Die funcdie_mem
;
3137 Dwarf_Die
*funcdie
= NULL
;
3138 string funcname
= "";
3139 Dwarf_Die
*scopes
= NULL
;
3140 int nscopes
= dwarf_getscopes (cu
, pc
, &scopes
);
3141 for (int i
= 0; funcname
== "" && i
< nscopes
; i
++)
3143 Dwarf_Die
*scope
= &scopes
[i
];
3144 int tag
= dwarf_tag (scope
);
3145 if (tag
== DW_TAG_subprogram
3146 || tag
== DW_TAG_inlined_subroutine
3147 || tag
== DW_TAG_entry_point
)
3148 funcname
= die_name_string (scope
);
3151 funcdie_mem
= *scope
;
3152 funcdie
= &funcdie_mem
;
3157 /* source location */
3159 locstr
+= _("<unknown> at ") + pc_die_line_string (pc
, NULL
);
3162 int nscopes
= dwarf_getscopes_die (funcdie
, &scopes
);
3165 /* scopes[0] == funcdie, the lowest level, for which we already have
3166 the name. This is the actual source location where it
3169 locstr
+= _(" at ");
3170 locstr
+= pc_die_line_string (pc
, NULL
);
3172 /* last_scope is the source location where the next inlined frame/function
3174 Dwarf_Die
*last_scope
= &scopes
[0];
3175 for (int i
= 1; i
< nscopes
; i
++)
3177 Dwarf_Die
*scope
= &scopes
[i
];
3178 int tag
= dwarf_tag (scope
);
3179 if (tag
!= DW_TAG_inlined_subroutine
3180 && tag
!= DW_TAG_entry_point
3181 && tag
!= DW_TAG_subprogram
)
3184 locstr
+= _(" inlined by ");
3185 locstr
+= die_name_string (scope
);
3186 locstr
+= _(" at ");
3187 locstr
+= pc_die_line_string (pc
, last_scope
);
3189 /* Found the "top-level" in which everything was inlined. */
3190 if (tag
== DW_TAG_subprogram
)
3199 locstr
+= _(" at ");
3200 locstr
+= pc_die_line_string (pc
, NULL
);
3209 dwflpp::translate_location(struct obstack
*pool
,
3210 Dwarf_Attribute
*attr
, Dwarf_Die
*die
,
3212 Dwarf_Attribute
*fb_attr
,
3213 struct location
**tail
,
3214 const target_symbol
*e
)
3217 /* DW_AT_data_member_location, can be either constant offsets
3218 (struct member fields), or full blown location expressions. */
3220 /* There is no location expression, but a constant value instead. */
3221 if (dwarf_whatattr (attr
) == DW_AT_const_value
)
3225 *tail
= c_translate_constant (pool
, &loc2c_error
, this,
3226 &loc2c_emit_address
, 0, pc
, attr
);
3233 /* PR9768: formerly, we added pc+module_bias here. However, that bias value
3234 is not present in the pc value by the time we get it, so adding it would
3235 result in false negatives of variable reachibility. In other instances
3236 further below, the c_translate_FOO functions, the module_bias value used
3237 to be passed in, but instead should now be zero for the same reason. */
3240 switch (dwarf_getlocation_addr (attr
, pc
/*+ module_bias*/, &expr
, &len
, 1))
3242 case 1: /* Should always happen. */
3247 case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */
3249 Dwarf_Addr pc2
= pr15123_retry_addr (pc
, die
);
3258 string msg
= _F("not accessible at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3259 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3260 err
.details
.push_back(die_location_as_string(pc
, die
));
3261 err
.details
.push_back(die_location_as_function_string(pc
, die
));
3262 err
.details
.push_back(suggested_locations_string(attr
));
3266 default: /* Shouldn't happen. */
3269 string msg
= _F("dwarf_getlocation_addr failed at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3270 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3271 string dwarf_err
= _F("dwarf_error: %s", dwarf_errmsg(-1));
3272 err
.details
.push_back(dwarf_err
);
3273 err
.details
.push_back(die_location_as_string(pc
, die
));
3274 err
.details
.push_back(die_location_as_function_string(pc
, die
));
3275 err
.details
.push_back(suggested_locations_string(attr
));
3281 // pc is in the dw address space of the current module, which is what
3282 // c_translate_location expects. get_cfa_ops wants the global dwfl address.
3283 // cfa_ops only make sense for locals.
3286 Dwarf_Addr addr
= pc
+ module_bias
;
3287 cfa_ops
= get_cfa_ops (addr
);
3294 return c_translate_location (pool
, &loc2c_error
, this,
3295 &loc2c_emit_address
,
3297 pc
, attr
, expr
, len
, tail
, fb_attr
, cfa_ops
);
3302 dwflpp::get_members(Dwarf_Die
*typedie
, set
<string
>& members
, set
<string
> &dupes
)
3304 const int typetag
= dwarf_tag (typedie
);
3306 /* compile and partial unit included for recursion through
3307 imported_unit below. */
3308 if (typetag
!= DW_TAG_structure_type
&&
3309 typetag
!= DW_TAG_class_type
&&
3310 typetag
!= DW_TAG_union_type
&&
3311 typetag
!= DW_TAG_compile_unit
&&
3312 typetag
!= DW_TAG_partial_unit
)
3314 throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union",
3315 dwarf_type_name(typedie
).c_str()));
3318 // Try to get the first child of vardie.
3319 Dwarf_Die die_mem
, import
;
3320 Dwarf_Die
*die
= &die_mem
;
3321 switch (dwarf_child (typedie
, die
))
3323 case 1: // No children.
3324 throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie
).c_str()));
3327 default: // Shouldn't happen.
3328 throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie
).c_str(),
3335 // Add each sibling's name to members set
3338 int tag
= dwarf_tag(die
);
3340 /* The children of an imported_unit should be treated as members too. */
3341 if (tag
== DW_TAG_imported_unit
3342 && dwarf_attr_die(die
, DW_AT_import
, &import
))
3343 get_members(&import
, members
, dupes
);
3345 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3348 const char *member
= dwarf_diename (die
) ;
3350 if ( tag
== DW_TAG_member
&& member
!= NULL
)
3352 // Only output if this is new, to avoid inheritance dupes.
3353 if (dupes
.insert(member
).second
)
3354 members
.insert(member
);
3359 if (!dwarf_attr_die (die
, DW_AT_type
, &temp_die
))
3361 string source
= dwarf_decl_file(die
) ?: "<unknown source>";
3363 dwarf_decl_line(die
, &line
);
3364 throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous "
3365 "member at %s:%d", source
.c_str(), line
));
3368 get_members(&temp_die
, members
, dupes
);
3372 while (dwarf_siblingof (die
, die
) == 0);
3377 dwflpp::find_struct_member(const target_symbol::component
& c
,
3378 Dwarf_Die
*parentdie
,
3379 Dwarf_Die
*memberdie
,
3380 vector
<Dwarf_Die
>& dies
,
3381 vector
<Dwarf_Attribute
>& locs
)
3383 Dwarf_Attribute attr
;
3386 /* With inheritance, a subclass may mask member names of parent classes, so
3387 * our search among the inheritance tree must be breadth-first rather than
3388 * depth-first (recursive). The parentdie is still our starting point. */
3389 deque
<Dwarf_Die
> inheritees(1, *parentdie
);
3390 for (; !inheritees
.empty(); inheritees
.pop_front())
3392 switch (dwarf_child (&inheritees
.front(), &die
))
3394 case 0: /* First child found. */
3396 case 1: /* No children. */
3398 case -1: /* Error. */
3399 default: /* Shouldn't happen */
3400 throw SEMANTIC_ERROR (dwarf_type_name(&inheritees
.front()) + ": "
3401 + string (dwarf_errmsg (-1)),
3407 int tag
= dwarf_tag(&die
);
3408 /* recurse into imported units as if they are anonymoust structs */
3410 if (tag
== DW_TAG_imported_unit
3411 && dwarf_attr_die(&die
, DW_AT_import
, &import
)
3412 && find_struct_member(c
, &import
, memberdie
, dies
, locs
))
3415 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3418 const char *name
= dwarf_diename(&die
);
3419 if (tag
== DW_TAG_inheritance
)
3421 /* Remember inheritee for breadth-first search. */
3422 Dwarf_Die inheritee
;
3423 if (dwarf_attr_die (&die
, DW_AT_type
, &inheritee
))
3424 inheritees
.push_back(inheritee
);
3426 else if (name
== NULL
)
3428 /* Need to recurse for anonymous structs/unions. */
3430 if (dwarf_attr_die (&die
, DW_AT_type
, &subdie
) &&
3431 find_struct_member(c
, &subdie
, memberdie
, dies
, locs
))
3434 else if (name
== c
.member
)
3440 while (dwarf_siblingof (&die
, &die
) == 0);
3446 /* As we unwind the recursion, we need to build the chain of
3447 * locations that got to the final answer. */
3448 if (dwarf_attr_integrate (&die
, DW_AT_data_member_location
, &attr
))
3450 dies
.insert(dies
.begin(), die
);
3451 locs
.insert(locs
.begin(), attr
);
3454 /* Union members don't usually have a location,
3455 * but just use the containing union's location. */
3456 else if (dwarf_tag(parentdie
) != DW_TAG_union_type
)
3457 throw SEMANTIC_ERROR (_F("no location for field '%s':%s",
3458 c
.member
.c_str(), dwarf_errmsg(-1)), c
.tok
);
3465 dwarf_die_type (Dwarf_Die
*die
, Dwarf_Die
*typedie_mem
, const token
*tok
=NULL
)
3467 if (!dwarf_attr_die (die
, DW_AT_type
, typedie_mem
))
3468 throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok
);
3473 dwflpp::translate_components(struct obstack
*pool
,
3474 struct location
**tail
,
3476 const target_symbol
*e
,
3482 while (i
< e
->components
.size())
3484 const target_symbol::component
& c
= e
->components
[i
];
3486 /* XXX: This would be desirable, but we don't get the target_symbol token,
3487 and printing that gives us the file:line number too early anyway. */
3489 // Emit a marker to note which field is being access-attempted, to give
3490 // better error messages if deref() fails.
3491 string piece
= string(...target_symbol token
...) + string ("#") + lex_cast(components
[i
].second
);
3492 obstack_printf (pool
, "c->last_stmt = %s;", lex_cast_qstring(piece
).c_str());
3495 switch (dwarf_tag (typedie
))
3497 case DW_TAG_typedef
:
3498 case DW_TAG_const_type
:
3499 case DW_TAG_volatile_type
:
3500 case DW_TAG_restrict_type
:
3501 /* Just iterate on the referent type. */
3502 dwarf_die_type (typedie
, typedie
, c
.tok
);
3505 case DW_TAG_reference_type
:
3506 case DW_TAG_rvalue_reference_type
:
3509 l2c_ctx
.die
= typedie
;
3510 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3512 dwarf_die_type (typedie
, typedie
, c
.tok
);
3515 case DW_TAG_pointer_type
:
3516 /* A pointer with no type is a void* -- can't dereference it. */
3517 if (!dwarf_hasattr_integrate (typedie
, DW_AT_type
))
3518 throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c
).c_str(),
3519 dwarf_type_name(typedie
).c_str()), c
.tok
);
3523 l2c_ctx
.die
= typedie
;
3524 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3526 if (c
.type
!= target_symbol::comp_literal_array_index
&&
3527 c
.type
!= target_symbol::comp_expression_array_index
)
3529 dwarf_die_type (typedie
, typedie
, c
.tok
);
3532 /* else fall through as an array access */
3534 case DW_TAG_array_type
:
3535 if (c
.type
== target_symbol::comp_literal_array_index
)
3539 l2c_ctx
.die
= typedie
;
3540 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3544 else if (c
.type
== target_symbol::comp_expression_array_index
)
3546 string index
= "STAP_ARG_index" + lex_cast(i
);
3549 l2c_ctx
.die
= typedie
;
3550 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3555 throw SEMANTIC_ERROR (_F("invalid access '%s' for array type",
3556 lex_cast(c
).c_str()), c
.tok
);
3558 dwarf_die_type (typedie
, typedie
, c
.tok
);
3563 case DW_TAG_structure_type
:
3564 case DW_TAG_union_type
:
3565 case DW_TAG_class_type
:
3566 if (c
.type
!= target_symbol::comp_struct_member
)
3567 throw SEMANTIC_ERROR (_F("invalid access '%s' for %s",
3568 lex_cast(c
).c_str(), dwarf_type_name(typedie
).c_str()));
3570 if (dwarf_hasattr(typedie
, DW_AT_declaration
))
3572 Dwarf_Die
*tmpdie
= declaration_resolve(typedie
);
3574 throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie
).c_str()), c
.tok
);
3579 vector
<Dwarf_Die
> dies
;
3580 vector
<Dwarf_Attribute
> locs
;
3581 if (!find_struct_member(c
, typedie
, vardie
, dies
, locs
))
3583 /* Add a file:line hint for anonymous types */
3585 if (!dwarf_hasattr_integrate(typedie
, DW_AT_name
))
3588 const char *file
= dwarf_decl_file(typedie
);
3589 if (file
&& dwarf_decl_line(typedie
, &line
) == 0)
3590 source
= " (" + string(file
) + ":"
3591 + lex_cast(line
) + ")";
3594 set
<string
> members
, member_dupes
;
3595 get_members(typedie
, members
, member_dupes
);
3596 string sugs
= levenshtein_suggest(c
.member
, members
);
3598 sugs
= " (alternatives: " + sugs
+ ")";
3599 throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c
.member
.c_str(),
3600 dwarf_type_name(typedie
).c_str(), source
.c_str(),
3601 sugs
.c_str()), c
.tok
);
3604 for (unsigned j
= 0; j
< locs
.size(); ++j
)
3606 translate_location (pool
, &locs
[j
], &dies
[j
],
3610 dwarf_die_type (vardie
, typedie
, c
.tok
);
3614 case DW_TAG_enumeration_type
:
3615 case DW_TAG_base_type
:
3616 throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c
).c_str(),
3617 dwarf_type_name(typedie
).c_str()), c
.tok
);
3621 throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c
.tok
);
3625 throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie
).c_str(),
3626 lex_cast(dwarf_tag(typedie
)).c_str()), c
.tok
);
3634 dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die
*typedie
,
3635 Dwarf_Die
*innerdie
,
3636 const target_symbol
*e
)
3638 int typetag
= dwarf_tag (typedie
);
3639 *innerdie
= *typedie
;
3640 while (typetag
== DW_TAG_typedef
||
3641 typetag
== DW_TAG_const_type
||
3642 typetag
== DW_TAG_volatile_type
||
3643 typetag
== DW_TAG_restrict_type
)
3645 if (!dwarf_attr_die (innerdie
, DW_AT_type
, innerdie
))
3646 throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e
->tok
);
3647 typetag
= dwarf_tag (innerdie
);
3653 dwflpp::translate_final_fetch_or_store (struct obstack
*pool
,
3654 struct location
**tail
,
3655 Dwarf_Addr
/*module_bias*/,
3657 Dwarf_Die
*start_typedie
,
3659 const target_symbol
*e
,
3664 /* First boil away any qualifiers associated with the type DIE of
3665 the final location to be accessed. */
3667 resolve_unqualified_inner_typedie (start_typedie
, typedie
, e
);
3669 /* If we're looking for an address, then we can just provide what
3670 we computed to this point, without using a fetch/store. */
3674 throw SEMANTIC_ERROR (_("cannot write to member address"), e
->tok
);
3676 if (dwarf_hasattr_integrate (vardie
, DW_AT_bit_offset
))
3677 throw SEMANTIC_ERROR (_("cannot take address of bit-field"), e
->tok
);
3679 l2c_ctx
.die
= vardie
;
3680 c_translate_addressof (pool
, 1, 0, vardie
, typedie
, tail
, "STAP_RETVALUE");
3684 /* Then switch behavior depending on the type of fetch/store we
3685 want, and the type and pointer-ness of the final location. */
3687 int typetag
= dwarf_tag (typedie
);
3691 throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s", lex_cast(typetag
).c_str(),
3692 dwarf_type_name(typedie
).c_str()), e
->tok
);
3695 case DW_TAG_structure_type
:
3696 case DW_TAG_class_type
:
3697 case DW_TAG_union_type
:
3698 throw SEMANTIC_ERROR (_F("'%s' is being accessed instead of a member",
3699 dwarf_type_name(typedie
).c_str()), e
->tok
);
3702 case DW_TAG_base_type
:
3704 // Reject types we can't handle in systemtap
3706 Dwarf_Attribute encoding_attr
;
3707 Dwarf_Word encoding
= (Dwarf_Word
) -1;
3708 dwarf_formudata (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &encoding_attr
),
3710 if (encoding
== (Dwarf_Word
) -1)
3712 // clog << "bad type1 " << encoding << " diestr" << endl;
3713 throw SEMANTIC_ERROR (_F("unsupported type (mystery encoding %s for %s", lex_cast(encoding
).c_str(),
3714 dwarf_type_name(typedie
).c_str()), e
->tok
);
3717 if (encoding
== DW_ATE_float
3718 || encoding
== DW_ATE_complex_float
3719 /* XXX || many others? */)
3721 // clog << "bad type " << encoding << " diestr" << endl;
3722 throw SEMANTIC_ERROR (_F("unsupported type (encoding %s) for %s", lex_cast(encoding
).c_str(),
3723 dwarf_type_name(typedie
).c_str()), e
->tok
);
3726 // Fallthrough. enumeration_types are always scalar.
3727 case DW_TAG_enumeration_type
:
3729 l2c_ctx
.die
= vardie
;
3731 c_translate_store (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3734 c_translate_fetch (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3738 case DW_TAG_array_type
:
3739 case DW_TAG_pointer_type
:
3740 case DW_TAG_reference_type
:
3741 case DW_TAG_rvalue_reference_type
:
3745 if (typetag
== DW_TAG_array_type
)
3746 throw SEMANTIC_ERROR (_("cannot write to array address"), e
->tok
);
3747 if (typetag
== DW_TAG_reference_type
||
3748 typetag
== DW_TAG_rvalue_reference_type
)
3749 throw SEMANTIC_ERROR (_("cannot write to reference"), e
->tok
);
3750 assert (typetag
== DW_TAG_pointer_type
);
3751 l2c_ctx
.die
= typedie
;
3752 c_translate_pointer_store (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3757 // We have the pointer: cast it to an integral type via &(*(...))
3759 // NB: per bug #1187, at one point char*-like types were
3760 // automagically converted here to systemtap string values.
3761 // For several reasons, this was taken back out, leaving
3762 // pointer-to-string "conversion" (copying) to tapset functions.
3764 l2c_ctx
.die
= typedie
;
3765 if (typetag
== DW_TAG_array_type
)
3766 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
, NULL
, 0);
3768 c_translate_pointer (pool
, 1, 0 /* PR9768 */, typedie
, tail
);
3769 c_translate_addressof (pool
, 1, 0 /* PR9768 */, NULL
, NULL
, tail
,
3776 postlude
+= " STAP_RETVALUE = STAP_ARG_value;\n";
3781 dwflpp::express_as_string (string prelude
,
3783 struct location
*head
)
3786 char *buf
= 0; // NB: it would leak to pre-allocate a buffer here
3787 FILE *memstream
= open_memstream (&buf
, &bufsz
);
3790 fprintf(memstream
, "{\n");
3791 fprintf(memstream
, "%s", prelude
.c_str());
3793 unsigned int stack_depth
;
3794 bool deref
= c_emit_location (memstream
, head
, 1, &stack_depth
);
3796 // Ensure that DWARF keeps loc2c to a "reasonable" stack size
3797 // 32 intptr_t leads to max 256 bytes on the stack
3798 if (stack_depth
> 32)
3799 throw SEMANTIC_ERROR("oversized DWARF stack");
3801 fprintf(memstream
, "%s", postlude
.c_str());
3802 fprintf(memstream
, " goto out;\n");
3804 // dummy use of deref_fault label, to disable warning if deref() not used
3805 fprintf(memstream
, "if (0) goto deref_fault;\n");
3807 // XXX: deref flag not reliable; emit fault label unconditionally
3812 fprintf(memstream
, "}\n");
3821 dwflpp::vardie_from_symtable (Dwarf_Die
*vardie
, Dwarf_Addr
*addr
)
3823 const char *name
= dwarf_linkage_name (vardie
) ?: dwarf_diename (vardie
);
3825 if (sess
.verbose
> 2)
3826 clog
<< _F("finding symtable address for %s\n", name
);
3829 int syms
= dwfl_module_getsymtab (module
);
3830 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
3832 for (int i
= 0; *addr
== 0 && i
< syms
; i
++)
3836 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
3838 && ! strcmp (name
, symname
)
3839 && sym
.st_shndx
!= SHN_UNDEF
3840 && (GELF_ST_TYPE (sym
.st_info
) == STT_NOTYPE
// PR13284
3841 || GELF_ST_TYPE (sym
.st_info
) == STT_OBJECT
))
3842 *addr
= sym
.st_value
;
3845 // Don't relocate for the kernel, or kernel modules we handle those
3846 // specially in emit_address.
3847 if (dwfl_module_relocations (module
) == 1 && module_name
!= TOK_KERNEL
)
3848 dwfl_module_relocate_address (module
, addr
);
3850 if (sess
.verbose
> 2)
3851 clog
<< _F("found %s @%#" PRIx64
"\n", name
, *addr
);
3857 dwflpp::literal_stmt_for_local (vector
<Dwarf_Die
>& scopes
,
3859 string
const & local
,
3860 const target_symbol
*e
,
3865 Dwarf_Attribute fb_attr_mem
, *fb_attr
= NULL
;
3867 // NB: when addr_loc is used for a synthesized DW_OP_addr below, then it
3868 // needs to remain valid until express_as_string() has finished with it.
3871 fb_attr
= find_variable_and_frame_base (scopes
, pc
, local
, e
,
3872 &vardie
, &fb_attr_mem
);
3877 clog
<< _F("finding location for local '%s' near address %#" PRIx64
3878 ", module bias %#" PRIx64
"\n", local
.c_str(), pc
,
3881 clog
<< _F("finding location for global '%s' in CU '%s'\n",
3882 local
.c_str(), cu_name().c_str());
3885 struct obstack pool
;
3886 obstack_tracker
p (&pool
);
3888 struct location
*tail
= NULL
;
3890 /* Given $foo->bar->baz[NN], translate the location of foo. */
3892 struct location
*head
;
3894 Dwarf_Attribute attr_mem
;
3895 if (dwarf_attr_integrate (&vardie
, DW_AT_const_value
, &attr_mem
) == NULL
3896 && dwarf_attr_integrate (&vardie
, DW_AT_location
, &attr_mem
) == NULL
)
3898 memset(&addr_loc
, 0, sizeof(Dwarf_Op
));
3899 addr_loc
.atom
= DW_OP_addr
;
3900 // If it is an external variable try the symbol table. PR10622.
3901 if (dwarf_attr_integrate (&vardie
, DW_AT_external
, &attr_mem
) != NULL
3902 && vardie_from_symtable (&vardie
, &addr_loc
.number
) != 0)
3905 l2c_ctx
.die
= &vardie
;
3906 head
= c_translate_location (&pool
, &loc2c_error
, this,
3907 &loc2c_emit_address
,
3909 NULL
, &addr_loc
, 1, &tail
, NULL
, NULL
);
3913 string msg
= _F("failed to retrieve location attribute for '%s' [man error::dwarf]", local
.c_str());
3914 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3915 err
.details
.push_back(die_location_as_string(pc
, &vardie
));
3916 err
.details
.push_back(die_location_as_function_string(pc
, &vardie
));
3921 head
= translate_location (&pool
, &attr_mem
, &vardie
, pc
, fb_attr
, &tail
, e
);
3923 /* Translate the ->bar->baz[NN] parts. */
3926 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
3928 string msg
= _F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str());
3929 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3930 err
.details
.push_back(die_location_as_string(pc
, &vardie
));
3931 err
.details
.push_back(die_location_as_function_string(pc
, &vardie
));
3935 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
3937 /* Translate the assignment part, either
3938 x = $foo->bar->baz[NN]
3940 $foo->bar->baz[NN] = x
3943 string prelude
, postlude
;
3944 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
3945 &vardie
, &typedie
, lvalue
, e
,
3946 prelude
, postlude
, die_mem
);
3948 /* Write the translation to a string. */
3949 string result
= express_as_string(prelude
, postlude
, head
);
3954 dwflpp::type_die_for_local (vector
<Dwarf_Die
>& scopes
,
3956 string
const & local
,
3957 const target_symbol
*e
,
3961 Dwarf_Attribute attr_mem
;
3963 find_variable_and_frame_base (scopes
, pc
, local
, e
, &vardie
, &attr_mem
);
3965 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
3966 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str()), e
->tok
);
3968 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
3974 dwflpp::literal_stmt_for_return (Dwarf_Die
*scope_die
,
3976 const target_symbol
*e
,
3981 clog
<< _F("literal_stmt_for_return: finding return value for %s (%s)\n",
3982 (dwarf_diename(scope_die
) ?: "<unknown>"), (dwarf_diename(cu
) ?: "<unknown>"));
3984 struct obstack pool
;
3985 obstack_tracker
p (&pool
);
3986 struct location
*tail
= NULL
;
3988 /* Given $return->bar->baz[NN], translate the location of return. */
3989 const Dwarf_Op
*locops
;
3990 int nlocops
= dwfl_module_return_value_location (module
, scope_die
,
3993 throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)",
3994 (dwarf_diename(scope_die
) ?: "<unknown>"),
3995 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3996 // the function has no return value (e.g. "void" in C)
3997 else if (nlocops
== 0)
3998 throw SEMANTIC_ERROR(_F("function %s (%s) has no return value",
3999 (dwarf_diename(scope_die
) ?: "<unknown>"),
4000 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4003 l2c_ctx
.die
= scope_die
;
4004 struct location
*head
= c_translate_location (&pool
, &loc2c_error
, this,
4005 &loc2c_emit_address
,
4007 pc
, NULL
, locops
, nlocops
,
4010 /* Translate the ->bar->baz[NN] parts. */
4012 Dwarf_Die vardie
= *scope_die
, typedie
;
4013 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
4014 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4015 (dwarf_diename(&vardie
) ?: "<unknown>"),
4016 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4018 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
4020 /* Translate the assignment part, either
4021 x = $return->bar->baz[NN]
4023 $return->bar->baz[NN] = x
4026 string prelude
, postlude
;
4027 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
4028 &vardie
, &typedie
, lvalue
, e
,
4029 prelude
, postlude
, die_mem
);
4031 /* Write the translation to a string. */
4032 string result
= express_as_string(prelude
, postlude
, head
);
4037 dwflpp::type_die_for_return (Dwarf_Die
*scope_die
,
4039 const target_symbol
*e
,
4042 Dwarf_Die vardie
= *scope_die
;
4043 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
4044 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4045 (dwarf_diename(&vardie
) ?: "<unknown>"),
4046 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4048 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
4054 dwflpp::literal_stmt_for_pointer (Dwarf_Die
*start_typedie
,
4055 const target_symbol
*e
,
4060 clog
<< _F("literal_stmt_for_pointer: finding value for %s (%s)\n",
4061 dwarf_type_name(start_typedie
).c_str(), (dwarf_diename(cu
) ?: "<unknown>"));
4063 struct obstack pool
;
4064 obstack_tracker
p (&pool
);
4066 l2c_ctx
.die
= start_typedie
;
4067 struct location
*head
= c_translate_argument (&pool
, &loc2c_error
, this,
4068 &loc2c_emit_address
,
4069 1, "STAP_ARG_pointer");
4070 struct location
*tail
= head
;
4072 /* Translate the ->bar->baz[NN] parts. */
4075 Dwarf_Die typedie
= *start_typedie
, vardie
= typedie
;
4077 /* As a special case when typedie is not an array or pointer, we can
4078 * allow array indexing on STAP_ARG_pointer instead (since we do
4079 * know the pointee type and can determine its size). PR11556. */
4080 const target_symbol::component
* c
=
4081 e
->components
.empty() ? NULL
: &e
->components
[0];
4082 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
4083 c
->type
== target_symbol::comp_expression_array_index
))
4085 resolve_unqualified_inner_typedie (&typedie
, &typedie
, e
);
4086 int typetag
= dwarf_tag (&typedie
);
4087 if (typetag
!= DW_TAG_pointer_type
&&
4088 typetag
!= DW_TAG_array_type
)
4090 l2c_ctx
.die
= &typedie
;
4091 if (c
->type
== target_symbol::comp_literal_array_index
)
4092 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, NULL
, c
->num_index
);
4094 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, "STAP_ARG_index0", 0);
4099 /* Now translate the rest normally. */
4101 translate_components (&pool
, &tail
, 0, e
, &vardie
, &typedie
, first
);
4103 /* Translate the assignment part, either
4104 x = (STAP_ARG_pointer)->bar->baz[NN]
4106 (STAP_ARG_pointer)->bar->baz[NN] = x
4109 string prelude
, postlude
;
4110 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
4111 &vardie
, &typedie
, lvalue
, e
,
4112 prelude
, postlude
, die_mem
);
4114 /* Write the translation to a string. */
4115 string result
= express_as_string(prelude
, postlude
, head
);
4120 dwflpp::type_die_for_pointer (Dwarf_Die
*start_typedie
,
4121 const target_symbol
*e
,
4125 *typedie
= *start_typedie
;
4126 Dwarf_Die vardie
= *typedie
;
4128 /* Handle the same PR11556 case as above. */
4129 const target_symbol::component
* c
=
4130 e
->components
.empty() ? NULL
: &e
->components
[0];
4131 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
4132 c
->type
== target_symbol::comp_expression_array_index
))
4134 resolve_unqualified_inner_typedie (typedie
, typedie
, e
);
4135 int typetag
= dwarf_tag (typedie
);
4136 if (typetag
!= DW_TAG_pointer_type
&&
4137 typetag
!= DW_TAG_array_type
)
4141 translate_components (NULL
, NULL
, 0, e
, &vardie
, typedie
, first
);
4147 in_kprobes_function(systemtap_session
& sess
, Dwarf_Addr addr
)
4149 if (sess
.sym_kprobes_text_start
!= 0 && sess
.sym_kprobes_text_end
!= 0)
4151 // If the probe point address is anywhere in the __kprobes
4152 // address range, we can't use this probe point.
4153 if (addr
>= sess
.sym_kprobes_text_start
&& addr
< sess
.sym_kprobes_text_end
)
4160 enum dwflpp::blacklisted_type
4161 dwflpp::blacklisted_p(interned_string funcname
,
4162 interned_string filename
,
4164 interned_string module
,
4168 if (!blacklist_enabled
)
4169 return dwflpp::blacklisted_none
;
4171 enum dwflpp::blacklisted_type blacklisted
= dwflpp::blacklisted_none
;
4173 // check against section blacklist
4174 string section
= get_blacklist_section(addr
);
4176 // PR6503: modules don't need special init/exit treatment
4177 if (module
== TOK_KERNEL
&& !regexec (&blacklist_section
, section
.c_str(), 0, NULL
, 0))
4178 blacklisted
= dwflpp::blacklisted_section
;
4180 // Check for function marked '__kprobes'.
4181 else if (module
== TOK_KERNEL
&& in_kprobes_function(sess
, addr
))
4182 blacklisted
= dwflpp::blacklisted_kprobes
;
4184 // Check probe point against function blacklist
4185 else if (!regexec(&blacklist_func
, funcname
.to_string().c_str(), 0, NULL
, 0))
4186 blacklisted
= dwflpp::blacklisted_function
;
4188 // Check probe point against function return blacklist
4189 else if (has_return
&& !regexec(&blacklist_func_ret
, funcname
.to_string().c_str(), 0, NULL
, 0))
4190 blacklisted
= dwflpp::blacklisted_function_return
;
4192 // Check probe point against file blacklist
4193 else if (!regexec(&blacklist_file
, filename
.to_string().c_str(), 0, NULL
, 0))
4194 blacklisted
= dwflpp::blacklisted_file
;
4199 clog
<< _(" - blacklisted");
4202 blacklisted
= dwflpp::blacklisted_none
;
4204 clog
<< _(" but not skipped (guru mode enabled)");
4208 // This probe point is not blacklisted.
4214 dwflpp::build_kernel_blacklist()
4216 // We build up the regexps in these strings
4218 // Add ^ anchors at the front; $ will be added just before regcomp.
4221 string blfn_ret
= "^(";
4222 string blfile
= "^(";
4223 string blsection
= "^(";
4225 blsection
+= "\\.init\\."; // first alternative, no "|"
4226 blsection
+= "|\\.exit\\.";
4227 blsection
+= "|\\.devinit\\.";
4228 blsection
+= "|\\.devexit\\.";
4229 blsection
+= "|\\.cpuinit\\.";
4230 blsection
+= "|\\.cpuexit\\.";
4231 blsection
+= "|\\.meminit\\.";
4232 blsection
+= "|\\.memexit\\.";
4234 /* NOTE all include/asm .h blfile patterns might need "full path"
4235 so prefix those with '.*' - see PR13108 and PR13112. */
4236 blfile
+= "kernel/kprobes\\.c"; // first alternative, no "|"
4237 blfile
+= "|arch/.*/kernel/kprobes\\.c";
4238 blfile
+= "|.*/include/asm/io\\.h";
4239 blfile
+= "|.*/include/asm/io_64\\.h";
4240 blfile
+= "|.*/include/asm/bitops\\.h";
4241 blfile
+= "|drivers/ide/ide-iops\\.c";
4243 blfile
+= "|arch/.*/kernel/paravirt\\.c";
4244 blfile
+= "|.*/include/asm/paravirt\\.h";
4246 // XXX: it would be nice if these blacklisted functions were pulled
4247 // in dynamically, instead of being statically defined here.
4248 // Perhaps it could be populated from script files. A "noprobe
4249 // kernel.function("...")" construct might do the trick.
4251 // Most of these are marked __kprobes in newer kernels. We list
4252 // them here (anyway) so the translator can block them on older
4253 // kernels that don't have the __kprobes function decorator. This
4254 // also allows detection of problems at translate- rather than
4257 blfn
+= "atomic_notifier_call_chain"; // first blfn; no "|"
4258 blfn
+= "|default_do_nmi";
4261 blfn
+= "|do_debug";
4262 blfn
+= "|do_general_protection";
4265 blfn
+= "|do_page_fault";
4266 blfn
+= "|do_sparc64_fault";
4268 blfn
+= "|dummy_nmi_callback";
4269 blfn
+= "|flush_icache_range";
4270 blfn
+= "|ia64_bad_break";
4271 blfn
+= "|ia64_do_page_fault";
4272 blfn
+= "|ia64_fault";
4273 blfn
+= "|io_check_error";
4274 blfn
+= "|mem_parity_error";
4275 blfn
+= "|nmi_watchdog_tick";
4276 blfn
+= "|notifier_call_chain";
4277 blfn
+= "|oops_begin";
4278 blfn
+= "|oops_end";
4279 blfn
+= "|program_check_exception";
4280 blfn
+= "|single_step_exception";
4281 blfn
+= "|sync_regs";
4282 blfn
+= "|unhandled_fault";
4283 blfn
+= "|unknown_nmi_error";
4284 blfn
+= "|xen_[gs]et_debugreg";
4285 blfn
+= "|xen_irq_.*";
4286 blfn
+= "|xen_.*_fl_direct.*";
4287 blfn
+= "|check_events";
4288 blfn
+= "|xen_adjust_exception_frame";
4289 blfn
+= "|xen_iret.*";
4290 blfn
+= "|xen_sysret64.*";
4291 blfn
+= "|test_ti_thread_flag.*";
4292 blfn
+= "|inat_get_opcode_attribute";
4293 blfn
+= "|system_call_after_swapgs";
4294 blfn
+= "|HYPERVISOR_[gs]et_debugreg";
4295 blfn
+= "|HYPERVISOR_event_channel_op";
4297 blfn
+= "|hash_ptr";
4298 blfn
+= "|native_set_pte";
4301 blfn
+= "|.*raw_.*_lock.*";
4302 blfn
+= "|.*raw_.*_unlock.*";
4303 blfn
+= "|.*raw_.*_trylock.*";
4304 blfn
+= "|.*read_lock.*";
4305 blfn
+= "|.*read_unlock.*";
4306 blfn
+= "|.*read_trylock.*";
4307 blfn
+= "|.*write_lock.*";
4308 blfn
+= "|.*write_unlock.*";
4309 blfn
+= "|.*write_trylock.*";
4310 blfn
+= "|.*write_seqlock.*";
4311 blfn
+= "|.*write_sequnlock.*";
4312 blfn
+= "|.*spin_lock.*";
4313 blfn
+= "|.*spin_unlock.*";
4314 blfn
+= "|.*spin_trylock.*";
4315 blfn
+= "|.*spin_is_locked.*";
4316 blfn
+= "|rwsem_.*lock.*";
4317 blfn
+= "|.*mutex_.*lock.*";
4321 blfn
+= "|atomic_.*";
4322 blfn
+= "|atomic64_.*";
4324 // few other problematic cases
4329 blfn
+= "|.*apic.*|.*APIC.*";
4330 blfn
+= "|.*softirq.*";
4332 blfn
+= "|.*_intr.*";
4334 blfn
+= "|.*kernel_text.*";
4335 blfn
+= "|get_current";
4336 blfn
+= "|current_.*";
4337 blfn
+= "|.*exception_tables.*";
4338 blfn
+= "|.*setup_rt_frame.*";
4340 // PR 5759, CONFIG_PREEMPT kernels
4341 blfn
+= "|.*preempt_count.*";
4342 blfn
+= "|preempt_schedule";
4344 // These functions don't return, so return probes would never be recovered
4345 blfn_ret
+= "do_exit"; // no "|"
4346 blfn_ret
+= "|sys_exit";
4347 blfn_ret
+= "|sys_exit_group";
4349 // __switch_to changes "current" on x86_64 and i686, so return probes
4350 // would cause kernel panic, and it is marked as "__kprobes" on x86_64
4351 if (sess
.architecture
== "x86_64")
4352 blfn
+= "|__switch_to";
4353 if (sess
.architecture
== "i686")
4354 blfn_ret
+= "|__switch_to";
4356 // RHEL6 pre-beta 2.6.32-19.el6
4357 blfn
+= "|special_mapping_.*";
4358 blfn
+= "|.*_pte_.*"; // or "|smaps_pte_range";
4359 blfile
+= "|fs/seq_file\\.c";
4364 blsection
+= ")"; // NB: no $, sections match just the beginning
4366 if (sess
.verbose
> 2)
4368 clog
<< _("blacklist regexps:") << endl
;
4369 clog
<< "blfn: " << blfn
<< endl
;
4370 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4371 clog
<< "blfile: " << blfile
<< endl
;
4372 clog
<< "blsection: " << blsection
<< endl
;
4375 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4376 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4377 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4378 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4379 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4380 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4381 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4382 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4384 blacklist_enabled
= true;
4389 dwflpp::build_user_blacklist()
4391 // We build up the regexps in these strings
4393 // Add ^ anchors at the front; $ will be added just before regcomp.
4396 string blfn_ret
= "^(";
4397 string blfile
= "^(";
4398 string blsection
= "^(";
4400 // Non-matching placeholders until we have real things to match
4405 // These functions don't use the normal function-entry ABI, so can't be .return probed safely
4406 blfn_ret
+= "_start";
4411 blsection
+= ")"; // NB: no $, sections match just the beginning
4413 if (sess
.verbose
> 2)
4415 clog
<< _("blacklist regexps:") << endl
;
4416 clog
<< "blfn: " << blfn
<< endl
;
4417 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4418 clog
<< "blfile: " << blfile
<< endl
;
4419 clog
<< "blsection: " << blsection
<< endl
;
4422 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4423 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4424 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4425 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4426 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4427 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4428 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4429 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4431 blacklist_enabled
= true;
4436 dwflpp::get_blacklist_section(Dwarf_Addr addr
)
4438 string blacklist_section
;
4440 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
4441 // because dwfl_module_getelf can force costly section relocations
4442 // we don't really need, while either will do for this purpose.
4443 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (module
, &bias
))
4444 ?: dwfl_module_getelf (module
, &bias
));
4446 Dwarf_Addr offset
= addr
- bias
;
4451 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4452 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
4455 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
4457 continue; // XXX error?
4459 if (!(shdr
->sh_flags
& SHF_ALLOC
))
4462 GElf_Addr start
= shdr
->sh_addr
;
4463 GElf_Addr end
= start
+ shdr
->sh_size
;
4464 if (! (offset
>= start
&& offset
< end
))
4467 blacklist_section
= elf_strptr (elf
, shstrndx
, shdr
->sh_name
);
4471 return blacklist_section
;
4475 /* Find the section named 'section_name' in the current module
4476 * returning the section header using 'shdr_mem' */
4479 dwflpp::get_section(string section_name
, GElf_Shdr
*shdr_mem
, Elf
**elf_ret
)
4481 GElf_Shdr
*shdr
= NULL
;
4486 // Explicitly look in the main elf file first.
4487 elf
= dwfl_module_getelf (module
, &bias
);
4488 Elf_Scn
*probe_scn
= NULL
;
4490 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4492 bool have_section
= false;
4494 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4496 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4497 assert (shdr
!= NULL
);
4499 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4501 have_section
= true;
4506 // Older versions may put the section in the debuginfo dwarf file,
4507 // so check if it actually exists, if not take a look in the debuginfo file
4508 if (! have_section
|| (have_section
&& shdr
->sh_type
== SHT_NOBITS
))
4510 elf
= dwarf_getelf (dwfl_module_getdwarf (module
, &bias
));
4513 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4515 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4517 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4518 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4520 have_section
= true;
4536 dwflpp::relocate_address(Dwarf_Addr dw_addr
, interned_string
& reloc_section
)
4539 // libdw address, so adjust for bias gotten from dwfl_module_getdwarf
4540 Dwarf_Addr reloc_addr
= dw_addr
+ module_bias
;
4543 assert(module_name
== TOK_KERNEL
);
4546 else if (dwfl_module_relocations (module
) > 0)
4548 // This is a relocatable module; libdwfl already knows its
4549 // sections, so we can relativize addr.
4550 int idx
= dwfl_module_relocate_address (module
, &reloc_addr
);
4551 const char* r_s
= dwfl_module_relocation_info (module
, idx
, NULL
);
4553 reloc_section
= r_s
;
4555 if (reloc_section
== "" && dwfl_module_relocations (module
) == 1)
4556 reloc_section
= ".dynamic";
4559 reloc_section
= ".absolute";
4563 /* Returns the call frame address operations for the given program counter
4564 * in the libdw address space.
4567 dwflpp::get_cfa_ops (Dwarf_Addr pc
)
4569 Dwarf_Op
*cfa_ops
= NULL
;
4571 if (sess
.verbose
> 2)
4572 clog
<< "get_cfa_ops @0x" << hex
<< pc
<< dec
4573 << ", module_start @0x" << hex
<< module_start
<< dec
<< endl
;
4575 // Try debug_frame first, then fall back on eh_frame.
4576 size_t cfa_nops
= 0;
4577 Dwarf_Addr bias
= 0;
4578 Dwarf_Frame
*frame
= NULL
;
4579 Dwarf_CFI
*cfi
= dwfl_module_dwarf_cfi (module
, &bias
);
4582 if (sess
.verbose
> 3)
4583 clog
<< "got dwarf cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4584 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4585 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4586 else if (sess
.verbose
> 3)
4587 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4589 else if (sess
.verbose
> 3)
4590 clog
<< "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl
;
4592 if (cfa_ops
== NULL
)
4594 cfi
= dwfl_module_eh_cfi (module
, &bias
);
4597 if (sess
.verbose
> 3)
4598 clog
<< "got eh cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4599 Dwarf_Frame
*frame
= NULL
;
4600 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4601 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4602 else if (sess
.verbose
> 3)
4603 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4605 else if (sess
.verbose
> 3)
4606 clog
<< "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl
;
4610 if (sess
.verbose
> 2)
4612 if (cfa_ops
== NULL
)
4613 clog
<< _("not found cfa") << endl
;
4616 Dwarf_Addr frame_start
, frame_end
;
4618 int info
= dwarf_frame_info (frame
, &frame_start
, &frame_end
,
4620 clog
<< _F("found cfa, info: %d [start: %#" PRIx64
", end: %#" PRIx64
4621 ", nops: %zu", info
, frame_start
, frame_end
, cfa_nops
) << endl
;
4629 dwflpp::add_module_build_id_to_hash (Dwfl_Module
*m
,
4630 void **userdata
__attribute__ ((unused
)),
4635 string modname
= name
;
4636 systemtap_session
* s
= (systemtap_session
*)arg
;
4637 if (pending_interrupts
)
4638 return DWARF_CB_ABORT
;
4640 // Extract the build ID
4641 const unsigned char *bits
;
4643 int bits_length
= dwfl_module_build_id(m
, &bits
, &vaddr
);
4646 // Convert the binary bits to a hex string
4647 string hex
= hex_dump(bits
, bits_length
);
4649 // Store the build ID in the session
4650 s
->build_ids
.push_back(hex
);
4658 // Perform PR15123 heuristic for given variable at given address.
4659 // Return alternate pc address to do location-list lookup at, or 0 if
4663 dwflpp::pr15123_retry_addr (Dwarf_Addr pc
, Dwarf_Die
* die
)
4665 // For PR15123, we'd like to detect the situation where the
4666 // incoming PC may point to a couple-of-byte instruction
4667 // sequence that gcc emits for CFLAGS=-mfentry, and where
4668 // context variables are in fact available throughout, *but* due
4669 // to the bug, the dwarf debuginfo location-list only starts a
4670 // few instructions later. Prologue searching does not resolve
4671 // this as a line-record is in place at the -mfentry prologue.
4673 // Detecting this is complicated because ...
4674 // - we only want to do this if -mfentry was actually used
4675 // - if <pc> points to the a function entry point
4676 // - if the architecture is familiar enough that we can have a
4677 // hard-coded constant to skip over the prologue.
4679 // Otherwise, we could give a false-positive - return corrupted
4682 // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches
4683 // was used. Without it, set the PR15123_ASSUME_MFENTRY environment
4684 // variable to override the -mfentry test.
4686 if (getenv ("PR15123_DISABLE"))
4689 if (!getenv ("PR15123_ASSUME_MFENTRY")) {
4691 string producer
, version
;
4692 dwarf_diecu (die
, &cudie
, NULL
, NULL
);
4693 if (!is_gcc_producer(&cudie
, producer
, version
))
4695 if (producer
.find("-mfentry") == string::npos
)
4699 // Determine if this pc maps to the beginning of a
4700 // real function (not some inlined doppelganger. This
4701 // is made tricker by this->function may not be
4702 // pointing at the right DIE (say e.g. stap encountered
4703 // the inlined copy first, so was focus_on_function'd).
4704 vector
<Dwarf_Die
> scopes
= getscopes(pc
);
4705 if (scopes
.size() == 0)
4708 Dwarf_Die outer_function_die
= scopes
[0];
4710 if (!die_entrypc(& outer_function_die
, &entrypc
) || entrypc
!= pc
)
4711 return 0; // (will fail on retry, so we won't loop more than once)
4713 if (sess
.architecture
== "i386" ||
4714 sess
.architecture
== "x86_64") {
4715 /* pull the trigger */
4716 if (sess
.verbose
> 2)
4717 clog
<< _("retrying variable location-list lookup at address pc+5\n");
4725 dwflpp::has_gnu_debugdata ()
4727 Dwarf_Addr load_addr
;
4728 // Note we really want the actual elf file, not the dwarf .debug file.
4729 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
4731 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
4733 // Get the gnu_debugdata section header
4734 Elf_Scn
*scn
= NULL
;
4735 GElf_Shdr
*gnu_debugdata_shdr
= NULL
;
4736 GElf_Shdr gnu_debugdata_shdr_mem
;
4737 while ((scn
= elf_nextscn (elf
, scn
)))
4739 gnu_debugdata_shdr
= gelf_getshdr (scn
, &gnu_debugdata_shdr_mem
);
4740 assert (gnu_debugdata_shdr
!= NULL
);
4741 if (strcmp (elf_strptr (elf
, shstrndx
, gnu_debugdata_shdr
->sh_name
), ".gnu_debugdata") == 0)
4747 // If not GCC, return false. Otherwise, return true and set vers.
4749 dwflpp::is_gcc_producer(Dwarf_Die
*cudie
, string
& producer
, string
& version
)
4751 Dwarf_Attribute producer_attr
;
4752 if (!dwarf_attr_integrate(cudie
, DW_AT_producer
, &producer_attr
))
4755 // GNU {C|C++|...} x.x.x YYYYMMDD ...
4756 const char *cproducer
= dwarf_formstring(&producer_attr
);
4759 producer
= cproducer
;
4761 vector
<string
> tokens
;
4762 tokenize(producer
, tokens
);
4764 if (tokens
.size() < 3
4765 || tokens
[0] != "GNU"
4766 || tokens
[2].find_first_not_of(".0123456789") != string::npos
)
4769 version
= tokens
[2];
4774 die_has_loclist(Dwarf_Die
*begin_die
)
4777 Dwarf_Attribute loc
;
4779 if (dwarf_child(begin_die
, &die
) != 0)
4784 switch (dwarf_tag(&die
))
4786 case DW_TAG_formal_parameter
:
4787 case DW_TAG_variable
:
4788 if (dwarf_attr_integrate(&die
, DW_AT_location
, &loc
)
4789 && dwarf_whatform(&loc
) == DW_FORM_sec_offset
)
4793 if (dwarf_haschildren (&die
))
4794 if (die_has_loclist(&die
))
4799 while (dwarf_siblingof (&die
, &die
) == 0);
4805 dwflpp::has_valid_locs ()
4809 // The current CU has valid location info (implying we do not need to skip the
4811 // - it was compiled with -O2 -g (in which case, GCC outputs proper location
4812 // info for the prologue), and
4813 // - it was compiled by GCC >= 4.5 (previous versions could have had invalid
4814 // debug info in the prologue, see GDB's PR13777)
4815 // Note that clang behaves similarly to GCC here: non-optimized code does not
4816 // have location lists, while optimized code does. In the check below, even if
4817 // the producer is not GCC, we assume that it is valid to do the loclist check
4818 // afterwards (which it is for clang).
4821 if (is_gcc_producer(cu
, prod
, vers
)
4822 && strverscmp(vers
.c_str(), "4.5") < 0)
4825 // We determine if the current CU has been optimized with -O2 -g by looking
4826 // for any data objects whose DW_AT_location is a location list. This is also
4827 // how GDB determines whether to skip the prologue or not. See GDB's PR12573
4828 // and also RHBZ612253#c6.
4829 if (!die_has_loclist(cu
))
4832 if (sess
.verbose
> 2)
4833 clog
<< _F("CU '%s' in module '%s' has valid locs",
4834 cu_name().c_str(), module_name
.c_str()) << endl
;
4839 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */