]>
Commit | Line | Data |
---|---|---|
56e12059 | 1 | // tapset resolution |
bbafcb1e | 2 | // Copyright (C) 2005-2010 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" |
5f8ca04f | 24 | #include "setupdwfl.h" |
aff5d390 | 25 | #include "sys/sdt.h" |
bd2b1e68 | 26 | |
3b579393 FCE |
27 | #include <cstdlib> |
28 | #include <algorithm> | |
bd2b1e68 | 29 | #include <deque> |
56e12059 | 30 | #include <iostream> |
bd2b1e68 | 31 | #include <map> |
ec4373ff | 32 | #include <set> |
56e12059 | 33 | #include <sstream> |
bd2b1e68 | 34 | #include <stdexcept> |
b55bc428 | 35 | #include <vector> |
e36387d7 | 36 | #include <cstdarg> |
29e64872 | 37 | #include <cassert> |
1969b5bc | 38 | #include <iomanip> |
f781f849 | 39 | #include <cerrno> |
bd2b1e68 GH |
40 | |
41 | extern "C" { | |
df8fadee | 42 | #include <fcntl.h> |
bd2b1e68 | 43 | #include <elfutils/libdwfl.h> |
7a053d3b | 44 | #include <elfutils/libdw.h> |
77de5e9e GH |
45 | #include <dwarf.h> |
46 | #include <elf.h> | |
47 | #include <obstack.h> | |
b20febf3 | 48 | #include <glob.h> |
30a279be | 49 | #include <fnmatch.h> |
5f0a03a6 | 50 | #include <stdio.h> |
349dc70e | 51 | #include <sys/types.h> |
aaf7ffe8 | 52 | #include <math.h> |
aff5d390 | 53 | #include <regex.h> |
4b1ad75e RM |
54 | |
55 | #define __STDC_FORMAT_MACROS | |
56 | #include <inttypes.h> | |
bd2b1e68 | 57 | } |
77de5e9e | 58 | |
56e12059 FCE |
59 | |
60 | using namespace std; | |
2171f774 | 61 | using namespace __gnu_cxx; |
56e12059 | 62 | |
47dd066d | 63 | |
b20febf3 FCE |
64 | |
65 | // ------------------------------------------------------------------------ | |
66 | void | |
a58d79d0 | 67 | common_probe_entryfn_prologue (translator_output* o, string statestr, |
c12d974f | 68 | string new_pp, |
912e8c59 | 69 | bool overload_processing) |
b20febf3 | 70 | { |
72d18b98 | 71 | o->newline() << "struct context* __restrict__ c;"; |
e0a17418 JS |
72 | o->newline() << "#if !INTERRUPTIBLE"; |
73 | o->newline() << "unsigned long flags;"; | |
74 | o->newline() << "#endif"; | |
b20febf3 | 75 | |
a58d79d0 DS |
76 | if (overload_processing) |
77 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
78 | else | |
79 | o->newline() << "#ifdef STP_TIMING"; | |
80 | o->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
b20febf3 | 81 | o->newline() << "#endif"; |
b20febf3 | 82 | |
e0a17418 JS |
83 | o->newline() << "#if INTERRUPTIBLE"; |
84 | o->newline() << "preempt_disable ();"; | |
85 | o->newline() << "#else"; | |
86 | o->newline() << "local_irq_save (flags);"; | |
87 | o->newline() << "#endif"; | |
b20febf3 | 88 | |
c931ec8a | 89 | // Check for enough free enough stack space |
d05a1d00 | 90 | o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space |
a63401b1 | 91 | o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space |
d05a1d00 FCE |
92 | // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack |
93 | // NB: see also CONFIG_DEBUG_STACKOVERFLOW | |
b3c3ca7c FCE |
94 | o->newline() << "atomic_inc (& skipped_count);"; |
95 | o->newline() << "#ifdef STP_TIMING"; | |
96 | o->newline() << "atomic_inc (& skipped_count_lowstack);"; | |
97 | o->newline() << "#endif"; | |
c931ec8a FCE |
98 | o->newline() << "goto probe_epilogue;"; |
99 | o->newline(-1) << "}"; | |
100 | ||
b20febf3 FCE |
101 | o->newline() << "if (atomic_read (&session_state) != " << statestr << ")"; |
102 | o->newline(1) << "goto probe_epilogue;"; | |
103 | o->indent(-1); | |
9a604fac | 104 | |
4a0ae64c | 105 | o->newline() << "c = contexts[smp_processor_id()];"; |
b3c3ca7c | 106 | o->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
9c736061 FCE |
107 | o->newline(1) << "#if !INTERRUPTIBLE"; |
108 | o->newline() << "atomic_inc (& skipped_count);"; | |
109 | o->newline() << "#endif"; | |
b3c3ca7c FCE |
110 | o->newline() << "#ifdef STP_TIMING"; |
111 | o->newline() << "atomic_inc (& skipped_count_reentrant);"; | |
c12d974f FCE |
112 | o->newline() << "#ifdef DEBUG_REENTRANCY"; |
113 | o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
114 | << new_pp << ", c->probe_point ?: \"?\", smp_processor_id());"; | |
115 | // NB: There is a conceivable race condition here with reading | |
116 | // c->probe_point, knowing that this other probe is sort of running. | |
117 | // However, in reality, it's interrupted. Plus even if it were able | |
118 | // to somehow start again, and stop before we read c->probe_point, | |
119 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
120 | o->newline() << "#endif"; | |
b3c3ca7c | 121 | o->newline() << "#endif"; |
9a604fac | 122 | o->newline() << "atomic_dec (& c->busy);"; |
b20febf3 | 123 | o->newline() << "goto probe_epilogue;"; |
9a604fac FCE |
124 | o->newline(-1) << "}"; |
125 | o->newline(); | |
1e00cfb1 | 126 | o->newline() << "c->last_stmt = 0;"; |
9a604fac | 127 | o->newline() << "c->last_error = 0;"; |
a7ed0d3e | 128 | o->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter |
22f8b401 | 129 | o->newline() << "c->regs = 0;"; |
b916df9c | 130 | o->newline() << "c->unwaddr = 0;"; |
c12d974f | 131 | o->newline() << "c->probe_point = " << new_pp << ";"; |
b916df9c | 132 | // reset unwound address cache |
fcff848e | 133 | o->newline() << "c->pi = 0;"; |
af234c40 | 134 | o->newline() << "c->pi_longs = 0;"; |
9addf322 | 135 | o->newline() << "c->regparm = 0;"; |
bc54e71c MH |
136 | o->newline() << "c->marker_name = NULL;"; |
137 | o->newline() << "c->marker_format = NULL;"; | |
e0a17418 JS |
138 | |
139 | o->newline() << "#if INTERRUPTIBLE"; | |
140 | o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
141 | o->newline() << "#else"; | |
142 | o->newline() << "c->actionremaining = MAXACTION;"; | |
143 | o->newline() << "#endif"; | |
dbb68664 FCE |
144 | o->newline() << "#ifdef STP_TIMING"; |
145 | o->newline() << "c->statp = 0;"; | |
146 | o->newline() << "#endif"; | |
5e562a69 | 147 | o->newline() << "c->ri = 0;"; |
9915575b FCE |
148 | // NB: The following would actually be incorrect. |
149 | // That's because cycles_sum/cycles_base values are supposed to survive | |
150 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
151 | // cycles), the values will be reset. | |
152 | /* | |
f0e6dc63 FCE |
153 | o->newline() << "#ifdef STP_OVERLOAD"; |
154 | o->newline() << "c->cycles_sum = 0;"; | |
155 | o->newline() << "c->cycles_base = 0;"; | |
41c262f3 | 156 | o->newline() << "#endif"; |
9915575b | 157 | */ |
b20febf3 | 158 | } |
9a604fac | 159 | |
a44a0785 | 160 | |
b20febf3 | 161 | void |
a58d79d0 | 162 | common_probe_entryfn_epilogue (translator_output* o, |
912e8c59 | 163 | bool overload_processing) |
b20febf3 | 164 | { |
a58d79d0 DS |
165 | if (overload_processing) |
166 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
167 | else | |
168 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 169 | o->newline() << "{"; |
a58d79d0 DS |
170 | o->newline(1) << "cycles_t cycles_atend = get_cycles ();"; |
171 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
172 | // fewer, if the hardware counter rolls over really quickly. We | |
173 | // handle 32-bit wraparound here. | |
174 | o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
175 | o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
176 | o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
177 | o->indent(-1); | |
dbb68664 | 178 | |
a58d79d0 | 179 | o->newline() << "#ifdef STP_TIMING"; |
dbb68664 | 180 | o->newline() << "if (likely (c->statp)) _stp_stat_add(*c->statp, cycles_elapsed);"; |
a58d79d0 DS |
181 | o->newline() << "#endif"; |
182 | ||
183 | if (overload_processing) | |
184 | { | |
185 | o->newline() << "#ifdef STP_OVERLOAD"; | |
186 | o->newline() << "{"; | |
187 | // If the cycle count has wrapped (cycles_atend > cycles_base), | |
188 | // let's go ahead and pretend the interval has been reached. | |
189 | // This should reset cycles_base and cycles_sum. | |
190 | o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; | |
191 | o->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
192 | o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
193 | o->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
194 | ||
195 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
196 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
197 | // has overloaded the system and we need to quit. | |
198 | o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; | |
199 | o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
200 | o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
201 | o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);"; | |
551e9f14 | 202 | o->newline() << "atomic_inc (&error_count);"; |
a58d79d0 | 203 | o->newline(-1) << "}"; |
e57b735a | 204 | |
a58d79d0 DS |
205 | o->newline() << "c->cycles_base = cycles_atend;"; |
206 | o->newline() << "c->cycles_sum = 0;"; | |
207 | o->newline(-1) << "}"; | |
208 | o->newline(-1) << "}"; | |
209 | o->newline() << "#endif"; | |
210 | } | |
e57b735a | 211 | |
440f755a JS |
212 | o->newline(-1) << "}"; |
213 | o->newline() << "#endif"; | |
e57b735a | 214 | |
440f755a JS |
215 | o->newline() << "c->probe_point = 0;"; // vacated |
216 | o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; | |
217 | o->newline(1) << "if (c->last_stmt != NULL)"; | |
218 | o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
219 | o->newline(-1) << "else"; | |
220 | o->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
221 | o->indent(-1); | |
222 | o->newline() << "atomic_inc (& error_count);"; | |
223 | o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {"; | |
224 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
225 | o->newline() << "_stp_exit ();"; | |
226 | o->newline(-1) << "}"; | |
227 | o->newline(-1) << "}"; | |
228 | o->newline() << "atomic_dec (&c->busy);"; | |
e57b735a | 229 | |
440f755a JS |
230 | o->newline(-1) << "probe_epilogue:"; // context is free |
231 | o->indent(1); | |
e57b735a | 232 | |
440f755a JS |
233 | // Check for excessive skip counts. |
234 | o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {"; | |
079915a5 | 235 | o->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(& session_state, STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))"; |
f65166cc | 236 | o->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");"; |
440f755a | 237 | o->newline(-1) << "}"; |
e57b735a | 238 | |
440f755a JS |
239 | o->newline() << "#if INTERRUPTIBLE"; |
240 | o->newline() << "preempt_enable_no_resched ();"; | |
241 | o->newline() << "#else"; | |
242 | o->newline() << "local_irq_restore (flags);"; | |
243 | o->newline() << "#endif"; | |
244 | } | |
e57b735a | 245 | |
e57b735a | 246 | |
440f755a | 247 | // ------------------------------------------------------------------------ |
e57b735a | 248 | |
440f755a JS |
249 | // ------------------------------------------------------------------------ |
250 | // Dwarf derived probes. "We apologize for the inconvience." | |
251 | // ------------------------------------------------------------------------ | |
e57b735a | 252 | |
4627ed58 JS |
253 | static const string TOK_KERNEL("kernel"); |
254 | static const string TOK_MODULE("module"); | |
255 | static const string TOK_FUNCTION("function"); | |
256 | static const string TOK_INLINE("inline"); | |
257 | static const string TOK_CALL("call"); | |
258 | static const string TOK_RETURN("return"); | |
259 | static const string TOK_MAXACTIVE("maxactive"); | |
260 | static const string TOK_STATEMENT("statement"); | |
261 | static const string TOK_ABSOLUTE("absolute"); | |
262 | static const string TOK_PROCESS("process"); | |
a794dbeb | 263 | static const string TOK_PROVIDER("provider"); |
4627ed58 JS |
264 | static const string TOK_MARK("mark"); |
265 | static const string TOK_TRACE("trace"); | |
266 | static const string TOK_LABEL("label"); | |
63b4fd14 | 267 | static const string TOK_LIBRARY("library"); |
e57b735a | 268 | |
1adf8ef1 | 269 | static int query_cu (Dwarf_Die * cudie, void * arg); |
6b517475 | 270 | static void query_addr(Dwarf_Addr addr, dwarf_query *q); |
e57b735a | 271 | |
440f755a JS |
272 | // Can we handle this query with just symbol-table info? |
273 | enum dbinfo_reqt | |
274 | { | |
275 | dbr_unknown, | |
276 | dbr_none, // kernel.statement(NUM).absolute | |
277 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
278 | dbr_need_dwarf | |
279 | }; | |
e57b735a | 280 | |
20e4a32c | 281 | |
440f755a JS |
282 | struct base_query; // forward decls |
283 | struct dwarf_query; | |
284 | struct dwflpp; | |
285 | struct symbol_table; | |
20e4a32c | 286 | |
a781f401 | 287 | |
440f755a JS |
288 | struct |
289 | symbol_table | |
290 | { | |
291 | module_info *mod_info; // associated module | |
292 | map<string, func_info*> map_by_name; | |
1c6b77e5 JS |
293 | multimap<Dwarf_Addr, func_info*> map_by_addr; |
294 | typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t; | |
440f755a JS |
295 | typedef pair<iterator_t, iterator_t> range_t; |
296 | #ifdef __powerpc__ | |
297 | GElf_Word opd_section; | |
298 | #endif | |
2867a2a1 JS |
299 | void add_symbol(const char *name, bool weak, bool descriptor, |
300 | Dwarf_Addr addr, Dwarf_Addr *high_addr); | |
440f755a | 301 | enum info_status read_symbols(FILE *f, const string& path); |
83ca3872 MW |
302 | enum info_status read_from_elf_file(const string& path, |
303 | const systemtap_session &sess); | |
304 | enum info_status read_from_text_file(const string& path, | |
305 | const systemtap_session &sess); | |
440f755a JS |
306 | enum info_status get_from_elf(); |
307 | void prepare_section_rejection(Dwfl_Module *mod); | |
308 | bool reject_section(GElf_Word section); | |
440f755a JS |
309 | void purge_syscall_stubs(); |
310 | func_info *lookup_symbol(const string& name); | |
311 | Dwarf_Addr lookup_symbol_address(const string& name); | |
312 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
7a053d3b | 313 | |
440f755a JS |
314 | symbol_table(module_info *mi) : mod_info(mi) {} |
315 | ~symbol_table(); | |
316 | }; | |
77de5e9e | 317 | |
440f755a JS |
318 | static bool null_die(Dwarf_Die *die) |
319 | { | |
320 | static Dwarf_Die null = { 0 }; | |
321 | return (!die || !memcmp(die, &null, sizeof(null))); | |
322 | } | |
c4ce66a1 JS |
323 | |
324 | ||
7a053d3b | 325 | enum |
bd2b1e68 | 326 | function_spec_type |
7a053d3b | 327 | { |
bd2b1e68 GH |
328 | function_alone, |
329 | function_and_file, | |
7a053d3b | 330 | function_file_and_line |
bd2b1e68 GH |
331 | }; |
332 | ||
ec4373ff | 333 | |
bd2b1e68 | 334 | struct dwarf_builder; |
f10534c6 | 335 | struct dwarf_var_expanding_visitor; |
77de5e9e | 336 | |
2930abc7 | 337 | |
b20febf3 FCE |
338 | // XXX: This class is a candidate for subclassing to separate |
339 | // the relocation vs non-relocation variants. Likewise for | |
340 | // kprobe vs kretprobe variants. | |
341 | ||
342 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 343 | { |
b20febf3 FCE |
344 | dwarf_derived_probe (const string& function, |
345 | const string& filename, | |
346 | int line, | |
347 | const string& module, | |
348 | const string& section, | |
349 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 350 | Dwarf_Addr addr, |
b20febf3 FCE |
351 | dwarf_query & q, |
352 | Dwarf_Die* scope_die); | |
20e4a32c | 353 | |
b20febf3 FCE |
354 | string module; |
355 | string section; | |
356 | Dwarf_Addr addr; | |
63b4fd14 | 357 | string path; |
27dc09b1 | 358 | bool has_process; |
2930abc7 | 359 | bool has_return; |
c9bad430 | 360 | bool has_maxactive; |
63b4fd14 | 361 | bool has_library; |
c9bad430 | 362 | long maxactive_val; |
b642c901 SC |
363 | string user_path; |
364 | string user_lib; | |
b95e2b79 | 365 | bool access_vars; |
2930abc7 | 366 | |
af234c40 JS |
367 | unsigned saved_longs, saved_strings; |
368 | dwarf_derived_probe* entry_handler; | |
369 | ||
b8da0ad1 | 370 | void printsig (std::ostream &o) const; |
6b66b9f7 | 371 | virtual void join_group (systemtap_session& s); |
9020300d | 372 | void emit_probe_local_init(translator_output * o); |
d0bfd2ac | 373 | void getargs(std::list<std::string> &arg_set) const; |
0a98fd42 | 374 | |
27dc09b1 DB |
375 | void emit_unprivileged_assertion (translator_output*); |
376 | void print_dupe_stamp(ostream& o); | |
377 | ||
bd2b1e68 | 378 | // Pattern registration helpers. |
7a053d3b | 379 | static void register_statement_variants(match_node * root, |
27dc09b1 DB |
380 | dwarf_builder * dw, |
381 | bool bind_unprivileged_p = false); | |
fd6602a0 | 382 | static void register_function_variants(match_node * root, |
27dc09b1 DB |
383 | dwarf_builder * dw, |
384 | bool bind_unprivileged_p = false); | |
7a053d3b | 385 | static void register_function_and_statement_variants(match_node * root, |
27dc09b1 DB |
386 | dwarf_builder * dw, |
387 | bool bind_unprivileged_p = false); | |
c4ce66a1 | 388 | static void register_patterns(systemtap_session& s); |
6b66b9f7 JS |
389 | |
390 | protected: | |
391 | dwarf_derived_probe(probe *base, | |
392 | probe_point *location, | |
393 | Dwarf_Addr addr, | |
394 | bool has_return): | |
395 | derived_probe(base, location), addr(addr), has_return(has_return), | |
af234c40 JS |
396 | has_maxactive(0), maxactive_val(0), access_vars(false), |
397 | saved_longs(0), saved_strings(0), entry_handler(0) | |
6b66b9f7 JS |
398 | {} |
399 | ||
400 | private: | |
d0bfd2ac | 401 | list<string> args; |
f10534c6 | 402 | void saveargs(dwarf_query& q, Dwarf_Die* scope_die, dwarf_var_expanding_visitor& v); |
20c6c071 GH |
403 | }; |
404 | ||
dc38c0ae | 405 | |
6b66b9f7 | 406 | struct uprobe_derived_probe: public dwarf_derived_probe |
6d0f3f0c | 407 | { |
6d0f3f0c | 408 | int pid; // 0 => unrestricted |
0973d815 | 409 | |
6d0f3f0c FCE |
410 | uprobe_derived_probe (const string& function, |
411 | const string& filename, | |
412 | int line, | |
413 | const string& module, | |
6d0f3f0c FCE |
414 | const string& section, |
415 | Dwarf_Addr dwfl_addr, | |
416 | Dwarf_Addr addr, | |
417 | dwarf_query & q, | |
6b66b9f7 JS |
418 | Dwarf_Die* scope_die): |
419 | dwarf_derived_probe(function, filename, line, module, section, | |
420 | dwfl_addr, addr, q, scope_die), pid(0) | |
421 | {} | |
6d0f3f0c | 422 | |
0973d815 FCE |
423 | // alternate constructor for process(PID).statement(ADDR).absolute |
424 | uprobe_derived_probe (probe *base, | |
425 | probe_point *location, | |
426 | int pid, | |
427 | Dwarf_Addr addr, | |
6b66b9f7 JS |
428 | bool has_return): |
429 | dwarf_derived_probe(base, location, addr, has_return), pid(pid) | |
430 | {} | |
9ace370f | 431 | |
6d0f3f0c | 432 | void join_group (systemtap_session& s); |
2865d17a DB |
433 | |
434 | void emit_unprivileged_assertion (translator_output*); | |
8f6d8c2b | 435 | void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); } |
6d0f3f0c FCE |
436 | }; |
437 | ||
dc38c0ae DS |
438 | struct dwarf_derived_probe_group: public derived_probe_group |
439 | { | |
440 | private: | |
b20febf3 FCE |
441 | multimap<string,dwarf_derived_probe*> probes_by_module; |
442 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
443 | |
444 | public: | |
b20febf3 FCE |
445 | void enroll (dwarf_derived_probe* probe); |
446 | void emit_module_decls (systemtap_session& s); | |
447 | void emit_module_init (systemtap_session& s); | |
448 | void emit_module_exit (systemtap_session& s); | |
dc38c0ae DS |
449 | }; |
450 | ||
451 | ||
20c6c071 | 452 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 453 | struct base_query |
20c6c071 | 454 | { |
c4ce66a1 JS |
455 | base_query(dwflpp & dw, literal_map_t const & params); |
456 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 457 | virtual ~base_query() {} |
bd2b1e68 | 458 | |
5227f1ea | 459 | systemtap_session & sess; |
2c384610 | 460 | dwflpp & dw; |
5227f1ea | 461 | |
bd2b1e68 | 462 | // Parameter extractors. |
86bf665e | 463 | static bool has_null_param(literal_map_t const & params, |
888af770 | 464 | string const & k); |
86bf665e | 465 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 466 | string const & k, string & v); |
86bf665e | 467 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 468 | string const & k, long & v); |
86bf665e | 469 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 470 | string const & k, Dwarf_Addr & v); |
b55bc428 | 471 | |
2c384610 DS |
472 | // Extracted parameters. |
473 | bool has_kernel; | |
91af0778 FCE |
474 | bool has_module; |
475 | bool has_process; | |
63b4fd14 | 476 | bool has_library; |
2c384610 | 477 | string module_val; // has_kernel => module_val = "kernel" |
63b4fd14 | 478 | string path; // executable path if module is a .so |
2c384610 DS |
479 | |
480 | virtual void handle_query_module() = 0; | |
481 | }; | |
482 | ||
483 | ||
c4ce66a1 JS |
484 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
485 | sess(dw.sess), dw(dw) | |
2c384610 | 486 | { |
91af0778 | 487 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
488 | if (has_kernel) |
489 | module_val = "kernel"; | |
91af0778 FCE |
490 | |
491 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
492 | if (has_module) | |
493 | has_process = false; | |
4baf0e53 | 494 | else |
d0a7f5a9 | 495 | { |
63b4fd14 | 496 | string library_name; |
d0a7f5a9 | 497 | has_process = get_string_param(params, TOK_PROCESS, module_val); |
63b4fd14 SC |
498 | has_library = get_string_param (params, TOK_LIBRARY, library_name); |
499 | if (has_library) | |
500 | { | |
501 | path = find_executable (module_val); | |
502 | module_val = find_executable (library_name, "LD_LIBRARY_PATH"); | |
503 | } | |
504 | else if (has_process) | |
d0a7f5a9 FCE |
505 | module_val = find_executable (module_val); |
506 | } | |
91af0778 FCE |
507 | |
508 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
509 | } |
510 | ||
c4ce66a1 JS |
511 | base_query::base_query(dwflpp & dw, const string & module_val) |
512 | : sess(dw.sess), dw(dw), module_val(module_val) | |
513 | { | |
514 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
515 | // which means that userspace modules won't get any PATH searching. | |
516 | if (module_val.find('/') == string::npos) | |
517 | { | |
518 | has_kernel = (module_val == TOK_KERNEL); | |
519 | has_module = !has_kernel; | |
520 | has_process = false; | |
521 | } | |
522 | else | |
523 | { | |
524 | has_kernel = has_module = false; | |
525 | has_process = true; | |
526 | } | |
527 | } | |
528 | ||
2c384610 | 529 | bool |
86bf665e | 530 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
531 | string const & k) |
532 | { | |
888af770 | 533 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
534 | } |
535 | ||
536 | ||
537 | bool | |
86bf665e | 538 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
539 | string const & k, string & v) |
540 | { | |
541 | return derived_probe_builder::get_param (params, k, v); | |
542 | } | |
543 | ||
544 | ||
545 | bool | |
86bf665e | 546 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
547 | string const & k, long & v) |
548 | { | |
549 | int64_t value; | |
550 | bool present = derived_probe_builder::get_param (params, k, value); | |
551 | v = (long) value; | |
552 | return present; | |
553 | } | |
554 | ||
555 | ||
556 | bool | |
86bf665e | 557 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
558 | string const & k, Dwarf_Addr & v) |
559 | { | |
560 | int64_t value; | |
561 | bool present = derived_probe_builder::get_param (params, k, value); | |
562 | v = (Dwarf_Addr) value; | |
563 | return present; | |
564 | } | |
565 | ||
2c384610 DS |
566 | struct dwarf_query : public base_query |
567 | { | |
e1278bd4 | 568 | dwarf_query(probe * base_probe, |
2c384610 DS |
569 | probe_point * base_loc, |
570 | dwflpp & dw, | |
86bf665e | 571 | literal_map_t const & params, |
b642c901 SC |
572 | vector<derived_probe *> & results, |
573 | const string user_path, | |
574 | const string user_lib); | |
2c384610 | 575 | |
c4ce66a1 JS |
576 | vector<derived_probe *> & results; |
577 | probe * base_probe; | |
578 | probe_point * base_loc; | |
b642c901 SC |
579 | string user_path; |
580 | string user_lib; | |
c4ce66a1 | 581 | |
2c384610 | 582 | virtual void handle_query_module(); |
5f0a03a6 JK |
583 | void query_module_dwarf(); |
584 | void query_module_symtab(); | |
2c384610 | 585 | |
2930abc7 FCE |
586 | void add_probe_point(string const & funcname, |
587 | char const * filename, | |
588 | int line, | |
589 | Dwarf_Die *scope_die, | |
590 | Dwarf_Addr addr); | |
36f9dd1d | 591 | |
857bdfd1 JS |
592 | // Track addresses we've already seen in a given module |
593 | set<Dwarf_Addr> alias_dupes; | |
594 | ||
7fdd3e2c JS |
595 | // Track inlines we've already seen as well |
596 | // NB: this can't be compared just by entrypc, as inlines can overlap | |
597 | set<inline_instance_info> inline_dupes; | |
598 | ||
2930abc7 | 599 | // Extracted parameters. |
7a053d3b | 600 | string function_val; |
20c6c071 GH |
601 | |
602 | bool has_function_str; | |
603 | bool has_statement_str; | |
604 | bool has_function_num; | |
605 | bool has_statement_num; | |
7a053d3b RM |
606 | string statement_str_val; |
607 | string function_str_val; | |
c239d28c GH |
608 | Dwarf_Addr statement_num_val; |
609 | Dwarf_Addr function_num_val; | |
20c6c071 | 610 | |
b8da0ad1 FCE |
611 | bool has_call; |
612 | bool has_inline; | |
20c6c071 GH |
613 | bool has_return; |
614 | ||
c9bad430 DS |
615 | bool has_maxactive; |
616 | long maxactive_val; | |
617 | ||
20c6c071 GH |
618 | bool has_label; |
619 | string label_val; | |
620 | ||
621 | bool has_relative; | |
622 | long relative_val; | |
623 | ||
37ebca01 FCE |
624 | bool has_absolute; |
625 | ||
467bea43 SC |
626 | bool has_mark; |
627 | ||
5f0a03a6 JK |
628 | enum dbinfo_reqt dbinfo_reqt; |
629 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
630 | ||
7d6d0afc | 631 | void parse_function_spec(const string & spec); |
20c6c071 | 632 | function_spec_type spec_type; |
7d6d0afc | 633 | vector<string> scopes; |
20c6c071 GH |
634 | string function; |
635 | string file; | |
0c8b7d37 | 636 | line_t line_type; |
879eb9e9 | 637 | int line[2]; |
5f0a03a6 | 638 | bool query_done; // Found exact match |
20c6c071 | 639 | |
bd25380d | 640 | set<string> filtered_srcfiles; |
7e1279ea FCE |
641 | |
642 | // Map official entrypc -> func_info object | |
86bf665e TM |
643 | inline_instance_map_t filtered_inlines; |
644 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
645 | bool choose_next_line; |
646 | Dwarf_Addr entrypc_for_next_line; | |
4df79aaf JS |
647 | |
648 | void query_module_functions (); | |
b55bc428 FCE |
649 | }; |
650 | ||
98afd80e FCE |
651 | |
652 | struct dwarf_builder: public derived_probe_builder | |
b55bc428 | 653 | { |
665e1256 | 654 | map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */ |
7a24d422 | 655 | map <string,dwflpp*> user_dw; |
b642c901 SC |
656 | string user_path; |
657 | string user_lib; | |
ae2552da | 658 | dwarf_builder() {} |
aa30ccd3 | 659 | |
ae2552da | 660 | dwflpp *get_kern_dw(systemtap_session& sess, const string& module) |
707bf35e | 661 | { |
ea14cf67 FCE |
662 | if (kern_dw[module] == 0) |
663 | kern_dw[module] = new dwflpp(sess, module, true); // might throw | |
ae2552da | 664 | return kern_dw[module]; |
707bf35e JS |
665 | } |
666 | ||
667 | dwflpp *get_user_dw(systemtap_session& sess, const string& module) | |
668 | { | |
ea14cf67 FCE |
669 | if (user_dw[module] == 0) |
670 | user_dw[module] = new dwflpp(sess, module, false); // might throw | |
707bf35e JS |
671 | return user_dw[module]; |
672 | } | |
7a24d422 FCE |
673 | |
674 | /* NB: not virtual, so can be called from dtor too: */ | |
06aca46a | 675 | void dwarf_build_no_more (bool verbose) |
aa30ccd3 | 676 | { |
ae2552da FCE |
677 | for (map<string,dwflpp*>::iterator udi = kern_dw.begin(); |
678 | udi != kern_dw.end(); | |
679 | udi ++) | |
aa30ccd3 | 680 | { |
7a24d422 | 681 | if (verbose) |
ae2552da FCE |
682 | clog << "dwarf_builder releasing kernel dwflpp " << udi->first << endl; |
683 | delete udi->second; | |
aa30ccd3 | 684 | } |
ae2552da | 685 | kern_dw.erase (kern_dw.begin(), kern_dw.end()); |
7a24d422 FCE |
686 | |
687 | for (map<string,dwflpp*>::iterator udi = user_dw.begin(); | |
688 | udi != user_dw.end(); | |
689 | udi ++) | |
690 | { | |
691 | if (verbose) | |
692 | clog << "dwarf_builder releasing user dwflpp " << udi->first << endl; | |
693 | delete udi->second; | |
694 | } | |
695 | user_dw.erase (user_dw.begin(), user_dw.end()); | |
696 | } | |
697 | ||
698 | void build_no_more (systemtap_session &s) | |
699 | { | |
700 | dwarf_build_no_more (s.verbose > 3); | |
aa30ccd3 FCE |
701 | } |
702 | ||
e38d6504 RM |
703 | ~dwarf_builder() |
704 | { | |
7a24d422 | 705 | dwarf_build_no_more (false); |
c8959a29 | 706 | } |
aa30ccd3 | 707 | |
5227f1ea | 708 | virtual void build(systemtap_session & sess, |
7a053d3b | 709 | probe * base, |
20c6c071 | 710 | probe_point * location, |
86bf665e | 711 | literal_map_t const & parameters, |
20c6c071 | 712 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
713 | }; |
714 | ||
5111fc3e | 715 | |
e1278bd4 | 716 | dwarf_query::dwarf_query(probe * base_probe, |
20c6c071 GH |
717 | probe_point * base_loc, |
718 | dwflpp & dw, | |
86bf665e | 719 | literal_map_t const & params, |
b642c901 SC |
720 | vector<derived_probe *> & results, |
721 | const string user_path, | |
722 | const string user_lib) | |
c4ce66a1 | 723 | : base_query(dw, params), results(results), |
b642c901 SC |
724 | base_probe(base_probe), base_loc(base_loc), |
725 | user_path(user_path), user_lib(user_lib) | |
bd2b1e68 GH |
726 | { |
727 | // Reduce the query to more reasonable semantic values (booleans, | |
728 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
729 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
730 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
731 | ||
732 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
733 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
734 | ||
0f336e95 SC |
735 | has_label = get_string_param(params, TOK_LABEL, label_val); |
736 | ||
b8da0ad1 FCE |
737 | has_call = has_null_param(params, TOK_CALL); |
738 | has_inline = has_null_param(params, TOK_INLINE); | |
bd2b1e68 | 739 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 740 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 741 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 742 | has_mark = false; |
37ebca01 | 743 | |
bd2b1e68 | 744 | if (has_function_str) |
7d6d0afc | 745 | parse_function_spec(function_str_val); |
bd2b1e68 | 746 | else if (has_statement_str) |
7d6d0afc | 747 | parse_function_spec(statement_str_val); |
0daad364 | 748 | |
5f0a03a6 JK |
749 | dbinfo_reqt = assess_dbinfo_reqt(); |
750 | query_done = false; | |
0daad364 JS |
751 | } |
752 | ||
753 | ||
440f755a JS |
754 | func_info_map_t * |
755 | get_filtered_functions(dwarf_query *q) | |
756 | { | |
757 | return &q->filtered_functions; | |
758 | } | |
759 | ||
760 | ||
761 | inline_instance_map_t * | |
762 | get_filtered_inlines(dwarf_query *q) | |
763 | { | |
764 | return &q->filtered_inlines; | |
765 | } | |
766 | ||
767 | ||
2c384610 | 768 | void |
5f0a03a6 | 769 | dwarf_query::query_module_dwarf() |
2c384610 DS |
770 | { |
771 | if (has_function_num || has_statement_num) | |
772 | { | |
773 | // If we have module("foo").function(0xbeef) or | |
774 | // module("foo").statement(0xbeef), the address is relative | |
775 | // to the start of the module, so we seek the function | |
776 | // number plus the module's bias. | |
6b517475 JS |
777 | Dwarf_Addr addr = has_function_num ? |
778 | function_num_val : statement_num_val; | |
08d1d520 MW |
779 | |
780 | // These are raw addresses, we need to know what the elf_bias | |
781 | // is to feed it to libdwfl based functions. | |
782 | Dwarf_Addr elf_bias; | |
783 | Elf *elf = dwfl_module_getelf (dw.module, &elf_bias); | |
784 | assert(elf); | |
785 | addr += elf_bias; | |
6b517475 | 786 | query_addr(addr, this); |
2c384610 DS |
787 | } |
788 | else | |
789 | { | |
790 | // Otherwise if we have a function("foo") or statement("foo") | |
791 | // specifier, we have to scan over all the CUs looking for | |
792 | // the function(s) in question | |
793 | assert(has_function_str || has_statement_str); | |
4df79aaf JS |
794 | |
795 | // For simple cases, no wildcard and no source:line, we can do a very | |
796 | // quick function lookup in a module-wide cache. | |
797 | if (spec_type == function_alone && !dw.name_has_wildcard(function)) | |
798 | query_module_functions(); | |
799 | else | |
800 | dw.iterate_over_cus(&query_cu, this); | |
2c384610 DS |
801 | } |
802 | } | |
803 | ||
5f0a03a6 JK |
804 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
805 | dwarf_query * q); | |
806 | ||
807 | void | |
808 | dwarf_query::query_module_symtab() | |
809 | { | |
810 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
811 | if (dbinfo_reqt == dbr_need_dwarf) | |
812 | return; | |
813 | ||
814 | module_info *mi = dw.mod_info; | |
815 | if (dbinfo_reqt == dbr_need_symtab) | |
816 | { | |
817 | if (mi->symtab_status == info_unknown) | |
818 | mi->get_symtab(this); | |
819 | if (mi->symtab_status == info_absent) | |
820 | return; | |
821 | } | |
822 | ||
823 | func_info *fi = NULL; | |
824 | symbol_table *sym_table = mi->sym_table; | |
825 | ||
826 | if (has_function_str) | |
827 | { | |
828 | // Per dwarf_query::assess_dbinfo_reqt()... | |
829 | assert(spec_type == function_alone); | |
830 | if (dw.name_has_wildcard(function_str_val)) | |
831 | { | |
832 | // Until we augment the blacklist sufficently... | |
833 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
834 | { | |
835 | // e.g., kernel.function("*") | |
836 | cerr << "Error: Pattern '" | |
837 | << function_str_val | |
838 | << "' matches every instruction address in the symbol table," | |
839 | << endl | |
840 | << "some of which aren't even functions." | |
841 | << " Please be more precise." | |
842 | << endl; | |
843 | return; | |
844 | } | |
2e67a43b | 845 | symbol_table::iterator_t iter; |
1c6b77e5 JS |
846 | for (iter = sym_table->map_by_addr.begin(); |
847 | iter != sym_table->map_by_addr.end(); | |
2e67a43b | 848 | ++iter) |
5f0a03a6 | 849 | { |
1c6b77e5 | 850 | fi = iter->second; |
5f0a03a6 JK |
851 | if (!null_die(&fi->die)) |
852 | continue; // already handled in query_module_dwarf() | |
853 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
854 | query_func_info(fi->addr, *fi, this); | |
855 | } | |
856 | } | |
857 | else | |
858 | { | |
859 | fi = sym_table->lookup_symbol(function_str_val); | |
2867a2a1 | 860 | if (fi && !fi->descriptor && null_die(&fi->die)) |
5f0a03a6 JK |
861 | query_func_info(fi->addr, *fi, this); |
862 | } | |
863 | } | |
864 | else | |
865 | { | |
866 | assert(has_function_num || has_statement_num); | |
867 | // Find the "function" in which the indicated address resides. | |
868 | Dwarf_Addr addr = | |
869 | (has_function_num ? function_num_val : statement_num_val); | |
870 | fi = sym_table->get_func_containing_address(addr); | |
871 | if (!fi) | |
872 | { | |
83ca3872 MW |
873 | if (! sess.suppress_warnings) |
874 | cerr << "Warning: address " | |
875 | << hex << addr << dec | |
876 | << " out of range for module " | |
877 | << dw.module_name; | |
5f0a03a6 JK |
878 | return; |
879 | } | |
880 | if (!null_die(&fi->die)) | |
881 | { | |
882 | // addr looks like it's in the compilation unit containing | |
883 | // the indicated function, but query_module_dwarf() didn't | |
884 | // match addr to any compilation unit, so addr must be | |
885 | // above that cu's address range. | |
83ca3872 MW |
886 | if (! sess.suppress_warnings) |
887 | cerr << "Warning: address " | |
888 | << hex << addr << dec | |
889 | << " maps to no known compilation unit in module " | |
890 | << dw.module_name; | |
5f0a03a6 JK |
891 | return; |
892 | } | |
893 | query_func_info(fi->addr, *fi, this); | |
894 | } | |
895 | } | |
896 | ||
897 | void | |
898 | dwarf_query::handle_query_module() | |
899 | { | |
1c6b77e5 JS |
900 | bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab; |
901 | dw.get_module_dwarf(false, report); | |
902 | ||
903 | // prebuild the symbol table to resolve aliases | |
904 | dw.mod_info->get_symtab(this); | |
905 | ||
857bdfd1 JS |
906 | // reset the dupe-checking for each new module |
907 | alias_dupes.clear(); | |
7fdd3e2c | 908 | inline_dupes.clear(); |
857bdfd1 | 909 | |
5f0a03a6 JK |
910 | if (dw.mod_info->dwarf_status == info_present) |
911 | query_module_dwarf(); | |
1c6b77e5 | 912 | |
5f0a03a6 JK |
913 | // Consult the symbol table if we haven't found all we're looking for. |
914 | // asm functions can show up in the symbol table but not in dwarf. | |
915 | if (sess.consult_symtab && !query_done) | |
916 | query_module_symtab(); | |
917 | } | |
918 | ||
2c384610 | 919 | |
7d6d0afc JS |
920 | void |
921 | dwarf_query::parse_function_spec(const string & spec) | |
bd2b1e68 | 922 | { |
1d12a9b2 JS |
923 | line_type = ABSOLUTE; |
924 | line[0] = line[1] = 0; | |
925 | ||
7d6d0afc | 926 | size_t src_pos, line_pos, dash_pos, scope_pos, next_scope_pos; |
bd2b1e68 | 927 | |
7d6d0afc JS |
928 | // look for named scopes |
929 | scope_pos = 0; | |
930 | next_scope_pos = spec.find("::"); | |
931 | while (next_scope_pos != string::npos) | |
bd2b1e68 | 932 | { |
7d6d0afc JS |
933 | scopes.push_back(spec.substr(scope_pos, next_scope_pos - scope_pos)); |
934 | scope_pos = next_scope_pos + 2; | |
935 | next_scope_pos = spec.find("::", scope_pos); | |
bd2b1e68 GH |
936 | } |
937 | ||
7d6d0afc JS |
938 | // look for a source separator |
939 | src_pos = spec.find('@', scope_pos); | |
940 | if (src_pos == string::npos) | |
bd2b1e68 | 941 | { |
7d6d0afc JS |
942 | function = spec.substr(scope_pos); |
943 | spec_type = function_alone; | |
bd2b1e68 | 944 | } |
7d6d0afc | 945 | else |
879eb9e9 | 946 | { |
7d6d0afc | 947 | function = spec.substr(scope_pos, src_pos - scope_pos); |
7a053d3b | 948 | |
7d6d0afc JS |
949 | // look for a line-number separator |
950 | line_pos = spec.find_first_of(":+", src_pos); | |
951 | if (line_pos == string::npos) | |
952 | { | |
953 | file = spec.substr(src_pos + 1); | |
954 | spec_type = function_and_file; | |
955 | } | |
956 | else | |
957 | { | |
958 | file = spec.substr(src_pos + 1, line_pos - src_pos - 1); | |
959 | ||
960 | // classify the line spec | |
961 | spec_type = function_file_and_line; | |
962 | if (spec[line_pos] == '+') | |
963 | line_type = RELATIVE; | |
964 | else if (spec[line_pos + 1] == '*' && | |
965 | spec.length() == line_pos + 2) | |
966 | line_type = WILDCARD; | |
967 | else | |
968 | line_type = ABSOLUTE; | |
969 | ||
970 | if (line_type != WILDCARD) | |
971 | try | |
972 | { | |
973 | // try to parse either N or N-M | |
974 | dash_pos = spec.find('-', line_pos + 1); | |
975 | if (dash_pos == string::npos) | |
976 | line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1)); | |
977 | else | |
978 | { | |
979 | line_type = RANGE; | |
980 | line[0] = lex_cast<int>(spec.substr(line_pos + 1, | |
981 | dash_pos - line_pos - 1)); | |
982 | line[1] = lex_cast<int>(spec.substr(dash_pos + 1)); | |
983 | } | |
984 | } | |
985 | catch (runtime_error & exn) | |
986 | { | |
987 | goto bad; | |
988 | } | |
989 | } | |
bd2b1e68 GH |
990 | } |
991 | ||
7d6d0afc JS |
992 | if (function.empty() || |
993 | (spec_type != function_alone && file.empty())) | |
bd2b1e68 GH |
994 | goto bad; |
995 | ||
7d6d0afc | 996 | if (sess.verbose > 2) |
bd2b1e68 | 997 | { |
7d6d0afc | 998 | clog << "parsed '" << spec << "'"; |
41c262f3 | 999 | |
7d6d0afc JS |
1000 | if (!scopes.empty()) |
1001 | clog << ", scope '" << scopes[0] << "'"; | |
1002 | for (unsigned i = 1; i < scopes.size(); ++i) | |
1003 | clog << "::'" << scopes[i] << "'"; | |
41c262f3 | 1004 | |
7d6d0afc JS |
1005 | clog << ", func '" << function << "'"; |
1006 | ||
1007 | if (spec_type != function_alone) | |
1008 | clog << ", file '" << file << "'"; | |
1009 | ||
1010 | if (spec_type == function_file_and_line) | |
1011 | { | |
1012 | clog << ", line "; | |
1013 | switch (line_type) | |
1014 | { | |
1015 | case ABSOLUTE: | |
1016 | clog << line[0]; | |
1017 | break; | |
1018 | ||
1019 | case RELATIVE: | |
1020 | clog << "+" << line[0]; | |
1021 | break; | |
1022 | ||
1023 | case RANGE: | |
1024 | clog << line[0] << " - " << line[1]; | |
1025 | break; | |
1026 | ||
1027 | case WILDCARD: | |
1028 | clog << "*"; | |
1029 | break; | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | clog << endl; | |
bd2b1e68 GH |
1034 | } |
1035 | ||
7d6d0afc JS |
1036 | return; |
1037 | ||
1038 | bad: | |
1039 | throw semantic_error("malformed specification '" + spec + "'", | |
1040 | base_probe->tok); | |
bd2b1e68 GH |
1041 | } |
1042 | ||
1043 | ||
36f9dd1d | 1044 | void |
b20febf3 FCE |
1045 | dwarf_query::add_probe_point(const string& funcname, |
1046 | const char* filename, | |
36f9dd1d | 1047 | int line, |
b20febf3 | 1048 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
1049 | Dwarf_Addr addr) |
1050 | { | |
b20febf3 | 1051 | string reloc_section; // base section for relocation purposes |
27646582 | 1052 | Dwarf_Addr reloc_addr; // relocated |
b20febf3 | 1053 | const string& module = dw.module_name; // "kernel" or other |
36f9dd1d | 1054 | |
37ebca01 FCE |
1055 | assert (! has_absolute); // already handled in dwarf_builder::build() |
1056 | ||
789448a3 | 1057 | reloc_addr = dw.relocate_address(addr, reloc_section); |
2930abc7 | 1058 | |
7f9f3386 FCE |
1059 | if (sess.verbose > 1) |
1060 | { | |
b20febf3 FCE |
1061 | clog << "probe " << funcname << "@" << filename << ":" << line; |
1062 | if (string(module) == TOK_KERNEL) | |
1063 | clog << " kernel"; | |
91af0778 | 1064 | else if (has_module) |
b20febf3 | 1065 | clog << " module=" << module; |
91af0778 FCE |
1066 | else if (has_process) |
1067 | clog << " process=" << module; | |
b20febf3 | 1068 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
b20febf3 | 1069 | clog << " pc=0x" << hex << addr << dec; |
7f9f3386 | 1070 | } |
4baf0e53 | 1071 | |
27646582 | 1072 | bool bad = dw.blacklisted_p (funcname, filename, line, module, |
789448a3 | 1073 | addr, has_return); |
b20febf3 FCE |
1074 | if (sess.verbose > 1) |
1075 | clog << endl; | |
7f9f3386 | 1076 | |
84048984 FCE |
1077 | if (module == TOK_KERNEL) |
1078 | { | |
1079 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
1080 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 1081 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
1082 | } |
1083 | ||
b20febf3 FCE |
1084 | if (! bad) |
1085 | { | |
1a0dbc5a | 1086 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
1087 | |
1088 | if (has_process) | |
1089 | { | |
1090 | results.push_back (new uprobe_derived_probe(funcname, filename, line, | |
6b66b9f7 | 1091 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1092 | *this, scope_die)); |
1093 | } | |
1094 | else | |
1095 | { | |
1096 | assert (has_kernel || has_module); | |
1097 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 1098 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1099 | *this, scope_die)); |
1100 | } | |
b20febf3 | 1101 | } |
2930abc7 FCE |
1102 | } |
1103 | ||
5f0a03a6 JK |
1104 | enum dbinfo_reqt |
1105 | dwarf_query::assess_dbinfo_reqt() | |
1106 | { | |
1107 | if (has_absolute) | |
1108 | { | |
1109 | // kernel.statement(NUM).absolute | |
1110 | return dbr_none; | |
1111 | } | |
1112 | if (has_inline) | |
1113 | { | |
1114 | // kernel.function("f").inline or module("m").function("f").inline | |
1115 | return dbr_need_dwarf; | |
1116 | } | |
1117 | if (has_function_str && spec_type == function_alone) | |
1118 | { | |
1119 | // kernel.function("f") or module("m").function("f") | |
1120 | return dbr_need_symtab; | |
1121 | } | |
1122 | if (has_statement_num) | |
1123 | { | |
1124 | // kernel.statement(NUM) or module("m").statement(NUM) | |
1125 | // Technically, all we need is the module offset (or _stext, for | |
1126 | // the kernel). But for that we need either the ELF file or (for | |
1127 | // _stext) the symbol table. In either case, the symbol table | |
1128 | // is available, and that allows us to map the NUM (address) | |
1129 | // to a function, which is goodness. | |
1130 | return dbr_need_symtab; | |
1131 | } | |
1132 | if (has_function_num) | |
1133 | { | |
1134 | // kernel.function(NUM) or module("m").function(NUM) | |
1135 | // Need the symbol table so we can back up from NUM to the | |
1136 | // start of the function. | |
1137 | return dbr_need_symtab; | |
1138 | } | |
1139 | // Symbol table tells us nothing about source files or line numbers. | |
1140 | return dbr_need_dwarf; | |
1141 | } | |
2930abc7 FCE |
1142 | |
1143 | ||
b8da0ad1 FCE |
1144 | // The critical determining factor when interpreting a pattern |
1145 | // string is, perhaps surprisingly: "presence of a lineno". The | |
1146 | // presence of a lineno changes the search strategy completely. | |
1147 | // | |
1148 | // Compare the two cases: | |
1149 | // | |
1150 | // 1. {statement,function}(foo@file.c:lineno) | |
1151 | // - find the files matching file.c | |
1152 | // - in each file, find the functions matching foo | |
1153 | // - query the file for line records matching lineno | |
1154 | // - iterate over the line records, | |
1155 | // - and iterate over the functions, | |
1156 | // - if(haspc(function.DIE, line.addr)) | |
1157 | // - if looking for statements: probe(lineno.addr) | |
1158 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
1159 | // | |
1160 | // 2. {statement,function}(foo@file.c) | |
1161 | // - find the files matching file.c | |
1162 | // - in each file, find the functions matching foo | |
1163 | // - probe(function.{entrypc,return,etc.}) | |
1164 | // | |
1165 | // Thus the first decision we make is based on the presence of a | |
1166 | // lineno, and we enter entirely different sets of callbacks | |
1167 | // depending on that decision. | |
1168 | // | |
1169 | // Note that the first case is a generalization fo the second, in that | |
1170 | // we could theoretically search through line records for matching | |
1171 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
1172 | // are already cached elsewhere, so we can do an "index scan" as an | |
1173 | // optimization. | |
7e1279ea | 1174 | |
bd2b1e68 | 1175 | static void |
4cd232e4 | 1176 | query_statement (string const & func, |
20e4a32c | 1177 | char const * file, |
4cd232e4 | 1178 | int line, |
bcc12710 | 1179 | Dwarf_Die *scope_die, |
20e4a32c | 1180 | Dwarf_Addr stmt_addr, |
4cd232e4 | 1181 | dwarf_query * q) |
bd2b1e68 | 1182 | { |
39bcd429 FCE |
1183 | try |
1184 | { | |
cee35f73 | 1185 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 1186 | line, scope_die, stmt_addr); |
39bcd429 FCE |
1187 | } |
1188 | catch (const semantic_error& e) | |
1189 | { | |
1190 | q->sess.print_error (e); | |
1191 | } | |
bd2b1e68 GH |
1192 | } |
1193 | ||
6b517475 JS |
1194 | static void |
1195 | query_addr(Dwarf_Addr addr, dwarf_query *q) | |
1196 | { | |
1197 | dwflpp &dw = q->dw; | |
1198 | ||
08d1d520 MW |
1199 | if (q->sess.verbose > 2) |
1200 | clog << "query_addr 0x" << hex << addr << dec << endl; | |
6b517475 JS |
1201 | |
1202 | // First pick which CU contains this address | |
1203 | Dwarf_Die* cudie = dw.query_cu_containing_address(addr); | |
1204 | if (!cudie) // address could be wildly out of range | |
1205 | return; | |
1206 | dw.focus_on_cu(cudie); | |
1207 | ||
1208 | // Now compensate for the dw bias | |
1209 | addr -= dw.module_bias; | |
1210 | ||
1211 | // Per PR5787, we look up the scope die even for | |
1212 | // statement_num's, for blacklist sensitivity and $var | |
1213 | // resolution purposes. | |
1214 | ||
1215 | // Find the scopes containing this address | |
1216 | vector<Dwarf_Die> scopes = dw.getscopes(addr); | |
1217 | if (scopes.empty()) | |
1218 | return; | |
1219 | ||
1220 | // Look for the innermost containing function | |
1221 | Dwarf_Die *fnscope = NULL; | |
1222 | for (size_t i = 0; i < scopes.size(); ++i) | |
1223 | { | |
1224 | int tag = dwarf_tag(&scopes[i]); | |
1225 | if ((tag == DW_TAG_subprogram && !q->has_inline) || | |
1226 | (tag == DW_TAG_inlined_subroutine && | |
1227 | !q->has_call && !q->has_return)) | |
1228 | { | |
1229 | fnscope = &scopes[i]; | |
1230 | break; | |
1231 | } | |
1232 | } | |
1233 | if (!fnscope) | |
1234 | return; | |
1235 | dw.focus_on_function(fnscope); | |
1236 | ||
1237 | Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0]; | |
1238 | ||
1239 | const char *file = dwarf_decl_file(fnscope); | |
1240 | int line; | |
1241 | dwarf_decl_line(fnscope, &line); | |
1242 | ||
1243 | // Function probes should reset the addr to the function entry | |
1244 | // and possibly perform prologue searching | |
1245 | if (q->has_function_num) | |
1246 | { | |
1247 | dw.die_entrypc(fnscope, &addr); | |
1248 | if (dwarf_tag(fnscope) == DW_TAG_subprogram && | |
1249 | (q->sess.prologue_searching || q->has_process)) // PR 6871 | |
1250 | { | |
1251 | func_info func; | |
1252 | func.die = *fnscope; | |
1253 | func.name = dw.function_name; | |
1254 | func.decl_file = file; | |
1255 | func.decl_line = line; | |
1256 | func.entrypc = addr; | |
1257 | ||
1258 | func_info_map_t funcs(1, func); | |
1259 | dw.resolve_prologue_endings (funcs); | |
1260 | if (funcs[0].prologue_end) | |
1261 | addr = funcs[0].prologue_end; | |
1262 | } | |
1263 | } | |
1264 | else | |
1265 | { | |
1266 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr)); | |
1267 | if (address_line) | |
1268 | { | |
1269 | file = address_line.linesrc(); | |
1270 | line = address_line.lineno(); | |
1271 | } | |
1272 | ||
1273 | // Verify that a raw address matches the beginning of a | |
1274 | // statement. This is a somewhat lame check that the address | |
1275 | // is at the start of an assembly instruction. Mark probes are in the | |
1276 | // middle of a macro and thus not strictly at a statement beginning. | |
1277 | // Guru mode may override this check. | |
1278 | if (!q->has_mark && (!address_line || address_line.addr() != addr)) | |
1279 | { | |
1280 | stringstream msg; | |
1281 | msg << "address 0x" << hex << addr | |
1282 | << " does not match the beginning of a statement"; | |
1283 | if (address_line) | |
1284 | msg << " (try 0x" << hex << address_line.addr() << ")"; | |
1285 | else | |
1286 | msg << " (no line info found for '" << dw.cu_name() | |
1287 | << "', in module '" << dw.module_name << "')"; | |
1288 | if (! q->sess.guru_mode) | |
1289 | throw semantic_error(msg.str()); | |
1290 | else if (! q->sess.suppress_warnings) | |
1291 | q->sess.print_warning(msg.str()); | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | // Build a probe at this point | |
1296 | query_statement(dw.function_name, file, line, scope, addr, q); | |
1297 | } | |
1298 | ||
8096dd7d JS |
1299 | static void |
1300 | query_label (string const & func, | |
1301 | char const * label, | |
1302 | char const * file, | |
1303 | int line, | |
1304 | Dwarf_Die *scope_die, | |
1305 | Dwarf_Addr stmt_addr, | |
1306 | dwarf_query * q) | |
1307 | { | |
6b517475 JS |
1308 | assert (q->has_statement_str || q->has_function_str); |
1309 | ||
8096dd7d JS |
1310 | size_t i = q->results.size(); |
1311 | ||
1312 | // weed out functions whose decl_file isn't one of | |
1313 | // the source files that we actually care about | |
6b517475 | 1314 | if (q->spec_type != function_alone && |
8096dd7d JS |
1315 | q->filtered_srcfiles.count(file) == 0) |
1316 | return; | |
1317 | ||
1318 | query_statement(func, file, line, scope_die, stmt_addr, q); | |
1319 | ||
c72aa911 JS |
1320 | // after the fact, insert the label back into the derivation chain |
1321 | probe_point::component* ppc = | |
1322 | new probe_point::component(TOK_LABEL, new literal_string (label)); | |
1323 | for (; i < q->results.size(); ++i) | |
1324 | { | |
1325 | derived_probe* p = q->results[i]; | |
1326 | probe_point* pp = new probe_point(*p->locations[0]); | |
1327 | pp->components.push_back (ppc); | |
1328 | p->base = p->base->create_alias(p->locations[0], pp); | |
1329 | } | |
8096dd7d JS |
1330 | } |
1331 | ||
7e1279ea | 1332 | static void |
3e961ba6 | 1333 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
1334 | dwarf_query * q) |
1335 | { | |
b6581717 | 1336 | try |
7e1279ea | 1337 | { |
b6581717 GH |
1338 | if (q->has_return) |
1339 | { | |
1340 | throw semantic_error ("cannot probe .return of inline function '" + ii.name + "'"); | |
1341 | } | |
1342 | else | |
1343 | { | |
b0ee93c4 | 1344 | if (q->sess.verbose>2) |
20e4a32c | 1345 | clog << "querying entrypc " |
3e961ba6 | 1346 | << hex << ii.entrypc << dec |
db22e55f | 1347 | << " of instance of inline '" << ii.name << "'\n"; |
20e4a32c | 1348 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
3e961ba6 | 1349 | &ii.die, ii.entrypc, q); |
b6581717 | 1350 | } |
7e1279ea | 1351 | } |
b6581717 | 1352 | catch (semantic_error &e) |
7e1279ea | 1353 | { |
b6581717 | 1354 | q->sess.print_error (e); |
7e1279ea FCE |
1355 | } |
1356 | } | |
1357 | ||
1358 | static void | |
1359 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 1360 | func_info & fi, |
7e1279ea FCE |
1361 | dwarf_query * q) |
1362 | { | |
b6581717 | 1363 | try |
7e1279ea | 1364 | { |
b6581717 GH |
1365 | if (q->has_return) |
1366 | { | |
1367 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
1368 | // kretprobe based on the entrypc in this case. | |
20e4a32c | 1369 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
1370 | &fi.die, entrypc, q); |
1371 | } | |
1372 | else | |
1373 | { | |
35dc8b04 | 1374 | if (fi.prologue_end != 0) |
44f75386 | 1375 | { |
44f75386 FCE |
1376 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1377 | &fi.die, fi.prologue_end, q); | |
1378 | } | |
1379 | else | |
1380 | { | |
1381 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
1382 | &fi.die, entrypc, q); | |
1383 | } | |
b6581717 | 1384 | } |
7e1279ea | 1385 | } |
b6581717 | 1386 | catch (semantic_error &e) |
7e1279ea | 1387 | { |
b6581717 | 1388 | q->sess.print_error (e); |
7e1279ea FCE |
1389 | } |
1390 | } | |
1391 | ||
1392 | ||
bd4b874d SC |
1393 | static void |
1394 | query_srcfile_label (const dwarf_line_t& line, void * arg) | |
1395 | { | |
1396 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1397 | ||
1398 | Dwarf_Addr addr = line.addr(); | |
1399 | ||
1400 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1401 | i != q->filtered_functions.end(); ++i) | |
1402 | if (q->dw.die_has_pc (i->die, addr)) | |
f09d0d1e JS |
1403 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, |
1404 | q, query_label); | |
1405 | ||
1406 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1407 | i != q->filtered_inlines.end(); ++i) | |
1408 | if (q->dw.die_has_pc (i->die, addr)) | |
1409 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1410 | q, query_label); | |
bd4b874d SC |
1411 | } |
1412 | ||
7e1279ea | 1413 | static void |
86bf665e | 1414 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
1415 | { |
1416 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1417 | ||
86bf665e | 1418 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 1419 | |
86bf665e | 1420 | int lineno = line.lineno(); |
847bf07f | 1421 | |
86bf665e | 1422 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
1423 | i != q->filtered_functions.end(); ++i) |
1424 | { | |
3e961ba6 | 1425 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1426 | { |
b0ee93c4 | 1427 | if (q->sess.verbose>3) |
db22e55f | 1428 | clog << "function DIE lands on srcfile\n"; |
4cd232e4 | 1429 | if (q->has_statement_str) |
3e961ba6 | 1430 | query_statement (i->name, i->decl_file, |
847bf07f | 1431 | lineno, // NB: not q->line ! |
3e961ba6 | 1432 | &(i->die), addr, q); |
4cd232e4 | 1433 | else |
3e961ba6 | 1434 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 1435 | } |
20e4a32c RM |
1436 | } |
1437 | ||
86bf665e | 1438 | for (inline_instance_map_t::iterator i |
897820ca GH |
1439 | = q->filtered_inlines.begin(); |
1440 | i != q->filtered_inlines.end(); ++i) | |
1441 | { | |
3e961ba6 | 1442 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1443 | { |
b0ee93c4 | 1444 | if (q->sess.verbose>3) |
db22e55f | 1445 | clog << "inline instance DIE lands on srcfile\n"; |
897820ca | 1446 | if (q->has_statement_str) |
3e961ba6 JB |
1447 | query_statement (i->name, i->decl_file, |
1448 | q->line[0], &(i->die), addr, q); | |
897820ca | 1449 | else |
3e961ba6 | 1450 | query_inline_instance_info (*i, q); |
897820ca | 1451 | } |
20e4a32c | 1452 | } |
7e1279ea FCE |
1453 | } |
1454 | ||
1455 | ||
7fdd3e2c JS |
1456 | bool |
1457 | inline_instance_info::operator<(const inline_instance_info& other) const | |
1458 | { | |
1459 | if (entrypc != other.entrypc) | |
1460 | return entrypc < other.entrypc; | |
1461 | ||
1462 | if (decl_line != other.decl_line) | |
1463 | return decl_line < other.decl_line; | |
1464 | ||
1465 | int cmp = name.compare(other.name); | |
1466 | if (!cmp) | |
1467 | cmp = strcmp(decl_file, other.decl_file); | |
1468 | return cmp < 0; | |
1469 | } | |
1470 | ||
1471 | ||
4fa7b22b | 1472 | static int |
7e1279ea | 1473 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
1474 | { |
1475 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
6b517475 JS |
1476 | assert (q->has_statement_str || q->has_function_str); |
1477 | assert (!q->has_call && !q->has_return); | |
bd2b1e68 | 1478 | |
39bcd429 | 1479 | try |
7a053d3b | 1480 | { |
b0ee93c4 | 1481 | if (q->sess.verbose>2) |
6b517475 | 1482 | clog << "selected inline instance of " << q->dw.function_name << "\n"; |
7e1279ea | 1483 | |
6b517475 JS |
1484 | Dwarf_Addr entrypc; |
1485 | if (q->dw.die_entrypc (die, &entrypc)) | |
1486 | { | |
1487 | inline_instance_info inl; | |
1488 | inl.die = *die; | |
1489 | inl.name = q->dw.function_name; | |
1490 | inl.entrypc = entrypc; | |
1491 | q->dw.function_file (&inl.decl_file); | |
1492 | q->dw.function_line (&inl.decl_line); | |
1493 | ||
1494 | // make sure that this inline hasn't already | |
1495 | // been matched from a different CU | |
1496 | if (q->inline_dupes.insert(inl).second) | |
1497 | q->filtered_inlines.push_back(inl); | |
1498 | } | |
7e1279ea FCE |
1499 | return DWARF_CB_OK; |
1500 | } | |
1501 | catch (const semantic_error& e) | |
1502 | { | |
1503 | q->sess.print_error (e); | |
1504 | return DWARF_CB_ABORT; | |
1505 | } | |
1506 | } | |
bb788f9f | 1507 | |
7e1279ea | 1508 | static int |
2da9cedb | 1509 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 1510 | { |
2da9cedb | 1511 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
6b517475 | 1512 | assert (q->has_statement_str || q->has_function_str); |
bb788f9f | 1513 | |
bd25380d JS |
1514 | // weed out functions whose decl_file isn't one of |
1515 | // the source files that we actually care about | |
6b517475 | 1516 | if (q->spec_type != function_alone && |
bd25380d | 1517 | q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0) |
8096dd7d | 1518 | return DWARF_CB_OK; |
bd25380d | 1519 | |
7e1279ea FCE |
1520 | try |
1521 | { | |
7e1279ea FCE |
1522 | q->dw.focus_on_function (func); |
1523 | ||
7d6d0afc JS |
1524 | if (!q->dw.function_scope_matches(q->scopes)) |
1525 | return DWARF_CB_OK; | |
1526 | ||
857bdfd1 JS |
1527 | // make sure that this function address hasn't |
1528 | // already been matched under an aliased name | |
1529 | Dwarf_Addr addr; | |
1530 | if (!q->dw.func_is_inline() && | |
1531 | dwarf_entrypc(func, &addr) == 0 && | |
1532 | !q->alias_dupes.insert(addr).second) | |
1533 | return DWARF_CB_OK; | |
1534 | ||
6b517475 | 1535 | if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return)) |
7e1279ea | 1536 | { |
b0ee93c4 | 1537 | if (q->sess.verbose>3) |
db22e55f FCE |
1538 | clog << "checking instances of inline " << q->dw.function_name |
1539 | << "\n"; | |
2da9cedb | 1540 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
7e1279ea | 1541 | } |
396afcee | 1542 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 1543 | { |
6b517475 JS |
1544 | if (q->sess.verbose>2) |
1545 | clog << "selected function " << q->dw.function_name << "\n"; | |
1546 | ||
1547 | func_info func; | |
1548 | q->dw.function_die (&func.die); | |
1549 | func.name = q->dw.function_name; | |
1550 | q->dw.function_file (&func.decl_file); | |
1551 | q->dw.function_line (&func.decl_line); | |
1552 | ||
1553 | Dwarf_Addr entrypc; | |
1554 | if (q->dw.function_entrypc (&entrypc)) | |
1555 | { | |
1556 | func.entrypc = entrypc; | |
1557 | q->filtered_functions.push_back (func); | |
1558 | } | |
1559 | /* else this function is fully inlined, just ignore it */ | |
7e1279ea | 1560 | } |
39bcd429 | 1561 | return DWARF_CB_OK; |
bd2b1e68 | 1562 | } |
39bcd429 | 1563 | catch (const semantic_error& e) |
bd2b1e68 | 1564 | { |
39bcd429 FCE |
1565 | q->sess.print_error (e); |
1566 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1567 | } |
bd2b1e68 GH |
1568 | } |
1569 | ||
1570 | static int | |
1571 | query_cu (Dwarf_Die * cudie, void * arg) | |
1572 | { | |
20c6c071 | 1573 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
6b517475 JS |
1574 | assert (q->has_statement_str || q->has_function_str); |
1575 | ||
49abf162 | 1576 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 1577 | |
39bcd429 | 1578 | try |
bd2b1e68 | 1579 | { |
7e1279ea | 1580 | q->dw.focus_on_cu (cudie); |
b5d77020 | 1581 | |
b0ee93c4 | 1582 | if (false && q->sess.verbose>2) |
54417494 | 1583 | clog << "focused on CU '" << q->dw.cu_name() |
db22e55f | 1584 | << "', in module '" << q->dw.module_name << "'\n"; |
d9b516ca | 1585 | |
6b517475 JS |
1586 | q->filtered_srcfiles.clear(); |
1587 | q->filtered_functions.clear(); | |
1588 | q->filtered_inlines.clear(); | |
1589 | ||
1590 | // In this path, we find "abstract functions", record | |
1591 | // information about them, and then (depending on lineno | |
1592 | // matching) possibly emit one or more of the function's | |
1593 | // associated addresses. Unfortunately the control of this | |
1594 | // cannot easily be turned inside out. | |
1595 | ||
1596 | if (q->spec_type != function_alone) | |
39bcd429 | 1597 | { |
6b517475 JS |
1598 | // If we have a pattern string with a filename, we need |
1599 | // to elaborate the srcfile mask in question first. | |
1600 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
1601 | ||
1602 | // If we have a file pattern and *no* srcfile matches, there's | |
1603 | // no need to look further into this CU, so skip. | |
1604 | if (q->filtered_srcfiles.empty()) | |
1605 | return DWARF_CB_OK; | |
1606 | } | |
e4c58386 | 1607 | |
6b517475 JS |
1608 | // Pick up [entrypc, name, DIE] tuples for all the functions |
1609 | // matching the query, and fill in the prologue endings of them | |
1610 | // all in a single pass. | |
5898b6e1 | 1611 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function); |
6b517475 JS |
1612 | if (rc != DWARF_CB_OK) |
1613 | q->query_done = true; | |
1614 | ||
1615 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 | |
1616 | && !q->has_statement_str) // PR 2608 | |
1617 | if (! q->filtered_functions.empty()) | |
1618 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
1619 | ||
1620 | if (q->spec_type == function_file_and_line) | |
1621 | { | |
1622 | // If we have a pattern string with target *line*, we | |
1623 | // have to look at lines in all the matched srcfiles. | |
1624 | void (* callback) (const dwarf_line_t&, void*) = | |
1625 | q->has_label ? query_srcfile_label : query_srcfile_line; | |
1626 | for (set<string>::const_iterator i = q->filtered_srcfiles.begin(); | |
1627 | i != q->filtered_srcfiles.end(); ++i) | |
1628 | q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str, | |
1629 | q->line_type, callback, q->function, q); | |
1630 | } | |
1631 | else if (q->has_label) | |
1632 | { | |
1633 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1634 | i != q->filtered_functions.end(); ++i) | |
1635 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1636 | q, query_label); | |
1637 | ||
1638 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1639 | i != q->filtered_inlines.end(); ++i) | |
1640 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1641 | q, query_label); | |
39bcd429 | 1642 | } |
6b517475 JS |
1643 | else |
1644 | { | |
1645 | // Otherwise, simply probe all resolved functions. | |
1646 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1647 | i != q->filtered_functions.end(); ++i) | |
1648 | query_func_info (i->entrypc, *i, q); | |
1649 | ||
1650 | // And all inline instances (if we're not excluding inlines with ".call") | |
1651 | if (! q->has_call) | |
1652 | for (inline_instance_map_t::iterator i | |
1653 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) | |
1654 | query_inline_instance_info (*i, q); | |
1655 | } | |
39bcd429 | 1656 | return DWARF_CB_OK; |
bd2b1e68 | 1657 | } |
39bcd429 | 1658 | catch (const semantic_error& e) |
bd2b1e68 | 1659 | { |
39bcd429 FCE |
1660 | q->sess.print_error (e); |
1661 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1662 | } |
bd2b1e68 GH |
1663 | } |
1664 | ||
0ce64fb8 | 1665 | |
4df79aaf JS |
1666 | void |
1667 | dwarf_query::query_module_functions () | |
1668 | { | |
1669 | try | |
1670 | { | |
1671 | filtered_srcfiles.clear(); | |
1672 | filtered_functions.clear(); | |
1673 | filtered_inlines.clear(); | |
1674 | ||
1675 | // Collect all module functions so we know which CUs are interesting | |
1676 | int rc = dw.iterate_single_function(query_dwarf_func, this, function); | |
1677 | if (rc != DWARF_CB_OK) | |
1678 | { | |
1679 | query_done = true; | |
1680 | return; | |
1681 | } | |
1682 | ||
1683 | set<void*> used_cus; // by cu->addr | |
1684 | vector<Dwarf_Die> cus; | |
1685 | Dwarf_Die cu_mem; | |
1686 | ||
1687 | for (func_info_map_t::iterator i = filtered_functions.begin(); | |
1688 | i != filtered_functions.end(); ++i) | |
1689 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1690 | used_cus.insert(cu_mem.addr).second) | |
1691 | cus.push_back(cu_mem); | |
1692 | ||
1693 | for (inline_instance_map_t::iterator i = filtered_inlines.begin(); | |
1694 | i != filtered_inlines.end(); ++i) | |
1695 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1696 | used_cus.insert(cu_mem.addr).second) | |
1697 | cus.push_back(cu_mem); | |
1698 | ||
1699 | // Reset the dupes since we didn't actually collect them the first time | |
1700 | alias_dupes.clear(); | |
1701 | inline_dupes.clear(); | |
1702 | ||
1703 | // Run the query again on the individual CUs | |
1704 | for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i) | |
1705 | query_cu(&*i, this); | |
1706 | } | |
1707 | catch (const semantic_error& e) | |
1708 | { | |
1709 | sess.print_error (e); | |
1710 | } | |
1711 | } | |
1712 | ||
1713 | ||
5f0a03a6 JK |
1714 | static void |
1715 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
1716 | { | |
1717 | // Validate the machine code in this elf file against the | |
1718 | // session machine. This is important, in case the wrong kind | |
1719 | // of debuginfo is being automagically processed by elfutils. | |
1720 | // While we can tell i686 apart from x86-64, unfortunately | |
1721 | // we can't help confusing i586 vs i686 (both EM_386). | |
1722 | ||
1723 | Dwarf_Addr bias; | |
1724 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
1725 | // because dwfl_module_getelf can force costly section relocations | |
1726 | // we don't really need, while either will do for this purpose. | |
1727 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
1728 | ?: dwfl_module_getelf (mod, &bias)); | |
1729 | ||
1730 | GElf_Ehdr ehdr_mem; | |
1731 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 1732 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
5f0a03a6 JK |
1733 | int elf_machine = em->e_machine; |
1734 | const char* debug_filename = ""; | |
1735 | const char* main_filename = ""; | |
1736 | (void) dwfl_module_info (mod, NULL, NULL, | |
1737 | NULL, NULL, NULL, | |
1738 | & main_filename, | |
1739 | & debug_filename); | |
1740 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
1741 | |
1742 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
1743 | string expect_machine2; | |
5f0a03a6 | 1744 | |
d27e6fd5 | 1745 | // NB: See also the 'uname -m' squashing done in main.cxx. |
5f0a03a6 JK |
1746 | switch (elf_machine) |
1747 | { | |
756c9462 FCE |
1748 | // x86 and ppc are bi-architecture; a 64-bit kernel |
1749 | // can normally run either 32-bit or 64-bit *userspace*. | |
1750 | case EM_386: | |
1751 | expect_machine = "i?86"; | |
1752 | if (! q->has_process) break; // 32-bit kernel/module | |
1753 | /* FALLSTHROUGH */ | |
1754 | case EM_X86_64: | |
1755 | expect_machine2 = "x86_64"; | |
1756 | break; | |
1757 | case EM_PPC: | |
756c9462 | 1758 | case EM_PPC64: |
5a1c472e | 1759 | expect_machine = "powerpc"; |
756c9462 | 1760 | break; |
3fe7d888 | 1761 | case EM_S390: expect_machine = "s390"; break; |
5f0a03a6 | 1762 | case EM_IA_64: expect_machine = "ia64"; break; |
d27e6fd5 | 1763 | case EM_ARM: expect_machine = "arm*"; break; |
5f0a03a6 JK |
1764 | // XXX: fill in some more of these |
1765 | default: expect_machine = "?"; break; | |
1766 | } | |
1767 | ||
1768 | if (! debug_filename) debug_filename = main_filename; | |
1769 | if (! debug_filename) debug_filename = name; | |
1770 | ||
756c9462 FCE |
1771 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
1772 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
1773 | { |
1774 | stringstream msg; | |
756c9462 FCE |
1775 | msg << "ELF machine " << expect_machine << "|" << expect_machine2 |
1776 | << " (code " << elf_machine | |
5f0a03a6 JK |
1777 | << ") mismatch with target " << sess_machine |
1778 | << " in '" << debug_filename << "'"; | |
1779 | throw semantic_error(msg.str ()); | |
1780 | } | |
1781 | ||
1782 | if (q->sess.verbose>2) | |
1783 | clog << "focused on module '" << q->dw.module_name | |
1784 | << " = [0x" << hex << q->dw.module_start | |
1785 | << "-0x" << q->dw.module_end | |
1786 | << ", bias 0x" << q->dw.module_bias << "]" << dec | |
1787 | << " file " << debug_filename | |
756c9462 | 1788 | << " ELF machine " << expect_machine << "|" << expect_machine2 |
5f0a03a6 JK |
1789 | << " (code " << elf_machine << ")" |
1790 | << "\n"; | |
1791 | } | |
1d3a40b6 | 1792 | |
91af0778 FCE |
1793 | |
1794 | ||
1795 | static Dwarf_Addr | |
1796 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
1797 | { | |
1798 | int syments = dwfl_module_getsymtab(m); | |
1799 | assert(syments); | |
1800 | for (int i = 1; i < syments; ++i) | |
1801 | { | |
1802 | GElf_Sym sym; | |
1803 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
1804 | if (name != NULL && strcmp(name, wanted) == 0) | |
1805 | return sym.st_value; | |
1806 | } | |
1807 | ||
1808 | return 0; | |
1809 | } | |
1810 | ||
1811 | ||
1812 | ||
bd2b1e68 | 1813 | static int |
b8da0ad1 | 1814 | query_module (Dwfl_Module *mod, |
91af0778 | 1815 | void **, |
b8da0ad1 | 1816 | const char *name, |
6f4c1275 | 1817 | Dwarf_Addr addr, |
b8da0ad1 | 1818 | void *arg) |
bd2b1e68 | 1819 | { |
91af0778 | 1820 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 1821 | |
39bcd429 | 1822 | try |
e38d6504 | 1823 | { |
91af0778 FCE |
1824 | module_info* mi = q->sess.module_cache->cache[name]; |
1825 | if (mi == 0) | |
1826 | { | |
1827 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
1828 | ||
6f4c1275 FCE |
1829 | mi->mod = mod; |
1830 | mi->addr = addr; | |
91af0778 | 1831 | |
6f4c1275 FCE |
1832 | const char* debug_filename = ""; |
1833 | const char* main_filename = ""; | |
1834 | (void) dwfl_module_info (mod, NULL, NULL, | |
1835 | NULL, NULL, NULL, | |
1836 | & main_filename, | |
1837 | & debug_filename); | |
1838 | ||
1839 | if (q->sess.ignore_vmlinux && name == TOK_KERNEL) | |
91af0778 FCE |
1840 | { |
1841 | // report_kernel() in elfutils found vmlinux, but pretend it didn't. | |
1842 | // Given a non-null path, returning 1 means keep reporting modules. | |
1843 | mi->dwarf_status = info_absent; | |
1844 | } | |
6f4c1275 | 1845 | else if (debug_filename || main_filename) |
91af0778 | 1846 | { |
6f4c1275 FCE |
1847 | mi->elf_path = debug_filename ?: main_filename; |
1848 | } | |
1849 | else if (name == TOK_KERNEL) | |
1850 | { | |
1851 | mi->dwarf_status = info_absent; | |
91af0778 | 1852 | } |
91af0778 FCE |
1853 | } |
1854 | // OK, enough of that module_info caching business. | |
1855 | ||
5f0a03a6 | 1856 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 1857 | |
39bcd429 FCE |
1858 | // If we have enough information in the pattern to skip a module and |
1859 | // the module does not match that information, return early. | |
b8da0ad1 | 1860 | if (!q->dw.module_name_matches(q->module_val)) |
0e14e079 | 1861 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
0cbbf9d1 FCE |
1862 | |
1863 | // Don't allow module("*kernel*") type expressions to match the | |
1864 | // elfutils module "kernel", which we refer to in the probe | |
1865 | // point syntax exclusively as "kernel.*". | |
1866 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
0e14e079 | 1867 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
b5d77020 | 1868 | |
5f0a03a6 JK |
1869 | if (mod) |
1870 | validate_module_elf(mod, name, q); | |
1871 | else | |
91af0778 FCE |
1872 | assert(q->has_kernel); // and no vmlinux to examine |
1873 | ||
1874 | if (q->sess.verbose>2) | |
1875 | cerr << "focused on module '" << q->dw.module_name << "'\n"; | |
1876 | ||
1877 | ||
1878 | // Collect a few kernel addresses. XXX: these belong better | |
1879 | // to the sess.module_info["kernel"] struct. | |
1880 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 1881 | { |
91af0778 FCE |
1882 | if (! q->sess.sym_kprobes_text_start) |
1883 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
1884 | if (! q->sess.sym_kprobes_text_end) | |
1885 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
1886 | if (! q->sess.sym_stext) | |
1887 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
1888 | } |
1889 | ||
91af0778 | 1890 | // Finally, search the module for matches of the probe point. |
2c384610 | 1891 | q->handle_query_module(); |
bb788f9f | 1892 | |
91af0778 | 1893 | |
b8da0ad1 | 1894 | // If we know that there will be no more matches, abort early. |
0e14e079 | 1895 | if (q->dw.module_name_final_match(q->module_val) || pending_interrupts) |
b8da0ad1 FCE |
1896 | return DWARF_CB_ABORT; |
1897 | else | |
1898 | return DWARF_CB_OK; | |
7a053d3b | 1899 | } |
39bcd429 | 1900 | catch (const semantic_error& e) |
bd2b1e68 | 1901 | { |
39bcd429 FCE |
1902 | q->sess.print_error (e); |
1903 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1904 | } |
bd2b1e68 GH |
1905 | } |
1906 | ||
35d4ab18 | 1907 | |
de688825 | 1908 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 1909 | { |
77de5e9e | 1910 | dwarf_query & q; |
bcc12710 | 1911 | Dwarf_Die *scope_die; |
77de5e9e | 1912 | Dwarf_Addr addr; |
8c819921 | 1913 | block *add_block; |
2260f4e3 | 1914 | block *add_call_probe; // synthesized from .return probes with saved $vars |
af234c40 JS |
1915 | unsigned saved_longs, saved_strings; // data saved within kretprobes |
1916 | map<std::string, expression *> return_ts_map; | |
729455a7 | 1917 | vector<Dwarf_Die> scopes; |
b95e2b79 | 1918 | bool visited; |
77de5e9e | 1919 | |
de688825 | 1920 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
af234c40 JS |
1921 | q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL), |
1922 | saved_longs(0), saved_strings(0), visited(false) {} | |
70208613 JS |
1923 | expression* gen_mapped_saved_return(expression* e, const string& base_name); |
1924 | expression* gen_kretprobe_saved_return(expression* e, const string& base_name); | |
a7999c82 JS |
1925 | void visit_target_symbol_saved_return (target_symbol* e); |
1926 | void visit_target_symbol_context (target_symbol* e); | |
d7f3e0c5 | 1927 | void visit_target_symbol (target_symbol* e); |
c24447be | 1928 | void visit_cast_op (cast_op* e); |
729455a7 JS |
1929 | private: |
1930 | vector<Dwarf_Die>& getscopes(target_symbol *e); | |
77de5e9e GH |
1931 | }; |
1932 | ||
1933 | ||
de688825 | 1934 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 1935 | |
a50de939 DS |
1936 | |
1937 | var_expanding_visitor::var_expanding_visitor () | |
1938 | { | |
1939 | // FIXME: for the time being, by default we only support plain '$foo | |
1940 | // = bar', not '+=' or any other op= variant. This is fixable, but a | |
1941 | // bit ugly. | |
1942 | // | |
1943 | // If derived classes desire to add additional operator support, add | |
1944 | // new operators to this list in the derived class constructor. | |
1945 | valid_ops.insert ("="); | |
1946 | } | |
1947 | ||
1948 | ||
87214add JS |
1949 | bool |
1950 | var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop, | |
1951 | expression*& lvalue, expression*& rvalue) | |
77de5e9e | 1952 | { |
e57b735a GH |
1953 | // Our job would normally be to require() the left and right sides |
1954 | // into a new assignment. What we're doing is slightly trickier: | |
1955 | // we're pushing a functioncall** onto a stack, and if our left | |
1956 | // child sets the functioncall* for that value, we're going to | |
1957 | // assume our left child was a target symbol -- transformed into a | |
1958 | // set_target_foo(value) call, and it wants to take our right child | |
1959 | // as the argument "value". | |
1960 | // | |
1961 | // This is why some people claim that languages with | |
1962 | // constructor-decomposing case expressions have a leg up on | |
1963 | // visitors. | |
1964 | ||
1965 | functioncall *fcall = NULL; | |
d9b516ca | 1966 | |
a50de939 | 1967 | // Let visit_target_symbol know what operator it should handle. |
87214add JS |
1968 | const string* old_op = op; |
1969 | op = &eop; | |
a50de939 | 1970 | |
e57b735a | 1971 | target_symbol_setter_functioncalls.push (&fcall); |
87214add | 1972 | replace (lvalue); |
e57b735a | 1973 | target_symbol_setter_functioncalls.pop (); |
87214add JS |
1974 | replace (rvalue); |
1975 | ||
1976 | op = old_op; | |
e57b735a GH |
1977 | |
1978 | if (fcall != NULL) | |
77de5e9e | 1979 | { |
e57b735a GH |
1980 | // Our left child is informing us that it was a target variable |
1981 | // and it has been replaced with a set_target_foo() function | |
1982 | // call; we are going to provide that function call -- with the | |
1983 | // right child spliced in as sole argument -- in place of | |
de688825 | 1984 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a | 1985 | |
87214add | 1986 | if (valid_ops.find (eop) == valid_ops.end ()) |
a50de939 DS |
1987 | { |
1988 | // Build up a list of supported operators. | |
1989 | string ops; | |
1990 | std::set<string>::iterator i; | |
1991 | for (i = valid_ops.begin(); i != valid_ops.end(); i++) | |
1992 | ops += " " + *i + ","; | |
1993 | ops.resize(ops.size() - 1); // chop off the last ',' | |
1994 | ||
1995 | // Throw the error. | |
1996 | throw semantic_error ("Only the following assign operators are" | |
1997 | " implemented on target variables:" + ops, | |
87214add | 1998 | tok); |
a50de939 | 1999 | } |
e57b735a | 2000 | |
87214add JS |
2001 | assert (lvalue == fcall); |
2002 | if (rvalue) | |
2003 | fcall->args.push_back (rvalue); | |
4ed05b15 | 2004 | provide (fcall); |
87214add | 2005 | return true; |
77de5e9e | 2006 | } |
e57b735a | 2007 | else |
87214add JS |
2008 | return false; |
2009 | } | |
2010 | ||
2011 | ||
2012 | void | |
2013 | var_expanding_visitor::visit_assignment (assignment* e) | |
2014 | { | |
2015 | if (!rewrite_lvalue (e->tok, e->op, e->left, e->right)) | |
2016 | provide (e); | |
2017 | } | |
2018 | ||
2019 | ||
2020 | void | |
2021 | var_expanding_visitor::visit_pre_crement (pre_crement* e) | |
2022 | { | |
2023 | expression *dummy = NULL; | |
2024 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2025 | provide (e); | |
2026 | } | |
2027 | ||
2028 | ||
2029 | void | |
2030 | var_expanding_visitor::visit_post_crement (post_crement* e) | |
2031 | { | |
2032 | expression *dummy = NULL; | |
2033 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2034 | provide (e); | |
2035 | } | |
2036 | ||
2037 | ||
2038 | void | |
2039 | var_expanding_visitor::visit_delete_statement (delete_statement* s) | |
2040 | { | |
2041 | string fakeop = "delete"; | |
2042 | expression *dummy = NULL; | |
2043 | if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy)) | |
2044 | provide (s); | |
e57b735a | 2045 | } |
d9b516ca | 2046 | |
d7f3e0c5 | 2047 | |
30263a73 FCE |
2048 | void |
2049 | var_expanding_visitor::visit_defined_op (defined_op* e) | |
2050 | { | |
2051 | bool resolved = true; | |
2052 | ||
2053 | defined_ops.push (e); | |
2054 | try { | |
2055 | // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is | |
2056 | // defined with a target_symbol* operand, a subsidiary call may attempt to | |
2057 | // rewrite it to a general expression* instead, and require<> happily | |
2058 | // casts to/from void*, causing possible memory corruption. We use | |
2059 | // expression* here, being the general case of rewritten $variable. | |
2060 | expression *foo1 = e->operand; | |
2061 | foo1 = require (foo1); | |
2062 | ||
c69a87e0 | 2063 | // NB: Formerly, we had some curious cases to consider here, depending on what |
30263a73 | 2064 | // various visit_target_symbol() implementations do for successful or |
c69a87e0 FCE |
2065 | // erroneous resolutions. Some would signal a visit_target_symbol failure |
2066 | // with an exception, with a set flag within the target_symbol, or nothing | |
2067 | // at all. | |
30263a73 | 2068 | // |
c69a87e0 FCE |
2069 | // Now, failures always have to be signalled with a |
2070 | // saved_conversion_error being chained to the target_symbol. | |
2071 | // Successes have to result in an attempted rewrite of the | |
850bfddd | 2072 | // target_symbol (via provide()). |
780f11ff | 2073 | // |
c69a87e0 FCE |
2074 | // Edna Mode: "no capes". fche: "no exceptions". |
2075 | ||
30263a73 FCE |
2076 | // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error |
2077 | // | |
2078 | // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error | |
2079 | // | |
2080 | // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic | |
2081 | // dwarf probe to take care of it. | |
2082 | // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor | |
2083 | // to pass through @defined() to the synthetic dwarf probe. | |
780f11ff | 2084 | // |
30263a73 FCE |
2085 | // utrace: success: rewrites to function; failure: semantic_error |
2086 | // | |
850bfddd | 2087 | // procfs: success: rewrites to function; failure: semantic_error |
30263a73 FCE |
2088 | |
2089 | target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1); | |
c69a87e0 | 2090 | if (foo2 && foo2->saved_conversion_error) // failing |
30263a73 | 2091 | resolved = false; |
a45664f4 | 2092 | else if (foo2) // unresolved but not marked failing |
b7aedf26 | 2093 | { |
780f11ff JS |
2094 | // There are some visitors that won't touch certain target_symbols, |
2095 | // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should | |
2096 | // leave it for now so some other visitor can have a chance. | |
b7aedf26 JS |
2097 | e->operand = foo2; |
2098 | provide (e); | |
2099 | return; | |
2100 | } | |
30263a73 FCE |
2101 | else // resolved, rewritten to some other expression type |
2102 | resolved = true; | |
780f11ff | 2103 | } catch (const semantic_error& e) { |
c69a87e0 | 2104 | assert (0); // should not happen |
30263a73 FCE |
2105 | } |
2106 | defined_ops.pop (); | |
2107 | ||
2108 | literal_number* ln = new literal_number (resolved ? 1 : 0); | |
2109 | ln->tok = e->tok; | |
2110 | provide (ln); | |
2111 | } | |
2112 | ||
2113 | ||
5f36109e JS |
2114 | struct dwarf_pretty_print |
2115 | { | |
2116 | dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc, | |
2117 | const string& local, bool userspace_p, | |
2118 | const target_symbol& e): | |
2119 | dw(dw), local(local), scopes(scopes), pc(pc), | |
2120 | pointer(NULL), userspace_p(userspace_p) | |
2121 | { | |
2122 | init_ts (e); | |
2123 | dw.type_die_for_local (scopes, pc, local, ts, &base_type); | |
2124 | } | |
2125 | ||
2126 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc, | |
2127 | bool userspace_p, const target_symbol& e): | |
2128 | dw(dw), scopes(1, *scope_die), pc(pc), | |
2129 | pointer(NULL), userspace_p(userspace_p) | |
2130 | { | |
2131 | init_ts (e); | |
2132 | dw.type_die_for_return (&scopes[0], pc, ts, &base_type); | |
2133 | } | |
2134 | ||
2135 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer, | |
2136 | bool userspace_p, const target_symbol& e): | |
2137 | dw(dw), pc(0), pointer(pointer), pointer_type(*type_die), | |
2138 | userspace_p(userspace_p) | |
2139 | { | |
2140 | init_ts (e); | |
2141 | dw.type_die_for_pointer (type_die, ts, &base_type); | |
2142 | } | |
2143 | ||
2144 | functioncall* expand (); | |
2145 | ||
2146 | private: | |
2147 | dwflpp& dw; | |
2148 | target_symbol* ts; | |
7d11d8c9 | 2149 | bool print_full; |
5f36109e JS |
2150 | Dwarf_Die base_type; |
2151 | ||
2152 | string local; | |
2153 | vector<Dwarf_Die> scopes; | |
2154 | Dwarf_Addr pc; | |
2155 | ||
2156 | expression* pointer; | |
2157 | Dwarf_Die pointer_type; | |
2158 | ||
2159 | const bool userspace_p; | |
2160 | ||
2161 | void recurse (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2162 | print_format* pf, bool top=false); |
5f36109e | 2163 | void recurse_base (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2164 | print_format* pf); |
5f36109e | 2165 | void recurse_array (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2166 | print_format* pf, bool top); |
5f36109e | 2167 | void recurse_pointer (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2168 | print_format* pf, bool top); |
5f36109e | 2169 | void recurse_struct (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2170 | print_format* pf, bool top); |
5f36109e | 2171 | void recurse_struct_members (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2172 | print_format* pf, int& count); |
5f36109e JS |
2173 | |
2174 | void init_ts (const target_symbol& e); | |
2175 | expression* deref (target_symbol* e); | |
2176 | }; | |
2177 | ||
2178 | ||
2179 | void | |
2180 | dwarf_pretty_print::init_ts (const target_symbol& e) | |
2181 | { | |
2182 | // Work with a new target_symbol so we can modify arguments | |
2183 | ts = new target_symbol (e); | |
2184 | ||
2185 | if (ts->addressof) | |
2186 | throw semantic_error("cannot take address of pretty-printed variable", ts->tok); | |
2187 | ||
2188 | if (ts->components.empty() || | |
2189 | ts->components.back().type != target_symbol::comp_pretty_print) | |
2190 | throw semantic_error("invalid target_symbol for pretty-print", ts->tok); | |
7d11d8c9 | 2191 | print_full = ts->components.back().member.length() > 1; |
5f36109e JS |
2192 | ts->components.pop_back(); |
2193 | } | |
2194 | ||
2195 | ||
2196 | functioncall* | |
2197 | dwarf_pretty_print::expand () | |
2198 | { | |
2199 | static unsigned tick = 0; | |
2200 | ||
2201 | // function pretty_print_X([pointer], [arg1, arg2, ...]) { | |
7d11d8c9 JS |
2202 | // try { |
2203 | // return sprintf("{.foo=...}", (ts)->foo, ...) | |
2204 | // } catch { | |
2205 | // return "ERROR" | |
2206 | // } | |
5f36109e JS |
2207 | // } |
2208 | ||
2209 | // Create the function decl and call. | |
2210 | ||
2211 | functiondecl *fdecl = new functiondecl; | |
2212 | fdecl->tok = ts->tok; | |
2213 | fdecl->synthetic = true; | |
2214 | fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++); | |
2215 | fdecl->type = pe_string; | |
2216 | ||
2217 | functioncall* fcall = new functioncall; | |
2218 | fcall->tok = ts->tok; | |
2219 | fcall->function = fdecl->name; | |
2220 | fcall->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
2221 | fcall->referent = fdecl; // XXX | |
2222 | ||
2223 | // If there's a <pointer>, replace it with a new var and make that | |
2224 | // the first function argument. | |
2225 | if (pointer) | |
2226 | { | |
2227 | vardecl *v = new vardecl; | |
2228 | v->type = pe_long; | |
2229 | v->name = "pointer"; | |
2230 | v->tok = ts->tok; | |
2231 | fdecl->formal_args.push_back (v); | |
2232 | fcall->args.push_back (pointer); | |
2233 | ||
2234 | symbol* sym = new symbol; | |
2235 | sym->tok = ts->tok; | |
2236 | sym->name = v->name; | |
2237 | sym->referent = v; // XXX | |
2238 | pointer = sym; | |
2239 | } | |
2240 | ||
2241 | // For each expression argument, replace it with a function argument. | |
2242 | for (unsigned i = 0; i < ts->components.size(); ++i) | |
2243 | if (ts->components[i].type == target_symbol::comp_expression_array_index) | |
2244 | { | |
2245 | vardecl *v = new vardecl; | |
2246 | v->type = pe_long; | |
2247 | v->name = "index" + lex_cast(i); | |
2248 | v->tok = ts->tok; | |
2249 | fdecl->formal_args.push_back (v); | |
2250 | fcall->args.push_back (ts->components[i].expr_index); | |
2251 | ||
2252 | symbol* sym = new symbol; | |
2253 | sym->tok = ts->tok; | |
2254 | sym->name = v->name; | |
2255 | sym->referent = v; // XXX | |
2256 | ts->components[i].expr_index = sym; | |
2257 | } | |
2258 | ||
2259 | // Create the return sprintf. | |
2260 | token* pf_tok = new token(*ts->tok); | |
2261 | pf_tok->content = "sprintf"; | |
2262 | print_format* pf = print_format::create(pf_tok); | |
2263 | return_statement* rs = new return_statement; | |
2264 | rs->tok = ts->tok; | |
2265 | rs->value = pf; | |
5f36109e JS |
2266 | |
2267 | // Recurse into the actual values. | |
7d11d8c9 | 2268 | recurse (&base_type, ts, pf, true); |
5f36109e JS |
2269 | pf->components = print_format::string_to_components(pf->raw_components); |
2270 | ||
7d11d8c9 JS |
2271 | // Create the try-catch net |
2272 | try_block* tb = new try_block; | |
2273 | tb->tok = ts->tok; | |
2274 | tb->try_block = rs; | |
2275 | tb->catch_error_var = 0; | |
2276 | return_statement* rs2 = new return_statement; | |
2277 | rs2->tok = ts->tok; | |
2278 | rs2->value = new literal_string ("ERROR"); | |
2279 | rs2->value->tok = ts->tok; | |
2280 | tb->catch_block = rs2; | |
2281 | fdecl->body = tb; | |
2282 | ||
5f36109e JS |
2283 | dw.sess.functions[fdecl->name] = fdecl; |
2284 | return fcall; | |
2285 | } | |
2286 | ||
2287 | ||
2288 | void | |
2289 | dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e, | |
7d11d8c9 | 2290 | print_format* pf, bool top) |
5f36109e JS |
2291 | { |
2292 | Dwarf_Die type; | |
2293 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2294 | ||
2295 | switch (dwarf_tag(&type)) | |
2296 | { | |
2297 | default: | |
2298 | // XXX need a warning? | |
2299 | // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type)) | |
2300 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2301 | pf->raw_components.append("?"); | |
2302 | break; | |
2303 | ||
2304 | case DW_TAG_enumeration_type: | |
2305 | case DW_TAG_base_type: | |
7d11d8c9 | 2306 | recurse_base (&type, e, pf); |
5f36109e JS |
2307 | break; |
2308 | ||
2309 | case DW_TAG_array_type: | |
7d11d8c9 | 2310 | recurse_array (&type, e, pf, top); |
5f36109e JS |
2311 | break; |
2312 | ||
2313 | case DW_TAG_pointer_type: | |
2314 | case DW_TAG_reference_type: | |
2315 | case DW_TAG_rvalue_reference_type: | |
7d11d8c9 | 2316 | recurse_pointer (&type, e, pf, top); |
5f36109e JS |
2317 | break; |
2318 | ||
2319 | case DW_TAG_subroutine_type: | |
2320 | pf->raw_components.append("<function>:%p"); | |
2321 | pf->args.push_back(deref(e)); | |
2322 | break; | |
2323 | ||
2324 | case DW_TAG_union_type: | |
5f36109e JS |
2325 | case DW_TAG_structure_type: |
2326 | case DW_TAG_class_type: | |
7d11d8c9 | 2327 | recurse_struct (&type, e, pf, top); |
5f36109e JS |
2328 | break; |
2329 | } | |
2330 | } | |
2331 | ||
2332 | ||
2333 | void | |
2334 | dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2335 | print_format* pf) |
5f36109e JS |
2336 | { |
2337 | Dwarf_Attribute attr; | |
2338 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2339 | dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr), | |
2340 | &encoding); | |
2341 | bool push = true; | |
2342 | switch (encoding) | |
2343 | { | |
2344 | case DW_ATE_float: | |
2345 | case DW_ATE_complex_float: | |
2346 | // XXX need a warning? | |
2347 | // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding) | |
2348 | // + ") for " + dwarf_type_name(type), e->tok); | |
2349 | pf->raw_components.append("?"); | |
2350 | push = false; | |
2351 | break; | |
2352 | ||
2353 | case DW_ATE_signed_char: | |
2354 | case DW_ATE_unsigned_char: | |
2355 | pf->raw_components.append("'%c'"); | |
2356 | break; | |
2357 | ||
2358 | case DW_ATE_unsigned: | |
2359 | pf->raw_components.append("%u"); | |
2360 | break; | |
2361 | ||
2362 | default: | |
2363 | pf->raw_components.append("%i"); | |
2364 | break; | |
2365 | } | |
2366 | if (push) | |
2367 | pf->args.push_back(deref(e)); | |
2368 | } | |
2369 | ||
2370 | ||
2371 | void | |
2372 | dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2373 | print_format* pf, bool top) |
5f36109e | 2374 | { |
7d11d8c9 JS |
2375 | if (!top && !print_full) |
2376 | { | |
2377 | pf->raw_components.append("[...]"); | |
2378 | return; | |
2379 | } | |
2380 | ||
5f36109e JS |
2381 | Dwarf_Die childtype; |
2382 | dwarf_attr_die (type, DW_AT_type, &childtype); | |
2383 | pf->raw_components.append("["); | |
2384 | ||
2385 | // We print the array up to the first 5 elements. | |
2386 | // XXX how can we determine the array size? | |
2387 | // ... for now, just print the first element | |
2388 | unsigned i, size = 1; | |
2389 | for (i=0; i < size && i < 5; ++i) | |
2390 | { | |
2391 | if (i > 0) | |
2392 | pf->raw_components.append(", "); | |
2393 | target_symbol* e2 = new target_symbol(*e); | |
2394 | e2->components.push_back (target_symbol::component(e->tok, i)); | |
7d11d8c9 | 2395 | recurse (&childtype, e2, pf); |
5f36109e JS |
2396 | } |
2397 | if (i < size || 1/*XXX until real size is known */) | |
2398 | pf->raw_components.append(", ..."); | |
2399 | pf->raw_components.append("]"); | |
2400 | } | |
2401 | ||
2402 | ||
2403 | void | |
2404 | dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2405 | print_format* pf, bool top) |
5f36109e | 2406 | { |
7d11d8c9 JS |
2407 | // We chase to top-level pointers, but leave the rest alone |
2408 | Dwarf_Die pointee; | |
2409 | int typetag = dwarf_tag (type); | |
2410 | if (top | |
2411 | && (typetag == DW_TAG_pointer_type || | |
2412 | typetag == DW_TAG_reference_type || | |
2413 | typetag == DW_TAG_rvalue_reference_type) | |
2414 | && dwarf_attr_die (type, DW_AT_type, &pointee)) | |
5f36109e | 2415 | { |
7d11d8c9 | 2416 | recurse (&pointee, e, pf, top); |
5f36109e JS |
2417 | } |
2418 | else | |
2419 | { | |
7d11d8c9 JS |
2420 | pf->raw_components.append("%p"); |
2421 | pf->args.push_back(deref(e)); | |
5f36109e JS |
2422 | } |
2423 | } | |
2424 | ||
2425 | ||
2426 | void | |
2427 | dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2428 | print_format* pf, bool top) |
5f36109e | 2429 | { |
bdec0e18 JS |
2430 | if (dwarf_hasattr(type, DW_AT_declaration)) |
2431 | { | |
2432 | Dwarf_Die *resolved = dw.declaration_resolve(dwarf_diename(type)); | |
2433 | if (!resolved) | |
2434 | { | |
2435 | // could be an error, but for now just stub it | |
2436 | // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok); | |
2437 | pf->raw_components.append("{...}"); | |
2438 | return; | |
2439 | } | |
2440 | type = resolved; | |
2441 | } | |
2442 | ||
5f36109e JS |
2443 | int count = 0; |
2444 | pf->raw_components.append("{"); | |
7d11d8c9 JS |
2445 | if (top || print_full) |
2446 | recurse_struct_members (type, e, pf, count); | |
2447 | else | |
2448 | pf->raw_components.append("..."); | |
5f36109e JS |
2449 | pf->raw_components.append("}"); |
2450 | } | |
2451 | ||
2452 | ||
2453 | void | |
2454 | dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2455 | print_format* pf, int& count) |
5f36109e JS |
2456 | { |
2457 | Dwarf_Die child, childtype; | |
2458 | if (dwarf_child (type, &child) == 0) | |
2459 | do | |
2460 | { | |
2461 | target_symbol* e2 = e; | |
2462 | ||
2463 | // skip static members | |
2464 | if (dwarf_hasattr(&child, DW_AT_declaration)) | |
2465 | continue; | |
2466 | ||
2467 | int tag = dwarf_tag (&child); | |
2468 | ||
2469 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) | |
2470 | continue; | |
2471 | ||
2472 | dwarf_attr_die (&child, DW_AT_type, &childtype); | |
2473 | ||
2474 | if (tag == DW_TAG_inheritance) | |
2475 | { | |
7d11d8c9 | 2476 | recurse_struct_members (&childtype, e, pf, count); |
5f36109e JS |
2477 | continue; |
2478 | } | |
2479 | ||
2480 | int childtag = dwarf_tag (&childtype); | |
2481 | const char *member = dwarf_diename (&child); | |
2482 | if (++count > 1) | |
2483 | pf->raw_components.append(", "); | |
2484 | if (member) | |
2485 | { | |
2486 | // XXX should we filter out "_vptr.foo" members? | |
2487 | ||
2488 | pf->raw_components.append("."); | |
2489 | pf->raw_components.append(member); | |
2490 | ||
2491 | e2 = new target_symbol(*e); | |
2492 | e2->components.push_back (target_symbol::component(e->tok, member)); | |
2493 | } | |
2494 | else if (childtag == DW_TAG_union_type) | |
2495 | pf->raw_components.append("<union>"); | |
2496 | else if (childtag == DW_TAG_structure_type) | |
2497 | pf->raw_components.append("<class>"); | |
2498 | else if (childtag == DW_TAG_class_type) | |
2499 | pf->raw_components.append("<struct>"); | |
2500 | pf->raw_components.append("="); | |
7d11d8c9 | 2501 | recurse (&childtype, e2, pf); |
5f36109e JS |
2502 | } |
2503 | while (dwarf_siblingof (&child, &child) == 0); | |
2504 | } | |
2505 | ||
2506 | ||
2507 | expression* | |
2508 | dwarf_pretty_print::deref (target_symbol* e) | |
2509 | { | |
2510 | static unsigned tick = 0; | |
2511 | ||
2512 | // Synthesize a function to dereference the dwarf fields, | |
2513 | // with a pointer parameter that is the base tracepoint variable | |
2514 | functiondecl *fdecl = new functiondecl; | |
2515 | fdecl->synthetic = true; | |
2516 | fdecl->tok = e->tok; | |
2517 | embeddedcode *ec = new embeddedcode; | |
2518 | ec->tok = e->tok; | |
2519 | ||
2520 | fdecl->name = "_dwarf_pretty_print_deref_" + lex_cast(tick++); | |
2521 | fdecl->body = ec; | |
2522 | ||
2523 | // Synthesize a functioncall. | |
2524 | functioncall* fcall = new functioncall; | |
2525 | fcall->tok = e->tok; | |
2526 | fcall->function = fdecl->name; | |
2527 | fcall->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
2528 | fcall->referent = fdecl; // XXX | |
2529 | ||
2530 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime | |
2531 | ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n"; | |
2532 | ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n"; | |
2533 | ||
2534 | if (pointer) | |
2535 | { | |
2536 | ec->code += dw.literal_stmt_for_pointer (&pointer_type, e, | |
2537 | false, fdecl->type); | |
2538 | ||
2539 | vardecl *v = new vardecl; | |
2540 | v->type = pe_long; | |
2541 | v->name = "pointer"; | |
2542 | v->tok = e->tok; | |
2543 | fdecl->formal_args.push_back(v); | |
2544 | fcall->args.push_back(pointer); | |
2545 | } | |
2546 | else if (!local.empty()) | |
2547 | ec->code += dw.literal_stmt_for_local (scopes, pc, local, e, | |
2548 | false, fdecl->type); | |
2549 | else | |
2550 | ec->code += dw.literal_stmt_for_return (&scopes[0], pc, e, | |
2551 | false, fdecl->type); | |
2552 | ||
2553 | // Any non-literal indexes need to be passed in too. | |
2554 | for (unsigned i = 0; i < e->components.size(); ++i) | |
2555 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
2556 | { | |
2557 | vardecl *v = new vardecl; | |
2558 | v->type = pe_long; | |
2559 | v->name = "index" + lex_cast(i); | |
2560 | v->tok = e->tok; | |
2561 | fdecl->formal_args.push_back(v); | |
2562 | fcall->args.push_back(e->components[i].expr_index); | |
2563 | } | |
2564 | ||
2565 | ec->code += "/* pure */"; | |
2566 | ec->code += "/* unprivileged */"; | |
2567 | ||
2568 | // PR10601 | |
2569 | ec->code += "\n#undef fetch_register\n"; | |
2570 | ec->code += "\n#undef store_register\n"; | |
2571 | ||
2572 | dw.sess.functions[fdecl->name] = fdecl; | |
2573 | return fcall; | |
2574 | } | |
2575 | ||
2576 | ||
e57b735a | 2577 | void |
a7999c82 | 2578 | dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e) |
e57b735a | 2579 | { |
a7999c82 JS |
2580 | // Get the full name of the target symbol. |
2581 | stringstream ts_name_stream; | |
2582 | e->print(ts_name_stream); | |
2583 | string ts_name = ts_name_stream.str(); | |
2584 | ||
2585 | // Check and make sure we haven't already seen this target | |
2586 | // variable in this return probe. If we have, just return our | |
2587 | // last replacement. | |
af234c40 | 2588 | map<string, expression *>::iterator i = return_ts_map.find(ts_name); |
a7999c82 | 2589 | if (i != return_ts_map.end()) |
85ecf79a | 2590 | { |
a7999c82 JS |
2591 | provide (i->second); |
2592 | return; | |
2593 | } | |
85ecf79a | 2594 | |
70208613 JS |
2595 | // Attempt the expansion directly first, so if there's a problem with the |
2596 | // variable we won't have a bogus entry probe lying around. Like in | |
2597 | // saveargs(), we pretend for a moment that we're not in a .return. | |
2598 | bool saved_has_return = q.has_return; | |
2599 | q.has_return = false; | |
2600 | expression *repl = e; | |
2601 | replace (repl); | |
2602 | q.has_return = saved_has_return; | |
2603 | target_symbol* n = dynamic_cast<target_symbol*>(repl); | |
2604 | if (n && n->saved_conversion_error) | |
2605 | { | |
2606 | provide (repl); | |
2607 | return; | |
2608 | } | |
2609 | ||
af234c40 JS |
2610 | expression *exp; |
2611 | if (!q.has_process && | |
2612 | strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0) | |
70208613 | 2613 | exp = gen_kretprobe_saved_return(repl, e->base_name); |
af234c40 | 2614 | else |
70208613 | 2615 | exp = gen_mapped_saved_return(repl, e->base_name); |
af234c40 JS |
2616 | |
2617 | // Provide the variable to our parent so it can be used as a | |
2618 | // substitute for the target symbol. | |
2619 | provide (exp); | |
2620 | ||
2621 | // Remember this replacement since we might be able to reuse | |
2622 | // it later if the same return probe references this target | |
2623 | // symbol again. | |
2624 | return_ts_map[ts_name] = exp; | |
2625 | } | |
2626 | ||
2627 | expression* | |
70208613 JS |
2628 | dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e, |
2629 | const string& base_name) | |
af234c40 | 2630 | { |
a7999c82 JS |
2631 | // We've got to do several things here to handle target |
2632 | // variables in return probes. | |
85ecf79a | 2633 | |
a7999c82 JS |
2634 | // (1) Synthesize two global arrays. One is the cache of the |
2635 | // target variable and the other contains a thread specific | |
2636 | // nesting level counter. The arrays will look like | |
2637 | // this: | |
2638 | // | |
2639 | // _dwarf_tvar_{name}_{num} | |
2640 | // _dwarf_tvar_{name}_{num}_ctr | |
2641 | ||
2642 | string aname = (string("_dwarf_tvar_") | |
70208613 | 2643 | + base_name.substr(1) |
aca66a36 | 2644 | + "_" + lex_cast(tick++)); |
a7999c82 JS |
2645 | vardecl* vd = new vardecl; |
2646 | vd->name = aname; | |
2647 | vd->tok = e->tok; | |
2648 | q.sess.globals.push_back (vd); | |
2649 | ||
2650 | string ctrname = aname + "_ctr"; | |
2651 | vd = new vardecl; | |
2652 | vd->name = ctrname; | |
2653 | vd->tok = e->tok; | |
2654 | q.sess.globals.push_back (vd); | |
2655 | ||
2656 | // (2) Create a new code block we're going to insert at the | |
2657 | // beginning of this probe to get the cached value into a | |
2658 | // temporary variable. We'll replace the target variable | |
2659 | // reference with the temporary variable reference. The code | |
2660 | // will look like this: | |
2661 | // | |
2662 | // _dwarf_tvar_tid = tid() | |
2663 | // _dwarf_tvar_{name}_{num}_tmp | |
2664 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2665 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2666 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2667 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
2668 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
2669 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
2670 | ||
2671 | // (2a) Synthesize the tid temporary expression, which will look | |
2672 | // like this: | |
2673 | // | |
2674 | // _dwarf_tvar_tid = tid() | |
2675 | symbol* tidsym = new symbol; | |
2676 | tidsym->name = string("_dwarf_tvar_tid"); | |
2677 | tidsym->tok = e->tok; | |
85ecf79a | 2678 | |
a7999c82 JS |
2679 | if (add_block == NULL) |
2680 | { | |
2681 | add_block = new block; | |
2682 | add_block->tok = e->tok; | |
8c819921 | 2683 | |
a7999c82 JS |
2684 | // Synthesize a functioncall to grab the thread id. |
2685 | functioncall* fc = new functioncall; | |
2686 | fc->tok = e->tok; | |
2687 | fc->function = string("tid"); | |
8c819921 | 2688 | |
a7999c82 | 2689 | // Assign the tid to '_dwarf_tvar_tid'. |
8c819921 DS |
2690 | assignment* a = new assignment; |
2691 | a->tok = e->tok; | |
2692 | a->op = "="; | |
a7999c82 JS |
2693 | a->left = tidsym; |
2694 | a->right = fc; | |
8c819921 DS |
2695 | |
2696 | expr_statement* es = new expr_statement; | |
2697 | es->tok = e->tok; | |
2698 | es->value = a; | |
8c819921 | 2699 | add_block->statements.push_back (es); |
a7999c82 | 2700 | } |
8c819921 | 2701 | |
a7999c82 JS |
2702 | // (2b) Synthesize an array reference and assign it to a |
2703 | // temporary variable (that we'll use as replacement for the | |
2704 | // target variable reference). It will look like this: | |
2705 | // | |
2706 | // _dwarf_tvar_{name}_{num}_tmp | |
2707 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2708 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2709 | ||
2710 | arrayindex* ai_tvar_base = new arrayindex; | |
2711 | ai_tvar_base->tok = e->tok; | |
2712 | ||
2713 | symbol* sym = new symbol; | |
2714 | sym->name = aname; | |
2715 | sym->tok = e->tok; | |
2716 | ai_tvar_base->base = sym; | |
2717 | ||
2718 | ai_tvar_base->indexes.push_back(tidsym); | |
2719 | ||
2720 | // We need to create a copy of the array index in its current | |
2721 | // state so we can have 2 variants of it (the original and one | |
2722 | // that post-decrements the second index). | |
2723 | arrayindex* ai_tvar = new arrayindex; | |
2724 | arrayindex* ai_tvar_postdec = new arrayindex; | |
2725 | *ai_tvar = *ai_tvar_base; | |
2726 | *ai_tvar_postdec = *ai_tvar_base; | |
2727 | ||
2728 | // Synthesize the | |
2729 | // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the | |
2730 | // second index into the array. | |
2731 | arrayindex* ai_ctr = new arrayindex; | |
2732 | ai_ctr->tok = e->tok; | |
2733 | ||
2734 | sym = new symbol; | |
2735 | sym->name = ctrname; | |
2736 | sym->tok = e->tok; | |
2737 | ai_ctr->base = sym; | |
2738 | ai_ctr->indexes.push_back(tidsym); | |
2739 | ai_tvar->indexes.push_back(ai_ctr); | |
2740 | ||
2741 | symbol* tmpsym = new symbol; | |
2742 | tmpsym->name = aname + "_tmp"; | |
2743 | tmpsym->tok = e->tok; | |
2744 | ||
2745 | assignment* a = new assignment; | |
2746 | a->tok = e->tok; | |
2747 | a->op = "="; | |
2748 | a->left = tmpsym; | |
2749 | a->right = ai_tvar; | |
2750 | ||
2751 | expr_statement* es = new expr_statement; | |
2752 | es->tok = e->tok; | |
2753 | es->value = a; | |
2754 | ||
2755 | add_block->statements.push_back (es); | |
2756 | ||
2757 | // (2c) Add a post-decrement to the second array index and | |
2758 | // delete the array value. It will look like this: | |
2759 | // | |
2760 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2761 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
2762 | ||
2763 | post_crement* pc = new post_crement; | |
2764 | pc->tok = e->tok; | |
2765 | pc->op = "--"; | |
2766 | pc->operand = ai_ctr; | |
2767 | ai_tvar_postdec->indexes.push_back(pc); | |
2768 | ||
2769 | delete_statement* ds = new delete_statement; | |
2770 | ds->tok = e->tok; | |
2771 | ds->value = ai_tvar_postdec; | |
2772 | ||
2773 | add_block->statements.push_back (ds); | |
2774 | ||
2775 | // (2d) Delete the counter value if it is 0. It will look like | |
2776 | // this: | |
2777 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
2778 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
2779 | ||
2780 | ds = new delete_statement; | |
2781 | ds->tok = e->tok; | |
2782 | ds->value = ai_ctr; | |
2783 | ||
2784 | unary_expression *ue = new unary_expression; | |
2785 | ue->tok = e->tok; | |
2786 | ue->op = "!"; | |
2787 | ue->operand = ai_ctr; | |
2788 | ||
2789 | if_statement *ifs = new if_statement; | |
2790 | ifs->tok = e->tok; | |
2791 | ifs->condition = ue; | |
2792 | ifs->thenblock = ds; | |
2793 | ifs->elseblock = NULL; | |
2794 | ||
2795 | add_block->statements.push_back (ifs); | |
2796 | ||
2797 | // (3) We need an entry probe that saves the value for us in the | |
2798 | // global array we created. Create the entry probe, which will | |
2799 | // look like this: | |
2800 | // | |
2260f4e3 | 2801 | // probe kernel.function("{function}").call { |
a7999c82 JS |
2802 | // _dwarf_tvar_tid = tid() |
2803 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2804 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2805 | // = ${param} | |
2806 | // } | |
2807 | ||
2260f4e3 | 2808 | if (add_call_probe == NULL) |
a7999c82 | 2809 | { |
2260f4e3 FCE |
2810 | add_call_probe = new block; |
2811 | add_call_probe->tok = e->tok; | |
4baf0e53 | 2812 | |
a7999c82 JS |
2813 | // Synthesize a functioncall to grab the thread id. |
2814 | functioncall* fc = new functioncall; | |
2815 | fc->tok = e->tok; | |
2816 | fc->function = string("tid"); | |
4baf0e53 | 2817 | |
a7999c82 JS |
2818 | // Assign the tid to '_dwarf_tvar_tid'. |
2819 | assignment* a = new assignment; | |
8fc05e57 DS |
2820 | a->tok = e->tok; |
2821 | a->op = "="; | |
a7999c82 JS |
2822 | a->left = tidsym; |
2823 | a->right = fc; | |
8fc05e57 | 2824 | |
a7999c82 | 2825 | expr_statement* es = new expr_statement; |
8fc05e57 DS |
2826 | es->tok = e->tok; |
2827 | es->value = a; | |
2260f4e3 | 2828 | add_call_probe = new block(add_call_probe, es); |
85ecf79a | 2829 | } |
cf2a1f85 | 2830 | |
a7999c82 JS |
2831 | // Save the value, like this: |
2832 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2833 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2834 | // = ${param} | |
2835 | arrayindex* ai_tvar_preinc = new arrayindex; | |
2836 | *ai_tvar_preinc = *ai_tvar_base; | |
2837 | ||
2838 | pre_crement* preinc = new pre_crement; | |
2839 | preinc->tok = e->tok; | |
2840 | preinc->op = "++"; | |
2841 | preinc->operand = ai_ctr; | |
2842 | ai_tvar_preinc->indexes.push_back(preinc); | |
2843 | ||
2844 | a = new assignment; | |
2845 | a->tok = e->tok; | |
2846 | a->op = "="; | |
2847 | a->left = ai_tvar_preinc; | |
2848 | a->right = e; | |
2849 | ||
2850 | es = new expr_statement; | |
2851 | es->tok = e->tok; | |
2852 | es->value = a; | |
2853 | ||
2260f4e3 | 2854 | add_call_probe = new block(add_call_probe, es); |
a7999c82 JS |
2855 | |
2856 | // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to | |
2857 | // our parent so it can be used as a substitute for the target | |
2858 | // symbol. | |
af234c40 JS |
2859 | return tmpsym; |
2860 | } | |
a7999c82 | 2861 | |
af234c40 JS |
2862 | |
2863 | expression* | |
70208613 JS |
2864 | dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e, |
2865 | const string& base_name) | |
af234c40 JS |
2866 | { |
2867 | // The code for this is simple. | |
2868 | // | |
2869 | // .call: | |
2870 | // _set_kretprobe_long(index, $value) | |
2871 | // | |
2872 | // .return: | |
2873 | // _get_kretprobe_long(index) | |
2874 | // | |
2875 | // (or s/long/string/ for things like $$parms) | |
2876 | ||
2877 | unsigned index; | |
2878 | string setfn, getfn; | |
2879 | ||
2880 | // Cheesy way to predetermine that this is a string -- if the second | |
2881 | // character is a '$', then we're looking at a $$vars, $$parms, or $$locals. | |
2882 | // XXX We need real type resolution here, especially if we are ever to | |
2883 | // support an @entry construct. | |
70208613 | 2884 | if (base_name[1] == '$') |
af234c40 JS |
2885 | { |
2886 | index = saved_strings++; | |
2887 | setfn = "_set_kretprobe_string"; | |
2888 | getfn = "_get_kretprobe_string"; | |
2889 | } | |
2890 | else | |
2891 | { | |
2892 | index = saved_longs++; | |
2893 | setfn = "_set_kretprobe_long"; | |
2894 | getfn = "_get_kretprobe_long"; | |
2895 | } | |
2896 | ||
2897 | // Create the entry code | |
2898 | // _set_kretprobe_{long|string}(index, $value) | |
2899 | ||
2900 | if (add_call_probe == NULL) | |
2901 | { | |
2902 | add_call_probe = new block; | |
2903 | add_call_probe->tok = e->tok; | |
2904 | } | |
2905 | ||
2906 | functioncall* set_fc = new functioncall; | |
2907 | set_fc->tok = e->tok; | |
2908 | set_fc->function = setfn; | |
2909 | set_fc->args.push_back(new literal_number(index)); | |
2910 | set_fc->args.back()->tok = e->tok; | |
2911 | set_fc->args.push_back(e); | |
2912 | ||
2913 | expr_statement* set_es = new expr_statement; | |
2914 | set_es->tok = e->tok; | |
2915 | set_es->value = set_fc; | |
2916 | ||
2917 | add_call_probe->statements.push_back(set_es); | |
2918 | ||
2919 | // Create the return code | |
2920 | // _get_kretprobe_{long|string}(index) | |
2921 | ||
2922 | functioncall* get_fc = new functioncall; | |
2923 | get_fc->tok = e->tok; | |
2924 | get_fc->function = getfn; | |
2925 | get_fc->args.push_back(new literal_number(index)); | |
2926 | get_fc->args.back()->tok = e->tok; | |
2927 | ||
2928 | return get_fc; | |
a7999c82 | 2929 | } |
a43ba433 | 2930 | |
2cb3fe26 | 2931 | |
a7999c82 JS |
2932 | void |
2933 | dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
2934 | { | |
9aa8ffce | 2935 | if (null_die(scope_die)) |
a7999c82 | 2936 | return; |
2cb3fe26 | 2937 | |
5f36109e JS |
2938 | target_symbol *tsym = new target_symbol(*e); |
2939 | ||
2940 | string format = "=%#x"; | |
2941 | if (!e->components.empty() && | |
2942 | e->components[0].type == target_symbol::comp_pretty_print) | |
2943 | format = "=%s"; | |
a43ba433 | 2944 | |
a7999c82 JS |
2945 | // Convert $$parms to sprintf of a list of parms and active local vars |
2946 | // which we recursively evaluate | |
a43ba433 | 2947 | |
a7999c82 JS |
2948 | // NB: we synthesize a new token here rather than reusing |
2949 | // e->tok, because print_format::print likes to use | |
2950 | // its tok->content. | |
5f36109e | 2951 | token* pf_tok = new token(*e->tok); |
a7999c82 | 2952 | pf_tok->type = tok_identifier; |
b393f6f2 | 2953 | pf_tok->content = "sprintf"; |
2cb3fe26 | 2954 | |
d5e178c1 | 2955 | print_format* pf = print_format::create(pf_tok); |
a7999c82 JS |
2956 | |
2957 | if (q.has_return && (e->base_name == "$$return")) | |
2958 | { | |
a7999c82 JS |
2959 | tsym->base_name = "$return"; |
2960 | ||
2961 | // Ignore any variable that isn't accessible. | |
2962 | tsym->saved_conversion_error = 0; | |
2963 | expression *texp = tsym; | |
8b095b45 | 2964 | replace (texp); // NB: throws nothing ... |
a7999c82 | 2965 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
a43ba433 | 2966 | { |
2cb3fe26 | 2967 | |
a43ba433 FCE |
2968 | } |
2969 | else | |
2970 | { | |
a7999c82 | 2971 | pf->raw_components += "return"; |
5f36109e | 2972 | pf->raw_components += format; |
a7999c82 JS |
2973 | pf->args.push_back(texp); |
2974 | } | |
2975 | } | |
2976 | else | |
2977 | { | |
2978 | // non-.return probe: support $$parms, $$vars, $$locals | |
345bbb3d | 2979 | bool first = true; |
a7999c82 | 2980 | Dwarf_Die result; |
9aa8ffce | 2981 | vector<Dwarf_Die> scopes = q.dw.getscopes_die(scope_die); |
a7999c82 JS |
2982 | if (dwarf_child (&scopes[0], &result) == 0) |
2983 | do | |
2984 | { | |
2985 | switch (dwarf_tag (&result)) | |
00cf3709 | 2986 | { |
a7999c82 JS |
2987 | case DW_TAG_variable: |
2988 | if (e->base_name == "$$parms") | |
2989 | continue; | |
2990 | break; | |
2991 | case DW_TAG_formal_parameter: | |
2992 | if (e->base_name == "$$locals") | |
2993 | continue; | |
2994 | break; | |
2995 | ||
2996 | default: | |
2997 | continue; | |
2998 | } | |
41c262f3 | 2999 | |
a7999c82 JS |
3000 | const char *diename = dwarf_diename (&result); |
3001 | if (! diename) continue; | |
f76427a2 | 3002 | |
345bbb3d MW |
3003 | if (! first) |
3004 | pf->raw_components += " "; | |
3005 | pf->raw_components += diename; | |
3006 | ||
a7999c82 JS |
3007 | tsym->base_name = "$"; |
3008 | tsym->base_name += diename; | |
41c262f3 | 3009 | |
a7999c82 JS |
3010 | // Ignore any variable that isn't accessible. |
3011 | tsym->saved_conversion_error = 0; | |
3012 | expression *texp = tsym; | |
8b095b45 | 3013 | replace (texp); // NB: throws nothing ... |
a7999c82 JS |
3014 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
3015 | { | |
3016 | if (q.sess.verbose>2) | |
a43ba433 | 3017 | { |
a7999c82 JS |
3018 | for (semantic_error *c = tsym->saved_conversion_error; |
3019 | c != 0; | |
3020 | c = c->chain) { | |
3021 | clog << "variable location problem: " << c->what() << endl; | |
3022 | } | |
a43ba433 | 3023 | } |
a7999c82 | 3024 | |
345bbb3d | 3025 | pf->raw_components += "=?"; |
00cf3709 | 3026 | } |
a7999c82 JS |
3027 | else |
3028 | { | |
5f36109e | 3029 | pf->raw_components += format; |
a7999c82 JS |
3030 | pf->args.push_back(texp); |
3031 | } | |
345bbb3d | 3032 | first = false; |
a7999c82 JS |
3033 | } |
3034 | while (dwarf_siblingof (&result, &result) == 0); | |
3035 | } | |
2cb3fe26 | 3036 | |
a7999c82 JS |
3037 | pf->components = print_format::string_to_components(pf->raw_components); |
3038 | provide (pf); | |
3039 | } | |
3040 | ||
2cb3fe26 | 3041 | |
a7999c82 JS |
3042 | void |
3043 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
3044 | { | |
3045 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
3046 | visited = true; | |
30263a73 FCE |
3047 | bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e)); |
3048 | // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars | |
a7999c82 | 3049 | |
70208613 | 3050 | try |
a7999c82 | 3051 | { |
c69a87e0 FCE |
3052 | bool lvalue = is_active_lvalue(e); |
3053 | if (lvalue && !q.sess.guru_mode) | |
3054 | throw semantic_error("write to target variable not permitted", e->tok); | |
2cb3fe26 | 3055 | |
c69a87e0 | 3056 | // XXX: process $context vars should be writeable |
70208613 | 3057 | |
c69a87e0 FCE |
3058 | // See if we need to generate a new probe to save/access function |
3059 | // parameters from a return probe. PR 1382. | |
3060 | if (q.has_return | |
3061 | && !defined_being_checked | |
3062 | && e->base_name != "$return" // not the special return-value variable handled below | |
3063 | && e->base_name != "$$return") // nor the other special variable handled below | |
3064 | { | |
3065 | if (lvalue) | |
3066 | throw semantic_error("write to target variable not permitted in .return probes", e->tok); | |
3067 | visit_target_symbol_saved_return(e); | |
3068 | return; | |
3069 | } | |
e57b735a | 3070 | |
c69a87e0 FCE |
3071 | if (e->base_name == "$$vars" |
3072 | || e->base_name == "$$parms" | |
3073 | || e->base_name == "$$locals" | |
3074 | || (q.has_return && (e->base_name == "$$return"))) | |
3075 | { | |
3076 | if (lvalue) | |
3077 | throw semantic_error("cannot write to context variable", e->tok); | |
70208613 | 3078 | |
c69a87e0 FCE |
3079 | if (e->addressof) |
3080 | throw semantic_error("cannot take address of context variable", e->tok); | |
70208613 | 3081 | |
5f36109e JS |
3082 | e->assert_no_components("dwarf", true); |
3083 | ||
c69a87e0 FCE |
3084 | visit_target_symbol_context(e); |
3085 | return; | |
3086 | } | |
70208613 | 3087 | |
5f36109e JS |
3088 | if (!e->components.empty() && |
3089 | e->components.back().type == target_symbol::comp_pretty_print) | |
3090 | { | |
3091 | if (lvalue) | |
3092 | throw semantic_error("cannot write to pretty-printed variable", e->tok); | |
3093 | ||
3094 | if (q.has_return && (e->base_name == "$return")) | |
3095 | { | |
3096 | dwarf_pretty_print dpp (q.dw, scope_die, addr, | |
3097 | q.has_process, *e); | |
3098 | dpp.expand()->visit(this); | |
3099 | } | |
3100 | else | |
3101 | { | |
3102 | dwarf_pretty_print dpp (q.dw, getscopes(e), addr, | |
3103 | e->base_name.substr(1), | |
3104 | q.has_process, *e); | |
3105 | dpp.expand()->visit(this); | |
3106 | } | |
3107 | return; | |
3108 | } | |
3109 | ||
c69a87e0 FCE |
3110 | // Synthesize a function. |
3111 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 3112 | fdecl->synthetic = true; |
c69a87e0 FCE |
3113 | fdecl->tok = e->tok; |
3114 | embeddedcode *ec = new embeddedcode; | |
3115 | ec->tok = e->tok; | |
70208613 | 3116 | |
c69a87e0 FCE |
3117 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
3118 | + "_" + e->base_name.substr(1) | |
3119 | + "_" + lex_cast(tick++)); | |
70208613 | 3120 | |
b5a0dd41 FCE |
3121 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
3122 | ec->code += "\n#define fetch_register " + string(q.has_process?"u":"k") + "_fetch_register\n"; | |
3123 | ec->code += "#define store_register " + string(q.has_process?"u":"k") + "_store_register\n"; | |
70208613 | 3124 | |
a43ba433 | 3125 | if (q.has_return && (e->base_name == "$return")) |
e19fda4e | 3126 | { |
b5a0dd41 | 3127 | ec->code += q.dw.literal_stmt_for_return (scope_die, |
e19fda4e | 3128 | addr, |
b4c34c26 | 3129 | e, |
e19fda4e DS |
3130 | lvalue, |
3131 | fdecl->type); | |
3132 | } | |
3133 | else | |
3134 | { | |
b5a0dd41 | 3135 | ec->code += q.dw.literal_stmt_for_local (getscopes(e), |
e19fda4e DS |
3136 | addr, |
3137 | e->base_name.substr(1), | |
b4c34c26 | 3138 | e, |
e19fda4e DS |
3139 | lvalue, |
3140 | fdecl->type); | |
3141 | } | |
3142 | ||
1b07c728 FCE |
3143 | if (! lvalue) |
3144 | ec->code += "/* pure */"; | |
64211010 DB |
3145 | |
3146 | ec->code += "/* unprivileged */"; | |
b5a0dd41 FCE |
3147 | |
3148 | // PR10601 | |
3149 | ec->code += "\n#undef fetch_register\n"; | |
3150 | ec->code += "\n#undef store_register\n"; | |
c69a87e0 FCE |
3151 | |
3152 | fdecl->name = fname; | |
3153 | fdecl->body = ec; | |
70208613 | 3154 | |
c69a87e0 FCE |
3155 | // Any non-literal indexes need to be passed in too. |
3156 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3157 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3158 | { | |
3159 | vardecl *v = new vardecl; | |
3160 | v->type = pe_long; | |
3161 | v->name = "index" + lex_cast(i); | |
3162 | v->tok = e->tok; | |
3163 | fdecl->formal_args.push_back(v); | |
3164 | } | |
70208613 | 3165 | |
c69a87e0 FCE |
3166 | if (lvalue) |
3167 | { | |
3168 | // Modify the fdecl so it carries a single pe_long formal | |
3169 | // argument called "value". | |
70208613 | 3170 | |
c69a87e0 FCE |
3171 | // FIXME: For the time being we only support setting target |
3172 | // variables which have base types; these are 'pe_long' in | |
3173 | // stap's type vocabulary. Strings and pointers might be | |
3174 | // reasonable, some day, but not today. | |
70208613 | 3175 | |
c69a87e0 FCE |
3176 | vardecl *v = new vardecl; |
3177 | v->type = pe_long; | |
3178 | v->name = "value"; | |
3179 | v->tok = e->tok; | |
3180 | fdecl->formal_args.push_back(v); | |
3181 | } | |
3182 | q.sess.functions[fdecl->name]=fdecl; | |
70208613 | 3183 | |
c69a87e0 FCE |
3184 | // Synthesize a functioncall. |
3185 | functioncall* n = new functioncall; | |
3186 | n->tok = e->tok; | |
3187 | n->function = fname; | |
3188 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
70208613 | 3189 | |
c69a87e0 FCE |
3190 | // Any non-literal indexes need to be passed in too. |
3191 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3192 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3193 | n->args.push_back(require(e->components[i].expr_index)); | |
70208613 | 3194 | |
c69a87e0 FCE |
3195 | if (lvalue) |
3196 | { | |
3197 | // Provide the functioncall to our parent, so that it can be | |
3198 | // used to substitute for the assignment node immediately above | |
3199 | // us. | |
3200 | assert(!target_symbol_setter_functioncalls.empty()); | |
3201 | *(target_symbol_setter_functioncalls.top()) = n; | |
3202 | } | |
70208613 | 3203 | |
c69a87e0 | 3204 | provide (n); |
66d284f4 FCE |
3205 | } |
3206 | catch (const semantic_error& er) | |
3207 | { | |
9fab2262 JS |
3208 | // We suppress this error message, and pass the unresolved |
3209 | // target_symbol to the next pass. We hope that this value ends | |
3210 | // up not being referenced after all, so it can be optimized out | |
3211 | // quietly. | |
1af1e62d | 3212 | e->chain (er); |
9fab2262 | 3213 | provide (e); |
66d284f4 | 3214 | } |
77de5e9e GH |
3215 | } |
3216 | ||
3217 | ||
c24447be JS |
3218 | void |
3219 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
3220 | { | |
3221 | // Fill in our current module context if needed | |
3222 | if (e->module.empty()) | |
3223 | e->module = q.dw.module_name; | |
3224 | ||
3225 | var_expanding_visitor::visit_cast_op(e); | |
3226 | } | |
3227 | ||
3228 | ||
729455a7 JS |
3229 | vector<Dwarf_Die>& |
3230 | dwarf_var_expanding_visitor::getscopes(target_symbol *e) | |
3231 | { | |
3232 | if (scopes.empty()) | |
3233 | { | |
3234 | // If the address is at the beginning of the scope_die, we can do a fast | |
3235 | // getscopes from there. Otherwise we need to look it up by address. | |
3236 | Dwarf_Addr entrypc; | |
3237 | if (q.dw.die_entrypc(scope_die, &entrypc) && entrypc == addr) | |
3238 | scopes = q.dw.getscopes(scope_die); | |
3239 | else | |
3240 | scopes = q.dw.getscopes(addr); | |
3241 | ||
3242 | if (scopes.empty()) | |
3243 | throw semantic_error ("unable to find any scopes containing " | |
3244 | + lex_cast_hex(addr) | |
3245 | + ((scope_die == NULL) ? "" | |
3246 | : (string (" in ") | |
3247 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
3248 | + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") | |
3249 | + ")")) | |
3250 | + " while searching for local '" | |
3251 | + e->base_name.substr(1) + "'", | |
3252 | e->tok); | |
3253 | } | |
3254 | return scopes; | |
3255 | } | |
3256 | ||
3257 | ||
5f36109e JS |
3258 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor |
3259 | { | |
3260 | systemtap_session& s; | |
3261 | dwarf_builder& db; | |
3262 | ||
3263 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
3264 | s(s), db(db) {} | |
3265 | void visit_cast_op (cast_op* e); | |
3266 | void filter_special_modules(string& module); | |
3267 | }; | |
3268 | ||
3269 | ||
c4ce66a1 JS |
3270 | struct dwarf_cast_query : public base_query |
3271 | { | |
946e1a48 | 3272 | cast_op& e; |
c4ce66a1 | 3273 | const bool lvalue; |
5f36109e JS |
3274 | const bool userspace_p; |
3275 | functioncall*& result; | |
c4ce66a1 | 3276 | |
5f36109e JS |
3277 | dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue, |
3278 | const bool userspace_p, functioncall*& result): | |
abb41d92 | 3279 | base_query(dw, module), e(e), lvalue(lvalue), |
5f36109e | 3280 | userspace_p(userspace_p), result(result) {} |
c4ce66a1 JS |
3281 | |
3282 | void handle_query_module(); | |
c4ce66a1 JS |
3283 | }; |
3284 | ||
3285 | ||
c4ce66a1 JS |
3286 | void |
3287 | dwarf_cast_query::handle_query_module() | |
3288 | { | |
5f36109e JS |
3289 | static unsigned tick = 0; |
3290 | ||
3291 | if (result) | |
c4ce66a1 JS |
3292 | return; |
3293 | ||
ea1e477a JS |
3294 | // look for the type in any CU |
3295 | Dwarf_Die* type_die = dw.declaration_resolve_other_cus(e.type.c_str()); | |
3296 | if (!type_die) | |
3297 | return; | |
c4ce66a1 | 3298 | |
5f36109e JS |
3299 | string code; |
3300 | exp_type type = pe_long; | |
3301 | ||
ea1e477a | 3302 | try |
c4ce66a1 | 3303 | { |
ea1e477a JS |
3304 | Dwarf_Die cu_mem; |
3305 | dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL)); | |
5f36109e JS |
3306 | |
3307 | if (!e.components.empty() && | |
3308 | e.components.back().type == target_symbol::comp_pretty_print) | |
3309 | { | |
3310 | if (lvalue) | |
3311 | throw semantic_error("cannot write to pretty-printed variable", e.tok); | |
3312 | ||
3313 | dwarf_pretty_print dpp(dw, type_die, e.operand, userspace_p, e); | |
3314 | result = dpp.expand(); | |
3315 | return; | |
3316 | } | |
3317 | ||
3318 | code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type); | |
ea1e477a JS |
3319 | } |
3320 | catch (const semantic_error& er) | |
3321 | { | |
3322 | // NB: we can have multiple errors, since a @cast | |
1af1e62d JS |
3323 | // may be attempted using several different modules: |
3324 | // @cast(ptr, "type", "module1:module2:...") | |
3325 | e.chain (er); | |
c4ce66a1 | 3326 | } |
c4ce66a1 | 3327 | |
5f36109e JS |
3328 | if (code.empty()) |
3329 | return; | |
c4ce66a1 | 3330 | |
5f36109e JS |
3331 | string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get") |
3332 | + "_" + e.base_name.substr(1) | |
3333 | + "_" + lex_cast(tick++)); | |
c4ce66a1 | 3334 | |
5f36109e JS |
3335 | // Synthesize a function. |
3336 | functiondecl *fdecl = new functiondecl; | |
3337 | fdecl->synthetic = true; | |
3338 | fdecl->tok = e.tok; | |
3339 | fdecl->type = type; | |
3340 | fdecl->name = fname; | |
3341 | ||
3342 | embeddedcode *ec = new embeddedcode; | |
3343 | ec->tok = e.tok; | |
3344 | fdecl->body = ec; | |
3345 | ||
3346 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime | |
3347 | ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n"; | |
3348 | ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n"; | |
3349 | ||
3350 | ec->code += code; | |
3351 | ||
3352 | // Give the fdecl an argument for the pointer we're trying to cast | |
3353 | vardecl *v1 = new vardecl; | |
3354 | v1->type = pe_long; | |
3355 | v1->name = "pointer"; | |
3356 | v1->tok = e.tok; | |
3357 | fdecl->formal_args.push_back(v1); | |
3358 | ||
3359 | // Any non-literal indexes need to be passed in too. | |
3360 | for (unsigned i = 0; i < e.components.size(); ++i) | |
3361 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
3362 | { | |
3363 | vardecl *v = new vardecl; | |
3364 | v->type = pe_long; | |
3365 | v->name = "index" + lex_cast(i); | |
3366 | v->tok = e.tok; | |
3367 | fdecl->formal_args.push_back(v); | |
3368 | } | |
3369 | ||
3370 | if (lvalue) | |
3371 | { | |
3372 | // Modify the fdecl so it carries a second pe_long formal | |
3373 | // argument called "value". | |
3374 | ||
3375 | // FIXME: For the time being we only support setting target | |
3376 | // variables which have base types; these are 'pe_long' in | |
3377 | // stap's type vocabulary. Strings and pointers might be | |
3378 | // reasonable, some day, but not today. | |
3379 | ||
3380 | vardecl *v2 = new vardecl; | |
3381 | v2->type = pe_long; | |
3382 | v2->name = "value"; | |
3383 | v2->tok = e.tok; | |
3384 | fdecl->formal_args.push_back(v2); | |
3385 | } | |
3386 | else | |
3387 | ec->code += "/* pure */"; | |
3388 | ||
3389 | ec->code += "/* unprivileged */"; | |
3390 | ||
3391 | // PR10601 | |
3392 | ec->code += "\n#undef fetch_register\n"; | |
3393 | ec->code += "\n#undef store_register\n"; | |
3394 | ||
3395 | dw.sess.functions[fdecl->name] = fdecl; | |
3396 | ||
3397 | // Synthesize a functioncall. | |
3398 | functioncall* n = new functioncall; | |
3399 | n->tok = e.tok; | |
3400 | n->function = fname; | |
3401 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
3402 | n->args.push_back(e.operand); | |
3403 | ||
3404 | // Any non-literal indexes need to be passed in too. | |
3405 | for (unsigned i = 0; i < e.components.size(); ++i) | |
3406 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
3407 | n->args.push_back(e.components[i].expr_index); | |
3408 | ||
3409 | result = n; | |
3410 | } | |
c4ce66a1 JS |
3411 | |
3412 | ||
fb0274bc JS |
3413 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
3414 | { | |
d90053e7 | 3415 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 3416 | // for those cases, build a module including that header |
d90053e7 | 3417 | if (module[module.size() - 1] == '>' && |
60d98537 | 3418 | (module[0] == '<' || startswith(module, "kernel<"))) |
fb0274bc JS |
3419 | { |
3420 | string cached_module; | |
3421 | if (s.use_cache) | |
3422 | { | |
3423 | // see if the cached module exists | |
a2639cb7 | 3424 | cached_module = find_typequery_hash(s, module); |
d105f664 | 3425 | if (!cached_module.empty() && !s.poison_cache) |
fb0274bc JS |
3426 | { |
3427 | int fd = open(cached_module.c_str(), O_RDONLY); | |
3428 | if (fd != -1) | |
3429 | { | |
3430 | if (s.verbose > 2) | |
3431 | clog << "Pass 2: using cached " << cached_module << endl; | |
3432 | module = cached_module; | |
3433 | close(fd); | |
3434 | return; | |
3435 | } | |
3436 | } | |
3437 | } | |
3438 | ||
3439 | // no cached module, time to make it | |
d90053e7 | 3440 | if (make_typequery(s, module) == 0) |
fb0274bc | 3441 | { |
e16dc041 | 3442 | // try to save typequery in the cache |
fb0274bc | 3443 | if (s.use_cache) |
e16dc041 | 3444 | copy_file(module, cached_module, s.verbose > 2); |
fb0274bc JS |
3445 | } |
3446 | } | |
3447 | } | |
3448 | ||
3449 | ||
c4ce66a1 JS |
3450 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
3451 | { | |
3452 | bool lvalue = is_active_lvalue(e); | |
3453 | if (lvalue && !s.guru_mode) | |
3454 | throw semantic_error("write to typecast value not permitted", e->tok); | |
3455 | ||
3456 | if (e->module.empty()) | |
3457 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
3458 | ||
5f36109e | 3459 | functioncall* result = NULL; |
8b31197b JS |
3460 | |
3461 | // split the module string by ':' for alternatives | |
3462 | vector<string> modules; | |
3463 | tokenize(e->module, modules, ":"); | |
b5a0dd41 | 3464 | bool userspace_p=false; // PR10601 |
5f36109e | 3465 | for (unsigned i = 0; !result && i < modules.size(); ++i) |
c4ce66a1 | 3466 | { |
8b31197b | 3467 | string& module = modules[i]; |
fb0274bc | 3468 | filter_special_modules(module); |
abb41d92 | 3469 | |
c4ce66a1 JS |
3470 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
3471 | // which means that userspace modules won't get any PATH searching. | |
3472 | dwflpp* dw; | |
707bf35e JS |
3473 | try |
3474 | { | |
b5a0dd41 FCE |
3475 | userspace_p=is_user_module (module); |
3476 | if (! userspace_p) | |
707bf35e JS |
3477 | { |
3478 | // kernel or kernel module target | |
ae2552da | 3479 | dw = db.get_kern_dw(s, module); |
707bf35e JS |
3480 | } |
3481 | else | |
3482 | { | |
3483 | module = find_executable (module); // canonicalize it | |
3484 | dw = db.get_user_dw(s, module); | |
3485 | } | |
3486 | } | |
3487 | catch (const semantic_error& er) | |
3488 | { | |
3489 | /* ignore and go to the next module */ | |
3490 | continue; | |
3491 | } | |
c4ce66a1 | 3492 | |
5f36109e | 3493 | dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result); |
51178501 | 3494 | dw->iterate_over_modules(&query_module, &q); |
c4ce66a1 | 3495 | } |
abb41d92 | 3496 | |
5f36109e | 3497 | if (!result) |
c4ce66a1 | 3498 | { |
946e1a48 JS |
3499 | // We pass the unresolved cast_op to the next pass, and hope |
3500 | // that this value ends up not being referenced after all, so | |
3501 | // it can be optimized out quietly. | |
c4ce66a1 JS |
3502 | provide (e); |
3503 | return; | |
3504 | } | |
3505 | ||
c4ce66a1 JS |
3506 | if (lvalue) |
3507 | { | |
3508 | // Provide the functioncall to our parent, so that it can be | |
3509 | // used to substitute for the assignment node immediately above | |
3510 | // us. | |
3511 | assert(!target_symbol_setter_functioncalls.empty()); | |
5f36109e | 3512 | *(target_symbol_setter_functioncalls.top()) = result; |
c4ce66a1 JS |
3513 | } |
3514 | ||
5f36109e | 3515 | result->visit (this); |
77de5e9e GH |
3516 | } |
3517 | ||
3518 | ||
b8da0ad1 FCE |
3519 | void |
3520 | dwarf_derived_probe::printsig (ostream& o) const | |
3521 | { | |
3522 | // Instead of just printing the plain locations, we add a PC value | |
3523 | // as a comment as a way of telling e.g. apart multiple inlined | |
3524 | // function instances. This is distinct from the verbose/clog | |
3525 | // output, since this part goes into the cache hash calculations. | |
3526 | sole_location()->print (o); | |
6d0f3f0c | 3527 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
3528 | printsig_nested (o); |
3529 | } | |
3530 | ||
3531 | ||
3532 | ||
dc38c0ae | 3533 | void |
b20febf3 FCE |
3534 | dwarf_derived_probe::join_group (systemtap_session& s) |
3535 | { | |
af234c40 JS |
3536 | // skip probes which are paired entry-handlers |
3537 | if (!has_return && (saved_longs || saved_strings)) | |
3538 | return; | |
3539 | ||
b20febf3 FCE |
3540 | if (! s.dwarf_derived_probes) |
3541 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
3542 | s.dwarf_derived_probes->enroll (this); | |
b642c901 SC |
3543 | |
3544 | enable_task_finder(s); | |
b20febf3 FCE |
3545 | } |
3546 | ||
3547 | ||
3548 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, | |
3549 | const string& filename, | |
3550 | int line, | |
91af0778 | 3551 | // module & section specify a relocation |
b20febf3 FCE |
3552 | // base for <addr>, unless section=="" |
3553 | // (equivalently module=="kernel") | |
3554 | const string& module, | |
3555 | const string& section, | |
3556 | // NB: dwfl_addr is the virtualized | |
3557 | // address for this symbol. | |
3558 | Dwarf_Addr dwfl_addr, | |
3559 | // addr is the section-offset for | |
3560 | // actual relocation. | |
3561 | Dwarf_Addr addr, | |
3562 | dwarf_query& q, | |
37ebca01 | 3563 | Dwarf_Die* scope_die /* may be null */) |
1939ea32 | 3564 | : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ), |
b20febf3 | 3565 | module (module), section (section), addr (addr), |
63b4fd14 | 3566 | path (q.path), |
27dc09b1 | 3567 | has_process (q.has_process), |
c9bad430 DS |
3568 | has_return (q.has_return), |
3569 | has_maxactive (q.has_maxactive), | |
63b4fd14 | 3570 | has_library (q.has_library), |
6b66b9f7 | 3571 | maxactive_val (q.maxactive_val), |
b642c901 SC |
3572 | user_path (q.user_path), |
3573 | user_lib (q.user_lib), | |
af234c40 | 3574 | access_vars(false), |
63b4fd14 | 3575 | saved_longs(0), saved_strings(0), |
af234c40 | 3576 | entry_handler(0) |
bd2b1e68 | 3577 | { |
b642c901 SC |
3578 | if (user_lib.size() != 0) |
3579 | has_library = true; | |
3580 | ||
6b66b9f7 JS |
3581 | if (q.has_process) |
3582 | { | |
3583 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. | |
3584 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
3585 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
3586 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
3587 | // XXX Assert invariants here too? | |
3588 | } | |
3589 | else | |
3590 | { | |
3591 | // Assert kernel relocation invariants | |
3592 | if (section == "" && dwfl_addr != addr) // addr should be absolute | |
3593 | throw semantic_error ("missing relocation base against", tok); | |
3594 | if (section != "" && dwfl_addr == addr) // addr should be an offset | |
3595 | throw semantic_error ("inconsistent relocation address", tok); | |
3596 | } | |
2930abc7 | 3597 | |
21beacc9 FCE |
3598 | // XXX: hack for strange g++/gcc's |
3599 | #ifndef USHRT_MAX | |
3600 | #define USHRT_MAX 32767 | |
3601 | #endif | |
3602 | ||
606fd9c8 | 3603 | // Range limit maxactive() value |
6b66b9f7 | 3604 | if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX)) |
606fd9c8 | 3605 | throw semantic_error ("maxactive value out of range [0," |
aca66a36 | 3606 | + lex_cast(USHRT_MAX) + "]", |
f1a0157a | 3607 | q.base_loc->components.front()->tok); |
606fd9c8 | 3608 | |
de688825 | 3609 | // Expand target variables in the probe body |
5f0a03a6 | 3610 | if (!null_die(scope_die)) |
8fc05e57 | 3611 | { |
6b66b9f7 | 3612 | // XXX: user-space deref's for q.has_process! |
de688825 | 3613 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
8b095b45 | 3614 | v.replace (this->body); |
6b66b9f7 JS |
3615 | if (!q.has_process) |
3616 | access_vars = v.visited; | |
37ebca01 FCE |
3617 | |
3618 | // If during target-variable-expanding the probe, we added a new block | |
3619 | // of code, add it to the start of the probe. | |
3620 | if (v.add_block) | |
ba6f838d | 3621 | this->body = new block(v.add_block, this->body); |
2260f4e3 FCE |
3622 | |
3623 | // If when target-variable-expanding the probe, we need to synthesize a | |
3624 | // sibling function-entry probe. We don't go through the whole probe derivation | |
3625 | // business (PR10642) that could lead to wildcard/alias resolution, or for that | |
3626 | // dwarf-induced duplication. | |
3627 | if (v.add_call_probe) | |
37ebca01 | 3628 | { |
2260f4e3 FCE |
3629 | assert (q.has_return && !q.has_call); |
3630 | ||
3631 | // We temporarily replace q.base_probe. | |
3632 | statement* old_body = q.base_probe->body; | |
3633 | q.base_probe->body = v.add_call_probe; | |
3634 | q.has_return = false; | |
3635 | q.has_call = true; | |
af234c40 | 3636 | |
da23eceb | 3637 | if (q.has_process) |
af234c40 JS |
3638 | entry_handler = new uprobe_derived_probe (funcname, filename, line, |
3639 | module, section, dwfl_addr, | |
3640 | addr, q, scope_die); | |
da23eceb | 3641 | else |
af234c40 JS |
3642 | entry_handler = new dwarf_derived_probe (funcname, filename, line, |
3643 | module, section, dwfl_addr, | |
3644 | addr, q, scope_die); | |
3645 | ||
3646 | saved_longs = entry_handler->saved_longs = v.saved_longs; | |
3647 | saved_strings = entry_handler->saved_strings = v.saved_strings; | |
3648 | ||
3649 | q.results.push_back (entry_handler); | |
2260f4e3 FCE |
3650 | |
3651 | q.has_return = true; | |
3652 | q.has_call = false; | |
3653 | q.base_probe->body = old_body; | |
37ebca01 | 3654 | } |
f10534c6 WH |
3655 | // Save the local variables for listing mode |
3656 | if (q.sess.listing_mode_vars) | |
3657 | saveargs(q, scope_die, v); | |
8fc05e57 | 3658 | } |
37ebca01 | 3659 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 3660 | |
f10534c6 | 3661 | // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs |
0a98fd42 | 3662 | |
5d23847d | 3663 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
3664 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
3665 | // point. This allows a user to see what function / file / line | |
3666 | // number any particular match of the wildcards. | |
2930abc7 | 3667 | |
a229fcd7 | 3668 | vector<probe_point::component*> comps; |
91af0778 FCE |
3669 | if (q.has_kernel) |
3670 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
3671 | else if(q.has_module) | |
3672 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
3673 | else if(q.has_process) | |
3674 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
3675 | else | |
3676 | assert (0); | |
b5d77020 | 3677 | |
db520b00 FCE |
3678 | string fn_or_stmt; |
3679 | if (q.has_function_str || q.has_function_num) | |
3680 | fn_or_stmt = "function"; | |
3681 | else | |
3682 | fn_or_stmt = "statement"; | |
a229fcd7 | 3683 | |
b8da0ad1 | 3684 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 3685 | { |
4cd232e4 | 3686 | string retro_name = funcname; |
b20febf3 | 3687 | if (filename != "") |
cee35f73 | 3688 | { |
fb84c077 | 3689 | retro_name += ("@" + string (filename)); |
cee35f73 | 3690 | if (line > 0) |
aca66a36 | 3691 | retro_name += (":" + lex_cast (line)); |
cee35f73 | 3692 | } |
db520b00 FCE |
3693 | comps.push_back |
3694 | (new probe_point::component | |
3695 | (fn_or_stmt, new literal_string (retro_name))); | |
3696 | } | |
b8da0ad1 | 3697 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
3698 | { |
3699 | Dwarf_Addr retro_addr; | |
3700 | if (q.has_function_num) | |
3701 | retro_addr = q.function_num_val; | |
3702 | else | |
3703 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
3704 | comps.push_back (new probe_point::component |
3705 | (fn_or_stmt, | |
9ea68eb9 | 3706 | new literal_number(retro_addr, true))); |
37ebca01 FCE |
3707 | |
3708 | if (q.has_absolute) | |
3709 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
3710 | } |
3711 | ||
b8da0ad1 FCE |
3712 | if (q.has_call) |
3713 | comps.push_back (new probe_point::component(TOK_CALL)); | |
3714 | if (q.has_inline) | |
3715 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 3716 | if (has_return) |
b8da0ad1 FCE |
3717 | comps.push_back (new probe_point::component(TOK_RETURN)); |
3718 | if (has_maxactive) | |
3719 | comps.push_back (new probe_point::component | |
3720 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 3721 | |
5d23847d FCE |
3722 | // Overwrite it. |
3723 | this->sole_location()->components = comps; | |
2930abc7 FCE |
3724 | } |
3725 | ||
bd2b1e68 | 3726 | |
0a98fd42 | 3727 | void |
f10534c6 | 3728 | dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die, dwarf_var_expanding_visitor& v) |
0a98fd42 | 3729 | { |
9aa8ffce | 3730 | if (null_die(scope_die)) |
0a98fd42 | 3731 | return; |
0a98fd42 | 3732 | |
0a98fd42 | 3733 | string type_name; |
0a98fd42 JS |
3734 | Dwarf_Die type_die; |
3735 | ||
3736 | if (has_return && | |
3d1ad340 | 3737 | dwarf_attr_die (scope_die, DW_AT_type, &type_die) && |
f1c8f8a5 | 3738 | dwarf_type_name(&type_die, type_name)) |
d0bfd2ac | 3739 | args.push_back("$return:"+type_name); |
0a98fd42 | 3740 | |
36b155fa | 3741 | /* Pretend that we aren't in a .return for a moment, just so we can |
e29b2f5a | 3742 | * check whether variables are accessible. We don't want any of the |
36b155fa FCE |
3743 | * entry-saving code generated during listing mode. This works |
3744 | * because the set of $context variables available in a .return | |
3745 | * probe (apart from $return) is the same set as available for the | |
3746 | * corresponding .call probe, since we collect those variables at | |
3747 | * .call time. */ | |
d87623a1 JS |
3748 | bool saved_has_return = has_return; |
3749 | q.has_return = has_return = false; | |
3750 | ||
0a98fd42 | 3751 | Dwarf_Die arg; |
9aa8ffce | 3752 | vector<Dwarf_Die> scopes = q.dw.getscopes_die(scope_die); |
0a98fd42 JS |
3753 | if (dwarf_child (&scopes[0], &arg) == 0) |
3754 | do | |
3755 | { | |
3756 | switch (dwarf_tag (&arg)) | |
3757 | { | |
3758 | case DW_TAG_variable: | |
3759 | case DW_TAG_formal_parameter: | |
3760 | break; | |
3761 | ||
3762 | default: | |
3763 | continue; | |
3764 | } | |
3765 | ||
3766 | const char *arg_name = dwarf_diename (&arg); | |
3767 | if (!arg_name) | |
3768 | continue; | |
3769 | ||
3770 | type_name.clear(); | |
3d1ad340 | 3771 | if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) || |
f1c8f8a5 | 3772 | !dwarf_type_name(&type_die, type_name)) |
0a98fd42 JS |
3773 | continue; |
3774 | ||
f10534c6 WH |
3775 | /* trick from visit_target_symbol_context */ |
3776 | target_symbol *tsym = new target_symbol; | |
f1a0157a | 3777 | tsym->tok = q.base_loc->components.front()->tok; |
f10534c6 WH |
3778 | tsym->base_name = "$"; |
3779 | tsym->base_name += arg_name; | |
3780 | ||
3781 | /* Ignore any variable that isn't accessible */ | |
3782 | tsym->saved_conversion_error = 0; | |
3783 | v.require (tsym); | |
3784 | if (!tsym->saved_conversion_error) | |
d0bfd2ac | 3785 | args.push_back("$"+string(arg_name)+":"+type_name); |
0a98fd42 JS |
3786 | } |
3787 | while (dwarf_siblingof (&arg, &arg) == 0); | |
d87623a1 JS |
3788 | |
3789 | /* restore the .return status of the probe */ | |
3790 | q.has_return = has_return = saved_has_return; | |
0a98fd42 JS |
3791 | } |
3792 | ||
3793 | ||
3794 | void | |
d0bfd2ac | 3795 | dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const |
0a98fd42 | 3796 | { |
d0bfd2ac | 3797 | arg_set.insert(arg_set.end(), args.begin(), args.end()); |
0a98fd42 JS |
3798 | } |
3799 | ||
3800 | ||
27dc09b1 DB |
3801 | void |
3802 | dwarf_derived_probe::emit_unprivileged_assertion (translator_output* o) | |
3803 | { | |
3804 | if (has_process) | |
3805 | { | |
3806 | // These probes are allowed for unprivileged users, but only in the | |
3807 | // context of processes which they own. | |
3808 | emit_process_owner_assertion (o); | |
3809 | return; | |
3810 | } | |
3811 | ||
3812 | // Other probes must contain the default assertion which aborts | |
3813 | // if executed by an unprivileged user. | |
3814 | derived_probe::emit_unprivileged_assertion (o); | |
3815 | } | |
3816 | ||
3817 | ||
3818 | void | |
3819 | dwarf_derived_probe::print_dupe_stamp(ostream& o) | |
3820 | { | |
3821 | if (has_process) | |
3822 | { | |
3823 | // These probes are allowed for unprivileged users, but only in the | |
3824 | // context of processes which they own. | |
3825 | print_dupe_stamp_unprivileged_process_owner (o); | |
3826 | return; | |
3827 | } | |
3828 | ||
3829 | // Other probes must contain the default dupe stamp | |
3830 | derived_probe::print_dupe_stamp (o); | |
3831 | } | |
3832 | ||
64211010 | 3833 | |
7a053d3b | 3834 | void |
20c6c071 | 3835 | dwarf_derived_probe::register_statement_variants(match_node * root, |
27dc09b1 DB |
3836 | dwarf_builder * dw, |
3837 | bool bind_unprivileged_p) | |
bd2b1e68 | 3838 | { |
27dc09b1 DB |
3839 | root |
3840 | ->bind_unprivileged(bind_unprivileged_p) | |
3841 | ->bind(dw); | |
54efe513 GH |
3842 | } |
3843 | ||
7a053d3b | 3844 | void |
fd6602a0 | 3845 | dwarf_derived_probe::register_function_variants(match_node * root, |
27dc09b1 DB |
3846 | dwarf_builder * dw, |
3847 | bool bind_unprivileged_p) | |
2865d17a | 3848 | { |
27dc09b1 DB |
3849 | root |
3850 | ->bind_unprivileged(bind_unprivileged_p) | |
3851 | ->bind(dw); | |
3852 | root->bind(TOK_INLINE) | |
3853 | ->bind_unprivileged(bind_unprivileged_p) | |
3854 | ->bind(dw); | |
3855 | root->bind(TOK_CALL) | |
3856 | ->bind_unprivileged(bind_unprivileged_p) | |
3857 | ->bind(dw); | |
3858 | root->bind(TOK_RETURN) | |
3859 | ->bind_unprivileged(bind_unprivileged_p) | |
3860 | ->bind(dw); | |
3861 | root->bind(TOK_RETURN) | |
3862 | ->bind_unprivileged(bind_unprivileged_p) | |
3863 | ->bind_num(TOK_MAXACTIVE)->bind(dw); | |
bd2b1e68 GH |
3864 | } |
3865 | ||
7a053d3b | 3866 | void |
27dc09b1 DB |
3867 | dwarf_derived_probe::register_function_and_statement_variants( |
3868 | match_node * root, | |
3869 | dwarf_builder * dw, | |
3870 | bool bind_unprivileged_p | |
3871 | ) | |
bd2b1e68 GH |
3872 | { |
3873 | // Here we match 4 forms: | |
3874 | // | |
3875 | // .function("foo") | |
3876 | // .function(0xdeadbeef) | |
3877 | // .statement("foo") | |
3878 | // .statement(0xdeadbeef) | |
3879 | ||
27dc09b1 DB |
3880 | register_function_variants(root->bind_str(TOK_FUNCTION), dw, bind_unprivileged_p); |
3881 | register_function_variants(root->bind_num(TOK_FUNCTION), dw, bind_unprivileged_p); | |
3882 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw, bind_unprivileged_p); | |
3883 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw, bind_unprivileged_p); | |
bd2b1e68 GH |
3884 | } |
3885 | ||
3886 | void | |
c4ce66a1 | 3887 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 3888 | { |
c4ce66a1 | 3889 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
3890 | dwarf_builder *dw = new dwarf_builder(); |
3891 | ||
c4ce66a1 JS |
3892 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
3893 | s.code_filters.push_back(filter); | |
3894 | ||
27dc09b1 DB |
3895 | register_function_and_statement_variants(root->bind(TOK_KERNEL), dw); |
3896 | register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw); | |
3897 | ||
3898 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
3899 | ->bind(dw); | |
3900 | root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL) | |
3901 | ->bind(dw); | |
d2c9ec9b | 3902 | |
27dc09b1 DB |
3903 | register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw, |
3904 | true/*bind_unprivileged*/); | |
d2c9ec9b | 3905 | root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL) |
27dc09b1 | 3906 | ->bind_unprivileged() |
d2c9ec9b | 3907 | ->bind(dw); |
63b4fd14 | 3908 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_MARK) |
27dc09b1 | 3909 | ->bind_unprivileged() |
63b4fd14 | 3910 | ->bind(dw); |
a794dbeb FCE |
3911 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) |
3912 | ->bind_unprivileged() | |
3913 | ->bind(dw); | |
d2c9ec9b | 3914 | root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK) |
27dc09b1 | 3915 | ->bind_unprivileged() |
d2c9ec9b | 3916 | ->bind(dw); |
a794dbeb FCE |
3917 | root->bind_str(TOK_PROCESS)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) |
3918 | ->bind_unprivileged() | |
3919 | ->bind(dw); | |
3920 | // XXX what uses this? | |
d2c9ec9b | 3921 | root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK) |
27dc09b1 | 3922 | ->bind_unprivileged() |
d2c9ec9b | 3923 | ->bind(dw); |
bd2b1e68 GH |
3924 | } |
3925 | ||
9020300d FCE |
3926 | void |
3927 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
3928 | { | |
b95e2b79 MH |
3929 | if (access_vars) |
3930 | { | |
3931 | // if accessing $variables, emit bsp cache setup for speeding up | |
3932 | o->newline() << "bspcache(c->unwaddr, c->regs);"; | |
3933 | } | |
9020300d | 3934 | } |
2930abc7 | 3935 | |
b20febf3 | 3936 | // ------------------------------------------------------------------------ |
46b84a80 DS |
3937 | |
3938 | void | |
b20febf3 | 3939 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 3940 | { |
b20febf3 | 3941 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
3942 | |
3943 | // XXX: probes put at the same address should all share a | |
3944 | // single kprobe/kretprobe, and have their handlers executed | |
3945 | // sequentially. | |
b55bc428 FCE |
3946 | } |
3947 | ||
7a053d3b | 3948 | void |
775d51e5 | 3949 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 3950 | { |
b20febf3 | 3951 | if (probes_by_module.empty()) return; |
2930abc7 | 3952 | |
775d51e5 DS |
3953 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
3954 | ||
3955 | // Warn of misconfigured kernels | |
f41595cc FCE |
3956 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
3957 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
3958 | s.op->newline() << "#endif"; | |
775d51e5 | 3959 | s.op->newline(); |
f41595cc | 3960 | |
f07c3b68 | 3961 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 3962 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
3963 | s.op->newline() << "#endif"; |
3964 | ||
14cf7e42 SC |
3965 | // Forward decls |
3966 | s.op->newline() << "#include \"kprobes-common.h\""; | |
3967 | ||
b20febf3 FCE |
3968 | // Forward declare the master entry functions |
3969 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
3970 | s.op->line() << " struct pt_regs *regs);"; | |
3971 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
3972 | s.op->line() << " struct pt_regs *regs);"; | |
3973 | ||
42cb22bd MH |
3974 | // Emit an array of kprobe/kretprobe pointers |
3975 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
3976 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
3977 | s.op->newline() << "#endif"; | |
3978 | ||
b20febf3 | 3979 | // Emit the actual probe list. |
606fd9c8 FCE |
3980 | |
3981 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
3982 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
3983 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
14cf7e42 | 3984 | s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
606fd9c8 FCE |
3985 | // NB: bss! |
3986 | ||
4c2732a1 | 3987 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
3988 | s.op->newline(1) << "const unsigned return_p:1;"; |
3989 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 3990 | s.op->newline() << "const unsigned optional_p:1;"; |
b20febf3 | 3991 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 3992 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 | 3993 | |
af234c40 JS |
3994 | // data saved in the kretprobe_instance packet |
3995 | s.op->newline() << "const unsigned short saved_longs;"; | |
3996 | s.op->newline() << "const unsigned short saved_strings;"; | |
3997 | ||
606fd9c8 FCE |
3998 | // Let's find some stats for the three embedded strings. Maybe they |
3999 | // are small and uniform enough to justify putting char[MAX]'s into | |
4000 | // the array instead of relocated char*'s. | |
4001 | size_t module_name_max = 0, section_name_max = 0, pp_name_max = 0; | |
4002 | size_t module_name_tot = 0, section_name_tot = 0, pp_name_tot = 0; | |
4003 | size_t all_name_cnt = probes_by_module.size(); // for average | |
4004 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4005 | { | |
4006 | dwarf_derived_probe* p = it->second; | |
4007 | #define DOIT(var,expr) do { \ | |
4008 | size_t var##_size = (expr) + 1; \ | |
4009 | var##_max = max (var##_max, var##_size); \ | |
4010 | var##_tot += var##_size; } while (0) | |
4011 | DOIT(module_name, p->module.size()); | |
4012 | DOIT(section_name, p->section.size()); | |
4013 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
4014 | #undef DOIT | |
4015 | } | |
4016 | ||
4017 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
4018 | // the amount of average waste (max - avg) to the relocation data size | |
4019 | // (3 native long words). | |
4020 | #define CALCIT(var) \ | |
4021 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
4022 | { \ | |
4023 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
4024 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
4025 | << "[" << var##_name_max << "]" << endl; \ | |
4026 | } \ | |
4027 | else \ | |
4028 | { \ | |
b0986e7a | 4029 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
4030 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
4031 | } | |
4032 | ||
4033 | CALCIT(module); | |
4034 | CALCIT(section); | |
4035 | CALCIT(pp); | |
e6fe60e7 | 4036 | #undef CALCIT |
606fd9c8 | 4037 | |
b0986e7a DS |
4038 | s.op->newline() << "const unsigned long address;"; |
4039 | s.op->newline() << "void (* const ph) (struct context*);"; | |
af234c40 | 4040 | s.op->newline() << "void (* const entry_ph) (struct context*);"; |
b642c901 SC |
4041 | s.op->newline() << "const unsigned long sdt_sem_offset;"; |
4042 | s.op->newline() << "unsigned long sdt_sem_address;"; | |
4043 | s.op->newline() << "struct task_struct *tsk;"; | |
4044 | s.op->newline() << "const char *pathname;"; | |
4045 | s.op->newline() << "struct stap_task_finder_target finder;"; | |
b20febf3 FCE |
4046 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
4047 | s.op->indent(1); | |
4048 | ||
4049 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 4050 | { |
b20febf3 FCE |
4051 | dwarf_derived_probe* p = it->second; |
4052 | s.op->newline() << "{"; | |
4053 | if (p->has_return) | |
4054 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 4055 | if (p->has_maxactive) |
606fd9c8 FCE |
4056 | { |
4057 | s.op->line() << " .maxactive_p=1,"; | |
4058 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
4059 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
4060 | } | |
af234c40 JS |
4061 | if (p->saved_longs || p->saved_strings) |
4062 | { | |
4063 | if (p->saved_longs) | |
4064 | s.op->line() << " .saved_longs=" << p->saved_longs << ","; | |
4065 | if (p->saved_strings) | |
4066 | s.op->line() << " .saved_strings=" << p->saved_strings << ","; | |
4067 | if (p->entry_handler) | |
4068 | s.op->line() << " .entry_ph=&" << p->entry_handler->name << ","; | |
4069 | } | |
b350f56b JS |
4070 | if (p->locations[0]->optional) |
4071 | s.op->line() << " .optional_p=1,"; | |
dc38c256 | 4072 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
4073 | s.op->line() << " .module=\"" << p->module << "\","; |
4074 | s.op->line() << " .section=\"" << p->section << "\","; | |
b20febf3 | 4075 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
b642c901 SC |
4076 | if (p->sdt_semaphore_addr != 0) |
4077 | { | |
4078 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" << hex << p->sdt_semaphore_addr << dec << "ULL,"; | |
4079 | s.op->line() << " .sdt_sem_address=0,"; | |
4080 | if (p->has_library) | |
4081 | { | |
4082 | s.op->line() << " .finder={"; | |
4083 | s.op->line() << " .pid=0,"; | |
4084 | s.op->line() << " .procname=\"" << p->user_path << "\","; | |
4085 | s.op->line() << " .mmap_callback=&stap_kprobe_mmap_found,"; | |
4086 | s.op->line() << " },"; | |
4087 | s.op->line() << " .pathname=\"" << p->user_lib << "\","; | |
4088 | } | |
4089 | else | |
4090 | { | |
4091 | s.op->line() << " .finder={"; | |
4092 | s.op->line() << " .pid=0,"; | |
4093 | s.op->line() << " .procname=\"" << p->user_path << "\","; | |
4094 | s.op->line() << " .callback=&stap_kprobe_process_found,"; | |
4095 | s.op->line() << " },"; | |
4096 | s.op->line() << " .pathname=\"" << p->user_path << "\","; | |
4097 | } | |
4098 | ||
4099 | } | |
4100 | else | |
4101 | s.op->line() << " .sdt_sem_offset=0,"; | |
b20febf3 | 4102 | s.op->line() << " .ph=&" << p->name; |
b642c901 | 4103 | |
b20febf3 | 4104 | s.op->line() << " },"; |
2930abc7 | 4105 | } |
2930abc7 | 4106 | |
b20febf3 FCE |
4107 | s.op->newline(-1) << "};"; |
4108 | ||
4109 | // Emit the kprobes callback function | |
4110 | s.op->newline(); | |
4111 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4112 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
4113 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
4114 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
4115 | // Check that the index is plausible | |
4116 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4117 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4118 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4119 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4120 | s.op->line() << "];"; | |
c12d974f | 4121 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 | 4122 | s.op->newline() << "c->regs = regs;"; |
6415ddde MW |
4123 | |
4124 | // Make it look like the IP is set as it wouldn't have been replaced | |
4125 | // by a breakpoint instruction when calling real probe handler. Reset | |
4126 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
4127 | s.op->newline() << "{"; | |
4128 | s.op->indent(1); | |
4129 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);"; | |
259d54c0 | 4130 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
b20febf3 | 4131 | s.op->newline() << "(*sdp->ph) (c);"; |
259d54c0 | 4132 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4133 | s.op->newline(-1) << "}"; |
4134 | ||
b20febf3 FCE |
4135 | common_probe_entryfn_epilogue (s.op); |
4136 | s.op->newline() << "return 0;"; | |
4137 | s.op->newline(-1) << "}"; | |
4138 | ||
4139 | // Same for kretprobes | |
4140 | s.op->newline(); | |
af234c40 JS |
4141 | s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,"; |
4142 | s.op->line() << " struct pt_regs *regs, int entry) {"; | |
b20febf3 | 4143 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; |
606fd9c8 FCE |
4144 | |
4145 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 4146 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
4147 | // Check that the index is plausible |
4148 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4149 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4150 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4151 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4152 | s.op->line() << "];"; | |
4153 | ||
c12d974f | 4154 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 | 4155 | s.op->newline() << "c->regs = regs;"; |
af234c40 JS |
4156 | |
4157 | // for assisting runtime's backtrace logic and accessing kretprobe data packets | |
4158 | s.op->newline() << "c->pi = inst;"; | |
4159 | s.op->newline() << "c->pi_longs = sdp->saved_longs;"; | |
6415ddde MW |
4160 | |
4161 | // Make it look like the IP is set as it wouldn't have been replaced | |
4162 | // by a breakpoint instruction when calling real probe handler. Reset | |
4163 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
4164 | s.op->newline() << "{"; | |
4165 | s.op->indent(1); | |
4166 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);"; | |
5e562a69 TM |
4167 | s.op->newline() << "if (entry) {"; |
4168 | s.op->indent(1); | |
259d54c0 | 4169 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
5e562a69 TM |
4170 | s.op->newline() << "(sdp->entry_ph) (c);"; |
4171 | s.op->newline(-1) << "} else {"; | |
4172 | s.op->indent(1); | |
4173 | s.op->newline() << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);"; | |
4174 | s.op->newline() << "(sdp->ph) (c);"; | |
4175 | s.op->newline(-1) << "}"; | |
259d54c0 | 4176 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4177 | s.op->newline(-1) << "}"; |
4178 | ||
b20febf3 FCE |
4179 | common_probe_entryfn_epilogue (s.op); |
4180 | s.op->newline() << "return 0;"; | |
4181 | s.op->newline(-1) << "}"; | |
af234c40 JS |
4182 | |
4183 | s.op->newline(); | |
4184 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4185 | s.op->line() << " struct pt_regs *regs) {"; | |
4186 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);"; | |
4187 | s.op->newline(-1) << "}"; | |
4188 | ||
4189 | s.op->newline(); | |
4190 | s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,"; | |
4191 | s.op->line() << " struct pt_regs *regs) {"; | |
4192 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);"; | |
4193 | s.op->newline(-1) << "}"; | |
b642c901 | 4194 | |
14cf7e42 SC |
4195 | s.op->newline(); |
4196 | s.op->newline() << "#include \"kprobes-common.c\""; | |
4197 | s.op->newline(); | |
20c6c071 | 4198 | } |
ec4373ff | 4199 | |
20c6c071 | 4200 | |
dc38c0ae | 4201 | void |
b20febf3 FCE |
4202 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
4203 | { | |
b642c901 SC |
4204 | p_b_m_iterator it; |
4205 | for (it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4206 | { | |
4207 | dwarf_derived_probe* p = it->second; | |
4208 | if (p->sdt_semaphore_addr != 0) | |
4209 | break; | |
4210 | } | |
4211 | if (it != probes_by_module.end()) // Ignore if there are no semaphores | |
4212 | { | |
4213 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_dwarf_probes); i++) {"; | |
4214 | s.op->newline(1) << "int rc;"; | |
4215 | s.op->newline() << "struct stap_dwarf_probe *p = &stap_dwarf_probes[i];"; | |
4216 | s.op->newline() << "probe_point = p->pp;"; // for error messages | |
4217 | s.op->newline() << "if (p->sdt_sem_offset) {"; | |
4218 | s.op->newline(1) << "rc = stap_register_task_finder_target(&p->finder);"; | |
4219 | s.op->newline(-1) << "}"; | |
4220 | s.op->newline() << "if (rc) break;"; | |
4221 | s.op->newline(-1) << "}"; | |
4222 | } | |
4223 | ||
b20febf3 FCE |
4224 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
4225 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 4226 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
ea549ffc | 4227 | s.op->newline() << "unsigned long relocated_addr = _stp_module_relocate (sdp->module, sdp->section, sdp->address, NULL);"; |
b20febf3 | 4228 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
6d0f3f0c | 4229 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
b20febf3 | 4230 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 4231 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 4232 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 4233 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 4234 | s.op->newline(-1) << "} else {"; |
f07c3b68 | 4235 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
c9bad430 | 4236 | s.op->newline(-1) << "}"; |
606fd9c8 | 4237 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
af234c40 JS |
4238 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; |
4239 | s.op->newline() << "if (sdp->entry_ph) {"; | |
4240 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; | |
4241 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
4242 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
4243 | s.op->newline(-1) << "}"; | |
4244 | s.op->newline() << "#endif"; | |
e4cb375f MH |
4245 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
4246 | s.op->newline() << "#ifdef __ia64__"; | |
4247 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
4248 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4249 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4250 | s.op->newline() << "if (rc == 0) {"; | |
4251 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
4252 | s.op->newline() << "if (rc != 0)"; | |
4253 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4254 | s.op->newline(-2) << "}"; | |
4255 | s.op->newline() << "#else"; | |
606fd9c8 | 4256 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 4257 | s.op->newline() << "#endif"; |
b20febf3 | 4258 | s.op->newline(-1) << "} else {"; |
e4cb375f | 4259 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
4260 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
4261 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
4262 | s.op->newline() << "#ifdef __ia64__"; |
4263 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
4264 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4265 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4266 | s.op->newline() << "if (rc == 0) {"; | |
4267 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
4268 | s.op->newline() << "if (rc != 0)"; | |
4269 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4270 | s.op->newline(-2) << "}"; | |
4271 | s.op->newline() << "#else"; | |
606fd9c8 | 4272 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 4273 | s.op->newline() << "#endif"; |
b20febf3 | 4274 | s.op->newline(-1) << "}"; |
9063462a FCE |
4275 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
4276 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 4277 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 4278 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);"; |
b350f56b | 4279 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
9063462a FCE |
4280 | // XXX: shall we increment numskipped? |
4281 | s.op->newline(-1) << "}"; | |
4282 | ||
4283 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 4284 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 4285 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
4286 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
4287 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
4288 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
4289 | s.op->newline() << "#ifdef __ia64__"; |
4290 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
4291 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
4292 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
4293 | // not run for this early-abort case. | |
4294 | s.op->newline(-1) << "}"; | |
4295 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 4296 | s.op->newline(-1) << "}"; |
9063462a FCE |
4297 | #endif |
4298 | ||
b20febf3 FCE |
4299 | s.op->newline() << "else sdp->registered_p = 1;"; |
4300 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
4301 | } |
4302 | ||
4303 | ||
46b84a80 | 4304 | void |
b20febf3 | 4305 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 4306 | { |
b642c901 SC |
4307 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
4308 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4309 | s.op->newline() << "unsigned short sdt_semaphore = 0;"; // NB: fixed size | |
4310 | s.op->newline() << "if (sdp->sdt_sem_address && __access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 0)) {"; | |
4311 | s.op->newline(1) << "sdt_semaphore --;"; | |
4312 | s.op->newline() << "__access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 1);"; | |
4313 | s.op->newline(-1) << "}"; | |
4314 | s.op->newline(-1) << "}"; | |
4315 | ||
42cb22bd MH |
4316 | //Unregister kprobes by batch interfaces. |
4317 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4318 | s.op->newline() << "j = 0;"; | |
4319 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4320 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4321 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
4322 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4323 | s.op->newline() << "if (!sdp->return_p)"; | |
4324 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
4325 | s.op->newline(-2) << "}"; | |
4326 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
4327 | s.op->newline() << "j = 0;"; | |
4328 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4329 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4330 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
4331 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4332 | s.op->newline() << "if (sdp->return_p)"; | |
4333 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
4334 | s.op->newline(-2) << "}"; | |
4335 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
4336 | s.op->newline() << "#ifdef __ia64__"; |
4337 | s.op->newline() << "j = 0;"; | |
4338 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4339 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4340 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
4341 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4342 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
4343 | s.op->newline(-1) << "}"; | |
4344 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
4345 | s.op->newline() << "#endif"; | |
42cb22bd MH |
4346 | s.op->newline() << "#endif"; |
4347 | ||
b20febf3 FCE |
4348 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
4349 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 4350 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
4351 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
4352 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 4353 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 4354 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 4355 | s.op->newline() << "#endif"; |
606fd9c8 | 4356 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; |
73209876 FCE |
4357 | s.op->newline() << "#ifdef STP_TIMING"; |
4358 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
d01eaa30 | 4359 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; |
73209876 | 4360 | s.op->newline(-1) << "#endif"; |
606fd9c8 | 4361 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; |
73209876 FCE |
4362 | s.op->newline() << "#ifdef STP_TIMING"; |
4363 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
ceca1799 | 4364 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; |
73209876 | 4365 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 4366 | s.op->newline(-1) << "} else {"; |
42cb22bd | 4367 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 4368 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 4369 | s.op->newline() << "#endif"; |
606fd9c8 | 4370 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; |
73209876 FCE |
4371 | s.op->newline() << "#ifdef STP_TIMING"; |
4372 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
ceca1799 | 4373 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->pp, kp->u.kp.nmissed);"; |
73209876 | 4374 | s.op->newline(-1) << "#endif"; |
b20febf3 | 4375 | s.op->newline(-1) << "}"; |
e4cb375f MH |
4376 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
4377 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
4378 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
4379 | s.op->newline() << "sdp->registered_p = 0;"; |
4380 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
4381 | } |
4382 | ||
aff5d390 | 4383 | struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor |
7a05f484 | 4384 | { |
f83336a5 FCE |
4385 | sdt_uprobe_var_expanding_visitor(systemtap_session& s, |
4386 | int elf_machine, | |
4387 | const string & process_name, | |
a794dbeb | 4388 | const string & provider_name, |
aff5d390 SC |
4389 | const string & probe_name, |
4390 | const string & arg_string, | |
4391 | int arg_count): | |
f83336a5 | 4392 | session (s), process_name (process_name), |
a794dbeb | 4393 | provider_name (provider_name), probe_name (probe_name), arg_count (arg_count) |
a8ec7719 | 4394 | { |
f83336a5 FCE |
4395 | /* Register name mapping table depends on the elf machine of this particular |
4396 | probe target process/file, not upon the host. So we can't just | |
4397 | #ifdef _i686_ etc. */ | |
4398 | if (elf_machine == EM_X86_64) { | |
4399 | dwarf_regs["rax"] = dwarf_regs["eax"] = dwarf_regs["ax"] = dwarf_regs["al"] = 0; | |
4400 | dwarf_regs["rdx"] = dwarf_regs["edx"] = dwarf_regs["dx"] = dwarf_regs["dl"] = 1; | |
4401 | dwarf_regs["rcx"] = dwarf_regs["ecx"] = dwarf_regs["cx"] = dwarf_regs["cl"] = 2; | |
4402 | dwarf_regs["rbx"] = dwarf_regs["ebx"] = dwarf_regs["bx"] = dwarf_regs["bl"] = 3; | |
4403 | dwarf_regs["rsi"] = dwarf_regs["esi"] = dwarf_regs["si"] = dwarf_regs["sil"] = 4; | |
4404 | dwarf_regs["rdi"] = dwarf_regs["edi"] = dwarf_regs["di"] = dwarf_regs["dil"] = 5; | |
4405 | dwarf_regs["rbp"] = dwarf_regs["ebp"] = dwarf_regs["bp"] = 6; | |
4406 | dwarf_regs["rsp"] = dwarf_regs["esp"] = dwarf_regs["sp"] = 7; | |
4407 | dwarf_regs["r8"] = dwarf_regs["r8d"] = dwarf_regs["r8w"] = dwarf_regs["r8b"] = 8; | |
4408 | dwarf_regs["r9"] = dwarf_regs["r9d"] = dwarf_regs["r9w"] = dwarf_regs["r9b"] = 9; | |
4409 | dwarf_regs["r10"] = dwarf_regs["r10d"] = dwarf_regs["r10w"] = dwarf_regs["r10b"] = 10; | |
4410 | dwarf_regs["r11"] = dwarf_regs["r11d"] = dwarf_regs["r11w"] = dwarf_regs["r11b"] = 11; | |
4411 | dwarf_regs["r12"] = dwarf_regs["r12d"] = dwarf_regs["r12w"] = dwarf_regs["r12b"] = 12; | |
4412 | dwarf_regs["r13"] = dwarf_regs["r13d"] = dwarf_regs["r13w"] = dwarf_regs["r13b"] = 13; | |
4413 | dwarf_regs["r14"] = dwarf_regs["r14d"] = dwarf_regs["r14w"] = dwarf_regs["r14b"] = 14; | |
4414 | dwarf_regs["r15"] = dwarf_regs["r15d"] = dwarf_regs["r15w"] = dwarf_regs["r15b"] = 15; | |
4415 | } else if (elf_machine == EM_386) { | |
4416 | dwarf_regs["eax"] = dwarf_regs["ax"] = dwarf_regs["al"] = 0; | |
4417 | dwarf_regs["ecx"] = dwarf_regs["cx"] = dwarf_regs["cl"] = 1; | |
4418 | dwarf_regs["edx"] = dwarf_regs["dx"] = dwarf_regs["dl"] = 2; | |
4419 | dwarf_regs["ebx"] = dwarf_regs["bx"] = dwarf_regs["bl"] = 3; | |
4420 | dwarf_regs["esp"] = dwarf_regs["sp"] = 4; | |
4421 | dwarf_regs["ebp"] = dwarf_regs["bp"] = 5; | |
4422 | dwarf_regs["esi"] = dwarf_regs["si"] = dwarf_regs["sil"] = 6; | |
4423 | dwarf_regs["edi"] = dwarf_regs["di"] = dwarf_regs["dil"] = 7; | |
c8bc2044 | 4424 | } else if (arg_count) { |
f83336a5 FCE |
4425 | throw semantic_error (string("Unsupported architecture ") |
4426 | + "(" + process_name + " ELF code " + lex_cast(elf_machine) + ")" | |
4427 | + "for dwarfless sdt probes."); | |
4428 | } | |
4429 | ||
aff5d390 SC |
4430 | |
4431 | tokenize(arg_string, arg_tokens, " "); | |
4432 | assert(arg_count >= 0 && arg_count <= 10); | |
a8ec7719 | 4433 | } |
f83336a5 | 4434 | systemtap_session& session; |
aff5d390 | 4435 | const string & process_name; |
a794dbeb | 4436 | const string & provider_name; |
aff5d390 | 4437 | const string & probe_name; |
7a05f484 | 4438 | int arg_count; |
aff5d390 SC |
4439 | vector<string> arg_tokens; |
4440 | map<string,int> dwarf_regs; | |
4441 | ||
4442 | void visit_target_symbol (target_symbol* e); | |
4443 | void visit_defined_op (defined_op* e); | |
4444 | }; | |
4445 | ||
4446 | struct sdt_kprobe_var_expanding_visitor: public var_expanding_visitor | |
4447 | { | |
4448 | sdt_kprobe_var_expanding_visitor(const string & process_name, | |
a794dbeb | 4449 | const string & provider_name, |
aff5d390 SC |
4450 | const string & probe_name, |
4451 | const string & arg_string, | |
4452 | int arg_count): | |
a794dbeb | 4453 | process_name (process_name), provider_name (provider_name), probe_name (probe_name), |
aff5d390 SC |
4454 | arg_count (arg_count) |
4455 | { | |
4456 | tokenize(arg_string, arg_tokens, " "); | |
4457 | assert(arg_count >= 0 && arg_count <= 10); | |
4458 | } | |
4459 | const string & process_name; | |
a794dbeb | 4460 | const string & provider_name; |
aff5d390 SC |
4461 | const string & probe_name; |
4462 | int arg_count; | |
4463 | vector<string> arg_tokens; | |
7a05f484 SC |
4464 | |
4465 | void visit_target_symbol (target_symbol* e); | |
30263a73 | 4466 | void visit_defined_op (defined_op* e); |
7a05f484 SC |
4467 | }; |
4468 | ||
4469 | void | |
aff5d390 | 4470 | sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
7a05f484 | 4471 | { |
63ea4244 | 4472 | try |
7a05f484 | 4473 | { |
c69a87e0 | 4474 | if (e->base_name == "$$name") |
aff5d390 SC |
4475 | { |
4476 | if (e->addressof) | |
4477 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
7a05f484 | 4478 | |
aff5d390 SC |
4479 | literal_string *myname = new literal_string (probe_name); |
4480 | myname->tok = e->tok; | |
4481 | provide(myname); | |
4482 | return; | |
4483 | } | |
a794dbeb FCE |
4484 | if (e->base_name == "$$provider") |
4485 | { | |
4486 | if (e->addressof) | |
4487 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
4488 | ||
4489 | literal_string *myname = new literal_string (provider_name); | |
4490 | myname->tok = e->tok; | |
4491 | provide(myname); | |
4492 | return; | |
4493 | } | |
aff5d390 SC |
4494 | if (arg_count == 0) |
4495 | { | |
4496 | if (startswith(e->base_name, "$")) | |
4497 | { | |
4498 | // NB: Either | |
4499 | // 1) uprobe1_type $argN or $FOO (we don't know the arg_count) | |
4500 | // 2) uprobe2_type $FOO (no probe args) | |
4501 | // both of which get resolved later. | |
4502 | provide(e); | |
4503 | return; | |
4504 | } | |
4505 | else // uprobe2_type with no args | |
4506 | return; | |
4507 | } | |
63ea4244 | 4508 | |
c69a87e0 | 4509 | int argno = 0; |
a794dbeb | 4510 | // XXX: check for $arg prefix! |
c69a87e0 | 4511 | try |
aff5d390 SC |
4512 | { |
4513 | argno = lex_cast<int>(e->base_name.substr(4)); | |
4514 | } | |
c69a87e0 | 4515 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF |
aff5d390 SC |
4516 | { |
4517 | throw semantic_error("invalid argument number", e->tok); | |
4518 | } | |
c69a87e0 | 4519 | if (argno < 1 || argno > arg_count) |
aff5d390 SC |
4520 | throw semantic_error("invalid argument number", e->tok); |
4521 | bool arg_in_memory = false; | |
4522 | functioncall *fc = new functioncall; | |
4523 | binary_expression *be = new binary_expression; | |
4524 | string reg; | |
4525 | string disp_str; | |
4526 | int disp = 0; | |
4527 | // NB: uprobes-based sdt.h; $argFOO gets resolved later. | |
4528 | ||
4529 | string tok = arg_tokens[argno-1]; | |
4530 | ||
4531 | regex_t preg; | |
4532 | // this pattern matches 0xD(%R) | (%R) | %R | |
4533 | const char *pattern = "\\([0x]*-*[0-9]*\\)\\([(]*\\)\\(%[0-9a-z][0-9a-z][0-9a-z]*\\)\\([)]*\\)"; | |
4534 | int rc; | |
4535 | size_t nmatch = 5; | |
4536 | regmatch_t pmatch[5]; | |
4537 | ||
4538 | if (0 != (rc = regcomp(&preg, pattern, 0))) | |
4539 | throw semantic_error("Failed to parse probe operand"); | |
4540 | if (0 != (rc = regexec(&preg, arg_tokens[argno-1].c_str(), nmatch, pmatch, 0))) | |
4541 | throw semantic_error("Unsupported assembler operand while accessing " + probe_name + " " + e->base_name + " " + arg_tokens[argno-1], e->tok); | |
4542 | // Is there a displacement? | |
4543 | if (pmatch[2].rm_so > pmatch[1].rm_so) | |
4544 | { | |
4545 | string disp_str = (char*)&arg_tokens[argno-1][pmatch[1].rm_so]; | |
4546 | disp = lex_cast<int>(disp_str.substr(0,pmatch[1].rm_eo - pmatch[1].rm_so)); | |
4547 | } | |
4548 | // Is there an indirect register? | |
4549 | if ((arg_tokens[argno-1][pmatch[2].rm_so]) == '(') | |
4550 | arg_in_memory = true; | |
4551 | // Is there a register? | |
4552 | if (pmatch[3].rm_eo >= pmatch[3].rm_so) | |
4553 | { | |
4554 | reg = (char*)&arg_tokens[argno-1][pmatch[3].rm_so+1]; | |
4555 | reg.erase(pmatch[3].rm_eo - pmatch[3].rm_so - 1); | |
4556 | } | |
4557 | ||
4558 | if (reg.length() == 0) | |
4559 | throw semantic_error("Unsupported assembler operand while accessing " + probe_name + " " + e->base_name, e->tok); | |
4560 | ||
4561 | // synthesize user_long(%{fetch_register(R)%} + D) | |
4562 | fc->function = "user_long"; | |
4563 | fc->tok = e->tok; | |
4564 | be->tok = e->tok; | |
4565 | ||
4566 | embedded_expr *get_arg1 = new embedded_expr; | |
4567 | get_arg1->tok = e->tok; | |
4568 | get_arg1->code = string("/* unpriviledged */ /* pure */") | |
4569 | + (is_user_module (process_name) | |
4570 | ? string("u_fetch_register(") | |
4571 | : string("k_fetch_register(")) | |
4572 | + lex_cast(dwarf_regs[reg]) + string(")"); | |
f83336a5 | 4573 | // XXX: may we ever need to cast that to a narrower type? |
aff5d390 SC |
4574 | |
4575 | be->left = get_arg1; | |
4576 | be->op = "+"; | |
4577 | literal_number* inc = new literal_number(disp); | |
4578 | inc->tok = e->tok; | |
4579 | be->right = inc; | |
4580 | fc->args.push_back(be); | |
4581 | ||
4582 | if (e->components.empty()) // We have a scalar | |
4583 | { | |
4584 | if (e->addressof) | |
4585 | throw semantic_error("cannot take address of sdt variable", e->tok); | |
63ea4244 | 4586 | |
aff5d390 SC |
4587 | if (arg_in_memory) |
4588 | provide(fc); | |
4589 | else | |
4590 | provide(be); | |
4591 | return; | |
4592 | } | |
4593 | cast_op *cast = new cast_op; | |
4594 | cast->base_name = "@cast"; | |
4595 | cast->tok = e->tok; | |
4596 | if (arg_in_memory) | |
4597 | cast->operand = fc; | |
4598 | else | |
4599 | cast->operand = be; | |
4600 | cast->components = e->components; | |
4601 | cast->type = probe_name + "_arg" + lex_cast(argno); | |
4602 | cast->module = process_name; | |
4603 | ||
4604 | cast->visit(this); | |
4605 | } | |
4606 | catch (const semantic_error &er) | |
4607 | { | |
4608 | e->chain (er); | |
4609 | provide (e); | |
4610 | } | |
4611 | } | |
4612 | ||
4613 | ||
4614 | void | |
4615 | sdt_kprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
4616 | { | |
4617 | try | |
4618 | { | |
4619 | if (e->base_name == "$$name") | |
4620 | { | |
4621 | if (e->addressof) | |
4622 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
4623 | ||
4624 | literal_string *myname = new literal_string (probe_name); | |
4625 | myname->tok = e->tok; | |
4626 | provide(myname); | |
4627 | return; | |
4628 | } | |
a794dbeb FCE |
4629 | if (e->base_name == "$$provider") |
4630 | { | |
4631 | if (e->addressof) | |
4632 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
4633 | ||
4634 | literal_string *myname = new literal_string (provider_name); | |
4635 | myname->tok = e->tok; | |
4636 | provide(myname); | |
4637 | return; | |
4638 | } | |
aff5d390 SC |
4639 | |
4640 | int argno = 0; | |
4641 | try | |
4642 | { | |
4643 | argno = lex_cast<int>(e->base_name.substr(4)); | |
4644 | } | |
4645 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF | |
4646 | { | |
4647 | throw semantic_error("invalid argument number", e->tok); | |
4648 | } | |
4649 | if (argno < 1 || argno > arg_count) | |
4650 | throw semantic_error("invalid argument number", e->tok); | |
c69a87e0 FCE |
4651 | bool lvalue = is_active_lvalue(e); |
4652 | functioncall *fc = new functioncall; | |
63ea4244 | 4653 | |
c69a87e0 FCE |
4654 | // First two args are hidden: 1. pointer to probe name 2. task id |
4655 | if (arg_count < 2) | |
aff5d390 SC |
4656 | { |
4657 | fc->function = "ulong_arg"; | |
4658 | fc->type = pe_long; | |
4659 | fc->tok = e->tok; | |
4660 | // skip the hidden args | |
4661 | literal_number* num = new literal_number(argno + 2); | |
4662 | num->tok = e->tok; | |
4663 | fc->args.push_back(num); | |
4664 | } | |
4665 | else | |
4666 | { | |
4667 | // args are passed in arg3 as members of a struct | |
4668 | fc->function = "user_long"; | |
4669 | fc->tok = e->tok; | |
4670 | binary_expression *be = new binary_expression; | |
4671 | be->tok = e->tok; | |
4672 | functioncall *get_arg1 = new functioncall; | |
4673 | get_arg1->function = "pointer_arg"; | |
4674 | get_arg1->tok = e->tok; | |
4675 | // arg3 is the pointer to a struct of arguments | |
4676 | literal_number* num = new literal_number(3); | |
4677 | num->tok = e->tok; | |
4678 | get_arg1->args.push_back(num); | |
4679 | ||
4680 | be->left = get_arg1; | |
4681 | be->op = "+"; | |
4682 | // offset in struct to the desired arg | |
4683 | literal_number* inc = new literal_number((argno - 1) * 8); | |
4684 | inc->tok = e->tok; | |
4685 | be->right = inc; | |
4686 | fc->args.push_back(be); | |
4687 | } | |
c69a87e0 | 4688 | if (lvalue) |
aff5d390 | 4689 | *(target_symbol_setter_functioncalls.top()) = fc; |
63ea4244 | 4690 | |
aff5d390 SC |
4691 | if (e->components.empty()) // We have a scalar |
4692 | { | |
4693 | if (e->addressof) | |
4694 | throw semantic_error("cannot take address of sdt variable", e->tok); | |
63ea4244 | 4695 | |
aff5d390 SC |
4696 | provide(fc); |
4697 | return; | |
4698 | } | |
c69a87e0 FCE |
4699 | cast_op *cast = new cast_op; |
4700 | cast->base_name = "@cast"; | |
4701 | cast->tok = e->tok; | |
4702 | cast->operand = fc; | |
4703 | cast->components = e->components; | |
4704 | cast->type = probe_name + "_arg" + lex_cast(argno); | |
4705 | cast->module = process_name; | |
63ea4244 | 4706 | |
c69a87e0 | 4707 | cast->visit(this); |
7a05f484 | 4708 | } |
c69a87e0 | 4709 | catch (const semantic_error &er) |
ad002306 | 4710 | { |
1af1e62d | 4711 | e->chain (er); |
c69a87e0 | 4712 | provide (e); |
ad002306 | 4713 | } |
7a05f484 | 4714 | } |
46b84a80 | 4715 | |
aff5d390 | 4716 | |
30263a73 | 4717 | // See var_expanding_visitor::visit_defined_op for a background on |
aff5d390 | 4718 | // this callback, |
30263a73 | 4719 | void |
aff5d390 | 4720 | sdt_uprobe_var_expanding_visitor::visit_defined_op (defined_op *e) |
30263a73 | 4721 | { |
aff5d390 SC |
4722 | provide (e); |
4723 | } | |
4724 | ||
4725 | void | |
4726 | sdt_kprobe_var_expanding_visitor::visit_defined_op (defined_op *e) | |
4727 | { | |
4728 | var_expanding_visitor::visit_defined_op (e); | |
30263a73 FCE |
4729 | } |
4730 | ||
4731 | ||
edce5b67 JS |
4732 | struct sdt_query : public base_query |
4733 | { | |
4734 | sdt_query(probe * base_probe, probe_point * base_loc, | |
4735 | dwflpp & dw, literal_map_t const & params, | |
4736 | vector<derived_probe *> & results); | |
4737 | ||
4738 | void handle_query_module(); | |
4739 | ||
4740 | private: | |
15284963 | 4741 | stap_sdt_probe_type probe_type; |
edce5b67 JS |
4742 | probe * base_probe; |
4743 | probe_point * base_loc; | |
6846cfc8 | 4744 | literal_map_t const & params; |
edce5b67 | 4745 | vector<derived_probe *> & results; |
a794dbeb FCE |
4746 | string pp_mark; |
4747 | string pp_provider; | |
edce5b67 JS |
4748 | |
4749 | set<string> probes_handled; | |
4750 | ||
4751 | Elf_Data *pdata; | |
4752 | size_t probe_scn_offset; | |
4753 | size_t probe_scn_addr; | |
aff5d390 SC |
4754 | uint64_t arg_count; |
4755 | uint64_t pc; | |
4756 | string arg_string; | |
edce5b67 | 4757 | string probe_name; |
a794dbeb | 4758 | string provider_name; |
edce5b67 JS |
4759 | |
4760 | bool init_probe_scn(); | |
4761 | bool get_next_probe(); | |
4762 | ||
4763 | void convert_probe(probe *base); | |
4ddb6dd0 | 4764 | void record_semaphore(vector<derived_probe *> & results, unsigned start); |
c72aa911 | 4765 | probe* convert_location(); |
aff5d390 SC |
4766 | bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type;} |
4767 | bool have_kprobe() {return probe_type == kprobe1_type || probe_type == kprobe2_type;} | |
edce5b67 JS |
4768 | }; |
4769 | ||
4770 | ||
4771 | sdt_query::sdt_query(probe * base_probe, probe_point * base_loc, | |
4772 | dwflpp & dw, literal_map_t const & params, | |
4773 | vector<derived_probe *> & results): | |
4774 | base_query(dw, params), base_probe(base_probe), | |
6846cfc8 | 4775 | base_loc(base_loc), params(params), results(results) |
edce5b67 | 4776 | { |
a794dbeb FCE |
4777 | assert(get_string_param(params, TOK_MARK, pp_mark)); |
4778 | get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified | |
4779 | ||
ef428667 FCE |
4780 | // PR10245: permit usage of dtrace-y "-" separator in marker name; |
4781 | // map it to double-underscores. | |
4782 | size_t pos = 0; | |
4783 | while (1) // there may be more than one | |
4784 | { | |
a794dbeb | 4785 | size_t i = pp_mark.find("-", pos); |
ef428667 | 4786 | if (i == string::npos) break; |
a794dbeb | 4787 | pp_mark.replace (i, 1, "__"); |
ef428667 FCE |
4788 | pos = i+1; // resume searching after the inserted __ |
4789 | } | |
a794dbeb FCE |
4790 | |
4791 | // XXX: same for pp_provider? | |
edce5b67 JS |
4792 | } |
4793 | ||
4794 | ||
4795 | void | |
4796 | sdt_query::handle_query_module() | |
4797 | { | |
4798 | if (!init_probe_scn()) | |
4799 | return; | |
4800 | ||
4801 | if (sess.verbose > 3) | |
a794dbeb | 4802 | clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl; |
edce5b67 JS |
4803 | |
4804 | while (get_next_probe()) | |
4805 | { | |
aff5d390 | 4806 | if (! have_uprobe() |
696ec154 | 4807 | && !probes_handled.insert(probe_name).second) |
edce5b67 JS |
4808 | continue; |
4809 | ||
39a3e397 SC |
4810 | if (sess.verbose > 3) |
4811 | { | |
4812 | clog << "matched probe_name " << probe_name << " probe_type "; | |
4813 | switch (probe_type) | |
4814 | { | |
aff5d390 SC |
4815 | case uprobe1_type: |
4816 | clog << "uprobe1 at 0x" << hex << pc << dec << endl; | |
4817 | break; | |
4818 | case uprobe2_type: | |
4819 | clog << "uprobe2 at 0x" << hex << pc << dec << endl; | |
4820 | break; | |
4821 | case kprobe1_type: | |
4822 | clog << "kprobe1" << endl; | |
39a3e397 | 4823 | break; |
aff5d390 SC |
4824 | case kprobe2_type: |
4825 | clog << "kprobe2" << endl; | |
39a3e397 | 4826 | break; |
39a3e397 SC |
4827 | } |
4828 | } | |
30263a73 | 4829 | |
c72aa911 JS |
4830 | // Extend the derivation chain |
4831 | probe *new_base = convert_location(); | |
4832 | probe_point *new_location = new_base->locations[0]; | |
edce5b67 | 4833 | |
aff5d390 SC |
4834 | bool kprobe_found = false; |
4835 | if (have_kprobe()) | |
edce5b67 JS |
4836 | { |
4837 | convert_probe(new_base); | |
aff5d390 SC |
4838 | kprobe_found = true; |
4839 | // Expand the local variables in the probe body | |
4840 | sdt_kprobe_var_expanding_visitor svv (module_val, | |
a794dbeb | 4841 | provider_name, |
aff5d390 SC |
4842 | probe_name, |
4843 | arg_string, | |
4844 | arg_count); | |
4845 | svv.replace (new_base->body); | |
edce5b67 | 4846 | } |
aff5d390 SC |
4847 | else |
4848 | { | |
f83336a5 FCE |
4849 | /* Figure out the architecture of this particular ELF file. |
4850 | The dwarfless register-name mappings depend on it. */ | |
4851 | Dwarf_Addr bias; | |
4852 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.mod_info->mod, &bias)) | |
4853 | ?: dwfl_module_getelf (dw.mod_info->mod, &bias)); | |
4854 | GElf_Ehdr ehdr_mem; | |
4855 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
4856 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } | |
4857 | int elf_machine = em->e_machine; | |
4858 | sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, | |
4859 | module_val, | |
a794dbeb | 4860 | provider_name, |
aff5d390 SC |
4861 | probe_name, |
4862 | arg_string, | |
4863 | arg_count); | |
4864 | svv.replace (new_base->body); | |
4865 | } | |
4866 | ||
edce5b67 JS |
4867 | unsigned i = results.size(); |
4868 | ||
aff5d390 | 4869 | if (have_kprobe()) |
4ddb6dd0 JS |
4870 | derive_probes(sess, new_base, results); |
4871 | ||
edce5b67 JS |
4872 | else |
4873 | { | |
a794dbeb | 4874 | // XXX: why not derive_probes() in the uprobes case case too? |
edce5b67 JS |
4875 | literal_map_t params; |
4876 | for (unsigned i = 0; i < new_location->components.size(); ++i) | |
4877 | { | |
4878 | probe_point::component *c = new_location->components[i]; | |
4879 | params[c->functor] = c->arg; | |
4880 | } | |
4881 | ||
aff5d390 SC |
4882 | dwarf_query q(new_base, new_location, dw, params, results, "", ""); |
4883 | q.has_mark = true; // enables mid-statement probing | |
edce5b67 | 4884 | |
aff5d390 SC |
4885 | if (probe_type == uprobe1_type) |
4886 | dw.iterate_over_modules(&query_module, &q); | |
4887 | else if (probe_type == uprobe2_type) | |
4888 | { | |
4889 | Dwarf_Addr bias; | |
4890 | string section; | |
4891 | Elf* elf = dwfl_module_getelf (q.dw.mod_info->mod, &bias); | |
4892 | assert(elf); | |
4893 | Dwarf_Addr reloc_addr = q.statement_num_val + bias; | |
4894 | if (dwfl_module_relocations (q.dw.mod_info->mod) > 0) | |
4895 | { | |
4896 | dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr); | |
4897 | section = ".dynamic"; | |
4898 | } | |
4899 | else | |
4900 | section = ".absolute"; | |
4901 | ||
4902 | uprobe_derived_probe* p = | |
4903 | new uprobe_derived_probe ("", "", 0, q.module_val, section, | |
4904 | q.statement_num_val, reloc_addr, q, 0); | |
4905 | results.push_back (p); | |
4906 | } | |
4907 | } | |
4ddb6dd0 | 4908 | record_semaphore(results, i); |
edce5b67 JS |
4909 | } |
4910 | } | |
4911 | ||
4912 | ||
4913 | bool | |
4914 | sdt_query::init_probe_scn() | |
4915 | { | |
4916 | Elf* elf; | |
4917 | GElf_Shdr shdr_mem; | |
4918 | GElf_Shdr *shdr = NULL; | |
4919 | Dwarf_Addr bias; | |
4920 | size_t shstrndx; | |
4921 | ||
4922 | // Explicitly look in the main elf file first. | |
4923 | elf = dwfl_module_getelf (dw.module, &bias); | |
4924 | Elf_Scn *probe_scn = NULL; | |
4925 | ||
4926 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); | |
4927 | ||
4928 | bool have_probes = false; | |
4929 | ||
4930 | // Is there a .probes section? | |
4931 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
4932 | { | |
4933 | shdr = gelf_getshdr (probe_scn, &shdr_mem); | |
4934 | assert (shdr != NULL); | |
4935 | ||
4936 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0) | |
4937 | { | |
4938 | have_probes = true; | |
4939 | break; | |
4940 | } | |
4941 | } | |
4942 | ||
4943 | // Older versions put .probes section in the debuginfo dwarf file, | |
4944 | // so check if it actually exists, if not take a look in the debuginfo file | |
4945 | if (! have_probes || (have_probes && shdr->sh_type == SHT_NOBITS)) | |
4946 | { | |
4947 | elf = dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias)); | |
4948 | if (! elf) | |
4949 | return false; | |
4950 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); | |
4951 | probe_scn = NULL; | |
4952 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
4953 | { | |
4954 | shdr = gelf_getshdr (probe_scn, &shdr_mem); | |
4955 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), | |
4956 | ".probes") == 0) | |
aff5d390 SC |
4957 | { |
4958 | have_probes = true; | |
4959 | break; | |
4960 | } | |
edce5b67 JS |
4961 | } |
4962 | } | |
4963 | ||
4964 | if (!have_probes) | |
4965 | return false; | |
4966 | ||
4967 | pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); | |
4968 | probe_scn_offset = 0; | |
4969 | probe_scn_addr = shdr->sh_addr; | |
4970 | assert (pdata != NULL); | |
4971 | if (sess.verbose > 4) | |
4972 | clog << "got .probes elf scn_addr@0x" << probe_scn_addr << dec | |
4973 | << ", size: " << pdata->d_size << endl; | |
4974 | return true; | |
4975 | } | |
4976 | ||
4977 | bool | |
4978 | sdt_query::get_next_probe() | |
4979 | { | |
3a31e709 SC |
4980 | // Extract probe info from the .probes section, e.g. |
4981 | // 74657374 5f70726f 62655f32 00000000 test_probe_2.... | |
4982 | // 50524233 00000000 980c2000 00000000 PRB3...... ..... | |
4983 | // 01000000 00000000 00000000 00000000 ................ | |
4984 | // test_probe_2 is probe_name, probe_type is 50524233, | |
4985 | // *probe_name (pbe->name) is 980c2000, probe_arg (pbe->arg) is 1 | |
4986 | // probe_scn_offset is position currently being scanned in .probes | |
edce5b67 JS |
4987 | |
4988 | while (probe_scn_offset < pdata->d_size) | |
4989 | { | |
aff5d390 SC |
4990 | stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset); |
4991 | stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset); | |
15284963 | 4992 | probe_type = (stap_sdt_probe_type)(pbe_v1->type_a); |
aff5d390 | 4993 | if (! have_uprobe() && ! have_kprobe()) |
edce5b67 JS |
4994 | { |
4995 | // Unless this is a mangled .probes section, this happens | |
4996 | // because the name of the probe comes first, followed by | |
4997 | // the sentinel. | |
4998 | if (sess.verbose > 5) | |
4999 | clog << "got unknown probe_type: 0x" << hex << probe_type | |
5000 | << dec << endl; | |
5001 | probe_scn_offset += sizeof(__uint32_t); | |
5002 | continue; | |
5003 | } | |
aff5d390 SC |
5004 | if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b |
5005 | { | |
5006 | pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t)); | |
5007 | if (pbe_v1->type_b != uprobe1_type && pbe_v1->type_b != kprobe1_type) | |
5008 | continue; | |
5009 | } | |
5010 | ||
5011 | if (probe_type == uprobe1_type || probe_type == kprobe1_type) | |
5012 | { | |
5013 | probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr); | |
a794dbeb | 5014 | provider_name = ""; // unknown |
aff5d390 SC |
5015 | if (probe_type == uprobe1_type) |
5016 | { | |
5017 | pc = pbe_v1->arg; | |
5018 | arg_count = 0; | |
5019 | } | |
5020 | else if (probe_type == kprobe1_type) | |
5021 | arg_count = pbe_v1->arg; | |
5022 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v1); | |
5023 | } | |
5024 | else if (probe_type == uprobe2_type || probe_type == kprobe2_type) | |
5025 | { | |
5026 | probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr); | |
a794dbeb | 5027 | provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr); |
aff5d390 SC |
5028 | arg_count = pbe_v2->arg_count; |
5029 | pc = pbe_v2->pc; | |
5030 | if (pbe_v2->arg_string) | |
5031 | arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr); | |
5032 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v2); | |
5033 | } | |
edce5b67 | 5034 | if (sess.verbose > 4) |
a794dbeb | 5035 | clog << "saw .probes " << probe_name << (provider_name != "" ? " (provider "+provider_name+") " : "") |
aff5d390 | 5036 | << "@0x" << hex << pc << dec << endl; |
edce5b67 | 5037 | |
a794dbeb FCE |
5038 | if (dw.function_name_matches_pattern (probe_name, pp_mark) |
5039 | && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider))) | |
39a3e397 | 5040 | return true; |
edce5b67 JS |
5041 | else |
5042 | continue; | |
5043 | } | |
5044 | return false; | |
5045 | } | |
5046 | ||
5047 | ||
6846cfc8 | 5048 | void |
4ddb6dd0 | 5049 | sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start) |
6846cfc8 | 5050 | { |
a794dbeb FCE |
5051 | for (unsigned i=0; i<2; i++) { |
5052 | // prefer with-provider symbol; look without provider prefix for backward compatibility only | |
5053 | string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore"; | |
5054 | // XXX: multiple addresses? | |
5055 | if (sess.verbose > 2) | |
5056 | clog << "looking for semaphore symbol " << semaphore; | |
5057 | ||
5058 | Dwarf_Addr addr = lookup_symbol_address(dw.module, semaphore.c_str()); | |
5059 | if (addr) | |
5060 | { | |
5061 | if (dwfl_module_relocations (dw.module) > 0) | |
5062 | dwfl_module_relocate_address (dw.module, &addr); | |
5063 | // XXX: relocation basis? | |
5064 | for (unsigned i = start; i < results.size(); ++i) | |
5065 | results[i]->sdt_semaphore_addr = addr; | |
5066 | if (sess.verbose > 2) | |
5067 | clog << ", found at 0x" << hex << addr << dec << endl; | |
5068 | return; | |
5069 | } | |
5070 | else | |
5071 | if (sess.verbose > 2) | |
5072 | clog << ", not found" << endl; | |
5073 | } | |
6846cfc8 SC |
5074 | } |
5075 | ||
5076 | ||
edce5b67 JS |
5077 | void |
5078 | sdt_query::convert_probe (probe *base) | |
5079 | { | |
5080 | block *b = new block; | |
5081 | b->tok = base->body->tok; | |
5082 | ||
5083 | // XXX: Does this also need to happen for i386 under x86_64 stap? | |
a794dbeb | 5084 | #ifdef __i386__ /* XXX: probably s.architecture[] instead */ |
aff5d390 | 5085 | if (have_kprobe()) |
edce5b67 JS |
5086 | { |
5087 | functioncall *rp = new functioncall; | |
edce5b67 JS |
5088 | rp->function = "regparm"; |
5089 | rp->tok = b->tok; | |
5090 | literal_number* littid = new literal_number(0); | |
5091 | littid->tok = b->tok; | |
5092 | rp->args.push_back(littid); | |
5093 | expr_statement* es = new expr_statement; | |
5094 | es->tok = b->tok; | |
5095 | es->value = rp; | |
5096 | b->statements.push_back(es); | |
5097 | } | |
5098 | #endif | |
5099 | ||
aff5d390 | 5100 | if (have_kprobe()) |
edce5b67 | 5101 | { |
aff5d390 | 5102 | // Generate: if (arg2 != kprobe2_type) next; |
edce5b67 JS |
5103 | if_statement *istid = new if_statement; |
5104 | istid->thenblock = new next_statement; | |
5105 | istid->elseblock = NULL; | |
5106 | istid->tok = b->tok; | |
63ea4244 | 5107 | istid->thenblock->tok = b->tok; |
edce5b67 JS |
5108 | comparison *betid = new comparison; |
5109 | betid->op = "!="; | |
5110 | betid->tok = b->tok; | |
5111 | ||
5112 | functioncall *arg2 = new functioncall; | |
5113 | arg2->function = "ulong_arg"; | |
5114 | arg2->tok = b->tok; | |
5115 | literal_number* num = new literal_number(2); | |
5116 | num->tok = b->tok; | |
5117 | arg2->args.push_back(num); | |
5118 | ||
5119 | betid->left = arg2; | |
aff5d390 | 5120 | literal_number* littid = new literal_number(probe_type); |
edce5b67 JS |
5121 | littid->tok = b->tok; |
5122 | betid->right = littid; | |
5123 | istid->condition = betid; | |
5124 | b->statements.push_back(istid); | |
5125 | } | |
5126 | ||
5127 | // Generate: if (arg1 != mark("label")) next; | |
5128 | functioncall *fc = new functioncall; | |
bbafcb1e | 5129 | fc->function = "ulong_arg"; |
edce5b67 | 5130 | fc->tok = b->tok; |
bbafcb1e | 5131 | literal_number* num = new literal_number(1); |
edce5b67 JS |
5132 | num->tok = b->tok; |
5133 | fc->args.push_back(num); | |
5134 | ||
5135 | functioncall *fcus = new functioncall; | |
5136 | fcus->function = "user_string"; | |
5137 | fcus->type = pe_string; | |
5138 | fcus->tok = b->tok; | |
5139 | fcus->args.push_back(fc); | |
5140 | ||
5141 | if_statement *is = new if_statement; | |
5142 | is->thenblock = new next_statement; | |
5143 | is->elseblock = NULL; | |
5144 | is->tok = b->tok; | |
63ea4244 | 5145 | is->thenblock->tok = b->tok; |
edce5b67 JS |
5146 | comparison *be = new comparison; |
5147 | be->op = "!="; | |
5148 | be->tok = b->tok; | |
5149 | be->left = fcus; | |
5150 | be->right = new literal_string(probe_name); | |
63ea4244 | 5151 | be->right->tok = b->tok; |
edce5b67 JS |
5152 | is->condition = be; |
5153 | b->statements.push_back(is); | |
5154 | ||
5155 | // Now replace the body | |
5156 | b->statements.push_back(base->body); | |
5157 | base->body = b; | |
5158 | } | |
5159 | ||
5160 | ||
c72aa911 JS |
5161 | probe* |
5162 | sdt_query::convert_location () | |
edce5b67 | 5163 | { |
c72aa911 JS |
5164 | probe_point* specific_loc = new probe_point(*base_loc); |
5165 | probe_point* derived_loc = new probe_point(*base_loc); | |
edce5b67 | 5166 | |
c72aa911 JS |
5167 | for (unsigned i = 0; i < derived_loc->components.size(); ++i) |
5168 | if (derived_loc->components[i]->functor == TOK_MARK) | |
5169 | { | |
5170 | // replace the possibly wildcarded arg with the specific marker name | |
5171 | specific_loc->components[i] = | |
5172 | new probe_point::component(TOK_MARK, new literal_string(probe_name)); | |
edce5b67 | 5173 | |
a794dbeb FCE |
5174 | // XXX: similarly, fill in a TOK_PROVIDER() on the base_loc if |
5175 | // one was missing/unspecified/wildcarded. | |
5176 | ||
aff5d390 SC |
5177 | if (sess.verbose > 3) |
5178 | switch (probe_type) | |
5179 | { | |
5180 | case uprobe1_type: | |
5181 | clog << "probe_type == uprobe1, use statement addr: 0x" | |
5182 | << hex << pc << dec << endl; | |
5183 | break; | |
5184 | case uprobe2_type: | |
5185 | clog << "probe_type == uprobe2, use statement addr: 0x" | |
5186 | << hex << pc << dec << endl; | |
5187 | break; | |
5188 | ||
5189 | case kprobe1_type: | |
5190 | clog << "probe_type == kprobe1" << endl; | |
5191 | break; | |
5192 | case kprobe2_type: | |
5193 | clog << "probe_type == kprobe2" << endl; | |
5194 | break; | |
5195 | default: | |
5196 | clog << "probe_type == use_uprobe_no_dwarf, use label name: " | |
a794dbeb | 5197 | << "_stapprobe1_" << pp_mark << endl; |
aff5d390 SC |
5198 | } |
5199 | ||
c72aa911 JS |
5200 | switch (probe_type) |
5201 | { | |
aff5d390 SC |
5202 | case uprobe1_type: |
5203 | case uprobe2_type: | |
c72aa911 JS |
5204 | // process("executable").statement(probe_arg) |
5205 | derived_loc->components[i] = | |
9ea68eb9 | 5206 | new probe_point::component(TOK_STATEMENT, |
aff5d390 | 5207 | new literal_number(pc, true)); |
c72aa911 JS |
5208 | break; |
5209 | ||
aff5d390 SC |
5210 | case kprobe1_type: |
5211 | case kprobe2_type: | |
b642c901 SC |
5212 | // kernel.function("*getegid*") |
5213 | derived_loc->components[i] = | |
5214 | new probe_point::component(TOK_FUNCTION, new literal_string("*getegid*")); | |
5215 | if (derived_loc->components[i - 1]->functor == TOK_LIBRARY) | |
5216 | derived_loc->components.erase (derived_loc->components.begin() + i - 1); | |
5217 | break; | |
c72aa911 | 5218 | |
a794dbeb | 5219 | default: // deprecated |
c72aa911 JS |
5220 | // process("executable").function("*").label("_stapprobe1_MARK_NAME") |
5221 | derived_loc->components[i] = | |
5222 | new probe_point::component(TOK_FUNCTION, new literal_string("*")); | |
5223 | derived_loc->components.push_back | |
5224 | (new probe_point::component(TOK_LABEL, | |
a794dbeb | 5225 | new literal_string("_stapprobe1_" + pp_mark))); |
c72aa911 JS |
5226 | break; |
5227 | } | |
5228 | } | |
5229 | else if (derived_loc->components[i]->functor == TOK_PROCESS | |
aff5d390 | 5230 | && have_kprobe()) |
63b4fd14 | 5231 | { |
c72aa911 | 5232 | derived_loc->components[i] = new probe_point::component(TOK_KERNEL); |
63b4fd14 | 5233 | } |
edce5b67 | 5234 | |
c72aa911 | 5235 | return base_probe->create_alias(derived_loc, specific_loc); |
edce5b67 JS |
5236 | } |
5237 | ||
5238 | ||
20c6c071 | 5239 | void |
5227f1ea | 5240 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 5241 | probe * base, |
20c6c071 | 5242 | probe_point * location, |
86bf665e | 5243 | literal_map_t const & parameters, |
20c6c071 GH |
5244 | vector<derived_probe *> & finished_results) |
5245 | { | |
b20febf3 FCE |
5246 | // NB: the kernel/user dwlfpp objects are long-lived. |
5247 | // XXX: but they should be per-session, as this builder object | |
5248 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 5249 | |
7a24d422 FCE |
5250 | dwflpp* dw = 0; |
5251 | ||
7a24d422 | 5252 | string module_name; |
ae2552da FCE |
5253 | if (has_null_param (parameters, TOK_KERNEL)) |
5254 | { | |
5255 | dw = get_kern_dw(sess, "kernel"); | |
5256 | } | |
5257 | else if (get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 5258 | { |
ae2552da | 5259 | dw = get_kern_dw(sess, module_name); |
b8da0ad1 | 5260 | } |
7a24d422 | 5261 | else if (get_param (parameters, TOK_PROCESS, module_name)) |
b8da0ad1 | 5262 | { |
63b4fd14 | 5263 | string library_name; |
b642c901 | 5264 | user_path = find_executable (module_name); // canonicalize it |
63b4fd14 | 5265 | if (get_param (parameters, TOK_LIBRARY, library_name)) |
b642c901 SC |
5266 | { |
5267 | module_name = find_executable (library_name, "LD_LIBRARY_PATH"); | |
5268 | user_lib = module_name; | |
5269 | } | |
63b4fd14 | 5270 | else |
b642c901 | 5271 | module_name = user_path; // canonicalize it |
d0a7f5a9 | 5272 | |
e34d5d13 FCE |
5273 | if (sess.kernel_config["CONFIG_UTRACE"] != string("y")) |
5274 | throw semantic_error ("process probes not available without kernel CONFIG_UTRACE"); | |
5275 | ||
7a24d422 FCE |
5276 | // user-space target; we use one dwflpp instance per module name |
5277 | // (= program or shared library) | |
707bf35e | 5278 | dw = get_user_dw(sess, module_name); |
c8959a29 | 5279 | } |
20c6c071 | 5280 | |
5896cd05 MW |
5281 | if (sess.verbose > 3) |
5282 | clog << "dwarf_builder::build for " << module_name << endl; | |
5283 | ||
a794dbeb FCE |
5284 | string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __ |
5285 | if (get_param(parameters, TOK_MARK, dummy_mark_name)) | |
f28a8c28 | 5286 | { |
edce5b67 JS |
5287 | sdt_query sdtq(base, location, *dw, parameters, finished_results); |
5288 | dw->iterate_over_modules(&query_module, &sdtq); | |
5289 | return; | |
7a05f484 | 5290 | } |
20c6c071 | 5291 | |
b642c901 | 5292 | dwarf_query q(base, location, *dw, parameters, finished_results, user_path, user_lib); |
7a24d422 FCE |
5293 | |
5294 | // XXX: kernel.statement.absolute is a special case that requires no | |
5295 | // dwfl processing. This code should be in a separate builder. | |
7a24d422 | 5296 | if (q.has_kernel && q.has_absolute) |
37ebca01 | 5297 | { |
4baf0e53 | 5298 | // assert guru mode for absolute probes |
37ebca01 FCE |
5299 | if (! q.base_probe->privileged) |
5300 | { | |
edce5b67 JS |
5301 | throw semantic_error ("absolute statement probe in unprivileged script", |
5302 | q.base_probe->tok); | |
37ebca01 FCE |
5303 | } |
5304 | ||
5305 | // For kernel.statement(NUM).absolute probe points, we bypass | |
5306 | // all the debuginfo stuff: We just wire up a | |
5307 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 5308 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
5309 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
5310 | q.statement_num_val, q.statement_num_val, | |
5311 | q, 0); | |
37ebca01 | 5312 | finished_results.push_back (p); |
1a0dbc5a | 5313 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
5314 | return; |
5315 | } | |
5316 | ||
51178501 | 5317 | dw->iterate_over_modules(&query_module, &q); |
5f0a03a6 JK |
5318 | } |
5319 | ||
5320 | symbol_table::~symbol_table() | |
5321 | { | |
c9efa5c9 | 5322 | delete_map(map_by_addr); |
5f0a03a6 JK |
5323 | } |
5324 | ||
5325 | void | |
2867a2a1 JS |
5326 | symbol_table::add_symbol(const char *name, bool weak, bool descriptor, |
5327 | Dwarf_Addr addr, Dwarf_Addr *high_addr) | |
5f0a03a6 | 5328 | { |
ab91b232 JK |
5329 | #ifdef __powerpc__ |
5330 | // Map ".sys_foo" to "sys_foo". | |
5331 | if (name[0] == '.') | |
5332 | name++; | |
5333 | #endif | |
5f0a03a6 JK |
5334 | func_info *fi = new func_info(); |
5335 | fi->addr = addr; | |
5336 | fi->name = name; | |
ab91b232 | 5337 | fi->weak = weak; |
2867a2a1 | 5338 | fi->descriptor = descriptor; |
5f0a03a6 JK |
5339 | map_by_name[fi->name] = fi; |
5340 | // TODO: Use a multimap in case there are multiple static | |
5341 | // functions with the same name? | |
1c6b77e5 | 5342 | map_by_addr.insert(make_pair(addr, fi)); |
5f0a03a6 JK |
5343 | } |
5344 | ||
5345 | enum info_status | |
5346 | symbol_table::read_symbols(FILE *f, const string& path) | |
5347 | { | |
5348 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
5349 | int ret; | |
2e67a43b TM |
5350 | char *name = 0; |
5351 | char *mod = 0; | |
5f0a03a6 JK |
5352 | char type; |
5353 | unsigned long long addr; | |
5354 | Dwarf_Addr high_addr = 0; | |
5355 | int line = 0; | |
5356 | ||
5357 | // %as (non-POSIX) mallocs space for the string and stores its address. | |
5358 | while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0) | |
5359 | { | |
2e67a43b TM |
5360 | auto_free free_name(name); |
5361 | auto_free free_mod(mod); | |
5f0a03a6 JK |
5362 | line++; |
5363 | if (ret < 3) | |
5364 | { | |
41c262f3 | 5365 | cerr << "Symbol table error: Line " |
5f0a03a6 JK |
5366 | << line |
5367 | << " of symbol list from " | |
5368 | << path | |
5369 | << " is not in correct format: address type name [module]"; | |
5370 | // Caller should delete symbol_table object. | |
5371 | return info_absent; | |
5372 | } | |
2e67a43b | 5373 | else if (ret > 3) |
5f0a03a6 JK |
5374 | { |
5375 | // Modules are loaded above the kernel, so if we're getting | |
5376 | // modules, we're done. | |
2e67a43b | 5377 | break; |
5f0a03a6 | 5378 | } |
ab91b232 | 5379 | if (type == 'T' || type == 't' || type == 'W') |
2867a2a1 | 5380 | add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr); |
5f0a03a6 JK |
5381 | } |
5382 | ||
1c6b77e5 | 5383 | if (map_by_addr.size() < 1) |
5f0a03a6 JK |
5384 | { |
5385 | cerr << "Symbol table error: " | |
5386 | << path << " contains no function symbols." << endl; | |
5387 | return info_absent; | |
5388 | } | |
5389 | return info_present; | |
5390 | } | |
5391 | ||
5392 | // NB: This currently unused. We use get_from_elf() instead because | |
5393 | // that gives us raw addresses -- which we need for modules -- whereas | |
5394 | // nm provides the address relative to the beginning of the section. | |
5395 | enum info_status | |
83ca3872 MW |
5396 | symbol_table::read_from_elf_file(const string &path, |
5397 | const systemtap_session &sess) | |
5f0a03a6 JK |
5398 | { |
5399 | FILE *f; | |
5400 | string cmd = string("/usr/bin/nm -n --defined-only ") + path; | |
5401 | f = popen(cmd.c_str(), "r"); | |
5402 | if (!f) | |
5403 | { | |
5404 | // nm failures are detected by pclose, not popen. | |
5405 | cerr << "Internal error reading symbol table from " | |
5406 | << path << " -- " << strerror (errno); | |
5407 | return info_absent; | |
5408 | } | |
5409 | enum info_status status = read_symbols(f, path); | |
5410 | if (pclose(f) != 0) | |
5411 | { | |
83ca3872 | 5412 | if (status == info_present && ! sess.suppress_warnings) |
5f0a03a6 JK |
5413 | cerr << "Warning: nm cannot read symbol table from " << path; |
5414 | return info_absent; | |
5415 | } | |
5416 | return status; | |
5417 | } | |
5418 | ||
5419 | enum info_status | |
83ca3872 MW |
5420 | symbol_table::read_from_text_file(const string& path, |
5421 | const systemtap_session &sess) | |
5f0a03a6 JK |
5422 | { |
5423 | FILE *f = fopen(path.c_str(), "r"); | |
5424 | if (!f) | |
5425 | { | |
83ca3872 MW |
5426 | if (! sess.suppress_warnings) |
5427 | cerr << "Warning: cannot read symbol table from " | |
5428 | << path << " -- " << strerror (errno); | |
5f0a03a6 JK |
5429 | return info_absent; |
5430 | } | |
5431 | enum info_status status = read_symbols(f, path); | |
5432 | (void) fclose(f); | |
5433 | return status; | |
5434 | } | |
5435 | ||
46f7b6be JK |
5436 | void |
5437 | symbol_table::prepare_section_rejection(Dwfl_Module *mod) | |
5438 | { | |
5439 | #ifdef __powerpc__ | |
5440 | /* | |
5441 | * The .opd section contains function descriptors that can look | |
5442 | * just like function entry points. For example, there's a function | |
5443 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
5444 | * Reject all symbols in .opd. | |
5445 | */ | |
5446 | opd_section = SHN_UNDEF; | |
5447 | Dwarf_Addr bias; | |
5448 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
5449 | ?: dwfl_module_getelf (mod, &bias)); | |
5450 | Elf_Scn* scn = 0; | |
5451 | size_t shstrndx; | |
5452 | ||
5453 | if (!elf) | |
5454 | return; | |
fcc30d6d | 5455 | if (elf_getshdrstrndx (elf, &shstrndx) != 0) |
46f7b6be JK |
5456 | return; |
5457 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
5458 | { | |
5459 | GElf_Shdr shdr_mem; | |
5460 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
5461 | if (!shdr) | |
5462 | continue; | |
5463 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
5464 | if (!strcmp(name, ".opd")) | |
5465 | { | |
5466 | opd_section = elf_ndxscn(scn); | |
5467 | return; | |
5468 | } | |
5469 | } | |
5470 | #endif | |
5471 | } | |
5472 | ||
5473 | bool | |
5474 | symbol_table::reject_section(GElf_Word section) | |
5475 | { | |
5476 | if (section == SHN_UNDEF) | |
5477 | return true; | |
5478 | #ifdef __powerpc__ | |
5479 | if (section == opd_section) | |
5480 | return true; | |
5481 | #endif | |
5482 | return false; | |
5483 | } | |
5484 | ||
5f0a03a6 JK |
5485 | enum info_status |
5486 | symbol_table::get_from_elf() | |
5487 | { | |
5488 | Dwarf_Addr high_addr = 0; | |
5489 | Dwfl_Module *mod = mod_info->mod; | |
5490 | int syments = dwfl_module_getsymtab(mod); | |
5491 | assert(syments); | |
46f7b6be | 5492 | prepare_section_rejection(mod); |
5f0a03a6 JK |
5493 | for (int i = 1; i < syments; ++i) |
5494 | { | |
5495 | GElf_Sym sym; | |
ab91b232 JK |
5496 | GElf_Word section; |
5497 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
2867a2a1 | 5498 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC) |
ab91b232 | 5499 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
2867a2a1 | 5500 | reject_section(section), sym.st_value, &high_addr); |
5f0a03a6 JK |
5501 | } |
5502 | return info_present; | |
5503 | } | |
5504 | ||
5f0a03a6 JK |
5505 | func_info * |
5506 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
5507 | { | |
1c6b77e5 JS |
5508 | iterator_t iter = map_by_addr.upper_bound(addr); |
5509 | if (iter == map_by_addr.begin()) | |
5f0a03a6 | 5510 | return NULL; |
2e67a43b | 5511 | else |
1c6b77e5 | 5512 | return (--iter)->second; |
5f0a03a6 JK |
5513 | } |
5514 | ||
5515 | func_info * | |
5516 | symbol_table::lookup_symbol(const string& name) | |
5517 | { | |
5518 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
5519 | if (i == map_by_name.end()) | |
5520 | return NULL; | |
5521 | return i->second; | |
5522 | } | |
5523 | ||
5524 | Dwarf_Addr | |
5525 | symbol_table::lookup_symbol_address(const string& name) | |
5526 | { | |
5527 | func_info *fi = lookup_symbol(name); | |
5528 | if (fi) | |
5529 | return fi->addr; | |
5530 | return 0; | |
5531 | } | |
5532 | ||
ab91b232 JK |
5533 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
5534 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
5535 | // For system calls not implemented elsewhere, this weak symbol shows up | |
5536 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
5537 | // we refuse to consider such symbols. Here we delete them from our | |
5538 | // symbol table. | |
5539 | // TODO: Consider generalizing this and/or making it part of blacklist | |
5540 | // processing. | |
5541 | void | |
5542 | symbol_table::purge_syscall_stubs() | |
5543 | { | |
5544 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
5545 | if (stub_addr == 0) | |
5546 | return; | |
1c6b77e5 | 5547 | range_t purge_range = map_by_addr.equal_range(stub_addr); |
2e67a43b TM |
5548 | for (iterator_t iter = purge_range.first; |
5549 | iter != purge_range.second; | |
1c6b77e5 | 5550 | ) |
ab91b232 | 5551 | { |
1c6b77e5 | 5552 | func_info *fi = iter->second; |
2e67a43b | 5553 | if (fi->weak && fi->name != "sys_ni_syscall") |
ab91b232 | 5554 | { |
2e67a43b | 5555 | map_by_name.erase(fi->name); |
1c6b77e5 | 5556 | map_by_addr.erase(iter++); |
2e67a43b | 5557 | delete fi; |
2e67a43b | 5558 | } |
1c6b77e5 JS |
5559 | else |
5560 | iter++; | |
ab91b232 JK |
5561 | } |
5562 | } | |
5563 | ||
5f0a03a6 JK |
5564 | void |
5565 | module_info::get_symtab(dwarf_query *q) | |
5566 | { | |
5567 | systemtap_session &sess = q->sess; | |
5568 | ||
1c6b77e5 JS |
5569 | if (symtab_status != info_unknown) |
5570 | return; | |
5571 | ||
5f0a03a6 JK |
5572 | sym_table = new symbol_table(this); |
5573 | if (!elf_path.empty()) | |
5574 | { | |
83ca3872 MW |
5575 | if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty() |
5576 | && ! sess.suppress_warnings) | |
5f0a03a6 JK |
5577 | cerr << "Warning: reading symbol table from " |
5578 | << elf_path | |
5579 | << " -- ignoring " | |
5580 | << sess.kernel_symtab_path | |
83ca3872 | 5581 | << endl; |
5f0a03a6 JK |
5582 | symtab_status = sym_table->get_from_elf(); |
5583 | } | |
5584 | else | |
5585 | { | |
5586 | assert(name == TOK_KERNEL); | |
5587 | if (sess.kernel_symtab_path.empty()) | |
5588 | { | |
5589 | symtab_status = info_absent; | |
5590 | cerr << "Error: Cannot find vmlinux." | |
5591 | << " Consider using --kmap instead of --kelf." | |
5592 | << endl;; | |
5593 | } | |
5594 | else | |
5595 | { | |
5596 | symtab_status = | |
83ca3872 | 5597 | sym_table->read_from_text_file(sess.kernel_symtab_path, sess); |
5f0a03a6 JK |
5598 | if (symtab_status == info_present) |
5599 | { | |
5600 | sess.sym_kprobes_text_start = | |
5601 | sym_table->lookup_symbol_address("__kprobes_text_start"); | |
5602 | sess.sym_kprobes_text_end = | |
5603 | sym_table->lookup_symbol_address("__kprobes_text_end"); | |
5604 | sess.sym_stext = sym_table->lookup_symbol_address("_stext"); | |
5f0a03a6 JK |
5605 | } |
5606 | } | |
5607 | } | |
5608 | if (symtab_status == info_absent) | |
5609 | { | |
5610 | delete sym_table; | |
5611 | sym_table = NULL; | |
5612 | return; | |
5613 | } | |
5614 | ||
ab91b232 JK |
5615 | if (name == TOK_KERNEL) |
5616 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
5617 | } |
5618 | ||
1c6b77e5 JS |
5619 | // update_symtab reconciles data between the elf symbol table and the dwarf |
5620 | // function enumeration. It updates the symbol table entries with the dwarf | |
5621 | // die that describes the function, which also signals to query_module_symtab | |
5622 | // that a statement probe isn't needed. In return, it also adds aliases to the | |
5623 | // function table for names that share the same addr/die. | |
5624 | void | |
5625 | module_info::update_symtab(cu_function_cache_t *funcs) | |
5626 | { | |
5627 | if (!sym_table) | |
5628 | return; | |
5629 | ||
5630 | cu_function_cache_t new_funcs; | |
5631 | ||
5632 | for (cu_function_cache_t::iterator func = funcs->begin(); | |
5633 | func != funcs->end(); func++) | |
5634 | { | |
5635 | // optimization: inlines will never be in the symbol table | |
5636 | if (dwarf_func_inline(&func->second) != 0) | |
5637 | continue; | |
5638 | ||
5639 | func_info *fi = sym_table->lookup_symbol(func->first); | |
5640 | if (!fi) | |
5641 | continue; | |
5642 | ||
5643 | // iterate over all functions at the same address | |
5644 | symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr); | |
5645 | for (symbol_table::iterator_t it = er.first; it != er.second; ++it) | |
5646 | { | |
5647 | // update this function with the dwarf die | |
5648 | it->second->die = func->second; | |
5649 | ||
5650 | // if this function is a new alias, then | |
5651 | // save it to merge into the function cache | |
5652 | if (it->second != fi) | |
b7478964 | 5653 | new_funcs.insert(make_pair(it->second->name, it->second->die)); |
1c6b77e5 JS |
5654 | } |
5655 | } | |
5656 | ||
5657 | // add all discovered aliases back into the function cache | |
5658 | // NB: this won't replace any names that dwarf may have already found | |
5659 | funcs->insert(new_funcs.begin(), new_funcs.end()); | |
5660 | } | |
5661 | ||
5f0a03a6 JK |
5662 | module_info::~module_info() |
5663 | { | |
5664 | if (sym_table) | |
5665 | delete sym_table; | |
b55bc428 FCE |
5666 | } |
5667 | ||
935447c8 | 5668 | // ------------------------------------------------------------------------ |
888af770 | 5669 | // user-space probes |
935447c8 DS |
5670 | // ------------------------------------------------------------------------ |
5671 | ||
935447c8 | 5672 | |
888af770 | 5673 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> |
935447c8 | 5674 | { |
89ba3085 FCE |
5675 | private: |
5676 | string make_pbm_key (uprobe_derived_probe* p) { | |
5677 | return p->module + "|" + p->section + "|" + lex_cast(p->pid); | |
5678 | } | |
5679 | ||
935447c8 | 5680 | public: |
888af770 | 5681 | void emit_module_decls (systemtap_session& s); |
935447c8 DS |
5682 | void emit_module_init (systemtap_session& s); |
5683 | void emit_module_exit (systemtap_session& s); | |
5684 | }; | |
5685 | ||
5686 | ||
888af770 FCE |
5687 | void |
5688 | uprobe_derived_probe::join_group (systemtap_session& s) | |
5689 | { | |
5690 | if (! s.uprobe_derived_probes) | |
5691 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
5692 | s.uprobe_derived_probes->enroll (this); | |
93646f4d | 5693 | enable_task_finder(s); |
a96d1db0 | 5694 | |
8a03658e JS |
5695 | // Ask buildrun.cxx to build extra module if needed, and |
5696 | // signal staprun to load that module | |
5697 | s.need_uprobes = true; | |
a96d1db0 DN |
5698 | } |
5699 | ||
888af770 | 5700 | |
2865d17a DB |
5701 | void |
5702 | uprobe_derived_probe::emit_unprivileged_assertion (translator_output* o) | |
5703 | { | |
5704 | // These probes are allowed for unprivileged users, but only in the | |
5705 | // context of processes which they own. | |
5706 | emit_process_owner_assertion (o); | |
5707 | } | |
5708 | ||
5709 | ||
888af770 | 5710 | struct uprobe_builder: public derived_probe_builder |
a96d1db0 | 5711 | { |
888af770 | 5712 | uprobe_builder() {} |
a96d1db0 DN |
5713 | virtual void build(systemtap_session & sess, |
5714 | probe * base, | |
5715 | probe_point * location, | |
86bf665e | 5716 | literal_map_t const & parameters, |
a96d1db0 DN |
5717 | vector<derived_probe *> & finished_results) |
5718 | { | |
888af770 | 5719 | int64_t process, address; |
a96d1db0 | 5720 | |
888af770 | 5721 | bool b1 = get_param (parameters, TOK_PROCESS, process); |
ced347a9 | 5722 | (void) b1; |
888af770 | 5723 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 5724 | (void) b2; |
888af770 FCE |
5725 | bool rr = has_null_param (parameters, TOK_RETURN); |
5726 | assert (b1 && b2); // by pattern_root construction | |
a96d1db0 | 5727 | |
0973d815 | 5728 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
a96d1db0 DN |
5729 | } |
5730 | }; | |
5731 | ||
5732 | ||
5733 | void | |
775d51e5 | 5734 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) |
a96d1db0 | 5735 | { |
888af770 | 5736 | if (probes.empty()) return; |
775d51e5 | 5737 | s.op->newline() << "/* ---- user probes ---- */"; |
471fca5e TM |
5738 | // If uprobes isn't in the kernel, pull it in from the runtime. |
5739 | ||
5740 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; | |
5741 | s.op->newline() << "#include <linux/uprobes.h>"; | |
5742 | s.op->newline() << "#else"; | |
5743 | s.op->newline() << "#include \"uprobes/uprobes.h\""; | |
5744 | s.op->newline() << "#endif"; | |
5745 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; | |
5746 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
5747 | s.op->newline() << "#endif"; | |
a96d1db0 | 5748 | |
43241c44 FCE |
5749 | // We'll probably need at least this many: |
5750 | unsigned minuprobes = probes.size(); | |
5751 | // .. but we don't want so many that .bss is inflated (PR10507): | |
5752 | unsigned uprobesize = 64; | |
5753 | unsigned maxuprobesmem = 10*1024*1024; // 10 MB | |
5754 | unsigned maxuprobes = maxuprobesmem / uprobesize; | |
5755 | ||
aaf7ffe8 FCE |
5756 | // Let's choose a value on the geometric middle. This should end up |
5757 | // between minuprobes and maxuprobes. It's OK if this number turns | |
5758 | // out to be < minuprobes or > maxuprobes. At worst, we get a | |
5759 | // run-time error of one kind (too few: missed uprobe registrations) | |
5760 | // or another (too many: vmalloc errors at module load time). | |
5761 | unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes); | |
43241c44 | 5762 | |
6d0f3f0c | 5763 | s.op->newline() << "#ifndef MAXUPROBES"; |
43241c44 | 5764 | s.op->newline() << "#define MAXUPROBES " << default_maxuprobes; |
6d0f3f0c | 5765 | s.op->newline() << "#endif"; |
a96d1db0 | 5766 | |
cc52276b WC |
5767 | // Forward decls |
5768 | s.op->newline() << "#include \"uprobes-common.h\""; | |
5769 | ||
5e112f92 FCE |
5770 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
5771 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
cc52276b WC |
5772 | // XXX: consider a slab cache or somesuch for stap_uprobes |
5773 | s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];"; | |
5e112f92 | 5774 | s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
a96d1db0 | 5775 | |
89ba3085 FCE |
5776 | s.op->assert_0_indent(); |
5777 | ||
89ba3085 FCE |
5778 | // Assign task-finder numbers as we build up the stap_uprobe_tf table. |
5779 | // This means we process probes[] in two passes. | |
5780 | map <string,unsigned> module_index; | |
5781 | unsigned module_index_ctr = 0; | |
5782 | ||
cc52276b WC |
5783 | // not const since embedded task_finder_target struct changes |
5784 | s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {"; | |
89ba3085 FCE |
5785 | s.op->indent(1); |
5786 | for (unsigned i=0; i<probes.size(); i++) | |
5787 | { | |
5788 | uprobe_derived_probe *p = probes[i]; | |
5789 | string pbmkey = make_pbm_key (p); | |
5790 | if (module_index.find (pbmkey) == module_index.end()) | |
5791 | { | |
5792 | module_index[pbmkey] = module_index_ctr++; | |
5793 | ||
5794 | s.op->newline() << "{"; | |
5795 | // NB: it's essential that make_pbm_key() use all of and | |
5796 | // only the same fields as we're about to emit. | |
5797 | s.op->line() << " .finder={"; | |
5798 | if (p->pid != 0) | |
5799 | s.op->line() << " .pid=" << p->pid; | |
5800 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program | |
5801 | { | |
5802 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
5803 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
5804 | } | |
aff5d390 | 5805 | if (p->section != ".absolute") // ET_DYN |
89ba3085 | 5806 | { |
63b4fd14 SC |
5807 | if (p->has_library && p->sdt_semaphore_addr != 0) |
5808 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
89ba3085 FCE |
5809 | s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, "; |
5810 | s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, "; | |
19d91f6c | 5811 | s.op->line() << " .callback=&stap_uprobe_process_munmap,"; |
89ba3085 FCE |
5812 | } |
5813 | ||
5814 | s.op->line() << " },"; | |
5815 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
5816 | s.op->line() << " },"; | |
5817 | } | |
5818 | else | |
5819 | ; // skip it in this pass, already have a suitable stap_uprobe_tf slot for it. | |
5820 | } | |
5821 | s.op->newline(-1) << "};"; | |
5822 | ||
5823 | s.op->assert_0_indent(); | |
5824 | ||
cc52276b WC |
5825 | // NB: read-only structure |
5826 | s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {"; | |
a96d1db0 | 5827 | s.op->indent(1); |
888af770 FCE |
5828 | for (unsigned i =0; i<probes.size(); i++) |
5829 | { | |
5830 | uprobe_derived_probe* p = probes[i]; | |
5831 | s.op->newline() << "{"; | |
89ba3085 FCE |
5832 | string key = make_pbm_key (p); |
5833 | unsigned value = module_index[key]; | |
759e1d76 FCE |
5834 | if (value != 0) |
5835 | s.op->line() << " .tfi=" << value << ","; | |
6b66b9f7 | 5836 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
888af770 FCE |
5837 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
5838 | s.op->line() << " .ph=&" << p->name << ","; | |
4ddb6dd0 | 5839 | |
038c38c6 | 5840 | if (p->sdt_semaphore_addr != 0) |
63b4fd14 | 5841 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" |
038c38c6 | 5842 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; |
4ddb6dd0 JS |
5843 | |
5844 | if (p->has_return) | |
5845 | s.op->line() << " .return_p=1,"; | |
888af770 FCE |
5846 | s.op->line() << " },"; |
5847 | } | |
5848 | s.op->newline(-1) << "};"; | |
a96d1db0 | 5849 | |
89ba3085 FCE |
5850 | s.op->assert_0_indent(); |
5851 | ||
48e685da | 5852 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 5853 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
89ba3085 | 5854 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 5855 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
5856 | s.op->newline() << "if (sup->spec_index < 0 ||" |
5857 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
a96d1db0 | 5858 | s.op->newline() << "c->regs = regs;"; |
fd9187da | 5859 | s.op->newline() << "c->ri = GET_PC_URETPROBE_NONE;"; |
6415ddde MW |
5860 | |
5861 | // Make it look like the IP is set as it would in the actual user | |
5862 | // task when calling real probe handler. Reset IP regs on return, so | |
5863 | // we don't confuse uprobes. PR10458 | |
5864 | s.op->newline() << "{"; | |
5865 | s.op->indent(1); | |
5866 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);"; | |
259d54c0 | 5867 | s.op->newline() << "SET_REG_IP(regs, inst->vaddr);"; |
6d0f3f0c | 5868 | s.op->newline() << "(*sups->ph) (c);"; |
259d54c0 | 5869 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
5870 | s.op->newline(-1) << "}"; |
5871 | ||
a96d1db0 | 5872 | common_probe_entryfn_epilogue (s.op); |
888af770 | 5873 | s.op->newline(-1) << "}"; |
a96d1db0 | 5874 | |
48e685da | 5875 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 5876 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
89ba3085 | 5877 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 5878 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
5e562a69 | 5879 | s.op->newline() << "c->ri = inst;"; |
6d0f3f0c FCE |
5880 | s.op->newline() << "if (sup->spec_index < 0 ||" |
5881 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 FCE |
5882 | // XXX: kretprobes saves "c->pi = inst;" too |
5883 | s.op->newline() << "c->regs = regs;"; | |
6415ddde MW |
5884 | |
5885 | // Make it look like the IP is set as it would in the actual user | |
5886 | // task when calling real probe handler. Reset IP regs on return, so | |
5887 | // we don't confuse uprobes. PR10458 | |
5888 | s.op->newline() << "{"; | |
5889 | s.op->indent(1); | |
5890 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);"; | |
5e562a69 | 5891 | s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);"; |
6d0f3f0c | 5892 | s.op->newline() << "(*sups->ph) (c);"; |
259d54c0 | 5893 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
5894 | s.op->newline(-1) << "}"; |
5895 | ||
888af770 | 5896 | common_probe_entryfn_epilogue (s.op); |
a96d1db0 DN |
5897 | s.op->newline(-1) << "}"; |
5898 | ||
89ba3085 | 5899 | s.op->newline(); |
cc52276b | 5900 | s.op->newline() << "#include \"uprobes-common.c\""; |
6d0f3f0c | 5901 | s.op->newline(); |
888af770 | 5902 | } |
935447c8 DS |
5903 | |
5904 | ||
888af770 FCE |
5905 | void |
5906 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
935447c8 | 5907 | { |
888af770 | 5908 | if (probes.empty()) return; |
935447c8 | 5909 | |
5e112f92 | 5910 | s.op->newline() << "/* ---- user probes ---- */"; |
935447c8 | 5911 | |
01b05e2e | 5912 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
5913 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
5914 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
5915 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
5916 | // initialized to zero. This is so that we can use | |
5917 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
5918 | s.op->newline(-1) << "}"; |
5919 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
935447c8 | 5920 | |
89ba3085 FCE |
5921 | // Set up the task_finders |
5922 | s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {"; | |
5923 | s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];"; | |
5924 | s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better | |
5925 | s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);"; | |
935447c8 | 5926 | |
5e112f92 FCE |
5927 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any |
5928 | // finders already registered, since mere registration does not | |
5929 | // cause any utrace or memory allocation actions. That happens only | |
5930 | // later, once the task finder engine starts running. So, for a | |
5931 | // partial initialization requiring unwind, we need do nothing. | |
5932 | s.op->newline() << "if (rc) break;"; | |
a7a68293 | 5933 | |
888af770 FCE |
5934 | s.op->newline(-1) << "}"; |
5935 | } | |
d0ea46ce | 5936 | |
d0a7f5a9 | 5937 | |
888af770 FCE |
5938 | void |
5939 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
5940 | { | |
5941 | if (probes.empty()) return; | |
5942 | s.op->newline() << "/* ---- user probes ---- */"; | |
e56e51c9 | 5943 | |
6d0f3f0c FCE |
5944 | // NB: there is no stap_unregister_task_finder_target call; |
5945 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
5946 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
5947 | // | |
5948 | // This function blocks until all callbacks are completed, so there | |
5949 | // is supposed to be no possibility of any registration-related code starting | |
5950 | // to run in parallel with our shutdown here. So we don't need to protect the | |
5951 | // stap_uprobes[] array with the mutex. | |
d0a7f5a9 | 5952 | |
01b05e2e | 5953 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 | 5954 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
89ba3085 | 5955 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6d0f3f0c | 5956 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd | 5957 | |
8faa1fc5 | 5958 | // PR10655: decrement that ENABLED semaphore |
c116c31b | 5959 | s.op->newline() << "if (sup->sdt_sem_address) {"; |
8faa1fc5 FCE |
5960 | s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size |
5961 | s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);"; | |
5962 | s.op->newline() << "struct task_struct *tsk;"; | |
5963 | s.op->newline() << "rcu_read_lock();"; | |
6846cfc8 | 5964 | |
8faa1fc5 FCE |
5965 | // XXX: what a gross cut & paste job from tapset/task.stp, just for a lousy pid->task_struct* lookup |
5966 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)"; | |
5967 | s.op->newline() << " { struct pid *p_pid = find_get_pid(pid);"; | |
5968 | s.op->newline() << " tsk = pid_task(p_pid, PIDTYPE_PID);"; | |
5969 | s.op->newline() << " put_pid(p_pid); }"; | |
5970 | s.op->newline() << "#else"; | |
5971 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)"; | |
5972 | s.op->newline() << " tsk = find_task_by_vpid (pid);"; | |
5973 | s.op->newline() << "#else"; | |
5974 | s.op->newline() << " tsk = find_task_by_pid (pid);"; | |
5975 | s.op->newline() << "#endif /* 2.6.24 */"; | |
5976 | s.op->newline() << "#endif /* 2.6.31 */"; | |
5977 | ||
5978 | s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching | |
3c5b8e2b | 5979 | s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {"; |
63b4fd14 | 5980 | s.op->newline(1) << "sdt_semaphore --;"; |
903b9fcd | 5981 | s.op->newline() << "#ifdef DEBUG_UPROBES"; |
c116c31b | 5982 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);"; |
903b9fcd | 5983 | s.op->newline() << "#endif"; |
3c5b8e2b | 5984 | s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);"; |
93c84191 | 5985 | s.op->newline(-1) << "}"; |
8faa1fc5 FCE |
5986 | // XXX: need to analyze possibility of race condition |
5987 | s.op->newline(-1) << "}"; | |
5988 | s.op->newline() << "rcu_read_unlock();"; | |
5989 | s.op->newline(-1) << "}"; | |
6846cfc8 | 5990 | |
3568f1dd FCE |
5991 | s.op->newline() << "if (sups->return_p) {"; |
5992 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
89ba3085 | 5993 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);"; |
3568f1dd | 5994 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
5995 | // NB: PR6829 does not change that we still need to unregister at |
5996 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
5997 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
5998 | s.op->newline(-1) << "} else {"; | |
5999 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
8faa1fc5 | 6000 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);"; |
3568f1dd FCE |
6001 | s.op->newline() << "#endif"; |
6002 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
6003 | s.op->newline(-1) << "}"; | |
935447c8 | 6004 | |
6d0f3f0c | 6005 | s.op->newline() << "sup->spec_index = -1;"; |
935447c8 | 6006 | |
3568f1dd FCE |
6007 | // XXX: uprobe missed counts? |
6008 | ||
6d0f3f0c | 6009 | s.op->newline(-1) << "}"; |
935447c8 | 6010 | |
5e112f92 | 6011 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; |
935447c8 DS |
6012 | } |
6013 | ||
e6fe60e7 AM |
6014 | // ------------------------------------------------------------------------ |
6015 | // Kprobe derived probes | |
6016 | // ------------------------------------------------------------------------ | |
6017 | ||
4627ed58 | 6018 | static const string TOK_KPROBE("kprobe"); |
935447c8 | 6019 | |
bae55db9 | 6020 | struct kprobe_derived_probe: public derived_probe |
d0ea46ce | 6021 | { |
bae55db9 JS |
6022 | kprobe_derived_probe (probe *base, |
6023 | probe_point *location, | |
6024 | const string& name, | |
6025 | int64_t stmt_addr, | |
6026 | bool has_return, | |
6027 | bool has_statement, | |
6028 | bool has_maxactive, | |
b642c901 SC |
6029 | bool has_path, |
6030 | bool has_library, | |
6031 | long maxactive_val, | |
6032 | const string& path, | |
6033 | const string& library | |
bae55db9 JS |
6034 | ); |
6035 | string symbol_name; | |
6036 | Dwarf_Addr addr; | |
6037 | bool has_return; | |
6038 | bool has_statement; | |
6039 | bool has_maxactive; | |
b642c901 SC |
6040 | bool has_path; |
6041 | bool has_library; | |
bae55db9 | 6042 | long maxactive_val; |
b642c901 SC |
6043 | string path; |
6044 | string library; | |
bae55db9 JS |
6045 | bool access_var; |
6046 | void printsig (std::ostream &o) const; | |
6047 | void join_group (systemtap_session& s); | |
6048 | }; | |
d0ea46ce | 6049 | |
bae55db9 JS |
6050 | struct kprobe_derived_probe_group: public derived_probe_group |
6051 | { | |
6052 | private: | |
6053 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
6054 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
d0ea46ce | 6055 | |
bae55db9 JS |
6056 | public: |
6057 | void enroll (kprobe_derived_probe* probe); | |
6058 | void emit_module_decls (systemtap_session& s); | |
6059 | void emit_module_init (systemtap_session& s); | |
6060 | void emit_module_exit (systemtap_session& s); | |
6061 | }; | |
d0ea46ce | 6062 | |
e6fe60e7 AM |
6063 | kprobe_derived_probe::kprobe_derived_probe (probe *base, |
6064 | probe_point *location, | |
b6371390 | 6065 | const string& name, |
e6fe60e7 | 6066 | int64_t stmt_addr, |
b6371390 JS |
6067 | bool has_return, |
6068 | bool has_statement, | |
6069 | bool has_maxactive, | |
b642c901 SC |
6070 | bool has_path, |
6071 | bool has_library, | |
6072 | long maxactive_val, | |
6073 | const string& path, | |
6074 | const string& library | |
b6371390 | 6075 | ): |
e6fe60e7 AM |
6076 | derived_probe (base, location), |
6077 | symbol_name (name), addr (stmt_addr), | |
b6371390 | 6078 | has_return (has_return), has_statement (has_statement), |
b642c901 SC |
6079 | has_maxactive (has_maxactive), has_path (has_path), |
6080 | has_library (has_library), | |
6081 | maxactive_val (maxactive_val), | |
6082 | path (path), library (library) | |
e6fe60e7 AM |
6083 | { |
6084 | this->tok = base->tok; | |
6085 | this->access_var = false; | |
d0ea46ce | 6086 | |
e6fe60e7 AM |
6087 | #ifndef USHRT_MAX |
6088 | #define USHRT_MAX 32767 | |
6089 | #endif | |
d0ea46ce | 6090 | |
46856d8d JS |
6091 | // Expansion of $target variables in the probe body produces an error during |
6092 | // translate phase, since we're not using debuginfo | |
d0ea46ce | 6093 | |
e6fe60e7 | 6094 | vector<probe_point::component*> comps; |
46856d8d | 6095 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 6096 | |
46856d8d JS |
6097 | if (has_statement) |
6098 | { | |
9ea68eb9 JS |
6099 | comps.push_back (new probe_point::component(TOK_STATEMENT, |
6100 | new literal_number(addr, true))); | |
46856d8d JS |
6101 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); |
6102 | } | |
6103 | else | |
6104 | { | |
6105 | size_t pos = name.find(':'); | |
6106 | if (pos != string::npos) | |
d0ea46ce | 6107 | { |
46856d8d JS |
6108 | string module = name.substr(0, pos); |
6109 | string function = name.substr(pos + 1); | |
6110 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
6111 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
6112 | } | |
6113 | else | |
6114 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 6115 | } |
d0ea46ce | 6116 | |
b6371390 JS |
6117 | if (has_return) |
6118 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
6119 | if (has_maxactive) | |
6120 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d0ea46ce | 6121 | |
e6fe60e7 AM |
6122 | this->sole_location()->components = comps; |
6123 | } | |
d0ea46ce | 6124 | |
e6fe60e7 AM |
6125 | void kprobe_derived_probe::printsig (ostream& o) const |
6126 | { | |
6127 | sole_location()->print (o); | |
6128 | o << " /* " << " name = " << symbol_name << "*/"; | |
6129 | printsig_nested (o); | |
6130 | } | |
d0ea46ce | 6131 | |
e6fe60e7 AM |
6132 | void kprobe_derived_probe::join_group (systemtap_session& s) |
6133 | { | |
d0ea46ce | 6134 | |
e6fe60e7 AM |
6135 | if (! s.kprobe_derived_probes) |
6136 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
6137 | s.kprobe_derived_probes->enroll (this); | |
d0ea46ce | 6138 | |
e6fe60e7 | 6139 | } |
d0ea46ce | 6140 | |
e6fe60e7 AM |
6141 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) |
6142 | { | |
6143 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
6144 | // probes of same symbol should share single kprobe/kretprobe | |
6145 | } | |
d0ea46ce | 6146 | |
e6fe60e7 AM |
6147 | void |
6148 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6149 | { | |
6150 | if (probes_by_module.empty()) return; | |
d0ea46ce | 6151 | |
e6fe60e7 | 6152 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; |
d0ea46ce | 6153 | |
e6fe60e7 AM |
6154 | // Warn of misconfigured kernels |
6155 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
6156 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
6157 | s.op->newline() << "#endif"; | |
6158 | s.op->newline(); | |
d0ea46ce | 6159 | |
f07c3b68 | 6160 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 6161 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
6162 | s.op->newline() << "#endif"; |
6163 | ||
e6fe60e7 | 6164 | // Forward declare the master entry functions |
88747011 | 6165 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 6166 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 6167 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 | 6168 | s.op->line() << " struct pt_regs *regs);"; |
d0ea46ce | 6169 | |
e6fe60e7 AM |
6170 | // Emit an array of kprobe/kretprobe pointers |
6171 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
c9116e99 | 6172 | s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];"; |
e6fe60e7 | 6173 | s.op->newline() << "#endif"; |
d0ea46ce | 6174 | |
e6fe60e7 | 6175 | // Emit the actual probe list. |
d0ea46ce | 6176 | |
e6fe60e7 AM |
6177 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; |
6178 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
6179 | s.op->newline() << "#ifdef __ia64__"; | |
6180 | s.op->newline() << "struct kprobe dummy;"; | |
6181 | s.op->newline() << "#endif"; | |
6182 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
6183 | // NB: bss! | |
d0ea46ce | 6184 | |
e6fe60e7 AM |
6185 | s.op->newline() << "static struct stap_dwarfless_probe {"; |
6186 | s.op->newline(1) << "const unsigned return_p:1;"; | |
6187 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 6188 | s.op->newline() << "const unsigned optional_p:1;"; |
e6fe60e7 AM |
6189 | s.op->newline() << "unsigned registered_p:1;"; |
6190 | s.op->newline() << "const unsigned short maxactive_val;"; | |
935447c8 | 6191 | |
e6fe60e7 AM |
6192 | // Function Names are mostly small and uniform enough to justify putting |
6193 | // char[MAX]'s into the array instead of relocated char*'s. | |
935447c8 | 6194 | |
e6fe60e7 AM |
6195 | size_t pp_name_max = 0, symbol_string_name_max = 0; |
6196 | size_t pp_name_tot = 0, symbol_string_name_tot = 0; | |
6197 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
6270adc1 | 6198 | { |
e6fe60e7 AM |
6199 | kprobe_derived_probe* p = it->second; |
6200 | #define DOIT(var,expr) do { \ | |
6201 | size_t var##_size = (expr) + 1; \ | |
6202 | var##_max = max (var##_max, var##_size); \ | |
6203 | var##_tot += var##_size; } while (0) | |
6204 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
6205 | DOIT(symbol_string_name, p->symbol_name.size()); | |
6206 | #undef DOIT | |
6270adc1 MH |
6207 | } |
6208 | ||
e6fe60e7 AM |
6209 | #define CALCIT(var) \ |
6210 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
935447c8 | 6211 | |
e6fe60e7 AM |
6212 | CALCIT(pp); |
6213 | CALCIT(symbol_string); | |
6214 | #undef CALCIT | |
6270adc1 | 6215 | |
bd659351 | 6216 | s.op->newline() << "unsigned long address;"; |
e6fe60e7 AM |
6217 | s.op->newline() << "void (* const ph) (struct context*);"; |
6218 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; | |
6219 | s.op->indent(1); | |
6270adc1 | 6220 | |
e6fe60e7 AM |
6221 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
6222 | { | |
6223 | kprobe_derived_probe* p = it->second; | |
6224 | s.op->newline() << "{"; | |
6225 | if (p->has_return) | |
6226 | s.op->line() << " .return_p=1,"; | |
6270adc1 | 6227 | |
e6fe60e7 AM |
6228 | if (p->has_maxactive) |
6229 | { | |
6230 | s.op->line() << " .maxactive_p=1,"; | |
6231 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
6232 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
6233 | } | |
6270adc1 | 6234 | |
b350f56b JS |
6235 | if (p->locations[0]->optional) |
6236 | s.op->line() << " .optional_p=1,"; | |
6237 | ||
e6fe60e7 | 6238 | if (p->has_statement) |
c8d9d15e | 6239 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 6240 | else |
c8d9d15e | 6241 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
5d67b47c | 6242 | |
e6fe60e7 AM |
6243 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
6244 | s.op->line() << " .ph=&" << p->name; | |
6245 | s.op->line() << " },"; | |
935447c8 DS |
6246 | } |
6247 | ||
e6fe60e7 | 6248 | s.op->newline(-1) << "};"; |
5d67b47c | 6249 | |
e6fe60e7 AM |
6250 | // Emit the kprobes callback function |
6251 | s.op->newline(); | |
88747011 | 6252 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
6253 | s.op->line() << " struct pt_regs *regs) {"; |
6254 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
6255 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
6256 | // Check that the index is plausible | |
6257 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
6258 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
6259 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
6260 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
6261 | s.op->line() << "];"; | |
6262 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); | |
6263 | s.op->newline() << "c->regs = regs;"; | |
6415ddde MW |
6264 | |
6265 | // Make it look like the IP is set as it wouldn't have been replaced | |
6266 | // by a breakpoint instruction when calling real probe handler. Reset | |
6267 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
6268 | s.op->newline() << "{"; | |
6269 | s.op->indent(1); | |
6270 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);"; | |
259d54c0 | 6271 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
e6fe60e7 | 6272 | s.op->newline() << "(*sdp->ph) (c);"; |
259d54c0 | 6273 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
6274 | s.op->newline(-1) << "}"; |
6275 | ||
e6fe60e7 AM |
6276 | common_probe_entryfn_epilogue (s.op); |
6277 | s.op->newline() << "return 0;"; | |
6278 | s.op->newline(-1) << "}"; | |
935447c8 | 6279 | |
e6fe60e7 AM |
6280 | // Same for kretprobes |
6281 | s.op->newline(); | |
88747011 | 6282 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
6283 | s.op->line() << " struct pt_regs *regs) {"; |
6284 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
935447c8 | 6285 | |
e6fe60e7 AM |
6286 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
6287 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
6288 | // Check that the index is plausible | |
6289 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
6290 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
6291 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
6292 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
6293 | s.op->line() << "];"; | |
935447c8 | 6294 | |
e6fe60e7 AM |
6295 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
6296 | s.op->newline() << "c->regs = regs;"; | |
6297 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
6415ddde MW |
6298 | |
6299 | // Make it look like the IP is set as it wouldn't have been replaced | |
6300 | // by a breakpoint instruction when calling real probe handler. Reset | |
6301 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
6302 | s.op->newline() << "{"; | |
6303 | s.op->indent(1); | |
6304 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);"; | |
259d54c0 | 6305 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
e6fe60e7 | 6306 | s.op->newline() << "(*sdp->ph) (c);"; |
259d54c0 | 6307 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
6308 | s.op->newline(-1) << "}"; |
6309 | ||
e6fe60e7 AM |
6310 | common_probe_entryfn_epilogue (s.op); |
6311 | s.op->newline() << "return 0;"; | |
6312 | s.op->newline(-1) << "}"; | |
bd659351 | 6313 | |
03a4ec63 | 6314 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
bd659351 MW |
6315 | s.op->newline() << "static int kprobe_resolve(void *data, const char *name,"; |
6316 | s.op->newline() << " struct module *owner,"; | |
6317 | s.op->newline() << " unsigned long val) {"; | |
6318 | s.op->newline(1) << "int i;"; | |
fc1d2aa2 MW |
6319 | s.op->newline() << "int *p = (int *) data;"; |
6320 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() | |
6321 | << " && *p > 0; i++) {"; | |
bd659351 MW |
6322 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
6323 | s.op->newline() << "if (! sdp->address)"; | |
fc1d2aa2 | 6324 | s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {"; |
bd659351 | 6325 | s.op->newline(1) << "sdp->address = val;"; |
fc1d2aa2 MW |
6326 | s.op->newline() << "(*p)--;"; |
6327 | s.op->newline(-1) << "}"; | |
6328 | s.op->newline(-2) << "}"; | |
6329 | s.op->newline() << "return (p > 0) ? 0 : -1;"; | |
bd659351 | 6330 | s.op->newline(-1) << "}"; |
03a4ec63 | 6331 | s.op->newline() << "#endif"; |
935447c8 DS |
6332 | } |
6333 | ||
e6fe60e7 | 6334 | |
6270adc1 | 6335 | void |
e6fe60e7 | 6336 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) |
6270adc1 | 6337 | { |
03a4ec63 | 6338 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
fc1d2aa2 MW |
6339 | s.op->newline() << "{"; |
6340 | s.op->newline(1) << "int p = 0;"; | |
6341 | s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {"; | |
6342 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6343 | s.op->newline() << "if (! sdp->address)"; | |
6344 | s.op->newline(1) << "p++;"; | |
6345 | s.op->newline(-2) << "}"; | |
6346 | s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);"; | |
6347 | s.op->newline(-1) << "}"; | |
03a4ec63 | 6348 | s.op->newline() << "#endif"; |
bd659351 | 6349 | |
e6fe60e7 | 6350 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 6351 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 6352 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e | 6353 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
03a4ec63 MW |
6354 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; |
6355 | ||
6356 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; | |
bd659351 MW |
6357 | s.op->newline() << "if (! addr) {"; |
6358 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
9319b767 MW |
6359 | s.op->newline() << "if (!sdp->optional_p)"; |
6360 | s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);"; | |
6361 | s.op->newline(-1) << "continue;"; | |
bd659351 | 6362 | s.op->newline(-1) << "}"; |
03a4ec63 MW |
6363 | s.op->newline() << "#endif"; |
6364 | ||
e6fe60e7 AM |
6365 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
6366 | s.op->newline() << "if (sdp->return_p) {"; | |
c8d9d15e | 6367 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
03a4ec63 | 6368 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
e6fe60e7 AM |
6369 | s.op->newline() << "if (sdp->maxactive_p) {"; |
6370 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
6371 | s.op->newline(-1) << "} else {"; | |
f07c3b68 | 6372 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
e6fe60e7 | 6373 | s.op->newline(-1) << "}"; |
88747011 | 6374 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
6375 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
6376 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 6377 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
c8d9d15e JS |
6378 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
6379 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
e6fe60e7 AM |
6380 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
6381 | s.op->newline() << "if (rc == 0) {"; | |
6382 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
6383 | s.op->newline() << "if (rc != 0)"; | |
6384 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
6385 | s.op->newline(-2) << "}"; | |
6386 | s.op->newline() << "#else"; | |
6387 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
6388 | s.op->newline() << "#endif"; | |
6389 | s.op->newline(-1) << "} else {"; | |
6390 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 6391 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
03a4ec63 | 6392 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
88747011 | 6393 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 6394 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 6395 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e JS |
6396 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
6397 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; | |
e6fe60e7 AM |
6398 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
6399 | s.op->newline() << "if (rc == 0) {"; | |
6400 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
6401 | s.op->newline() << "if (rc != 0)"; | |
6402 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
6403 | s.op->newline(-2) << "}"; | |
6404 | s.op->newline() << "#else"; | |
6405 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
6406 | s.op->newline() << "#endif"; | |
6407 | s.op->newline(-1) << "}"; | |
6408 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
6409 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 6410 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 6411 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);"; |
b350f56b | 6412 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
e6fe60e7 AM |
6413 | // XXX: shall we increment numskipped? |
6414 | s.op->newline(-1) << "}"; | |
6270adc1 | 6415 | |
e6fe60e7 AM |
6416 | s.op->newline() << "else sdp->registered_p = 1;"; |
6417 | s.op->newline(-1) << "}"; // for loop | |
6270adc1 MH |
6418 | } |
6419 | ||
e6fe60e7 AM |
6420 | void |
6421 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
935447c8 | 6422 | { |
e6fe60e7 AM |
6423 | //Unregister kprobes by batch interfaces. |
6424 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
6425 | s.op->newline() << "j = 0;"; | |
6426 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
6427 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6428 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6429 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
6430 | s.op->newline() << "if (!sdp->return_p)"; | |
c9116e99 | 6431 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;"; |
e6fe60e7 | 6432 | s.op->newline(-2) << "}"; |
c9116e99 | 6433 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
6434 | s.op->newline() << "j = 0;"; |
6435 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
6436 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6437 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6438 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
6439 | s.op->newline() << "if (sdp->return_p)"; | |
c9116e99 | 6440 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;"; |
e6fe60e7 | 6441 | s.op->newline(-2) << "}"; |
c9116e99 | 6442 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
6443 | s.op->newline() << "#ifdef __ia64__"; |
6444 | s.op->newline() << "j = 0;"; | |
6445 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
6446 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6447 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6448 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
c9116e99 | 6449 | s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;"; |
e6fe60e7 | 6450 | s.op->newline(-1) << "}"; |
c9116e99 | 6451 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
6452 | s.op->newline() << "#endif"; |
6453 | s.op->newline() << "#endif"; | |
3e3bd7b6 | 6454 | |
e6fe60e7 AM |
6455 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
6456 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6457 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6458 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
6459 | s.op->newline() << "if (sdp->return_p) {"; | |
6460 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
6461 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
6462 | s.op->newline() << "#endif"; | |
6463 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; | |
6464 | s.op->newline() << "#ifdef STP_TIMING"; | |
6465 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
6466 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; | |
6467 | s.op->newline(-1) << "#endif"; | |
6468 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; | |
6469 | s.op->newline() << "#ifdef STP_TIMING"; | |
6470 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
ceca1799 | 6471 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; |
e6fe60e7 AM |
6472 | s.op->newline(-1) << "#endif"; |
6473 | s.op->newline(-1) << "} else {"; | |
6474 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
6475 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
6476 | s.op->newline() << "#endif"; | |
6477 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; | |
6478 | s.op->newline() << "#ifdef STP_TIMING"; | |
6479 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
ceca1799 | 6480 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->pp, kp->u.kp.nmissed);"; |
e6fe60e7 AM |
6481 | s.op->newline(-1) << "#endif"; |
6482 | s.op->newline(-1) << "}"; | |
6483 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
6484 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
6485 | s.op->newline() << "#endif"; | |
6486 | s.op->newline() << "sdp->registered_p = 0;"; | |
6487 | s.op->newline(-1) << "}"; | |
f8a968bc JS |
6488 | } |
6489 | ||
e6fe60e7 | 6490 | struct kprobe_builder: public derived_probe_builder |
3c1b3d06 | 6491 | { |
e6fe60e7 AM |
6492 | kprobe_builder() {} |
6493 | virtual void build(systemtap_session & sess, | |
6494 | probe * base, | |
6495 | probe_point * location, | |
6496 | literal_map_t const & parameters, | |
6497 | vector<derived_probe *> & finished_results); | |
6498 | }; | |
3c1b3d06 FCE |
6499 | |
6500 | ||
79189b84 | 6501 | void |
e6fe60e7 AM |
6502 | kprobe_builder::build(systemtap_session & sess, |
6503 | probe * base, | |
6504 | probe_point * location, | |
6505 | literal_map_t const & parameters, | |
6506 | vector<derived_probe *> & finished_results) | |
79189b84 | 6507 | { |
e6fe60e7 | 6508 | string function_string_val, module_string_val; |
b642c901 | 6509 | string path, library; |
b6371390 JS |
6510 | int64_t statement_num_val = 0, maxactive_val = 0; |
6511 | bool has_function_str, has_module_str, has_statement_num; | |
6512 | bool has_absolute, has_return, has_maxactive; | |
b642c901 | 6513 | bool has_path, has_library; |
79189b84 | 6514 | |
b6371390 JS |
6515 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
6516 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
6517 | has_return = has_null_param (parameters, TOK_RETURN); | |
6518 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
6519 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
6520 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
b642c901 SC |
6521 | has_path = get_param (parameters, TOK_PROCESS, path); |
6522 | has_library = get_param (parameters, TOK_LIBRARY, library); | |
6523 | ||
6524 | if (has_path) | |
6525 | path = find_executable (path); | |
6526 | if (has_library) | |
6527 | library = find_executable (library, "LD_LIBRARY_PATH"); | |
6528 | ||
b6371390 | 6529 | if (has_function_str) |
6fb70fb7 | 6530 | { |
b6371390 JS |
6531 | if (has_module_str) |
6532 | function_string_val = module_string_val + ":" + function_string_val; | |
86758d5f | 6533 | |
b6371390 JS |
6534 | finished_results.push_back (new kprobe_derived_probe (base, |
6535 | location, function_string_val, | |
6536 | 0, has_return, | |
6537 | has_statement_num, | |
6538 | has_maxactive, | |
b642c901 SC |
6539 | has_path, |
6540 | has_library, | |
6541 | maxactive_val, | |
6542 | path, | |
6543 | library)); | |
6fb70fb7 | 6544 | } |
e6fe60e7 | 6545 | else |
b6371390 JS |
6546 | { |
6547 | // assert guru mode for absolute probes | |
6548 | if ( has_statement_num && has_absolute && !base->privileged ) | |
6549 | throw semantic_error ("absolute statement probe in unprivileged script", base->tok); | |
6550 | ||
6551 | finished_results.push_back (new kprobe_derived_probe (base, | |
6552 | location, "", | |
6553 | statement_num_val, | |
6554 | has_return, | |
6555 | has_statement_num, | |
6556 | has_maxactive, | |
b642c901 SC |
6557 | has_path, |
6558 | has_library, | |
6559 | maxactive_val, | |
6560 | path, | |
6561 | library)); | |
96b030fe | 6562 | } |
79189b84 JS |
6563 | } |
6564 | ||
dd225250 PS |
6565 | // ------------------------------------------------------------------------ |
6566 | // Hardware breakpoint based probes. | |
6567 | // ------------------------------------------------------------------------ | |
6568 | ||
6569 | static const string TOK_HWBKPT("data"); | |
6570 | static const string TOK_HWBKPT_WRITE("write"); | |
6571 | static const string TOK_HWBKPT_RW("rw"); | |
6572 | static const string TOK_LENGTH("length"); | |
6573 | ||
6574 | #define HWBKPT_READ 0 | |
6575 | #define HWBKPT_WRITE 1 | |
6576 | #define HWBKPT_RW 2 | |
6577 | struct hwbkpt_derived_probe: public derived_probe | |
6578 | { | |
6579 | hwbkpt_derived_probe (probe *base, | |
6580 | probe_point *location, | |
6581 | uint64_t addr, | |
6582 | string symname, | |
6583 | unsigned int len, | |
6584 | bool has_only_read_access, | |
6585 | bool has_only_write_access, | |
6586 | bool has_rw_access | |
6587 | ); | |
6588 | Dwarf_Addr hwbkpt_addr; | |
6589 | string symbol_name; | |
6590 | unsigned int hwbkpt_access,hwbkpt_len; | |
6591 | ||
6592 | void printsig (std::ostream &o) const; | |
6593 | void join_group (systemtap_session& s); | |
6594 | }; | |
6595 | ||
6596 | struct hwbkpt_derived_probe_group: public derived_probe_group | |
6597 | { | |
dd225250 | 6598 | private: |
dac77b80 | 6599 | vector<hwbkpt_derived_probe*> hwbkpt_probes; |
dd225250 PS |
6600 | |
6601 | public: | |
6602 | void enroll (hwbkpt_derived_probe* probe, systemtap_session& s); | |
6603 | void emit_module_decls (systemtap_session& s); | |
6604 | void emit_module_init (systemtap_session& s); | |
6605 | void emit_module_exit (systemtap_session& s); | |
6606 | }; | |
6607 | ||
6608 | hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base, | |
9ea68eb9 JS |
6609 | probe_point *location, |
6610 | uint64_t addr, | |
6611 | string symname, | |
6612 | unsigned int len, | |
6613 | bool has_only_read_access, | |
6614 | bool has_only_write_access, | |
6615 | bool has_rw_access | |
6616 | ): | |
dd225250 PS |
6617 | derived_probe (base, location), |
6618 | hwbkpt_addr (addr), | |
6619 | symbol_name (symname), | |
6620 | hwbkpt_len (len) | |
6621 | { | |
6622 | this->tok = base->tok; | |
6623 | ||
6624 | vector<probe_point::component*> comps; | |
6625 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
6626 | ||
6627 | if (hwbkpt_addr) | |
9ea68eb9 JS |
6628 | comps.push_back (new probe_point::component (TOK_HWBKPT, |
6629 | new literal_number(hwbkpt_addr, true))); | |
6630 | else if (symbol_name.size()) | |
6631 | comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name))); | |
dd225250 PS |
6632 | |
6633 | comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len))); | |
6634 | ||
6635 | if (has_only_read_access) | |
9ea68eb9 | 6636 | this->hwbkpt_access = HWBKPT_READ ; |
dd225250 PS |
6637 | //TODO add code for comps.push_back for read, since this flag is not for x86 |
6638 | ||
6639 | else | |
9ea68eb9 JS |
6640 | { |
6641 | if (has_only_write_access) | |
6642 | { | |
6643 | this->hwbkpt_access = HWBKPT_WRITE ; | |
6644 | comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE)); | |
6645 | } | |
6646 | else | |
6647 | { | |
6648 | this->hwbkpt_access = HWBKPT_RW ; | |
6649 | comps.push_back (new probe_point::component(TOK_HWBKPT_RW)); | |
6650 | } | |
6651 | } | |
dd225250 PS |
6652 | |
6653 | this->sole_location()->components = comps; | |
6654 | } | |
6655 | ||
6656 | void hwbkpt_derived_probe::printsig (ostream& o) const | |
6657 | { | |
6658 | sole_location()->print (o); | |
6659 | printsig_nested (o); | |
6660 | } | |
6661 | ||
6662 | void hwbkpt_derived_probe::join_group (systemtap_session& s) | |
6663 | { | |
dac77b80 FCE |
6664 | if (! s.hwbkpt_derived_probes) |
6665 | s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group (); | |
dd225250 PS |
6666 | s.hwbkpt_derived_probes->enroll (this, s); |
6667 | } | |
6668 | ||
6669 | void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s) | |
6670 | { | |
dac77b80 FCE |
6671 | hwbkpt_probes.push_back (p); |
6672 | ||
6673 | unsigned max_hwbkpt_probes_by_arch = 0; | |
6674 | if (s.architecture == "i386" || s.architecture == "x86_64") | |
6675 | max_hwbkpt_probes_by_arch = 4; | |
6676 | else if (s.architecture == "s390") | |
6677 | max_hwbkpt_probes_by_arch = 1; | |
6678 | ||
6679 | if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch) | |
6680 | if (! s.suppress_warnings) | |
6681 | s.print_warning ("Too many hardware breakpoint probes requested for " + s.architecture | |
6682 | + "(" + lex_cast(hwbkpt_probes.size()) + | |
6683 | " vs. " + lex_cast(max_hwbkpt_probes_by_arch) + ")"); | |
dd225250 PS |
6684 | } |
6685 | ||
6686 | void | |
6687 | hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6688 | { | |
dac77b80 | 6689 | if (hwbkpt_probes.empty()) return; |
dd225250 PS |
6690 | |
6691 | s.op->newline() << "/* ---- hwbkpt-based probes ---- */"; | |
6692 | ||
6693 | s.op->newline() << "#include <linux/perf_event.h>"; | |
6694 | s.op->newline() << "#include <linux/hw_breakpoint.h>"; | |
6695 | s.op->newline(); | |
6696 | ||
6697 | // Forward declare the master entry functions | |
6698 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
6699 | s.op->line() << " int nmi,"; | |
6700 | s.op->line() << " struct perf_sample_data *data,"; | |
6701 | s.op->line() << " struct pt_regs *regs);"; | |
79189b84 | 6702 | |
dd225250 PS |
6703 | // Emit the actual probe list. |
6704 | ||
6705 | s.op->newline() << "static struct perf_event_attr "; | |
dac77b80 | 6706 | s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
6707 | |
6708 | s.op->newline() << "static struct perf_event **"; | |
dac77b80 | 6709 | s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
6710 | s.op->newline() << "static struct stap_hwbkpt_probe {"; |
6711 | s.op->newline() << "int registered_p:1;"; | |
6712 | // registered_p = 0 signifies a probe that failed registration | |
6713 | // registered_p = 1 signifies a probe that got registered successfully | |
6714 | ||
6715 | // Probe point & Symbol Names are mostly small and uniform enough | |
6716 | // to justify putting const char*. | |
dac77b80 FCE |
6717 | s.op->newline() << "const char * const pp;"; |
6718 | s.op->newline() << "const char * const symbol;"; | |
dd225250 PS |
6719 | |
6720 | s.op->newline() << "const unsigned long address;"; | |
6721 | s.op->newline() << "uint8_t atype;"; | |
bb0a4e12 | 6722 | s.op->newline() << "unsigned int len;"; |
dd225250 PS |
6723 | s.op->newline() << "void (* const ph) (struct context*);"; |
6724 | s.op->newline() << "} stap_hwbkpt_probes[] = {"; | |
6725 | s.op->indent(1); | |
6726 | ||
dac77b80 | 6727 | for (unsigned int it = 0; it < hwbkpt_probes.size(); it++) |
dd225250 | 6728 | { |
dac77b80 | 6729 | hwbkpt_derived_probe* p = hwbkpt_probes.at(it); |
dd225250 PS |
6730 | s.op->newline() << "{"; |
6731 | s.op->line() << " .registered_p=1,"; | |
6732 | if (p->symbol_name.size()) | |
6733 | s.op->line() << " .address=(unsigned long)0x0" << "ULL,"; | |
6734 | else | |
6735 | s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,"; | |
6736 | switch(p->hwbkpt_access){ | |
6737 | case HWBKPT_READ: | |
6738 | s.op->line() << " .atype=HW_BREAKPOINT_R ,"; | |
bb0a4e12 | 6739 | break; |
dd225250 PS |
6740 | case HWBKPT_WRITE: |
6741 | s.op->line() << " .atype=HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 6742 | break; |
dd225250 PS |
6743 | case HWBKPT_RW: |
6744 | s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 6745 | break; |
dd225250 PS |
6746 | }; |
6747 | s.op->line() << " .len=" << p->hwbkpt_len << ","; | |
6748 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
6749 | s.op->line() << " .symbol=\"" << p->symbol_name << "\","; | |
6750 | s.op->line() << " .ph=&" << p->name << ""; | |
6751 | s.op->line() << " },"; | |
6752 | } | |
dac77b80 | 6753 | s.op->newline(-1) << "};"; |
dd225250 PS |
6754 | |
6755 | // Emit the hwbkpt callback function | |
6756 | s.op->newline() ; | |
6757 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
6758 | s.op->line() << " int nmi,"; | |
6759 | s.op->line() << " struct perf_sample_data *data,"; | |
6760 | s.op->line() << " struct pt_regs *regs) {"; | |
dac77b80 FCE |
6761 | s.op->newline(1) << "unsigned int i;"; |
6762 | s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;"; | |
6763 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; | |
6764 | s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
6765 | // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead? | |
6766 | s.op->newline() << "if (bp->attr.bp_addr==hp->bp_addr && bp->attr.bp_type==hp->bp_type && bp->attr.bp_len==hp->bp_len) {"; | |
6767 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];"; | |
dd225250 PS |
6768 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
6769 | s.op->newline() << "c->regs = regs;"; | |
6770 | s.op->newline() << "(*sdp->ph) (c);"; | |
6771 | common_probe_entryfn_epilogue (s.op); | |
dac77b80 | 6772 | s.op->newline(-1) << "}"; |
dd225250 PS |
6773 | s.op->newline(-1) << "}"; |
6774 | s.op->newline() << "return 0;"; | |
dac77b80 | 6775 | s.op->newline(-1) << "}"; |
dd225250 PS |
6776 | } |
6777 | ||
6778 | void | |
6779 | hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s) | |
6780 | { | |
dac77b80 | 6781 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 PS |
6782 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
6783 | s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
6784 | s.op->newline() << "void *addr = (void *) sdp->address;"; | |
6785 | s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;"; | |
dac77b80 FCE |
6786 | s.op->newline() << "hw_breakpoint_init(hp);"; |
6787 | s.op->newline() << "if (addr)"; | |
6788 | s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;"; | |
6789 | s.op->newline(-1) << "else { "; | |
6790 | s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);"; | |
6791 | s.op->newline() << "if (!hp->bp_addr) { "; | |
6792 | s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->pp,hwbkpt_symbol_name);"; | |
6793 | s.op->newline() << "continue;"; | |
6794 | s.op->newline(-1) << "}"; | |
6795 | s.op->newline(-1) << "}"; | |
6796 | s.op->newline() << "hp->bp_type = sdp->atype;"; | |
6797 | ||
6798 | // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!). | |
6799 | if (s.architecture == "i386" || s.architecture == "x86_64" ) | |
6800 | { | |
6801 | s.op->newline() << "switch(sdp->len) {"; | |
6802 | s.op->newline() << "case 1:"; | |
6803 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;"; | |
6804 | s.op->newline() << "break;"; | |
6805 | s.op->newline(-1) << "case 2:"; | |
6806 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;"; | |
6807 | s.op->newline() << "break;"; | |
6808 | s.op->newline(-1) << "case 3:"; | |
6809 | s.op->newline() << "case 4:"; | |
6810 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;"; | |
6811 | s.op->newline() << "break;"; | |
6812 | s.op->newline(-1) << "case 5:"; | |
6813 | s.op->newline() << "case 6:"; | |
6814 | s.op->newline() << "case 7:"; | |
6815 | s.op->newline() << "case 8:"; | |
6816 | s.op->newline() << "default:"; // XXX: could instead reject | |
6817 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;"; | |
6818 | s.op->newline() << "break;"; | |
6819 | s.op->newline(-1) << "}"; | |
6820 | } | |
6821 | else // other architectures presumed straightforward | |
6822 | s.op->newline() << "hp->bp_len = sdp->len;"; | |
6823 | ||
dd225250 | 6824 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
dac77b80 FCE |
6825 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);"; |
6826 | s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {"; | |
6827 | s.op->newline(1) << "int err_code = PTR_ERR(stap_hwbkpt_ret_array[i]);"; | |
6828 | s.op->newline(0) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, err_code, addr, hwbkpt_symbol_name);"; | |
6829 | s.op->newline(-1) << "}"; | |
dd225250 | 6830 | s.op->newline() << " else sdp->registered_p = 1;"; |
dd225250 PS |
6831 | s.op->newline(-1) << "}"; // for loop |
6832 | } | |
6833 | ||
6834 | void | |
6835 | hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s) | |
6836 | { | |
6837 | //Unregister hwbkpt probes. | |
dac77b80 | 6838 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 | 6839 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
dac77b80 FCE |
6840 | s.op->newline() << "if (sdp->registered_p == 0) continue;"; |
6841 | s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);"; | |
dd225250 PS |
6842 | s.op->newline() << "sdp->registered_p = 0;"; |
6843 | s.op->newline(-1) << "}"; | |
6844 | } | |
6845 | ||
6846 | struct hwbkpt_builder: public derived_probe_builder | |
6847 | { | |
6848 | hwbkpt_builder() {} | |
6849 | virtual void build(systemtap_session & sess, | |
6850 | probe * base, | |
6851 | probe_point * location, | |
6852 | literal_map_t const & parameters, | |
6853 | vector<derived_probe *> & finished_results); | |
6854 | }; | |
6855 | ||
6856 | void | |
6857 | hwbkpt_builder::build(systemtap_session & sess, | |
6858 | probe * base, | |
6859 | probe_point * location, | |
6860 | literal_map_t const & parameters, | |
6861 | vector<derived_probe *> & finished_results) | |
6862 | { | |
6863 | string symbol_str_val; | |
6864 | int64_t hwbkpt_address, len; | |
6865 | bool has_addr, has_symbol_str, has_write, has_rw, has_len; | |
6866 | ||
b47f3a55 FCE |
6867 | if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y"))) |
6868 | throw semantic_error ("CONFIG_PERF_EVENTS not available on this kernel", | |
6869 | location->components[0]->tok); | |
6870 | if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y"))) | |
6871 | throw semantic_error ("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel", | |
6872 | location->components[0]->tok); | |
6873 | ||
dd225250 PS |
6874 | has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address); |
6875 | has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val); | |
6876 | has_len = get_param (parameters, TOK_LENGTH, len); | |
6877 | has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end()); | |
6878 | has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end()); | |
6879 | ||
6880 | if (!has_len) | |
6881 | len = 1; | |
6882 | ||
6883 | if (has_addr) | |
6884 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
6885 | location, | |
6886 | hwbkpt_address, | |
6887 | "",len,0, | |
6888 | has_write, | |
6889 | has_rw)); | |
6890 | else // has symbol_str | |
6891 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
6892 | location, | |
6893 | 0, | |
6894 | symbol_str_val,len,0, | |
6895 | has_write, | |
6896 | has_rw)); | |
6897 | } | |
342d3f96 | 6898 | |
0a6f5a3f JS |
6899 | // ------------------------------------------------------------------------ |
6900 | // statically inserted kernel-tracepoint derived probes | |
6901 | // ------------------------------------------------------------------------ | |
6902 | ||
6fb70fb7 | 6903 | struct tracepoint_arg |
79189b84 | 6904 | { |
ad370dcc | 6905 | string name, c_type, typecast; |
dcaa1a65 | 6906 | bool usable, used, isptr; |
f8a968bc | 6907 | Dwarf_Die type_die; |
dcaa1a65 | 6908 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 6909 | }; |
79189b84 | 6910 | |
0a6f5a3f JS |
6911 | struct tracepoint_derived_probe: public derived_probe |
6912 | { | |
79189b84 JS |
6913 | tracepoint_derived_probe (systemtap_session& s, |
6914 | dwflpp& dw, Dwarf_Die& func_die, | |
6915 | const string& tracepoint_name, | |
6916 | probe* base_probe, probe_point* location); | |
bc9a523d | 6917 | |
79189b84 | 6918 | systemtap_session& sess; |
6fb70fb7 JS |
6919 | string tracepoint_name, header; |
6920 | vector <struct tracepoint_arg> args; | |
bc9a523d | 6921 | |
6fb70fb7 | 6922 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
d0bfd2ac | 6923 | void getargs (std::list<std::string> &arg_set) const; |
79189b84 | 6924 | void join_group (systemtap_session& s); |
3e3bd7b6 | 6925 | void print_dupe_stamp(ostream& o); |
0a6f5a3f | 6926 | }; |
79189b84 JS |
6927 | |
6928 | ||
0a6f5a3f | 6929 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> |
79189b84 | 6930 | { |
79189b84 JS |
6931 | void emit_module_decls (systemtap_session& s); |
6932 | void emit_module_init (systemtap_session& s); | |
6933 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f | 6934 | }; |
79189b84 | 6935 | |
bc9a523d | 6936 | |
f8a968bc JS |
6937 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
6938 | { | |
6939 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
6940 | vector <struct tracepoint_arg>& args): | |
6941 | dw (dw), probe_name (probe_name), args (args) {} | |
6942 | dwflpp& dw; | |
6943 | const string& probe_name; | |
6944 | vector <struct tracepoint_arg>& args; | |
bc9a523d | 6945 | |
f8a968bc JS |
6946 | void visit_target_symbol (target_symbol* e); |
6947 | void visit_target_symbol_arg (target_symbol* e); | |
6948 | void visit_target_symbol_context (target_symbol* e); | |
6949 | }; | |
79189b84 JS |
6950 | |
6951 | ||
f8a968bc JS |
6952 | void |
6953 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
75ead1f7 | 6954 | { |
f8a968bc | 6955 | string argname = e->base_name.substr(1); |
75ead1f7 | 6956 | |
f8a968bc JS |
6957 | // search for a tracepoint parameter matching this name |
6958 | tracepoint_arg *arg = NULL; | |
6959 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 6960 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
6961 | { |
6962 | arg = &args[i]; | |
6963 | arg->used = true; | |
6964 | break; | |
6965 | } | |
75ead1f7 | 6966 | |
f8a968bc JS |
6967 | if (arg == NULL) |
6968 | { | |
6969 | stringstream alternatives; | |
6970 | for (unsigned i = 0; i < args.size(); ++i) | |
6971 | alternatives << " $" << args[i].name; | |
046e7190 | 6972 | alternatives << " $$name $$parms $$vars"; |
75ead1f7 | 6973 | |
f8a968bc JS |
6974 | // We hope that this value ends up not being referenced after all, so it |
6975 | // can be optimized out quietly. | |
c69a87e0 | 6976 | throw semantic_error("unable to find tracepoint variable '" + e->base_name |
f8a968bc JS |
6977 | + "' (alternatives:" + alternatives.str () + ")", e->tok); |
6978 | // NB: we can have multiple errors, since a target variable | |
6979 | // may be expanded in several different contexts: | |
6980 | // trace ("*") { $foo->bar } | |
f8a968bc | 6981 | } |
75ead1f7 | 6982 | |
f8a968bc | 6983 | // make sure we're not dereferencing base types |
dc5a09fc JS |
6984 | if (!arg->isptr) |
6985 | e->assert_no_components("tracepoint"); | |
75ead1f7 | 6986 | |
f8a968bc JS |
6987 | // we can only write to dereferenced fields, and only if guru mode is on |
6988 | bool lvalue = is_active_lvalue(e); | |
6989 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
6990 | throw semantic_error("write to tracepoint variable '" + e->base_name | |
6991 | + "' not permitted", e->tok); | |
c69a87e0 | 6992 | |
ad370dcc JS |
6993 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
6994 | // is also meaningless until you dereference past a pointer member. It's | |
6995 | // harder to detect and prevent that though... | |
75ead1f7 | 6996 | |
f8a968bc JS |
6997 | if (e->components.empty()) |
6998 | { | |
03c75a4a JS |
6999 | if (e->addressof) |
7000 | throw semantic_error("cannot take address of tracepoint variable", e->tok); | |
a45664f4 | 7001 | |
3e3bd7b6 | 7002 | // Just grab the value from the probe locals |
a45664f4 JS |
7003 | symbol* sym = new symbol; |
7004 | sym->tok = e->tok; | |
7005 | sym->name = "__tracepoint_arg_" + arg->name; | |
7006 | provide (sym); | |
f8a968bc JS |
7007 | } |
7008 | else | |
7009 | { | |
5f36109e JS |
7010 | // make a copy of the original as a bare target symbol for the tracepoint |
7011 | // value, which will be passed into the dwarf dereferencing code | |
7012 | target_symbol* e2 = deep_copy_visitor::deep_copy(e); | |
7013 | e2->components.clear(); | |
7014 | ||
7015 | if (e->components.back().type == target_symbol::comp_pretty_print) | |
7016 | { | |
7017 | if (lvalue) | |
7018 | throw semantic_error("cannot write to pretty-printed variable", e->tok); | |
7019 | ||
7020 | dwarf_pretty_print dpp(dw, &arg->type_die, e2, false, *e); | |
7021 | dpp.expand()->visit (this); | |
7022 | return; | |
7023 | } | |
7024 | ||
f8a968bc JS |
7025 | // Synthesize a function to dereference the dwarf fields, |
7026 | // with a pointer parameter that is the base tracepoint variable | |
7027 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 7028 | fdecl->synthetic = true; |
f8a968bc JS |
7029 | fdecl->tok = e->tok; |
7030 | embeddedcode *ec = new embeddedcode; | |
7031 | ec->tok = e->tok; | |
75ead1f7 | 7032 | |
f8a968bc JS |
7033 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") |
7034 | + "_" + e->base_name.substr(1) | |
aca66a36 | 7035 | + "_" + lex_cast(tick++)); |
75ead1f7 | 7036 | |
f8a968bc JS |
7037 | fdecl->name = fname; |
7038 | fdecl->body = ec; | |
75ead1f7 | 7039 | |
b5a0dd41 FCE |
7040 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
7041 | ec->code += "\n#define fetch_register k_fetch_register\n"; | |
7042 | ec->code += "#define store_register k_store_register\n"; | |
c69a87e0 FCE |
7043 | |
7044 | ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e, | |
f8a968bc | 7045 | lvalue, fdecl->type); |
75ead1f7 | 7046 | |
f8a968bc JS |
7047 | // Give the fdecl an argument for the raw tracepoint value |
7048 | vardecl *v1 = new vardecl; | |
7049 | v1->type = pe_long; | |
7050 | v1->name = "pointer"; | |
7051 | v1->tok = e->tok; | |
7052 | fdecl->formal_args.push_back(v1); | |
75ead1f7 | 7053 | |
6fda2dff JS |
7054 | // Any non-literal indexes need to be passed in too. |
7055 | for (unsigned i = 0; i < e->components.size(); ++i) | |
7056 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
7057 | { | |
7058 | vardecl *v = new vardecl; | |
7059 | v->type = pe_long; | |
aca66a36 | 7060 | v->name = "index" + lex_cast(i); |
6fda2dff JS |
7061 | v->tok = e->tok; |
7062 | fdecl->formal_args.push_back(v); | |
7063 | } | |
7064 | ||
f8a968bc JS |
7065 | if (lvalue) |
7066 | { | |
7067 | // Modify the fdecl so it carries a pe_long formal | |
7068 | // argument called "value". | |
75ead1f7 | 7069 | |
f8a968bc JS |
7070 | // FIXME: For the time being we only support setting target |
7071 | // variables which have base types; these are 'pe_long' in | |
7072 | // stap's type vocabulary. Strings and pointers might be | |
7073 | // reasonable, some day, but not today. | |
7074 | ||
7075 | vardecl *v2 = new vardecl; | |
7076 | v2->type = pe_long; | |
7077 | v2->name = "value"; | |
7078 | v2->tok = e->tok; | |
7079 | fdecl->formal_args.push_back(v2); | |
7080 | } | |
7081 | else | |
7082 | ec->code += "/* pure */"; | |
7083 | ||
64211010 DB |
7084 | ec->code += "/* unprivileged */"; |
7085 | ||
b5a0dd41 FCE |
7086 | // PR10601 |
7087 | ec->code += "\n#undef fetch_register\n"; | |
7088 | ec->code += "\n#undef store_register\n"; | |
aff5d390 | 7089 | |
f8a968bc | 7090 | dw.sess.functions[fdecl->name] = fdecl; |
75ead1f7 | 7091 | |
f8a968bc JS |
7092 | // Synthesize a functioncall. |
7093 | functioncall* n = new functioncall; | |
7094 | n->tok = e->tok; | |
7095 | n->function = fname; | |
7096 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
6fda2dff JS |
7097 | n->args.push_back(require(e2)); |
7098 | ||
7099 | // Any non-literal indexes need to be passed in too. | |
7100 | for (unsigned i = 0; i < e->components.size(); ++i) | |
7101 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
7102 | n->args.push_back(require(e->components[i].expr_index)); | |
75ead1f7 | 7103 | |
f8a968bc JS |
7104 | if (lvalue) |
7105 | { | |
7106 | // Provide the functioncall to our parent, so that it can be | |
7107 | // used to substitute for the assignment node immediately above | |
7108 | // us. | |
7109 | assert(!target_symbol_setter_functioncalls.empty()); | |
7110 | *(target_symbol_setter_functioncalls.top()) = n; | |
7111 | } | |
75ead1f7 | 7112 | |
f8a968bc JS |
7113 | provide (n); |
7114 | } | |
75ead1f7 JS |
7115 | } |
7116 | ||
7117 | ||
f8a968bc JS |
7118 | void |
7119 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
0a6f5a3f | 7120 | { |
03c75a4a JS |
7121 | if (e->addressof) |
7122 | throw semantic_error("cannot take address of context variable", e->tok); | |
7123 | ||
f8a968bc JS |
7124 | if (is_active_lvalue (e)) |
7125 | throw semantic_error("write to tracepoint '" + e->base_name + "' not permitted", e->tok); | |
0a6f5a3f | 7126 | |
f8a968bc JS |
7127 | if (e->base_name == "$$name") |
7128 | { | |
5f36109e JS |
7129 | e->assert_no_components("tracepoint"); |
7130 | ||
f8a968bc JS |
7131 | // Synthesize a functioncall. |
7132 | functioncall* n = new functioncall; | |
7133 | n->tok = e->tok; | |
7134 | n->function = "_mark_name_get"; | |
7135 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
7136 | provide (n); | |
7137 | } | |
046e7190 | 7138 | else if (e->base_name == "$$vars" || e->base_name == "$$parms") |
f8a968bc | 7139 | { |
5f36109e JS |
7140 | e->assert_no_components("tracepoint", true); |
7141 | ||
f8a968bc JS |
7142 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate |
7143 | // NB: we synthesize a new token here rather than reusing | |
7144 | // e->tok, because print_format::print likes to use | |
7145 | // its tok->content. | |
7146 | token* pf_tok = new token(*e->tok); | |
7147 | pf_tok->content = "sprintf"; | |
0a6f5a3f | 7148 | |
d5e178c1 | 7149 | print_format* pf = print_format::create(pf_tok); |
0a6f5a3f | 7150 | |
f8a968bc | 7151 | for (unsigned i = 0; i < args.size(); ++i) |
b278033a | 7152 | { |
dcaa1a65 JS |
7153 | if (!args[i].usable) |
7154 | continue; | |
f8a968bc JS |
7155 | if (i > 0) |
7156 | pf->raw_components += " "; | |
7157 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 7158 | target_symbol *tsym = new target_symbol; |
f8a968bc JS |
7159 | tsym->tok = e->tok; |
7160 | tsym->base_name = "$" + args[i].name; | |
5f36109e | 7161 | tsym->components = e->components; |
b278033a | 7162 | |
f8a968bc JS |
7163 | // every variable should always be accessible! |
7164 | tsym->saved_conversion_error = 0; | |
7165 | expression *texp = require (tsym); // NB: throws nothing ... | |
7166 | assert (!tsym->saved_conversion_error); // ... but this is how we know it happened. | |
b278033a | 7167 | |
5f36109e JS |
7168 | if (!e->components.empty() && |
7169 | e->components[0].type == target_symbol::comp_pretty_print) | |
7170 | pf->raw_components += "=%s"; | |
7171 | else | |
7172 | pf->raw_components += args[i].isptr ? "=%p" : "=%#x"; | |
f8a968bc JS |
7173 | pf->args.push_back(texp); |
7174 | } | |
0a6f5a3f | 7175 | |
f8a968bc JS |
7176 | pf->components = print_format::string_to_components(pf->raw_components); |
7177 | provide (pf); | |
b278033a | 7178 | } |
f8a968bc JS |
7179 | else |
7180 | assert(0); // shouldn't get here | |
0a6f5a3f JS |
7181 | } |
7182 | ||
0a6f5a3f | 7183 | void |
f8a968bc | 7184 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a6f5a3f | 7185 | { |
aff5d390 | 7186 | try |
c69a87e0 FCE |
7187 | { |
7188 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
aff5d390 | 7189 | |
c69a87e0 FCE |
7190 | if (e->base_name == "$$name" || |
7191 | e->base_name == "$$parms" || | |
7192 | e->base_name == "$$vars") | |
7193 | visit_target_symbol_context (e); | |
7194 | else | |
7195 | visit_target_symbol_arg (e); | |
7196 | } | |
7197 | catch (const semantic_error &er) | |
7198 | { | |
1af1e62d | 7199 | e->chain (er); |
c69a87e0 FCE |
7200 | provide (e); |
7201 | } | |
0a6f5a3f JS |
7202 | } |
7203 | ||
7204 | ||
7205 | ||
79189b84 JS |
7206 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
7207 | dwflpp& dw, Dwarf_Die& func_die, | |
7208 | const string& tracepoint_name, | |
7209 | probe* base, probe_point* loc): | |
7210 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), | |
7211 | sess (s), tracepoint_name (tracepoint_name) | |
56894e91 | 7212 | { |
79189b84 JS |
7213 | // create synthetic probe point name; preserve condition |
7214 | vector<probe_point::component*> comps; | |
7215 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
7216 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
7217 | this->sole_location()->components = comps; | |
7218 | ||
6fb70fb7 JS |
7219 | // fill out the available arguments in this tracepoint |
7220 | build_args(dw, func_die); | |
56894e91 | 7221 | |
6fb70fb7 JS |
7222 | // determine which header defined this tracepoint |
7223 | string decl_file = dwarf_decl_file(&func_die); | |
7224 | size_t header_pos = decl_file.rfind("trace/"); | |
7225 | if (header_pos == string::npos) | |
7226 | throw semantic_error ("cannot parse header location for tracepoint '" | |
7227 | + tracepoint_name + "' in '" | |
7228 | + decl_file + "'"); | |
7229 | header = decl_file.substr(header_pos); | |
56894e91 | 7230 | |
6fb70fb7 JS |
7231 | // tracepoints from FOO_event_types.h should really be included from FOO.h |
7232 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
7233 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 7234 | // XXX: see also PR9993. |
6fb70fb7 JS |
7235 | header_pos = header.find("_event_types"); |
7236 | if (header_pos != string::npos) | |
7237 | header.erase(header_pos, 12); | |
56894e91 | 7238 | |
f8a968bc JS |
7239 | // Now expand the local variables in the probe body |
7240 | tracepoint_var_expanding_visitor v (dw, name, args); | |
8b095b45 | 7241 | v.replace (this->body); |
a45664f4 JS |
7242 | for (unsigned i = 0; i < args.size(); i++) |
7243 | if (args[i].used) | |
7244 | { | |
7245 | vardecl* v = new vardecl; | |
7246 | v->name = "__tracepoint_arg_" + args[i].name; | |
7247 | v->tok = this->tok; | |
58701b78 | 7248 | v->set_arity(0, this->tok); |
a45664f4 JS |
7249 | v->type = pe_long; |
7250 | v->skip_init = true; | |
7251 | this->locals.push_back (v); | |
7252 | } | |
56894e91 | 7253 | |
79189b84 JS |
7254 | if (sess.verbose > 2) |
7255 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name | |
7256 | << "'" << endl; | |
7257 | } | |
dc38c0ae | 7258 | |
56894e91 | 7259 | |
f8a968bc | 7260 | static bool |
dcaa1a65 | 7261 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
46b84a80 | 7262 | { |
dcaa1a65 | 7263 | switch (dwarf_tag(&arg.type_die)) |
b20febf3 | 7264 | { |
f8a968bc JS |
7265 | case DW_TAG_typedef: |
7266 | case DW_TAG_const_type: | |
7267 | case DW_TAG_volatile_type: | |
7268 | // iterate on the referent type | |
3d1ad340 | 7269 | return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die) |
dcaa1a65 | 7270 | && resolve_tracepoint_arg_type(arg)); |
f8a968bc JS |
7271 | case DW_TAG_base_type: |
7272 | // base types will simply be treated as script longs | |
dcaa1a65 | 7273 | arg.isptr = false; |
f8a968bc JS |
7274 | return true; |
7275 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
7276 | // pointers can be treated as script longs, |
7277 | // and if we know their type, they can also be dereferenced | |
3d1ad340 | 7278 | if (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)) |
dcaa1a65 | 7279 | arg.isptr = true; |
ad370dcc JS |
7280 | arg.typecast = "(intptr_t)"; |
7281 | return true; | |
7282 | case DW_TAG_structure_type: | |
7283 | case DW_TAG_union_type: | |
7284 | // for structs/unions which are passed by value, we turn it into | |
7285 | // a pointer that can be dereferenced. | |
7286 | arg.isptr = true; | |
7287 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 7288 | return true; |
f8a968bc JS |
7289 | default: |
7290 | // should we consider other types too? | |
7291 | return false; | |
b20febf3 | 7292 | } |
56894e91 JS |
7293 | } |
7294 | ||
7295 | ||
7296 | void | |
6fb70fb7 | 7297 | tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die) |
56894e91 | 7298 | { |
6fb70fb7 JS |
7299 | Dwarf_Die arg; |
7300 | if (dwarf_child(&func_die, &arg) == 0) | |
7301 | do | |
7302 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
7303 | { | |
7304 | // build a tracepoint_arg for this parameter | |
7305 | tracepoint_arg tparg; | |
23d106b9 | 7306 | tparg.name = dwarf_diename(&arg); |
56894e91 | 7307 | |
6fb70fb7 | 7308 | // read the type of this parameter |
3d1ad340 | 7309 | if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die) |
f1c8f8a5 | 7310 | || !dwarf_type_name(&tparg.type_die, tparg.c_type)) |
6fb70fb7 JS |
7311 | throw semantic_error ("cannot get type of tracepoint '" |
7312 | + tracepoint_name + "' parameter '" | |
7313 | + tparg.name + "'"); | |
a68f81a2 | 7314 | |
dcaa1a65 | 7315 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
7316 | args.push_back(tparg); |
7317 | if (sess.verbose > 4) | |
7318 | clog << "found parameter for tracepoint '" << tracepoint_name | |
7319 | << "': type:'" << tparg.c_type | |
7320 | << "' name:'" << tparg.name << "'" << endl; | |
7321 | } | |
7322 | while (dwarf_siblingof(&arg, &arg) == 0); | |
56894e91 JS |
7323 | } |
7324 | ||
dc38c0ae | 7325 | void |
d0bfd2ac | 7326 | tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const |
dc38c0ae | 7327 | { |
dcaa1a65 JS |
7328 | for (unsigned i = 0; i < args.size(); ++i) |
7329 | if (args[i].usable) | |
d0bfd2ac | 7330 | arg_set.push_back("$"+args[i].name+":"+args[i].c_type); |
dc38c0ae DS |
7331 | } |
7332 | ||
79189b84 JS |
7333 | void |
7334 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
197a4d62 | 7335 | { |
79189b84 JS |
7336 | if (! s.tracepoint_derived_probes) |
7337 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
7338 | s.tracepoint_derived_probes->enroll (this); | |
7339 | } | |
e38d6504 | 7340 | |
56894e91 | 7341 | |
197a4d62 | 7342 | void |
3e3bd7b6 | 7343 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) |
56894e91 | 7344 | { |
3e3bd7b6 JS |
7345 | for (unsigned i = 0; i < args.size(); i++) |
7346 | if (args[i].used) | |
7347 | o << "__tracepoint_arg_" << args[i].name << endl; | |
197a4d62 | 7348 | } |
56894e91 | 7349 | |
3e3bd7b6 | 7350 | |
9e0cd21a | 7351 | static vector<string> tracepoint_extra_headers (systemtap_session& s) |
47dd066d | 7352 | { |
3c1b3d06 FCE |
7353 | vector<string> they_live; |
7354 | // PR 9993 | |
7355 | // XXX: may need this to be configurable | |
7356 | they_live.push_back ("linux/skbuff.h"); | |
9e0cd21a FCE |
7357 | |
7358 | // PR11649: conditional extra header | |
7359 | // for kvm tracepoints in 2.6.33ish | |
7360 | if (s.kernel_config["CONFIG_KVM"] != string("")) { | |
7361 | they_live.push_back ("linux/kvm_host.h"); | |
7362 | } | |
3c1b3d06 FCE |
7363 | return they_live; |
7364 | } | |
47dd066d WC |
7365 | |
7366 | ||
7367 | void | |
79189b84 | 7368 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) |
47dd066d | 7369 | { |
79189b84 JS |
7370 | if (probes.empty()) |
7371 | return; | |
47dd066d | 7372 | |
96b030fe JS |
7373 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
7374 | s.op->newline(); | |
79189b84 | 7375 | |
3c1b3d06 FCE |
7376 | // PR9993: Add extra headers to work around undeclared types in individual |
7377 | // include/trace/foo.h files | |
9e0cd21a | 7378 | const vector<string>& extra_headers = tracepoint_extra_headers (s); |
3c1b3d06 FCE |
7379 | for (unsigned z=0; z<extra_headers.size(); z++) |
7380 | s.op->newline() << "#include <" << extra_headers[z] << ">\n"; | |
47dd066d | 7381 | |
6fb70fb7 JS |
7382 | for (unsigned i = 0; i < probes.size(); ++i) |
7383 | { | |
7384 | tracepoint_derived_probe *p = probes[i]; | |
47dd066d | 7385 | |
96b030fe JS |
7386 | // emit a separate entry function for each probe, since tracepoints |
7387 | // don't provide any sort of context pointer. | |
392e08b7 | 7388 | s.op->newline() << "#undef TRACE_INCLUDE_FILE"; |
6fb70fb7 | 7389 | s.op->newline() << "#include <" << p->header << ">"; |
5f73a260 JS |
7390 | |
7391 | // Starting in 2.6.35, at the same time NOARGS was added, the callback | |
7392 | // always has a void* as the first parameter. PR11599 | |
7393 | s.op->newline() << "#ifdef DECLARE_TRACE_NOARGS"; | |
7394 | s.op->newline() << "#define STAP_TP_DATA , NULL"; | |
7395 | s.op->newline() << "#define STAP_TP_PROTO void *cb_data" | |
7396 | << " __attribute__ ((unused))"; | |
7397 | if (!p->args.empty()) | |
7398 | s.op->line() << ","; | |
7399 | s.op->newline() << "#else"; | |
7400 | s.op->newline() << "#define STAP_TP_DATA"; | |
7401 | s.op->newline() << "#define STAP_TP_PROTO"; | |
7402 | if (p->args.empty()) | |
7403 | s.op->line() << " void"; | |
7404 | s.op->newline() << "#endif"; | |
7405 | ||
6fb70fb7 | 7406 | s.op->newline() << "static void enter_tracepoint_probe_" << i << "("; |
5f73a260 JS |
7407 | s.op->newline(2) << "STAP_TP_PROTO"; |
7408 | ||
6fb70fb7 JS |
7409 | for (unsigned j = 0; j < p->args.size(); ++j) |
7410 | { | |
7411 | if (j > 0) | |
5f73a260 JS |
7412 | s.op->line() << ","; |
7413 | s.op->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
6fb70fb7 | 7414 | } |
5f73a260 JS |
7415 | s.op->newline() << ")"; |
7416 | s.op->newline(-2) << "{"; | |
7417 | ||
6fb70fb7 | 7418 | s.op->indent(1); |
dd225250 | 7419 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", |
c12d974f | 7420 | lex_cast_qstring (*p->sole_location())); |
f8a968bc | 7421 | s.op->newline() << "c->marker_name = " |
c12d974f FCE |
7422 | << lex_cast_qstring (p->tracepoint_name) |
7423 | << ";"; | |
f8a968bc JS |
7424 | for (unsigned j = 0; j < p->args.size(); ++j) |
7425 | if (p->args[j].used) | |
7426 | { | |
66671fd8 | 7427 | s.op->newline() << "c->probe_locals." << p->name << ".__tracepoint_arg_" |
f8a968bc | 7428 | << p->args[j].name << " = (int64_t)"; |
ad370dcc | 7429 | s.op->line() << p->args[j].typecast; |
f8a968bc JS |
7430 | s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";"; |
7431 | } | |
6fb70fb7 JS |
7432 | s.op->newline() << p->name << " (c);"; |
7433 | common_probe_entryfn_epilogue (s.op); | |
7434 | s.op->newline(-1) << "}"; | |
47dd066d | 7435 | |
96b030fe JS |
7436 | // emit normalized registration functions |
7437 | s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {"; | |
7438 | s.op->newline(1) << "return register_trace_" << p->tracepoint_name | |
5f73a260 | 7439 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
96b030fe | 7440 | s.op->newline(-1) << "}"; |
47dd066d | 7441 | |
86758d5f JS |
7442 | // NB: we're not prepared to deal with unreg failures. However, failures |
7443 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
7444 | // weren't even registered. The former should be OKed by the initial | |
7445 | // registration call, and the latter is safe to ignore. | |
7446 | s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {"; | |
7447 | s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name | |
5f73a260 | 7448 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
96b030fe | 7449 | s.op->newline(-1) << "}"; |
6fb70fb7 | 7450 | s.op->newline(); |
5f73a260 JS |
7451 | |
7452 | s.op->newline() << "#undef STAP_TP_DATA"; | |
7453 | s.op->newline() << "#undef STAP_TP_PROTO"; | |
7454 | s.op->newline(); | |
af304783 DS |
7455 | } |
7456 | ||
96b030fe JS |
7457 | // emit an array of registration functions for easy init/shutdown |
7458 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
7459 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 7460 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
7461 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
7462 | s.op->indent(1); | |
7463 | for (unsigned i = 0; i < probes.size(); ++i) | |
7464 | { | |
7465 | s.op->newline () << "{"; | |
7466 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
7467 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
7468 | s.op->line() << " },"; | |
7469 | } | |
7470 | s.op->newline(-1) << "};"; | |
7471 | s.op->newline(); | |
47dd066d WC |
7472 | } |
7473 | ||
7474 | ||
79189b84 JS |
7475 | void |
7476 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
47dd066d | 7477 | { |
79189b84 JS |
7478 | if (probes.size () == 0) |
7479 | return; | |
47dd066d | 7480 | |
79189b84 | 7481 | s.op->newline() << "/* init tracepoint probes */"; |
96b030fe JS |
7482 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
7483 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
7484 | s.op->newline() << "if (rc) {"; | |
7485 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
7486 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
7487 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
7488 | s.op->newline(-1) << "}"; | |
7489 | s.op->newline(-1) << "}"; | |
47dd066d | 7490 | |
bc9a523d FCE |
7491 | // This would be technically proper (on those autoconf-detectable |
7492 | // kernels that include this function in tracepoint.h), however we | |
7493 | // already make several calls to synchronze_sched() during our | |
7494 | // shutdown processes. | |
47dd066d | 7495 | |
bc9a523d FCE |
7496 | // s.op->newline() << "if (rc)"; |
7497 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
7498 | // s.op->indent(-1); | |
79189b84 | 7499 | } |
47dd066d WC |
7500 | |
7501 | ||
79189b84 JS |
7502 | void |
7503 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
47dd066d | 7504 | { |
79189b84 JS |
7505 | if (probes.empty()) |
7506 | return; | |
47dd066d | 7507 | |
96b030fe JS |
7508 | s.op->newline() << "/* deregister tracepoint probes */"; |
7509 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
7510 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
7511 | s.op->indent(-1); | |
47dd066d | 7512 | |
bc9a523d | 7513 | // Not necessary: see above. |
47dd066d | 7514 | |
bc9a523d | 7515 | // s.op->newline() << "tracepoint_synchronize_unregister();"; |
79189b84 | 7516 | } |
b20febf3 | 7517 | |
47dd066d | 7518 | |
75ead1f7 | 7519 | struct tracepoint_query : public base_query |
47dd066d | 7520 | { |
75ead1f7 JS |
7521 | tracepoint_query(dwflpp & dw, const string & tracepoint, |
7522 | probe * base_probe, probe_point * base_loc, | |
7523 | vector<derived_probe *> & results): | |
7524 | base_query(dw, "*"), tracepoint(tracepoint), | |
7525 | base_probe(base_probe), base_loc(base_loc), | |
7526 | results(results) {} | |
47dd066d | 7527 | |
75ead1f7 | 7528 | const string& tracepoint; |
47dd066d | 7529 | |
75ead1f7 JS |
7530 | probe * base_probe; |
7531 | probe_point * base_loc; | |
7532 | vector<derived_probe *> & results; | |
f982c59b | 7533 | set<string> probed_names; |
47dd066d | 7534 | |
75ead1f7 JS |
7535 | void handle_query_module(); |
7536 | int handle_query_cu(Dwarf_Die * cudie); | |
7537 | int handle_query_func(Dwarf_Die * func); | |
b20febf3 | 7538 | |
75ead1f7 JS |
7539 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); |
7540 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
7541 | }; | |
47dd066d WC |
7542 | |
7543 | ||
7544 | void | |
75ead1f7 | 7545 | tracepoint_query::handle_query_module() |
47dd066d | 7546 | { |
75ead1f7 JS |
7547 | // look for the tracepoints in each CU |
7548 | dw.iterate_over_cus(tracepoint_query_cu, this); | |
47dd066d WC |
7549 | } |
7550 | ||
7551 | ||
75ead1f7 JS |
7552 | int |
7553 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
47dd066d | 7554 | { |
75ead1f7 | 7555 | dw.focus_on_cu (cudie); |
47dd066d | 7556 | |
75ead1f7 JS |
7557 | // look at each function to see if it's a tracepoint |
7558 | string function = "stapprobe_" + tracepoint; | |
7559 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
47dd066d WC |
7560 | } |
7561 | ||
7562 | ||
75ead1f7 JS |
7563 | int |
7564 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
47dd066d | 7565 | { |
75ead1f7 | 7566 | dw.focus_on_function (func); |
47dd066d | 7567 | |
60d98537 | 7568 | assert(startswith(dw.function_name, "stapprobe_")); |
75ead1f7 | 7569 | string tracepoint_instance = dw.function_name.substr(10); |
f982c59b JS |
7570 | |
7571 | // check for duplicates -- sometimes tracepoint headers may be indirectly | |
7572 | // included in more than one of our tracequery modules. | |
7573 | if (!probed_names.insert(tracepoint_instance).second) | |
7574 | return DWARF_CB_OK; | |
7575 | ||
79189b84 JS |
7576 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
7577 | tracepoint_instance, | |
7578 | base_probe, base_loc); | |
7579 | results.push_back (dp); | |
75ead1f7 | 7580 | return DWARF_CB_OK; |
47dd066d WC |
7581 | } |
7582 | ||
7583 | ||
75ead1f7 JS |
7584 | int |
7585 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
47dd066d | 7586 | { |
75ead1f7 JS |
7587 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); |
7588 | if (pending_interrupts) return DWARF_CB_ABORT; | |
7589 | return q->handle_query_cu(cudie); | |
47dd066d WC |
7590 | } |
7591 | ||
7592 | ||
75ead1f7 JS |
7593 | int |
7594 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
47dd066d | 7595 | { |
75ead1f7 JS |
7596 | tracepoint_query * q = static_cast<tracepoint_query *>(query); |
7597 | if (pending_interrupts) return DWARF_CB_ABORT; | |
7598 | return q->handle_query_func(func); | |
47dd066d WC |
7599 | } |
7600 | ||
7601 | ||
0a6f5a3f | 7602 | struct tracepoint_builder: public derived_probe_builder |
47dd066d | 7603 | { |
0a6f5a3f JS |
7604 | private: |
7605 | dwflpp *dw; | |
7606 | bool init_dw(systemtap_session& s); | |
55e50c24 JS |
7607 | string get_tracequery_module(systemtap_session& s, |
7608 | const vector<string>& headers); | |
47dd066d | 7609 | |
0a6f5a3f | 7610 | public: |
47dd066d | 7611 | |
0a6f5a3f JS |
7612 | tracepoint_builder(): dw(0) {} |
7613 | ~tracepoint_builder() { delete dw; } | |
47dd066d | 7614 | |
0a6f5a3f JS |
7615 | void build_no_more (systemtap_session& s) |
7616 | { | |
7617 | if (dw && s.verbose > 3) | |
7618 | clog << "tracepoint_builder releasing dwflpp" << endl; | |
7619 | delete dw; | |
7620 | dw = NULL; | |
7621 | } | |
47dd066d | 7622 | |
0a6f5a3f JS |
7623 | void build(systemtap_session& s, |
7624 | probe *base, probe_point *location, | |
7625 | literal_map_t const& parameters, | |
7626 | vector<derived_probe*>& finished_results); | |
7627 | }; | |
47dd066d | 7628 | |
47dd066d | 7629 | |
f982c59b JS |
7630 | string |
7631 | tracepoint_builder::get_tracequery_module(systemtap_session& s, | |
55e50c24 | 7632 | const vector<string>& headers) |
0a6f5a3f | 7633 | { |
c95eddf7 | 7634 | if (s.verbose > 2) |
55e50c24 JS |
7635 | { |
7636 | clog << "Pass 2: getting a tracequery for " | |
7637 | << headers.size() << " headers:" << endl; | |
7638 | for (size_t i = 0; i < headers.size(); ++i) | |
7639 | clog << " " << headers[i] << endl; | |
7640 | } | |
c95eddf7 | 7641 | |
a2639cb7 | 7642 | string tracequery_path; |
b278033a JS |
7643 | if (s.use_cache) |
7644 | { | |
7645 | // see if the cached module exists | |
55e50c24 | 7646 | tracequery_path = find_tracequery_hash(s, headers); |
d105f664 | 7647 | if (!tracequery_path.empty() && !s.poison_cache) |
b278033a | 7648 | { |
a2639cb7 | 7649 | int fd = open(tracequery_path.c_str(), O_RDONLY); |
b278033a JS |
7650 | if (fd != -1) |
7651 | { | |
7652 | if (s.verbose > 2) | |
a2639cb7 | 7653 | clog << "Pass 2: using cached " << tracequery_path << endl; |
b278033a | 7654 | close(fd); |
f982c59b | 7655 | return tracequery_path; |
b278033a JS |
7656 | } |
7657 | } | |
7658 | } | |
47dd066d | 7659 | |
b278033a | 7660 | // no cached module, time to make it |
f982c59b | 7661 | |
55e50c24 JS |
7662 | // PR9993: Add extra headers to work around undeclared types in individual |
7663 | // include/trace/foo.h files | |
9e0cd21a | 7664 | vector<string> short_headers = tracepoint_extra_headers(s); |
55e50c24 JS |
7665 | |
7666 | // add each requested tracepoint header | |
7667 | for (size_t i = 0; i < headers.size(); ++i) | |
7668 | { | |
7669 | const string &header = headers[i]; | |
7670 | size_t root_pos = header.rfind("/include/"); | |
7671 | short_headers.push_back((root_pos != string::npos) ? | |
7672 | header.substr(root_pos + 9) : | |
7673 | header); | |
7674 | } | |
f982c59b | 7675 | |
0a6f5a3f | 7676 | string tracequery_ko; |
55e50c24 | 7677 | int rc = make_tracequery(s, tracequery_ko, short_headers); |
0a6f5a3f | 7678 | if (rc != 0) |
c95eddf7 | 7679 | tracequery_ko = "/dev/null"; |
47dd066d | 7680 | |
e16dc041 | 7681 | // try to save tracequery in the cache |
b278033a | 7682 | if (s.use_cache) |
e16dc041 JS |
7683 | copy_file(tracequery_ko, tracequery_path, s.verbose > 2); |
7684 | ||
f982c59b JS |
7685 | return tracequery_ko; |
7686 | } | |
7687 | ||
7688 | ||
7689 | bool | |
7690 | tracepoint_builder::init_dw(systemtap_session& s) | |
7691 | { | |
7692 | if (dw != NULL) | |
7693 | return true; | |
7694 | ||
7695 | vector<string> tracequery_modules; | |
55e50c24 | 7696 | vector<string> system_headers; |
f982c59b JS |
7697 | |
7698 | glob_t trace_glob; | |
7699 | string globs[] = { | |
f982c59b | 7700 | "/include/trace/events/*.h", |
f982c59b | 7701 | "/source/include/trace/events/*.h", |
508968a7 JS |
7702 | "/include/trace/*.h", |
7703 | "/source/include/trace/*.h", | |
f982c59b JS |
7704 | }; |
7705 | for (unsigned z = 0; z < sizeof(globs) / sizeof(globs[0]); z++) | |
7706 | { | |
7707 | string glob_str(s.kernel_build_tree + globs[z]); | |
7708 | glob(glob_str.c_str(), 0, NULL, &trace_glob); | |
7709 | for (unsigned i = 0; i < trace_glob.gl_pathc; ++i) | |
7710 | { | |
7711 | string header(trace_glob.gl_pathv[i]); | |
7712 | ||
7713 | // filter out a few known "internal-only" headers | |
60d98537 JS |
7714 | if (endswith(header, "/define_trace.h") || |
7715 | endswith(header, "/ftrace.h") || | |
7716 | endswith(header, "/trace_events.h") || | |
7717 | endswith(header, "_event_types.h")) | |
f982c59b JS |
7718 | continue; |
7719 | ||
55e50c24 | 7720 | system_headers.push_back(header); |
f982c59b JS |
7721 | } |
7722 | globfree(&trace_glob); | |
7723 | } | |
7724 | ||
55e50c24 JS |
7725 | // First attempt to do all system headers in one go |
7726 | string tracequery_path = get_tracequery_module(s, system_headers); | |
7727 | // NB: An empty tracequery means that the header didn't compile correctly | |
7728 | if (get_file_size(tracequery_path)) | |
7729 | tracequery_modules.push_back(tracequery_path); | |
7730 | else | |
7731 | // Otherwise try to do them one at a time (PR10424) | |
7732 | for (size_t i = 0; i < system_headers.size(); ++i) | |
7733 | { | |
392e08b7 FCE |
7734 | if (pending_interrupts) return false; |
7735 | ||
55e50c24 JS |
7736 | vector<string> one_header(1, system_headers[i]); |
7737 | tracequery_path = get_tracequery_module(s, one_header); | |
7738 | if (get_file_size(tracequery_path)) | |
7739 | tracequery_modules.push_back(tracequery_path); | |
7740 | } | |
7741 | ||
f982c59b JS |
7742 | // TODO: consider other sources of tracepoint headers too, like from |
7743 | // a command-line parameter or some environment or .systemtaprc | |
47dd066d | 7744 | |
59c11f91 | 7745 | dw = new dwflpp(s, tracequery_modules, true); |
0a6f5a3f JS |
7746 | return true; |
7747 | } | |
47dd066d | 7748 | |
0a6f5a3f JS |
7749 | void |
7750 | tracepoint_builder::build(systemtap_session& s, | |
7751 | probe *base, probe_point *location, | |
7752 | literal_map_t const& parameters, | |
7753 | vector<derived_probe*>& finished_results) | |
7754 | { | |
7755 | if (!init_dw(s)) | |
7756 | return; | |
47dd066d | 7757 | |
75ead1f7 JS |
7758 | string tracepoint; |
7759 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
47dd066d | 7760 | |
75ead1f7 | 7761 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); |
51178501 | 7762 | dw->iterate_over_modules(&query_module, &q); |
47dd066d | 7763 | } |
47dd066d | 7764 | |
e6fe60e7 | 7765 | |
b55bc428 | 7766 | // ------------------------------------------------------------------------ |
bd2b1e68 | 7767 | // Standard tapset registry. |
b55bc428 FCE |
7768 | // ------------------------------------------------------------------------ |
7769 | ||
7a053d3b | 7770 | void |
f8220a7b | 7771 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 7772 | { |
47e0478e | 7773 | register_tapset_been(s); |
93646f4d | 7774 | register_tapset_itrace(s); |
dd0e4fa7 | 7775 | register_tapset_mark(s); |
7a212aa8 | 7776 | register_tapset_procfs(s); |
912e8c59 | 7777 | register_tapset_timers(s); |
b84779a5 | 7778 | register_tapset_utrace(s); |
b98a8d73 | 7779 | |
7a24d422 | 7780 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 7781 | dwarf_derived_probe::register_patterns(s); |
30a279be | 7782 | |
888af770 FCE |
7783 | // XXX: user-space starter set |
7784 | s.pattern_root->bind_num(TOK_PROCESS) | |
7785 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
27dc09b1 | 7786 | ->bind_unprivileged() |
888af770 FCE |
7787 | ->bind(new uprobe_builder ()); |
7788 | s.pattern_root->bind_num(TOK_PROCESS) | |
7789 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
27dc09b1 | 7790 | ->bind_unprivileged() |
888af770 FCE |
7791 | ->bind(new uprobe_builder ()); |
7792 | ||
0a6f5a3f JS |
7793 | // kernel tracepoint probes |
7794 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
7795 | ->bind(new tracepoint_builder()); | |
7796 | ||
e6fe60e7 AM |
7797 | // Kprobe based probe |
7798 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
7799 | ->bind(new kprobe_builder()); | |
7800 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) | |
7801 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
7802 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
7803 | ->bind(new kprobe_builder()); | |
b6371390 JS |
7804 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
7805 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
7806 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
7807 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
7808 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
7809 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
7810 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
7811 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
7812 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
dd225250 PS |
7813 | |
7814 | //Hwbkpt based probe | |
b47f3a55 FCE |
7815 | // NB: we formerly registered the probe point types only if the kernel configuration |
7816 | // allowed it. However, we get better error messages if we allow probes to resolve. | |
7817 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7818 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
7819 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
7820 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
7821 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7822 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
7823 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
7824 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
7825 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7826 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
7827 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7828 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
7829 | // length supported with address only, not symbol names | |
83ea76b1 WC |
7830 | |
7831 | //perf event based probe | |
4763f713 | 7832 | register_tapset_perf(s); |
b55bc428 | 7833 | } |
dc38c0ae DS |
7834 | |
7835 | ||
b20febf3 FCE |
7836 | vector<derived_probe_group*> |
7837 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 7838 | { |
b20febf3 | 7839 | vector<derived_probe_group*> g; |
912e8c59 JS |
7840 | |
7841 | #define DOONE(x) \ | |
7842 | if (s. x##_derived_probes) \ | |
7843 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
7844 | |
7845 | // Note that order *is* important here. We want to make sure we | |
7846 | // register (actually run) begin probes before any other probe type | |
7847 | // is run. Similarly, when unregistering probes, we want to | |
7848 | // unregister (actually run) end probes after every other probe type | |
7849 | // has be unregistered. To do the latter, | |
7850 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
7851 | DOONE(be); |
7852 | DOONE(dwarf); | |
888af770 | 7853 | DOONE(uprobe); |
b20febf3 FCE |
7854 | DOONE(timer); |
7855 | DOONE(profile); | |
7856 | DOONE(mark); | |
0a6f5a3f | 7857 | DOONE(tracepoint); |
e6fe60e7 | 7858 | DOONE(kprobe); |
dd225250 | 7859 | DOONE(hwbkpt); |
83ea76b1 | 7860 | DOONE(perf); |
b20febf3 | 7861 | DOONE(hrtimer); |
ce82316f | 7862 | DOONE(procfs); |
935447c8 DS |
7863 | |
7864 | // Another "order is important" item. We want to make sure we | |
7865 | // "register" the dummy task_finder probe group after all probe | |
7866 | // groups that use the task_finder. | |
7867 | DOONE(utrace); | |
a96d1db0 | 7868 | DOONE(itrace); |
935447c8 | 7869 | DOONE(task_finder); |
b20febf3 FCE |
7870 | #undef DOONE |
7871 | return g; | |
46b84a80 | 7872 | } |
73267b89 JS |
7873 | |
7874 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |