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