1 // C++ interface to dwfl
2 // Copyright (C) 2005-2014 Red Hat Inc.
3 // Copyright (C) 2005-2007 Intel Corporation.
4 // Copyright (C) 2008 James.Bottomley@HansenPartnership.com
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
1292 sess
.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s",
1293 module_name
.c_str(), interpreter
.c_str()));
1297 vector
<string
> ldd_command
;
1298 ldd_command
.push_back("/usr/bin/env");
1299 ldd_command
.push_back("LD_TRACE_LOADED_OBJECTS=1");
1300 ldd_command
.push_back("LD_WARN=yes");
1301 ldd_command
.push_back("LD_BIND_NOW=yes");
1302 ldd_command
.push_back(interpreter
);
1303 ldd_command
.push_back(module_name
);
1307 pid_t child
= stap_spawn_piped(sess
.verbose
, ldd_command
, NULL
, &child_fd
);
1308 if (child
<= 0 || !(fp
= fdopen(child_fd
, "r")))
1309 clog
<< _F("library iteration on %s failed: %s",
1310 module_name
.c_str(), strerror(errno
)) << endl
;
1313 while (1) // this parsing loop borrowed from add_unwindsym_ldd
1318 unsigned long int addr
= 0;
1320 char *line
= fgets (linebuf
, 256, fp
);
1321 if (line
== 0) break; // EOF or error
1323 #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)
1324 #define MS_FMT "%ms"
1326 #define MS_FMT "%as"
1328 // Try soname => shlib (0xaddr)
1329 int nf
= sscanf (line
, MS_FMT
" => " MS_FMT
" (0x%lx)",
1330 &soname
, &shlib
, &addr
);
1331 if (nf
!= 3 || shlib
[0] != '/')
1333 // Try shlib (0xaddr)
1334 nf
= sscanf (line
, " " MS_FMT
" (0x%lx)", &shlib
, &addr
);
1335 if (nf
!= 2 || shlib
[0] != '/')
1336 continue; // fewer than expected fields, or bad shlib.
1339 if (added
.find (shlib
) == added
.end())
1341 if (sess
.verbose
> 2)
1343 clog
<< _F("Added -d '%s", shlib
);
1345 clog
<< _F("' due to '%s'", soname
);
1350 added
.insert (shlib
);
1356 if ((fclose(fp
) || stap_waitpid(sess
.verbose
, child
)))
1357 sess
.print_warning("failed to read libraries from " + module_name
+ ": " + strerror(errno
));
1360 for (std::set
<std::string
>::iterator it
= added
.begin();
1364 string modname
= *it
;
1365 (callback
) (data
, modname
.c_str());
1370 /* For each plt section in the current module call 'callback', pass the plt entry
1371 * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */
1374 dwflpp::iterate_over_plt
<void>(void *object
, void (*callback
)(void*,
1378 Dwarf_Addr load_addr
;
1379 // Note we really want the actual elf file, not the dwarf .debug file.
1380 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
1382 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
1384 // Get the load address
1385 for (int i
= 0; ; i
++)
1389 phdr
= gelf_getphdr (elf
, i
, &mem
);
1392 if (phdr
->p_type
== PT_LOAD
)
1394 load_addr
= phdr
->p_vaddr
;
1399 // Get the plt section header
1400 Elf_Scn
*scn
= NULL
;
1401 GElf_Shdr
*plt_shdr
= NULL
;
1402 GElf_Shdr plt_shdr_mem
;
1403 while ((scn
= elf_nextscn (elf
, scn
)))
1405 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &plt_shdr_mem
);
1406 assert (shdr
!= NULL
);
1407 if (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".plt") == 0)
1413 if (plt_shdr
== NULL
)
1416 // Layout of the plt section
1417 int plt0_entry_size
;
1420 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1421 switch (em
->e_machine
)
1423 case EM_386
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1424 case EM_X86_64
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1425 case EM_ARM
: plt0_entry_size
= 20; plt_entry_size
= 12; break;
1426 case EM_AARCH64
:plt0_entry_size
= 32; plt_entry_size
= 16; break;
1431 throw SEMANTIC_ERROR(".plt is not supported on this architecture");
1435 while ((scn
= elf_nextscn (elf
, scn
)))
1438 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
1439 bool have_rela
= false;
1440 bool have_rel
= false;
1444 assert (shdr
!= NULL
);
1446 if ((have_rela
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rela.plt") == 0))
1447 || (have_rel
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rel.plt") == 0)))
1449 /* Get the data of the section. */
1450 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1451 assert (data
!= NULL
);
1452 /* Get the symbol table information. */
1453 Elf_Scn
*symscn
= elf_getscn (elf
, shdr
->sh_link
);
1454 GElf_Shdr symshdr_mem
;
1455 GElf_Shdr
*symshdr
= gelf_getshdr (symscn
, &symshdr_mem
);
1456 assert (symshdr
!= NULL
);
1457 Elf_Data
*symdata
= elf_getdata (symscn
, NULL
);
1458 assert (symdata
!= NULL
);
1460 unsigned int nsyms
= shdr
->sh_size
/ shdr
->sh_entsize
;
1462 for (unsigned int cnt
= 0; cnt
< nsyms
; ++cnt
)
1465 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1466 if (em
== 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
1469 GElf_Rela
*rela
= NULL
;
1471 GElf_Rel
*rel
= NULL
;
1474 rela
= gelf_getrela (data
, cnt
, &relamem
);
1475 assert (rela
!= NULL
);
1479 rel
= gelf_getrel (data
, cnt
, &relmem
);
1480 assert (rel
!= NULL
);
1484 Elf_Data
*xndxdata
= NULL
;
1486 gelf_getsymshndx (symdata
, xndxdata
,
1487 GELF_R_SYM (have_rela
? rela
->r_info
: rel
->r_info
),
1489 assert (sym
!= NULL
);
1490 Dwarf_Addr addr
= plt_shdr
->sh_offset
+ plt0_entry_size
+ cnt
* plt_entry_size
;
1492 if (elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
))
1493 (*callback
) (object
, elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
), addr
+ load_addr
);
1502 // Comparator function for sorting
1504 compare_lines(Dwarf_Line
* a
, Dwarf_Line
* b
)
1509 int lineno_a
= DWARF_LINENO(a
);
1510 int lineno_b
= DWARF_LINENO(b
);
1511 if (lineno_a
== lineno_b
)
1512 return DWARF_LINEADDR(a
) < DWARF_LINEADDR(b
);
1513 return lineno_a
< lineno_b
;
1516 // Comparator object for searching Dwarf_Lines with a specific lineno when we
1517 // don't have a Dwarf_Line of our own to search for (hence why a or b is always
1519 struct lineno_comparator
{
1521 lineno_comparator(int lineno
): lineno(lineno
) {}
1522 bool operator() (Dwarf_Line
* a
, Dwarf_Line
* b
)
1526 return DWARF_LINENO(a
) < lineno
;
1528 return lineno
< DWARF_LINENO(b
);
1532 // Returns a range of lines in between begin and end with wanted lineno. If
1533 // none exist, points to where it would have been.
1534 static lines_range_t
1535 lineno_equal_range(lines_t
* v
, int lineno
)
1537 lineno_comparator
lc(lineno
);
1538 return equal_range(v
->begin(), v
->end(), (Dwarf_Line
*)NULL
, lc
);
1541 // Interface to CU lines cache sorted by lineno
1543 dwflpp::get_cu_lines_sorted_by_lineno(const char *srcfile
)
1547 srcfile_lines_cache_t
*srcfile_lines
= cu_lines_cache
[cu
];
1550 srcfile_lines
= new srcfile_lines_cache_t();
1551 cu_lines_cache
[cu
] = srcfile_lines
;
1554 lines_t
*lines
= (*srcfile_lines
)[srcfile
];
1557 size_t nlines_cu
= 0;
1558 Dwarf_Lines
*lines_cu
= NULL
;
1559 DWARF_ASSERT("dwarf_getsrclines",
1560 dwarf_getsrclines(cu
, &lines_cu
, &nlines_cu
));
1562 lines
= new lines_t();
1563 (*srcfile_lines
)[srcfile
] = lines
;
1565 for (size_t i
= 0; i
< nlines_cu
; i
++)
1567 Dwarf_Line
*line
= dwarf_onesrcline(lines_cu
, i
);
1568 const char *linesrc
= DWARF_LINESRC(line
);
1569 if (strcmp(srcfile
, linesrc
))
1571 lines
->push_back(line
);
1574 if (lines
->size() > 1)
1575 sort(lines
->begin(), lines
->end(), compare_lines
);
1577 if (sess
.verbose
> 3)
1579 clog
<< _F("found the following lines for %s:", srcfile
) << endl
;
1580 lines_t::iterator i
;
1581 for (i
= lines
->begin(); i
!= lines
->end(); ++i
)
1582 cout
<< DWARF_LINENO(*i
) << " = " << hex
1583 << DWARF_LINEADDR(*i
) << dec
<< endl
;
1590 get_func_first_line(Dwarf_Die
*cu
, base_func_info
& func
)
1592 // dwarf_getsrc_die() uses binary search to find the Dwarf_Line, but will
1593 // return the wrong line if not found.
1594 Dwarf_Line
*line
= dwarf_getsrc_die(cu
, func
.entrypc
);
1595 if (line
&& DWARF_LINEADDR(line
) == func
.entrypc
)
1598 // Line not found (or line at wrong addr). We have to resort to a slower
1599 // linear method. We won't find an exact match (probably this is an inlined
1600 // instance), so just settle on the first Dwarf_Line with lowest addr which
1601 // falls in the die.
1603 Dwarf_Lines
*lines
= NULL
;
1604 DWARF_ASSERT("dwarf_getsrclines",
1605 dwarf_getsrclines(cu
, &lines
, &nlines
));
1607 for (size_t i
= 0; i
< nlines
; i
++)
1609 line
= dwarf_onesrcline(lines
, i
);
1610 if (dwarf_haspc(&func
.die
, DWARF_LINEADDR(line
)))
1617 collect_lines_in_die(lines_range_t range
, Dwarf_Die
*die
)
1619 lines_t lines_in_die
;
1620 for (lines_t::iterator line
= range
.first
;
1621 line
!= range
.second
; ++line
)
1622 if (dwarf_haspc(die
, DWARF_LINEADDR(*line
)))
1623 lines_in_die
.push_back(*line
);
1624 return lines_in_die
;
1628 add_matching_lines_in_func(Dwarf_Die
*cu
,
1630 base_func_info
& func
,
1631 lines_t
& matching_lines
)
1633 Dwarf_Line
*start_line
= get_func_first_line(cu
, func
);
1637 for (int lineno
= DWARF_LINENO(start_line
);;)
1639 lines_range_t range
= lineno_equal_range(cu_lines
, lineno
);
1641 // We consider the lineno still part of the die if at least one of them
1642 // falls in the die.
1643 lines_t lines_in_die
= collect_lines_in_die(range
, &func
.die
);
1644 if (lines_in_die
.empty())
1647 // Just pick the first LR even if there are more than one. Since the lines
1648 // are sorted by lineno and then addr, the first one is the one with the
1650 matching_lines
.push_back(lines_in_die
[0]);
1652 // break out if there are no more lines, otherwise, go to the next lineno
1653 if (range
.second
== cu_lines
->end())
1655 lineno
= DWARF_LINENO(*range
.second
);
1660 dwflpp::collect_all_lines(char const * srcfile
,
1661 base_func_info_map_t
& funcs
,
1662 lines_t
& matching_lines
)
1664 // This is where we handle WILDCARD lineno types.
1665 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1666 for (base_func_info_map_t::iterator func
= funcs
.begin();
1667 func
!= funcs
.end(); ++func
)
1668 add_matching_lines_in_func(cu
, cu_lines
, *func
, matching_lines
);
1673 add_matching_line_in_die(lines_t
*cu_lines
,
1674 lines_t
& matching_lines
,
1675 Dwarf_Die
*die
, int lineno
)
1677 lines_range_t lineno_range
= lineno_equal_range(cu_lines
, lineno
);
1678 lines_t lines_in_die
= collect_lines_in_die(lineno_range
, die
);
1679 if (lines_in_die
.empty())
1682 // Even if there are more than 1 LRs, just pick the first one. Since the lines
1683 // are sorted by lineno and then addr, the first one is the one with the
1684 // lowest addr. This is similar to what GDB does.
1685 matching_lines
.push_back(lines_in_die
[0]);
1689 dwflpp::collect_lines_for_single_lineno(char const * srcfile
,
1692 base_func_info_map_t
& funcs
,
1693 lines_t
& matching_lines
)
1695 /* Here, we handle ABSOLUTE and RELATIVE lineno types. Relative line numbers
1696 * are a bit special. The issue is that functions (esp. inlined ones) may not
1697 * even have a LR corresponding to the first valid line of code. So, applying
1698 * an offset to the 'first' LR found in the DIE can be quite imprecise.
1699 * Instead, we use decl_line, which although does not necessarily have a
1700 * LR associated with it (it can sometimes still happen esp. if the code is
1701 * written in OTB-style), it serves as an anchor on which we can apply the
1702 * offset to yield a lineno that will not change with compiler optimization.
1703 * It also has the added benefit of being consistent with the lineno
1704 * printed by e.g. stap -l kernel.function("vfs_read"), so users can be
1705 * confident from what lineno we adjust.
1707 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1708 for (base_func_info_map_t::iterator func
= funcs
.begin();
1709 func
!= funcs
.end(); ++func
)
1710 add_matching_line_in_die(cu_lines
, matching_lines
, &func
->die
,
1711 is_relative
? lineno
+ func
->decl_line
1716 functions_have_lineno(base_func_info_map_t
& funcs
,
1717 lines_t
*lines
, int lineno
)
1719 lines_range_t lineno_range
= lineno_equal_range(lines
, lineno
);
1720 if (lineno_range
.first
== lineno_range
.second
)
1721 return false; // no LRs at this lineno
1723 for (base_func_info_map_t::iterator func
= funcs
.begin();
1724 func
!= funcs
.end(); ++func
)
1725 if (!collect_lines_in_die(lineno_range
, &func
->die
).empty())
1731 // returns pair of valid linenos surrounding target lineno
1733 dwflpp::get_nearest_linenos(char const * srcfile
,
1735 base_func_info_map_t
& funcs
)
1738 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1740 // Look around lineno for linenos with LRs.
1741 pair
<int,int> nearest_linenos
= make_pair(-1, -1);
1742 for (size_t i
= 1; i
< 6; ++i
)
1744 if (nearest_linenos
.first
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
-i
))
1745 nearest_linenos
.first
= lineno
- i
;
1746 if (nearest_linenos
.second
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
+i
))
1747 nearest_linenos
.second
= lineno
+ i
;
1750 return nearest_linenos
;
1753 // returns nearest valid lineno to target lineno
1755 dwflpp::get_nearest_lineno(char const * srcfile
,
1757 base_func_info_map_t
& funcs
)
1760 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1762 if (nearest_linenos
.first
> 0
1763 && nearest_linenos
.second
> 0)
1765 // pick the nearest line number (break tie to upper)
1766 if (lineno
- nearest_linenos
.first
< nearest_linenos
.second
- lineno
)
1767 return nearest_linenos
.first
;
1769 return nearest_linenos
.second
;
1771 else if (nearest_linenos
.first
> 0)
1772 return nearest_linenos
.first
;
1773 else if (nearest_linenos
.second
> 0)
1774 return nearest_linenos
.second
;
1780 dwflpp::suggest_alternative_linenos(char const * srcfile
,
1782 base_func_info_map_t
& funcs
)
1785 pair
<int,int> nearest_linenos
= get_nearest_linenos(srcfile
, lineno
, funcs
);
1787 stringstream advice
;
1788 advice
<< _F("no line records for %s:%d [man error::dwarf]", srcfile
, lineno
);
1790 if (nearest_linenos
.first
> 0 || nearest_linenos
.second
> 0)
1792 //TRANSLATORS: Here we are trying to advise what source file
1793 //TRANSLATORS: to attempt.
1794 advice
<< _(" (try ");
1795 if (nearest_linenos
.first
> 0)
1796 advice
<< ":" << nearest_linenos
.first
;
1797 if (nearest_linenos
.first
> 0 && nearest_linenos
.second
> 0)
1798 advice
<< _(" or ");
1799 if (nearest_linenos
.second
> 0)
1800 advice
<< ":" << nearest_linenos
.second
;
1803 throw SEMANTIC_ERROR (advice
.str());
1806 static base_func_info_map_t
1807 get_funcs_in_srcfile(base_func_info_map_t
& funcs
,
1808 const char * srcfile
)
1810 base_func_info_map_t matching_funcs
;
1811 for (base_func_info_map_t::iterator func
= funcs
.begin();
1812 func
!= funcs
.end(); ++func
)
1813 if (strcmp(srcfile
, func
->decl_file
) == 0)
1814 matching_funcs
.push_back(*func
);
1815 return matching_funcs
;
1819 dwflpp::iterate_over_srcfile_lines
<void>(char const * srcfile
,
1820 const vector
<int>& linenos
,
1821 enum lineno_t lineno_type
,
1822 base_func_info_map_t
& funcs
,
1823 void (* callback
) (Dwarf_Addr
,
1828 /* Matching line records (LRs) to user-provided linenos is trickier than it
1829 * seems. The fate of all functions is one of three possibilities:
1830 * 1. it's a normal function, with a subprogram DIE and a bona fide lowpc
1831 * and highpc attribute.
1832 * 2. it's an inlined function (one/multiple inlined_subroutine DIE, with one
1833 * abstract_origin DIE)
1834 * 3. it's both a normal function and an inlined function. For example, if
1835 * the funtion has been inlined only in some places, we'll have a DIE for
1836 * the normal subprogram DIE as well as inlined_subroutine DIEs.
1838 * Multiple LRs for the same lineno but different addresses can simply happen
1839 * due to the function appearing in multiple forms. E.g. a function inlined
1840 * in two spots can yield two sets of LRs for its linenos at the different
1841 * addresses where it is inlined.
1842 * This is why the collect_* functions used here try to match up LRs back
1843 * to their originating DIEs. For example, in the function
1844 * collect_lines_for_single_lineno(), we filter first by DIE so that a lineno
1845 * corresponding to multiple addrs in multiple inlined_subroutine DIEs yields
1846 * a probe for each of them.
1850 // only work on the functions found in the current srcfile
1851 base_func_info_map_t current_funcs
= get_funcs_in_srcfile(funcs
, srcfile
);
1852 if (current_funcs
.empty())
1856 lines_t matching_lines
;
1857 if (lineno_type
== ABSOLUTE
)
1858 collect_lines_for_single_lineno(srcfile
, linenos
[0], false, /* is_relative */
1859 current_funcs
, matching_lines
);
1860 else if (lineno_type
== RELATIVE
)
1861 collect_lines_for_single_lineno(srcfile
, linenos
[0], true, /* is_relative */
1862 current_funcs
, matching_lines
);
1863 else if (lineno_type
== WILDCARD
)
1864 collect_all_lines(srcfile
, current_funcs
, matching_lines
);
1865 else if (lineno_type
== ENUMERATED
)
1866 for (vector
<int>::const_iterator it
= linenos
.begin(); it
!= linenos
.end(); it
++)
1867 collect_lines_for_single_lineno(srcfile
, *it
, false, /* is_relative */
1868 current_funcs
, matching_lines
);
1870 // should we try to collect the nearest line if we didn't collect everything on first try?
1871 if (matching_lines
.empty() && has_nearest
)
1873 int lineno
= linenos
[0];
1874 if (lineno_type
== RELATIVE
)
1875 // just pick the first function and make it relative to that
1876 lineno
+= current_funcs
[0].decl_line
;
1878 int nearest_lineno
= get_nearest_lineno(srcfile
, lineno
, current_funcs
);
1879 if (nearest_lineno
> 0)
1880 collect_lines_for_single_lineno(srcfile
, nearest_lineno
, false, /* is_relative */
1881 current_funcs
, matching_lines
);
1884 // call back with matching lines
1885 if (!matching_lines
.empty())
1887 set
<Dwarf_Addr
> probed_addrs
;
1888 for (lines_t::iterator line
= matching_lines
.begin();
1889 line
!= matching_lines
.end(); ++line
)
1891 int lineno
= DWARF_LINENO(*line
);
1892 Dwarf_Addr addr
= DWARF_LINEADDR(*line
);
1893 bool is_new_addr
= probed_addrs
.insert(addr
).second
;
1895 callback(addr
, lineno
, data
);
1898 // No LRs found at the wanted lineno. So let's suggest other ones if user was
1899 // targeting a specific lineno (ABSOLUTE or RELATIVE).
1900 else if (lineno_type
== ABSOLUTE
||
1901 lineno_type
== RELATIVE
)
1903 int lineno
= linenos
[0];
1904 if (lineno_type
== RELATIVE
)
1905 // just pick the first function and make it relative to that
1906 lineno
+= current_funcs
[0].decl_line
;
1908 suggest_alternative_linenos(srcfile
, lineno
, current_funcs
);
1914 dwflpp::iterate_over_labels
<void>(Dwarf_Die
*begin_die
,
1916 const base_func_info
& function
,
1917 const vector
<int>& linenos
,
1918 enum lineno_t lineno_type
,
1920 void (* callback
)(const base_func_info
&,
1930 Dwarf_Die die
, import
;
1932 int res
= dwarf_child (begin_die
, &die
);
1934 return; // die without children, bail out.
1938 switch (dwarf_tag(&die
))
1941 name
= dwarf_diename (&die
);
1944 || (name_has_wildcard(sym
)
1945 && function_name_matches_pattern (name
, sym
))))
1947 // Don't try to be smart. Just drop no addr labels.
1948 Dwarf_Addr stmt_addr
;
1949 if (dwarf_lowpc (&die
, &stmt_addr
) == 0)
1951 // Get the file/line number for this label
1953 const char *file
= dwarf_decl_file (&die
) ?: "<unknown source>";
1954 dwarf_decl_line (&die
, &dline
);
1956 vector
<Dwarf_Die
> scopes
= getscopes_die(&die
);
1957 if (scopes
.size() > 1)
1960 if (!inner_die_containing_pc(scopes
[1], stmt_addr
, scope
))
1962 sess
.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not "
1963 "contained by its scope '%s' (dieoffset: %s) -- bad"
1964 " debuginfo? [man error::dwarf]", name
, lex_cast_hex(stmt_addr
).c_str(),
1965 lex_cast_hex(dwarf_dieoffset(&die
)).c_str(),
1966 (dwarf_diename(&scope
) ?: "<unknown>"),
1967 lex_cast_hex(dwarf_dieoffset(&scope
)).c_str()));
1970 bool matches_lineno
;
1971 if (lineno_type
== ABSOLUTE
)
1972 matches_lineno
= dline
== linenos
[0];
1973 else if (lineno_type
== RELATIVE
)
1974 matches_lineno
= dline
== linenos
[0] + function
.decl_line
;
1975 else if (lineno_type
== ENUMERATED
)
1976 matches_lineno
= (binary_search(linenos
.begin(), linenos
.end(), dline
));
1978 matches_lineno
= true;
1981 callback(function
, name
, file
, dline
,
1982 &scope
, stmt_addr
, data
);
1988 case DW_TAG_subprogram
:
1989 case DW_TAG_inlined_subroutine
:
1990 // Stay within our filtered function
1993 case DW_TAG_imported_unit
:
1994 // Iterate over the children of the imported unit as if they
1995 // were inserted in place.
1996 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
1997 iterate_over_labels (&import
, sym
, function
, linenos
,
1998 lineno_type
, data
, callback
);
2002 if (dwarf_haschildren (&die
))
2003 iterate_over_labels (&die
, sym
, function
, linenos
,
2004 lineno_type
, data
, callback
);
2008 while (dwarf_siblingof (&die
, &die
) == 0);
2011 // Mini 'query-like' struct to help us navigate callbacks during
2012 // external function resolution
2013 struct external_function_query
{
2019 external_function_query(dwflpp
* dw
, const string
& name
):
2020 dw(dw
), name(name
), addr(0), resolved(false) {}
2024 dwflpp::external_function_cu_callback (Dwarf_Die
* cu
, external_function_query
*efq
)
2026 efq
->dw
->focus_on_cu(cu
);
2027 return efq
->dw
->iterate_over_functions(external_function_func_callback
,
2032 dwflpp::external_function_func_callback (Dwarf_Die
* func
, external_function_query
*efq
)
2034 Dwarf_Attribute external
;
2035 Dwarf_Addr func_addr
;
2036 if (dwarf_attr_integrate(func
, DW_AT_external
, &external
) != NULL
&&
2037 dwarf_lowpc(func
, &func_addr
) == 0)
2040 efq
->addr
= func_addr
;
2041 efq
->resolved
= true;
2042 return DWARF_CB_ABORT
; // we found it so stop here
2048 dwflpp::iterate_over_callees
<void>(Dwarf_Die
*begin_die
,
2050 long recursion_depth
,
2052 void (* callback
)(base_func_info
&,
2056 base_func_info
& caller
,
2057 stack
<Dwarf_Addr
> *callers
)
2061 Dwarf_Die die
, import
;
2063 // DIE of abstract_origin found in die
2066 // callee's entry pc (= where we'll probe)
2067 Dwarf_Addr func_addr
;
2069 // caller's unwind pc during call (to match against bt for filtering)
2070 Dwarf_Addr caller_uw_addr
;
2072 Dwarf_Attribute attr
;
2074 base_func_info callee
;
2075 if (dwarf_child(begin_die
, &die
) != 0)
2076 return; // die without children, bail out.
2078 bool free_callers
= false;
2079 if (callers
== NULL
) /* first call */
2081 callers
= new stack
<Dwarf_Addr
>();
2082 free_callers
= true;
2087 bool inlined
= false;
2088 switch (dwarf_tag(&die
))
2090 case DW_TAG_inlined_subroutine
:
2092 case DW_TAG_GNU_call_site
:
2093 callee
.name
= dwarf_diename(&die
) ?: "";
2094 if (callee
.name
.empty())
2096 if (callee
.name
!= sym
)
2098 if (!name_has_wildcard(sym
))
2100 if (!function_name_matches_pattern(callee
.name
, sym
))
2104 /* In both cases (call sites and inlines), we want the
2105 * abstract_origin. The difference is that in inlines, the addr is
2106 * in the die itself, whereas for call sites, the addr is in the
2107 * abstract_origin's die.
2108 * Note that in the case of inlines, we're only calling back
2109 * for that inline instance, not all. This is what we want, since
2110 * it will only be triggered when 'called' from the target func,
2111 * which is something we have to emulate for non-inlined funcs
2112 * (which is the purpose of the caller_uw_addr below) */
2113 if (dwarf_attr_die(&die
, DW_AT_abstract_origin
, &origin
) == NULL
)
2116 // the low_pc of the die in either cases is the pc that would
2117 // show up in a backtrace (inlines are a special case in which
2118 // the die's low_pc is also the abstract_origin's low_pc = the
2119 // 'start' of the inline instance)
2120 if (dwarf_lowpc(&die
, &caller_uw_addr
) != 0)
2124 func_addr
= caller_uw_addr
;
2125 else if (dwarf_lowpc(&origin
, &func_addr
) != 0)
2127 // function doesn't have a low_pc, is it external?
2128 if (dwarf_attr_integrate(&origin
, DW_AT_external
,
2131 // let's iterate over the CUs and find it. NB: it's
2132 // possible we could have also done this by creating a
2133 // probe point with .exported tacked on and rerunning it
2134 // through derive_probe(). But since we're already on the
2135 // dwflpp side of things, and we already have access to
2136 // everything we need, let's try to be self-sufficient.
2138 // remember old focus
2139 Dwarf_Die
*old_cu
= cu
;
2141 external_function_query
efq(this, dwarf_linkage_name(&origin
) ?: callee
.name
);
2142 iterate_over_cus(external_function_cu_callback
, &efq
, false);
2147 if (!efq
.resolved
) // did we resolve it?
2150 func_addr
= efq
.addr
;
2153 // non-external function without low_pc, jump ship
2157 // We now have the addr to probe in func_addr, and the DIE
2158 // from which to obtain file/line info in origin
2160 // Get the file/line number for this callee
2161 callee
.decl_file
= dwarf_decl_file (&origin
) ?: "<unknown source>";
2162 dwarf_decl_line (&origin
, &callee
.decl_line
);
2164 // add as a caller to match against
2166 callers
->push(caller_uw_addr
);
2168 callee
.die
= inlined
? die
: origin
;
2169 callee
.entrypc
= func_addr
;
2170 callback(callee
, caller
, callers
, data
);
2172 // If it's a tail call, print a warning that it may not be caught
2174 && dwarf_attr_integrate(&die
, DW_AT_GNU_tail_call
, &attr
) != NULL
)
2175 sess
.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: "
2176 ".callee probe may not fire. Try placing the probe "
2177 "directly on the callee function instead.",
2178 callee
.name
.c_str(), caller
.name
.c_str()));
2180 // For .callees(N) probes, we recurse on this callee. Note that we
2181 // pass the callee we just found as the caller arg for this recursion,
2182 // since it (the callee we just found) will be the caller of whatever
2183 // callees found inside this recursion.
2184 if (recursion_depth
> 1)
2185 iterate_over_callees(inlined
? &die
: &origin
,
2186 sym
, recursion_depth
-1, data
,
2187 callback
, callee
, callers
);
2193 case DW_TAG_subprogram
:
2194 break; // don't leave our filtered func
2196 case DW_TAG_imported_unit
:
2197 // Iterate over the children of the imported unit as if they
2198 // were inserted in place.
2199 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2200 // NB: we pass the same caller arg into it
2201 iterate_over_callees (&import
, sym
, recursion_depth
, data
,
2202 callback
, caller
, callers
);
2206 if (dwarf_haschildren (&die
))
2207 // NB: we pass the same caller arg into it
2208 iterate_over_callees (&die
, sym
, recursion_depth
, data
,
2209 callback
, caller
, callers
);
2213 while (dwarf_siblingof (&die
, &die
) == 0);
2215 if (free_callers
&& callers
!= NULL
)
2221 dwflpp::collect_srcfiles_matching (string
const & pattern
,
2222 set
<string
> & filtered_srcfiles
)
2228 Dwarf_Files
*srcfiles
;
2230 // PR 5049: implicit * in front of given path pattern.
2231 // NB: fnmatch() is used without FNM_PATHNAME.
2232 string prefixed_pattern
= string("*/") + pattern
;
2234 DWARF_ASSERT ("dwarf_getsrcfiles",
2235 dwarf_getsrcfiles (cu
, &srcfiles
, &nfiles
));
2237 for (size_t i
= 0; i
< nfiles
; ++i
)
2239 char const * fname
= dwarf_filesrc (srcfiles
, i
, NULL
, NULL
);
2240 if (fnmatch (pattern
.c_str(), fname
, 0) == 0 ||
2241 fnmatch (prefixed_pattern
.c_str(), fname
, 0) == 0)
2243 filtered_srcfiles
.insert (fname
);
2245 clog
<< _F("selected source file '%s'\n", fname
);
2253 dwflpp::resolve_prologue_endings (func_info_map_t
& funcs
)
2255 // When a program is compiled with no optimization, GCC does no variable
2256 // tracking, which means that location info is actually only really valid
2257 // after the prologue, even though GCC reports it as valid during. So we need
2258 // to find the prologue ends to get accurate info. This may or may not be the
2259 // first address that has a source line distinct from the function
2266 Dwarf_Lines
*lines
= NULL
;
2269 malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue
2270 sys_get?id in kernel/timer.c no-prologue
2271 sys_exit_group tail-recursive
2272 {do_,}sys_open extra-long-prologue (gcc 3.4)
2273 cpu_to_logical_apicid NULL-decl_file
2276 // Fetch all srcline records, sorted by address. No need to free lines, it's a
2277 // direct pointer to the CU's cached lines.
2278 if (dwarf_getsrclines(cu
, &lines
, &nlines
) != 0
2279 || lines
== NULL
|| nlines
== 0)
2281 if (sess
.verbose
> 2)
2282 clog
<< _F("aborting prologue search: no source lines found for cu '%s'\n",
2287 // Dump them into our own array for easier searching. They should already be
2288 // sorted by addr, but we doublecheck that here. We want to keep the indices
2289 // between lines and addrs the same.
2290 vector
<Dwarf_Addr
> addrs
;
2291 for (size_t i
= 0; i
< nlines
; i
++)
2293 Dwarf_Line
* line
= dwarf_onesrcline(lines
, i
);
2294 Dwarf_Addr addr
= DWARF_LINEADDR(line
);
2295 if (!addrs
.empty() && addr
< addrs
.back())
2296 throw SEMANTIC_ERROR(_("lines from dwarf_getsrclines() not sorted"));
2297 addrs
.push_back(addr
);
2299 // We normally ignore a function's decl_line, since it is associated with the
2300 // line at which the identifier appears in the declaration, and has no
2301 // meaningful relation to the lineno associated with the entrypc (which is
2302 // normally the lineno of '{', which could occur at the same line as the
2303 // declaration, or lower down).
2304 // However, if the CU was compiled using GCC < 4.4, then the decl_line
2305 // actually represents the lineno of '{' as well, in which case if the lineno
2306 // associated with the entrypc is != to the decl_line, it means the compiler
2307 // scraped/optimized off some of the beginning of the function and the safest
2308 // thing we can do is consider it naked.
2309 bool consider_decl_line
= false;
2312 if (is_gcc_producer(cu
, prod
, vers
)
2313 && strverscmp(vers
.c_str(), "4.4.0") < 0)
2314 consider_decl_line
= true;
2317 for(func_info_map_t::iterator it
= funcs
.begin(); it
!= funcs
.end(); it
++)
2320 Dwarf_Addr
* bkpts
= 0;
2321 int n
= dwarf_entry_breakpoints (& it
->die
, & bkpts
);
2326 Dwarf_Addr entrypc
= it
->entrypc
;
2327 Dwarf_Addr highpc
; // NB: highpc is exclusive: [entrypc,highpc)
2328 DWFL_ASSERT ("dwarf_highpc", dwarf_highpc (& it
->die
,
2331 if (it
->decl_file
== 0) it
->decl_file
= "";
2333 unsigned entrypc_srcline_idx
= 0;
2334 Dwarf_Line
*entrypc_srcline
= NULL
;
2336 vector
<Dwarf_Addr
>::const_iterator it_addr
=
2337 lower_bound(addrs
.begin(), addrs
.end(), entrypc
);
2338 if (it_addr
!= addrs
.end() && *it_addr
== entrypc
)
2340 entrypc_srcline_idx
= it_addr
- addrs
.begin();
2341 entrypc_srcline
= dwarf_onesrcline(lines
, entrypc_srcline_idx
);
2345 if (!entrypc_srcline
)
2347 if (sess
.verbose
> 2)
2348 clog
<< _F("missing entrypc dwarf line record for function '%s'\n",
2350 // This is probably an inlined function. We'll end up using
2351 // its lowpc as a probe address.
2357 if (sess
.verbose
> 2)
2358 clog
<< _F("null entrypc dwarf line record for function '%s'\n",
2360 // This is probably an inlined function. We'll skip this instance;
2366 clog
<< _F("searching for prologue of function '%s' %#" PRIx64
"-%#" PRIx64
2367 "@%s:%d\n", it
->name
.c_str(), entrypc
, highpc
, it
->decl_file
,
2370 // For each function, we look for the prologue-end marker (e.g. clang
2371 // outputs one). If there is no explicit marker (e.g. GCC does not), we
2372 // accept a bigger or equal lineno as a prologue end (this catches GCC's
2373 // 0-line advances).
2375 // We may have to skip a few because some old compilers plop
2376 // in dummy line records for longer prologues. If we go too
2377 // far (addr >= highpc), we take the previous one. Or, it may
2378 // be the first one, if the function had no prologue, and thus
2379 // the entrypc maps to a statement in the body rather than the
2382 int entrypc_srcline_lineno
= DWARF_LINENO(entrypc_srcline
);
2383 unsigned postprologue_srcline_idx
= entrypc_srcline_idx
;
2384 Dwarf_Line
*postprologue_srcline
= entrypc_srcline
;
2386 while (postprologue_srcline_idx
< nlines
)
2388 postprologue_srcline
= dwarf_onesrcline(lines
,
2389 postprologue_srcline_idx
);
2390 Dwarf_Addr lineaddr
= DWARF_LINEADDR(postprologue_srcline
);
2391 const char* linesrc
= DWARF_LINESRC(postprologue_srcline
);
2392 int lineno
= DWARF_LINENO(postprologue_srcline
);
2393 bool lineprologue_end
= DWARF_LINEPROLOGUEEND(postprologue_srcline
);
2396 clog
<< _F("checking line record %#" PRIx64
"@%s:%d%s\n", lineaddr
,
2397 linesrc
, lineno
, lineprologue_end
? " (marked)" : "");
2399 // have we passed the function?
2400 if (lineaddr
>= highpc
)
2402 // is there an explicit prologue_end marker?
2403 if (lineprologue_end
)
2405 // is it a different file?
2406 if (strcmp(linesrc
, it
->decl_file
))
2408 // OK, it's the same file, but is it a different line?
2409 if (lineno
!= entrypc_srcline_lineno
)
2411 // Same file and line, is this a second line record (e.g. 0-line advance)?
2412 if (postprologue_srcline_idx
!= entrypc_srcline_idx
)
2414 // This is the first iteration. Is decl_line meaningful and is the
2415 // lineno past the decl_line?
2416 if (consider_decl_line
&& lineno
!= it
->decl_line
)
2419 // Let's try the next srcline.
2420 postprologue_srcline_idx
++;
2422 } // loop over srclines
2425 Dwarf_Addr postprologue_addr
= DWARF_LINEADDR(postprologue_srcline
);
2426 if (postprologue_addr
>= highpc
)
2428 // pick addr of previous line record
2429 Dwarf_Line
*lr
= dwarf_onesrcline(lines
, postprologue_srcline_idx
-1);
2430 postprologue_addr
= DWARF_LINEADDR(lr
);
2433 it
->prologue_end
= postprologue_addr
;
2437 clog
<< _F("prologue found function '%s'", it
->name
.c_str());
2438 // Add a little classification datum
2439 //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free)
2440 if (postprologue_addr
== entrypc
)
2441 clog
<< _(" (naked)");
2442 //TRANSLATORS: Here we're adding some classification datum (ie we went over)
2443 if (DWARF_LINEADDR(postprologue_srcline
) >= highpc
)
2444 clog
<< _(" (tail-call?)");
2445 //TRANSLATORS: Here we're adding some classification datum (ie it was marked)
2446 if (DWARF_LINEPROLOGUEEND(postprologue_srcline
))
2447 clog
<< _(" (marked)");
2449 clog
<< " = 0x" << hex
<< postprologue_addr
<< dec
<< "\n";
2452 } // loop over functions
2457 dwflpp::function_entrypc (Dwarf_Addr
* addr
)
2460 // PR10574: reject 0, which tends to be eliminated COMDAT
2461 return (dwarf_entrypc (function
, addr
) == 0 && *addr
!= 0);
2466 dwflpp::die_entrypc (Dwarf_Die
* die
, Dwarf_Addr
* addr
)
2469 string lookup_method
;
2473 lookup_method
= "dwarf_entrypc";
2474 rc
= dwarf_entrypc (die
, addr
);
2478 lookup_method
= "dwarf_ranges";
2483 ptrdiff_t offset
= dwarf_ranges (die
, 0, &base
, &begin
, &end
);
2484 if (offset
< 0) rc
= -1;
2485 else if (offset
> 0)
2490 // Now we need to check that there are no more ranges
2491 // associated with this function, which could conceivably
2492 // happen if a function is inlined, then pieces of it are
2493 // split amongst different conditional branches. It's not
2494 // obvious which of them to favour. As a heuristic, we
2495 // pick the beginning of the first range, and ignore the
2496 // others (but with a warning).
2499 while ((offset
= dwarf_ranges (die
, offset
, &base
, &begin
, &end
)) > 0)
2502 lookup_method
+= _F(", ignored %s more", lex_cast(extra
).c_str());
2506 // PR10574: reject subprograms where the entrypc address turns out
2507 // to be 0, since they tend to correspond to duplicate-eliminated
2508 // COMDAT copies of C++ functions.
2509 if (rc
== 0 && *addr
== 0)
2511 lookup_method
+= _(" (skip comdat)");
2515 if (sess
.verbose
> 2)
2516 clog
<< _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64
" (rc %d)", lookup_method
.c_str(),
2517 lex_cast_hex(dwarf_dieoffset(die
)).c_str(), *addr
, rc
) << endl
;
2524 dwflpp::function_die (Dwarf_Die
*d
)
2532 dwflpp::function_file (char const ** c
)
2536 *c
= dwarf_decl_file (function
) ?: "<unknown source>";
2541 dwflpp::function_line (int *linep
)
2544 dwarf_decl_line (function
, linep
);
2549 dwflpp::die_has_pc (Dwarf_Die
& die
, Dwarf_Addr pc
)
2551 int res
= dwarf_haspc (&die
, pc
);
2552 // dwarf_ranges will return -1 if a function die has no DW_AT_ranges
2554 // DWARF_ASSERT ("dwarf_haspc", res);
2560 dwflpp::inner_die_containing_pc(Dwarf_Die
& scope
, Dwarf_Addr addr
,
2565 // Sometimes we're in a bad scope to begin with -- just let it be. This can
2566 // happen for example if the compiler outputs a label PC that's just outside
2567 // the lexical scope. We can't really do anything about that, but variables
2568 // will probably not be accessible in this case.
2569 if (!die_has_pc(scope
, addr
))
2572 Dwarf_Die child
, import
;
2573 int rc
= dwarf_child(&result
, &child
);
2576 switch (dwarf_tag (&child
))
2578 case DW_TAG_imported_unit
:
2579 // The children of the imported unit need to be treated as if
2580 // they are inserted here. So look inside and set result if
2582 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2584 Dwarf_Die import_result
;
2585 if (inner_die_containing_pc(import
, addr
, import_result
))
2587 result
= import_result
;
2593 // lexical tags to recurse within the same starting scope
2594 // NB: this intentionally doesn't cross into inlines!
2595 case DW_TAG_lexical_block
:
2596 case DW_TAG_with_stmt
:
2597 case DW_TAG_catch_block
:
2598 case DW_TAG_try_block
:
2599 case DW_TAG_entry_point
:
2600 if (die_has_pc(child
, addr
))
2603 rc
= dwarf_child(&result
, &child
);
2607 rc
= dwarf_siblingof(&child
, &child
);
2614 dwflpp::loc2c_error (void *, const char *fmt
, ...)
2616 const char *msg
= "?";
2621 rc
= vasprintf (& tmp
, fmt
, ap
);
2627 throw SEMANTIC_ERROR (msg
);
2631 // This function generates code used for addressing computations of
2632 // target variables.
2634 dwflpp::emit_address (struct obstack
*pool
, Dwarf_Addr address
)
2636 int n
= dwfl_module_relocations (module
);
2637 DWFL_ASSERT ("dwfl_module_relocations", n
>= 0);
2638 Dwarf_Addr reloc_address
= address
;
2639 const char *secname
= "";
2642 int i
= dwfl_module_relocate_address (module
, &reloc_address
);
2643 DWFL_ASSERT ("dwfl_module_relocate_address", i
>= 0);
2644 secname
= dwfl_module_relocation_info (module
, i
, NULL
);
2647 if (sess
.verbose
> 2)
2649 clog
<< _F("emit dwarf addr %#" PRIx64
" => module %s section %s relocaddr %#" PRIx64
,
2650 address
, module_name
.c_str (), (secname
?: "null"),
2651 reloc_address
) << endl
;
2654 if (n
> 0 && !(n
== 1 && secname
== NULL
))
2656 DWFL_ASSERT ("dwfl_module_relocation_info", secname
);
2657 if (n
> 1 || secname
[0] != '\0')
2659 // This gives us the module name, and section name within the
2660 // module, for a kernel module (or other ET_REL module object).
2661 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2662 obstack_printf (pool
, "addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2663 module_name
.c_str(), secname
, reloc_address
);
2664 obstack_printf (pool
, "addr; })");
2666 else if (n
== 1 && module_name
== TOK_KERNEL
&& secname
[0] == '\0')
2668 // elfutils' way of telling us that this is a relocatable kernel address, which we
2669 // need to treat the same way here as dwarf_query::add_probe_point does: _stext.
2670 address
-= sess
.sym_stext
;
2672 // Note we "cache" the result here through a static because the
2673 // kernel will never move after being loaded (unlike modules and
2674 // user-space dynamic share libraries).
2675 obstack_printf (pool
, "({ static unsigned long addr = 0; ");
2676 obstack_printf (pool
, "if (addr==0) addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2677 module_name
.c_str(), secname
, address
); // PR10000 NB: not reloc_address
2678 obstack_printf (pool
, "addr; })");
2682 obstack_printf (pool
, "/* pragma:vma */");
2683 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2684 obstack_printf (pool
, "addr = _stp_umodule_relocate (\"%s\",%#" PRIx64
", current); ",
2685 resolve_path(module_name
.c_str()).c_str(), address
);
2686 obstack_printf (pool
, "addr; })");
2690 obstack_printf (pool
, "%#" PRIx64
"UL", address
); // assume as constant
2695 dwflpp::loc2c_emit_address (void *arg
, struct obstack
*pool
,
2698 static_cast<dwflpp
*>(arg
)->emit_address (pool
, address
);
2703 dwflpp::get_locals(vector
<Dwarf_Die
>& scopes
, set
<string
>& locals
)
2705 // XXX Shouldn't this be walking up to outer scopes too?
2707 get_locals_die(scopes
[0], locals
);
2711 dwflpp::get_locals_die(Dwarf_Die
& die
, set
<string
>& locals
)
2713 // Try to get the first child of die.
2714 Dwarf_Die child
, import
;
2715 if (dwarf_child (&die
, &child
) == 0)
2720 // Output each sibling's name (that is a variable or
2721 // parameter) to 'o'.
2722 switch (dwarf_tag (&child
))
2724 case DW_TAG_variable
:
2725 case DW_TAG_formal_parameter
:
2726 name
= dwarf_diename (&child
);
2728 locals
.insert(string("$") + name
);
2730 case DW_TAG_imported_unit
:
2731 // Treat the imported unit children as if they are
2732 // children of the given DIE.
2733 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2734 get_locals_die (import
, locals
);
2740 while (dwarf_siblingof (&child
, &child
) == 0);
2746 dwflpp::find_variable_and_frame_base (vector
<Dwarf_Die
>& scopes
,
2748 string
const & local
,
2749 const target_symbol
*e
,
2751 Dwarf_Attribute
*fb_attr_mem
)
2753 Dwarf_Die
*scope_die
= &scopes
[0];
2754 Dwarf_Attribute
*fb_attr
= NULL
;
2758 int declaring_scope
= dwarf_getscopevar (&scopes
[0], scopes
.size(),
2762 if (declaring_scope
< 0)
2765 get_locals(scopes
, locals
);
2766 string sugs
= levenshtein_suggest(local
, locals
); // probably not that many, so no limit
2768 throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)",
2770 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2771 module_name
.c_str(),
2772 lex_cast_hex(pc
).c_str(),
2773 (scope_die
== NULL
) ? "" : _("in"),
2774 (dwarf_diename(scope_die
) ?: "<unknown>"),
2775 (dwarf_diename(cu
) ?: "<unknown>"),
2777 ? (_("<no alternatives>"))
2778 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2781 throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)",
2783 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2784 module_name
.c_str(),
2785 (scope_die
== NULL
) ? "" : _("in"),
2786 (dwarf_diename(scope_die
) ?: "<unknown>"),
2789 ? (_("<no alternatives>"))
2790 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2794 /* Some GCC versions would output duplicate external variables, one
2795 without a location attribute. If so, try to find the other if it
2796 exists in the same scope. See GCC PR51410. */
2797 Dwarf_Attribute attr_mem
;
2798 if (dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
) == NULL
2799 && dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
) == NULL
2800 && dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
) != NULL
2801 && dwarf_tag(&scopes
[declaring_scope
]) == DW_TAG_compile_unit
)
2803 Dwarf_Die orig_vardie
= *vardie
;
2804 bool alt_found
= false;
2805 if (dwarf_child(&scopes
[declaring_scope
], vardie
) == 0)
2808 // Note, not handling DW_TAG_imported_unit, assuming GCC
2809 // version is recent enough to not need this workaround if
2810 // we would see an imported unit.
2811 if (dwarf_tag (vardie
) == DW_TAG_variable
2812 && strcmp (dwarf_diename (vardie
), local
.c_str ()) == 0
2813 && (dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
)
2815 && ((dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
)
2817 || (dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
)
2821 while (!alt_found
&& dwarf_siblingof(vardie
, vardie
) == 0);
2824 *vardie
= orig_vardie
;
2827 // Global vars don't need (cannot use) frame base in location descriptor.
2831 /* We start out walking the "lexical scopes" as returned by
2832 * as returned by dwarf_getscopes for the address, starting with the
2833 * declaring_scope that the variable was found in.
2835 vector
<Dwarf_Die
> physcopes
, *fbscopes
= &scopes
;
2836 for (size_t inner
= declaring_scope
;
2837 inner
< fbscopes
->size() && fb_attr
== NULL
;
2840 Dwarf_Die
& scope
= (*fbscopes
)[inner
];
2841 switch (dwarf_tag (&scope
))
2845 case DW_TAG_subprogram
:
2846 case DW_TAG_entry_point
:
2847 fb_attr
= dwarf_attr_integrate (&scope
,
2851 case DW_TAG_inlined_subroutine
:
2852 /* Unless we already are going through the "pyshical die tree",
2853 * we now need to start walking the die tree where this
2854 * subroutine is inlined to find the appropriate frame base. */
2855 if (declaring_scope
!= -1)
2857 physcopes
= getscopes_die(&scope
);
2858 if (physcopes
.empty())
2859 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
2860 local
.c_str()), e
->tok
);
2861 fbscopes
= &physcopes
;
2862 inner
= 0; // zero is current scope, for look will increase.
2863 declaring_scope
= -1;
2872 /* Returns a human readable string with suggested locations where a
2873 DIE attribute is valid. */
2875 suggested_locations_string(Dwarf_Attribute
*attr
)
2879 locsstr
= "<no alternatives for NULL attribute>";
2882 #if _ELFUTILS_PREREQ (0, 158)
2885 Dwarf_Addr base
, start
, end
;
2888 off
= dwarf_getlocations (attr
, off
, &base
,
2893 locsstr
= _("alternative locations: ");
2898 locsstr
+= lex_cast_hex(start
);
2900 locsstr
+= lex_cast_hex(end
);
2903 off
= dwarf_getlocations (attr
, off
, &base
,
2911 locsstr
= _("<no alternative locations>");
2913 locsstr
= _F("<error getting alternative locations: %s>",
2916 locsstr
= "<cannot suggest any alternative locations, elfutils too old>";
2917 #endif /* _ELFUTILS_PREREQ (0, 158) */
2923 /* Produce a human readable name for a DIE. */
2925 die_name_string (Dwarf_Die
*die
)
2928 const char *name
= dwarf_linkage_name(die
);
2930 name
= dwarf_diename (die
);
2932 size_t demangle_buffer_len
= 0;
2933 char *demangle_buffer
= NULL
;
2934 if (name
!= NULL
&& name
[0] == '_' && name
[1] == 'Z')
2937 char *dsymname
= abi::__cxa_demangle (name
, demangle_buffer
,
2938 &demangle_buffer_len
, &status
);
2940 name
= demangle_buffer
= dsymname
;
2945 res
= _("<unknown");
2946 free (demangle_buffer
);
2951 /* Returns a source line and column string based on the inlined DIE
2952 or based on the pc if DIE is NULL. */
2954 dwflpp::pc_die_line_string (Dwarf_Addr pc
, Dwarf_Die
*die
)
2959 const char *src
= NULL
;
2964 Dwarf_Line
*line
= dwarf_getsrc_die (cu
, pc
);
2967 src
= dwarf_linesrc (line
, NULL
, NULL
);
2968 dwarf_lineno (line
, &lineno
);
2969 dwarf_linecol (line
, &col
);
2975 if (dwarf_getsrcfiles (cu
, &files
, NULL
) == 0)
2977 Dwarf_Attribute attr
;
2979 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_file
, &attr
),
2982 src
= dwarf_filesrc (files
, val
, NULL
, NULL
);
2983 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_line
,
2987 if (dwarf_formudata (dwarf_attr (die
, DW_AT_call_column
,
3000 linestr
+= ":" + lex_cast(lineno
);
3002 linestr
+= ":" + lex_cast(col
);
3006 linestr
+= _("unknown source");
3011 /* Returns a human readable DIE offset for use in error messages.
3012 Includes DIE offset and DWARF file used. */
3014 dwflpp::die_location_as_string(Dwarf_Addr pc
, Dwarf_Die
*die
)
3019 locstr
+= _("dieoffset: ");
3020 locstr
+= lex_cast_hex(dwarf_dieoffset(die
));
3023 const char *debugfile
;
3024 locstr
+= _(" from ");
3025 if (dwfl_module_info (module
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
3026 &debugfile
) == NULL
|| debugfile
== NULL
)
3028 locstr
+= _("unknown debug file for ");
3029 locstr
+= module_name
;
3032 locstr
+= debugfile
;
3037 /* Returns a human readable (inlined) function and source file/line location
3038 for a DIE and pc location. */
3040 dwflpp::die_location_as_function_string(Dwarf_Addr pc
, Dwarf_Die
*die
)
3043 locstr
= _("function: ");
3045 /* Find the first function-like DIE with a name in scope. */
3046 Dwarf_Die funcdie_mem
;
3047 Dwarf_Die
*funcdie
= NULL
;
3048 string funcname
= "";
3049 Dwarf_Die
*scopes
= NULL
;
3050 int nscopes
= dwarf_getscopes (cu
, pc
, &scopes
);
3051 for (int i
= 0; funcname
== "" && i
< nscopes
; i
++)
3053 Dwarf_Die
*scope
= &scopes
[i
];
3054 int tag
= dwarf_tag (scope
);
3055 if (tag
== DW_TAG_subprogram
3056 || tag
== DW_TAG_inlined_subroutine
3057 || tag
== DW_TAG_entry_point
)
3058 funcname
= die_name_string (scope
);
3061 funcdie_mem
= *scope
;
3062 funcdie
= &funcdie_mem
;
3067 /* source location */
3069 locstr
+= _("<unknown> at ") + pc_die_line_string (pc
, NULL
);
3072 int nscopes
= dwarf_getscopes_die (funcdie
, &scopes
);
3075 /* scopes[0] == funcdie, the lowest level, for which we already have
3076 the name. This is the actual source location where it
3079 locstr
+= _(" at ");
3080 locstr
+= pc_die_line_string (pc
, NULL
);
3082 /* last_scope is the source location where the next inlined frame/function
3084 Dwarf_Die
*last_scope
= &scopes
[0];
3085 for (int i
= 1; i
< nscopes
; i
++)
3087 Dwarf_Die
*scope
= &scopes
[i
];
3088 int tag
= dwarf_tag (scope
);
3089 if (tag
!= DW_TAG_inlined_subroutine
3090 && tag
!= DW_TAG_entry_point
3091 && tag
!= DW_TAG_subprogram
)
3094 locstr
+= _(" inlined by ");
3095 locstr
+= die_name_string (scope
);
3096 locstr
+= _(" at ");
3097 locstr
+= pc_die_line_string (pc
, last_scope
);
3099 /* Found the "top-level" in which everything was inlined. */
3100 if (tag
== DW_TAG_subprogram
)
3109 locstr
+= _(" at ");
3110 locstr
+= pc_die_line_string (pc
, NULL
);
3119 dwflpp::translate_location(struct obstack
*pool
,
3120 Dwarf_Attribute
*attr
, Dwarf_Die
*die
,
3122 Dwarf_Attribute
*fb_attr
,
3123 struct location
**tail
,
3124 const target_symbol
*e
)
3127 /* DW_AT_data_member_location, can be either constant offsets
3128 (struct member fields), or full blown location expressions. */
3130 /* There is no location expression, but a constant value instead. */
3131 if (dwarf_whatattr (attr
) == DW_AT_const_value
)
3133 *tail
= c_translate_constant (pool
, &loc2c_error
, this,
3134 &loc2c_emit_address
, 0, pc
, attr
);
3141 /* PR9768: formerly, we added pc+module_bias here. However, that bias value
3142 is not present in the pc value by the time we get it, so adding it would
3143 result in false negatives of variable reachibility. In other instances
3144 further below, the c_translate_FOO functions, the module_bias value used
3145 to be passed in, but instead should now be zero for the same reason. */
3148 switch (dwarf_getlocation_addr (attr
, pc
/*+ module_bias*/, &expr
, &len
, 1))
3150 case 1: /* Should always happen. */
3155 case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */
3157 Dwarf_Addr pc2
= pr15123_retry_addr (pc
, die
);
3166 string msg
= _F("not accessible at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3167 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3168 err
.details
.push_back(die_location_as_string(pc
, die
));
3169 err
.details
.push_back(die_location_as_function_string(pc
, die
));
3170 err
.details
.push_back(suggested_locations_string(attr
));
3174 default: /* Shouldn't happen. */
3177 string msg
= _F("dwarf_getlocation_addr failed at this address (pc: %s) [man error::dwarf]", lex_cast_hex(pc
).c_str());
3178 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3179 string dwarf_err
= _F("dwarf_error: %s", dwarf_errmsg(-1));
3180 err
.details
.push_back(dwarf_err
);
3181 err
.details
.push_back(die_location_as_string(pc
, die
));
3182 err
.details
.push_back(die_location_as_function_string(pc
, die
));
3183 err
.details
.push_back(suggested_locations_string(attr
));
3189 // pc is in the dw address space of the current module, which is what
3190 // c_translate_location expects. get_cfa_ops wants the global dwfl address.
3191 // cfa_ops only make sense for locals.
3194 Dwarf_Addr addr
= pc
+ module_bias
;
3195 cfa_ops
= get_cfa_ops (addr
);
3200 return c_translate_location (pool
, &loc2c_error
, this,
3201 &loc2c_emit_address
,
3203 pc
, attr
, expr
, len
, tail
, fb_attr
, cfa_ops
);
3208 dwflpp::get_members(Dwarf_Die
*typedie
, set
<string
>& members
, set
<string
> &dupes
)
3210 const int typetag
= dwarf_tag (typedie
);
3212 /* compile and partial unit included for recursion through
3213 imported_unit below. */
3214 if (typetag
!= DW_TAG_structure_type
&&
3215 typetag
!= DW_TAG_class_type
&&
3216 typetag
!= DW_TAG_union_type
&&
3217 typetag
!= DW_TAG_compile_unit
&&
3218 typetag
!= DW_TAG_partial_unit
)
3220 throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union",
3221 dwarf_type_name(typedie
).c_str()));
3224 // Try to get the first child of vardie.
3225 Dwarf_Die die_mem
, import
;
3226 Dwarf_Die
*die
= &die_mem
;
3227 switch (dwarf_child (typedie
, die
))
3229 case 1: // No children.
3230 throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie
).c_str()));
3233 default: // Shouldn't happen.
3234 throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie
).c_str(),
3241 // Add each sibling's name to members set
3244 int tag
= dwarf_tag(die
);
3246 /* The children of an imported_unit should be treated as members too. */
3247 if (tag
== DW_TAG_imported_unit
3248 && dwarf_attr_die(die
, DW_AT_import
, &import
))
3249 get_members(&import
, members
, dupes
);
3251 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3254 const char *member
= dwarf_diename (die
) ;
3256 if ( tag
== DW_TAG_member
&& member
!= NULL
)
3258 // Only output if this is new, to avoid inheritance dupes.
3259 if (dupes
.insert(member
).second
)
3260 members
.insert(member
);
3265 if (!dwarf_attr_die (die
, DW_AT_type
, &temp_die
))
3267 string source
= dwarf_decl_file(die
) ?: "<unknown source>";
3269 dwarf_decl_line(die
, &line
);
3270 throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous "
3271 "member at %s:%d", source
.c_str(), line
));
3274 get_members(&temp_die
, members
, dupes
);
3278 while (dwarf_siblingof (die
, die
) == 0);
3283 dwflpp::find_struct_member(const target_symbol::component
& c
,
3284 Dwarf_Die
*parentdie
,
3285 Dwarf_Die
*memberdie
,
3286 vector
<Dwarf_Die
>& dies
,
3287 vector
<Dwarf_Attribute
>& locs
)
3289 Dwarf_Attribute attr
;
3292 /* With inheritance, a subclass may mask member names of parent classes, so
3293 * our search among the inheritance tree must be breadth-first rather than
3294 * depth-first (recursive). The parentdie is still our starting point. */
3295 deque
<Dwarf_Die
> inheritees(1, *parentdie
);
3296 for (; !inheritees
.empty(); inheritees
.pop_front())
3298 switch (dwarf_child (&inheritees
.front(), &die
))
3300 case 0: /* First child found. */
3302 case 1: /* No children. */
3304 case -1: /* Error. */
3305 default: /* Shouldn't happen */
3306 throw SEMANTIC_ERROR (dwarf_type_name(&inheritees
.front()) + ": "
3307 + string (dwarf_errmsg (-1)),
3313 int tag
= dwarf_tag(&die
);
3314 /* recurse into imported units as if they are anonymoust structs */
3316 if (tag
== DW_TAG_imported_unit
3317 && dwarf_attr_die(&die
, DW_AT_import
, &import
)
3318 && find_struct_member(c
, &import
, memberdie
, dies
, locs
))
3321 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
3324 const char *name
= dwarf_diename(&die
);
3325 if (tag
== DW_TAG_inheritance
)
3327 /* Remember inheritee for breadth-first search. */
3328 Dwarf_Die inheritee
;
3329 if (dwarf_attr_die (&die
, DW_AT_type
, &inheritee
))
3330 inheritees
.push_back(inheritee
);
3332 else if (name
== NULL
)
3334 /* Need to recurse for anonymous structs/unions. */
3336 if (dwarf_attr_die (&die
, DW_AT_type
, &subdie
) &&
3337 find_struct_member(c
, &subdie
, memberdie
, dies
, locs
))
3340 else if (name
== c
.member
)
3346 while (dwarf_siblingof (&die
, &die
) == 0);
3352 /* As we unwind the recursion, we need to build the chain of
3353 * locations that got to the final answer. */
3354 if (dwarf_attr_integrate (&die
, DW_AT_data_member_location
, &attr
))
3356 dies
.insert(dies
.begin(), die
);
3357 locs
.insert(locs
.begin(), attr
);
3360 /* Union members don't usually have a location,
3361 * but just use the containing union's location. */
3362 else if (dwarf_tag(parentdie
) != DW_TAG_union_type
)
3363 throw SEMANTIC_ERROR (_F("no location for field '%s':%s",
3364 c
.member
.c_str(), dwarf_errmsg(-1)), c
.tok
);
3371 dwarf_die_type (Dwarf_Die
*die
, Dwarf_Die
*typedie_mem
, const token
*tok
=NULL
)
3373 if (!dwarf_attr_die (die
, DW_AT_type
, typedie_mem
))
3374 throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok
);
3379 dwflpp::translate_components(struct obstack
*pool
,
3380 struct location
**tail
,
3382 const target_symbol
*e
,
3388 while (i
< e
->components
.size())
3390 const target_symbol::component
& c
= e
->components
[i
];
3392 /* XXX: This would be desirable, but we don't get the target_symbol token,
3393 and printing that gives us the file:line number too early anyway. */
3395 // Emit a marker to note which field is being access-attempted, to give
3396 // better error messages if deref() fails.
3397 string piece
= string(...target_symbol token
...) + string ("#") + lex_cast(components
[i
].second
);
3398 obstack_printf (pool
, "c->last_stmt = %s;", lex_cast_qstring(piece
).c_str());
3401 switch (dwarf_tag (typedie
))
3403 case DW_TAG_typedef
:
3404 case DW_TAG_const_type
:
3405 case DW_TAG_volatile_type
:
3406 case DW_TAG_restrict_type
:
3407 /* Just iterate on the referent type. */
3408 dwarf_die_type (typedie
, typedie
, c
.tok
);
3411 case DW_TAG_reference_type
:
3412 case DW_TAG_rvalue_reference_type
:
3414 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3415 dwarf_die_type (typedie
, typedie
, c
.tok
);
3418 case DW_TAG_pointer_type
:
3419 /* A pointer with no type is a void* -- can't dereference it. */
3420 if (!dwarf_hasattr_integrate (typedie
, DW_AT_type
))
3421 throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c
).c_str(),
3422 dwarf_type_name(typedie
).c_str()), c
.tok
);
3425 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3426 if (c
.type
!= target_symbol::comp_literal_array_index
&&
3427 c
.type
!= target_symbol::comp_expression_array_index
)
3429 dwarf_die_type (typedie
, typedie
, c
.tok
);
3432 /* else fall through as an array access */
3434 case DW_TAG_array_type
:
3435 if (c
.type
== target_symbol::comp_literal_array_index
)
3438 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3441 else if (c
.type
== target_symbol::comp_expression_array_index
)
3443 string index
= "STAP_ARG_index" + lex_cast(i
);
3445 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3449 throw SEMANTIC_ERROR (_F("invalid access '%s' for array type",
3450 lex_cast(c
).c_str()), c
.tok
);
3452 dwarf_die_type (typedie
, typedie
, c
.tok
);
3457 case DW_TAG_structure_type
:
3458 case DW_TAG_union_type
:
3459 case DW_TAG_class_type
:
3460 if (c
.type
!= target_symbol::comp_struct_member
)
3461 throw SEMANTIC_ERROR (_F("invalid access '%s' for %s",
3462 lex_cast(c
).c_str(), dwarf_type_name(typedie
).c_str()));
3464 if (dwarf_hasattr(typedie
, DW_AT_declaration
))
3466 Dwarf_Die
*tmpdie
= declaration_resolve(typedie
);
3468 throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie
).c_str()), c
.tok
);
3473 vector
<Dwarf_Die
> dies
;
3474 vector
<Dwarf_Attribute
> locs
;
3475 if (!find_struct_member(c
, typedie
, vardie
, dies
, locs
))
3477 /* Add a file:line hint for anonymous types */
3479 if (!dwarf_hasattr_integrate(typedie
, DW_AT_name
))
3482 const char *file
= dwarf_decl_file(typedie
);
3483 if (file
&& dwarf_decl_line(typedie
, &line
) == 0)
3484 source
= " (" + string(file
) + ":"
3485 + lex_cast(line
) + ")";
3488 set
<string
> members
, member_dupes
;
3489 get_members(typedie
, members
, member_dupes
);
3490 string sugs
= levenshtein_suggest(c
.member
, members
);
3492 sugs
= " (alternatives: " + sugs
+ ")";
3493 throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c
.member
.c_str(),
3494 dwarf_type_name(typedie
).c_str(), source
.c_str(),
3495 sugs
.c_str()), c
.tok
);
3498 for (unsigned j
= 0; j
< locs
.size(); ++j
)
3500 translate_location (pool
, &locs
[j
], &dies
[j
],
3504 dwarf_die_type (vardie
, typedie
, c
.tok
);
3508 case DW_TAG_enumeration_type
:
3509 case DW_TAG_base_type
:
3510 throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c
).c_str(),
3511 dwarf_type_name(typedie
).c_str()), c
.tok
);
3515 throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c
.tok
);
3519 throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie
).c_str(),
3520 lex_cast(dwarf_tag(typedie
)).c_str()), c
.tok
);
3528 dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die
*typedie
,
3529 Dwarf_Die
*innerdie
,
3530 const target_symbol
*e
)
3532 int typetag
= dwarf_tag (typedie
);
3533 *innerdie
= *typedie
;
3534 while (typetag
== DW_TAG_typedef
||
3535 typetag
== DW_TAG_const_type
||
3536 typetag
== DW_TAG_volatile_type
||
3537 typetag
== DW_TAG_restrict_type
)
3539 if (!dwarf_attr_die (innerdie
, DW_AT_type
, innerdie
))
3540 throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e
->tok
);
3541 typetag
= dwarf_tag (innerdie
);
3547 dwflpp::translate_final_fetch_or_store (struct obstack
*pool
,
3548 struct location
**tail
,
3549 Dwarf_Addr
/*module_bias*/,
3551 Dwarf_Die
*start_typedie
,
3553 const target_symbol
*e
,
3558 /* First boil away any qualifiers associated with the type DIE of
3559 the final location to be accessed. */
3561 resolve_unqualified_inner_typedie (start_typedie
, typedie
, e
);
3563 /* If we're looking for an address, then we can just provide what
3564 we computed to this point, without using a fetch/store. */
3568 throw SEMANTIC_ERROR (_("cannot write to member address"), e
->tok
);
3570 if (dwarf_hasattr_integrate (vardie
, DW_AT_bit_offset
))
3571 throw SEMANTIC_ERROR (_("cannot take address of bit-field"), e
->tok
);
3573 c_translate_addressof (pool
, 1, 0, vardie
, typedie
, tail
, "STAP_RETVALUE");
3577 /* Then switch behavior depending on the type of fetch/store we
3578 want, and the type and pointer-ness of the final location. */
3580 int typetag
= dwarf_tag (typedie
);
3584 throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s", lex_cast(typetag
).c_str(),
3585 dwarf_type_name(typedie
).c_str()), e
->tok
);
3588 case DW_TAG_structure_type
:
3589 case DW_TAG_class_type
:
3590 case DW_TAG_union_type
:
3591 throw SEMANTIC_ERROR (_F("'%s' is being accessed instead of a member",
3592 dwarf_type_name(typedie
).c_str()), e
->tok
);
3595 case DW_TAG_base_type
:
3597 // Reject types we can't handle in systemtap
3599 Dwarf_Attribute encoding_attr
;
3600 Dwarf_Word encoding
= (Dwarf_Word
) -1;
3601 dwarf_formudata (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &encoding_attr
),
3603 if (encoding
== (Dwarf_Word
) -1)
3605 // clog << "bad type1 " << encoding << " diestr" << endl;
3606 throw SEMANTIC_ERROR (_F("unsupported type (mystery encoding %s for %s", lex_cast(encoding
).c_str(),
3607 dwarf_type_name(typedie
).c_str()), e
->tok
);
3610 if (encoding
== DW_ATE_float
3611 || encoding
== DW_ATE_complex_float
3612 /* XXX || many others? */)
3614 // clog << "bad type " << encoding << " diestr" << endl;
3615 throw SEMANTIC_ERROR (_F("unsupported type (encoding %s) for %s", lex_cast(encoding
).c_str(),
3616 dwarf_type_name(typedie
).c_str()), e
->tok
);
3619 // Fallthrough. enumeration_types are always scalar.
3620 case DW_TAG_enumeration_type
:
3623 c_translate_store (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3626 c_translate_fetch (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3630 case DW_TAG_array_type
:
3631 case DW_TAG_pointer_type
:
3632 case DW_TAG_reference_type
:
3633 case DW_TAG_rvalue_reference_type
:
3637 if (typetag
== DW_TAG_array_type
)
3638 throw SEMANTIC_ERROR (_("cannot write to array address"), e
->tok
);
3639 if (typetag
== DW_TAG_reference_type
||
3640 typetag
== DW_TAG_rvalue_reference_type
)
3641 throw SEMANTIC_ERROR (_("cannot write to reference"), e
->tok
);
3642 assert (typetag
== DW_TAG_pointer_type
);
3643 c_translate_pointer_store (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3648 // We have the pointer: cast it to an integral type via &(*(...))
3650 // NB: per bug #1187, at one point char*-like types were
3651 // automagically converted here to systemtap string values.
3652 // For several reasons, this was taken back out, leaving
3653 // pointer-to-string "conversion" (copying) to tapset functions.
3655 if (typetag
== DW_TAG_array_type
)
3656 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
, NULL
, 0);
3658 c_translate_pointer (pool
, 1, 0 /* PR9768 */, typedie
, tail
);
3659 c_translate_addressof (pool
, 1, 0 /* PR9768 */, NULL
, NULL
, tail
,
3666 postlude
+= " STAP_RETVALUE = STAP_ARG_value;\n";
3671 dwflpp::express_as_string (string prelude
,
3673 struct location
*head
)
3676 char *buf
= 0; // NB: it would leak to pre-allocate a buffer here
3677 FILE *memstream
= open_memstream (&buf
, &bufsz
);
3680 fprintf(memstream
, "{\n");
3681 fprintf(memstream
, "%s", prelude
.c_str());
3683 unsigned int stack_depth
;
3684 bool deref
= c_emit_location (memstream
, head
, 1, &stack_depth
);
3686 // Ensure that DWARF keeps loc2c to a "reasonable" stack size
3687 // 32 intptr_t leads to max 256 bytes on the stack
3688 if (stack_depth
> 32)
3689 throw SEMANTIC_ERROR("oversized DWARF stack");
3691 fprintf(memstream
, "%s", postlude
.c_str());
3692 fprintf(memstream
, " goto out;\n");
3694 // dummy use of deref_fault label, to disable warning if deref() not used
3695 fprintf(memstream
, "if (0) goto deref_fault;\n");
3697 // XXX: deref flag not reliable; emit fault label unconditionally
3702 fprintf(memstream
, "}\n");
3711 dwflpp::vardie_from_symtable (Dwarf_Die
*vardie
, Dwarf_Addr
*addr
)
3713 const char *name
= dwarf_linkage_name (vardie
) ?: dwarf_diename (vardie
);
3715 if (sess
.verbose
> 2)
3716 clog
<< _F("finding symtable address for %s\n", name
);
3719 int syms
= dwfl_module_getsymtab (module
);
3720 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
3722 for (int i
= 0; *addr
== 0 && i
< syms
; i
++)
3726 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
3728 && ! strcmp (name
, symname
)
3729 && sym
.st_shndx
!= SHN_UNDEF
3730 && (GELF_ST_TYPE (sym
.st_info
) == STT_NOTYPE
// PR13284
3731 || GELF_ST_TYPE (sym
.st_info
) == STT_OBJECT
))
3732 *addr
= sym
.st_value
;
3735 // Don't relocate for the kernel, or kernel modules we handle those
3736 // specially in emit_address.
3737 if (dwfl_module_relocations (module
) == 1 && module_name
!= TOK_KERNEL
)
3738 dwfl_module_relocate_address (module
, addr
);
3740 if (sess
.verbose
> 2)
3741 clog
<< _F("found %s @%#" PRIx64
"\n", name
, *addr
);
3747 dwflpp::literal_stmt_for_local (vector
<Dwarf_Die
>& scopes
,
3749 string
const & local
,
3750 const target_symbol
*e
,
3755 Dwarf_Attribute fb_attr_mem
, *fb_attr
= NULL
;
3757 fb_attr
= find_variable_and_frame_base (scopes
, pc
, local
, e
,
3758 &vardie
, &fb_attr_mem
);
3763 clog
<< _F("finding location for local '%s' near address %#" PRIx64
3764 ", module bias %#" PRIx64
"\n", local
.c_str(), pc
,
3767 clog
<< _F("finding location for global '%s' in CU '%s'\n",
3768 local
.c_str(), cu_name().c_str());
3772 #define obstack_chunk_alloc malloc
3773 #define obstack_chunk_free free
3775 struct obstack pool
;
3776 obstack_init (&pool
);
3777 struct location
*tail
= NULL
;
3779 /* Given $foo->bar->baz[NN], translate the location of foo. */
3781 struct location
*head
;
3783 Dwarf_Attribute attr_mem
;
3784 if (dwarf_attr_integrate (&vardie
, DW_AT_const_value
, &attr_mem
) == NULL
3785 && dwarf_attr_integrate (&vardie
, DW_AT_location
, &attr_mem
) == NULL
)
3788 memset(&addr_loc
, 0, sizeof(Dwarf_Op
));
3789 addr_loc
.atom
= DW_OP_addr
;
3790 // If it is an external variable try the symbol table. PR10622.
3791 if (dwarf_attr_integrate (&vardie
, DW_AT_external
, &attr_mem
) != NULL
3792 && vardie_from_symtable (&vardie
, &addr_loc
.number
) != 0)
3794 head
= c_translate_location (&pool
, &loc2c_error
, this,
3795 &loc2c_emit_address
,
3797 NULL
, &addr_loc
, 1, &tail
, NULL
, NULL
);
3801 string msg
= _F("failed to retrieve location attribute for '%s' [man error::dwarf]", local
.c_str());
3802 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3803 err
.details
.push_back(die_location_as_string(pc
, &vardie
));
3804 err
.details
.push_back(die_location_as_function_string(pc
, &vardie
));
3809 head
= translate_location (&pool
, &attr_mem
, &vardie
, pc
, fb_attr
, &tail
, e
);
3811 /* Translate the ->bar->baz[NN] parts. */
3814 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
3816 string msg
= _F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str());
3817 semantic_error
err(ERR_SRC
, msg
, e
->tok
);
3818 err
.details
.push_back(die_location_as_string(pc
, &vardie
));
3819 err
.details
.push_back(die_location_as_function_string(pc
, &vardie
));
3823 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
3825 /* Translate the assignment part, either
3826 x = $foo->bar->baz[NN]
3828 $foo->bar->baz[NN] = x
3831 string prelude
, postlude
;
3832 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
3833 &vardie
, &typedie
, lvalue
, e
,
3834 prelude
, postlude
, die_mem
);
3836 /* Write the translation to a string. */
3837 string result
= express_as_string(prelude
, postlude
, head
);
3838 obstack_free (&pool
, 0);
3843 dwflpp::type_die_for_local (vector
<Dwarf_Die
>& scopes
,
3845 string
const & local
,
3846 const target_symbol
*e
,
3850 Dwarf_Attribute attr_mem
;
3852 find_variable_and_frame_base (scopes
, pc
, local
, e
, &vardie
, &attr_mem
);
3854 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
3855 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str()), e
->tok
);
3857 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
3863 dwflpp::literal_stmt_for_return (Dwarf_Die
*scope_die
,
3865 const target_symbol
*e
,
3870 clog
<< _F("literal_stmt_for_return: finding return value for %s (%s)\n",
3871 (dwarf_diename(scope_die
) ?: "<unknown>"), (dwarf_diename(cu
) ?: "<unknown>"));
3873 struct obstack pool
;
3874 obstack_init (&pool
);
3875 struct location
*tail
= NULL
;
3877 /* Given $return->bar->baz[NN], translate the location of return. */
3878 const Dwarf_Op
*locops
;
3879 int nlocops
= dwfl_module_return_value_location (module
, scope_die
,
3883 throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)",
3884 (dwarf_diename(scope_die
) ?: "<unknown>"),
3885 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3887 // the function has no return value (e.g. "void" in C)
3888 else if (nlocops
== 0)
3890 throw SEMANTIC_ERROR(_F("function %s (%s) has no return value",
3891 (dwarf_diename(scope_die
) ?: "<unknown>"),
3892 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3895 struct location
*head
= c_translate_location (&pool
, &loc2c_error
, this,
3896 &loc2c_emit_address
,
3898 pc
, NULL
, locops
, nlocops
,
3901 /* Translate the ->bar->baz[NN] parts. */
3903 Dwarf_Die vardie
= *scope_die
, typedie
;
3904 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
3905 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
3906 (dwarf_diename(&vardie
) ?: "<unknown>"),
3907 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3909 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
3911 /* Translate the assignment part, either
3912 x = $return->bar->baz[NN]
3914 $return->bar->baz[NN] = x
3917 string prelude
, postlude
;
3918 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
3919 &vardie
, &typedie
, lvalue
, e
,
3920 prelude
, postlude
, die_mem
);
3922 /* Write the translation to a string. */
3923 string result
= express_as_string(prelude
, postlude
, head
);
3924 obstack_free (&pool
, 0);
3929 dwflpp::type_die_for_return (Dwarf_Die
*scope_die
,
3931 const target_symbol
*e
,
3934 Dwarf_Die vardie
= *scope_die
;
3935 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
3936 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
3937 (dwarf_diename(&vardie
) ?: "<unknown>"),
3938 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3940 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
3946 dwflpp::literal_stmt_for_pointer (Dwarf_Die
*start_typedie
,
3947 const target_symbol
*e
,
3952 clog
<< _F("literal_stmt_for_pointer: finding value for %s (%s)\n",
3953 dwarf_type_name(start_typedie
).c_str(), (dwarf_diename(cu
) ?: "<unknown>"));
3955 struct obstack pool
;
3956 obstack_init (&pool
);
3957 struct location
*head
= c_translate_argument (&pool
, &loc2c_error
, this,
3958 &loc2c_emit_address
,
3959 1, "STAP_ARG_pointer");
3960 struct location
*tail
= head
;
3962 /* Translate the ->bar->baz[NN] parts. */
3965 Dwarf_Die typedie
= *start_typedie
, vardie
= typedie
;
3967 /* As a special case when typedie is not an array or pointer, we can
3968 * allow array indexing on STAP_ARG_pointer instead (since we do
3969 * know the pointee type and can determine its size). PR11556. */
3970 const target_symbol::component
* c
=
3971 e
->components
.empty() ? NULL
: &e
->components
[0];
3972 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
3973 c
->type
== target_symbol::comp_expression_array_index
))
3975 resolve_unqualified_inner_typedie (&typedie
, &typedie
, e
);
3976 int typetag
= dwarf_tag (&typedie
);
3977 if (typetag
!= DW_TAG_pointer_type
&&
3978 typetag
!= DW_TAG_array_type
)
3980 if (c
->type
== target_symbol::comp_literal_array_index
)
3981 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, NULL
, c
->num_index
);
3983 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, "STAP_ARG_index0", 0);
3988 /* Now translate the rest normally. */
3990 translate_components (&pool
, &tail
, 0, e
, &vardie
, &typedie
, first
);
3992 /* Translate the assignment part, either
3993 x = (STAP_ARG_pointer)->bar->baz[NN]
3995 (STAP_ARG_pointer)->bar->baz[NN] = x
3998 string prelude
, postlude
;
3999 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
4000 &vardie
, &typedie
, lvalue
, e
,
4001 prelude
, postlude
, die_mem
);
4003 /* Write the translation to a string. */
4004 string result
= express_as_string(prelude
, postlude
, head
);
4005 obstack_free (&pool
, 0);
4010 dwflpp::type_die_for_pointer (Dwarf_Die
*start_typedie
,
4011 const target_symbol
*e
,
4015 *typedie
= *start_typedie
;
4016 Dwarf_Die vardie
= *typedie
;
4018 /* Handle the same PR11556 case as above. */
4019 const target_symbol::component
* c
=
4020 e
->components
.empty() ? NULL
: &e
->components
[0];
4021 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
4022 c
->type
== target_symbol::comp_expression_array_index
))
4024 resolve_unqualified_inner_typedie (typedie
, typedie
, e
);
4025 int typetag
= dwarf_tag (typedie
);
4026 if (typetag
!= DW_TAG_pointer_type
&&
4027 typetag
!= DW_TAG_array_type
)
4031 translate_components (NULL
, NULL
, 0, e
, &vardie
, typedie
, first
);
4037 in_kprobes_function(systemtap_session
& sess
, Dwarf_Addr addr
)
4039 if (sess
.sym_kprobes_text_start
!= 0 && sess
.sym_kprobes_text_end
!= 0)
4041 // If the probe point address is anywhere in the __kprobes
4042 // address range, we can't use this probe point.
4043 if (addr
>= sess
.sym_kprobes_text_start
&& addr
< sess
.sym_kprobes_text_end
)
4050 enum dwflpp::blacklisted_type
4051 dwflpp::blacklisted_p(const string
& funcname
,
4052 const string
& filename
,
4054 const string
& module
,
4058 if (!blacklist_enabled
)
4059 return dwflpp::blacklisted_none
;
4061 enum dwflpp::blacklisted_type blacklisted
= dwflpp::blacklisted_none
;
4063 // check against section blacklist
4064 string section
= get_blacklist_section(addr
);
4066 // PR6503: modules don't need special init/exit treatment
4067 if (module
== TOK_KERNEL
&& !regexec (&blacklist_section
, section
.c_str(), 0, NULL
, 0))
4068 blacklisted
= dwflpp::blacklisted_section
;
4070 // Check for function marked '__kprobes'.
4071 else if (module
== TOK_KERNEL
&& in_kprobes_function(sess
, addr
))
4072 blacklisted
= dwflpp::blacklisted_kprobes
;
4074 // Check probe point against function blacklist
4075 else if (!regexec(&blacklist_func
, funcname
.c_str(), 0, NULL
, 0))
4076 blacklisted
= dwflpp::blacklisted_function
;
4078 // Check probe point against function return blacklist
4079 else if (has_return
&& !regexec(&blacklist_func_ret
, funcname
.c_str(), 0, NULL
, 0))
4080 blacklisted
= dwflpp::blacklisted_function_return
;
4082 // Check probe point against file blacklist
4083 else if (!regexec(&blacklist_file
, filename
.c_str(), 0, NULL
, 0))
4084 blacklisted
= dwflpp::blacklisted_file
;
4089 clog
<< _(" - blacklisted");
4092 blacklisted
= dwflpp::blacklisted_none
;
4094 clog
<< _(" but not skipped (guru mode enabled)");
4098 // This probe point is not blacklisted.
4104 dwflpp::build_kernel_blacklist()
4106 // We build up the regexps in these strings
4108 // Add ^ anchors at the front; $ will be added just before regcomp.
4111 string blfn_ret
= "^(";
4112 string blfile
= "^(";
4113 string blsection
= "^(";
4115 blsection
+= "\\.init\\."; // first alternative, no "|"
4116 blsection
+= "|\\.exit\\.";
4117 blsection
+= "|\\.devinit\\.";
4118 blsection
+= "|\\.devexit\\.";
4119 blsection
+= "|\\.cpuinit\\.";
4120 blsection
+= "|\\.cpuexit\\.";
4121 blsection
+= "|\\.meminit\\.";
4122 blsection
+= "|\\.memexit\\.";
4124 /* NOTE all include/asm .h blfile patterns might need "full path"
4125 so prefix those with '.*' - see PR13108 and PR13112. */
4126 blfile
+= "kernel/kprobes\\.c"; // first alternative, no "|"
4127 blfile
+= "|arch/.*/kernel/kprobes\\.c";
4128 blfile
+= "|.*/include/asm/io\\.h";
4129 blfile
+= "|.*/include/asm/io_64\\.h";
4130 blfile
+= "|.*/include/asm/bitops\\.h";
4131 blfile
+= "|drivers/ide/ide-iops\\.c";
4133 blfile
+= "|arch/.*/kernel/paravirt\\.c";
4134 blfile
+= "|.*/include/asm/paravirt\\.h";
4136 // XXX: it would be nice if these blacklisted functions were pulled
4137 // in dynamically, instead of being statically defined here.
4138 // Perhaps it could be populated from script files. A "noprobe
4139 // kernel.function("...")" construct might do the trick.
4141 // Most of these are marked __kprobes in newer kernels. We list
4142 // them here (anyway) so the translator can block them on older
4143 // kernels that don't have the __kprobes function decorator. This
4144 // also allows detection of problems at translate- rather than
4147 blfn
+= "atomic_notifier_call_chain"; // first blfn; no "|"
4148 blfn
+= "|default_do_nmi";
4151 blfn
+= "|do_debug";
4152 blfn
+= "|do_general_protection";
4155 blfn
+= "|do_page_fault";
4156 blfn
+= "|do_sparc64_fault";
4158 blfn
+= "|dummy_nmi_callback";
4159 blfn
+= "|flush_icache_range";
4160 blfn
+= "|ia64_bad_break";
4161 blfn
+= "|ia64_do_page_fault";
4162 blfn
+= "|ia64_fault";
4163 blfn
+= "|io_check_error";
4164 blfn
+= "|mem_parity_error";
4165 blfn
+= "|nmi_watchdog_tick";
4166 blfn
+= "|notifier_call_chain";
4167 blfn
+= "|oops_begin";
4168 blfn
+= "|oops_end";
4169 blfn
+= "|program_check_exception";
4170 blfn
+= "|single_step_exception";
4171 blfn
+= "|sync_regs";
4172 blfn
+= "|unhandled_fault";
4173 blfn
+= "|unknown_nmi_error";
4174 blfn
+= "|xen_[gs]et_debugreg";
4175 blfn
+= "|xen_irq_.*";
4176 blfn
+= "|xen_.*_fl_direct.*";
4177 blfn
+= "|check_events";
4178 blfn
+= "|xen_adjust_exception_frame";
4179 blfn
+= "|xen_iret.*";
4180 blfn
+= "|xen_sysret64.*";
4181 blfn
+= "|test_ti_thread_flag.*";
4182 blfn
+= "|inat_get_opcode_attribute";
4183 blfn
+= "|system_call_after_swapgs";
4184 blfn
+= "|HYPERVISOR_[gs]et_debugreg";
4185 blfn
+= "|HYPERVISOR_event_channel_op";
4187 blfn
+= "|hash_ptr";
4188 blfn
+= "|native_set_pte";
4191 blfn
+= "|.*raw_.*_lock.*";
4192 blfn
+= "|.*raw_.*_unlock.*";
4193 blfn
+= "|.*raw_.*_trylock.*";
4194 blfn
+= "|.*read_lock.*";
4195 blfn
+= "|.*read_unlock.*";
4196 blfn
+= "|.*read_trylock.*";
4197 blfn
+= "|.*write_lock.*";
4198 blfn
+= "|.*write_unlock.*";
4199 blfn
+= "|.*write_trylock.*";
4200 blfn
+= "|.*write_seqlock.*";
4201 blfn
+= "|.*write_sequnlock.*";
4202 blfn
+= "|.*spin_lock.*";
4203 blfn
+= "|.*spin_unlock.*";
4204 blfn
+= "|.*spin_trylock.*";
4205 blfn
+= "|.*spin_is_locked.*";
4206 blfn
+= "|rwsem_.*lock.*";
4207 blfn
+= "|.*mutex_.*lock.*";
4211 blfn
+= "|atomic_.*";
4212 blfn
+= "|atomic64_.*";
4214 // few other problematic cases
4219 blfn
+= "|.*apic.*|.*APIC.*";
4220 blfn
+= "|.*softirq.*";
4222 blfn
+= "|.*_intr.*";
4224 blfn
+= "|.*kernel_text.*";
4225 blfn
+= "|get_current";
4226 blfn
+= "|current_.*";
4227 blfn
+= "|.*exception_tables.*";
4228 blfn
+= "|.*setup_rt_frame.*";
4230 // PR 5759, CONFIG_PREEMPT kernels
4231 blfn
+= "|.*preempt_count.*";
4232 blfn
+= "|preempt_schedule";
4234 // These functions don't return, so return probes would never be recovered
4235 blfn_ret
+= "do_exit"; // no "|"
4236 blfn_ret
+= "|sys_exit";
4237 blfn_ret
+= "|sys_exit_group";
4239 // __switch_to changes "current" on x86_64 and i686, so return probes
4240 // would cause kernel panic, and it is marked as "__kprobes" on x86_64
4241 if (sess
.architecture
== "x86_64")
4242 blfn
+= "|__switch_to";
4243 if (sess
.architecture
== "i686")
4244 blfn_ret
+= "|__switch_to";
4246 // RHEL6 pre-beta 2.6.32-19.el6
4247 blfn
+= "|special_mapping_.*";
4248 blfn
+= "|.*_pte_.*"; // or "|smaps_pte_range";
4249 blfile
+= "|fs/seq_file\\.c";
4254 blsection
+= ")"; // NB: no $, sections match just the beginning
4256 if (sess
.verbose
> 2)
4258 clog
<< _("blacklist regexps:") << endl
;
4259 clog
<< "blfn: " << blfn
<< endl
;
4260 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4261 clog
<< "blfile: " << blfile
<< endl
;
4262 clog
<< "blsection: " << blsection
<< endl
;
4265 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4266 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4267 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4268 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4269 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4270 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4271 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4272 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4274 blacklist_enabled
= true;
4279 dwflpp::build_user_blacklist()
4281 // We build up the regexps in these strings
4283 // Add ^ anchors at the front; $ will be added just before regcomp.
4286 string blfn_ret
= "^(";
4287 string blfile
= "^(";
4288 string blsection
= "^(";
4290 // Non-matching placeholders until we have real things to match
4295 // These functions don't use the normal function-entry ABI, so can't be .return probed safely
4296 blfn_ret
+= "_start";
4301 blsection
+= ")"; // NB: no $, sections match just the beginning
4303 if (sess
.verbose
> 2)
4305 clog
<< _("blacklist regexps:") << endl
;
4306 clog
<< "blfn: " << blfn
<< endl
;
4307 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
4308 clog
<< "blfile: " << blfile
<< endl
;
4309 clog
<< "blsection: " << blsection
<< endl
;
4312 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4313 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
4314 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4315 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
4316 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4317 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
4318 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
4319 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
4321 blacklist_enabled
= true;
4326 dwflpp::get_blacklist_section(Dwarf_Addr addr
)
4328 string blacklist_section
;
4330 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
4331 // because dwfl_module_getelf can force costly section relocations
4332 // we don't really need, while either will do for this purpose.
4333 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (module
, &bias
))
4334 ?: dwfl_module_getelf (module
, &bias
));
4336 Dwarf_Addr offset
= addr
- bias
;
4341 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4342 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
4345 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
4347 continue; // XXX error?
4349 if (!(shdr
->sh_flags
& SHF_ALLOC
))
4352 GElf_Addr start
= shdr
->sh_addr
;
4353 GElf_Addr end
= start
+ shdr
->sh_size
;
4354 if (! (offset
>= start
&& offset
< end
))
4357 blacklist_section
= elf_strptr (elf
, shstrndx
, shdr
->sh_name
);
4361 return blacklist_section
;
4365 /* Find the section named 'section_name' in the current module
4366 * returning the section header using 'shdr_mem' */
4369 dwflpp::get_section(string section_name
, GElf_Shdr
*shdr_mem
, Elf
**elf_ret
)
4371 GElf_Shdr
*shdr
= NULL
;
4376 // Explicitly look in the main elf file first.
4377 elf
= dwfl_module_getelf (module
, &bias
);
4378 Elf_Scn
*probe_scn
= NULL
;
4380 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4382 bool have_section
= false;
4384 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4386 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4387 assert (shdr
!= NULL
);
4389 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4391 have_section
= true;
4396 // Older versions may put the section in the debuginfo dwarf file,
4397 // so check if it actually exists, if not take a look in the debuginfo file
4398 if (! have_section
|| (have_section
&& shdr
->sh_type
== SHT_NOBITS
))
4400 elf
= dwarf_getelf (dwfl_module_getdwarf (module
, &bias
));
4403 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4405 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4407 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4408 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4410 have_section
= true;
4426 dwflpp::relocate_address(Dwarf_Addr dw_addr
, string
& reloc_section
)
4429 // libdw address, so adjust for bias gotten from dwfl_module_getdwarf
4430 Dwarf_Addr reloc_addr
= dw_addr
+ module_bias
;
4433 assert(module_name
== TOK_KERNEL
);
4436 else if (dwfl_module_relocations (module
) > 0)
4438 // This is a relocatable module; libdwfl already knows its
4439 // sections, so we can relativize addr.
4440 int idx
= dwfl_module_relocate_address (module
, &reloc_addr
);
4441 const char* r_s
= dwfl_module_relocation_info (module
, idx
, NULL
);
4443 reloc_section
= r_s
;
4445 if (reloc_section
== "" && dwfl_module_relocations (module
) == 1)
4446 reloc_section
= ".dynamic";
4449 reloc_section
= ".absolute";
4453 /* Returns the call frame address operations for the given program counter
4454 * in the libdw address space.
4457 dwflpp::get_cfa_ops (Dwarf_Addr pc
)
4459 Dwarf_Op
*cfa_ops
= NULL
;
4461 if (sess
.verbose
> 2)
4462 clog
<< "get_cfa_ops @0x" << hex
<< pc
<< dec
4463 << ", module_start @0x" << hex
<< module_start
<< dec
<< endl
;
4465 // Try debug_frame first, then fall back on eh_frame.
4466 size_t cfa_nops
= 0;
4467 Dwarf_Addr bias
= 0;
4468 Dwarf_Frame
*frame
= NULL
;
4469 Dwarf_CFI
*cfi
= dwfl_module_dwarf_cfi (module
, &bias
);
4472 if (sess
.verbose
> 3)
4473 clog
<< "got dwarf cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4474 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4475 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4476 else if (sess
.verbose
> 3)
4477 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4479 else if (sess
.verbose
> 3)
4480 clog
<< "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl
;
4482 if (cfa_ops
== NULL
)
4484 cfi
= dwfl_module_eh_cfi (module
, &bias
);
4487 if (sess
.verbose
> 3)
4488 clog
<< "got eh cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4489 Dwarf_Frame
*frame
= NULL
;
4490 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4491 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4492 else if (sess
.verbose
> 3)
4493 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4495 else if (sess
.verbose
> 3)
4496 clog
<< "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl
;
4500 if (sess
.verbose
> 2)
4502 if (cfa_ops
== NULL
)
4503 clog
<< _("not found cfa") << endl
;
4506 Dwarf_Addr frame_start
, frame_end
;
4508 int info
= dwarf_frame_info (frame
, &frame_start
, &frame_end
,
4510 clog
<< _F("found cfa, info: %d [start: %#" PRIx64
", end: %#" PRIx64
4511 ", nops: %zu", info
, frame_start
, frame_end
, cfa_nops
) << endl
;
4519 dwflpp::add_module_build_id_to_hash (Dwfl_Module
*m
,
4520 void **userdata
__attribute__ ((unused
)),
4525 string modname
= name
;
4526 systemtap_session
* s
= (systemtap_session
*)arg
;
4527 if (pending_interrupts
)
4528 return DWARF_CB_ABORT
;
4530 // Extract the build ID
4531 const unsigned char *bits
;
4533 int bits_length
= dwfl_module_build_id(m
, &bits
, &vaddr
);
4536 // Convert the binary bits to a hex string
4537 string hex
= hex_dump(bits
, bits_length
);
4539 // Store the build ID in the session
4540 s
->build_ids
.push_back(hex
);
4548 // Perform PR15123 heuristic for given variable at given address.
4549 // Return alternate pc address to do location-list lookup at, or 0 if
4553 dwflpp::pr15123_retry_addr (Dwarf_Addr pc
, Dwarf_Die
* die
)
4555 // For PR15123, we'd like to detect the situation where the
4556 // incoming PC may point to a couple-of-byte instruction
4557 // sequence that gcc emits for CFLAGS=-mfentry, and where
4558 // context variables are in fact available throughout, *but* due
4559 // to the bug, the dwarf debuginfo location-list only starts a
4560 // few instructions later. Prologue searching does not resolve
4561 // this as a line-record is in place at the -mfentry prologue.
4563 // Detecting this is complicated because ...
4564 // - we only want to do this if -mfentry was actually used
4565 // - if <pc> points to the a function entry point
4566 // - if the architecture is familiar enough that we can have a
4567 // hard-coded constant to skip over the prologue.
4569 // Otherwise, we could give a false-positive - return corrupted
4572 // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches
4573 // was used. Without it, set the PR15123_ASSUME_MFENTRY environment
4574 // variable to override the -mfentry test.
4576 if (getenv ("PR15123_DISABLE"))
4579 if (!getenv ("PR15123_ASSUME_MFENTRY")) {
4581 string producer
, version
;
4582 dwarf_diecu (die
, &cudie
, NULL
, NULL
);
4583 if (!is_gcc_producer(&cudie
, producer
, version
))
4585 if (producer
.find("-mfentry") == string::npos
)
4589 // Determine if this pc maps to the beginning of a
4590 // real function (not some inlined doppelganger. This
4591 // is made tricker by this->function may not be
4592 // pointing at the right DIE (say e.g. stap encountered
4593 // the inlined copy first, so was focus_on_function'd).
4594 vector
<Dwarf_Die
> scopes
= getscopes(pc
);
4595 if (scopes
.size() == 0)
4598 Dwarf_Die outer_function_die
= scopes
[0];
4600 if (!die_entrypc(& outer_function_die
, &entrypc
) || entrypc
!= pc
)
4601 return 0; // (will fail on retry, so we won't loop more than once)
4603 if (sess
.architecture
== "i386" ||
4604 sess
.architecture
== "x86_64") {
4605 /* pull the trigger */
4606 if (sess
.verbose
> 2)
4607 clog
<< _("retrying variable location-list lookup at address pc+5\n");
4615 dwflpp::has_gnu_debugdata ()
4617 Dwarf_Addr load_addr
;
4618 // Note we really want the actual elf file, not the dwarf .debug file.
4619 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
4621 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
4623 // Get the gnu_debugdata section header
4624 Elf_Scn
*scn
= NULL
;
4625 GElf_Shdr
*gnu_debugdata_shdr
= NULL
;
4626 GElf_Shdr gnu_debugdata_shdr_mem
;
4627 while ((scn
= elf_nextscn (elf
, scn
)))
4629 gnu_debugdata_shdr
= gelf_getshdr (scn
, &gnu_debugdata_shdr_mem
);
4630 assert (gnu_debugdata_shdr
!= NULL
);
4631 if (strcmp (elf_strptr (elf
, shstrndx
, gnu_debugdata_shdr
->sh_name
), ".gnu_debugdata") == 0)
4637 // If not GCC, return false. Otherwise, return true and set vers.
4639 dwflpp::is_gcc_producer(Dwarf_Die
*cudie
, string
& producer
, string
& version
)
4641 Dwarf_Attribute producer_attr
;
4642 if (!dwarf_attr_integrate(cudie
, DW_AT_producer
, &producer_attr
))
4645 // GNU {C|C++|...} x.x.x YYYYMMDD ...
4646 const char *cproducer
= dwarf_formstring(&producer_attr
);
4649 producer
= cproducer
;
4651 vector
<string
> tokens
;
4652 tokenize(producer
, tokens
);
4654 if (tokens
.size() < 3
4655 || tokens
[0] != "GNU"
4656 || tokens
[2].find_first_not_of(".0123456789") != string::npos
)
4659 version
= tokens
[2];
4664 die_has_loclist(Dwarf_Die
*begin_die
)
4667 Dwarf_Attribute loc
;
4669 if (dwarf_child(begin_die
, &die
) != 0)
4674 switch (dwarf_tag(&die
))
4676 case DW_TAG_formal_parameter
:
4677 case DW_TAG_variable
:
4678 if (dwarf_attr_integrate(&die
, DW_AT_location
, &loc
)
4679 && dwarf_whatform(&loc
) == DW_FORM_sec_offset
)
4683 if (dwarf_haschildren (&die
))
4684 if (die_has_loclist(&die
))
4689 while (dwarf_siblingof (&die
, &die
) == 0);
4695 dwflpp::has_valid_locs ()
4699 // The current CU has valid location info (implying we do not need to skip the
4701 // - it was compiled with -O2 -g (in which case, GCC outputs proper location
4702 // info for the prologue), and
4703 // - it was compiled by GCC >= 4.5 (previous versions could have had invalid
4704 // debug info in the prologue, see GDB's PR13777)
4705 // Note that clang behaves similarly to GCC here: non-optimized code does not
4706 // have location lists, while optimized code does. In the check below, even if
4707 // the producer is not GCC, we assume that it is valid to do the loclist check
4708 // afterwards (which it is for clang).
4711 if (is_gcc_producer(cu
, prod
, vers
)
4712 && strverscmp(vers
.c_str(), "4.5") < 0)
4715 // We determine if the current CU has been optimized with -O2 -g by looking
4716 // for any data objects whose DW_AT_location is a location list. This is also
4717 // how GDB determines whether to skip the prologue or not. See GDB's PR12573
4718 // and also RHBZ612253#c6.
4719 if (!die_has_loclist(cu
))
4722 if (sess
.verbose
> 2)
4723 clog
<< _F("CU '%s' in module '%s' has valid locs",
4724 cu_name().c_str(), module_name
.c_str()) << endl
;
4729 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */