]>
Commit | Line | Data |
---|---|---|
56e12059 | 1 | // tapset resolution |
12b44fb3 | 2 | // Copyright (C) 2005-2009 Red Hat Inc. |
aa30ccd3 | 3 | // Copyright (C) 2005-2007 Intel Corporation. |
0b8f6579 | 4 | // Copyright (C) 2008 James.Bottomley@HansenPartnership.com |
56e12059 FCE |
5 | // |
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 | |
9 | // later version. | |
10 | ||
11 | #include "config.h" | |
12 | #include "staptree.h" | |
13 | #include "elaborate.h" | |
b55bc428 | 14 | #include "tapsets.h" |
93646f4d | 15 | #include "task_finder.h" |
56e12059 | 16 | #include "translate.h" |
dc38c0ae | 17 | #include "session.h" |
72dbc915 | 18 | #include "util.h" |
0a6f5a3f | 19 | #include "buildrun.h" |
86bf665e | 20 | #include "dwarf_wrappers.h" |
2e67a43b | 21 | #include "auto_free.h" |
b278033a | 22 | #include "hash.h" |
440f755a | 23 | #include "dwflpp.h" |
bd2b1e68 | 24 | |
3b579393 FCE |
25 | #include <cstdlib> |
26 | #include <algorithm> | |
bd2b1e68 | 27 | #include <deque> |
56e12059 | 28 | #include <iostream> |
bd2b1e68 | 29 | #include <map> |
ec4373ff | 30 | #include <set> |
56e12059 | 31 | #include <sstream> |
bd2b1e68 | 32 | #include <stdexcept> |
b55bc428 | 33 | #include <vector> |
e36387d7 | 34 | #include <cstdarg> |
29e64872 | 35 | #include <cassert> |
1969b5bc | 36 | #include <iomanip> |
f781f849 | 37 | #include <cerrno> |
bd2b1e68 GH |
38 | |
39 | extern "C" { | |
df8fadee | 40 | #include <fcntl.h> |
bd2b1e68 | 41 | #include <elfutils/libdwfl.h> |
7a053d3b | 42 | #include <elfutils/libdw.h> |
77de5e9e GH |
43 | #include <dwarf.h> |
44 | #include <elf.h> | |
45 | #include <obstack.h> | |
b20febf3 | 46 | #include <glob.h> |
30a279be | 47 | #include <fnmatch.h> |
5f0a03a6 | 48 | #include <stdio.h> |
349dc70e | 49 | #include <sys/types.h> |
4b1ad75e RM |
50 | |
51 | #define __STDC_FORMAT_MACROS | |
52 | #include <inttypes.h> | |
bd2b1e68 | 53 | } |
77de5e9e | 54 | |
56e12059 FCE |
55 | |
56 | using namespace std; | |
2171f774 | 57 | using namespace __gnu_cxx; |
56e12059 | 58 | |
46b84a80 | 59 | |
b20febf3 | 60 | |
b20febf3 FCE |
61 | // ------------------------------------------------------------------------ |
62 | void | |
a58d79d0 | 63 | common_probe_entryfn_prologue (translator_output* o, string statestr, |
c12d974f | 64 | string new_pp, |
912e8c59 | 65 | bool overload_processing) |
b20febf3 | 66 | { |
72d18b98 | 67 | o->newline() << "struct context* __restrict__ c;"; |
e0a17418 JS |
68 | o->newline() << "#if !INTERRUPTIBLE"; |
69 | o->newline() << "unsigned long flags;"; | |
70 | o->newline() << "#endif"; | |
b20febf3 | 71 | |
a58d79d0 DS |
72 | if (overload_processing) |
73 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
74 | else | |
75 | o->newline() << "#ifdef STP_TIMING"; | |
76 | o->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
b20febf3 | 77 | o->newline() << "#endif"; |
b20febf3 | 78 | |
e0a17418 JS |
79 | o->newline() << "#if INTERRUPTIBLE"; |
80 | o->newline() << "preempt_disable ();"; | |
81 | o->newline() << "#else"; | |
82 | o->newline() << "local_irq_save (flags);"; | |
83 | o->newline() << "#endif"; | |
b20febf3 | 84 | |
c931ec8a | 85 | // Check for enough free enough stack space |
d05a1d00 | 86 | o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space |
a63401b1 | 87 | o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space |
d05a1d00 FCE |
88 | // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack |
89 | // NB: see also CONFIG_DEBUG_STACKOVERFLOW | |
b3c3ca7c FCE |
90 | o->newline() << "atomic_inc (& skipped_count);"; |
91 | o->newline() << "#ifdef STP_TIMING"; | |
92 | o->newline() << "atomic_inc (& skipped_count_lowstack);"; | |
93 | o->newline() << "#endif"; | |
c931ec8a FCE |
94 | o->newline() << "goto probe_epilogue;"; |
95 | o->newline(-1) << "}"; | |
96 | ||
b20febf3 FCE |
97 | o->newline() << "if (atomic_read (&session_state) != " << statestr << ")"; |
98 | o->newline(1) << "goto probe_epilogue;"; | |
99 | o->indent(-1); | |
9a604fac | 100 | |
a44a0785 | 101 | o->newline() << "c = per_cpu_ptr (contexts, smp_processor_id());"; |
b3c3ca7c | 102 | o->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
9c736061 FCE |
103 | o->newline(1) << "#if !INTERRUPTIBLE"; |
104 | o->newline() << "atomic_inc (& skipped_count);"; | |
105 | o->newline() << "#endif"; | |
b3c3ca7c FCE |
106 | o->newline() << "#ifdef STP_TIMING"; |
107 | o->newline() << "atomic_inc (& skipped_count_reentrant);"; | |
c12d974f FCE |
108 | o->newline() << "#ifdef DEBUG_REENTRANCY"; |
109 | o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
110 | << new_pp << ", c->probe_point ?: \"?\", smp_processor_id());"; | |
111 | // NB: There is a conceivable race condition here with reading | |
112 | // c->probe_point, knowing that this other probe is sort of running. | |
113 | // However, in reality, it's interrupted. Plus even if it were able | |
114 | // to somehow start again, and stop before we read c->probe_point, | |
115 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
116 | o->newline() << "#endif"; | |
b3c3ca7c | 117 | o->newline() << "#endif"; |
9a604fac | 118 | o->newline() << "atomic_dec (& c->busy);"; |
b20febf3 | 119 | o->newline() << "goto probe_epilogue;"; |
9a604fac FCE |
120 | o->newline(-1) << "}"; |
121 | o->newline(); | |
1e00cfb1 | 122 | o->newline() << "c->last_stmt = 0;"; |
9a604fac | 123 | o->newline() << "c->last_error = 0;"; |
9a604fac | 124 | o->newline() << "c->nesting = 0;"; |
22f8b401 | 125 | o->newline() << "c->regs = 0;"; |
b916df9c | 126 | o->newline() << "c->unwaddr = 0;"; |
c12d974f | 127 | o->newline() << "c->probe_point = " << new_pp << ";"; |
b916df9c | 128 | // reset unwound address cache |
fcff848e | 129 | o->newline() << "c->pi = 0;"; |
9addf322 | 130 | o->newline() << "c->regparm = 0;"; |
bc54e71c MH |
131 | o->newline() << "c->marker_name = NULL;"; |
132 | o->newline() << "c->marker_format = NULL;"; | |
e0a17418 JS |
133 | |
134 | o->newline() << "#if INTERRUPTIBLE"; | |
135 | o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
136 | o->newline() << "#else"; | |
137 | o->newline() << "c->actionremaining = MAXACTION;"; | |
138 | o->newline() << "#endif"; | |
dbb68664 FCE |
139 | o->newline() << "#ifdef STP_TIMING"; |
140 | o->newline() << "c->statp = 0;"; | |
141 | o->newline() << "#endif"; | |
9915575b FCE |
142 | // NB: The following would actually be incorrect. |
143 | // That's because cycles_sum/cycles_base values are supposed to survive | |
144 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
145 | // cycles), the values will be reset. | |
146 | /* | |
f0e6dc63 FCE |
147 | o->newline() << "#ifdef STP_OVERLOAD"; |
148 | o->newline() << "c->cycles_sum = 0;"; | |
149 | o->newline() << "c->cycles_base = 0;"; | |
41c262f3 | 150 | o->newline() << "#endif"; |
9915575b | 151 | */ |
b20febf3 | 152 | } |
9a604fac | 153 | |
a44a0785 | 154 | |
b20febf3 | 155 | void |
a58d79d0 | 156 | common_probe_entryfn_epilogue (translator_output* o, |
912e8c59 | 157 | bool overload_processing) |
b20febf3 | 158 | { |
a58d79d0 DS |
159 | if (overload_processing) |
160 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
161 | else | |
162 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 163 | o->newline() << "{"; |
a58d79d0 DS |
164 | o->newline(1) << "cycles_t cycles_atend = get_cycles ();"; |
165 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
166 | // fewer, if the hardware counter rolls over really quickly. We | |
167 | // handle 32-bit wraparound here. | |
168 | o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
169 | o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
170 | o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
171 | o->indent(-1); | |
dbb68664 | 172 | |
a58d79d0 | 173 | o->newline() << "#ifdef STP_TIMING"; |
dbb68664 | 174 | o->newline() << "if (likely (c->statp)) _stp_stat_add(*c->statp, cycles_elapsed);"; |
a58d79d0 DS |
175 | o->newline() << "#endif"; |
176 | ||
177 | if (overload_processing) | |
178 | { | |
179 | o->newline() << "#ifdef STP_OVERLOAD"; | |
180 | o->newline() << "{"; | |
181 | // If the cycle count has wrapped (cycles_atend > cycles_base), | |
182 | // let's go ahead and pretend the interval has been reached. | |
183 | // This should reset cycles_base and cycles_sum. | |
184 | o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; | |
185 | o->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
186 | o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
187 | o->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
188 | ||
189 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
190 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
191 | // has overloaded the system and we need to quit. | |
192 | o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; | |
193 | o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
194 | o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
195 | o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);"; | |
551e9f14 | 196 | o->newline() << "atomic_inc (&error_count);"; |
a58d79d0 DS |
197 | o->newline(-1) << "}"; |
198 | ||
199 | o->newline() << "c->cycles_base = cycles_atend;"; | |
200 | o->newline() << "c->cycles_sum = 0;"; | |
201 | o->newline(-1) << "}"; | |
202 | o->newline(-1) << "}"; | |
203 | o->newline() << "#endif"; | |
204 | } | |
205 | ||
440f755a JS |
206 | o->newline(-1) << "}"; |
207 | o->newline() << "#endif"; | |
e57b735a | 208 | |
440f755a JS |
209 | o->newline() << "c->probe_point = 0;"; // vacated |
210 | o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; | |
211 | o->newline(1) << "if (c->last_stmt != NULL)"; | |
212 | o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
213 | o->newline(-1) << "else"; | |
214 | o->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
215 | o->indent(-1); | |
216 | o->newline() << "atomic_inc (& error_count);"; | |
217 | o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {"; | |
218 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
219 | o->newline() << "_stp_exit ();"; | |
220 | o->newline(-1) << "}"; | |
221 | o->newline(-1) << "}"; | |
222 | o->newline() << "atomic_dec (&c->busy);"; | |
e57b735a | 223 | |
440f755a JS |
224 | o->newline(-1) << "probe_epilogue:"; // context is free |
225 | o->indent(1); | |
e57b735a | 226 | |
440f755a JS |
227 | // Check for excessive skip counts. |
228 | o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {"; | |
229 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
230 | o->newline() << "_stp_exit ();"; | |
231 | o->newline(-1) << "}"; | |
e57b735a | 232 | |
440f755a JS |
233 | o->newline() << "#if INTERRUPTIBLE"; |
234 | o->newline() << "preempt_enable_no_resched ();"; | |
235 | o->newline() << "#else"; | |
236 | o->newline() << "local_irq_restore (flags);"; | |
237 | o->newline() << "#endif"; | |
238 | } | |
e57b735a | 239 | |
e57b735a | 240 | |
440f755a | 241 | // ------------------------------------------------------------------------ |
e57b735a | 242 | |
440f755a JS |
243 | // ------------------------------------------------------------------------ |
244 | // Dwarf derived probes. "We apologize for the inconvience." | |
245 | // ------------------------------------------------------------------------ | |
e57b735a | 246 | |
4627ed58 JS |
247 | static const string TOK_KERNEL("kernel"); |
248 | static const string TOK_MODULE("module"); | |
249 | static const string TOK_FUNCTION("function"); | |
250 | static const string TOK_INLINE("inline"); | |
251 | static const string TOK_CALL("call"); | |
252 | static const string TOK_RETURN("return"); | |
253 | static const string TOK_MAXACTIVE("maxactive"); | |
254 | static const string TOK_STATEMENT("statement"); | |
255 | static const string TOK_ABSOLUTE("absolute"); | |
256 | static const string TOK_PROCESS("process"); | |
257 | static const string TOK_MARK("mark"); | |
258 | static const string TOK_TRACE("trace"); | |
259 | static const string TOK_LABEL("label"); | |
e57b735a | 260 | |
1adf8ef1 | 261 | static int query_cu (Dwarf_Die * cudie, void * arg); |
e57b735a | 262 | |
440f755a JS |
263 | // Can we handle this query with just symbol-table info? |
264 | enum dbinfo_reqt | |
265 | { | |
266 | dbr_unknown, | |
267 | dbr_none, // kernel.statement(NUM).absolute | |
268 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
269 | dbr_need_dwarf | |
270 | }; | |
e57b735a | 271 | |
20e4a32c | 272 | |
440f755a JS |
273 | struct base_query; // forward decls |
274 | struct dwarf_query; | |
275 | struct dwflpp; | |
276 | struct symbol_table; | |
20e4a32c | 277 | |
a781f401 | 278 | |
440f755a JS |
279 | struct |
280 | symbol_table | |
281 | { | |
282 | module_info *mod_info; // associated module | |
283 | map<string, func_info*> map_by_name; | |
1c6b77e5 JS |
284 | multimap<Dwarf_Addr, func_info*> map_by_addr; |
285 | typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t; | |
440f755a JS |
286 | typedef pair<iterator_t, iterator_t> range_t; |
287 | #ifdef __powerpc__ | |
288 | GElf_Word opd_section; | |
289 | #endif | |
440f755a JS |
290 | void add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
291 | Dwarf_Addr *high_addr); | |
440f755a JS |
292 | enum info_status read_symbols(FILE *f, const string& path); |
293 | enum info_status read_from_elf_file(const string& path); | |
294 | enum info_status read_from_text_file(const string& path); | |
295 | enum info_status get_from_elf(); | |
296 | void prepare_section_rejection(Dwfl_Module *mod); | |
297 | bool reject_section(GElf_Word section); | |
440f755a JS |
298 | void purge_syscall_stubs(); |
299 | func_info *lookup_symbol(const string& name); | |
300 | Dwarf_Addr lookup_symbol_address(const string& name); | |
301 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
7a053d3b | 302 | |
440f755a JS |
303 | symbol_table(module_info *mi) : mod_info(mi) {} |
304 | ~symbol_table(); | |
305 | }; | |
77de5e9e | 306 | |
440f755a JS |
307 | static bool null_die(Dwarf_Die *die) |
308 | { | |
309 | static Dwarf_Die null = { 0 }; | |
310 | return (!die || !memcmp(die, &null, sizeof(null))); | |
311 | } | |
c4ce66a1 JS |
312 | |
313 | ||
7a053d3b | 314 | enum |
bd2b1e68 | 315 | function_spec_type |
7a053d3b | 316 | { |
bd2b1e68 GH |
317 | function_alone, |
318 | function_and_file, | |
7a053d3b | 319 | function_file_and_line |
bd2b1e68 GH |
320 | }; |
321 | ||
ec4373ff | 322 | |
bd2b1e68 | 323 | struct dwarf_builder; |
77de5e9e | 324 | |
2930abc7 | 325 | |
b20febf3 FCE |
326 | // XXX: This class is a candidate for subclassing to separate |
327 | // the relocation vs non-relocation variants. Likewise for | |
328 | // kprobe vs kretprobe variants. | |
329 | ||
330 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 331 | { |
b20febf3 FCE |
332 | dwarf_derived_probe (const string& function, |
333 | const string& filename, | |
334 | int line, | |
335 | const string& module, | |
336 | const string& section, | |
337 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 338 | Dwarf_Addr addr, |
b20febf3 FCE |
339 | dwarf_query & q, |
340 | Dwarf_Die* scope_die); | |
20e4a32c | 341 | |
b20febf3 FCE |
342 | string module; |
343 | string section; | |
344 | Dwarf_Addr addr; | |
2930abc7 | 345 | bool has_return; |
c9bad430 DS |
346 | bool has_maxactive; |
347 | long maxactive_val; | |
b95e2b79 | 348 | bool access_vars; |
2930abc7 | 349 | |
b8da0ad1 | 350 | void printsig (std::ostream &o) const; |
b20febf3 | 351 | void join_group (systemtap_session& s); |
9020300d FCE |
352 | void emit_probe_local_init(translator_output * o); |
353 | ||
bd2b1e68 | 354 | // Pattern registration helpers. |
7a053d3b | 355 | static void register_statement_variants(match_node * root, |
bd2b1e68 | 356 | dwarf_builder * dw); |
fd6602a0 FCE |
357 | static void register_function_variants(match_node * root, |
358 | dwarf_builder * dw); | |
7a053d3b | 359 | static void register_function_and_statement_variants(match_node * root, |
bd2b1e68 | 360 | dwarf_builder * dw); |
c4ce66a1 | 361 | static void register_patterns(systemtap_session& s); |
20c6c071 GH |
362 | }; |
363 | ||
dc38c0ae | 364 | |
6d0f3f0c FCE |
365 | struct uprobe_derived_probe: public derived_probe |
366 | { | |
367 | bool return_p; | |
368 | string module; // * => unrestricted | |
369 | int pid; // 0 => unrestricted | |
370 | string section; // empty => absolute address | |
371 | Dwarf_Addr address; | |
372 | // bool has_maxactive; | |
373 | // long maxactive_val; | |
0973d815 | 374 | |
6d0f3f0c FCE |
375 | uprobe_derived_probe (const string& function, |
376 | const string& filename, | |
377 | int line, | |
378 | const string& module, | |
379 | int pid, | |
380 | const string& section, | |
381 | Dwarf_Addr dwfl_addr, | |
382 | Dwarf_Addr addr, | |
383 | dwarf_query & q, | |
384 | Dwarf_Die* scope_die); | |
385 | ||
0973d815 FCE |
386 | // alternate constructor for process(PID).statement(ADDR).absolute |
387 | uprobe_derived_probe (probe *base, | |
388 | probe_point *location, | |
389 | int pid, | |
390 | Dwarf_Addr addr, | |
391 | bool return_p); | |
392 | ||
6d0f3f0c FCE |
393 | void printsig (std::ostream &o) const; |
394 | void join_group (systemtap_session& s); | |
395 | }; | |
396 | ||
dc38c0ae DS |
397 | struct dwarf_derived_probe_group: public derived_probe_group |
398 | { | |
399 | private: | |
b20febf3 FCE |
400 | multimap<string,dwarf_derived_probe*> probes_by_module; |
401 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
402 | |
403 | public: | |
b20febf3 FCE |
404 | void enroll (dwarf_derived_probe* probe); |
405 | void emit_module_decls (systemtap_session& s); | |
406 | void emit_module_init (systemtap_session& s); | |
407 | void emit_module_exit (systemtap_session& s); | |
dc38c0ae DS |
408 | }; |
409 | ||
410 | ||
20c6c071 | 411 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 412 | struct base_query |
20c6c071 | 413 | { |
c4ce66a1 JS |
414 | base_query(dwflpp & dw, literal_map_t const & params); |
415 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 416 | virtual ~base_query() {} |
bd2b1e68 | 417 | |
5227f1ea | 418 | systemtap_session & sess; |
2c384610 | 419 | dwflpp & dw; |
5227f1ea | 420 | |
bd2b1e68 | 421 | // Parameter extractors. |
86bf665e | 422 | static bool has_null_param(literal_map_t const & params, |
888af770 | 423 | string const & k); |
86bf665e | 424 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 425 | string const & k, string & v); |
86bf665e | 426 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 427 | string const & k, long & v); |
86bf665e | 428 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 429 | string const & k, Dwarf_Addr & v); |
b55bc428 | 430 | |
2c384610 DS |
431 | // Extracted parameters. |
432 | bool has_kernel; | |
91af0778 FCE |
433 | bool has_module; |
434 | bool has_process; | |
2c384610 DS |
435 | string module_val; // has_kernel => module_val = "kernel" |
436 | ||
437 | virtual void handle_query_module() = 0; | |
438 | }; | |
439 | ||
440 | ||
c4ce66a1 JS |
441 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
442 | sess(dw.sess), dw(dw) | |
2c384610 | 443 | { |
91af0778 | 444 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
445 | if (has_kernel) |
446 | module_val = "kernel"; | |
91af0778 FCE |
447 | |
448 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
449 | if (has_module) | |
450 | has_process = false; | |
4baf0e53 | 451 | else |
d0a7f5a9 FCE |
452 | { |
453 | has_process = get_string_param(params, TOK_PROCESS, module_val); | |
06aca46a | 454 | if (has_process) |
d0a7f5a9 FCE |
455 | module_val = find_executable (module_val); |
456 | } | |
91af0778 FCE |
457 | |
458 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
459 | } |
460 | ||
c4ce66a1 JS |
461 | base_query::base_query(dwflpp & dw, const string & module_val) |
462 | : sess(dw.sess), dw(dw), module_val(module_val) | |
463 | { | |
464 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
465 | // which means that userspace modules won't get any PATH searching. | |
466 | if (module_val.find('/') == string::npos) | |
467 | { | |
468 | has_kernel = (module_val == TOK_KERNEL); | |
469 | has_module = !has_kernel; | |
470 | has_process = false; | |
471 | } | |
472 | else | |
473 | { | |
474 | has_kernel = has_module = false; | |
475 | has_process = true; | |
476 | } | |
477 | } | |
478 | ||
2c384610 | 479 | bool |
86bf665e | 480 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
481 | string const & k) |
482 | { | |
888af770 | 483 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
484 | } |
485 | ||
486 | ||
487 | bool | |
86bf665e | 488 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
489 | string const & k, string & v) |
490 | { | |
491 | return derived_probe_builder::get_param (params, k, v); | |
492 | } | |
493 | ||
494 | ||
495 | bool | |
86bf665e | 496 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
497 | string const & k, long & v) |
498 | { | |
499 | int64_t value; | |
500 | bool present = derived_probe_builder::get_param (params, k, value); | |
501 | v = (long) value; | |
502 | return present; | |
503 | } | |
504 | ||
505 | ||
506 | bool | |
86bf665e | 507 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
508 | string const & k, Dwarf_Addr & v) |
509 | { | |
510 | int64_t value; | |
511 | bool present = derived_probe_builder::get_param (params, k, value); | |
512 | v = (Dwarf_Addr) value; | |
513 | return present; | |
514 | } | |
515 | ||
2c384610 DS |
516 | struct dwarf_query : public base_query |
517 | { | |
518 | dwarf_query(systemtap_session & sess, | |
519 | probe * base_probe, | |
520 | probe_point * base_loc, | |
521 | dwflpp & dw, | |
86bf665e | 522 | literal_map_t const & params, |
2c384610 DS |
523 | vector<derived_probe *> & results); |
524 | ||
c4ce66a1 JS |
525 | vector<derived_probe *> & results; |
526 | probe * base_probe; | |
527 | probe_point * base_loc; | |
528 | ||
2c384610 | 529 | virtual void handle_query_module(); |
5f0a03a6 JK |
530 | void query_module_dwarf(); |
531 | void query_module_symtab(); | |
2c384610 | 532 | |
2930abc7 FCE |
533 | void add_probe_point(string const & funcname, |
534 | char const * filename, | |
535 | int line, | |
536 | Dwarf_Die *scope_die, | |
537 | Dwarf_Addr addr); | |
36f9dd1d | 538 | |
2930abc7 | 539 | // Extracted parameters. |
7a053d3b | 540 | string function_val; |
20c6c071 GH |
541 | |
542 | bool has_function_str; | |
543 | bool has_statement_str; | |
544 | bool has_function_num; | |
545 | bool has_statement_num; | |
7a053d3b RM |
546 | string statement_str_val; |
547 | string function_str_val; | |
c239d28c GH |
548 | Dwarf_Addr statement_num_val; |
549 | Dwarf_Addr function_num_val; | |
20c6c071 | 550 | |
b8da0ad1 FCE |
551 | bool has_call; |
552 | bool has_inline; | |
20c6c071 GH |
553 | bool has_return; |
554 | ||
c9bad430 DS |
555 | bool has_maxactive; |
556 | long maxactive_val; | |
557 | ||
20c6c071 GH |
558 | bool has_label; |
559 | string label_val; | |
560 | ||
561 | bool has_relative; | |
562 | long relative_val; | |
563 | ||
37ebca01 FCE |
564 | bool has_absolute; |
565 | ||
467bea43 SC |
566 | bool has_mark; |
567 | ||
5f0a03a6 JK |
568 | enum dbinfo_reqt dbinfo_reqt; |
569 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
570 | ||
20c6c071 GH |
571 | function_spec_type parse_function_spec(string & spec); |
572 | function_spec_type spec_type; | |
573 | string function; | |
574 | string file; | |
0c8b7d37 | 575 | line_t line_type; |
879eb9e9 | 576 | int line[2]; |
5f0a03a6 | 577 | bool query_done; // Found exact match |
20c6c071 | 578 | |
7e1279ea FCE |
579 | set<char const *> filtered_srcfiles; |
580 | ||
581 | // Map official entrypc -> func_info object | |
86bf665e TM |
582 | inline_instance_map_t filtered_inlines; |
583 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
584 | bool choose_next_line; |
585 | Dwarf_Addr entrypc_for_next_line; | |
b55bc428 FCE |
586 | }; |
587 | ||
98afd80e FCE |
588 | |
589 | struct dwarf_builder: public derived_probe_builder | |
b55bc428 | 590 | { |
665e1256 | 591 | map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */ |
7a24d422 | 592 | map <string,dwflpp*> user_dw; |
ae2552da | 593 | dwarf_builder() {} |
aa30ccd3 | 594 | |
ae2552da | 595 | dwflpp *get_kern_dw(systemtap_session& sess, const string& module) |
707bf35e | 596 | { |
ae2552da FCE |
597 | if (kern_dw.find(module) == kern_dw.end()) |
598 | kern_dw[module] = new dwflpp(sess, module, true); | |
599 | return kern_dw[module]; | |
707bf35e JS |
600 | } |
601 | ||
602 | dwflpp *get_user_dw(systemtap_session& sess, const string& module) | |
603 | { | |
604 | if (user_dw.find(module) == user_dw.end()) | |
ae2552da | 605 | user_dw[module] = new dwflpp(sess, module, false); |
707bf35e JS |
606 | return user_dw[module]; |
607 | } | |
7a24d422 FCE |
608 | |
609 | /* NB: not virtual, so can be called from dtor too: */ | |
06aca46a | 610 | void dwarf_build_no_more (bool verbose) |
aa30ccd3 | 611 | { |
ae2552da FCE |
612 | for (map<string,dwflpp*>::iterator udi = kern_dw.begin(); |
613 | udi != kern_dw.end(); | |
614 | udi ++) | |
aa30ccd3 | 615 | { |
7a24d422 | 616 | if (verbose) |
ae2552da FCE |
617 | clog << "dwarf_builder releasing kernel dwflpp " << udi->first << endl; |
618 | delete udi->second; | |
aa30ccd3 | 619 | } |
ae2552da | 620 | kern_dw.erase (kern_dw.begin(), kern_dw.end()); |
7a24d422 FCE |
621 | |
622 | for (map<string,dwflpp*>::iterator udi = user_dw.begin(); | |
623 | udi != user_dw.end(); | |
624 | udi ++) | |
625 | { | |
626 | if (verbose) | |
627 | clog << "dwarf_builder releasing user dwflpp " << udi->first << endl; | |
628 | delete udi->second; | |
629 | } | |
630 | user_dw.erase (user_dw.begin(), user_dw.end()); | |
631 | } | |
632 | ||
633 | void build_no_more (systemtap_session &s) | |
634 | { | |
635 | dwarf_build_no_more (s.verbose > 3); | |
aa30ccd3 FCE |
636 | } |
637 | ||
e38d6504 RM |
638 | ~dwarf_builder() |
639 | { | |
7a24d422 | 640 | dwarf_build_no_more (false); |
c8959a29 | 641 | } |
aa30ccd3 | 642 | |
5227f1ea | 643 | virtual void build(systemtap_session & sess, |
7a053d3b | 644 | probe * base, |
20c6c071 | 645 | probe_point * location, |
86bf665e | 646 | literal_map_t const & parameters, |
20c6c071 | 647 | vector<derived_probe *> & finished_results); |
7a05f484 | 648 | |
611ab826 | 649 | set<string> probes_handled; |
7a05f484 SC |
650 | struct probe_table |
651 | { | |
652 | public: | |
653 | enum probe_types | |
654 | { | |
ad002306 SC |
655 | uprobe_type = 0x31425250, // "PRB1" |
656 | kprobe_type = 0x32425250, // "PRB2" | |
657 | utrace_type = 0x33425250, // "PRB3" | |
7a05f484 | 658 | } probe_type; |
7a05f484 | 659 | __uint64_t probe_arg; |
ad002306 | 660 | string & mark_name; |
7a05f484 | 661 | string probe_name; |
59d00eb0 | 662 | probe_table(string & mark_name, systemtap_session & sess, dwflpp * dw); |
0c3bfb1e SC |
663 | bool is_uprobe() {return probe_type == uprobe_type;}; |
664 | bool is_utrace() {return probe_type == utrace_type;}; | |
7a05f484 | 665 | bool get_next_probe(); |
5111fc3e SC |
666 | void convert_probe(probe *new_base); |
667 | void convert_location(probe *new_base, probe_point *new_location); | |
7a05f484 SC |
668 | |
669 | private: | |
ad002306 SC |
670 | bool have_probes; |
671 | systemtap_session & sess; | |
7a05f484 | 672 | dwflpp* dw; |
7a05f484 SC |
673 | Elf_Data *pdata; |
674 | size_t probe_scn_offset; | |
675 | size_t probe_scn_addr; | |
676 | }; | |
b55bc428 FCE |
677 | }; |
678 | ||
59d00eb0 JS |
679 | dwarf_builder::probe_table::probe_table(string& mark_name, systemtap_session & sess, dwflpp* dw): |
680 | mark_name(mark_name), sess(sess), dw(dw) | |
7a05f484 SC |
681 | { |
682 | Elf* elf; | |
683 | GElf_Shdr shdr_mem; | |
5e8208c0 | 684 | GElf_Shdr *shdr = NULL; |
7a05f484 SC |
685 | Dwarf_Addr bias; |
686 | size_t shstrndx; | |
687 | ||
d438dd9b MW |
688 | // Explicitly look in the main elf file first. |
689 | elf = dwfl_module_getelf (dw->module, &bias); | |
7a05f484 SC |
690 | Elf_Scn *probe_scn = NULL; |
691 | ||
fcc30d6d | 692 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); |
7a05f484 | 693 | |
7a05f484 | 694 | have_probes = false; |
ad002306 | 695 | |
7a05f484 SC |
696 | // Is there a .probes section? |
697 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
698 | { | |
699 | shdr = gelf_getshdr (probe_scn, &shdr_mem); | |
700 | assert (shdr != NULL); | |
701 | ||
702 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0) | |
703 | { | |
704 | have_probes = true; | |
705 | break; | |
706 | } | |
707 | } | |
5e8208c0 | 708 | |
7a05f484 | 709 | // Older versions put .probes section in the debuginfo dwarf file, |
d438dd9b MW |
710 | // so check if it actually exists, if not take a look in the debuginfo file |
711 | if (! have_probes || (have_probes && shdr->sh_type == SHT_NOBITS)) | |
7a05f484 | 712 | { |
d438dd9b MW |
713 | elf = dwarf_getelf (dwfl_module_getdwarf (dw->module, &bias)); |
714 | if (! elf) | |
715 | return; | |
fcc30d6d | 716 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); |
7a05f484 SC |
717 | probe_scn = NULL; |
718 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
719 | { | |
720 | shdr = gelf_getshdr (probe_scn, &shdr_mem); | |
721 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), | |
722 | ".probes") == 0) | |
723 | have_probes = true; | |
724 | break; | |
725 | } | |
726 | } | |
727 | ||
5e8208c0 JS |
728 | if (!have_probes) |
729 | return; | |
730 | ||
7a05f484 SC |
731 | pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
732 | probe_scn_offset = 0; | |
733 | probe_scn_addr = shdr->sh_addr; | |
734 | assert (pdata != NULL); | |
735 | if (sess.verbose > 4) | |
736 | clog << "got .probes elf scn_addr@0x" << probe_scn_addr << dec | |
737 | << ", size: " << pdata->d_size << endl; | |
738 | } | |
739 | ||
740 | bool | |
741 | dwarf_builder::probe_table::get_next_probe() | |
742 | { | |
743 | if (! have_probes) | |
744 | return false; | |
ad002306 | 745 | |
7a05f484 SC |
746 | // Extract probe info from the .probes section |
747 | if (sess.verbose > 3) | |
748 | clog << "probe_type == use_uprobe, use statement addr" << endl; | |
ad002306 | 749 | |
7a05f484 SC |
750 | while (probe_scn_offset < pdata->d_size) |
751 | { | |
ad002306 SC |
752 | struct probe_entry |
753 | { | |
754 | __uint64_t name; | |
755 | __uint64_t arg; | |
756 | } *pbe; | |
757 | __uint32_t *type = (__uint32_t*) ((char*)pdata->d_buf + probe_scn_offset); | |
758 | probe_type = (enum probe_types)*type; | |
7a05f484 SC |
759 | if (probe_type != uprobe_type && probe_type != kprobe_type |
760 | && probe_type != utrace_type) | |
761 | { | |
762 | // Unless this is a mangled .probes section, this happens | |
763 | // because the name of the probe comes first, followed by | |
764 | // the sentinel. | |
765 | if (sess.verbose > 5) | |
766 | clog << "got unknown probe_type: 0x" << hex << probe_type | |
767 | << dec << endl; | |
ad002306 | 768 | probe_scn_offset += sizeof(__uint32_t); |
7a05f484 SC |
769 | continue; |
770 | } | |
ad002306 SC |
771 | probe_scn_offset += sizeof(__uint32_t); |
772 | probe_scn_offset += probe_scn_offset % sizeof(__uint64_t); | |
773 | pbe = (struct probe_entry*) ((char*)pdata->d_buf + probe_scn_offset); | |
774 | probe_name = (char*)((char*)pdata->d_buf + pbe->name - (char*)probe_scn_addr); | |
775 | probe_arg = pbe->arg; | |
7a05f484 SC |
776 | if (sess.verbose > 4) |
777 | clog << "saw .probes " << probe_name | |
778 | << "@0x" << hex << probe_arg << dec << endl; | |
ad002306 SC |
779 | |
780 | probe_scn_offset += sizeof (struct probe_entry); | |
781 | if ((mark_name == probe_name) | |
782 | || (dw->name_has_wildcard (mark_name) | |
783 | && dw->function_name_matches_pattern (probe_name, mark_name))) | |
7a05f484 SC |
784 | { |
785 | if (sess.verbose > 3) | |
786 | clog << "found probe_name" << probe_name << " at 0x" | |
787 | << hex << probe_arg << dec << endl; | |
788 | return true; | |
789 | } | |
790 | else | |
791 | continue; | |
792 | } | |
793 | return false; | |
794 | } | |
888af770 | 795 | |
5111fc3e SC |
796 | |
797 | void | |
798 | dwarf_builder::probe_table::convert_probe (probe *new_base) | |
799 | { | |
800 | block *b = ((block*)(new_base->body)); | |
801 | if (probe_type == utrace_type) | |
802 | { | |
803 | // Generate: if ($syscall != 0xbead) next; | |
804 | if_statement *issc = new if_statement; | |
805 | issc->thenblock = new next_statement; | |
806 | issc->elseblock = NULL; | |
807 | issc->tok = new_base->body->tok; | |
808 | comparison *besc = new comparison; | |
809 | besc->op = "!="; | |
810 | besc->tok = new_base->body->tok; | |
811 | functioncall* n = new functioncall; | |
812 | n->tok = new_base->body->tok; | |
813 | n->function = "_utrace_syscall_nr"; | |
814 | n->referent = 0; | |
815 | besc->left = n; | |
816 | literal_number* fake_syscall = new literal_number(0xbead); | |
817 | fake_syscall->tok = new_base->body->tok; | |
818 | besc->right = fake_syscall; | |
819 | issc->condition = besc; | |
820 | b->statements.insert(b->statements.begin(),(statement*) issc); | |
821 | ||
822 | // Generate: if (ulong_arg(2) != task_tid(task_current())) next; | |
823 | if_statement *istid = new if_statement; | |
824 | istid->thenblock = new next_statement; | |
825 | istid->elseblock = NULL; | |
826 | istid->tok = new_base->body->tok; | |
827 | comparison *betid = new comparison; | |
828 | betid->op = "!="; | |
829 | betid->tok = new_base->body->tok; | |
830 | functioncall* task_tid = new functioncall; | |
831 | task_tid->tok = new_base->body->tok; | |
832 | task_tid->function = "task_tid"; | |
833 | task_tid->referent = 0; | |
834 | functioncall* task_current = new functioncall; | |
835 | task_current->tok = new_base->body->tok; | |
836 | task_current->function = "task_current"; | |
837 | task_current->referent = 0; | |
838 | task_tid->args.push_back(task_current); | |
839 | betid->left = task_tid; | |
840 | functioncall *arg2tid = new functioncall; | |
841 | arg2tid->tok = new_base->body->tok; | |
0c3bfb1e | 842 | arg2tid->function = "_utrace_syscall_arg"; |
5111fc3e | 843 | arg2tid->tok = new_base->body->tok; |
0c3bfb1e | 844 | literal_number* littid = new literal_number(1); |
5111fc3e SC |
845 | littid->tok = new_base->body->tok; |
846 | arg2tid->args.push_back(littid); | |
847 | ||
848 | betid->right = arg2tid; | |
849 | istid->condition = betid; | |
850 | b->statements.insert(b->statements.begin(),(statement*) istid); | |
851 | } | |
852 | ||
853 | // Generate: if (arg1 != mark("label")) next; | |
854 | functioncall *fc = new functioncall; | |
0c3bfb1e | 855 | fc->function = (probe_type == utrace_type) ? "_utrace_syscall_arg" : "ulong_arg"; |
5111fc3e | 856 | fc->tok = new_base->body->tok; |
0c3bfb1e | 857 | literal_number* num = new literal_number((probe_type == utrace_type) ? 0 : 1); |
5111fc3e SC |
858 | num->tok = new_base->body->tok; |
859 | fc->args.push_back(num); | |
860 | ||
861 | functioncall *fcus = new functioncall; | |
862 | fcus->function = "user_string"; | |
863 | fcus->type = pe_string; | |
864 | fcus->tok = new_base->body->tok; | |
865 | fcus->args.push_back(fc); | |
866 | ||
867 | if_statement *is = new if_statement; | |
868 | is->thenblock = new next_statement; | |
869 | is->elseblock = NULL; | |
870 | is->tok = new_base->body->tok; | |
871 | comparison *be = new comparison; | |
872 | be->op = "!="; | |
873 | be->tok = new_base->body->tok; | |
874 | be->left = fcus; | |
875 | be->right = new literal_string(mark_name); | |
876 | is->condition = be; | |
877 | b->statements.insert(b->statements.begin(),(statement*) is); | |
2fd285e6 SC |
878 | |
879 | #ifdef __i386__ | |
880 | if (probe_type == kprobe_type) | |
881 | { | |
882 | functioncall *rp = new functioncall; | |
883 | rp->tok = new_base->body->tok; | |
884 | rp->function = "regparm"; | |
885 | rp->tok = new_base->body->tok; | |
886 | literal_number* littid = new literal_number(0); | |
887 | littid->tok = new_base->body->tok; | |
888 | rp->args.push_back(littid); | |
889 | expr_statement* es = new expr_statement; | |
890 | es->tok = new_base->body->tok; | |
891 | es->value = rp; | |
892 | b->statements.insert(b->statements.begin(),(statement*) es); | |
893 | } | |
894 | #endif | |
5111fc3e SC |
895 | } |
896 | ||
897 | ||
898 | void | |
899 | dwarf_builder::probe_table::convert_location (probe *new_base, | |
900 | probe_point *new_location) | |
901 | { | |
902 | if (probe_type == kprobe_type) | |
903 | { | |
904 | new_location->components[0]->functor = "kernel"; | |
905 | new_location->components[0]->arg = NULL; | |
906 | new_location->components[1]->functor = "function"; | |
907 | new_location->components[1]->arg = new literal_string("*getegid*"); | |
908 | new_base->locations.push_back(new_location); | |
909 | } | |
910 | else if (probe_type == utrace_type) | |
911 | { | |
912 | // process("executable").syscall | |
913 | new_location->components[1]->functor = "syscall"; | |
914 | new_location->components[1]->arg = NULL; | |
915 | new_base->locations.push_back(new_location); | |
916 | } | |
917 | } | |
918 | ||
919 | ||
5227f1ea GH |
920 | dwarf_query::dwarf_query(systemtap_session & sess, |
921 | probe * base_probe, | |
20c6c071 GH |
922 | probe_point * base_loc, |
923 | dwflpp & dw, | |
86bf665e | 924 | literal_map_t const & params, |
20c6c071 | 925 | vector<derived_probe *> & results) |
c4ce66a1 JS |
926 | : base_query(dw, params), results(results), |
927 | base_probe(base_probe), base_loc(base_loc) | |
bd2b1e68 GH |
928 | { |
929 | // Reduce the query to more reasonable semantic values (booleans, | |
930 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
931 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
932 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
933 | ||
934 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
935 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
936 | ||
0f336e95 SC |
937 | has_label = get_string_param(params, TOK_LABEL, label_val); |
938 | ||
b8da0ad1 FCE |
939 | has_call = has_null_param(params, TOK_CALL); |
940 | has_inline = has_null_param(params, TOK_INLINE); | |
bd2b1e68 | 941 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 942 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 943 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 944 | has_mark = false; |
37ebca01 | 945 | |
bd2b1e68 GH |
946 | if (has_function_str) |
947 | spec_type = parse_function_spec(function_str_val); | |
948 | else if (has_statement_str) | |
949 | spec_type = parse_function_spec(statement_str_val); | |
0daad364 | 950 | |
5f0a03a6 JK |
951 | dbinfo_reqt = assess_dbinfo_reqt(); |
952 | query_done = false; | |
0daad364 JS |
953 | } |
954 | ||
955 | ||
440f755a JS |
956 | func_info_map_t * |
957 | get_filtered_functions(dwarf_query *q) | |
958 | { | |
959 | return &q->filtered_functions; | |
960 | } | |
961 | ||
962 | ||
963 | inline_instance_map_t * | |
964 | get_filtered_inlines(dwarf_query *q) | |
965 | { | |
966 | return &q->filtered_inlines; | |
967 | } | |
968 | ||
969 | ||
970 | void | |
971 | add_label_name(dwarf_query *q, const char *name) | |
972 | { | |
973 | // this is a kludge to let the listing mode show labels to the user | |
974 | if (q->sess.listing_mode) | |
975 | q->results.back()->locations[0]->components.push_back | |
976 | (new probe_point::component(TOK_LABEL, new literal_string (name))); | |
977 | } | |
978 | ||
979 | ||
2c384610 | 980 | void |
5f0a03a6 | 981 | dwarf_query::query_module_dwarf() |
2c384610 DS |
982 | { |
983 | if (has_function_num || has_statement_num) | |
984 | { | |
985 | // If we have module("foo").function(0xbeef) or | |
986 | // module("foo").statement(0xbeef), the address is relative | |
987 | // to the start of the module, so we seek the function | |
988 | // number plus the module's bias. | |
989 | ||
990 | Dwarf_Addr addr; | |
991 | if (has_function_num) | |
992 | addr = function_num_val; | |
993 | else | |
994 | addr = statement_num_val; | |
4baf0e53 | 995 | |
1f8592d1 MW |
996 | // Translate to an actual symbol address. |
997 | addr = dw.literal_addr_to_sym_addr (addr); | |
998 | ||
1adf8ef1 JS |
999 | Dwarf_Die* cudie = dw.query_cu_containing_address(addr); |
1000 | if (cudie) // address could be wildly out of range | |
1001 | query_cu(cudie, this); | |
2c384610 DS |
1002 | } |
1003 | else | |
1004 | { | |
1005 | // Otherwise if we have a function("foo") or statement("foo") | |
1006 | // specifier, we have to scan over all the CUs looking for | |
1007 | // the function(s) in question | |
1008 | assert(has_function_str || has_statement_str); | |
1009 | dw.iterate_over_cus(&query_cu, this); | |
1010 | } | |
1011 | } | |
1012 | ||
5f0a03a6 JK |
1013 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
1014 | dwarf_query * q); | |
1015 | ||
1016 | void | |
1017 | dwarf_query::query_module_symtab() | |
1018 | { | |
1019 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
1020 | if (dbinfo_reqt == dbr_need_dwarf) | |
1021 | return; | |
1022 | ||
1023 | module_info *mi = dw.mod_info; | |
1024 | if (dbinfo_reqt == dbr_need_symtab) | |
1025 | { | |
1026 | if (mi->symtab_status == info_unknown) | |
1027 | mi->get_symtab(this); | |
1028 | if (mi->symtab_status == info_absent) | |
1029 | return; | |
1030 | } | |
1031 | ||
1032 | func_info *fi = NULL; | |
1033 | symbol_table *sym_table = mi->sym_table; | |
1034 | ||
1035 | if (has_function_str) | |
1036 | { | |
1037 | // Per dwarf_query::assess_dbinfo_reqt()... | |
1038 | assert(spec_type == function_alone); | |
1039 | if (dw.name_has_wildcard(function_str_val)) | |
1040 | { | |
1041 | // Until we augment the blacklist sufficently... | |
1042 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
1043 | { | |
1044 | // e.g., kernel.function("*") | |
1045 | cerr << "Error: Pattern '" | |
1046 | << function_str_val | |
1047 | << "' matches every instruction address in the symbol table," | |
1048 | << endl | |
1049 | << "some of which aren't even functions." | |
1050 | << " Please be more precise." | |
1051 | << endl; | |
1052 | return; | |
1053 | } | |
2e67a43b | 1054 | symbol_table::iterator_t iter; |
1c6b77e5 JS |
1055 | for (iter = sym_table->map_by_addr.begin(); |
1056 | iter != sym_table->map_by_addr.end(); | |
2e67a43b | 1057 | ++iter) |
5f0a03a6 | 1058 | { |
1c6b77e5 | 1059 | fi = iter->second; |
5f0a03a6 JK |
1060 | if (!null_die(&fi->die)) |
1061 | continue; // already handled in query_module_dwarf() | |
1062 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
1063 | query_func_info(fi->addr, *fi, this); | |
1064 | } | |
1065 | } | |
1066 | else | |
1067 | { | |
1068 | fi = sym_table->lookup_symbol(function_str_val); | |
1069 | if (fi && null_die(&fi->die)) | |
1070 | query_func_info(fi->addr, *fi, this); | |
1071 | } | |
1072 | } | |
1073 | else | |
1074 | { | |
1075 | assert(has_function_num || has_statement_num); | |
1076 | // Find the "function" in which the indicated address resides. | |
1077 | Dwarf_Addr addr = | |
1078 | (has_function_num ? function_num_val : statement_num_val); | |
1079 | fi = sym_table->get_func_containing_address(addr); | |
1080 | if (!fi) | |
1081 | { | |
1082 | cerr << "Warning: address " | |
1083 | << hex << addr << dec | |
1084 | << " out of range for module " | |
1085 | << dw.module_name; | |
1086 | return; | |
1087 | } | |
1088 | if (!null_die(&fi->die)) | |
1089 | { | |
1090 | // addr looks like it's in the compilation unit containing | |
1091 | // the indicated function, but query_module_dwarf() didn't | |
1092 | // match addr to any compilation unit, so addr must be | |
1093 | // above that cu's address range. | |
1094 | cerr << "Warning: address " | |
1095 | << hex << addr << dec | |
1096 | << " maps to no known compilation unit in module " | |
1097 | << dw.module_name; | |
1098 | return; | |
1099 | } | |
1100 | query_func_info(fi->addr, *fi, this); | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | void | |
1105 | dwarf_query::handle_query_module() | |
1106 | { | |
1c6b77e5 JS |
1107 | bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab; |
1108 | dw.get_module_dwarf(false, report); | |
1109 | ||
1110 | // prebuild the symbol table to resolve aliases | |
1111 | dw.mod_info->get_symtab(this); | |
1112 | ||
5f0a03a6 JK |
1113 | if (dw.mod_info->dwarf_status == info_present) |
1114 | query_module_dwarf(); | |
1c6b77e5 | 1115 | |
5f0a03a6 JK |
1116 | // Consult the symbol table if we haven't found all we're looking for. |
1117 | // asm functions can show up in the symbol table but not in dwarf. | |
1118 | if (sess.consult_symtab && !query_done) | |
1119 | query_module_symtab(); | |
1120 | } | |
1121 | ||
2c384610 | 1122 | |
bd2b1e68 | 1123 | function_spec_type |
20c6c071 | 1124 | dwarf_query::parse_function_spec(string & spec) |
bd2b1e68 GH |
1125 | { |
1126 | string::const_iterator i = spec.begin(), e = spec.end(); | |
7a053d3b | 1127 | |
bd2b1e68 GH |
1128 | function.clear(); |
1129 | file.clear(); | |
879eb9e9 SC |
1130 | line[0] = 0; |
1131 | line[1] = 0; | |
bd2b1e68 GH |
1132 | |
1133 | while (i != e && *i != '@') | |
1134 | { | |
0c8b7d37 | 1135 | if (*i == ':' || *i == '+') |
bd2b1e68 GH |
1136 | goto bad; |
1137 | function += *i++; | |
1138 | } | |
1139 | ||
1140 | if (i == e) | |
1141 | { | |
b0ee93c4 | 1142 | if (sess.verbose>2) |
7a053d3b RM |
1143 | clog << "parsed '" << spec |
1144 | << "' -> func '" << function | |
db22e55f | 1145 | << "'\n"; |
bd2b1e68 GH |
1146 | return function_alone; |
1147 | } | |
1148 | ||
1149 | if (i++ == e) | |
1150 | goto bad; | |
1151 | ||
0c8b7d37 | 1152 | while (i != e && *i != ':' && *i != '+') |
bd2b1e68 | 1153 | file += *i++; |
41c262f3 | 1154 | if (*i == ':') |
879eb9e9 SC |
1155 | { |
1156 | if (*(i + 1) == '*') | |
1157 | line_type = WILDCARD; | |
1158 | else | |
1159 | line_type = ABSOLUTE; | |
1160 | } | |
0c8b7d37 SC |
1161 | else if (*i == '+') |
1162 | line_type = RELATIVE; | |
7a053d3b | 1163 | |
bd2b1e68 GH |
1164 | if (i == e) |
1165 | { | |
b0ee93c4 | 1166 | if (sess.verbose>2) |
7a053d3b RM |
1167 | clog << "parsed '" << spec |
1168 | << "' -> func '"<< function | |
1169 | << "', file '" << file | |
db22e55f | 1170 | << "'\n"; |
bd2b1e68 GH |
1171 | return function_and_file; |
1172 | } | |
1173 | ||
1174 | if (i++ == e) | |
1175 | goto bad; | |
1176 | ||
1177 | try | |
1178 | { | |
879eb9e9 SC |
1179 | if (line_type != WILDCARD) |
1180 | { | |
1181 | string::const_iterator dash = i; | |
41c262f3 | 1182 | |
879eb9e9 SC |
1183 | while (dash != e && *dash != '-') |
1184 | dash++; | |
1185 | if (dash == e) | |
1186 | line[0] = line[1] = lex_cast<int>(string(i, e)); | |
1187 | else | |
1188 | { | |
1189 | line_type = RANGE; | |
1190 | line[0] = lex_cast<int>(string(i, dash)); | |
1191 | line[1] = lex_cast<int>(string(dash + 1, e)); | |
1192 | } | |
1193 | } | |
41c262f3 | 1194 | |
b0ee93c4 | 1195 | if (sess.verbose>2) |
7a053d3b RM |
1196 | clog << "parsed '" << spec |
1197 | << "' -> func '"<< function | |
1198 | << "', file '" << file | |
db22e55f | 1199 | << "', line " << line << "\n"; |
bd2b1e68 GH |
1200 | return function_file_and_line; |
1201 | } | |
1202 | catch (runtime_error & exn) | |
1203 | { | |
1204 | goto bad; | |
1205 | } | |
1206 | ||
1207 | bad: | |
7a053d3b | 1208 | throw semantic_error("malformed specification '" + spec + "'", |
20c6c071 | 1209 | base_probe->tok); |
bd2b1e68 GH |
1210 | } |
1211 | ||
1212 | ||
36f9dd1d | 1213 | void |
b20febf3 FCE |
1214 | dwarf_query::add_probe_point(const string& funcname, |
1215 | const char* filename, | |
36f9dd1d | 1216 | int line, |
b20febf3 | 1217 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
1218 | Dwarf_Addr addr) |
1219 | { | |
b20febf3 | 1220 | string reloc_section; // base section for relocation purposes |
27646582 | 1221 | Dwarf_Addr reloc_addr; // relocated |
b20febf3 FCE |
1222 | string blacklist_section; // linking section for blacklist purposes |
1223 | const string& module = dw.module_name; // "kernel" or other | |
36f9dd1d | 1224 | |
37ebca01 FCE |
1225 | assert (! has_absolute); // already handled in dwarf_builder::build() |
1226 | ||
27646582 | 1227 | reloc_addr = dw.relocate_address(addr, reloc_section, blacklist_section); |
2930abc7 | 1228 | |
7f9f3386 FCE |
1229 | if (sess.verbose > 1) |
1230 | { | |
b20febf3 FCE |
1231 | clog << "probe " << funcname << "@" << filename << ":" << line; |
1232 | if (string(module) == TOK_KERNEL) | |
1233 | clog << " kernel"; | |
91af0778 | 1234 | else if (has_module) |
b20febf3 | 1235 | clog << " module=" << module; |
91af0778 FCE |
1236 | else if (has_process) |
1237 | clog << " process=" << module; | |
b20febf3 FCE |
1238 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
1239 | if (blacklist_section != "") clog << " section=" << blacklist_section; | |
1240 | clog << " pc=0x" << hex << addr << dec; | |
7f9f3386 | 1241 | } |
4baf0e53 | 1242 | |
27646582 JS |
1243 | bool bad = dw.blacklisted_p (funcname, filename, line, module, |
1244 | blacklist_section, addr, has_return); | |
b20febf3 FCE |
1245 | if (sess.verbose > 1) |
1246 | clog << endl; | |
7f9f3386 | 1247 | |
84048984 FCE |
1248 | if (module == TOK_KERNEL) |
1249 | { | |
1250 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
1251 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 1252 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
1253 | } |
1254 | ||
b20febf3 FCE |
1255 | if (! bad) |
1256 | { | |
1a0dbc5a | 1257 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
1258 | |
1259 | if (has_process) | |
1260 | { | |
1261 | results.push_back (new uprobe_derived_probe(funcname, filename, line, | |
1262 | module, 0, reloc_section, addr, reloc_addr, | |
1263 | *this, scope_die)); | |
1264 | } | |
1265 | else | |
1266 | { | |
1267 | assert (has_kernel || has_module); | |
1268 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 1269 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1270 | *this, scope_die)); |
1271 | } | |
b20febf3 | 1272 | } |
2930abc7 FCE |
1273 | } |
1274 | ||
5f0a03a6 JK |
1275 | enum dbinfo_reqt |
1276 | dwarf_query::assess_dbinfo_reqt() | |
1277 | { | |
1278 | if (has_absolute) | |
1279 | { | |
1280 | // kernel.statement(NUM).absolute | |
1281 | return dbr_none; | |
1282 | } | |
1283 | if (has_inline) | |
1284 | { | |
1285 | // kernel.function("f").inline or module("m").function("f").inline | |
1286 | return dbr_need_dwarf; | |
1287 | } | |
1288 | if (has_function_str && spec_type == function_alone) | |
1289 | { | |
1290 | // kernel.function("f") or module("m").function("f") | |
1291 | return dbr_need_symtab; | |
1292 | } | |
1293 | if (has_statement_num) | |
1294 | { | |
1295 | // kernel.statement(NUM) or module("m").statement(NUM) | |
1296 | // Technically, all we need is the module offset (or _stext, for | |
1297 | // the kernel). But for that we need either the ELF file or (for | |
1298 | // _stext) the symbol table. In either case, the symbol table | |
1299 | // is available, and that allows us to map the NUM (address) | |
1300 | // to a function, which is goodness. | |
1301 | return dbr_need_symtab; | |
1302 | } | |
1303 | if (has_function_num) | |
1304 | { | |
1305 | // kernel.function(NUM) or module("m").function(NUM) | |
1306 | // Need the symbol table so we can back up from NUM to the | |
1307 | // start of the function. | |
1308 | return dbr_need_symtab; | |
1309 | } | |
1310 | // Symbol table tells us nothing about source files or line numbers. | |
1311 | return dbr_need_dwarf; | |
1312 | } | |
2930abc7 FCE |
1313 | |
1314 | ||
b8da0ad1 FCE |
1315 | // The critical determining factor when interpreting a pattern |
1316 | // string is, perhaps surprisingly: "presence of a lineno". The | |
1317 | // presence of a lineno changes the search strategy completely. | |
1318 | // | |
1319 | // Compare the two cases: | |
1320 | // | |
1321 | // 1. {statement,function}(foo@file.c:lineno) | |
1322 | // - find the files matching file.c | |
1323 | // - in each file, find the functions matching foo | |
1324 | // - query the file for line records matching lineno | |
1325 | // - iterate over the line records, | |
1326 | // - and iterate over the functions, | |
1327 | // - if(haspc(function.DIE, line.addr)) | |
1328 | // - if looking for statements: probe(lineno.addr) | |
1329 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
1330 | // | |
1331 | // 2. {statement,function}(foo@file.c) | |
1332 | // - find the files matching file.c | |
1333 | // - in each file, find the functions matching foo | |
1334 | // - probe(function.{entrypc,return,etc.}) | |
1335 | // | |
1336 | // Thus the first decision we make is based on the presence of a | |
1337 | // lineno, and we enter entirely different sets of callbacks | |
1338 | // depending on that decision. | |
1339 | // | |
1340 | // Note that the first case is a generalization fo the second, in that | |
1341 | // we could theoretically search through line records for matching | |
1342 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
1343 | // are already cached elsewhere, so we can do an "index scan" as an | |
1344 | // optimization. | |
7e1279ea | 1345 | |
bd2b1e68 | 1346 | static void |
4cd232e4 | 1347 | query_statement (string const & func, |
20e4a32c | 1348 | char const * file, |
4cd232e4 | 1349 | int line, |
bcc12710 | 1350 | Dwarf_Die *scope_die, |
20e4a32c | 1351 | Dwarf_Addr stmt_addr, |
4cd232e4 | 1352 | dwarf_query * q) |
bd2b1e68 | 1353 | { |
39bcd429 FCE |
1354 | try |
1355 | { | |
cee35f73 | 1356 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 1357 | line, scope_die, stmt_addr); |
39bcd429 FCE |
1358 | } |
1359 | catch (const semantic_error& e) | |
1360 | { | |
1361 | q->sess.print_error (e); | |
1362 | } | |
bd2b1e68 GH |
1363 | } |
1364 | ||
7e1279ea | 1365 | static void |
3e961ba6 | 1366 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
1367 | dwarf_query * q) |
1368 | { | |
b6581717 | 1369 | try |
7e1279ea | 1370 | { |
b6581717 GH |
1371 | if (q->has_return) |
1372 | { | |
1373 | throw semantic_error ("cannot probe .return of inline function '" + ii.name + "'"); | |
1374 | } | |
1375 | else | |
1376 | { | |
b0ee93c4 | 1377 | if (q->sess.verbose>2) |
20e4a32c | 1378 | clog << "querying entrypc " |
3e961ba6 | 1379 | << hex << ii.entrypc << dec |
db22e55f | 1380 | << " of instance of inline '" << ii.name << "'\n"; |
20e4a32c | 1381 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
3e961ba6 | 1382 | &ii.die, ii.entrypc, q); |
b6581717 | 1383 | } |
7e1279ea | 1384 | } |
b6581717 | 1385 | catch (semantic_error &e) |
7e1279ea | 1386 | { |
b6581717 | 1387 | q->sess.print_error (e); |
7e1279ea FCE |
1388 | } |
1389 | } | |
1390 | ||
1391 | static void | |
1392 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 1393 | func_info & fi, |
7e1279ea FCE |
1394 | dwarf_query * q) |
1395 | { | |
b6581717 | 1396 | try |
7e1279ea | 1397 | { |
b6581717 GH |
1398 | if (q->has_return) |
1399 | { | |
1400 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
1401 | // kretprobe based on the entrypc in this case. | |
20e4a32c | 1402 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
1403 | &fi.die, entrypc, q); |
1404 | } | |
1405 | else | |
1406 | { | |
35dc8b04 | 1407 | if (fi.prologue_end != 0) |
44f75386 | 1408 | { |
44f75386 FCE |
1409 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1410 | &fi.die, fi.prologue_end, q); | |
1411 | } | |
1412 | else | |
1413 | { | |
1414 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
1415 | &fi.die, entrypc, q); | |
1416 | } | |
b6581717 | 1417 | } |
7e1279ea | 1418 | } |
b6581717 | 1419 | catch (semantic_error &e) |
7e1279ea | 1420 | { |
b6581717 | 1421 | q->sess.print_error (e); |
7e1279ea FCE |
1422 | } |
1423 | } | |
1424 | ||
1425 | ||
bd4b874d SC |
1426 | static void |
1427 | query_srcfile_label (const dwarf_line_t& line, void * arg) | |
1428 | { | |
1429 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1430 | ||
1431 | Dwarf_Addr addr = line.addr(); | |
1432 | ||
1433 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1434 | i != q->filtered_functions.end(); ++i) | |
1435 | if (q->dw.die_has_pc (i->die, addr)) | |
440f755a JS |
1436 | q->dw.iterate_over_labels (&i->die, q->label_val.c_str(), q->function.c_str(), |
1437 | q, query_statement); | |
bd4b874d SC |
1438 | } |
1439 | ||
7e1279ea | 1440 | static void |
86bf665e | 1441 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
1442 | { |
1443 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1444 | ||
86bf665e | 1445 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 1446 | |
86bf665e | 1447 | int lineno = line.lineno(); |
847bf07f | 1448 | |
86bf665e | 1449 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
1450 | i != q->filtered_functions.end(); ++i) |
1451 | { | |
3e961ba6 | 1452 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1453 | { |
b0ee93c4 | 1454 | if (q->sess.verbose>3) |
db22e55f | 1455 | clog << "function DIE lands on srcfile\n"; |
4cd232e4 | 1456 | if (q->has_statement_str) |
3e961ba6 | 1457 | query_statement (i->name, i->decl_file, |
847bf07f | 1458 | lineno, // NB: not q->line ! |
3e961ba6 | 1459 | &(i->die), addr, q); |
4cd232e4 | 1460 | else |
3e961ba6 | 1461 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 1462 | } |
20e4a32c RM |
1463 | } |
1464 | ||
86bf665e | 1465 | for (inline_instance_map_t::iterator i |
897820ca GH |
1466 | = q->filtered_inlines.begin(); |
1467 | i != q->filtered_inlines.end(); ++i) | |
1468 | { | |
3e961ba6 | 1469 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1470 | { |
b0ee93c4 | 1471 | if (q->sess.verbose>3) |
db22e55f | 1472 | clog << "inline instance DIE lands on srcfile\n"; |
897820ca | 1473 | if (q->has_statement_str) |
3e961ba6 JB |
1474 | query_statement (i->name, i->decl_file, |
1475 | q->line[0], &(i->die), addr, q); | |
897820ca | 1476 | else |
3e961ba6 | 1477 | query_inline_instance_info (*i, q); |
897820ca | 1478 | } |
20e4a32c | 1479 | } |
7e1279ea FCE |
1480 | } |
1481 | ||
1482 | ||
4fa7b22b | 1483 | static int |
7e1279ea | 1484 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
1485 | { |
1486 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
7e1279ea | 1487 | assert (!q->has_statement_num); |
bd2b1e68 | 1488 | |
39bcd429 | 1489 | try |
7a053d3b | 1490 | { |
b0ee93c4 | 1491 | if (q->sess.verbose>2) |
db22e55f | 1492 | clog << "examining inline instance of " << q->dw.function_name << "\n"; |
7e1279ea | 1493 | |
4baf0e53 | 1494 | if ((q->has_function_str && ! q->has_call) |
b8da0ad1 | 1495 | || q->has_statement_str) |
7e1279ea | 1496 | { |
b0ee93c4 | 1497 | if (q->sess.verbose>2) |
db22e55f FCE |
1498 | clog << "selected inline instance of " << q->dw.function_name |
1499 | << "\n"; | |
7e1279ea FCE |
1500 | |
1501 | Dwarf_Addr entrypc; | |
1502 | if (q->dw.die_entrypc (die, &entrypc)) | |
1503 | { | |
1504 | inline_instance_info inl; | |
1505 | inl.die = *die; | |
1506 | inl.name = q->dw.function_name; | |
3e961ba6 | 1507 | inl.entrypc = entrypc; |
4cd232e4 GH |
1508 | q->dw.function_file (&inl.decl_file); |
1509 | q->dw.function_line (&inl.decl_line); | |
3e961ba6 | 1510 | q->filtered_inlines.push_back(inl); |
7e1279ea FCE |
1511 | } |
1512 | } | |
1513 | return DWARF_CB_OK; | |
1514 | } | |
1515 | catch (const semantic_error& e) | |
1516 | { | |
1517 | q->sess.print_error (e); | |
1518 | return DWARF_CB_ABORT; | |
1519 | } | |
1520 | } | |
bb788f9f | 1521 | |
7e1279ea | 1522 | static int |
2da9cedb | 1523 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 1524 | { |
2da9cedb | 1525 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
bb788f9f | 1526 | |
7e1279ea FCE |
1527 | try |
1528 | { | |
7e1279ea FCE |
1529 | q->dw.focus_on_function (func); |
1530 | ||
20e4a32c | 1531 | if (q->dw.func_is_inline () |
b8da0ad1 | 1532 | && (! q->has_call) && (! q->has_return) |
1c6b77e5 | 1533 | && (q->has_statement_str || q->has_function_str)) |
7e1279ea | 1534 | { |
b0ee93c4 | 1535 | if (q->sess.verbose>3) |
db22e55f FCE |
1536 | clog << "checking instances of inline " << q->dw.function_name |
1537 | << "\n"; | |
2da9cedb | 1538 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
275f40a6 FCE |
1539 | |
1540 | if (q->dw.function_name_final_match (q->function)) | |
1541 | return DWARF_CB_ABORT; | |
7e1279ea | 1542 | } |
396afcee | 1543 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 1544 | { |
7e1279ea FCE |
1545 | bool record_this_function = false; |
1546 | ||
1c6b77e5 | 1547 | if (q->has_statement_str || q->has_function_str) |
7e1279ea FCE |
1548 | { |
1549 | record_this_function = true; | |
1550 | } | |
e4c58386 | 1551 | else if (q->has_function_num || q->has_statement_num) |
7e1279ea | 1552 | { |
e4c58386 | 1553 | Dwarf_Addr query_addr = |
9b692b91 SC |
1554 | (q->has_function_num ? q->function_num_val : |
1555 | q->has_statement_num ? q->statement_num_val : | |
1556 | (assert(0) , 0)); | |
7e1279ea FCE |
1557 | Dwarf_Die d; |
1558 | q->dw.function_die (&d); | |
20e4a32c | 1559 | |
1f8592d1 MW |
1560 | // Translate literal address to symbol address, then |
1561 | // compensate for dw bias. | |
1562 | query_addr = q->dw.literal_addr_to_sym_addr(query_addr); | |
1563 | query_addr -= q->dw.module_bias; | |
1564 | ||
86bf665e | 1565 | if (q->dw.die_has_pc (d, query_addr)) |
7e1279ea FCE |
1566 | record_this_function = true; |
1567 | } | |
1568 | ||
1569 | if (record_this_function) | |
1570 | { | |
b0ee93c4 | 1571 | if (q->sess.verbose>2) |
db22e55f | 1572 | clog << "selected function " << q->dw.function_name << "\n"; |
7e1279ea | 1573 | |
e4c58386 FCE |
1574 | func_info func; |
1575 | q->dw.function_die (&func.die); | |
1576 | func.name = q->dw.function_name; | |
1577 | q->dw.function_file (&func.decl_file); | |
1578 | q->dw.function_line (&func.decl_line); | |
1579 | ||
1580 | if (q->has_function_num || q->has_function_str || q->has_statement_str) | |
1581 | { | |
1582 | Dwarf_Addr entrypc; | |
1583 | if (q->dw.function_entrypc (&entrypc)) | |
3e961ba6 JB |
1584 | { |
1585 | func.entrypc = entrypc; | |
1586 | q->filtered_functions.push_back (func); | |
1587 | } | |
e4c58386 | 1588 | else |
552fdd9f JB |
1589 | /* this function just be fully inlined, just ignore it */ |
1590 | return DWARF_CB_OK; | |
e4c58386 FCE |
1591 | } |
1592 | else if (q->has_statement_num) | |
1593 | { | |
3e961ba6 | 1594 | func.entrypc = q->statement_num_val; |
1f8592d1 MW |
1595 | |
1596 | // Translate literal address to symbol address, then | |
1597 | // compensate for dw bias (will be used for query dw funcs). | |
1598 | func.entrypc = q->dw.literal_addr_to_sym_addr(func.entrypc); | |
1599 | func.entrypc -= q->dw.module_bias; | |
1600 | ||
3e961ba6 | 1601 | q->filtered_functions.push_back (func); |
275f40a6 FCE |
1602 | if (q->dw.function_name_final_match (q->function)) |
1603 | return DWARF_CB_ABORT; | |
e4c58386 FCE |
1604 | } |
1605 | else | |
1606 | assert(0); | |
1607 | ||
1608 | if (q->dw.function_name_final_match (q->function)) | |
1609 | return DWARF_CB_ABORT; | |
7e1279ea FCE |
1610 | } |
1611 | } | |
39bcd429 | 1612 | return DWARF_CB_OK; |
bd2b1e68 | 1613 | } |
39bcd429 | 1614 | catch (const semantic_error& e) |
bd2b1e68 | 1615 | { |
39bcd429 FCE |
1616 | q->sess.print_error (e); |
1617 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1618 | } |
bd2b1e68 GH |
1619 | } |
1620 | ||
1621 | static int | |
1622 | query_cu (Dwarf_Die * cudie, void * arg) | |
1623 | { | |
20c6c071 | 1624 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
49abf162 | 1625 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 1626 | |
39bcd429 | 1627 | try |
bd2b1e68 | 1628 | { |
7e1279ea | 1629 | q->dw.focus_on_cu (cudie); |
b5d77020 | 1630 | |
b0ee93c4 | 1631 | if (false && q->sess.verbose>2) |
b5d77020 | 1632 | clog << "focused on CU '" << q->dw.cu_name |
db22e55f | 1633 | << "', in module '" << q->dw.module_name << "'\n"; |
d9b516ca | 1634 | |
e4c58386 | 1635 | if (q->has_statement_str || q->has_statement_num |
54efe513 | 1636 | || q->has_function_str || q->has_function_num) |
7e1279ea FCE |
1637 | { |
1638 | q->filtered_srcfiles.clear(); | |
1639 | q->filtered_functions.clear(); | |
1640 | q->filtered_inlines.clear(); | |
1641 | ||
1642 | // In this path, we find "abstract functions", record | |
1643 | // information about them, and then (depending on lineno | |
1644 | // matching) possibly emit one or more of the function's | |
1645 | // associated addresses. Unfortunately the control of this | |
1646 | // cannot easily be turned inside out. | |
1647 | ||
b8da0ad1 | 1648 | if ((q->has_statement_str || q->has_function_str) |
7e1279ea FCE |
1649 | && (q->spec_type != function_alone)) |
1650 | { | |
1651 | // If we have a pattern string with a filename, we need | |
1652 | // to elaborate the srcfile mask in question first. | |
1653 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
1654 | ||
1655 | // If we have a file pattern and *no* srcfile matches, there's | |
1656 | // no need to look further into this CU, so skip. | |
1657 | if (q->filtered_srcfiles.empty()) | |
1658 | return DWARF_CB_OK; | |
1659 | } | |
86bf665e TM |
1660 | // Verify that a raw address matches the beginning of a |
1661 | // statement. This is a somewhat lame check that the address | |
467bea43 SC |
1662 | // is at the start of an assembly instruction. Mark probes are in the |
1663 | // middle of a macro and thus not strictly at a statement beginning. | |
6a35d102 | 1664 | // Guru mode may override this check. |
467bea43 | 1665 | if (q->has_statement_num && ! q->has_mark) |
86bf665e TM |
1666 | { |
1667 | Dwarf_Addr queryaddr = q->statement_num_val; | |
1668 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, queryaddr)); | |
1669 | Dwarf_Addr lineaddr = 0; | |
1670 | if (address_line) | |
1671 | lineaddr = address_line.addr(); | |
1672 | if (!address_line || lineaddr != queryaddr) | |
1673 | { | |
1674 | stringstream msg; | |
1675 | msg << "address 0x" << hex << queryaddr | |
1b1b4ceb | 1676 | << " does not match the beginning of a statement"; |
4116c576 MW |
1677 | if (address_line) |
1678 | msg << " (try 0x" << hex << lineaddr << ")"; | |
6a35d102 MW |
1679 | if (! q->sess.guru_mode) |
1680 | throw semantic_error(msg.str()); | |
1681 | else if (! q->sess.suppress_warnings) | |
1682 | q->sess.print_warning(msg.str()); | |
86bf665e TM |
1683 | } |
1684 | } | |
7e1279ea FCE |
1685 | // Pick up [entrypc, name, DIE] tuples for all the functions |
1686 | // matching the query, and fill in the prologue endings of them | |
1687 | // all in a single pass. | |
2da9cedb JS |
1688 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, |
1689 | q->function, | |
1690 | q->has_statement_num); | |
5f0a03a6 JK |
1691 | if (rc != DWARF_CB_OK) |
1692 | q->query_done = true; | |
44f75386 | 1693 | |
35dc8b04 | 1694 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 |
e4c58386 | 1695 | && !q->has_statement_str && !q->has_statement_num) // PR 2608 |
44f75386 FCE |
1696 | if (! q->filtered_functions.empty()) |
1697 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
7e1279ea | 1698 | |
bd4b874d SC |
1699 | if (q->has_label) |
1700 | { | |
1701 | if (q->line[0] == 0) // No line number specified | |
440f755a JS |
1702 | q->dw.iterate_over_labels (q->dw.cu, q->label_val.c_str(), q->function.c_str(), |
1703 | q, query_statement); | |
bd4b874d SC |
1704 | else |
1705 | for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin(); | |
1706 | i != q->filtered_srcfiles.end(); ++i) | |
1707 | q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str, | |
1708 | q->line_type, query_srcfile_label, q); | |
1709 | } | |
1710 | else if ((q->has_statement_str || q->has_function_str) | |
7e1279ea FCE |
1711 | && (q->spec_type == function_file_and_line)) |
1712 | { | |
1713 | // If we have a pattern string with target *line*, we | |
20e4a32c | 1714 | // have to look at lines in all the matched srcfiles. |
7e1279ea FCE |
1715 | for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin(); |
1716 | i != q->filtered_srcfiles.end(); ++i) | |
897820ca | 1717 | q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str, |
0c8b7d37 | 1718 | q->line_type, query_srcfile_line, q); |
7e1279ea FCE |
1719 | } |
1720 | else | |
1721 | { | |
e4c58386 | 1722 | // Otherwise, simply probe all resolved functions. |
86bf665e | 1723 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
e4c58386 | 1724 | i != q->filtered_functions.end(); ++i) |
3e961ba6 | 1725 | query_func_info (i->entrypc, *i, q); |
e4c58386 FCE |
1726 | |
1727 | // And all inline instances (if we're not excluding inlines with ".call") | |
1728 | if (! q->has_call) | |
86bf665e | 1729 | for (inline_instance_map_t::iterator i |
54efe513 | 1730 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) |
3e961ba6 | 1731 | query_inline_instance_info (*i, q); |
7e1279ea FCE |
1732 | } |
1733 | } | |
39bcd429 FCE |
1734 | else |
1735 | { | |
e4c58386 FCE |
1736 | // Before PR 5787, we used to have this: |
1737 | #if 0 | |
7e1279ea FCE |
1738 | // Otherwise we have a statement number, and we can just |
1739 | // query it directly within this module. | |
7e1279ea FCE |
1740 | assert (q->has_statement_num); |
1741 | Dwarf_Addr query_addr = q->statement_num_val; | |
b8da0ad1 | 1742 | query_addr = q->dw.module_address_to_global(query_addr); |
7e1279ea | 1743 | |
bcc12710 | 1744 | query_statement ("", "", -1, NULL, query_addr, q); |
e4c58386 FCE |
1745 | #endif |
1746 | // But now, we traverse CUs/functions even for | |
1747 | // statement_num's, for blacklist sensitivity and $var | |
1748 | // resolution purposes. | |
1749 | ||
1750 | assert (0); // NOTREACHED | |
39bcd429 FCE |
1751 | } |
1752 | return DWARF_CB_OK; | |
bd2b1e68 | 1753 | } |
39bcd429 | 1754 | catch (const semantic_error& e) |
bd2b1e68 | 1755 | { |
39bcd429 FCE |
1756 | q->sess.print_error (e); |
1757 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1758 | } |
bd2b1e68 GH |
1759 | } |
1760 | ||
0ce64fb8 | 1761 | |
5f0a03a6 JK |
1762 | static void |
1763 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
1764 | { | |
1765 | // Validate the machine code in this elf file against the | |
1766 | // session machine. This is important, in case the wrong kind | |
1767 | // of debuginfo is being automagically processed by elfutils. | |
1768 | // While we can tell i686 apart from x86-64, unfortunately | |
1769 | // we can't help confusing i586 vs i686 (both EM_386). | |
1770 | ||
1771 | Dwarf_Addr bias; | |
1772 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
1773 | // because dwfl_module_getelf can force costly section relocations | |
1774 | // we don't really need, while either will do for this purpose. | |
1775 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
1776 | ?: dwfl_module_getelf (mod, &bias)); | |
1777 | ||
1778 | GElf_Ehdr ehdr_mem; | |
1779 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 1780 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
5f0a03a6 JK |
1781 | int elf_machine = em->e_machine; |
1782 | const char* debug_filename = ""; | |
1783 | const char* main_filename = ""; | |
1784 | (void) dwfl_module_info (mod, NULL, NULL, | |
1785 | NULL, NULL, NULL, | |
1786 | & main_filename, | |
1787 | & debug_filename); | |
1788 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
1789 | |
1790 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
1791 | string expect_machine2; | |
5f0a03a6 JK |
1792 | |
1793 | switch (elf_machine) | |
1794 | { | |
756c9462 FCE |
1795 | // x86 and ppc are bi-architecture; a 64-bit kernel |
1796 | // can normally run either 32-bit or 64-bit *userspace*. | |
1797 | case EM_386: | |
1798 | expect_machine = "i?86"; | |
1799 | if (! q->has_process) break; // 32-bit kernel/module | |
1800 | /* FALLSTHROUGH */ | |
1801 | case EM_X86_64: | |
1802 | expect_machine2 = "x86_64"; | |
1803 | break; | |
1804 | case EM_PPC: | |
1805 | expect_machine = "ppc"; | |
1806 | if (! q->has_process) break; // 32-bit kernel/module | |
1807 | /* FALLSTHROUGH */ | |
1808 | case EM_PPC64: | |
1809 | expect_machine2 = "ppc64"; | |
1810 | break; | |
5f0a03a6 JK |
1811 | case EM_S390: expect_machine = "s390x"; break; |
1812 | case EM_IA_64: expect_machine = "ia64"; break; | |
1813 | case EM_ARM: expect_machine = "armv*"; break; | |
1814 | // XXX: fill in some more of these | |
1815 | default: expect_machine = "?"; break; | |
1816 | } | |
1817 | ||
1818 | if (! debug_filename) debug_filename = main_filename; | |
1819 | if (! debug_filename) debug_filename = name; | |
1820 | ||
756c9462 FCE |
1821 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
1822 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
1823 | { |
1824 | stringstream msg; | |
756c9462 FCE |
1825 | msg << "ELF machine " << expect_machine << "|" << expect_machine2 |
1826 | << " (code " << elf_machine | |
5f0a03a6 JK |
1827 | << ") mismatch with target " << sess_machine |
1828 | << " in '" << debug_filename << "'"; | |
1829 | throw semantic_error(msg.str ()); | |
1830 | } | |
1831 | ||
1832 | if (q->sess.verbose>2) | |
1833 | clog << "focused on module '" << q->dw.module_name | |
1834 | << " = [0x" << hex << q->dw.module_start | |
1835 | << "-0x" << q->dw.module_end | |
1836 | << ", bias 0x" << q->dw.module_bias << "]" << dec | |
1837 | << " file " << debug_filename | |
756c9462 | 1838 | << " ELF machine " << expect_machine << "|" << expect_machine2 |
5f0a03a6 JK |
1839 | << " (code " << elf_machine << ")" |
1840 | << "\n"; | |
1841 | } | |
1d3a40b6 | 1842 | |
91af0778 FCE |
1843 | |
1844 | ||
1845 | static Dwarf_Addr | |
1846 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
1847 | { | |
1848 | int syments = dwfl_module_getsymtab(m); | |
1849 | assert(syments); | |
1850 | for (int i = 1; i < syments; ++i) | |
1851 | { | |
1852 | GElf_Sym sym; | |
1853 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
1854 | if (name != NULL && strcmp(name, wanted) == 0) | |
1855 | return sym.st_value; | |
1856 | } | |
1857 | ||
1858 | return 0; | |
1859 | } | |
1860 | ||
1861 | ||
1862 | ||
bd2b1e68 | 1863 | static int |
b8da0ad1 | 1864 | query_module (Dwfl_Module *mod, |
91af0778 | 1865 | void **, |
b8da0ad1 | 1866 | const char *name, |
6f4c1275 | 1867 | Dwarf_Addr addr, |
b8da0ad1 | 1868 | void *arg) |
bd2b1e68 | 1869 | { |
91af0778 | 1870 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 1871 | |
39bcd429 | 1872 | try |
e38d6504 | 1873 | { |
91af0778 FCE |
1874 | module_info* mi = q->sess.module_cache->cache[name]; |
1875 | if (mi == 0) | |
1876 | { | |
1877 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
1878 | ||
6f4c1275 FCE |
1879 | mi->mod = mod; |
1880 | mi->addr = addr; | |
91af0778 | 1881 | |
6f4c1275 FCE |
1882 | const char* debug_filename = ""; |
1883 | const char* main_filename = ""; | |
1884 | (void) dwfl_module_info (mod, NULL, NULL, | |
1885 | NULL, NULL, NULL, | |
1886 | & main_filename, | |
1887 | & debug_filename); | |
1888 | ||
1889 | if (q->sess.ignore_vmlinux && name == TOK_KERNEL) | |
91af0778 FCE |
1890 | { |
1891 | // report_kernel() in elfutils found vmlinux, but pretend it didn't. | |
1892 | // Given a non-null path, returning 1 means keep reporting modules. | |
1893 | mi->dwarf_status = info_absent; | |
1894 | } | |
6f4c1275 | 1895 | else if (debug_filename || main_filename) |
91af0778 | 1896 | { |
6f4c1275 FCE |
1897 | mi->elf_path = debug_filename ?: main_filename; |
1898 | } | |
1899 | else if (name == TOK_KERNEL) | |
1900 | { | |
1901 | mi->dwarf_status = info_absent; | |
91af0778 | 1902 | } |
91af0778 FCE |
1903 | } |
1904 | // OK, enough of that module_info caching business. | |
1905 | ||
5f0a03a6 | 1906 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 1907 | |
39bcd429 FCE |
1908 | // If we have enough information in the pattern to skip a module and |
1909 | // the module does not match that information, return early. | |
b8da0ad1 | 1910 | if (!q->dw.module_name_matches(q->module_val)) |
0e14e079 | 1911 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
0cbbf9d1 FCE |
1912 | |
1913 | // Don't allow module("*kernel*") type expressions to match the | |
1914 | // elfutils module "kernel", which we refer to in the probe | |
1915 | // point syntax exclusively as "kernel.*". | |
1916 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
0e14e079 | 1917 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
b5d77020 | 1918 | |
5f0a03a6 JK |
1919 | if (mod) |
1920 | validate_module_elf(mod, name, q); | |
1921 | else | |
91af0778 FCE |
1922 | assert(q->has_kernel); // and no vmlinux to examine |
1923 | ||
1924 | if (q->sess.verbose>2) | |
1925 | cerr << "focused on module '" << q->dw.module_name << "'\n"; | |
1926 | ||
1927 | ||
1928 | // Collect a few kernel addresses. XXX: these belong better | |
1929 | // to the sess.module_info["kernel"] struct. | |
1930 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 1931 | { |
91af0778 FCE |
1932 | if (! q->sess.sym_kprobes_text_start) |
1933 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
1934 | if (! q->sess.sym_kprobes_text_end) | |
1935 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
1936 | if (! q->sess.sym_stext) | |
1937 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
1938 | } |
1939 | ||
91af0778 | 1940 | // Finally, search the module for matches of the probe point. |
2c384610 | 1941 | q->handle_query_module(); |
bb788f9f | 1942 | |
91af0778 | 1943 | |
b8da0ad1 | 1944 | // If we know that there will be no more matches, abort early. |
0e14e079 | 1945 | if (q->dw.module_name_final_match(q->module_val) || pending_interrupts) |
b8da0ad1 FCE |
1946 | return DWARF_CB_ABORT; |
1947 | else | |
1948 | return DWARF_CB_OK; | |
7a053d3b | 1949 | } |
39bcd429 | 1950 | catch (const semantic_error& e) |
bd2b1e68 | 1951 | { |
39bcd429 FCE |
1952 | q->sess.print_error (e); |
1953 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1954 | } |
bd2b1e68 GH |
1955 | } |
1956 | ||
35d4ab18 | 1957 | |
de688825 | 1958 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 1959 | { |
77de5e9e | 1960 | dwarf_query & q; |
bcc12710 | 1961 | Dwarf_Die *scope_die; |
77de5e9e | 1962 | Dwarf_Addr addr; |
8c819921 | 1963 | block *add_block; |
8fc05e57 | 1964 | probe *add_probe; |
cd5b28b2 | 1965 | std::map<std::string, symbol *> return_ts_map; |
b95e2b79 | 1966 | bool visited; |
77de5e9e | 1967 | |
de688825 | 1968 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
b95e2b79 | 1969 | q(q), scope_die(sd), addr(a), add_block(NULL), add_probe(NULL), visited(false) {} |
d7f3e0c5 | 1970 | void visit_target_symbol (target_symbol* e); |
c24447be | 1971 | void visit_cast_op (cast_op* e); |
77de5e9e GH |
1972 | }; |
1973 | ||
1974 | ||
de688825 | 1975 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 1976 | |
77de5e9e | 1977 | void |
de688825 | 1978 | var_expanding_visitor::visit_assignment (assignment* e) |
77de5e9e | 1979 | { |
e57b735a GH |
1980 | // Our job would normally be to require() the left and right sides |
1981 | // into a new assignment. What we're doing is slightly trickier: | |
1982 | // we're pushing a functioncall** onto a stack, and if our left | |
1983 | // child sets the functioncall* for that value, we're going to | |
1984 | // assume our left child was a target symbol -- transformed into a | |
1985 | // set_target_foo(value) call, and it wants to take our right child | |
1986 | // as the argument "value". | |
1987 | // | |
1988 | // This is why some people claim that languages with | |
1989 | // constructor-decomposing case expressions have a leg up on | |
1990 | // visitors. | |
1991 | ||
1992 | functioncall *fcall = NULL; | |
1993 | expression *new_left, *new_right; | |
d9b516ca | 1994 | |
e57b735a | 1995 | target_symbol_setter_functioncalls.push (&fcall); |
4ed05b15 | 1996 | new_left = require (e->left); |
e57b735a | 1997 | target_symbol_setter_functioncalls.pop (); |
4ed05b15 | 1998 | new_right = require (e->right); |
e57b735a GH |
1999 | |
2000 | if (fcall != NULL) | |
77de5e9e | 2001 | { |
e57b735a GH |
2002 | // Our left child is informing us that it was a target variable |
2003 | // and it has been replaced with a set_target_foo() function | |
2004 | // call; we are going to provide that function call -- with the | |
2005 | // right child spliced in as sole argument -- in place of | |
de688825 | 2006 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a GH |
2007 | |
2008 | // FIXME: for the time being, we only support plan $foo = bar, | |
2009 | // not += or any other op= variant. This is fixable, but a bit | |
2010 | // ugly. | |
2011 | if (e->op != "=") | |
2012 | throw semantic_error ("Operator-assign expressions on target " | |
2013 | "variables not implemented", e->tok); | |
2014 | ||
2015 | assert (new_left == fcall); | |
2016 | fcall->args.push_back (new_right); | |
4ed05b15 | 2017 | provide (fcall); |
77de5e9e | 2018 | } |
e57b735a GH |
2019 | else |
2020 | { | |
de688825 JS |
2021 | e->left = new_left; |
2022 | e->right = new_right; | |
2023 | provide (e); | |
e57b735a GH |
2024 | } |
2025 | } | |
d9b516ca | 2026 | |
d7f3e0c5 | 2027 | |
e57b735a | 2028 | void |
de688825 | 2029 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
e57b735a GH |
2030 | { |
2031 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
b95e2b79 | 2032 | visited = true; |
e57b735a | 2033 | |
cf2a1f85 DS |
2034 | bool lvalue = is_active_lvalue(e); |
2035 | if (lvalue && !q.sess.guru_mode) | |
2036 | throw semantic_error("write to target variable not permitted", e->tok); | |
2037 | ||
a43ba433 FCE |
2038 | // See if we need to generate a new probe to save/access function |
2039 | // parameters from a return probe. PR 1382. | |
2040 | if (q.has_return | |
2041 | && e->base_name != "$return" // not the special return-value variable handled below | |
2042 | && e->base_name != "$$return") // nor the other special variable handled below | |
85ecf79a DS |
2043 | { |
2044 | if (lvalue) | |
2045 | throw semantic_error("write to target variable not permitted in .return probes", e->tok); | |
2046 | ||
cd5b28b2 DS |
2047 | // Get the full name of the target symbol. |
2048 | stringstream ts_name_stream; | |
2049 | e->print(ts_name_stream); | |
2050 | string ts_name = ts_name_stream.str(); | |
2051 | ||
2052 | // Check and make sure we haven't already seen this target | |
2053 | // variable in this return probe. If we have, just return our | |
2054 | // last replacement. | |
2055 | map<string, symbol *>::iterator i = return_ts_map.find(ts_name); | |
2056 | if (i != return_ts_map.end()) | |
2057 | { | |
4ed05b15 | 2058 | provide (i->second); |
cd5b28b2 DS |
2059 | return; |
2060 | } | |
2061 | ||
85ecf79a DS |
2062 | // We've got to do several things here to handle target |
2063 | // variables in return probes. | |
2064 | ||
5e600bd6 DS |
2065 | // (1) Synthesize two global arrays. One is the cache of the |
2066 | // target variable and the other contains a thread specific | |
2067 | // nesting level counter. The arrays will look like | |
2068 | // this: | |
85ecf79a DS |
2069 | // |
2070 | // _dwarf_tvar_{name}_{num} | |
85ecf79a DS |
2071 | // _dwarf_tvar_{name}_{num}_ctr |
2072 | ||
2073 | string aname = (string("_dwarf_tvar_") | |
2074 | + e->base_name.substr(1) | |
2075 | + "_" + lex_cast<string>(tick++)); | |
2076 | vardecl* vd = new vardecl; | |
2077 | vd->name = aname; | |
2078 | vd->tok = e->tok; | |
2079 | q.sess.globals.push_back (vd); | |
2080 | ||
2081 | string ctrname = aname + "_ctr"; | |
2082 | vd = new vardecl; | |
2083 | vd->name = ctrname; | |
2084 | vd->tok = e->tok; | |
2085 | q.sess.globals.push_back (vd); | |
2086 | ||
8c819921 DS |
2087 | // (2) Create a new code block we're going to insert at the |
2088 | // beginning of this probe to get the cached value into a | |
2089 | // temporary variable. We'll replace the target variable | |
2090 | // reference with the temporary variable reference. The code | |
2091 | // will look like this: | |
2092 | // | |
2093 | // _dwarf_tvar_tid = tid() | |
2094 | // _dwarf_tvar_{name}_{num}_tmp | |
2095 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2096 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2097 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2098 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
46392da3 DS |
2099 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) |
2100 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
8c819921 DS |
2101 | |
2102 | // (2a) Synthesize the tid temporary expression, which will look | |
85ecf79a DS |
2103 | // like this: |
2104 | // | |
8c819921 DS |
2105 | // _dwarf_tvar_tid = tid() |
2106 | symbol* tidsym = new symbol; | |
2107 | tidsym->name = string("_dwarf_tvar_tid"); | |
2108 | tidsym->tok = e->tok; | |
85ecf79a | 2109 | |
8c819921 DS |
2110 | if (add_block == NULL) |
2111 | { | |
8fc05e57 DS |
2112 | add_block = new block; |
2113 | add_block->tok = e->tok; | |
2114 | ||
2115 | // Synthesize a functioncall to grab the thread id. | |
2116 | functioncall* fc = new functioncall; | |
2117 | fc->tok = e->tok; | |
2118 | fc->function = string("tid"); | |
2119 | ||
2120 | // Assign the tid to '_dwarf_tvar_tid'. | |
2121 | assignment* a = new assignment; | |
2122 | a->tok = e->tok; | |
2123 | a->op = "="; | |
2124 | a->left = tidsym; | |
2125 | a->right = fc; | |
2126 | ||
2127 | expr_statement* es = new expr_statement; | |
2128 | es->tok = e->tok; | |
2129 | es->value = a; | |
2130 | add_block->statements.push_back (es); | |
8c819921 DS |
2131 | } |
2132 | ||
2133 | // (2b) Synthesize an array reference and assign it to a | |
2134 | // temporary variable (that we'll use as replacement for the | |
2135 | // target variable reference). It will look like this: | |
2136 | // | |
2137 | // _dwarf_tvar_{name}_{num}_tmp | |
2138 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2139 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2140 | ||
8fc05e57 DS |
2141 | arrayindex* ai_tvar_base = new arrayindex; |
2142 | ai_tvar_base->tok = e->tok; | |
85ecf79a DS |
2143 | |
2144 | symbol* sym = new symbol; | |
2145 | sym->name = aname; | |
2146 | sym->tok = e->tok; | |
8fc05e57 | 2147 | ai_tvar_base->base = sym; |
8c819921 | 2148 | |
8fc05e57 | 2149 | ai_tvar_base->indexes.push_back(tidsym); |
85ecf79a | 2150 | |
8c819921 DS |
2151 | // We need to create a copy of the array index in its current |
2152 | // state so we can have 2 variants of it (the original and one | |
2153 | // that post-decrements the second index). | |
8fc05e57 DS |
2154 | arrayindex* ai_tvar = new arrayindex; |
2155 | arrayindex* ai_tvar_postdec = new arrayindex; | |
2156 | *ai_tvar = *ai_tvar_base; | |
2157 | *ai_tvar_postdec = *ai_tvar_base; | |
85ecf79a | 2158 | |
8c819921 DS |
2159 | // Synthesize the |
2160 | // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the | |
85ecf79a | 2161 | // second index into the array. |
8c819921 DS |
2162 | arrayindex* ai_ctr = new arrayindex; |
2163 | ai_ctr->tok = e->tok; | |
5e600bd6 | 2164 | |
85ecf79a DS |
2165 | sym = new symbol; |
2166 | sym->name = ctrname; | |
2167 | sym->tok = e->tok; | |
8c819921 DS |
2168 | ai_ctr->base = sym; |
2169 | ai_ctr->indexes.push_back(tidsym); | |
2170 | ai_tvar->indexes.push_back(ai_ctr); | |
2171 | ||
2172 | symbol* tmpsym = new symbol; | |
2173 | tmpsym->name = aname + "_tmp"; | |
2174 | tmpsym->tok = e->tok; | |
2175 | ||
2176 | assignment* a = new assignment; | |
2177 | a->tok = e->tok; | |
2178 | a->op = "="; | |
2179 | a->left = tmpsym; | |
2180 | a->right = ai_tvar; | |
2181 | ||
2182 | expr_statement* es = new expr_statement; | |
2183 | es->tok = e->tok; | |
2184 | es->value = a; | |
2185 | ||
2186 | add_block->statements.push_back (es); | |
2187 | ||
2188 | // (2c) Add a post-decrement to the second array index and | |
2189 | // delete the array value. It will look like this: | |
2190 | // | |
2191 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2192 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
5e600bd6 | 2193 | |
85ecf79a DS |
2194 | post_crement* pc = new post_crement; |
2195 | pc->tok = e->tok; | |
2196 | pc->op = "--"; | |
8c819921 | 2197 | pc->operand = ai_ctr; |
8fc05e57 | 2198 | ai_tvar_postdec->indexes.push_back(pc); |
8c819921 DS |
2199 | |
2200 | delete_statement* ds = new delete_statement; | |
2201 | ds->tok = e->tok; | |
8fc05e57 | 2202 | ds->value = ai_tvar_postdec; |
8c819921 DS |
2203 | |
2204 | add_block->statements.push_back (ds); | |
85ecf79a | 2205 | |
46392da3 DS |
2206 | // (2d) Delete the counter value if it is 0. It will look like |
2207 | // this: | |
2208 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
2209 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
4baf0e53 | 2210 | |
46392da3 DS |
2211 | ds = new delete_statement; |
2212 | ds->tok = e->tok; | |
2213 | ds->value = ai_ctr; | |
2214 | ||
2215 | unary_expression *ue = new unary_expression; | |
2216 | ue->tok = e->tok; | |
2217 | ue->op = "!"; | |
2218 | ue->operand = ai_ctr; | |
2219 | ||
2220 | if_statement *ifs = new if_statement; | |
2221 | ifs->tok = e->tok; | |
2222 | ifs->condition = ue; | |
2223 | ifs->thenblock = ds; | |
2224 | ifs->elseblock = NULL; | |
4baf0e53 | 2225 | |
46392da3 DS |
2226 | add_block->statements.push_back (ifs); |
2227 | ||
85ecf79a | 2228 | // (3) We need an entry probe that saves the value for us in the |
8fc05e57 DS |
2229 | // global array we created. Create the entry probe, which will |
2230 | // look like this: | |
85ecf79a | 2231 | // |
85ecf79a | 2232 | // probe kernel.function("{function}") { |
8fc05e57 DS |
2233 | // _dwarf_tvar_tid = tid() |
2234 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2235 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
85ecf79a DS |
2236 | // = ${param} |
2237 | // } | |
2238 | ||
8fc05e57 | 2239 | if (add_probe == NULL) |
85ecf79a | 2240 | { |
8fc05e57 DS |
2241 | add_probe = new probe; |
2242 | add_probe->tok = e->tok; | |
2243 | ||
2244 | // We need the name of the current probe point, minus the | |
2245 | // ".return" (or anything after it, such as ".maxactive(N)"). | |
2246 | // Create a new probe point, copying all the components, | |
2247 | // stopping when we see the ".return" component. | |
2248 | probe_point* pp = new probe_point; | |
2249 | for (unsigned c = 0; c < q.base_loc->components.size(); c++) | |
2250 | { | |
2251 | if (q.base_loc->components[c]->functor == "return") | |
2252 | break; | |
2253 | else | |
2254 | pp->components.push_back(q.base_loc->components[c]); | |
2255 | } | |
2256 | pp->tok = e->tok; | |
2257 | pp->optional = q.base_loc->optional; | |
2258 | add_probe->locations.push_back(pp); | |
2259 | ||
2260 | add_probe->body = new block; | |
2261 | add_probe->body->tok = e->tok; | |
2262 | ||
2263 | // Synthesize a functioncall to grab the thread id. | |
2264 | functioncall* fc = new functioncall; | |
2265 | fc->tok = e->tok; | |
2266 | fc->function = string("tid"); | |
2267 | ||
2268 | // Assign the tid to '_dwarf_tvar_tid'. | |
2269 | assignment* a = new assignment; | |
2270 | a->tok = e->tok; | |
2271 | a->op = "="; | |
2272 | a->left = tidsym; | |
2273 | a->right = fc; | |
2274 | ||
2275 | expr_statement* es = new expr_statement; | |
2276 | es->tok = e->tok; | |
2277 | es->value = a; | |
ba6f838d | 2278 | add_probe->body = new block(add_probe->body, es); |
8fc05e57 DS |
2279 | |
2280 | vardecl* vd = new vardecl; | |
2281 | vd->tok = e->tok; | |
2282 | vd->name = tidsym->name; | |
2283 | vd->type = pe_long; | |
2284 | vd->set_arity(0); | |
2285 | add_probe->locals.push_back(vd); | |
85ecf79a | 2286 | } |
8fc05e57 DS |
2287 | |
2288 | // Save the value, like this: | |
2289 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2290 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2291 | // = ${param} | |
2292 | arrayindex* ai_tvar_preinc = new arrayindex; | |
2293 | *ai_tvar_preinc = *ai_tvar_base; | |
4baf0e53 | 2294 | |
8fc05e57 DS |
2295 | pre_crement* preinc = new pre_crement; |
2296 | preinc->tok = e->tok; | |
2297 | preinc->op = "++"; | |
2298 | preinc->operand = ai_ctr; | |
2299 | ai_tvar_preinc->indexes.push_back(preinc); | |
4baf0e53 | 2300 | |
8fc05e57 DS |
2301 | a = new assignment; |
2302 | a->tok = e->tok; | |
2303 | a->op = "="; | |
2304 | a->left = ai_tvar_preinc; | |
2305 | a->right = e; | |
2306 | ||
2307 | es = new expr_statement; | |
2308 | es->tok = e->tok; | |
2309 | es->value = a; | |
2310 | ||
ba6f838d | 2311 | add_probe->body = new block(add_probe->body, es); |
85ecf79a | 2312 | |
8c819921 DS |
2313 | // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to |
2314 | // our parent so it can be used as a substitute for the target | |
2315 | // symbol. | |
4ed05b15 | 2316 | provide (tmpsym); |
cd5b28b2 DS |
2317 | |
2318 | // (5) Remember this replacement since we might be able to reuse | |
2319 | // it later if the same return probe references this target | |
2320 | // symbol again. | |
2321 | return_ts_map[ts_name] = tmpsym; | |
85ecf79a DS |
2322 | return; |
2323 | } | |
cf2a1f85 | 2324 | |
2cb3fe26 SC |
2325 | if (e->base_name == "$$vars" |
2326 | || e->base_name == "$$parms" | |
a43ba433 FCE |
2327 | || e->base_name == "$$locals" |
2328 | || (q.has_return && (e->base_name == "$$return"))) | |
2cb3fe26 SC |
2329 | { |
2330 | Dwarf_Die *scopes; | |
2331 | if (dwarf_getscopes_die (scope_die, &scopes) == 0) | |
2332 | return; | |
41c262f3 | 2333 | |
03c75a4a JS |
2334 | if (e->addressof) |
2335 | throw semantic_error("cannot take address of context variable", e->tok); | |
2336 | ||
2cb3fe26 SC |
2337 | target_symbol *tsym = new target_symbol; |
2338 | print_format* pf = new print_format; | |
2339 | ||
2340 | // Convert $$parms to sprintf of a list of parms and active local vars | |
2341 | // which we recursively evaluate | |
a43ba433 FCE |
2342 | |
2343 | // NB: we synthesize a new token here rather than reusing | |
2344 | // e->tok, because print_format::print likes to use | |
2345 | // its tok->content. | |
2346 | token* pf_tok = new token; | |
2347 | pf_tok->location = e->tok->location; | |
2348 | pf_tok->type = tok_identifier; | |
2349 | pf_tok->content = "sprint"; | |
2350 | ||
2351 | pf->tok = pf_tok; | |
2cb3fe26 SC |
2352 | pf->print_to_stream = false; |
2353 | pf->print_with_format = true; | |
2354 | pf->print_with_delim = false; | |
2355 | pf->print_with_newline = false; | |
2356 | pf->print_char = false; | |
2357 | ||
a43ba433 FCE |
2358 | if (q.has_return && (e->base_name == "$$return")) |
2359 | { | |
2360 | tsym->tok = e->tok; | |
2361 | tsym->base_name = "$return"; | |
41c262f3 | 2362 | |
a43ba433 FCE |
2363 | // Ignore any variable that isn't accessible. |
2364 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
2365 | expression *texp = tsym; |
2366 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
2367 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
2368 | { | |
2cb3fe26 | 2369 | |
a43ba433 FCE |
2370 | } |
2371 | else | |
2372 | { | |
fd574705 | 2373 | pf->raw_components += "return"; |
a43ba433 | 2374 | pf->raw_components += "=%#x "; |
4ed05b15 | 2375 | pf->args.push_back(texp); |
a43ba433 FCE |
2376 | } |
2377 | } | |
2378 | else | |
2379 | { | |
2380 | // non-.return probe: support $$parms, $$vars, $$locals | |
2381 | Dwarf_Die result; | |
2382 | if (dwarf_child (&scopes[0], &result) == 0) | |
2383 | do | |
00cf3709 | 2384 | { |
a43ba433 FCE |
2385 | switch (dwarf_tag (&result)) |
2386 | { | |
2387 | case DW_TAG_variable: | |
2388 | if (e->base_name == "$$parms") | |
2389 | continue; | |
2390 | break; | |
2391 | case DW_TAG_formal_parameter: | |
2392 | if (e->base_name == "$$locals") | |
2393 | continue; | |
2394 | break; | |
41c262f3 | 2395 | |
a43ba433 FCE |
2396 | default: |
2397 | continue; | |
2398 | } | |
41c262f3 | 2399 | |
a43ba433 | 2400 | const char *diename = dwarf_diename (&result); |
f76427a2 FCE |
2401 | if (! diename) continue; |
2402 | ||
a43ba433 FCE |
2403 | tsym->tok = e->tok; |
2404 | tsym->base_name = "$"; | |
2405 | tsym->base_name += diename; | |
41c262f3 | 2406 | |
a43ba433 FCE |
2407 | // Ignore any variable that isn't accessible. |
2408 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
2409 | expression *texp = tsym; |
2410 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
2411 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
2412 | { | |
12b44fb3 FCE |
2413 | if (q.sess.verbose>2) |
2414 | { | |
2415 | for (semantic_error *c = tsym->saved_conversion_error; | |
2416 | c != 0; | |
2417 | c = c->chain) { | |
2418 | clog << "variable location problem: " << c->what() << endl; | |
2419 | } | |
2420 | } | |
2421 | ||
a43ba433 FCE |
2422 | pf->raw_components += diename; |
2423 | pf->raw_components += "=? "; | |
2424 | } | |
2425 | else | |
2426 | { | |
2427 | pf->raw_components += diename; | |
2428 | pf->raw_components += "=%#x "; | |
4ed05b15 | 2429 | pf->args.push_back(texp); |
a43ba433 | 2430 | } |
00cf3709 | 2431 | } |
a43ba433 FCE |
2432 | while (dwarf_siblingof (&result, &result) == 0); |
2433 | } | |
2cb3fe26 | 2434 | |
2cb3fe26 | 2435 | pf->components = print_format::string_to_components(pf->raw_components); |
4ed05b15 | 2436 | provide (pf); |
2cb3fe26 SC |
2437 | |
2438 | return; | |
2439 | } | |
2440 | ||
e57b735a | 2441 | // Synthesize a function. |
d7f3e0c5 | 2442 | functiondecl *fdecl = new functiondecl; |
7b99c7d3 | 2443 | fdecl->tok = e->tok; |
d7f3e0c5 | 2444 | embeddedcode *ec = new embeddedcode; |
5e309481 | 2445 | ec->tok = e->tok; |
e8fbc5e8 | 2446 | |
1b07c728 | 2447 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
20e4a32c | 2448 | + "_" + e->base_name.substr(1) |
e57b735a GH |
2449 | + "_" + lex_cast<string>(tick++)); |
2450 | ||
66d284f4 | 2451 | try |
e57b735a | 2452 | { |
a43ba433 | 2453 | if (q.has_return && (e->base_name == "$return")) |
e19fda4e DS |
2454 | { |
2455 | ec->code = q.dw.literal_stmt_for_return (scope_die, | |
2456 | addr, | |
b4c34c26 | 2457 | e, |
e19fda4e DS |
2458 | lvalue, |
2459 | fdecl->type); | |
2460 | } | |
2461 | else | |
2462 | { | |
2463 | ec->code = q.dw.literal_stmt_for_local (scope_die, | |
2464 | addr, | |
2465 | e->base_name.substr(1), | |
b4c34c26 | 2466 | e, |
e19fda4e DS |
2467 | lvalue, |
2468 | fdecl->type); | |
2469 | } | |
2470 | ||
1b07c728 FCE |
2471 | if (! lvalue) |
2472 | ec->code += "/* pure */"; | |
66d284f4 FCE |
2473 | } |
2474 | catch (const semantic_error& er) | |
2475 | { | |
3bd0d4df RA |
2476 | if (!q.sess.skip_badvars) |
2477 | { | |
2478 | // We suppress this error message, and pass the unresolved | |
2479 | // target_symbol to the next pass. We hope that this value ends | |
2480 | // up not being referenced after all, so it can be optimized out | |
2481 | // quietly. | |
2482 | provide (e); | |
2483 | semantic_error* saveme = new semantic_error (er); // copy it | |
2484 | saveme->tok1 = e->tok; // XXX: token not passed to q.dw code generation routines | |
2485 | // NB: we can have multiple errors, since a $target variable | |
2486 | // may be expanded in several different contexts: | |
2487 | // function ("*") { $var } | |
2488 | saveme->chain = e->saved_conversion_error; | |
2489 | e->saved_conversion_error = saveme; | |
2490 | } | |
ad002306 | 2491 | else |
3bd0d4df | 2492 | { |
ad002306 | 2493 | // Upon user request for ignoring context, the symbol is replaced |
3bd0d4df RA |
2494 | // with a literal 0 and a warning message displayed |
2495 | literal_number* ln_zero = new literal_number (0); | |
2496 | ln_zero->tok = e->tok; | |
2497 | provide (ln_zero); | |
8c1b9e27 FCE |
2498 | if (!q.sess.suppress_warnings) |
2499 | q.sess.print_warning ("Bad $context variable being substituted with literal 0", | |
2500 | e->tok); | |
3bd0d4df | 2501 | } |
1cde5ba5 JS |
2502 | delete fdecl; |
2503 | delete ec; | |
cbfbbf69 | 2504 | return; |
66d284f4 | 2505 | } |
e57b735a | 2506 | |
d7f3e0c5 GH |
2507 | fdecl->name = fname; |
2508 | fdecl->body = ec; | |
e57b735a GH |
2509 | if (lvalue) |
2510 | { | |
2511 | // Modify the fdecl so it carries a single pe_long formal | |
2512 | // argument called "value". | |
2513 | ||
2514 | // FIXME: For the time being we only support setting target | |
2515 | // variables which have base types; these are 'pe_long' in | |
2516 | // stap's type vocabulary. Strings and pointers might be | |
2517 | // reasonable, some day, but not today. | |
2518 | ||
2519 | vardecl *v = new vardecl; | |
2520 | v->type = pe_long; | |
2521 | v->name = "value"; | |
2522 | v->tok = e->tok; | |
2523 | fdecl->formal_args.push_back(v); | |
2524 | } | |
f76427a2 | 2525 | q.sess.functions[fdecl->name]=fdecl; |
d9b516ca | 2526 | |
e57b735a | 2527 | // Synthesize a functioncall. |
d7f3e0c5 GH |
2528 | functioncall* n = new functioncall; |
2529 | n->tok = e->tok; | |
2530 | n->function = fname; | |
35d4ab18 | 2531 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session |
e57b735a GH |
2532 | |
2533 | if (lvalue) | |
2534 | { | |
2535 | // Provide the functioncall to our parent, so that it can be | |
2536 | // used to substitute for the assignment node immediately above | |
2537 | // us. | |
2538 | assert(!target_symbol_setter_functioncalls.empty()); | |
2539 | *(target_symbol_setter_functioncalls.top()) = n; | |
2540 | } | |
2541 | ||
4ed05b15 | 2542 | provide (n); |
77de5e9e GH |
2543 | } |
2544 | ||
2545 | ||
c24447be JS |
2546 | void |
2547 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
2548 | { | |
2549 | // Fill in our current module context if needed | |
2550 | if (e->module.empty()) | |
2551 | e->module = q.dw.module_name; | |
2552 | ||
2553 | var_expanding_visitor::visit_cast_op(e); | |
2554 | } | |
2555 | ||
2556 | ||
c4ce66a1 JS |
2557 | struct dwarf_cast_query : public base_query |
2558 | { | |
946e1a48 | 2559 | cast_op& e; |
c4ce66a1 | 2560 | const bool lvalue; |
c4ce66a1 | 2561 | |
abb41d92 JS |
2562 | exp_type& pe_type; |
2563 | string& code; | |
c4ce66a1 | 2564 | |
946e1a48 | 2565 | dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, |
abb41d92 JS |
2566 | bool lvalue, exp_type& pe_type, string& code): |
2567 | base_query(dw, module), e(e), lvalue(lvalue), | |
2568 | pe_type(pe_type), code(code) {} | |
c4ce66a1 JS |
2569 | |
2570 | void handle_query_module(); | |
2571 | int handle_query_cu(Dwarf_Die * cudie); | |
2572 | ||
2573 | static int cast_query_cu (Dwarf_Die * cudie, void * arg); | |
2574 | }; | |
2575 | ||
2576 | ||
c4ce66a1 JS |
2577 | void |
2578 | dwarf_cast_query::handle_query_module() | |
2579 | { | |
abb41d92 | 2580 | if (!code.empty()) |
c4ce66a1 JS |
2581 | return; |
2582 | ||
2583 | // look for the type in each CU | |
2584 | dw.iterate_over_cus(cast_query_cu, this); | |
2585 | } | |
2586 | ||
2587 | ||
2588 | int | |
2589 | dwarf_cast_query::handle_query_cu(Dwarf_Die * cudie) | |
2590 | { | |
abb41d92 | 2591 | if (!code.empty()) |
c4ce66a1 JS |
2592 | return DWARF_CB_ABORT; |
2593 | ||
2594 | dw.focus_on_cu (cudie); | |
2595 | Dwarf_Die* type_die = dw.declaration_resolve(e.type.c_str()); | |
2596 | if (type_die) | |
2597 | { | |
2598 | try | |
2599 | { | |
b4c34c26 | 2600 | code = dw.literal_stmt_for_pointer (type_die, &e, |
c4ce66a1 JS |
2601 | lvalue, pe_type); |
2602 | } | |
946e1a48 | 2603 | catch (const semantic_error& er) |
c4ce66a1 | 2604 | { |
946e1a48 JS |
2605 | // XXX might be better to try again in another CU |
2606 | // NB: we can have multiple errors, since a @cast | |
2607 | // may be expanded in several different contexts: | |
2608 | // function ("*") { @cast(...) } | |
2609 | semantic_error* new_er = new semantic_error(er); | |
2610 | new_er->chain = e.saved_conversion_error; | |
2611 | e.saved_conversion_error = new_er; | |
c4ce66a1 | 2612 | } |
c4ce66a1 JS |
2613 | return DWARF_CB_ABORT; |
2614 | } | |
2615 | return DWARF_CB_OK; | |
2616 | } | |
2617 | ||
2618 | ||
2619 | int | |
2620 | dwarf_cast_query::cast_query_cu (Dwarf_Die * cudie, void * arg) | |
2621 | { | |
2622 | dwarf_cast_query * q = static_cast<dwarf_cast_query *>(arg); | |
2623 | if (pending_interrupts) return DWARF_CB_ABORT; | |
2624 | return q->handle_query_cu(cudie); | |
2625 | } | |
2626 | ||
2627 | ||
2628 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor | |
2629 | { | |
2630 | systemtap_session& s; | |
2631 | dwarf_builder& db; | |
2632 | ||
2633 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
2634 | s(s), db(db) {} | |
2635 | void visit_cast_op (cast_op* e); | |
fb0274bc | 2636 | void filter_special_modules(string& module); |
c4ce66a1 JS |
2637 | }; |
2638 | ||
2639 | ||
fb0274bc JS |
2640 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
2641 | { | |
d90053e7 | 2642 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 2643 | // for those cases, build a module including that header |
d90053e7 JS |
2644 | if (module[module.size() - 1] == '>' && |
2645 | (module[0] == '<' || module.compare(0, 7, "kernel<") == 0)) | |
fb0274bc JS |
2646 | { |
2647 | string cached_module; | |
2648 | if (s.use_cache) | |
2649 | { | |
2650 | // see if the cached module exists | |
2651 | find_typequery_hash(s, module, cached_module); | |
2652 | if (!cached_module.empty()) | |
2653 | { | |
2654 | int fd = open(cached_module.c_str(), O_RDONLY); | |
2655 | if (fd != -1) | |
2656 | { | |
2657 | if (s.verbose > 2) | |
2658 | clog << "Pass 2: using cached " << cached_module << endl; | |
2659 | module = cached_module; | |
2660 | close(fd); | |
2661 | return; | |
2662 | } | |
2663 | } | |
2664 | } | |
2665 | ||
2666 | // no cached module, time to make it | |
d90053e7 | 2667 | if (make_typequery(s, module) == 0) |
fb0274bc | 2668 | { |
fb0274bc JS |
2669 | if (s.use_cache) |
2670 | { | |
2671 | // try to save typequery in the cache | |
2672 | if (s.verbose > 2) | |
d90053e7 | 2673 | clog << "Copying " << module |
fb0274bc | 2674 | << " to " << cached_module << endl; |
d90053e7 | 2675 | if (copy_file(module.c_str(), |
fb0274bc | 2676 | cached_module.c_str()) != 0) |
d90053e7 | 2677 | cerr << "Copy failed (\"" << module << "\" to \"" |
fb0274bc JS |
2678 | << cached_module << "\"): " << strerror(errno) << endl; |
2679 | } | |
2680 | } | |
2681 | } | |
2682 | } | |
2683 | ||
2684 | ||
c4ce66a1 JS |
2685 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
2686 | { | |
2687 | bool lvalue = is_active_lvalue(e); | |
2688 | if (lvalue && !s.guru_mode) | |
2689 | throw semantic_error("write to typecast value not permitted", e->tok); | |
2690 | ||
2691 | if (e->module.empty()) | |
2692 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
2693 | ||
c4ce66a1 JS |
2694 | string code; |
2695 | exp_type type = pe_long; | |
8b31197b JS |
2696 | |
2697 | // split the module string by ':' for alternatives | |
2698 | vector<string> modules; | |
2699 | tokenize(e->module, modules, ":"); | |
2700 | for (unsigned i = 0; code.empty() && i < modules.size(); ++i) | |
c4ce66a1 | 2701 | { |
8b31197b | 2702 | string& module = modules[i]; |
fb0274bc | 2703 | filter_special_modules(module); |
abb41d92 | 2704 | |
c4ce66a1 JS |
2705 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
2706 | // which means that userspace modules won't get any PATH searching. | |
2707 | dwflpp* dw; | |
707bf35e JS |
2708 | try |
2709 | { | |
2710 | if (module.find('/') == string::npos) | |
2711 | { | |
2712 | // kernel or kernel module target | |
ae2552da | 2713 | dw = db.get_kern_dw(s, module); |
707bf35e JS |
2714 | } |
2715 | else | |
2716 | { | |
2717 | module = find_executable (module); // canonicalize it | |
2718 | dw = db.get_user_dw(s, module); | |
2719 | } | |
2720 | } | |
2721 | catch (const semantic_error& er) | |
2722 | { | |
2723 | /* ignore and go to the next module */ | |
2724 | continue; | |
2725 | } | |
c4ce66a1 | 2726 | |
abb41d92 | 2727 | dwarf_cast_query q (*dw, module, *e, lvalue, type, code); |
51178501 | 2728 | dw->iterate_over_modules(&query_module, &q); |
c4ce66a1 | 2729 | } |
abb41d92 JS |
2730 | |
2731 | if (code.empty()) | |
c4ce66a1 | 2732 | { |
946e1a48 JS |
2733 | // We pass the unresolved cast_op to the next pass, and hope |
2734 | // that this value ends up not being referenced after all, so | |
2735 | // it can be optimized out quietly. | |
c4ce66a1 JS |
2736 | provide (e); |
2737 | return; | |
2738 | } | |
2739 | ||
2740 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") | |
2741 | + "_" + e->base_name.substr(1) | |
2742 | + "_" + lex_cast<string>(tick++)); | |
2743 | ||
2744 | // Synthesize a function. | |
2745 | functiondecl *fdecl = new functiondecl; | |
2746 | fdecl->tok = e->tok; | |
2747 | fdecl->type = type; | |
2748 | fdecl->name = fname; | |
2749 | ||
2750 | embeddedcode *ec = new embeddedcode; | |
2751 | ec->tok = e->tok; | |
2752 | ec->code = code; | |
2753 | fdecl->body = ec; | |
2754 | ||
2755 | // Give the fdecl an argument for the pointer we're trying to cast | |
2756 | vardecl *v1 = new vardecl; | |
2757 | v1->type = pe_long; | |
2758 | v1->name = "pointer"; | |
2759 | v1->tok = e->tok; | |
2760 | fdecl->formal_args.push_back(v1); | |
2761 | ||
2762 | if (lvalue) | |
2763 | { | |
2764 | // Modify the fdecl so it carries a second pe_long formal | |
2765 | // argument called "value". | |
2766 | ||
2767 | // FIXME: For the time being we only support setting target | |
2768 | // variables which have base types; these are 'pe_long' in | |
2769 | // stap's type vocabulary. Strings and pointers might be | |
2770 | // reasonable, some day, but not today. | |
2771 | ||
2772 | vardecl *v2 = new vardecl; | |
2773 | v2->type = pe_long; | |
2774 | v2->name = "value"; | |
2775 | v2->tok = e->tok; | |
2776 | fdecl->formal_args.push_back(v2); | |
2777 | } | |
2778 | else | |
2779 | ec->code += "/* pure */"; | |
2780 | ||
2781 | s.functions[fdecl->name] = fdecl; | |
2782 | ||
2783 | // Synthesize a functioncall. | |
2784 | functioncall* n = new functioncall; | |
2785 | n->tok = e->tok; | |
2786 | n->function = fname; | |
2787 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
2788 | n->args.push_back(e->operand); | |
2789 | ||
2790 | if (lvalue) | |
2791 | { | |
2792 | // Provide the functioncall to our parent, so that it can be | |
2793 | // used to substitute for the assignment node immediately above | |
2794 | // us. | |
2795 | assert(!target_symbol_setter_functioncalls.empty()); | |
2796 | *(target_symbol_setter_functioncalls.top()) = n; | |
2797 | } | |
2798 | ||
2799 | provide (n); | |
77de5e9e GH |
2800 | } |
2801 | ||
2802 | ||
b8da0ad1 FCE |
2803 | void |
2804 | dwarf_derived_probe::printsig (ostream& o) const | |
2805 | { | |
2806 | // Instead of just printing the plain locations, we add a PC value | |
2807 | // as a comment as a way of telling e.g. apart multiple inlined | |
2808 | // function instances. This is distinct from the verbose/clog | |
2809 | // output, since this part goes into the cache hash calculations. | |
2810 | sole_location()->print (o); | |
6d0f3f0c | 2811 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
2812 | printsig_nested (o); |
2813 | } | |
2814 | ||
2815 | ||
2816 | ||
dc38c0ae | 2817 | void |
b20febf3 FCE |
2818 | dwarf_derived_probe::join_group (systemtap_session& s) |
2819 | { | |
2820 | if (! s.dwarf_derived_probes) | |
2821 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
2822 | s.dwarf_derived_probes->enroll (this); | |
2823 | } | |
2824 | ||
2825 | ||
2826 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, | |
2827 | const string& filename, | |
2828 | int line, | |
91af0778 | 2829 | // module & section specify a relocation |
b20febf3 FCE |
2830 | // base for <addr>, unless section=="" |
2831 | // (equivalently module=="kernel") | |
2832 | const string& module, | |
2833 | const string& section, | |
2834 | // NB: dwfl_addr is the virtualized | |
2835 | // address for this symbol. | |
2836 | Dwarf_Addr dwfl_addr, | |
2837 | // addr is the section-offset for | |
2838 | // actual relocation. | |
2839 | Dwarf_Addr addr, | |
2840 | dwarf_query& q, | |
37ebca01 | 2841 | Dwarf_Die* scope_die /* may be null */) |
1939ea32 | 2842 | : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ), |
b20febf3 | 2843 | module (module), section (section), addr (addr), |
c9bad430 DS |
2844 | has_return (q.has_return), |
2845 | has_maxactive (q.has_maxactive), | |
2846 | maxactive_val (q.maxactive_val) | |
bd2b1e68 | 2847 | { |
b20febf3 | 2848 | // Assert relocation invariants |
4baf0e53 | 2849 | if (section == "" && dwfl_addr != addr) // addr should be absolute |
84048984 FCE |
2850 | throw semantic_error ("missing relocation base against", q.base_loc->tok); |
2851 | if (section != "" && dwfl_addr == addr) // addr should be an offset | |
b20febf3 | 2852 | throw semantic_error ("inconsistent relocation address", q.base_loc->tok); |
df8fadee | 2853 | |
b20febf3 | 2854 | this->tok = q.base_probe->tok; |
b95e2b79 | 2855 | this->access_vars = false; |
2930abc7 | 2856 | |
21beacc9 FCE |
2857 | // XXX: hack for strange g++/gcc's |
2858 | #ifndef USHRT_MAX | |
2859 | #define USHRT_MAX 32767 | |
2860 | #endif | |
2861 | ||
606fd9c8 FCE |
2862 | // Range limit maxactive() value |
2863 | if (q.has_maxactive && (q.maxactive_val < 0 || q.maxactive_val > USHRT_MAX)) | |
2864 | throw semantic_error ("maxactive value out of range [0," | |
2865 | + lex_cast<string>(USHRT_MAX) + "]", | |
2866 | q.base_loc->tok); | |
2867 | ||
de688825 | 2868 | // Expand target variables in the probe body |
5f0a03a6 | 2869 | if (!null_die(scope_die)) |
8fc05e57 | 2870 | { |
de688825 | 2871 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
4ed05b15 | 2872 | this->body = v.require (this->body); |
b95e2b79 | 2873 | this->access_vars = v.visited; |
37ebca01 FCE |
2874 | |
2875 | // If during target-variable-expanding the probe, we added a new block | |
2876 | // of code, add it to the start of the probe. | |
2877 | if (v.add_block) | |
ba6f838d | 2878 | this->body = new block(v.add_block, this->body); |
37ebca01 FCE |
2879 | // If when target-variable-expanding the probe, we added a new |
2880 | // probe, add it in a new file to the list of files to be processed. | |
2881 | if (v.add_probe) | |
2882 | { | |
2883 | stapfile *f = new stapfile; | |
2884 | f->probes.push_back(v.add_probe); | |
2885 | q.sess.files.push_back(f); | |
2886 | } | |
8fc05e57 | 2887 | } |
37ebca01 | 2888 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 2889 | |
5d23847d | 2890 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
2891 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
2892 | // point. This allows a user to see what function / file / line | |
2893 | // number any particular match of the wildcards. | |
2930abc7 | 2894 | |
a229fcd7 | 2895 | vector<probe_point::component*> comps; |
91af0778 FCE |
2896 | if (q.has_kernel) |
2897 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
2898 | else if(q.has_module) | |
2899 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
2900 | else if(q.has_process) | |
2901 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
2902 | else | |
2903 | assert (0); | |
b5d77020 | 2904 | |
db520b00 FCE |
2905 | string fn_or_stmt; |
2906 | if (q.has_function_str || q.has_function_num) | |
2907 | fn_or_stmt = "function"; | |
2908 | else | |
2909 | fn_or_stmt = "statement"; | |
a229fcd7 | 2910 | |
b8da0ad1 | 2911 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 2912 | { |
4cd232e4 | 2913 | string retro_name = funcname; |
b20febf3 | 2914 | if (filename != "") |
cee35f73 | 2915 | { |
fb84c077 | 2916 | retro_name += ("@" + string (filename)); |
cee35f73 | 2917 | if (line > 0) |
fb84c077 | 2918 | retro_name += (":" + lex_cast<string> (line)); |
cee35f73 | 2919 | } |
db520b00 FCE |
2920 | comps.push_back |
2921 | (new probe_point::component | |
2922 | (fn_or_stmt, new literal_string (retro_name))); | |
2923 | } | |
b8da0ad1 | 2924 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
2925 | { |
2926 | Dwarf_Addr retro_addr; | |
2927 | if (q.has_function_num) | |
2928 | retro_addr = q.function_num_val; | |
2929 | else | |
2930 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
2931 | comps.push_back (new probe_point::component |
2932 | (fn_or_stmt, | |
2933 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
37ebca01 FCE |
2934 | |
2935 | if (q.has_absolute) | |
2936 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
2937 | } |
2938 | ||
b8da0ad1 FCE |
2939 | if (q.has_call) |
2940 | comps.push_back (new probe_point::component(TOK_CALL)); | |
2941 | if (q.has_inline) | |
2942 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 2943 | if (has_return) |
b8da0ad1 FCE |
2944 | comps.push_back (new probe_point::component(TOK_RETURN)); |
2945 | if (has_maxactive) | |
2946 | comps.push_back (new probe_point::component | |
2947 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 2948 | |
5d23847d FCE |
2949 | // Overwrite it. |
2950 | this->sole_location()->components = comps; | |
2930abc7 FCE |
2951 | } |
2952 | ||
bd2b1e68 | 2953 | |
7a053d3b | 2954 | void |
20c6c071 | 2955 | dwarf_derived_probe::register_statement_variants(match_node * root, |
bd2b1e68 GH |
2956 | dwarf_builder * dw) |
2957 | { | |
54efe513 | 2958 | root->bind(dw); |
54efe513 GH |
2959 | } |
2960 | ||
7a053d3b | 2961 | void |
fd6602a0 | 2962 | dwarf_derived_probe::register_function_variants(match_node * root, |
c9bad430 | 2963 | dwarf_builder * dw) |
bd2b1e68 | 2964 | { |
fd6602a0 | 2965 | root->bind(dw); |
b8da0ad1 FCE |
2966 | root->bind(TOK_INLINE)->bind(dw); |
2967 | root->bind(TOK_CALL)->bind(dw); | |
fd6602a0 | 2968 | root->bind(TOK_RETURN)->bind(dw); |
c9bad430 | 2969 | root->bind(TOK_RETURN)->bind_num(TOK_MAXACTIVE)->bind(dw); |
bd2b1e68 GH |
2970 | } |
2971 | ||
7a053d3b | 2972 | void |
20c6c071 | 2973 | dwarf_derived_probe::register_function_and_statement_variants(match_node * root, |
bd2b1e68 GH |
2974 | dwarf_builder * dw) |
2975 | { | |
2976 | // Here we match 4 forms: | |
2977 | // | |
2978 | // .function("foo") | |
2979 | // .function(0xdeadbeef) | |
2980 | // .statement("foo") | |
2981 | // .statement(0xdeadbeef) | |
2982 | ||
fd6602a0 FCE |
2983 | register_function_variants(root->bind_str(TOK_FUNCTION), dw); |
2984 | register_function_variants(root->bind_num(TOK_FUNCTION), dw); | |
20c6c071 GH |
2985 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw); |
2986 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw); | |
bd2b1e68 GH |
2987 | } |
2988 | ||
2989 | void | |
c4ce66a1 | 2990 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 2991 | { |
c4ce66a1 | 2992 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
2993 | dwarf_builder *dw = new dwarf_builder(); |
2994 | ||
c4ce66a1 JS |
2995 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
2996 | s.code_filters.push_back(filter); | |
2997 | ||
20c6c071 GH |
2998 | register_function_and_statement_variants(root->bind(TOK_KERNEL), dw); |
2999 | register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw); | |
37ebca01 | 3000 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(dw); |
0f336e95 SC |
3001 | root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); |
3002 | root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); | |
37ebca01 | 3003 | |
7a24d422 | 3004 | register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw); |
f28a8c28 SC |
3005 | root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)->bind(dw); |
3006 | root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK)->bind(dw); | |
bd2b1e68 GH |
3007 | } |
3008 | ||
9020300d FCE |
3009 | void |
3010 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
3011 | { | |
b95e2b79 MH |
3012 | if (access_vars) |
3013 | { | |
3014 | // if accessing $variables, emit bsp cache setup for speeding up | |
3015 | o->newline() << "bspcache(c->unwaddr, c->regs);"; | |
3016 | } | |
9020300d | 3017 | } |
2930abc7 | 3018 | |
b20febf3 | 3019 | // ------------------------------------------------------------------------ |
46b84a80 DS |
3020 | |
3021 | void | |
b20febf3 | 3022 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 3023 | { |
b20febf3 | 3024 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
3025 | |
3026 | // XXX: probes put at the same address should all share a | |
3027 | // single kprobe/kretprobe, and have their handlers executed | |
3028 | // sequentially. | |
b55bc428 FCE |
3029 | } |
3030 | ||
7a053d3b | 3031 | void |
775d51e5 | 3032 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 3033 | { |
b20febf3 | 3034 | if (probes_by_module.empty()) return; |
2930abc7 | 3035 | |
775d51e5 DS |
3036 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
3037 | ||
3038 | // Warn of misconfigured kernels | |
f41595cc FCE |
3039 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
3040 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
3041 | s.op->newline() << "#endif"; | |
775d51e5 | 3042 | s.op->newline(); |
f41595cc | 3043 | |
f07c3b68 FCE |
3044 | s.op->newline() << "#ifndef KRETACTIVE"; |
3045 | s.op->newline() << "#define KRETACTIVE (max(15,6*NR_CPUS))"; | |
3046 | s.op->newline() << "#endif"; | |
3047 | ||
b20febf3 FCE |
3048 | // Forward declare the master entry functions |
3049 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
3050 | s.op->line() << " struct pt_regs *regs);"; | |
3051 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
3052 | s.op->line() << " struct pt_regs *regs);"; | |
3053 | ||
42cb22bd MH |
3054 | // Emit an array of kprobe/kretprobe pointers |
3055 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
3056 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
3057 | s.op->newline() << "#endif"; | |
3058 | ||
b20febf3 | 3059 | // Emit the actual probe list. |
606fd9c8 FCE |
3060 | |
3061 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
3062 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
3063 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
4c2732a1 | 3064 | s.op->newline() << "static struct stap_dwarf_kprobe {"; |
b20febf3 | 3065 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; |
e4cb375f MH |
3066 | s.op->newline() << "#ifdef __ia64__"; |
3067 | s.op->newline() << "struct kprobe dummy;"; | |
3068 | s.op->newline() << "#endif"; | |
606fd9c8 FCE |
3069 | s.op->newline(-1) << "} stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
3070 | // NB: bss! | |
3071 | ||
4c2732a1 | 3072 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
3073 | s.op->newline(1) << "const unsigned return_p:1;"; |
3074 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 3075 | s.op->newline() << "const unsigned optional_p:1;"; |
b20febf3 | 3076 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 3077 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 FCE |
3078 | |
3079 | // Let's find some stats for the three embedded strings. Maybe they | |
3080 | // are small and uniform enough to justify putting char[MAX]'s into | |
3081 | // the array instead of relocated char*'s. | |
3082 | size_t module_name_max = 0, section_name_max = 0, pp_name_max = 0; | |
3083 | size_t module_name_tot = 0, section_name_tot = 0, pp_name_tot = 0; | |
3084 | size_t all_name_cnt = probes_by_module.size(); // for average | |
3085 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
3086 | { | |
3087 | dwarf_derived_probe* p = it->second; | |
3088 | #define DOIT(var,expr) do { \ | |
3089 | size_t var##_size = (expr) + 1; \ | |
3090 | var##_max = max (var##_max, var##_size); \ | |
3091 | var##_tot += var##_size; } while (0) | |
3092 | DOIT(module_name, p->module.size()); | |
3093 | DOIT(section_name, p->section.size()); | |
3094 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
3095 | #undef DOIT | |
3096 | } | |
3097 | ||
3098 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
3099 | // the amount of average waste (max - avg) to the relocation data size | |
3100 | // (3 native long words). | |
3101 | #define CALCIT(var) \ | |
3102 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
3103 | { \ | |
3104 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
3105 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
3106 | << "[" << var##_name_max << "]" << endl; \ | |
3107 | } \ | |
3108 | else \ | |
3109 | { \ | |
b0986e7a | 3110 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
3111 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
3112 | } | |
3113 | ||
3114 | CALCIT(module); | |
3115 | CALCIT(section); | |
3116 | CALCIT(pp); | |
e6fe60e7 | 3117 | #undef CALCIT |
606fd9c8 | 3118 | |
b0986e7a DS |
3119 | s.op->newline() << "const unsigned long address;"; |
3120 | s.op->newline() << "void (* const ph) (struct context*);"; | |
b20febf3 FCE |
3121 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
3122 | s.op->indent(1); | |
3123 | ||
3124 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 3125 | { |
b20febf3 FCE |
3126 | dwarf_derived_probe* p = it->second; |
3127 | s.op->newline() << "{"; | |
3128 | if (p->has_return) | |
3129 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 3130 | if (p->has_maxactive) |
606fd9c8 FCE |
3131 | { |
3132 | s.op->line() << " .maxactive_p=1,"; | |
3133 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
3134 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
3135 | } | |
b350f56b JS |
3136 | if (p->locations[0]->optional) |
3137 | s.op->line() << " .optional_p=1,"; | |
dc38c256 | 3138 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
3139 | s.op->line() << " .module=\"" << p->module << "\","; |
3140 | s.op->line() << " .section=\"" << p->section << "\","; | |
b20febf3 FCE |
3141 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
3142 | s.op->line() << " .ph=&" << p->name; | |
3143 | s.op->line() << " },"; | |
2930abc7 | 3144 | } |
2930abc7 | 3145 | |
b20febf3 FCE |
3146 | s.op->newline(-1) << "};"; |
3147 | ||
3148 | // Emit the kprobes callback function | |
3149 | s.op->newline(); | |
3150 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
3151 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
3152 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
3153 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
3154 | // Check that the index is plausible | |
3155 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
3156 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
3157 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
3158 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
3159 | s.op->line() << "];"; | |
c12d974f | 3160 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
3161 | s.op->newline() << "c->regs = regs;"; |
3162 | s.op->newline() << "(*sdp->ph) (c);"; | |
3163 | common_probe_entryfn_epilogue (s.op); | |
3164 | s.op->newline() << "return 0;"; | |
3165 | s.op->newline(-1) << "}"; | |
3166 | ||
3167 | // Same for kretprobes | |
3168 | s.op->newline(); | |
3169 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
3170 | s.op->line() << " struct pt_regs *regs) {"; | |
3171 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
606fd9c8 FCE |
3172 | |
3173 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 3174 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
3175 | // Check that the index is plausible |
3176 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
3177 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
3178 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
3179 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
3180 | s.op->line() << "];"; | |
3181 | ||
c12d974f | 3182 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
3183 | s.op->newline() << "c->regs = regs;"; |
3184 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
3185 | s.op->newline() << "(*sdp->ph) (c);"; | |
3186 | common_probe_entryfn_epilogue (s.op); | |
3187 | s.op->newline() << "return 0;"; | |
3188 | s.op->newline(-1) << "}"; | |
20c6c071 | 3189 | } |
ec4373ff | 3190 | |
20c6c071 | 3191 | |
dc38c0ae | 3192 | void |
b20febf3 FCE |
3193 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
3194 | { | |
3195 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
3196 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 3197 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
f1bad60c | 3198 | s.op->newline() << "unsigned long relocated_addr = _stp_module_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 3199 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
6d0f3f0c | 3200 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
b20febf3 | 3201 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 3202 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 3203 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 3204 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 3205 | s.op->newline(-1) << "} else {"; |
f07c3b68 | 3206 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
c9bad430 | 3207 | s.op->newline(-1) << "}"; |
606fd9c8 | 3208 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
e4cb375f MH |
3209 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
3210 | s.op->newline() << "#ifdef __ia64__"; | |
3211 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
3212 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
3213 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
3214 | s.op->newline() << "if (rc == 0) {"; | |
3215 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
3216 | s.op->newline() << "if (rc != 0)"; | |
3217 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
3218 | s.op->newline(-2) << "}"; | |
3219 | s.op->newline() << "#else"; | |
606fd9c8 | 3220 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 3221 | s.op->newline() << "#endif"; |
b20febf3 | 3222 | s.op->newline(-1) << "} else {"; |
e4cb375f | 3223 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
3224 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
3225 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
3226 | s.op->newline() << "#ifdef __ia64__"; |
3227 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
3228 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
3229 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
3230 | s.op->newline() << "if (rc == 0) {"; | |
3231 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
3232 | s.op->newline() << "if (rc != 0)"; | |
3233 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
3234 | s.op->newline(-2) << "}"; | |
3235 | s.op->newline() << "#else"; | |
606fd9c8 | 3236 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 3237 | s.op->newline() << "#endif"; |
b20febf3 | 3238 | s.op->newline(-1) << "}"; |
9063462a FCE |
3239 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
3240 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 3241 | s.op->newline() << "if (!sdp->optional_p)"; |
4a4edc21 | 3242 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, relocated_addr, rc);"; |
b350f56b | 3243 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
9063462a FCE |
3244 | // XXX: shall we increment numskipped? |
3245 | s.op->newline(-1) << "}"; | |
3246 | ||
3247 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 3248 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 3249 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
3250 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
3251 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
3252 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
3253 | s.op->newline() << "#ifdef __ia64__"; |
3254 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
3255 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
3256 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
3257 | // not run for this early-abort case. | |
3258 | s.op->newline(-1) << "}"; | |
3259 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 3260 | s.op->newline(-1) << "}"; |
9063462a FCE |
3261 | #endif |
3262 | ||
b20febf3 FCE |
3263 | s.op->newline() << "else sdp->registered_p = 1;"; |
3264 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
3265 | } |
3266 | ||
3267 | ||
46b84a80 | 3268 | void |
b20febf3 | 3269 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 3270 | { |
42cb22bd MH |
3271 | //Unregister kprobes by batch interfaces. |
3272 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
3273 | s.op->newline() << "j = 0;"; | |
3274 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
3275 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
3276 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
3277 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
3278 | s.op->newline() << "if (!sdp->return_p)"; | |
3279 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
3280 | s.op->newline(-2) << "}"; | |
3281 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
3282 | s.op->newline() << "j = 0;"; | |
3283 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
3284 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
3285 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
3286 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
3287 | s.op->newline() << "if (sdp->return_p)"; | |
3288 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
3289 | s.op->newline(-2) << "}"; | |
3290 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
3291 | s.op->newline() << "#ifdef __ia64__"; |
3292 | s.op->newline() << "j = 0;"; | |
3293 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
3294 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
3295 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
3296 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
3297 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
3298 | s.op->newline(-1) << "}"; | |
3299 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
3300 | s.op->newline() << "#endif"; | |
42cb22bd MH |
3301 | s.op->newline() << "#endif"; |
3302 | ||
b20febf3 FCE |
3303 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
3304 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 3305 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
3306 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
3307 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 3308 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 3309 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 3310 | s.op->newline() << "#endif"; |
606fd9c8 | 3311 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; |
73209876 FCE |
3312 | s.op->newline() << "#ifdef STP_TIMING"; |
3313 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
d01eaa30 | 3314 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; |
73209876 | 3315 | s.op->newline(-1) << "#endif"; |
606fd9c8 | 3316 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; |
73209876 FCE |
3317 | s.op->newline() << "#ifdef STP_TIMING"; |
3318 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
d01eaa30 | 3319 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; |
73209876 | 3320 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 3321 | s.op->newline(-1) << "} else {"; |
42cb22bd | 3322 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 3323 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 3324 | s.op->newline() << "#endif"; |
606fd9c8 | 3325 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; |
73209876 FCE |
3326 | s.op->newline() << "#ifdef STP_TIMING"; |
3327 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
d01eaa30 | 3328 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);"; |
73209876 | 3329 | s.op->newline(-1) << "#endif"; |
b20febf3 | 3330 | s.op->newline(-1) << "}"; |
e4cb375f MH |
3331 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
3332 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
3333 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
3334 | s.op->newline() << "sdp->registered_p = 0;"; |
3335 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
3336 | } |
3337 | ||
7a05f484 SC |
3338 | struct sdt_var_expanding_visitor: public var_expanding_visitor |
3339 | { | |
9f02b156 | 3340 | sdt_var_expanding_visitor(string & process_name, string & probe_name, |
0c3bfb1e | 3341 | int arg_count, bool have_reg_args, bool utrace_probe): |
9f02b156 | 3342 | process_name (process_name), probe_name (probe_name), |
0c3bfb1e SC |
3343 | have_reg_args (have_reg_args), utrace_probe (utrace_probe), |
3344 | arg_count (arg_count) | |
a8ec7719 JS |
3345 | { |
3346 | assert(!have_reg_args || (arg_count >= 0 && arg_count <= 10)); | |
3347 | } | |
56e33af5 SC |
3348 | string & process_name; |
3349 | string & probe_name; | |
7a05f484 | 3350 | bool have_reg_args; |
0c3bfb1e | 3351 | bool utrace_probe; |
7a05f484 SC |
3352 | int arg_count; |
3353 | ||
3354 | void visit_target_symbol (target_symbol* e); | |
3355 | }; | |
3356 | ||
3357 | void | |
3358 | sdt_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
3359 | { | |
3360 | if (e->base_name == "$$name") | |
3361 | { | |
03c75a4a JS |
3362 | if (e->addressof) |
3363 | throw semantic_error("cannot take address of sdt variable", e->tok); | |
3364 | ||
7a05f484 SC |
3365 | literal_string *myname = new literal_string (probe_name); |
3366 | myname->tok = e->tok; | |
3367 | provide(myname); | |
3368 | return; | |
3369 | } | |
3370 | else if (e->base_name.find("$arg") == string::npos || ! have_reg_args) | |
3371 | { | |
3372 | provide(e); | |
3373 | return; | |
3374 | } | |
a8ec7719 JS |
3375 | |
3376 | int argno = lex_cast<int>(e->base_name.substr(4)); | |
3377 | if (argno < 1 || argno > arg_count) | |
3378 | throw semantic_error ("invalid argument number", e->tok); | |
3379 | ||
7a05f484 | 3380 | bool lvalue = is_active_lvalue(e); |
7a05f484 SC |
3381 | functioncall *fc = new functioncall; |
3382 | ||
5111fc3e SC |
3383 | // First two args are hidden: 1. pointer to probe name 2. task id |
3384 | if (arg_count < 2) | |
7a05f484 | 3385 | { |
0c3bfb1e | 3386 | fc->function = utrace_probe ? "_utrace_syscall_arg" : "ulong_arg"; |
7a05f484 SC |
3387 | fc->type = pe_long; |
3388 | fc->tok = e->tok; | |
0c3bfb1e | 3389 | literal_number* num = new literal_number(argno + (utrace_probe ? 1 : 2)); |
7a05f484 SC |
3390 | num->tok = e->tok; |
3391 | fc->args.push_back(num); | |
3392 | } | |
3393 | else // args passed as a struct | |
3394 | { | |
3395 | fc->function = "user_long"; | |
3396 | fc->tok = e->tok; | |
3397 | binary_expression *be = new binary_expression; | |
3398 | be->tok = e->tok; | |
3399 | functioncall *get_arg1 = new functioncall; | |
0c3bfb1e | 3400 | get_arg1->function = utrace_probe ? "_utrace_syscall_arg" : "pointer_arg"; |
7a05f484 | 3401 | get_arg1->tok = e->tok; |
0c3bfb1e | 3402 | literal_number* num = new literal_number((utrace_probe ? 2 : 3)); |
7a05f484 SC |
3403 | num->tok = e->tok; |
3404 | get_arg1->args.push_back(num); | |
3405 | ||
3406 | be->left = get_arg1; | |
3407 | be->op = "+"; | |
3408 | literal_number* inc = new literal_number((argno - 1) * 8); | |
3409 | be->right = inc; | |
3410 | fc->args.push_back(be); | |
3411 | } | |
ad002306 | 3412 | |
7a05f484 SC |
3413 | if (lvalue) |
3414 | *(target_symbol_setter_functioncalls.top()) = fc; | |
3415 | ||
ad002306 SC |
3416 | if (e->components.empty()) |
3417 | { | |
03c75a4a JS |
3418 | if (e->addressof) |
3419 | throw semantic_error("cannot take address of sdt variable", e->tok); | |
3420 | ||
ad002306 SC |
3421 | provide(fc); |
3422 | return; | |
3423 | } | |
7a05f484 SC |
3424 | cast_op *cast = new cast_op; |
3425 | cast->base_name = "@cast"; | |
3426 | cast->tok = e->tok; | |
3427 | cast->operand = fc; | |
3428 | cast->components = e->components; | |
ad002306 | 3429 | cast->type = probe_name + "_arg" + lex_cast<string>(argno); |
56e33af5 | 3430 | cast->module = process_name; |
7a05f484 | 3431 | |
ad002306 | 3432 | provide(cast); |
7a05f484 | 3433 | } |
46b84a80 | 3434 | |
20c6c071 | 3435 | void |
5227f1ea | 3436 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 3437 | probe * base, |
20c6c071 | 3438 | probe_point * location, |
86bf665e | 3439 | literal_map_t const & parameters, |
20c6c071 GH |
3440 | vector<derived_probe *> & finished_results) |
3441 | { | |
b20febf3 FCE |
3442 | // NB: the kernel/user dwlfpp objects are long-lived. |
3443 | // XXX: but they should be per-session, as this builder object | |
3444 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 3445 | |
7a24d422 FCE |
3446 | dwflpp* dw = 0; |
3447 | ||
7a24d422 | 3448 | string module_name; |
ae2552da FCE |
3449 | if (has_null_param (parameters, TOK_KERNEL)) |
3450 | { | |
3451 | dw = get_kern_dw(sess, "kernel"); | |
3452 | } | |
3453 | else if (get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 3454 | { |
ae2552da | 3455 | dw = get_kern_dw(sess, module_name); |
b8da0ad1 | 3456 | } |
7a24d422 | 3457 | else if (get_param (parameters, TOK_PROCESS, module_name)) |
b8da0ad1 | 3458 | { |
d0a7f5a9 FCE |
3459 | module_name = find_executable (module_name); // canonicalize it |
3460 | ||
7a24d422 FCE |
3461 | // user-space target; we use one dwflpp instance per module name |
3462 | // (= program or shared library) | |
707bf35e | 3463 | dw = get_user_dw(sess, module_name); |
c8959a29 | 3464 | } |
20c6c071 | 3465 | |
5896cd05 MW |
3466 | if (sess.verbose > 3) |
3467 | clog << "dwarf_builder::build for " << module_name << endl; | |
3468 | ||
3e1e31fb JS |
3469 | string mark_name; |
3470 | if (get_param(parameters, TOK_MARK, mark_name)) | |
7a05f484 | 3471 | { |
59d00eb0 | 3472 | probe_table probe_table(mark_name, sess, dw); |
7a05f484 SC |
3473 | if (! probe_table.get_next_probe()) |
3474 | return; | |
ad002306 | 3475 | |
7a05f484 SC |
3476 | if (sess.verbose > 3) |
3477 | clog << "TOK_MARK: " << probe_table.mark_name << endl; | |
01a419e4 | 3478 | |
0c3bfb1e | 3479 | if (probe_table.is_uprobe()) |
7a05f484 SC |
3480 | { |
3481 | do | |
3482 | { | |
ad002306 SC |
3483 | probe *new_base = new probe; |
3484 | *new_base = *base; | |
3485 | ||
3486 | new_base->body = deep_copy_visitor::deep_copy(base->body); | |
3487 | probe_point *new_location = new probe_point; | |
3488 | *new_location = *location; | |
3489 | new_base->locations.clear(); | |
3490 | ||
3491 | const token* sv_tok = new_location->components[1]->arg->tok; | |
3492 | new_location->components[1]->functor = TOK_STATEMENT; | |
3493 | new_location->components[1]->arg = new literal_number((long)probe_table.probe_arg); | |
3494 | new_location->components[1]->arg->tok = sv_tok; | |
3495 | ((literal_map_t&)parameters)[TOK_STATEMENT] = new_location->components[1]->arg; | |
3496 | new_base->locations.push_back(new_location); | |
7a05f484 SC |
3497 | |
3498 | if (sess.verbose > 3) | |
3499 | clog << "probe_type == use_uprobe, use statement addr: 0x" | |
3500 | << hex << probe_table.probe_arg << dec << endl; | |
ad002306 | 3501 | |
7a05f484 | 3502 | // Now expand the local variables in the probe body |
fc4a80b8 | 3503 | sdt_var_expanding_visitor svv (module_name, probe_table.probe_name, |
0c3bfb1e | 3504 | probe_table.probe_arg, false, false); |
ad002306 | 3505 | new_base->body = svv.require (new_base->body); |
7a05f484 | 3506 | |
ad002306 | 3507 | dwarf_query q(sess, new_base, new_location, *dw, parameters, finished_results); |
7a05f484 SC |
3508 | q.has_mark = true; |
3509 | dw->iterate_over_modules(&query_module, &q); | |
3510 | if (sess.listing_mode) | |
3511 | { | |
3512 | finished_results.back()->locations[0]->components[1]->functor = TOK_MARK; | |
3513 | finished_results.back()->locations[0]->components[1]->arg = new literal_string (probe_table.probe_name.c_str()); | |
3514 | } | |
3515 | } | |
3516 | while (probe_table.get_next_probe()); | |
3517 | return; | |
3518 | } | |
59b2ec52 | 3519 | |
5111fc3e SC |
3520 | else if (probe_table.probe_type == probe_table.kprobe_type |
3521 | || probe_table.probe_type == probe_table.utrace_type) | |
7a05f484 SC |
3522 | { |
3523 | do | |
3524 | { | |
611ab826 | 3525 | set<string>::iterator pb; |
de7c5583 | 3526 | pb = probes_handled.find(probe_table.probe_name); |
611ab826 | 3527 | if (pb == probes_handled.end()) |
de7c5583 | 3528 | probes_handled.insert (probe_table.probe_name); |
611ab826 SC |
3529 | else |
3530 | return; | |
3531 | ||
ad002306 SC |
3532 | probe *new_base = new probe; |
3533 | *new_base = *base; | |
3534 | ||
3535 | new_base->body = deep_copy_visitor::deep_copy(base->body); | |
3536 | probe_point *new_location = new probe_point; | |
3537 | *new_location = *location; | |
3538 | new_base->locations.clear(); | |
3539 | ||
5111fc3e | 3540 | probe_table.convert_probe(new_base); |
7a05f484 | 3541 | |
5111fc3e | 3542 | // Expand the local variables in the probe body |
fc4a80b8 | 3543 | sdt_var_expanding_visitor svv (module_name, probe_table.probe_name, |
0c3bfb1e SC |
3544 | probe_table.probe_arg, true, |
3545 | probe_table.is_utrace()); | |
ad002306 | 3546 | new_base->body = svv.require (new_base->body); |
5111fc3e | 3547 | probe_table.convert_location(new_base, new_location); |
ad002306 | 3548 | derive_probes(sess, new_base, finished_results); |
de7c5583 SC |
3549 | if (sess.listing_mode) |
3550 | { | |
3551 | finished_results.back()->locations[0]->components[0]->functor = TOK_FUNCTION; | |
3552 | finished_results.back()->locations[0]->components[0]->arg = new literal_string (module_name); | |
3553 | finished_results.back()->locations[0]->components[1]->functor = TOK_MARK; | |
3554 | finished_results.back()->locations[0]->components[1]->arg = new literal_string (probe_table.probe_name.c_str()); | |
3555 | } | |
7a05f484 SC |
3556 | } |
3557 | while (probe_table.get_next_probe()); | |
3558 | return; | |
3559 | } | |
1f0cfd98 | 3560 | |
7a05f484 SC |
3561 | else |
3562 | { | |
3563 | location->components[1]->functor = TOK_FUNCTION; | |
3564 | location->components[1]->arg = new literal_string("*"); | |
3565 | ((literal_map_t&)parameters)[TOK_FUNCTION] = location->components[1]->arg; | |
3566 | location->components.push_back(new probe_point::component(TOK_LABEL)); | |
3567 | location->components[2]->arg = new literal_string("_stapprobe1_" + probe_table.mark_name); | |
3568 | ((literal_map_t&)parameters).erase(TOK_MARK); | |
3569 | ((literal_map_t&)parameters).insert(pair<string,literal*>(TOK_LABEL, location->components[2]->arg)); | |
3570 | ||
3571 | if (sess.verbose > 3) | |
3572 | clog << "probe_type == use_uprobe_no_dwarf, use label name: " | |
3573 | << "_stapprobe1_" << probe_table.mark_name << endl; | |
3574 | } | |
5896cd05 | 3575 | |
7a05f484 SC |
3576 | dw->module = 0; |
3577 | } | |
ad002306 | 3578 | |
c8959a29 | 3579 | dwarf_query q(sess, base, location, *dw, parameters, finished_results); |
20c6c071 | 3580 | |
7a24d422 FCE |
3581 | |
3582 | // XXX: kernel.statement.absolute is a special case that requires no | |
3583 | // dwfl processing. This code should be in a separate builder. | |
3584 | ||
3585 | if (q.has_kernel && q.has_absolute) | |
37ebca01 | 3586 | { |
4baf0e53 | 3587 | // assert guru mode for absolute probes |
37ebca01 FCE |
3588 | if (! q.base_probe->privileged) |
3589 | { | |
3590 | throw semantic_error ("absolute statement probe in unprivileged script", q.base_probe->tok); | |
3591 | } | |
3592 | ||
3593 | // For kernel.statement(NUM).absolute probe points, we bypass | |
3594 | // all the debuginfo stuff: We just wire up a | |
3595 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 3596 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
3597 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
3598 | q.statement_num_val, q.statement_num_val, | |
3599 | q, 0); | |
37ebca01 | 3600 | finished_results.push_back (p); |
1a0dbc5a | 3601 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
3602 | return; |
3603 | } | |
3604 | ||
51178501 | 3605 | dw->iterate_over_modules(&query_module, &q); |
5f0a03a6 JK |
3606 | } |
3607 | ||
3608 | symbol_table::~symbol_table() | |
3609 | { | |
1c6b77e5 JS |
3610 | for (iterator_t i = map_by_addr.begin(); i != map_by_addr.end(); ++i) |
3611 | delete i->second; | |
5f0a03a6 JK |
3612 | } |
3613 | ||
3614 | void | |
ab91b232 JK |
3615 | symbol_table::add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
3616 | Dwarf_Addr *high_addr) | |
5f0a03a6 | 3617 | { |
ab91b232 JK |
3618 | #ifdef __powerpc__ |
3619 | // Map ".sys_foo" to "sys_foo". | |
3620 | if (name[0] == '.') | |
3621 | name++; | |
3622 | #endif | |
5f0a03a6 JK |
3623 | func_info *fi = new func_info(); |
3624 | fi->addr = addr; | |
3625 | fi->name = name; | |
ab91b232 | 3626 | fi->weak = weak; |
5f0a03a6 JK |
3627 | map_by_name[fi->name] = fi; |
3628 | // TODO: Use a multimap in case there are multiple static | |
3629 | // functions with the same name? | |
1c6b77e5 | 3630 | map_by_addr.insert(make_pair(addr, fi)); |
5f0a03a6 JK |
3631 | } |
3632 | ||
3633 | enum info_status | |
3634 | symbol_table::read_symbols(FILE *f, const string& path) | |
3635 | { | |
3636 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
3637 | int ret; | |
2e67a43b TM |
3638 | char *name = 0; |
3639 | char *mod = 0; | |
5f0a03a6 JK |
3640 | char type; |
3641 | unsigned long long addr; | |
3642 | Dwarf_Addr high_addr = 0; | |
3643 | int line = 0; | |
3644 | ||
3645 | // %as (non-POSIX) mallocs space for the string and stores its address. | |
3646 | while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0) | |
3647 | { | |
2e67a43b TM |
3648 | auto_free free_name(name); |
3649 | auto_free free_mod(mod); | |
5f0a03a6 JK |
3650 | line++; |
3651 | if (ret < 3) | |
3652 | { | |
41c262f3 | 3653 | cerr << "Symbol table error: Line " |
5f0a03a6 JK |
3654 | << line |
3655 | << " of symbol list from " | |
3656 | << path | |
3657 | << " is not in correct format: address type name [module]"; | |
3658 | // Caller should delete symbol_table object. | |
3659 | return info_absent; | |
3660 | } | |
2e67a43b | 3661 | else if (ret > 3) |
5f0a03a6 JK |
3662 | { |
3663 | // Modules are loaded above the kernel, so if we're getting | |
3664 | // modules, we're done. | |
2e67a43b | 3665 | break; |
5f0a03a6 | 3666 | } |
ab91b232 JK |
3667 | if (type == 'T' || type == 't' || type == 'W') |
3668 | add_symbol(name, (type == 'W'), (Dwarf_Addr) addr, &high_addr); | |
5f0a03a6 JK |
3669 | } |
3670 | ||
1c6b77e5 | 3671 | if (map_by_addr.size() < 1) |
5f0a03a6 JK |
3672 | { |
3673 | cerr << "Symbol table error: " | |
3674 | << path << " contains no function symbols." << endl; | |
3675 | return info_absent; | |
3676 | } | |
3677 | return info_present; | |
3678 | } | |
3679 | ||
3680 | // NB: This currently unused. We use get_from_elf() instead because | |
3681 | // that gives us raw addresses -- which we need for modules -- whereas | |
3682 | // nm provides the address relative to the beginning of the section. | |
3683 | enum info_status | |
3684 | symbol_table::read_from_elf_file(const string &path) | |
3685 | { | |
3686 | FILE *f; | |
3687 | string cmd = string("/usr/bin/nm -n --defined-only ") + path; | |
3688 | f = popen(cmd.c_str(), "r"); | |
3689 | if (!f) | |
3690 | { | |
3691 | // nm failures are detected by pclose, not popen. | |
3692 | cerr << "Internal error reading symbol table from " | |
3693 | << path << " -- " << strerror (errno); | |
3694 | return info_absent; | |
3695 | } | |
3696 | enum info_status status = read_symbols(f, path); | |
3697 | if (pclose(f) != 0) | |
3698 | { | |
3699 | if (status == info_present) | |
3700 | cerr << "Warning: nm cannot read symbol table from " << path; | |
3701 | return info_absent; | |
3702 | } | |
3703 | return status; | |
3704 | } | |
3705 | ||
3706 | enum info_status | |
3707 | symbol_table::read_from_text_file(const string& path) | |
3708 | { | |
3709 | FILE *f = fopen(path.c_str(), "r"); | |
3710 | if (!f) | |
3711 | { | |
3712 | cerr << "Warning: cannot read symbol table from " | |
3713 | << path << " -- " << strerror (errno); | |
3714 | return info_absent; | |
3715 | } | |
3716 | enum info_status status = read_symbols(f, path); | |
3717 | (void) fclose(f); | |
3718 | return status; | |
3719 | } | |
3720 | ||
46f7b6be JK |
3721 | void |
3722 | symbol_table::prepare_section_rejection(Dwfl_Module *mod) | |
3723 | { | |
3724 | #ifdef __powerpc__ | |
3725 | /* | |
3726 | * The .opd section contains function descriptors that can look | |
3727 | * just like function entry points. For example, there's a function | |
3728 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
3729 | * Reject all symbols in .opd. | |
3730 | */ | |
3731 | opd_section = SHN_UNDEF; | |
3732 | Dwarf_Addr bias; | |
3733 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
3734 | ?: dwfl_module_getelf (mod, &bias)); | |
3735 | Elf_Scn* scn = 0; | |
3736 | size_t shstrndx; | |
3737 | ||
3738 | if (!elf) | |
3739 | return; | |
fcc30d6d | 3740 | if (elf_getshdrstrndx (elf, &shstrndx) != 0) |
46f7b6be JK |
3741 | return; |
3742 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
3743 | { | |
3744 | GElf_Shdr shdr_mem; | |
3745 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
3746 | if (!shdr) | |
3747 | continue; | |
3748 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
3749 | if (!strcmp(name, ".opd")) | |
3750 | { | |
3751 | opd_section = elf_ndxscn(scn); | |
3752 | return; | |
3753 | } | |
3754 | } | |
3755 | #endif | |
3756 | } | |
3757 | ||
3758 | bool | |
3759 | symbol_table::reject_section(GElf_Word section) | |
3760 | { | |
3761 | if (section == SHN_UNDEF) | |
3762 | return true; | |
3763 | #ifdef __powerpc__ | |
3764 | if (section == opd_section) | |
3765 | return true; | |
3766 | #endif | |
3767 | return false; | |
3768 | } | |
3769 | ||
5f0a03a6 JK |
3770 | enum info_status |
3771 | symbol_table::get_from_elf() | |
3772 | { | |
3773 | Dwarf_Addr high_addr = 0; | |
3774 | Dwfl_Module *mod = mod_info->mod; | |
3775 | int syments = dwfl_module_getsymtab(mod); | |
3776 | assert(syments); | |
46f7b6be | 3777 | prepare_section_rejection(mod); |
5f0a03a6 JK |
3778 | for (int i = 1; i < syments; ++i) |
3779 | { | |
3780 | GElf_Sym sym; | |
ab91b232 JK |
3781 | GElf_Word section; |
3782 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
46f7b6be JK |
3783 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC && |
3784 | !reject_section(section)) | |
ab91b232 JK |
3785 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
3786 | sym.st_value, &high_addr); | |
5f0a03a6 JK |
3787 | } |
3788 | return info_present; | |
3789 | } | |
3790 | ||
5f0a03a6 JK |
3791 | func_info * |
3792 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
3793 | { | |
1c6b77e5 JS |
3794 | iterator_t iter = map_by_addr.upper_bound(addr); |
3795 | if (iter == map_by_addr.begin()) | |
5f0a03a6 | 3796 | return NULL; |
2e67a43b | 3797 | else |
1c6b77e5 | 3798 | return (--iter)->second; |
5f0a03a6 JK |
3799 | } |
3800 | ||
3801 | func_info * | |
3802 | symbol_table::lookup_symbol(const string& name) | |
3803 | { | |
3804 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
3805 | if (i == map_by_name.end()) | |
3806 | return NULL; | |
3807 | return i->second; | |
3808 | } | |
3809 | ||
3810 | Dwarf_Addr | |
3811 | symbol_table::lookup_symbol_address(const string& name) | |
3812 | { | |
3813 | func_info *fi = lookup_symbol(name); | |
3814 | if (fi) | |
3815 | return fi->addr; | |
3816 | return 0; | |
3817 | } | |
3818 | ||
ab91b232 JK |
3819 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
3820 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
3821 | // For system calls not implemented elsewhere, this weak symbol shows up | |
3822 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
3823 | // we refuse to consider such symbols. Here we delete them from our | |
3824 | // symbol table. | |
3825 | // TODO: Consider generalizing this and/or making it part of blacklist | |
3826 | // processing. | |
3827 | void | |
3828 | symbol_table::purge_syscall_stubs() | |
3829 | { | |
3830 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
3831 | if (stub_addr == 0) | |
3832 | return; | |
1c6b77e5 | 3833 | range_t purge_range = map_by_addr.equal_range(stub_addr); |
2e67a43b TM |
3834 | for (iterator_t iter = purge_range.first; |
3835 | iter != purge_range.second; | |
1c6b77e5 | 3836 | ) |
ab91b232 | 3837 | { |
1c6b77e5 | 3838 | func_info *fi = iter->second; |
2e67a43b | 3839 | if (fi->weak && fi->name != "sys_ni_syscall") |
ab91b232 | 3840 | { |
2e67a43b | 3841 | map_by_name.erase(fi->name); |
1c6b77e5 | 3842 | map_by_addr.erase(iter++); |
2e67a43b | 3843 | delete fi; |
2e67a43b | 3844 | } |
1c6b77e5 JS |
3845 | else |
3846 | iter++; | |
ab91b232 JK |
3847 | } |
3848 | } | |
3849 | ||
5f0a03a6 JK |
3850 | void |
3851 | module_info::get_symtab(dwarf_query *q) | |
3852 | { | |
3853 | systemtap_session &sess = q->sess; | |
3854 | ||
1c6b77e5 JS |
3855 | if (symtab_status != info_unknown) |
3856 | return; | |
3857 | ||
5f0a03a6 JK |
3858 | sym_table = new symbol_table(this); |
3859 | if (!elf_path.empty()) | |
3860 | { | |
3861 | if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()) | |
3862 | cerr << "Warning: reading symbol table from " | |
3863 | << elf_path | |
3864 | << " -- ignoring " | |
3865 | << sess.kernel_symtab_path | |
3866 | << endl ;; | |
3867 | symtab_status = sym_table->get_from_elf(); | |
3868 | } | |
3869 | else | |
3870 | { | |
3871 | assert(name == TOK_KERNEL); | |
3872 | if (sess.kernel_symtab_path.empty()) | |
3873 | { | |
3874 | symtab_status = info_absent; | |
3875 | cerr << "Error: Cannot find vmlinux." | |
3876 | << " Consider using --kmap instead of --kelf." | |
3877 | << endl;; | |
3878 | } | |
3879 | else | |
3880 | { | |
3881 | symtab_status = | |
3882 | sym_table->read_from_text_file(sess.kernel_symtab_path); | |
3883 | if (symtab_status == info_present) | |
3884 | { | |
3885 | sess.sym_kprobes_text_start = | |
3886 | sym_table->lookup_symbol_address("__kprobes_text_start"); | |
3887 | sess.sym_kprobes_text_end = | |
3888 | sym_table->lookup_symbol_address("__kprobes_text_end"); | |
3889 | sess.sym_stext = sym_table->lookup_symbol_address("_stext"); | |
5f0a03a6 JK |
3890 | } |
3891 | } | |
3892 | } | |
3893 | if (symtab_status == info_absent) | |
3894 | { | |
3895 | delete sym_table; | |
3896 | sym_table = NULL; | |
3897 | return; | |
3898 | } | |
3899 | ||
ab91b232 JK |
3900 | if (name == TOK_KERNEL) |
3901 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
3902 | } |
3903 | ||
1c6b77e5 JS |
3904 | // update_symtab reconciles data between the elf symbol table and the dwarf |
3905 | // function enumeration. It updates the symbol table entries with the dwarf | |
3906 | // die that describes the function, which also signals to query_module_symtab | |
3907 | // that a statement probe isn't needed. In return, it also adds aliases to the | |
3908 | // function table for names that share the same addr/die. | |
3909 | void | |
3910 | module_info::update_symtab(cu_function_cache_t *funcs) | |
3911 | { | |
3912 | if (!sym_table) | |
3913 | return; | |
3914 | ||
3915 | cu_function_cache_t new_funcs; | |
3916 | ||
3917 | for (cu_function_cache_t::iterator func = funcs->begin(); | |
3918 | func != funcs->end(); func++) | |
3919 | { | |
3920 | // optimization: inlines will never be in the symbol table | |
3921 | if (dwarf_func_inline(&func->second) != 0) | |
3922 | continue; | |
3923 | ||
3924 | func_info *fi = sym_table->lookup_symbol(func->first); | |
3925 | if (!fi) | |
3926 | continue; | |
3927 | ||
3928 | // iterate over all functions at the same address | |
3929 | symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr); | |
3930 | for (symbol_table::iterator_t it = er.first; it != er.second; ++it) | |
3931 | { | |
3932 | // update this function with the dwarf die | |
3933 | it->second->die = func->second; | |
3934 | ||
3935 | // if this function is a new alias, then | |
3936 | // save it to merge into the function cache | |
3937 | if (it->second != fi) | |
3938 | new_funcs[it->second->name] = it->second->die; | |
3939 | } | |
3940 | } | |
3941 | ||
3942 | // add all discovered aliases back into the function cache | |
3943 | // NB: this won't replace any names that dwarf may have already found | |
3944 | funcs->insert(new_funcs.begin(), new_funcs.end()); | |
3945 | } | |
3946 | ||
5f0a03a6 JK |
3947 | module_info::~module_info() |
3948 | { | |
3949 | if (sym_table) | |
3950 | delete sym_table; | |
b55bc428 FCE |
3951 | } |
3952 | ||
888af770 FCE |
3953 | // ------------------------------------------------------------------------ |
3954 | // user-space probes | |
3955 | // ------------------------------------------------------------------------ | |
3956 | ||
888af770 FCE |
3957 | |
3958 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> | |
3959 | { | |
3960 | public: | |
3961 | void emit_module_decls (systemtap_session& s); | |
3962 | void emit_module_init (systemtap_session& s); | |
3963 | void emit_module_exit (systemtap_session& s); | |
3964 | }; | |
3965 | ||
3966 | ||
6d0f3f0c FCE |
3967 | uprobe_derived_probe::uprobe_derived_probe (const string& function, |
3968 | const string& filename, | |
3969 | int line, | |
3970 | const string& module, | |
3971 | int pid, | |
3972 | const string& section, | |
3973 | Dwarf_Addr dwfl_addr, | |
3974 | Dwarf_Addr addr, | |
3975 | dwarf_query & q, | |
3976 | Dwarf_Die* scope_die /* may be null */): | |
3977 | derived_probe (q.base_probe, new probe_point (*q.base_loc) /* .components soon rewritten */ ), | |
3978 | return_p (q.has_return), module (module), pid (pid), section (section), address (addr) | |
4baf0e53 | 3979 | { |
17c128f2 FCE |
3980 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. |
3981 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
3982 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
3983 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
6d0f3f0c FCE |
3984 | |
3985 | this->tok = q.base_probe->tok; | |
3986 | ||
de688825 | 3987 | // Expand target variables in the probe body |
6d0f3f0c FCE |
3988 | if (!null_die(scope_die)) |
3989 | { | |
de688825 | 3990 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); // XXX: user-space deref's! |
4ed05b15 | 3991 | this->body = v.require (this->body); |
6d0f3f0c FCE |
3992 | |
3993 | // If during target-variable-expanding the probe, we added a new block | |
3994 | // of code, add it to the start of the probe. | |
3995 | if (v.add_block) | |
3996 | this->body = new block(v.add_block, this->body); | |
3997 | ||
3998 | // If when target-variable-expanding the probe, we added a new | |
3999 | // probe, add it in a new file to the list of files to be processed. | |
4000 | if (v.add_probe) | |
4001 | { | |
4002 | stapfile *f = new stapfile; | |
4003 | f->probes.push_back(v.add_probe); | |
4004 | q.sess.files.push_back(f); | |
4005 | } | |
4006 | } | |
4007 | // else - null scope_die - $target variables will produce an error during translate phase | |
4008 | ||
4009 | // Reset the sole element of the "locations" vector as a | |
4010 | // "reverse-engineered" form of the incoming (q.base_loc) probe | |
4011 | // point. This allows a user to see what function / file / line | |
4012 | // number any particular match of the wildcards. | |
4013 | ||
4014 | vector<probe_point::component*> comps; | |
4015 | if(q.has_process) | |
4016 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
4017 | else | |
4018 | assert (0); | |
06aca46a | 4019 | |
6d0f3f0c FCE |
4020 | string fn_or_stmt; |
4021 | if (q.has_function_str || q.has_function_num) | |
4022 | fn_or_stmt = "function"; | |
4023 | else | |
4024 | fn_or_stmt = "statement"; | |
4025 | ||
4026 | if (q.has_function_str || q.has_statement_str) | |
4027 | { | |
4028 | string retro_name = function; | |
4029 | if (filename != "") | |
fb84c077 FCE |
4030 | { |
4031 | retro_name += ("@" + string (filename)); | |
4032 | if (line > 0) | |
4033 | retro_name += (":" + lex_cast<string> (line)); | |
4034 | } | |
6d0f3f0c FCE |
4035 | comps.push_back |
4036 | (new probe_point::component | |
4037 | (fn_or_stmt, new literal_string (retro_name))); | |
4038 | } | |
4039 | else if (q.has_function_num || q.has_statement_num) | |
4040 | { | |
4041 | Dwarf_Addr retro_addr; | |
4042 | if (q.has_function_num) | |
4043 | retro_addr = q.function_num_val; | |
4044 | else | |
4045 | retro_addr = q.statement_num_val; | |
4046 | comps.push_back (new probe_point::component | |
4047 | (fn_or_stmt, | |
4048 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
4049 | ||
4050 | if (q.has_absolute) | |
4051 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
4052 | } | |
4053 | ||
4054 | if (q.has_call) | |
4055 | comps.push_back (new probe_point::component(TOK_CALL)); | |
4056 | if (q.has_inline) | |
4057 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
4058 | if (return_p) | |
4059 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
4060 | /* | |
4061 | if (has_maxactive) | |
4062 | comps.push_back (new probe_point::component | |
4063 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
4064 | */ | |
4065 | ||
4066 | // Overwrite it. | |
4067 | this->sole_location()->components = comps; | |
4068 | } | |
4069 | ||
4070 | ||
0973d815 FCE |
4071 | uprobe_derived_probe::uprobe_derived_probe (probe *base, |
4072 | probe_point *location, | |
4073 | int pid, | |
4074 | Dwarf_Addr addr, | |
4075 | bool has_return): | |
4076 | derived_probe (base, location), // location is not rewritten here | |
4077 | return_p (has_return), pid (pid), address (addr) | |
4078 | { | |
4079 | } | |
4080 | ||
4081 | ||
6d0f3f0c FCE |
4082 | void |
4083 | uprobe_derived_probe::printsig (ostream& o) const | |
4084 | { | |
4085 | // Same as dwarf_derived_probe. | |
4086 | sole_location()->print (o); | |
4087 | o << " /* pc=" << section << "+0x" << hex << address << dec << " */"; | |
4088 | printsig_nested (o); | |
888af770 FCE |
4089 | } |
4090 | ||
4091 | ||
4092 | void | |
4093 | uprobe_derived_probe::join_group (systemtap_session& s) | |
4094 | { | |
4095 | if (! s.uprobe_derived_probes) | |
4096 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
4097 | s.uprobe_derived_probes->enroll (this); | |
93646f4d | 4098 | enable_task_finder(s); |
8a03658e JS |
4099 | |
4100 | // Ask buildrun.cxx to build extra module if needed, and | |
4101 | // signal staprun to load that module | |
4102 | s.need_uprobes = true; | |
888af770 FCE |
4103 | } |
4104 | ||
4105 | ||
4106 | struct uprobe_builder: public derived_probe_builder | |
4107 | { | |
4108 | uprobe_builder() {} | |
4109 | virtual void build(systemtap_session & sess, | |
4110 | probe * base, | |
4111 | probe_point * location, | |
86bf665e | 4112 | literal_map_t const & parameters, |
888af770 FCE |
4113 | vector<derived_probe *> & finished_results) |
4114 | { | |
4115 | int64_t process, address; | |
4116 | ||
4117 | bool b1 = get_param (parameters, TOK_PROCESS, process); | |
ced347a9 | 4118 | (void) b1; |
888af770 | 4119 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 4120 | (void) b2; |
888af770 FCE |
4121 | bool rr = has_null_param (parameters, TOK_RETURN); |
4122 | assert (b1 && b2); // by pattern_root construction | |
4baf0e53 | 4123 | |
0973d815 | 4124 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
888af770 FCE |
4125 | } |
4126 | }; | |
4127 | ||
4128 | ||
4129 | void | |
775d51e5 | 4130 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) |
888af770 FCE |
4131 | { |
4132 | if (probes.empty()) return; | |
775d51e5 | 4133 | s.op->newline() << "/* ---- user probes ---- */"; |
4baf0e53 | 4134 | |
c480bf3d | 4135 | // If uprobes isn't in the kernel, pull it in from the runtime. |
6274464e | 4136 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; |
888af770 | 4137 | s.op->newline() << "#include <linux/uprobes.h>"; |
c480bf3d | 4138 | s.op->newline() << "#else"; |
6274464e | 4139 | s.op->newline() << "#include \"uprobes/uprobes.h\""; |
c480bf3d | 4140 | s.op->newline() << "#endif"; |
9d451878 JK |
4141 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; |
4142 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
4143 | s.op->newline() << "#endif"; | |
6d0f3f0c | 4144 | |
01b05e2e | 4145 | s.op->newline() << "#ifndef MULTIPLE_UPROBES"; |
478c850f FCE |
4146 | s.op->newline() << "#define MULTIPLE_UPROBES 256"; // maximum possible armed uprobes per process() probe point |
4147 | // or apprx. max number of processes mapping a shared library | |
01b05e2e | 4148 | s.op->newline() << "#endif"; |
6d0f3f0c | 4149 | s.op->newline() << "#ifndef MAXUPROBES"; |
01b05e2e | 4150 | s.op->newline() << "#define MAXUPROBES (MULTIPLE_UPROBES * " << probes.size() << ")"; |
6d0f3f0c | 4151 | s.op->newline() << "#endif"; |
888af770 | 4152 | |
5e112f92 FCE |
4153 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
4154 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
4c2732a1 | 4155 | s.op->newline() << "static struct stap_uprobe {"; |
888af770 | 4156 | s.op->newline(1) << "union { struct uprobe up; struct uretprobe urp; };"; |
6d0f3f0c | 4157 | s.op->newline() << "int spec_index;"; // index into stap_uprobe_specs; <0 == free && unregistered |
01b05e2e | 4158 | s.op->newline(-1) << "} stap_uprobes [MAXUPROBES];"; |
5e112f92 | 4159 | s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
6d0f3f0c | 4160 | |
c9a05b1c | 4161 | // Emit vma callbacks. |
dd163639 | 4162 | s.op->newline() << "#ifdef STP_NEED_VMA_TRACKER"; |
c9a05b1c MW |
4163 | s.op->newline() << "static struct stap_task_finder_target stap_uprobe_vmcbs[] = {"; |
4164 | s.op->indent(1); | |
4165 | for (unsigned i = 0; i < probes.size(); i++) | |
4166 | { | |
4167 | uprobe_derived_probe* p = probes[i]; | |
4168 | if (p->pid != 0) | |
4169 | emit_vma_callback_probe_decl (s, "", p->pid); | |
4170 | else | |
4171 | emit_vma_callback_probe_decl (s, p->module, (int64_t)0); | |
4172 | } | |
4173 | s.op->newline(-1) << "};"; | |
dd163639 | 4174 | s.op->newline() << "#endif"; |
c9a05b1c | 4175 | |
4c2732a1 | 4176 | s.op->newline() << "static struct stap_uprobe_spec {"; |
5e112f92 | 4177 | s.op->newline(1) << "struct stap_task_finder_target finder;"; |
888af770 | 4178 | s.op->newline() << "unsigned long address;"; |
17c128f2 | 4179 | s.op->newline() << "const char *pathname;"; |
888af770 FCE |
4180 | s.op->newline() << "const char *pp;"; |
4181 | s.op->newline() << "void (*ph) (struct context*);"; | |
6d0f3f0c FCE |
4182 | s.op->newline() << "unsigned return_p:1;"; |
4183 | s.op->newline(-1) << "} stap_uprobe_specs [] = {"; | |
888af770 FCE |
4184 | s.op->indent(1); |
4185 | for (unsigned i =0; i<probes.size(); i++) | |
4186 | { | |
4187 | uprobe_derived_probe* p = probes[i]; | |
4188 | s.op->newline() << "{"; | |
0973d815 FCE |
4189 | s.op->line() << " .finder = {"; |
4190 | if (p->pid != 0) | |
17c128f2 FCE |
4191 | s.op->line() << " .pid=" << p->pid; |
4192 | else if (p->section == ".absolute") | |
0973d815 | 4193 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; |
06aca46a | 4194 | // else ".dynamic" gets pathname=0, pid=0, activating task_finder "global tracing" |
0973d815 | 4195 | s.op->line() << "},"; |
17c128f2 FCE |
4196 | if (p->section != ".absolute") |
4197 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
dc38c256 | 4198 | s.op->line() << " .address=(unsigned long)0x" << hex << p->address << dec << "ULL,"; |
888af770 FCE |
4199 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
4200 | s.op->line() << " .ph=&" << p->name << ","; | |
4201 | if (p->return_p) s.op->line() << " .return_p=1,"; | |
4202 | s.op->line() << " },"; | |
4203 | } | |
4204 | s.op->newline(-1) << "};"; | |
4205 | ||
48e685da | 4206 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 4207 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
6d0f3f0c | 4208 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 4209 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
4210 | s.op->newline() << "if (sup->spec_index < 0 ||" |
4211 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 | 4212 | s.op->newline() << "c->regs = regs;"; |
6d0f3f0c | 4213 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
4214 | common_probe_entryfn_epilogue (s.op); |
4215 | s.op->newline(-1) << "}"; | |
4216 | ||
48e685da | 4217 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 4218 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
6d0f3f0c | 4219 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 4220 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
4221 | s.op->newline() << "if (sup->spec_index < 0 ||" |
4222 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 FCE |
4223 | // XXX: kretprobes saves "c->pi = inst;" too |
4224 | s.op->newline() << "c->regs = regs;"; | |
6d0f3f0c | 4225 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
4226 | common_probe_entryfn_epilogue (s.op); |
4227 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 4228 | |
17c128f2 FCE |
4229 | |
4230 | ||
6d0f3f0c FCE |
4231 | // NB: Because these utrace callbacks only occur before / after |
4232 | // userspace instructions run, there is no concurrency control issue | |
4233 | // between active uprobe callbacks and these registration / | |
06aca46a | 4234 | // unregistration pieces. |
6d0f3f0c FCE |
4235 | |
4236 | // We protect the stap_uprobe->spec_index (which also serves as a | |
d41d451c FCE |
4237 | // free/busy flag) value with the outer protective stap_probes_lock |
4238 | // spinlock, to protect it against concurrent registration / | |
4239 | // unregistration. | |
6d0f3f0c FCE |
4240 | |
4241 | s.op->newline(); | |
17c128f2 FCE |
4242 | s.op->newline() << "static int stap_uprobe_change (struct task_struct *tsk, int register_p, unsigned long relocation, struct stap_uprobe_spec *sups) {"; |
4243 | s.op->newline(1) << "int spec_index = (sups - stap_uprobe_specs);"; | |
6d0f3f0c | 4244 | s.op->newline() << "int handled_p = 0;"; |
d41d451c | 4245 | s.op->newline() << "int slotted_p = 0;"; |
6d0f3f0c FCE |
4246 | s.op->newline() << "int rc = 0;"; |
4247 | s.op->newline() << "int i;"; | |
3568f1dd | 4248 | |
d41d451c | 4249 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; |
01b05e2e | 4250 | s.op->newline() << "for (i=0; i<MAXUPROBES; i++) {"; // XXX: slow linear search |
5e112f92 | 4251 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; |
6d0f3f0c FCE |
4252 | |
4253 | // register new uprobe | |
4254 | s.op->newline() << "if (register_p && sup->spec_index < 0) {"; | |
9d451878 JK |
4255 | s.op->newline(1) << "#if (UPROBES_API_VERSION < 2)"; |
4256 | // See PR6829 comment. | |
4257 | s.op->newline() << "if (sup->spec_index == -1 && sup->up.kdata != NULL) continue;"; | |
80b4ad8b | 4258 | s.op->newline() << "else if (sup->spec_index == -2 && sup->urp.u.kdata != NULL) continue;"; |
9d451878 | 4259 | s.op->newline() << "#endif"; |
80b4ad8b | 4260 | s.op->newline() << "sup->spec_index = spec_index;"; |
d41d451c FCE |
4261 | s.op->newline() << "slotted_p = 1;"; |
4262 | s.op->newline() << "break;"; | |
4263 | s.op->newline(-1) << "} else if (!register_p && " | |
4264 | << "sup->spec_index == spec_index && " // a u[ret]probe set up for this probe point | |
4265 | << "((sups->return_p && sup->urp.u.pid == tsk->tgid && sup->urp.u.vaddr == relocation + sups->address) ||" // dying uretprobe | |
4266 | << "(!sups->return_p && sup->up.pid == tsk->tgid && sup->up.vaddr == relocation + sups->address))) {"; // dying uprobe | |
4267 | s.op->newline(1) << "slotted_p = 1;"; | |
4268 | s.op->newline() << "break;"; // exit to-free slot search | |
4269 | s.op->newline(-1) << "}"; | |
4270 | ||
4271 | s.op->newline(-1) << "}"; | |
4272 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; | |
4273 | ||
4274 | s.op->newline() << "#ifdef DEBUG_UPROBES"; | |
4275 | s.op->newline() << "printk (KERN_INFO \"%cuprobe spec %d idx %d process %s[%d] reloc %p pp '%s'\\n\", "; | |
4276 | s.op->line() << "(register_p ? '+' : '-'), spec_index, (slotted_p ? i : -1), tsk->comm, tsk->tgid, (void*) relocation, sups->pp);"; | |
4277 | s.op->newline() << "#endif"; | |
4278 | ||
4279 | // Here, slotted_p implies that `i' points to the single | |
4280 | // stap_uprobes[] element that has been slotted in for registration | |
4281 | // or unregistration processing. !slotted_p implies that the table | |
4282 | // was full (registration; MAXUPROBES) or that no matching entry was | |
4283 | // found (unregistration; should not happen). | |
4284 | ||
4285 | s.op->newline() << "if (register_p && slotted_p) {"; | |
4286 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
6d0f3f0c FCE |
4287 | s.op->newline() << "if (sups->return_p) {"; |
4288 | s.op->newline(1) << "sup->urp.u.pid = tsk->tgid;"; | |
17c128f2 | 4289 | s.op->newline() << "sup->urp.u.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
4290 | s.op->newline() << "sup->urp.handler = &enter_uretprobe_probe;"; |
4291 | s.op->newline() << "rc = register_uretprobe (& sup->urp);"; | |
4292 | s.op->newline(-1) << "} else {"; | |
4293 | s.op->newline(1) << "sup->up.pid = tsk->tgid;"; | |
17c128f2 | 4294 | s.op->newline() << "sup->up.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
4295 | s.op->newline() << "sup->up.handler = &enter_uprobe_probe;"; |
4296 | s.op->newline() << "rc = register_uprobe (& sup->up);"; | |
4297 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 4298 | s.op->newline() << "if (rc) {"; // failed to register |
d41d451c FCE |
4299 | s.op->newline(1) << "printk (KERN_WARNING \"uprobe failed %s[%d] '%s' addr %p rc %d\\n\", tsk->comm, tsk->tgid, sups->pp, (void*)(relocation + sups->address), rc);"; |
4300 | // NB: we need to release this slot, so we need to borrow the mutex temporarily. | |
4301 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; | |
3568f1dd | 4302 | s.op->newline() << "sup->spec_index = -1;"; |
d41d451c | 4303 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
6d0f3f0c FCE |
4304 | s.op->newline(-1) << "} else {"; |
4305 | s.op->newline(1) << "handled_p = 1;"; // success | |
4306 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 4307 | |
d41d451c FCE |
4308 | s.op->newline(-1) << "} else if (!register_p && slotted_p) {"; |
4309 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
9d451878 JK |
4310 | s.op->newline() << "int unregistered_flag;"; |
4311 | // PR6829, PR9940: | |
4312 | // Here we're unregistering for one of two reasons: | |
4313 | // 1. the process image is going away (or gone) due to exit or exec; or | |
4314 | // 2. the vma containing the probepoint has been unmapped. | |
4315 | // In case 1, it's sort of a nop, because uprobes will notice the event | |
4316 | // and dispose of the probes eventually, if it hasn't already. But by | |
4317 | // calling unmap_u[ret]probe() ourselves, we free up sup right away. | |
4318 | // | |
4319 | // In both cases, we must use unmap_u[ret]probe instead of | |
4320 | // unregister_u[ret]probe, so uprobes knows not to try to restore the | |
4321 | // original opcode. | |
4322 | s.op->newline() << "#if (UPROBES_API_VERSION >= 2)"; | |
4323 | s.op->newline() << "if (sups->return_p)"; | |
4324 | s.op->newline(1) << "unmap_uretprobe (& sup->urp);"; | |
4325 | s.op->newline(-1) << "else"; | |
4326 | s.op->newline(1) << "unmap_uprobe (& sup->up);"; | |
4327 | s.op->newline(-1) << "unregistered_flag = -1;"; | |
4328 | s.op->newline() << "#else"; | |
4329 | // Uprobes lacks unmap_u[ret]probe. Before reusing sup, we must wait | |
4330 | // until uprobes turns loose of the u[ret]probe on its own, as indicated | |
4331 | // by uprobe.kdata = NULL. | |
4332 | s.op->newline() << "unregistered_flag = (sups->return_p ? -2 : -1);"; | |
4333 | s.op->newline() << "#endif"; | |
d41d451c | 4334 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; |
9d451878 | 4335 | s.op->newline() << "sup->spec_index = unregistered_flag;"; |
5e112f92 | 4336 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
d41d451c FCE |
4337 | s.op->newline() << "handled_p = 1;"; |
4338 | s.op->newline(-1) << "}"; // if slotted_p | |
4339 | ||
4340 | // NB: handled_p implies slotted_p | |
4341 | ||
6d0f3f0c | 4342 | s.op->newline() << "if (! handled_p) {"; |
73209876 FCE |
4343 | s.op->newline(1) << "#ifdef STP_TIMING"; |
4344 | s.op->newline() << "atomic_inc (register_p ? & skipped_count_uprobe_reg : & skipped_count_uprobe_unreg);"; | |
4345 | s.op->newline() << "#endif"; | |
4346 | // NB: duplicates common_entryfn_epilogue, but then this is not a probe entry fn epilogue. | |
4347 | s.op->newline() << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
6d0f3f0c FCE |
4348 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; |
4349 | s.op->newline() << "_stp_exit ();"; | |
4350 | s.op->newline(-1) << "}"; | |
4351 | s.op->newline(-1) << "}"; | |
06aca46a | 4352 | |
6d0f3f0c FCE |
4353 | s.op->newline() << "return 0;"; // XXX: or rc? |
4354 | s.op->newline(-1) << "}"; | |
4355 | s.op->assert_0_indent(); | |
4356 | ||
17c128f2 FCE |
4357 | |
4358 | // The task_finder_callback we use for ET_EXEC targets. | |
4359 | s.op->newline(); | |
4360 | s.op->newline() << "static int stap_uprobe_process_found (struct stap_task_finder_target *tgt, struct task_struct *tsk, int register_p, int process_p) {"; | |
4361 | ||
4362 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; | |
4363 | s.op->newline() << "if (! process_p) return 0;"; | |
4364 | s.op->newline(0) << "return stap_uprobe_change (tsk, register_p, 0, sups);"; | |
4365 | s.op->newline(-1) << "}"; | |
4366 | ||
782040b3 | 4367 | // The task_finder_mmap_callback we use for ET_DYN targets. |
17c128f2 | 4368 | s.op->newline(); |
782040b3 | 4369 | s.op->newline() << "static int stap_uprobe_mmap_found (struct stap_task_finder_target *tgt, struct task_struct *tsk, char *path, unsigned long addr, unsigned long length, unsigned long offset, unsigned long vm_flags) {"; |
17c128f2 FCE |
4370 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; |
4371 | // 1 - shared libraries' executable segments load from offset 0 - ld.so convention | |
782040b3 | 4372 | s.op->newline() << "if (offset != 0) return 0;"; |
17c128f2 | 4373 | // 2 - the shared library we're interested in |
782040b3 | 4374 | s.op->newline() << "if (path == NULL || strcmp (path, sups->pathname)) return 0;"; |
17c128f2 | 4375 | // 3 - probe address within the mapping limits; test should not fail |
782040b3 DS |
4376 | s.op->newline() << "if (sups->address >= addr && sups->address < (addr + length)) return 0;"; |
4377 | // 4 - mapping should be executable | |
4378 | s.op->newline() << "if (!(vm_flags & VM_EXEC)) return 0;"; | |
06aca46a | 4379 | |
c16d425a | 4380 | s.op->newline() << "#ifdef DEBUG_TASK_FINDER_VMA"; |
782040b3 | 4381 | s.op->newline() << "printk (KERN_INFO \"vmchange pid %d path %s addr %p length %lu offset %p\\n\", tsk->tgid, path, (void *) addr, length, (void*) offset);"; |
c16d425a FCE |
4382 | s.op->newline() << "printk (KERN_INFO \"sups %p pp %s path %s address %p\\n\", sups, sups->pp, sups->pathname ?: \"\", (void*) sups->address);"; |
4383 | s.op->newline() << "#endif"; | |
4384 | ||
782040b3 | 4385 | s.op->newline(0) << "return stap_uprobe_change (tsk, 1, addr, sups);"; |
17c128f2 FCE |
4386 | s.op->newline(-1) << "}"; |
4387 | s.op->assert_0_indent(); | |
4388 | ||
4389 | ||
6d0f3f0c | 4390 | s.op->newline(); |
888af770 FCE |
4391 | } |
4392 | ||
4393 | ||
4394 | void | |
4395 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
4396 | { | |
4397 | if (probes.empty()) return; | |
dd163639 | 4398 | s.op->newline() << "#ifdef STP_NEED_VMA_TRACKER"; |
782040b3 | 4399 | s.op->newline() << "_stp_sym_init();"; |
c9a05b1c MW |
4400 | s.op->newline() << "/* ---- uprobe vma callbacks ---- */"; |
4401 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_uprobe_vmcbs); i++) {"; | |
4402 | s.op->indent(1); | |
4403 | s.op->newline() << "struct stap_task_finder_target *r = &stap_uprobe_vmcbs[i];"; | |
4404 | s.op->newline() << "rc = stap_register_task_finder_target(r);"; | |
4405 | s.op->newline(-1) << "}"; | |
dd163639 | 4406 | s.op->newline() << "#endif"; |
c9a05b1c | 4407 | |
5e112f92 FCE |
4408 | s.op->newline() << "/* ---- user probes ---- */"; |
4409 | ||
01b05e2e | 4410 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
4411 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
4412 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
4413 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
4414 | // initialized to zero. This is so that we can use | |
4415 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
4416 | s.op->newline(-1) << "}"; |
4417 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
888af770 FCE |
4418 | |
4419 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
6d0f3f0c FCE |
4420 | s.op->newline(1) << "struct stap_uprobe_spec *sups = & stap_uprobe_specs[i];"; |
4421 | s.op->newline() << "probe_point = sups->pp;"; // for error messages | |
17c128f2 | 4422 | s.op->newline() << "if (sups->finder.pathname) sups->finder.callback = & stap_uprobe_process_found;"; |
782040b3 | 4423 | s.op->newline() << "else if (sups->pathname) sups->finder.mmap_callback = & stap_uprobe_mmap_found;"; |
6d0f3f0c | 4424 | s.op->newline() << "rc = stap_register_task_finder_target (& sups->finder);"; |
5e112f92 FCE |
4425 | |
4426 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any | |
4427 | // finders already registered, since mere registration does not | |
4428 | // cause any utrace or memory allocation actions. That happens only | |
4429 | // later, once the task finder engine starts running. So, for a | |
4430 | // partial initialization requiring unwind, we need do nothing. | |
4431 | s.op->newline() << "if (rc) break;"; | |
4432 | ||
888af770 FCE |
4433 | s.op->newline(-1) << "}"; |
4434 | } | |
4435 | ||
4436 | ||
4437 | void | |
4438 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
4439 | { | |
4440 | if (probes.empty()) return; | |
4441 | s.op->newline() << "/* ---- user probes ---- */"; | |
4442 | ||
6d0f3f0c FCE |
4443 | // NB: there is no stap_unregister_task_finder_target call; |
4444 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
4445 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
4446 | // | |
4447 | // This function blocks until all callbacks are completed, so there | |
4448 | // is supposed to be no possibility of any registration-related code starting | |
4449 | // to run in parallel with our shutdown here. So we don't need to protect the | |
4450 | // stap_uprobes[] array with the mutex. | |
5e112f92 | 4451 | |
01b05e2e | 4452 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
4453 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
4454 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; | |
6d0f3f0c | 4455 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd FCE |
4456 | |
4457 | s.op->newline() << "if (sups->return_p) {"; | |
4458 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 4459 | s.op->newline() << "printk (KERN_INFO \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);"; |
3568f1dd | 4460 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
4461 | // NB: PR6829 does not change that we still need to unregister at |
4462 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
4463 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
4464 | s.op->newline(-1) << "} else {"; | |
4465 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 4466 | s.op->newline() << "printk (KERN_INFO \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->urp.u.pid, (void*) sup->urp.u.vaddr);"; |
3568f1dd FCE |
4467 | s.op->newline() << "#endif"; |
4468 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
4469 | s.op->newline(-1) << "}"; | |
4470 | ||
6d0f3f0c | 4471 | s.op->newline() << "sup->spec_index = -1;"; |
3568f1dd FCE |
4472 | |
4473 | // XXX: uprobe missed counts? | |
4474 | ||
6d0f3f0c | 4475 | s.op->newline(-1) << "}"; |
5e112f92 FCE |
4476 | |
4477 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; | |
888af770 FCE |
4478 | } |
4479 | ||
e6fe60e7 AM |
4480 | // ------------------------------------------------------------------------ |
4481 | // Kprobe derived probes | |
4482 | // ------------------------------------------------------------------------ | |
4483 | ||
4627ed58 | 4484 | static const string TOK_KPROBE("kprobe"); |
e6fe60e7 | 4485 | |
bae55db9 JS |
4486 | struct kprobe_derived_probe: public derived_probe |
4487 | { | |
4488 | kprobe_derived_probe (probe *base, | |
4489 | probe_point *location, | |
4490 | const string& name, | |
4491 | int64_t stmt_addr, | |
4492 | bool has_return, | |
4493 | bool has_statement, | |
4494 | bool has_maxactive, | |
4495 | long maxactive_val | |
4496 | ); | |
4497 | string symbol_name; | |
4498 | Dwarf_Addr addr; | |
4499 | bool has_return; | |
4500 | bool has_statement; | |
4501 | bool has_maxactive; | |
4502 | long maxactive_val; | |
4503 | bool access_var; | |
4504 | void printsig (std::ostream &o) const; | |
4505 | void join_group (systemtap_session& s); | |
4506 | }; | |
4507 | ||
4508 | struct kprobe_derived_probe_group: public derived_probe_group | |
4509 | { | |
4510 | private: | |
4511 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
4512 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
4513 | ||
4514 | public: | |
4515 | void enroll (kprobe_derived_probe* probe); | |
4516 | void emit_module_decls (systemtap_session& s); | |
4517 | void emit_module_init (systemtap_session& s); | |
4518 | void emit_module_exit (systemtap_session& s); | |
4519 | }; | |
4520 | ||
e6fe60e7 AM |
4521 | kprobe_derived_probe::kprobe_derived_probe (probe *base, |
4522 | probe_point *location, | |
b6371390 | 4523 | const string& name, |
e6fe60e7 | 4524 | int64_t stmt_addr, |
b6371390 JS |
4525 | bool has_return, |
4526 | bool has_statement, | |
4527 | bool has_maxactive, | |
4528 | long maxactive_val | |
4529 | ): | |
e6fe60e7 AM |
4530 | derived_probe (base, location), |
4531 | symbol_name (name), addr (stmt_addr), | |
b6371390 JS |
4532 | has_return (has_return), has_statement (has_statement), |
4533 | has_maxactive (has_maxactive), maxactive_val (maxactive_val) | |
e6fe60e7 AM |
4534 | { |
4535 | this->tok = base->tok; | |
4536 | this->access_var = false; | |
4537 | ||
4538 | #ifndef USHRT_MAX | |
4539 | #define USHRT_MAX 32767 | |
4540 | #endif | |
4541 | ||
46856d8d JS |
4542 | // Expansion of $target variables in the probe body produces an error during |
4543 | // translate phase, since we're not using debuginfo | |
4544 | ||
e6fe60e7 | 4545 | vector<probe_point::component*> comps; |
46856d8d | 4546 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 4547 | |
46856d8d JS |
4548 | if (has_statement) |
4549 | { | |
4550 | comps.push_back (new probe_point::component(TOK_STATEMENT, new literal_number(addr))); | |
4551 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); | |
4552 | } | |
4553 | else | |
4554 | { | |
4555 | size_t pos = name.find(':'); | |
4556 | if (pos != string::npos) | |
4557 | { | |
4558 | string module = name.substr(0, pos); | |
4559 | string function = name.substr(pos + 1); | |
4560 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
4561 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
4562 | } | |
4563 | else | |
4564 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 4565 | } |
888af770 | 4566 | |
b6371390 JS |
4567 | if (has_return) |
4568 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
4569 | if (has_maxactive) | |
4570 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
4571 | ||
e6fe60e7 AM |
4572 | this->sole_location()->components = comps; |
4573 | } | |
888af770 | 4574 | |
e6fe60e7 AM |
4575 | void kprobe_derived_probe::printsig (ostream& o) const |
4576 | { | |
4577 | sole_location()->print (o); | |
4578 | o << " /* " << " name = " << symbol_name << "*/"; | |
4579 | printsig_nested (o); | |
4580 | } | |
4581 | ||
4582 | void kprobe_derived_probe::join_group (systemtap_session& s) | |
4583 | { | |
4584 | ||
4585 | if (! s.kprobe_derived_probes) | |
4586 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
4587 | s.kprobe_derived_probes->enroll (this); | |
4588 | ||
4589 | } | |
4590 | ||
4591 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) | |
4592 | { | |
4593 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
4594 | // probes of same symbol should share single kprobe/kretprobe | |
4595 | } | |
4596 | ||
4597 | void | |
4598 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
4599 | { | |
4600 | if (probes_by_module.empty()) return; | |
4601 | ||
4602 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; | |
4603 | ||
4604 | // Warn of misconfigured kernels | |
4605 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
4606 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
4607 | s.op->newline() << "#endif"; | |
4608 | s.op->newline(); | |
4609 | ||
f07c3b68 FCE |
4610 | s.op->newline() << "#ifndef KRETACTIVE"; |
4611 | s.op->newline() << "#define KRETACTIVE (max(15,6*NR_CPUS))"; | |
4612 | s.op->newline() << "#endif"; | |
4613 | ||
e6fe60e7 | 4614 | // Forward declare the master entry functions |
88747011 | 4615 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 4616 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 4617 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
4618 | s.op->line() << " struct pt_regs *regs);"; |
4619 | ||
4620 | // Emit an array of kprobe/kretprobe pointers | |
4621 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
c9116e99 | 4622 | s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];"; |
e6fe60e7 AM |
4623 | s.op->newline() << "#endif"; |
4624 | ||
4625 | // Emit the actual probe list. | |
4626 | ||
4627 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; | |
4628 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
4629 | s.op->newline() << "#ifdef __ia64__"; | |
4630 | s.op->newline() << "struct kprobe dummy;"; | |
4631 | s.op->newline() << "#endif"; | |
4632 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
4633 | // NB: bss! | |
4634 | ||
4635 | s.op->newline() << "static struct stap_dwarfless_probe {"; | |
4636 | s.op->newline(1) << "const unsigned return_p:1;"; | |
4637 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 4638 | s.op->newline() << "const unsigned optional_p:1;"; |
e6fe60e7 AM |
4639 | s.op->newline() << "unsigned registered_p:1;"; |
4640 | s.op->newline() << "const unsigned short maxactive_val;"; | |
4641 | ||
4642 | // Function Names are mostly small and uniform enough to justify putting | |
4643 | // char[MAX]'s into the array instead of relocated char*'s. | |
4644 | ||
4645 | size_t pp_name_max = 0, symbol_string_name_max = 0; | |
4646 | size_t pp_name_tot = 0, symbol_string_name_tot = 0; | |
4647 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4648 | { | |
4649 | kprobe_derived_probe* p = it->second; | |
4650 | #define DOIT(var,expr) do { \ | |
4651 | size_t var##_size = (expr) + 1; \ | |
4652 | var##_max = max (var##_max, var##_size); \ | |
4653 | var##_tot += var##_size; } while (0) | |
4654 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
4655 | DOIT(symbol_string_name, p->symbol_name.size()); | |
4656 | #undef DOIT | |
4657 | } | |
4658 | ||
4659 | #define CALCIT(var) \ | |
4660 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
4661 | ||
4662 | CALCIT(pp); | |
4663 | CALCIT(symbol_string); | |
4664 | #undef CALCIT | |
4665 | ||
4666 | s.op->newline() << "const unsigned long address;"; | |
4667 | s.op->newline() << "void (* const ph) (struct context*);"; | |
4668 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; | |
4669 | s.op->indent(1); | |
4670 | ||
4671 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4672 | { | |
4673 | kprobe_derived_probe* p = it->second; | |
4674 | s.op->newline() << "{"; | |
4675 | if (p->has_return) | |
4676 | s.op->line() << " .return_p=1,"; | |
4677 | ||
4678 | if (p->has_maxactive) | |
4679 | { | |
4680 | s.op->line() << " .maxactive_p=1,"; | |
4681 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
4682 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
4683 | } | |
c8d9d15e | 4684 | |
b350f56b JS |
4685 | if (p->locations[0]->optional) |
4686 | s.op->line() << " .optional_p=1,"; | |
4687 | ||
e6fe60e7 | 4688 | if (p->has_statement) |
c8d9d15e | 4689 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 4690 | else |
c8d9d15e | 4691 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
e6fe60e7 AM |
4692 | |
4693 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
4694 | s.op->line() << " .ph=&" << p->name; | |
4695 | s.op->line() << " },"; | |
4696 | } | |
4697 | ||
4698 | s.op->newline(-1) << "};"; | |
4699 | ||
4700 | // Emit the kprobes callback function | |
4701 | s.op->newline(); | |
88747011 | 4702 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
4703 | s.op->line() << " struct pt_regs *regs) {"; |
4704 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
4705 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
4706 | // Check that the index is plausible | |
4707 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
4708 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4709 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4710 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4711 | s.op->line() << "];"; | |
4712 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); | |
4713 | s.op->newline() << "c->regs = regs;"; | |
4714 | s.op->newline() << "(*sdp->ph) (c);"; | |
4715 | common_probe_entryfn_epilogue (s.op); | |
4716 | s.op->newline() << "return 0;"; | |
4717 | s.op->newline(-1) << "}"; | |
4718 | ||
4719 | // Same for kretprobes | |
4720 | s.op->newline(); | |
88747011 | 4721 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
4722 | s.op->line() << " struct pt_regs *regs) {"; |
4723 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
4724 | ||
4725 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
4726 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
4727 | // Check that the index is plausible | |
4728 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
4729 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4730 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4731 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4732 | s.op->line() << "];"; | |
4733 | ||
4734 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); | |
4735 | s.op->newline() << "c->regs = regs;"; | |
4736 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
4737 | s.op->newline() << "(*sdp->ph) (c);"; | |
4738 | common_probe_entryfn_epilogue (s.op); | |
4739 | s.op->newline() << "return 0;"; | |
4740 | s.op->newline(-1) << "}"; | |
4741 | } | |
4742 | ||
4743 | ||
4744 | void | |
4745 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
4746 | { | |
e6fe60e7 | 4747 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 4748 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 4749 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e JS |
4750 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
4751 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; | |
e6fe60e7 AM |
4752 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
4753 | s.op->newline() << "if (sdp->return_p) {"; | |
c8d9d15e | 4754 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
9f38e653 | 4755 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
e6fe60e7 AM |
4756 | s.op->newline() << "if (sdp->maxactive_p) {"; |
4757 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
4758 | s.op->newline(-1) << "} else {"; | |
f07c3b68 | 4759 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
e6fe60e7 | 4760 | s.op->newline(-1) << "}"; |
88747011 | 4761 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
4762 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
4763 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 4764 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
c8d9d15e JS |
4765 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
4766 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
e6fe60e7 AM |
4767 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
4768 | s.op->newline() << "if (rc == 0) {"; | |
4769 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
4770 | s.op->newline() << "if (rc != 0)"; | |
4771 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4772 | s.op->newline(-2) << "}"; | |
4773 | s.op->newline() << "#else"; | |
4774 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
4775 | s.op->newline() << "#endif"; | |
4776 | s.op->newline(-1) << "} else {"; | |
4777 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 4778 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
9f38e653 | 4779 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
88747011 | 4780 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 4781 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 4782 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e JS |
4783 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
4784 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; | |
e6fe60e7 AM |
4785 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
4786 | s.op->newline() << "if (rc == 0) {"; | |
4787 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
4788 | s.op->newline() << "if (rc != 0)"; | |
4789 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4790 | s.op->newline(-2) << "}"; | |
4791 | s.op->newline() << "#else"; | |
4792 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
4793 | s.op->newline() << "#endif"; | |
4794 | s.op->newline(-1) << "}"; | |
4795 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
4796 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 4797 | s.op->newline() << "if (!sdp->optional_p)"; |
4a4edc21 | 4798 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, addr, rc);"; |
b350f56b | 4799 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
e6fe60e7 AM |
4800 | // XXX: shall we increment numskipped? |
4801 | s.op->newline(-1) << "}"; | |
4802 | ||
4803 | s.op->newline() << "else sdp->registered_p = 1;"; | |
4804 | s.op->newline(-1) << "}"; // for loop | |
e6fe60e7 AM |
4805 | } |
4806 | ||
4807 | void | |
4808 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
4809 | { | |
4810 | //Unregister kprobes by batch interfaces. | |
4811 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4812 | s.op->newline() << "j = 0;"; | |
4813 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4814 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
4815 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
4816 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4817 | s.op->newline() << "if (!sdp->return_p)"; | |
c9116e99 | 4818 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;"; |
e6fe60e7 | 4819 | s.op->newline(-2) << "}"; |
c9116e99 | 4820 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
4821 | s.op->newline() << "j = 0;"; |
4822 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4823 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
4824 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
4825 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4826 | s.op->newline() << "if (sdp->return_p)"; | |
c9116e99 | 4827 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;"; |
e6fe60e7 | 4828 | s.op->newline(-2) << "}"; |
c9116e99 | 4829 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
4830 | s.op->newline() << "#ifdef __ia64__"; |
4831 | s.op->newline() << "j = 0;"; | |
4832 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4833 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
4834 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
4835 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
c9116e99 | 4836 | s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;"; |
e6fe60e7 | 4837 | s.op->newline(-1) << "}"; |
c9116e99 | 4838 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
4839 | s.op->newline() << "#endif"; |
4840 | s.op->newline() << "#endif"; | |
4841 | ||
4842 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4843 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
4844 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
4845 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4846 | s.op->newline() << "if (sdp->return_p) {"; | |
4847 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4848 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
4849 | s.op->newline() << "#endif"; | |
4850 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; | |
4851 | s.op->newline() << "#ifdef STP_TIMING"; | |
4852 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
4853 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; | |
4854 | s.op->newline(-1) << "#endif"; | |
4855 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; | |
4856 | s.op->newline() << "#ifdef STP_TIMING"; | |
4857 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
4858 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; | |
4859 | s.op->newline(-1) << "#endif"; | |
4860 | s.op->newline(-1) << "} else {"; | |
4861 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4862 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
4863 | s.op->newline() << "#endif"; | |
4864 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; | |
4865 | s.op->newline() << "#ifdef STP_TIMING"; | |
4866 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
4867 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);"; | |
4868 | s.op->newline(-1) << "#endif"; | |
4869 | s.op->newline(-1) << "}"; | |
4870 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
4871 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
4872 | s.op->newline() << "#endif"; | |
4873 | s.op->newline() << "sdp->registered_p = 0;"; | |
4874 | s.op->newline(-1) << "}"; | |
4875 | } | |
4876 | ||
4877 | struct kprobe_builder: public derived_probe_builder | |
4878 | { | |
4879 | kprobe_builder() {} | |
4880 | virtual void build(systemtap_session & sess, | |
4881 | probe * base, | |
4882 | probe_point * location, | |
4883 | literal_map_t const & parameters, | |
4884 | vector<derived_probe *> & finished_results); | |
4885 | }; | |
4886 | ||
4887 | ||
4888 | void | |
4889 | kprobe_builder::build(systemtap_session & sess, | |
4890 | probe * base, | |
4891 | probe_point * location, | |
4892 | literal_map_t const & parameters, | |
4893 | vector<derived_probe *> & finished_results) | |
4894 | { | |
4895 | string function_string_val, module_string_val; | |
b6371390 JS |
4896 | int64_t statement_num_val = 0, maxactive_val = 0; |
4897 | bool has_function_str, has_module_str, has_statement_num; | |
4898 | bool has_absolute, has_return, has_maxactive; | |
e6fe60e7 | 4899 | |
b6371390 JS |
4900 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
4901 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
4902 | has_return = has_null_param (parameters, TOK_RETURN); | |
4903 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
4904 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
4905 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
e6fe60e7 | 4906 | |
b6371390 JS |
4907 | if (has_function_str) |
4908 | { | |
4909 | if (has_module_str) | |
4910 | function_string_val = module_string_val + ":" + function_string_val; | |
4911 | ||
4912 | finished_results.push_back (new kprobe_derived_probe (base, | |
4913 | location, function_string_val, | |
4914 | 0, has_return, | |
4915 | has_statement_num, | |
4916 | has_maxactive, | |
4917 | maxactive_val)); | |
4918 | } | |
e6fe60e7 | 4919 | else |
b6371390 JS |
4920 | { |
4921 | // assert guru mode for absolute probes | |
4922 | if ( has_statement_num && has_absolute && !base->privileged ) | |
4923 | throw semantic_error ("absolute statement probe in unprivileged script", base->tok); | |
4924 | ||
4925 | finished_results.push_back (new kprobe_derived_probe (base, | |
4926 | location, "", | |
4927 | statement_num_val, | |
4928 | has_return, | |
4929 | has_statement_num, | |
4930 | has_maxactive, | |
4931 | maxactive_val)); | |
4932 | } | |
e6fe60e7 | 4933 | } |
b6371390 JS |
4934 | |
4935 | ||
342d3f96 | 4936 | |
0a6f5a3f JS |
4937 | // ------------------------------------------------------------------------ |
4938 | // statically inserted kernel-tracepoint derived probes | |
4939 | // ------------------------------------------------------------------------ | |
4940 | ||
6fb70fb7 JS |
4941 | struct tracepoint_arg |
4942 | { | |
ad370dcc | 4943 | string name, c_type, typecast; |
dcaa1a65 | 4944 | bool usable, used, isptr; |
f8a968bc | 4945 | Dwarf_Die type_die; |
dcaa1a65 | 4946 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 4947 | }; |
0a6f5a3f JS |
4948 | |
4949 | struct tracepoint_derived_probe: public derived_probe | |
4950 | { | |
79189b84 JS |
4951 | tracepoint_derived_probe (systemtap_session& s, |
4952 | dwflpp& dw, Dwarf_Die& func_die, | |
4953 | const string& tracepoint_name, | |
4954 | probe* base_probe, probe_point* location); | |
4955 | ||
4956 | systemtap_session& sess; | |
6fb70fb7 JS |
4957 | string tracepoint_name, header; |
4958 | vector <struct tracepoint_arg> args; | |
79189b84 | 4959 | |
6fb70fb7 | 4960 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
e2086848 | 4961 | void printargs (std::ostream &o) const; |
79189b84 | 4962 | void join_group (systemtap_session& s); |
3e3bd7b6 | 4963 | void print_dupe_stamp(ostream& o); |
f8a968bc | 4964 | void emit_probe_context_vars (translator_output* o); |
0a6f5a3f JS |
4965 | }; |
4966 | ||
4967 | ||
4968 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> | |
4969 | { | |
79189b84 JS |
4970 | void emit_module_decls (systemtap_session& s); |
4971 | void emit_module_init (systemtap_session& s); | |
4972 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f JS |
4973 | }; |
4974 | ||
4975 | ||
f8a968bc JS |
4976 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
4977 | { | |
4978 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
4979 | vector <struct tracepoint_arg>& args): | |
4980 | dw (dw), probe_name (probe_name), args (args) {} | |
4981 | dwflpp& dw; | |
4982 | const string& probe_name; | |
4983 | vector <struct tracepoint_arg>& args; | |
4984 | ||
4985 | void visit_target_symbol (target_symbol* e); | |
4986 | void visit_target_symbol_arg (target_symbol* e); | |
4987 | void visit_target_symbol_context (target_symbol* e); | |
4988 | }; | |
4989 | ||
4990 | ||
4991 | void | |
4992 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
4993 | { | |
4994 | string argname = e->base_name.substr(1); | |
4995 | ||
4996 | // search for a tracepoint parameter matching this name | |
4997 | tracepoint_arg *arg = NULL; | |
4998 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 4999 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
5000 | { |
5001 | arg = &args[i]; | |
5002 | arg->used = true; | |
5003 | break; | |
5004 | } | |
5005 | ||
5006 | if (arg == NULL) | |
5007 | { | |
5008 | stringstream alternatives; | |
5009 | for (unsigned i = 0; i < args.size(); ++i) | |
5010 | alternatives << " $" << args[i].name; | |
046e7190 | 5011 | alternatives << " $$name $$parms $$vars"; |
f8a968bc JS |
5012 | |
5013 | // We hope that this value ends up not being referenced after all, so it | |
5014 | // can be optimized out quietly. | |
5015 | semantic_error* saveme = | |
5016 | new semantic_error("unable to find tracepoint variable '" + e->base_name | |
5017 | + "' (alternatives:" + alternatives.str () + ")", e->tok); | |
5018 | // NB: we can have multiple errors, since a target variable | |
5019 | // may be expanded in several different contexts: | |
5020 | // trace ("*") { $foo->bar } | |
5021 | saveme->chain = e->saved_conversion_error; | |
5022 | e->saved_conversion_error = saveme; | |
5023 | provide (e); | |
5024 | return; | |
5025 | } | |
5026 | ||
5027 | // make sure we're not dereferencing base types | |
5028 | if (!e->components.empty() && !arg->isptr) | |
5029 | switch (e->components[0].first) | |
5030 | { | |
5031 | case target_symbol::comp_literal_array_index: | |
5032 | throw semantic_error("tracepoint variable '" + e->base_name | |
5033 | + "' may not be used as array", e->tok); | |
5034 | case target_symbol::comp_struct_member: | |
5035 | throw semantic_error("tracepoint variable '" + e->base_name | |
5036 | + "' may not be used as a structure", e->tok); | |
5037 | default: | |
5038 | throw semantic_error("invalid use of tracepoint variable '" | |
5039 | + e->base_name + "'", e->tok); | |
5040 | } | |
5041 | ||
5042 | // we can only write to dereferenced fields, and only if guru mode is on | |
5043 | bool lvalue = is_active_lvalue(e); | |
5044 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
5045 | throw semantic_error("write to tracepoint variable '" + e->base_name | |
5046 | + "' not permitted", e->tok); | |
ad370dcc JS |
5047 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
5048 | // is also meaningless until you dereference past a pointer member. It's | |
5049 | // harder to detect and prevent that though... | |
f8a968bc JS |
5050 | |
5051 | if (e->components.empty()) | |
5052 | { | |
03c75a4a JS |
5053 | if (e->addressof) |
5054 | throw semantic_error("cannot take address of tracepoint variable", e->tok); | |
5055 | ||
3e3bd7b6 JS |
5056 | // Just grab the value from the probe locals |
5057 | e->probe_context_var = "__tracepoint_arg_" + arg->name; | |
5058 | e->type = pe_long; | |
5059 | provide (e); | |
f8a968bc JS |
5060 | } |
5061 | else | |
5062 | { | |
5063 | // Synthesize a function to dereference the dwarf fields, | |
5064 | // with a pointer parameter that is the base tracepoint variable | |
5065 | functiondecl *fdecl = new functiondecl; | |
5066 | fdecl->tok = e->tok; | |
5067 | embeddedcode *ec = new embeddedcode; | |
5068 | ec->tok = e->tok; | |
5069 | ||
5070 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") | |
5071 | + "_" + e->base_name.substr(1) | |
5072 | + "_" + lex_cast<string>(tick++)); | |
5073 | ||
5074 | fdecl->name = fname; | |
5075 | fdecl->body = ec; | |
5076 | ||
5077 | try | |
5078 | { | |
b4c34c26 | 5079 | ec->code = dw.literal_stmt_for_pointer (&arg->type_die, e, |
f8a968bc JS |
5080 | lvalue, fdecl->type); |
5081 | } | |
5082 | catch (const semantic_error& er) | |
5083 | { | |
5084 | // We suppress this error message, and pass the unresolved | |
5085 | // variable to the next pass. We hope that this value ends | |
5086 | // up not being referenced after all, so it can be optimized out | |
5087 | // quietly. | |
5088 | semantic_error* saveme = new semantic_error (er); // copy it | |
5089 | saveme->tok1 = e->tok; // XXX: token not passed to dw code generation routines | |
5090 | // NB: we can have multiple errors, since a target variable | |
5091 | // may be expanded in several different contexts: | |
5092 | // trace ("*") { $foo->bar } | |
5093 | saveme->chain = e->saved_conversion_error; | |
5094 | e->saved_conversion_error = saveme; | |
5095 | provide (e); | |
5096 | return; | |
5097 | } | |
5098 | ||
5099 | // Give the fdecl an argument for the raw tracepoint value | |
5100 | vardecl *v1 = new vardecl; | |
5101 | v1->type = pe_long; | |
5102 | v1->name = "pointer"; | |
5103 | v1->tok = e->tok; | |
5104 | fdecl->formal_args.push_back(v1); | |
5105 | ||
5106 | if (lvalue) | |
5107 | { | |
5108 | // Modify the fdecl so it carries a pe_long formal | |
5109 | // argument called "value". | |
5110 | ||
5111 | // FIXME: For the time being we only support setting target | |
5112 | // variables which have base types; these are 'pe_long' in | |
5113 | // stap's type vocabulary. Strings and pointers might be | |
5114 | // reasonable, some day, but not today. | |
5115 | ||
5116 | vardecl *v2 = new vardecl; | |
5117 | v2->type = pe_long; | |
5118 | v2->name = "value"; | |
5119 | v2->tok = e->tok; | |
5120 | fdecl->formal_args.push_back(v2); | |
5121 | } | |
5122 | else | |
5123 | ec->code += "/* pure */"; | |
5124 | ||
5125 | dw.sess.functions[fdecl->name] = fdecl; | |
5126 | ||
5127 | // Synthesize a functioncall. | |
5128 | functioncall* n = new functioncall; | |
5129 | n->tok = e->tok; | |
5130 | n->function = fname; | |
5131 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
5132 | ||
5133 | // make the original a bare target symbol for the tracepoint value, | |
5134 | // which will be passed into the dwarf dereferencing code | |
5135 | e->components.clear(); | |
5136 | n->args.push_back(require(e)); | |
5137 | ||
5138 | if (lvalue) | |
5139 | { | |
5140 | // Provide the functioncall to our parent, so that it can be | |
5141 | // used to substitute for the assignment node immediately above | |
5142 | // us. | |
5143 | assert(!target_symbol_setter_functioncalls.empty()); | |
5144 | *(target_symbol_setter_functioncalls.top()) = n; | |
5145 | } | |
5146 | ||
5147 | provide (n); | |
5148 | } | |
5149 | } | |
5150 | ||
5151 | ||
5152 | void | |
5153 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
5154 | { | |
03c75a4a JS |
5155 | if (e->addressof) |
5156 | throw semantic_error("cannot take address of context variable", e->tok); | |
5157 | ||
f8a968bc JS |
5158 | if (is_active_lvalue (e)) |
5159 | throw semantic_error("write to tracepoint '" + e->base_name + "' not permitted", e->tok); | |
5160 | ||
5161 | if (!e->components.empty()) | |
5162 | switch (e->components[0].first) | |
5163 | { | |
5164 | case target_symbol::comp_literal_array_index: | |
5165 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as array", | |
5166 | e->tok); | |
5167 | case target_symbol::comp_struct_member: | |
5168 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as a structure", | |
5169 | e->tok); | |
5170 | default: | |
5171 | throw semantic_error("invalid tracepoint '" + e->base_name + "' use", e->tok); | |
5172 | } | |
5173 | ||
5174 | if (e->base_name == "$$name") | |
5175 | { | |
5176 | // Synthesize a functioncall. | |
5177 | functioncall* n = new functioncall; | |
5178 | n->tok = e->tok; | |
5179 | n->function = "_mark_name_get"; | |
5180 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
5181 | provide (n); | |
5182 | } | |
046e7190 | 5183 | else if (e->base_name == "$$vars" || e->base_name == "$$parms") |
f8a968bc | 5184 | { |
f8a968bc JS |
5185 | print_format* pf = new print_format; |
5186 | ||
5187 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
5188 | // NB: we synthesize a new token here rather than reusing | |
5189 | // e->tok, because print_format::print likes to use | |
5190 | // its tok->content. | |
5191 | token* pf_tok = new token(*e->tok); | |
5192 | pf_tok->content = "sprintf"; | |
5193 | ||
5194 | pf->tok = pf_tok; | |
5195 | pf->print_to_stream = false; | |
5196 | pf->print_with_format = true; | |
5197 | pf->print_with_delim = false; | |
5198 | pf->print_with_newline = false; | |
5199 | pf->print_char = false; | |
5200 | ||
5201 | for (unsigned i = 0; i < args.size(); ++i) | |
5202 | { | |
dcaa1a65 JS |
5203 | if (!args[i].usable) |
5204 | continue; | |
f8a968bc JS |
5205 | if (i > 0) |
5206 | pf->raw_components += " "; | |
5207 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 5208 | target_symbol *tsym = new target_symbol; |
f8a968bc JS |
5209 | tsym->tok = e->tok; |
5210 | tsym->base_name = "$" + args[i].name; | |
5211 | ||
5212 | // every variable should always be accessible! | |
5213 | tsym->saved_conversion_error = 0; | |
5214 | expression *texp = require (tsym); // NB: throws nothing ... | |
5215 | assert (!tsym->saved_conversion_error); // ... but this is how we know it happened. | |
5216 | ||
d0ad1746 | 5217 | pf->raw_components += args[i].isptr ? "=%p" : "=%#x"; |
f8a968bc JS |
5218 | pf->args.push_back(texp); |
5219 | } | |
5220 | ||
5221 | pf->components = print_format::string_to_components(pf->raw_components); | |
5222 | provide (pf); | |
5223 | } | |
5224 | else | |
5225 | assert(0); // shouldn't get here | |
5226 | } | |
5227 | ||
5228 | void | |
5229 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
5230 | { | |
5231 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
5232 | ||
046e7190 JS |
5233 | if (e->base_name == "$$name" || |
5234 | e->base_name == "$$parms" || | |
5235 | e->base_name == "$$vars") | |
f8a968bc JS |
5236 | visit_target_symbol_context (e); |
5237 | else | |
5238 | visit_target_symbol_arg (e); | |
5239 | } | |
5240 | ||
5241 | ||
5242 | ||
79189b84 JS |
5243 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
5244 | dwflpp& dw, Dwarf_Die& func_die, | |
5245 | const string& tracepoint_name, | |
5246 | probe* base, probe_point* loc): | |
5247 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), | |
5248 | sess (s), tracepoint_name (tracepoint_name) | |
5249 | { | |
5250 | // create synthetic probe point name; preserve condition | |
5251 | vector<probe_point::component*> comps; | |
5252 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
5253 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
5254 | this->sole_location()->components = comps; | |
5255 | ||
6fb70fb7 JS |
5256 | // fill out the available arguments in this tracepoint |
5257 | build_args(dw, func_die); | |
5258 | ||
5259 | // determine which header defined this tracepoint | |
5260 | string decl_file = dwarf_decl_file(&func_die); | |
5261 | size_t header_pos = decl_file.rfind("trace/"); | |
5262 | if (header_pos == string::npos) | |
5263 | throw semantic_error ("cannot parse header location for tracepoint '" | |
5264 | + tracepoint_name + "' in '" | |
5265 | + decl_file + "'"); | |
5266 | header = decl_file.substr(header_pos); | |
5267 | ||
5268 | // tracepoints from FOO_event_types.h should really be included from FOO.h | |
5269 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
5270 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 5271 | // XXX: see also PR9993. |
6fb70fb7 JS |
5272 | header_pos = header.find("_event_types"); |
5273 | if (header_pos != string::npos) | |
5274 | header.erase(header_pos, 12); | |
5275 | ||
f8a968bc JS |
5276 | // Now expand the local variables in the probe body |
5277 | tracepoint_var_expanding_visitor v (dw, name, args); | |
5278 | this->body = v.require (this->body); | |
5279 | ||
79189b84 JS |
5280 | if (sess.verbose > 2) |
5281 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name | |
5282 | << "'" << endl; | |
5283 | } | |
5284 | ||
5285 | ||
6fb70fb7 JS |
5286 | static bool |
5287 | dwarf_type_name(Dwarf_Die& type_die, string& c_type) | |
5288 | { | |
219e62c7 JS |
5289 | // if we've gotten down to a basic type, then we're done |
5290 | bool done = true; | |
5291 | switch (dwarf_tag(&type_die)) | |
6fb70fb7 | 5292 | { |
219e62c7 JS |
5293 | case DW_TAG_structure_type: |
5294 | c_type.append("struct "); | |
5295 | break; | |
5296 | case DW_TAG_union_type: | |
5297 | c_type.append("union "); | |
5298 | break; | |
5299 | case DW_TAG_typedef: | |
5300 | case DW_TAG_base_type: | |
5301 | break; | |
5302 | default: | |
5303 | done = false; | |
5304 | break; | |
5305 | } | |
5306 | if (done) | |
5307 | { | |
5308 | c_type.append(dwarf_diename_integrate(&type_die)); | |
6fb70fb7 JS |
5309 | return true; |
5310 | } | |
5311 | ||
5312 | // otherwise, this die is a type modifier. | |
5313 | ||
5314 | // recurse into the referent type | |
219e62c7 | 5315 | // if it can't be named, just call it "void" |
6fb70fb7 JS |
5316 | Dwarf_Attribute subtype_attr; |
5317 | Dwarf_Die subtype_die; | |
5318 | if (!dwarf_attr_integrate(&type_die, DW_AT_type, &subtype_attr) | |
5319 | || !dwarf_formref_die(&subtype_attr, &subtype_die) | |
5320 | || !dwarf_type_name(subtype_die, c_type)) | |
219e62c7 | 5321 | c_type = "void"; |
6fb70fb7 JS |
5322 | |
5323 | const char *suffix = NULL; | |
5324 | switch (dwarf_tag(&type_die)) | |
5325 | { | |
5326 | case DW_TAG_pointer_type: | |
5327 | suffix = "*"; | |
5328 | break; | |
5329 | case DW_TAG_array_type: | |
5330 | suffix = "[]"; | |
5331 | break; | |
5332 | case DW_TAG_const_type: | |
5333 | suffix = " const"; | |
5334 | break; | |
5335 | case DW_TAG_volatile_type: | |
5336 | suffix = " volatile"; | |
5337 | break; | |
5338 | default: | |
5339 | return false; | |
5340 | } | |
5341 | c_type.append(suffix); | |
219e62c7 JS |
5342 | |
5343 | // XXX HACK! The va_list isn't usable as found in the debuginfo... | |
5344 | if (c_type == "struct __va_list_tag*") | |
5345 | c_type = "va_list"; | |
5346 | ||
6fb70fb7 JS |
5347 | return true; |
5348 | } | |
5349 | ||
5350 | ||
f8a968bc | 5351 | static bool |
dcaa1a65 | 5352 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
f8a968bc JS |
5353 | { |
5354 | Dwarf_Attribute type_attr; | |
dcaa1a65 | 5355 | switch (dwarf_tag(&arg.type_die)) |
f8a968bc JS |
5356 | { |
5357 | case DW_TAG_typedef: | |
5358 | case DW_TAG_const_type: | |
5359 | case DW_TAG_volatile_type: | |
5360 | // iterate on the referent type | |
dcaa1a65 JS |
5361 | return (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) |
5362 | && dwarf_formref_die(&type_attr, &arg.type_die) | |
5363 | && resolve_tracepoint_arg_type(arg)); | |
f8a968bc JS |
5364 | case DW_TAG_base_type: |
5365 | // base types will simply be treated as script longs | |
dcaa1a65 | 5366 | arg.isptr = false; |
f8a968bc JS |
5367 | return true; |
5368 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
5369 | // pointers can be treated as script longs, |
5370 | // and if we know their type, they can also be dereferenced | |
5371 | if (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) | |
5372 | && dwarf_formref_die(&type_attr, &arg.type_die)) | |
5373 | arg.isptr = true; | |
ad370dcc JS |
5374 | arg.typecast = "(intptr_t)"; |
5375 | return true; | |
5376 | case DW_TAG_structure_type: | |
5377 | case DW_TAG_union_type: | |
5378 | // for structs/unions which are passed by value, we turn it into | |
5379 | // a pointer that can be dereferenced. | |
5380 | arg.isptr = true; | |
5381 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 5382 | return true; |
f8a968bc JS |
5383 | default: |
5384 | // should we consider other types too? | |
5385 | return false; | |
5386 | } | |
5387 | } | |
5388 | ||
5389 | ||
6fb70fb7 JS |
5390 | void |
5391 | tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die) | |
5392 | { | |
5393 | Dwarf_Die arg; | |
5394 | if (dwarf_child(&func_die, &arg) == 0) | |
5395 | do | |
5396 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
5397 | { | |
5398 | // build a tracepoint_arg for this parameter | |
5399 | tracepoint_arg tparg; | |
5400 | tparg.name = dwarf_diename_integrate(&arg); | |
5401 | ||
5402 | // read the type of this parameter | |
5403 | Dwarf_Attribute type_attr; | |
6fb70fb7 | 5404 | if (!dwarf_attr_integrate (&arg, DW_AT_type, &type_attr) |
f8a968bc | 5405 | || !dwarf_formref_die (&type_attr, &tparg.type_die) |
dcaa1a65 | 5406 | || !dwarf_type_name(tparg.type_die, tparg.c_type)) |
6fb70fb7 JS |
5407 | throw semantic_error ("cannot get type of tracepoint '" |
5408 | + tracepoint_name + "' parameter '" | |
5409 | + tparg.name + "'"); | |
5410 | ||
dcaa1a65 | 5411 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
5412 | args.push_back(tparg); |
5413 | if (sess.verbose > 4) | |
5414 | clog << "found parameter for tracepoint '" << tracepoint_name | |
5415 | << "': type:'" << tparg.c_type | |
5416 | << "' name:'" << tparg.name << "'" << endl; | |
5417 | } | |
5418 | while (dwarf_siblingof(&arg, &arg) == 0); | |
5419 | } | |
5420 | ||
e2086848 WH |
5421 | void |
5422 | tracepoint_derived_probe::printargs(std::ostream &o) const | |
5423 | { | |
dcaa1a65 JS |
5424 | for (unsigned i = 0; i < args.size(); ++i) |
5425 | if (args[i].usable) | |
5426 | o << " $" << args[i].name << ":" << args[i].c_type; | |
e2086848 | 5427 | } |
6fb70fb7 | 5428 | |
79189b84 JS |
5429 | void |
5430 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
5431 | { | |
5432 | if (! s.tracepoint_derived_probes) | |
5433 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
5434 | s.tracepoint_derived_probes->enroll (this); | |
5435 | } | |
5436 | ||
5437 | ||
3e3bd7b6 JS |
5438 | void |
5439 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) | |
5440 | { | |
5441 | for (unsigned i = 0; i < args.size(); i++) | |
5442 | if (args[i].used) | |
5443 | o << "__tracepoint_arg_" << args[i].name << endl; | |
5444 | } | |
5445 | ||
5446 | ||
f8a968bc JS |
5447 | void |
5448 | tracepoint_derived_probe::emit_probe_context_vars (translator_output* o) | |
5449 | { | |
5450 | for (unsigned i = 0; i < args.size(); i++) | |
5451 | if (args[i].used) | |
5452 | o->newline() << "int64_t __tracepoint_arg_" << args[i].name << ";"; | |
5453 | } | |
5454 | ||
5455 | ||
3c1b3d06 FCE |
5456 | static vector<string> tracepoint_extra_headers () |
5457 | { | |
5458 | vector<string> they_live; | |
5459 | // PR 9993 | |
5460 | // XXX: may need this to be configurable | |
5461 | they_live.push_back ("linux/skbuff.h"); | |
5462 | return they_live; | |
5463 | } | |
5464 | ||
5465 | ||
79189b84 JS |
5466 | void |
5467 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) | |
5468 | { | |
5469 | if (probes.empty()) | |
5470 | return; | |
5471 | ||
96b030fe JS |
5472 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
5473 | s.op->newline(); | |
79189b84 | 5474 | |
3c1b3d06 FCE |
5475 | // PR9993: Add extra headers to work around undeclared types in individual |
5476 | // include/trace/foo.h files | |
5477 | const vector<string>& extra_headers = tracepoint_extra_headers (); | |
5478 | for (unsigned z=0; z<extra_headers.size(); z++) | |
5479 | s.op->newline() << "#include <" << extra_headers[z] << ">\n"; | |
5480 | ||
6fb70fb7 JS |
5481 | for (unsigned i = 0; i < probes.size(); ++i) |
5482 | { | |
5483 | tracepoint_derived_probe *p = probes[i]; | |
96b030fe JS |
5484 | |
5485 | // emit a separate entry function for each probe, since tracepoints | |
5486 | // don't provide any sort of context pointer. | |
6fb70fb7 JS |
5487 | s.op->newline() << "#include <" << p->header << ">"; |
5488 | s.op->newline() << "static void enter_tracepoint_probe_" << i << "("; | |
8df306c4 JS |
5489 | if (p->args.size() == 0) |
5490 | s.op->line() << "void"; | |
6fb70fb7 JS |
5491 | for (unsigned j = 0; j < p->args.size(); ++j) |
5492 | { | |
5493 | if (j > 0) | |
5494 | s.op->line() << ", "; | |
5495 | s.op->line() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
5496 | } | |
5497 | s.op->line() << ") {"; | |
5498 | s.op->indent(1); | |
ad002306 | 5499 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", |
c12d974f | 5500 | lex_cast_qstring (*p->sole_location())); |
f8a968bc | 5501 | s.op->newline() << "c->marker_name = " |
c12d974f FCE |
5502 | << lex_cast_qstring (p->tracepoint_name) |
5503 | << ";"; | |
f8a968bc JS |
5504 | for (unsigned j = 0; j < p->args.size(); ++j) |
5505 | if (p->args[j].used) | |
5506 | { | |
5507 | s.op->newline() << "c->locals[0]." << p->name << ".__tracepoint_arg_" | |
5508 | << p->args[j].name << " = (int64_t)"; | |
ad370dcc | 5509 | s.op->line() << p->args[j].typecast; |
f8a968bc JS |
5510 | s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";"; |
5511 | } | |
6fb70fb7 JS |
5512 | s.op->newline() << p->name << " (c);"; |
5513 | common_probe_entryfn_epilogue (s.op); | |
5514 | s.op->newline(-1) << "}"; | |
96b030fe JS |
5515 | |
5516 | // emit normalized registration functions | |
5517 | s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {"; | |
5518 | s.op->newline(1) << "return register_trace_" << p->tracepoint_name | |
5519 | << "(enter_tracepoint_probe_" << i << ");"; | |
5520 | s.op->newline(-1) << "}"; | |
86758d5f JS |
5521 | |
5522 | // NB: we're not prepared to deal with unreg failures. However, failures | |
5523 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
5524 | // weren't even registered. The former should be OKed by the initial | |
5525 | // registration call, and the latter is safe to ignore. | |
5526 | s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {"; | |
5527 | s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name | |
96b030fe JS |
5528 | << "(enter_tracepoint_probe_" << i << ");"; |
5529 | s.op->newline(-1) << "}"; | |
6fb70fb7 JS |
5530 | s.op->newline(); |
5531 | } | |
96b030fe JS |
5532 | |
5533 | // emit an array of registration functions for easy init/shutdown | |
5534 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
5535 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 5536 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
5537 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
5538 | s.op->indent(1); | |
5539 | for (unsigned i = 0; i < probes.size(); ++i) | |
5540 | { | |
5541 | s.op->newline () << "{"; | |
5542 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
5543 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
5544 | s.op->line() << " },"; | |
5545 | } | |
5546 | s.op->newline(-1) << "};"; | |
5547 | s.op->newline(); | |
79189b84 JS |
5548 | } |
5549 | ||
5550 | ||
5551 | void | |
5552 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
5553 | { | |
5554 | if (probes.size () == 0) | |
5555 | return; | |
5556 | ||
5557 | s.op->newline() << "/* init tracepoint probes */"; | |
96b030fe JS |
5558 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
5559 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
5560 | s.op->newline() << "if (rc) {"; | |
5561 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
5562 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
5563 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
5564 | s.op->newline(-1) << "}"; | |
5565 | s.op->newline(-1) << "}"; | |
bc9a523d FCE |
5566 | |
5567 | // This would be technically proper (on those autoconf-detectable | |
5568 | // kernels that include this function in tracepoint.h), however we | |
5569 | // already make several calls to synchronze_sched() during our | |
5570 | // shutdown processes. | |
5571 | ||
5572 | // s.op->newline() << "if (rc)"; | |
5573 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
5574 | // s.op->indent(-1); | |
79189b84 JS |
5575 | } |
5576 | ||
5577 | ||
5578 | void | |
5579 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
5580 | { | |
5581 | if (probes.empty()) | |
5582 | return; | |
5583 | ||
96b030fe JS |
5584 | s.op->newline() << "/* deregister tracepoint probes */"; |
5585 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
5586 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
5587 | s.op->indent(-1); | |
bc9a523d FCE |
5588 | |
5589 | // Not necessary: see above. | |
5590 | ||
5591 | // s.op->newline() << "tracepoint_synchronize_unregister();"; | |
79189b84 JS |
5592 | } |
5593 | ||
5594 | ||
75ead1f7 JS |
5595 | struct tracepoint_query : public base_query |
5596 | { | |
5597 | tracepoint_query(dwflpp & dw, const string & tracepoint, | |
5598 | probe * base_probe, probe_point * base_loc, | |
5599 | vector<derived_probe *> & results): | |
5600 | base_query(dw, "*"), tracepoint(tracepoint), | |
5601 | base_probe(base_probe), base_loc(base_loc), | |
5602 | results(results) {} | |
5603 | ||
5604 | const string& tracepoint; | |
5605 | ||
5606 | probe * base_probe; | |
5607 | probe_point * base_loc; | |
5608 | vector<derived_probe *> & results; | |
5609 | ||
5610 | void handle_query_module(); | |
5611 | int handle_query_cu(Dwarf_Die * cudie); | |
5612 | int handle_query_func(Dwarf_Die * func); | |
5613 | ||
5614 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); | |
5615 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
5616 | }; | |
5617 | ||
5618 | ||
5619 | void | |
5620 | tracepoint_query::handle_query_module() | |
5621 | { | |
5622 | // look for the tracepoints in each CU | |
5623 | dw.iterate_over_cus(tracepoint_query_cu, this); | |
5624 | } | |
5625 | ||
5626 | ||
5627 | int | |
5628 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
5629 | { | |
5630 | dw.focus_on_cu (cudie); | |
5631 | ||
5632 | // look at each function to see if it's a tracepoint | |
5633 | string function = "stapprobe_" + tracepoint; | |
5634 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
5635 | } | |
5636 | ||
5637 | ||
5638 | int | |
5639 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
5640 | { | |
5641 | dw.focus_on_function (func); | |
5642 | ||
5643 | assert(dw.function_name.compare(0, 10, "stapprobe_") == 0); | |
5644 | string tracepoint_instance = dw.function_name.substr(10); | |
79189b84 JS |
5645 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
5646 | tracepoint_instance, | |
5647 | base_probe, base_loc); | |
5648 | results.push_back (dp); | |
75ead1f7 JS |
5649 | return DWARF_CB_OK; |
5650 | } | |
5651 | ||
5652 | ||
5653 | int | |
5654 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
5655 | { | |
5656 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); | |
5657 | if (pending_interrupts) return DWARF_CB_ABORT; | |
5658 | return q->handle_query_cu(cudie); | |
5659 | } | |
5660 | ||
5661 | ||
5662 | int | |
5663 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
5664 | { | |
5665 | tracepoint_query * q = static_cast<tracepoint_query *>(query); | |
5666 | if (pending_interrupts) return DWARF_CB_ABORT; | |
5667 | return q->handle_query_func(func); | |
5668 | } | |
5669 | ||
5670 | ||
0a6f5a3f JS |
5671 | struct tracepoint_builder: public derived_probe_builder |
5672 | { | |
5673 | private: | |
5674 | dwflpp *dw; | |
5675 | bool init_dw(systemtap_session& s); | |
5676 | ||
5677 | public: | |
3c1b3d06 | 5678 | |
0a6f5a3f JS |
5679 | tracepoint_builder(): dw(0) {} |
5680 | ~tracepoint_builder() { delete dw; } | |
5681 | ||
5682 | void build_no_more (systemtap_session& s) | |
5683 | { | |
5684 | if (dw && s.verbose > 3) | |
5685 | clog << "tracepoint_builder releasing dwflpp" << endl; | |
5686 | delete dw; | |
5687 | dw = NULL; | |
5688 | } | |
5689 | ||
5690 | void build(systemtap_session& s, | |
5691 | probe *base, probe_point *location, | |
5692 | literal_map_t const& parameters, | |
5693 | vector<derived_probe*>& finished_results); | |
5694 | }; | |
5695 | ||
5696 | ||
5697 | bool | |
5698 | tracepoint_builder::init_dw(systemtap_session& s) | |
5699 | { | |
5700 | if (dw != NULL) | |
5701 | return true; | |
5702 | ||
b278033a JS |
5703 | if (s.use_cache) |
5704 | { | |
5705 | // see if the cached module exists | |
5706 | find_tracequery_hash(s); | |
5707 | if (!s.tracequery_path.empty()) | |
5708 | { | |
5709 | int fd = open(s.tracequery_path.c_str(), O_RDONLY); | |
5710 | if (fd != -1) | |
5711 | { | |
5712 | if (s.verbose > 2) | |
5713 | clog << "Pass 2: using cached " << s.tracequery_path << endl; | |
5714 | ||
ae2552da | 5715 | dw = new dwflpp(s, s.tracequery_path, false); |
b278033a JS |
5716 | close(fd); |
5717 | return true; | |
5718 | } | |
5719 | } | |
5720 | } | |
5721 | ||
5722 | // no cached module, time to make it | |
0a6f5a3f | 5723 | string tracequery_ko; |
3c1b3d06 | 5724 | int rc = make_tracequery(s, tracequery_ko, tracepoint_extra_headers()); |
0a6f5a3f JS |
5725 | if (rc != 0) |
5726 | return false; | |
5727 | ||
b278033a JS |
5728 | if (s.use_cache) |
5729 | { | |
5730 | // try to save tracequery in the cache | |
5731 | if (s.verbose > 2) | |
5732 | clog << "Copying " << tracequery_ko | |
5733 | << " to " << s.tracequery_path << endl; | |
5734 | if (copy_file(tracequery_ko.c_str(), | |
5735 | s.tracequery_path.c_str()) != 0) | |
5736 | cerr << "Copy failed (\"" << tracequery_ko << "\" to \"" | |
5737 | << s.tracequery_path << "\"): " << strerror(errno) << endl; | |
5738 | } | |
0a6f5a3f | 5739 | |
ae2552da | 5740 | dw = new dwflpp(s, tracequery_ko, false); |
0a6f5a3f JS |
5741 | return true; |
5742 | } | |
5743 | ||
5744 | ||
5745 | void | |
5746 | tracepoint_builder::build(systemtap_session& s, | |
5747 | probe *base, probe_point *location, | |
5748 | literal_map_t const& parameters, | |
5749 | vector<derived_probe*>& finished_results) | |
5750 | { | |
5751 | if (!init_dw(s)) | |
5752 | return; | |
5753 | ||
75ead1f7 JS |
5754 | string tracepoint; |
5755 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
5756 | ||
5757 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); | |
51178501 | 5758 | dw->iterate_over_modules(&query_module, &q); |
0a6f5a3f JS |
5759 | } |
5760 | ||
5761 | ||
5762 | ||
b55bc428 | 5763 | // ------------------------------------------------------------------------ |
bd2b1e68 | 5764 | // Standard tapset registry. |
b55bc428 FCE |
5765 | // ------------------------------------------------------------------------ |
5766 | ||
7a053d3b | 5767 | void |
f8220a7b | 5768 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 5769 | { |
47e0478e | 5770 | register_tapset_been(s); |
93646f4d | 5771 | register_tapset_itrace(s); |
dd0e4fa7 | 5772 | register_tapset_mark(s); |
01c2eefe | 5773 | register_tapset_perfmon(s); |
7a212aa8 | 5774 | register_tapset_procfs(s); |
912e8c59 | 5775 | register_tapset_timers(s); |
b84779a5 | 5776 | register_tapset_utrace(s); |
912e8c59 | 5777 | |
b98a8d73 | 5778 | |
7a24d422 | 5779 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 5780 | dwarf_derived_probe::register_patterns(s); |
30a279be | 5781 | |
888af770 FCE |
5782 | // XXX: user-space starter set |
5783 | s.pattern_root->bind_num(TOK_PROCESS) | |
5784 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
5785 | ->bind(new uprobe_builder ()); | |
5786 | s.pattern_root->bind_num(TOK_PROCESS) | |
5787 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
5788 | ->bind(new uprobe_builder ()); | |
5789 | ||
0a6f5a3f JS |
5790 | // kernel tracepoint probes |
5791 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
5792 | ->bind(new tracepoint_builder()); | |
5793 | ||
e6fe60e7 AM |
5794 | // Kprobe based probe |
5795 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
5796 | ->bind(new kprobe_builder()); | |
5797 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) | |
5798 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
5799 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
5800 | ->bind(new kprobe_builder()); | |
b6371390 JS |
5801 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
5802 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
5803 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
5804 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
5805 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
5806 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
5807 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
5808 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
5809 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
b55bc428 | 5810 | } |
dc38c0ae DS |
5811 | |
5812 | ||
b20febf3 FCE |
5813 | vector<derived_probe_group*> |
5814 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 5815 | { |
b20febf3 | 5816 | vector<derived_probe_group*> g; |
912e8c59 JS |
5817 | |
5818 | #define DOONE(x) \ | |
5819 | if (s. x##_derived_probes) \ | |
5820 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
5821 | |
5822 | // Note that order *is* important here. We want to make sure we | |
5823 | // register (actually run) begin probes before any other probe type | |
5824 | // is run. Similarly, when unregistering probes, we want to | |
5825 | // unregister (actually run) end probes after every other probe type | |
5826 | // has be unregistered. To do the latter, | |
5827 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
5828 | DOONE(be); |
5829 | DOONE(dwarf); | |
888af770 | 5830 | DOONE(uprobe); |
b20febf3 FCE |
5831 | DOONE(timer); |
5832 | DOONE(profile); | |
5833 | DOONE(mark); | |
0a6f5a3f | 5834 | DOONE(tracepoint); |
e6fe60e7 | 5835 | DOONE(kprobe); |
b20febf3 FCE |
5836 | DOONE(hrtimer); |
5837 | DOONE(perfmon); | |
ce82316f | 5838 | DOONE(procfs); |
935447c8 DS |
5839 | |
5840 | // Another "order is important" item. We want to make sure we | |
5841 | // "register" the dummy task_finder probe group after all probe | |
5842 | // groups that use the task_finder. | |
5843 | DOONE(utrace); | |
a96d1db0 | 5844 | DOONE(itrace); |
935447c8 | 5845 | DOONE(task_finder); |
b20febf3 FCE |
5846 | #undef DOONE |
5847 | return g; | |
46b84a80 | 5848 | } |
73267b89 JS |
5849 | |
5850 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |