2 // Copyright (C) 2005-2011 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
13 #include "elaborate.h"
15 #include "task_finder.h"
16 #include "translate.h"
20 #include "dwarf_wrappers.h"
21 #include "auto_free.h"
24 #include "setupdwfl.h"
27 #include "sdt_types.h"
46 #include <elfutils/libdwfl.h>
47 #include <elfutils/libdw.h>
54 #include <sys/types.h>
60 #define __STDC_FORMAT_MACROS
66 using namespace __gnu_cxx
;
70 // ------------------------------------------------------------------------
73 common_probe_init (derived_probe
* p
)
75 assert(p
->session_index
!= (unsigned)-1);
76 return "(&stap_probes[" + lex_cast(p
->session_index
) + "])";
81 common_probe_entryfn_prologue (translator_output
* o
, string statestr
,
82 string probe
, bool overload_processing
)
84 o
->newline() << "#ifdef STP_ALIBI";
85 o
->newline() << "atomic_inc(&(" << probe
<< "->alibi));";
86 o
->newline() << "#else";
88 o
->newline() << "struct context* __restrict__ c;";
89 o
->newline() << "#if !INTERRUPTIBLE";
90 o
->newline() << "unsigned long flags;";
91 o
->newline() << "#endif";
93 if (overload_processing
)
94 o
->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
96 o
->newline() << "#ifdef STP_TIMING";
97 o
->newline() << "cycles_t cycles_atstart = get_cycles ();";
98 o
->newline() << "#endif";
100 o
->newline() << "#ifdef STP_TIMING";
101 o
->newline() << "Stat stat = " << probe
<< "->timing;";
102 o
->newline() << "#endif";
104 o
->newline() << "#if INTERRUPTIBLE";
105 o
->newline() << "preempt_disable ();";
106 o
->newline() << "#else";
107 o
->newline() << "local_irq_save (flags);";
108 o
->newline() << "#endif";
110 // Check for enough free enough stack space
111 o
->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
112 o
->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
113 // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack
114 // NB: see also CONFIG_DEBUG_STACKOVERFLOW
115 o
->newline() << "atomic_inc (& skipped_count);";
116 o
->newline() << "#ifdef STP_TIMING";
117 o
->newline() << "atomic_inc (& skipped_count_lowstack);";
118 o
->newline() << "#endif";
119 o
->newline() << "goto probe_epilogue;";
120 o
->newline(-1) << "}";
122 o
->newline() << "if (atomic_read (&session_state) != " << statestr
<< ")";
123 o
->newline(1) << "goto probe_epilogue;";
126 o
->newline() << "c = contexts[smp_processor_id()];";
127 o
->newline() << "if (atomic_inc_return (& c->busy) != 1) {";
128 o
->newline(1) << "#if !INTERRUPTIBLE";
129 o
->newline() << "atomic_inc (& skipped_count);";
130 o
->newline() << "#endif";
131 o
->newline() << "#ifdef STP_TIMING";
132 o
->newline() << "atomic_inc (& skipped_count_reentrant);";
133 o
->newline() << "#ifdef DEBUG_REENTRANCY";
134 o
->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", "
135 << probe
<< "->pp, c->probe_point ?: \"?\", smp_processor_id());";
136 // NB: There is a conceivable race condition here with reading
137 // c->probe_point, knowing that this other probe is sort of running.
138 // However, in reality, it's interrupted. Plus even if it were able
139 // to somehow start again, and stop before we read c->probe_point,
140 // at least we have that ?: "?" bit in there to avoid a NULL deref.
141 o
->newline() << "#endif";
142 o
->newline() << "#endif";
143 o
->newline() << "atomic_dec (& c->busy);";
144 o
->newline() << "goto probe_epilogue;";
145 o
->newline(-1) << "}";
147 o
->newline() << "c->last_stmt = 0;";
148 o
->newline() << "c->last_error = 0;";
149 o
->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
150 o
->newline() << "c->regs = 0;";
151 o
->newline() << "#if defined __ia64__";
152 o
->newline() << "c->unwaddr = 0;";
153 o
->newline() << "#endif";
154 o
->newline() << "c->probe_point = " << probe
<< "->pp;";
155 o
->newline() << "#ifdef STP_NEED_PROBE_NAME";
156 o
->newline() << "c->probe_name = " << probe
<< "->pn;";
157 o
->newline() << "#else";
158 o
->newline() << "c->probe_name = 0;";
159 o
->newline() << "#endif";
160 // reset unwound address cache
161 o
->newline() << "c->pi = 0;";
162 o
->newline() << "c->pi_longs = 0;";
163 o
->newline() << "c->regflags = 0;";
164 o
->newline() << "c->regparm = 0;";
165 o
->newline() << "c->marker_name = NULL;";
166 o
->newline() << "c->marker_format = NULL;";
168 o
->newline() << "#if INTERRUPTIBLE";
169 o
->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
170 o
->newline() << "#else";
171 o
->newline() << "c->actionremaining = MAXACTION;";
172 o
->newline() << "#endif";
173 o
->newline() << "c->ri = 0;";
174 // NB: The following would actually be incorrect.
175 // That's because cycles_sum/cycles_base values are supposed to survive
176 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
177 // cycles), the values will be reset.
179 o->newline() << "#ifdef STP_OVERLOAD";
180 o->newline() << "c->cycles_sum = 0;";
181 o->newline() << "c->cycles_base = 0;";
182 o->newline() << "#endif";
188 common_probe_entryfn_epilogue (translator_output
* o
,
189 bool overload_processing
)
191 if (overload_processing
)
192 o
->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
194 o
->newline() << "#ifdef STP_TIMING";
196 o
->newline(1) << "cycles_t cycles_atend = get_cycles ();";
197 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
198 // fewer, if the hardware counter rolls over really quickly. We
199 // handle 32-bit wraparound here.
200 o
->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
201 o
->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
202 o
->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
205 o
->newline() << "#ifdef STP_TIMING";
206 o
->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
207 o
->newline() << "#endif";
209 if (overload_processing
)
211 o
->newline() << "#ifdef STP_OVERLOAD";
213 // If the cycle count has wrapped (cycles_atend > cycles_base),
214 // let's go ahead and pretend the interval has been reached.
215 // This should reset cycles_base and cycles_sum.
216 o
->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
217 o
->newline(1) << "? (cycles_atend - c->cycles_base)";
218 o
->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
219 o
->newline(-1) << "c->cycles_sum += cycles_elapsed;";
221 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
222 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
223 // has overloaded the system and we need to quit.
224 o
->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
225 o
->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
226 o
->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
227 o
->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);";
228 o
->newline() << "atomic_inc (&error_count);";
229 o
->newline(-1) << "}";
231 o
->newline() << "c->cycles_base = cycles_atend;";
232 o
->newline() << "c->cycles_sum = 0;";
233 o
->newline(-1) << "}";
234 o
->newline(-1) << "}";
235 o
->newline() << "#endif";
238 o
->newline(-1) << "}";
239 o
->newline() << "#endif";
241 o
->newline() << "c->probe_point = 0;"; // vacated
242 o
->newline() << "c->probe_name = 0;";
243 o
->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
244 o
->newline(1) << "if (c->last_stmt != NULL)";
245 o
->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
246 o
->newline(-1) << "else";
247 o
->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
249 o
->newline() << "atomic_inc (& error_count);";
250 o
->newline() << "if (atomic_read (& error_count) > MAXERRORS) {";
251 o
->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);";
252 o
->newline() << "_stp_exit ();";
253 o
->newline(-1) << "}";
254 o
->newline(-1) << "}";
255 o
->newline() << "atomic_dec (&c->busy);";
257 o
->newline(-1) << "probe_epilogue:"; // context is free
260 // Check for excessive skip counts.
261 o
->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {";
262 o
->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(& session_state, STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
263 o
->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
264 o
->newline(-1) << "}";
266 o
->newline() << "#if INTERRUPTIBLE";
267 o
->newline() << "preempt_enable_no_resched ();";
268 o
->newline() << "#else";
269 o
->newline() << "local_irq_restore (flags);";
270 o
->newline() << "#endif";
272 o
->newline() << "#endif // STP_ALIBI";
276 // ------------------------------------------------------------------------
278 // ------------------------------------------------------------------------
279 // Dwarf derived probes. "We apologize for the inconvience."
280 // ------------------------------------------------------------------------
282 static const string
TOK_KERNEL("kernel");
283 static const string
TOK_MODULE("module");
284 static const string
TOK_FUNCTION("function");
285 static const string
TOK_INLINE("inline");
286 static const string
TOK_CALL("call");
287 static const string
TOK_RETURN("return");
288 static const string
TOK_MAXACTIVE("maxactive");
289 static const string
TOK_STATEMENT("statement");
290 static const string
TOK_ABSOLUTE("absolute");
291 static const string
TOK_PROCESS("process");
292 static const string
TOK_PROVIDER("provider");
293 static const string
TOK_MARK("mark");
294 static const string
TOK_TRACE("trace");
295 static const string
TOK_LABEL("label");
296 static const string
TOK_LIBRARY("library");
298 static int query_cu (Dwarf_Die
* cudie
, void * arg
);
299 static void query_addr(Dwarf_Addr addr
, dwarf_query
*q
);
301 // Can we handle this query with just symbol-table info?
305 dbr_none
, // kernel.statement(NUM).absolute
306 dbr_need_symtab
, // can get by with symbol table if there's no dwarf
311 struct base_query
; // forward decls
320 module_info
*mod_info
; // associated module
321 map
<string
, func_info
*> map_by_name
;
322 multimap
<Dwarf_Addr
, func_info
*> map_by_addr
;
323 typedef multimap
<Dwarf_Addr
, func_info
*>::iterator iterator_t
;
324 typedef pair
<iterator_t
, iterator_t
> range_t
;
326 GElf_Word opd_section
;
328 void add_symbol(const char *name
, bool weak
, bool descriptor
,
329 Dwarf_Addr addr
, Dwarf_Addr
*high_addr
);
330 enum info_status
read_symbols(FILE *f
, const string
& path
);
331 enum info_status
read_from_elf_file(const string
& path
,
332 const systemtap_session
&sess
);
333 enum info_status
read_from_text_file(const string
& path
,
334 const systemtap_session
&sess
);
335 enum info_status
get_from_elf();
336 void prepare_section_rejection(Dwfl_Module
*mod
);
337 bool reject_section(GElf_Word section
);
338 void purge_syscall_stubs();
339 func_info
*lookup_symbol(const string
& name
);
340 Dwarf_Addr
lookup_symbol_address(const string
& name
);
341 func_info
*get_func_containing_address(Dwarf_Addr addr
);
343 symbol_table(module_info
*mi
) : mod_info(mi
) {}
347 static bool null_die(Dwarf_Die
*die
)
349 static Dwarf_Die null
= { 0 };
350 return (!die
|| !memcmp(die
, &null
, sizeof(null
)));
359 function_file_and_line
363 struct dwarf_builder
;
364 struct dwarf_var_expanding_visitor
;
367 // XXX: This class is a candidate for subclassing to separate
368 // the relocation vs non-relocation variants. Likewise for
369 // kprobe vs kretprobe variants.
371 struct dwarf_derived_probe
: public derived_probe
373 dwarf_derived_probe (const string
& function
,
374 const string
& filename
,
376 const string
& module
,
377 const string
& section
,
378 Dwarf_Addr dwfl_addr
,
381 Dwarf_Die
* scope_die
);
396 unsigned saved_longs
, saved_strings
;
397 dwarf_derived_probe
* entry_handler
;
399 void printsig (std::ostream
&o
) const;
400 virtual void join_group (systemtap_session
& s
);
401 void emit_probe_local_init(translator_output
* o
);
402 void getargs(std::list
<std::string
> &arg_set
) const;
404 void emit_unprivileged_assertion (translator_output
*);
405 void print_dupe_stamp(ostream
& o
);
407 // Pattern registration helpers.
408 static void register_statement_variants(match_node
* root
,
410 bool bind_unprivileged_p
= false);
411 static void register_function_variants(match_node
* root
,
413 bool bind_unprivileged_p
= false);
414 static void register_function_and_statement_variants(match_node
* root
,
416 bool bind_unprivileged_p
= false);
417 static void register_patterns(systemtap_session
& s
);
420 dwarf_derived_probe(probe
*base
,
421 probe_point
*location
,
424 derived_probe(base
, location
), addr(addr
), has_return(has_return
),
425 has_maxactive(0), maxactive_val(0), access_vars(false),
426 saved_longs(0), saved_strings(0), entry_handler(0)
431 void saveargs(dwarf_query
& q
, Dwarf_Die
* scope_die
, Dwarf_Addr dwfl_addr
);
435 struct uprobe_derived_probe
: public dwarf_derived_probe
437 int pid
; // 0 => unrestricted
439 uprobe_derived_probe (const string
& function
,
440 const string
& filename
,
442 const string
& module
,
443 const string
& section
,
444 Dwarf_Addr dwfl_addr
,
447 Dwarf_Die
* scope_die
):
448 dwarf_derived_probe(function
, filename
, line
, module
, section
,
449 dwfl_addr
, addr
, q
, scope_die
), pid(0)
452 // alternate constructor for process(PID).statement(ADDR).absolute
453 uprobe_derived_probe (probe
*base
,
454 probe_point
*location
,
458 dwarf_derived_probe(base
, location
, addr
, has_return
), pid(pid
)
461 void join_group (systemtap_session
& s
);
463 void emit_unprivileged_assertion (translator_output
*);
464 void print_dupe_stamp(ostream
& o
) { print_dupe_stamp_unprivileged_process_owner (o
); }
465 void getargs(std::list
<std::string
> &arg_set
) const;
466 void saveargs(int nargs
);
471 struct dwarf_derived_probe_group
: public derived_probe_group
475 multimap
<string
,dwarf_derived_probe
*> probes_by_module
;
476 typedef multimap
<string
,dwarf_derived_probe
*>::iterator p_b_m_iterator
;
479 dwarf_derived_probe_group(): has_semaphores(false) {}
480 void enroll (dwarf_derived_probe
* probe
);
481 void emit_module_decls (systemtap_session
& s
);
482 void emit_module_init (systemtap_session
& s
);
483 void emit_module_exit (systemtap_session
& s
);
487 // Helper struct to thread through the dwfl callbacks.
490 base_query(dwflpp
& dw
, literal_map_t
const & params
);
491 base_query(dwflpp
& dw
, const string
& module_val
);
492 virtual ~base_query() {}
494 systemtap_session
& sess
;
497 // Parameter extractors.
498 static bool has_null_param(literal_map_t
const & params
,
500 static bool get_string_param(literal_map_t
const & params
,
501 string
const & k
, string
& v
);
502 static bool get_number_param(literal_map_t
const & params
,
503 string
const & k
, long & v
);
504 static bool get_number_param(literal_map_t
const & params
,
505 string
const & k
, Dwarf_Addr
& v
);
507 // Extracted parameters.
512 string module_val
; // has_kernel => module_val = "kernel"
513 string path
; // executable path if module is a .so
515 virtual void handle_query_module() = 0;
519 base_query::base_query(dwflpp
& dw
, literal_map_t
const & params
):
520 sess(dw
.sess
), dw(dw
)
522 has_kernel
= has_null_param (params
, TOK_KERNEL
);
524 module_val
= "kernel";
526 has_module
= get_string_param (params
, TOK_MODULE
, module_val
);
532 has_process
= get_string_param(params
, TOK_PROCESS
, module_val
);
533 has_library
= get_string_param (params
, TOK_LIBRARY
, library_name
);
536 path
= find_executable (module_val
);
537 module_val
= find_executable (library_name
, "LD_LIBRARY_PATH");
539 else if (has_process
)
540 module_val
= find_executable (module_val
);
543 assert (has_kernel
|| has_process
|| has_module
);
546 base_query::base_query(dwflpp
& dw
, const string
& module_val
)
547 : sess(dw
.sess
), dw(dw
), module_val(module_val
)
549 // NB: This uses '/' to distinguish between kernel modules and userspace,
550 // which means that userspace modules won't get any PATH searching.
551 if (module_val
.find('/') == string::npos
)
553 has_kernel
= (module_val
== TOK_KERNEL
);
554 has_module
= !has_kernel
;
559 has_kernel
= has_module
= false;
565 base_query::has_null_param(literal_map_t
const & params
,
568 return derived_probe_builder::has_null_param(params
, k
);
573 base_query::get_string_param(literal_map_t
const & params
,
574 string
const & k
, string
& v
)
576 return derived_probe_builder::get_param (params
, k
, v
);
581 base_query::get_number_param(literal_map_t
const & params
,
582 string
const & k
, long & v
)
585 bool present
= derived_probe_builder::get_param (params
, k
, value
);
592 base_query::get_number_param(literal_map_t
const & params
,
593 string
const & k
, Dwarf_Addr
& v
)
596 bool present
= derived_probe_builder::get_param (params
, k
, value
);
597 v
= (Dwarf_Addr
) value
;
601 struct dwarf_query
: public base_query
603 dwarf_query(probe
* base_probe
,
604 probe_point
* base_loc
,
606 literal_map_t
const & params
,
607 vector
<derived_probe
*> & results
,
608 const string user_path
,
609 const string user_lib
);
611 vector
<derived_probe
*> & results
;
612 set
<string
> inlined_non_returnable
; // function names
614 probe_point
* base_loc
;
618 virtual void handle_query_module();
619 void query_module_dwarf();
620 void query_module_symtab();
622 void add_probe_point(string
const & funcname
,
623 char const * filename
,
625 Dwarf_Die
*scope_die
,
628 // Track addresses we've already seen in a given module
629 set
<Dwarf_Addr
> alias_dupes
;
631 // Track inlines we've already seen as well
632 // NB: this can't be compared just by entrypc, as inlines can overlap
633 set
<inline_instance_info
> inline_dupes
;
635 // Extracted parameters.
638 bool has_function_str
;
639 bool has_statement_str
;
640 bool has_function_num
;
641 bool has_statement_num
;
642 string statement_str_val
;
643 string function_str_val
;
644 Dwarf_Addr statement_num_val
;
645 Dwarf_Addr function_num_val
;
664 enum dbinfo_reqt dbinfo_reqt
;
665 enum dbinfo_reqt
assess_dbinfo_reqt();
667 void parse_function_spec(const string
& spec
);
668 function_spec_type spec_type
;
669 vector
<string
> scopes
;
674 bool query_done
; // Found exact match
676 set
<string
> filtered_srcfiles
;
678 // Map official entrypc -> func_info object
679 inline_instance_map_t filtered_inlines
;
680 func_info_map_t filtered_functions
;
681 bool choose_next_line
;
682 Dwarf_Addr entrypc_for_next_line
;
684 void query_module_functions ();
688 static void delete_session_module_cache (systemtap_session
& s
); // forward decl
691 struct dwarf_builder
: public derived_probe_builder
693 map
<string
,dwflpp
*> kern_dw
; /* NB: key string could be a wildcard */
694 map
<string
,dwflpp
*> user_dw
;
699 dwflpp
*get_kern_dw(systemtap_session
& sess
, const string
& module
)
701 if (kern_dw
[module
] == 0)
702 kern_dw
[module
] = new dwflpp(sess
, module
, true); // might throw
703 return kern_dw
[module
];
706 dwflpp
*get_user_dw(systemtap_session
& sess
, const string
& module
)
708 if (user_dw
[module
] == 0)
709 user_dw
[module
] = new dwflpp(sess
, module
, false); // might throw
710 return user_dw
[module
];
713 /* NB: not virtual, so can be called from dtor too: */
714 void dwarf_build_no_more (bool verbose
)
720 void build_no_more (systemtap_session
&s
)
722 dwarf_build_no_more (s
.verbose
> 3);
723 delete_session_module_cache (s
);
728 dwarf_build_no_more (false);
731 virtual void build(systemtap_session
& sess
,
733 probe_point
* location
,
734 literal_map_t
const & parameters
,
735 vector
<derived_probe
*> & finished_results
);
739 dwarf_query::dwarf_query(probe
* base_probe
,
740 probe_point
* base_loc
,
742 literal_map_t
const & params
,
743 vector
<derived_probe
*> & results
,
744 const string user_path
,
745 const string user_lib
)
746 : base_query(dw
, params
), results(results
),
747 base_probe(base_probe
), base_loc(base_loc
),
748 user_path(user_path
), user_lib(user_lib
)
750 // Reduce the query to more reasonable semantic values (booleans,
751 // extracted strings, numbers, etc).
752 has_function_str
= get_string_param(params
, TOK_FUNCTION
, function_str_val
);
753 has_function_num
= get_number_param(params
, TOK_FUNCTION
, function_num_val
);
755 has_statement_str
= get_string_param(params
, TOK_STATEMENT
, statement_str_val
);
756 has_statement_num
= get_number_param(params
, TOK_STATEMENT
, statement_num_val
);
758 has_label
= get_string_param(params
, TOK_LABEL
, label_val
);
760 has_call
= has_null_param(params
, TOK_CALL
);
761 has_inline
= has_null_param(params
, TOK_INLINE
);
762 has_return
= has_null_param(params
, TOK_RETURN
);
763 has_maxactive
= get_number_param(params
, TOK_MAXACTIVE
, maxactive_val
);
764 has_absolute
= has_null_param(params
, TOK_ABSOLUTE
);
767 if (has_function_str
)
768 parse_function_spec(function_str_val
);
769 else if (has_statement_str
)
770 parse_function_spec(statement_str_val
);
772 dbinfo_reqt
= assess_dbinfo_reqt();
778 get_filtered_functions(dwarf_query
*q
)
780 return &q
->filtered_functions
;
784 inline_instance_map_t
*
785 get_filtered_inlines(dwarf_query
*q
)
787 return &q
->filtered_inlines
;
792 dwarf_query::query_module_dwarf()
794 if (has_function_num
|| has_statement_num
)
796 // If we have module("foo").function(0xbeef) or
797 // module("foo").statement(0xbeef), the address is relative
798 // to the start of the module, so we seek the function
799 // number plus the module's bias.
800 Dwarf_Addr addr
= has_function_num
?
801 function_num_val
: statement_num_val
;
803 // These are raw addresses, we need to know what the elf_bias
804 // is to feed it to libdwfl based functions.
806 Elf
*elf
= dwfl_module_getelf (dw
.module
, &elf_bias
);
809 query_addr(addr
, this);
813 // Otherwise if we have a function("foo") or statement("foo")
814 // specifier, we have to scan over all the CUs looking for
815 // the function(s) in question
816 assert(has_function_str
|| has_statement_str
);
818 // For simple cases, no wildcard and no source:line, we can do a very
819 // quick function lookup in a module-wide cache.
820 if (spec_type
== function_alone
&&
821 !dw
.name_has_wildcard(function
) &&
822 !startswith(function
, "_Z"))
823 query_module_functions();
825 dw
.iterate_over_cus(&query_cu
, this);
829 static void query_func_info (Dwarf_Addr entrypc
, func_info
& fi
,
833 dwarf_query::query_module_symtab()
835 // Get the symbol table if it's necessary, sufficient, and not already got.
836 if (dbinfo_reqt
== dbr_need_dwarf
)
839 module_info
*mi
= dw
.mod_info
;
840 if (dbinfo_reqt
== dbr_need_symtab
)
842 if (mi
->symtab_status
== info_unknown
)
843 mi
->get_symtab(this);
844 if (mi
->symtab_status
== info_absent
)
848 func_info
*fi
= NULL
;
849 symbol_table
*sym_table
= mi
->sym_table
;
851 if (has_function_str
)
853 // Per dwarf_query::assess_dbinfo_reqt()...
854 assert(spec_type
== function_alone
);
855 if (dw
.name_has_wildcard(function_str_val
))
857 // Until we augment the blacklist sufficently...
858 if (function_str_val
.find_first_not_of("*?") == string::npos
)
860 // e.g., kernel.function("*")
861 cerr
<< "Error: Pattern '"
863 << "' matches every instruction address in the symbol table,"
865 << "some of which aren't even functions."
866 << " Please be more precise."
870 symbol_table::iterator_t iter
;
871 for (iter
= sym_table
->map_by_addr
.begin();
872 iter
!= sym_table
->map_by_addr
.end();
876 if (!null_die(&fi
->die
))
877 continue; // already handled in query_module_dwarf()
878 if (dw
.function_name_matches_pattern(fi
->name
, function_str_val
))
879 query_func_info(fi
->addr
, *fi
, this);
884 fi
= sym_table
->lookup_symbol(function_str_val
);
885 if (fi
&& !fi
->descriptor
&& null_die(&fi
->die
))
886 query_func_info(fi
->addr
, *fi
, this);
891 assert(has_function_num
|| has_statement_num
);
892 // Find the "function" in which the indicated address resides.
894 (has_function_num
? function_num_val
: statement_num_val
);
895 fi
= sym_table
->get_func_containing_address(addr
);
898 if (! sess
.suppress_warnings
)
899 cerr
<< "Warning: address "
900 << hex
<< addr
<< dec
901 << " out of range for module "
905 if (!null_die(&fi
->die
))
907 // addr looks like it's in the compilation unit containing
908 // the indicated function, but query_module_dwarf() didn't
909 // match addr to any compilation unit, so addr must be
910 // above that cu's address range.
911 if (! sess
.suppress_warnings
)
912 cerr
<< "Warning: address "
913 << hex
<< addr
<< dec
914 << " maps to no known compilation unit in module "
918 query_func_info(fi
->addr
, *fi
, this);
923 dwarf_query::handle_query_module()
925 bool report
= dbinfo_reqt
== dbr_need_dwarf
|| !sess
.consult_symtab
;
926 dw
.get_module_dwarf(false, report
);
928 // prebuild the symbol table to resolve aliases
929 dw
.mod_info
->get_symtab(this);
931 // reset the dupe-checking for each new module
933 inline_dupes
.clear();
935 if (dw
.mod_info
->dwarf_status
== info_present
)
936 query_module_dwarf();
938 // Consult the symbol table if we haven't found all we're looking for.
939 // asm functions can show up in the symbol table but not in dwarf.
940 if (sess
.consult_symtab
&& !query_done
)
941 query_module_symtab();
946 dwarf_query::parse_function_spec(const string
& spec
)
948 line_type
= ABSOLUTE
;
949 line
[0] = line
[1] = 0;
951 size_t src_pos
, line_pos
, dash_pos
, scope_pos
, next_scope_pos
;
953 // look for named scopes
955 next_scope_pos
= spec
.find("::");
956 while (next_scope_pos
!= string::npos
)
958 scopes
.push_back(spec
.substr(scope_pos
, next_scope_pos
- scope_pos
));
959 scope_pos
= next_scope_pos
+ 2;
960 next_scope_pos
= spec
.find("::", scope_pos
);
963 // look for a source separator
964 src_pos
= spec
.find('@', scope_pos
);
965 if (src_pos
== string::npos
)
967 function
= spec
.substr(scope_pos
);
968 spec_type
= function_alone
;
972 function
= spec
.substr(scope_pos
, src_pos
- scope_pos
);
974 // look for a line-number separator
975 line_pos
= spec
.find_first_of(":+", src_pos
);
976 if (line_pos
== string::npos
)
978 file
= spec
.substr(src_pos
+ 1);
979 spec_type
= function_and_file
;
983 file
= spec
.substr(src_pos
+ 1, line_pos
- src_pos
- 1);
985 // classify the line spec
986 spec_type
= function_file_and_line
;
987 if (spec
[line_pos
] == '+')
988 line_type
= RELATIVE
;
989 else if (spec
[line_pos
+ 1] == '*' &&
990 spec
.length() == line_pos
+ 2)
991 line_type
= WILDCARD
;
993 line_type
= ABSOLUTE
;
995 if (line_type
!= WILDCARD
)
998 // try to parse either N or N-M
999 dash_pos
= spec
.find('-', line_pos
+ 1);
1000 if (dash_pos
== string::npos
)
1001 line
[0] = line
[1] = lex_cast
<int>(spec
.substr(line_pos
+ 1));
1005 line
[0] = lex_cast
<int>(spec
.substr(line_pos
+ 1,
1006 dash_pos
- line_pos
- 1));
1007 line
[1] = lex_cast
<int>(spec
.substr(dash_pos
+ 1));
1010 catch (runtime_error
& exn
)
1017 if (function
.empty() ||
1018 (spec_type
!= function_alone
&& file
.empty()))
1021 if (sess
.verbose
> 2)
1023 clog
<< "parsed '" << spec
<< "'";
1025 if (!scopes
.empty())
1026 clog
<< ", scope '" << scopes
[0] << "'";
1027 for (unsigned i
= 1; i
< scopes
.size(); ++i
)
1028 clog
<< "::'" << scopes
[i
] << "'";
1030 clog
<< ", func '" << function
<< "'";
1032 if (spec_type
!= function_alone
)
1033 clog
<< ", file '" << file
<< "'";
1035 if (spec_type
== function_file_and_line
)
1045 clog
<< "+" << line
[0];
1049 clog
<< line
[0] << " - " << line
[1];
1064 throw semantic_error("malformed specification '" + spec
+ "'",
1070 dwarf_query::add_probe_point(const string
& dw_funcname
,
1071 const char* filename
,
1073 Dwarf_Die
* scope_die
,
1076 string reloc_section
; // base section for relocation purposes
1077 Dwarf_Addr reloc_addr
; // relocated
1078 const string
& module
= dw
.module_name
; // "kernel" or other
1079 string funcname
= dw_funcname
;
1081 assert (! has_absolute
); // already handled in dwarf_builder::build()
1083 reloc_addr
= dw
.relocate_address(addr
, reloc_section
);
1085 // If we originally used the linkage name, then let's call it that way
1086 const char* linkage_name
;
1087 if (scope_die
&& startswith (this->function
, "_Z")
1088 && (linkage_name
= dwarf_linkage_name (scope_die
)))
1089 funcname
= linkage_name
;
1091 if (sess
.verbose
> 1)
1093 clog
<< "probe " << funcname
<< "@" << filename
<< ":" << line
;
1094 if (string(module
) == TOK_KERNEL
)
1096 else if (has_module
)
1097 clog
<< " module=" << module
;
1098 else if (has_process
)
1099 clog
<< " process=" << module
;
1100 if (reloc_section
!= "") clog
<< " reloc=" << reloc_section
;
1101 clog
<< " pc=0x" << hex
<< addr
<< dec
;
1104 bool bad
= dw
.blacklisted_p (funcname
, filename
, line
, module
,
1106 if (sess
.verbose
> 1)
1109 if (module
== TOK_KERNEL
)
1111 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1112 reloc_addr
= addr
- sess
.sym_stext
;
1113 reloc_section
= "_stext"; // a message to runtime's _stp_module_relocate
1118 sess
.unwindsym_modules
.insert (module
);
1122 results
.push_back (new uprobe_derived_probe(funcname
, filename
, line
,
1123 module
, reloc_section
, addr
, reloc_addr
,
1128 assert (has_kernel
|| has_module
);
1129 results
.push_back (new dwarf_derived_probe(funcname
, filename
, line
,
1130 module
, reloc_section
, addr
, reloc_addr
,
1137 dwarf_query::assess_dbinfo_reqt()
1141 // kernel.statement(NUM).absolute
1146 // kernel.function("f").inline or module("m").function("f").inline
1147 return dbr_need_dwarf
;
1149 if (has_function_str
&& spec_type
== function_alone
)
1151 // kernel.function("f") or module("m").function("f")
1152 return dbr_need_symtab
;
1154 if (has_statement_num
)
1156 // kernel.statement(NUM) or module("m").statement(NUM)
1157 // Technically, all we need is the module offset (or _stext, for
1158 // the kernel). But for that we need either the ELF file or (for
1159 // _stext) the symbol table. In either case, the symbol table
1160 // is available, and that allows us to map the NUM (address)
1161 // to a function, which is goodness.
1162 return dbr_need_symtab
;
1164 if (has_function_num
)
1166 // kernel.function(NUM) or module("m").function(NUM)
1167 // Need the symbol table so we can back up from NUM to the
1168 // start of the function.
1169 return dbr_need_symtab
;
1171 // Symbol table tells us nothing about source files or line numbers.
1172 return dbr_need_dwarf
;
1176 // The critical determining factor when interpreting a pattern
1177 // string is, perhaps surprisingly: "presence of a lineno". The
1178 // presence of a lineno changes the search strategy completely.
1180 // Compare the two cases:
1182 // 1. {statement,function}(foo@file.c:lineno)
1183 // - find the files matching file.c
1184 // - in each file, find the functions matching foo
1185 // - query the file for line records matching lineno
1186 // - iterate over the line records,
1187 // - and iterate over the functions,
1188 // - if(haspc(function.DIE, line.addr))
1189 // - if looking for statements: probe(lineno.addr)
1190 // - if looking for functions: probe(function.{entrypc,return,etc.})
1192 // 2. {statement,function}(foo@file.c)
1193 // - find the files matching file.c
1194 // - in each file, find the functions matching foo
1195 // - probe(function.{entrypc,return,etc.})
1197 // Thus the first decision we make is based on the presence of a
1198 // lineno, and we enter entirely different sets of callbacks
1199 // depending on that decision.
1201 // Note that the first case is a generalization fo the second, in that
1202 // we could theoretically search through line records for matching
1203 // file names (a "table scan" in rdbms lingo). Luckily, file names
1204 // are already cached elsewhere, so we can do an "index scan" as an
1208 query_statement (string
const & func
,
1211 Dwarf_Die
*scope_die
,
1212 Dwarf_Addr stmt_addr
,
1217 q
->add_probe_point(func
, file
? file
: "",
1218 line
, scope_die
, stmt_addr
);
1220 catch (const semantic_error
& e
)
1222 q
->sess
.print_error (e
);
1227 query_addr(Dwarf_Addr addr
, dwarf_query
*q
)
1231 if (q
->sess
.verbose
> 2)
1232 clog
<< "query_addr 0x" << hex
<< addr
<< dec
<< endl
;
1234 // First pick which CU contains this address
1235 Dwarf_Die
* cudie
= dw
.query_cu_containing_address(addr
);
1236 if (!cudie
) // address could be wildly out of range
1238 dw
.focus_on_cu(cudie
);
1240 // Now compensate for the dw bias
1241 addr
-= dw
.module_bias
;
1243 // Per PR5787, we look up the scope die even for
1244 // statement_num's, for blacklist sensitivity and $var
1245 // resolution purposes.
1247 // Find the scopes containing this address
1248 vector
<Dwarf_Die
> scopes
= dw
.getscopes(addr
);
1252 // Look for the innermost containing function
1253 Dwarf_Die
*fnscope
= NULL
;
1254 for (size_t i
= 0; i
< scopes
.size(); ++i
)
1256 int tag
= dwarf_tag(&scopes
[i
]);
1257 if ((tag
== DW_TAG_subprogram
&& !q
->has_inline
) ||
1258 (tag
== DW_TAG_inlined_subroutine
&&
1259 !q
->has_call
&& !q
->has_return
))
1261 fnscope
= &scopes
[i
];
1267 dw
.focus_on_function(fnscope
);
1269 Dwarf_Die
*scope
= q
->has_function_num
? fnscope
: &scopes
[0];
1271 const char *file
= dwarf_decl_file(fnscope
);
1273 dwarf_decl_line(fnscope
, &line
);
1275 // Function probes should reset the addr to the function entry
1276 // and possibly perform prologue searching
1277 if (q
->has_function_num
)
1279 dw
.die_entrypc(fnscope
, &addr
);
1280 if (dwarf_tag(fnscope
) == DW_TAG_subprogram
&&
1281 (q
->sess
.prologue_searching
|| q
->has_process
)) // PR 6871
1284 func
.die
= *fnscope
;
1285 func
.name
= dw
.function_name
;
1286 func
.decl_file
= file
;
1287 func
.decl_line
= line
;
1288 func
.entrypc
= addr
;
1290 func_info_map_t
funcs(1, func
);
1291 dw
.resolve_prologue_endings (funcs
);
1292 if (funcs
[0].prologue_end
)
1293 addr
= funcs
[0].prologue_end
;
1298 dwarf_line_t
address_line(dwarf_getsrc_die(cudie
, addr
));
1301 file
= address_line
.linesrc();
1302 line
= address_line
.lineno();
1305 // Verify that a raw address matches the beginning of a
1306 // statement. This is a somewhat lame check that the address
1307 // is at the start of an assembly instruction. Mark probes are in the
1308 // middle of a macro and thus not strictly at a statement beginning.
1309 // Guru mode may override this check.
1310 if (!q
->has_mark
&& (!address_line
|| address_line
.addr() != addr
))
1313 msg
<< "address 0x" << hex
<< addr
1314 << " does not match the beginning of a statement";
1316 msg
<< " (try 0x" << hex
<< address_line
.addr() << ")";
1318 msg
<< " (no line info found for '" << dw
.cu_name()
1319 << "', in module '" << dw
.module_name
<< "')";
1320 if (! q
->sess
.guru_mode
)
1321 throw semantic_error(msg
.str());
1322 else if (! q
->sess
.suppress_warnings
)
1323 q
->sess
.print_warning(msg
.str());
1327 // Build a probe at this point
1328 query_statement(dw
.function_name
, file
, line
, scope
, addr
, q
);
1332 query_label (string
const & func
,
1336 Dwarf_Die
*scope_die
,
1337 Dwarf_Addr stmt_addr
,
1340 assert (q
->has_statement_str
|| q
->has_function_str
);
1342 size_t i
= q
->results
.size();
1344 // weed out functions whose decl_file isn't one of
1345 // the source files that we actually care about
1346 if (q
->spec_type
!= function_alone
&&
1347 q
->filtered_srcfiles
.count(file
) == 0)
1350 query_statement(func
, file
, line
, scope_die
, stmt_addr
, q
);
1352 // after the fact, insert the label back into the derivation chain
1353 probe_point::component
* ppc
=
1354 new probe_point::component(TOK_LABEL
, new literal_string (label
));
1355 for (; i
< q
->results
.size(); ++i
)
1357 derived_probe
* p
= q
->results
[i
];
1358 probe_point
* pp
= new probe_point(*p
->locations
[0]);
1359 pp
->components
.push_back (ppc
);
1360 p
->base
= p
->base
->create_alias(p
->locations
[0], pp
);
1365 query_inline_instance_info (inline_instance_info
& ii
,
1370 assert (! q
->has_return
); // checked by caller already
1371 if (q
->sess
.verbose
>2)
1372 clog
<< "querying entrypc "
1373 << hex
<< ii
.entrypc
<< dec
1374 << " of instance of inline '" << ii
.name
<< "'\n";
1375 query_statement (ii
.name
, ii
.decl_file
, ii
.decl_line
,
1376 &ii
.die
, ii
.entrypc
, q
);
1378 catch (semantic_error
&e
)
1380 q
->sess
.print_error (e
);
1385 query_func_info (Dwarf_Addr entrypc
,
1393 // NB. dwarf_derived_probe::emit_registrations will emit a
1394 // kretprobe based on the entrypc in this case.
1395 query_statement (fi
.name
, fi
.decl_file
, fi
.decl_line
,
1396 &fi
.die
, entrypc
, q
);
1400 if (fi
.prologue_end
!= 0)
1402 query_statement (fi
.name
, fi
.decl_file
, fi
.decl_line
,
1403 &fi
.die
, fi
.prologue_end
, q
);
1407 query_statement (fi
.name
, fi
.decl_file
, fi
.decl_line
,
1408 &fi
.die
, entrypc
, q
);
1412 catch (semantic_error
&e
)
1414 q
->sess
.print_error (e
);
1420 query_srcfile_label (const dwarf_line_t
& line
, void * arg
)
1422 dwarf_query
* q
= static_cast<dwarf_query
*>(arg
);
1424 Dwarf_Addr addr
= line
.addr();
1426 for (func_info_map_t::iterator i
= q
->filtered_functions
.begin();
1427 i
!= q
->filtered_functions
.end(); ++i
)
1428 if (q
->dw
.die_has_pc (i
->die
, addr
))
1429 q
->dw
.iterate_over_labels (&i
->die
, q
->label_val
, i
->name
,
1432 for (inline_instance_map_t::iterator i
= q
->filtered_inlines
.begin();
1433 i
!= q
->filtered_inlines
.end(); ++i
)
1434 if (q
->dw
.die_has_pc (i
->die
, addr
))
1435 q
->dw
.iterate_over_labels (&i
->die
, q
->label_val
, i
->name
,
1440 query_srcfile_line (const dwarf_line_t
& line
, void * arg
)
1442 dwarf_query
* q
= static_cast<dwarf_query
*>(arg
);
1444 Dwarf_Addr addr
= line
.addr();
1446 int lineno
= line
.lineno();
1448 for (func_info_map_t::iterator i
= q
->filtered_functions
.begin();
1449 i
!= q
->filtered_functions
.end(); ++i
)
1451 if (q
->dw
.die_has_pc (i
->die
, addr
))
1453 if (q
->sess
.verbose
>3)
1454 clog
<< "function DIE lands on srcfile\n";
1455 if (q
->has_statement_str
)
1458 q
->dw
.inner_die_containing_pc(i
->die
, addr
, scope
);
1459 query_statement (i
->name
, i
->decl_file
,
1460 lineno
, // NB: not q->line !
1464 query_func_info (i
->entrypc
, *i
, q
);
1468 for (inline_instance_map_t::iterator i
1469 = q
->filtered_inlines
.begin();
1470 i
!= q
->filtered_inlines
.end(); ++i
)
1472 if (q
->dw
.die_has_pc (i
->die
, addr
))
1474 if (q
->sess
.verbose
>3)
1475 clog
<< "inline instance DIE lands on srcfile\n";
1476 if (q
->has_statement_str
)
1479 q
->dw
.inner_die_containing_pc(i
->die
, addr
, scope
);
1480 query_statement (i
->name
, i
->decl_file
,
1481 q
->line
[0], &scope
, addr
, q
);
1484 query_inline_instance_info (*i
, q
);
1491 inline_instance_info::operator<(const inline_instance_info
& other
) const
1493 if (entrypc
!= other
.entrypc
)
1494 return entrypc
< other
.entrypc
;
1496 if (decl_line
!= other
.decl_line
)
1497 return decl_line
< other
.decl_line
;
1499 int cmp
= name
.compare(other
.name
);
1501 cmp
= strcmp(decl_file
, other
.decl_file
);
1507 query_dwarf_inline_instance (Dwarf_Die
* die
, void * arg
)
1509 dwarf_query
* q
= static_cast<dwarf_query
*>(arg
);
1510 assert (q
->has_statement_str
|| q
->has_function_str
);
1511 assert (!q
->has_call
&& !q
->has_return
);
1515 if (q
->sess
.verbose
>2)
1516 clog
<< "selected inline instance of " << q
->dw
.function_name
<< "\n";
1519 if (q
->dw
.die_entrypc (die
, &entrypc
))
1521 inline_instance_info inl
;
1523 inl
.name
= q
->dw
.function_name
;
1524 inl
.entrypc
= entrypc
;
1525 q
->dw
.function_file (&inl
.decl_file
);
1526 q
->dw
.function_line (&inl
.decl_line
);
1528 // make sure that this inline hasn't already
1529 // been matched from a different CU
1530 if (q
->inline_dupes
.insert(inl
).second
)
1531 q
->filtered_inlines
.push_back(inl
);
1535 catch (const semantic_error
& e
)
1537 q
->sess
.print_error (e
);
1538 return DWARF_CB_ABORT
;
1543 query_dwarf_func (Dwarf_Die
* func
, base_query
* bq
)
1545 dwarf_query
* q
= static_cast<dwarf_query
*>(bq
);
1546 assert (q
->has_statement_str
|| q
->has_function_str
);
1548 // weed out functions whose decl_file isn't one of
1549 // the source files that we actually care about
1550 if (q
->spec_type
!= function_alone
&&
1551 q
->filtered_srcfiles
.count(dwarf_decl_file(func
)?:"") == 0)
1556 q
->dw
.focus_on_function (func
);
1558 if (!q
->dw
.function_scope_matches(q
->scopes
))
1561 // make sure that this function address hasn't
1562 // already been matched under an aliased name
1564 if (!q
->dw
.func_is_inline() &&
1565 dwarf_entrypc(func
, &addr
) == 0 &&
1566 !q
->alias_dupes
.insert(addr
).second
)
1569 if (q
->dw
.func_is_inline () && (! q
->has_call
) && (! q
->has_return
))
1571 if (q
->sess
.verbose
>3)
1572 clog
<< "checking instances of inline " << q
->dw
.function_name
1574 q
->dw
.iterate_over_inline_instances (query_dwarf_inline_instance
, q
);
1576 else if (q
->dw
.func_is_inline () && (q
->has_return
)) // PR 11553
1578 q
->inlined_non_returnable
.insert (q
->dw
.function_name
);
1580 else if (!q
->dw
.func_is_inline () && (! q
->has_inline
))
1582 if (q
->sess
.verbose
>2)
1583 clog
<< "selected function " << q
->dw
.function_name
<< "\n";
1586 q
->dw
.function_die (&func
.die
);
1587 func
.name
= q
->dw
.function_name
;
1588 q
->dw
.function_file (&func
.decl_file
);
1589 q
->dw
.function_line (&func
.decl_line
);
1592 if (q
->dw
.function_entrypc (&entrypc
))
1594 func
.entrypc
= entrypc
;
1595 q
->filtered_functions
.push_back (func
);
1597 /* else this function is fully inlined, just ignore it */
1601 catch (const semantic_error
& e
)
1603 q
->sess
.print_error (e
);
1604 return DWARF_CB_ABORT
;
1609 query_cu (Dwarf_Die
* cudie
, void * arg
)
1611 dwarf_query
* q
= static_cast<dwarf_query
*>(arg
);
1612 assert (q
->has_statement_str
|| q
->has_function_str
);
1614 if (pending_interrupts
) return DWARF_CB_ABORT
;
1618 q
->dw
.focus_on_cu (cudie
);
1620 if (false && q
->sess
.verbose
>2)
1621 clog
<< "focused on CU '" << q
->dw
.cu_name()
1622 << "', in module '" << q
->dw
.module_name
<< "'\n";
1624 q
->filtered_srcfiles
.clear();
1625 q
->filtered_functions
.clear();
1626 q
->filtered_inlines
.clear();
1628 // In this path, we find "abstract functions", record
1629 // information about them, and then (depending on lineno
1630 // matching) possibly emit one or more of the function's
1631 // associated addresses. Unfortunately the control of this
1632 // cannot easily be turned inside out.
1634 if (q
->spec_type
!= function_alone
)
1636 // If we have a pattern string with a filename, we need
1637 // to elaborate the srcfile mask in question first.
1638 q
->dw
.collect_srcfiles_matching (q
->file
, q
->filtered_srcfiles
);
1640 // If we have a file pattern and *no* srcfile matches, there's
1641 // no need to look further into this CU, so skip.
1642 if (q
->filtered_srcfiles
.empty())
1646 // Pick up [entrypc, name, DIE] tuples for all the functions
1647 // matching the query, and fill in the prologue endings of them
1648 // all in a single pass.
1649 int rc
= q
->dw
.iterate_over_functions (query_dwarf_func
, q
, q
->function
);
1650 if (rc
!= DWARF_CB_OK
)
1651 q
->query_done
= true;
1653 if ((q
->sess
.prologue_searching
|| q
->has_process
) // PR 6871
1654 && !q
->has_statement_str
) // PR 2608
1655 if (! q
->filtered_functions
.empty())
1656 q
->dw
.resolve_prologue_endings (q
->filtered_functions
);
1658 if (q
->spec_type
== function_file_and_line
)
1660 // .statement(...:NN) often gets mixed up with .function(...:NN)
1661 if (q
->has_function_str
&& ! q
->sess
.suppress_warnings
)
1662 q
->sess
.print_warning ("For probing a particular line, use a "
1663 ".statement() probe, not .function()",
1664 q
->base_probe
->tok
);
1666 // If we have a pattern string with target *line*, we
1667 // have to look at lines in all the matched srcfiles.
1668 void (* callback
) (const dwarf_line_t
&, void*) =
1669 q
->has_label
? query_srcfile_label
: query_srcfile_line
;
1670 for (set
<string
>::const_iterator i
= q
->filtered_srcfiles
.begin();
1671 i
!= q
->filtered_srcfiles
.end(); ++i
)
1672 q
->dw
.iterate_over_srcfile_lines (i
->c_str(), q
->line
, q
->has_statement_str
,
1673 q
->line_type
, callback
, q
->function
, q
);
1675 else if (q
->has_label
)
1677 for (func_info_map_t::iterator i
= q
->filtered_functions
.begin();
1678 i
!= q
->filtered_functions
.end(); ++i
)
1679 q
->dw
.iterate_over_labels (&i
->die
, q
->label_val
, i
->name
,
1682 for (inline_instance_map_t::iterator i
= q
->filtered_inlines
.begin();
1683 i
!= q
->filtered_inlines
.end(); ++i
)
1684 q
->dw
.iterate_over_labels (&i
->die
, q
->label_val
, i
->name
,
1689 // Otherwise, simply probe all resolved functions.
1690 for (func_info_map_t::iterator i
= q
->filtered_functions
.begin();
1691 i
!= q
->filtered_functions
.end(); ++i
)
1692 query_func_info (i
->entrypc
, *i
, q
);
1694 // And all inline instances (if we're not excluding inlines with ".call")
1696 for (inline_instance_map_t::iterator i
1697 = q
->filtered_inlines
.begin(); i
!= q
->filtered_inlines
.end(); ++i
)
1698 query_inline_instance_info (*i
, q
);
1702 catch (const semantic_error
& e
)
1704 q
->sess
.print_error (e
);
1705 return DWARF_CB_ABORT
;
1711 dwarf_query::query_module_functions ()
1715 filtered_srcfiles
.clear();
1716 filtered_functions
.clear();
1717 filtered_inlines
.clear();
1719 // Collect all module functions so we know which CUs are interesting
1720 int rc
= dw
.iterate_single_function(query_dwarf_func
, this, function
);
1721 if (rc
!= DWARF_CB_OK
)
1727 set
<void*> used_cus
; // by cu->addr
1728 vector
<Dwarf_Die
> cus
;
1731 for (func_info_map_t::iterator i
= filtered_functions
.begin();
1732 i
!= filtered_functions
.end(); ++i
)
1733 if (dwarf_diecu(&i
->die
, &cu_mem
, NULL
, NULL
) &&
1734 used_cus
.insert(cu_mem
.addr
).second
)
1735 cus
.push_back(cu_mem
);
1737 for (inline_instance_map_t::iterator i
= filtered_inlines
.begin();
1738 i
!= filtered_inlines
.end(); ++i
)
1739 if (dwarf_diecu(&i
->die
, &cu_mem
, NULL
, NULL
) &&
1740 used_cus
.insert(cu_mem
.addr
).second
)
1741 cus
.push_back(cu_mem
);
1743 // Reset the dupes since we didn't actually collect them the first time
1744 alias_dupes
.clear();
1745 inline_dupes
.clear();
1747 // Run the query again on the individual CUs
1748 for (vector
<Dwarf_Die
>::iterator i
= cus
.begin(); i
!= cus
.end(); ++i
)
1749 query_cu(&*i
, this);
1751 catch (const semantic_error
& e
)
1753 sess
.print_error (e
);
1759 validate_module_elf (Dwfl_Module
*mod
, const char *name
, base_query
*q
)
1761 // Validate the machine code in this elf file against the
1762 // session machine. This is important, in case the wrong kind
1763 // of debuginfo is being automagically processed by elfutils.
1764 // While we can tell i686 apart from x86-64, unfortunately
1765 // we can't help confusing i586 vs i686 (both EM_386).
1768 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
1769 // because dwfl_module_getelf can force costly section relocations
1770 // we don't really need, while either will do for this purpose.
1771 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (mod
, &bias
))
1772 ?: dwfl_module_getelf (mod
, &bias
));
1775 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
1776 if (em
== 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
1777 int elf_machine
= em
->e_machine
;
1778 const char* debug_filename
= "";
1779 const char* main_filename
= "";
1780 (void) dwfl_module_info (mod
, NULL
, NULL
,
1784 const string
& sess_machine
= q
->sess
.architecture
;
1786 string expect_machine
; // to match sess.machine (i.e., kernel machine)
1787 string expect_machine2
;
1789 // NB: See also the 'uname -m' squashing done in main.cxx.
1790 switch (elf_machine
)
1792 // x86 and ppc are bi-architecture; a 64-bit kernel
1793 // can normally run either 32-bit or 64-bit *userspace*.
1795 expect_machine
= "i?86";
1796 if (! q
->has_process
) break; // 32-bit kernel/module
1799 expect_machine2
= "x86_64";
1803 expect_machine
= "powerpc";
1805 case EM_S390
: expect_machine
= "s390"; break;
1806 case EM_IA_64
: expect_machine
= "ia64"; break;
1807 case EM_ARM
: expect_machine
= "arm*"; break;
1808 // XXX: fill in some more of these
1809 default: expect_machine
= "?"; break;
1812 if (! debug_filename
) debug_filename
= main_filename
;
1813 if (! debug_filename
) debug_filename
= name
;
1815 if (fnmatch (expect_machine
.c_str(), sess_machine
.c_str(), 0) != 0 &&
1816 fnmatch (expect_machine2
.c_str(), sess_machine
.c_str(), 0) != 0)
1819 msg
<< "ELF machine " << expect_machine
<< "|" << expect_machine2
1820 << " (code " << elf_machine
1821 << ") mismatch with target " << sess_machine
1822 << " in '" << debug_filename
<< "'";
1823 throw semantic_error(msg
.str ());
1826 if (q
->sess
.verbose
>2)
1827 clog
<< "focused on module '" << q
->dw
.module_name
1828 << " = [0x" << hex
<< q
->dw
.module_start
1829 << "-0x" << q
->dw
.module_end
1830 << ", bias 0x" << q
->dw
.module_bias
<< "]" << dec
1831 << " file " << debug_filename
1832 << " ELF machine " << expect_machine
<< "|" << expect_machine2
1833 << " (code " << elf_machine
<< ")"
1840 lookup_symbol_address (Dwfl_Module
*m
, const char* wanted
)
1842 int syments
= dwfl_module_getsymtab(m
);
1844 for (int i
= 1; i
< syments
; ++i
)
1847 const char *name
= dwfl_module_getsym(m
, i
, &sym
, NULL
);
1848 if (name
!= NULL
&& strcmp(name
, wanted
) == 0)
1849 return sym
.st_value
;
1858 query_module (Dwfl_Module
*mod
,
1864 base_query
*q
= static_cast<base_query
*>(arg
);
1868 module_info
* mi
= q
->sess
.module_cache
->cache
[name
];
1871 mi
= q
->sess
.module_cache
->cache
[name
] = new module_info(name
);
1876 const char* debug_filename
= "";
1877 const char* main_filename
= "";
1878 (void) dwfl_module_info (mod
, NULL
, NULL
,
1883 if (q
->sess
.ignore_vmlinux
&& name
== TOK_KERNEL
)
1885 // report_kernel() in elfutils found vmlinux, but pretend it didn't.
1886 // Given a non-null path, returning 1 means keep reporting modules.
1887 mi
->dwarf_status
= info_absent
;
1889 else if (debug_filename
|| main_filename
)
1891 mi
->elf_path
= debug_filename
?: main_filename
;
1893 else if (name
== TOK_KERNEL
)
1895 mi
->dwarf_status
= info_absent
;
1898 // OK, enough of that module_info caching business.
1900 q
->dw
.focus_on_module(mod
, mi
);
1902 // If we have enough information in the pattern to skip a module and
1903 // the module does not match that information, return early.
1904 if (!q
->dw
.module_name_matches(q
->module_val
))
1905 return pending_interrupts
? DWARF_CB_ABORT
: DWARF_CB_OK
;
1907 // Don't allow module("*kernel*") type expressions to match the
1908 // elfutils module "kernel", which we refer to in the probe
1909 // point syntax exclusively as "kernel.*".
1910 if (q
->dw
.module_name
== TOK_KERNEL
&& ! q
->has_kernel
)
1911 return pending_interrupts
? DWARF_CB_ABORT
: DWARF_CB_OK
;
1914 validate_module_elf(mod
, name
, q
);
1916 assert(q
->has_kernel
); // and no vmlinux to examine
1918 if (q
->sess
.verbose
>2)
1919 cerr
<< "focused on module '" << q
->dw
.module_name
<< "'\n";
1922 // Collect a few kernel addresses. XXX: these belong better
1923 // to the sess.module_info["kernel"] struct.
1924 if (q
->dw
.module_name
== TOK_KERNEL
)
1926 if (! q
->sess
.sym_kprobes_text_start
)
1927 q
->sess
.sym_kprobes_text_start
= lookup_symbol_address (mod
, "__kprobes_text_start");
1928 if (! q
->sess
.sym_kprobes_text_end
)
1929 q
->sess
.sym_kprobes_text_end
= lookup_symbol_address (mod
, "__kprobes_text_end");
1930 if (! q
->sess
.sym_stext
)
1931 q
->sess
.sym_stext
= lookup_symbol_address (mod
, "_stext");
1934 // Finally, search the module for matches of the probe point.
1935 q
->handle_query_module();
1938 // If we know that there will be no more matches, abort early.
1939 if (q
->dw
.module_name_final_match(q
->module_val
) || pending_interrupts
)
1940 return DWARF_CB_ABORT
;
1944 catch (const semantic_error
& e
)
1946 q
->sess
.print_error (e
);
1947 return DWARF_CB_ABORT
;
1952 // This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
1953 // but the needed declaration for module_cache is not available there.
1954 // Nor for that matter in session.cxx. Only in this CU is that field ever
1955 // set (in query_module() above), so we clean it up here too.
1957 delete_session_module_cache (systemtap_session
& s
)
1959 if (s
.module_cache
) {
1961 clog
<< "deleting module_cache" << endl
;
1962 delete s
.module_cache
;
1968 struct dwarf_var_expanding_visitor
: public var_expanding_visitor
1971 Dwarf_Die
*scope_die
;
1974 block
*add_call_probe
; // synthesized from .return probes with saved $vars
1975 bool add_block_tid
, add_call_probe_tid
;
1976 unsigned saved_longs
, saved_strings
; // data saved within kretprobes
1977 map
<std::string
, expression
*> return_ts_map
;
1978 vector
<Dwarf_Die
> scopes
;
1981 dwarf_var_expanding_visitor(dwarf_query
& q
, Dwarf_Die
*sd
, Dwarf_Addr a
):
1982 q(q
), scope_die(sd
), addr(a
), add_block(NULL
), add_call_probe(NULL
),
1983 add_block_tid(false), add_call_probe_tid(false),
1984 saved_longs(0), saved_strings(0), visited(false) {}
1985 expression
* gen_mapped_saved_return(expression
* e
, const string
& name
);
1986 expression
* gen_kretprobe_saved_return(expression
* e
);
1987 void visit_target_symbol_saved_return (target_symbol
* e
);
1988 void visit_target_symbol_context (target_symbol
* e
);
1989 void visit_target_symbol (target_symbol
* e
);
1990 void visit_cast_op (cast_op
* e
);
1991 void visit_entry_op (entry_op
* e
);
1993 vector
<Dwarf_Die
>& getscopes(target_symbol
*e
);
1997 unsigned var_expanding_visitor::tick
= 0;
2000 var_expanding_visitor::var_expanding_visitor ()
2002 // FIXME: for the time being, by default we only support plain '$foo
2003 // = bar', not '+=' or any other op= variant. This is fixable, but a
2006 // If derived classes desire to add additional operator support, add
2007 // new operators to this list in the derived class constructor.
2008 valid_ops
.insert ("=");
2013 var_expanding_visitor::rewrite_lvalue(const token
* tok
, const std::string
& eop
,
2014 expression
*& lvalue
, expression
*& rvalue
)
2016 // Our job would normally be to require() the left and right sides
2017 // into a new assignment. What we're doing is slightly trickier:
2018 // we're pushing a functioncall** onto a stack, and if our left
2019 // child sets the functioncall* for that value, we're going to
2020 // assume our left child was a target symbol -- transformed into a
2021 // set_target_foo(value) call, and it wants to take our right child
2022 // as the argument "value".
2024 // This is why some people claim that languages with
2025 // constructor-decomposing case expressions have a leg up on
2028 functioncall
*fcall
= NULL
;
2030 // Let visit_target_symbol know what operator it should handle.
2031 const string
* old_op
= op
;
2034 target_symbol_setter_functioncalls
.push (&fcall
);
2036 target_symbol_setter_functioncalls
.pop ();
2043 // Our left child is informing us that it was a target variable
2044 // and it has been replaced with a set_target_foo() function
2045 // call; we are going to provide that function call -- with the
2046 // right child spliced in as sole argument -- in place of
2047 // ourselves, in the var expansion we're in the middle of making.
2049 if (valid_ops
.find (eop
) == valid_ops
.end ())
2051 // Build up a list of supported operators.
2053 std::set
<string
>::iterator i
;
2054 for (i
= valid_ops
.begin(); i
!= valid_ops
.end(); i
++)
2055 ops
+= " " + *i
+ ",";
2056 ops
.resize(ops
.size() - 1); // chop off the last ','
2059 throw semantic_error ("Only the following assign operators are"
2060 " implemented on target variables:" + ops
,
2064 assert (lvalue
== fcall
);
2066 fcall
->args
.push_back (rvalue
);
2076 var_expanding_visitor::visit_assignment (assignment
* e
)
2078 if (!rewrite_lvalue (e
->tok
, e
->op
, e
->left
, e
->right
))
2084 var_expanding_visitor::visit_pre_crement (pre_crement
* e
)
2086 expression
*dummy
= NULL
;
2087 if (!rewrite_lvalue (e
->tok
, e
->op
, e
->operand
, dummy
))
2093 var_expanding_visitor::visit_post_crement (post_crement
* e
)
2095 expression
*dummy
= NULL
;
2096 if (!rewrite_lvalue (e
->tok
, e
->op
, e
->operand
, dummy
))
2102 var_expanding_visitor::visit_delete_statement (delete_statement
* s
)
2104 string fakeop
= "delete";
2105 expression
*dummy
= NULL
;
2106 if (!rewrite_lvalue (s
->tok
, fakeop
, s
->value
, dummy
))
2112 var_expanding_visitor::visit_defined_op (defined_op
* e
)
2114 bool resolved
= true;
2116 defined_ops
.push (e
);
2118 // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is
2119 // defined with a target_symbol* operand, a subsidiary call may attempt to
2120 // rewrite it to a general expression* instead, and require<> happily
2121 // casts to/from void*, causing possible memory corruption. We use
2122 // expression* here, being the general case of rewritten $variable.
2123 expression
*foo1
= e
->operand
;
2124 foo1
= require (foo1
);
2126 // NB: Formerly, we had some curious cases to consider here, depending on what
2127 // various visit_target_symbol() implementations do for successful or
2128 // erroneous resolutions. Some would signal a visit_target_symbol failure
2129 // with an exception, with a set flag within the target_symbol, or nothing
2132 // Now, failures always have to be signalled with a
2133 // saved_conversion_error being chained to the target_symbol.
2134 // Successes have to result in an attempted rewrite of the
2135 // target_symbol (via provide()).
2137 // Edna Mode: "no capes". fche: "no exceptions".
2139 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2141 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2143 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2144 // dwarf probe to take care of it.
2145 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2146 // to pass through @defined() to the synthetic dwarf probe.
2148 // utrace: success: rewrites to function; failure: semantic_error
2150 // procfs: success: rewrites to function; failure: semantic_error
2152 target_symbol
* foo2
= dynamic_cast<target_symbol
*> (foo1
);
2153 if (foo2
&& foo2
->saved_conversion_error
) // failing
2155 else if (foo2
) // unresolved but not marked failing
2157 // There are some visitors that won't touch certain target_symbols,
2158 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2159 // leave it for now so some other visitor can have a chance.
2164 else // resolved, rewritten to some other expression type
2166 } catch (const semantic_error
& e
) {
2167 assert (0); // should not happen
2171 literal_number
* ln
= new literal_number (resolved
? 1 : 0);
2177 struct dwarf_pretty_print
2179 dwarf_pretty_print (dwflpp
& dw
, vector
<Dwarf_Die
>& scopes
, Dwarf_Addr pc
,
2180 const string
& local
, bool userspace_p
,
2181 const target_symbol
& e
):
2182 dw(dw
), local(local
), scopes(scopes
), pc(pc
), pointer(NULL
),
2183 userspace_p(userspace_p
), deref_p(true)
2186 dw
.type_die_for_local (scopes
, pc
, local
, ts
, &base_type
);
2189 dwarf_pretty_print (dwflpp
& dw
, Dwarf_Die
*scope_die
, Dwarf_Addr pc
,
2190 bool userspace_p
, const target_symbol
& e
):
2191 dw(dw
), scopes(1, *scope_die
), pc(pc
), pointer(NULL
),
2192 userspace_p(userspace_p
), deref_p(true)
2195 dw
.type_die_for_return (&scopes
[0], pc
, ts
, &base_type
);
2198 dwarf_pretty_print (dwflpp
& dw
, Dwarf_Die
*type_die
, expression
* pointer
,
2199 bool deref_p
, bool userspace_p
, const target_symbol
& e
):
2200 dw(dw
), pc(0), pointer(pointer
), pointer_type(*type_die
),
2201 userspace_p(userspace_p
), deref_p(deref_p
)
2204 dw
.type_die_for_pointer (type_die
, ts
, &base_type
);
2207 functioncall
* expand ();
2213 Dwarf_Die base_type
;
2216 vector
<Dwarf_Die
> scopes
;
2219 expression
* pointer
;
2220 Dwarf_Die pointer_type
;
2222 const bool userspace_p
, deref_p
;
2224 void recurse (Dwarf_Die
* type
, target_symbol
* e
,
2225 print_format
* pf
, bool top
=false);
2226 void recurse_base (Dwarf_Die
* type
, target_symbol
* e
,
2228 void recurse_array (Dwarf_Die
* type
, target_symbol
* e
,
2229 print_format
* pf
, bool top
);
2230 void recurse_pointer (Dwarf_Die
* type
, target_symbol
* e
,
2231 print_format
* pf
, bool top
);
2232 void recurse_struct (Dwarf_Die
* type
, target_symbol
* e
,
2233 print_format
* pf
, bool top
);
2234 void recurse_struct_members (Dwarf_Die
* type
, target_symbol
* e
,
2235 print_format
* pf
, int& count
);
2236 bool print_chars (Dwarf_Die
* type
, target_symbol
* e
, print_format
* pf
);
2238 void init_ts (const target_symbol
& e
);
2239 expression
* deref (target_symbol
* e
);
2240 bool push_deref (print_format
* pf
, const string
& fmt
, target_symbol
* e
);
2245 dwarf_pretty_print::init_ts (const target_symbol
& e
)
2247 // Work with a new target_symbol so we can modify arguments
2248 ts
= new target_symbol (e
);
2251 throw semantic_error("cannot take address of pretty-printed variable", ts
->tok
);
2253 if (ts
->components
.empty() ||
2254 ts
->components
.back().type
!= target_symbol::comp_pretty_print
)
2255 throw semantic_error("invalid target_symbol for pretty-print", ts
->tok
);
2256 print_full
= ts
->components
.back().member
.length() > 1;
2257 ts
->components
.pop_back();
2262 dwarf_pretty_print::expand ()
2264 static unsigned tick
= 0;
2266 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
2268 // return sprintf("{.foo=...}", (ts)->foo, ...)
2274 // Create the function decl and call.
2276 functiondecl
*fdecl
= new functiondecl
;
2277 fdecl
->tok
= ts
->tok
;
2278 fdecl
->synthetic
= true;
2279 fdecl
->name
= "_dwarf_pretty_print_" + lex_cast(tick
++);
2280 fdecl
->type
= pe_string
;
2282 functioncall
* fcall
= new functioncall
;
2283 fcall
->tok
= ts
->tok
;
2284 fcall
->function
= fdecl
->name
;
2285 fcall
->type
= pe_string
;
2287 // If there's a <pointer>, replace it with a new var and make that
2288 // the first function argument.
2291 vardecl
*v
= new vardecl
;
2293 v
->name
= "pointer";
2295 fdecl
->formal_args
.push_back (v
);
2296 fcall
->args
.push_back (pointer
);
2298 symbol
* sym
= new symbol
;
2300 sym
->name
= v
->name
;
2304 // For each expression argument, replace it with a function argument.
2305 for (unsigned i
= 0; i
< ts
->components
.size(); ++i
)
2306 if (ts
->components
[i
].type
== target_symbol::comp_expression_array_index
)
2308 vardecl
*v
= new vardecl
;
2310 v
->name
= "index" + lex_cast(i
);
2312 fdecl
->formal_args
.push_back (v
);
2313 fcall
->args
.push_back (ts
->components
[i
].expr_index
);
2315 symbol
* sym
= new symbol
;
2317 sym
->name
= v
->name
;
2318 ts
->components
[i
].expr_index
= sym
;
2321 // Create the return sprintf.
2322 token
* pf_tok
= new token(*ts
->tok
);
2323 pf_tok
->content
= "sprintf";
2324 print_format
* pf
= print_format::create(pf_tok
);
2325 return_statement
* rs
= new return_statement
;
2329 // Recurse into the actual values.
2330 recurse (&base_type
, ts
, pf
, true);
2331 pf
->components
= print_format::string_to_components(pf
->raw_components
);
2333 // Create the try-catch net
2334 try_block
* tb
= new try_block
;
2337 tb
->catch_error_var
= 0;
2338 return_statement
* rs2
= new return_statement
;
2340 rs2
->value
= new literal_string ("ERROR");
2341 rs2
->value
->tok
= ts
->tok
;
2342 tb
->catch_block
= rs2
;
2345 fdecl
->join (dw
.sess
);
2351 dwarf_pretty_print::recurse (Dwarf_Die
* start_type
, target_symbol
* e
,
2352 print_format
* pf
, bool top
)
2355 dw
.resolve_unqualified_inner_typedie (start_type
, &type
, e
);
2357 switch (dwarf_tag(&type
))
2360 // XXX need a warning?
2361 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
2362 // + ") for " + dwarf_type_name(&type), e->tok);
2363 pf
->raw_components
.append("?");
2366 case DW_TAG_enumeration_type
:
2367 case DW_TAG_base_type
:
2368 recurse_base (&type
, e
, pf
);
2371 case DW_TAG_array_type
:
2372 recurse_array (&type
, e
, pf
, top
);
2375 case DW_TAG_pointer_type
:
2376 case DW_TAG_reference_type
:
2377 case DW_TAG_rvalue_reference_type
:
2378 recurse_pointer (&type
, e
, pf
, top
);
2381 case DW_TAG_subroutine_type
:
2382 push_deref (pf
, "<function>:%p", e
);
2385 case DW_TAG_union_type
:
2386 case DW_TAG_structure_type
:
2387 case DW_TAG_class_type
:
2388 recurse_struct (&type
, e
, pf
, top
);
2395 dwarf_pretty_print::recurse_base (Dwarf_Die
* type
, target_symbol
* e
,
2398 Dwarf_Attribute attr
;
2399 Dwarf_Word encoding
= (Dwarf_Word
) -1;
2400 dwarf_formudata (dwarf_attr_integrate (type
, DW_AT_encoding
, &attr
),
2405 case DW_ATE_complex_float
:
2406 // XXX need a warning?
2407 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
2408 // + ") for " + dwarf_type_name(type), e->tok);
2409 pf
->raw_components
.append("?");
2412 case DW_ATE_signed_char
:
2413 case DW_ATE_unsigned_char
:
2414 push_deref (pf
, "'%c'", e
);
2417 case DW_ATE_unsigned
:
2418 push_deref (pf
, "%u", e
);
2422 push_deref (pf
, "%i", e
);
2429 dwarf_pretty_print::recurse_array (Dwarf_Die
* type
, target_symbol
* e
,
2430 print_format
* pf
, bool top
)
2432 if (!top
&& !print_full
)
2434 pf
->raw_components
.append("[...]");
2438 Dwarf_Die childtype
;
2439 dwarf_attr_die (type
, DW_AT_type
, &childtype
);
2441 if (print_chars (&childtype
, e
, pf
))
2444 pf
->raw_components
.append("[");
2446 // We print the array up to the first 5 elements.
2447 // XXX how can we determine the array size?
2448 // ... for now, just print the first element
2449 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
2450 unsigned i
, size
= 1;
2451 for (i
=0; i
< size
&& i
< 5 && pf
->args
.size() < 32; ++i
)
2454 pf
->raw_components
.append(", ");
2455 target_symbol
* e2
= new target_symbol(*e
);
2456 e2
->components
.push_back (target_symbol::component(e
->tok
, i
));
2457 recurse (&childtype
, e2
, pf
);
2459 if (i
< size
|| 1/*XXX until real size is known */)
2460 pf
->raw_components
.append(", ...");
2461 pf
->raw_components
.append("]");
2466 dwarf_pretty_print::recurse_pointer (Dwarf_Die
* type
, target_symbol
* e
,
2467 print_format
* pf
, bool top
)
2469 // We chase to top-level pointers, but leave the rest alone
2472 if (dwarf_attr_die (type
, DW_AT_type
, &pointee
))
2476 dw
.resolve_unqualified_inner_typedie (&pointee
, &pointee
, e
);
2479 catch (const semantic_error
&) {}
2484 if (print_chars (&pointee
, e
, pf
))
2489 recurse (&pointee
, e
, pf
, top
);
2494 push_deref (pf
, "%p", e
);
2499 dwarf_pretty_print::recurse_struct (Dwarf_Die
* type
, target_symbol
* e
,
2500 print_format
* pf
, bool top
)
2502 if (dwarf_hasattr(type
, DW_AT_declaration
))
2504 Dwarf_Die
*resolved
= dw
.declaration_resolve(type
);
2507 // could be an error, but for now just stub it
2508 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
2509 pf
->raw_components
.append("{...}");
2516 pf
->raw_components
.append("{");
2517 if (top
|| print_full
)
2518 recurse_struct_members (type
, e
, pf
, count
);
2520 pf
->raw_components
.append("...");
2521 pf
->raw_components
.append("}");
2526 dwarf_pretty_print::recurse_struct_members (Dwarf_Die
* type
, target_symbol
* e
,
2527 print_format
* pf
, int& count
)
2529 Dwarf_Die child
, childtype
;
2530 if (dwarf_child (type
, &child
) == 0)
2533 target_symbol
* e2
= e
;
2535 // skip static members
2536 if (dwarf_hasattr(&child
, DW_AT_declaration
))
2539 int tag
= dwarf_tag (&child
);
2541 if (tag
!= DW_TAG_member
&& tag
!= DW_TAG_inheritance
)
2544 dwarf_attr_die (&child
, DW_AT_type
, &childtype
);
2546 if (tag
== DW_TAG_inheritance
)
2548 recurse_struct_members (&childtype
, e
, pf
, count
);
2552 int childtag
= dwarf_tag (&childtype
);
2553 const char *member
= dwarf_diename (&child
);
2555 // "_vptr.foo" members are C++ virtual function tables,
2556 // which (generally?) aren't interesting for users.
2557 if (member
&& startswith(member
, "_vptr."))
2561 pf
->raw_components
.append(", ");
2563 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
2564 if (pf
->args
.size() >= 32)
2566 pf
->raw_components
.append("...");
2572 pf
->raw_components
.append(".");
2573 pf
->raw_components
.append(member
);
2575 e2
= new target_symbol(*e
);
2576 e2
->components
.push_back (target_symbol::component(e
->tok
, member
));
2578 else if (childtag
== DW_TAG_union_type
)
2579 pf
->raw_components
.append("<union>");
2580 else if (childtag
== DW_TAG_structure_type
)
2581 pf
->raw_components
.append("<class>");
2582 else if (childtag
== DW_TAG_class_type
)
2583 pf
->raw_components
.append("<struct>");
2584 pf
->raw_components
.append("=");
2585 recurse (&childtype
, e2
, pf
);
2587 while (dwarf_siblingof (&child
, &child
) == 0);
2592 dwarf_pretty_print::print_chars (Dwarf_Die
* start_type
, target_symbol
* e
,
2596 dw
.resolve_unqualified_inner_typedie (start_type
, &type
, e
);
2597 const char *name
= dwarf_diename (&type
);
2598 if (name
&& (name
== string("char") || name
== string("unsigned char")))
2600 if (push_deref (pf
, "\"%s\"", e
))
2602 // steal the last arg for a string access
2603 assert (!pf
->args
.empty());
2604 functioncall
* fcall
= new functioncall
;
2605 fcall
->tok
= e
->tok
;
2606 fcall
->function
= userspace_p
? "user_string2" : "kernel_string2";
2607 fcall
->args
.push_back (pf
->args
.back());
2608 expression
*err_msg
= new literal_string ("<unknown>");
2609 err_msg
->tok
= e
->tok
;
2610 fcall
->args
.push_back (err_msg
);
2611 pf
->args
.back() = fcall
;
2620 dwarf_pretty_print::deref (target_symbol
* e
)
2622 static unsigned tick
= 0;
2626 assert (pointer
&& e
->components
.empty());
2630 // Synthesize a function to dereference the dwarf fields,
2631 // with a pointer parameter that is the base tracepoint variable
2632 functiondecl
*fdecl
= new functiondecl
;
2633 fdecl
->synthetic
= true;
2634 fdecl
->tok
= e
->tok
;
2635 embeddedcode
*ec
= new embeddedcode
;
2638 fdecl
->name
= "_dwarf_pretty_print_deref_" + lex_cast(tick
++);
2641 // Synthesize a functioncall.
2642 functioncall
* fcall
= new functioncall
;
2643 fcall
->tok
= e
->tok
;
2644 fcall
->function
= fdecl
->name
;
2646 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
2647 ec
->code
+= "\n#define fetch_register " + string(userspace_p
?"u":"k") + "_fetch_register\n";
2648 ec
->code
+= "#define store_register " + string(userspace_p
?"u":"k") + "_store_register\n";
2652 ec
->code
+= dw
.literal_stmt_for_pointer (&pointer_type
, e
,
2653 false, fdecl
->type
);
2655 vardecl
*v
= new vardecl
;
2657 v
->name
= "pointer";
2659 fdecl
->formal_args
.push_back(v
);
2660 fcall
->args
.push_back(pointer
);
2662 else if (!local
.empty())
2663 ec
->code
+= dw
.literal_stmt_for_local (scopes
, pc
, local
, e
,
2664 false, fdecl
->type
);
2666 ec
->code
+= dw
.literal_stmt_for_return (&scopes
[0], pc
, e
,
2667 false, fdecl
->type
);
2669 // Any non-literal indexes need to be passed in too.
2670 for (unsigned i
= 0; i
< e
->components
.size(); ++i
)
2671 if (e
->components
[i
].type
== target_symbol::comp_expression_array_index
)
2673 vardecl
*v
= new vardecl
;
2675 v
->name
= "index" + lex_cast(i
);
2677 fdecl
->formal_args
.push_back(v
);
2678 fcall
->args
.push_back(e
->components
[i
].expr_index
);
2681 ec
->code
+= "/* pure */";
2682 ec
->code
+= "/* unprivileged */";
2685 ec
->code
+= "\n#undef fetch_register\n";
2686 ec
->code
+= "\n#undef store_register\n";
2688 fdecl
->join (dw
.sess
);
2694 dwarf_pretty_print::push_deref (print_format
* pf
, const string
& fmt
,
2697 expression
* e2
= NULL
;
2702 catch (const semantic_error
&)
2704 pf
->raw_components
.append ("?");
2707 pf
->raw_components
.append (fmt
);
2708 pf
->args
.push_back (e2
);
2714 dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol
* e
)
2716 // Get the full name of the target symbol.
2717 stringstream ts_name_stream
;
2718 e
->print(ts_name_stream
);
2719 string ts_name
= ts_name_stream
.str();
2721 // Check and make sure we haven't already seen this target
2722 // variable in this return probe. If we have, just return our
2723 // last replacement.
2724 map
<string
, expression
*>::iterator i
= return_ts_map
.find(ts_name
);
2725 if (i
!= return_ts_map
.end())
2727 provide (i
->second
);
2731 // Attempt the expansion directly first, so if there's a problem with the
2732 // variable we won't have a bogus entry probe lying around. Like in
2733 // saveargs(), we pretend for a moment that we're not in a .return.
2734 bool saved_has_return
= q
.has_return
;
2735 q
.has_return
= false;
2736 expression
*repl
= e
;
2738 q
.has_return
= saved_has_return
;
2739 target_symbol
* n
= dynamic_cast<target_symbol
*>(repl
);
2740 if (n
&& n
->saved_conversion_error
)
2747 if (!q
.has_process
&&
2748 strverscmp(q
.sess
.kernel_base_release
.c_str(), "2.6.25") >= 0)
2749 exp
= gen_kretprobe_saved_return(repl
);
2751 exp
= gen_mapped_saved_return(repl
, e
->name
);
2753 // Provide the variable to our parent so it can be used as a
2754 // substitute for the target symbol.
2757 // Remember this replacement since we might be able to reuse
2758 // it later if the same return probe references this target
2760 return_ts_map
[ts_name
] = exp
;
2764 dwarf_var_expanding_visitor::gen_mapped_saved_return(expression
* e
,
2767 // We've got to do several things here to handle target
2768 // variables in return probes.
2770 // (1) Synthesize two global arrays. One is the cache of the
2771 // target variable and the other contains a thread specific
2772 // nesting level counter. The arrays will look like
2775 // _dwarf_tvar_{name}_{num}
2776 // _dwarf_tvar_{name}_{num}_ctr
2778 string aname
= (string("_dwarf_tvar_")
2780 + "_" + lex_cast(tick
++));
2781 vardecl
* vd
= new vardecl
;
2784 q
.sess
.globals
.push_back (vd
);
2786 string ctrname
= aname
+ "_ctr";
2790 q
.sess
.globals
.push_back (vd
);
2792 // (2) Create a new code block we're going to insert at the
2793 // beginning of this probe to get the cached value into a
2794 // temporary variable. We'll replace the target variable
2795 // reference with the temporary variable reference. The code
2796 // will look like this:
2798 // _dwarf_tvar_tid = tid()
2799 // _dwarf_tvar_{name}_{num}_tmp
2800 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2801 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2802 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2803 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
2804 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
2805 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
2807 // (2a) Synthesize the tid temporary expression, which will look
2810 // _dwarf_tvar_tid = tid()
2811 symbol
* tidsym
= new symbol
;
2812 tidsym
->name
= string("_dwarf_tvar_tid");
2813 tidsym
->tok
= e
->tok
;
2815 if (add_block
== NULL
)
2817 add_block
= new block
;
2818 add_block
->tok
= e
->tok
;
2823 // Synthesize a functioncall to grab the thread id.
2824 functioncall
* fc
= new functioncall
;
2826 fc
->function
= string("tid");
2828 // Assign the tid to '_dwarf_tvar_tid'.
2829 assignment
* a
= new assignment
;
2835 expr_statement
* es
= new expr_statement
;
2838 add_block
->statements
.push_back (es
);
2839 add_block_tid
= true;
2842 // (2b) Synthesize an array reference and assign it to a
2843 // temporary variable (that we'll use as replacement for the
2844 // target variable reference). It will look like this:
2846 // _dwarf_tvar_{name}_{num}_tmp
2847 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2848 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2850 arrayindex
* ai_tvar_base
= new arrayindex
;
2851 ai_tvar_base
->tok
= e
->tok
;
2853 symbol
* sym
= new symbol
;
2856 ai_tvar_base
->base
= sym
;
2858 ai_tvar_base
->indexes
.push_back(tidsym
);
2860 // We need to create a copy of the array index in its current
2861 // state so we can have 2 variants of it (the original and one
2862 // that post-decrements the second index).
2863 arrayindex
* ai_tvar
= new arrayindex
;
2864 arrayindex
* ai_tvar_postdec
= new arrayindex
;
2865 *ai_tvar
= *ai_tvar_base
;
2866 *ai_tvar_postdec
= *ai_tvar_base
;
2869 // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the
2870 // second index into the array.
2871 arrayindex
* ai_ctr
= new arrayindex
;
2872 ai_ctr
->tok
= e
->tok
;
2875 sym
->name
= ctrname
;
2878 ai_ctr
->indexes
.push_back(tidsym
);
2879 ai_tvar
->indexes
.push_back(ai_ctr
);
2881 symbol
* tmpsym
= new symbol
;
2882 tmpsym
->name
= aname
+ "_tmp";
2883 tmpsym
->tok
= e
->tok
;
2885 assignment
* a
= new assignment
;
2891 expr_statement
* es
= new expr_statement
;
2895 add_block
->statements
.push_back (es
);
2897 // (2c) Add a post-decrement to the second array index and
2898 // delete the array value. It will look like this:
2900 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2901 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
2903 post_crement
* pc
= new post_crement
;
2906 pc
->operand
= ai_ctr
;
2907 ai_tvar_postdec
->indexes
.push_back(pc
);
2909 delete_statement
* ds
= new delete_statement
;
2911 ds
->value
= ai_tvar_postdec
;
2913 add_block
->statements
.push_back (ds
);
2915 // (2d) Delete the counter value if it is 0. It will look like
2917 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
2918 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
2920 ds
= new delete_statement
;
2924 unary_expression
*ue
= new unary_expression
;
2927 ue
->operand
= ai_ctr
;
2929 if_statement
*ifs
= new if_statement
;
2931 ifs
->condition
= ue
;
2932 ifs
->thenblock
= ds
;
2933 ifs
->elseblock
= NULL
;
2935 add_block
->statements
.push_back (ifs
);
2937 // (3) We need an entry probe that saves the value for us in the
2938 // global array we created. Create the entry probe, which will
2941 // probe kernel.function("{function}").call {
2942 // _dwarf_tvar_tid = tid()
2943 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2944 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2948 if (add_call_probe
== NULL
)
2950 add_call_probe
= new block
;
2951 add_call_probe
->tok
= e
->tok
;
2954 if (!add_call_probe_tid
)
2956 // Synthesize a functioncall to grab the thread id.
2957 functioncall
* fc
= new functioncall
;
2959 fc
->function
= string("tid");
2961 // Assign the tid to '_dwarf_tvar_tid'.
2962 assignment
* a
= new assignment
;
2968 expr_statement
* es
= new expr_statement
;
2971 add_call_probe
= new block(add_call_probe
, es
);
2972 add_call_probe_tid
= true;
2975 // Save the value, like this:
2976 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2977 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2979 arrayindex
* ai_tvar_preinc
= new arrayindex
;
2980 *ai_tvar_preinc
= *ai_tvar_base
;
2982 pre_crement
* preinc
= new pre_crement
;
2983 preinc
->tok
= e
->tok
;
2985 preinc
->operand
= ai_ctr
;
2986 ai_tvar_preinc
->indexes
.push_back(preinc
);
2991 a
->left
= ai_tvar_preinc
;
2994 es
= new expr_statement
;
2998 add_call_probe
= new block(add_call_probe
, es
);
3000 // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to
3001 // our parent so it can be used as a substitute for the target
3008 dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression
* e
)
3010 // The code for this is simple.
3013 // _set_kretprobe_long(index, $value)
3016 // _get_kretprobe_long(index)
3018 // (or s/long/string/ for things like $$parms)
3021 string setfn
, getfn
;
3023 // We need the caller to predetermine the type of the expression!
3027 index
= saved_strings
++;
3028 setfn
= "_set_kretprobe_string";
3029 getfn
= "_get_kretprobe_string";
3032 index
= saved_longs
++;
3033 setfn
= "_set_kretprobe_long";
3034 getfn
= "_get_kretprobe_long";
3037 throw semantic_error("unknown type to save in kretprobe", e
->tok
);
3040 // Create the entry code
3041 // _set_kretprobe_{long|string}(index, $value)
3043 if (add_call_probe
== NULL
)
3045 add_call_probe
= new block
;
3046 add_call_probe
->tok
= e
->tok
;
3049 functioncall
* set_fc
= new functioncall
;
3050 set_fc
->tok
= e
->tok
;
3051 set_fc
->function
= setfn
;
3052 set_fc
->args
.push_back(new literal_number(index
));
3053 set_fc
->args
.back()->tok
= e
->tok
;
3054 set_fc
->args
.push_back(e
);
3056 expr_statement
* set_es
= new expr_statement
;
3057 set_es
->tok
= e
->tok
;
3058 set_es
->value
= set_fc
;
3060 add_call_probe
->statements
.push_back(set_es
);
3062 // Create the return code
3063 // _get_kretprobe_{long|string}(index)
3065 functioncall
* get_fc
= new functioncall
;
3066 get_fc
->tok
= e
->tok
;
3067 get_fc
->function
= getfn
;
3068 get_fc
->args
.push_back(new literal_number(index
));
3069 get_fc
->args
.back()->tok
= e
->tok
;
3076 dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol
* e
)
3078 if (null_die(scope_die
))
3081 target_symbol
*tsym
= new target_symbol(*e
);
3083 bool pretty
= (!e
->components
.empty() &&
3084 e
->components
[0].type
== target_symbol::comp_pretty_print
);
3085 string format
= pretty
? "=%s" : "=%#x";
3087 // Convert $$parms to sprintf of a list of parms and active local vars
3088 // which we recursively evaluate
3090 // NB: we synthesize a new token here rather than reusing
3091 // e->tok, because print_format::print likes to use
3092 // its tok->content.
3093 token
* pf_tok
= new token(*e
->tok
);
3094 pf_tok
->type
= tok_identifier
;
3095 pf_tok
->content
= "sprintf";
3097 print_format
* pf
= print_format::create(pf_tok
);
3099 if (q
.has_return
&& (e
->name
== "$$return"))
3101 tsym
->name
= "$return";
3103 // Ignore any variable that isn't accessible.
3104 tsym
->saved_conversion_error
= 0;
3105 expression
*texp
= tsym
;
3106 replace (texp
); // NB: throws nothing ...
3107 if (tsym
->saved_conversion_error
) // ... but this is how we know it happened.
3113 pf
->raw_components
+= "return";
3114 pf
->raw_components
+= format
;
3115 pf
->args
.push_back(texp
);
3120 // non-.return probe: support $$parms, $$vars, $$locals
3123 vector
<Dwarf_Die
> scopes
= q
.dw
.getscopes(scope_die
);
3124 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
3126 if (dwarf_tag(&scopes
[i
]) == DW_TAG_compile_unit
)
3127 break; // we don't want file-level variables
3128 if (dwarf_child (&scopes
[i
], &result
) == 0)
3131 switch (dwarf_tag (&result
))
3133 case DW_TAG_variable
:
3134 if (e
->name
== "$$parms")
3137 case DW_TAG_formal_parameter
:
3138 if (e
->name
== "$$locals")
3146 const char *diename
= dwarf_diename (&result
);
3147 if (! diename
) continue;
3150 pf
->raw_components
+= " ";
3151 pf
->raw_components
+= diename
;
3154 // Write a placeholder for ugly aggregates
3156 if (!pretty
&& dwarf_attr_die(&result
, DW_AT_type
, &type
))
3158 q
.dw
.resolve_unqualified_inner_typedie(&type
, &type
, e
);
3159 switch (dwarf_tag(&type
))
3161 case DW_TAG_union_type
:
3162 case DW_TAG_structure_type
:
3163 case DW_TAG_class_type
:
3164 pf
->raw_components
+= "={...}";
3167 case DW_TAG_array_type
:
3168 pf
->raw_components
+= "=[...]";
3174 tsym
->name
+= diename
;
3176 // Ignore any variable that isn't accessible.
3177 tsym
->saved_conversion_error
= 0;
3178 expression
*texp
= tsym
;
3179 replace (texp
); // NB: throws nothing ...
3180 if (tsym
->saved_conversion_error
) // ... but this is how we know it happened.
3182 if (q
.sess
.verbose
>2)
3184 for (semantic_error
*c
= tsym
->saved_conversion_error
;
3187 clog
<< "variable location problem: " << c
->what() << endl
;
3191 pf
->raw_components
+= "=?";
3195 pf
->raw_components
+= format
;
3196 pf
->args
.push_back(texp
);
3199 while (dwarf_siblingof (&result
, &result
) == 0);
3203 pf
->components
= print_format::string_to_components(pf
->raw_components
);
3204 pf
->type
= pe_string
;
3210 dwarf_var_expanding_visitor::visit_target_symbol (target_symbol
*e
)
3212 assert(e
->name
.size() > 0 && e
->name
[0] == '$');
3214 bool defined_being_checked
= (defined_ops
.size() > 0 && (defined_ops
.top()->operand
== e
));
3215 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
3219 bool lvalue
= is_active_lvalue(e
);
3220 if (lvalue
&& !q
.sess
.guru_mode
)
3221 throw semantic_error("write to target variable not permitted", e
->tok
);
3223 // XXX: process $context vars should be writeable
3225 // See if we need to generate a new probe to save/access function
3226 // parameters from a return probe. PR 1382.
3228 && !defined_being_checked
3229 && e
->name
!= "$return" // not the special return-value variable handled below
3230 && e
->name
!= "$$return") // nor the other special variable handled below
3233 throw semantic_error("write to target variable not permitted in .return probes", e
->tok
);
3234 visit_target_symbol_saved_return(e
);
3238 if (e
->name
== "$$vars" || e
->name
== "$$parms" || e
->name
== "$$locals"
3239 || (q
.has_return
&& (e
->name
== "$$return")))
3242 throw semantic_error("cannot write to context variable", e
->tok
);
3245 throw semantic_error("cannot take address of context variable", e
->tok
);
3247 e
->assert_no_components("dwarf", true);
3249 visit_target_symbol_context(e
);
3253 if (!e
->components
.empty() &&
3254 e
->components
.back().type
== target_symbol::comp_pretty_print
)
3257 throw semantic_error("cannot write to pretty-printed variable", e
->tok
);
3259 if (q
.has_return
&& (e
->name
== "$return"))
3261 dwarf_pretty_print
dpp (q
.dw
, scope_die
, addr
,
3263 dpp
.expand()->visit(this);
3267 dwarf_pretty_print
dpp (q
.dw
, getscopes(e
), addr
,
3270 dpp
.expand()->visit(this);
3275 // Synthesize a function.
3276 functiondecl
*fdecl
= new functiondecl
;
3277 fdecl
->synthetic
= true;
3278 fdecl
->tok
= e
->tok
;
3279 embeddedcode
*ec
= new embeddedcode
;
3282 string fname
= (string(lvalue
? "_dwarf_tvar_set" : "_dwarf_tvar_get")
3283 + "_" + e
->name
.substr(1)
3284 + "_" + lex_cast(tick
++));
3286 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3287 ec
->code
+= "\n#define fetch_register " + string(q
.has_process
?"u":"k") + "_fetch_register\n";
3288 ec
->code
+= "#define store_register " + string(q
.has_process
?"u":"k") + "_store_register\n";
3290 if (q
.has_return
&& (e
->name
== "$return"))
3292 ec
->code
+= q
.dw
.literal_stmt_for_return (scope_die
,
3300 ec
->code
+= q
.dw
.literal_stmt_for_local (getscopes(e
),
3309 ec
->code
+= "/* pure */";
3311 ec
->code
+= "/* unprivileged */";
3314 ec
->code
+= "\n#undef fetch_register\n";
3315 ec
->code
+= "\n#undef store_register\n";
3317 fdecl
->name
= fname
;
3320 // Any non-literal indexes need to be passed in too.
3321 for (unsigned i
= 0; i
< e
->components
.size(); ++i
)
3322 if (e
->components
[i
].type
== target_symbol::comp_expression_array_index
)
3324 vardecl
*v
= new vardecl
;
3326 v
->name
= "index" + lex_cast(i
);
3328 fdecl
->formal_args
.push_back(v
);
3333 // Modify the fdecl so it carries a single pe_long formal
3334 // argument called "value".
3336 // FIXME: For the time being we only support setting target
3337 // variables which have base types; these are 'pe_long' in
3338 // stap's type vocabulary. Strings and pointers might be
3339 // reasonable, some day, but not today.
3341 vardecl
*v
= new vardecl
;
3345 fdecl
->formal_args
.push_back(v
);
3347 fdecl
->join (q
.sess
);
3349 // Synthesize a functioncall.
3350 functioncall
* n
= new functioncall
;
3352 n
->function
= fname
;
3353 n
->type
= fdecl
->type
;
3355 // Any non-literal indexes need to be passed in too.
3356 for (unsigned i
= 0; i
< e
->components
.size(); ++i
)
3357 if (e
->components
[i
].type
== target_symbol::comp_expression_array_index
)
3358 n
->args
.push_back(require(e
->components
[i
].expr_index
));
3362 // Provide the functioncall to our parent, so that it can be
3363 // used to substitute for the assignment node immediately above
3365 assert(!target_symbol_setter_functioncalls
.empty());
3366 *(target_symbol_setter_functioncalls
.top()) = n
;
3371 catch (const semantic_error
& er
)
3373 // We suppress this error message, and pass the unresolved
3374 // target_symbol to the next pass. We hope that this value ends
3375 // up not being referenced after all, so it can be optimized out
3384 dwarf_var_expanding_visitor::visit_cast_op (cast_op
*e
)
3386 // Fill in our current module context if needed
3387 if (e
->module
.empty())
3388 e
->module
= q
.dw
.module_name
;
3390 var_expanding_visitor::visit_cast_op(e
);
3395 dwarf_var_expanding_visitor::visit_entry_op (entry_op
*e
)
3397 expression
*repl
= e
;
3400 // expand the operand as if it weren't a return probe
3401 q
.has_return
= false;
3402 replace (e
->operand
);
3403 q
.has_return
= true;
3405 // XXX it would be nice to use gen_kretprobe_saved_return when available,
3406 // but it requires knowing the types already, which is problematic for
3407 // arbitrary expressons.
3408 repl
= gen_mapped_saved_return (e
->operand
, "@entry");
3415 dwarf_var_expanding_visitor::getscopes(target_symbol
*e
)
3419 scopes
= q
.dw
.getscopes(scope_die
);
3421 throw semantic_error ("unable to find any scopes containing "
3422 + lex_cast_hex(addr
)
3423 + ((scope_die
== NULL
) ? ""
3425 + (dwarf_diename(scope_die
) ?: "<unknown>")
3426 + "(" + (dwarf_diename(q
.dw
.cu
) ?: "<unknown>")
3428 + " while searching for local '"
3429 + e
->name
.substr(1) + "'",
3436 struct dwarf_cast_expanding_visitor
: public var_expanding_visitor
3438 systemtap_session
& s
;
3441 dwarf_cast_expanding_visitor(systemtap_session
& s
, dwarf_builder
& db
):
3443 void visit_cast_op (cast_op
* e
);
3444 void filter_special_modules(string
& module
);
3448 struct dwarf_cast_query
: public base_query
3452 const bool userspace_p
;
3453 functioncall
*& result
;
3455 dwarf_cast_query(dwflpp
& dw
, const string
& module
, cast_op
& e
, bool lvalue
,
3456 const bool userspace_p
, functioncall
*& result
):
3457 base_query(dw
, module
), e(e
), lvalue(lvalue
),
3458 userspace_p(userspace_p
), result(result
) {}
3460 void handle_query_module();
3465 dwarf_cast_query::handle_query_module()
3467 static unsigned tick
= 0;
3472 // look for the type in any CU
3473 Dwarf_Die
* type_die
= NULL
;
3474 if (startswith(e
.type_name
, "class "))
3476 // normalize to match dwflpp::global_alias_caching_callback
3477 string struct_name
= "struct " + e
.type_name
.substr(6);
3478 type_die
= dw
.declaration_resolve_other_cus(struct_name
);
3481 type_die
= dw
.declaration_resolve_other_cus(e
.type_name
);
3483 // NB: We now index the types as "struct name"/"union name"/etc. instead of
3484 // just "name". But since we didn't require users to be explicit before, and
3485 // actually sort of discouraged it, we must be flexible now. So if a lookup
3486 // fails with a bare name, try augmenting it.
3488 !startswith(e
.type_name
, "class ") &&
3489 !startswith(e
.type_name
, "struct ") &&
3490 !startswith(e
.type_name
, "union ") &&
3491 !startswith(e
.type_name
, "enum "))
3493 type_die
= dw
.declaration_resolve_other_cus("struct " + e
.type_name
);
3495 type_die
= dw
.declaration_resolve_other_cus("union " + e
.type_name
);
3497 type_die
= dw
.declaration_resolve_other_cus("enum " + e
.type_name
);
3504 exp_type type
= pe_long
;
3509 dw
.focus_on_cu(dwarf_diecu(type_die
, &cu_mem
, NULL
, NULL
));
3511 if (!e
.components
.empty() &&
3512 e
.components
.back().type
== target_symbol::comp_pretty_print
)
3515 throw semantic_error("cannot write to pretty-printed variable", e
.tok
);
3517 dwarf_pretty_print
dpp(dw
, type_die
, e
.operand
, true, userspace_p
, e
);
3518 result
= dpp
.expand();
3522 code
= dw
.literal_stmt_for_pointer (type_die
, &e
, lvalue
, type
);
3524 catch (const semantic_error
& er
)
3526 // NB: we can have multiple errors, since a @cast
3527 // may be attempted using several different modules:
3528 // @cast(ptr, "type", "module1:module2:...")
3535 string fname
= (string(lvalue
? "_dwarf_cast_set" : "_dwarf_cast_get")
3536 + "_" + e
.name
.substr(1)
3537 + "_" + lex_cast(tick
++));
3539 // Synthesize a function.
3540 functiondecl
*fdecl
= new functiondecl
;
3541 fdecl
->synthetic
= true;
3544 fdecl
->name
= fname
;
3546 embeddedcode
*ec
= new embeddedcode
;
3550 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3551 ec
->code
+= "\n#define fetch_register " + string(userspace_p
?"u":"k") + "_fetch_register\n";
3552 ec
->code
+= "#define store_register " + string(userspace_p
?"u":"k") + "_store_register\n";
3556 // Give the fdecl an argument for the pointer we're trying to cast
3557 vardecl
*v1
= new vardecl
;
3559 v1
->name
= "pointer";
3561 fdecl
->formal_args
.push_back(v1
);
3563 // Any non-literal indexes need to be passed in too.
3564 for (unsigned i
= 0; i
< e
.components
.size(); ++i
)
3565 if (e
.components
[i
].type
== target_symbol::comp_expression_array_index
)
3567 vardecl
*v
= new vardecl
;
3569 v
->name
= "index" + lex_cast(i
);
3571 fdecl
->formal_args
.push_back(v
);
3576 // Modify the fdecl so it carries a second pe_long formal
3577 // argument called "value".
3579 // FIXME: For the time being we only support setting target
3580 // variables which have base types; these are 'pe_long' in
3581 // stap's type vocabulary. Strings and pointers might be
3582 // reasonable, some day, but not today.
3584 vardecl
*v2
= new vardecl
;
3588 fdecl
->formal_args
.push_back(v2
);
3591 ec
->code
+= "/* pure */";
3593 ec
->code
+= "/* unprivileged */";
3596 ec
->code
+= "\n#undef fetch_register\n";
3597 ec
->code
+= "\n#undef store_register\n";
3599 fdecl
->join (dw
.sess
);
3601 // Synthesize a functioncall.
3602 functioncall
* n
= new functioncall
;
3604 n
->function
= fname
;
3605 n
->args
.push_back(e
.operand
);
3607 // Any non-literal indexes need to be passed in too.
3608 for (unsigned i
= 0; i
< e
.components
.size(); ++i
)
3609 if (e
.components
[i
].type
== target_symbol::comp_expression_array_index
)
3610 n
->args
.push_back(e
.components
[i
].expr_index
);
3616 void dwarf_cast_expanding_visitor::filter_special_modules(string
& module
)
3618 // look for "<path/to/header>" or "kernel<path/to/header>"
3619 // for those cases, build a module including that header
3620 if (module
[module
.size() - 1] == '>' &&
3621 (module
[0] == '<' || startswith(module
, "kernel<")))
3623 string cached_module
;
3626 // see if the cached module exists
3627 cached_module
= find_typequery_hash(s
, module
);
3628 if (!cached_module
.empty() && !s
.poison_cache
)
3630 int fd
= open(cached_module
.c_str(), O_RDONLY
);
3634 clog
<< "Pass 2: using cached " << cached_module
<< endl
;
3635 module
= cached_module
;
3642 // no cached module, time to make it
3643 if (make_typequery(s
, module
) == 0)
3645 // try to save typequery in the cache
3647 copy_file(module
, cached_module
, s
.verbose
> 2);
3653 void dwarf_cast_expanding_visitor::visit_cast_op (cast_op
* e
)
3655 bool lvalue
= is_active_lvalue(e
);
3656 if (lvalue
&& !s
.guru_mode
)
3657 throw semantic_error("write to typecast value not permitted", e
->tok
);
3659 if (e
->module
.empty())
3660 e
->module
= "kernel"; // "*" may also be reasonable to search all kernel modules
3662 functioncall
* result
= NULL
;
3664 // split the module string by ':' for alternatives
3665 vector
<string
> modules
;
3666 tokenize(e
->module
, modules
, ":");
3667 bool userspace_p
=false; // PR10601
3668 for (unsigned i
= 0; !result
&& i
< modules
.size(); ++i
)
3670 string
& module
= modules
[i
];
3671 filter_special_modules(module
);
3673 // NB: This uses '/' to distinguish between kernel modules and userspace,
3674 // which means that userspace modules won't get any PATH searching.
3678 userspace_p
=is_user_module (module
);
3681 // kernel or kernel module target
3682 dw
= db
.get_kern_dw(s
, module
);
3686 module
= find_executable (module
); // canonicalize it
3687 dw
= db
.get_user_dw(s
, module
);
3690 catch (const semantic_error
& er
)
3692 /* ignore and go to the next module */
3696 dwarf_cast_query
q (*dw
, module
, *e
, lvalue
, userspace_p
, result
);
3697 dw
->iterate_over_modules(&query_module
, &q
);
3702 // We pass the unresolved cast_op to the next pass, and hope
3703 // that this value ends up not being referenced after all, so
3704 // it can be optimized out quietly.
3711 // Provide the functioncall to our parent, so that it can be
3712 // used to substitute for the assignment node immediately above
3714 assert(!target_symbol_setter_functioncalls
.empty());
3715 *(target_symbol_setter_functioncalls
.top()) = result
;
3718 result
->visit (this);
3723 dwarf_derived_probe::printsig (ostream
& o
) const
3725 // Instead of just printing the plain locations, we add a PC value
3726 // as a comment as a way of telling e.g. apart multiple inlined
3727 // function instances. This is distinct from the verbose/clog
3728 // output, since this part goes into the cache hash calculations.
3729 sole_location()->print (o
);
3730 o
<< " /* pc=" << section
<< "+0x" << hex
<< addr
<< dec
<< " */";
3731 printsig_nested (o
);
3737 dwarf_derived_probe::join_group (systemtap_session
& s
)
3739 // skip probes which are paired entry-handlers
3740 if (!has_return
&& (saved_longs
|| saved_strings
))
3743 if (! s
.dwarf_derived_probes
)
3744 s
.dwarf_derived_probes
= new dwarf_derived_probe_group ();
3745 s
.dwarf_derived_probes
->enroll (this);
3747 if (sdt_semaphore_addr
!= 0)
3748 enable_task_finder(s
);
3752 dwarf_derived_probe::dwarf_derived_probe(const string
& funcname
,
3753 const string
& filename
,
3755 // module & section specify a relocation
3756 // base for <addr>, unless section==""
3757 // (equivalently module=="kernel")
3758 const string
& module
,
3759 const string
& section
,
3760 // NB: dwfl_addr is the virtualized
3761 // address for this symbol.
3762 Dwarf_Addr dwfl_addr
,
3763 // addr is the section-offset for
3764 // actual relocation.
3767 Dwarf_Die
* scope_die
/* may be null */)
3768 : derived_probe (q
.base_probe
, q
.base_loc
, true /* .components soon rewritten */ ),
3769 module (module
), section (section
), addr (addr
),
3771 has_process (q
.has_process
),
3772 has_return (q
.has_return
),
3773 has_maxactive (q
.has_maxactive
),
3774 has_library (q
.has_library
),
3775 maxactive_val (q
.maxactive_val
),
3776 user_path (q
.user_path
),
3777 user_lib (q
.user_lib
),
3779 saved_longs(0), saved_strings(0),
3782 if (user_lib
.size() != 0)
3787 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
3788 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
3789 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
3790 // by the incoming section value (".absolute" vs. ".dynamic").
3791 // XXX Assert invariants here too?
3795 // Assert kernel relocation invariants
3796 if (section
== "" && dwfl_addr
!= addr
) // addr should be absolute
3797 throw semantic_error ("missing relocation base against", tok
);
3798 if (section
!= "" && dwfl_addr
== addr
) // addr should be an offset
3799 throw semantic_error ("inconsistent relocation address", tok
);
3802 // XXX: hack for strange g++/gcc's
3804 #define USHRT_MAX 32767
3807 // Range limit maxactive() value
3808 if (has_maxactive
&& (maxactive_val
< 0 || maxactive_val
> USHRT_MAX
))
3809 throw semantic_error ("maxactive value out of range [0,"
3810 + lex_cast(USHRT_MAX
) + "]",
3811 q
.base_loc
->components
.front()->tok
);
3813 // Expand target variables in the probe body
3814 if (!null_die(scope_die
))
3816 // XXX: user-space deref's for q.has_process!
3817 dwarf_var_expanding_visitor
v (q
, scope_die
, dwfl_addr
);
3818 v
.replace (this->body
);
3820 access_vars
= v
.visited
;
3822 // If during target-variable-expanding the probe, we added a new block
3823 // of code, add it to the start of the probe.
3825 this->body
= new block(v
.add_block
, this->body
);
3827 // If when target-variable-expanding the probe, we need to synthesize a
3828 // sibling function-entry probe. We don't go through the whole probe derivation
3829 // business (PR10642) that could lead to wildcard/alias resolution, or for that
3830 // dwarf-induced duplication.
3831 if (v
.add_call_probe
)
3833 assert (q
.has_return
&& !q
.has_call
);
3835 // We temporarily replace q.base_probe.
3836 statement
* old_body
= q
.base_probe
->body
;
3837 q
.base_probe
->body
= v
.add_call_probe
;
3838 q
.has_return
= false;
3842 entry_handler
= new uprobe_derived_probe (funcname
, filename
, line
,
3843 module
, section
, dwfl_addr
,
3844 addr
, q
, scope_die
);
3846 entry_handler
= new dwarf_derived_probe (funcname
, filename
, line
,
3847 module
, section
, dwfl_addr
,
3848 addr
, q
, scope_die
);
3850 saved_longs
= entry_handler
->saved_longs
= v
.saved_longs
;
3851 saved_strings
= entry_handler
->saved_strings
= v
.saved_strings
;
3853 q
.results
.push_back (entry_handler
);
3855 q
.has_return
= true;
3857 q
.base_probe
->body
= old_body
;
3859 // Save the local variables for listing mode
3860 if (q
.sess
.listing_mode_vars
)
3861 saveargs(q
, scope_die
, dwfl_addr
);
3863 // else - null scope_die - $target variables will produce an error during translate phase
3865 // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs
3867 // Reset the sole element of the "locations" vector as a
3868 // "reverse-engineered" form of the incoming (q.base_loc) probe
3869 // point. This allows a user to see what function / file / line
3870 // number any particular match of the wildcards.
3872 vector
<probe_point::component
*> comps
;
3874 comps
.push_back (new probe_point::component(TOK_KERNEL
));
3875 else if(q
.has_module
)
3876 comps
.push_back (new probe_point::component(TOK_MODULE
, new literal_string(module
)));
3877 else if(q
.has_process
)
3878 comps
.push_back (new probe_point::component(TOK_PROCESS
, new literal_string(module
)));
3883 if (q
.has_function_str
|| q
.has_function_num
)
3884 fn_or_stmt
= "function";
3886 fn_or_stmt
= "statement";
3888 if (q
.has_function_str
|| q
.has_statement_str
)
3890 string retro_name
= funcname
;
3893 retro_name
+= ("@" + string (filename
));
3895 retro_name
+= (":" + lex_cast (line
));
3898 (new probe_point::component
3899 (fn_or_stmt
, new literal_string (retro_name
)));
3901 else if (q
.has_function_num
|| q
.has_statement_num
)
3903 Dwarf_Addr retro_addr
;
3904 if (q
.has_function_num
)
3905 retro_addr
= q
.function_num_val
;
3907 retro_addr
= q
.statement_num_val
;
3908 comps
.push_back (new probe_point::component
3910 new literal_number(retro_addr
, true)));
3913 comps
.push_back (new probe_point::component (TOK_ABSOLUTE
));
3917 comps
.push_back (new probe_point::component(TOK_CALL
));
3919 comps
.push_back (new probe_point::component(TOK_INLINE
));
3921 comps
.push_back (new probe_point::component(TOK_RETURN
));
3923 comps
.push_back (new probe_point::component
3924 (TOK_MAXACTIVE
, new literal_number(maxactive_val
)));
3927 this->sole_location()->components
= comps
;
3932 dwarf_derived_probe::saveargs(dwarf_query
& q
, Dwarf_Die
* scope_die
,
3933 Dwarf_Addr dwfl_addr
)
3935 if (null_die(scope_die
))
3938 bool verbose
= q
.sess
.verbose
> 2;
3941 clog
<< "saveargs: examining '" << (dwarf_diename(scope_die
) ?: "<unknown>")
3942 << "' (dieoffset: " << lex_cast_hex(dwarf_dieoffset(scope_die
)) << ")\n";
3946 /* Only save the return value if it has a type. */
3949 if (dwarf_attr_die (scope_die
, DW_AT_type
, &type_die
) &&
3950 dwarf_type_name(&type_die
, type_name
))
3951 args
.push_back("$return:"+type_name
);
3954 clog
<< "saveargs: failed to retrieve type name for return value"
3955 << " (dieoffset: " << lex_cast_hex(dwarf_dieoffset(scope_die
))
3960 vector
<Dwarf_Die
> scopes
= q
.dw
.getscopes(scope_die
);
3961 for (unsigned i
= 0; i
< scopes
.size(); ++i
)
3963 if (dwarf_tag(&scopes
[i
]) == DW_TAG_compile_unit
)
3964 break; // we don't want file-level variables
3965 if (dwarf_child (&scopes
[i
], &arg
) == 0)
3968 switch (dwarf_tag (&arg
))
3970 case DW_TAG_variable
:
3971 case DW_TAG_formal_parameter
:
3978 /* Ignore this local if it has no name. */
3979 const char *arg_name
= dwarf_diename (&arg
);
3983 clog
<< "saveargs: failed to retrieve name for local (dieoffset: "
3984 << lex_cast_hex(dwarf_dieoffset(&arg
)) << ")\n";
3989 clog
<< "saveargs: finding location for local '"
3990 << arg_name
<< "' (dieoffset: "
3991 << lex_cast_hex(dwarf_dieoffset(&arg
)) << ")\n";
3993 /* Ignore this local if it has no location (or not at this PC). */
3994 /* NB: It still may not be directly accessible, e.g. if it is an
3995 * aggregate type, implicit_pointer, etc., but the user can later
3996 * figure out how to access the interesting parts. */
3997 Dwarf_Attribute attr_mem
;
3998 if (!dwarf_attr_integrate (&arg
, DW_AT_const_value
, &attr_mem
))
4002 if (!dwarf_attr_integrate (&arg
, DW_AT_location
, &attr_mem
))
4005 clog
<< "saveargs: failed to resolve the location for local '"
4006 << arg_name
<< "' (dieoffset: "
4007 << lex_cast_hex(dwarf_dieoffset(&arg
)) << ")\n";
4010 else if (!(dwarf_getlocation_addr(&attr_mem
, dwfl_addr
, &expr
,
4011 &len
, 1) == 1 && len
> 0))
4014 clog
<< "saveargs: local '" << arg_name
<< "' (dieoffset: "
4015 << lex_cast_hex(dwarf_dieoffset(&arg
))
4016 << ") is not available at this address ("
4017 << lex_cast_hex(dwfl_addr
) << ")\n";
4022 /* Ignore this local if it has no type. */
4025 if (!dwarf_attr_die (&arg
, DW_AT_type
, &type_die
) ||
4026 !dwarf_type_name(&type_die
, type_name
))
4029 clog
<< "saveargs: failed to retrieve type name for local '"
4030 << arg_name
<< "' (dieoffset: "
4031 << lex_cast_hex(dwarf_dieoffset(&arg
)) << ")\n";
4035 /* This local looks good -- save it! */
4036 args
.push_back("$"+string(arg_name
)+":"+type_name
);
4038 while (dwarf_siblingof (&arg
, &arg
) == 0);
4044 dwarf_derived_probe::getargs(std::list
<std::string
> &arg_set
) const
4046 arg_set
.insert(arg_set
.end(), args
.begin(), args
.end());
4051 dwarf_derived_probe::emit_unprivileged_assertion (translator_output
* o
)
4055 // These probes are allowed for unprivileged users, but only in the
4056 // context of processes which they own.
4057 emit_process_owner_assertion (o
);
4061 // Other probes must contain the default assertion which aborts
4062 // if executed by an unprivileged user.
4063 derived_probe::emit_unprivileged_assertion (o
);
4068 dwarf_derived_probe::print_dupe_stamp(ostream
& o
)
4072 // These probes are allowed for unprivileged users, but only in the
4073 // context of processes which they own.
4074 print_dupe_stamp_unprivileged_process_owner (o
);
4078 // Other probes must contain the default dupe stamp
4079 derived_probe::print_dupe_stamp (o
);
4084 dwarf_derived_probe::register_statement_variants(match_node
* root
,
4086 bool bind_unprivileged_p
)
4089 ->bind_unprivileged(bind_unprivileged_p
)
4094 dwarf_derived_probe::register_function_variants(match_node
* root
,
4096 bool bind_unprivileged_p
)
4099 ->bind_unprivileged(bind_unprivileged_p
)
4101 root
->bind(TOK_INLINE
)
4102 ->bind_unprivileged(bind_unprivileged_p
)
4104 root
->bind(TOK_CALL
)
4105 ->bind_unprivileged(bind_unprivileged_p
)
4107 root
->bind(TOK_RETURN
)
4108 ->bind_unprivileged(bind_unprivileged_p
)
4110 root
->bind(TOK_RETURN
)
4111 ->bind_unprivileged(bind_unprivileged_p
)
4112 ->bind_num(TOK_MAXACTIVE
)->bind(dw
);
4116 dwarf_derived_probe::register_function_and_statement_variants(
4119 bool bind_unprivileged_p
4122 // Here we match 4 forms:
4125 // .function(0xdeadbeef)
4126 // .statement("foo")
4127 // .statement(0xdeadbeef)
4129 register_function_variants(root
->bind_str(TOK_FUNCTION
), dw
, bind_unprivileged_p
);
4130 register_function_variants(root
->bind_num(TOK_FUNCTION
), dw
, bind_unprivileged_p
);
4131 register_statement_variants(root
->bind_str(TOK_STATEMENT
), dw
, bind_unprivileged_p
);
4132 register_statement_variants(root
->bind_num(TOK_STATEMENT
), dw
, bind_unprivileged_p
);
4136 dwarf_derived_probe::register_patterns(systemtap_session
& s
)
4138 match_node
* root
= s
.pattern_root
;
4139 dwarf_builder
*dw
= new dwarf_builder();
4141 update_visitor
*filter
= new dwarf_cast_expanding_visitor(s
, *dw
);
4142 s
.code_filters
.push_back(filter
);
4144 register_function_and_statement_variants(root
->bind(TOK_KERNEL
), dw
);
4145 register_function_and_statement_variants(root
->bind_str(TOK_MODULE
), dw
);
4147 root
->bind(TOK_KERNEL
)->bind_num(TOK_STATEMENT
)->bind(TOK_ABSOLUTE
)
4149 root
->bind(TOK_KERNEL
)->bind_str(TOK_FUNCTION
)->bind_str(TOK_LABEL
)
4152 register_function_and_statement_variants(root
->bind_str(TOK_PROCESS
), dw
,
4153 true/*bind_unprivileged*/);
4154 root
->bind_str(TOK_PROCESS
)->bind_str(TOK_FUNCTION
)->bind_str(TOK_LABEL
)
4155 ->bind_unprivileged()
4157 root
->bind_str(TOK_PROCESS
)->bind_str(TOK_LIBRARY
)->bind_str(TOK_MARK
)
4158 ->bind_unprivileged()
4160 root
->bind_str(TOK_PROCESS
)->bind_str(TOK_LIBRARY
)->bind_str(TOK_PROVIDER
)->bind_str(TOK_MARK
)
4161 ->bind_unprivileged()
4163 root
->bind_str(TOK_PROCESS
)->bind_str(TOK_MARK
)
4164 ->bind_unprivileged()
4166 root
->bind_str(TOK_PROCESS
)->bind_str(TOK_PROVIDER
)->bind_str(TOK_MARK
)
4167 ->bind_unprivileged()
4172 dwarf_derived_probe::emit_probe_local_init(translator_output
* o
)
4176 // if accessing $variables, emit bsp cache setup for speeding up
4177 o
->newline() << "#if defined __ia64__";
4178 o
->newline() << "bspcache(c->unwaddr, c->regs);";
4179 o
->newline() << "#endif";
4183 // ------------------------------------------------------------------------
4186 dwarf_derived_probe_group::enroll (dwarf_derived_probe
* p
)
4188 if (p
->sdt_semaphore_addr
!= 0)
4189 has_semaphores
= true;
4190 probes_by_module
.insert (make_pair (p
->module
, p
));
4192 // XXX: probes put at the same address should all share a
4193 // single kprobe/kretprobe, and have their handlers executed
4198 dwarf_derived_probe_group::emit_module_decls (systemtap_session
& s
)
4200 if (probes_by_module
.empty()) return;
4202 s
.op
->newline() << "/* ---- dwarf probes ---- */";
4204 // Warn of misconfigured kernels
4205 s
.op
->newline() << "#if ! defined(CONFIG_KPROBES)";
4206 s
.op
->newline() << "#error \"Need CONFIG_KPROBES!\"";
4207 s
.op
->newline() << "#endif";
4210 s
.op
->newline() << "#ifndef KRETACTIVE";
4211 s
.op
->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
4212 s
.op
->newline() << "#endif";
4215 s
.op
->newline() << "#include \"kprobes-common.h\"";
4217 // Forward declare the master entry functions
4218 s
.op
->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4219 s
.op
->line() << " struct pt_regs *regs);";
4220 s
.op
->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4221 s
.op
->line() << " struct pt_regs *regs);";
4223 // Emit an array of kprobe/kretprobe pointers
4224 s
.op
->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4225 s
.op
->newline() << "static void * stap_unreg_kprobes[" << probes_by_module
.size() << "];";
4226 s
.op
->newline() << "#endif";
4228 // Emit the actual probe list.
4230 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
4231 // struct stap_dwarf_probe, but it being initialized data makes it add
4232 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
4233 s
.op
->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module
.size() << "];";
4236 s
.op
->newline() << "static struct stap_dwarf_probe {";
4237 s
.op
->newline(1) << "const unsigned return_p:1;";
4238 s
.op
->newline() << "const unsigned maxactive_p:1;";
4239 s
.op
->newline() << "const unsigned optional_p:1;";
4240 s
.op
->newline() << "unsigned registered_p:1;";
4241 s
.op
->newline() << "const unsigned short maxactive_val;";
4243 // data saved in the kretprobe_instance packet
4244 s
.op
->newline() << "const unsigned short saved_longs;";
4245 s
.op
->newline() << "const unsigned short saved_strings;";
4247 // Let's find some stats for the embedded strings. Maybe they
4248 // are small and uniform enough to justify putting char[MAX]'s into
4249 // the array instead of relocated char*'s.
4250 size_t module_name_max
= 0, section_name_max
= 0;
4251 size_t module_name_tot
= 0, section_name_tot
= 0;
4252 size_t all_name_cnt
= probes_by_module
.size(); // for average
4253 for (p_b_m_iterator it
= probes_by_module
.begin(); it
!= probes_by_module
.end(); it
++)
4255 dwarf_derived_probe
* p
= it
->second
;
4256 #define DOIT(var,expr) do { \
4257 size_t var##_size = (expr) + 1; \
4258 var##_max = max (var##_max, var##_size); \
4259 var##_tot += var##_size; } while (0)
4260 DOIT(module_name
, p
->module
.size());
4261 DOIT(section_name
, p
->section
.size());
4265 // Decide whether it's worthwhile to use char[] or char* by comparing
4266 // the amount of average waste (max - avg) to the relocation data size
4267 // (3 native long words).
4268 #define CALCIT(var) \
4269 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
4271 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
4272 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
4273 << "[" << var##_name_max << "]" << endl; \
4277 s.op->newline() << "const char * const " << #var << ";"; \
4278 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
4285 s
.op
->newline() << "const unsigned long address;";
4286 s
.op
->newline() << "struct stap_probe * const probe;";
4287 s
.op
->newline() << "struct stap_probe * const entry_probe;";
4290 s
.op
->newline() << "const unsigned long sdt_sem_offset;";
4291 s
.op
->newline() << "unsigned long sdt_sem_address;";
4292 s
.op
->newline() << "struct task_struct *tsk;";
4293 s
.op
->newline() << "const char *pathname;";
4294 s
.op
->newline() << "struct stap_task_finder_target finder;";
4296 s
.op
->newline(-1) << "} stap_dwarf_probes[] = {";
4299 for (p_b_m_iterator it
= probes_by_module
.begin(); it
!= probes_by_module
.end(); it
++)
4301 dwarf_derived_probe
* p
= it
->second
;
4302 s
.op
->newline() << "{";
4304 s
.op
->line() << " .return_p=1,";
4305 if (p
->has_maxactive
)
4307 s
.op
->line() << " .maxactive_p=1,";
4308 assert (p
->maxactive_val
>= 0 && p
->maxactive_val
<= USHRT_MAX
);
4309 s
.op
->line() << " .maxactive_val=" << p
->maxactive_val
<< ",";
4311 if (p
->saved_longs
|| p
->saved_strings
)
4314 s
.op
->line() << " .saved_longs=" << p
->saved_longs
<< ",";
4315 if (p
->saved_strings
)
4316 s
.op
->line() << " .saved_strings=" << p
->saved_strings
<< ",";
4317 if (p
->entry_handler
)
4318 s
.op
->line() << " .entry_probe=" << common_probe_init (p
->entry_handler
) << ",";
4320 if (p
->locations
[0]->optional
)
4321 s
.op
->line() << " .optional_p=1,";
4322 s
.op
->line() << " .address=(unsigned long)0x" << hex
<< p
->addr
<< dec
<< "ULL,";
4323 s
.op
->line() << " .module=\"" << p
->module
<< "\",";
4324 s
.op
->line() << " .section=\"" << p
->section
<< "\",";
4325 s
.op
->line() << " .probe=" << common_probe_init (p
) << ",";
4326 if (p
->sdt_semaphore_addr
!= 0)
4328 s
.op
->line() << " .sdt_sem_offset=(unsigned long)0x" << hex
<< p
->sdt_semaphore_addr
<< dec
<< "ULL,";
4329 s
.op
->line() << " .sdt_sem_address=0,";
4332 s
.op
->line() << " .finder={";
4333 s
.op
->line() << " .pid=0,";
4334 s
.op
->line() << " .procname=\"" << p
->user_path
<< "\",";
4335 s
.op
->line() << " .mmap_callback=&stap_kprobe_mmap_found,";
4336 s
.op
->line() << " },";
4337 s
.op
->line() << " .pathname=\"" << p
->user_lib
<< "\",";
4341 s
.op
->line() << " .finder={";
4342 s
.op
->line() << " .pid=0,";
4343 s
.op
->line() << " .procname=\"" << p
->user_path
<< "\",";
4344 s
.op
->line() << " .callback=&stap_kprobe_process_found,";
4345 s
.op
->line() << " },";
4346 s
.op
->line() << " .pathname=\"" << p
->user_path
<< "\",";
4351 s
.op
->line() << " },";
4354 s
.op
->newline(-1) << "};";
4356 // Emit the kprobes callback function
4358 s
.op
->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4359 s
.op
->line() << " struct pt_regs *regs) {";
4360 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4361 s
.op
->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
4362 // Check that the index is plausible
4363 s
.op
->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4364 s
.op
->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module
.size() << ")?";
4365 s
.op
->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4366 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4367 s
.op
->line() << "];";
4368 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "sdp->probe");
4369 s
.op
->newline() << "c->regs = regs;";
4371 // Make it look like the IP is set as it wouldn't have been replaced
4372 // by a breakpoint instruction when calling real probe handler. Reset
4373 // IP regs on return, so we don't confuse kprobes. PR10458
4374 s
.op
->newline() << "{";
4376 s
.op
->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
4377 s
.op
->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
4378 s
.op
->newline() << "(*sdp->probe->ph) (c);";
4379 s
.op
->newline() << "SET_REG_IP(regs, kprobes_ip);";
4380 s
.op
->newline(-1) << "}";
4382 common_probe_entryfn_epilogue (s
.op
);
4383 s
.op
->newline() << "return 0;";
4384 s
.op
->newline(-1) << "}";
4386 // Same for kretprobes
4388 s
.op
->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
4389 s
.op
->line() << " struct pt_regs *regs, int entry) {";
4390 s
.op
->newline(1) << "struct kretprobe *krp = inst->rp;";
4392 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4393 s
.op
->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
4394 // Check that the index is plausible
4395 s
.op
->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4396 s
.op
->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module
.size() << ")?";
4397 s
.op
->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4398 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4399 s
.op
->line() << "];";
4401 s
.op
->newline() << "struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
4402 s
.op
->newline() << "if (sp) {";
4404 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "sp");
4405 s
.op
->newline() << "c->regs = regs;";
4407 // for assisting runtime's backtrace logic and accessing kretprobe data packets
4408 s
.op
->newline() << "c->pi = inst;";
4409 s
.op
->newline() << "c->pi_longs = sdp->saved_longs;";
4411 // Make it look like the IP is set as it wouldn't have been replaced
4412 // by a breakpoint instruction when calling real probe handler. Reset
4413 // IP regs on return, so we don't confuse kprobes. PR10458
4414 s
.op
->newline() << "{";
4415 s
.op
->newline(1) << "unsigned long kprobes_ip = REG_IP(c->regs);";
4416 s
.op
->newline() << "if (entry)";
4417 s
.op
->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
4418 s
.op
->newline(-1) << "else";
4419 s
.op
->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
4420 s
.op
->newline(-1) << "(sp->ph) (c);";
4421 s
.op
->newline() << "SET_REG_IP(regs, kprobes_ip);";
4422 s
.op
->newline(-1) << "}";
4424 common_probe_entryfn_epilogue (s
.op
);
4425 s
.op
->newline(-1) << "}";
4426 s
.op
->newline() << "return 0;";
4427 s
.op
->newline(-1) << "}";
4430 s
.op
->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4431 s
.op
->line() << " struct pt_regs *regs) {";
4432 s
.op
->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
4433 s
.op
->newline(-1) << "}";
4436 s
.op
->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
4437 s
.op
->line() << " struct pt_regs *regs) {";
4438 s
.op
->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
4439 s
.op
->newline(-1) << "}";
4443 s
.op
->newline() << "#define KPROBES_TASK_FINDER 1";
4444 s
.op
->newline() << "#include \"kprobes-common.c\"";
4450 dwarf_derived_probe_group::emit_module_init (systemtap_session
& s
)
4452 if (has_semaphores
) // Ignore if there are no semaphores
4454 s
.op
->newline() << "for (i=0; i<ARRAY_SIZE(stap_dwarf_probes); i++) {";
4455 s
.op
->newline(1) << "int rc;";
4456 s
.op
->newline() << "struct stap_dwarf_probe *p = &stap_dwarf_probes[i];";
4457 s
.op
->newline() << "probe_point = p->probe->pp;"; // for error messages
4458 s
.op
->newline() << "if (p->sdt_sem_offset) {";
4459 s
.op
->newline(1) << "rc = stap_register_task_finder_target(&p->finder);";
4460 s
.op
->newline(-1) << "}";
4461 s
.op
->newline() << "if (rc) break;";
4462 s
.op
->newline(-1) << "}";
4465 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
4466 s
.op
->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4467 s
.op
->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4468 s
.op
->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
4469 s
.op
->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
4470 s
.op
->newline() << "probe_point = sdp->probe->pp;"; // for error messages
4471 s
.op
->newline() << "if (sdp->return_p) {";
4472 s
.op
->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
4473 s
.op
->newline() << "if (sdp->maxactive_p) {";
4474 s
.op
->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
4475 s
.op
->newline(-1) << "} else {";
4476 s
.op
->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
4477 s
.op
->newline(-1) << "}";
4478 s
.op
->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
4479 s
.op
->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
4480 s
.op
->newline() << "if (sdp->entry_probe) {";
4481 s
.op
->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
4482 s
.op
->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
4483 s
.op
->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
4484 s
.op
->newline(-1) << "}";
4485 s
.op
->newline() << "#endif";
4486 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4487 s
.op
->newline() << "#ifdef __ia64__";
4488 s
.op
->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
4489 s
.op
->newline() << "kp->dummy.pre_handler = NULL;";
4490 s
.op
->newline() << "rc = register_kprobe (& kp->dummy);";
4491 s
.op
->newline() << "if (rc == 0) {";
4492 s
.op
->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
4493 s
.op
->newline() << "if (rc != 0)";
4494 s
.op
->newline(1) << "unregister_kprobe (& kp->dummy);";
4495 s
.op
->newline(-2) << "}";
4496 s
.op
->newline() << "#else";
4497 s
.op
->newline() << "rc = register_kretprobe (& kp->u.krp);";
4498 s
.op
->newline() << "#endif";
4499 s
.op
->newline(-1) << "} else {";
4500 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4501 s
.op
->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
4502 s
.op
->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
4503 s
.op
->newline() << "#ifdef __ia64__";
4504 s
.op
->newline() << "kp->dummy.addr = kp->u.kp.addr;";
4505 s
.op
->newline() << "kp->dummy.pre_handler = NULL;";
4506 s
.op
->newline() << "rc = register_kprobe (& kp->dummy);";
4507 s
.op
->newline() << "if (rc == 0) {";
4508 s
.op
->newline(1) << "rc = register_kprobe (& kp->u.kp);";
4509 s
.op
->newline() << "if (rc != 0)";
4510 s
.op
->newline(1) << "unregister_kprobe (& kp->dummy);";
4511 s
.op
->newline(-2) << "}";
4512 s
.op
->newline() << "#else";
4513 s
.op
->newline() << "rc = register_kprobe (& kp->u.kp);";
4514 s
.op
->newline() << "#endif";
4515 s
.op
->newline(-1) << "}";
4516 s
.op
->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
4517 s
.op
->newline(1) << "sdp->registered_p = 0;";
4518 s
.op
->newline() << "if (!sdp->optional_p)";
4519 s
.op
->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
4520 s
.op
->newline(-1) << "rc = 0;"; // continue with other probes
4521 // XXX: shall we increment numskipped?
4522 s
.op
->newline(-1) << "}";
4524 #if 0 /* pre PR 6749; XXX consider making an option */
4525 s
.op
->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
4526 s
.op
->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
4527 s
.op
->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
4528 s
.op
->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
4529 s
.op
->newline() << "else unregister_kprobe (&kp2->u.kp);";
4530 s
.op
->newline() << "#ifdef __ia64__";
4531 s
.op
->newline() << "unregister_kprobe (&kp2->dummy);";
4532 s
.op
->newline() << "#endif";
4533 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
4534 // not run for this early-abort case.
4535 s
.op
->newline(-1) << "}";
4536 s
.op
->newline() << "break;"; // don't attempt to register any more probes
4537 s
.op
->newline(-1) << "}";
4540 s
.op
->newline() << "else sdp->registered_p = 1;";
4541 s
.op
->newline(-1) << "}"; // for loop
4546 dwarf_derived_probe_group::emit_module_exit (systemtap_session
& s
)
4550 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
4551 s
.op
->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4552 s
.op
->newline() << "unsigned short sdt_semaphore = 0;"; // NB: fixed size
4553 s
.op
->newline() << "if (sdp->sdt_sem_address && __access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 0)) {";
4554 s
.op
->newline(1) << "sdt_semaphore --;";
4555 s
.op
->newline() << "__access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 1);";
4556 s
.op
->newline(-1) << "}";
4557 s
.op
->newline(-1) << "}";
4560 //Unregister kprobes by batch interfaces.
4561 s
.op
->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4562 s
.op
->newline() << "j = 0;";
4563 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
4564 s
.op
->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4565 s
.op
->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4566 s
.op
->newline() << "if (! sdp->registered_p) continue;";
4567 s
.op
->newline() << "if (!sdp->return_p)";
4568 s
.op
->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
4569 s
.op
->newline(-2) << "}";
4570 s
.op
->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4571 s
.op
->newline() << "j = 0;";
4572 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
4573 s
.op
->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4574 s
.op
->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4575 s
.op
->newline() << "if (! sdp->registered_p) continue;";
4576 s
.op
->newline() << "if (sdp->return_p)";
4577 s
.op
->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
4578 s
.op
->newline(-2) << "}";
4579 s
.op
->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
4580 s
.op
->newline() << "#ifdef __ia64__";
4581 s
.op
->newline() << "j = 0;";
4582 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
4583 s
.op
->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4584 s
.op
->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4585 s
.op
->newline() << "if (! sdp->registered_p) continue;";
4586 s
.op
->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
4587 s
.op
->newline(-1) << "}";
4588 s
.op
->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4589 s
.op
->newline() << "#endif";
4590 s
.op
->newline() << "#endif";
4592 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
4593 s
.op
->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4594 s
.op
->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4595 s
.op
->newline() << "if (! sdp->registered_p) continue;";
4596 s
.op
->newline() << "if (sdp->return_p) {";
4597 s
.op
->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
4598 s
.op
->newline(1) << "unregister_kretprobe (&kp->u.krp);";
4599 s
.op
->newline() << "#endif";
4600 s
.op
->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
4601 s
.op
->newline() << "#ifdef STP_TIMING";
4602 s
.op
->newline() << "if (kp->u.krp.nmissed)";
4603 s
.op
->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
4604 s
.op
->newline(-1) << "#endif";
4605 s
.op
->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
4606 s
.op
->newline() << "#ifdef STP_TIMING";
4607 s
.op
->newline() << "if (kp->u.krp.kp.nmissed)";
4608 s
.op
->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
4609 s
.op
->newline(-1) << "#endif";
4610 s
.op
->newline(-1) << "} else {";
4611 s
.op
->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
4612 s
.op
->newline(1) << "unregister_kprobe (&kp->u.kp);";
4613 s
.op
->newline() << "#endif";
4614 s
.op
->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
4615 s
.op
->newline() << "#ifdef STP_TIMING";
4616 s
.op
->newline() << "if (kp->u.kp.nmissed)";
4617 s
.op
->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
4618 s
.op
->newline(-1) << "#endif";
4619 s
.op
->newline(-1) << "}";
4620 s
.op
->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
4621 s
.op
->newline() << "unregister_kprobe (&kp->dummy);";
4622 s
.op
->newline() << "#endif";
4623 s
.op
->newline() << "sdp->registered_p = 0;";
4624 s
.op
->newline(-1) << "}";
4628 struct sdt_kprobe_var_expanding_visitor
: public var_expanding_visitor
4630 sdt_kprobe_var_expanding_visitor(const string
& process_name
,
4631 const string
& provider_name
,
4632 const string
& probe_name
,
4633 const string
& arg_string
,
4635 process_name (process_name
), provider_name (provider_name
), probe_name (probe_name
),
4636 arg_count (arg_count
)
4638 tokenize(arg_string
, arg_tokens
, " ");
4639 assert(arg_count
>= 0 && arg_count
<= 10);
4641 const string
& process_name
;
4642 const string
& provider_name
;
4643 const string
& probe_name
;
4645 vector
<string
> arg_tokens
;
4647 void visit_target_symbol (target_symbol
* e
);
4651 struct sdt_uprobe_var_expanding_visitor
: public var_expanding_visitor
4653 sdt_uprobe_var_expanding_visitor(systemtap_session
& s
,
4655 const string
& process_name
,
4656 const string
& provider_name
,
4657 const string
& probe_name
,
4658 stap_sdt_probe_type probe_type
,
4659 const string
& arg_string
,
4661 session (s
), elf_machine (elf_machine
), process_name (process_name
),
4662 provider_name (provider_name
), probe_name (probe_name
),
4663 probe_type (probe_type
), arg_count ((unsigned) ac
)
4665 /* Register name mapping table depends on the elf machine of this particular
4666 probe target process/file, not upon the host. So we can't just
4667 #ifdef _i686_ etc. */
4668 if (elf_machine
== EM_X86_64
) {
4669 dwarf_regs
["%rax"] = dwarf_regs
["%eax"] = dwarf_regs
["%ax"] = dwarf_regs
["%al"] = dwarf_regs
["%ah"] = 0;
4670 dwarf_regs
["%rdx"] = dwarf_regs
["%edx"] = dwarf_regs
["%dx"] = dwarf_regs
["%dl"] = dwarf_regs
["%dh"] = 1;
4671 dwarf_regs
["%rcx"] = dwarf_regs
["%ecx"] = dwarf_regs
["%cx"] = dwarf_regs
["%cl"] = dwarf_regs
["%ch"] = 2;
4672 dwarf_regs
["%rbx"] = dwarf_regs
["%ebx"] = dwarf_regs
["%bx"] = dwarf_regs
["%bl"] = dwarf_regs
["%bh"] = 3;
4673 dwarf_regs
["%rsi"] = dwarf_regs
["%esi"] = dwarf_regs
["%si"] = dwarf_regs
["%sil"] = 4;
4674 dwarf_regs
["%rdi"] = dwarf_regs
["%edi"] = dwarf_regs
["%di"] = dwarf_regs
["%dil"] = 5;
4675 dwarf_regs
["%rbp"] = dwarf_regs
["%ebp"] = dwarf_regs
["%bp"] = 6;
4676 dwarf_regs
["%rsp"] = dwarf_regs
["%esp"] = dwarf_regs
["%sp"] = 7;
4677 dwarf_regs
["%r8"] = dwarf_regs
["%r8d"] = dwarf_regs
["%r8w"] = dwarf_regs
["%r8b"] = 8;
4678 dwarf_regs
["%r9"] = dwarf_regs
["%r9d"] = dwarf_regs
["%r9w"] = dwarf_regs
["%r9b"] = 9;
4679 dwarf_regs
["%r10"] = dwarf_regs
["%r10d"] = dwarf_regs
["%r10w"] = dwarf_regs
["%r10b"] = 10;
4680 dwarf_regs
["%r11"] = dwarf_regs
["%r11d"] = dwarf_regs
["%r11w"] = dwarf_regs
["%r11b"] = 11;
4681 dwarf_regs
["%r12"] = dwarf_regs
["%r12d"] = dwarf_regs
["%r12w"] = dwarf_regs
["%r12b"] = 12;
4682 dwarf_regs
["%r13"] = dwarf_regs
["%r13d"] = dwarf_regs
["%r13w"] = dwarf_regs
["%r13b"] = 13;
4683 dwarf_regs
["%r14"] = dwarf_regs
["%r14d"] = dwarf_regs
["%r14w"] = dwarf_regs
["%r14b"] = 14;
4684 dwarf_regs
["%r15"] = dwarf_regs
["%r15d"] = dwarf_regs
["%r15w"] = dwarf_regs
["%r15b"] = 15;
4685 } else if (elf_machine
== EM_386
) {
4686 dwarf_regs
["%eax"] = dwarf_regs
["%ax"] = dwarf_regs
["%al"] = dwarf_regs
["%ah"] = 0;
4687 dwarf_regs
["%ecx"] = dwarf_regs
["%cx"] = dwarf_regs
["%cl"] = dwarf_regs
["%ch"] = 1;
4688 dwarf_regs
["%edx"] = dwarf_regs
["%dx"] = dwarf_regs
["%dl"] = dwarf_regs
["%dh"] = 2;
4689 dwarf_regs
["%ebx"] = dwarf_regs
["%bx"] = dwarf_regs
["%bl"] = dwarf_regs
["%bh"] = 3;
4690 dwarf_regs
["%esp"] = dwarf_regs
["%sp"] = 4;
4691 dwarf_regs
["%ebp"] = dwarf_regs
["%bp"] = 5;
4692 dwarf_regs
["%esi"] = dwarf_regs
["%si"] = dwarf_regs
["%sil"] = 6;
4693 dwarf_regs
["%edi"] = dwarf_regs
["%di"] = dwarf_regs
["%dil"] = 7;
4694 } else if (elf_machine
== EM_PPC
|| elf_machine
== EM_PPC64
) {
4695 dwarf_regs
["%r0"] = 0; dwarf_regs
["%r1"] = 1; dwarf_regs
["%r2"] = 2;
4696 dwarf_regs
["%r3"] = 3; dwarf_regs
["%r4"] = 4; dwarf_regs
["%r5"] = 5;
4697 dwarf_regs
["%r6"] = 6; dwarf_regs
["%r7"] = 7; dwarf_regs
["%r8"] = 8;
4698 dwarf_regs
["%r9"] = 9; dwarf_regs
["%r10"] = 10; dwarf_regs
["%r11"] = 11;
4699 dwarf_regs
["%r12"] = 12; dwarf_regs
["%r13"] = 13; dwarf_regs
["%r14"] = 14;
4700 dwarf_regs
["%r15"] = 15; dwarf_regs
["%r16"] = 16; dwarf_regs
["%r17"] = 17;
4701 dwarf_regs
["%r18"] = 18; dwarf_regs
["%r19"] = 19; dwarf_regs
["%r20"] = 20;
4702 dwarf_regs
["%r21"] = 21; dwarf_regs
["%r22"] = 22; dwarf_regs
["%r23"] = 23;
4703 dwarf_regs
["%r24"] = 24; dwarf_regs
["%r25"] = 25; dwarf_regs
["%r26"] = 26;
4704 dwarf_regs
["%r27"] = 27; dwarf_regs
["%r28"] = 28; dwarf_regs
["%r29"] = 29;
4705 dwarf_regs
["%r30"] = 30; dwarf_regs
["%r31"] = 31;
4706 // PR11821: unadorned register "names" without -mregnames
4707 dwarf_regs
["0"] = 0; dwarf_regs
["1"] = 1; dwarf_regs
["2"] = 2;
4708 dwarf_regs
["3"] = 3; dwarf_regs
["4"] = 4; dwarf_regs
["5"] = 5;
4709 dwarf_regs
["6"] = 6; dwarf_regs
["7"] = 7; dwarf_regs
["8"] = 8;
4710 dwarf_regs
["9"] = 9; dwarf_regs
["10"] = 10; dwarf_regs
["11"] = 11;
4711 dwarf_regs
["12"] = 12; dwarf_regs
["13"] = 13; dwarf_regs
["14"] = 14;
4712 dwarf_regs
["15"] = 15; dwarf_regs
["16"] = 16; dwarf_regs
["17"] = 17;
4713 dwarf_regs
["18"] = 18; dwarf_regs
["19"] = 19; dwarf_regs
["20"] = 20;
4714 dwarf_regs
["21"] = 21; dwarf_regs
["22"] = 22; dwarf_regs
["23"] = 23;
4715 dwarf_regs
["24"] = 24; dwarf_regs
["25"] = 25; dwarf_regs
["26"] = 26;
4716 dwarf_regs
["27"] = 27; dwarf_regs
["28"] = 28; dwarf_regs
["29"] = 29;
4717 dwarf_regs
["30"] = 30; dwarf_regs
["31"] = 31;
4718 } else if (elf_machine
== EM_S390
) {
4719 dwarf_regs
["%r0"] = 0; dwarf_regs
["%r1"] = 1; dwarf_regs
["%r2"] = 2;
4720 dwarf_regs
["%r3"] = 3; dwarf_regs
["%r4"] = 4; dwarf_regs
["%r5"] = 5;
4721 dwarf_regs
["%r6"] = 6; dwarf_regs
["%r7"] = 7; dwarf_regs
["%r8"] = 8;
4722 dwarf_regs
["%r9"] = 9; dwarf_regs
["%r10"] = 10; dwarf_regs
["%r11"] = 11;
4723 dwarf_regs
["%r12"] = 12; dwarf_regs
["%r13"] = 13; dwarf_regs
["%r14"] = 14;
4724 dwarf_regs
["%r15"] = 15;
4725 } else if (arg_count
) {
4726 /* permit this case; just fall back to dwarf */
4729 need_debug_info
= false;
4730 tokenize(arg_string
, arg_tokens
, " ");
4731 assert(arg_count
>= 0 && arg_count
<= 10);
4734 systemtap_session
& session
;
4736 const string
& process_name
;
4737 const string
& provider_name
;
4738 const string
& probe_name
;
4739 stap_sdt_probe_type probe_type
;
4741 vector
<string
> arg_tokens
;
4742 map
<string
,int> dwarf_regs
;
4743 bool need_debug_info
;
4745 void visit_target_symbol (target_symbol
* e
);
4746 __uint64_t
get_register_width (string
);
4751 sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol
*e
)
4755 if (e
->name
== "$$name")
4758 throw semantic_error("cannot take address of sdt context variable", e
->tok
);
4760 literal_string
*myname
= new literal_string (probe_name
);
4761 myname
->tok
= e
->tok
;
4765 if (e
->name
== "$$provider")
4768 throw semantic_error("cannot take address of sdt context variable", e
->tok
);
4770 literal_string
*myname
= new literal_string (provider_name
);
4771 myname
->tok
= e
->tok
;
4776 unsigned argno
= 0; // the N in $argN
4779 if (startswith(e
->name
, "$arg"))
4780 argno
= lex_cast
<unsigned>(e
->name
.substr(4));
4782 catch (const runtime_error
& f
) // non-integral $arg suffix: e.g. $argKKKSDF
4787 if (arg_count
== 0 || // a sdt.h variant without .probe-stored arg_count
4788 argno
< 1 || argno
> arg_count
) // a $argN with out-of-range N
4791 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
4792 // 2) uprobe2_type $FOO (no probe args)
4793 // both of which get resolved later.
4794 need_debug_info
= true;
4799 assert (arg_tokens
.size() >= argno
);
4800 string asmarg
= arg_tokens
[argno
-1]; // $arg1 => arg_tokens[0]
4802 // Now we try to parse this thing, which is an assembler operand
4803 // expression. If we can't, we warn, back down to need_debug_info
4804 // and hope for the best. Here is the syntax for a few architectures.
4805 // Note that the power iN syntax is only for V3 sdt.h; gcc emits N.
4806 // literal reg reg reg +
4808 // x86 $N %rR (%rR) N(%rR)
4809 // power iN R (R) N(R)
4811 // s390 N %rR 0(rR) N(r15)
4812 // arm #N rR [rR] [rR, #N]
4814 expression
* argexpr
= 0; // filled in in case of successful parse
4816 string percent_regnames
;
4818 vector
<string
> matches
;
4822 // Parse the leading length
4824 if (asmarg
.find('@') != string::npos
)
4826 precision
= lex_cast
<int>(asmarg
.substr(0, asmarg
.find('@')));
4827 asmarg
= asmarg
.substr(asmarg
.find('@')+1);
4831 // V1/V2 do not have precision field so default to signed long
4832 // V3 asm does not have precision field so default to unsigned long
4833 if (probe_type
== uprobe3_type
)
4834 precision
= sizeof(long); // this is an asm probe
4836 precision
= -sizeof(long);
4839 // test for a numeric literal.
4840 // Only accept (signed) decimals throughout. XXX
4842 // PR11821. NB: on powerpc, literals are not prefixed with $,
4843 // so this regex does not match. But that's OK, since without
4844 // -mregnames, we can't tell them apart from register numbers
4845 // anyway. With -mregnames, we could, if gcc somehow
4846 // communicated to us the presence of that option, but alas it
4847 // doesn't. http://gcc.gnu.org/PR44995.
4848 rc
= regexp_match (asmarg
, "^[i\\$][-]?[0-9][0-9]*$", matches
);
4851 string sn
= matches
[0].substr(1);
4855 // We have to pay attention to the size & sign, as gcc sometimes
4856 // propagates constants that don't quite match, like a negative
4857 // value to fill an unsigned type.
4860 case -1: n
= lex_cast
< int8_t>(sn
); break;
4861 case 1: n
= lex_cast
< uint8_t>(sn
); break;
4862 case -2: n
= lex_cast
< int16_t>(sn
); break;
4863 case 2: n
= lex_cast
<uint16_t>(sn
); break;
4864 case -4: n
= lex_cast
< int32_t>(sn
); break;
4865 case 4: n
= lex_cast
<uint32_t>(sn
); break;
4867 case -8: n
= lex_cast
< int64_t>(sn
); break;
4868 case 8: n
= lex_cast
<uint64_t>(sn
); break;
4871 catch (std::runtime_error
&)
4875 literal_number
* ln
= new literal_number(n
);
4881 if (dwarf_regs
.empty())
4884 // Build regex pieces out of the known dwarf_regs. We keep two separate
4885 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
4886 // and ones with no prefix (and thus only usable in unambiguous contexts).
4887 for (map
<string
,int>::iterator ri
= dwarf_regs
.begin(); ri
!= dwarf_regs
.end(); ri
++)
4889 string regname
= ri
->first
;
4890 assert (regname
!= "");
4891 regnames
+= string("|")+regname
;
4892 if (regname
[0]=='%')
4893 percent_regnames
+= string("|")+regname
;
4895 // clip off leading |
4896 regnames
= regnames
.substr(1);
4897 percent_regnames
= percent_regnames
.substr(1);
4899 // test for REGISTER
4900 // NB: Because PR11821, we must use percent_regnames here.
4901 if (elf_machine
== EM_PPC
|| elf_machine
== EM_PPC64
)
4902 rc
= regexp_match (asmarg
, string("^(")+regnames
+string(")$"), matches
);
4904 rc
= regexp_match (asmarg
, string("^(")+percent_regnames
+string(")$"), matches
);
4907 string regname
= matches
[1];
4908 if (dwarf_regs
.find (regname
) != dwarf_regs
.end()) // known register
4910 embedded_expr
*get_arg1
= new embedded_expr
;
4911 string width_adjust
;
4912 switch (get_register_width (regname
))
4914 case 0xff: width_adjust
= ") & 0xff"; break;
4915 case 0xff00: width_adjust
= ">>8) & 0xff"; break;
4917 // preserve 16 bit register signness
4918 width_adjust
= (precision
> 0) ? ") & 0xffff" : ")";
4920 default: width_adjust
= ")";
4922 string type
= (precision
< 0 ? "(int" : "(uint")
4923 + lex_cast(abs(precision
) * 8) + "_t)((";
4924 get_arg1
->tok
= e
->tok
;
4925 get_arg1
->code
= string("/* unprivileged */ /* pure */")
4926 + string(" (int64_t)") + type
4927 + (is_user_module (process_name
)
4928 ? string("u_fetch_register(")
4929 : string("k_fetch_register("))
4930 + lex_cast(dwarf_regs
[regname
]) + string("))")
4935 // invalid register name, fall through
4938 // test for OFFSET(REGISTER)
4939 // NB: Despite PR11821, we can use regnames here, since the parentheses
4940 // make things unambiguous.
4941 rc
= regexp_match (asmarg
, string("^([-]?[0-9]*)[(](")+regnames
+string(")[)]$"), matches
);
4944 string dispstr
= matches
[1];
4951 disp
= lex_cast
<int64_t>(dispstr
); // should decode positive/negative hex/decimal
4953 catch (const runtime_error
& f
) // unparseable offset
4955 goto not_matched
; // can't just 'break' out of
4956 // this case or use a sentinel
4957 // value, unfortunately
4960 string regname
= matches
[2];
4961 if (dwarf_regs
.find (regname
) != dwarf_regs
.end()) // known register
4963 // synthesize user_long(%{fetch_register(R)%} + D)
4965 embedded_expr
*get_arg1
= new embedded_expr
;
4966 get_arg1
->tok
= e
->tok
;
4967 get_arg1
->code
= string("/* unprivileged */ /* pure */")
4968 + (is_user_module (process_name
)
4969 ? string("u_fetch_register(")
4970 : string("k_fetch_register("))
4971 + lex_cast(dwarf_regs
[regname
]) + string(")");
4972 // XXX: may we ever need to cast that to a narrower type?
4974 literal_number
* inc
= new literal_number(disp
);
4977 binary_expression
*be
= new binary_expression
;
4979 be
->left
= get_arg1
;
4983 functioncall
*fc
= new functioncall
;
4987 fc
->function
= "user_int8"; break;
4989 fc
->function
= "user_uint16"; break;
4991 fc
->function
= "user_int16"; break;
4993 fc
->function
= "user_uint32"; break;
4995 fc
->function
= "user_int32"; break;
4997 fc
->function
= "user_int64"; break;
4998 default: fc
->function
= "user_long";
5001 fc
->args
.push_back(be
);
5006 // invalid register name, fall through
5010 // The asmarg operand was not recognized. Back down to dwarf.
5011 if (! session
.suppress_warnings
)
5012 session
.print_warning ("Downgrading SDT_V2 probe argument to dwarf, can't parse '"+asmarg
+"'", e
->tok
);
5013 assert (argexpr
== 0);
5014 need_debug_info
= true;
5019 assert (argexpr
!= 0);
5021 if (session
.verbose
> 2)
5022 clog
<< "mapped asm operand " << asmarg
<< " to " << *argexpr
<< endl
;
5024 if (e
->components
.empty()) // We have a scalar
5027 throw semantic_error("cannot take address of sdt variable", e
->tok
);
5033 cast_op
*cast
= new cast_op
;
5034 cast
->name
= "@cast";
5036 cast
->operand
= argexpr
;
5037 cast
->components
= e
->components
;
5038 cast
->type_name
= probe_name
+ "_arg" + lex_cast(argno
);
5039 cast
->module
= process_name
;
5046 catch (const semantic_error
&er
)
5055 sdt_uprobe_var_expanding_visitor::get_register_width (string regname
)
5057 if (elf_machine
== EM_X86_64
|| elf_machine
== EM_386
) {
5058 int regno
= dwarf_regs
.find (regname
)->second
;
5060 if (regno
< 4 && regname
[2] == 'l') return 0xff;
5062 else if (regno
< 4 && regname
[2] == 'h') return 0xff00;
5064 else if (regno
< 4 && regname
[2] == 'x') return 0xffff;
5066 else if (regno
< 8 && regname
.rfind('l') != regname
.npos
) return 0xff; // sil
5068 else if (regno
< 8 && regname
.length() == 3) return 0xffff; // si
5070 else if (regno
< 16 && regname
.rfind('b') != regname
.npos
) return 0xff; // r8b
5072 else if (regno
< 16 && regname
.rfind('w') != regname
.npos
) return 0xffff; // r8w
5073 else return 0xffffffff;
5075 else return 0xffffffff;
5080 sdt_kprobe_var_expanding_visitor::visit_target_symbol (target_symbol
*e
)
5084 if (e
->name
== "$$name")
5087 throw semantic_error("cannot take address of sdt context variable", e
->tok
);
5089 literal_string
*myname
= new literal_string (probe_name
);
5090 myname
->tok
= e
->tok
;
5094 if (e
->name
== "$$provider")
5097 throw semantic_error("cannot take address of sdt context variable", e
->tok
);
5099 literal_string
*myname
= new literal_string (provider_name
);
5100 myname
->tok
= e
->tok
;
5108 if (startswith(e
->name
, "$arg"))
5109 argno
= lex_cast
<int>(e
->name
.substr(4));
5111 catch (const runtime_error
& f
) // non-integral $arg suffix: e.g. $argKKKSDF
5115 throw semantic_error("invalid variable, must be of the form $argN", e
->tok
);
5116 if (argno
< 1 || argno
> arg_count
)
5117 throw semantic_error("invalid argument number", e
->tok
);
5119 bool lvalue
= is_active_lvalue(e
);
5120 functioncall
*fc
= new functioncall
;
5122 // First two args are hidden: 1. pointer to probe name 2. task id
5125 fc
->function
= "long_arg";
5128 // skip the hidden args
5129 literal_number
* num
= new literal_number(argno
+ 2);
5131 fc
->args
.push_back(num
);
5135 // args are passed in arg3 as members of a struct
5136 fc
->function
= "user_long";
5138 binary_expression
*be
= new binary_expression
;
5140 functioncall
*get_arg1
= new functioncall
;
5141 get_arg1
->function
= "pointer_arg";
5142 get_arg1
->tok
= e
->tok
;
5143 // arg3 is the pointer to a struct of arguments
5144 literal_number
* num
= new literal_number(3);
5146 get_arg1
->args
.push_back(num
);
5148 be
->left
= get_arg1
;
5150 // offset in struct to the desired arg
5151 literal_number
* inc
= new literal_number((argno
- 1) * 8);
5154 fc
->args
.push_back(be
);
5157 *(target_symbol_setter_functioncalls
.top()) = fc
;
5159 if (e
->components
.empty()) // We have a scalar
5162 throw semantic_error("cannot take address of sdt variable", e
->tok
);
5167 cast_op
*cast
= new cast_op
;
5168 cast
->name
= "@cast";
5171 cast
->components
= e
->components
;
5172 cast
->type_name
= probe_name
+ "_arg" + lex_cast(argno
);
5173 cast
->module
= process_name
;
5177 catch (const semantic_error
&er
)
5185 struct sdt_query
: public base_query
5187 sdt_query(probe
* base_probe
, probe_point
* base_loc
,
5188 dwflpp
& dw
, literal_map_t
const & params
,
5189 vector
<derived_probe
*> & results
);
5191 void handle_query_module();
5194 stap_sdt_probe_type probe_type
;
5195 enum {probe_section
=0, note_section
=1} probe_loc
;
5197 probe_point
* base_loc
;
5198 literal_map_t
const & params
;
5199 vector
<derived_probe
*> & results
;
5203 set
<string
> probes_handled
;
5206 size_t probe_scn_offset
;
5207 size_t probe_scn_addr
;
5213 string provider_name
;
5214 Dwarf_Addr semaphore
;
5216 bool init_probe_scn();
5217 bool get_next_probe();
5218 void iterate_over_probe_entries();
5219 void handle_probe_entry();
5221 static void setup_note_probe_entry_callback (void *object
, int type
, const char *data
, size_t len
);
5222 void setup_note_probe_entry (int type
, const char *data
, size_t len
);
5224 void convert_probe(probe
*base
);
5225 void record_semaphore(vector
<derived_probe
*> & results
, unsigned start
);
5226 probe
* convert_location();
5227 bool have_uprobe() {return probe_type
== uprobe1_type
|| probe_type
== uprobe2_type
|| probe_type
== uprobe3_type
;}
5228 bool have_kprobe() {return probe_type
== kprobe1_type
|| probe_type
== kprobe2_type
;}
5229 bool have_debuginfo_uprobe(bool need_debug_info
)
5230 {return probe_type
== uprobe1_type
5231 || ((probe_type
== uprobe2_type
|| probe_type
== uprobe3_type
)
5232 && need_debug_info
);}
5233 bool have_debuginfoless_uprobe() {return probe_type
== uprobe2_type
|| probe_type
== uprobe3_type
;}
5237 sdt_query::sdt_query(probe
* base_probe
, probe_point
* base_loc
,
5238 dwflpp
& dw
, literal_map_t
const & params
,
5239 vector
<derived_probe
*> & results
):
5240 base_query(dw
, params
), base_probe(base_probe
),
5241 base_loc(base_loc
), params(params
), results(results
)
5243 assert(get_string_param(params
, TOK_MARK
, pp_mark
));
5244 get_string_param(params
, TOK_PROVIDER
, pp_provider
); // pp_provider == "" -> unspecified
5246 // PR10245: permit usage of dtrace-y "-" separator in marker name;
5247 // map it to double-underscores.
5249 while (1) // there may be more than one
5251 size_t i
= pp_mark
.find("-", pos
);
5252 if (i
== string::npos
) break;
5253 pp_mark
.replace (i
, 1, "__");
5254 pos
= i
+1; // resume searching after the inserted __
5257 // XXX: same for pp_provider?
5262 sdt_query::handle_probe_entry()
5265 && !probes_handled
.insert(probe_name
).second
)
5268 if (sess
.verbose
> 3)
5270 clog
<< "matched probe_name " << probe_name
<< " probe_type ";
5274 clog
<< "uprobe1 at 0x" << hex
<< pc
<< dec
<< endl
;
5277 clog
<< "uprobe2 at 0x" << hex
<< pc
<< dec
<< endl
;
5280 clog
<< "uprobe3 at 0x" << hex
<< pc
<< dec
<< endl
;
5283 clog
<< "kprobe1" << endl
;
5286 clog
<< "kprobe2" << endl
;
5291 // Extend the derivation chain
5292 probe
*new_base
= convert_location();
5293 probe_point
*new_location
= new_base
->locations
[0];
5295 bool kprobe_found
= false;
5296 bool need_debug_info
= false;
5299 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (dw
.mod_info
->mod
, &bias
))
5300 ?: dwfl_module_getelf (dw
.mod_info
->mod
, &bias
));
5304 convert_probe(new_base
);
5305 kprobe_found
= true;
5306 // Expand the local variables in the probe body
5307 sdt_kprobe_var_expanding_visitor
svv (module_val
,
5312 svv
.replace (new_base
->body
);
5316 /* Figure out the architecture of this particular ELF file.
5317 The dwarfless register-name mappings depend on it. */
5319 GElf_Ehdr
* em
= gelf_getehdr (elf
, &ehdr_mem
);
5320 if (em
== 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
5321 int elf_machine
= em
->e_machine
;
5322 sdt_uprobe_var_expanding_visitor
svv (sess
, elf_machine
,
5329 svv
.replace (new_base
->body
);
5330 need_debug_info
= svv
.need_debug_info
;
5333 unsigned i
= results
.size();
5336 derive_probes(sess
, new_base
, results
);
5340 // XXX: why not derive_probes() in the uprobes case too?
5341 literal_map_t params
;
5342 for (unsigned i
= 0; i
< new_location
->components
.size(); ++i
)
5344 probe_point::component
*c
= new_location
->components
[i
];
5345 params
[c
->functor
] = c
->arg
;
5348 dwarf_query
q(new_base
, new_location
, dw
, params
, results
, "", "");
5349 q
.has_mark
= true; // enables mid-statement probing
5351 // V2 probes need dwarf info in case of a variable reference
5352 if (have_debuginfo_uprobe(need_debug_info
))
5353 dw
.iterate_over_modules(&query_module
, &q
);
5354 else if (have_debuginfoless_uprobe())
5357 Dwarf_Addr reloc_addr
= q
.statement_num_val
+ bias
;
5358 if (dwfl_module_relocations (q
.dw
.mod_info
->mod
) > 0)
5360 dwfl_module_relocate_address (q
.dw
.mod_info
->mod
, &reloc_addr
);
5361 section
= ".dynamic";
5364 section
= ".absolute";
5366 uprobe_derived_probe
* p
=
5367 new uprobe_derived_probe ("", "", 0, q
.module_val
, section
,
5368 q
.statement_num_val
, reloc_addr
, q
, 0);
5369 p
->saveargs (arg_count
);
5370 results
.push_back (p
);
5373 sess
.unwindsym_modules
.insert (dw
.module_name
);
5374 record_semaphore(results
, i
);
5379 sdt_query::handle_query_module()
5381 if (!init_probe_scn())
5384 if (sess
.verbose
> 3)
5385 clog
<< "TOK_MARK: " << pp_mark
<< " TOK_PROVIDER: " << pp_provider
<< endl
;
5387 if (probe_loc
== note_section
)
5390 GElf_Shdr
*shdr
= dw
.get_section (".stapsdt.base", &shdr_mem
);
5393 base
= shdr
->sh_addr
;
5396 dw
.iterate_over_notes ((void*) this, &sdt_query::setup_note_probe_entry_callback
);
5399 iterate_over_probe_entries ();
5404 sdt_query::init_probe_scn()
5409 GElf_Shdr
*shdr
= dw
.get_section (".note.stapsdt", &shdr_mem
);
5412 probe_loc
= note_section
;
5416 shdr
= dw
.get_section (".probes", &shdr_mem
, &elf
);
5419 pdata
= elf_getdata_rawchunk (elf
, shdr
->sh_offset
, shdr
->sh_size
, ELF_T_BYTE
);
5420 probe_scn_offset
= 0;
5421 probe_scn_addr
= shdr
->sh_addr
;
5422 assert (pdata
!= NULL
);
5423 if (sess
.verbose
> 4)
5424 clog
<< "got .probes elf scn_addr@0x" << probe_scn_addr
<< dec
5425 << ", size: " << pdata
->d_size
<< endl
;
5426 probe_loc
= probe_section
;
5434 sdt_query::setup_note_probe_entry_callback (void *object
, int type
, const char *data
, size_t len
)
5436 sdt_query
*me
= (sdt_query
*)object
;
5437 me
->setup_note_probe_entry (type
, data
, len
);
5442 sdt_query::setup_note_probe_entry (int type
, const char *data
, size_t len
)
5444 // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME
5445 // && !memcmp (data->d_buf + name_off,
5446 // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME))
5448 // probes are in the .note.stapsdt section
5449 #define _SDT_NOTE_TYPE 3
5450 if (type
!= _SDT_NOTE_TYPE
)
5459 Elf
* elf
= (dwfl_module_getelf (dw
.mod_info
->mod
, &bias
));
5462 &buf
, ELF_T_ADDR
, EV_CURRENT
,
5463 gelf_fsize (elf
, ELF_T_ADDR
, 3, EV_CURRENT
), 0, 0
5465 assert (dst
.d_size
<= sizeof buf
);
5467 if (len
< dst
.d_size
+ 3)
5472 (void *) data
, ELF_T_ADDR
, EV_CURRENT
,
5476 if (gelf_xlatetom (elf
, &dst
, &src
,
5477 elf_getident (elf
, NULL
)[EI_DATA
]) == NULL
)
5478 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
5480 probe_type
= uprobe3_type
;
5481 const char * provider
= data
+ dst
.d_size
;
5482 provider_name
= provider
;
5483 const char *name
= (const char*)memchr (provider
, '\0', data
+ len
- provider
);
5484 probe_name
= ++name
;
5486 // Did we find a matching probe?
5487 if (! (dw
.function_name_matches_pattern (probe_name
, pp_mark
)
5488 && ((pp_provider
== "")
5489 || dw
.function_name_matches_pattern (provider_name
, pp_provider
))))
5492 const char *args
= (const char*)memchr (name
, '\0', data
+ len
- name
);
5493 if (args
++ == NULL
||
5494 memchr (args
, '\0', data
+ len
- name
) != data
+ len
- 1)
5500 for (unsigned i
= 0; i
< arg_string
.length(); i
++)
5501 if (arg_string
[i
] == ' ')
5503 if (arg_string
.length() != 0)
5507 if (gelf_getclass (elf
) == ELFCLASS32
)
5510 base_ref
= buf
.a32
[1];
5511 semaphore
= buf
.a32
[2];
5516 base_ref
= buf
.a64
[1];
5517 semaphore
= buf
.a64
[2];
5520 semaphore
+= base
- base_ref
;
5521 pc
+= base
- base_ref
;
5523 if (sess
.verbose
> 4)
5524 clog
<< "saw .note.stapsdt " << probe_name
<< (provider_name
!= "" ? " (provider "+provider_name
+") " : "")
5525 << "@0x" << hex
<< pc
<< dec
<< endl
;
5527 handle_probe_entry();
5532 sdt_query::iterate_over_probe_entries()
5534 // probes are in the .probe section
5535 while (probe_scn_offset
< pdata
->d_size
)
5537 stap_sdt_probe_entry_v1
*pbe_v1
= (stap_sdt_probe_entry_v1
*) ((char*)pdata
->d_buf
+ probe_scn_offset
);
5538 stap_sdt_probe_entry_v2
*pbe_v2
= (stap_sdt_probe_entry_v2
*) ((char*)pdata
->d_buf
+ probe_scn_offset
);
5539 probe_type
= (stap_sdt_probe_type
)(pbe_v1
->type_a
);
5540 if (! have_uprobe() && ! have_kprobe())
5542 // Unless this is a mangled .probes section, this happens
5543 // because the name of the probe comes first, followed by
5545 if (sess
.verbose
> 5)
5546 clog
<< "got unknown probe_type: 0x" << hex
<< probe_type
5548 probe_scn_offset
+= sizeof(__uint32_t
);
5551 if ((long)pbe_v1
% sizeof(__uint64_t
)) // we have stap_sdt_probe_entry_v1.type_b
5553 pbe_v1
= (stap_sdt_probe_entry_v1
*)((char*)pbe_v1
- sizeof(__uint32_t
));
5554 if (pbe_v1
->type_b
!= uprobe1_type
&& pbe_v1
->type_b
!= kprobe1_type
)
5558 if (probe_type
== uprobe1_type
|| probe_type
== kprobe1_type
)
5560 if (pbe_v1
->name
== 0) // No name possibly means we have a .so with a relocation
5563 probe_name
= (char*)((char*)pdata
->d_buf
+ pbe_v1
->name
- (char*)probe_scn_addr
);
5564 provider_name
= ""; // unknown
5565 if (probe_type
== uprobe1_type
)
5570 else if (probe_type
== kprobe1_type
)
5571 arg_count
= pbe_v1
->arg
;
5572 probe_scn_offset
+= sizeof (stap_sdt_probe_entry_v1
);
5574 else if (probe_type
== uprobe2_type
|| probe_type
== kprobe2_type
)
5576 if (pbe_v2
->name
== 0) // No name possibly means we have a .so with a relocation
5578 semaphore
= pbe_v2
->semaphore
;
5579 probe_name
= (char*)((char*)pdata
->d_buf
+ pbe_v2
->name
- (char*)probe_scn_addr
);
5580 provider_name
= (char*)((char*)pdata
->d_buf
+ pbe_v2
->provider
- (char*)probe_scn_addr
);
5581 arg_count
= pbe_v2
->arg_count
;
5583 if (pbe_v2
->arg_string
)
5584 arg_string
= (char*)((char*)pdata
->d_buf
+ pbe_v2
->arg_string
- (char*)probe_scn_addr
);
5585 // skip over pbe_v2, probe_name text and provider text
5586 probe_scn_offset
= ((long)(pbe_v2
->name
) - (long)(probe_scn_addr
)) + probe_name
.length();
5587 probe_scn_offset
+= sizeof (__uint32_t
) - probe_scn_offset
% sizeof (__uint32_t
);
5589 if (sess
.verbose
> 4)
5590 clog
<< "saw .probes " << probe_name
<< (provider_name
!= "" ? " (provider "+provider_name
+") " : "")
5591 << "@0x" << hex
<< pc
<< dec
<< endl
;
5593 if (dw
.function_name_matches_pattern (probe_name
, pp_mark
)
5594 && ((pp_provider
== "") || dw
.function_name_matches_pattern (provider_name
, pp_provider
)))
5595 handle_probe_entry ();
5601 sdt_query::record_semaphore (vector
<derived_probe
*> & results
, unsigned start
)
5603 for (unsigned i
=0; i
<2; i
++) {
5604 // prefer with-provider symbol; look without provider prefix for backward compatibility only
5605 string semaphore
= (i
==0 ? (provider_name
+"_") : "") + probe_name
+ "_semaphore";
5606 // XXX: multiple addresses?
5607 if (sess
.verbose
> 2)
5608 clog
<< "looking for semaphore symbol " << semaphore
;
5611 if (this->semaphore
)
5612 addr
= this->semaphore
;
5614 addr
= lookup_symbol_address(dw
.module
, semaphore
.c_str());
5617 if (probe_type
!= uprobe3_type
5618 && dwfl_module_relocations (dw
.module
) > 0)
5619 dwfl_module_relocate_address (dw
.module
, &addr
);
5620 // XXX: relocation basis?
5621 for (unsigned i
= start
; i
< results
.size(); ++i
)
5622 results
[i
]->sdt_semaphore_addr
= addr
;
5623 if (sess
.verbose
> 2)
5624 clog
<< ", found at 0x" << hex
<< addr
<< dec
<< endl
;
5628 if (sess
.verbose
> 2)
5629 clog
<< ", not found" << endl
;
5635 sdt_query::convert_probe (probe
*base
)
5637 block
*b
= new block
;
5638 b
->tok
= base
->body
->tok
;
5640 // XXX: Does this also need to happen for i386 under x86_64 stap?
5641 if (sess
.architecture
== "i386" && have_kprobe())
5643 functioncall
*rp
= new functioncall
;
5644 rp
->function
= "regparm";
5646 literal_number
* littid
= new literal_number(0);
5647 littid
->tok
= b
->tok
;
5648 rp
->args
.push_back(littid
);
5649 expr_statement
* es
= new expr_statement
;
5652 b
->statements
.push_back(es
);
5657 // Generate: if (arg2 != kprobe2_type) next;
5658 if_statement
*istid
= new if_statement
;
5659 istid
->thenblock
= new next_statement
;
5660 istid
->elseblock
= NULL
;
5661 istid
->tok
= b
->tok
;
5662 istid
->thenblock
->tok
= b
->tok
;
5663 comparison
*betid
= new comparison
;
5665 betid
->tok
= b
->tok
;
5667 functioncall
*arg2
= new functioncall
;
5668 arg2
->function
= "ulong_arg";
5670 literal_number
* num
= new literal_number(2);
5672 arg2
->args
.push_back(num
);
5675 literal_number
* littid
= new literal_number(probe_type
);
5676 littid
->tok
= b
->tok
;
5677 betid
->right
= littid
;
5678 istid
->condition
= betid
;
5679 b
->statements
.push_back(istid
);
5682 // Generate: if (arg1 != mark("label")) next;
5683 functioncall
*fc
= new functioncall
;
5684 fc
->function
= "ulong_arg";
5686 literal_number
* num
= new literal_number(1);
5688 fc
->args
.push_back(num
);
5690 functioncall
*fcus
= new functioncall
;
5691 fcus
->function
= "user_string";
5692 fcus
->type
= pe_string
;
5694 fcus
->args
.push_back(fc
);
5696 if_statement
*is
= new if_statement
;
5697 is
->thenblock
= new next_statement
;
5698 is
->elseblock
= NULL
;
5700 is
->thenblock
->tok
= b
->tok
;
5701 comparison
*be
= new comparison
;
5705 be
->right
= new literal_string(probe_name
);
5706 be
->right
->tok
= b
->tok
;
5708 b
->statements
.push_back(is
);
5710 // Now replace the body
5711 b
->statements
.push_back(base
->body
);
5717 sdt_query::convert_location ()
5719 probe_point
* specific_loc
= new probe_point(*base_loc
);
5720 vector
<probe_point::component
*> derived_comps
;
5722 vector
<probe_point::component
*>::iterator it
;
5723 for (it
= specific_loc
->components
.begin();
5724 it
!= specific_loc
->components
.end(); ++it
)
5725 if ((*it
)->functor
== TOK_PROCESS
)
5728 // start the kernel probe_point
5729 derived_comps
.push_back(new probe_point::component(TOK_KERNEL
));
5731 // copy the process name
5732 derived_comps
.push_back(*it
);
5734 else if ((*it
)->functor
== TOK_LIBRARY
)
5737 // copy the library name for process probes
5738 derived_comps
.push_back(*it
);
5740 else if ((*it
)->functor
== TOK_PROVIDER
)
5742 // replace the possibly wildcarded arg with the specific provider name
5743 *it
= new probe_point::component(TOK_PROVIDER
,
5744 new literal_string(provider_name
));
5746 else if ((*it
)->functor
== TOK_MARK
)
5748 // replace the possibly wildcarded arg with the specific marker name
5749 *it
= new probe_point::component(TOK_MARK
,
5750 new literal_string(probe_name
));
5752 if (sess
.verbose
> 3)
5756 clog
<< "probe_type == uprobe1, use statement addr: 0x"
5757 << hex
<< pc
<< dec
<< endl
;
5760 clog
<< "probe_type == uprobe2, use statement addr: 0x"
5761 << hex
<< pc
<< dec
<< endl
;
5764 clog
<< "probe_type == uprobe3, use statement addr: 0x"
5765 << hex
<< pc
<< dec
<< endl
;
5768 clog
<< "probe_type == kprobe1" << endl
;
5771 clog
<< "probe_type == kprobe2" << endl
;
5774 clog
<< "probe_type == use_uprobe_no_dwarf, use label name: "
5775 << "_stapprobe1_" << pp_mark
<< endl
;
5783 // process("executable").statement(probe_arg)
5784 derived_comps
.push_back
5785 (new probe_point::component(TOK_STATEMENT
,
5786 new literal_number(pc
, true)));
5791 // kernel.function("*getegid*")
5792 derived_comps
.push_back
5793 (new probe_point::component(TOK_FUNCTION
,
5794 new literal_string("*getegid*")));
5797 default: // deprecated
5798 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
5799 derived_comps
.push_back
5800 (new probe_point::component(TOK_FUNCTION
,
5801 new literal_string("*")));
5802 derived_comps
.push_back
5803 (new probe_point::component(TOK_LABEL
,
5804 new literal_string("_stapprobe1_" + pp_mark
)));
5809 probe_point
* derived_loc
= new probe_point(*specific_loc
);
5810 derived_loc
->components
= derived_comps
;
5811 return base_probe
->create_alias(derived_loc
, specific_loc
);
5816 dwarf_builder::build(systemtap_session
& sess
,
5818 probe_point
* location
,
5819 literal_map_t
const & parameters
,
5820 vector
<derived_probe
*> & finished_results
)
5822 // NB: the kernel/user dwlfpp objects are long-lived.
5823 // XXX: but they should be per-session, as this builder object
5824 // may be reused if we try to cross-instrument multiple targets.
5829 if (has_null_param (parameters
, TOK_KERNEL
))
5831 dw
= get_kern_dw(sess
, "kernel");
5833 else if (get_param (parameters
, TOK_MODULE
, module_name
))
5835 // NB: glob patterns get expanded later, during the offline
5836 // elfutils module listing.
5837 dw
= get_kern_dw(sess
, module_name
);
5839 else if (get_param (parameters
, TOK_PROCESS
, module_name
))
5841 string library_name
;
5843 // PR6456 process("/bin/*") glob handling
5844 if (contains_glob_chars (module_name
))
5846 // Expand glob via rewriting the probe-point process("....")
5847 // parameter, asserted to be the first one.
5849 assert (location
->components
.size() > 0);
5850 assert (location
->components
[0]->functor
== TOK_PROCESS
);
5851 assert (location
->components
[0]->arg
);
5852 literal_string
* lit
= dynamic_cast<literal_string
*>(location
->components
[0]->arg
);
5855 // Evaluate glob here, and call derive_probes recursively with each match.
5857 int rc
= glob (module_name
.c_str(), 0, NULL
, & the_blob
);
5858 if (rc
) throw semantic_error ("glob " + module_name
+ " error (" + lex_cast (rc
) + ")");
5859 for (unsigned i
= 0; i
< the_blob
.gl_pathc
; ++i
)
5861 if (pending_interrupts
) return;
5863 const char* globbed
= the_blob
.gl_pathv
[i
];
5866 if (access (globbed
, X_OK
) == 0
5867 && stat (globbed
, &st
) == 0
5868 && S_ISREG (st
.st_mode
)) // see find_executable()
5870 // Need to call canonicalize here, in order to path-expand
5871 // patterns like process("stap*"). Otherwise it may go through
5872 // to the next round of expansion as ("stap"), leading to a $PATH
5873 // search that's not consistent with the glob search already done.
5875 char *cf
= canonicalize_file_name (globbed
);
5876 if (cf
) globbed
= cf
;
5878 if (sess
.verbose
> 1)
5879 clog
<< "Expanded process(\"" << module_name
<< "\") to "
5880 << "process(\"" << globbed
<< "\")" << endl
;
5881 // synthesize a new probe_point, with the glob-expanded string
5882 probe_point
*pp
= new probe_point (*location
);
5883 probe_point::component
* ppc
= new probe_point::component (TOK_PROCESS
,
5884 new literal_string (globbed
));
5885 ppc
->tok
= location
->components
[0]->tok
; // overwrite [0] slot, pattern matched above
5886 pp
->components
[0] = ppc
;
5888 probe
* new_probe
= new probe (*base
, pp
);
5890 // We override "optional = true" here, as if the
5891 // wildcarded probe point was given a "?" suffix.
5893 // This is because wildcard probes will be expected
5894 // by users to apply only to some subset of the
5895 // matching binaries, in the sense of "any", rather
5896 // than "all", sort of similarly how
5897 // module("*").function("...") patterns work.
5899 derive_probes (sess
, new_probe
, finished_results
,
5900 true /* NB: not location->optional */ );
5904 globfree (& the_blob
);
5905 return; // avoid falling through
5908 user_path
= find_executable (module_name
); // canonicalize it
5910 // if the executable starts with "#!", we look for the interpreter of the script
5912 ifstream
script_file (user_path
.c_str () );
5914 if (script_file
.good ())
5918 getline (script_file
, line
);
5920 if (line
.compare (0, 2, "#!") == 0)
5922 string path_head
= line
.substr(2);
5924 // remove white spaces at the beginning of the string
5925 size_t p2
= path_head
.find_first_not_of(" \t");
5927 if (p2
!= string::npos
)
5929 string path
= path_head
.substr(p2
);
5931 // remove white spaces at the end of the string
5932 p2
= path
.find_last_not_of(" \t\n");
5933 if (string::npos
!= p2
)
5936 // handle "#!/usr/bin/env" redirect
5938 if (path
.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
5940 offset
= sizeof("/bin/env")-1;
5942 else if (path
.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
5944 offset
= sizeof("/usr/bin/env")-1;
5949 size_t p3
= path
.find_first_not_of(" \t", offset
);
5951 if (p3
!= string::npos
)
5953 string env_path
= path
.substr(p3
);
5954 user_path
= find_executable (env_path
);
5959 user_path
= find_executable (path
);
5964 if (access (user_path
.c_str(), X_OK
) == 0
5965 && stat (user_path
.c_str(), &st
) == 0
5966 && S_ISREG (st
.st_mode
)) // see find_executable()
5968 if (sess
.verbose
> 1)
5969 clog
<< "Expanded process(\"" << module_name
<< "\") to "
5970 << "process(\"" << user_path
<< "\")" << endl
;
5972 assert (location
->components
.size() > 0);
5973 assert (location
->components
[0]->functor
== TOK_PROCESS
);
5974 assert (location
->components
[0]->arg
);
5975 literal_string
* lit
= dynamic_cast<literal_string
*>(location
->components
[0]->arg
);
5978 // synthesize a new probe_point, with the expanded string
5979 probe_point
*pp
= new probe_point (*location
);
5980 probe_point::component
* ppc
= new probe_point::component (TOK_PROCESS
,
5981 new literal_string (user_path
.c_str()));
5982 ppc
->tok
= location
->components
[0]->tok
; // overwrite [0] slot, pattern matched above
5983 pp
->components
[0] = ppc
;
5985 probe
* new_probe
= new probe (*base
, pp
);
5987 derive_probes (sess
, new_probe
, finished_results
);
5989 script_file
.close();
5995 script_file
.close();
5998 if (get_param (parameters
, TOK_LIBRARY
, library_name
))
6000 module_name
= find_executable (library_name
, "LD_LIBRARY_PATH");
6001 user_lib
= module_name
;
6004 module_name
= user_path
; // canonicalize it
6006 if (sess
.kernel_config
["CONFIG_UTRACE"] != string("y"))
6007 throw semantic_error ("process probes not available without kernel CONFIG_UTRACE");
6009 // user-space target; we use one dwflpp instance per module name
6010 // (= program or shared library)
6011 dw
= get_user_dw(sess
, module_name
);
6014 if (sess
.verbose
> 3)
6015 clog
<< "dwarf_builder::build for " << module_name
<< endl
;
6017 string dummy_mark_name
; // NB: PR10245: dummy value, need not substitute - => __
6018 if (get_param(parameters
, TOK_MARK
, dummy_mark_name
))
6020 sdt_query
sdtq(base
, location
, *dw
, parameters
, finished_results
);
6021 dw
->iterate_over_modules(&query_module
, &sdtq
);
6025 unsigned results_pre
= finished_results
.size();
6026 dwarf_query
q(base
, location
, *dw
, parameters
, finished_results
, user_path
, user_lib
);
6028 // XXX: kernel.statement.absolute is a special case that requires no
6029 // dwfl processing. This code should be in a separate builder.
6030 if (q
.has_kernel
&& q
.has_absolute
)
6032 // assert guru mode for absolute probes
6033 if (! q
.base_probe
->privileged
)
6035 throw semantic_error ("absolute statement probe in unprivileged script",
6039 // For kernel.statement(NUM).absolute probe points, we bypass
6040 // all the debuginfo stuff: We just wire up a
6041 // dwarf_derived_probe right here and now.
6042 dwarf_derived_probe
* p
=
6043 new dwarf_derived_probe ("", "", 0, "kernel", "",
6044 q
.statement_num_val
, q
.statement_num_val
,
6046 finished_results
.push_back (p
);
6047 sess
.unwindsym_modules
.insert ("kernel");
6051 dw
->iterate_over_modules(&query_module
, &q
);
6054 // PR11553 special processing: .return probes requested, but
6055 // some inlined function instances matched.
6056 unsigned i_n_r
= q
.inlined_non_returnable
.size();
6057 unsigned results_post
= finished_results
.size();
6060 if ((results_pre
== results_post
) && (! sess
.suppress_warnings
)) // no matches; issue warning
6063 for (set
<string
>::iterator it
= q
.inlined_non_returnable
.begin();
6064 it
!= q
.inlined_non_returnable
.end();
6067 quicklist
+= " " + (*it
);
6068 if (quicklist
.size() > 80) // heuristic, don't make an overlong report line
6070 quicklist
+= " ...";
6075 sess
.print_warning ("cannot probe .return of " + lex_cast(i_n_r
) + " inlined function(s):" + quicklist
);
6076 // There will be also a "no matches" semantic error generated.
6078 if (sess
.verbose
> 1)
6079 clog
<< "skipped .return probe of " + lex_cast(i_n_r
) + " inlined function(s)" << endl
;
6080 if ((sess
.verbose
> 3) || (sess
.verbose
> 2 && results_pre
== results_post
)) // issue details with high verbosity
6082 for (set
<string
>::iterator it
= q
.inlined_non_returnable
.begin();
6083 it
!= q
.inlined_non_returnable
.end();
6085 clog
<< (*it
) << " ";
6091 symbol_table::~symbol_table()
6093 delete_map(map_by_addr
);
6097 symbol_table::add_symbol(const char *name
, bool weak
, bool descriptor
,
6098 Dwarf_Addr addr
, Dwarf_Addr
*high_addr
)
6101 // Map ".sys_foo" to "sys_foo".
6105 func_info
*fi
= new func_info();
6109 fi
->descriptor
= descriptor
;
6110 map_by_name
[fi
->name
] = fi
;
6111 // TODO: Use a multimap in case there are multiple static
6112 // functions with the same name?
6113 map_by_addr
.insert(make_pair(addr
, fi
));
6117 symbol_table::read_symbols(FILE *f
, const string
& path
)
6119 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
6124 unsigned long long addr
;
6125 Dwarf_Addr high_addr
= 0;
6128 // %as (non-POSIX) mallocs space for the string and stores its address.
6129 while ((ret
= fscanf(f
, "%llx %c %as [%as", &addr
, &type
, &name
, &mod
)) > 0)
6131 auto_free
free_name(name
);
6132 auto_free
free_mod(mod
);
6136 cerr
<< "Symbol table error: Line "
6138 << " of symbol list from "
6140 << " is not in correct format: address type name [module]";
6141 // Caller should delete symbol_table object.
6146 // Modules are loaded above the kernel, so if we're getting
6147 // modules, we're done.
6150 if (type
== 'T' || type
== 't' || type
== 'W')
6151 add_symbol(name
, (type
== 'W'), false, (Dwarf_Addr
) addr
, &high_addr
);
6154 if (map_by_addr
.size() < 1)
6156 cerr
<< "Symbol table error: "
6157 << path
<< " contains no function symbols." << endl
;
6160 return info_present
;
6163 // NB: This currently unused. We use get_from_elf() instead because
6164 // that gives us raw addresses -- which we need for modules -- whereas
6165 // nm provides the address relative to the beginning of the section.
6167 symbol_table::read_from_elf_file(const string
&path
,
6168 const systemtap_session
&sess
)
6171 string cmd
= string("/usr/bin/nm -n --defined-only ") + path
;
6172 f
= popen(cmd
.c_str(), "r");
6175 // nm failures are detected by pclose, not popen.
6176 cerr
<< "Internal error reading symbol table from "
6177 << path
<< " -- " << strerror (errno
);
6180 enum info_status status
= read_symbols(f
, path
);
6183 if (status
== info_present
&& ! sess
.suppress_warnings
)
6184 cerr
<< "Warning: nm cannot read symbol table from " << path
;
6191 symbol_table::read_from_text_file(const string
& path
,
6192 const systemtap_session
&sess
)
6194 FILE *f
= fopen(path
.c_str(), "r");
6197 if (! sess
.suppress_warnings
)
6198 cerr
<< "Warning: cannot read symbol table from "
6199 << path
<< " -- " << strerror (errno
);
6202 enum info_status status
= read_symbols(f
, path
);
6208 symbol_table::prepare_section_rejection(Dwfl_Module
*mod
)
6212 * The .opd section contains function descriptors that can look
6213 * just like function entry points. For example, there's a function
6214 * descriptor called "do_exit" that links to the entry point ".do_exit".
6215 * Reject all symbols in .opd.
6217 opd_section
= SHN_UNDEF
;
6219 Elf
* elf
= (dwarf_getelf (dwfl_module_getdwarf (mod
, &bias
))
6220 ?: dwfl_module_getelf (mod
, &bias
));
6226 if (elf_getshdrstrndx (elf
, &shstrndx
) != 0)
6228 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
)
6231 GElf_Shdr
*shdr
= gelf_getshdr(scn
, &shdr_mem
);
6234 const char *name
= elf_strptr(elf
, shstrndx
, shdr
->sh_name
);
6235 if (!strcmp(name
, ".opd"))
6237 opd_section
= elf_ndxscn(scn
);
6245 symbol_table::reject_section(GElf_Word section
)
6247 if (section
== SHN_UNDEF
)
6250 if (section
== opd_section
)
6257 symbol_table::get_from_elf()
6259 Dwarf_Addr high_addr
= 0;
6260 Dwfl_Module
*mod
= mod_info
->mod
;
6261 int syments
= dwfl_module_getsymtab(mod
);
6263 prepare_section_rejection(mod
);
6264 for (int i
= 1; i
< syments
; ++i
)
6268 const char *name
= dwfl_module_getsym(mod
, i
, &sym
, §ion
);
6269 if (name
&& GELF_ST_TYPE(sym
.st_info
) == STT_FUNC
)
6270 add_symbol(name
, (GELF_ST_BIND(sym
.st_info
) == STB_WEAK
),
6271 reject_section(section
), sym
.st_value
, &high_addr
);
6273 return info_present
;
6277 symbol_table::get_func_containing_address(Dwarf_Addr addr
)
6279 iterator_t iter
= map_by_addr
.upper_bound(addr
);
6280 if (iter
== map_by_addr
.begin())
6283 return (--iter
)->second
;
6287 symbol_table::lookup_symbol(const string
& name
)
6289 map
<string
, func_info
*>::iterator i
= map_by_name
.find(name
);
6290 if (i
== map_by_name
.end())
6296 symbol_table::lookup_symbol_address(const string
& name
)
6298 func_info
*fi
= lookup_symbol(name
);
6304 // This is the kernel symbol table. The kernel macro cond_syscall creates
6305 // a weak symbol for each system call and maps it to sys_ni_syscall.
6306 // For system calls not implemented elsewhere, this weak symbol shows up
6307 // in the kernel symbol table. Following the precedent of dwarfful stap,
6308 // we refuse to consider such symbols. Here we delete them from our
6310 // TODO: Consider generalizing this and/or making it part of blacklist
6313 symbol_table::purge_syscall_stubs()
6315 Dwarf_Addr stub_addr
= lookup_symbol_address("sys_ni_syscall");
6318 range_t purge_range
= map_by_addr
.equal_range(stub_addr
);
6319 for (iterator_t iter
= purge_range
.first
;
6320 iter
!= purge_range
.second
;
6323 func_info
*fi
= iter
->second
;
6324 if (fi
->weak
&& fi
->name
!= "sys_ni_syscall")
6326 map_by_name
.erase(fi
->name
);
6327 map_by_addr
.erase(iter
++);
6336 module_info::get_symtab(dwarf_query
*q
)
6338 systemtap_session
&sess
= q
->sess
;
6340 if (symtab_status
!= info_unknown
)
6343 sym_table
= new symbol_table(this);
6344 if (!elf_path
.empty())
6346 if (name
== TOK_KERNEL
&& !sess
.kernel_symtab_path
.empty()
6347 && ! sess
.suppress_warnings
)
6348 cerr
<< "Warning: reading symbol table from "
6351 << sess
.kernel_symtab_path
6353 symtab_status
= sym_table
->get_from_elf();
6357 assert(name
== TOK_KERNEL
);
6358 if (sess
.kernel_symtab_path
.empty())
6360 symtab_status
= info_absent
;
6361 cerr
<< "Error: Cannot find vmlinux."
6362 << " Consider using --kmap instead of --kelf."
6368 sym_table
->read_from_text_file(sess
.kernel_symtab_path
, sess
);
6369 if (symtab_status
== info_present
)
6371 sess
.sym_kprobes_text_start
=
6372 sym_table
->lookup_symbol_address("__kprobes_text_start");
6373 sess
.sym_kprobes_text_end
=
6374 sym_table
->lookup_symbol_address("__kprobes_text_end");
6375 sess
.sym_stext
= sym_table
->lookup_symbol_address("_stext");
6379 if (symtab_status
== info_absent
)
6386 if (name
== TOK_KERNEL
)
6387 sym_table
->purge_syscall_stubs();
6390 // update_symtab reconciles data between the elf symbol table and the dwarf
6391 // function enumeration. It updates the symbol table entries with the dwarf
6392 // die that describes the function, which also signals to query_module_symtab
6393 // that a statement probe isn't needed. In return, it also adds aliases to the
6394 // function table for names that share the same addr/die.
6396 module_info::update_symtab(cu_function_cache_t
*funcs
)
6401 cu_function_cache_t new_funcs
;
6403 for (cu_function_cache_t::iterator func
= funcs
->begin();
6404 func
!= funcs
->end(); func
++)
6406 // optimization: inlines will never be in the symbol table
6407 if (dwarf_func_inline(&func
->second
) != 0)
6410 // XXX We may want to make additional efforts to match mangled elf names
6411 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
6412 // missing, so we may also need to try matching by address. See also the
6413 // notes about _Z in dwflpp::iterate_over_functions().
6415 func_info
*fi
= sym_table
->lookup_symbol(func
->first
);
6419 // iterate over all functions at the same address
6420 symbol_table::range_t er
= sym_table
->map_by_addr
.equal_range(fi
->addr
);
6421 for (symbol_table::iterator_t it
= er
.first
; it
!= er
.second
; ++it
)
6423 // update this function with the dwarf die
6424 it
->second
->die
= func
->second
;
6426 // if this function is a new alias, then
6427 // save it to merge into the function cache
6428 if (it
->second
!= fi
)
6429 new_funcs
.insert(make_pair(it
->second
->name
, it
->second
->die
));
6433 // add all discovered aliases back into the function cache
6434 // NB: this won't replace any names that dwarf may have already found
6435 funcs
->insert(new_funcs
.begin(), new_funcs
.end());
6438 module_info::~module_info()
6444 // ------------------------------------------------------------------------
6445 // user-space probes
6446 // ------------------------------------------------------------------------
6449 struct uprobe_derived_probe_group
: public generic_dpg
<uprobe_derived_probe
>
6452 string
make_pbm_key (uprobe_derived_probe
* p
) {
6453 return p
->module
+ "|" + p
->section
+ "|" + lex_cast(p
->pid
);
6457 void emit_module_decls (systemtap_session
& s
);
6458 void emit_module_init (systemtap_session
& s
);
6459 void emit_module_exit (systemtap_session
& s
);
6464 uprobe_derived_probe::join_group (systemtap_session
& s
)
6466 if (! s
.uprobe_derived_probes
)
6467 s
.uprobe_derived_probes
= new uprobe_derived_probe_group ();
6468 s
.uprobe_derived_probes
->enroll (this);
6469 enable_task_finder(s
);
6471 // Ask buildrun.cxx to build extra module if needed, and
6472 // signal staprun to load that module
6473 s
.need_uprobes
= true;
6478 uprobe_derived_probe::getargs(std::list
<std::string
> &arg_set
) const
6480 dwarf_derived_probe::getargs(arg_set
);
6481 arg_set
.insert(arg_set
.end(), args
.begin(), args
.end());
6486 uprobe_derived_probe::saveargs(int nargs
)
6488 for (int i
= 1; i
<= nargs
; i
++)
6489 args
.push_back("$arg" + lex_cast (i
) + ":long");
6494 uprobe_derived_probe::emit_unprivileged_assertion (translator_output
* o
)
6496 // These probes are allowed for unprivileged users, but only in the
6497 // context of processes which they own.
6498 emit_process_owner_assertion (o
);
6502 struct uprobe_builder
: public derived_probe_builder
6505 virtual void build(systemtap_session
& sess
,
6507 probe_point
* location
,
6508 literal_map_t
const & parameters
,
6509 vector
<derived_probe
*> & finished_results
)
6511 int64_t process
, address
;
6513 bool b1
= get_param (parameters
, TOK_PROCESS
, process
);
6515 bool b2
= get_param (parameters
, TOK_STATEMENT
, address
);
6517 bool rr
= has_null_param (parameters
, TOK_RETURN
);
6518 assert (b1
&& b2
); // by pattern_root construction
6520 finished_results
.push_back(new uprobe_derived_probe(base
, location
, process
, address
, rr
));
6526 uprobe_derived_probe_group::emit_module_decls (systemtap_session
& s
)
6528 if (probes
.empty()) return;
6529 s
.op
->newline() << "/* ---- user probes ---- */";
6530 // If uprobes isn't in the kernel, pull it in from the runtime.
6532 s
.op
->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
6533 s
.op
->newline() << "#include <linux/uprobes.h>";
6534 s
.op
->newline() << "#else";
6535 s
.op
->newline() << "#include \"uprobes/uprobes.h\"";
6536 s
.op
->newline() << "#endif";
6537 s
.op
->newline() << "#ifndef UPROBES_API_VERSION";
6538 s
.op
->newline() << "#define UPROBES_API_VERSION 1";
6539 s
.op
->newline() << "#endif";
6541 // We'll probably need at least this many:
6542 unsigned minuprobes
= probes
.size();
6543 // .. but we don't want so many that .bss is inflated (PR10507):
6544 unsigned uprobesize
= 64;
6545 unsigned maxuprobesmem
= 10*1024*1024; // 10 MB
6546 unsigned maxuprobes
= maxuprobesmem
/ uprobesize
;
6548 // Let's choose a value on the geometric middle. This should end up
6549 // between minuprobes and maxuprobes. It's OK if this number turns
6550 // out to be < minuprobes or > maxuprobes. At worst, we get a
6551 // run-time error of one kind (too few: missed uprobe registrations)
6552 // or another (too many: vmalloc errors at module load time).
6553 unsigned default_maxuprobes
= (unsigned)sqrt((double)minuprobes
* (double)maxuprobes
);
6555 s
.op
->newline() << "#ifndef MAXUPROBES";
6556 s
.op
->newline() << "#define MAXUPROBES " << default_maxuprobes
;
6557 s
.op
->newline() << "#endif";
6560 s
.op
->newline() << "#include \"uprobes-common.h\"";
6562 // In .bss, the shared pool of uprobe/uretprobe structs. These are
6563 // too big to embed in the initialized .data stap_uprobe_spec array.
6564 // XXX: consider a slab cache or somesuch for stap_uprobes
6565 s
.op
->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6566 s
.op
->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
6568 s
.op
->assert_0_indent();
6570 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
6571 // This means we process probes[] in two passes.
6572 map
<string
,unsigned> module_index
;
6573 unsigned module_index_ctr
= 0;
6575 // not const since embedded task_finder_target struct changes
6576 s
.op
->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
6578 for (unsigned i
=0; i
<probes
.size(); i
++)
6580 uprobe_derived_probe
*p
= probes
[i
];
6581 string pbmkey
= make_pbm_key (p
);
6582 if (module_index
.find (pbmkey
) == module_index
.end())
6584 module_index
[pbmkey
] = module_index_ctr
++;
6586 s
.op
->newline() << "{";
6587 // NB: it's essential that make_pbm_key() use all of and
6588 // only the same fields as we're about to emit.
6589 s
.op
->line() << " .finder={";
6591 s
.op
->line() << " .pid=" << p
->pid
<< ",";
6593 if (p
->section
== "") // .statement(addr).absolute
6594 s
.op
->line() << " .callback=&stap_uprobe_process_found,";
6595 else if (p
->section
== ".absolute") // proxy for ET_EXEC -> exec()'d program
6597 s
.op
->line() << " .procname=" << lex_cast_qstring(p
->module
) << ",";
6598 s
.op
->line() << " .callback=&stap_uprobe_process_found,";
6600 else if (p
->section
!= ".absolute") // ET_DYN
6602 if (p
->has_library
&& p
->sdt_semaphore_addr
!= 0)
6603 s
.op
->line() << " .procname=\"" << p
->path
<< "\", ";
6604 s
.op
->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
6605 s
.op
->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
6606 s
.op
->line() << " .callback=&stap_uprobe_process_munmap,";
6609 s
.op
->line() << " },";
6610 if (p
->module
!= "")
6611 s
.op
->line() << " .pathname=" << lex_cast_qstring(p
->module
) << ", ";
6612 s
.op
->line() << " },";
6615 ; // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
6617 s
.op
->newline(-1) << "};";
6619 s
.op
->assert_0_indent();
6621 // NB: read-only structure
6622 s
.op
->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
6624 for (unsigned i
=0; i
<probes
.size(); i
++)
6626 uprobe_derived_probe
* p
= probes
[i
];
6627 s
.op
->newline() << "{";
6628 string key
= make_pbm_key (p
);
6629 unsigned value
= module_index
[key
];
6631 s
.op
->line() << " .tfi=" << value
<< ",";
6632 s
.op
->line() << " .address=(unsigned long)0x" << hex
<< p
->addr
<< dec
<< "ULL,";
6633 s
.op
->line() << " .probe=" << common_probe_init (p
) << ",";
6635 if (p
->sdt_semaphore_addr
!= 0)
6636 s
.op
->line() << " .sdt_sem_offset=(unsigned long)0x"
6637 << hex
<< p
->sdt_semaphore_addr
<< dec
<< "ULL,";
6640 s
.op
->line() << " .return_p=1,";
6641 s
.op
->line() << " },";
6643 s
.op
->newline(-1) << "};";
6645 s
.op
->assert_0_indent();
6647 s
.op
->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
6648 s
.op
->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
6649 s
.op
->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6650 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "sups->probe");
6651 s
.op
->newline() << "if (sup->spec_index < 0 ||"
6652 << "sup->spec_index >= " << probes
.size() << ") return;"; // XXX: should not happen
6653 s
.op
->newline() << "c->regs = regs;";
6654 s
.op
->newline() << "c->ri = GET_PC_URETPROBE_NONE;";
6655 s
.op
->newline() << "c->regflags |= _STP_REGS_USER_FLAG;";
6657 // Make it look like the IP is set as it would in the actual user
6658 // task when calling real probe handler. Reset IP regs on return, so
6659 // we don't confuse uprobes. PR10458
6660 s
.op
->newline() << "{";
6662 s
.op
->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
6663 s
.op
->newline() << "SET_REG_IP(regs, inst->vaddr);";
6664 s
.op
->newline() << "(*sups->probe->ph) (c);";
6665 s
.op
->newline() << "SET_REG_IP(regs, uprobes_ip);";
6666 s
.op
->newline(-1) << "}";
6668 common_probe_entryfn_epilogue (s
.op
);
6669 s
.op
->newline(-1) << "}";
6671 s
.op
->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
6672 s
.op
->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
6673 s
.op
->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6674 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "sups->probe");
6675 s
.op
->newline() << "c->ri = inst;";
6676 s
.op
->newline() << "if (sup->spec_index < 0 ||"
6677 << "sup->spec_index >= " << probes
.size() << ") return;"; // XXX: should not happen
6678 // XXX: kretprobes saves "c->pi = inst;" too
6679 s
.op
->newline() << "c->regs = regs;";
6680 s
.op
->newline() << "c->regflags |= _STP_REGS_USER_FLAG;";
6682 // Make it look like the IP is set as it would in the actual user
6683 // task when calling real probe handler. Reset IP regs on return, so
6684 // we don't confuse uprobes. PR10458
6685 s
.op
->newline() << "{";
6687 s
.op
->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
6688 s
.op
->newline() << "SET_REG_IP(regs, inst->ret_addr);";
6689 s
.op
->newline() << "(*sups->probe->ph) (c);";
6690 s
.op
->newline() << "SET_REG_IP(regs, uprobes_ip);";
6691 s
.op
->newline(-1) << "}";
6693 common_probe_entryfn_epilogue (s
.op
);
6694 s
.op
->newline(-1) << "}";
6697 s
.op
->newline() << "#include \"uprobes-common.c\"";
6703 uprobe_derived_probe_group::emit_module_init (systemtap_session
& s
)
6705 if (probes
.empty()) return;
6707 s
.op
->newline() << "/* ---- user probes ---- */";
6709 s
.op
->newline() << "for (j=0; j<MAXUPROBES; j++) {";
6710 s
.op
->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
6711 s
.op
->newline() << "sup->spec_index = -1;"; // free slot
6712 // NB: we assume the rest of the struct (specificaly, sup->up) is
6713 // initialized to zero. This is so that we can use
6714 // sup->up->kdata = NULL for "really free!" PR 6829.
6715 s
.op
->newline(-1) << "}";
6716 s
.op
->newline() << "mutex_init (& stap_uprobes_lock);";
6718 // Set up the task_finders
6719 s
.op
->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
6720 s
.op
->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
6721 s
.op
->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
6722 s
.op
->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
6724 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
6725 // finders already registered, since mere registration does not
6726 // cause any utrace or memory allocation actions. That happens only
6727 // later, once the task finder engine starts running. So, for a
6728 // partial initialization requiring unwind, we need do nothing.
6729 s
.op
->newline() << "if (rc) break;";
6731 s
.op
->newline(-1) << "}";
6736 uprobe_derived_probe_group::emit_module_exit (systemtap_session
& s
)
6738 if (probes
.empty()) return;
6739 s
.op
->newline() << "/* ---- user probes ---- */";
6741 // NB: there is no stap_unregister_task_finder_target call;
6742 // important stuff like utrace cleanups are done by
6743 // __stp_task_finder_cleanup() via stap_stop_task_finder().
6745 // This function blocks until all callbacks are completed, so there
6746 // is supposed to be no possibility of any registration-related code starting
6747 // to run in parallel with our shutdown here. So we don't need to protect the
6748 // stap_uprobes[] array with the mutex.
6750 s
.op
->newline() << "for (j=0; j<MAXUPROBES; j++) {";
6751 s
.op
->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
6752 s
.op
->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6753 s
.op
->newline() << "if (sup->spec_index < 0) continue;"; // free slot
6755 // PR10655: decrement that ENABLED semaphore
6756 s
.op
->newline() << "if (sup->sdt_sem_address) {";
6757 s
.op
->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
6758 s
.op
->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
6759 s
.op
->newline() << "struct task_struct *tsk;";
6760 s
.op
->newline() << "rcu_read_lock();";
6762 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
6763 // that the pid is always in the global namespace, not in any
6764 // private namespace.
6765 s
.op
->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
6766 // We'd like to call find_task_by_pid_ns() here, but it isn't
6767 // exported. So, we call what it calls...
6768 s
.op
->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
6769 s
.op
->newline() << "#else";
6770 s
.op
->newline() << " tsk = find_task_by_pid (pid);";
6771 s
.op
->newline() << "#endif /* 2.6.24 */";
6773 s
.op
->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
6774 s
.op
->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
6775 s
.op
->newline(1) << "sdt_semaphore --;";
6776 s
.op
->newline() << "#ifdef DEBUG_UPROBES";
6777 s
.op
->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
6778 s
.op
->newline() << "#endif";
6779 s
.op
->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
6780 s
.op
->newline(-1) << "}";
6781 // XXX: need to analyze possibility of race condition
6782 s
.op
->newline(-1) << "}";
6783 s
.op
->newline() << "rcu_read_unlock();";
6784 s
.op
->newline(-1) << "}";
6786 s
.op
->newline() << "if (sups->return_p) {";
6787 s
.op
->newline(1) << "#ifdef DEBUG_UPROBES";
6788 s
.op
->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
6789 s
.op
->newline() << "#endif";
6790 // NB: PR6829 does not change that we still need to unregister at
6791 // *this* time -- when the script as a whole exits.
6792 s
.op
->newline() << "unregister_uretprobe (& sup->urp);";
6793 s
.op
->newline(-1) << "} else {";
6794 s
.op
->newline(1) << "#ifdef DEBUG_UPROBES";
6795 s
.op
->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
6796 s
.op
->newline() << "#endif";
6797 s
.op
->newline() << "unregister_uprobe (& sup->up);";
6798 s
.op
->newline(-1) << "}";
6800 s
.op
->newline() << "sup->spec_index = -1;";
6802 // XXX: uprobe missed counts?
6804 s
.op
->newline(-1) << "}";
6806 s
.op
->newline() << "mutex_destroy (& stap_uprobes_lock);";
6809 // ------------------------------------------------------------------------
6810 // Kprobe derived probes
6811 // ------------------------------------------------------------------------
6813 static const string
TOK_KPROBE("kprobe");
6815 struct kprobe_derived_probe
: public derived_probe
6817 kprobe_derived_probe (probe
*base
,
6818 probe_point
*location
,
6828 const string
& library
6841 void printsig (std::ostream
&o
) const;
6842 void join_group (systemtap_session
& s
);
6845 struct kprobe_derived_probe_group
: public derived_probe_group
6848 multimap
<string
,kprobe_derived_probe
*> probes_by_module
;
6849 typedef multimap
<string
,kprobe_derived_probe
*>::iterator p_b_m_iterator
;
6852 void enroll (kprobe_derived_probe
* probe
);
6853 void emit_module_decls (systemtap_session
& s
);
6854 void emit_module_init (systemtap_session
& s
);
6855 void emit_module_exit (systemtap_session
& s
);
6858 kprobe_derived_probe::kprobe_derived_probe (probe
*base
,
6859 probe_point
*location
,
6869 const string
& library
6871 derived_probe (base
, location
, true /* .components soon rewritten */ ),
6872 symbol_name (name
), addr (stmt_addr
),
6873 has_return (has_return
), has_statement (has_statement
),
6874 has_maxactive (has_maxactive
), has_path (has_path
),
6875 has_library (has_library
),
6876 maxactive_val (maxactive_val
),
6877 path (path
), library (library
)
6879 this->tok
= base
->tok
;
6880 this->access_var
= false;
6883 #define USHRT_MAX 32767
6886 // Expansion of $target variables in the probe body produces an error during
6887 // translate phase, since we're not using debuginfo
6889 vector
<probe_point::component
*> comps
;
6890 comps
.push_back (new probe_point::component(TOK_KPROBE
));
6894 comps
.push_back (new probe_point::component(TOK_STATEMENT
,
6895 new literal_number(addr
, true)));
6896 comps
.push_back (new probe_point::component(TOK_ABSOLUTE
));
6900 size_t pos
= name
.find(':');
6901 if (pos
!= string::npos
)
6903 string module
= name
.substr(0, pos
);
6904 string function
= name
.substr(pos
+ 1);
6905 comps
.push_back (new probe_point::component(TOK_MODULE
, new literal_string(module
)));
6906 comps
.push_back (new probe_point::component(TOK_FUNCTION
, new literal_string(function
)));
6909 comps
.push_back (new probe_point::component(TOK_FUNCTION
, new literal_string(name
)));
6913 comps
.push_back (new probe_point::component(TOK_RETURN
));
6915 comps
.push_back (new probe_point::component(TOK_MAXACTIVE
, new literal_number(maxactive_val
)));
6917 this->sole_location()->components
= comps
;
6920 void kprobe_derived_probe::printsig (ostream
& o
) const
6922 sole_location()->print (o
);
6923 o
<< " /* " << " name = " << symbol_name
<< "*/";
6924 printsig_nested (o
);
6927 void kprobe_derived_probe::join_group (systemtap_session
& s
)
6930 if (! s
.kprobe_derived_probes
)
6931 s
.kprobe_derived_probes
= new kprobe_derived_probe_group ();
6932 s
.kprobe_derived_probes
->enroll (this);
6936 void kprobe_derived_probe_group::enroll (kprobe_derived_probe
* p
)
6938 probes_by_module
.insert (make_pair (p
->symbol_name
, p
));
6939 // probes of same symbol should share single kprobe/kretprobe
6943 kprobe_derived_probe_group::emit_module_decls (systemtap_session
& s
)
6945 if (probes_by_module
.empty()) return;
6947 s
.op
->newline() << "/* ---- kprobe-based probes ---- */";
6949 // Warn of misconfigured kernels
6950 s
.op
->newline() << "#if ! defined(CONFIG_KPROBES)";
6951 s
.op
->newline() << "#error \"Need CONFIG_KPROBES!\"";
6952 s
.op
->newline() << "#endif";
6955 s
.op
->newline() << "#ifndef KRETACTIVE";
6956 s
.op
->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
6957 s
.op
->newline() << "#endif";
6959 // Forward declare the master entry functions
6960 s
.op
->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
6961 s
.op
->line() << " struct pt_regs *regs);";
6962 s
.op
->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
6963 s
.op
->line() << " struct pt_regs *regs);";
6965 // Emit an array of kprobe/kretprobe pointers
6966 s
.op
->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
6967 s
.op
->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module
.size() << "];";
6968 s
.op
->newline() << "#endif";
6970 // Emit the actual probe list.
6972 s
.op
->newline() << "static struct stap_dwarfless_kprobe {";
6973 s
.op
->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
6974 s
.op
->newline() << "#ifdef __ia64__";
6975 s
.op
->newline() << "struct kprobe dummy;";
6976 s
.op
->newline() << "#endif";
6977 s
.op
->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module
.size() << "];";
6980 s
.op
->newline() << "static struct stap_dwarfless_probe {";
6981 s
.op
->newline(1) << "const unsigned return_p:1;";
6982 s
.op
->newline() << "const unsigned maxactive_p:1;";
6983 s
.op
->newline() << "const unsigned optional_p:1;";
6984 s
.op
->newline() << "unsigned registered_p:1;";
6985 s
.op
->newline() << "const unsigned short maxactive_val;";
6987 // Function Names are mostly small and uniform enough to justify putting
6988 // char[MAX]'s into the array instead of relocated char*'s.
6990 size_t symbol_string_name_max
= 0;
6991 size_t symbol_string_name_tot
= 0;
6992 for (p_b_m_iterator it
= probes_by_module
.begin(); it
!= probes_by_module
.end(); it
++)
6994 kprobe_derived_probe
* p
= it
->second
;
6995 #define DOIT(var,expr) do { \
6996 size_t var##_size = (expr) + 1; \
6997 var##_max = max (var##_max, var##_size); \
6998 var##_tot += var##_size; } while (0)
6999 DOIT(symbol_string_name
, p
->symbol_name
.size());
7003 #define CALCIT(var) \
7004 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
7006 CALCIT(symbol_string
);
7009 s
.op
->newline() << "unsigned long address;";
7010 s
.op
->newline() << "struct stap_probe * const probe;";
7011 s
.op
->newline(-1) << "} stap_dwarfless_probes[] = {";
7014 for (p_b_m_iterator it
= probes_by_module
.begin(); it
!= probes_by_module
.end(); it
++)
7016 kprobe_derived_probe
* p
= it
->second
;
7017 s
.op
->newline() << "{";
7019 s
.op
->line() << " .return_p=1,";
7021 if (p
->has_maxactive
)
7023 s
.op
->line() << " .maxactive_p=1,";
7024 assert (p
->maxactive_val
>= 0 && p
->maxactive_val
<= USHRT_MAX
);
7025 s
.op
->line() << " .maxactive_val=" << p
->maxactive_val
<< ",";
7028 if (p
->locations
[0]->optional
)
7029 s
.op
->line() << " .optional_p=1,";
7031 if (p
->has_statement
)
7032 s
.op
->line() << " .address=(unsigned long)0x" << hex
<< p
->addr
<< dec
<< "ULL,";
7034 s
.op
->line() << " .symbol_string=\"" << p
->symbol_name
<< "\",";
7036 s
.op
->line() << " .probe=" << common_probe_init (p
) << ",";
7037 s
.op
->line() << " },";
7040 s
.op
->newline(-1) << "};";
7042 // Emit the kprobes callback function
7044 s
.op
->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
7045 s
.op
->line() << " struct pt_regs *regs) {";
7046 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
7047 s
.op
->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
7048 // Check that the index is plausible
7049 s
.op
->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
7050 s
.op
->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module
.size() << ")?";
7051 s
.op
->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
7052 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
7053 s
.op
->line() << "];";
7054 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "sdp->probe");
7055 s
.op
->newline() << "c->regs = regs;";
7057 // Make it look like the IP is set as it wouldn't have been replaced
7058 // by a breakpoint instruction when calling real probe handler. Reset
7059 // IP regs on return, so we don't confuse kprobes. PR10458
7060 s
.op
->newline() << "{";
7062 s
.op
->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
7063 s
.op
->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
7064 s
.op
->newline() << "(*sdp->probe->ph) (c);";
7065 s
.op
->newline() << "SET_REG_IP(regs, kprobes_ip);";
7066 s
.op
->newline(-1) << "}";
7068 common_probe_entryfn_epilogue (s
.op
);
7069 s
.op
->newline() << "return 0;";
7070 s
.op
->newline(-1) << "}";
7072 // Same for kretprobes
7074 s
.op
->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
7075 s
.op
->line() << " struct pt_regs *regs) {";
7076 s
.op
->newline(1) << "struct kretprobe *krp = inst->rp;";
7078 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
7079 s
.op
->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
7080 // Check that the index is plausible
7081 s
.op
->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
7082 s
.op
->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module
.size() << ")?";
7083 s
.op
->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
7084 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
7085 s
.op
->line() << "];";
7087 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "sdp->probe");
7088 s
.op
->newline() << "c->regs = regs;";
7089 s
.op
->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic
7091 // Make it look like the IP is set as it wouldn't have been replaced
7092 // by a breakpoint instruction when calling real probe handler. Reset
7093 // IP regs on return, so we don't confuse kprobes. PR10458
7094 s
.op
->newline() << "{";
7096 s
.op
->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
7097 s
.op
->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
7098 s
.op
->newline() << "(*sdp->probe->ph) (c);";
7099 s
.op
->newline() << "SET_REG_IP(regs, kprobes_ip);";
7100 s
.op
->newline(-1) << "}";
7102 common_probe_entryfn_epilogue (s
.op
);
7103 s
.op
->newline() << "return 0;";
7104 s
.op
->newline(-1) << "}";
7106 s
.op
->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
7107 s
.op
->newline() << "static int kprobe_resolve(void *data, const char *name,";
7108 s
.op
->newline() << " struct module *owner,";
7109 s
.op
->newline() << " unsigned long val) {";
7110 s
.op
->newline(1) << "int i;";
7111 s
.op
->newline() << "int *p = (int *) data;";
7112 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size()
7113 << " && *p > 0; i++) {";
7114 s
.op
->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
7115 s
.op
->newline() << "if (! sdp->address)";
7116 s
.op
->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
7117 s
.op
->newline(1) << "sdp->address = val;";
7118 s
.op
->newline() << "(*p)--;";
7119 s
.op
->newline(-1) << "}";
7120 s
.op
->newline(-2) << "}";
7121 s
.op
->newline() << "return (p > 0) ? 0 : -1;";
7122 s
.op
->newline(-1) << "}";
7123 s
.op
->newline() << "#endif";
7128 kprobe_derived_probe_group::emit_module_init (systemtap_session
& s
)
7130 s
.op
->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
7131 s
.op
->newline() << "{";
7132 s
.op
->newline(1) << "int p = 0;";
7133 s
.op
->newline() << "for (i = 0; i < " << probes_by_module
.size() << "; i++) {";
7134 s
.op
->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
7135 s
.op
->newline() << "if (! sdp->address)";
7136 s
.op
->newline(1) << "p++;";
7137 s
.op
->newline(-2) << "}";
7138 s
.op
->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
7139 s
.op
->newline(-1) << "}";
7140 s
.op
->newline() << "#endif";
7142 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
7143 s
.op
->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
7144 s
.op
->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
7145 s
.op
->newline() << "void *addr = (void *) sdp->address;";
7146 s
.op
->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
7148 s
.op
->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
7149 s
.op
->newline() << "if (! addr) {";
7150 s
.op
->newline(1) << "sdp->registered_p = 0;";
7151 s
.op
->newline() << "if (!sdp->optional_p)";
7152 s
.op
->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
7153 s
.op
->newline(-1) << "continue;";
7154 s
.op
->newline(-1) << "}";
7155 s
.op
->newline() << "#endif";
7157 s
.op
->newline() << "probe_point = sdp->probe->pp;"; // for error messages
7158 s
.op
->newline() << "if (sdp->return_p) {";
7159 s
.op
->newline(1) << "kp->u.krp.kp.addr = addr;";
7160 s
.op
->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
7161 s
.op
->newline() << "if (sdp->maxactive_p) {";
7162 s
.op
->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
7163 s
.op
->newline(-1) << "} else {";
7164 s
.op
->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
7165 s
.op
->newline(-1) << "}";
7166 s
.op
->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
7167 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
7168 s
.op
->newline() << "#ifdef __ia64__";
7169 s
.op
->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
7170 s
.op
->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
7171 s
.op
->newline() << "kp->dummy.pre_handler = NULL;";
7172 s
.op
->newline() << "rc = register_kprobe (& kp->dummy);";
7173 s
.op
->newline() << "if (rc == 0) {";
7174 s
.op
->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
7175 s
.op
->newline() << "if (rc != 0)";
7176 s
.op
->newline(1) << "unregister_kprobe (& kp->dummy);";
7177 s
.op
->newline(-2) << "}";
7178 s
.op
->newline() << "#else";
7179 s
.op
->newline() << "rc = register_kretprobe (& kp->u.krp);";
7180 s
.op
->newline() << "#endif";
7181 s
.op
->newline(-1) << "} else {";
7182 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
7183 s
.op
->newline(1) << "kp->u.kp.addr = addr;";
7184 s
.op
->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
7185 s
.op
->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
7186 s
.op
->newline() << "#ifdef __ia64__";
7187 s
.op
->newline() << "kp->dummy.pre_handler = NULL;";
7188 s
.op
->newline() << "kp->dummy.addr = kp->u.kp.addr;";
7189 s
.op
->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
7190 s
.op
->newline() << "rc = register_kprobe (& kp->dummy);";
7191 s
.op
->newline() << "if (rc == 0) {";
7192 s
.op
->newline(1) << "rc = register_kprobe (& kp->u.kp);";
7193 s
.op
->newline() << "if (rc != 0)";
7194 s
.op
->newline(1) << "unregister_kprobe (& kp->dummy);";
7195 s
.op
->newline(-2) << "}";
7196 s
.op
->newline() << "#else";
7197 s
.op
->newline() << "rc = register_kprobe (& kp->u.kp);";
7198 s
.op
->newline() << "#endif";
7199 s
.op
->newline(-1) << "}";
7200 s
.op
->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
7201 s
.op
->newline(1) << "sdp->registered_p = 0;";
7202 s
.op
->newline() << "if (!sdp->optional_p)";
7203 s
.op
->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
7204 s
.op
->newline(-1) << "rc = 0;"; // continue with other probes
7205 // XXX: shall we increment numskipped?
7206 s
.op
->newline(-1) << "}";
7208 s
.op
->newline() << "else sdp->registered_p = 1;";
7209 s
.op
->newline(-1) << "}"; // for loop
7213 kprobe_derived_probe_group::emit_module_exit (systemtap_session
& s
)
7215 //Unregister kprobes by batch interfaces.
7216 s
.op
->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
7217 s
.op
->newline() << "j = 0;";
7218 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
7219 s
.op
->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
7220 s
.op
->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
7221 s
.op
->newline() << "if (! sdp->registered_p) continue;";
7222 s
.op
->newline() << "if (!sdp->return_p)";
7223 s
.op
->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
7224 s
.op
->newline(-2) << "}";
7225 s
.op
->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
7226 s
.op
->newline() << "j = 0;";
7227 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
7228 s
.op
->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
7229 s
.op
->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
7230 s
.op
->newline() << "if (! sdp->registered_p) continue;";
7231 s
.op
->newline() << "if (sdp->return_p)";
7232 s
.op
->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
7233 s
.op
->newline(-2) << "}";
7234 s
.op
->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
7235 s
.op
->newline() << "#ifdef __ia64__";
7236 s
.op
->newline() << "j = 0;";
7237 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
7238 s
.op
->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
7239 s
.op
->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
7240 s
.op
->newline() << "if (! sdp->registered_p) continue;";
7241 s
.op
->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
7242 s
.op
->newline(-1) << "}";
7243 s
.op
->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
7244 s
.op
->newline() << "#endif";
7245 s
.op
->newline() << "#endif";
7247 s
.op
->newline() << "for (i=0; i<" << probes_by_module
.size() << "; i++) {";
7248 s
.op
->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
7249 s
.op
->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
7250 s
.op
->newline() << "if (! sdp->registered_p) continue;";
7251 s
.op
->newline() << "if (sdp->return_p) {";
7252 s
.op
->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
7253 s
.op
->newline(1) << "unregister_kretprobe (&kp->u.krp);";
7254 s
.op
->newline() << "#endif";
7255 s
.op
->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
7256 s
.op
->newline() << "#ifdef STP_TIMING";
7257 s
.op
->newline() << "if (kp->u.krp.nmissed)";
7258 s
.op
->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
7259 s
.op
->newline(-1) << "#endif";
7260 s
.op
->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
7261 s
.op
->newline() << "#ifdef STP_TIMING";
7262 s
.op
->newline() << "if (kp->u.krp.kp.nmissed)";
7263 s
.op
->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
7264 s
.op
->newline(-1) << "#endif";
7265 s
.op
->newline(-1) << "} else {";
7266 s
.op
->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
7267 s
.op
->newline(1) << "unregister_kprobe (&kp->u.kp);";
7268 s
.op
->newline() << "#endif";
7269 s
.op
->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
7270 s
.op
->newline() << "#ifdef STP_TIMING";
7271 s
.op
->newline() << "if (kp->u.kp.nmissed)";
7272 s
.op
->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
7273 s
.op
->newline(-1) << "#endif";
7274 s
.op
->newline(-1) << "}";
7275 s
.op
->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
7276 s
.op
->newline() << "unregister_kprobe (&kp->dummy);";
7277 s
.op
->newline() << "#endif";
7278 s
.op
->newline() << "sdp->registered_p = 0;";
7279 s
.op
->newline(-1) << "}";
7282 struct kprobe_builder
: public derived_probe_builder
7285 virtual void build(systemtap_session
& sess
,
7287 probe_point
* location
,
7288 literal_map_t
const & parameters
,
7289 vector
<derived_probe
*> & finished_results
);
7294 kprobe_builder::build(systemtap_session
& sess
,
7296 probe_point
* location
,
7297 literal_map_t
const & parameters
,
7298 vector
<derived_probe
*> & finished_results
)
7300 string function_string_val
, module_string_val
;
7301 string path
, library
;
7302 int64_t statement_num_val
= 0, maxactive_val
= 0;
7303 bool has_function_str
, has_module_str
, has_statement_num
;
7304 bool has_absolute
, has_return
, has_maxactive
;
7305 bool has_path
, has_library
;
7307 has_function_str
= get_param(parameters
, TOK_FUNCTION
, function_string_val
);
7308 has_module_str
= get_param(parameters
, TOK_MODULE
, module_string_val
);
7309 has_return
= has_null_param (parameters
, TOK_RETURN
);
7310 has_maxactive
= get_param(parameters
, TOK_MAXACTIVE
, maxactive_val
);
7311 has_statement_num
= get_param(parameters
, TOK_STATEMENT
, statement_num_val
);
7312 has_absolute
= has_null_param (parameters
, TOK_ABSOLUTE
);
7313 has_path
= get_param (parameters
, TOK_PROCESS
, path
);
7314 has_library
= get_param (parameters
, TOK_LIBRARY
, library
);
7317 path
= find_executable (path
);
7319 library
= find_executable (library
, "LD_LIBRARY_PATH");
7321 if (has_function_str
)
7324 function_string_val
= module_string_val
+ ":" + function_string_val
;
7326 finished_results
.push_back (new kprobe_derived_probe (base
,
7327 location
, function_string_val
,
7339 // assert guru mode for absolute probes
7340 if ( has_statement_num
&& has_absolute
&& !base
->privileged
)
7341 throw semantic_error ("absolute statement probe in unprivileged script", base
->tok
);
7343 finished_results
.push_back (new kprobe_derived_probe (base
,
7357 // ------------------------------------------------------------------------
7358 // Hardware breakpoint based probes.
7359 // ------------------------------------------------------------------------
7361 static const string
TOK_HWBKPT("data");
7362 static const string
TOK_HWBKPT_WRITE("write");
7363 static const string
TOK_HWBKPT_RW("rw");
7364 static const string
TOK_LENGTH("length");
7366 #define HWBKPT_READ 0
7367 #define HWBKPT_WRITE 1
7369 struct hwbkpt_derived_probe
: public derived_probe
7371 hwbkpt_derived_probe (probe
*base
,
7372 probe_point
*location
,
7376 bool has_only_read_access
,
7377 bool has_only_write_access
,
7380 Dwarf_Addr hwbkpt_addr
;
7382 unsigned int hwbkpt_access
,hwbkpt_len
;
7384 void printsig (std::ostream
&o
) const;
7385 void join_group (systemtap_session
& s
);
7388 struct hwbkpt_derived_probe_group
: public derived_probe_group
7391 vector
<hwbkpt_derived_probe
*> hwbkpt_probes
;
7394 void enroll (hwbkpt_derived_probe
* probe
, systemtap_session
& s
);
7395 void emit_module_decls (systemtap_session
& s
);
7396 void emit_module_init (systemtap_session
& s
);
7397 void emit_module_exit (systemtap_session
& s
);
7400 hwbkpt_derived_probe::hwbkpt_derived_probe (probe
*base
,
7401 probe_point
*location
,
7405 bool has_only_read_access
,
7406 bool has_only_write_access
,
7409 derived_probe (base
, location
, true /* .components soon rewritten */ ),
7411 symbol_name (symname
),
7414 this->tok
= base
->tok
;
7416 vector
<probe_point::component
*> comps
;
7417 comps
.push_back (new probe_point::component(TOK_KERNEL
));
7420 comps
.push_back (new probe_point::component (TOK_HWBKPT
,
7421 new literal_number(hwbkpt_addr
, true)));
7422 else if (symbol_name
.size())
7423 comps
.push_back (new probe_point::component (TOK_HWBKPT
, new literal_string(symbol_name
)));
7425 comps
.push_back (new probe_point::component (TOK_LENGTH
, new literal_number(hwbkpt_len
)));
7427 if (has_only_read_access
)
7428 this->hwbkpt_access
= HWBKPT_READ
;
7429 //TODO add code for comps.push_back for read, since this flag is not for x86
7433 if (has_only_write_access
)
7435 this->hwbkpt_access
= HWBKPT_WRITE
;
7436 comps
.push_back (new probe_point::component(TOK_HWBKPT_WRITE
));
7440 this->hwbkpt_access
= HWBKPT_RW
;
7441 comps
.push_back (new probe_point::component(TOK_HWBKPT_RW
));
7445 this->sole_location()->components
= comps
;
7448 void hwbkpt_derived_probe::printsig (ostream
& o
) const
7450 sole_location()->print (o
);
7451 printsig_nested (o
);
7454 void hwbkpt_derived_probe::join_group (systemtap_session
& s
)
7456 if (! s
.hwbkpt_derived_probes
)
7457 s
.hwbkpt_derived_probes
= new hwbkpt_derived_probe_group ();
7458 s
.hwbkpt_derived_probes
->enroll (this, s
);
7461 void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe
* p
, systemtap_session
& s
)
7463 hwbkpt_probes
.push_back (p
);
7465 unsigned max_hwbkpt_probes_by_arch
= 0;
7466 if (s
.architecture
== "i386" || s
.architecture
== "x86_64")
7467 max_hwbkpt_probes_by_arch
= 4;
7468 else if (s
.architecture
== "s390")
7469 max_hwbkpt_probes_by_arch
= 1;
7471 if (hwbkpt_probes
.size() >= max_hwbkpt_probes_by_arch
)
7472 if (! s
.suppress_warnings
)
7473 s
.print_warning ("Too many hardware breakpoint probes requested for " + s
.architecture
7474 + "(" + lex_cast(hwbkpt_probes
.size()) +
7475 " vs. " + lex_cast(max_hwbkpt_probes_by_arch
) + ")");
7479 hwbkpt_derived_probe_group::emit_module_decls (systemtap_session
& s
)
7481 if (hwbkpt_probes
.empty()) return;
7483 s
.op
->newline() << "/* ---- hwbkpt-based probes ---- */";
7485 s
.op
->newline() << "#include <linux/perf_event.h>";
7486 s
.op
->newline() << "#include <linux/hw_breakpoint.h>";
7489 // Forward declare the master entry functions
7490 s
.op
->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
7491 s
.op
->line() << " int nmi,";
7492 s
.op
->line() << " struct perf_sample_data *data,";
7493 s
.op
->line() << " struct pt_regs *regs);";
7495 // Emit the actual probe list.
7497 s
.op
->newline() << "static struct perf_event_attr ";
7498 s
.op
->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes
.size() << "];";
7500 s
.op
->newline() << "static struct perf_event **";
7501 s
.op
->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes
.size() << "];";
7502 s
.op
->newline() << "static struct stap_hwbkpt_probe {";
7503 s
.op
->newline() << "int registered_p:1;";
7504 // registered_p = 0 signifies a probe that failed registration
7505 // registered_p = 1 signifies a probe that got registered successfully
7507 // Symbol Names are mostly small and uniform enough
7508 // to justify putting const char*.
7509 s
.op
->newline() << "const char * const symbol;";
7511 s
.op
->newline() << "const unsigned long address;";
7512 s
.op
->newline() << "uint8_t atype;";
7513 s
.op
->newline() << "unsigned int len;";
7514 s
.op
->newline() << "struct stap_probe * const probe;";
7515 s
.op
->newline() << "} stap_hwbkpt_probes[] = {";
7518 for (unsigned int it
= 0; it
< hwbkpt_probes
.size(); it
++)
7520 hwbkpt_derived_probe
* p
= hwbkpt_probes
.at(it
);
7521 s
.op
->newline() << "{";
7522 s
.op
->line() << " .registered_p=1,";
7523 if (p
->symbol_name
.size())
7524 s
.op
->line() << " .address=(unsigned long)0x0" << "ULL,";
7526 s
.op
->line() << " .address=(unsigned long)0x" << hex
<< p
->hwbkpt_addr
<< dec
<< "ULL,";
7527 switch(p
->hwbkpt_access
){
7529 s
.op
->line() << " .atype=HW_BREAKPOINT_R ,";
7532 s
.op
->line() << " .atype=HW_BREAKPOINT_W ,";
7535 s
.op
->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
7538 s
.op
->line() << " .len=" << p
->hwbkpt_len
<< ",";
7539 s
.op
->line() << " .probe=" << common_probe_init (p
) << ",";
7540 s
.op
->line() << " .symbol=\"" << p
->symbol_name
<< "\",";
7541 s
.op
->line() << " },";
7543 s
.op
->newline(-1) << "};";
7545 // Emit the hwbkpt callback function
7547 s
.op
->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
7548 s
.op
->line() << " int nmi,";
7549 s
.op
->line() << " struct perf_sample_data *data,";
7550 s
.op
->line() << " struct pt_regs *regs) {";
7551 s
.op
->newline(1) << "unsigned int i;";
7552 s
.op
->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
7553 s
.op
->newline() << "for (i=0; i<" << hwbkpt_probes
.size() << "; i++) {";
7554 s
.op
->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
7555 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
7556 s
.op
->newline() << "if (bp->attr.bp_addr==hp->bp_addr && bp->attr.bp_type==hp->bp_type && bp->attr.bp_len==hp->bp_len) {";
7557 s
.op
->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
7558 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "sdp->probe");
7559 s
.op
->newline() << "c->regs = regs;";
7560 s
.op
->newline() << "(*sdp->probe->ph) (c);";
7561 common_probe_entryfn_epilogue (s
.op
);
7562 s
.op
->newline(-1) << "}";
7563 s
.op
->newline(-1) << "}";
7564 s
.op
->newline() << "return 0;";
7565 s
.op
->newline(-1) << "}";
7569 hwbkpt_derived_probe_group::emit_module_init (systemtap_session
& s
)
7571 s
.op
->newline() << "for (i=0; i<" << hwbkpt_probes
.size() << "; i++) {";
7572 s
.op
->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
7573 s
.op
->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
7574 s
.op
->newline() << "void *addr = (void *) sdp->address;";
7575 s
.op
->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
7576 s
.op
->newline() << "hw_breakpoint_init(hp);";
7577 s
.op
->newline() << "if (addr)";
7578 s
.op
->newline(1) << "hp->bp_addr = (unsigned long) addr;";
7579 s
.op
->newline(-1) << "else { ";
7580 s
.op
->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
7581 s
.op
->newline() << "if (!hp->bp_addr) { ";
7582 s
.op
->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
7583 s
.op
->newline() << "continue;";
7584 s
.op
->newline(-1) << "}";
7585 s
.op
->newline(-1) << "}";
7586 s
.op
->newline() << "hp->bp_type = sdp->atype;";
7588 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
7589 if (s
.architecture
== "i386" || s
.architecture
== "x86_64" )
7591 s
.op
->newline() << "switch(sdp->len) {";
7592 s
.op
->newline() << "case 1:";
7593 s
.op
->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
7594 s
.op
->newline() << "break;";
7595 s
.op
->newline(-1) << "case 2:";
7596 s
.op
->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
7597 s
.op
->newline() << "break;";
7598 s
.op
->newline(-1) << "case 3:";
7599 s
.op
->newline() << "case 4:";
7600 s
.op
->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
7601 s
.op
->newline() << "break;";
7602 s
.op
->newline(-1) << "case 5:";
7603 s
.op
->newline() << "case 6:";
7604 s
.op
->newline() << "case 7:";
7605 s
.op
->newline() << "case 8:";
7606 s
.op
->newline() << "default:"; // XXX: could instead reject
7607 s
.op
->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
7608 s
.op
->newline() << "break;";
7609 s
.op
->newline(-1) << "}";
7611 else // other architectures presumed straightforward
7612 s
.op
->newline() << "hp->bp_len = sdp->len;";
7614 s
.op
->newline() << "probe_point = sdp->probe->pp;"; // for error messages
7615 s
.op
->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
7616 s
.op
->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
7617 s
.op
->newline(1) << "int err_code = PTR_ERR(stap_hwbkpt_ret_array[i]);";
7618 s
.op
->newline(0) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, err_code, addr, hwbkpt_symbol_name);";
7619 s
.op
->newline(-1) << "}";
7620 s
.op
->newline() << " else sdp->registered_p = 1;";
7621 s
.op
->newline(-1) << "}"; // for loop
7625 hwbkpt_derived_probe_group::emit_module_exit (systemtap_session
& s
)
7627 //Unregister hwbkpt probes.
7628 s
.op
->newline() << "for (i=0; i<" << hwbkpt_probes
.size() << "; i++) {";
7629 s
.op
->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
7630 s
.op
->newline() << "if (sdp->registered_p == 0) continue;";
7631 s
.op
->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
7632 s
.op
->newline() << "sdp->registered_p = 0;";
7633 s
.op
->newline(-1) << "}";
7636 struct hwbkpt_builder
: public derived_probe_builder
7639 virtual void build(systemtap_session
& sess
,
7641 probe_point
* location
,
7642 literal_map_t
const & parameters
,
7643 vector
<derived_probe
*> & finished_results
);
7647 hwbkpt_builder::build(systemtap_session
& sess
,
7649 probe_point
* location
,
7650 literal_map_t
const & parameters
,
7651 vector
<derived_probe
*> & finished_results
)
7653 string symbol_str_val
;
7654 int64_t hwbkpt_address
, len
;
7655 bool has_addr
, has_symbol_str
, has_write
, has_rw
, has_len
;
7657 if (! (sess
.kernel_config
["CONFIG_PERF_EVENTS"] == string("y")))
7658 throw semantic_error ("CONFIG_PERF_EVENTS not available on this kernel",
7659 location
->components
[0]->tok
);
7660 if (! (sess
.kernel_config
["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
7661 throw semantic_error ("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel",
7662 location
->components
[0]->tok
);
7664 has_addr
= get_param (parameters
, TOK_HWBKPT
, hwbkpt_address
);
7665 has_symbol_str
= get_param (parameters
, TOK_HWBKPT
, symbol_str_val
);
7666 has_len
= get_param (parameters
, TOK_LENGTH
, len
);
7667 has_write
= (parameters
.find(TOK_HWBKPT_WRITE
) != parameters
.end());
7668 has_rw
= (parameters
.find(TOK_HWBKPT_RW
) != parameters
.end());
7674 finished_results
.push_back (new hwbkpt_derived_probe (base
,
7680 else // has symbol_str
7681 finished_results
.push_back (new hwbkpt_derived_probe (base
,
7684 symbol_str_val
,len
,0,
7689 // ------------------------------------------------------------------------
7690 // statically inserted kernel-tracepoint derived probes
7691 // ------------------------------------------------------------------------
7693 struct tracepoint_arg
7695 string name
, c_type
, typecast
;
7696 bool usable
, used
, isptr
;
7698 tracepoint_arg(): usable(false), used(false), isptr(false) {}
7701 struct tracepoint_derived_probe
: public derived_probe
7703 tracepoint_derived_probe (systemtap_session
& s
,
7704 dwflpp
& dw
, Dwarf_Die
& func_die
,
7705 const string
& tracepoint_name
,
7706 probe
* base_probe
, probe_point
* location
);
7708 systemtap_session
& sess
;
7709 string tracepoint_name
, header
;
7710 vector
<struct tracepoint_arg
> args
;
7712 void build_args(dwflpp
& dw
, Dwarf_Die
& func_die
);
7713 void getargs (std::list
<std::string
> &arg_set
) const;
7714 void join_group (systemtap_session
& s
);
7715 void print_dupe_stamp(ostream
& o
);
7719 struct tracepoint_derived_probe_group
: public generic_dpg
<tracepoint_derived_probe
>
7721 void emit_module_decls (systemtap_session
& s
);
7722 void emit_module_init (systemtap_session
& s
);
7723 void emit_module_exit (systemtap_session
& s
);
7727 struct tracepoint_var_expanding_visitor
: public var_expanding_visitor
7729 tracepoint_var_expanding_visitor(dwflpp
& dw
, const string
& probe_name
,
7730 vector
<struct tracepoint_arg
>& args
):
7731 dw (dw
), probe_name (probe_name
), args (args
) {}
7733 const string
& probe_name
;
7734 vector
<struct tracepoint_arg
>& args
;
7736 void visit_target_symbol (target_symbol
* e
);
7737 void visit_target_symbol_arg (target_symbol
* e
);
7738 void visit_target_symbol_context (target_symbol
* e
);
7743 tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol
* e
)
7745 string argname
= e
->name
.substr(1);
7747 // search for a tracepoint parameter matching this name
7748 tracepoint_arg
*arg
= NULL
;
7749 for (unsigned i
= 0; i
< args
.size(); ++i
)
7750 if (args
[i
].usable
&& args
[i
].name
== argname
)
7759 stringstream alternatives
;
7760 for (unsigned i
= 0; i
< args
.size(); ++i
)
7761 alternatives
<< " $" << args
[i
].name
;
7762 alternatives
<< " $$name $$parms $$vars";
7764 // We hope that this value ends up not being referenced after all, so it
7765 // can be optimized out quietly.
7766 throw semantic_error("unable to find tracepoint variable '" + e
->name
7767 + "' (alternatives:" + alternatives
.str () + ")", e
->tok
);
7768 // NB: we can have multiple errors, since a target variable
7769 // may be expanded in several different contexts:
7770 // trace ("*") { $foo->bar }
7773 // make sure we're not dereferencing base types
7775 e
->assert_no_components("tracepoint", true);
7777 // we can only write to dereferenced fields, and only if guru mode is on
7778 bool lvalue
= is_active_lvalue(e
);
7779 if (lvalue
&& (!dw
.sess
.guru_mode
|| e
->components
.empty()))
7780 throw semantic_error("write to tracepoint variable '" + e
->name
7781 + "' not permitted", e
->tok
);
7783 // XXX: if a struct/union arg is passed by value, then writing to its fields
7784 // is also meaningless until you dereference past a pointer member. It's
7785 // harder to detect and prevent that though...
7787 if (e
->components
.empty())
7790 throw semantic_error("cannot take address of tracepoint variable", e
->tok
);
7792 // Just grab the value from the probe locals
7793 symbol
* sym
= new symbol
;
7795 sym
->name
= "__tracepoint_arg_" + arg
->name
;
7800 // make a copy of the original as a bare target symbol for the tracepoint
7801 // value, which will be passed into the dwarf dereferencing code
7802 target_symbol
* e2
= deep_copy_visitor::deep_copy(e
);
7803 e2
->components
.clear();
7805 if (e
->components
.back().type
== target_symbol::comp_pretty_print
)
7808 throw semantic_error("cannot write to pretty-printed variable", e
->tok
);
7810 dwarf_pretty_print
dpp(dw
, &arg
->type_die
, e2
, arg
->isptr
, false, *e
);
7811 dpp
.expand()->visit (this);
7815 // Synthesize a function to dereference the dwarf fields,
7816 // with a pointer parameter that is the base tracepoint variable
7817 functiondecl
*fdecl
= new functiondecl
;
7818 fdecl
->synthetic
= true;
7819 fdecl
->tok
= e
->tok
;
7820 embeddedcode
*ec
= new embeddedcode
;
7823 string fname
= (string(lvalue
? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
7824 + "_" + e
->name
.substr(1)
7825 + "_" + lex_cast(tick
++));
7827 fdecl
->name
= fname
;
7830 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
7831 ec
->code
+= "\n#define fetch_register k_fetch_register\n";
7832 ec
->code
+= "#define store_register k_store_register\n";
7834 ec
->code
+= dw
.literal_stmt_for_pointer (&arg
->type_die
, e
,
7835 lvalue
, fdecl
->type
);
7837 // Give the fdecl an argument for the raw tracepoint value
7838 vardecl
*v1
= new vardecl
;
7840 v1
->name
= "pointer";
7842 fdecl
->formal_args
.push_back(v1
);
7844 // Any non-literal indexes need to be passed in too.
7845 for (unsigned i
= 0; i
< e
->components
.size(); ++i
)
7846 if (e
->components
[i
].type
== target_symbol::comp_expression_array_index
)
7848 vardecl
*v
= new vardecl
;
7850 v
->name
= "index" + lex_cast(i
);
7852 fdecl
->formal_args
.push_back(v
);
7857 // Modify the fdecl so it carries a pe_long formal
7858 // argument called "value".
7860 // FIXME: For the time being we only support setting target
7861 // variables which have base types; these are 'pe_long' in
7862 // stap's type vocabulary. Strings and pointers might be
7863 // reasonable, some day, but not today.
7865 vardecl
*v2
= new vardecl
;
7869 fdecl
->formal_args
.push_back(v2
);
7872 ec
->code
+= "/* pure */";
7874 ec
->code
+= "/* unprivileged */";
7877 ec
->code
+= "\n#undef fetch_register\n";
7878 ec
->code
+= "\n#undef store_register\n";
7880 fdecl
->join (dw
.sess
);
7882 // Synthesize a functioncall.
7883 functioncall
* n
= new functioncall
;
7885 n
->function
= fname
;
7886 n
->args
.push_back(require(e2
));
7888 // Any non-literal indexes need to be passed in too.
7889 for (unsigned i
= 0; i
< e
->components
.size(); ++i
)
7890 if (e
->components
[i
].type
== target_symbol::comp_expression_array_index
)
7891 n
->args
.push_back(require(e
->components
[i
].expr_index
));
7895 // Provide the functioncall to our parent, so that it can be
7896 // used to substitute for the assignment node immediately above
7898 assert(!target_symbol_setter_functioncalls
.empty());
7899 *(target_symbol_setter_functioncalls
.top()) = n
;
7908 tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol
* e
)
7911 throw semantic_error("cannot take address of context variable", e
->tok
);
7913 if (is_active_lvalue (e
))
7914 throw semantic_error("write to tracepoint '" + e
->name
+ "' not permitted", e
->tok
);
7916 if (e
->name
== "$$name")
7918 e
->assert_no_components("tracepoint");
7920 // Synthesize an embedded expression.
7921 embedded_expr
*expr
= new embedded_expr
;
7923 expr
->code
= string("/* string */ /* pure */ ")
7924 + string("c->marker_name ? c->marker_name : \"\"");
7927 else if (e
->name
== "$$vars" || e
->name
== "$$parms")
7929 e
->assert_no_components("tracepoint", true);
7931 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
7932 // NB: we synthesize a new token here rather than reusing
7933 // e->tok, because print_format::print likes to use
7934 // its tok->content.
7935 token
* pf_tok
= new token(*e
->tok
);
7936 pf_tok
->content
= "sprintf";
7938 print_format
* pf
= print_format::create(pf_tok
);
7940 for (unsigned i
= 0; i
< args
.size(); ++i
)
7942 if (!args
[i
].usable
)
7945 pf
->raw_components
+= " ";
7946 pf
->raw_components
+= args
[i
].name
;
7947 target_symbol
*tsym
= new target_symbol
;
7949 tsym
->name
= "$" + args
[i
].name
;
7950 tsym
->components
= e
->components
;
7952 // every variable should always be accessible!
7953 tsym
->saved_conversion_error
= 0;
7954 expression
*texp
= require (tsym
); // NB: throws nothing ...
7955 if (tsym
->saved_conversion_error
) // ... but this is how we know it happened.
7957 if (dw
.sess
.verbose
>2)
7958 for (semantic_error
*c
= tsym
->saved_conversion_error
;
7959 c
!= 0; c
= c
->chain
)
7960 clog
<< "variable location problem: " << c
->what() << endl
;
7961 pf
->raw_components
+= "=?";
7965 if (!e
->components
.empty() &&
7966 e
->components
[0].type
== target_symbol::comp_pretty_print
)
7967 pf
->raw_components
+= "=%s";
7969 pf
->raw_components
+= args
[i
].isptr
? "=%p" : "=%#x";
7970 pf
->args
.push_back(texp
);
7973 pf
->components
= print_format::string_to_components(pf
->raw_components
);
7977 assert(0); // shouldn't get here
7981 tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol
* e
)
7985 assert(e
->name
.size() > 0 && e
->name
[0] == '$');
7987 if (e
->name
== "$$name" || e
->name
== "$$parms" || e
->name
== "$$vars")
7988 visit_target_symbol_context (e
);
7990 visit_target_symbol_arg (e
);
7992 catch (const semantic_error
&er
)
8001 tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session
& s
,
8002 dwflpp
& dw
, Dwarf_Die
& func_die
,
8003 const string
& tracepoint_name
,
8004 probe
* base
, probe_point
* loc
):
8005 derived_probe (base
, loc
, true /* .components soon rewritten */),
8006 sess (s
), tracepoint_name (tracepoint_name
)
8008 // create synthetic probe point name; preserve condition
8009 vector
<probe_point::component
*> comps
;
8010 comps
.push_back (new probe_point::component (TOK_KERNEL
));
8011 comps
.push_back (new probe_point::component (TOK_TRACE
, new literal_string (tracepoint_name
)));
8012 this->sole_location()->components
= comps
;
8014 // fill out the available arguments in this tracepoint
8015 build_args(dw
, func_die
);
8017 // determine which header defined this tracepoint
8018 string decl_file
= dwarf_decl_file(&func_die
);
8021 #if 0 /* This convention is not enforced. */
8022 size_t header_pos
= decl_file
.rfind("trace/");
8023 if (header_pos
== string::npos
)
8024 throw semantic_error ("cannot parse header location for tracepoint '"
8025 + tracepoint_name
+ "' in '"
8027 header
= decl_file
.substr(header_pos
);
8030 // tracepoints from FOO_event_types.h should really be included from FOO.h
8031 // XXX can dwarf tell us the include hierarchy? it would be better to
8032 // ... walk up to see which one was directly included by tracequery.c
8033 // XXX: see also PR9993.
8034 size_t header_pos
= header
.find("_event_types");
8035 if (header_pos
!= string::npos
)
8036 header
.erase(header_pos
, 12);
8038 // Now expand the local variables in the probe body
8039 tracepoint_var_expanding_visitor
v (dw
, name
, args
);
8040 v
.replace (this->body
);
8041 for (unsigned i
= 0; i
< args
.size(); i
++)
8044 vardecl
* v
= new vardecl
;
8045 v
->name
= "__tracepoint_arg_" + args
[i
].name
;
8047 v
->set_arity(0, this->tok
);
8049 v
->skip_init
= true;
8050 this->locals
.push_back (v
);
8053 if (sess
.verbose
> 2)
8054 clog
<< "tracepoint-based " << name
<< " tracepoint='" << tracepoint_name
8060 resolve_tracepoint_arg_type(tracepoint_arg
& arg
)
8063 switch (dwarf_tag(&arg
.type_die
))
8065 case DW_TAG_typedef
:
8066 case DW_TAG_const_type
:
8067 case DW_TAG_volatile_type
:
8068 // iterate on the referent type
8069 return (dwarf_attr_die(&arg
.type_die
, DW_AT_type
, &arg
.type_die
)
8070 && resolve_tracepoint_arg_type(arg
));
8071 case DW_TAG_base_type
:
8072 // base types will simply be treated as script longs
8075 case DW_TAG_pointer_type
:
8076 // pointers can be treated as script longs,
8077 // and if we know their type, they can also be dereferenced
8078 type
= arg
.type_die
;
8079 while (dwarf_attr_die(&arg
.type_die
, DW_AT_type
, &arg
.type_die
))
8081 // It still might be a non-type, e.g. const void,
8082 // so we need to strip away all qualifiers.
8083 int tag
= dwarf_tag(&arg
.type_die
);
8084 if (tag
!= DW_TAG_typedef
&&
8085 tag
!= DW_TAG_const_type
&&
8086 tag
!= DW_TAG_volatile_type
)
8093 arg
.type_die
= type
;
8094 arg
.typecast
= "(intptr_t)";
8096 case DW_TAG_structure_type
:
8097 case DW_TAG_union_type
:
8098 // for structs/unions which are passed by value, we turn it into
8099 // a pointer that can be dereferenced.
8101 arg
.typecast
= "(intptr_t)&";
8104 // should we consider other types too?
8111 tracepoint_derived_probe::build_args(dwflpp
& dw
, Dwarf_Die
& func_die
)
8114 if (dwarf_child(&func_die
, &arg
) == 0)
8116 if (dwarf_tag(&arg
) == DW_TAG_formal_parameter
)
8118 // build a tracepoint_arg for this parameter
8119 tracepoint_arg tparg
;
8120 tparg
.name
= dwarf_diename(&arg
);
8122 // read the type of this parameter
8123 if (!dwarf_attr_die (&arg
, DW_AT_type
, &tparg
.type_die
)
8124 || !dwarf_type_name(&tparg
.type_die
, tparg
.c_type
))
8125 throw semantic_error ("cannot get type of tracepoint '"
8126 + tracepoint_name
+ "' parameter '"
8127 + tparg
.name
+ "'");
8129 tparg
.usable
= resolve_tracepoint_arg_type(tparg
);
8130 args
.push_back(tparg
);
8131 if (sess
.verbose
> 4)
8132 clog
<< "found parameter for tracepoint '" << tracepoint_name
8133 << "': type:'" << tparg
.c_type
8134 << "' name:'" << tparg
.name
<< "'" << endl
;
8136 while (dwarf_siblingof(&arg
, &arg
) == 0);
8140 tracepoint_derived_probe::getargs(std::list
<std::string
> &arg_set
) const
8142 for (unsigned i
= 0; i
< args
.size(); ++i
)
8144 arg_set
.push_back("$"+args
[i
].name
+":"+args
[i
].c_type
);
8148 tracepoint_derived_probe::join_group (systemtap_session
& s
)
8150 if (! s
.tracepoint_derived_probes
)
8151 s
.tracepoint_derived_probes
= new tracepoint_derived_probe_group ();
8152 s
.tracepoint_derived_probes
->enroll (this);
8157 tracepoint_derived_probe::print_dupe_stamp(ostream
& o
)
8159 for (unsigned i
= 0; i
< args
.size(); i
++)
8161 o
<< "__tracepoint_arg_" << args
[i
].name
<< endl
;
8165 static vector
<string
> tracepoint_extra_decls (systemtap_session
& s
)
8167 vector
<string
> they_live
;
8169 // XXX: may need this to be configurable
8170 they_live
.push_back ("#include <linux/skbuff.h>");
8172 // PR11649: conditional extra header
8173 // for kvm tracepoints in 2.6.33ish
8174 if (s
.kernel_config
["CONFIG_KVM"] != string("")) {
8175 they_live
.push_back ("#include <linux/kvm_host.h>");
8178 if (s
.kernel_config
["CONFIG_XFS_FS"] != string("")) {
8179 they_live
.push_back ("#define XFS_BIG_BLKNOS 1");
8180 if (s
.kernel_source_tree
!= "")
8181 they_live
.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
8182 they_live
.push_back ("struct xfs_mount;");
8183 they_live
.push_back ("struct xfs_inode;");
8184 they_live
.push_back ("struct xfs_buf;");
8185 they_live
.push_back ("struct xfs_bmbt_irec;");
8188 if (s
.kernel_config
["CONFIG_NFSD"] != string("")) {
8189 they_live
.push_back ("struct rpc_task;");
8192 they_live
.push_back ("#include <asm/cputime.h>");
8199 tracepoint_derived_probe_group::emit_module_decls (systemtap_session
& s
)
8204 s
.op
->newline() << "/* ---- tracepoint probes ---- */";
8207 // PR9993: Add extra headers to work around undeclared types in individual
8208 // include/trace/foo.h files
8209 const vector
<string
>& extra_decls
= tracepoint_extra_decls (s
);
8210 for (unsigned z
=0; z
<extra_decls
.size(); z
++)
8211 s
.op
->newline() << extra_decls
[z
] << "\n";
8213 for (unsigned i
= 0; i
< probes
.size(); ++i
)
8215 tracepoint_derived_probe
*p
= probes
[i
];
8217 // emit a separate entry function for each probe, since tracepoints
8218 // don't provide any sort of context pointer.
8219 s
.op
->newline() << "#undef TRACE_INCLUDE_FILE";
8220 s
.op
->newline() << "#undef TRACE_INCLUDE_PATH";
8222 // strip include/ substring, the same way as done in get_tracequery_module()
8223 string header
= p
->header
;
8224 size_t root_pos
= header
.rfind("include/");
8225 header
= ((root_pos
!= string::npos
) ? header
.substr(root_pos
+ 8) : header
);
8226 s
.op
->newline() << "#include <" << header
<< ">";
8228 // Starting in 2.6.35, at the same time NOARGS was added, the callback
8229 // always has a void* as the first parameter. PR11599
8230 s
.op
->newline() << "#ifdef DECLARE_TRACE_NOARGS";
8231 s
.op
->newline() << "#define STAP_TP_DATA , NULL";
8232 s
.op
->newline() << "#define STAP_TP_PROTO void *cb_data"
8233 << " __attribute__ ((unused))";
8234 if (!p
->args
.empty())
8235 s
.op
->line() << ",";
8236 s
.op
->newline() << "#else";
8237 s
.op
->newline() << "#define STAP_TP_DATA";
8238 s
.op
->newline() << "#define STAP_TP_PROTO";
8239 if (p
->args
.empty())
8240 s
.op
->line() << " void";
8241 s
.op
->newline() << "#endif";
8243 s
.op
->newline() << "static void enter_tracepoint_probe_" << i
<< "(";
8244 s
.op
->newline(2) << "STAP_TP_PROTO";
8246 for (unsigned j
= 0; j
< p
->args
.size(); ++j
)
8249 s
.op
->line() << ",";
8250 s
.op
->newline() << p
->args
[j
].c_type
<< " __tracepoint_arg_" << p
->args
[j
].name
;
8252 s
.op
->newline() << ")";
8253 s
.op
->newline(-2) << "{";
8255 s
.op
->newline(1) << "struct stap_probe * const probe = "
8256 << common_probe_init (p
) << ";";
8257 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "probe");
8258 s
.op
->newline() << "c->marker_name = "
8259 << lex_cast_qstring (p
->tracepoint_name
)
8261 for (unsigned j
= 0; j
< p
->args
.size(); ++j
)
8262 if (p
->args
[j
].used
)
8264 s
.op
->newline() << "c->probe_locals." << p
->name
<< ".__tracepoint_arg_"
8265 << p
->args
[j
].name
<< " = (int64_t)";
8266 s
.op
->line() << p
->args
[j
].typecast
;
8267 s
.op
->line() << "__tracepoint_arg_" << p
->args
[j
].name
<< ";";
8269 s
.op
->newline() << "(*probe->ph) (c);";
8270 common_probe_entryfn_epilogue (s
.op
);
8271 s
.op
->newline(-1) << "}";
8273 // emit normalized registration functions
8274 s
.op
->newline() << "static int register_tracepoint_probe_" << i
<< "(void) {";
8275 s
.op
->newline(1) << "return register_trace_" << p
->tracepoint_name
8276 << "(enter_tracepoint_probe_" << i
<< " STAP_TP_DATA);";
8277 s
.op
->newline(-1) << "}";
8279 // NB: we're not prepared to deal with unreg failures. However, failures
8280 // can only occur if the tracepoint doesn't exist (yet?), or if we
8281 // weren't even registered. The former should be OKed by the initial
8282 // registration call, and the latter is safe to ignore.
8283 s
.op
->newline() << "static void unregister_tracepoint_probe_" << i
<< "(void) {";
8284 s
.op
->newline(1) << "(void) unregister_trace_" << p
->tracepoint_name
8285 << "(enter_tracepoint_probe_" << i
<< " STAP_TP_DATA);";
8286 s
.op
->newline(-1) << "}";
8289 s
.op
->newline() << "#undef STAP_TP_DATA";
8290 s
.op
->newline() << "#undef STAP_TP_PROTO";
8294 // emit an array of registration functions for easy init/shutdown
8295 s
.op
->newline() << "static struct stap_tracepoint_probe {";
8296 s
.op
->newline(1) << "int (*reg)(void);";
8297 s
.op
->newline(0) << "void (*unreg)(void);";
8298 s
.op
->newline(-1) << "} stap_tracepoint_probes[] = {";
8300 for (unsigned i
= 0; i
< probes
.size(); ++i
)
8302 s
.op
->newline () << "{";
8303 s
.op
->line() << " .reg=®ister_tracepoint_probe_" << i
<< ",";
8304 s
.op
->line() << " .unreg=&unregister_tracepoint_probe_" << i
;
8305 s
.op
->line() << " },";
8307 s
.op
->newline(-1) << "};";
8313 tracepoint_derived_probe_group::emit_module_init (systemtap_session
&s
)
8315 if (probes
.size () == 0)
8318 s
.op
->newline() << "/* init tracepoint probes */";
8319 s
.op
->newline() << "for (i=0; i<" << probes
.size() << "; i++) {";
8320 s
.op
->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
8321 s
.op
->newline() << "if (rc) {";
8322 s
.op
->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
8323 s
.op
->newline(1) << "stap_tracepoint_probes[j].unreg();";
8324 s
.op
->newline(-1) << "break;"; // don't attempt to register any more probes
8325 s
.op
->newline(-1) << "}";
8326 s
.op
->newline(-1) << "}";
8328 // This would be technically proper (on those autoconf-detectable
8329 // kernels that include this function in tracepoint.h), however we
8330 // already make several calls to synchronze_sched() during our
8331 // shutdown processes.
8333 // s.op->newline() << "if (rc)";
8334 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
8335 // s.op->indent(-1);
8340 tracepoint_derived_probe_group::emit_module_exit (systemtap_session
& s
)
8345 s
.op
->newline() << "/* deregister tracepoint probes */";
8346 s
.op
->newline() << "for (i=0; i<" << probes
.size() << "; i++)";
8347 s
.op
->newline(1) << "stap_tracepoint_probes[i].unreg();";
8350 // Not necessary: see above.
8352 // s.op->newline() << "tracepoint_synchronize_unregister();";
8356 struct tracepoint_query
: public base_query
8358 tracepoint_query(dwflpp
& dw
, const string
& tracepoint
,
8359 probe
* base_probe
, probe_point
* base_loc
,
8360 vector
<derived_probe
*> & results
):
8361 base_query(dw
, "*"), tracepoint(tracepoint
),
8362 base_probe(base_probe
), base_loc(base_loc
),
8365 const string
& tracepoint
;
8368 probe_point
* base_loc
;
8369 vector
<derived_probe
*> & results
;
8370 set
<string
> probed_names
;
8372 void handle_query_module();
8373 int handle_query_cu(Dwarf_Die
* cudie
);
8374 int handle_query_func(Dwarf_Die
* func
);
8376 static int tracepoint_query_cu (Dwarf_Die
* cudie
, void * arg
);
8377 static int tracepoint_query_func (Dwarf_Die
* func
, base_query
* query
);
8382 tracepoint_query::handle_query_module()
8384 // look for the tracepoints in each CU
8385 dw
.iterate_over_cus(tracepoint_query_cu
, this);
8390 tracepoint_query::handle_query_cu(Dwarf_Die
* cudie
)
8392 dw
.focus_on_cu (cudie
);
8394 // look at each function to see if it's a tracepoint
8395 string function
= "stapprobe_" + tracepoint
;
8396 return dw
.iterate_over_functions (tracepoint_query_func
, this, function
);
8401 tracepoint_query::handle_query_func(Dwarf_Die
* func
)
8403 dw
.focus_on_function (func
);
8405 assert(startswith(dw
.function_name
, "stapprobe_"));
8406 string tracepoint_instance
= dw
.function_name
.substr(10);
8408 // check for duplicates -- sometimes tracepoint headers may be indirectly
8409 // included in more than one of our tracequery modules.
8410 if (!probed_names
.insert(tracepoint_instance
).second
)
8413 derived_probe
*dp
= new tracepoint_derived_probe (dw
.sess
, dw
, *func
,
8414 tracepoint_instance
,
8415 base_probe
, base_loc
);
8416 results
.push_back (dp
);
8422 tracepoint_query::tracepoint_query_cu (Dwarf_Die
* cudie
, void * arg
)
8424 tracepoint_query
* q
= static_cast<tracepoint_query
*>(arg
);
8425 if (pending_interrupts
) return DWARF_CB_ABORT
;
8426 return q
->handle_query_cu(cudie
);
8431 tracepoint_query::tracepoint_query_func (Dwarf_Die
* func
, base_query
* query
)
8433 tracepoint_query
* q
= static_cast<tracepoint_query
*>(query
);
8434 if (pending_interrupts
) return DWARF_CB_ABORT
;
8435 return q
->handle_query_func(func
);
8439 struct tracepoint_builder
: public derived_probe_builder
8443 bool init_dw(systemtap_session
& s
);
8444 string
get_tracequery_module(systemtap_session
& s
,
8445 const vector
<string
>& headers
);
8449 tracepoint_builder(): dw(0) {}
8450 ~tracepoint_builder() { delete dw
; }
8452 void build_no_more (systemtap_session
& s
)
8454 if (dw
&& s
.verbose
> 3)
8455 clog
<< "tracepoint_builder releasing dwflpp" << endl
;
8459 delete_session_module_cache (s
);
8462 void build(systemtap_session
& s
,
8463 probe
*base
, probe_point
*location
,
8464 literal_map_t
const& parameters
,
8465 vector
<derived_probe
*>& finished_results
);
8470 tracepoint_builder::get_tracequery_module(systemtap_session
& s
,
8471 const vector
<string
>& headers
)
8475 clog
<< "Pass 2: getting a tracequery for "
8476 << headers
.size() << " headers:" << endl
;
8477 for (size_t i
= 0; i
< headers
.size(); ++i
)
8478 clog
<< " " << headers
[i
] << endl
;
8481 string tracequery_path
;
8484 // see if the cached module exists
8485 tracequery_path
= find_tracequery_hash(s
, headers
);
8486 if (!tracequery_path
.empty() && !s
.poison_cache
)
8488 int fd
= open(tracequery_path
.c_str(), O_RDONLY
);
8492 clog
<< "Pass 2: using cached " << tracequery_path
<< endl
;
8494 return tracequery_path
;
8499 // no cached module, time to make it
8501 // PR9993: Add extra headers to work around undeclared types in individual
8502 // include/trace/foo.h files
8503 vector
<string
> short_decls
= tracepoint_extra_decls(s
);
8505 // add each requested tracepoint header
8506 for (size_t i
= 0; i
< headers
.size(); ++i
)
8508 const string
&header
= headers
[i
];
8509 size_t root_pos
= header
.rfind("include/");
8510 short_decls
.push_back(string("#include <") +
8511 ((root_pos
!= string::npos
) ? header
.substr(root_pos
+ 8) : header
) +
8515 string tracequery_ko
;
8516 int rc
= make_tracequery(s
, tracequery_ko
, short_decls
);
8518 tracequery_ko
= "/dev/null";
8520 // try to save tracequery in the cache
8522 copy_file(tracequery_ko
, tracequery_path
, s
.verbose
> 2);
8524 return tracequery_ko
;
8530 tracepoint_builder::init_dw(systemtap_session
& s
)
8535 vector
<string
> tracequery_modules
;
8536 vector
<string
> system_headers
;
8540 // find kernel_source_tree
8541 if (s
.kernel_source_tree
== "")
8544 DwflPtr dwfl_ptr
= setup_dwfl_kernel ("kernel", &found
, s
);
8545 Dwfl
*dwfl
= dwfl_ptr
.get()->dwfl
;
8548 Dwarf_Die
*cudie
= 0;
8550 while ((cudie
= dwfl_nextcu (dwfl
, cudie
, &bias
)) != NULL
)
8552 if (pending_interrupts
) break;
8553 Dwarf_Attribute attr
;
8554 const char* name
= dwarf_formstring (dwarf_attr (cudie
, DW_AT_comp_dir
, &attr
));
8558 clog
<< "Located kernel source tree (DW_AT_comp_dir) at '" << name
<< "'" << endl
;
8560 s
.kernel_source_tree
= name
;
8561 break; // skip others; modern Kbuild uses same comp_dir for them all
8568 vector
<string
> glob_prefixes
;
8569 glob_prefixes
.push_back (s
.kernel_build_tree
);
8570 if (s
.kernel_source_tree
!= "")
8571 glob_prefixes
.push_back (s
.kernel_source_tree
);
8574 vector
<string
> glob_suffixes
;
8575 glob_suffixes
.push_back("include/trace/events/*.h");
8576 glob_suffixes
.push_back("include/trace/*.h");
8577 glob_suffixes
.push_back("arch/x86/kvm/*trace.h");
8578 glob_suffixes
.push_back("fs/xfs/linux-2.6/xfs_tr*.h");
8580 // compute cartesian product
8581 vector
<string
> globs
;
8582 for (unsigned i
=0; i
<glob_prefixes
.size(); i
++)
8583 for (unsigned j
=0; j
<glob_suffixes
.size(); j
++)
8584 globs
.push_back (glob_prefixes
[i
]+string("/")+glob_suffixes
[j
]);
8586 for (unsigned z
= 0; z
< globs
.size(); z
++)
8588 string glob_str
= globs
[z
];
8590 clog
<< "Checking tracepoint glob " << glob_str
<< endl
;
8592 glob(glob_str
.c_str(), 0, NULL
, &trace_glob
);
8593 for (unsigned i
= 0; i
< trace_glob
.gl_pathc
; ++i
)
8595 string
header(trace_glob
.gl_pathv
[i
]);
8597 // filter out a few known "internal-only" headers
8598 if (endswith(header
, "/define_trace.h") ||
8599 endswith(header
, "/ftrace.h") ||
8600 endswith(header
, "/trace_events.h") ||
8601 endswith(header
, "_event_types.h"))
8604 system_headers
.push_back(header
);
8606 globfree(&trace_glob
);
8609 // First attempt to do all system headers in one go
8610 string tracequery_path
= get_tracequery_module(s
, system_headers
);
8611 // NB: An empty tracequery means that the header didn't compile correctly
8612 if (get_file_size(tracequery_path
))
8613 tracequery_modules
.push_back(tracequery_path
);
8615 // Otherwise try to do them one at a time (PR10424)
8616 for (size_t i
= 0; i
< system_headers
.size(); ++i
)
8618 if (pending_interrupts
) return false;
8619 vector
<string
> one_header(1, system_headers
[i
]);
8620 tracequery_path
= get_tracequery_module(s
, one_header
);
8621 if (get_file_size(tracequery_path
))
8622 tracequery_modules
.push_back(tracequery_path
);
8625 // TODO: consider other sources of tracepoint headers too, like from
8626 // a command-line parameter or some environment or .systemtaprc
8628 dw
= new dwflpp(s
, tracequery_modules
, true);
8633 tracepoint_builder::build(systemtap_session
& s
,
8634 probe
*base
, probe_point
*location
,
8635 literal_map_t
const& parameters
,
8636 vector
<derived_probe
*>& finished_results
)
8642 assert(get_param (parameters
, TOK_TRACE
, tracepoint
));
8644 tracepoint_query
q(*dw
, tracepoint
, base
, location
, finished_results
);
8645 dw
->iterate_over_modules(&query_module
, &q
);
8649 // ------------------------------------------------------------------------
8650 // Standard tapset registry.
8651 // ------------------------------------------------------------------------
8654 register_standard_tapsets(systemtap_session
& s
)
8656 register_tapset_been(s
);
8657 register_tapset_itrace(s
);
8658 register_tapset_mark(s
);
8659 register_tapset_procfs(s
);
8660 register_tapset_timers(s
);
8661 register_tapset_utrace(s
);
8663 // dwarf-based kprobe/uprobe parts
8664 dwarf_derived_probe::register_patterns(s
);
8666 // XXX: user-space starter set
8667 s
.pattern_root
->bind_num(TOK_PROCESS
)
8668 ->bind_num(TOK_STATEMENT
)->bind(TOK_ABSOLUTE
)
8669 ->bind_unprivileged()
8670 ->bind(new uprobe_builder ());
8671 s
.pattern_root
->bind_num(TOK_PROCESS
)
8672 ->bind_num(TOK_STATEMENT
)->bind(TOK_ABSOLUTE
)->bind(TOK_RETURN
)
8673 ->bind_unprivileged()
8674 ->bind(new uprobe_builder ());
8676 // kernel tracepoint probes
8677 s
.pattern_root
->bind(TOK_KERNEL
)->bind_str(TOK_TRACE
)
8678 ->bind(new tracepoint_builder());
8680 // Kprobe based probe
8681 s
.pattern_root
->bind(TOK_KPROBE
)->bind_str(TOK_FUNCTION
)
8682 ->bind(new kprobe_builder());
8683 s
.pattern_root
->bind(TOK_KPROBE
)->bind_str(TOK_MODULE
)
8684 ->bind_str(TOK_FUNCTION
)->bind(new kprobe_builder());
8685 s
.pattern_root
->bind(TOK_KPROBE
)->bind_str(TOK_FUNCTION
)->bind(TOK_RETURN
)
8686 ->bind(new kprobe_builder());
8687 s
.pattern_root
->bind(TOK_KPROBE
)->bind_str(TOK_FUNCTION
)->bind(TOK_RETURN
)
8688 ->bind_num(TOK_MAXACTIVE
)->bind(new kprobe_builder());
8689 s
.pattern_root
->bind(TOK_KPROBE
)->bind_str(TOK_MODULE
)
8690 ->bind_str(TOK_FUNCTION
)->bind(TOK_RETURN
)->bind(new kprobe_builder());
8691 s
.pattern_root
->bind(TOK_KPROBE
)->bind_str(TOK_MODULE
)
8692 ->bind_str(TOK_FUNCTION
)->bind(TOK_RETURN
)
8693 ->bind_num(TOK_MAXACTIVE
)->bind(new kprobe_builder());
8694 s
.pattern_root
->bind(TOK_KPROBE
)->bind_num(TOK_STATEMENT
)
8695 ->bind(TOK_ABSOLUTE
)->bind(new kprobe_builder());
8697 //Hwbkpt based probe
8698 // NB: we formerly registered the probe point types only if the kernel configuration
8699 // allowed it. However, we get better error messages if we allow probes to resolve.
8700 s
.pattern_root
->bind(TOK_KERNEL
)->bind_num(TOK_HWBKPT
)
8701 ->bind(TOK_HWBKPT_WRITE
)->bind(new hwbkpt_builder());
8702 s
.pattern_root
->bind(TOK_KERNEL
)->bind_str(TOK_HWBKPT
)
8703 ->bind(TOK_HWBKPT_WRITE
)->bind(new hwbkpt_builder());
8704 s
.pattern_root
->bind(TOK_KERNEL
)->bind_num(TOK_HWBKPT
)
8705 ->bind(TOK_HWBKPT_RW
)->bind(new hwbkpt_builder());
8706 s
.pattern_root
->bind(TOK_KERNEL
)->bind_str(TOK_HWBKPT
)
8707 ->bind(TOK_HWBKPT_RW
)->bind(new hwbkpt_builder());
8708 s
.pattern_root
->bind(TOK_KERNEL
)->bind_num(TOK_HWBKPT
)
8709 ->bind_num(TOK_LENGTH
)->bind(TOK_HWBKPT_WRITE
)->bind(new hwbkpt_builder());
8710 s
.pattern_root
->bind(TOK_KERNEL
)->bind_num(TOK_HWBKPT
)
8711 ->bind_num(TOK_LENGTH
)->bind(TOK_HWBKPT_RW
)->bind(new hwbkpt_builder());
8712 // length supported with address only, not symbol names
8714 //perf event based probe
8715 register_tapset_perf(s
);
8719 vector
<derived_probe_group
*>
8720 all_session_groups(systemtap_session
& s
)
8722 vector
<derived_probe_group
*> g
;
8725 if (s. x##_derived_probes) \
8726 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
8728 // Note that order *is* important here. We want to make sure we
8729 // register (actually run) begin probes before any other probe type
8730 // is run. Similarly, when unregistering probes, we want to
8731 // unregister (actually run) end probes after every other probe type
8732 // has be unregistered. To do the latter,
8733 // c_unparser::emit_module_exit() will run this list backwards.
8747 // Another "order is important" item. We want to make sure we
8748 // "register" the dummy task_finder probe group after all probe
8749 // groups that use the task_finder.
8757 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */