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
14 #include "elaborate.h"
16 #include "task_finder.h"
17 #include "translate.h"
21 #include "dwarf_wrappers.h"
22 #include "auto_free.h"
24 #include "rpm_finder.h"
25 #include "setupdwfl.h"
43 #include <elfutils/libdwfl.h>
44 #include <elfutils/libdw.h>
52 #include <sys/types.h>
55 #define __STDC_FORMAT_MACROS
59 // Older glibc elf.h don't know about this new constant.
60 #ifndef STB_GNU_UNIQUE
61 #define STB_GNU_UNIQUE 10
65 // debug flag to compare to the uncached version from libdw
66 // #define DEBUG_DWFLPP_GETSCOPES 1
70 using namespace __gnu_cxx
;
73 static string
TOK_KERNEL("kernel");
76 dwflpp::dwflpp(systemtap_session
& session
, const string
& name
, bool kernel_p
):
77 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
78 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
79 module_dwarf(NULL
), function(NULL
), blacklist_func(), blacklist_func_ret(),
80 blacklist_file(), blacklist_enabled(false)
83 setup_kernel(name
, session
);
86 vector
<string
> modules
;
87 modules
.push_back(name
);
92 dwflpp::dwflpp(systemtap_session
& session
, const vector
<string
>& names
,
94 sess(session
), module(NULL
), module_bias(0), mod_info(NULL
),
95 module_start(0), module_end(0), cu(NULL
), dwfl(NULL
),
96 module_dwarf(NULL
), function(NULL
), blacklist_enabled(false)
106 delete_map(module_cu_cache
);
107 delete_map(cu_function_cache
);
108 delete_map(mod_function_cache
);
109 delete_map(cu_inl_function_cache
);
110 delete_map(global_alias_cache
);
111 delete_map(cu_die_parent_cache
);
113 cu_lines_cache_t::iterator i
;
114 for (i
= cu_lines_cache
.begin(); i
!= cu_lines_cache
.end(); ++i
)
115 delete_map(*i
->second
);
116 delete_map(cu_lines_cache
);
120 // NB: don't "delete mod_info;", as that may be shared
121 // between dwflpp instances, and are stored in
122 // session.module_cache[] anyway.
126 module_cache::~module_cache ()
133 dwflpp::get_module_dwarf(bool required
, bool report
)
135 module_dwarf
= dwfl_module_getdwarf(module
, &module_bias
);
136 mod_info
->dwarf_status
= (module_dwarf
? info_present
: info_absent
);
137 if (!module_dwarf
&& report
)
139 string msg
= _("cannot find ");
140 if (module_name
== "")
143 msg
+= string("module ") + module_name
;
146 int i
= dwfl_errno();
148 msg
+= string(": ") + dwfl_errmsg (i
);
150 msg
+= " [man warning::debuginfo]";
152 /* add module_name to list to find rpm */
153 find_debug_rpms(sess
, module_name
.c_str());
156 throw SEMANTIC_ERROR (msg
);
158 sess
.print_warning(msg
);
164 dwflpp::focus_on_module(Dwfl_Module
* m
, module_info
* mi
)
170 module_name
= dwfl_module_info(module
, NULL
, &module_start
, &module_end
,
171 NULL
, NULL
, NULL
, NULL
) ?: "module";
175 assert(mi
&& mi
->name
&& mi
->name
== TOK_KERNEL
);
176 module_name
= mi
->name
;
179 module_bias
= mi
->bias
;
182 // Reset existing pointers and names
188 function_name
.clear();
194 dwflpp::focus_on_cu(Dwarf_Die
* c
)
201 // Reset existing pointers and names
202 function_name
.clear();
208 dwflpp::cu_name(void)
210 return dwarf_diename(cu
) ?: "<unknown source>";
215 dwflpp::focus_on_function(Dwarf_Die
* f
)
222 function_name
= dwarf_diename(function
) ?: "function";
226 /* Return the Dwarf_Die for the given address in the current module.
227 * The address should be in the module address address space (this
228 * function will take care of any dw bias).
231 dwflpp::query_cu_containing_address(Dwarf_Addr a
)
238 Dwarf_Die
* cudie
= dwfl_module_addrdie(module
, a
, &bias
);
239 assert(bias
== module_bias
);
245 dwflpp::module_name_matches(const string
& pattern
)
247 bool t
= (fnmatch(pattern
.c_str(), module_name
.c_str(), 0) == 0);
248 if (t
&& sess
.verbose
>3)
249 clog
<< _F("pattern '%s' matches module '%s'\n",
250 pattern
.c_str(), module_name
.c_str());
251 if (!t
&& sess
.verbose
>4)
252 clog
<< _F("pattern '%s' does not match module '%s'\n",
253 pattern
.c_str(), module_name
.c_str());
260 dwflpp::name_has_wildcard (const string
& pattern
)
262 return (pattern
.find('*') != string::npos
||
263 pattern
.find('?') != string::npos
||
264 pattern
.find('[') != string::npos
);
269 dwflpp::module_name_final_match(const string
& pattern
)
271 // Assume module_name_matches(). Can there be any more matches?
272 // Not unless the pattern is a wildcard, since module names are
274 return !name_has_wildcard(pattern
);
279 dwflpp::function_name_matches_pattern(const string
& name
, const string
& pattern
)
281 bool t
= (fnmatch(pattern
.c_str(), name
.c_str(), 0) == 0);
282 if (t
&& sess
.verbose
>3)
283 clog
<< _F("pattern '%s' matches function '%s'\n", pattern
.c_str(), name
.c_str());
289 dwflpp::function_name_matches(const string
& pattern
)
292 return function_name_matches_pattern(function_name
, pattern
);
297 dwflpp::function_scope_matches(const vector
<string
>& scopes
)
299 // walk up the containing scopes
300 Dwarf_Die
* die
= function
;
301 for (int i
= scopes
.size() - 1; i
>= 0; --i
)
303 die
= get_parent_scope(die
);
305 // check if this scope matches, and prepend it if so
306 // NB: a NULL die is the global scope, compared as ""
307 string name
= dwarf_diename(die
) ?: "";
308 if (name_has_wildcard(scopes
[i
]) ?
309 function_name_matches_pattern(name
, scopes
[i
]) :
311 function_name
= name
+ "::" + function_name
;
315 // make sure there's no more if we're at the global scope
324 dwflpp::setup_kernel(const string
& name
, systemtap_session
& s
, bool debuginfo_needed
)
326 if (! sess
.module_cache
)
327 sess
.module_cache
= new module_cache ();
329 unsigned offline_search_matches
= 0;
330 dwfl
= setup_dwfl_kernel(name
, &offline_search_matches
, sess
);
332 if (offline_search_matches
< 1)
334 if (debuginfo_needed
) {
335 // Suggest a likely kernel dir to find debuginfo rpm for
336 string dir
= string(sess
.sysroot
+ "/lib/modules/" + sess
.kernel_release
);
337 find_debug_rpms(sess
, dir
.c_str());
339 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
340 sess
.architecture
.c_str(), sess
.kernel_build_tree
.c_str()));
348 assert_no_interrupts();
349 off
= dwfl_getmodules (dwfl
, &add_module_build_id_to_hash
, &s
, off
);
352 DWFL_ASSERT("dwfl_getmodules", off
== 0);
355 build_kernel_blacklist();
359 dwflpp::setup_kernel(const vector
<string
> &names
, bool debuginfo_needed
)
361 if (! sess
.module_cache
)
362 sess
.module_cache
= new module_cache ();
364 unsigned offline_search_matches
= 0;
365 set
<string
> offline_search_names(names
.begin(), names
.end());
366 dwfl
= setup_dwfl_kernel(offline_search_names
,
367 &offline_search_matches
,
370 if (offline_search_matches
< offline_search_names
.size())
372 if (debuginfo_needed
) {
373 // Suggest a likely kernel dir to find debuginfo rpm for
374 string dir
= string(sess
.sysroot
+ "/lib/modules/" + sess
.kernel_release
);
375 find_debug_rpms(sess
, dir
.c_str());
377 throw SEMANTIC_ERROR (_F("missing %s kernel/module debuginfo [man warning::debuginfo] under '%s'",
378 sess
.architecture
.c_str(), sess
.kernel_build_tree
.c_str()));
381 build_kernel_blacklist();
386 dwflpp::setup_user(const vector
<string
>& modules
, bool debuginfo_needed
)
388 if (! sess
.module_cache
)
389 sess
.module_cache
= new module_cache ();
391 vector
<string
>::const_iterator it
= modules
.begin();
392 dwfl
= setup_dwfl_user(it
, modules
.end(), debuginfo_needed
, sess
);
393 if (debuginfo_needed
&& it
!= modules
.end())
394 DWFL_ASSERT (string(_F("missing process %s %s debuginfo",
395 (*it
).c_str(), sess
.architecture
.c_str())),
398 build_user_blacklist();
402 dwflpp::iterate_over_modules
<void>(int (*callback
)(Dwfl_Module
*,
409 dwfl_getmodules (dwfl
, callback
, data
, 0);
411 // Don't complain if we exited dwfl_getmodules early.
412 // This could be a $target variable error that will be
413 // reported soon anyway.
414 // DWFL_ASSERT("dwfl_getmodules", off == 0);
416 // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the
417 // "kernel" module is not reported in the loop above. However, we
418 // may be able to make do with symbol table data.
423 dwflpp::iterate_over_cus
<void>(int (*callback
)(Dwarf_Die
*, void*),
427 get_module_dwarf(false);
428 Dwarf
*dw
= module_dwarf
;
431 vector
<Dwarf_Die
>* v
= module_cu_cache
[dw
];
434 v
= new vector
<Dwarf_Die
>;
435 module_cu_cache
[dw
] = v
;
440 while (dwarf_nextcu (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) == 0)
442 assert_no_interrupts();
445 die
= dwarf_offdie (dw
, off
+ cuhl
, &die_mem
);
446 /* Skip partial units. */
447 if (dwarf_tag (die
) == DW_TAG_compile_unit
)
448 v
->push_back (*die
); /* copy */
453 if (want_types
&& module_tus_read
.find(dw
) == module_tus_read
.end())
455 // Process type units.
459 uint64_t type_signature
;
460 while (dwarf_next_unit (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
, NULL
,
461 &type_signature
, NULL
) == 0)
463 assert_no_interrupts();
466 die
= dwarf_offdie_types (dw
, off
+ cuhl
, &die_mem
);
467 /* Skip partial units. */
468 if (dwarf_tag (die
) == DW_TAG_type_unit
)
469 v
->push_back (*die
); /* copy */
472 module_tus_read
.insert(dw
);
475 for (vector
<Dwarf_Die
>::iterator i
= v
->begin(); i
!= v
->end(); ++i
)
477 int rc
= (*callback
)(&*i
, data
);
478 assert_no_interrupts();
479 if (rc
!= DWARF_CB_OK
)
486 dwflpp::func_is_inline()
489 return dwarf_func_inline (function
) != 0;
494 dwflpp::func_is_exported()
496 const char *name
= dwarf_linkage_name (function
) ?: dwarf_diename (function
);
500 int syms
= dwfl_module_getsymtab (module
);
501 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
503 for (int i
= 0; i
< syms
; i
++)
507 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
509 && strcmp (name
, symname
) == 0)
511 if (GELF_ST_TYPE(sym
.st_info
) == STT_FUNC
512 && (GELF_ST_BIND(sym
.st_info
) == STB_GLOBAL
513 || GELF_ST_BIND(sym
.st_info
) == STB_WEAK
514 || GELF_ST_BIND(sym
.st_info
) == STB_GNU_UNIQUE
))
524 dwflpp::cache_inline_instances (Dwarf_Die
* die
)
526 // If this is an inline instance, link it back to its origin
528 if (dwarf_tag(die
) == DW_TAG_inlined_subroutine
&&
529 dwarf_attr_die(die
, DW_AT_abstract_origin
, &origin
))
531 vector
<Dwarf_Die
>*& v
= cu_inl_function_cache
[origin
.addr
];
533 v
= new vector
<Dwarf_Die
>;
537 // Recurse through other scopes that may contain inlines
538 Dwarf_Die child
, import
;
539 if (dwarf_child(die
, &child
) == 0)
542 switch (dwarf_tag (&child
))
544 // tags that could contain inlines
545 case DW_TAG_compile_unit
:
547 case DW_TAG_lexical_block
:
548 case DW_TAG_with_stmt
:
549 case DW_TAG_catch_block
:
550 case DW_TAG_try_block
:
551 case DW_TAG_entry_point
:
552 case DW_TAG_inlined_subroutine
:
553 case DW_TAG_subprogram
:
554 cache_inline_instances(&child
);
557 // imported dies should be followed
558 case DW_TAG_imported_unit
:
559 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
560 cache_inline_instances(&import
);
563 // nothing to do for other tags
568 while (dwarf_siblingof(&child
, &child
) == 0);
573 dwflpp::iterate_over_inline_instances
<void>(int (*callback
)(Dwarf_Die
*, void*),
577 assert (func_is_inline ());
579 if (cu_inl_function_cache_done
.insert(cu
->addr
).second
)
580 cache_inline_instances(cu
);
582 vector
<Dwarf_Die
>* v
= cu_inl_function_cache
[function
->addr
];
586 for (vector
<Dwarf_Die
>::iterator i
= v
->begin(); i
!= v
->end(); ++i
)
588 int rc
= (*callback
)(&*i
, data
);
589 assert_no_interrupts();
590 if (rc
!= DWARF_CB_OK
)
597 dwflpp::cache_die_parents(cu_die_parent_cache_t
* parents
, Dwarf_Die
* die
)
599 // Record and recurse through DIEs we care about
600 Dwarf_Die child
, import
;
601 if (dwarf_child(die
, &child
) == 0)
604 switch (dwarf_tag (&child
))
606 // normal tags to recurse
607 case DW_TAG_compile_unit
:
609 case DW_TAG_lexical_block
:
610 case DW_TAG_with_stmt
:
611 case DW_TAG_catch_block
:
612 case DW_TAG_try_block
:
613 case DW_TAG_entry_point
:
614 case DW_TAG_inlined_subroutine
:
615 case DW_TAG_subprogram
:
616 case DW_TAG_namespace
:
617 case DW_TAG_class_type
:
618 case DW_TAG_structure_type
:
619 parents
->insert(make_pair(child
.addr
, *die
));
620 cache_die_parents(parents
, &child
);
623 // record only, nothing to recurse
625 parents
->insert(make_pair(child
.addr
, *die
));
628 // imported dies should be followed
629 case DW_TAG_imported_unit
:
630 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
632 parents
->insert(make_pair(import
.addr
, *die
));
633 cache_die_parents(parents
, &import
);
637 // nothing to do for other tags
642 while (dwarf_siblingof(&child
, &child
) == 0);
646 cu_die_parent_cache_t
*
647 dwflpp::get_die_parents()
651 cu_die_parent_cache_t
*& parents
= cu_die_parent_cache
[cu
->addr
];
654 parents
= new cu_die_parent_cache_t
;
655 cache_die_parents(parents
, cu
);
656 if (sess
.verbose
> 4)
657 clog
<< _F("die parent cache %s:%s size %zu", module_name
.c_str(),
658 cu_name().c_str(), parents
->size()) << endl
;
665 dwflpp::getscopes_die(Dwarf_Die
* die
)
667 cu_die_parent_cache_t
*parents
= get_die_parents();
669 vector
<Dwarf_Die
> scopes
;
670 Dwarf_Die
*scope
= die
;
671 cu_die_parent_cache_t::iterator it
;
674 scopes
.push_back(*scope
);
675 it
= parents
->find(scope
->addr
);
678 while (it
!= parents
->end());
680 #ifdef DEBUG_DWFLPP_GETSCOPES
681 Dwarf_Die
*dscopes
= NULL
;
682 int nscopes
= dwarf_getscopes_die(die
, &dscopes
);
684 assert(nscopes
== (int)scopes
.size());
685 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
686 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
694 std::vector
<Dwarf_Die
>
695 dwflpp::getscopes(Dwarf_Die
* die
)
697 cu_die_parent_cache_t
*parents
= get_die_parents();
699 vector
<Dwarf_Die
> scopes
;
702 Dwarf_Die
*scope
= die
;
703 cu_die_parent_cache_t::iterator it
;
706 scopes
.push_back(*scope
);
707 if (dwarf_tag(scope
) == DW_TAG_inlined_subroutine
&&
708 dwarf_attr_die(scope
, DW_AT_abstract_origin
, &origin
))
711 it
= parents
->find(scope
->addr
);
714 while (it
!= parents
->end());
716 #ifdef DEBUG_DWFLPP_GETSCOPES
717 // there isn't an exact libdw equivalent, but if dwarf_getscopes on the
718 // entrypc returns the same first die, then all the scopes should match
720 if (die_entrypc(die
, &pc
))
722 Dwarf_Die
*dscopes
= NULL
;
723 int nscopes
= dwarf_getscopes(cu
, pc
, &dscopes
);
724 if (nscopes
> 0 && dscopes
[0].addr
== die
->addr
)
726 assert(nscopes
== (int)scopes
.size());
727 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
728 assert(scopes
[i
].addr
== dscopes
[i
].addr
);
738 std::vector
<Dwarf_Die
>
739 dwflpp::getscopes(Dwarf_Addr pc
)
741 // The die_parent_cache doesn't help us without knowing where the pc is
742 // contained, so we have to do this one the old fashioned way.
746 vector
<Dwarf_Die
> scopes
;
748 Dwarf_Die
* dwarf_scopes
;
749 int nscopes
= dwarf_getscopes(cu
, pc
, &dwarf_scopes
);
752 scopes
.assign(dwarf_scopes
, dwarf_scopes
+ nscopes
);
756 #ifdef DEBUG_DWFLPP_GETSCOPES
757 // check that getscopes on the starting die gets the same result
760 vector
<Dwarf_Die
> other
= getscopes(&scopes
[0]);
761 assert(scopes
.size() == other
.size());
762 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
763 assert(scopes
[i
].addr
== other
[i
].addr
);
772 dwflpp::get_parent_scope(Dwarf_Die
* die
)
774 Dwarf_Die specification
;
775 if (dwarf_attr_die(die
, DW_AT_specification
, &specification
))
776 die
= &specification
;
778 cu_die_parent_cache_t
*parents
= get_die_parents();
779 cu_die_parent_cache_t::iterator it
= parents
->find(die
->addr
);
780 while (it
!= parents
->end())
782 Dwarf_Die
* scope
= &it
->second
;
783 switch (dwarf_tag (scope
))
785 case DW_TAG_namespace
:
786 case DW_TAG_class_type
:
787 case DW_TAG_structure_type
:
793 it
= parents
->find(scope
->addr
);
799 cache_type_prefix(Dwarf_Die
* type
)
801 switch (dwarf_tag(type
))
803 case DW_TAG_enumeration_type
:
805 case DW_TAG_structure_type
:
806 case DW_TAG_class_type
:
807 // treating struct/class as equals
809 case DW_TAG_union_type
:
815 /* GCC might generate a struct/class without DW_AT_declaration,
816 but that only contains members which have DW_AT_declaration
817 set. We aren't interested in those. PR14434 (GCC bug #54181). */
819 has_only_decl_members (Dwarf_Die
*die
)
821 Dwarf_Die child
, import
;
822 if (dwarf_child(die
, &child
) != 0)
823 return false; /* no members */
827 if (! dwarf_hasattr(&child
, DW_AT_declaration
))
828 return false; /* real member found. */
829 int tag
= dwarf_tag(&child
);
830 if ((tag
== DW_TAG_namespace
831 || tag
== DW_TAG_structure_type
832 || tag
== DW_TAG_class_type
)
833 && ! has_only_decl_members (&child
))
834 return false; /* real grand child member found. */
836 // Unlikely to ever happen, but if there is an imported unit
837 // then check its children as if they are children of this DIE.
838 if (tag
== DW_TAG_imported_unit
839 && dwarf_attr_die(&child
, DW_AT_import
, &import
)
840 && ! has_only_decl_members (&import
))
843 while (dwarf_siblingof(&child
, &child
) == 0);
845 return true; /* Tried all children and grandchildren. */
849 dwflpp::global_alias_caching_callback(Dwarf_Die
*die
, bool has_inner_types
,
850 const string
& prefix
, cu_type_cache_t
*cache
)
852 const char *name
= dwarf_diename(die
);
854 if (!name
|| dwarf_hasattr(die
, DW_AT_declaration
)
855 || has_only_decl_members(die
))
858 int tag
= dwarf_tag(die
);
859 if (has_inner_types
&& (tag
== DW_TAG_namespace
860 || tag
== DW_TAG_structure_type
861 || tag
== DW_TAG_class_type
))
862 iterate_over_types(die
, has_inner_types
, prefix
+ name
+ "::",
863 global_alias_caching_callback
, cache
);
865 if (tag
!= DW_TAG_namespace
)
867 string type_name
= prefix
+ cache_type_prefix(die
) + name
;
868 if (cache
->find(type_name
) == cache
->end())
869 (*cache
)[type_name
] = *die
;
876 dwflpp::global_alias_caching_callback_cus(Dwarf_Die
*die
, dwflpp
*dw
)
878 mod_cu_type_cache_t
*global_alias_cache
;
879 global_alias_cache
= &dw
->global_alias_cache
;
881 cu_type_cache_t
*v
= (*global_alias_cache
)[die
->addr
];
885 v
= new cu_type_cache_t
;
886 (*global_alias_cache
)[die
->addr
] = v
;
887 iterate_over_globals(die
, global_alias_caching_callback
, v
);
893 dwflpp::declaration_resolve_other_cus(const string
& name
)
895 iterate_over_cus(global_alias_caching_callback_cus
, this, true);
896 for (mod_cu_type_cache_t::iterator i
= global_alias_cache
.begin();
897 i
!= global_alias_cache
.end(); ++i
)
899 cu_type_cache_t
*v
= (*i
).second
;
900 if (v
->find(name
) != v
->end())
901 return & ((*v
)[name
]);
908 dwflpp::declaration_resolve(const string
& name
)
910 cu_type_cache_t
*v
= global_alias_cache
[cu
->addr
];
911 if (v
== 0) // need to build the cache, just once per encountered module/cu
913 v
= new cu_type_cache_t
;
914 global_alias_cache
[cu
->addr
] = v
;
915 iterate_over_globals(cu
, global_alias_caching_callback
, v
);
916 if (sess
.verbose
> 4)
917 clog
<< _F("global alias cache %s:%s size %zu", module_name
.c_str(),
918 cu_name().c_str(), v
->size()) << endl
;
921 // XXX: it may be desirable to search other modules' declarations
922 // too, in case a module/shared-library processes a
923 // forward-declared pointer type only, where the actual definition
924 // may only be in vmlinux or the application.
926 if (v
->find(name
) == v
->end())
927 return declaration_resolve_other_cus(name
);
929 return & ((*v
)[name
]);
933 dwflpp::declaration_resolve(Dwarf_Die
*type
)
935 const char* name
= dwarf_diename(type
);
939 string type_name
= cache_type_prefix(type
) + string(name
);
940 return declaration_resolve(type_name
);
945 dwflpp::cu_function_caching_callback (Dwarf_Die
* func
, cu_function_cache_t
*v
)
947 const char *name
= dwarf_diename(func
);
951 v
->insert(make_pair(string(name
), *func
));
957 dwflpp::mod_function_caching_callback (Dwarf_Die
* cu
, cu_function_cache_t
*v
)
959 // need to cast callback to func which accepts void*
960 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
967 dwflpp::iterate_over_functions
<void>(int (*callback
)(Dwarf_Die
*, void*),
968 void *data
, const string
& function
)
970 int rc
= DWARF_CB_OK
;
974 cu_function_cache_t
*v
= cu_function_cache
[cu
->addr
];
977 v
= new cu_function_cache_t
;
978 cu_function_cache
[cu
->addr
] = v
;
979 // need to cast callback to func which accepts void*
980 dwarf_getfuncs (cu
, (int (*)(Dwarf_Die
*, void*))cu_function_caching_callback
,
982 if (sess
.verbose
> 4)
983 clog
<< _F("function cache %s:%s size %zu", module_name
.c_str(),
984 cu_name().c_str(), v
->size()) << endl
;
985 mod_info
->update_symtab(v
);
988 cu_function_cache_t::iterator it
;
989 cu_function_cache_range_t range
= v
->equal_range(function
);
990 if (range
.first
!= range
.second
)
992 for (it
= range
.first
; it
!= range
.second
; ++it
)
994 Dwarf_Die
& die
= it
->second
;
995 if (sess
.verbose
> 4)
996 clog
<< _F("function cache %s:%s hit %s", module_name
.c_str(),
997 cu_name().c_str(), function
.c_str()) << endl
;
998 rc
= (*callback
)(& die
, data
);
999 if (rc
!= DWARF_CB_OK
) break;
1002 else if (startswith(function
, "_Z"))
1004 // C++ names are mangled starting with a "_Z" prefix. Most of the time
1005 // we can discover the mangled name from a die's MIPS_linkage_name
1006 // attribute, so we read that to match against the user's function
1007 // pattern. Note that this isn't perfect, as not all will have that
1008 // attribute (notably ctors and dtors), but we do what we can...
1009 for (it
= v
->begin(); it
!= v
->end(); ++it
)
1011 if (pending_interrupts
) return DWARF_CB_ABORT
;
1012 Dwarf_Die
& die
= it
->second
;
1013 const char* linkage_name
= NULL
;
1014 if ((linkage_name
= dwarf_linkage_name (&die
))
1015 && function_name_matches_pattern (linkage_name
, function
))
1017 if (sess
.verbose
> 4)
1018 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1019 cu_name().c_str(), linkage_name
, function
.c_str()) << endl
;
1021 rc
= (*callback
)(& die
, data
);
1022 if (rc
!= DWARF_CB_OK
) break;
1026 else if (name_has_wildcard (function
))
1028 for (it
= v
->begin(); it
!= v
->end(); ++it
)
1030 if (pending_interrupts
) return DWARF_CB_ABORT
;
1031 const string
& func_name
= it
->first
;
1032 Dwarf_Die
& die
= it
->second
;
1033 if (function_name_matches_pattern (func_name
, function
))
1035 if (sess
.verbose
> 4)
1036 clog
<< _F("function cache %s:%s match %s vs %s", module_name
.c_str(),
1037 cu_name().c_str(), func_name
.c_str(), function
.c_str()) << endl
;
1039 rc
= (*callback
)(& die
, data
);
1040 if (rc
!= DWARF_CB_OK
) break;
1044 else // not a linkage name or wildcard and no match in this CU
1053 dwflpp::iterate_single_function
<void>(int (*callback
)(Dwarf_Die
*, void*),
1054 void *data
, const string
& function
)
1056 int rc
= DWARF_CB_OK
;
1059 get_module_dwarf(false);
1063 cu_function_cache_t
*v
= mod_function_cache
[module_dwarf
];
1066 v
= new cu_function_cache_t
;
1067 mod_function_cache
[module_dwarf
] = v
;
1068 iterate_over_cus (mod_function_caching_callback
, v
, false);
1069 if (sess
.verbose
> 4)
1070 clog
<< _F("module function cache %s size %zu", module_name
.c_str(),
1072 mod_info
->update_symtab(v
);
1075 cu_function_cache_t::iterator it
;
1076 cu_function_cache_range_t range
= v
->equal_range(function
);
1077 if (range
.first
!= range
.second
)
1079 for (it
= range
.first
; it
!= range
.second
; ++it
)
1082 Dwarf_Die
& die
= it
->second
;
1083 if (sess
.verbose
> 4)
1084 clog
<< _F("module function cache %s hit %s", module_name
.c_str(),
1085 function
.c_str()) << endl
;
1087 // since we're iterating out of cu-context, we need each focus
1088 focus_on_cu(dwarf_diecu(&die
, &cu_mem
, NULL
, NULL
));
1090 rc
= (*callback
)(& die
, data
);
1091 if (rc
!= DWARF_CB_OK
) break;
1095 // undo the focus_on_cu
1097 this->function_name
.clear();
1098 this->function
= NULL
;
1104 /* This basically only goes one level down from the compile unit so it
1105 * only picks up top level stuff (i.e. nothing in a lower scope) */
1107 dwflpp::iterate_over_globals
<void>(Dwarf_Die
*cu_die
,
1108 int (*callback
)(Dwarf_Die
*,
1115 assert (dwarf_tag(cu_die
) == DW_TAG_compile_unit
1116 || dwarf_tag(cu_die
) == DW_TAG_type_unit
1117 || dwarf_tag(cu_die
) == DW_TAG_partial_unit
);
1119 // Ignore partial_unit, if they get imported by a real unit, then
1120 // iterate_over_types will traverse them.
1121 if (dwarf_tag(cu_die
) == DW_TAG_partial_unit
)
1124 // If this is C++, recurse for any inner types
1125 bool has_inner_types
= dwarf_srclang(cu_die
) == DW_LANG_C_plus_plus
;
1127 return iterate_over_types(cu_die
, has_inner_types
, "", callback
, data
);
1131 dwflpp::iterate_over_types
<void>(Dwarf_Die
*top_die
,
1132 bool has_inner_types
,
1133 const string
& prefix
,
1134 int (* callback
)(Dwarf_Die
*,
1140 int rc
= DWARF_CB_OK
;
1141 Dwarf_Die die
, import
;
1145 if (dwarf_child(top_die
, &die
) != 0)
1149 /* We're only currently looking for named types,
1150 * although other types of declarations exist */
1151 switch (dwarf_tag(&die
))
1153 case DW_TAG_base_type
:
1154 case DW_TAG_enumeration_type
:
1155 case DW_TAG_structure_type
:
1156 case DW_TAG_class_type
:
1157 case DW_TAG_typedef
:
1158 case DW_TAG_union_type
:
1159 case DW_TAG_namespace
:
1160 rc
= (*callback
)(&die
, has_inner_types
, prefix
, data
);
1163 case DW_TAG_imported_unit
:
1164 // Follow the imported_unit and iterate over its contents
1165 // (either a partial_unit or a full compile_unit), all its
1166 // children should be treated as if they appear in this place.
1167 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
1168 rc
= iterate_over_types(&import
, has_inner_types
, prefix
,
1172 while (rc
== DWARF_CB_OK
&& dwarf_siblingof(&die
, &die
) == 0);
1178 /* For each notes section in the current module call 'callback', use
1179 * 'data' for the notes buffer and pass 'object' back in case
1180 * 'callback' is a method */
1183 dwflpp::iterate_over_notes
<void>(void *object
, void (*callback
)(void*,
1189 // Note we really want the actual elf file, not the dwarf .debug file.
1190 // Older binutils had a bug where they mangled the SHT_NOTE type during
1192 Elf
* elf
= dwfl_module_getelf (module
, &bias
);
1194 if (elf_getshdrstrndx (elf
, &shstrndx
))
1197 Elf_Scn
*scn
= NULL
;
1199 vector
<Dwarf_Die
> notes
;
1201 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
1204 if (gelf_getshdr (scn
, &shdr
) == NULL
)
1206 switch (shdr
.sh_type
)
1209 if (!(shdr
.sh_flags
& SHF_ALLOC
))
1211 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1216 for (size_t offset
= 0;
1217 (next
= gelf_getnote (data
, offset
, &nhdr
, &name_off
, &desc_off
)) > 0;
1219 (*callback
) (object
, nhdr
.n_type
, (const char*)((long)(data
->d_buf
) + (long)desc_off
), nhdr
.n_descsz
);
1228 /* For each entry in the .dynamic section in the current module call 'callback'
1229 * returning 'object' in case 'callback' is a method */
1232 dwflpp::iterate_over_libraries
<void>(void (*callback
)(void*, const char*),
1235 std::set
<std::string
> added
;
1238 assert (this->module_name
.length() != 0);
1241 // We cannot use this: dwarf_getelf (dwfl_module_getdwarf (module, &bias))
1242 Elf
*elf
= dwfl_module_getelf (module
, &bias
);
1243 // elf_getphdrnum (elf, &phnum) is not available in all versions of elfutils
1244 // needs libelf from elfutils 0.144+
1245 for (int i
= 0; ; i
++)
1249 phdr
= gelf_getphdr (elf
, i
, &mem
);
1252 if (phdr
->p_type
== PT_INTERP
)
1255 char *filedata
= elf_rawfile (elf
, &maxsize
);
1257 if (filedata
!= NULL
&& phdr
->p_offset
< maxsize
)
1258 interpreter
= (char*) (filedata
+ phdr
->p_offset
);
1263 if (interpreter
.length() == 0)
1265 // If it gets cumbersome to maintain this whitelist, we could just check for
1266 // startswith("/lib/ld") || startswith("/lib64/ld"), and trust that no admin
1267 // would install untrustworthy loaders in those paths.
1268 // See also http://sourceware.org/git/?p=glibc.git;a=blob;f=shlib-versions;hb=HEAD
1269 if (interpreter
!= "/lib/ld.so.1" // s390, ppc
1270 && interpreter
!= "/lib/ld64.so.1" // s390x, ppc64
1271 && interpreter
!= "/lib64/ld64.so.1"
1272 && interpreter
!= "/lib/ld-linux-ia64.so.2" // ia64
1273 && interpreter
!= "/emul/ia32-linux/lib/ld-linux.so.2"
1274 && interpreter
!= "/lib64/ld-linux-x86-64.so.2" // x8664
1275 && interpreter
!= "/lib/ld-linux.so.2" // x86
1276 && interpreter
!= "/lib/ld-linux.so.3" // arm
1277 && interpreter
!= "/lib/ld-linux-armhf.so.3" // arm
1280 sess
.print_warning (_F("module %s --ldd skipped: unsupported interpreter: %s",
1281 module_name
.c_str(), interpreter
.c_str()));
1285 vector
<string
> ldd_command
;
1286 ldd_command
.push_back("/usr/bin/env");
1287 ldd_command
.push_back("LD_TRACE_LOADED_OBJECTS=1");
1288 ldd_command
.push_back("LD_WARN=yes");
1289 ldd_command
.push_back("LD_BIND_NOW=yes");
1290 ldd_command
.push_back(interpreter
);
1291 ldd_command
.push_back(module_name
);
1295 pid_t child
= stap_spawn_piped(sess
.verbose
, ldd_command
, NULL
, &child_fd
);
1296 if (child
<= 0 || !(fp
= fdopen(child_fd
, "r")))
1297 clog
<< _F("library iteration on %s failed: %s",
1298 module_name
.c_str(), strerror(errno
)) << endl
;
1301 while (1) // this parsing loop borrowed from add_unwindsym_ldd
1306 unsigned long int addr
= 0;
1308 char *line
= fgets (linebuf
, 256, fp
);
1309 if (line
== 0) break; // EOF or error
1311 #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)
1312 #define MS_FMT "%ms"
1314 #define MS_FMT "%as"
1316 // Try soname => shlib (0xaddr)
1317 int nf
= sscanf (line
, MS_FMT
" => " MS_FMT
" (0x%lx)",
1318 &soname
, &shlib
, &addr
);
1319 if (nf
!= 3 || shlib
[0] != '/')
1321 // Try shlib (0xaddr)
1322 nf
= sscanf (line
, " " MS_FMT
" (0x%lx)", &shlib
, &addr
);
1323 if (nf
!= 2 || shlib
[0] != '/')
1324 continue; // fewer than expected fields, or bad shlib.
1327 if (added
.find (shlib
) == added
.end())
1329 if (sess
.verbose
> 2)
1331 clog
<< _F("Added -d '%s", shlib
);
1333 clog
<< _F("' due to '%s'", soname
);
1338 added
.insert (shlib
);
1344 if ((fclose(fp
) || stap_waitpid(sess
.verbose
, child
)))
1345 sess
.print_warning("failed to read libraries from " + module_name
+ ": " + strerror(errno
));
1348 for (std::set
<std::string
>::iterator it
= added
.begin();
1352 string modname
= *it
;
1353 (callback
) (data
, modname
.c_str());
1358 /* For each plt section in the current module call 'callback', pass the plt entry
1359 * 'address' and 'name' back, and pass 'object' back in case 'callback' is a method */
1362 dwflpp::iterate_over_plt
<void>(void *object
, void (*callback
)(void*,
1366 Dwarf_Addr load_addr
;
1367 // Note we really want the actual elf file, not the dwarf .debug file.
1368 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
1370 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
1372 // Get the load address
1373 for (int i
= 0; ; i
++)
1377 phdr
= gelf_getphdr (elf
, i
, &mem
);
1380 if (phdr
->p_type
== PT_LOAD
)
1382 load_addr
= phdr
->p_vaddr
;
1387 // Get the plt section header
1388 Elf_Scn
*scn
= NULL
;
1389 GElf_Shdr
*plt_shdr
= NULL
;
1390 GElf_Shdr plt_shdr_mem
;
1391 while ((scn
= elf_nextscn (elf
, scn
)))
1393 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &plt_shdr_mem
);
1394 assert (shdr
!= NULL
);
1395 if (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".plt") == 0)
1401 if (plt_shdr
== NULL
)
1404 // Layout of the plt section
1405 int plt0_entry_size
;
1408 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1409 switch (em
->e_machine
)
1411 case EM_386
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1412 case EM_X86_64
: plt0_entry_size
= 16; plt_entry_size
= 16; break;
1413 case EM_ARM
: plt0_entry_size
= 20; plt_entry_size
= 12; break;
1418 throw SEMANTIC_ERROR(".plt is not supported on this architecture");
1422 while ((scn
= elf_nextscn (elf
, scn
)))
1425 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
1426 bool have_rela
= false;
1427 bool have_rel
= false;
1431 assert (shdr
!= NULL
);
1433 if ((have_rela
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rela.plt") == 0))
1434 || (have_rel
= (strcmp (elf_strptr (elf
, shstrndx
, shdr
->sh_name
), ".rel.plt") == 0)))
1436 /* Get the data of the section. */
1437 Elf_Data
*data
= elf_getdata (scn
, NULL
);
1438 assert (data
!= NULL
);
1439 /* Get the symbol table information. */
1440 Elf_Scn
*symscn
= elf_getscn (elf
, shdr
->sh_link
);
1441 GElf_Shdr symshdr_mem
;
1442 GElf_Shdr
*symshdr
= gelf_getshdr (symscn
, &symshdr_mem
);
1443 assert (symshdr
!= NULL
);
1444 Elf_Data
*symdata
= elf_getdata (symscn
, NULL
);
1445 assert (symdata
!= NULL
);
1447 unsigned int nsyms
= shdr
->sh_size
/ shdr
->sh_entsize
;
1449 for (unsigned int cnt
= 0; cnt
< nsyms
; ++cnt
)
1452 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1453 if (em
== 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
1456 GElf_Rela
*rela
= NULL
;
1458 GElf_Rel
*rel
= NULL
;
1461 rela
= gelf_getrela (data
, cnt
, &relamem
);
1462 assert (rela
!= NULL
);
1466 rel
= gelf_getrel (data
, cnt
, &relmem
);
1467 assert (rel
!= NULL
);
1471 Elf_Data
*xndxdata
= NULL
;
1473 gelf_getsymshndx (symdata
, xndxdata
,
1474 GELF_R_SYM (have_rela
? rela
->r_info
: rel
->r_info
),
1476 assert (sym
!= NULL
);
1477 Dwarf_Addr addr
= plt_shdr
->sh_offset
+ plt0_entry_size
+ cnt
* plt_entry_size
;
1479 if (elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
))
1480 (*callback
) (object
, elf_strptr (elf
, symshdr
->sh_link
, sym
->st_name
), addr
+ load_addr
);
1489 // Comparator function for sorting
1491 compare_lines(Dwarf_Line
* a
, Dwarf_Line
* b
)
1496 int lineno_a
= DWARF_LINENO(a
);
1497 int lineno_b
= DWARF_LINENO(b
);
1498 if (lineno_a
== lineno_b
)
1499 return DWARF_LINEADDR(a
) < DWARF_LINEADDR(b
);
1500 return lineno_a
< lineno_b
;
1503 // Comparator object for searching Dwarf_Lines with a specific lineno when we
1504 // don't have a Dwarf_Line of our own to search for (hence why a or b is always
1506 struct lineno_comparator
{
1508 lineno_comparator(int lineno
): lineno(lineno
) {}
1509 bool operator() (Dwarf_Line
* a
, Dwarf_Line
* b
)
1513 return DWARF_LINENO(a
) < lineno
;
1515 return lineno
< DWARF_LINENO(b
);
1519 // Returns a range of lines in between begin and end with wanted lineno. If
1520 // none exist, points to where it would have been.
1521 static lines_range_t
1522 lineno_equal_range(lines_t
* v
, int lineno
)
1524 lineno_comparator
lc(lineno
);
1525 return equal_range(v
->begin(), v
->end(), (Dwarf_Line
*)NULL
, lc
);
1528 // Interface to CU lines cache sorted by lineno
1530 dwflpp::get_cu_lines_sorted_by_lineno(const char *srcfile
)
1534 srcfile_lines_cache_t
*srcfile_lines
= cu_lines_cache
[cu
];
1537 srcfile_lines
= new srcfile_lines_cache_t();
1538 cu_lines_cache
[cu
] = srcfile_lines
;
1541 lines_t
*lines
= (*srcfile_lines
)[srcfile
];
1544 size_t nlines_cu
= 0;
1545 Dwarf_Lines
*lines_cu
= NULL
;
1546 DWARF_ASSERT("dwarf_getsrclines",
1547 dwarf_getsrclines(cu
, &lines_cu
, &nlines_cu
));
1549 lines
= new lines_t();
1550 (*srcfile_lines
)[srcfile
] = lines
;
1552 for (size_t i
= 0; i
< nlines_cu
; i
++)
1554 Dwarf_Line
*line
= dwarf_onesrcline(lines_cu
, i
);
1555 const char *linesrc
= DWARF_LINESRC(line
);
1556 if (strcmp(srcfile
, linesrc
))
1558 lines
->push_back(line
);
1561 if (lines
->size() > 1)
1562 sort(lines
->begin(), lines
->end(), compare_lines
);
1564 if (sess
.verbose
> 3)
1566 clog
<< "found the following lines for %s:" << endl
;
1567 lines_t::iterator i
;
1568 for (i
= lines
->begin(); i
!= lines
->end(); ++i
)
1569 cout
<< DWARF_LINENO(*i
) << " = " << hex
1570 << DWARF_LINEADDR(*i
) << dec
<< endl
;
1577 get_func_first_line(Dwarf_Die
*cu
, base_func_info
& func
)
1579 // dwarf_getsrc_die() uses binary search to find the Dwarf_Line, but will
1580 // return the wrong line if not found.
1581 Dwarf_Line
*line
= dwarf_getsrc_die(cu
, func
.entrypc
);
1582 if (line
&& DWARF_LINEADDR(line
) == func
.entrypc
)
1585 // Line not found (or line at wrong addr). We have to resort to a slower
1586 // linear method. We won't find an exact match (probably this is an inlined
1587 // instance), so just settle on the first Dwarf_Line with lowest addr which
1588 // falls in the die.
1590 Dwarf_Lines
*lines
= NULL
;
1591 DWARF_ASSERT("dwarf_getsrclines",
1592 dwarf_getsrclines(cu
, &lines
, &nlines
));
1594 for (size_t i
= 0; i
< nlines
; i
++)
1596 line
= dwarf_onesrcline(lines
, i
);
1597 if (dwarf_haspc(&func
.die
, DWARF_LINEADDR(line
)))
1604 collect_lines_in_die(lines_range_t range
, Dwarf_Die
*die
)
1606 lines_t lines_in_die
;
1607 for (lines_t::iterator line
= range
.first
;
1608 line
!= range
.second
; ++line
)
1609 if (dwarf_haspc(die
, DWARF_LINEADDR(*line
)))
1610 lines_in_die
.push_back(*line
);
1611 return lines_in_die
;
1615 add_matching_lines_in_func(Dwarf_Die
*cu
,
1617 base_func_info
& func
,
1618 lines_t
& matching_lines
)
1620 Dwarf_Line
*start_line
= get_func_first_line(cu
, func
);
1624 for (int lineno
= DWARF_LINENO(start_line
);;)
1626 lines_range_t range
= lineno_equal_range(cu_lines
, lineno
);
1628 // We consider the lineno still part of the die if at least one of them
1629 // falls in the die.
1630 lines_t lines_in_die
= collect_lines_in_die(range
, &func
.die
);
1631 if (lines_in_die
.empty())
1634 // Just pick the first LR even if there are more than one. Since the lines
1635 // are sorted by lineno and then addr, the first one is the one with the
1637 matching_lines
.push_back(lines_in_die
[0]);
1639 // break out if there are no more lines, otherwise, go to the next lineno
1640 if (range
.second
== cu_lines
->end())
1642 lineno
= DWARF_LINENO(*range
.second
);
1647 dwflpp::collect_all_lines(char const * srcfile
,
1648 base_func_info_map_t
& funcs
,
1649 lines_t
& matching_lines
)
1651 // This is where we handle WILDCARD lineno types.
1652 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1653 for (base_func_info_map_t::iterator func
= funcs
.begin();
1654 func
!= funcs
.end(); ++func
)
1655 add_matching_lines_in_func(cu
, cu_lines
, *func
, matching_lines
);
1660 add_matching_line_in_die(lines_t
*cu_lines
,
1661 lines_t
& matching_lines
,
1662 Dwarf_Die
*die
, int lineno
)
1664 lines_range_t lineno_range
= lineno_equal_range(cu_lines
, lineno
);
1665 lines_t lines_in_die
= collect_lines_in_die(lineno_range
, die
);
1666 if (lines_in_die
.empty())
1669 // Even if there are more than 1 LRs, just pick the first one. Since the lines
1670 // are sorted by lineno and then addr, the first one is the one with the
1671 // lowest addr. This is similar to what GDB does.
1672 matching_lines
.push_back(lines_in_die
[0]);
1676 dwflpp::collect_lines_for_single_lineno(char const * srcfile
,
1679 base_func_info_map_t
& funcs
,
1680 lines_t
& matching_lines
)
1682 /* Here, we handle ABSOLUTE and RELATIVE lineno types. Relative line numbers
1683 * are a bit special. The issue is that functions (esp. inlined ones) may not
1684 * even have a LR corresponding to the first valid line of code. So, applying
1685 * an offset to the 'first' LR found in the DIE can be quite imprecise.
1686 * Instead, we use decl_line, which although does not necessarily have a
1687 * LR associated with it (it can sometimes still happen esp. if the code is
1688 * written in OTB-style), it serves as an anchor on which we can apply the
1689 * offset to yield a lineno that will not change with compiler optimization.
1690 * It also has the added benefit of being consistent with the lineno
1691 * printed by e.g. stap -l kernel.function("vfs_read"), so users can be
1692 * confident from what lineno we adjust.
1694 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1695 for (base_func_info_map_t::iterator func
= funcs
.begin();
1696 func
!= funcs
.end(); ++func
)
1697 add_matching_line_in_die(cu_lines
, matching_lines
, &func
->die
,
1698 is_relative
? lineno
+ func
->decl_line
1703 functions_have_lineno(base_func_info_map_t
& funcs
,
1704 lines_t
*lines
, int lineno
)
1706 lines_range_t lineno_range
= lineno_equal_range(lines
, lineno
);
1707 if (lineno_range
.first
== lineno_range
.second
)
1708 return false; // no LRs at this lineno
1710 for (base_func_info_map_t::iterator func
= funcs
.begin();
1711 func
!= funcs
.end(); ++func
)
1712 if (!collect_lines_in_die(lineno_range
, &func
->die
).empty())
1719 dwflpp::suggest_alternative_linenos(char const * srcfile
,
1721 base_func_info_map_t
& funcs
)
1724 lines_t
*cu_lines
= get_cu_lines_sorted_by_lineno(srcfile
);
1726 // Look around lineno for linenos with LRs.
1729 for (size_t i
= 1; i
< 6; ++i
)
1731 if (lo_try
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
-i
))
1732 lo_try
= lineno
- i
;
1733 if (hi_try
== -1 && functions_have_lineno(funcs
, cu_lines
, lineno
+i
))
1734 hi_try
= lineno
+ i
;
1737 stringstream advice
;
1738 advice
<< _F("no line records for %s:%d [man error::dwarf]", srcfile
, lineno
);
1740 if (lo_try
> 0 || hi_try
> 0)
1742 //TRANSLATORS: Here we are trying to advise what source file
1743 //TRANSLATORS: to attempt.
1744 advice
<< _(" (try ");
1746 advice
<< ":" << lo_try
;
1747 if (lo_try
> 0 && hi_try
> 0)
1748 advice
<< _(" or ");
1750 advice
<< ":" << hi_try
;
1753 throw SEMANTIC_ERROR (advice
.str());
1756 static base_func_info_map_t
1757 get_funcs_in_srcfile(base_func_info_map_t
& funcs
,
1758 const char * srcfile
)
1760 base_func_info_map_t matching_funcs
;
1761 for (base_func_info_map_t::iterator func
= funcs
.begin();
1762 func
!= funcs
.end(); ++func
)
1763 if (strcmp(srcfile
, func
->decl_file
) == 0)
1764 matching_funcs
.push_back(*func
);
1765 return matching_funcs
;
1769 dwflpp::iterate_over_srcfile_lines
<void>(char const * srcfile
,
1771 enum lineno_t lineno_type
,
1772 base_func_info_map_t
& funcs
,
1773 void (* callback
) (Dwarf_Addr
,
1777 /* Matching line records (LRs) to user-provided linenos is trickier than it
1778 * seems. The fate of all functions is one of three possibilities:
1779 * 1. it's a normal function, with a subprogram DIE and a bona fide lowpc
1780 * and highpc attribute.
1781 * 2. it's an inlined function (one/multiple inlined_subroutine DIE, with one
1782 * abstract_origin DIE)
1783 * 3. it's both a normal function and an inlined function. For example, if
1784 * the funtion has been inlined only in some places, we'll have a DIE for
1785 * the normal subprogram DIE as well as inlined_subroutine DIEs.
1787 * Multiple LRs for the same lineno but different addresses can simply happen
1788 * due to the function appearing in multiple forms. E.g. a function inlined
1789 * in two spots can yield two sets of LRs for its linenos at the different
1790 * addresses where it is inlined.
1791 * This is why the collect_* functions used here try to match up LRs back
1792 * to their originating DIEs. For example, in the function
1793 * collect_lines_for_single_lineno(), we filter first by DIE so that a lineno
1794 * corresponding to multiple addrs in multiple inlined_subroutine DIEs yields
1795 * a probe for each of them.
1799 // only work on the functions found in the current srcfile
1800 base_func_info_map_t current_funcs
= get_funcs_in_srcfile(funcs
, srcfile
);
1801 if (current_funcs
.empty())
1805 lines_t matching_lines
;
1806 if (lineno_type
== ABSOLUTE
)
1807 collect_lines_for_single_lineno(srcfile
, linenos
[0], false, /* is_relative */
1808 current_funcs
, matching_lines
);
1809 else if (lineno_type
== RELATIVE
)
1810 collect_lines_for_single_lineno(srcfile
, linenos
[0], true, /* is_relative */
1811 current_funcs
, matching_lines
);
1812 else if (lineno_type
== WILDCARD
)
1813 collect_all_lines(srcfile
, current_funcs
, matching_lines
);
1815 for (int lineno
= linenos
[0]; lineno
<= linenos
[1]; lineno
++)
1816 collect_lines_for_single_lineno(srcfile
, lineno
, false, /* is_relative */
1817 current_funcs
, matching_lines
);
1819 // call back with matching lines
1820 if (!matching_lines
.empty())
1822 set
<Dwarf_Addr
> probed_addrs
;
1823 for (lines_t::iterator line
= matching_lines
.begin();
1824 line
!= matching_lines
.end(); ++line
)
1826 int lineno
= DWARF_LINENO(*line
);
1827 Dwarf_Addr addr
= DWARF_LINEADDR(*line
);
1828 bool is_new_addr
= probed_addrs
.insert(addr
).second
;
1830 callback(addr
, lineno
, data
);
1833 // No LRs found at the wanted lineno. So let's suggest other ones if user was
1834 // targeting a specific lineno (ABSOLUTE or RELATIVE).
1835 else if (lineno_type
== ABSOLUTE
||
1836 lineno_type
== RELATIVE
)
1838 int lineno
= linenos
[0];
1839 if (lineno_type
== RELATIVE
)
1840 // just pick the first function and make it relative to that
1841 lineno
+= current_funcs
[0].decl_line
;
1842 suggest_alternative_linenos(srcfile
, lineno
, current_funcs
);
1848 dwflpp::iterate_over_labels
<void>(Dwarf_Die
*begin_die
,
1850 const string
& function
,
1852 void (* callback
)(const string
&,
1862 Dwarf_Die die
, import
;
1864 int res
= dwarf_child (begin_die
, &die
);
1866 return; // die without children, bail out.
1870 switch (dwarf_tag(&die
))
1873 name
= dwarf_diename (&die
);
1876 || (name_has_wildcard(sym
)
1877 && function_name_matches_pattern (name
, sym
))))
1879 // Don't try to be smart. Just drop no addr labels.
1880 Dwarf_Addr stmt_addr
;
1881 if (dwarf_lowpc (&die
, &stmt_addr
) == 0)
1883 // Get the file/line number for this label
1885 const char *file
= dwarf_decl_file (&die
) ?: "<unknown source>";
1886 dwarf_decl_line (&die
, &dline
);
1888 vector
<Dwarf_Die
> scopes
= getscopes_die(&die
);
1889 if (scopes
.size() > 1)
1892 if (!inner_die_containing_pc(scopes
[1], stmt_addr
, scope
))
1894 sess
.print_warning(_F("label '%s' at address %s (dieoffset: %s) is not "
1895 "contained by its scope '%s' (dieoffset: %s) -- bad"
1896 " debuginfo? [man error::dwarf]", name
, lex_cast_hex(stmt_addr
).c_str(),
1897 lex_cast_hex(dwarf_dieoffset(&die
)).c_str(),
1898 (dwarf_diename(&scope
) ?: "<unknown>"),
1899 lex_cast_hex(dwarf_dieoffset(&scope
)).c_str()));
1901 callback(function
, name
, file
, dline
,
1902 &scope
, stmt_addr
, data
);
1908 case DW_TAG_subprogram
:
1909 case DW_TAG_inlined_subroutine
:
1910 // Stay within our filtered function
1913 case DW_TAG_imported_unit
:
1914 // Iterate over the children of the imported unit as if they
1915 // were inserted in place.
1916 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
1917 iterate_over_labels (&import
, sym
, function
, data
, callback
);
1921 if (dwarf_haschildren (&die
))
1922 iterate_over_labels (&die
, sym
, function
, data
, callback
);
1926 while (dwarf_siblingof (&die
, &die
) == 0);
1929 // Mini 'query-like' struct to help us navigate callbacks during
1930 // external function resolution
1931 struct external_function_query
{
1937 external_function_query(dwflpp
* dw
, const string
& name
):
1938 dw(dw
), name(name
), resolved(false) {}
1942 dwflpp::external_function_cu_callback (Dwarf_Die
* cu
, external_function_query
*efq
)
1944 efq
->dw
->focus_on_cu(cu
);
1945 return efq
->dw
->iterate_over_functions(external_function_func_callback
,
1950 dwflpp::external_function_func_callback (Dwarf_Die
* func
, external_function_query
*efq
)
1952 Dwarf_Attribute external
;
1953 Dwarf_Addr func_addr
;
1954 if (dwarf_attr_integrate(func
, DW_AT_external
, &external
) != NULL
&&
1955 dwarf_lowpc(func
, &func_addr
) == 0)
1958 efq
->addr
= func_addr
;
1959 efq
->resolved
= true;
1960 return DWARF_CB_ABORT
; // we found it so stop here
1966 dwflpp::iterate_over_callees
<void>(Dwarf_Die
*begin_die
,
1968 long recursion_depth
,
1970 void (* callback
)(base_func_info
&,
1974 base_func_info
& caller
,
1975 stack
<Dwarf_Addr
> *callers
)
1979 Dwarf_Die die
, import
;
1981 // DIE of abstract_origin found in die
1984 // callee's entry pc (= where we'll probe)
1985 Dwarf_Addr func_addr
;
1987 // caller's unwind pc during call (to match against bt for filtering)
1988 Dwarf_Addr caller_uw_addr
;
1990 Dwarf_Attribute attr
;
1992 base_func_info callee
;
1993 if (dwarf_child(begin_die
, &die
) != 0)
1994 return; // die without children, bail out.
1996 bool free_callers
= false;
1997 if (callers
== NULL
) /* first call */
1999 callers
= new stack
<Dwarf_Addr
>();
2000 free_callers
= true;
2005 bool inlined
= false;
2006 switch (dwarf_tag(&die
))
2008 case DW_TAG_inlined_subroutine
:
2010 case DW_TAG_GNU_call_site
:
2011 callee
.name
= dwarf_diename(&die
) ?: "";
2012 if (callee
.name
.empty())
2014 if (callee
.name
!= sym
)
2016 if (!name_has_wildcard(sym
))
2018 if (!function_name_matches_pattern(callee
.name
, sym
))
2022 /* In both cases (call sites and inlines), we want the
2023 * abstract_origin. The difference is that in inlines, the addr is
2024 * in the die itself, whereas for call sites, the addr is in the
2025 * abstract_origin's die.
2026 * Note that in the case of inlines, we're only calling back
2027 * for that inline instance, not all. This is what we want, since
2028 * it will only be triggered when 'called' from the target func,
2029 * which is something we have to emulate for non-inlined funcs
2030 * (which is the purpose of the caller_uw_addr below) */
2031 if (dwarf_attr_die(&die
, DW_AT_abstract_origin
, &origin
) == NULL
)
2034 // the low_pc of the die in either cases is the pc that would
2035 // show up in a backtrace (inlines are a special case in which
2036 // the die's low_pc is also the abstract_origin's low_pc = the
2037 // 'start' of the inline instance)
2038 if (dwarf_lowpc(&die
, &caller_uw_addr
) != 0)
2042 func_addr
= caller_uw_addr
;
2043 else if (dwarf_lowpc(&origin
, &func_addr
) != 0)
2045 // function doesn't have a low_pc, is it external?
2046 if (dwarf_attr_integrate(&origin
, DW_AT_external
,
2049 // let's iterate over the CUs and find it. NB: it's
2050 // possible we could have also done this by creating a
2051 // probe point with .exported tacked on and rerunning it
2052 // through derive_probe(). But since we're already on the
2053 // dwflpp side of things, and we already have access to
2054 // everything we need, let's try to be self-sufficient.
2056 // remember old focus
2057 Dwarf_Die
*old_cu
= cu
;
2059 external_function_query
efq(this, dwarf_linkage_name(&origin
) ?: callee
.name
);
2060 iterate_over_cus(external_function_cu_callback
, &efq
, false);
2065 if (!efq
.resolved
) // did we resolve it?
2068 func_addr
= efq
.addr
;
2071 // non-external function without low_pc, jump ship
2075 // We now have the addr to probe in func_addr, and the DIE
2076 // from which to obtain file/line info in origin
2078 // Get the file/line number for this callee
2079 callee
.decl_file
= dwarf_decl_file (&origin
) ?: "<unknown source>";
2080 dwarf_decl_line (&origin
, &callee
.decl_line
);
2082 // add as a caller to match against
2084 callers
->push(caller_uw_addr
);
2086 callee
.die
= inlined
? die
: origin
;
2087 callee
.entrypc
= func_addr
;
2088 callback(callee
, caller
, callers
, data
);
2090 // If it's a tail call, print a warning that it may not be caught
2092 && dwarf_attr_integrate(&die
, DW_AT_GNU_tail_call
, &attr
) != NULL
)
2093 sess
.print_warning (_F("Callee \"%s\" in function \"%s\" is a tail call: "
2094 ".callee probe may not fire. Try placing the probe "
2095 "directly on the callee function instead.",
2096 callee
.name
.c_str(), caller
.name
.c_str()));
2098 // For .callees(N) probes, we recurse on this callee. Note that we
2099 // pass the callee we just found as the caller arg for this recursion,
2100 // since it (the callee we just found) will be the caller of whatever
2101 // callees found inside this recursion.
2102 if (recursion_depth
> 1)
2103 iterate_over_callees(inlined
? &die
: &origin
,
2104 sym
, recursion_depth
-1, data
,
2105 callback
, callee
, callers
);
2111 case DW_TAG_subprogram
:
2112 break; // don't leave our filtered func
2114 case DW_TAG_imported_unit
:
2115 // Iterate over the children of the imported unit as if they
2116 // were inserted in place.
2117 if (dwarf_attr_die(&die
, DW_AT_import
, &import
))
2118 // NB: we pass the same caller arg into it
2119 iterate_over_callees (&import
, sym
, recursion_depth
, data
,
2120 callback
, caller
, callers
);
2124 if (dwarf_haschildren (&die
))
2125 // NB: we pass the same caller arg into it
2126 iterate_over_callees (&die
, sym
, recursion_depth
, data
,
2127 callback
, caller
, callers
);
2131 while (dwarf_siblingof (&die
, &die
) == 0);
2133 if (free_callers
&& callers
!= NULL
)
2139 dwflpp::collect_srcfiles_matching (string
const & pattern
,
2140 set
<string
> & filtered_srcfiles
)
2146 Dwarf_Files
*srcfiles
;
2148 // PR 5049: implicit * in front of given path pattern.
2149 // NB: fnmatch() is used without FNM_PATHNAME.
2150 string prefixed_pattern
= string("*/") + pattern
;
2152 DWARF_ASSERT ("dwarf_getsrcfiles",
2153 dwarf_getsrcfiles (cu
, &srcfiles
, &nfiles
));
2155 for (size_t i
= 0; i
< nfiles
; ++i
)
2157 char const * fname
= dwarf_filesrc (srcfiles
, i
, NULL
, NULL
);
2158 if (fnmatch (pattern
.c_str(), fname
, 0) == 0 ||
2159 fnmatch (prefixed_pattern
.c_str(), fname
, 0) == 0)
2161 filtered_srcfiles
.insert (fname
);
2163 clog
<< _F("selected source file '%s'\n", fname
);
2171 dwflpp::resolve_prologue_endings (func_info_map_t
& funcs
)
2173 // When a program is compiled with no optimization, GCC does no variable
2174 // tracking, which means that location info is actually only really valid
2175 // after the prologue, even though GCC reports it as valid during. So we need
2176 // to find the prologue ends to get accurate info. This may or may not be the
2177 // first address that has a source line distinct from the function
2184 Dwarf_Lines
*lines
= NULL
;
2187 malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue
2188 sys_get?id in kernel/timer.c no-prologue
2189 sys_exit_group tail-recursive
2190 {do_,}sys_open extra-long-prologue (gcc 3.4)
2191 cpu_to_logical_apicid NULL-decl_file
2194 // Fetch all srcline records, sorted by address. No need to free lines, it's a
2195 // direct pointer to the CU's cached lines.
2196 DWARF_ASSERT ("dwarf_getsrclines",
2197 dwarf_getsrclines(cu
, &lines
, &nlines
));
2199 // We normally ignore a function's decl_line, since it is associated with the
2200 // line at which the identifier appears in the declaration, and has no
2201 // meaningful relation to the lineno associated with the entrypc (which is
2202 // normally the lineno of '{', which could occur at the same line as the
2203 // declaration, or lower down).
2204 // However, if the CU was compiled using GCC < 4.4, then the decl_line
2205 // actually represents the lineno of '{' as well, in which case if the lineno
2206 // associated with the entrypc is != to the decl_line, it means the compiler
2207 // scraped/optimized off some of the beginning of the function and the safest
2208 // thing we can do is consider it naked.
2209 bool consider_decl_line
= false;
2212 if (is_gcc_producer(cu
, prod
, vers
)
2213 && strverscmp(vers
.c_str(), "4.4.0") < 0)
2214 consider_decl_line
= true;
2217 for(func_info_map_t::iterator it
= funcs
.begin(); it
!= funcs
.end(); it
++)
2220 Dwarf_Addr
* bkpts
= 0;
2221 int n
= dwarf_entry_breakpoints (& it
->die
, & bkpts
);
2226 Dwarf_Addr entrypc
= it
->entrypc
;
2227 Dwarf_Addr highpc
; // NB: highpc is exclusive: [entrypc,highpc)
2228 DWFL_ASSERT ("dwarf_highpc", dwarf_highpc (& it
->die
,
2231 if (it
->decl_file
== 0) it
->decl_file
= "";
2233 unsigned entrypc_srcline_idx
= 0;
2234 Dwarf_Line
*entrypc_srcline
= NULL
;
2235 // open-code binary search for exact match
2237 unsigned l
= 0, h
= nlines
;
2240 entrypc_srcline_idx
= (l
+ h
) / 2;
2241 Dwarf_Line
* lr
= dwarf_onesrcline(lines
, entrypc_srcline_idx
);
2242 Dwarf_Addr addr
= DWARF_LINEADDR(lr
);
2243 if (addr
== entrypc
) { entrypc_srcline
= lr
; break; }
2244 else if (l
+ 1 == h
) { break; } // ran off bottom of tree
2245 else if (addr
< entrypc
) { l
= entrypc_srcline_idx
; }
2246 else { h
= entrypc_srcline_idx
; }
2249 if (!entrypc_srcline
)
2251 if (sess
.verbose
> 2)
2252 clog
<< _F("missing entrypc dwarf line record for function '%s'\n",
2254 // This is probably an inlined function. We'll end up using
2255 // its lowpc as a probe address.
2261 if (sess
.verbose
> 2)
2262 clog
<< _F("null entrypc dwarf line record for function '%s'\n",
2264 // This is probably an inlined function. We'll skip this instance;
2270 clog
<< _F("searching for prologue of function '%s' %#" PRIx64
"-%#" PRIx64
2271 "@%s:%d\n", it
->name
.c_str(), entrypc
, highpc
, it
->decl_file
,
2274 // For each function, we look for the prologue-end marker (e.g. clang
2275 // outputs one). If there is no explicit marker (e.g. GCC does not), we
2276 // accept a bigger or equal lineno as a prologue end (this catches GCC's
2277 // 0-line advances).
2279 // We may have to skip a few because some old compilers plop
2280 // in dummy line records for longer prologues. If we go too
2281 // far (addr >= highpc), we take the previous one. Or, it may
2282 // be the first one, if the function had no prologue, and thus
2283 // the entrypc maps to a statement in the body rather than the
2286 int entrypc_srcline_lineno
= DWARF_LINENO(entrypc_srcline
);
2287 unsigned postprologue_srcline_idx
= entrypc_srcline_idx
;
2288 Dwarf_Line
*postprologue_srcline
= entrypc_srcline
;
2290 while (postprologue_srcline_idx
< nlines
)
2292 postprologue_srcline
= dwarf_onesrcline(lines
,
2293 postprologue_srcline_idx
);
2294 Dwarf_Addr lineaddr
= DWARF_LINEADDR(postprologue_srcline
);
2295 const char* linesrc
= DWARF_LINESRC(postprologue_srcline
);
2296 int lineno
= DWARF_LINENO(postprologue_srcline
);
2297 bool lineprologue_end
= DWARF_LINEPROLOGUEEND(postprologue_srcline
);
2300 clog
<< _F("checking line record %#" PRIx64
"@%s:%d%s\n", lineaddr
,
2301 linesrc
, lineno
, lineprologue_end
? " (marked)" : "");
2303 // have we passed the function?
2304 if (lineaddr
>= highpc
)
2306 // is there an explicit prologue_end marker?
2307 if (lineprologue_end
)
2309 // is it a different file?
2310 if (strcmp(linesrc
, it
->decl_file
))
2312 // OK, it's the same file, but is it a different line?
2313 if (lineno
!= entrypc_srcline_lineno
)
2315 // Same file and line, is this a second line record (e.g. 0-line advance)?
2316 if (postprologue_srcline_idx
!= entrypc_srcline_idx
)
2318 // This is the first iteration. Is decl_line meaningful and is the
2319 // lineno past the decl_line?
2320 if (consider_decl_line
&& lineno
!= it
->decl_line
)
2323 // Let's try the next srcline.
2324 postprologue_srcline_idx
++;
2326 } // loop over srclines
2329 Dwarf_Addr postprologue_addr
= DWARF_LINEADDR(postprologue_srcline
);
2330 if (postprologue_addr
>= highpc
)
2332 // pick addr of previous line record
2333 Dwarf_Line
*lr
= dwarf_onesrcline(lines
, postprologue_srcline_idx
-1);
2334 postprologue_addr
= DWARF_LINEADDR(lr
);
2337 it
->prologue_end
= postprologue_addr
;
2341 clog
<< _F("prologue found function '%s'", it
->name
.c_str());
2342 // Add a little classification datum
2343 //TRANSLATORS: Here we're adding some classification datum (ie Prologue Free)
2344 if (postprologue_addr
== entrypc
)
2345 clog
<< _(" (naked)");
2346 //TRANSLATORS: Here we're adding some classification datum (ie we went over)
2347 if (DWARF_LINEADDR(postprologue_srcline
) >= highpc
)
2348 clog
<< _(" (tail-call?)");
2349 //TRANSLATORS: Here we're adding some classification datum (ie it was marked)
2350 if (DWARF_LINEPROLOGUEEND(postprologue_srcline
))
2351 clog
<< _(" (marked)");
2353 clog
<< " = 0x" << hex
<< postprologue_addr
<< dec
<< "\n";
2356 } // loop over functions
2361 dwflpp::function_entrypc (Dwarf_Addr
* addr
)
2364 // PR10574: reject 0, which tends to be eliminated COMDAT
2365 return (dwarf_entrypc (function
, addr
) == 0 && *addr
!= 0);
2370 dwflpp::die_entrypc (Dwarf_Die
* die
, Dwarf_Addr
* addr
)
2373 string lookup_method
;
2377 lookup_method
= "dwarf_entrypc";
2378 rc
= dwarf_entrypc (die
, addr
);
2382 lookup_method
= "dwarf_ranges";
2387 ptrdiff_t offset
= dwarf_ranges (die
, 0, &base
, &begin
, &end
);
2388 if (offset
< 0) rc
= -1;
2389 else if (offset
> 0)
2394 // Now we need to check that there are no more ranges
2395 // associated with this function, which could conceivably
2396 // happen if a function is inlined, then pieces of it are
2397 // split amongst different conditional branches. It's not
2398 // obvious which of them to favour. As a heuristic, we
2399 // pick the beginning of the first range, and ignore the
2400 // others (but with a warning).
2403 while ((offset
= dwarf_ranges (die
, offset
, &base
, &begin
, &end
)) > 0)
2406 lookup_method
+= _F(", ignored %s more", lex_cast(extra
).c_str());
2410 // PR10574: reject subprograms where the entrypc address turns out
2411 // to be 0, since they tend to correspond to duplicate-eliminated
2412 // COMDAT copies of C++ functions.
2413 if (rc
== 0 && *addr
== 0)
2415 lookup_method
+= _(" (skip comdat)");
2419 if (sess
.verbose
> 2)
2420 clog
<< _F("entry-pc lookup (%s dieoffset: %s) = %#" PRIx64
" (rc %d)", lookup_method
.c_str(),
2421 lex_cast_hex(dwarf_dieoffset(die
)).c_str(), *addr
, rc
) << endl
;
2428 dwflpp::function_die (Dwarf_Die
*d
)
2436 dwflpp::function_file (char const ** c
)
2440 *c
= dwarf_decl_file (function
) ?: "<unknown source>";
2445 dwflpp::function_line (int *linep
)
2448 dwarf_decl_line (function
, linep
);
2453 dwflpp::die_has_pc (Dwarf_Die
& die
, Dwarf_Addr pc
)
2455 int res
= dwarf_haspc (&die
, pc
);
2456 // dwarf_ranges will return -1 if a function die has no DW_AT_ranges
2458 // DWARF_ASSERT ("dwarf_haspc", res);
2464 dwflpp::inner_die_containing_pc(Dwarf_Die
& scope
, Dwarf_Addr addr
,
2469 // Sometimes we're in a bad scope to begin with -- just let it be. This can
2470 // happen for example if the compiler outputs a label PC that's just outside
2471 // the lexical scope. We can't really do anything about that, but variables
2472 // will probably not be accessible in this case.
2473 if (!die_has_pc(scope
, addr
))
2476 Dwarf_Die child
, import
;
2477 int rc
= dwarf_child(&result
, &child
);
2480 switch (dwarf_tag (&child
))
2482 case DW_TAG_imported_unit
:
2483 // The children of the imported unit need to be treated as if
2484 // they are inserted here. So look inside and set result if
2486 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2488 Dwarf_Die import_result
;
2489 if (inner_die_containing_pc(import
, addr
, import_result
))
2491 result
= import_result
;
2497 // lexical tags to recurse within the same starting scope
2498 // NB: this intentionally doesn't cross into inlines!
2499 case DW_TAG_lexical_block
:
2500 case DW_TAG_with_stmt
:
2501 case DW_TAG_catch_block
:
2502 case DW_TAG_try_block
:
2503 case DW_TAG_entry_point
:
2504 if (die_has_pc(child
, addr
))
2507 rc
= dwarf_child(&result
, &child
);
2511 rc
= dwarf_siblingof(&child
, &child
);
2518 dwflpp::loc2c_error (void *, const char *fmt
, ...)
2520 const char *msg
= "?";
2525 rc
= vasprintf (& tmp
, fmt
, ap
);
2531 throw SEMANTIC_ERROR (msg
);
2535 // This function generates code used for addressing computations of
2536 // target variables.
2538 dwflpp::emit_address (struct obstack
*pool
, Dwarf_Addr address
)
2540 int n
= dwfl_module_relocations (module
);
2541 DWFL_ASSERT ("dwfl_module_relocations", n
>= 0);
2542 Dwarf_Addr reloc_address
= address
;
2543 const char *secname
= "";
2546 int i
= dwfl_module_relocate_address (module
, &reloc_address
);
2547 DWFL_ASSERT ("dwfl_module_relocate_address", i
>= 0);
2548 secname
= dwfl_module_relocation_info (module
, i
, NULL
);
2551 if (sess
.verbose
> 2)
2553 clog
<< _F("emit dwarf addr %#" PRIx64
" => module %s section %s relocaddr %#" PRIx64
,
2554 address
, module_name
.c_str (), (secname
?: "null"),
2555 reloc_address
) << endl
;
2558 if (n
> 0 && !(n
== 1 && secname
== NULL
))
2560 DWFL_ASSERT ("dwfl_module_relocation_info", secname
);
2561 if (n
> 1 || secname
[0] != '\0')
2563 // This gives us the module name, and section name within the
2564 // module, for a kernel module (or other ET_REL module object).
2565 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2566 obstack_printf (pool
, "addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2567 module_name
.c_str(), secname
, reloc_address
);
2568 obstack_printf (pool
, "addr; })");
2570 else if (n
== 1 && module_name
== TOK_KERNEL
&& secname
[0] == '\0')
2572 // elfutils' way of telling us that this is a relocatable kernel address, which we
2573 // need to treat the same way here as dwarf_query::add_probe_point does: _stext.
2574 address
-= sess
.sym_stext
;
2576 // Note we "cache" the result here through a static because the
2577 // kernel will never move after being loaded (unlike modules and
2578 // user-space dynamic share libraries).
2579 obstack_printf (pool
, "({ static unsigned long addr = 0; ");
2580 obstack_printf (pool
, "if (addr==0) addr = _stp_kmodule_relocate (\"%s\",\"%s\",%#" PRIx64
"); ",
2581 module_name
.c_str(), secname
, address
); // PR10000 NB: not reloc_address
2582 obstack_printf (pool
, "addr; })");
2586 obstack_printf (pool
, "/* pragma:vma */");
2587 obstack_printf (pool
, "({ unsigned long addr = 0; ");
2588 obstack_printf (pool
, "addr = _stp_umodule_relocate (\"%s\",%#" PRIx64
", current); ",
2589 resolve_path(module_name
.c_str()).c_str(), address
);
2590 obstack_printf (pool
, "addr; })");
2594 obstack_printf (pool
, "%#" PRIx64
"UL", address
); // assume as constant
2599 dwflpp::loc2c_emit_address (void *arg
, struct obstack
*pool
,
2602 static_cast<dwflpp
*>(arg
)->emit_address (pool
, address
);
2607 dwflpp::get_locals(vector
<Dwarf_Die
>& scopes
, set
<string
>& locals
)
2609 // XXX Shouldn't this be walking up to outer scopes too?
2611 get_locals_die(scopes
[0], locals
);
2615 dwflpp::get_locals_die(Dwarf_Die
& die
, set
<string
>& locals
)
2617 // Try to get the first child of die.
2618 Dwarf_Die child
, import
;
2619 if (dwarf_child (&die
, &child
) == 0)
2624 // Output each sibling's name (that is a variable or
2625 // parameter) to 'o'.
2626 switch (dwarf_tag (&child
))
2628 case DW_TAG_variable
:
2629 case DW_TAG_formal_parameter
:
2630 name
= dwarf_diename (&child
);
2632 locals
.insert(string("$") + name
);
2634 case DW_TAG_imported_unit
:
2635 // Treat the imported unit children as if they are
2636 // children of the given DIE.
2637 if (dwarf_attr_die(&child
, DW_AT_import
, &import
))
2638 get_locals_die (import
, locals
);
2644 while (dwarf_siblingof (&child
, &child
) == 0);
2650 dwflpp::find_variable_and_frame_base (vector
<Dwarf_Die
>& scopes
,
2652 string
const & local
,
2653 const target_symbol
*e
,
2655 Dwarf_Attribute
*fb_attr_mem
)
2657 Dwarf_Die
*scope_die
= &scopes
[0];
2658 Dwarf_Attribute
*fb_attr
= NULL
;
2662 int declaring_scope
= dwarf_getscopevar (&scopes
[0], scopes
.size(),
2666 if (declaring_scope
< 0)
2669 get_locals(scopes
, locals
);
2670 string sugs
= levenshtein_suggest(local
, locals
); // probably not that many, so no limit
2672 throw SEMANTIC_ERROR (_F("unable to find local '%s', [man error::dwarf] dieoffset %s in %s, near pc %s %s %s %s (%s)",
2674 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2675 module_name
.c_str(),
2676 lex_cast_hex(pc
).c_str(),
2677 (scope_die
== NULL
) ? "" : _("in"),
2678 (dwarf_diename(scope_die
) ?: "<unknown>"),
2679 (dwarf_diename(cu
) ?: "<unknown>"),
2681 ? (_("<no alternatives>"))
2682 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2685 throw SEMANTIC_ERROR (_F("unable to find global '%s', [man error::dwarf] dieoffset %s in %s, %s %s %s (%s)",
2687 lex_cast_hex(dwarf_dieoffset(scope_die
)).c_str(),
2688 module_name
.c_str(),
2689 (scope_die
== NULL
) ? "" : _("in"),
2690 (dwarf_diename(scope_die
) ?: "<unknown>"),
2693 ? (_("<no alternatives>"))
2694 : (_("alternatives: ") + sugs
+ ")")).c_str()),
2698 /* Some GCC versions would output duplicate external variables, one
2699 without a location attribute. If so, try to find the other if it
2700 exists in the same scope. See GCC PR51410. */
2701 Dwarf_Attribute attr_mem
;
2702 if (dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
) == NULL
2703 && dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
) == NULL
2704 && dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
) != NULL
2705 && dwarf_tag(&scopes
[declaring_scope
]) == DW_TAG_compile_unit
)
2707 Dwarf_Die orig_vardie
= *vardie
;
2708 bool alt_found
= false;
2709 if (dwarf_child(&scopes
[declaring_scope
], vardie
) == 0)
2712 // Note, not handling DW_TAG_imported_unit, assuming GCC
2713 // version is recent enough to not need this workaround if
2714 // we would see an imported unit.
2715 if (dwarf_tag (vardie
) == DW_TAG_variable
2716 && strcmp (dwarf_diename (vardie
), local
.c_str ()) == 0
2717 && (dwarf_attr_integrate (vardie
, DW_AT_external
, &attr_mem
)
2719 && ((dwarf_attr_integrate (vardie
, DW_AT_const_value
, &attr_mem
)
2721 || (dwarf_attr_integrate (vardie
, DW_AT_location
, &attr_mem
)
2725 while (!alt_found
&& dwarf_siblingof(vardie
, vardie
) == 0);
2728 *vardie
= orig_vardie
;
2731 // Global vars don't need (cannot use) frame base in location descriptor.
2735 /* We start out walking the "lexical scopes" as returned by
2736 * as returned by dwarf_getscopes for the address, starting with the
2737 * declaring_scope that the variable was found in.
2739 vector
<Dwarf_Die
> physcopes
, *fbscopes
= &scopes
;
2740 for (size_t inner
= declaring_scope
;
2741 inner
< fbscopes
->size() && fb_attr
== NULL
;
2744 Dwarf_Die
& scope
= (*fbscopes
)[inner
];
2745 switch (dwarf_tag (&scope
))
2749 case DW_TAG_subprogram
:
2750 case DW_TAG_entry_point
:
2751 fb_attr
= dwarf_attr_integrate (&scope
,
2755 case DW_TAG_inlined_subroutine
:
2756 /* Unless we already are going through the "pyshical die tree",
2757 * we now need to start walking the die tree where this
2758 * subroutine is inlined to find the appropriate frame base. */
2759 if (declaring_scope
!= -1)
2761 physcopes
= getscopes_die(&scope
);
2762 if (physcopes
.empty())
2763 throw SEMANTIC_ERROR (_F("unable to get die scopes for '%s' in an inlined subroutine",
2764 local
.c_str()), e
->tok
);
2765 fbscopes
= &physcopes
;
2766 inner
= 0; // zero is current scope, for look will increase.
2767 declaring_scope
= -1;
2778 dwflpp::translate_location(struct obstack
*pool
,
2779 Dwarf_Attribute
*attr
, Dwarf_Die
*die
,
2781 Dwarf_Attribute
*fb_attr
,
2782 struct location
**tail
,
2783 const target_symbol
*e
)
2786 /* DW_AT_data_member_location, can be either constant offsets
2787 (struct member fields), or full blown location expressions. */
2789 /* There is no location expression, but a constant value instead. */
2790 if (dwarf_whatattr (attr
) == DW_AT_const_value
)
2792 *tail
= c_translate_constant (pool
, &loc2c_error
, this,
2793 &loc2c_emit_address
, 0, pc
, attr
);
2800 /* PR9768: formerly, we added pc+module_bias here. However, that bias value
2801 is not present in the pc value by the time we get it, so adding it would
2802 result in false negatives of variable reachibility. In other instances
2803 further below, the c_translate_FOO functions, the module_bias value used
2804 to be passed in, but instead should now be zero for the same reason. */
2807 switch (dwarf_getlocation_addr (attr
, pc
/*+ module_bias*/, &expr
, &len
, 1))
2809 case 1: /* Should always happen. */
2814 case 0: /* Shouldn't happen.... but can, e.g. due to PR15123. */
2816 Dwarf_Addr pc2
= pr15123_retry_addr (pc
, die
);
2824 throw SEMANTIC_ERROR (_F("not accessible at this address [man error::dwarf] (%s, dieoffset: %s)",
2825 lex_cast_hex(pc
).c_str(), lex_cast_hex(dwarf_dieoffset(die
)).c_str()),
2828 default: /* Shouldn't happen. */
2830 throw SEMANTIC_ERROR (_F("dwarf_getlocation_addr failed [man error::dwarf] , %s", dwarf_errmsg(-1)), e
->tok
);
2834 // pc is in the dw address space of the current module, which is what
2835 // c_translate_location expects. get_cfa_ops wants the global dwfl address.
2836 // cfa_ops only make sense for locals.
2839 Dwarf_Addr addr
= pc
+ module_bias
;
2840 cfa_ops
= get_cfa_ops (addr
);
2845 return c_translate_location (pool
, &loc2c_error
, this,
2846 &loc2c_emit_address
,
2848 pc
, attr
, expr
, len
, tail
, fb_attr
, cfa_ops
);
2853 dwflpp::get_members(Dwarf_Die
*typedie
, set
<string
>& members
, set
<string
> &dupes
)
2855 const int typetag
= dwarf_tag (typedie
);
2857 /* compile and partial unit included for recursion through
2858 imported_unit below. */
2859 if (typetag
!= DW_TAG_structure_type
&&
2860 typetag
!= DW_TAG_class_type
&&
2861 typetag
!= DW_TAG_union_type
&&
2862 typetag
!= DW_TAG_compile_unit
&&
2863 typetag
!= DW_TAG_partial_unit
)
2865 throw SEMANTIC_ERROR(_F("Type %s isn't a struct/class/union",
2866 dwarf_type_name(typedie
).c_str()));
2869 // Try to get the first child of vardie.
2870 Dwarf_Die die_mem
, import
;
2871 Dwarf_Die
*die
= &die_mem
;
2872 switch (dwarf_child (typedie
, die
))
2874 case 1: // No children.
2875 throw SEMANTIC_ERROR(_F("Type %s is empty", dwarf_type_name(typedie
).c_str()));
2878 default: // Shouldn't happen.
2879 throw SEMANTIC_ERROR(_F("Type %s: %s", dwarf_type_name(typedie
).c_str(),
2886 // Add each sibling's name to members set
2889 int tag
= dwarf_tag(die
);
2891 /* The children of an imported_unit should be treated as members too. */
2892 if (tag
== DW_TAG_imported_unit
2893 && dwarf_attr_die(die
, DW_AT_import
, &import
))
2894 get_members(&import
, members
, dupes
);
2896 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
2899 const char *member
= dwarf_diename (die
) ;
2901 if ( tag
== DW_TAG_member
&& member
!= NULL
)
2903 // Only output if this is new, to avoid inheritance dupes.
2904 if (dupes
.insert(member
).second
)
2905 members
.insert(member
);
2910 if (!dwarf_attr_die (die
, DW_AT_type
, &temp_die
))
2912 string source
= dwarf_decl_file(die
) ?: "<unknown source>";
2914 dwarf_decl_line(die
, &line
);
2915 throw SEMANTIC_ERROR(_F("Couldn't obtain type attribute for anonymous "
2916 "member at %s:%d", source
.c_str(), line
));
2919 get_members(&temp_die
, members
, dupes
);
2923 while (dwarf_siblingof (die
, die
) == 0);
2928 dwflpp::find_struct_member(const target_symbol::component
& c
,
2929 Dwarf_Die
*parentdie
,
2930 Dwarf_Die
*memberdie
,
2931 vector
<Dwarf_Die
>& dies
,
2932 vector
<Dwarf_Attribute
>& locs
)
2934 Dwarf_Attribute attr
;
2937 /* With inheritance, a subclass may mask member names of parent classes, so
2938 * our search among the inheritance tree must be breadth-first rather than
2939 * depth-first (recursive). The parentdie is still our starting point. */
2940 deque
<Dwarf_Die
> inheritees(1, *parentdie
);
2941 for (; !inheritees
.empty(); inheritees
.pop_front())
2943 switch (dwarf_child (&inheritees
.front(), &die
))
2945 case 0: /* First child found. */
2947 case 1: /* No children. */
2949 case -1: /* Error. */
2950 default: /* Shouldn't happen */
2951 throw SEMANTIC_ERROR (dwarf_type_name(&inheritees
.front()) + ": "
2952 + string (dwarf_errmsg (-1)),
2958 int tag
= dwarf_tag(&die
);
2959 /* recurse into imported units as if they are anonymoust structs */
2961 if (tag
== DW_TAG_imported_unit
2962 && dwarf_attr_die(&die
, DW_AT_import
, &import
)
2963 && find_struct_member(c
, &import
, memberdie
, dies
, locs
))
2966 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
2969 const char *name
= dwarf_diename(&die
);
2970 if (tag
== DW_TAG_inheritance
)
2972 /* Remember inheritee for breadth-first search. */
2973 Dwarf_Die inheritee
;
2974 if (dwarf_attr_die (&die
, DW_AT_type
, &inheritee
))
2975 inheritees
.push_back(inheritee
);
2977 else if (name
== NULL
)
2979 /* Need to recurse for anonymous structs/unions. */
2981 if (dwarf_attr_die (&die
, DW_AT_type
, &subdie
) &&
2982 find_struct_member(c
, &subdie
, memberdie
, dies
, locs
))
2985 else if (name
== c
.member
)
2991 while (dwarf_siblingof (&die
, &die
) == 0);
2997 /* As we unwind the recursion, we need to build the chain of
2998 * locations that got to the final answer. */
2999 if (dwarf_attr_integrate (&die
, DW_AT_data_member_location
, &attr
))
3001 dies
.insert(dies
.begin(), die
);
3002 locs
.insert(locs
.begin(), attr
);
3005 /* Union members don't usually have a location,
3006 * but just use the containing union's location. */
3007 else if (dwarf_tag(parentdie
) != DW_TAG_union_type
)
3008 throw SEMANTIC_ERROR (_F("no location for field '%s':%s",
3009 c
.member
.c_str(), dwarf_errmsg(-1)), c
.tok
);
3016 dwarf_die_type (Dwarf_Die
*die
, Dwarf_Die
*typedie_mem
, const token
*tok
=NULL
)
3018 if (!dwarf_attr_die (die
, DW_AT_type
, typedie_mem
))
3019 throw SEMANTIC_ERROR (_F("cannot get type of field: %s", dwarf_errmsg(-1)), tok
);
3024 dwflpp::translate_components(struct obstack
*pool
,
3025 struct location
**tail
,
3027 const target_symbol
*e
,
3033 while (i
< e
->components
.size())
3035 const target_symbol::component
& c
= e
->components
[i
];
3037 /* XXX: This would be desirable, but we don't get the target_symbol token,
3038 and printing that gives us the file:line number too early anyway. */
3040 // Emit a marker to note which field is being access-attempted, to give
3041 // better error messages if deref() fails.
3042 string piece
= string(...target_symbol token
...) + string ("#") + lex_cast(components
[i
].second
);
3043 obstack_printf (pool
, "c->last_stmt = %s;", lex_cast_qstring(piece
).c_str());
3046 switch (dwarf_tag (typedie
))
3048 case DW_TAG_typedef
:
3049 case DW_TAG_const_type
:
3050 case DW_TAG_volatile_type
:
3051 /* Just iterate on the referent type. */
3052 dwarf_die_type (typedie
, typedie
, c
.tok
);
3055 case DW_TAG_reference_type
:
3056 case DW_TAG_rvalue_reference_type
:
3058 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3059 dwarf_die_type (typedie
, typedie
, c
.tok
);
3062 case DW_TAG_pointer_type
:
3063 /* A pointer with no type is a void* -- can't dereference it. */
3064 if (!dwarf_hasattr_integrate (typedie
, DW_AT_type
))
3065 throw SEMANTIC_ERROR (_F("invalid access '%s' vs '%s'", lex_cast(c
).c_str(),
3066 dwarf_type_name(typedie
).c_str()), c
.tok
);
3069 c_translate_pointer (pool
, 1, 0 /* PR9768*/, typedie
, tail
);
3070 if (c
.type
!= target_symbol::comp_literal_array_index
&&
3071 c
.type
!= target_symbol::comp_expression_array_index
)
3073 dwarf_die_type (typedie
, typedie
, c
.tok
);
3076 /* else fall through as an array access */
3078 case DW_TAG_array_type
:
3079 if (c
.type
== target_symbol::comp_literal_array_index
)
3082 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3085 else if (c
.type
== target_symbol::comp_expression_array_index
)
3087 string index
= "STAP_ARG_index" + lex_cast(i
);
3089 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3093 throw SEMANTIC_ERROR (_F("invalid access '%s' for array type",
3094 lex_cast(c
).c_str()), c
.tok
);
3096 dwarf_die_type (typedie
, typedie
, c
.tok
);
3101 case DW_TAG_structure_type
:
3102 case DW_TAG_union_type
:
3103 case DW_TAG_class_type
:
3104 if (c
.type
!= target_symbol::comp_struct_member
)
3105 throw SEMANTIC_ERROR (_F("invalid access '%s' for %s",
3106 lex_cast(c
).c_str(), dwarf_type_name(typedie
).c_str()));
3108 if (dwarf_hasattr(typedie
, DW_AT_declaration
))
3110 Dwarf_Die
*tmpdie
= declaration_resolve(typedie
);
3112 throw SEMANTIC_ERROR (_F("unresolved %s", dwarf_type_name(typedie
).c_str()), c
.tok
);
3117 vector
<Dwarf_Die
> dies
;
3118 vector
<Dwarf_Attribute
> locs
;
3119 if (!find_struct_member(c
, typedie
, vardie
, dies
, locs
))
3121 /* Add a file:line hint for anonymous types */
3123 if (!dwarf_hasattr_integrate(typedie
, DW_AT_name
))
3126 const char *file
= dwarf_decl_file(typedie
);
3127 if (file
&& dwarf_decl_line(typedie
, &line
) == 0)
3128 source
= " (" + string(file
) + ":"
3129 + lex_cast(line
) + ")";
3132 set
<string
> members
, member_dupes
;
3133 get_members(typedie
, members
, member_dupes
);
3134 string sugs
= levenshtein_suggest(c
.member
, members
);
3136 sugs
= " (alternatives: " + sugs
+ ")";
3137 throw SEMANTIC_ERROR(_F("unable to find member '%s' for %s%s%s", c
.member
.c_str(),
3138 dwarf_type_name(typedie
).c_str(), source
.c_str(),
3139 sugs
.c_str()), c
.tok
);
3142 for (unsigned j
= 0; j
< locs
.size(); ++j
)
3144 translate_location (pool
, &locs
[j
], &dies
[j
],
3148 dwarf_die_type (vardie
, typedie
, c
.tok
);
3152 case DW_TAG_enumeration_type
:
3153 case DW_TAG_base_type
:
3154 throw SEMANTIC_ERROR (_F("invalid access '%s' vs. %s", lex_cast(c
).c_str(),
3155 dwarf_type_name(typedie
).c_str()), c
.tok
);
3159 throw SEMANTIC_ERROR (_F("cannot find type: %s", dwarf_errmsg(-1)), c
.tok
);
3163 throw SEMANTIC_ERROR (_F("%s: unexpected type tag %s", dwarf_type_name(typedie
).c_str(),
3164 lex_cast(dwarf_tag(typedie
)).c_str()), c
.tok
);
3172 dwflpp::resolve_unqualified_inner_typedie (Dwarf_Die
*typedie
,
3173 Dwarf_Die
*innerdie
,
3174 const target_symbol
*e
)
3176 int typetag
= dwarf_tag (typedie
);
3177 *innerdie
= *typedie
;
3178 while (typetag
== DW_TAG_typedef
||
3179 typetag
== DW_TAG_const_type
||
3180 typetag
== DW_TAG_volatile_type
)
3182 if (!dwarf_attr_die (innerdie
, DW_AT_type
, innerdie
))
3183 throw SEMANTIC_ERROR (_F("cannot get type of pointee: %s", dwarf_errmsg(-1)), e
->tok
);
3184 typetag
= dwarf_tag (innerdie
);
3190 dwflpp::translate_final_fetch_or_store (struct obstack
*pool
,
3191 struct location
**tail
,
3192 Dwarf_Addr
/*module_bias*/,
3194 Dwarf_Die
*start_typedie
,
3196 const target_symbol
*e
,
3201 /* First boil away any qualifiers associated with the type DIE of
3202 the final location to be accessed. */
3204 Dwarf_Die typedie_mem
, *typedie
= &typedie_mem
;
3205 resolve_unqualified_inner_typedie (start_typedie
, typedie
, e
);
3207 /* If we're looking for an address, then we can just provide what
3208 we computed to this point, without using a fetch/store. */
3212 throw SEMANTIC_ERROR (_("cannot write to member address"), e
->tok
);
3214 if (dwarf_hasattr_integrate (vardie
, DW_AT_bit_offset
))
3215 throw SEMANTIC_ERROR (_("cannot take address of bit-field"), e
->tok
);
3217 c_translate_addressof (pool
, 1, 0, vardie
, typedie
, tail
, "STAP_RETVALUE");
3222 /* Then switch behavior depending on the type of fetch/store we
3223 want, and the type and pointer-ness of the final location. */
3225 int typetag
= dwarf_tag (typedie
);
3229 throw SEMANTIC_ERROR (_F("unsupported type tag %s for %s", lex_cast(typetag
).c_str(),
3230 dwarf_type_name(typedie
).c_str()), e
->tok
);
3233 case DW_TAG_structure_type
:
3234 case DW_TAG_class_type
:
3235 case DW_TAG_union_type
:
3236 throw SEMANTIC_ERROR (_F("'%s' is being accessed instead of a member",
3237 dwarf_type_name(typedie
).c_str()), e
->tok
);
3240 case DW_TAG_base_type
:
3242 // Reject types we can't handle in systemtap
3244 Dwarf_Attribute encoding_attr
;
3245 Dwarf_Word encoding
= (Dwarf_Word
) -1;
3246 dwarf_formudata (dwarf_attr_integrate (typedie
, DW_AT_encoding
, &encoding_attr
),
3248 if (encoding
== (Dwarf_Word
) -1)
3250 // clog << "bad type1 " << encoding << " diestr" << endl;
3251 throw SEMANTIC_ERROR (_F("unsupported type (mystery encoding %s for %s", lex_cast(encoding
).c_str(),
3252 dwarf_type_name(typedie
).c_str()), e
->tok
);
3255 if (encoding
== DW_ATE_float
3256 || encoding
== DW_ATE_complex_float
3257 /* XXX || many others? */)
3259 // clog << "bad type " << encoding << " diestr" << endl;
3260 throw SEMANTIC_ERROR (_F("unsupported type (encoding %s) for %s", lex_cast(encoding
).c_str(),
3261 dwarf_type_name(typedie
).c_str()), e
->tok
);
3264 // Fallthrough. enumeration_types are always scalar.
3265 case DW_TAG_enumeration_type
:
3269 c_translate_store (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3272 c_translate_fetch (pool
, 1, 0 /* PR9768 */, vardie
, typedie
, tail
,
3276 case DW_TAG_array_type
:
3277 case DW_TAG_pointer_type
:
3278 case DW_TAG_reference_type
:
3279 case DW_TAG_rvalue_reference_type
:
3284 if (typetag
== DW_TAG_array_type
)
3285 throw SEMANTIC_ERROR (_("cannot write to array address"), e
->tok
);
3286 if (typetag
== DW_TAG_reference_type
||
3287 typetag
== DW_TAG_rvalue_reference_type
)
3288 throw SEMANTIC_ERROR (_("cannot write to reference"), e
->tok
);
3289 assert (typetag
== DW_TAG_pointer_type
);
3290 c_translate_pointer_store (pool
, 1, 0 /* PR9768 */, typedie
, tail
,
3295 // We have the pointer: cast it to an integral type via &(*(...))
3297 // NB: per bug #1187, at one point char*-like types were
3298 // automagically converted here to systemtap string values.
3299 // For several reasons, this was taken back out, leaving
3300 // pointer-to-string "conversion" (copying) to tapset functions.
3303 if (typetag
== DW_TAG_array_type
)
3304 c_translate_array (pool
, 1, 0 /* PR9768 */, typedie
, tail
, NULL
, 0);
3306 c_translate_pointer (pool
, 1, 0 /* PR9768 */, typedie
, tail
);
3307 c_translate_addressof (pool
, 1, 0 /* PR9768 */, NULL
, NULL
, tail
,
3316 dwflpp::express_as_string (string prelude
,
3318 struct location
*head
)
3321 char *buf
= 0; // NB: it would leak to pre-allocate a buffer here
3322 FILE *memstream
= open_memstream (&buf
, &bufsz
);
3325 fprintf(memstream
, "{\n");
3326 fprintf(memstream
, "%s", prelude
.c_str());
3328 unsigned int stack_depth
;
3329 bool deref
= c_emit_location (memstream
, head
, 1, &stack_depth
);
3331 // Ensure that DWARF keeps loc2c to a "reasonable" stack size
3332 // 32 intptr_t leads to max 256 bytes on the stack
3333 if (stack_depth
> 32)
3334 throw SEMANTIC_ERROR("oversized DWARF stack");
3336 fprintf(memstream
, "%s", postlude
.c_str());
3337 fprintf(memstream
, " goto out;\n");
3339 // dummy use of deref_fault label, to disable warning if deref() not used
3340 fprintf(memstream
, "if (0) goto deref_fault;\n");
3342 // XXX: deref flag not reliable; emit fault label unconditionally
3347 fprintf(memstream
, "}\n");
3356 dwflpp::vardie_from_symtable (Dwarf_Die
*vardie
, Dwarf_Addr
*addr
)
3358 const char *name
= dwarf_linkage_name (vardie
) ?: dwarf_diename (vardie
);
3360 if (sess
.verbose
> 2)
3361 clog
<< _F("finding symtable address for %s\n", name
);
3364 int syms
= dwfl_module_getsymtab (module
);
3365 DWFL_ASSERT (_("Getting symbols"), syms
>= 0);
3367 for (int i
= 0; *addr
== 0 && i
< syms
; i
++)
3371 const char *symname
= dwfl_module_getsym(module
, i
, &sym
, &shndxp
);
3373 && ! strcmp (name
, symname
)
3374 && sym
.st_shndx
!= SHN_UNDEF
3375 && (GELF_ST_TYPE (sym
.st_info
) == STT_NOTYPE
// PR13284
3376 || GELF_ST_TYPE (sym
.st_info
) == STT_OBJECT
))
3377 *addr
= sym
.st_value
;
3380 // Don't relocate for the kernel, or kernel modules we handle those
3381 // specially in emit_address.
3382 if (dwfl_module_relocations (module
) == 1 && module_name
!= TOK_KERNEL
)
3383 dwfl_module_relocate_address (module
, addr
);
3385 if (sess
.verbose
> 2)
3386 clog
<< _F("found %s @%#" PRIx64
"\n", name
, *addr
);
3392 dwflpp::literal_stmt_for_local (vector
<Dwarf_Die
>& scopes
,
3394 string
const & local
,
3395 const target_symbol
*e
,
3400 Dwarf_Attribute fb_attr_mem
, *fb_attr
= NULL
;
3402 fb_attr
= find_variable_and_frame_base (scopes
, pc
, local
, e
,
3403 &vardie
, &fb_attr_mem
);
3408 clog
<< _F("finding location for local '%s' near address %#" PRIx64
3409 ", module bias %#" PRIx64
"\n", local
.c_str(), pc
,
3412 clog
<< _F("finding location for global '%s' in CU '%s'\n",
3413 local
.c_str(), cu_name().c_str());
3417 #define obstack_chunk_alloc malloc
3418 #define obstack_chunk_free free
3420 struct obstack pool
;
3421 obstack_init (&pool
);
3422 struct location
*tail
= NULL
;
3424 /* Given $foo->bar->baz[NN], translate the location of foo. */
3426 struct location
*head
;
3428 Dwarf_Attribute attr_mem
;
3429 if (dwarf_attr_integrate (&vardie
, DW_AT_const_value
, &attr_mem
) == NULL
3430 && dwarf_attr_integrate (&vardie
, DW_AT_location
, &attr_mem
) == NULL
)
3433 memset(&addr_loc
, 0, sizeof(Dwarf_Op
));
3434 addr_loc
.atom
= DW_OP_addr
;
3435 // If it is an external variable try the symbol table. PR10622.
3436 if (dwarf_attr_integrate (&vardie
, DW_AT_external
, &attr_mem
) != NULL
3437 && vardie_from_symtable (&vardie
, &addr_loc
.number
) != 0)
3439 head
= c_translate_location (&pool
, &loc2c_error
, this,
3440 &loc2c_emit_address
,
3442 NULL
, &addr_loc
, 1, &tail
, NULL
, NULL
);
3445 throw SEMANTIC_ERROR (_F("failed to retrieve location attribute for '%s' [man error::dwarf] (dieoffset: %s)",
3446 local
.c_str(), lex_cast_hex(dwarf_dieoffset(&vardie
)).c_str()), e
->tok
);
3449 head
= translate_location (&pool
, &attr_mem
, &vardie
, pc
, fb_attr
, &tail
, e
);
3451 /* Translate the ->bar->baz[NN] parts. */
3454 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
3455 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf] (dieoffset: %s)", local
.c_str(), lex_cast_hex(dwarf_dieoffset(&vardie
)).c_str()), e
->tok
);
3457 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
3459 /* Translate the assignment part, either
3460 x = $foo->bar->baz[NN]
3462 $foo->bar->baz[NN] = x
3465 string prelude
, postlude
;
3466 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
3467 &vardie
, &typedie
, lvalue
, e
,
3468 prelude
, postlude
, ty
);
3470 /* Write the translation to a string. */
3471 string result
= express_as_string(prelude
, postlude
, head
);
3472 obstack_free (&pool
, 0);
3477 dwflpp::type_die_for_local (vector
<Dwarf_Die
>& scopes
,
3479 string
const & local
,
3480 const target_symbol
*e
,
3484 Dwarf_Attribute attr_mem
;
3486 find_variable_and_frame_base (scopes
, pc
, local
, e
, &vardie
, &attr_mem
);
3488 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
3489 throw SEMANTIC_ERROR(_F("failed to retrieve type attribute for '%s' [man error::dwarf]", local
.c_str()), e
->tok
);
3491 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
3497 dwflpp::literal_stmt_for_return (Dwarf_Die
*scope_die
,
3499 const target_symbol
*e
,
3504 clog
<< _F("literal_stmt_for_return: finding return value for %s (%s)\n",
3505 (dwarf_diename(scope_die
) ?: "<unknown>"), (dwarf_diename(cu
) ?: "<unknown>"));
3507 struct obstack pool
;
3508 obstack_init (&pool
);
3509 struct location
*tail
= NULL
;
3511 /* Given $return->bar->baz[NN], translate the location of return. */
3512 const Dwarf_Op
*locops
;
3513 int nlocops
= dwfl_module_return_value_location (module
, scope_die
,
3517 throw SEMANTIC_ERROR(_F("failed to retrieve return value location for %s [man error::dwarf] (%s)",
3518 (dwarf_diename(scope_die
) ?: "<unknown>"),
3519 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3521 // the function has no return value (e.g. "void" in C)
3522 else if (nlocops
== 0)
3524 throw SEMANTIC_ERROR(_F("function %s (%s) has no return value",
3525 (dwarf_diename(scope_die
) ?: "<unknown>"),
3526 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3529 struct location
*head
= c_translate_location (&pool
, &loc2c_error
, this,
3530 &loc2c_emit_address
,
3532 pc
, NULL
, locops
, nlocops
,
3535 /* Translate the ->bar->baz[NN] parts. */
3537 Dwarf_Die vardie
= *scope_die
, typedie
;
3538 if (dwarf_attr_die (&vardie
, DW_AT_type
, &typedie
) == NULL
)
3539 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
3540 (dwarf_diename(&vardie
) ?: "<unknown>"),
3541 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3543 translate_components (&pool
, &tail
, pc
, e
, &vardie
, &typedie
);
3545 /* Translate the assignment part, either
3546 x = $return->bar->baz[NN]
3548 $return->bar->baz[NN] = x
3551 string prelude
, postlude
;
3552 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
3553 &vardie
, &typedie
, lvalue
, e
,
3554 prelude
, postlude
, ty
);
3556 /* Write the translation to a string. */
3557 string result
= express_as_string(prelude
, postlude
, head
);
3558 obstack_free (&pool
, 0);
3563 dwflpp::type_die_for_return (Dwarf_Die
*scope_die
,
3565 const target_symbol
*e
,
3568 Dwarf_Die vardie
= *scope_die
;
3569 if (dwarf_attr_die (&vardie
, DW_AT_type
, typedie
) == NULL
)
3570 throw SEMANTIC_ERROR(_F("failed to retrieve return value type attribute for %s [man error::dwarf] (%s)",
3571 (dwarf_diename(&vardie
) ?: "<unknown>"),
3572 (dwarf_diename(cu
) ?: "<unknown>")), e
->tok
);
3574 translate_components (NULL
, NULL
, pc
, e
, &vardie
, typedie
);
3580 dwflpp::literal_stmt_for_pointer (Dwarf_Die
*start_typedie
,
3581 const target_symbol
*e
,
3586 clog
<< _F("literal_stmt_for_pointer: finding value for %s (%s)\n",
3587 dwarf_type_name(start_typedie
).c_str(), (dwarf_diename(cu
) ?: "<unknown>"));
3589 struct obstack pool
;
3590 obstack_init (&pool
);
3591 struct location
*head
= c_translate_argument (&pool
, &loc2c_error
, this,
3592 &loc2c_emit_address
,
3593 1, "STAP_ARG_pointer");
3594 struct location
*tail
= head
;
3596 /* Translate the ->bar->baz[NN] parts. */
3599 Dwarf_Die typedie
= *start_typedie
, vardie
= typedie
;
3601 /* As a special case when typedie is not an array or pointer, we can
3602 * allow array indexing on STAP_ARG_pointer instead (since we do
3603 * know the pointee type and can determine its size). PR11556. */
3604 const target_symbol::component
* c
=
3605 e
->components
.empty() ? NULL
: &e
->components
[0];
3606 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
3607 c
->type
== target_symbol::comp_expression_array_index
))
3609 resolve_unqualified_inner_typedie (&typedie
, &typedie
, e
);
3610 int typetag
= dwarf_tag (&typedie
);
3611 if (typetag
!= DW_TAG_pointer_type
&&
3612 typetag
!= DW_TAG_array_type
)
3614 if (c
->type
== target_symbol::comp_literal_array_index
)
3615 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, NULL
, c
->num_index
);
3617 c_translate_array_pointer (&pool
, 1, &typedie
, &tail
, "STAP_ARG_index0", 0);
3622 /* Now translate the rest normally. */
3624 translate_components (&pool
, &tail
, 0, e
, &vardie
, &typedie
, first
);
3626 /* Translate the assignment part, either
3627 x = (STAP_ARG_pointer)->bar->baz[NN]
3629 (STAP_ARG_pointer)->bar->baz[NN] = x
3632 string prelude
, postlude
;
3633 translate_final_fetch_or_store (&pool
, &tail
, module_bias
,
3634 &vardie
, &typedie
, lvalue
, e
,
3635 prelude
, postlude
, ty
);
3637 /* Write the translation to a string. */
3638 string result
= express_as_string(prelude
, postlude
, head
);
3639 obstack_free (&pool
, 0);
3644 dwflpp::type_die_for_pointer (Dwarf_Die
*start_typedie
,
3645 const target_symbol
*e
,
3649 *typedie
= *start_typedie
;
3650 Dwarf_Die vardie
= *typedie
;
3652 /* Handle the same PR11556 case as above. */
3653 const target_symbol::component
* c
=
3654 e
->components
.empty() ? NULL
: &e
->components
[0];
3655 if (c
&& (c
->type
== target_symbol::comp_literal_array_index
||
3656 c
->type
== target_symbol::comp_expression_array_index
))
3658 resolve_unqualified_inner_typedie (typedie
, typedie
, e
);
3659 int typetag
= dwarf_tag (typedie
);
3660 if (typetag
!= DW_TAG_pointer_type
&&
3661 typetag
!= DW_TAG_array_type
)
3665 translate_components (NULL
, NULL
, 0, e
, &vardie
, typedie
, first
);
3671 in_kprobes_function(systemtap_session
& sess
, Dwarf_Addr addr
)
3673 if (sess
.sym_kprobes_text_start
!= 0 && sess
.sym_kprobes_text_end
!= 0)
3675 // If the probe point address is anywhere in the __kprobes
3676 // address range, we can't use this probe point.
3677 if (addr
>= sess
.sym_kprobes_text_start
&& addr
< sess
.sym_kprobes_text_end
)
3685 dwflpp::blacklisted_p(const string
& funcname
,
3686 const string
& filename
,
3688 const string
& module
,
3692 if (!blacklist_enabled
)
3695 bool blacklisted
= false;
3697 // check against section blacklist
3698 string section
= get_blacklist_section(addr
);
3699 // PR6503: modules don't need special init/exit treatment
3700 if (module
== TOK_KERNEL
&& !regexec (&blacklist_section
, section
.c_str(), 0, NULL
, 0))
3704 clog
<< _(" init/exit");
3707 // Check for function marked '__kprobes'.
3708 if (module
== TOK_KERNEL
&& in_kprobes_function(sess
, addr
))
3712 clog
<< _(" __kprobes");
3715 // Check probe point against file/function blacklists.
3716 int goodfn
= regexec (&blacklist_func
, funcname
.c_str(), 0, NULL
, 0);
3718 goodfn
= goodfn
&& regexec (&blacklist_func_ret
, funcname
.c_str(), 0, NULL
, 0);
3719 int goodfile
= regexec (&blacklist_file
, filename
.c_str(), 0, NULL
, 0);
3721 if (! (goodfn
&& goodfile
))
3725 clog
<< _(" file/function blacklist");
3728 if (sess
.guru_mode
&& blacklisted
)
3730 blacklisted
= false;
3732 clog
<< _(" - not skipped (guru mode enabled)");
3735 if (blacklisted
&& sess
.verbose
>1)
3736 clog
<< _(" - skipped");
3738 // This probe point is not blacklisted.
3744 dwflpp::build_kernel_blacklist()
3746 // We build up the regexps in these strings
3748 // Add ^ anchors at the front; $ will be added just before regcomp.
3751 string blfn_ret
= "^(";
3752 string blfile
= "^(";
3753 string blsection
= "^(";
3755 blsection
+= "\\.init\\."; // first alternative, no "|"
3756 blsection
+= "|\\.exit\\.";
3757 blsection
+= "|\\.devinit\\.";
3758 blsection
+= "|\\.devexit\\.";
3759 blsection
+= "|\\.cpuinit\\.";
3760 blsection
+= "|\\.cpuexit\\.";
3761 blsection
+= "|\\.meminit\\.";
3762 blsection
+= "|\\.memexit\\.";
3764 /* NOTE all include/asm .h blfile patterns might need "full path"
3765 so prefix those with '.*' - see PR13108 and PR13112. */
3766 blfile
+= "kernel/kprobes\\.c"; // first alternative, no "|"
3767 blfile
+= "|arch/.*/kernel/kprobes\\.c";
3768 blfile
+= "|.*/include/asm/io\\.h";
3769 blfile
+= "|.*/include/asm/io_64\\.h";
3770 blfile
+= "|.*/include/asm/bitops\\.h";
3771 blfile
+= "|drivers/ide/ide-iops\\.c";
3773 blfile
+= "|arch/.*/kernel/paravirt\\.c";
3774 blfile
+= "|.*/include/asm/paravirt\\.h";
3776 // XXX: it would be nice if these blacklisted functions were pulled
3777 // in dynamically, instead of being statically defined here.
3778 // Perhaps it could be populated from script files. A "noprobe
3779 // kernel.function("...")" construct might do the trick.
3781 // Most of these are marked __kprobes in newer kernels. We list
3782 // them here (anyway) so the translator can block them on older
3783 // kernels that don't have the __kprobes function decorator. This
3784 // also allows detection of problems at translate- rather than
3787 blfn
+= "atomic_notifier_call_chain"; // first blfn; no "|"
3788 blfn
+= "|default_do_nmi";
3791 blfn
+= "|do_debug";
3792 blfn
+= "|do_general_protection";
3795 blfn
+= "|do_page_fault";
3796 blfn
+= "|do_sparc64_fault";
3798 blfn
+= "|dummy_nmi_callback";
3799 blfn
+= "|flush_icache_range";
3800 blfn
+= "|ia64_bad_break";
3801 blfn
+= "|ia64_do_page_fault";
3802 blfn
+= "|ia64_fault";
3803 blfn
+= "|io_check_error";
3804 blfn
+= "|mem_parity_error";
3805 blfn
+= "|nmi_watchdog_tick";
3806 blfn
+= "|notifier_call_chain";
3807 blfn
+= "|oops_begin";
3808 blfn
+= "|oops_end";
3809 blfn
+= "|program_check_exception";
3810 blfn
+= "|single_step_exception";
3811 blfn
+= "|sync_regs";
3812 blfn
+= "|unhandled_fault";
3813 blfn
+= "|unknown_nmi_error";
3814 blfn
+= "|xen_[gs]et_debugreg";
3815 blfn
+= "|xen_irq_.*";
3816 blfn
+= "|xen_.*_fl_direct.*";
3817 blfn
+= "|check_events";
3818 blfn
+= "|xen_adjust_exception_frame";
3819 blfn
+= "|xen_iret.*";
3820 blfn
+= "|xen_sysret64.*";
3821 blfn
+= "|test_ti_thread_flag.*";
3822 blfn
+= "|inat_get_opcode_attribute";
3823 blfn
+= "|system_call_after_swapgs";
3824 blfn
+= "|HYPERVISOR_[gs]et_debugreg";
3825 blfn
+= "|HYPERVISOR_event_channel_op";
3827 blfn
+= "|hash_ptr";
3828 blfn
+= "|native_set_pte";
3831 blfn
+= "|.*raw_.*_lock.*";
3832 blfn
+= "|.*raw_.*_unlock.*";
3833 blfn
+= "|.*raw_.*_trylock.*";
3834 blfn
+= "|.*read_lock.*";
3835 blfn
+= "|.*read_unlock.*";
3836 blfn
+= "|.*read_trylock.*";
3837 blfn
+= "|.*write_lock.*";
3838 blfn
+= "|.*write_unlock.*";
3839 blfn
+= "|.*write_trylock.*";
3840 blfn
+= "|.*write_seqlock.*";
3841 blfn
+= "|.*write_sequnlock.*";
3842 blfn
+= "|.*spin_lock.*";
3843 blfn
+= "|.*spin_unlock.*";
3844 blfn
+= "|.*spin_trylock.*";
3845 blfn
+= "|.*spin_is_locked.*";
3846 blfn
+= "|rwsem_.*lock.*";
3847 blfn
+= "|.*mutex_.*lock.*";
3851 blfn
+= "|atomic_.*";
3852 blfn
+= "|atomic64_.*";
3854 // few other problematic cases
3859 blfn
+= "|.*apic.*|.*APIC.*";
3860 blfn
+= "|.*softirq.*";
3862 blfn
+= "|.*_intr.*";
3864 blfn
+= "|.*kernel_text.*";
3865 blfn
+= "|get_current";
3866 blfn
+= "|current_.*";
3867 blfn
+= "|.*exception_tables.*";
3868 blfn
+= "|.*setup_rt_frame.*";
3870 // PR 5759, CONFIG_PREEMPT kernels
3871 blfn
+= "|.*preempt_count.*";
3872 blfn
+= "|preempt_schedule";
3874 // These functions don't return, so return probes would never be recovered
3875 blfn_ret
+= "do_exit"; // no "|"
3876 blfn_ret
+= "|sys_exit";
3877 blfn_ret
+= "|sys_exit_group";
3879 // __switch_to changes "current" on x86_64 and i686, so return probes
3880 // would cause kernel panic, and it is marked as "__kprobes" on x86_64
3881 if (sess
.architecture
== "x86_64")
3882 blfn
+= "|__switch_to";
3883 if (sess
.architecture
== "i686")
3884 blfn_ret
+= "|__switch_to";
3886 // RHEL6 pre-beta 2.6.32-19.el6
3887 blfn
+= "|special_mapping_.*";
3888 blfn
+= "|.*_pte_.*"; // or "|smaps_pte_range";
3889 blfile
+= "|fs/seq_file\\.c";
3894 blsection
+= ")"; // NB: no $, sections match just the beginning
3896 if (sess
.verbose
> 2)
3898 clog
<< _("blacklist regexps:") << endl
;
3899 clog
<< "blfn: " << blfn
<< endl
;
3900 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
3901 clog
<< "blfile: " << blfile
<< endl
;
3902 clog
<< "blsection: " << blsection
<< endl
;
3905 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3906 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
3907 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3908 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
3909 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3910 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
3911 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3912 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
3914 blacklist_enabled
= true;
3919 dwflpp::build_user_blacklist()
3921 // We build up the regexps in these strings
3923 // Add ^ anchors at the front; $ will be added just before regcomp.
3926 string blfn_ret
= "^(";
3927 string blfile
= "^(";
3928 string blsection
= "^(";
3930 // Non-matching placeholders until we have real things to match
3935 // These functions don't use the normal function-entry ABI, so can't be .return probed safely
3936 blfn_ret
+= "_start";
3941 blsection
+= ")"; // NB: no $, sections match just the beginning
3943 if (sess
.verbose
> 2)
3945 clog
<< _("blacklist regexps:") << endl
;
3946 clog
<< "blfn: " << blfn
<< endl
;
3947 clog
<< "blfn_ret: " << blfn_ret
<< endl
;
3948 clog
<< "blfile: " << blfile
<< endl
;
3949 clog
<< "blsection: " << blsection
<< endl
;
3952 int rc
= regcomp (& blacklist_func
, blfn
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3953 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func regcomp failed"));
3954 rc
= regcomp (& blacklist_func_ret
, blfn_ret
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3955 if (rc
) throw SEMANTIC_ERROR (_("blacklist_func_ret regcomp failed"));
3956 rc
= regcomp (& blacklist_file
, blfile
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3957 if (rc
) throw SEMANTIC_ERROR (_("blacklist_file regcomp failed"));
3958 rc
= regcomp (& blacklist_section
, blsection
.c_str(), REG_NOSUB
|REG_EXTENDED
);
3959 if (rc
) throw SEMANTIC_ERROR (_("blacklist_section regcomp failed"));
3961 blacklist_enabled
= true;
3966 dwflpp::get_blacklist_section(Dwarf_Addr addr
)
3968 string blacklist_section
;
3970 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
3971 // because dwfl_module_getelf can force costly section relocations
3972 // we don't really need, while either will do for this purpose.
3973 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (module
, &bias
))
3974 ?: dwfl_module_getelf (module
, &bias
));
3976 Dwarf_Addr offset
= addr
- bias
;
3981 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
3982 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
3985 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
3987 continue; // XXX error?
3989 if (!(shdr
->sh_flags
& SHF_ALLOC
))
3992 GElf_Addr start
= shdr
->sh_addr
;
3993 GElf_Addr end
= start
+ shdr
->sh_size
;
3994 if (! (offset
>= start
&& offset
< end
))
3997 blacklist_section
= elf_strptr (elf
, shstrndx
, shdr
->sh_name
);
4001 return blacklist_section
;
4005 /* Find the section named 'section_name' in the current module
4006 * returning the section header using 'shdr_mem' */
4009 dwflpp::get_section(string section_name
, GElf_Shdr
*shdr_mem
, Elf
**elf_ret
)
4011 GElf_Shdr
*shdr
= NULL
;
4016 // Explicitly look in the main elf file first.
4017 elf
= dwfl_module_getelf (module
, &bias
);
4018 Elf_Scn
*probe_scn
= NULL
;
4020 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4022 bool have_section
= false;
4024 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4026 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4027 assert (shdr
!= NULL
);
4029 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4031 have_section
= true;
4036 // Older versions may put the section in the debuginfo dwarf file,
4037 // so check if it actually exists, if not take a look in the debuginfo file
4038 if (! have_section
|| (have_section
&& shdr
->sh_type
== SHT_NOBITS
))
4040 elf
= dwarf_getelf (dwfl_module_getdwarf (module
, &bias
));
4043 DWFL_ASSERT ("getshdrstrndx", elf_getshdrstrndx (elf
, &shstrndx
));
4045 while ((probe_scn
= elf_nextscn (elf
, probe_scn
)))
4047 shdr
= gelf_getshdr (probe_scn
, shdr_mem
);
4048 if (elf_strptr (elf
, shstrndx
, shdr
->sh_name
) == section_name
)
4050 have_section
= true;
4066 dwflpp::relocate_address(Dwarf_Addr dw_addr
, string
& reloc_section
)
4069 // libdw address, so adjust for bias gotten from dwfl_module_getdwarf
4070 Dwarf_Addr reloc_addr
= dw_addr
+ module_bias
;
4073 assert(module_name
== TOK_KERNEL
);
4076 else if (dwfl_module_relocations (module
) > 0)
4078 // This is a relocatable module; libdwfl already knows its
4079 // sections, so we can relativize addr.
4080 int idx
= dwfl_module_relocate_address (module
, &reloc_addr
);
4081 const char* r_s
= dwfl_module_relocation_info (module
, idx
, NULL
);
4083 reloc_section
= r_s
;
4085 if (reloc_section
== "" && dwfl_module_relocations (module
) == 1)
4086 reloc_section
= ".dynamic";
4089 reloc_section
= ".absolute";
4093 /* Returns the call frame address operations for the given program counter
4094 * in the libdw address space.
4097 dwflpp::get_cfa_ops (Dwarf_Addr pc
)
4099 Dwarf_Op
*cfa_ops
= NULL
;
4101 if (sess
.verbose
> 2)
4102 clog
<< "get_cfa_ops @0x" << hex
<< pc
<< dec
4103 << ", module_start @0x" << hex
<< module_start
<< dec
<< endl
;
4105 // Try debug_frame first, then fall back on eh_frame.
4106 size_t cfa_nops
= 0;
4107 Dwarf_Addr bias
= 0;
4108 Dwarf_Frame
*frame
= NULL
;
4109 Dwarf_CFI
*cfi
= dwfl_module_dwarf_cfi (module
, &bias
);
4112 if (sess
.verbose
> 3)
4113 clog
<< "got dwarf cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4114 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4115 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4116 else if (sess
.verbose
> 3)
4117 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4119 else if (sess
.verbose
> 3)
4120 clog
<< "dwfl_module_dwarf_cfi failed: " << dwfl_errmsg(-1) << endl
;
4122 if (cfa_ops
== NULL
)
4124 cfi
= dwfl_module_eh_cfi (module
, &bias
);
4127 if (sess
.verbose
> 3)
4128 clog
<< "got eh cfi bias: 0x" << hex
<< bias
<< dec
<< endl
;
4129 Dwarf_Frame
*frame
= NULL
;
4130 if (dwarf_cfi_addrframe (cfi
, pc
- bias
, &frame
) == 0)
4131 dwarf_frame_cfa (frame
, &cfa_ops
, &cfa_nops
);
4132 else if (sess
.verbose
> 3)
4133 clog
<< "dwarf_cfi_addrframe failed: " << dwarf_errmsg(-1) << endl
;
4135 else if (sess
.verbose
> 3)
4136 clog
<< "dwfl_module_eh_cfi failed: " << dwfl_errmsg(-1) << endl
;
4140 if (sess
.verbose
> 2)
4142 if (cfa_ops
== NULL
)
4143 clog
<< _("not found cfa") << endl
;
4146 Dwarf_Addr frame_start
, frame_end
;
4148 int info
= dwarf_frame_info (frame
, &frame_start
, &frame_end
,
4150 clog
<< _F("found cfa, info: %d [start: %#" PRIx64
", end: %#" PRIx64
4151 ", nops: %zu", info
, frame_start
, frame_end
, cfa_nops
) << endl
;
4159 dwflpp::add_module_build_id_to_hash (Dwfl_Module
*m
,
4160 void **userdata
__attribute__ ((unused
)),
4165 string modname
= name
;
4166 systemtap_session
* s
= (systemtap_session
*)arg
;
4167 if (pending_interrupts
)
4168 return DWARF_CB_ABORT
;
4170 // Extract the build ID
4171 const unsigned char *bits
;
4173 int bits_length
= dwfl_module_build_id(m
, &bits
, &vaddr
);
4176 // Convert the binary bits to a hex string
4177 string hex
= hex_dump(bits
, bits_length
);
4179 // Store the build ID in the session
4180 s
->build_ids
.push_back(hex
);
4188 // Perform PR15123 heuristic for given variable at given address.
4189 // Return alternate pc address to do location-list lookup at, or 0 if
4193 dwflpp::pr15123_retry_addr (Dwarf_Addr pc
, Dwarf_Die
* die
)
4195 // For PR15123, we'd like to detect the situation where the
4196 // incoming PC may point to a couple-of-byte instruction
4197 // sequence that gcc emits for CFLAGS=-mfentry, and where
4198 // context variables are in fact available throughout, *but* due
4199 // to the bug, the dwarf debuginfo location-list only starts a
4200 // few instructions later. Prologue searching does not resolve
4201 // this as a line-record is in place at the -mfentry prologue.
4203 // Detecting this is complicated because ...
4204 // - we only want to do this if -mfentry was actually used
4205 // - if <pc> points to the a function entry point
4206 // - if the architecture is familiar enough that we can have a
4207 // hard-coded constant to skip over the prologue.
4209 // Otherwise, we could give a false-positive - return corrupted
4212 // Use of -mfentry is detectable only if CFLAGS=-grecord-gcc-switches
4213 // was used. Without it, set the PR15123_ASSUME_MFENTRY environment
4214 // variable to override the -mfentry test.
4216 if (getenv ("PR15123_DISABLE"))
4219 if (!getenv ("PR15123_ASSUME_MFENTRY")) {
4221 string producer
, version
;
4222 dwarf_diecu (die
, &cudie
, NULL
, NULL
);
4223 if (!is_gcc_producer(&cudie
, producer
, version
))
4225 if (producer
.find("-mfentry") == string::npos
)
4229 // Determine if this pc maps to the beginning of a
4230 // real function (not some inlined doppelganger. This
4231 // is made tricker by this->function may not be
4232 // pointing at the right DIE (say e.g. stap encountered
4233 // the inlined copy first, so was focus_on_function'd).
4234 vector
<Dwarf_Die
> scopes
= getscopes(pc
);
4235 if (scopes
.size() == 0)
4238 Dwarf_Die outer_function_die
= scopes
[0];
4240 if (!die_entrypc(& outer_function_die
, &entrypc
) || entrypc
!= pc
)
4241 return 0; // (will fail on retry, so we won't loop more than once)
4243 if (sess
.architecture
== "i386" ||
4244 sess
.architecture
== "x86_64") {
4245 /* pull the trigger */
4246 if (sess
.verbose
> 2)
4247 clog
<< _("retrying variable location-list lookup at address pc+5\n");
4255 dwflpp::has_gnu_debugdata ()
4257 Dwarf_Addr load_addr
;
4258 // Note we really want the actual elf file, not the dwarf .debug file.
4259 Elf
* elf
= dwfl_module_getelf (module
, &load_addr
);
4261 assert (elf_getshdrstrndx (elf
, &shstrndx
) >= 0);
4263 // Get the gnu_debugdata section header
4264 Elf_Scn
*scn
= NULL
;
4265 GElf_Shdr
*gnu_debugdata_shdr
= NULL
;
4266 GElf_Shdr gnu_debugdata_shdr_mem
;
4267 while ((scn
= elf_nextscn (elf
, scn
)))
4269 gnu_debugdata_shdr
= gelf_getshdr (scn
, &gnu_debugdata_shdr_mem
);
4270 assert (gnu_debugdata_shdr
!= NULL
);
4271 if (strcmp (elf_strptr (elf
, shstrndx
, gnu_debugdata_shdr
->sh_name
), ".gnu_debugdata") == 0)
4277 // If not GCC, return false. Otherwise, return true and set vers.
4279 dwflpp::is_gcc_producer(Dwarf_Die
*cudie
, string
& producer
, string
& version
)
4281 Dwarf_Attribute producer_attr
;
4282 if (!dwarf_attr_integrate(cudie
, DW_AT_producer
, &producer_attr
))
4285 // GNU {C|C++|...} x.x.x YYYYMMDD ...
4286 const char *cproducer
= dwarf_formstring(&producer_attr
);
4289 producer
= cproducer
;
4291 vector
<string
> tokens
;
4292 tokenize(producer
, tokens
);
4294 if (tokens
.size() < 3
4295 || tokens
[0] != "GNU"
4296 || tokens
[2].find_first_not_of(".0123456789") != string::npos
)
4299 version
= tokens
[2];
4304 die_has_loclist(Dwarf_Die
*begin_die
)
4307 Dwarf_Attribute loc
;
4309 if (dwarf_child(begin_die
, &die
) != 0)
4314 switch (dwarf_tag(&die
))
4316 case DW_TAG_formal_parameter
:
4317 case DW_TAG_variable
:
4318 if (dwarf_attr_integrate(&die
, DW_AT_location
, &loc
)
4319 && dwarf_whatform(&loc
) == DW_FORM_sec_offset
)
4323 if (dwarf_haschildren (&die
))
4324 if (die_has_loclist(&die
))
4329 while (dwarf_siblingof (&die
, &die
) == 0);
4335 dwflpp::has_valid_locs ()
4339 // The current CU has valid location info (implying we do not need to skip the
4341 // - it was compiled with -O2 -g (in which case, GCC outputs proper location
4342 // info for the prologue), and
4343 // - it was compiled by GCC >= 4.5 (previous versions could have had invalid
4344 // debug info in the prologue, see GDB's PR13777)
4345 // Note that clang behaves similarly to GCC here: non-optimized code does not
4346 // have location lists, while optimized code does. In the check below, even if
4347 // the producer is not GCC, we assume that it is valid to do the loclist check
4348 // afterwards (which it is for clang).
4351 if (is_gcc_producer(cu
, prod
, vers
)
4352 && strverscmp(vers
.c_str(), "4.5") < 0)
4355 // We determine if the current CU has been optimized with -O2 -g by looking
4356 // for any data objects whose DW_AT_location is a location list. This is also
4357 // how GDB determines whether to skip the prologue or not. See GDB's PR12573
4358 // and also RHBZ612253#c6.
4359 if (!die_has_loclist(cu
))
4362 if (sess
.verbose
> 2)
4363 clog
<< _F("CU '%s' in module '%s' has valid locs",
4364 cu_name().c_str(), module_name
.c_str()) << endl
;
4369 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */