1 // C++ interface to dwfl
2 // Copyright (C) 2005-2009 Red Hat Inc.
3 // Copyright (C) 2005-2007 Intel Corporation.
4 // Copyright (C) 2008 James.Bottomley@HansenPartnership.com
6 // This file is part of systemtap, and is free software. You can
7 // redistribute it and/or modify it under the terms of the GNU General
8 // Public License (GPL); either version 2, or (at your option) any
15 #include "dwarf_wrappers.h"
16 #include "elaborate.h"
27 #include <elfutils/libdwfl.h>
31 #if !_ELFUTILS_PREREQ(0,142)
32 // Always use newer name, old name is deprecated in 0.142.
33 #define elf_getshdrstrndx elf_getshstrndx
38 struct inline_instance_info
;
43 enum line_t
{ ABSOLUTE
, RELATIVE
, RANGE
, WILDCARD
};
44 enum info_status
{ info_unknown
, info_present
, info_absent
};
46 #ifdef HAVE_TR1_UNORDERED_MAP
47 #include <tr1/unordered_map>
48 template<class K
, class V
> struct stap_map
{
49 typedef std::tr1::unordered_map
<K
, V
> type
;
52 #include <ext/hash_map>
53 template<class K
, class V
> struct stap_map
{
54 typedef __gnu_cxx::hash_map
<K
, V
, stap_map
> type
;
55 size_t operator() (std::string
const& s
) const
56 { __gnu_cxx::hash
<const char*> h
; return h(s
.c_str()); }
57 size_t operator() (void* const& p
) const
58 { __gnu_cxx::hash
<long> h
; return h(reinterpret_cast<long>(p
)); }
63 typedef stap_map
<Dwarf
*, std::vector
<Dwarf_Die
>*>::type module_cu_cache_t
;
66 typedef stap_map
<std::string
, Dwarf_Die
>::type cu_function_cache_t
;
68 // cu die -> (function -> die)
69 typedef stap_map
<void*, cu_function_cache_t
*>::type mod_cu_function_cache_t
;
71 // inline function die -> instance die[]
72 typedef stap_map
<void*, std::vector
<Dwarf_Die
>*>::type cu_inl_function_cache_t
;
74 typedef std::vector
<func_info
> func_info_map_t
;
75 typedef std::vector
<inline_instance_info
> inline_instance_map_t
;
78 /* XXX FIXME functions that dwflpp needs from tapsets.cxx */
79 func_info_map_t
*get_filtered_functions(dwarf_query
*q
);
80 inline_instance_map_t
*get_filtered_inlines(dwarf_query
*q
);
81 void add_label_name(dwarf_query
*q
, const char *name
);
92 symbol_table
*sym_table
;
93 info_status dwarf_status
; // module has dwarf info?
94 info_status symtab_status
; // symbol table cached?
96 void get_symtab(dwarf_query
*q
);
97 void update_symtab(cu_function_cache_t
*funcs
);
99 module_info(const char *name
) :
105 dwarf_status(info_unknown
),
106 symtab_status(info_unknown
)
116 std::map
<std::string
, module_info
*> cache
;
117 bool paths_collected
;
118 bool dwarf_collected
;
120 module_cache() : paths_collected(false), dwarf_collected(false) {}
127 : decl_file(NULL
), decl_line(-1), addr(0), prologue_end(0), weak(false)
129 std::memset(&die
, 0, sizeof(die
));
132 char const * decl_file
;
137 Dwarf_Addr prologue_end
;
142 struct inline_instance_info
144 inline_instance_info()
145 : decl_file(NULL
), decl_line(-1)
147 std::memset(&die
, 0, sizeof(die
));
150 char const * decl_file
;
159 systemtap_session
& sess
;
161 // These are "current" values we focus on.
162 Dwfl_Module
* module
;
163 Dwarf_Addr module_bias
;
164 module_info
* mod_info
;
166 // These describe the current module's PC address range
167 Dwarf_Addr module_start
;
168 Dwarf_Addr module_end
;
172 std::string module_name
;
173 std::string function_name
;
175 dwflpp(systemtap_session
& session
, const std::string
& user_module
, bool kernel_p
);
176 dwflpp(systemtap_session
& session
, const std::vector
<std::string
>& user_modules
);
179 void get_module_dwarf(bool required
= false, bool report
= true);
181 void focus_on_module(Dwfl_Module
* m
, module_info
* mi
);
182 void focus_on_cu(Dwarf_Die
* c
);
183 void focus_on_function(Dwarf_Die
* f
);
185 std::string
cu_name(void);
187 Dwarf_Die
*query_cu_containing_address(Dwarf_Addr a
);
189 bool module_name_matches(const std::string
& pattern
);
190 static bool name_has_wildcard(const std::string
& pattern
);
191 bool module_name_final_match(const std::string
& pattern
);
193 bool function_name_matches_pattern(const std::string
& name
, const std::string
& pattern
);
194 bool function_name_matches(const std::string
& pattern
);
195 bool function_name_final_match(const std::string
& pattern
);
197 void iterate_over_modules(int (* callback
)(Dwfl_Module
*, void **,
198 const char *, Dwarf_Addr
,
202 void iterate_over_cus (int (*callback
)(Dwarf_Die
* die
, void * arg
),
205 bool func_is_inline();
207 void iterate_over_inline_instances (int (* callback
)(Dwarf_Die
* die
, void * arg
),
210 Dwarf_Die
*declaration_resolve(const char *name
);
212 mod_cu_function_cache_t cu_function_cache
;
214 int iterate_over_functions (int (* callback
)(Dwarf_Die
* func
, base_query
* q
),
215 base_query
* q
, const std::string
& function
,
216 bool has_statement_num
=false);
218 void iterate_over_srcfile_lines (char const * srcfile
,
220 bool need_single_match
,
221 enum line_t line_type
,
222 void (* callback
) (const dwarf_line_t
& line
,
224 const std::string
& func_pattern
,
227 void iterate_over_labels (Dwarf_Die
*begin_die
,
229 const char *symfunction
,
231 void (* callback
)(const std::string
&,
238 void collect_srcfiles_matching (std::string
const & pattern
,
239 std::set
<std::string
> & filtered_srcfiles
);
241 void resolve_prologue_endings (func_info_map_t
& funcs
);
243 bool function_entrypc (Dwarf_Addr
* addr
);
244 bool die_entrypc (Dwarf_Die
* die
, Dwarf_Addr
* addr
);
246 void function_die (Dwarf_Die
*d
);
247 void function_file (char const ** c
);
248 void function_line (int *linep
);
250 bool die_has_pc (Dwarf_Die
& die
, Dwarf_Addr pc
);
252 std::string
literal_stmt_for_local (Dwarf_Die
*scope_die
,
254 std::string
const & local
,
255 const target_symbol
*e
,
260 std::string
literal_stmt_for_return (Dwarf_Die
*scope_die
,
262 const target_symbol
*e
,
266 std::string
literal_stmt_for_pointer (Dwarf_Die
*type_die
,
267 const target_symbol
*e
,
271 bool blacklisted_p(const std::string
& funcname
,
272 const std::string
& filename
,
274 const std::string
& module
,
275 const std::string
& section
,
279 Dwarf_Addr
relocate_address(Dwarf_Addr addr
,
280 std::string
& reloc_section
,
281 std::string
& blacklist_section
);
283 Dwarf_Addr
literal_addr_to_sym_addr(Dwarf_Addr lit_addr
);
289 // These are "current" values we focus on.
290 Dwarf
* module_dwarf
;
291 Dwarf_Die
* function
;
293 void setup_kernel(const std::string
& module_name
, bool debuginfo_needed
= true);
294 void setup_user(const std::vector
<std::string
>& modules
, bool debuginfo_needed
= true);
296 module_cu_cache_t module_cu_cache
;
298 cu_inl_function_cache_t cu_inl_function_cache
;
299 static int cu_inl_function_caching_callback (Dwarf_Die
* func
, void *arg
);
301 /* The global alias cache is used to resolve any DIE found in a
302 * module that is stubbed out with DW_AT_declaration with a defining
303 * DIE found in a different module. The current assumption is that
304 * this only applies to structures and unions, which have a global
305 * namespace (it deliberately only traverses program scope), so this
306 * cache is indexed by name. If other declaration lookups were
307 * added to it, it would have to be indexed by name and tag
309 mod_cu_function_cache_t global_alias_cache
;
310 static int global_alias_caching_callback(Dwarf_Die
*die
, void *arg
);
311 int iterate_over_globals (int (* callback
)(Dwarf_Die
*, void *),
314 static int cu_function_caching_callback (Dwarf_Die
* func
, void *arg
);
316 bool has_single_line_record (dwarf_query
* q
, char const * srcfile
, int lineno
);
318 static void loc2c_error (void *, const char *fmt
, ...);
320 // This function generates code used for addressing computations of
322 void emit_address (struct obstack
*pool
, Dwarf_Addr address
);
323 static void loc2c_emit_address (void *arg
, struct obstack
*pool
,
326 void print_locals(Dwarf_Die
*die
, std::ostream
&o
);
327 void print_members(Dwarf_Die
*vardie
, std::ostream
&o
);
329 Dwarf_Attribute
*find_variable_and_frame_base (Dwarf_Die
*scope_die
,
331 std::string
const & local
,
332 const target_symbol
*e
,
334 Dwarf_Attribute
*fb_attr_mem
);
336 struct location
*translate_location(struct obstack
*pool
,
337 Dwarf_Attribute
*attr
,
339 Dwarf_Attribute
*fb_attr
,
340 struct location
**tail
,
341 const target_symbol
*e
);
343 bool find_struct_member(const target_symbol::component
& c
,
344 Dwarf_Die
*parentdie
,
345 Dwarf_Die
*memberdie
,
346 std::vector
<Dwarf_Attribute
>& locs
);
348 Dwarf_Die
*translate_components(struct obstack
*pool
,
349 struct location
**tail
,
351 const target_symbol
*e
,
354 Dwarf_Attribute
*attr_mem
);
356 Dwarf_Die
*resolve_unqualified_inner_typedie (Dwarf_Die
*typedie_mem
,
357 Dwarf_Attribute
*attr_mem
,
358 const target_symbol
*e
);
360 void translate_final_fetch_or_store (struct obstack
*pool
,
361 struct location
**tail
,
362 Dwarf_Addr module_bias
,
364 Dwarf_Attribute
*attr_mem
,
366 const target_symbol
*e
,
371 std::string
express_as_string (std::string prelude
,
372 std::string postlude
,
373 struct location
*head
);
375 regex_t blacklist_func
; // function/statement probes
376 regex_t blacklist_func_ret
; // only for .return probes
377 regex_t blacklist_file
; // file name
378 bool blacklist_enabled
;
379 void build_blacklist();
380 std::string
get_blacklist_section(Dwarf_Addr addr
);
382 Dwarf_Addr pc_cached_scopes
;
383 int num_cached_scopes
;
384 Dwarf_Die
*cached_scopes
;
385 int dwarf_getscopes_cached (Dwarf_Addr pc
, Dwarf_Die
**scopes
);
387 // Returns the call frame address operations for the given program counter.
388 Dwarf_Op
*get_cfa_ops (Dwarf_Addr pc
);
394 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */