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 dwflpp::dwflpp(systemtap_session
& session
, const string
& name
, bool kernel_p
):
78 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
79 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
80 module_dwarf(NULL
), function(NULL
), blacklist_func(), blacklist_func_ret(),
81 blacklist_file(), blacklist_enabled(false)
84 setup_kernel(name
, session
);
87 vector
<string
> modules
;
88 modules
.push_back(name
);
93 dwflpp::dwflpp(systemtap_session
& session
, const vector
<string
>& names
,
95 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
96 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
97 module_dwarf(NULL
), function(NULL
), blacklist_enabled(false)
107 delete_map(module_cu_cache
);
108 delete_map(cu_function_cache
);
109 delete_map(mod_function_cache
);
110 delete_map(cu_inl_function_cache
);
111 delete_map(global_alias_cache
);
112 delete_map(cu_die_parent_cache
);
114 cu_lines_cache_t::iterator i
;
115 for (i
= cu_lines_cache
.begin(); i
!= cu_lines_cache
.end(); ++i
)
116 delete_map(*i
->second
);
117 delete_map(cu_lines_cache
);
121 // NB: don't "delete mod_info;", as that may be shared
122 // between dwflpp instances, and are stored in
123 // session.module_cache[] anyway.
127 module_cache::~module_cache ()
134 dwflpp::get_module_dwarf(bool required
, bool report
)
136 module_dwarf
= dwfl_module_getdwarf(module
, &module_bias
);
137 mod_info
->dwarf_status
= (module_dwarf
? info_present
: info_absent
);
138 if (!module_dwarf
&& report
)
140 string msg
= _("cannot find ");
141 if (module_name
== "")
144 msg
+= string("module ") + module_name
;
147 int i
= dwfl_errno();
149 msg
+= string(": ") + dwfl_errmsg (i
);
151 msg
+= " [man warning::debuginfo]";
153 /* add module_name to list to find rpm */
154 find_debug_rpms(sess
, module_name
.c_str());
157 throw SEMANTIC_ERROR (msg
);
159 sess
.print_warning(msg
);
165 dwflpp::focus_on_module(Dwfl_Module
* m
, module_info
* mi
)
171 module_name
= dwfl_module_info(module
, NULL
, &module_start
, &module_end
,
172 NULL
, NULL
, NULL
, NULL
) ?: "module";
176 assert(mi
&& mi
->name
&& mi
->name
== TOK_KERNEL
);
177 module_name
= mi
->name
;
180 module_bias
= mi
->bias
;
183 // Reset existing pointers and names
189 function_name
.clear();
195 dwflpp::focus_on_cu(Dwarf_Die
* c
)
202 // Reset existing pointers and names
203 function_name
.clear();
209 dwflpp::cu_name(void)
211 return dwarf_diename(cu
) ?: "<unknown source>";
216 dwflpp::focus_on_function(Dwarf_Die
* f
)
223 function_name
= dwarf_diename(function
) ?: "function";
227 /* Return the Dwarf_Die for the given address in the current module.
228 * The address should be in the module address address space (this
229 * function will take care of any dw bias).
232 dwflpp::query_cu_containing_address(Dwarf_Addr a
)
239 Dwarf_Die
* cudie
= dwfl_module_addrdie(module
, a
, &bias
);
240 assert(bias
== module_bias
);
246 dwflpp::module_name_matches(const string
& pattern
)
248 bool t
= (fnmatch(pattern
.c_str(), module_name
.c_str(), 0) == 0);
249 if (t
&& sess
.verbose
>3)
250 clog
<< _F("pattern '%s' matches module '%s'\n",
251 pattern
.c_str(), module_name
.c_str());
252 if (!t
&& sess
.verbose
>4)
253 clog
<< _F("pattern '%s' does not match module '%s'\n",
254 pattern
.c_str(), module_name
.c_str());
261 dwflpp::name_has_wildcard (const string
& pattern
)
263 return (pattern
.find('*') != string::npos
||
264 pattern
.find('?') != string::npos
||
265 pattern
.find('[') != string::npos
);
270 dwflpp::module_name_final_match(const string
& pattern
)
272 // Assume module_name_matches(). Can there be any more matches?
273 // Not unless the pattern is a wildcard, since module names are
275 return !name_has_wildcard(pattern
);
280 dwflpp::function_name_matches_pattern(const string
& name
, const string
& pattern
)
282 bool t
= (fnmatch(pattern
.c_str(), name
.c_str(), 0) == 0);
283 if (t
&& sess
.verbose
>3)
284 clog
<< _F("pattern '%s' matches function '%s'\n", pattern
.c_str(), name
.c_str());
290 dwflpp::function_name_matches(const string
& pattern
)
293 return function_name_matches_pattern(function_name
, pattern
);
298 dwflpp::function_scope_matches(const vector
<string
>& scopes
)
300 // walk up the containing scopes
301 Dwarf_Die
* die
= function
;
302 for (int i
= scopes
.size() - 1; i
>= 0; --i
)
304 die
= get_parent_scope(die
);
306 // check if this scope matches, and prepend it if so
307 // NB: a NULL die is the global scope, compared as ""
308 string name
= dwarf_diename(die
) ?: "";
309 if (name_has_wildcard(scopes
[i
]) ?
310 function_name_matches_pattern(name
, scopes
[i
]) :
312 function_name
= name
+ "::" + function_name
;
316 // make sure there's no more if we're at the global scope
325 dwflpp::setup_kernel(const string
& name
, systemtap_session
& s
, bool debuginfo_needed
)
327 if (! sess
.module_cache
)
328 sess
.module_cache
= new module_cache ();
330 unsigned offline_search_matches
= 0;
331 dwfl
= setup_dwfl_kernel(name
, &offline_search_matches
, sess
);
333 if (offline_search_matches
< 1)
335 if (debuginfo_needed
) {
336 // Suggest a likely kernel dir to find debuginfo rpm for
337 string dir
= string(sess
.sysroot
+ "/lib/modules/" + sess
.kernel_release
);
338 find_debug_rpms(sess
, dir
.c_str());
340 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
341 sess
.architecture
.c_str(), sess
.kernel_build_tree
.c_str()));
349 assert_no_interrupts();
350 off
= dwfl_getmodules (dwfl
, &add_module_build_id_to_hash
, &s
, off
);
353 DWFL_ASSERT("dwfl_getmodules", off
== 0);
356 build_kernel_blacklist();
360 dwflpp::setup_kernel(const vector
<string
> &names
, bool debuginfo_needed
)
362 if (! sess
.module_cache
)
363 sess
.module_cache
= new module_cache ();
365 unsigned offline_search_matches
= 0;
366 set
<string
> offline_search_names(names
.begin(), names
.end());
367 dwfl
= setup_dwfl_kernel(offline_search_names
,
368 &offline_search_matches
,
371 if (offline_search_matches
< offline_search_names
.size())
373 if (debuginfo_needed
) {
374 // Suggest a likely kernel dir to find debuginfo rpm for
375 string dir
= string(sess
.sysroot
+ "/lib/modules/" + sess
.kernel_release
);
376 find_debug_rpms(sess
, dir
.c_str());
378 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
379 sess
.architecture
.c_str(), sess
.kernel_build_tree
.c_str()));
382 build_kernel_blacklist();
387 dwflpp::setup_user(const vector
<string
>& modules
, bool debuginfo_needed
)
389 if (! sess
.module_cache
)
390 sess
.module_cache
= new module_cache ();
392 vector
<string
>::const_iterator it
= modules
.begin();
393 dwfl
= setup_dwfl_user(it
, modules
.end(), debuginfo_needed
, sess
);
394 if (debuginfo_needed
&& it
!= modules
.end())
395 DWFL_ASSERT (string(_F("missing process %s %s debuginfo",
396 (*it
).c_str(), sess
.architecture
.c_str())),
399 build_user_blacklist();
403 dwflpp::iterate_over_modules
<void>(int (*callback
)(Dwfl_Module
*,
410 dwfl_getmodules (dwfl
, callback
, data
, 0);
412 // Don't complain if we exited dwfl_getmodules early.
413 // This could be a $target variable error that will be
414 // reported soon anyway.
415 // DWFL_ASSERT("dwfl_getmodules", off == 0);
417 // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the
418 // "kernel" module is not reported in the loop above. However, we
419 // may be able to make do with symbol table data.
424 dwflpp::iterate_over_cus
<void>(int (*callback
)(Dwarf_Die
*, void*),
428 get_module_dwarf(false);
429 Dwarf
*dw
= module_dwarf
;
432 vector
<Dwarf_Die
>* v
= module_cu_cache
[dw
];
435 v
= new vector
<Dwarf_Die
>;
436 module_cu_cache
[dw
] = v
;
441 while (dwarf_nextcu (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) == 0)
443 assert_no_interrupts();
446 die
= dwarf_offdie (dw
, off
+ cuhl
, &die_mem
);
447 /* Skip partial units. */
448 if (dwarf_tag (die
) == DW_TAG_compile_unit
)
449 v
->push_back (*die
); /* copy */
454 if (want_types
&& module_tus_read
.find(dw
) == module_tus_read
.end())
456 // Process type units.
460 uint64_t type_signature
;
461 while (dwarf_next_unit (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
, NULL
,
462 &type_signature
, NULL
) == 0)
464 assert_no_interrupts();
467 die
= dwarf_offdie_types (dw
, off
+ cuhl
, &die_mem
);
468 /* Skip partial units. */
469 if (dwarf_tag (die
) == DW_TAG_type_unit
)
470 v
->push_back (*die
); /* copy */
473 module_tus_read
.insert(dw
);
476 for (vector
<Dwarf_Die
>::iterator i
= v
->begin(); i
!= v
->end(); ++i
)
478 int rc
= (*callback
)(&*i
, data
);
479 assert_no_interrupts();
480 if (rc
!= DWARF_CB_OK
)
487 dwflpp::func_is_inline()
490 return dwarf_func_inline (function
) != 0;
495 dwflpp::func_is_exported()
497 const char *name
= dwarf_linkage_name (function
) ?: dwarf_diename (function
);
501 int syms
= dwfl_module_getsymtab (module
);
502 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
504 for (int i
= 0; i
< syms
; i
++)
508 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
510 && strcmp (name
, symname
) == 0)
512 if (GELF_ST_TYPE(sym
.st_info
) == STT_FUNC
513 && (GELF_ST_BIND(sym
.st_info
) == STB_GLOBAL
514 || GELF_ST_BIND(sym
.st_info
) == STB_WEAK
515 || GELF_ST_BIND(sym
.st_info
) == STB_GNU_UNIQUE
))
525 dwflpp::cache_inline_instances (Dwarf_Die
* die
)
527 // If this is an inline instance, link it back to its origin
529 if (dwarf_tag(die
) == DW_TAG_inlined_subroutine
&&
530 dwarf_attr_die(die
, DW_AT_abstract_origin
, &origin
))
532 vector
<Dwarf_Die
>*& v
= cu_inl_function_cache
[origin
.addr
];
534 v
= new vector
<Dwarf_Die
>;
538 // Recurse through other scopes that may contain inlines
539 Dwarf_Die child
, import
;
540 if (dwarf_child(die
, &child
) == 0)
543 switch (dwarf_tag (&child
))
545 // tags that could contain inlines
546 case DW_TAG_compile_unit
:
548 case DW_TAG_lexical_block
:
549 case DW_TAG_with_stmt
:
550 case DW_TAG_catch_block
:
551 case DW_TAG_try_block
:
552 case DW_TAG_entry_point
:
553 case DW_TAG_inlined_subroutine
:
554 case DW_TAG_subprogram
:
555 cache_inline_instances(&child
);
558 // imported dies should be followed
559 case DW_TAG_imported_unit
:
560 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
561 cache_inline_instances(&import
);
564 // nothing to do for other tags
569 while (dwarf_siblingof(&child
, &child
) == 0);
574 dwflpp::iterate_over_inline_instances
<void>(int (*callback
)(Dwarf_Die
*, void*),
578 assert (func_is_inline ());
580 if (cu_inl_function_cache_done
.insert(cu
->addr
).second
)
581 cache_inline_instances(cu
);
583 vector
<Dwarf_Die
>* v
= cu_inl_function_cache
[function
->addr
];
587 for (vector
<Dwarf_Die
>::iterator i
= v
->begin(); i
!= v
->end(); ++i
)
589 int rc
= (*callback
)(&*i
, data
);
590 assert_no_interrupts();
591 if (rc
!= DWARF_CB_OK
)
598 dwflpp::cache_die_parents(cu_die_parent_cache_t
* parents
, Dwarf_Die
* die
)
600 // Record and recurse through DIEs we care about
601 Dwarf_Die child
, import
;
602 if (dwarf_child(die
, &child
) == 0)
605 switch (dwarf_tag (&child
))
607 // normal tags to recurse
608 case DW_TAG_compile_unit
:
610 case DW_TAG_lexical_block
:
611 case DW_TAG_with_stmt
:
612 case DW_TAG_catch_block
:
613 case DW_TAG_try_block
:
614 case DW_TAG_entry_point
:
615 case DW_TAG_inlined_subroutine
:
616 case DW_TAG_subprogram
:
617 case DW_TAG_namespace
:
618 case DW_TAG_class_type
:
619 case DW_TAG_structure_type
:
620 parents
->insert(make_pair(child
.addr
, *die
));
621 cache_die_parents(parents
, &child
);
624 // record only, nothing to recurse
626 parents
->insert(make_pair(child
.addr
, *die
));
629 // imported dies should be followed
630 case DW_TAG_imported_unit
:
631 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
633 parents
->insert(make_pair(import
.addr
, *die
));
634 cache_die_parents(parents
, &import
);
638 // nothing to do for other tags
643 while (dwarf_siblingof(&child
, &child
) == 0);
647 cu_die_parent_cache_t
*
648 dwflpp::get_die_parents()
652 cu_die_parent_cache_t
*& parents
= cu_die_parent_cache
[cu
->addr
];
655 parents
= new cu_die_parent_cache_t
;
656 cache_die_parents(parents
, cu
);
657 if (sess
.verbose
> 4)
658 clog
<< _F("die parent cache %s:%s size %zu", module_name
.c_str(),
659 cu_name().c_str(), parents
->size()) << endl
;
666 dwflpp::getscopes_die(Dwarf_Die
* die
)
668 cu_die_parent_cache_t
*parents
= get_die_parents();
670 vector
<Dwarf_Die
> scopes
;
671 Dwarf_Die
*scope
= die
;
672 cu_die_parent_cache_t::iterator it
;
675 scopes
.push_back(*scope
);
676 it
= parents
->find(scope
->addr
);
679 while (it
!= parents
->end());
681 #ifdef DEBUG_DWFLPP_GETSCOPES
682 Dwarf_Die
*dscopes
= NULL
;
683 int nscopes
= dwarf_getscopes_die(die
, &dscopes
);
685 assert(nscopes
== (int)scopes
.size());
686 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
687 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
695 std::vector
<Dwarf_Die
>
696 dwflpp::getscopes(Dwarf_Die
* die
)
698 cu_die_parent_cache_t
*parents
= get_die_parents();
700 vector
<Dwarf_Die
> scopes
;
703 Dwarf_Die
*scope
= die
;
704 cu_die_parent_cache_t::iterator it
;
707 scopes
.push_back(*scope
);
708 if (dwarf_tag(scope
) == DW_TAG_inlined_subroutine
&&
709 dwarf_attr_die(scope
, DW_AT_abstract_origin
, &origin
))
712 it
= parents
->find(scope
->addr
);
715 while (it
!= parents
->end());
717 #ifdef DEBUG_DWFLPP_GETSCOPES
718 // there isn't an exact libdw equivalent, but if dwarf_getscopes on the
719 // entrypc returns the same first die, then all the scopes should match
721 if (die_entrypc(die
, &pc
))
723 Dwarf_Die
*dscopes
= NULL
;
724 int nscopes
= dwarf_getscopes(cu
, pc
, &dscopes
);
725 if (nscopes
> 0 && dscopes
[0].addr
== die
->addr
)
727 assert(nscopes
== (int)scopes
.size());
728 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
729 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
739 std::vector
<Dwarf_Die
>
740 dwflpp::getscopes(Dwarf_Addr pc
)
742 // The die_parent_cache doesn't help us without knowing where the pc is
743 // contained, so we have to do this one the old fashioned way.
747 vector
<Dwarf_Die
> scopes
;
749 Dwarf_Die
* dwarf_scopes
;
750 int nscopes
= dwarf_getscopes(cu
, pc
, &dwarf_scopes
);
753 scopes
.assign(dwarf_scopes
, dwarf_scopes
+ nscopes
);
757 #ifdef DEBUG_DWFLPP_GETSCOPES
758 // check that getscopes on the starting die gets the same result
761 vector
<Dwarf_Die
> other
= getscopes(&scopes
[0]);
762 assert(scopes
.size() == other
.size());
763 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
764 assert(scopes
[i
].addr
== other
[i
].addr
);
773 dwflpp::get_parent_scope(Dwarf_Die
* die
)
775 Dwarf_Die specification
;
776 if (dwarf_attr_die(die
, DW_AT_specification
, &specification
))
777 die
= &specification
;
779 cu_die_parent_cache_t
*parents
= get_die_parents();
780 cu_die_parent_cache_t::iterator it
= parents
->find(die
->addr
);
781 while (it
!= parents
->end())
783 Dwarf_Die
* scope
= &it
->second
;
784 switch (dwarf_tag (scope
))
786 case DW_TAG_namespace
:
787 case DW_TAG_class_type
:
788 case DW_TAG_structure_type
:
794 it
= parents
->find(scope
->addr
);
800 cache_type_prefix(Dwarf_Die
* type
)
802 switch (dwarf_tag(type
))
804 case DW_TAG_enumeration_type
:
806 case DW_TAG_structure_type
:
807 case DW_TAG_class_type
:
808 // treating struct/class as equals
810 case DW_TAG_union_type
:
816 /* GCC might generate a struct/class without DW_AT_declaration,
817 but that only contains members which have DW_AT_declaration
818 set. We aren't interested in those. PR14434 (GCC bug #54181). */
820 has_only_decl_members (Dwarf_Die
*die
)
822 Dwarf_Die child
, import
;
823 if (dwarf_child(die
, &child
) != 0)
824 return false; /* no members */
828 if (! dwarf_hasattr(&child
, DW_AT_declaration
))
829 return false; /* real member found. */
830 int tag
= dwarf_tag(&child
);
831 if ((tag
== DW_TAG_namespace
832 || tag
== DW_TAG_structure_type
833 || tag
== DW_TAG_class_type
)
834 && ! has_only_decl_members (&child
))
835 return false; /* real grand child member found. */
837 // Unlikely to ever happen, but if there is an imported unit
838 // then check its children as if they are children of this DIE.
839 if (tag
== DW_TAG_imported_unit
840 && dwarf_attr_die(&child
, DW_AT_import
, &import
)
841 && ! has_only_decl_members (&import
))
844 while (dwarf_siblingof(&child
, &child
) == 0);
846 return true; /* Tried all children and grandchildren. */
850 dwflpp::global_alias_caching_callback(Dwarf_Die
*die
, bool has_inner_types
,
851 const string
& prefix
, cu_type_cache_t
*cache
)
853 const char *name
= dwarf_diename(die
);
855 if (!name
|| dwarf_hasattr(die
, DW_AT_declaration
)
856 || has_only_decl_members(die
))
859 int tag
= dwarf_tag(die
);
860 if (has_inner_types
&& (tag
== DW_TAG_namespace
861 || tag
== DW_TAG_structure_type
862 || tag
== DW_TAG_class_type
))
863 iterate_over_types(die
, has_inner_types
, prefix
+ name
+ "::",
864 global_alias_caching_callback
, cache
);
866 if (tag
!= DW_TAG_namespace
)
868 string type_name
= prefix
+ cache_type_prefix(die
) + name
;
869 if (cache
->find(type_name
) == cache
->end())
870 (*cache
)[type_name
] = *die
;
877 dwflpp::global_alias_caching_callback_cus(Dwarf_Die
*die
, dwflpp
*dw
)
879 mod_cu_type_cache_t
*global_alias_cache
;
880 global_alias_cache
= &dw
->global_alias_cache
;
882 cu_type_cache_t
*v
= (*global_alias_cache
)[die
->addr
];
886 v
= new cu_type_cache_t
;
887 (*global_alias_cache
)[die
->addr
] = v
;
888 iterate_over_globals(die
, global_alias_caching_callback
, v
);
894 dwflpp::declaration_resolve_other_cus(const string
& name
)
896 iterate_over_cus(global_alias_caching_callback_cus
, this, true);
897 for (mod_cu_type_cache_t::iterator i
= global_alias_cache
.begin();
898 i
!= global_alias_cache
.end(); ++i
)
900 cu_type_cache_t
*v
= (*i
).second
;
901 if (v
->find(name
) != v
->end())
902 return & ((*v
)[name
]);
909 dwflpp::declaration_resolve(const string
& name
)
911 cu_type_cache_t
*v
= global_alias_cache
[cu
->addr
];
912 if (v
== 0) // need to build the cache, just once per encountered module/cu
914 v
= new cu_type_cache_t
;
915 global_alias_cache
[cu
->addr
] = v
;
916 iterate_over_globals(cu
, global_alias_caching_callback
, v
);
917 if (sess
.verbose
> 4)
918 clog
<< _F("global alias cache %s:%s size %zu", module_name
.c_str(),
919 cu_name().c_str(), v
->size()) << endl
;
922 // XXX: it may be desirable to search other modules' declarations
923 // too, in case a module/shared-library processes a
924 // forward-declared pointer type only, where the actual definition
925 // may only be in vmlinux or the application.
927 if (v
->find(name
) == v
->end())
928 return declaration_resolve_other_cus(name
);
930 return & ((*v
)[name
]);
934 dwflpp::declaration_resolve(Dwarf_Die
*type
)
936 const char* name
= dwarf_diename(type
);
940 string type_name
= cache_type_prefix(type
) + string(name
);
941 return declaration_resolve(type_name
);
946 dwflpp::cu_function_caching_callback (Dwarf_Die
* func
, cu_function_cache_t
*v
)
948 const char *name
= dwarf_diename(func
);
952 v
->insert(make_pair(string(name
), *func
));
958 dwflpp::mod_function_caching_callback (Dwarf_Die
* cu
, cu_function_cache_t
*v
)
960 // need to cast callback to func which accepts void*
961 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
968 dwflpp::iterate_over_functions
<void>(int (*callback
)(Dwarf_Die
*, void*),
969 void *data
, const string
& function
)
971 int rc
= DWARF_CB_OK
;
975 cu_function_cache_t
*v
= cu_function_cache
[cu
->addr
];
978 v
= new cu_function_cache_t
;
979 cu_function_cache
[cu
->addr
] = v
;
980 // need to cast callback to func which accepts void*
981 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
983 if (sess
.verbose
> 4)
984 clog
<< _F("function cache %s:%s size %zu", module_name
.c_str(),
985 cu_name().c_str(), v
->size()) << endl
;
986 mod_info
->update_symtab(v
);
989 cu_function_cache_t::iterator it
;
990 cu_function_cache_range_t range
= v
->equal_range(function
);
991 if (range
.first
!= range
.second
)
993 for (it
= range
.first
; it
!= range
.second
; ++it
)
995 Dwarf_Die
& die
= it
->second
;
996 if (sess
.verbose
> 4)
997 clog
<< _F("function cache %s:%s hit %s", module_name
.c_str(),
998 cu_name().c_str(), function
.c_str()) << endl
;
999 rc
= (*callback
)(& die
, data
);
1000 if (rc
!= DWARF_CB_OK
) break;
1003 else if (startswith(function
, "_Z"))
1005 // C++ names are mangled starting with a "_Z" prefix. Most of the time
1006 // we can discover the mangled name from a die's MIPS_linkage_name
1007 // attribute, so we read that to match against the user's function
1008 // pattern. Note that this isn't perfect, as not all will have that
1009 // attribute (notably ctors and dtors), but we do what we can...
1010 for (it
= v
->begin(); it
!= v
->end(); ++it
)
1012 if (pending_interrupts
) return DWARF_CB_ABORT
;
1013 Dwarf_Die
& die
= it
->second
;
1014 const char* linkage_name
= NULL
;
1015 if ((linkage_name
= dwarf_linkage_name (&die
))
1016 && function_name_matches_pattern (linkage_name
, function
))
1018 if (sess
.verbose
> 4)
1019 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1020 cu_name().c_str(), linkage_name
, function
.c_str()) << endl
;
1022 rc
= (*callback
)(& die
, data
);
1023 if (rc
!= DWARF_CB_OK
) break;
1027 else if (name_has_wildcard (function
))
1029 for (it
= v
->begin(); it
!= v
->end(); ++it
)
1031 if (pending_interrupts
) return DWARF_CB_ABORT
;
1032 const string
& func_name
= it
->first
;
1033 Dwarf_Die
& die
= it
->second
;
1034 if (function_name_matches_pattern (func_name
, function
))
1036 if (sess
.verbose
> 4)
1037 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1038 cu_name().c_str(), func_name
.c_str(), function
.c_str()) << endl
;
1040 rc
= (*callback
)(& die
, data
);
1041 if (rc
!= DWARF_CB_OK
) break;
1045 else // not a linkage name or wildcard and no match in this CU
1054 dwflpp::iterate_single_function
<void>(int (*callback
)(Dwarf_Die
*, void*),
1055 void *data
, const string
& function
)
1057 int rc
= DWARF_CB_OK
;
1060 get_module_dwarf(false);
1064 cu_function_cache_t
*v
= mod_function_cache
[module_dwarf
];
1067 v
= new cu_function_cache_t
;
1068 mod_function_cache
[module_dwarf
] = v
;
1069 iterate_over_cus (mod_function_caching_callback
, v
, false);
1070 if (sess
.verbose
> 4)
1071 clog
<< _F("module function cache %s size %zu", module_name
.c_str(),
1073 mod_info
->update_symtab(v
);
1076 cu_function_cache_t::iterator it
;
1077 cu_function_cache_range_t range
= v
->equal_range(function
);
1078 if (range
.first
!= range
.second
)
1080 for (it
= range
.first
; it
!= range
.second
; ++it
)
1083 Dwarf_Die
& die
= it
->second
;
1084 if (sess
.verbose
> 4)
1085 clog
<< _F("module function cache %s hit %s", module_name
.c_str(),
1086 function
.c_str()) << endl
;
1088 // since we're iterating out of cu-context, we need each focus
1089 focus_on_cu(dwarf_diecu(&die
, &cu_mem
, NULL
, NULL
));
1091 rc
= (*callback
)(& die
, data
);
1092 if (rc
!= DWARF_CB_OK
) break;
1096 // undo the focus_on_cu
1098 this->function_name
.clear();
1099 this->function
= NULL
;
1105 /* This basically only goes one level down from the compile unit so it
1106 * only picks up top level stuff (i.e. nothing in a lower scope) */
1108 dwflpp::iterate_over_globals
<void>(Dwarf_Die
*cu_die
,
1109 int (*callback
)(Dwarf_Die
*,
1116 assert (dwarf_tag(cu_die
) == DW_TAG_compile_unit
1117 || dwarf_tag(cu_die
) == DW_TAG_type_unit
1118 || dwarf_tag(cu_die
) == DW_TAG_partial_unit
);
1120 // Ignore partial_unit, if they get imported by a real unit, then
1121 // iterate_over_types will traverse them.
1122 if (dwarf_tag(cu_die
) == DW_TAG_partial_unit
)
1125 // If this is C++, recurse for any inner types
1126 bool has_inner_types
= dwarf_srclang(cu_die
) == DW_LANG_C_plus_plus
;
1128 return iterate_over_types(cu_die
, has_inner_types
, "", callback
, data
);
1132 dwflpp::iterate_over_types
<void>(Dwarf_Die
*top_die
,
1133 bool has_inner_types
,
1134 const string
& prefix
,
1135 int (* callback
)(Dwarf_Die
*,
1141 int rc
= DWARF_CB_OK
;
1142 Dwarf_Die die
, import
;
1146 if (dwarf_child(top_die
, &die
) != 0)
1150 /* We're only currently looking for named types,
1151 * although other types of declarations exist */
1152 switch (dwarf_tag(&die
))
1154 case DW_TAG_base_type
:
1155 case DW_TAG_enumeration_type
:
1156 case DW_TAG_structure_type
:
1157 case DW_TAG_class_type
:
1158 case DW_TAG_typedef
:
1159 case DW_TAG_union_type
:
1160 case DW_TAG_namespace
:
1161 rc
= (*callback
)(&die
, has_inner_types
, prefix
, data
);
1164 case DW_TAG_imported_unit
:
1165 // Follow the imported_unit and iterate over its contents
1166 // (either a partial_unit or a full compile_unit), all its
1167 // children should be treated as if they appear in this place.
1168 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
1169 rc
= iterate_over_types(&import
, has_inner_types
, prefix
,
1173 while (rc
== DWARF_CB_OK
&& dwarf_siblingof(&die
, &die
) == 0);
1179 /* For each notes section in the current module call 'callback', use
1180 * 'data' for the notes buffer and pass 'object' back in case
1181 * 'callback' is a method */
1184 dwflpp::iterate_over_notes
<void>(void *object
, void (*callback
)(void*,
1192 // Note we really want the actual elf file, not the dwarf .debug file.
1193 // Older binutils had a bug where they mangled the SHT_NOTE type during
1195 Elf
* elf
= dwfl_module_getelf (module
, &bias
);
1197 if (elf_getshdrstrndx (elf
, &shstrndx
))
1200 Elf_Scn
*scn
= NULL
;
1202 vector
<Dwarf_Die
> notes
;
1204 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
1207 if (gelf_getshdr (scn
, &shdr
) == NULL
)
1209 switch (shdr
.sh_type
)
1212 if (!(shdr
.sh_flags
& SHF_ALLOC
))
1214 string scn_name
= elf_strptr(elf
, shstrndx
, shdr
.sh_name
);
1215 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1220 for (size_t offset
= 0;
1221 (next
= gelf_getnote (data
, offset
, &nhdr
, &name_off
, &desc_off
)) > 0;
1224 const char *note_name_addr
= (const char *)data
->d_buf
+ name_off
;
1225 const char *note_desc_addr
= (const char *)data
->d_buf
+ desc_off
;
1226 string note_name
= nhdr
.n_namesz
> 1 // n_namesz includes NULL
1227 ? string(note_name_addr
, nhdr
.n_namesz
-1) : "";
1228 (*callback
) (object
, scn_name
, note_name
, nhdr
.n_type
,
1229 note_desc_addr
, nhdr
.n_descsz
);
1239 /* For each entry in the .dynamic section in the current module call 'callback'
1240 * returning 'object' in case 'callback' is a method */
1243 dwflpp::iterate_over_libraries
<void>(void (*callback
)(void*, const char*),
1246 std::set
<std::string
> added
;
1249 assert (this->module_name
.length() != 0);
1252 // We cannot use this: dwarf_getelf (dwfl_module_getdwarf (module, &bias))
1253 Elf
*elf
= dwfl_module_getelf (module
, &bias
);
1254 // elf_getphdrnum (elf, &phnum) is not available in all versions of elfutils
1255 // needs libelf from elfutils 0.144+
1256 for (int i
= 0; ; i
++)
1260 phdr
= gelf_getphdr (elf
, i
, &mem
);
1263 if (phdr
->p_type
== PT_INTERP
)
1266 char *filedata
= elf_rawfile (elf
, &maxsize
);
1268 if (filedata
!= NULL
&& phdr
->p_offset
< maxsize
)
1269 interpreter
= (char*) (filedata
+ phdr
->p_offset
);
1274 if (interpreter
.length() == 0)
1276 // If it gets cumbersome to maintain this whitelist, we could just check for
1277 // startswith("/lib/ld") || startswith("/lib64/ld"), and trust that no admin
1278 // would install untrustworthy loaders in those paths.
1279 // See also http://sourceware.org/git/?p=glibc.git;a=blob;f=shlib-versions;hb=HEAD
1280 if (interpreter
!= "/lib/ld.so.1" // s390, ppc
1281 && interpreter
!= "/lib/ld64.so.1" // s390x, ppc64
1282 && interpreter
!= "/lib64/ld64.so.1"
1283 && interpreter
!= "/lib/ld-linux-ia64.so.2" // ia64
1284 && interpreter
!= "/emul/ia32-linux/lib/ld-linux.so.2"
1285 && interpreter
!= "/lib64/ld-linux-x86-64.so.2" // x8664
1286 && interpreter
!= "/lib/ld-linux.so.2" // x86
1287 && interpreter
!= "/lib/ld-linux.so.3" // arm
1288 && interpreter
!= "/lib/ld-linux-armhf.so.3" // arm
1289 && interpreter
!= "/lib/ld-linux-aarch64.so.1" // arm64
1290 && interpreter
!= "/lib64/ld64.so.2" // ppc64le
1293 sess
.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s",
1294 module_name
.c_str(), interpreter
.c_str()));
1298 vector
<string
> ldd_command
;
1299 ldd_command
.push_back("/usr/bin/env");
1300 ldd_command
.push_back("LD_TRACE_LOADED_OBJECTS=1");
1301 ldd_command
.push_back("LD_WARN=yes");
1302 ldd_command
.push_back("LD_BIND_NOW=yes");
1303 ldd_command
.push_back(interpreter
);
1304 ldd_command
.push_back(module_name
);
1308 pid_t child
= stap_spawn_piped(sess
.verbose
, ldd_command
, NULL
, &child_fd
);
1309 if (child
<= 0 || !(fp
= fdopen(child_fd
, "r")))
1310 clog
<< _F("library iteration on %s failed: %s",
1311 module_name
.c_str(), strerror(errno
)) << endl
;
1314 while (1) // this parsing loop borrowed from add_unwindsym_ldd
1319 unsigned long int addr
= 0;
1321 char *line
= fgets (linebuf
, 256, fp
);
1322 if (line
== 0) break; // EOF or error
1324 #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)
1325 #define MS_FMT "%ms"
1327 #define MS_FMT "%as"
1329 // Try soname => shlib (0xaddr)
1330 int nf
= sscanf (line
, MS_FMT
" => " MS_FMT
" (0x%lx)",
1331 &soname
, &shlib
, &addr
);
1332 if (nf
!= 3 || shlib
[0] != '/')
1334 // Try shlib (0xaddr)
1335 nf
= sscanf (line
, " " MS_FMT
" (0x%lx)", &shlib
, &addr
);
1336 if (nf
!= 2 || shlib
[0] != '/')
1337 continue; // fewer than expected fields, or bad shlib.
1340 if (added
.find (shlib
) == added
.end())
1342 if (sess
.verbose
> 2)
1344 clog
<< _F("Added -d '%s", shlib
);
1346 clog
<< _F("' due to '%s'", soname
);
1351 added
.insert (shlib
);
1357 if ((fclose(fp
) || stap_waitpid(sess
.verbose
, child
)))
1358 sess
.print_warning("failed to read libraries from " + module_name
+ ": " + strerror(errno
));
1361 for (std::set
<std::string
>::iterator it
= added
.begin();
1365 string modname
= *it
;
1366 (callback
) (data
, modname
.c_str());
1371 /* For each plt section in the current module call 'callback', pass the plt entry
1372 * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */
1375 dwflpp::iterate_over_plt
<void>(void *object
, void (*callback
)(void*,
1379 Dwarf_Addr load_addr
;
1380 // Note we really want the actual elf file, not the dwarf .debug file.
1381 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
1383 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
1385 // Get the load address
1386 for (int i
= 0; ; i
++)
1390 phdr
= gelf_getphdr (elf
, i
, &mem
);
1393 if (phdr
->p_type
== PT_LOAD
)
1395 load_addr
= phdr
->p_vaddr
;
1400 // Get the plt section header
1401 Elf_Scn
*scn
= NULL
;
1402 GElf_Shdr
*plt_shdr
= NULL
;
1403 GElf_Shdr plt_shdr_mem
;
1404 while ((scn
= elf_nextscn (elf
, scn
)))
1406 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &plt_shdr_mem
);
1407 assert (shdr
!= NULL
);
1408 if (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".plt") == 0)
1414 if (plt_shdr
== NULL
)
1417 // Layout of the plt section
1418 int plt0_entry_size
;
1421 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1422 switch (em
->e_machine
)
1424 case EM_386
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1425 case EM_X86_64
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1426 case EM_ARM
: plt0_entry_size
= 20; plt_entry_size
= 12; break;
1427 case EM_AARCH64
:plt0_entry_size
= 32; plt_entry_size
= 16; break;
1432 throw SEMANTIC_ERROR(".plt is not supported on this architecture");
1436 while ((scn
= elf_nextscn (elf
, scn
)))
1439 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
1440 bool have_rela
= false;
1441 bool have_rel
= false;
1445 assert (shdr
!= NULL
);
1447 if ((have_rela
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rela.plt") == 0))
1448 || (have_rel
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rel.plt") == 0)))
1450 /* Get the data of the section. */
1451 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1452 assert (data
!= NULL
);
1453 /* Get the symbol table information. */
1454 Elf_Scn
*symscn
= elf_getscn (elf
, shdr
->sh_link
);
1455 GElf_Shdr symshdr_mem
;
1456 GElf_Shdr
*symshdr
= gelf_getshdr (symscn
, &symshdr_mem
);
1457 assert (symshdr
!= NULL
);
1458 Elf_Data
*symdata
= elf_getdata (symscn
, NULL
);
1459 assert (symdata
!= NULL
);
1461 unsigned int nsyms
= shdr
->sh_size
/ shdr
->sh_entsize
;
1463 for (unsigned int cnt
= 0; cnt
< nsyms
; ++cnt
)
1466 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1467 if (em
== 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
1470 GElf_Rela
*rela
= NULL
;
1472 GElf_Rel
*rel
= NULL
;
1475 rela
= gelf_getrela (data
, cnt
, &relamem
);
1476 assert (rela
!= NULL
);
1480 rel
= gelf_getrel (data
, cnt
, &relmem
);
1481 assert (rel
!= NULL
);
1485 Elf_Data
*xndxdata
= NULL
;
1487 gelf_getsymshndx (symdata
, xndxdata
,
1488 GELF_R_SYM (have_rela
? rela
->r_info
: rel
->r_info
),
1490 assert (sym
!= NULL
);
1491 Dwarf_Addr addr
= plt_shdr
->sh_offset
+ plt0_entry_size
+ cnt
* plt_entry_size
;
1493 if (elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
))
1494 (*callback
) (object
, elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
), addr
+ load_addr
);
1503 // Comparator function for sorting
1505 compare_lines(Dwarf_Line
* a
, Dwarf_Line
* b
)
1510 int lineno_a
= DWARF_LINENO(a
);
1511 int lineno_b
= DWARF_LINENO(b
);
1512 if (lineno_a
== lineno_b
)
1513 return DWARF_LINEADDR(a
) < DWARF_LINEADDR(b
);
1514 return lineno_a
< lineno_b
;
1517 // Comparator object for searching Dwarf_Lines with a specific lineno when we
1518 // don't have a Dwarf_Line of our own to search for (hence why a or b is always
1520 struct lineno_comparator
{
1522 lineno_comparator(int lineno
): lineno(lineno
) {}
1523 bool operator() (Dwarf_Line
* a
, Dwarf_Line
* b
)
1527 return DWARF_LINENO(a
) < lineno
;
1529 return lineno
< DWARF_LINENO(b
);
1533 // Returns a range of lines in between begin and end with wanted lineno. If
1534 // none exist, points to where it would have been.
1535 static lines_range_t
1536 lineno_equal_range(lines_t
* v
, int lineno
)
1538 lineno_comparator
lc(lineno
);
1539 return equal_range(v
->begin(), v
->end(), (Dwarf_Line
*)NULL
, lc
);
1542 // Interface to CU lines cache sorted by lineno
1544 dwflpp::get_cu_lines_sorted_by_lineno(const char *srcfile
)
1548 srcfile_lines_cache_t
*srcfile_lines
= cu_lines_cache
[cu
];
1551 srcfile_lines
= new srcfile_lines_cache_t();
1552 cu_lines_cache
[cu
] = srcfile_lines
;
1555 lines_t
*lines
= (*srcfile_lines
)[srcfile
];
1558 size_t nlines_cu
= 0;
1559 Dwarf_Lines
*lines_cu
= NULL
;
1560 DWARF_ASSERT("dwarf_getsrclines",
1561 dwarf_getsrclines(cu
, &lines_cu
, &nlines_cu
));
1563 lines
= new lines_t();
1564 (*srcfile_lines
)[srcfile
] = lines
;
1566 for (size_t i
= 0; i
< nlines_cu
; i
++)
1568 Dwarf_Line
*line
= dwarf_onesrcline(lines_cu
, i
);
1569 const char *linesrc
= DWARF_LINESRC(line
);
1570 if (strcmp(srcfile
, linesrc
))
1572 lines
->push_back(line
);
1575 if (lines
->size() > 1)
1576 sort(lines
->begin(), lines
->end(), compare_lines
);
1578 if (sess
.verbose
> 3)
1580 clog
<< _F("found the following lines for %s:", srcfile
) << endl
;
1581 lines_t::iterator i
;
1582 for (i
= lines
->begin(); i
!= lines
->end(); ++i
)
1583 cout
<< DWARF_LINENO(*i
) << " = " << hex
1584 << DWARF_LINEADDR(*i
) << dec
<< endl
;
1591 get_func_first_line(Dwarf_Die
*cu
, base_func_info
& func
)
1593 // dwarf_getsrc_die() uses binary search to find the Dwarf_Line, but will
1594 // return the wrong line if not found.
1595 Dwarf_Line
*line
= dwarf_getsrc_die(cu
, func
.entrypc
);
1596 if (line
&& DWARF_LINEADDR(line
) == func
.entrypc
)
1599 // Line not found (or line at wrong addr). We have to resort to a slower
1600 // linear method. We won't find an exact match (probably this is an inlined
1601 // instance), so just settle on the first Dwarf_Line with lowest addr which
1602 // falls in the die.
1604 Dwarf_Lines
*lines
= NULL
;
1605 DWARF_ASSERT("dwarf_getsrclines",
1606 dwarf_getsrclines(cu
, &lines
, &nlines
));
1608 for (size_t i
= 0; i
< nlines
; i
++)
1610 line
= dwarf_onesrcline(lines
, i
);
1611 if (dwarf_haspc(&func
.die
, DWARF_LINEADDR(line
)))
1618 collect_lines_in_die(lines_range_t range
, Dwarf_Die
*die
)
1620 lines_t lines_in_die
;
1621 for (lines_t::iterator line
= range
.first
;
1622 line
!= range
.second
; ++line
)
1623 if (dwarf_haspc(die
, DWARF_LINEADDR(*line
)))
1624 lines_in_die
.push_back(*line
);
1625 return lines_in_die
;
1629 add_matching_lines_in_func(Dwarf_Die
*cu
,
1631 base_func_info
& func
,
1632 lines_t
& matching_lines
)
1634 Dwarf_Line
*start_line
= get_func_first_line(cu
, func
);
1638 for (int lineno
= DWARF_LINENO(start_line
);;)
1640 lines_range_t range
= lineno_equal_range(cu_lines
, lineno
);
1642 // We consider the lineno still part of the die if at least one of them
1643 // falls in the die.
1644 lines_t lines_in_die
= collect_lines_in_die(range
, &func
.die
);
1645 if (lines_in_die
.empty())
1648 // Just pick the first LR even if there are more than one. Since the lines
1649 // are sorted by lineno and then addr, the first one is the one with the
1651 matching_lines
.push_back(lines_in_die
[0]);
1653 // break out if there are no more lines, otherwise, go to the next lineno
1654 if (range
.second
== cu_lines
->end())
1656 lineno
= DWARF_LINENO(*range
.second
);
1661 dwflpp::collect_all_lines(char const * srcfile
,
1662 base_func_info_map_t
& funcs
,
1663 lines_t
& matching_lines
)
1665 // This is where we handle WILDCARD lineno types.
1666 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1667 for (base_func_info_map_t::iterator func
= funcs
.begin();
1668 func
!= funcs
.end(); ++func
)
1669 add_matching_lines_in_func(cu
, cu_lines
, *func
, matching_lines
);
1674 add_matching_line_in_die(lines_t
*cu_lines
,
1675 lines_t
& matching_lines
,
1676 Dwarf_Die
*die
, int lineno
)
1678 lines_range_t lineno_range
= lineno_equal_range(cu_lines
, lineno
);
1679 lines_t lines_in_die
= collect_lines_in_die(lineno_range
, die
);
1680 if (lines_in_die
.empty())
1683 // Even if there are more than 1 LRs, just pick the first one. Since the lines
1684 // are sorted by lineno and then addr, the first one is the one with the
1685 // lowest addr. This is similar to what GDB does.
1686 matching_lines
.push_back(lines_in_die
[0]);
1690 dwflpp::collect_lines_for_single_lineno(char const * srcfile
,
1693 base_func_info_map_t
& funcs
,
1694 lines_t
& matching_lines
)
1696 /* Here, we handle ABSOLUTE and RELATIVE lineno types. Relative line numbers
1697 * are a bit special. The issue is that functions (esp. inlined ones) may not
1698 * even have a LR corresponding to the first valid line of code. So, applying
1699 * an offset to the 'first' LR found in the DIE can be quite imprecise.
1700 * Instead, we use decl_line, which although does not necessarily have a
1701 * LR associated with it (it can sometimes still happen esp. if the code is
1702 * written in OTB-style), it serves as an anchor on which we can apply the
1703 * offset to yield a lineno that will not change with compiler optimization.
1704 * It also has the added benefit of being consistent with the lineno
1705 * printed by e.g. stap -l kernel.function("vfs_read"), so users can be
1706 * confident from what lineno we adjust.
1708 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1709 for (base_func_info_map_t::iterator func
= funcs
.begin();
1710 func
!= funcs
.end(); ++func
)
1711 add_matching_line_in_die(cu_lines
, matching_lines
, &func
->die
,
1712 is_relative
? lineno
+ func
->decl_line
1717 functions_have_lineno(base_func_info_map_t
& funcs
,
1718 lines_t
*lines
, int lineno
)
1720 lines_range_t lineno_range
= lineno_equal_range(lines
, lineno
);
1721 if (lineno_range
.first
== lineno_range
.second
)
1722 return false; // no LRs at this lineno
1724 for (base_func_info_map_t::iterator func
= funcs
.begin();
1725 func
!= funcs
.end(); ++func
)
1726 if (!collect_lines_in_die(lineno_range
, &func
->die
).empty())
1732 // returns pair of valid linenos surrounding target lineno
1734 dwflpp::get_nearest_linenos(char const * srcfile
,
1736 base_func_info_map_t
& funcs
)
1739 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1741 // Look around lineno for linenos with LRs.
1742 pair
<int,int> nearest_linenos
= make_pair(-1, -1);
1743 for (size_t i
= 1; i
< 6; ++i
)
1745 if (nearest_linenos
.first
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
-i
))
1746 nearest_linenos
.first
= lineno
- i
;
1747 if (nearest_linenos
.second
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
+i
))
1748 nearest_linenos
.second
= lineno
+ i
;
1751 return nearest_linenos
;
1754 // returns nearest valid lineno to target lineno
1756 dwflpp::get_nearest_lineno(char const * srcfile
,
1758 base_func_info_map_t
& funcs
)
1761 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1763 if (nearest_linenos
.first
> 0
1764 && nearest_linenos
.second
> 0)
1766 // pick the nearest line number (break tie to upper)
1767 if (lineno
- nearest_linenos
.first
< nearest_linenos
.second
- lineno
)
1768 return nearest_linenos
.first
;
1770 return nearest_linenos
.second
;
1772 else if (nearest_linenos
.first
> 0)
1773 return nearest_linenos
.first
;
1774 else if (nearest_linenos
.second
> 0)
1775 return nearest_linenos
.second
;
1781 dwflpp::suggest_alternative_linenos(char const * srcfile
,
1783 base_func_info_map_t
& funcs
)
1786 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1788 stringstream advice
;
1789 advice
<< _F("no line records for %s:%d [man error::dwarf]", srcfile
, lineno
);
1791 if (nearest_linenos
.first
> 0 || nearest_linenos
.second
> 0)
1793 //TRANSLATORS: Here we are trying to advise what source file
1794 //TRANSLATORS: to attempt.
1795 advice
<< _(" (try ");
1796 if (nearest_linenos
.first
> 0)
1797 advice
<< ":" << nearest_linenos
.first
;
1798 if (nearest_linenos
.first
> 0 && nearest_linenos
.second
> 0)
1799 advice
<< _(" or ");
1800 if (nearest_linenos
.second
> 0)
1801 advice
<< ":" << nearest_linenos
.second
;
1804 throw SEMANTIC_ERROR (advice
.str());
1807 static base_func_info_map_t
1808 get_funcs_in_srcfile(base_func_info_map_t
& funcs
,
1809 const char * srcfile
)
1811 base_func_info_map_t matching_funcs
;
1812 for (base_func_info_map_t::iterator func
= funcs
.begin();
1813 func
!= funcs
.end(); ++func
)
1814 if (strcmp(srcfile
, func
->decl_file
) == 0)
1815 matching_funcs
.push_back(*func
);
1816 return matching_funcs
;
1820 dwflpp::iterate_over_srcfile_lines
<void>(char const * srcfile
,
1821 const vector
<int>& linenos
,
1822 enum lineno_t lineno_type
,
1823 base_func_info_map_t
& funcs
,
1824 void (* callback
) (Dwarf_Addr
,
1829 /* Matching line records (LRs) to user-provided linenos is trickier than it
1830 * seems. The fate of all functions is one of three possibilities:
1831 * 1. it's a normal function, with a subprogram DIE and a bona fide lowpc
1832 * and highpc attribute.
1833 * 2. it's an inlined function (one/multiple inlined_subroutine DIE, with one
1834 * abstract_origin DIE)
1835 * 3. it's both a normal function and an inlined function. For example, if
1836 * the funtion has been inlined only in some places, we'll have a DIE for
1837 * the normal subprogram DIE as well as inlined_subroutine DIEs.
1839 * Multiple LRs for the same lineno but different addresses can simply happen
1840 * due to the function appearing in multiple forms. E.g. a function inlined
1841 * in two spots can yield two sets of LRs for its linenos at the different
1842 * addresses where it is inlined.
1843 * This is why the collect_* functions used here try to match up LRs back
1844 * to their originating DIEs. For example, in the function
1845 * collect_lines_for_single_lineno(), we filter first by DIE so that a lineno
1846 * corresponding to multiple addrs in multiple inlined_subroutine DIEs yields
1847 * a probe for each of them.
1851 // only work on the functions found in the current srcfile
1852 base_func_info_map_t current_funcs
= get_funcs_in_srcfile(funcs
, srcfile
);
1853 if (current_funcs
.empty())
1857 lines_t matching_lines
;
1858 if (lineno_type
== ABSOLUTE
)
1859 collect_lines_for_single_lineno(srcfile
, linenos
[0], false, /* is_relative */
1860 current_funcs
, matching_lines
);
1861 else if (lineno_type
== RELATIVE
)
1862 collect_lines_for_single_lineno(srcfile
, linenos
[0], true, /* is_relative */
1863 current_funcs
, matching_lines
);
1864 else if (lineno_type
== WILDCARD
)
1865 collect_all_lines(srcfile
, current_funcs
, matching_lines
);
1866 else if (lineno_type
== ENUMERATED
)
1868 set
<int> collected_linenos
;
1869 for (vector
<int>::const_iterator it
= linenos
.begin();
1870 it
!= linenos
.end(); it
++)
1872 // have we already collected this lineno?
1873 if (collected_linenos
.find(*it
) != collected_linenos
.end())
1876 // remember end iterator so we can tell if things were found later
1877 lines_t::const_iterator itend
= matching_lines
.end();
1879 collect_lines_for_single_lineno(srcfile
, *it
, false, /* is_relative */
1880 current_funcs
, matching_lines
);
1881 // add to set if we found LRs
1882 if (itend
!= matching_lines
.end())
1883 collected_linenos
.insert(*it
);
1885 // if we didn't find anything and .nearest is given, then try nearest
1886 if (itend
== matching_lines
.end() && has_nearest
)
1888 int nearest_lineno
= get_nearest_lineno(srcfile
, *it
,
1890 if (nearest_lineno
<= 0) // no valid nearest linenos
1893 bool new_lineno
= collected_linenos
.insert(nearest_lineno
).second
;
1895 collect_lines_for_single_lineno(srcfile
, nearest_lineno
,
1896 false, /* is_relative */
1897 current_funcs
, matching_lines
);
1902 // should we try to collect the nearest lines if we didn't collect everything
1903 // on first try? (ABSOLUTE and RELATIVE only: ENUMERATED handles it already
1904 // and WILDCARD doesn't need it)
1905 if (matching_lines
.empty() && has_nearest
&& (lineno_type
== ABSOLUTE
||
1906 lineno_type
== RELATIVE
))
1908 int lineno
= linenos
[0];
1909 if (lineno_type
== RELATIVE
)
1910 // just pick the first function and make it relative to that
1911 lineno
+= current_funcs
[0].decl_line
;
1913 int nearest_lineno
= get_nearest_lineno(srcfile
, lineno
, current_funcs
);
1914 if (nearest_lineno
> 0)
1915 collect_lines_for_single_lineno(srcfile
, nearest_lineno
,
1916 false, /* is_relative */
1917 current_funcs
, matching_lines
);
1920 // call back with matching lines
1921 if (!matching_lines
.empty())
1923 set
<Dwarf_Addr
> probed_addrs
;
1924 for (lines_t::iterator line
= matching_lines
.begin();
1925 line
!= matching_lines
.end(); ++line
)
1927 int lineno
= DWARF_LINENO(*line
);
1928 Dwarf_Addr addr
= DWARF_LINEADDR(*line
);
1929 bool is_new_addr
= probed_addrs
.insert(addr
).second
;
1931 callback(addr
, lineno
, data
);
1934 // No LRs found at the wanted lineno. So let's suggest other ones if user was
1935 // targeting a specific lineno (ABSOLUTE or RELATIVE).
1936 else if (lineno_type
== ABSOLUTE
||
1937 lineno_type
== RELATIVE
)
1939 int lineno
= linenos
[0];
1940 if (lineno_type
== RELATIVE
)
1941 // just pick the first function and make it relative to that
1942 lineno
+= current_funcs
[0].decl_line
;
1944 suggest_alternative_linenos(srcfile
, lineno
, current_funcs
);
1950 dwflpp::iterate_over_labels
<void>(Dwarf_Die
*begin_die
,
1952 const base_func_info
& function
,
1953 const vector
<int>& linenos
,
1954 enum lineno_t lineno_type
,
1956 void (* callback
)(const base_func_info
&,
1966 Dwarf_Die die
, import
;
1968 int res
= dwarf_child (begin_die
, &die
);
1970 return; // die without children, bail out.
1974 switch (dwarf_tag(&die
))
1977 name
= dwarf_diename (&die
);
1980 || (name_has_wildcard(sym
)
1981 && function_name_matches_pattern (name
, sym
))))
1983 // Don't try to be smart. Just drop no addr labels.
1984 Dwarf_Addr stmt_addr
;
1985 if (dwarf_lowpc (&die
, &stmt_addr
) == 0)
1987 // Get the file/line number for this label
1989 const char *file
= dwarf_decl_file (&die
) ?: "<unknown source>";
1990 dwarf_decl_line (&die
, &dline
);
1992 vector
<Dwarf_Die
> scopes
= getscopes_die(&die
);
1993 if (scopes
.size() > 1)
1996 if (!inner_die_containing_pc(scopes
[1], stmt_addr
, scope
))
1998 sess
.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not "
1999 "contained by its scope '%s' (dieoffset: %s) -- bad"
2000 " debuginfo? [man error::dwarf]", name
, lex_cast_hex(stmt_addr
).c_str(),
2001 lex_cast_hex(dwarf_dieoffset(&die
)).c_str(),
2002 (dwarf_diename(&scope
) ?: "<unknown>"),
2003 lex_cast_hex(dwarf_dieoffset(&scope
)).c_str()));
2006 bool matches_lineno
;
2007 if (lineno_type
== ABSOLUTE
)
2008 matches_lineno
= dline
== linenos
[0];
2009 else if (lineno_type
== RELATIVE
)
2010 matches_lineno
= dline
== linenos
[0] + function
.decl_line
;
2011 else if (lineno_type
== ENUMERATED
)
2012 matches_lineno
= (binary_search(linenos
.begin(), linenos
.end(), dline
));
2014 matches_lineno
= true;
2017 callback(function
, name
, file
, dline
,
2018 &scope
, stmt_addr
, data
);
2024 case DW_TAG_subprogram
:
2025 case DW_TAG_inlined_subroutine
:
2026 // Stay within our filtered function
2029 case DW_TAG_imported_unit
:
2030 // Iterate over the children of the imported unit as if they
2031 // were inserted in place.
2032 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2033 iterate_over_labels (&import
, sym
, function
, linenos
,
2034 lineno_type
, data
, callback
);
2038 if (dwarf_haschildren (&die
))
2039 iterate_over_labels (&die
, sym
, function
, linenos
,
2040 lineno_type
, data
, callback
);
2044 while (dwarf_siblingof (&die
, &die
) == 0);
2047 // Mini 'query-like' struct to help us navigate callbacks during
2048 // external function resolution
2049 struct external_function_query
{
2055 external_function_query(dwflpp
* dw
, const string
& name
):
2056 dw(dw
), name(name
), addr(0), resolved(false) {}
2060 dwflpp::external_function_cu_callback (Dwarf_Die
* cu
, external_function_query
*efq
)
2062 efq
->dw
->focus_on_cu(cu
);
2063 return efq
->dw
->iterate_over_functions(external_function_func_callback
,
2068 dwflpp::external_function_func_callback (Dwarf_Die
* func
, external_function_query
*efq
)
2070 Dwarf_Attribute external
;
2071 Dwarf_Addr func_addr
;
2072 if (dwarf_attr_integrate(func
, DW_AT_external
, &external
) != NULL
&&
2073 dwarf_lowpc(func
, &func_addr
) == 0)
2076 efq
->addr
= func_addr
;
2077 efq
->resolved
= true;
2078 return DWARF_CB_ABORT
; // we found it so stop here
2084 dwflpp::iterate_over_callees
<void>(Dwarf_Die
*begin_die
,
2086 long recursion_depth
,
2088 void (* callback
)(base_func_info
&,
2092 base_func_info
& caller
,
2093 stack
<Dwarf_Addr
> *callers
)
2097 Dwarf_Die die
, import
;
2099 // DIE of abstract_origin found in die
2102 // callee's entry pc (= where we'll probe)
2103 Dwarf_Addr func_addr
;
2105 // caller's unwind pc during call (to match against bt for filtering)
2106 Dwarf_Addr caller_uw_addr
;
2108 Dwarf_Attribute attr
;
2110 base_func_info callee
;
2111 if (dwarf_child(begin_die
, &die
) != 0)
2112 return; // die without children, bail out.
2114 bool free_callers
= false;
2115 if (callers
== NULL
) /* first call */
2117 callers
= new stack
<Dwarf_Addr
>();
2118 free_callers
= true;
2123 bool inlined
= false;
2124 switch (dwarf_tag(&die
))
2126 case DW_TAG_inlined_subroutine
:
2128 /* FALLTHROUGH */ /* thanks mjw */
2129 case DW_TAG_GNU_call_site
:
2130 callee
.name
= dwarf_diename(&die
) ?: "";
2131 if (callee
.name
.empty())
2133 if (callee
.name
!= sym
)
2135 if (!name_has_wildcard(sym
))
2137 if (!function_name_matches_pattern(callee
.name
, sym
))
2141 /* In both cases (call sites and inlines), we want the
2142 * abstract_origin. The difference is that in inlines, the addr is
2143 * in the die itself, whereas for call sites, the addr is in the
2144 * abstract_origin's die.
2145 * Note that in the case of inlines, we're only calling back
2146 * for that inline instance, not all. This is what we want, since
2147 * it will only be triggered when 'called' from the target func,
2148 * which is something we have to emulate for non-inlined funcs
2149 * (which is the purpose of the caller_uw_addr below) */
2150 if (dwarf_attr_die(&die
, DW_AT_abstract_origin
, &origin
) == NULL
)
2153 // the low_pc of the die in either cases is the pc that would
2154 // show up in a backtrace (inlines are a special case in which
2155 // the die's low_pc is also the abstract_origin's low_pc = the
2156 // 'start' of the inline instance)
2157 if (dwarf_lowpc(&die
, &caller_uw_addr
) != 0)
2161 func_addr
= caller_uw_addr
;
2162 else if (dwarf_lowpc(&origin
, &func_addr
) != 0)
2164 // function doesn't have a low_pc, is it external?
2165 if (dwarf_attr_integrate(&origin
, DW_AT_external
,
2168 // let's iterate over the CUs and find it. NB: it's
2169 // possible we could have also done this by creating a
2170 // probe point with .exported tacked on and rerunning it
2171 // through derive_probe(). But since we're already on the
2172 // dwflpp side of things, and we already have access to
2173 // everything we need, let's try to be self-sufficient.
2175 // remember old focus
2176 Dwarf_Die
*old_cu
= cu
;
2178 external_function_query
efq(this, dwarf_linkage_name(&origin
) ?: callee
.name
);
2179 iterate_over_cus(external_function_cu_callback
, &efq
, false);
2184 if (!efq
.resolved
) // did we resolve it?
2187 func_addr
= efq
.addr
;
2190 // non-external function without low_pc, jump ship
2194 // We now have the addr to probe in func_addr, and the DIE
2195 // from which to obtain file/line info in origin
2197 // Get the file/line number for this callee
2198 callee
.decl_file
= dwarf_decl_file (&origin
) ?: "<unknown source>";
2199 dwarf_decl_line (&origin
, &callee
.decl_line
);
2201 // add as a caller to match against
2203 callers
->push(caller_uw_addr
);
2205 callee
.die
= inlined
? die
: origin
;
2206 callee
.entrypc
= func_addr
;
2207 callback(callee
, caller
, callers
, data
);
2209 // If it's a tail call, print a warning that it may not be caught
2211 && dwarf_attr_integrate(&die
, DW_AT_GNU_tail_call
, &attr
) != NULL
)
2212 sess
.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: "
2213 ".callee probe may not fire. Try placing the probe "
2214 "directly on the callee function instead.",
2215 callee
.name
.c_str(), caller
.name
.c_str()));
2217 // For .callees(N) probes, we recurse on this callee. Note that we
2218 // pass the callee we just found as the caller arg for this recursion,
2219 // since it (the callee we just found) will be the caller of whatever
2220 // callees found inside this recursion.
2221 if (recursion_depth
> 1)
2222 iterate_over_callees(inlined
? &die
: &origin
,
2223 sym
, recursion_depth
-1, data
,
2224 callback
, callee
, callers
);
2230 case DW_TAG_subprogram
:
2231 break; // don't leave our filtered func
2233 case DW_TAG_imported_unit
:
2234 // Iterate over the children of the imported unit as if they
2235 // were inserted in place.
2236 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2237 // NB: we pass the same caller arg into it
2238 iterate_over_callees (&import
, sym
, recursion_depth
, data
,
2239 callback
, caller
, callers
);
2243 if (dwarf_haschildren (&die
))
2244 // NB: we pass the same caller arg into it
2245 iterate_over_callees (&die
, sym
, recursion_depth
, data
,
2246 callback
, caller
, callers
);
2250 while (dwarf_siblingof (&die
, &die
) == 0);
2252 if (free_callers
&& callers
!= NULL
)
2258 dwflpp::collect_srcfiles_matching (string
const & pattern
,
2259 set
<string
> & filtered_srcfiles
)
2265 Dwarf_Files
*srcfiles
;
2267 // PR 5049: implicit * in front of given path pattern.
2268 // NB: fnmatch() is used without FNM_PATHNAME.
2269 string prefixed_pattern
= string("*/") + pattern
;
2271 DWARF_ASSERT ("dwarf_getsrcfiles",
2272 dwarf_getsrcfiles (cu
, &srcfiles
, &nfiles
));
2274 for (size_t i
= 0; i
< nfiles
; ++i
)
2276 char const * fname
= dwarf_filesrc (srcfiles
, i
, NULL
, NULL
);
2277 if (fnmatch (pattern
.c_str(), fname
, 0) == 0 ||
2278 fnmatch (prefixed_pattern
.c_str(), fname
, 0) == 0)
2280 filtered_srcfiles
.insert (fname
);
2282 clog
<< _F("selected source file '%s'\n", fname
);
2290 dwflpp::resolve_prologue_endings (func_info_map_t
& funcs
)
2292 // When a program is compiled with no optimization, GCC does no variable
2293 // tracking, which means that location info is actually only really valid
2294 // after the prologue, even though GCC reports it as valid during. So we need
2295 // to find the prologue ends to get accurate info. This may or may not be the
2296 // first address that has a source line distinct from the function
2303 Dwarf_Lines
*lines
= NULL
;
2306 malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue
2307 sys_get?id in kernel/timer.c no-prologue
2308 sys_exit_group tail-recursive
2309 {do_,}sys_open extra-long-prologue (gcc 3.4)
2310 cpu_to_logical_apicid NULL-decl_file
2313 // Fetch all srcline records, sorted by address. No need to free lines, it's a
2314 // direct pointer to the CU's cached lines.
2315 if (dwarf_getsrclines(cu
, &lines
, &nlines
) != 0
2316 || lines
== NULL
|| nlines
== 0)
2318 if (sess
.verbose
> 2)
2319 clog
<< _F("aborting prologue search: no source lines found for cu '%s'\n",
2324 // Dump them into our own array for easier searching. They should already be
2325 // sorted by addr, but we doublecheck that here. We want to keep the indices
2326 // between lines and addrs the same.
2327 vector
<Dwarf_Addr
> addrs
;
2328 for (size_t i
= 0; i
< nlines
; i
++)
2330 Dwarf_Line
* line
= dwarf_onesrcline(lines
, i
);
2331 Dwarf_Addr addr
= DWARF_LINEADDR(line
);
2332 if (!addrs
.empty() && addr
< addrs
.back())
2333 throw SEMANTIC_ERROR(_("lines from dwarf_getsrclines() not sorted"));
2334 addrs
.push_back(addr
);
2336 // We normally ignore a function's decl_line, since it is associated with the
2337 // line at which the identifier appears in the declaration, and has no
2338 // meaningful relation to the lineno associated with the entrypc (which is
2339 // normally the lineno of '{', which could occur at the same line as the
2340 // declaration, or lower down).
2341 // However, if the CU was compiled using GCC < 4.4, then the decl_line
2342 // actually represents the lineno of '{' as well, in which case if the lineno
2343 // associated with the entrypc is != to the decl_line, it means the compiler
2344 // scraped/optimized off some of the beginning of the function and the safest
2345 // thing we can do is consider it naked.
2346 bool consider_decl_line
= false;
2349 if (is_gcc_producer(cu
, prod
, vers
)
2350 && strverscmp(vers
.c_str(), "4.4.0") < 0)
2351 consider_decl_line
= true;
2354 for(func_info_map_t::iterator it
= funcs
.begin(); it
!= funcs
.end(); it
++)
2357 Dwarf_Addr
* bkpts
= 0;
2358 int n
= dwarf_entry_breakpoints (& it
->die
, & bkpts
);
2363 Dwarf_Addr entrypc
= it
->entrypc
;
2364 Dwarf_Addr highpc
; // NB: highpc is exclusive: [entrypc,highpc)
2365 DWFL_ASSERT ("dwarf_highpc", dwarf_highpc (& it
->die
,
2368 if (it
->decl_file
== 0) it
->decl_file
= "";
2370 unsigned entrypc_srcline_idx
= 0;
2371 Dwarf_Line
*entrypc_srcline
= NULL
;
2373 vector
<Dwarf_Addr
>::const_iterator it_addr
=
2374 lower_bound(addrs
.begin(), addrs
.end(), entrypc
);
2375 if (it_addr
!= addrs
.end() && *it_addr
== entrypc
)
2377 entrypc_srcline_idx
= it_addr
- addrs
.begin();
2378 entrypc_srcline
= dwarf_onesrcline(lines
, entrypc_srcline_idx
);
2382 if (!entrypc_srcline
)
2384 if (sess
.verbose
> 2)
2385 clog
<< _F("missing entrypc dwarf line record for function '%s'\n",
2387 // This is probably an inlined function. We'll end up using
2388 // its lowpc as a probe address.
2394 if (sess
.verbose
> 2)
2395 clog
<< _F("null entrypc dwarf line record for function '%s'\n",
2397 // This is probably an inlined function. We'll skip this instance;
2403 clog
<< _F("searching for prologue of function '%s' %#" PRIx64
"-%#" PRIx64
2404 "@%s:%d\n", it
->name
.c_str(), entrypc
, highpc
, it
->decl_file
,
2407 // For each function, we look for the prologue-end marker (e.g. clang
2408 // outputs one). If there is no explicit marker (e.g. GCC does not), we
2409 // accept a bigger or equal lineno as a prologue end (this catches GCC's
2410 // 0-line advances).
2412 // We may have to skip a few because some old compilers plop
2413 // in dummy line records for longer prologues. If we go too
2414 // far (addr >= highpc), we take the previous one. Or, it may
2415 // be the first one, if the function had no prologue, and thus
2416 // the entrypc maps to a statement in the body rather than the
2419 int entrypc_srcline_lineno
= DWARF_LINENO(entrypc_srcline
);
2420 unsigned postprologue_srcline_idx
= entrypc_srcline_idx
;
2421 Dwarf_Line
*postprologue_srcline
= entrypc_srcline
;
2423 while (postprologue_srcline_idx
< nlines
)
2425 postprologue_srcline
= dwarf_onesrcline(lines
,
2426 postprologue_srcline_idx
);
2427 Dwarf_Addr lineaddr
= DWARF_LINEADDR(postprologue_srcline
);
2428 const char* linesrc
= DWARF_LINESRC(postprologue_srcline
);
2429 int lineno
= DWARF_LINENO(postprologue_srcline
);
2430 bool lineprologue_end
= DWARF_LINEPROLOGUEEND(postprologue_srcline
);
2433 clog
<< _F("checking line record %#" PRIx64
"@%s:%d%s\n", lineaddr
,
2434 linesrc
, lineno
, lineprologue_end
? " (marked)" : "");
2436 // have we passed the function?
2437 if (lineaddr
>= highpc
)
2439 // is there an explicit prologue_end marker?
2440 if (lineprologue_end
)
2442 // is it a different file?
2443 if (strcmp(linesrc
, it
->decl_file
))
2445 // OK, it's the same file, but is it a different line?
2446 if (lineno
!= entrypc_srcline_lineno
)
2448 // Same file and line, is this a second line record (e.g. 0-line advance)?
2449 if (postprologue_srcline_idx
!= entrypc_srcline_idx
)
2451 // This is the first iteration. Is decl_line meaningful and is the
2452 // lineno past the decl_line?
2453 if (consider_decl_line
&& lineno
!= it
->decl_line
)
2456 // Let's try the next srcline.
2457 postprologue_srcline_idx
++;
2459 } // loop over srclines
2462 Dwarf_Addr postprologue_addr
= DWARF_LINEADDR(postprologue_srcline
);
2463 if (postprologue_addr
>= highpc
)
2465 // pick addr of previous line record
2466 Dwarf_Line
*lr
= dwarf_onesrcline(lines
, postprologue_srcline_idx
-1);
2467 postprologue_addr
= DWARF_LINEADDR(lr
);
2470 it
->prologue_end
= postprologue_addr
;
2474 clog
<< _F("prologue found function '%s'", it
->name
.c_str());
2475 // Add a little classification datum
2476 //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free)
2477 if (postprologue_addr
== entrypc
)
2478 clog
<< _(" (naked)");
2479 //TRANSLATORS: Here we're adding some classification datum (ie we went over)
2480 if (DWARF_LINEADDR(postprologue_srcline
) >= highpc
)
2481 clog
<< _(" (tail-call?)");
2482 //TRANSLATORS: Here we're adding some classification datum (ie it was marked)
2483 if (DWARF_LINEPROLOGUEEND(postprologue_srcline
))
2484 clog
<< _(" (marked)");
2486 clog
<< " = 0x" << hex
<< postprologue_addr
<< dec
<< "\n";
2489 } // loop over functions
2494 dwflpp::function_entrypc (Dwarf_Addr
* addr
)
2497 // PR10574: reject 0, which tends to be eliminated COMDAT
2498 return (dwarf_entrypc (function
, addr
) == 0 && *addr
!= 0);
2503 dwflpp::die_entrypc (Dwarf_Die
* die
, Dwarf_Addr
* addr
)
2506 string lookup_method
;
2510 lookup_method
= "dwarf_entrypc";
2511 rc
= dwarf_entrypc (die
, addr
);
2515 lookup_method
= "dwarf_ranges";
2520 ptrdiff_t offset
= dwarf_ranges (die
, 0, &base
, &begin
, &end
);
2521 if (offset
< 0) rc
= -1;
2522 else if (offset
> 0)
2527 // Now we need to check that there are no more ranges
2528 // associated with this function, which could conceivably
2529 // happen if a function is inlined, then pieces of it are
2530 // split amongst different conditional branches. It's not
2531 // obvious which of them to favour. As a heuristic, we
2532 // pick the beginning of the first range, and ignore the
2533 // others (but with a warning).
2536 while ((offset
= dwarf_ranges (die
, offset
, &base
, &begin
, &end
)) > 0)
2539 lookup_method
+= _F(", ignored %s more", lex_cast(extra
).c_str());
2543 // PR10574: reject subprograms where the entrypc address turns out
2544 // to be 0, since they tend to correspond to duplicate-eliminated
2545 // COMDAT copies of C++ functions.
2546 if (rc
== 0 && *addr
== 0)
2548 lookup_method
+= _(" (skip comdat)");
2552 if (sess
.verbose
> 2)
2553 clog
<< _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64
" (rc %d)", lookup_method
.c_str(),
2554 lex_cast_hex(dwarf_dieoffset(die
)).c_str(), *addr
, rc
) << endl
;
2561 dwflpp::function_die (Dwarf_Die
*d
)
2569 dwflpp::function_file (char const ** c
)
2573 *c
= dwarf_decl_file (function
);
2576 // The line table might know.
2578 if (dwarf_lowpc(function
, &pc
) == 0)
2579 *c
= pc_line (pc
, NULL
, NULL
);
2582 *c
= "<unknown source>";
2588 dwflpp::function_line (int *linep
)
2591 if (dwarf_decl_line (function
, linep
) != 0)
2593 // The line table might know.
2595 if (dwarf_lowpc(function
, &pc
) == 0)
2596 pc_line (pc
, linep
, NULL
);
2602 dwflpp::die_has_pc (Dwarf_Die
& die
, Dwarf_Addr pc
)
2604 int res
= dwarf_haspc (&die
, pc
);
2605 // dwarf_ranges will return -1 if a function die has no DW_AT_ranges
2607 // DWARF_ASSERT ("dwarf_haspc", res);
2613 dwflpp::inner_die_containing_pc(Dwarf_Die
& scope
, Dwarf_Addr addr
,
2618 // Sometimes we're in a bad scope to begin with -- just let it be. This can
2619 // happen for example if the compiler outputs a label PC that's just outside
2620 // the lexical scope. We can't really do anything about that, but variables
2621 // will probably not be accessible in this case.
2622 if (!die_has_pc(scope
, addr
))
2625 Dwarf_Die child
, import
;
2626 int rc
= dwarf_child(&result
, &child
);
2629 switch (dwarf_tag (&child
))
2631 case DW_TAG_imported_unit
:
2632 // The children of the imported unit need to be treated as if
2633 // they are inserted here. So look inside and set result if
2635 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2637 Dwarf_Die import_result
;
2638 if (inner_die_containing_pc(import
, addr
, import_result
))
2640 result
= import_result
;
2646 // lexical tags to recurse within the same starting scope
2647 // NB: this intentionally doesn't cross into inlines!
2648 case DW_TAG_lexical_block
:
2649 case DW_TAG_with_stmt
:
2650 case DW_TAG_catch_block
:
2651 case DW_TAG_try_block
:
2652 case DW_TAG_entry_point
:
2653 if (die_has_pc(child
, addr
))
2656 rc
= dwarf_child(&result
, &child
);
2660 rc
= dwarf_siblingof(&child
, &child
);
2667 dwflpp::loc2c_error (void *arg
, const char *fmt
, ...)
2669 const char *msg
= "?";
2674 rc
= vasprintf (& tmp
, fmt
, ap
);
2681 dwflpp
*pp
= (dwflpp
*) arg
;
2682 semantic_error
err(ERR_SRC
, msg
);
2683 string what
= pp
->die_location_as_string(pp
->l2c_ctx
.pc
, pp
->l2c_ctx
.die
);
2684 string where
= pp
->die_location_as_function_string (pp
->l2c_ctx
.pc
,
2686 err
.details
.push_back(what
);
2687 err
.details
.push_back(where
);
2692 // This function generates code used for addressing computations of
2693 // target variables.
2695 dwflpp::emit_address (struct obstack
*pool
, Dwarf_Addr address
)
2697 int n
= dwfl_module_relocations (module
);
2698 DWFL_ASSERT ("dwfl_module_relocations", n
>= 0);
2699 Dwarf_Addr reloc_address
= address
;
2700 const char *secname
= "";
2703 int i
= dwfl_module_relocate_address (module
, &reloc_address
);
2704 DWFL_ASSERT ("dwfl_module_relocate_address", i
>= 0);
2705 secname
= dwfl_module_relocation_info (module
, i
, NULL
);
2708 if (sess
.verbose
> 2)
2710 clog
<< _F("emit dwarf addr %#" PRIx64
" => module %s section %s relocaddr %#" PRIx64
,
2711 address
, module_name
.c_str (), (secname
?: "null"),
2712 reloc_address
) << endl
;
2715 if (n
> 0 && !(n
== 1 && secname
== NULL
))
2717 DWFL_ASSERT ("dwfl_module_relocation_info", secname
);
2718 if (n
> 1 || secname
[0] != '\0')
2720 // This gives us the module name, and section name within the
2721 // module, for a kernel module (or other ET_REL module object).
2722 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2723 obstack_printf (pool
, "addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2724 module_name
.c_str(), secname
, reloc_address
);
2725 obstack_printf (pool
, "addr; })");
2727 else if (n
== 1 && module_name
== TOK_KERNEL
&& secname
[0] == '\0')
2729 // elfutils' way of telling us that this is a relocatable kernel address, which we
2730 // need to treat the same way here as dwarf_query::add_probe_point does: _stext.
2731 address
-= sess
.sym_stext
;
2733 // Note we "cache" the result here through a static because the
2734 // kernel will never move after being loaded (unlike modules and
2735 // user-space dynamic share libraries).
2736 obstack_printf (pool
, "({ static unsigned long addr = 0; ");
2737 obstack_printf (pool
, "if (addr==0) addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2738 module_name
.c_str(), secname
, address
); // PR10000 NB: not reloc_address
2739 obstack_printf (pool
, "addr; })");
2743 obstack_printf (pool
, "/* pragma:vma */");
2744 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2745 obstack_printf (pool
, "addr = _stp_umodule_relocate (\"%s\",%#" PRIx64
", current); ",
2746 resolve_path(module_name
.c_str()).c_str(), address
);
2747 obstack_printf (pool
, "addr; })");
2751 obstack_printf (pool
, "%#" PRIx64
"UL", address
); // assume as constant
2756 dwflpp::loc2c_emit_address (void *arg
, struct obstack
*pool
,
2759 static_cast<dwflpp
*>(arg
)->emit_address (pool
, address
);
2764 dwflpp::get_locals(vector
<Dwarf_Die
>& scopes
, set
<string
>& locals
)
2766 // XXX Shouldn't this be walking up to outer scopes too?
2768 get_locals_die(scopes
[0], locals
);
2772 dwflpp::get_locals_die(Dwarf_Die
& die
, set
<string
>& locals
)
2774 // Try to get the first child of die.
2775 Dwarf_Die child
, import
;
2776 if (dwarf_child (&die
, &child
) == 0)
2781 // Output each sibling's name (that is a variable or
2782 // parameter) to 'o'.
2783 switch (dwarf_tag (&child
))
2785 case DW_TAG_variable
:
2786 case DW_TAG_formal_parameter
:
2787 name
= dwarf_diename (&child
);
2789 locals
.insert(string("$") + name
);
2791 case DW_TAG_imported_unit
:
2792 // Treat the imported unit children as if they are
2793 // children of the given DIE.
2794 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2795 get_locals_die (import
, locals
);
2801 while (dwarf_siblingof (&child
, &child
) == 0);
2807 dwflpp::find_variable_and_frame_base (vector
<Dwarf_Die
>& scopes
,
2809 string
const & local
,
2810 const target_symbol
*e
,
2812 Dwarf_Attribute
*fb_attr_mem
)
2814 Dwarf_Die
*scope_die
= &scopes
[0];
2815 Dwarf_Attribute
*fb_attr
= NULL
;
2819 int declaring_scope
= dwarf_getscopevar (&scopes
[0], scopes
.size(),
2823 if (declaring_scope
< 0)
2826 get_locals(scopes
, locals
);
2827 string sugs
= levenshtein_suggest(local
, locals
); // probably not that many, so no limit
2829 throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)",
2831 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2832 module_name
.c_str(),
2833 lex_cast_hex(pc
).c_str(),
2834 (scope_die
== NULL
) ? "" : _("in"),
2835 (dwarf_diename(scope_die
) ?: "<unknown>"),
2836 (dwarf_diename(cu
) ?: "<unknown>"),
2838 ? (_("<no alternatives>"))
2839 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2842 throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)",
2844 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2845 module_name
.c_str(),
2846 (scope_die
== NULL
) ? "" : _("in"),
2847 (dwarf_diename(scope_die
) ?: "<unknown>"),
2850 ? (_("<no alternatives>"))
2851 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2855 /* Some GCC versions would output duplicate external variables, one
2856 without a location attribute. If so, try to find the other if it
2857 exists in the same scope. See GCC PR51410. */
2858 Dwarf_Attribute attr_mem
;
2859 if (dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
) == NULL
2860 && dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
) == NULL
2861 && dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
) != NULL
2862 && dwarf_tag(&scopes
[declaring_scope
]) == DW_TAG_compile_unit
)
2864 Dwarf_Die orig_vardie
= *vardie
;
2865 bool alt_found
= false;
2866 if (dwarf_child(&scopes
[declaring_scope
], vardie
) == 0)
2869 // Note, not handling DW_TAG_imported_unit, assuming GCC
2870 // version is recent enough to not need this workaround if
2871 // we would see an imported unit.
2872 if (dwarf_tag (vardie
) == DW_TAG_variable
2873 && strcmp (dwarf_diename (vardie
), local
.c_str ()) == 0
2874 && (dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
)
2876 && ((dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
)
2878 || (dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
)
2882 while (!alt_found
&& dwarf_siblingof(vardie
, vardie
) == 0);
2885 *vardie
= orig_vardie
;
2888 // Global vars don't need (cannot use) frame base in location descriptor.
2892 /* We start out walking the "lexical scopes" as returned by
2893 * as returned by dwarf_getscopes for the address, starting with the
2894 * declaring_scope that the variable was found in.
2896 vector
<Dwarf_Die
> physcopes
, *fbscopes
= &scopes
;
2897 for (size_t inner
= declaring_scope
;
2898 inner
< fbscopes
->size() && fb_attr
== NULL
;
2901 Dwarf_Die
& scope
= (*fbscopes
)[inner
];
2902 switch (dwarf_tag (&scope
))
2906 case DW_TAG_subprogram
:
2907 case DW_TAG_entry_point
:
2908 fb_attr
= dwarf_attr_integrate (&scope
,
2912 case DW_TAG_inlined_subroutine
:
2913 /* Unless we already are going through the "pyshical die tree",
2914 * we now need to start walking the die tree where this
2915 * subroutine is inlined to find the appropriate frame base. */
2916 if (declaring_scope
!= -1)
2918 physcopes
= getscopes_die(&scope
);
2919 if (physcopes
.empty())
2920 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
2921 local
.c_str()), e
->tok
);
2922 fbscopes
= &physcopes
;
2923 inner
= 0; // zero is current scope, for look will increase.
2924 declaring_scope
= -1;
2933 /* Returns a human readable string with suggested locations where a
2934 DIE attribute is valid. */
2936 suggested_locations_string(Dwarf_Attribute
*attr
)
2940 locsstr
= "<no alternatives for NULL attribute>";
2943 #if _ELFUTILS_PREREQ (0, 158)
2946 Dwarf_Addr base
, start
, end
;
2949 off
= dwarf_getlocations (attr
, off
, &base
,
2954 locsstr
= _("alternative locations: ");
2959 locsstr
+= lex_cast_hex(start
);
2961 locsstr
+= lex_cast_hex(end
);
2964 off
= dwarf_getlocations (attr
, off
, &base
,
2972 locsstr
= _("<no alternative locations>");
2974 locsstr
= _F("<error getting alternative locations: %s>",
2977 locsstr
= "<cannot suggest any alternative locations, elfutils too old>";
2978 #endif /* _ELFUTILS_PREREQ (0, 158) */
2984 /* Produce a human readable name for a DIE. */
2986 die_name_string (Dwarf_Die
*die
)
2989 const char *name
= dwarf_linkage_name(die
);
2991 name
= dwarf_diename (die
);
2993 size_t demangle_buffer_len
= 0;
2994 char *demangle_buffer
= NULL
;
2995 if (name
!= NULL
&& name
[0] == '_' && name
[1] == 'Z')
2998 char *dsymname
= abi::__cxa_demangle (name
, demangle_buffer
,
2999 &demangle_buffer_len
, &status
);
3001 name
= demangle_buffer
= dsymname
;
3006 res
= _("<unknown");
3007 free (demangle_buffer
);
3012 /* Returns a source file name, line and column information based on the
3013 pc and the current cu. */
3015 dwflpp::pc_line (Dwarf_Addr pc
, int *lineno
, int *colno
)
3019 Dwarf_Line
*line
= dwarf_getsrc_die (cu
, pc
);
3023 dwarf_lineno (line
, lineno
);
3025 dwarf_linecol (line
, colno
);
3026 return dwarf_linesrc (line
, NULL
, NULL
);
3033 /* Returns a source line and column string based on the inlined DIE
3034 or based on the pc if DIE is NULL. */
3036 dwflpp::pc_die_line_string (Dwarf_Addr pc
, Dwarf_Die
*die
)
3041 const char *src
= NULL
;
3045 src
= pc_line (pc
, &lineno
, &col
);
3049 if (dwarf_getsrcfiles (cu
, &files
, NULL
) == 0)
3051 Dwarf_Attribute attr
;
3053 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_file
, &attr
),
3056 src
= dwarf_filesrc (files
, val
, NULL
, NULL
);
3057 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_line
,
3061 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_column
,
3068 src
= pc_line (pc
, &lineno
, &col
);
3076 linestr
+= ":" + lex_cast(lineno
);
3078 linestr
+= ":" + lex_cast(col
);
3082 linestr
+= _("unknown source");
3087 /* Returns a human readable DIE offset for use in error messages.
3088 Includes DIE offset and DWARF file used. */
3090 dwflpp::die_location_as_string(Dwarf_Addr pc
, Dwarf_Die
*die
)
3095 locstr
+= _("dieoffset: ");
3096 locstr
+= lex_cast_hex(dwarf_dieoffset(die
));
3099 const char *debugfile
;
3100 locstr
+= _(" from ");
3101 if (dwfl_module_info (module
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
3102 &debugfile
) == NULL
|| debugfile
== NULL
)
3104 locstr
+= _("unknown debug file for ");
3105 locstr
+= module_name
;
3108 locstr
+= debugfile
;
3113 /* Returns a human readable (inlined) function and source file/line location
3114 for a DIE and pc location. */
3116 dwflpp::die_location_as_function_string(Dwarf_Addr pc
, Dwarf_Die
*die
)
3119 locstr
= _("function: ");
3121 /* Find the first function-like DIE with a name in scope. */
3122 Dwarf_Die funcdie_mem
;
3123 Dwarf_Die
*funcdie
= NULL
;
3124 string funcname
= "";
3125 Dwarf_Die
*scopes
= NULL
;
3126 int nscopes
= dwarf_getscopes (cu
, pc
, &scopes
);
3127 for (int i
= 0; funcname
== "" && i
< nscopes
; i
++)
3129 Dwarf_Die
*scope
= &scopes
[i
];
3130 int tag
= dwarf_tag (scope
);
3131 if (tag
== DW_TAG_subprogram
3132 || tag
== DW_TAG_inlined_subroutine
3133 || tag
== DW_TAG_entry_point
)
3134 funcname
= die_name_string (scope
);
3137 funcdie_mem
= *scope
;
3138 funcdie
= &funcdie_mem
;
3143 /* source location */
3145 locstr
+= _("<unknown> at ") + pc_die_line_string (pc
, NULL
);
3148 int nscopes
= dwarf_getscopes_die (funcdie
, &scopes
);
3151 /* scopes[0] == funcdie, the lowest level, for which we already have
3152 the name. This is the actual source location where it
3155 locstr
+= _(" at ");
3156 locstr
+= pc_die_line_string (pc
, NULL
);
3158 /* last_scope is the source location where the next inlined frame/function
3160 Dwarf_Die
*last_scope
= &scopes
[0];
3161 for (int i
= 1; i
< nscopes
; i
++)
3163 Dwarf_Die
*scope
= &scopes
[i
];
3164 int tag
= dwarf_tag (scope
);
3165 if (tag
!= DW_TAG_inlined_subroutine
3166 && tag
!= DW_TAG_entry_point
3167 && tag
!= DW_TAG_subprogram
)
3170 locstr
+= _(" inlined by ");
3171 locstr
+= die_name_string (scope
);
3172 locstr
+= _(" at ");
3173 locstr
+= pc_die_line_string (pc
, last_scope
);
3175 /* Found the "top-level" in which everything was inlined. */
3176 if (tag
== DW_TAG_subprogram
)
3185 locstr
+= _(" at ");
3186 locstr
+= pc_die_line_string (pc
, NULL
);
3195 dwflpp::translate_location(struct obstack
*pool
,
3196 Dwarf_Attribute
*attr
, Dwarf_Die
*die
,
3198 Dwarf_Attribute
*fb_attr
,
3199 struct location
**tail
,
3200 const target_symbol
*e
)
3203 /* DW_AT_data_member_location, can be either constant offsets
3204 (struct member fields), or full blown location expressions. */
3206 /* There is no location expression, but a constant value instead. */
3207 if (dwarf_whatattr (attr
) == DW_AT_const_value
)
3211 *tail
= c_translate_constant (pool
, &loc2c_error
, this,
3212 &loc2c_emit_address
, 0, pc
, attr
);
3219 /* PR9768: formerly, we added pc+module_bias here. However, that bias value
3220 is not present in the pc value by the time we get it, so adding it would
3221 result in false negatives of variable reachibility. In other instances
3222 further below, the c_translate_FOO functions, the module_bias value used
3223 to be passed in, but instead should now be zero for the same reason. */
3226 switch (dwarf_getlocation_addr (attr
, pc
/*+ module_bias*/, &expr
, &len
, 1))
3228 case 1: /* Should always happen. */
3233 case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */
3235 Dwarf_Addr pc2
= pr15123_retry_addr (pc
, die
);
3244 string msg
= _F("not accessible at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3245 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3246 err
.details
.push_back(die_location_as_string(pc
, die
));
3247 err
.details
.push_back(die_location_as_function_string(pc
, die
));
3248 err
.details
.push_back(suggested_locations_string(attr
));
3252 default: /* Shouldn't happen. */
3255 string msg
= _F("dwarf_getlocation_addr failed at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3256 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3257 string dwarf_err
= _F("dwarf_error: %s", dwarf_errmsg(-1));
3258 err
.details
.push_back(dwarf_err
);
3259 err
.details
.push_back(die_location_as_string(pc
, die
));
3260 err
.details
.push_back(die_location_as_function_string(pc
, die
));
3261 err
.details
.push_back(suggested_locations_string(attr
));
3267 // pc is in the dw address space of the current module, which is what
3268 // c_translate_location expects. get_cfa_ops wants the global dwfl address.
3269 // cfa_ops only make sense for locals.
3272 Dwarf_Addr addr
= pc
+ module_bias
;
3273 cfa_ops
= get_cfa_ops (addr
);
3280 return c_translate_location (pool
, &loc2c_error
, this,
3281 &loc2c_emit_address
,
3283 pc
, attr
, expr
, len
, tail
, fb_attr
, cfa_ops
);
3288 dwflpp::get_members(Dwarf_Die
*typedie
, set
<string
>& members
, set
<string
> &dupes
)
3290 const int typetag
= dwarf_tag (typedie
);
3292 /* compile and partial unit included for recursion through
3293 imported_unit below. */
3294 if (typetag
!= DW_TAG_structure_type
&&
3295 typetag
!= DW_TAG_class_type
&&
3296 typetag
!= DW_TAG_union_type
&&
3297 typetag
!= DW_TAG_compile_unit
&&
3298 typetag
!= DW_TAG_partial_unit
)
3300 throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union",
3301 dwarf_type_name(typedie
).c_str()));
3304 // Try to get the first child of vardie.
3305 Dwarf_Die die_mem
, import
;
3306 Dwarf_Die
*die
= &die_mem
;
3307 switch (dwarf_child (typedie
, die
))
3309 case 1: // No children.
3310 throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie
).c_str()));
3313 default: // Shouldn't happen.
3314 throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie
).c_str(),
3321 // Add each sibling's name to members set
3324 int tag
= dwarf_tag(die
);
3326 /* The children of an imported_unit should be treated as members too. */
3327 if (tag
== DW_TAG_imported_unit
3328 && dwarf_attr_die(die
, DW_AT_import
, &import
))
3329 get_members(&import
, members
, dupes
);
3331 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3334 const char *member
= dwarf_diename (die
) ;
3336 if ( tag
== DW_TAG_member
&& member
!= NULL
)
3338 // Only output if this is new, to avoid inheritance dupes.
3339 if (dupes
.insert(member
).second
)
3340 members
.insert(member
);
3345 if (!dwarf_attr_die (die
, DW_AT_type
, &temp_die
))
3347 string source
= dwarf_decl_file(die
) ?: "<unknown source>";
3349 dwarf_decl_line(die
, &line
);
3350 throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous "
3351 "member at %s:%d", source
.c_str(), line
));
3354 get_members(&temp_die
, members
, dupes
);
3358 while (dwarf_siblingof (die
, die
) == 0);
3363 dwflpp::find_struct_member(const target_symbol::component
& c
,
3364 Dwarf_Die
*parentdie
,
3365 Dwarf_Die
*memberdie
,
3366 vector
<Dwarf_Die
>& dies
,
3367 vector
<Dwarf_Attribute
>& locs
)
3369 Dwarf_Attribute attr
;
3372 /* With inheritance, a subclass may mask member names of parent classes, so
3373 * our search among the inheritance tree must be breadth-first rather than
3374 * depth-first (recursive). The parentdie is still our starting point. */
3375 deque
<Dwarf_Die
> inheritees(1, *parentdie
);
3376 for (; !inheritees
.empty(); inheritees
.pop_front())
3378 switch (dwarf_child (&inheritees
.front(), &die
))
3380 case 0: /* First child found. */
3382 case 1: /* No children. */
3384 case -1: /* Error. */
3385 default: /* Shouldn't happen */
3386 throw SEMANTIC_ERROR (dwarf_type_name(&inheritees
.front()) + ": "
3387 + string (dwarf_errmsg (-1)),
3393 int tag
= dwarf_tag(&die
);
3394 /* recurse into imported units as if they are anonymoust structs */
3396 if (tag
== DW_TAG_imported_unit
3397 && dwarf_attr_die(&die
, DW_AT_import
, &import
)
3398 && find_struct_member(c
, &import
, memberdie
, dies
, locs
))
3401 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3404 const char *name
= dwarf_diename(&die
);
3405 if (tag
== DW_TAG_inheritance
)
3407 /* Remember inheritee for breadth-first search. */
3408 Dwarf_Die inheritee
;
3409 if (dwarf_attr_die (&die
, DW_AT_type
, &inheritee
))
3410 inheritees
.push_back(inheritee
);
3412 else if (name
== NULL
)
3414 /* Need to recurse for anonymous structs/unions. */
3416 if (dwarf_attr_die (&die
, DW_AT_type
, &subdie
) &&
3417 find_struct_member(c
, &subdie
, memberdie
, dies
, locs
))
3420 else if (name
== c
.member
)
3426 while (dwarf_siblingof (&die
, &die
) == 0);
3432 /* As we unwind the recursion, we need to build the chain of
3433 * locations that got to the final answer. */
3434 if (dwarf_attr_integrate (&die
, DW_AT_data_member_location
, &attr
))
3436 dies
.insert(dies
.begin(), die
);
3437 locs
.insert(locs
.begin(), attr
);
3440 /* Union members don't usually have a location,
3441 * but just use the containing union's location. */
3442 else if (dwarf_tag(parentdie
) != DW_TAG_union_type
)
3443 throw SEMANTIC_ERROR (_F("no location for field '%s':%s",
3444 c
.member
.c_str(), dwarf_errmsg(-1)), c
.tok
);
3451 dwarf_die_type (Dwarf_Die
*die
, Dwarf_Die
*typedie_mem
, const token
*tok
=NULL
)
3453 if (!dwarf_attr_die (die
, DW_AT_type
, typedie_mem
))
3454 throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok
);
3459 dwflpp::translate_components(struct obstack
*pool
,
3460 struct location
**tail
,
3462 const target_symbol
*e
,
3468 while (i
< e
->components
.size())
3470 const target_symbol::component
& c
= e
->components
[i
];
3472 /* XXX: This would be desirable, but we don't get the target_symbol token,
3473 and printing that gives us the file:line number too early anyway. */
3475 // Emit a marker to note which field is being access-attempted, to give
3476 // better error messages if deref() fails.
3477 string piece
= string(...target_symbol token
...) + string ("#") + lex_cast(components
[i
].second
);
3478 obstack_printf (pool
, "c->last_stmt = %s;", lex_cast_qstring(piece
).c_str());
3481 switch (dwarf_tag (typedie
))
3483 case DW_TAG_typedef
:
3484 case DW_TAG_const_type
:
3485 case DW_TAG_volatile_type
:
3486 case DW_TAG_restrict_type
:
3487 /* Just iterate on the referent type. */
3488 dwarf_die_type (typedie
, typedie
, c
.tok
);
3491 case DW_TAG_reference_type
:
3492 case DW_TAG_rvalue_reference_type
:
3495 l2c_ctx
.die
= typedie
;
3496 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3498 dwarf_die_type (typedie
, typedie
, c
.tok
);
3501 case DW_TAG_pointer_type
:
3502 /* A pointer with no type is a void* -- can't dereference it. */
3503 if (!dwarf_hasattr_integrate (typedie
, DW_AT_type
))
3504 throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c
).c_str(),
3505 dwarf_type_name(typedie
).c_str()), c
.tok
);
3509 l2c_ctx
.die
= typedie
;
3510 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3512 if (c
.type
!= target_symbol::comp_literal_array_index
&&
3513 c
.type
!= target_symbol::comp_expression_array_index
)
3515 dwarf_die_type (typedie
, typedie
, c
.tok
);
3518 /* else fall through as an array access */
3520 case DW_TAG_array_type
:
3521 if (c
.type
== target_symbol::comp_literal_array_index
)
3525 l2c_ctx
.die
= typedie
;
3526 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3530 else if (c
.type
== target_symbol::comp_expression_array_index
)
3532 string index
= "STAP_ARG_index" + lex_cast(i
);
3535 l2c_ctx
.die
= typedie
;
3536 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3541 throw SEMANTIC_ERROR (_F("invalid access '%s' for array type",
3542 lex_cast(c
).c_str()), c
.tok
);
3544 dwarf_die_type (typedie
, typedie
, c
.tok
);
3549 case DW_TAG_structure_type
:
3550 case DW_TAG_union_type
:
3551 case DW_TAG_class_type
:
3552 if (c
.type
!= target_symbol::comp_struct_member
)
3553 throw SEMANTIC_ERROR (_F("invalid access '%s' for %s",
3554 lex_cast(c
).c_str(), dwarf_type_name(typedie
).c_str()));
3556 if (dwarf_hasattr(typedie
, DW_AT_declaration
))
3558 Dwarf_Die
*tmpdie
= declaration_resolve(typedie
);
3560 throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie
).c_str()), c
.tok
);
3565 vector
<Dwarf_Die
> dies
;
3566 vector
<Dwarf_Attribute
> locs
;
3567 if (!find_struct_member(c
, typedie
, vardie
, dies
, locs
))
3569 /* Add a file:line hint for anonymous types */
3571 if (!dwarf_hasattr_integrate(typedie
, DW_AT_name
))
3574 const char *file
= dwarf_decl_file(typedie
);
3575 if (file
&& dwarf_decl_line(typedie
, &line
) == 0)
3576 source
= " (" + string(file
) + ":"
3577 + lex_cast(line
) + ")";
3580 set
<string
> members
, member_dupes
;
3581 get_members(typedie
, members
, member_dupes
);
3582 string sugs
= levenshtein_suggest(c
.member
, members
);
3584 sugs
= " (alternatives: " + sugs
+ ")";
3585 throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c
.member
.c_str(),
3586 dwarf_type_name(typedie
).c_str(), source
.c_str(),
3587 sugs
.c_str()), c
.tok
);
3590 for (unsigned j
= 0; j
< locs
.size(); ++j
)
3592 translate_location (pool
, &locs
[j
], &dies
[j
],
3596 dwarf_die_type (vardie
, typedie
, c
.tok
);
3600 case DW_TAG_enumeration_type
:
3601 case DW_TAG_base_type
:
3602 throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c
).c_str(),
3603 dwarf_type_name(typedie
).c_str()), c
.tok
);
3607 throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c
.tok
);
3611 throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie
).c_str(),
3612 lex_cast(dwarf_tag(typedie
)).c_str()), c
.tok
);
3620 dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die
*typedie
,
3621 Dwarf_Die
*innerdie
,
3622 const target_symbol
*e
)
3624 int typetag
= dwarf_tag (typedie
);
3625 *innerdie
= *typedie
;
3626 while (typetag
== DW_TAG_typedef
||
3627 typetag
== DW_TAG_const_type
||
3628 typetag
== DW_TAG_volatile_type
||
3629 typetag
== DW_TAG_restrict_type
)
3631 if (!dwarf_attr_die (innerdie
, DW_AT_type
, innerdie
))
3632 throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e
->tok
);
3633 typetag
= dwarf_tag (innerdie
);
3639 dwflpp::translate_final_fetch_or_store (struct obstack
*pool
,
3640 struct location
**tail
,
3641 Dwarf_Addr
/*module_bias*/,
3643 Dwarf_Die
*start_typedie
,
3645 const target_symbol
*e
,
3650 /* First boil away any qualifiers associated with the type DIE of
3651 the final location to be accessed. */
3653 resolve_unqualified_inner_typedie (start_typedie
, typedie
, e
);
3655 /* If we're looking for an address, then we can just provide what
3656 we computed to this point, without using a fetch/store. */
3660 throw SEMANTIC_ERROR (_("cannot write to member address"), e
->tok
);
3662 if (dwarf_hasattr_integrate (vardie
, DW_AT_bit_offset
))
3663 throw SEMANTIC_ERROR (_("cannot take address of bit-field"), e
->tok
);
3665 l2c_ctx
.die
= vardie
;
3666 c_translate_addressof (pool
, 1, 0, vardie
, typedie
, tail
, "STAP_RETVALUE");
3670 /* Then switch behavior depending on the type of fetch/store we
3671 want, and the type and pointer-ness of the final location. */
3673 int typetag
= dwarf_tag (typedie
);
3677 throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s", lex_cast(typetag
).c_str(),
3678 dwarf_type_name(typedie
).c_str()), e
->tok
);
3681 case DW_TAG_structure_type
:
3682 case DW_TAG_class_type
:
3683 case DW_TAG_union_type
:
3684 throw SEMANTIC_ERROR (_F("'%s' is being accessed instead of a member",
3685 dwarf_type_name(typedie
).c_str()), e
->tok
);
3688 case DW_TAG_base_type
:
3690 // Reject types we can't handle in systemtap
3692 Dwarf_Attribute encoding_attr
;
3693 Dwarf_Word encoding
= (Dwarf_Word
) -1;
3694 dwarf_formudata (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &encoding_attr
),
3696 if (encoding
== (Dwarf_Word
) -1)
3698 // clog << "bad type1 " << encoding << " diestr" << endl;
3699 throw SEMANTIC_ERROR (_F("unsupported type (mystery encoding %s for %s", lex_cast(encoding
).c_str(),
3700 dwarf_type_name(typedie
).c_str()), e
->tok
);
3703 if (encoding
== DW_ATE_float
3704 || encoding
== DW_ATE_complex_float
3705 /* XXX || many others? */)
3707 // clog << "bad type " << encoding << " diestr" << endl;
3708 throw SEMANTIC_ERROR (_F("unsupported type (encoding %s) for %s", lex_cast(encoding
).c_str(),
3709 dwarf_type_name(typedie
).c_str()), e
->tok
);
3712 // Fallthrough. enumeration_types are always scalar.
3713 case DW_TAG_enumeration_type
:
3715 l2c_ctx
.die
= vardie
;
3717 c_translate_store (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3720 c_translate_fetch (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3724 case DW_TAG_array_type
:
3725 case DW_TAG_pointer_type
:
3726 case DW_TAG_reference_type
:
3727 case DW_TAG_rvalue_reference_type
:
3731 if (typetag
== DW_TAG_array_type
)
3732 throw SEMANTIC_ERROR (_("cannot write to array address"), e
->tok
);
3733 if (typetag
== DW_TAG_reference_type
||
3734 typetag
== DW_TAG_rvalue_reference_type
)
3735 throw SEMANTIC_ERROR (_("cannot write to reference"), e
->tok
);
3736 assert (typetag
== DW_TAG_pointer_type
);
3737 l2c_ctx
.die
= typedie
;
3738 c_translate_pointer_store (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3743 // We have the pointer: cast it to an integral type via &(*(...))
3745 // NB: per bug #1187, at one point char*-like types were
3746 // automagically converted here to systemtap string values.
3747 // For several reasons, this was taken back out, leaving
3748 // pointer-to-string "conversion" (copying) to tapset functions.
3750 l2c_ctx
.die
= typedie
;
3751 if (typetag
== DW_TAG_array_type
)
3752 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
, NULL
, 0);
3754 c_translate_pointer (pool
, 1, 0 /* PR9768 */, typedie
, tail
);
3755 c_translate_addressof (pool
, 1, 0 /* PR9768 */, NULL
, NULL
, tail
,
3762 postlude
+= " STAP_RETVALUE = STAP_ARG_value;\n";
3767 dwflpp::express_as_string (string prelude
,
3769 struct location
*head
)
3772 char *buf
= 0; // NB: it would leak to pre-allocate a buffer here
3773 FILE *memstream
= open_memstream (&buf
, &bufsz
);
3776 fprintf(memstream
, "{\n");
3777 fprintf(memstream
, "%s", prelude
.c_str());
3779 unsigned int stack_depth
;
3780 bool deref
= c_emit_location (memstream
, head
, 1, &stack_depth
);
3782 // Ensure that DWARF keeps loc2c to a "reasonable" stack size
3783 // 32 intptr_t leads to max 256 bytes on the stack
3784 if (stack_depth
> 32)
3785 throw SEMANTIC_ERROR("oversized DWARF stack");
3787 fprintf(memstream
, "%s", postlude
.c_str());
3788 fprintf(memstream
, " goto out;\n");
3790 // dummy use of deref_fault label, to disable warning if deref() not used
3791 fprintf(memstream
, "if (0) goto deref_fault;\n");
3793 // XXX: deref flag not reliable; emit fault label unconditionally
3798 fprintf(memstream
, "}\n");
3807 dwflpp::vardie_from_symtable (Dwarf_Die
*vardie
, Dwarf_Addr
*addr
)
3809 const char *name
= dwarf_linkage_name (vardie
) ?: dwarf_diename (vardie
);
3811 if (sess
.verbose
> 2)
3812 clog
<< _F("finding symtable address for %s\n", name
);
3815 int syms
= dwfl_module_getsymtab (module
);
3816 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
3818 for (int i
= 0; *addr
== 0 && i
< syms
; i
++)
3822 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
3824 && ! strcmp (name
, symname
)
3825 && sym
.st_shndx
!= SHN_UNDEF
3826 && (GELF_ST_TYPE (sym
.st_info
) == STT_NOTYPE
// PR13284
3827 || GELF_ST_TYPE (sym
.st_info
) == STT_OBJECT
))
3828 *addr
= sym
.st_value
;
3831 // Don't relocate for the kernel, or kernel modules we handle those
3832 // specially in emit_address.
3833 if (dwfl_module_relocations (module
) == 1 && module_name
!= TOK_KERNEL
)
3834 dwfl_module_relocate_address (module
, addr
);
3836 if (sess
.verbose
> 2)
3837 clog
<< _F("found %s @%#" PRIx64
"\n", name
, *addr
);
3843 dwflpp::literal_stmt_for_local (vector
<Dwarf_Die
>& scopes
,
3845 string
const & local
,
3846 const target_symbol
*e
,
3851 Dwarf_Attribute fb_attr_mem
, *fb_attr
= NULL
;
3853 fb_attr
= find_variable_and_frame_base (scopes
, pc
, local
, e
,
3854 &vardie
, &fb_attr_mem
);
3859 clog
<< _F("finding location for local '%s' near address %#" PRIx64
3860 ", module bias %#" PRIx64
"\n", local
.c_str(), pc
,
3863 clog
<< _F("finding location for global '%s' in CU '%s'\n",
3864 local
.c_str(), cu_name().c_str());
3868 #define obstack_chunk_alloc malloc
3869 #define obstack_chunk_free free
3871 struct obstack pool
;
3872 obstack_init (&pool
);
3873 struct location
*tail
= NULL
;
3875 /* Given $foo->bar->baz[NN], translate the location of foo. */
3877 struct location
*head
;
3879 Dwarf_Attribute attr_mem
;
3880 if (dwarf_attr_integrate (&vardie
, DW_AT_const_value
, &attr_mem
) == NULL
3881 && dwarf_attr_integrate (&vardie
, DW_AT_location
, &attr_mem
) == NULL
)
3884 memset(&addr_loc
, 0, sizeof(Dwarf_Op
));
3885 addr_loc
.atom
= DW_OP_addr
;
3886 // If it is an external variable try the symbol table. PR10622.
3887 if (dwarf_attr_integrate (&vardie
, DW_AT_external
, &attr_mem
) != NULL
3888 && vardie_from_symtable (&vardie
, &addr_loc
.number
) != 0)
3891 l2c_ctx
.die
= &vardie
;
3892 head
= c_translate_location (&pool
, &loc2c_error
, this,
3893 &loc2c_emit_address
,
3895 NULL
, &addr_loc
, 1, &tail
, NULL
, NULL
);
3899 string msg
= _F("failed to retrieve location attribute for '%s' [man error::dwarf]", local
.c_str());
3900 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3901 err
.details
.push_back(die_location_as_string(pc
, &vardie
));
3902 err
.details
.push_back(die_location_as_function_string(pc
, &vardie
));
3907 head
= translate_location (&pool
, &attr_mem
, &vardie
, pc
, fb_attr
, &tail
, e
);
3909 /* Translate the ->bar->baz[NN] parts. */
3912 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
3914 string msg
= _F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str());
3915 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3916 err
.details
.push_back(die_location_as_string(pc
, &vardie
));
3917 err
.details
.push_back(die_location_as_function_string(pc
, &vardie
));
3921 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
3923 /* Translate the assignment part, either
3924 x = $foo->bar->baz[NN]
3926 $foo->bar->baz[NN] = x
3929 string prelude
, postlude
;
3930 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
3931 &vardie
, &typedie
, lvalue
, e
,
3932 prelude
, postlude
, die_mem
);
3934 /* Write the translation to a string. */
3935 string result
= express_as_string(prelude
, postlude
, head
);
3936 obstack_free (&pool
, 0);
3941 dwflpp::type_die_for_local (vector
<Dwarf_Die
>& scopes
,
3943 string
const & local
,
3944 const target_symbol
*e
,
3948 Dwarf_Attribute attr_mem
;
3950 find_variable_and_frame_base (scopes
, pc
, local
, e
, &vardie
, &attr_mem
);
3952 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
3953 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str()), e
->tok
);
3955 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
3961 dwflpp::literal_stmt_for_return (Dwarf_Die
*scope_die
,
3963 const target_symbol
*e
,
3968 clog
<< _F("literal_stmt_for_return: finding return value for %s (%s)\n",
3969 (dwarf_diename(scope_die
) ?: "<unknown>"), (dwarf_diename(cu
) ?: "<unknown>"));
3971 struct obstack pool
;
3972 obstack_init (&pool
);
3973 struct location
*tail
= NULL
;
3975 /* Given $return->bar->baz[NN], translate the location of return. */
3976 const Dwarf_Op
*locops
;
3977 int nlocops
= dwfl_module_return_value_location (module
, scope_die
,
3981 throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)",
3982 (dwarf_diename(scope_die
) ?: "<unknown>"),
3983 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3985 // the function has no return value (e.g. "void" in C)
3986 else if (nlocops
== 0)
3988 throw SEMANTIC_ERROR(_F("function %s (%s) has no return value",
3989 (dwarf_diename(scope_die
) ?: "<unknown>"),
3990 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3994 l2c_ctx
.die
= scope_die
;
3995 struct location
*head
= c_translate_location (&pool
, &loc2c_error
, this,
3996 &loc2c_emit_address
,
3998 pc
, NULL
, locops
, nlocops
,
4001 /* Translate the ->bar->baz[NN] parts. */
4003 Dwarf_Die vardie
= *scope_die
, typedie
;
4004 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
4005 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4006 (dwarf_diename(&vardie
) ?: "<unknown>"),
4007 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4009 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
4011 /* Translate the assignment part, either
4012 x = $return->bar->baz[NN]
4014 $return->bar->baz[NN] = x
4017 string prelude
, postlude
;
4018 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
4019 &vardie
, &typedie
, lvalue
, e
,
4020 prelude
, postlude
, die_mem
);
4022 /* Write the translation to a string. */
4023 string result
= express_as_string(prelude
, postlude
, head
);
4024 obstack_free (&pool
, 0);
4029 dwflpp::type_die_for_return (Dwarf_Die
*scope_die
,
4031 const target_symbol
*e
,
4034 Dwarf_Die vardie
= *scope_die
;
4035 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
4036 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
4037 (dwarf_diename(&vardie
) ?: "<unknown>"),
4038 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
4040 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
4046 dwflpp::literal_stmt_for_pointer (Dwarf_Die
*start_typedie
,
4047 const target_symbol
*e
,
4052 clog
<< _F("literal_stmt_for_pointer: finding value for %s (%s)\n",
4053 dwarf_type_name(start_typedie
).c_str(), (dwarf_diename(cu
) ?: "<unknown>"));
4055 struct obstack pool
;
4056 obstack_init (&pool
);
4058 l2c_ctx
.die
= start_typedie
;
4059 struct location
*head
= c_translate_argument (&pool
, &loc2c_error
, this,
4060 &loc2c_emit_address
,
4061 1, "STAP_ARG_pointer");
4062 struct location
*tail
= head
;
4064 /* Translate the ->bar->baz[NN] parts. */
4067 Dwarf_Die typedie
= *start_typedie
, vardie
= typedie
;
4069 /* As a special case when typedie is not an array or pointer, we can
4070 * allow array indexing on STAP_ARG_pointer instead (since we do
4071 * know the pointee type and can determine its size). PR11556. */
4072 const target_symbol::component
* c
=
4073 e
->components
.empty() ? NULL
: &e
->components
[0];
4074 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
4075 c
->type
== target_symbol::comp_expression_array_index
))
4077 resolve_unqualified_inner_typedie (&typedie
, &typedie
, e
);
4078 int typetag
= dwarf_tag (&typedie
);
4079 if (typetag
!= DW_TAG_pointer_type
&&
4080 typetag
!= DW_TAG_array_type
)
4082 l2c_ctx
.die
= &typedie
;
4083 if (c
->type
== target_symbol::comp_literal_array_index
)
4084 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, NULL
, c
->num_index
);
4086 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, "STAP_ARG_index0", 0);
4091 /* Now translate the rest normally. */
4093 translate_components (&pool
, &tail
, 0, e
, &vardie
, &typedie
, first
);
4095 /* Translate the assignment part, either
4096 x = (STAP_ARG_pointer)->bar->baz[NN]
4098 (STAP_ARG_pointer)->bar->baz[NN] = x
4101 string prelude
, postlude
;
4102 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
4103 &vardie
, &typedie
, lvalue
, e
,
4104 prelude
, postlude
, die_mem
);
4106 /* Write the translation to a string. */
4107 string result
= express_as_string(prelude
, postlude
, head
);
4108 obstack_free (&pool
, 0);
4113 dwflpp::type_die_for_pointer (Dwarf_Die
*start_typedie
,
4114 const target_symbol
*e
,
4118 *typedie
= *start_typedie
;
4119 Dwarf_Die vardie
= *typedie
;
4121 /* Handle the same PR11556 case as above. */
4122 const target_symbol::component
* c
=
4123 e
->components
.empty() ? NULL
: &e
->components
[0];
4124 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
4125 c
->type
== target_symbol::comp_expression_array_index
))
4127 resolve_unqualified_inner_typedie (typedie
, typedie
, e
);
4128 int typetag
= dwarf_tag (typedie
);
4129 if (typetag
!= DW_TAG_pointer_type
&&
4130 typetag
!= DW_TAG_array_type
)
4134 translate_components (NULL
, NULL
, 0, e
, &vardie
, typedie
, first
);
4140 in_kprobes_function(systemtap_session
& sess
, Dwarf_Addr addr
)
4142 if (sess
.sym_kprobes_text_start
!= 0 && sess
.sym_kprobes_text_end
!= 0)
4144 // If the probe point address is anywhere in the __kprobes
4145 // address range, we can't use this probe point.
4146 if (addr
>= sess
.sym_kprobes_text_start
&& addr
< sess
.sym_kprobes_text_end
)
4153 enum dwflpp::blacklisted_type
4154 dwflpp::blacklisted_p(const string
& funcname
,
4155 const string
& filename
,
4157 const string
& module
,
4161 if (!blacklist_enabled
)
4162 return dwflpp::blacklisted_none
;
4164 enum dwflpp::blacklisted_type blacklisted
= dwflpp::blacklisted_none
;
4166 // check against section blacklist
4167 string section
= get_blacklist_section(addr
);
4169 // PR6503: modules don't need special init/exit treatment
4170 if (module
== TOK_KERNEL
&& !regexec (&blacklist_section
, section
.c_str(), 0, NULL
, 0))
4171 blacklisted
= dwflpp::blacklisted_section
;
4173 // Check for function marked '__kprobes'.
4174 else if (module
== TOK_KERNEL
&& in_kprobes_function(sess
, addr
))
4175 blacklisted
= dwflpp::blacklisted_kprobes
;
4177 // Check probe point against function blacklist
4178 else if (!regexec(&blacklist_func
, funcname
.c_str(), 0, NULL
, 0))
4179 blacklisted
= dwflpp::blacklisted_function
;
4181 // Check probe point against function return blacklist
4182 else if (has_return
&& !regexec(&blacklist_func_ret
, funcname
.c_str(), 0, NULL
, 0))
4183 blacklisted
= dwflpp::blacklisted_function_return
;
4185 // Check probe point against file blacklist
4186 else if (!regexec(&blacklist_file
, filename
.c_str(), 0, NULL
, 0))
4187 blacklisted
= dwflpp::blacklisted_file
;
4192 clog
<< _(" - blacklisted");
4195 blacklisted
= dwflpp::blacklisted_none
;
4197 clog
<< _(" but not skipped (guru mode enabled)");
4201 // This probe point is not blacklisted.
4207 dwflpp::build_kernel_blacklist()
4209 // We build up the regexps in these strings
4211 // Add ^ anchors at the front; $ will be added just before regcomp.
4214 string blfn_ret
= "^(";
4215 string blfile
= "^(";
4216 string blsection
= "^(";
4218 blsection
+= "\\.init\\."; // first alternative, no "|"
4219 blsection
+= "|\\.exit\\.";
4220 blsection
+= "|\\.devinit\\.";
4221 blsection
+= "|\\.devexit\\.";
4222 blsection
+= "|\\.cpuinit\\.";
4223 blsection
+= "|\\.cpuexit\\.";
4224 blsection
+= "|\\.meminit\\.";
4225 blsection
+= "|\\.memexit\\.";
4227 /* NOTE all include/asm .h blfile patterns might need "full path"
4228 so prefix those with '.*' - see PR13108 and PR13112. */
4229 blfile
+= "kernel/kprobes\\.c"; // first alternative, no "|"
4230 blfile
+= "|arch/.*/kernel/kprobes\\.c";
4231 blfile
+= "|.*/include/asm/io\\.h";
4232 blfile
+= "|.*/include/asm/io_64\\.h";
4233 blfile
+= "|.*/include/asm/bitops\\.h";
4234 blfile
+= "|drivers/ide/ide-iops\\.c";
4236 blfile
+= "|arch/.*/kernel/paravirt\\.c";
4237 blfile
+= "|.*/include/asm/paravirt\\.h";
4239 // XXX: it would be nice if these blacklisted functions were pulled
4240 // in dynamically, instead of being statically defined here.
4241 // Perhaps it could be populated from script files. A "noprobe
4242 // kernel.function("...")" construct might do the trick.
4244 // Most of these are marked __kprobes in newer kernels. We list
4245 // them here (anyway) so the translator can block them on older
4246 // kernels that don't have the __kprobes function decorator. This
4247 // also allows detection of problems at translate- rather than
4250 blfn
+= "atomic_notifier_call_chain"; // first blfn; no "|"
4251 blfn
+= "|default_do_nmi";
4254 blfn
+= "|do_debug";
4255 blfn
+= "|do_general_protection";
4258 blfn
+= "|do_page_fault";
4259 blfn
+= "|do_sparc64_fault";
4261 blfn
+= "|dummy_nmi_callback";
4262 blfn
+= "|flush_icache_range";
4263 blfn
+= "|ia64_bad_break";
4264 blfn
+= "|ia64_do_page_fault";
4265 blfn
+= "|ia64_fault";
4266 blfn
+= "|io_check_error";
4267 blfn
+= "|mem_parity_error";
4268 blfn
+= "|nmi_watchdog_tick";
4269 blfn
+= "|notifier_call_chain";
4270 blfn
+= "|oops_begin";
4271 blfn
+= "|oops_end";
4272 blfn
+= "|program_check_exception";
4273 blfn
+= "|single_step_exception";
4274 blfn
+= "|sync_regs";
4275 blfn
+= "|unhandled_fault";
4276 blfn
+= "|unknown_nmi_error";
4277 blfn
+= "|xen_[gs]et_debugreg";
4278 blfn
+= "|xen_irq_.*";
4279 blfn
+= "|xen_.*_fl_direct.*";
4280 blfn
+= "|check_events";
4281 blfn
+= "|xen_adjust_exception_frame";
4282 blfn
+= "|xen_iret.*";
4283 blfn
+= "|xen_sysret64.*";
4284 blfn
+= "|test_ti_thread_flag.*";
4285 blfn
+= "|inat_get_opcode_attribute";
4286 blfn
+= "|system_call_after_swapgs";
4287 blfn
+= "|HYPERVISOR_[gs]et_debugreg";
4288 blfn
+= "|HYPERVISOR_event_channel_op";
4290 blfn
+= "|hash_ptr";
4291 blfn
+= "|native_set_pte";
4294 blfn
+= "|.*raw_.*_lock.*";
4295 blfn
+= "|.*raw_.*_unlock.*";
4296 blfn
+= "|.*raw_.*_trylock.*";
4297 blfn
+= "|.*read_lock.*";
4298 blfn
+= "|.*read_unlock.*";
4299 blfn
+= "|.*read_trylock.*";
4300 blfn
+= "|.*write_lock.*";
4301 blfn
+= "|.*write_unlock.*";
4302 blfn
+= "|.*write_trylock.*";
4303 blfn
+= "|.*write_seqlock.*";
4304 blfn
+= "|.*write_sequnlock.*";
4305 blfn
+= "|.*spin_lock.*";
4306 blfn
+= "|.*spin_unlock.*";
4307 blfn
+= "|.*spin_trylock.*";
4308 blfn
+= "|.*spin_is_locked.*";
4309 blfn
+= "|rwsem_.*lock.*";
4310 blfn
+= "|.*mutex_.*lock.*";
4314 blfn
+= "|atomic_.*";
4315 blfn
+= "|atomic64_.*";
4317 // few other problematic cases
4322 blfn
+= "|.*apic.*|.*APIC.*";
4323 blfn
+= "|.*softirq.*";
4325 blfn
+= "|.*_intr.*";
4327 blfn
+= "|.*kernel_text.*";
4328 blfn
+= "|get_current";
4329 blfn
+= "|current_.*";
4330 blfn
+= "|.*exception_tables.*";
4331 blfn
+= "|.*setup_rt_frame.*";
4333 // PR 5759, CONFIG_PREEMPT kernels
4334 blfn
+= "|.*preempt_count.*";
4335 blfn
+= "|preempt_schedule";
4337 // These functions don't return, so return probes would never be recovered
4338 blfn_ret
+= "do_exit"; // no "|"
4339 blfn_ret
+= "|sys_exit";
4340 blfn_ret
+= "|sys_exit_group";
4342 // __switch_to changes "current" on x86_64 and i686, so return probes
4343 // would cause kernel panic, and it is marked as "__kprobes" on x86_64
4344 if (sess
.architecture
== "x86_64")
4345 blfn
+= "|__switch_to";
4346 if (sess
.architecture
== "i686")
4347 blfn_ret
+= "|__switch_to";
4349 // RHEL6 pre-beta 2.6.32-19.el6
4350 blfn
+= "|special_mapping_.*";
4351 blfn
+= "|.*_pte_.*"; // or "|smaps_pte_range";
4352 blfile
+= "|fs/seq_file\\.c";
4357 blsection
+= ")"; // NB: no $, sections match just the beginning
4359 if (sess
.verbose
> 2)
4361 clog
<< _("blacklist regexps:") << endl
;
4362 clog
<< "blfn: " << blfn
<< endl
;
4363 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4364 clog
<< "blfile: " << blfile
<< endl
;
4365 clog
<< "blsection: " << blsection
<< endl
;
4368 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4369 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4370 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4371 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4372 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4373 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4374 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4375 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4377 blacklist_enabled
= true;
4382 dwflpp::build_user_blacklist()
4384 // We build up the regexps in these strings
4386 // Add ^ anchors at the front; $ will be added just before regcomp.
4389 string blfn_ret
= "^(";
4390 string blfile
= "^(";
4391 string blsection
= "^(";
4393 // Non-matching placeholders until we have real things to match
4398 // These functions don't use the normal function-entry ABI, so can't be .return probed safely
4399 blfn_ret
+= "_start";
4404 blsection
+= ")"; // NB: no $, sections match just the beginning
4406 if (sess
.verbose
> 2)
4408 clog
<< _("blacklist regexps:") << endl
;
4409 clog
<< "blfn: " << blfn
<< endl
;
4410 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4411 clog
<< "blfile: " << blfile
<< endl
;
4412 clog
<< "blsection: " << blsection
<< endl
;
4415 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4416 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4417 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4418 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4419 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4420 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4421 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4422 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4424 blacklist_enabled
= true;
4429 dwflpp::get_blacklist_section(Dwarf_Addr addr
)
4431 string blacklist_section
;
4433 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
4434 // because dwfl_module_getelf can force costly section relocations
4435 // we don't really need, while either will do for this purpose.
4436 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (module
, &bias
))
4437 ?: dwfl_module_getelf (module
, &bias
));
4439 Dwarf_Addr offset
= addr
- bias
;
4444 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4445 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
4448 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
4450 continue; // XXX error?
4452 if (!(shdr
->sh_flags
& SHF_ALLOC
))
4455 GElf_Addr start
= shdr
->sh_addr
;
4456 GElf_Addr end
= start
+ shdr
->sh_size
;
4457 if (! (offset
>= start
&& offset
< end
))
4460 blacklist_section
= elf_strptr (elf
, shstrndx
, shdr
->sh_name
);
4464 return blacklist_section
;
4468 /* Find the section named 'section_name' in the current module
4469 * returning the section header using 'shdr_mem' */
4472 dwflpp::get_section(string section_name
, GElf_Shdr
*shdr_mem
, Elf
**elf_ret
)
4474 GElf_Shdr
*shdr
= NULL
;
4479 // Explicitly look in the main elf file first.
4480 elf
= dwfl_module_getelf (module
, &bias
);
4481 Elf_Scn
*probe_scn
= NULL
;
4483 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4485 bool have_section
= false;
4487 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4489 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4490 assert (shdr
!= NULL
);
4492 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4494 have_section
= true;
4499 // Older versions may put the section in the debuginfo dwarf file,
4500 // so check if it actually exists, if not take a look in the debuginfo file
4501 if (! have_section
|| (have_section
&& shdr
->sh_type
== SHT_NOBITS
))
4503 elf
= dwarf_getelf (dwfl_module_getdwarf (module
, &bias
));
4506 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4508 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4510 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4511 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4513 have_section
= true;
4529 dwflpp::relocate_address(Dwarf_Addr dw_addr
, string
& reloc_section
)
4532 // libdw address, so adjust for bias gotten from dwfl_module_getdwarf
4533 Dwarf_Addr reloc_addr
= dw_addr
+ module_bias
;
4536 assert(module_name
== TOK_KERNEL
);
4539 else if (dwfl_module_relocations (module
) > 0)
4541 // This is a relocatable module; libdwfl already knows its
4542 // sections, so we can relativize addr.
4543 int idx
= dwfl_module_relocate_address (module
, &reloc_addr
);
4544 const char* r_s
= dwfl_module_relocation_info (module
, idx
, NULL
);
4546 reloc_section
= r_s
;
4548 if (reloc_section
== "" && dwfl_module_relocations (module
) == 1)
4549 reloc_section
= ".dynamic";
4552 reloc_section
= ".absolute";
4556 /* Returns the call frame address operations for the given program counter
4557 * in the libdw address space.
4560 dwflpp::get_cfa_ops (Dwarf_Addr pc
)
4562 Dwarf_Op
*cfa_ops
= NULL
;
4564 if (sess
.verbose
> 2)
4565 clog
<< "get_cfa_ops @0x" << hex
<< pc
<< dec
4566 << ", module_start @0x" << hex
<< module_start
<< dec
<< endl
;
4568 // Try debug_frame first, then fall back on eh_frame.
4569 size_t cfa_nops
= 0;
4570 Dwarf_Addr bias
= 0;
4571 Dwarf_Frame
*frame
= NULL
;
4572 Dwarf_CFI
*cfi
= dwfl_module_dwarf_cfi (module
, &bias
);
4575 if (sess
.verbose
> 3)
4576 clog
<< "got dwarf cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4577 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4578 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4579 else if (sess
.verbose
> 3)
4580 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4582 else if (sess
.verbose
> 3)
4583 clog
<< "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl
;
4585 if (cfa_ops
== NULL
)
4587 cfi
= dwfl_module_eh_cfi (module
, &bias
);
4590 if (sess
.verbose
> 3)
4591 clog
<< "got eh cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4592 Dwarf_Frame
*frame
= NULL
;
4593 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4594 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4595 else if (sess
.verbose
> 3)
4596 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4598 else if (sess
.verbose
> 3)
4599 clog
<< "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl
;
4603 if (sess
.verbose
> 2)
4605 if (cfa_ops
== NULL
)
4606 clog
<< _("not found cfa") << endl
;
4609 Dwarf_Addr frame_start
, frame_end
;
4611 int info
= dwarf_frame_info (frame
, &frame_start
, &frame_end
,
4613 clog
<< _F("found cfa, info: %d [start: %#" PRIx64
", end: %#" PRIx64
4614 ", nops: %zu", info
, frame_start
, frame_end
, cfa_nops
) << endl
;
4622 dwflpp::add_module_build_id_to_hash (Dwfl_Module
*m
,
4623 void **userdata
__attribute__ ((unused
)),
4628 string modname
= name
;
4629 systemtap_session
* s
= (systemtap_session
*)arg
;
4630 if (pending_interrupts
)
4631 return DWARF_CB_ABORT
;
4633 // Extract the build ID
4634 const unsigned char *bits
;
4636 int bits_length
= dwfl_module_build_id(m
, &bits
, &vaddr
);
4639 // Convert the binary bits to a hex string
4640 string hex
= hex_dump(bits
, bits_length
);
4642 // Store the build ID in the session
4643 s
->build_ids
.push_back(hex
);
4651 // Perform PR15123 heuristic for given variable at given address.
4652 // Return alternate pc address to do location-list lookup at, or 0 if
4656 dwflpp::pr15123_retry_addr (Dwarf_Addr pc
, Dwarf_Die
* die
)
4658 // For PR15123, we'd like to detect the situation where the
4659 // incoming PC may point to a couple-of-byte instruction
4660 // sequence that gcc emits for CFLAGS=-mfentry, and where
4661 // context variables are in fact available throughout, *but* due
4662 // to the bug, the dwarf debuginfo location-list only starts a
4663 // few instructions later. Prologue searching does not resolve
4664 // this as a line-record is in place at the -mfentry prologue.
4666 // Detecting this is complicated because ...
4667 // - we only want to do this if -mfentry was actually used
4668 // - if <pc> points to the a function entry point
4669 // - if the architecture is familiar enough that we can have a
4670 // hard-coded constant to skip over the prologue.
4672 // Otherwise, we could give a false-positive - return corrupted
4675 // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches
4676 // was used. Without it, set the PR15123_ASSUME_MFENTRY environment
4677 // variable to override the -mfentry test.
4679 if (getenv ("PR15123_DISABLE"))
4682 if (!getenv ("PR15123_ASSUME_MFENTRY")) {
4684 string producer
, version
;
4685 dwarf_diecu (die
, &cudie
, NULL
, NULL
);
4686 if (!is_gcc_producer(&cudie
, producer
, version
))
4688 if (producer
.find("-mfentry") == string::npos
)
4692 // Determine if this pc maps to the beginning of a
4693 // real function (not some inlined doppelganger. This
4694 // is made tricker by this->function may not be
4695 // pointing at the right DIE (say e.g. stap encountered
4696 // the inlined copy first, so was focus_on_function'd).
4697 vector
<Dwarf_Die
> scopes
= getscopes(pc
);
4698 if (scopes
.size() == 0)
4701 Dwarf_Die outer_function_die
= scopes
[0];
4703 if (!die_entrypc(& outer_function_die
, &entrypc
) || entrypc
!= pc
)
4704 return 0; // (will fail on retry, so we won't loop more than once)
4706 if (sess
.architecture
== "i386" ||
4707 sess
.architecture
== "x86_64") {
4708 /* pull the trigger */
4709 if (sess
.verbose
> 2)
4710 clog
<< _("retrying variable location-list lookup at address pc+5\n");
4718 dwflpp::has_gnu_debugdata ()
4720 Dwarf_Addr load_addr
;
4721 // Note we really want the actual elf file, not the dwarf .debug file.
4722 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
4724 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
4726 // Get the gnu_debugdata section header
4727 Elf_Scn
*scn
= NULL
;
4728 GElf_Shdr
*gnu_debugdata_shdr
= NULL
;
4729 GElf_Shdr gnu_debugdata_shdr_mem
;
4730 while ((scn
= elf_nextscn (elf
, scn
)))
4732 gnu_debugdata_shdr
= gelf_getshdr (scn
, &gnu_debugdata_shdr_mem
);
4733 assert (gnu_debugdata_shdr
!= NULL
);
4734 if (strcmp (elf_strptr (elf
, shstrndx
, gnu_debugdata_shdr
->sh_name
), ".gnu_debugdata") == 0)
4740 // If not GCC, return false. Otherwise, return true and set vers.
4742 dwflpp::is_gcc_producer(Dwarf_Die
*cudie
, string
& producer
, string
& version
)
4744 Dwarf_Attribute producer_attr
;
4745 if (!dwarf_attr_integrate(cudie
, DW_AT_producer
, &producer_attr
))
4748 // GNU {C|C++|...} x.x.x YYYYMMDD ...
4749 const char *cproducer
= dwarf_formstring(&producer_attr
);
4752 producer
= cproducer
;
4754 vector
<string
> tokens
;
4755 tokenize(producer
, tokens
);
4757 if (tokens
.size() < 3
4758 || tokens
[0] != "GNU"
4759 || tokens
[2].find_first_not_of(".0123456789") != string::npos
)
4762 version
= tokens
[2];
4767 die_has_loclist(Dwarf_Die
*begin_die
)
4770 Dwarf_Attribute loc
;
4772 if (dwarf_child(begin_die
, &die
) != 0)
4777 switch (dwarf_tag(&die
))
4779 case DW_TAG_formal_parameter
:
4780 case DW_TAG_variable
:
4781 if (dwarf_attr_integrate(&die
, DW_AT_location
, &loc
)
4782 && dwarf_whatform(&loc
) == DW_FORM_sec_offset
)
4786 if (dwarf_haschildren (&die
))
4787 if (die_has_loclist(&die
))
4792 while (dwarf_siblingof (&die
, &die
) == 0);
4798 dwflpp::has_valid_locs ()
4802 // The current CU has valid location info (implying we do not need to skip the
4804 // - it was compiled with -O2 -g (in which case, GCC outputs proper location
4805 // info for the prologue), and
4806 // - it was compiled by GCC >= 4.5 (previous versions could have had invalid
4807 // debug info in the prologue, see GDB's PR13777)
4808 // Note that clang behaves similarly to GCC here: non-optimized code does not
4809 // have location lists, while optimized code does. In the check below, even if
4810 // the producer is not GCC, we assume that it is valid to do the loclist check
4811 // afterwards (which it is for clang).
4814 if (is_gcc_producer(cu
, prod
, vers
)
4815 && strverscmp(vers
.c_str(), "4.5") < 0)
4818 // We determine if the current CU has been optimized with -O2 -g by looking
4819 // for any data objects whose DW_AT_location is a location list. This is also
4820 // how GDB determines whether to skip the prologue or not. See GDB's PR12573
4821 // and also RHBZ612253#c6.
4822 if (!die_has_loclist(cu
))
4825 if (sess
.verbose
> 2)
4826 clog
<< _F("CU '%s' in module '%s' has valid locs",
4827 cu_name().c_str(), module_name
.c_str()) << endl
;
4832 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */