]>
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 | // ------------------------------------------------------------------------ | |
faea5e16 JS |
66 | |
67 | string | |
68 | common_probe_init (derived_probe* p) | |
69 | { | |
70 | ostringstream o; | |
71 | o << "STAP_PROBE_INIT(&" << p->name << ", " | |
2d767770 JS |
72 | << lex_cast_qstring (*p->sole_location()) << ", " |
73 | << lex_cast_qstring (*p->script_location()) << ")"; | |
faea5e16 JS |
74 | return o.str(); |
75 | } | |
76 | ||
77 | ||
b20febf3 | 78 | void |
a58d79d0 | 79 | common_probe_entryfn_prologue (translator_output* o, string statestr, |
faea5e16 | 80 | string probe, bool overload_processing) |
b20febf3 | 81 | { |
72d18b98 | 82 | o->newline() << "struct context* __restrict__ c;"; |
e0a17418 JS |
83 | o->newline() << "#if !INTERRUPTIBLE"; |
84 | o->newline() << "unsigned long flags;"; | |
85 | o->newline() << "#endif"; | |
b20febf3 | 86 | |
a58d79d0 DS |
87 | if (overload_processing) |
88 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
89 | else | |
90 | o->newline() << "#ifdef STP_TIMING"; | |
91 | o->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
b20febf3 | 92 | o->newline() << "#endif"; |
b20febf3 | 93 | |
e0a17418 JS |
94 | o->newline() << "#if INTERRUPTIBLE"; |
95 | o->newline() << "preempt_disable ();"; | |
96 | o->newline() << "#else"; | |
97 | o->newline() << "local_irq_save (flags);"; | |
98 | o->newline() << "#endif"; | |
b20febf3 | 99 | |
c931ec8a | 100 | // Check for enough free enough stack space |
d05a1d00 | 101 | o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space |
a63401b1 | 102 | o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space |
d05a1d00 FCE |
103 | // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack |
104 | // NB: see also CONFIG_DEBUG_STACKOVERFLOW | |
b3c3ca7c FCE |
105 | o->newline() << "atomic_inc (& skipped_count);"; |
106 | o->newline() << "#ifdef STP_TIMING"; | |
107 | o->newline() << "atomic_inc (& skipped_count_lowstack);"; | |
108 | o->newline() << "#endif"; | |
c931ec8a FCE |
109 | o->newline() << "goto probe_epilogue;"; |
110 | o->newline(-1) << "}"; | |
111 | ||
b20febf3 FCE |
112 | o->newline() << "if (atomic_read (&session_state) != " << statestr << ")"; |
113 | o->newline(1) << "goto probe_epilogue;"; | |
114 | o->indent(-1); | |
9a604fac | 115 | |
4a0ae64c | 116 | o->newline() << "c = contexts[smp_processor_id()];"; |
b3c3ca7c | 117 | o->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
9c736061 FCE |
118 | o->newline(1) << "#if !INTERRUPTIBLE"; |
119 | o->newline() << "atomic_inc (& skipped_count);"; | |
120 | o->newline() << "#endif"; | |
b3c3ca7c FCE |
121 | o->newline() << "#ifdef STP_TIMING"; |
122 | o->newline() << "atomic_inc (& skipped_count_reentrant);"; | |
c12d974f FCE |
123 | o->newline() << "#ifdef DEBUG_REENTRANCY"; |
124 | o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
faea5e16 | 125 | << probe << ".pp, c->probe_point ?: \"?\", smp_processor_id());"; |
c12d974f FCE |
126 | // NB: There is a conceivable race condition here with reading |
127 | // c->probe_point, knowing that this other probe is sort of running. | |
128 | // However, in reality, it's interrupted. Plus even if it were able | |
129 | // to somehow start again, and stop before we read c->probe_point, | |
130 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
131 | o->newline() << "#endif"; | |
b3c3ca7c | 132 | o->newline() << "#endif"; |
9a604fac | 133 | o->newline() << "atomic_dec (& c->busy);"; |
b20febf3 | 134 | o->newline() << "goto probe_epilogue;"; |
9a604fac FCE |
135 | o->newline(-1) << "}"; |
136 | o->newline(); | |
1e00cfb1 | 137 | o->newline() << "c->last_stmt = 0;"; |
9a604fac | 138 | o->newline() << "c->last_error = 0;"; |
a7ed0d3e | 139 | o->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter |
22f8b401 | 140 | o->newline() << "c->regs = 0;"; |
b916df9c | 141 | o->newline() << "c->unwaddr = 0;"; |
faea5e16 | 142 | o->newline() << "c->probe_point = " << probe << ".pp;"; |
d48df0cf JS |
143 | o->newline() << "#ifdef STP_NEED_PROBE_NAME"; |
144 | o->newline() << "c->probe_name = " << probe << ".pn;"; | |
2d767770 | 145 | o->newline() << "#else"; |
d48df0cf | 146 | o->newline() << "c->probe_name = 0;"; |
2d767770 | 147 | o->newline() << "#endif"; |
b916df9c | 148 | // reset unwound address cache |
fcff848e | 149 | o->newline() << "c->pi = 0;"; |
af234c40 | 150 | o->newline() << "c->pi_longs = 0;"; |
9addf322 | 151 | o->newline() << "c->regparm = 0;"; |
bc54e71c MH |
152 | o->newline() << "c->marker_name = NULL;"; |
153 | o->newline() << "c->marker_format = NULL;"; | |
e0a17418 JS |
154 | |
155 | o->newline() << "#if INTERRUPTIBLE"; | |
156 | o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
157 | o->newline() << "#else"; | |
158 | o->newline() << "c->actionremaining = MAXACTION;"; | |
159 | o->newline() << "#endif"; | |
dbb68664 FCE |
160 | o->newline() << "#ifdef STP_TIMING"; |
161 | o->newline() << "c->statp = 0;"; | |
162 | o->newline() << "#endif"; | |
5e562a69 | 163 | o->newline() << "c->ri = 0;"; |
9915575b FCE |
164 | // NB: The following would actually be incorrect. |
165 | // That's because cycles_sum/cycles_base values are supposed to survive | |
166 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
167 | // cycles), the values will be reset. | |
168 | /* | |
f0e6dc63 FCE |
169 | o->newline() << "#ifdef STP_OVERLOAD"; |
170 | o->newline() << "c->cycles_sum = 0;"; | |
171 | o->newline() << "c->cycles_base = 0;"; | |
41c262f3 | 172 | o->newline() << "#endif"; |
9915575b | 173 | */ |
b20febf3 | 174 | } |
9a604fac | 175 | |
a44a0785 | 176 | |
b20febf3 | 177 | void |
a58d79d0 | 178 | common_probe_entryfn_epilogue (translator_output* o, |
912e8c59 | 179 | bool overload_processing) |
b20febf3 | 180 | { |
a58d79d0 DS |
181 | if (overload_processing) |
182 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
183 | else | |
184 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 185 | o->newline() << "{"; |
a58d79d0 DS |
186 | o->newline(1) << "cycles_t cycles_atend = get_cycles ();"; |
187 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
188 | // fewer, if the hardware counter rolls over really quickly. We | |
189 | // handle 32-bit wraparound here. | |
190 | o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
191 | o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
192 | o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
193 | o->indent(-1); | |
dbb68664 | 194 | |
a58d79d0 | 195 | o->newline() << "#ifdef STP_TIMING"; |
dbb68664 | 196 | o->newline() << "if (likely (c->statp)) _stp_stat_add(*c->statp, cycles_elapsed);"; |
a58d79d0 DS |
197 | o->newline() << "#endif"; |
198 | ||
199 | if (overload_processing) | |
200 | { | |
201 | o->newline() << "#ifdef STP_OVERLOAD"; | |
202 | o->newline() << "{"; | |
203 | // If the cycle count has wrapped (cycles_atend > cycles_base), | |
204 | // let's go ahead and pretend the interval has been reached. | |
205 | // This should reset cycles_base and cycles_sum. | |
206 | o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; | |
207 | o->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
208 | o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
209 | o->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
210 | ||
211 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
212 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
213 | // has overloaded the system and we need to quit. | |
214 | o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; | |
215 | o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
216 | o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
217 | o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);"; | |
551e9f14 | 218 | o->newline() << "atomic_inc (&error_count);"; |
a58d79d0 | 219 | o->newline(-1) << "}"; |
e57b735a | 220 | |
a58d79d0 DS |
221 | o->newline() << "c->cycles_base = cycles_atend;"; |
222 | o->newline() << "c->cycles_sum = 0;"; | |
223 | o->newline(-1) << "}"; | |
224 | o->newline(-1) << "}"; | |
225 | o->newline() << "#endif"; | |
226 | } | |
e57b735a | 227 | |
440f755a JS |
228 | o->newline(-1) << "}"; |
229 | o->newline() << "#endif"; | |
e57b735a | 230 | |
440f755a | 231 | o->newline() << "c->probe_point = 0;"; // vacated |
d48df0cf | 232 | o->newline() << "c->probe_name = 0;"; |
440f755a JS |
233 | o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; |
234 | o->newline(1) << "if (c->last_stmt != NULL)"; | |
235 | o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
236 | o->newline(-1) << "else"; | |
237 | o->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
238 | o->indent(-1); | |
239 | o->newline() << "atomic_inc (& error_count);"; | |
240 | o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {"; | |
241 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
242 | o->newline() << "_stp_exit ();"; | |
243 | o->newline(-1) << "}"; | |
244 | o->newline(-1) << "}"; | |
245 | o->newline() << "atomic_dec (&c->busy);"; | |
e57b735a | 246 | |
440f755a JS |
247 | o->newline(-1) << "probe_epilogue:"; // context is free |
248 | o->indent(1); | |
e57b735a | 249 | |
440f755a JS |
250 | // Check for excessive skip counts. |
251 | o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {"; | |
079915a5 | 252 | o->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(& session_state, STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))"; |
f65166cc | 253 | o->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");"; |
440f755a | 254 | o->newline(-1) << "}"; |
e57b735a | 255 | |
440f755a JS |
256 | o->newline() << "#if INTERRUPTIBLE"; |
257 | o->newline() << "preempt_enable_no_resched ();"; | |
258 | o->newline() << "#else"; | |
259 | o->newline() << "local_irq_restore (flags);"; | |
260 | o->newline() << "#endif"; | |
261 | } | |
e57b735a | 262 | |
e57b735a | 263 | |
440f755a | 264 | // ------------------------------------------------------------------------ |
e57b735a | 265 | |
440f755a JS |
266 | // ------------------------------------------------------------------------ |
267 | // Dwarf derived probes. "We apologize for the inconvience." | |
268 | // ------------------------------------------------------------------------ | |
e57b735a | 269 | |
4627ed58 JS |
270 | static const string TOK_KERNEL("kernel"); |
271 | static const string TOK_MODULE("module"); | |
272 | static const string TOK_FUNCTION("function"); | |
273 | static const string TOK_INLINE("inline"); | |
274 | static const string TOK_CALL("call"); | |
275 | static const string TOK_RETURN("return"); | |
276 | static const string TOK_MAXACTIVE("maxactive"); | |
277 | static const string TOK_STATEMENT("statement"); | |
278 | static const string TOK_ABSOLUTE("absolute"); | |
279 | static const string TOK_PROCESS("process"); | |
a794dbeb | 280 | static const string TOK_PROVIDER("provider"); |
4627ed58 JS |
281 | static const string TOK_MARK("mark"); |
282 | static const string TOK_TRACE("trace"); | |
283 | static const string TOK_LABEL("label"); | |
63b4fd14 | 284 | static const string TOK_LIBRARY("library"); |
e57b735a | 285 | |
1adf8ef1 | 286 | static int query_cu (Dwarf_Die * cudie, void * arg); |
6b517475 | 287 | static void query_addr(Dwarf_Addr addr, dwarf_query *q); |
e57b735a | 288 | |
440f755a JS |
289 | // Can we handle this query with just symbol-table info? |
290 | enum dbinfo_reqt | |
291 | { | |
292 | dbr_unknown, | |
293 | dbr_none, // kernel.statement(NUM).absolute | |
294 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
295 | dbr_need_dwarf | |
296 | }; | |
e57b735a | 297 | |
20e4a32c | 298 | |
440f755a JS |
299 | struct base_query; // forward decls |
300 | struct dwarf_query; | |
301 | struct dwflpp; | |
302 | struct symbol_table; | |
20e4a32c | 303 | |
a781f401 | 304 | |
440f755a JS |
305 | struct |
306 | symbol_table | |
307 | { | |
308 | module_info *mod_info; // associated module | |
309 | map<string, func_info*> map_by_name; | |
1c6b77e5 JS |
310 | multimap<Dwarf_Addr, func_info*> map_by_addr; |
311 | typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t; | |
440f755a JS |
312 | typedef pair<iterator_t, iterator_t> range_t; |
313 | #ifdef __powerpc__ | |
314 | GElf_Word opd_section; | |
315 | #endif | |
2867a2a1 JS |
316 | void add_symbol(const char *name, bool weak, bool descriptor, |
317 | Dwarf_Addr addr, Dwarf_Addr *high_addr); | |
440f755a | 318 | enum info_status read_symbols(FILE *f, const string& path); |
83ca3872 MW |
319 | enum info_status read_from_elf_file(const string& path, |
320 | const systemtap_session &sess); | |
321 | enum info_status read_from_text_file(const string& path, | |
322 | const systemtap_session &sess); | |
440f755a JS |
323 | enum info_status get_from_elf(); |
324 | void prepare_section_rejection(Dwfl_Module *mod); | |
325 | bool reject_section(GElf_Word section); | |
440f755a JS |
326 | void purge_syscall_stubs(); |
327 | func_info *lookup_symbol(const string& name); | |
328 | Dwarf_Addr lookup_symbol_address(const string& name); | |
329 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
7a053d3b | 330 | |
440f755a JS |
331 | symbol_table(module_info *mi) : mod_info(mi) {} |
332 | ~symbol_table(); | |
333 | }; | |
77de5e9e | 334 | |
440f755a JS |
335 | static bool null_die(Dwarf_Die *die) |
336 | { | |
337 | static Dwarf_Die null = { 0 }; | |
338 | return (!die || !memcmp(die, &null, sizeof(null))); | |
339 | } | |
c4ce66a1 JS |
340 | |
341 | ||
7a053d3b | 342 | enum |
bd2b1e68 | 343 | function_spec_type |
7a053d3b | 344 | { |
bd2b1e68 GH |
345 | function_alone, |
346 | function_and_file, | |
7a053d3b | 347 | function_file_and_line |
bd2b1e68 GH |
348 | }; |
349 | ||
ec4373ff | 350 | |
bd2b1e68 | 351 | struct dwarf_builder; |
f10534c6 | 352 | struct dwarf_var_expanding_visitor; |
77de5e9e | 353 | |
2930abc7 | 354 | |
b20febf3 FCE |
355 | // XXX: This class is a candidate for subclassing to separate |
356 | // the relocation vs non-relocation variants. Likewise for | |
357 | // kprobe vs kretprobe variants. | |
358 | ||
359 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 360 | { |
b20febf3 FCE |
361 | dwarf_derived_probe (const string& function, |
362 | const string& filename, | |
363 | int line, | |
364 | const string& module, | |
365 | const string& section, | |
366 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 367 | Dwarf_Addr addr, |
b20febf3 FCE |
368 | dwarf_query & q, |
369 | Dwarf_Die* scope_die); | |
20e4a32c | 370 | |
b20febf3 FCE |
371 | string module; |
372 | string section; | |
373 | Dwarf_Addr addr; | |
63b4fd14 | 374 | string path; |
27dc09b1 | 375 | bool has_process; |
2930abc7 | 376 | bool has_return; |
c9bad430 | 377 | bool has_maxactive; |
63b4fd14 | 378 | bool has_library; |
c9bad430 | 379 | long maxactive_val; |
b642c901 SC |
380 | string user_path; |
381 | string user_lib; | |
b95e2b79 | 382 | bool access_vars; |
2930abc7 | 383 | |
af234c40 JS |
384 | unsigned saved_longs, saved_strings; |
385 | dwarf_derived_probe* entry_handler; | |
386 | ||
b8da0ad1 | 387 | void printsig (std::ostream &o) const; |
6b66b9f7 | 388 | virtual void join_group (systemtap_session& s); |
9020300d | 389 | void emit_probe_local_init(translator_output * o); |
d0bfd2ac | 390 | void getargs(std::list<std::string> &arg_set) const; |
0a98fd42 | 391 | |
27dc09b1 DB |
392 | void emit_unprivileged_assertion (translator_output*); |
393 | void print_dupe_stamp(ostream& o); | |
394 | ||
bd2b1e68 | 395 | // Pattern registration helpers. |
7a053d3b | 396 | static void register_statement_variants(match_node * root, |
27dc09b1 DB |
397 | dwarf_builder * dw, |
398 | bool bind_unprivileged_p = false); | |
fd6602a0 | 399 | static void register_function_variants(match_node * root, |
27dc09b1 DB |
400 | dwarf_builder * dw, |
401 | bool bind_unprivileged_p = false); | |
7a053d3b | 402 | static void register_function_and_statement_variants(match_node * root, |
27dc09b1 DB |
403 | dwarf_builder * dw, |
404 | bool bind_unprivileged_p = false); | |
c4ce66a1 | 405 | static void register_patterns(systemtap_session& s); |
6b66b9f7 JS |
406 | |
407 | protected: | |
408 | dwarf_derived_probe(probe *base, | |
409 | probe_point *location, | |
410 | Dwarf_Addr addr, | |
411 | bool has_return): | |
412 | derived_probe(base, location), addr(addr), has_return(has_return), | |
af234c40 JS |
413 | has_maxactive(0), maxactive_val(0), access_vars(false), |
414 | saved_longs(0), saved_strings(0), entry_handler(0) | |
6b66b9f7 JS |
415 | {} |
416 | ||
417 | private: | |
d0bfd2ac | 418 | list<string> args; |
f10534c6 | 419 | void saveargs(dwarf_query& q, Dwarf_Die* scope_die, dwarf_var_expanding_visitor& v); |
20c6c071 GH |
420 | }; |
421 | ||
dc38c0ae | 422 | |
6b66b9f7 | 423 | struct uprobe_derived_probe: public dwarf_derived_probe |
6d0f3f0c | 424 | { |
6d0f3f0c | 425 | int pid; // 0 => unrestricted |
0973d815 | 426 | |
6d0f3f0c FCE |
427 | uprobe_derived_probe (const string& function, |
428 | const string& filename, | |
429 | int line, | |
430 | const string& module, | |
6d0f3f0c FCE |
431 | const string& section, |
432 | Dwarf_Addr dwfl_addr, | |
433 | Dwarf_Addr addr, | |
434 | dwarf_query & q, | |
6b66b9f7 JS |
435 | Dwarf_Die* scope_die): |
436 | dwarf_derived_probe(function, filename, line, module, section, | |
437 | dwfl_addr, addr, q, scope_die), pid(0) | |
438 | {} | |
6d0f3f0c | 439 | |
0973d815 FCE |
440 | // alternate constructor for process(PID).statement(ADDR).absolute |
441 | uprobe_derived_probe (probe *base, | |
442 | probe_point *location, | |
443 | int pid, | |
444 | Dwarf_Addr addr, | |
6b66b9f7 JS |
445 | bool has_return): |
446 | dwarf_derived_probe(base, location, addr, has_return), pid(pid) | |
447 | {} | |
9ace370f | 448 | |
6d0f3f0c | 449 | void join_group (systemtap_session& s); |
2865d17a DB |
450 | |
451 | void emit_unprivileged_assertion (translator_output*); | |
8f6d8c2b | 452 | void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); } |
6d0f3f0c FCE |
453 | }; |
454 | ||
dc38c0ae DS |
455 | struct dwarf_derived_probe_group: public derived_probe_group |
456 | { | |
457 | private: | |
b20febf3 FCE |
458 | multimap<string,dwarf_derived_probe*> probes_by_module; |
459 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
460 | |
461 | public: | |
b20febf3 FCE |
462 | void enroll (dwarf_derived_probe* probe); |
463 | void emit_module_decls (systemtap_session& s); | |
464 | void emit_module_init (systemtap_session& s); | |
465 | void emit_module_exit (systemtap_session& s); | |
dc38c0ae DS |
466 | }; |
467 | ||
468 | ||
20c6c071 | 469 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 470 | struct base_query |
20c6c071 | 471 | { |
c4ce66a1 JS |
472 | base_query(dwflpp & dw, literal_map_t const & params); |
473 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 474 | virtual ~base_query() {} |
bd2b1e68 | 475 | |
5227f1ea | 476 | systemtap_session & sess; |
2c384610 | 477 | dwflpp & dw; |
5227f1ea | 478 | |
bd2b1e68 | 479 | // Parameter extractors. |
86bf665e | 480 | static bool has_null_param(literal_map_t const & params, |
888af770 | 481 | string const & k); |
86bf665e | 482 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 483 | string const & k, string & v); |
86bf665e | 484 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 485 | string const & k, long & v); |
86bf665e | 486 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 487 | string const & k, Dwarf_Addr & v); |
b55bc428 | 488 | |
2c384610 DS |
489 | // Extracted parameters. |
490 | bool has_kernel; | |
91af0778 FCE |
491 | bool has_module; |
492 | bool has_process; | |
63b4fd14 | 493 | bool has_library; |
2c384610 | 494 | string module_val; // has_kernel => module_val = "kernel" |
63b4fd14 | 495 | string path; // executable path if module is a .so |
2c384610 DS |
496 | |
497 | virtual void handle_query_module() = 0; | |
498 | }; | |
499 | ||
500 | ||
c4ce66a1 JS |
501 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
502 | sess(dw.sess), dw(dw) | |
2c384610 | 503 | { |
91af0778 | 504 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
505 | if (has_kernel) |
506 | module_val = "kernel"; | |
91af0778 FCE |
507 | |
508 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
509 | if (has_module) | |
510 | has_process = false; | |
4baf0e53 | 511 | else |
d0a7f5a9 | 512 | { |
63b4fd14 | 513 | string library_name; |
d0a7f5a9 | 514 | has_process = get_string_param(params, TOK_PROCESS, module_val); |
63b4fd14 SC |
515 | has_library = get_string_param (params, TOK_LIBRARY, library_name); |
516 | if (has_library) | |
517 | { | |
518 | path = find_executable (module_val); | |
519 | module_val = find_executable (library_name, "LD_LIBRARY_PATH"); | |
520 | } | |
521 | else if (has_process) | |
d0a7f5a9 FCE |
522 | module_val = find_executable (module_val); |
523 | } | |
91af0778 FCE |
524 | |
525 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
526 | } |
527 | ||
c4ce66a1 JS |
528 | base_query::base_query(dwflpp & dw, const string & module_val) |
529 | : sess(dw.sess), dw(dw), module_val(module_val) | |
530 | { | |
531 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
532 | // which means that userspace modules won't get any PATH searching. | |
533 | if (module_val.find('/') == string::npos) | |
534 | { | |
535 | has_kernel = (module_val == TOK_KERNEL); | |
536 | has_module = !has_kernel; | |
537 | has_process = false; | |
538 | } | |
539 | else | |
540 | { | |
541 | has_kernel = has_module = false; | |
542 | has_process = true; | |
543 | } | |
544 | } | |
545 | ||
2c384610 | 546 | bool |
86bf665e | 547 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
548 | string const & k) |
549 | { | |
888af770 | 550 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
551 | } |
552 | ||
553 | ||
554 | bool | |
86bf665e | 555 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
556 | string const & k, string & v) |
557 | { | |
558 | return derived_probe_builder::get_param (params, k, v); | |
559 | } | |
560 | ||
561 | ||
562 | bool | |
86bf665e | 563 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
564 | string const & k, long & v) |
565 | { | |
566 | int64_t value; | |
567 | bool present = derived_probe_builder::get_param (params, k, value); | |
568 | v = (long) value; | |
569 | return present; | |
570 | } | |
571 | ||
572 | ||
573 | bool | |
86bf665e | 574 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
575 | string const & k, Dwarf_Addr & v) |
576 | { | |
577 | int64_t value; | |
578 | bool present = derived_probe_builder::get_param (params, k, value); | |
579 | v = (Dwarf_Addr) value; | |
580 | return present; | |
581 | } | |
582 | ||
2c384610 DS |
583 | struct dwarf_query : public base_query |
584 | { | |
e1278bd4 | 585 | dwarf_query(probe * base_probe, |
2c384610 DS |
586 | probe_point * base_loc, |
587 | dwflpp & dw, | |
86bf665e | 588 | literal_map_t const & params, |
b642c901 SC |
589 | vector<derived_probe *> & results, |
590 | const string user_path, | |
591 | const string user_lib); | |
2c384610 | 592 | |
c4ce66a1 | 593 | vector<derived_probe *> & results; |
8f14e444 | 594 | set<string> inlined_non_returnable; // function names |
c4ce66a1 JS |
595 | probe * base_probe; |
596 | probe_point * base_loc; | |
b642c901 SC |
597 | string user_path; |
598 | string user_lib; | |
c4ce66a1 | 599 | |
2c384610 | 600 | virtual void handle_query_module(); |
5f0a03a6 JK |
601 | void query_module_dwarf(); |
602 | void query_module_symtab(); | |
2c384610 | 603 | |
2930abc7 FCE |
604 | void add_probe_point(string const & funcname, |
605 | char const * filename, | |
606 | int line, | |
607 | Dwarf_Die *scope_die, | |
608 | Dwarf_Addr addr); | |
36f9dd1d | 609 | |
857bdfd1 JS |
610 | // Track addresses we've already seen in a given module |
611 | set<Dwarf_Addr> alias_dupes; | |
612 | ||
7fdd3e2c JS |
613 | // Track inlines we've already seen as well |
614 | // NB: this can't be compared just by entrypc, as inlines can overlap | |
615 | set<inline_instance_info> inline_dupes; | |
616 | ||
2930abc7 | 617 | // Extracted parameters. |
7a053d3b | 618 | string function_val; |
20c6c071 GH |
619 | |
620 | bool has_function_str; | |
621 | bool has_statement_str; | |
622 | bool has_function_num; | |
623 | bool has_statement_num; | |
7a053d3b RM |
624 | string statement_str_val; |
625 | string function_str_val; | |
c239d28c GH |
626 | Dwarf_Addr statement_num_val; |
627 | Dwarf_Addr function_num_val; | |
20c6c071 | 628 | |
b8da0ad1 FCE |
629 | bool has_call; |
630 | bool has_inline; | |
20c6c071 GH |
631 | bool has_return; |
632 | ||
c9bad430 DS |
633 | bool has_maxactive; |
634 | long maxactive_val; | |
635 | ||
20c6c071 GH |
636 | bool has_label; |
637 | string label_val; | |
638 | ||
639 | bool has_relative; | |
640 | long relative_val; | |
641 | ||
37ebca01 FCE |
642 | bool has_absolute; |
643 | ||
467bea43 SC |
644 | bool has_mark; |
645 | ||
5f0a03a6 JK |
646 | enum dbinfo_reqt dbinfo_reqt; |
647 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
648 | ||
7d6d0afc | 649 | void parse_function_spec(const string & spec); |
20c6c071 | 650 | function_spec_type spec_type; |
7d6d0afc | 651 | vector<string> scopes; |
20c6c071 GH |
652 | string function; |
653 | string file; | |
0c8b7d37 | 654 | line_t line_type; |
879eb9e9 | 655 | int line[2]; |
5f0a03a6 | 656 | bool query_done; // Found exact match |
20c6c071 | 657 | |
bd25380d | 658 | set<string> filtered_srcfiles; |
7e1279ea FCE |
659 | |
660 | // Map official entrypc -> func_info object | |
86bf665e TM |
661 | inline_instance_map_t filtered_inlines; |
662 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
663 | bool choose_next_line; |
664 | Dwarf_Addr entrypc_for_next_line; | |
4df79aaf JS |
665 | |
666 | void query_module_functions (); | |
b55bc428 FCE |
667 | }; |
668 | ||
98afd80e FCE |
669 | |
670 | struct dwarf_builder: public derived_probe_builder | |
b55bc428 | 671 | { |
665e1256 | 672 | map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */ |
7a24d422 | 673 | map <string,dwflpp*> user_dw; |
b642c901 SC |
674 | string user_path; |
675 | string user_lib; | |
ae2552da | 676 | dwarf_builder() {} |
aa30ccd3 | 677 | |
ae2552da | 678 | dwflpp *get_kern_dw(systemtap_session& sess, const string& module) |
707bf35e | 679 | { |
ea14cf67 FCE |
680 | if (kern_dw[module] == 0) |
681 | kern_dw[module] = new dwflpp(sess, module, true); // might throw | |
ae2552da | 682 | return kern_dw[module]; |
707bf35e JS |
683 | } |
684 | ||
685 | dwflpp *get_user_dw(systemtap_session& sess, const string& module) | |
686 | { | |
ea14cf67 FCE |
687 | if (user_dw[module] == 0) |
688 | user_dw[module] = new dwflpp(sess, module, false); // might throw | |
707bf35e JS |
689 | return user_dw[module]; |
690 | } | |
7a24d422 FCE |
691 | |
692 | /* NB: not virtual, so can be called from dtor too: */ | |
06aca46a | 693 | void dwarf_build_no_more (bool verbose) |
aa30ccd3 | 694 | { |
ae2552da FCE |
695 | for (map<string,dwflpp*>::iterator udi = kern_dw.begin(); |
696 | udi != kern_dw.end(); | |
697 | udi ++) | |
aa30ccd3 | 698 | { |
7a24d422 | 699 | if (verbose) |
ae2552da FCE |
700 | clog << "dwarf_builder releasing kernel dwflpp " << udi->first << endl; |
701 | delete udi->second; | |
aa30ccd3 | 702 | } |
ae2552da | 703 | kern_dw.erase (kern_dw.begin(), kern_dw.end()); |
7a24d422 FCE |
704 | |
705 | for (map<string,dwflpp*>::iterator udi = user_dw.begin(); | |
706 | udi != user_dw.end(); | |
707 | udi ++) | |
708 | { | |
709 | if (verbose) | |
710 | clog << "dwarf_builder releasing user dwflpp " << udi->first << endl; | |
711 | delete udi->second; | |
712 | } | |
713 | user_dw.erase (user_dw.begin(), user_dw.end()); | |
714 | } | |
715 | ||
716 | void build_no_more (systemtap_session &s) | |
717 | { | |
718 | dwarf_build_no_more (s.verbose > 3); | |
aa30ccd3 FCE |
719 | } |
720 | ||
e38d6504 RM |
721 | ~dwarf_builder() |
722 | { | |
7a24d422 | 723 | dwarf_build_no_more (false); |
c8959a29 | 724 | } |
aa30ccd3 | 725 | |
5227f1ea | 726 | virtual void build(systemtap_session & sess, |
7a053d3b | 727 | probe * base, |
20c6c071 | 728 | probe_point * location, |
86bf665e | 729 | literal_map_t const & parameters, |
20c6c071 | 730 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
731 | }; |
732 | ||
5111fc3e | 733 | |
e1278bd4 | 734 | dwarf_query::dwarf_query(probe * base_probe, |
20c6c071 GH |
735 | probe_point * base_loc, |
736 | dwflpp & dw, | |
86bf665e | 737 | literal_map_t const & params, |
b642c901 SC |
738 | vector<derived_probe *> & results, |
739 | const string user_path, | |
740 | const string user_lib) | |
c4ce66a1 | 741 | : base_query(dw, params), results(results), |
b642c901 SC |
742 | base_probe(base_probe), base_loc(base_loc), |
743 | user_path(user_path), user_lib(user_lib) | |
bd2b1e68 GH |
744 | { |
745 | // Reduce the query to more reasonable semantic values (booleans, | |
746 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
747 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
748 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
749 | ||
750 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
751 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
752 | ||
0f336e95 SC |
753 | has_label = get_string_param(params, TOK_LABEL, label_val); |
754 | ||
b8da0ad1 FCE |
755 | has_call = has_null_param(params, TOK_CALL); |
756 | has_inline = has_null_param(params, TOK_INLINE); | |
bd2b1e68 | 757 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 758 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 759 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 760 | has_mark = false; |
37ebca01 | 761 | |
bd2b1e68 | 762 | if (has_function_str) |
7d6d0afc | 763 | parse_function_spec(function_str_val); |
bd2b1e68 | 764 | else if (has_statement_str) |
7d6d0afc | 765 | parse_function_spec(statement_str_val); |
0daad364 | 766 | |
5f0a03a6 JK |
767 | dbinfo_reqt = assess_dbinfo_reqt(); |
768 | query_done = false; | |
0daad364 JS |
769 | } |
770 | ||
771 | ||
440f755a JS |
772 | func_info_map_t * |
773 | get_filtered_functions(dwarf_query *q) | |
774 | { | |
775 | return &q->filtered_functions; | |
776 | } | |
777 | ||
778 | ||
779 | inline_instance_map_t * | |
780 | get_filtered_inlines(dwarf_query *q) | |
781 | { | |
782 | return &q->filtered_inlines; | |
783 | } | |
784 | ||
785 | ||
2c384610 | 786 | void |
5f0a03a6 | 787 | dwarf_query::query_module_dwarf() |
2c384610 DS |
788 | { |
789 | if (has_function_num || has_statement_num) | |
790 | { | |
791 | // If we have module("foo").function(0xbeef) or | |
792 | // module("foo").statement(0xbeef), the address is relative | |
793 | // to the start of the module, so we seek the function | |
794 | // number plus the module's bias. | |
6b517475 JS |
795 | Dwarf_Addr addr = has_function_num ? |
796 | function_num_val : statement_num_val; | |
08d1d520 MW |
797 | |
798 | // These are raw addresses, we need to know what the elf_bias | |
799 | // is to feed it to libdwfl based functions. | |
800 | Dwarf_Addr elf_bias; | |
801 | Elf *elf = dwfl_module_getelf (dw.module, &elf_bias); | |
802 | assert(elf); | |
803 | addr += elf_bias; | |
6b517475 | 804 | query_addr(addr, this); |
2c384610 DS |
805 | } |
806 | else | |
807 | { | |
808 | // Otherwise if we have a function("foo") or statement("foo") | |
809 | // specifier, we have to scan over all the CUs looking for | |
810 | // the function(s) in question | |
811 | assert(has_function_str || has_statement_str); | |
4df79aaf JS |
812 | |
813 | // For simple cases, no wildcard and no source:line, we can do a very | |
814 | // quick function lookup in a module-wide cache. | |
815 | if (spec_type == function_alone && !dw.name_has_wildcard(function)) | |
816 | query_module_functions(); | |
817 | else | |
818 | dw.iterate_over_cus(&query_cu, this); | |
2c384610 DS |
819 | } |
820 | } | |
821 | ||
5f0a03a6 JK |
822 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
823 | dwarf_query * q); | |
824 | ||
825 | void | |
826 | dwarf_query::query_module_symtab() | |
827 | { | |
828 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
829 | if (dbinfo_reqt == dbr_need_dwarf) | |
830 | return; | |
831 | ||
832 | module_info *mi = dw.mod_info; | |
833 | if (dbinfo_reqt == dbr_need_symtab) | |
834 | { | |
835 | if (mi->symtab_status == info_unknown) | |
836 | mi->get_symtab(this); | |
837 | if (mi->symtab_status == info_absent) | |
838 | return; | |
839 | } | |
840 | ||
841 | func_info *fi = NULL; | |
842 | symbol_table *sym_table = mi->sym_table; | |
843 | ||
844 | if (has_function_str) | |
845 | { | |
846 | // Per dwarf_query::assess_dbinfo_reqt()... | |
847 | assert(spec_type == function_alone); | |
848 | if (dw.name_has_wildcard(function_str_val)) | |
849 | { | |
850 | // Until we augment the blacklist sufficently... | |
851 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
852 | { | |
853 | // e.g., kernel.function("*") | |
854 | cerr << "Error: Pattern '" | |
855 | << function_str_val | |
856 | << "' matches every instruction address in the symbol table," | |
857 | << endl | |
858 | << "some of which aren't even functions." | |
859 | << " Please be more precise." | |
860 | << endl; | |
861 | return; | |
862 | } | |
2e67a43b | 863 | symbol_table::iterator_t iter; |
1c6b77e5 JS |
864 | for (iter = sym_table->map_by_addr.begin(); |
865 | iter != sym_table->map_by_addr.end(); | |
2e67a43b | 866 | ++iter) |
5f0a03a6 | 867 | { |
1c6b77e5 | 868 | fi = iter->second; |
5f0a03a6 JK |
869 | if (!null_die(&fi->die)) |
870 | continue; // already handled in query_module_dwarf() | |
871 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
872 | query_func_info(fi->addr, *fi, this); | |
873 | } | |
874 | } | |
875 | else | |
876 | { | |
877 | fi = sym_table->lookup_symbol(function_str_val); | |
2867a2a1 | 878 | if (fi && !fi->descriptor && null_die(&fi->die)) |
5f0a03a6 JK |
879 | query_func_info(fi->addr, *fi, this); |
880 | } | |
881 | } | |
882 | else | |
883 | { | |
884 | assert(has_function_num || has_statement_num); | |
885 | // Find the "function" in which the indicated address resides. | |
886 | Dwarf_Addr addr = | |
887 | (has_function_num ? function_num_val : statement_num_val); | |
888 | fi = sym_table->get_func_containing_address(addr); | |
889 | if (!fi) | |
890 | { | |
83ca3872 MW |
891 | if (! sess.suppress_warnings) |
892 | cerr << "Warning: address " | |
893 | << hex << addr << dec | |
894 | << " out of range for module " | |
895 | << dw.module_name; | |
5f0a03a6 JK |
896 | return; |
897 | } | |
898 | if (!null_die(&fi->die)) | |
899 | { | |
900 | // addr looks like it's in the compilation unit containing | |
901 | // the indicated function, but query_module_dwarf() didn't | |
902 | // match addr to any compilation unit, so addr must be | |
903 | // above that cu's address range. | |
83ca3872 MW |
904 | if (! sess.suppress_warnings) |
905 | cerr << "Warning: address " | |
906 | << hex << addr << dec | |
907 | << " maps to no known compilation unit in module " | |
908 | << dw.module_name; | |
5f0a03a6 JK |
909 | return; |
910 | } | |
911 | query_func_info(fi->addr, *fi, this); | |
912 | } | |
913 | } | |
914 | ||
915 | void | |
916 | dwarf_query::handle_query_module() | |
917 | { | |
1c6b77e5 JS |
918 | bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab; |
919 | dw.get_module_dwarf(false, report); | |
920 | ||
921 | // prebuild the symbol table to resolve aliases | |
922 | dw.mod_info->get_symtab(this); | |
923 | ||
857bdfd1 JS |
924 | // reset the dupe-checking for each new module |
925 | alias_dupes.clear(); | |
7fdd3e2c | 926 | inline_dupes.clear(); |
857bdfd1 | 927 | |
5f0a03a6 JK |
928 | if (dw.mod_info->dwarf_status == info_present) |
929 | query_module_dwarf(); | |
1c6b77e5 | 930 | |
5f0a03a6 JK |
931 | // Consult the symbol table if we haven't found all we're looking for. |
932 | // asm functions can show up in the symbol table but not in dwarf. | |
933 | if (sess.consult_symtab && !query_done) | |
934 | query_module_symtab(); | |
935 | } | |
936 | ||
2c384610 | 937 | |
7d6d0afc JS |
938 | void |
939 | dwarf_query::parse_function_spec(const string & spec) | |
bd2b1e68 | 940 | { |
1d12a9b2 JS |
941 | line_type = ABSOLUTE; |
942 | line[0] = line[1] = 0; | |
943 | ||
7d6d0afc | 944 | size_t src_pos, line_pos, dash_pos, scope_pos, next_scope_pos; |
bd2b1e68 | 945 | |
7d6d0afc JS |
946 | // look for named scopes |
947 | scope_pos = 0; | |
948 | next_scope_pos = spec.find("::"); | |
949 | while (next_scope_pos != string::npos) | |
bd2b1e68 | 950 | { |
7d6d0afc JS |
951 | scopes.push_back(spec.substr(scope_pos, next_scope_pos - scope_pos)); |
952 | scope_pos = next_scope_pos + 2; | |
953 | next_scope_pos = spec.find("::", scope_pos); | |
bd2b1e68 GH |
954 | } |
955 | ||
7d6d0afc JS |
956 | // look for a source separator |
957 | src_pos = spec.find('@', scope_pos); | |
958 | if (src_pos == string::npos) | |
bd2b1e68 | 959 | { |
7d6d0afc JS |
960 | function = spec.substr(scope_pos); |
961 | spec_type = function_alone; | |
bd2b1e68 | 962 | } |
7d6d0afc | 963 | else |
879eb9e9 | 964 | { |
7d6d0afc | 965 | function = spec.substr(scope_pos, src_pos - scope_pos); |
7a053d3b | 966 | |
7d6d0afc JS |
967 | // look for a line-number separator |
968 | line_pos = spec.find_first_of(":+", src_pos); | |
969 | if (line_pos == string::npos) | |
970 | { | |
971 | file = spec.substr(src_pos + 1); | |
972 | spec_type = function_and_file; | |
973 | } | |
974 | else | |
975 | { | |
976 | file = spec.substr(src_pos + 1, line_pos - src_pos - 1); | |
977 | ||
978 | // classify the line spec | |
979 | spec_type = function_file_and_line; | |
980 | if (spec[line_pos] == '+') | |
981 | line_type = RELATIVE; | |
982 | else if (spec[line_pos + 1] == '*' && | |
983 | spec.length() == line_pos + 2) | |
984 | line_type = WILDCARD; | |
985 | else | |
986 | line_type = ABSOLUTE; | |
987 | ||
988 | if (line_type != WILDCARD) | |
989 | try | |
990 | { | |
991 | // try to parse either N or N-M | |
992 | dash_pos = spec.find('-', line_pos + 1); | |
993 | if (dash_pos == string::npos) | |
994 | line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1)); | |
995 | else | |
996 | { | |
997 | line_type = RANGE; | |
998 | line[0] = lex_cast<int>(spec.substr(line_pos + 1, | |
999 | dash_pos - line_pos - 1)); | |
1000 | line[1] = lex_cast<int>(spec.substr(dash_pos + 1)); | |
1001 | } | |
1002 | } | |
1003 | catch (runtime_error & exn) | |
1004 | { | |
1005 | goto bad; | |
1006 | } | |
1007 | } | |
bd2b1e68 GH |
1008 | } |
1009 | ||
7d6d0afc JS |
1010 | if (function.empty() || |
1011 | (spec_type != function_alone && file.empty())) | |
bd2b1e68 GH |
1012 | goto bad; |
1013 | ||
7d6d0afc | 1014 | if (sess.verbose > 2) |
bd2b1e68 | 1015 | { |
7d6d0afc | 1016 | clog << "parsed '" << spec << "'"; |
41c262f3 | 1017 | |
7d6d0afc JS |
1018 | if (!scopes.empty()) |
1019 | clog << ", scope '" << scopes[0] << "'"; | |
1020 | for (unsigned i = 1; i < scopes.size(); ++i) | |
1021 | clog << "::'" << scopes[i] << "'"; | |
41c262f3 | 1022 | |
7d6d0afc JS |
1023 | clog << ", func '" << function << "'"; |
1024 | ||
1025 | if (spec_type != function_alone) | |
1026 | clog << ", file '" << file << "'"; | |
1027 | ||
1028 | if (spec_type == function_file_and_line) | |
1029 | { | |
1030 | clog << ", line "; | |
1031 | switch (line_type) | |
1032 | { | |
1033 | case ABSOLUTE: | |
1034 | clog << line[0]; | |
1035 | break; | |
1036 | ||
1037 | case RELATIVE: | |
1038 | clog << "+" << line[0]; | |
1039 | break; | |
1040 | ||
1041 | case RANGE: | |
1042 | clog << line[0] << " - " << line[1]; | |
1043 | break; | |
1044 | ||
1045 | case WILDCARD: | |
1046 | clog << "*"; | |
1047 | break; | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | clog << endl; | |
bd2b1e68 GH |
1052 | } |
1053 | ||
7d6d0afc JS |
1054 | return; |
1055 | ||
1056 | bad: | |
1057 | throw semantic_error("malformed specification '" + spec + "'", | |
1058 | base_probe->tok); | |
bd2b1e68 GH |
1059 | } |
1060 | ||
1061 | ||
36f9dd1d | 1062 | void |
b20febf3 FCE |
1063 | dwarf_query::add_probe_point(const string& funcname, |
1064 | const char* filename, | |
36f9dd1d | 1065 | int line, |
b20febf3 | 1066 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
1067 | Dwarf_Addr addr) |
1068 | { | |
b20febf3 | 1069 | string reloc_section; // base section for relocation purposes |
27646582 | 1070 | Dwarf_Addr reloc_addr; // relocated |
b20febf3 | 1071 | const string& module = dw.module_name; // "kernel" or other |
36f9dd1d | 1072 | |
37ebca01 FCE |
1073 | assert (! has_absolute); // already handled in dwarf_builder::build() |
1074 | ||
789448a3 | 1075 | reloc_addr = dw.relocate_address(addr, reloc_section); |
2930abc7 | 1076 | |
7f9f3386 FCE |
1077 | if (sess.verbose > 1) |
1078 | { | |
b20febf3 FCE |
1079 | clog << "probe " << funcname << "@" << filename << ":" << line; |
1080 | if (string(module) == TOK_KERNEL) | |
1081 | clog << " kernel"; | |
91af0778 | 1082 | else if (has_module) |
b20febf3 | 1083 | clog << " module=" << module; |
91af0778 FCE |
1084 | else if (has_process) |
1085 | clog << " process=" << module; | |
b20febf3 | 1086 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
b20febf3 | 1087 | clog << " pc=0x" << hex << addr << dec; |
7f9f3386 | 1088 | } |
4baf0e53 | 1089 | |
27646582 | 1090 | bool bad = dw.blacklisted_p (funcname, filename, line, module, |
789448a3 | 1091 | addr, has_return); |
b20febf3 FCE |
1092 | if (sess.verbose > 1) |
1093 | clog << endl; | |
7f9f3386 | 1094 | |
84048984 FCE |
1095 | if (module == TOK_KERNEL) |
1096 | { | |
1097 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
1098 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 1099 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
1100 | } |
1101 | ||
b20febf3 FCE |
1102 | if (! bad) |
1103 | { | |
1a0dbc5a | 1104 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
1105 | |
1106 | if (has_process) | |
1107 | { | |
1108 | results.push_back (new uprobe_derived_probe(funcname, filename, line, | |
6b66b9f7 | 1109 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1110 | *this, scope_die)); |
1111 | } | |
1112 | else | |
1113 | { | |
1114 | assert (has_kernel || has_module); | |
1115 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 1116 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1117 | *this, scope_die)); |
1118 | } | |
b20febf3 | 1119 | } |
2930abc7 FCE |
1120 | } |
1121 | ||
5f0a03a6 JK |
1122 | enum dbinfo_reqt |
1123 | dwarf_query::assess_dbinfo_reqt() | |
1124 | { | |
1125 | if (has_absolute) | |
1126 | { | |
1127 | // kernel.statement(NUM).absolute | |
1128 | return dbr_none; | |
1129 | } | |
1130 | if (has_inline) | |
1131 | { | |
1132 | // kernel.function("f").inline or module("m").function("f").inline | |
1133 | return dbr_need_dwarf; | |
1134 | } | |
1135 | if (has_function_str && spec_type == function_alone) | |
1136 | { | |
1137 | // kernel.function("f") or module("m").function("f") | |
1138 | return dbr_need_symtab; | |
1139 | } | |
1140 | if (has_statement_num) | |
1141 | { | |
1142 | // kernel.statement(NUM) or module("m").statement(NUM) | |
1143 | // Technically, all we need is the module offset (or _stext, for | |
1144 | // the kernel). But for that we need either the ELF file or (for | |
1145 | // _stext) the symbol table. In either case, the symbol table | |
1146 | // is available, and that allows us to map the NUM (address) | |
1147 | // to a function, which is goodness. | |
1148 | return dbr_need_symtab; | |
1149 | } | |
1150 | if (has_function_num) | |
1151 | { | |
1152 | // kernel.function(NUM) or module("m").function(NUM) | |
1153 | // Need the symbol table so we can back up from NUM to the | |
1154 | // start of the function. | |
1155 | return dbr_need_symtab; | |
1156 | } | |
1157 | // Symbol table tells us nothing about source files or line numbers. | |
1158 | return dbr_need_dwarf; | |
1159 | } | |
2930abc7 FCE |
1160 | |
1161 | ||
b8da0ad1 FCE |
1162 | // The critical determining factor when interpreting a pattern |
1163 | // string is, perhaps surprisingly: "presence of a lineno". The | |
1164 | // presence of a lineno changes the search strategy completely. | |
1165 | // | |
1166 | // Compare the two cases: | |
1167 | // | |
1168 | // 1. {statement,function}(foo@file.c:lineno) | |
1169 | // - find the files matching file.c | |
1170 | // - in each file, find the functions matching foo | |
1171 | // - query the file for line records matching lineno | |
1172 | // - iterate over the line records, | |
1173 | // - and iterate over the functions, | |
1174 | // - if(haspc(function.DIE, line.addr)) | |
1175 | // - if looking for statements: probe(lineno.addr) | |
1176 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
1177 | // | |
1178 | // 2. {statement,function}(foo@file.c) | |
1179 | // - find the files matching file.c | |
1180 | // - in each file, find the functions matching foo | |
1181 | // - probe(function.{entrypc,return,etc.}) | |
1182 | // | |
1183 | // Thus the first decision we make is based on the presence of a | |
1184 | // lineno, and we enter entirely different sets of callbacks | |
1185 | // depending on that decision. | |
1186 | // | |
1187 | // Note that the first case is a generalization fo the second, in that | |
1188 | // we could theoretically search through line records for matching | |
1189 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
1190 | // are already cached elsewhere, so we can do an "index scan" as an | |
1191 | // optimization. | |
7e1279ea | 1192 | |
bd2b1e68 | 1193 | static void |
4cd232e4 | 1194 | query_statement (string const & func, |
20e4a32c | 1195 | char const * file, |
4cd232e4 | 1196 | int line, |
bcc12710 | 1197 | Dwarf_Die *scope_die, |
20e4a32c | 1198 | Dwarf_Addr stmt_addr, |
4cd232e4 | 1199 | dwarf_query * q) |
bd2b1e68 | 1200 | { |
39bcd429 FCE |
1201 | try |
1202 | { | |
cee35f73 | 1203 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 1204 | line, scope_die, stmt_addr); |
39bcd429 FCE |
1205 | } |
1206 | catch (const semantic_error& e) | |
1207 | { | |
1208 | q->sess.print_error (e); | |
1209 | } | |
bd2b1e68 GH |
1210 | } |
1211 | ||
6b517475 JS |
1212 | static void |
1213 | query_addr(Dwarf_Addr addr, dwarf_query *q) | |
1214 | { | |
1215 | dwflpp &dw = q->dw; | |
1216 | ||
08d1d520 MW |
1217 | if (q->sess.verbose > 2) |
1218 | clog << "query_addr 0x" << hex << addr << dec << endl; | |
6b517475 JS |
1219 | |
1220 | // First pick which CU contains this address | |
1221 | Dwarf_Die* cudie = dw.query_cu_containing_address(addr); | |
1222 | if (!cudie) // address could be wildly out of range | |
1223 | return; | |
1224 | dw.focus_on_cu(cudie); | |
1225 | ||
1226 | // Now compensate for the dw bias | |
1227 | addr -= dw.module_bias; | |
1228 | ||
1229 | // Per PR5787, we look up the scope die even for | |
1230 | // statement_num's, for blacklist sensitivity and $var | |
1231 | // resolution purposes. | |
1232 | ||
1233 | // Find the scopes containing this address | |
1234 | vector<Dwarf_Die> scopes = dw.getscopes(addr); | |
1235 | if (scopes.empty()) | |
1236 | return; | |
1237 | ||
1238 | // Look for the innermost containing function | |
1239 | Dwarf_Die *fnscope = NULL; | |
1240 | for (size_t i = 0; i < scopes.size(); ++i) | |
1241 | { | |
1242 | int tag = dwarf_tag(&scopes[i]); | |
1243 | if ((tag == DW_TAG_subprogram && !q->has_inline) || | |
1244 | (tag == DW_TAG_inlined_subroutine && | |
1245 | !q->has_call && !q->has_return)) | |
1246 | { | |
1247 | fnscope = &scopes[i]; | |
1248 | break; | |
1249 | } | |
1250 | } | |
1251 | if (!fnscope) | |
1252 | return; | |
1253 | dw.focus_on_function(fnscope); | |
1254 | ||
1255 | Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0]; | |
1256 | ||
1257 | const char *file = dwarf_decl_file(fnscope); | |
1258 | int line; | |
1259 | dwarf_decl_line(fnscope, &line); | |
1260 | ||
1261 | // Function probes should reset the addr to the function entry | |
1262 | // and possibly perform prologue searching | |
1263 | if (q->has_function_num) | |
1264 | { | |
1265 | dw.die_entrypc(fnscope, &addr); | |
1266 | if (dwarf_tag(fnscope) == DW_TAG_subprogram && | |
1267 | (q->sess.prologue_searching || q->has_process)) // PR 6871 | |
1268 | { | |
1269 | func_info func; | |
1270 | func.die = *fnscope; | |
1271 | func.name = dw.function_name; | |
1272 | func.decl_file = file; | |
1273 | func.decl_line = line; | |
1274 | func.entrypc = addr; | |
1275 | ||
1276 | func_info_map_t funcs(1, func); | |
1277 | dw.resolve_prologue_endings (funcs); | |
1278 | if (funcs[0].prologue_end) | |
1279 | addr = funcs[0].prologue_end; | |
1280 | } | |
1281 | } | |
1282 | else | |
1283 | { | |
1284 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr)); | |
1285 | if (address_line) | |
1286 | { | |
1287 | file = address_line.linesrc(); | |
1288 | line = address_line.lineno(); | |
1289 | } | |
1290 | ||
1291 | // Verify that a raw address matches the beginning of a | |
1292 | // statement. This is a somewhat lame check that the address | |
1293 | // is at the start of an assembly instruction. Mark probes are in the | |
1294 | // middle of a macro and thus not strictly at a statement beginning. | |
1295 | // Guru mode may override this check. | |
1296 | if (!q->has_mark && (!address_line || address_line.addr() != addr)) | |
1297 | { | |
1298 | stringstream msg; | |
1299 | msg << "address 0x" << hex << addr | |
1300 | << " does not match the beginning of a statement"; | |
1301 | if (address_line) | |
1302 | msg << " (try 0x" << hex << address_line.addr() << ")"; | |
1303 | else | |
1304 | msg << " (no line info found for '" << dw.cu_name() | |
1305 | << "', in module '" << dw.module_name << "')"; | |
1306 | if (! q->sess.guru_mode) | |
1307 | throw semantic_error(msg.str()); | |
1308 | else if (! q->sess.suppress_warnings) | |
1309 | q->sess.print_warning(msg.str()); | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | // Build a probe at this point | |
1314 | query_statement(dw.function_name, file, line, scope, addr, q); | |
1315 | } | |
1316 | ||
8096dd7d JS |
1317 | static void |
1318 | query_label (string const & func, | |
1319 | char const * label, | |
1320 | char const * file, | |
1321 | int line, | |
1322 | Dwarf_Die *scope_die, | |
1323 | Dwarf_Addr stmt_addr, | |
1324 | dwarf_query * q) | |
1325 | { | |
6b517475 JS |
1326 | assert (q->has_statement_str || q->has_function_str); |
1327 | ||
8096dd7d JS |
1328 | size_t i = q->results.size(); |
1329 | ||
1330 | // weed out functions whose decl_file isn't one of | |
1331 | // the source files that we actually care about | |
6b517475 | 1332 | if (q->spec_type != function_alone && |
8096dd7d JS |
1333 | q->filtered_srcfiles.count(file) == 0) |
1334 | return; | |
1335 | ||
1336 | query_statement(func, file, line, scope_die, stmt_addr, q); | |
1337 | ||
c72aa911 JS |
1338 | // after the fact, insert the label back into the derivation chain |
1339 | probe_point::component* ppc = | |
1340 | new probe_point::component(TOK_LABEL, new literal_string (label)); | |
1341 | for (; i < q->results.size(); ++i) | |
1342 | { | |
1343 | derived_probe* p = q->results[i]; | |
1344 | probe_point* pp = new probe_point(*p->locations[0]); | |
1345 | pp->components.push_back (ppc); | |
1346 | p->base = p->base->create_alias(p->locations[0], pp); | |
1347 | } | |
8096dd7d JS |
1348 | } |
1349 | ||
7e1279ea | 1350 | static void |
3e961ba6 | 1351 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
1352 | dwarf_query * q) |
1353 | { | |
b6581717 | 1354 | try |
7e1279ea | 1355 | { |
8f14e444 FCE |
1356 | assert (! q->has_return); // checked by caller already |
1357 | if (q->sess.verbose>2) | |
1358 | clog << "querying entrypc " | |
1359 | << hex << ii.entrypc << dec | |
1360 | << " of instance of inline '" << ii.name << "'\n"; | |
1361 | query_statement (ii.name, ii.decl_file, ii.decl_line, | |
1362 | &ii.die, ii.entrypc, q); | |
7e1279ea | 1363 | } |
b6581717 | 1364 | catch (semantic_error &e) |
7e1279ea | 1365 | { |
b6581717 | 1366 | q->sess.print_error (e); |
7e1279ea FCE |
1367 | } |
1368 | } | |
1369 | ||
1370 | static void | |
1371 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 1372 | func_info & fi, |
7e1279ea FCE |
1373 | dwarf_query * q) |
1374 | { | |
b6581717 | 1375 | try |
7e1279ea | 1376 | { |
b6581717 GH |
1377 | if (q->has_return) |
1378 | { | |
1379 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
1380 | // kretprobe based on the entrypc in this case. | |
20e4a32c | 1381 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
1382 | &fi.die, entrypc, q); |
1383 | } | |
1384 | else | |
1385 | { | |
35dc8b04 | 1386 | if (fi.prologue_end != 0) |
44f75386 | 1387 | { |
44f75386 FCE |
1388 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1389 | &fi.die, fi.prologue_end, q); | |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
1394 | &fi.die, entrypc, q); | |
1395 | } | |
b6581717 | 1396 | } |
7e1279ea | 1397 | } |
b6581717 | 1398 | catch (semantic_error &e) |
7e1279ea | 1399 | { |
b6581717 | 1400 | q->sess.print_error (e); |
7e1279ea FCE |
1401 | } |
1402 | } | |
1403 | ||
1404 | ||
bd4b874d SC |
1405 | static void |
1406 | query_srcfile_label (const dwarf_line_t& line, void * arg) | |
1407 | { | |
1408 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1409 | ||
1410 | Dwarf_Addr addr = line.addr(); | |
1411 | ||
1412 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1413 | i != q->filtered_functions.end(); ++i) | |
1414 | if (q->dw.die_has_pc (i->die, addr)) | |
f09d0d1e JS |
1415 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, |
1416 | q, query_label); | |
1417 | ||
1418 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1419 | i != q->filtered_inlines.end(); ++i) | |
1420 | if (q->dw.die_has_pc (i->die, addr)) | |
1421 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1422 | q, query_label); | |
bd4b874d SC |
1423 | } |
1424 | ||
7e1279ea | 1425 | static void |
86bf665e | 1426 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
1427 | { |
1428 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1429 | ||
86bf665e | 1430 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 1431 | |
86bf665e | 1432 | int lineno = line.lineno(); |
847bf07f | 1433 | |
86bf665e | 1434 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
1435 | i != q->filtered_functions.end(); ++i) |
1436 | { | |
3e961ba6 | 1437 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1438 | { |
b0ee93c4 | 1439 | if (q->sess.verbose>3) |
db22e55f | 1440 | clog << "function DIE lands on srcfile\n"; |
4cd232e4 | 1441 | if (q->has_statement_str) |
3e961ba6 | 1442 | query_statement (i->name, i->decl_file, |
847bf07f | 1443 | lineno, // NB: not q->line ! |
3e961ba6 | 1444 | &(i->die), addr, q); |
4cd232e4 | 1445 | else |
3e961ba6 | 1446 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 1447 | } |
20e4a32c RM |
1448 | } |
1449 | ||
86bf665e | 1450 | for (inline_instance_map_t::iterator i |
897820ca GH |
1451 | = q->filtered_inlines.begin(); |
1452 | i != q->filtered_inlines.end(); ++i) | |
1453 | { | |
3e961ba6 | 1454 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1455 | { |
b0ee93c4 | 1456 | if (q->sess.verbose>3) |
db22e55f | 1457 | clog << "inline instance DIE lands on srcfile\n"; |
897820ca | 1458 | if (q->has_statement_str) |
3e961ba6 JB |
1459 | query_statement (i->name, i->decl_file, |
1460 | q->line[0], &(i->die), addr, q); | |
897820ca | 1461 | else |
3e961ba6 | 1462 | query_inline_instance_info (*i, q); |
897820ca | 1463 | } |
20e4a32c | 1464 | } |
7e1279ea FCE |
1465 | } |
1466 | ||
1467 | ||
7fdd3e2c JS |
1468 | bool |
1469 | inline_instance_info::operator<(const inline_instance_info& other) const | |
1470 | { | |
1471 | if (entrypc != other.entrypc) | |
1472 | return entrypc < other.entrypc; | |
1473 | ||
1474 | if (decl_line != other.decl_line) | |
1475 | return decl_line < other.decl_line; | |
1476 | ||
1477 | int cmp = name.compare(other.name); | |
1478 | if (!cmp) | |
1479 | cmp = strcmp(decl_file, other.decl_file); | |
1480 | return cmp < 0; | |
1481 | } | |
1482 | ||
1483 | ||
4fa7b22b | 1484 | static int |
7e1279ea | 1485 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
1486 | { |
1487 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
6b517475 JS |
1488 | assert (q->has_statement_str || q->has_function_str); |
1489 | assert (!q->has_call && !q->has_return); | |
bd2b1e68 | 1490 | |
39bcd429 | 1491 | try |
7a053d3b | 1492 | { |
b0ee93c4 | 1493 | if (q->sess.verbose>2) |
6b517475 | 1494 | clog << "selected inline instance of " << q->dw.function_name << "\n"; |
7e1279ea | 1495 | |
6b517475 JS |
1496 | Dwarf_Addr entrypc; |
1497 | if (q->dw.die_entrypc (die, &entrypc)) | |
1498 | { | |
1499 | inline_instance_info inl; | |
1500 | inl.die = *die; | |
1501 | inl.name = q->dw.function_name; | |
1502 | inl.entrypc = entrypc; | |
1503 | q->dw.function_file (&inl.decl_file); | |
1504 | q->dw.function_line (&inl.decl_line); | |
1505 | ||
1506 | // make sure that this inline hasn't already | |
1507 | // been matched from a different CU | |
1508 | if (q->inline_dupes.insert(inl).second) | |
1509 | q->filtered_inlines.push_back(inl); | |
1510 | } | |
7e1279ea FCE |
1511 | return DWARF_CB_OK; |
1512 | } | |
1513 | catch (const semantic_error& e) | |
1514 | { | |
1515 | q->sess.print_error (e); | |
1516 | return DWARF_CB_ABORT; | |
1517 | } | |
1518 | } | |
bb788f9f | 1519 | |
7e1279ea | 1520 | static int |
2da9cedb | 1521 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 1522 | { |
2da9cedb | 1523 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
6b517475 | 1524 | assert (q->has_statement_str || q->has_function_str); |
bb788f9f | 1525 | |
bd25380d JS |
1526 | // weed out functions whose decl_file isn't one of |
1527 | // the source files that we actually care about | |
6b517475 | 1528 | if (q->spec_type != function_alone && |
bd25380d | 1529 | q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0) |
8096dd7d | 1530 | return DWARF_CB_OK; |
bd25380d | 1531 | |
7e1279ea FCE |
1532 | try |
1533 | { | |
7e1279ea FCE |
1534 | q->dw.focus_on_function (func); |
1535 | ||
7d6d0afc JS |
1536 | if (!q->dw.function_scope_matches(q->scopes)) |
1537 | return DWARF_CB_OK; | |
1538 | ||
857bdfd1 JS |
1539 | // make sure that this function address hasn't |
1540 | // already been matched under an aliased name | |
1541 | Dwarf_Addr addr; | |
1542 | if (!q->dw.func_is_inline() && | |
1543 | dwarf_entrypc(func, &addr) == 0 && | |
1544 | !q->alias_dupes.insert(addr).second) | |
1545 | return DWARF_CB_OK; | |
1546 | ||
6b517475 | 1547 | if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return)) |
7e1279ea | 1548 | { |
b0ee93c4 | 1549 | if (q->sess.verbose>3) |
db22e55f FCE |
1550 | clog << "checking instances of inline " << q->dw.function_name |
1551 | << "\n"; | |
2da9cedb | 1552 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
7e1279ea | 1553 | } |
8f14e444 FCE |
1554 | else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553 |
1555 | { | |
1556 | q->inlined_non_returnable.insert (q->dw.function_name); | |
1557 | } | |
396afcee | 1558 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 1559 | { |
6b517475 JS |
1560 | if (q->sess.verbose>2) |
1561 | clog << "selected function " << q->dw.function_name << "\n"; | |
1562 | ||
1563 | func_info func; | |
1564 | q->dw.function_die (&func.die); | |
1565 | func.name = q->dw.function_name; | |
1566 | q->dw.function_file (&func.decl_file); | |
1567 | q->dw.function_line (&func.decl_line); | |
1568 | ||
1569 | Dwarf_Addr entrypc; | |
1570 | if (q->dw.function_entrypc (&entrypc)) | |
1571 | { | |
1572 | func.entrypc = entrypc; | |
1573 | q->filtered_functions.push_back (func); | |
1574 | } | |
1575 | /* else this function is fully inlined, just ignore it */ | |
7e1279ea | 1576 | } |
39bcd429 | 1577 | return DWARF_CB_OK; |
bd2b1e68 | 1578 | } |
39bcd429 | 1579 | catch (const semantic_error& e) |
bd2b1e68 | 1580 | { |
39bcd429 FCE |
1581 | q->sess.print_error (e); |
1582 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1583 | } |
bd2b1e68 GH |
1584 | } |
1585 | ||
1586 | static int | |
1587 | query_cu (Dwarf_Die * cudie, void * arg) | |
1588 | { | |
20c6c071 | 1589 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
6b517475 JS |
1590 | assert (q->has_statement_str || q->has_function_str); |
1591 | ||
aa4d21c0 | 1592 | if (systemtap_session::pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 1593 | |
39bcd429 | 1594 | try |
bd2b1e68 | 1595 | { |
7e1279ea | 1596 | q->dw.focus_on_cu (cudie); |
b5d77020 | 1597 | |
b0ee93c4 | 1598 | if (false && q->sess.verbose>2) |
54417494 | 1599 | clog << "focused on CU '" << q->dw.cu_name() |
db22e55f | 1600 | << "', in module '" << q->dw.module_name << "'\n"; |
d9b516ca | 1601 | |
6b517475 JS |
1602 | q->filtered_srcfiles.clear(); |
1603 | q->filtered_functions.clear(); | |
1604 | q->filtered_inlines.clear(); | |
1605 | ||
1606 | // In this path, we find "abstract functions", record | |
1607 | // information about them, and then (depending on lineno | |
1608 | // matching) possibly emit one or more of the function's | |
1609 | // associated addresses. Unfortunately the control of this | |
1610 | // cannot easily be turned inside out. | |
1611 | ||
1612 | if (q->spec_type != function_alone) | |
39bcd429 | 1613 | { |
6b517475 JS |
1614 | // If we have a pattern string with a filename, we need |
1615 | // to elaborate the srcfile mask in question first. | |
1616 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
1617 | ||
1618 | // If we have a file pattern and *no* srcfile matches, there's | |
1619 | // no need to look further into this CU, so skip. | |
1620 | if (q->filtered_srcfiles.empty()) | |
1621 | return DWARF_CB_OK; | |
1622 | } | |
e4c58386 | 1623 | |
6b517475 JS |
1624 | // Pick up [entrypc, name, DIE] tuples for all the functions |
1625 | // matching the query, and fill in the prologue endings of them | |
1626 | // all in a single pass. | |
5898b6e1 | 1627 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function); |
6b517475 JS |
1628 | if (rc != DWARF_CB_OK) |
1629 | q->query_done = true; | |
1630 | ||
1631 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 | |
1632 | && !q->has_statement_str) // PR 2608 | |
1633 | if (! q->filtered_functions.empty()) | |
1634 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
1635 | ||
1636 | if (q->spec_type == function_file_and_line) | |
1637 | { | |
1638 | // If we have a pattern string with target *line*, we | |
1639 | // have to look at lines in all the matched srcfiles. | |
1640 | void (* callback) (const dwarf_line_t&, void*) = | |
1641 | q->has_label ? query_srcfile_label : query_srcfile_line; | |
1642 | for (set<string>::const_iterator i = q->filtered_srcfiles.begin(); | |
1643 | i != q->filtered_srcfiles.end(); ++i) | |
1644 | q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str, | |
1645 | q->line_type, callback, q->function, q); | |
1646 | } | |
1647 | else if (q->has_label) | |
1648 | { | |
1649 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1650 | i != q->filtered_functions.end(); ++i) | |
1651 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1652 | q, query_label); | |
1653 | ||
1654 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1655 | i != q->filtered_inlines.end(); ++i) | |
1656 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1657 | q, query_label); | |
39bcd429 | 1658 | } |
6b517475 JS |
1659 | else |
1660 | { | |
1661 | // Otherwise, simply probe all resolved functions. | |
1662 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1663 | i != q->filtered_functions.end(); ++i) | |
1664 | query_func_info (i->entrypc, *i, q); | |
1665 | ||
1666 | // And all inline instances (if we're not excluding inlines with ".call") | |
1667 | if (! q->has_call) | |
1668 | for (inline_instance_map_t::iterator i | |
1669 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) | |
1670 | query_inline_instance_info (*i, q); | |
1671 | } | |
39bcd429 | 1672 | return DWARF_CB_OK; |
bd2b1e68 | 1673 | } |
39bcd429 | 1674 | catch (const semantic_error& e) |
bd2b1e68 | 1675 | { |
39bcd429 FCE |
1676 | q->sess.print_error (e); |
1677 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1678 | } |
bd2b1e68 GH |
1679 | } |
1680 | ||
0ce64fb8 | 1681 | |
4df79aaf JS |
1682 | void |
1683 | dwarf_query::query_module_functions () | |
1684 | { | |
1685 | try | |
1686 | { | |
1687 | filtered_srcfiles.clear(); | |
1688 | filtered_functions.clear(); | |
1689 | filtered_inlines.clear(); | |
1690 | ||
1691 | // Collect all module functions so we know which CUs are interesting | |
1692 | int rc = dw.iterate_single_function(query_dwarf_func, this, function); | |
1693 | if (rc != DWARF_CB_OK) | |
1694 | { | |
1695 | query_done = true; | |
1696 | return; | |
1697 | } | |
1698 | ||
1699 | set<void*> used_cus; // by cu->addr | |
1700 | vector<Dwarf_Die> cus; | |
1701 | Dwarf_Die cu_mem; | |
1702 | ||
1703 | for (func_info_map_t::iterator i = filtered_functions.begin(); | |
1704 | i != filtered_functions.end(); ++i) | |
1705 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1706 | used_cus.insert(cu_mem.addr).second) | |
1707 | cus.push_back(cu_mem); | |
1708 | ||
1709 | for (inline_instance_map_t::iterator i = filtered_inlines.begin(); | |
1710 | i != filtered_inlines.end(); ++i) | |
1711 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1712 | used_cus.insert(cu_mem.addr).second) | |
1713 | cus.push_back(cu_mem); | |
1714 | ||
1715 | // Reset the dupes since we didn't actually collect them the first time | |
1716 | alias_dupes.clear(); | |
1717 | inline_dupes.clear(); | |
1718 | ||
1719 | // Run the query again on the individual CUs | |
1720 | for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i) | |
1721 | query_cu(&*i, this); | |
1722 | } | |
1723 | catch (const semantic_error& e) | |
1724 | { | |
1725 | sess.print_error (e); | |
1726 | } | |
1727 | } | |
1728 | ||
1729 | ||
5f0a03a6 JK |
1730 | static void |
1731 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
1732 | { | |
1733 | // Validate the machine code in this elf file against the | |
1734 | // session machine. This is important, in case the wrong kind | |
1735 | // of debuginfo is being automagically processed by elfutils. | |
1736 | // While we can tell i686 apart from x86-64, unfortunately | |
1737 | // we can't help confusing i586 vs i686 (both EM_386). | |
1738 | ||
1739 | Dwarf_Addr bias; | |
1740 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
1741 | // because dwfl_module_getelf can force costly section relocations | |
1742 | // we don't really need, while either will do for this purpose. | |
1743 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
1744 | ?: dwfl_module_getelf (mod, &bias)); | |
1745 | ||
1746 | GElf_Ehdr ehdr_mem; | |
1747 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 1748 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
5f0a03a6 JK |
1749 | int elf_machine = em->e_machine; |
1750 | const char* debug_filename = ""; | |
1751 | const char* main_filename = ""; | |
1752 | (void) dwfl_module_info (mod, NULL, NULL, | |
1753 | NULL, NULL, NULL, | |
1754 | & main_filename, | |
1755 | & debug_filename); | |
1756 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
1757 | |
1758 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
1759 | string expect_machine2; | |
5f0a03a6 | 1760 | |
d27e6fd5 | 1761 | // NB: See also the 'uname -m' squashing done in main.cxx. |
5f0a03a6 JK |
1762 | switch (elf_machine) |
1763 | { | |
756c9462 FCE |
1764 | // x86 and ppc are bi-architecture; a 64-bit kernel |
1765 | // can normally run either 32-bit or 64-bit *userspace*. | |
1766 | case EM_386: | |
1767 | expect_machine = "i?86"; | |
1768 | if (! q->has_process) break; // 32-bit kernel/module | |
1769 | /* FALLSTHROUGH */ | |
1770 | case EM_X86_64: | |
1771 | expect_machine2 = "x86_64"; | |
1772 | break; | |
1773 | case EM_PPC: | |
756c9462 | 1774 | case EM_PPC64: |
5a1c472e | 1775 | expect_machine = "powerpc"; |
756c9462 | 1776 | break; |
3fe7d888 | 1777 | case EM_S390: expect_machine = "s390"; break; |
5f0a03a6 | 1778 | case EM_IA_64: expect_machine = "ia64"; break; |
d27e6fd5 | 1779 | case EM_ARM: expect_machine = "arm*"; break; |
5f0a03a6 JK |
1780 | // XXX: fill in some more of these |
1781 | default: expect_machine = "?"; break; | |
1782 | } | |
1783 | ||
1784 | if (! debug_filename) debug_filename = main_filename; | |
1785 | if (! debug_filename) debug_filename = name; | |
1786 | ||
756c9462 FCE |
1787 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
1788 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
1789 | { |
1790 | stringstream msg; | |
756c9462 FCE |
1791 | msg << "ELF machine " << expect_machine << "|" << expect_machine2 |
1792 | << " (code " << elf_machine | |
5f0a03a6 JK |
1793 | << ") mismatch with target " << sess_machine |
1794 | << " in '" << debug_filename << "'"; | |
1795 | throw semantic_error(msg.str ()); | |
1796 | } | |
1797 | ||
1798 | if (q->sess.verbose>2) | |
1799 | clog << "focused on module '" << q->dw.module_name | |
1800 | << " = [0x" << hex << q->dw.module_start | |
1801 | << "-0x" << q->dw.module_end | |
1802 | << ", bias 0x" << q->dw.module_bias << "]" << dec | |
1803 | << " file " << debug_filename | |
756c9462 | 1804 | << " ELF machine " << expect_machine << "|" << expect_machine2 |
5f0a03a6 JK |
1805 | << " (code " << elf_machine << ")" |
1806 | << "\n"; | |
1807 | } | |
1d3a40b6 | 1808 | |
91af0778 FCE |
1809 | |
1810 | ||
1811 | static Dwarf_Addr | |
1812 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
1813 | { | |
1814 | int syments = dwfl_module_getsymtab(m); | |
1815 | assert(syments); | |
1816 | for (int i = 1; i < syments; ++i) | |
1817 | { | |
1818 | GElf_Sym sym; | |
1819 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
1820 | if (name != NULL && strcmp(name, wanted) == 0) | |
1821 | return sym.st_value; | |
1822 | } | |
1823 | ||
1824 | return 0; | |
1825 | } | |
1826 | ||
1827 | ||
1828 | ||
bd2b1e68 | 1829 | static int |
b8da0ad1 | 1830 | query_module (Dwfl_Module *mod, |
91af0778 | 1831 | void **, |
b8da0ad1 | 1832 | const char *name, |
6f4c1275 | 1833 | Dwarf_Addr addr, |
b8da0ad1 | 1834 | void *arg) |
bd2b1e68 | 1835 | { |
91af0778 | 1836 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 1837 | |
39bcd429 | 1838 | try |
e38d6504 | 1839 | { |
91af0778 FCE |
1840 | module_info* mi = q->sess.module_cache->cache[name]; |
1841 | if (mi == 0) | |
1842 | { | |
1843 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
1844 | ||
6f4c1275 FCE |
1845 | mi->mod = mod; |
1846 | mi->addr = addr; | |
91af0778 | 1847 | |
6f4c1275 FCE |
1848 | const char* debug_filename = ""; |
1849 | const char* main_filename = ""; | |
1850 | (void) dwfl_module_info (mod, NULL, NULL, | |
1851 | NULL, NULL, NULL, | |
1852 | & main_filename, | |
1853 | & debug_filename); | |
1854 | ||
1855 | if (q->sess.ignore_vmlinux && name == TOK_KERNEL) | |
91af0778 FCE |
1856 | { |
1857 | // report_kernel() in elfutils found vmlinux, but pretend it didn't. | |
1858 | // Given a non-null path, returning 1 means keep reporting modules. | |
1859 | mi->dwarf_status = info_absent; | |
1860 | } | |
6f4c1275 | 1861 | else if (debug_filename || main_filename) |
91af0778 | 1862 | { |
6f4c1275 FCE |
1863 | mi->elf_path = debug_filename ?: main_filename; |
1864 | } | |
1865 | else if (name == TOK_KERNEL) | |
1866 | { | |
1867 | mi->dwarf_status = info_absent; | |
91af0778 | 1868 | } |
91af0778 FCE |
1869 | } |
1870 | // OK, enough of that module_info caching business. | |
1871 | ||
5f0a03a6 | 1872 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 1873 | |
39bcd429 FCE |
1874 | // If we have enough information in the pattern to skip a module and |
1875 | // the module does not match that information, return early. | |
b8da0ad1 | 1876 | if (!q->dw.module_name_matches(q->module_val)) |
aa4d21c0 | 1877 | return systemtap_session::pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
0cbbf9d1 FCE |
1878 | |
1879 | // Don't allow module("*kernel*") type expressions to match the | |
1880 | // elfutils module "kernel", which we refer to in the probe | |
1881 | // point syntax exclusively as "kernel.*". | |
1882 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
aa4d21c0 | 1883 | return systemtap_session::pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
b5d77020 | 1884 | |
5f0a03a6 JK |
1885 | if (mod) |
1886 | validate_module_elf(mod, name, q); | |
1887 | else | |
91af0778 FCE |
1888 | assert(q->has_kernel); // and no vmlinux to examine |
1889 | ||
1890 | if (q->sess.verbose>2) | |
1891 | cerr << "focused on module '" << q->dw.module_name << "'\n"; | |
1892 | ||
1893 | ||
1894 | // Collect a few kernel addresses. XXX: these belong better | |
1895 | // to the sess.module_info["kernel"] struct. | |
1896 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 1897 | { |
91af0778 FCE |
1898 | if (! q->sess.sym_kprobes_text_start) |
1899 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
1900 | if (! q->sess.sym_kprobes_text_end) | |
1901 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
1902 | if (! q->sess.sym_stext) | |
1903 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
1904 | } |
1905 | ||
91af0778 | 1906 | // Finally, search the module for matches of the probe point. |
2c384610 | 1907 | q->handle_query_module(); |
bb788f9f | 1908 | |
91af0778 | 1909 | |
b8da0ad1 | 1910 | // If we know that there will be no more matches, abort early. |
aa4d21c0 | 1911 | if (q->dw.module_name_final_match(q->module_val) || systemtap_session::pending_interrupts) |
b8da0ad1 FCE |
1912 | return DWARF_CB_ABORT; |
1913 | else | |
1914 | return DWARF_CB_OK; | |
7a053d3b | 1915 | } |
39bcd429 | 1916 | catch (const semantic_error& e) |
bd2b1e68 | 1917 | { |
39bcd429 FCE |
1918 | q->sess.print_error (e); |
1919 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1920 | } |
bd2b1e68 GH |
1921 | } |
1922 | ||
35d4ab18 | 1923 | |
de688825 | 1924 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 1925 | { |
77de5e9e | 1926 | dwarf_query & q; |
bcc12710 | 1927 | Dwarf_Die *scope_die; |
77de5e9e | 1928 | Dwarf_Addr addr; |
8c819921 | 1929 | block *add_block; |
2260f4e3 | 1930 | block *add_call_probe; // synthesized from .return probes with saved $vars |
af234c40 JS |
1931 | unsigned saved_longs, saved_strings; // data saved within kretprobes |
1932 | map<std::string, expression *> return_ts_map; | |
729455a7 | 1933 | vector<Dwarf_Die> scopes; |
b95e2b79 | 1934 | bool visited; |
77de5e9e | 1935 | |
de688825 | 1936 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
af234c40 JS |
1937 | q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL), |
1938 | saved_longs(0), saved_strings(0), visited(false) {} | |
277c21bc JS |
1939 | expression* gen_mapped_saved_return(expression* e, const string& name); |
1940 | expression* gen_kretprobe_saved_return(expression* e, const string& name); | |
a7999c82 JS |
1941 | void visit_target_symbol_saved_return (target_symbol* e); |
1942 | void visit_target_symbol_context (target_symbol* e); | |
d7f3e0c5 | 1943 | void visit_target_symbol (target_symbol* e); |
c24447be | 1944 | void visit_cast_op (cast_op* e); |
729455a7 JS |
1945 | private: |
1946 | vector<Dwarf_Die>& getscopes(target_symbol *e); | |
77de5e9e GH |
1947 | }; |
1948 | ||
1949 | ||
de688825 | 1950 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 1951 | |
a50de939 DS |
1952 | |
1953 | var_expanding_visitor::var_expanding_visitor () | |
1954 | { | |
1955 | // FIXME: for the time being, by default we only support plain '$foo | |
1956 | // = bar', not '+=' or any other op= variant. This is fixable, but a | |
1957 | // bit ugly. | |
1958 | // | |
1959 | // If derived classes desire to add additional operator support, add | |
1960 | // new operators to this list in the derived class constructor. | |
1961 | valid_ops.insert ("="); | |
1962 | } | |
1963 | ||
1964 | ||
87214add JS |
1965 | bool |
1966 | var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop, | |
1967 | expression*& lvalue, expression*& rvalue) | |
77de5e9e | 1968 | { |
e57b735a GH |
1969 | // Our job would normally be to require() the left and right sides |
1970 | // into a new assignment. What we're doing is slightly trickier: | |
1971 | // we're pushing a functioncall** onto a stack, and if our left | |
1972 | // child sets the functioncall* for that value, we're going to | |
1973 | // assume our left child was a target symbol -- transformed into a | |
1974 | // set_target_foo(value) call, and it wants to take our right child | |
1975 | // as the argument "value". | |
1976 | // | |
1977 | // This is why some people claim that languages with | |
1978 | // constructor-decomposing case expressions have a leg up on | |
1979 | // visitors. | |
1980 | ||
1981 | functioncall *fcall = NULL; | |
d9b516ca | 1982 | |
a50de939 | 1983 | // Let visit_target_symbol know what operator it should handle. |
87214add JS |
1984 | const string* old_op = op; |
1985 | op = &eop; | |
a50de939 | 1986 | |
e57b735a | 1987 | target_symbol_setter_functioncalls.push (&fcall); |
87214add | 1988 | replace (lvalue); |
e57b735a | 1989 | target_symbol_setter_functioncalls.pop (); |
87214add JS |
1990 | replace (rvalue); |
1991 | ||
1992 | op = old_op; | |
e57b735a GH |
1993 | |
1994 | if (fcall != NULL) | |
77de5e9e | 1995 | { |
e57b735a GH |
1996 | // Our left child is informing us that it was a target variable |
1997 | // and it has been replaced with a set_target_foo() function | |
1998 | // call; we are going to provide that function call -- with the | |
1999 | // right child spliced in as sole argument -- in place of | |
de688825 | 2000 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a | 2001 | |
87214add | 2002 | if (valid_ops.find (eop) == valid_ops.end ()) |
a50de939 DS |
2003 | { |
2004 | // Build up a list of supported operators. | |
2005 | string ops; | |
2006 | std::set<string>::iterator i; | |
2007 | for (i = valid_ops.begin(); i != valid_ops.end(); i++) | |
2008 | ops += " " + *i + ","; | |
2009 | ops.resize(ops.size() - 1); // chop off the last ',' | |
2010 | ||
2011 | // Throw the error. | |
2012 | throw semantic_error ("Only the following assign operators are" | |
2013 | " implemented on target variables:" + ops, | |
87214add | 2014 | tok); |
a50de939 | 2015 | } |
e57b735a | 2016 | |
87214add JS |
2017 | assert (lvalue == fcall); |
2018 | if (rvalue) | |
2019 | fcall->args.push_back (rvalue); | |
4ed05b15 | 2020 | provide (fcall); |
87214add | 2021 | return true; |
77de5e9e | 2022 | } |
e57b735a | 2023 | else |
87214add JS |
2024 | return false; |
2025 | } | |
2026 | ||
2027 | ||
2028 | void | |
2029 | var_expanding_visitor::visit_assignment (assignment* e) | |
2030 | { | |
2031 | if (!rewrite_lvalue (e->tok, e->op, e->left, e->right)) | |
2032 | provide (e); | |
2033 | } | |
2034 | ||
2035 | ||
2036 | void | |
2037 | var_expanding_visitor::visit_pre_crement (pre_crement* e) | |
2038 | { | |
2039 | expression *dummy = NULL; | |
2040 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2041 | provide (e); | |
2042 | } | |
2043 | ||
2044 | ||
2045 | void | |
2046 | var_expanding_visitor::visit_post_crement (post_crement* e) | |
2047 | { | |
2048 | expression *dummy = NULL; | |
2049 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2050 | provide (e); | |
2051 | } | |
2052 | ||
2053 | ||
2054 | void | |
2055 | var_expanding_visitor::visit_delete_statement (delete_statement* s) | |
2056 | { | |
2057 | string fakeop = "delete"; | |
2058 | expression *dummy = NULL; | |
2059 | if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy)) | |
2060 | provide (s); | |
e57b735a | 2061 | } |
d9b516ca | 2062 | |
d7f3e0c5 | 2063 | |
30263a73 FCE |
2064 | void |
2065 | var_expanding_visitor::visit_defined_op (defined_op* e) | |
2066 | { | |
2067 | bool resolved = true; | |
2068 | ||
2069 | defined_ops.push (e); | |
2070 | try { | |
2071 | // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is | |
2072 | // defined with a target_symbol* operand, a subsidiary call may attempt to | |
2073 | // rewrite it to a general expression* instead, and require<> happily | |
2074 | // casts to/from void*, causing possible memory corruption. We use | |
2075 | // expression* here, being the general case of rewritten $variable. | |
2076 | expression *foo1 = e->operand; | |
2077 | foo1 = require (foo1); | |
2078 | ||
c69a87e0 | 2079 | // NB: Formerly, we had some curious cases to consider here, depending on what |
30263a73 | 2080 | // various visit_target_symbol() implementations do for successful or |
c69a87e0 FCE |
2081 | // erroneous resolutions. Some would signal a visit_target_symbol failure |
2082 | // with an exception, with a set flag within the target_symbol, or nothing | |
2083 | // at all. | |
30263a73 | 2084 | // |
c69a87e0 FCE |
2085 | // Now, failures always have to be signalled with a |
2086 | // saved_conversion_error being chained to the target_symbol. | |
2087 | // Successes have to result in an attempted rewrite of the | |
850bfddd | 2088 | // target_symbol (via provide()). |
780f11ff | 2089 | // |
c69a87e0 FCE |
2090 | // Edna Mode: "no capes". fche: "no exceptions". |
2091 | ||
30263a73 FCE |
2092 | // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error |
2093 | // | |
2094 | // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error | |
2095 | // | |
2096 | // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic | |
2097 | // dwarf probe to take care of it. | |
2098 | // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor | |
2099 | // to pass through @defined() to the synthetic dwarf probe. | |
780f11ff | 2100 | // |
30263a73 FCE |
2101 | // utrace: success: rewrites to function; failure: semantic_error |
2102 | // | |
850bfddd | 2103 | // procfs: success: rewrites to function; failure: semantic_error |
30263a73 FCE |
2104 | |
2105 | target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1); | |
c69a87e0 | 2106 | if (foo2 && foo2->saved_conversion_error) // failing |
30263a73 | 2107 | resolved = false; |
a45664f4 | 2108 | else if (foo2) // unresolved but not marked failing |
b7aedf26 | 2109 | { |
780f11ff JS |
2110 | // There are some visitors that won't touch certain target_symbols, |
2111 | // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should | |
2112 | // leave it for now so some other visitor can have a chance. | |
b7aedf26 JS |
2113 | e->operand = foo2; |
2114 | provide (e); | |
2115 | return; | |
2116 | } | |
30263a73 FCE |
2117 | else // resolved, rewritten to some other expression type |
2118 | resolved = true; | |
780f11ff | 2119 | } catch (const semantic_error& e) { |
c69a87e0 | 2120 | assert (0); // should not happen |
30263a73 FCE |
2121 | } |
2122 | defined_ops.pop (); | |
2123 | ||
2124 | literal_number* ln = new literal_number (resolved ? 1 : 0); | |
2125 | ln->tok = e->tok; | |
2126 | provide (ln); | |
2127 | } | |
2128 | ||
2129 | ||
5f36109e JS |
2130 | struct dwarf_pretty_print |
2131 | { | |
2132 | dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc, | |
2133 | const string& local, bool userspace_p, | |
2134 | const target_symbol& e): | |
2135 | dw(dw), local(local), scopes(scopes), pc(pc), | |
2136 | pointer(NULL), userspace_p(userspace_p) | |
2137 | { | |
2138 | init_ts (e); | |
2139 | dw.type_die_for_local (scopes, pc, local, ts, &base_type); | |
2140 | } | |
2141 | ||
2142 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc, | |
2143 | bool userspace_p, const target_symbol& e): | |
2144 | dw(dw), scopes(1, *scope_die), pc(pc), | |
2145 | pointer(NULL), userspace_p(userspace_p) | |
2146 | { | |
2147 | init_ts (e); | |
2148 | dw.type_die_for_return (&scopes[0], pc, ts, &base_type); | |
2149 | } | |
2150 | ||
2151 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer, | |
2152 | bool userspace_p, const target_symbol& e): | |
2153 | dw(dw), pc(0), pointer(pointer), pointer_type(*type_die), | |
2154 | userspace_p(userspace_p) | |
2155 | { | |
2156 | init_ts (e); | |
2157 | dw.type_die_for_pointer (type_die, ts, &base_type); | |
2158 | } | |
2159 | ||
2160 | functioncall* expand (); | |
2161 | ||
2162 | private: | |
2163 | dwflpp& dw; | |
2164 | target_symbol* ts; | |
7d11d8c9 | 2165 | bool print_full; |
5f36109e JS |
2166 | Dwarf_Die base_type; |
2167 | ||
2168 | string local; | |
2169 | vector<Dwarf_Die> scopes; | |
2170 | Dwarf_Addr pc; | |
2171 | ||
2172 | expression* pointer; | |
2173 | Dwarf_Die pointer_type; | |
2174 | ||
2175 | const bool userspace_p; | |
2176 | ||
2177 | void recurse (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2178 | print_format* pf, bool top=false); |
5f36109e | 2179 | void recurse_base (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2180 | print_format* pf); |
5f36109e | 2181 | void recurse_array (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2182 | print_format* pf, bool top); |
5f36109e | 2183 | void recurse_pointer (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2184 | print_format* pf, bool top); |
5f36109e | 2185 | void recurse_struct (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2186 | print_format* pf, bool top); |
5f36109e | 2187 | void recurse_struct_members (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2188 | print_format* pf, int& count); |
bbee5bb8 | 2189 | bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf); |
5f36109e JS |
2190 | |
2191 | void init_ts (const target_symbol& e); | |
2192 | expression* deref (target_symbol* e); | |
2193 | }; | |
2194 | ||
2195 | ||
2196 | void | |
2197 | dwarf_pretty_print::init_ts (const target_symbol& e) | |
2198 | { | |
2199 | // Work with a new target_symbol so we can modify arguments | |
2200 | ts = new target_symbol (e); | |
2201 | ||
2202 | if (ts->addressof) | |
2203 | throw semantic_error("cannot take address of pretty-printed variable", ts->tok); | |
2204 | ||
2205 | if (ts->components.empty() || | |
2206 | ts->components.back().type != target_symbol::comp_pretty_print) | |
2207 | throw semantic_error("invalid target_symbol for pretty-print", ts->tok); | |
7d11d8c9 | 2208 | print_full = ts->components.back().member.length() > 1; |
5f36109e JS |
2209 | ts->components.pop_back(); |
2210 | } | |
2211 | ||
2212 | ||
2213 | functioncall* | |
2214 | dwarf_pretty_print::expand () | |
2215 | { | |
2216 | static unsigned tick = 0; | |
2217 | ||
2218 | // function pretty_print_X([pointer], [arg1, arg2, ...]) { | |
7d11d8c9 JS |
2219 | // try { |
2220 | // return sprintf("{.foo=...}", (ts)->foo, ...) | |
2221 | // } catch { | |
2222 | // return "ERROR" | |
2223 | // } | |
5f36109e JS |
2224 | // } |
2225 | ||
2226 | // Create the function decl and call. | |
2227 | ||
2228 | functiondecl *fdecl = new functiondecl; | |
2229 | fdecl->tok = ts->tok; | |
2230 | fdecl->synthetic = true; | |
2231 | fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++); | |
2232 | fdecl->type = pe_string; | |
2233 | ||
2234 | functioncall* fcall = new functioncall; | |
2235 | fcall->tok = ts->tok; | |
2236 | fcall->function = fdecl->name; | |
5f36109e JS |
2237 | |
2238 | // If there's a <pointer>, replace it with a new var and make that | |
2239 | // the first function argument. | |
2240 | if (pointer) | |
2241 | { | |
2242 | vardecl *v = new vardecl; | |
2243 | v->type = pe_long; | |
2244 | v->name = "pointer"; | |
2245 | v->tok = ts->tok; | |
2246 | fdecl->formal_args.push_back (v); | |
2247 | fcall->args.push_back (pointer); | |
2248 | ||
2249 | symbol* sym = new symbol; | |
2250 | sym->tok = ts->tok; | |
2251 | sym->name = v->name; | |
5f36109e JS |
2252 | pointer = sym; |
2253 | } | |
2254 | ||
2255 | // For each expression argument, replace it with a function argument. | |
2256 | for (unsigned i = 0; i < ts->components.size(); ++i) | |
2257 | if (ts->components[i].type == target_symbol::comp_expression_array_index) | |
2258 | { | |
2259 | vardecl *v = new vardecl; | |
2260 | v->type = pe_long; | |
2261 | v->name = "index" + lex_cast(i); | |
2262 | v->tok = ts->tok; | |
2263 | fdecl->formal_args.push_back (v); | |
2264 | fcall->args.push_back (ts->components[i].expr_index); | |
2265 | ||
2266 | symbol* sym = new symbol; | |
2267 | sym->tok = ts->tok; | |
2268 | sym->name = v->name; | |
5f36109e JS |
2269 | ts->components[i].expr_index = sym; |
2270 | } | |
2271 | ||
2272 | // Create the return sprintf. | |
2273 | token* pf_tok = new token(*ts->tok); | |
2274 | pf_tok->content = "sprintf"; | |
2275 | print_format* pf = print_format::create(pf_tok); | |
2276 | return_statement* rs = new return_statement; | |
2277 | rs->tok = ts->tok; | |
2278 | rs->value = pf; | |
5f36109e JS |
2279 | |
2280 | // Recurse into the actual values. | |
7d11d8c9 | 2281 | recurse (&base_type, ts, pf, true); |
5f36109e JS |
2282 | pf->components = print_format::string_to_components(pf->raw_components); |
2283 | ||
7d11d8c9 JS |
2284 | // Create the try-catch net |
2285 | try_block* tb = new try_block; | |
2286 | tb->tok = ts->tok; | |
2287 | tb->try_block = rs; | |
2288 | tb->catch_error_var = 0; | |
2289 | return_statement* rs2 = new return_statement; | |
2290 | rs2->tok = ts->tok; | |
2291 | rs2->value = new literal_string ("ERROR"); | |
2292 | rs2->value->tok = ts->tok; | |
2293 | tb->catch_block = rs2; | |
2294 | fdecl->body = tb; | |
2295 | ||
f8809d54 | 2296 | fdecl->join (dw.sess); |
5f36109e JS |
2297 | return fcall; |
2298 | } | |
2299 | ||
2300 | ||
2301 | void | |
2302 | dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e, | |
7d11d8c9 | 2303 | print_format* pf, bool top) |
5f36109e JS |
2304 | { |
2305 | Dwarf_Die type; | |
2306 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2307 | ||
2308 | switch (dwarf_tag(&type)) | |
2309 | { | |
2310 | default: | |
2311 | // XXX need a warning? | |
2312 | // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type)) | |
2313 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2314 | pf->raw_components.append("?"); | |
2315 | break; | |
2316 | ||
2317 | case DW_TAG_enumeration_type: | |
2318 | case DW_TAG_base_type: | |
7d11d8c9 | 2319 | recurse_base (&type, e, pf); |
5f36109e JS |
2320 | break; |
2321 | ||
2322 | case DW_TAG_array_type: | |
7d11d8c9 | 2323 | recurse_array (&type, e, pf, top); |
5f36109e JS |
2324 | break; |
2325 | ||
2326 | case DW_TAG_pointer_type: | |
2327 | case DW_TAG_reference_type: | |
2328 | case DW_TAG_rvalue_reference_type: | |
7d11d8c9 | 2329 | recurse_pointer (&type, e, pf, top); |
5f36109e JS |
2330 | break; |
2331 | ||
2332 | case DW_TAG_subroutine_type: | |
2333 | pf->raw_components.append("<function>:%p"); | |
2334 | pf->args.push_back(deref(e)); | |
2335 | break; | |
2336 | ||
2337 | case DW_TAG_union_type: | |
5f36109e JS |
2338 | case DW_TAG_structure_type: |
2339 | case DW_TAG_class_type: | |
7d11d8c9 | 2340 | recurse_struct (&type, e, pf, top); |
5f36109e JS |
2341 | break; |
2342 | } | |
2343 | } | |
2344 | ||
2345 | ||
2346 | void | |
2347 | dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2348 | print_format* pf) |
5f36109e JS |
2349 | { |
2350 | Dwarf_Attribute attr; | |
2351 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2352 | dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr), | |
2353 | &encoding); | |
2354 | bool push = true; | |
2355 | switch (encoding) | |
2356 | { | |
2357 | case DW_ATE_float: | |
2358 | case DW_ATE_complex_float: | |
2359 | // XXX need a warning? | |
2360 | // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding) | |
2361 | // + ") for " + dwarf_type_name(type), e->tok); | |
2362 | pf->raw_components.append("?"); | |
2363 | push = false; | |
2364 | break; | |
2365 | ||
2366 | case DW_ATE_signed_char: | |
2367 | case DW_ATE_unsigned_char: | |
2368 | pf->raw_components.append("'%c'"); | |
2369 | break; | |
2370 | ||
2371 | case DW_ATE_unsigned: | |
2372 | pf->raw_components.append("%u"); | |
2373 | break; | |
2374 | ||
2375 | default: | |
2376 | pf->raw_components.append("%i"); | |
2377 | break; | |
2378 | } | |
2379 | if (push) | |
2380 | pf->args.push_back(deref(e)); | |
2381 | } | |
2382 | ||
2383 | ||
2384 | void | |
2385 | dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2386 | print_format* pf, bool top) |
5f36109e | 2387 | { |
7d11d8c9 JS |
2388 | if (!top && !print_full) |
2389 | { | |
2390 | pf->raw_components.append("[...]"); | |
2391 | return; | |
2392 | } | |
2393 | ||
5f36109e JS |
2394 | Dwarf_Die childtype; |
2395 | dwarf_attr_die (type, DW_AT_type, &childtype); | |
bbee5bb8 JS |
2396 | |
2397 | if (print_chars (&childtype, e, pf)) | |
2398 | return; | |
2399 | ||
5f36109e JS |
2400 | pf->raw_components.append("["); |
2401 | ||
2402 | // We print the array up to the first 5 elements. | |
2403 | // XXX how can we determine the array size? | |
2404 | // ... for now, just print the first element | |
2405 | unsigned i, size = 1; | |
2406 | for (i=0; i < size && i < 5; ++i) | |
2407 | { | |
2408 | if (i > 0) | |
2409 | pf->raw_components.append(", "); | |
2410 | target_symbol* e2 = new target_symbol(*e); | |
2411 | e2->components.push_back (target_symbol::component(e->tok, i)); | |
7d11d8c9 | 2412 | recurse (&childtype, e2, pf); |
5f36109e JS |
2413 | } |
2414 | if (i < size || 1/*XXX until real size is known */) | |
2415 | pf->raw_components.append(", ..."); | |
2416 | pf->raw_components.append("]"); | |
2417 | } | |
2418 | ||
2419 | ||
2420 | void | |
2421 | dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2422 | print_format* pf, bool top) |
5f36109e | 2423 | { |
7d11d8c9 JS |
2424 | // We chase to top-level pointers, but leave the rest alone |
2425 | Dwarf_Die pointee; | |
bbee5bb8 | 2426 | if (dwarf_attr_die (type, DW_AT_type, &pointee)) |
5f36109e | 2427 | { |
bbee5bb8 JS |
2428 | if (print_chars (&pointee, e, pf)) |
2429 | return; | |
2430 | ||
2431 | if (top) | |
2432 | { | |
2433 | recurse (&pointee, e, pf, top); | |
2434 | return; | |
2435 | } | |
5f36109e | 2436 | } |
bbee5bb8 JS |
2437 | |
2438 | pf->raw_components.append("%p"); | |
2439 | pf->args.push_back(deref(e)); | |
5f36109e JS |
2440 | } |
2441 | ||
2442 | ||
2443 | void | |
2444 | dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2445 | print_format* pf, bool top) |
5f36109e | 2446 | { |
bdec0e18 JS |
2447 | if (dwarf_hasattr(type, DW_AT_declaration)) |
2448 | { | |
2449 | Dwarf_Die *resolved = dw.declaration_resolve(dwarf_diename(type)); | |
2450 | if (!resolved) | |
2451 | { | |
2452 | // could be an error, but for now just stub it | |
2453 | // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok); | |
2454 | pf->raw_components.append("{...}"); | |
2455 | return; | |
2456 | } | |
2457 | type = resolved; | |
2458 | } | |
2459 | ||
5f36109e JS |
2460 | int count = 0; |
2461 | pf->raw_components.append("{"); | |
7d11d8c9 JS |
2462 | if (top || print_full) |
2463 | recurse_struct_members (type, e, pf, count); | |
2464 | else | |
2465 | pf->raw_components.append("..."); | |
5f36109e JS |
2466 | pf->raw_components.append("}"); |
2467 | } | |
2468 | ||
2469 | ||
2470 | void | |
2471 | dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2472 | print_format* pf, int& count) |
5f36109e JS |
2473 | { |
2474 | Dwarf_Die child, childtype; | |
2475 | if (dwarf_child (type, &child) == 0) | |
2476 | do | |
2477 | { | |
2478 | target_symbol* e2 = e; | |
2479 | ||
2480 | // skip static members | |
2481 | if (dwarf_hasattr(&child, DW_AT_declaration)) | |
2482 | continue; | |
2483 | ||
2484 | int tag = dwarf_tag (&child); | |
2485 | ||
2486 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) | |
2487 | continue; | |
2488 | ||
2489 | dwarf_attr_die (&child, DW_AT_type, &childtype); | |
2490 | ||
2491 | if (tag == DW_TAG_inheritance) | |
2492 | { | |
7d11d8c9 | 2493 | recurse_struct_members (&childtype, e, pf, count); |
5f36109e JS |
2494 | continue; |
2495 | } | |
2496 | ||
2497 | int childtag = dwarf_tag (&childtype); | |
2498 | const char *member = dwarf_diename (&child); | |
3a147004 JS |
2499 | |
2500 | // "_vptr.foo" members are C++ virtual function tables, | |
2501 | // which (generally?) aren't interesting for users. | |
2502 | if (member && startswith(member, "_vptr.")) | |
2503 | continue; | |
2504 | ||
5f36109e JS |
2505 | if (++count > 1) |
2506 | pf->raw_components.append(", "); | |
2507 | if (member) | |
2508 | { | |
5f36109e JS |
2509 | pf->raw_components.append("."); |
2510 | pf->raw_components.append(member); | |
2511 | ||
2512 | e2 = new target_symbol(*e); | |
2513 | e2->components.push_back (target_symbol::component(e->tok, member)); | |
2514 | } | |
2515 | else if (childtag == DW_TAG_union_type) | |
2516 | pf->raw_components.append("<union>"); | |
2517 | else if (childtag == DW_TAG_structure_type) | |
2518 | pf->raw_components.append("<class>"); | |
2519 | else if (childtag == DW_TAG_class_type) | |
2520 | pf->raw_components.append("<struct>"); | |
2521 | pf->raw_components.append("="); | |
7d11d8c9 | 2522 | recurse (&childtype, e2, pf); |
5f36109e JS |
2523 | } |
2524 | while (dwarf_siblingof (&child, &child) == 0); | |
2525 | } | |
2526 | ||
2527 | ||
bbee5bb8 JS |
2528 | bool |
2529 | dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e, | |
2530 | print_format* pf) | |
2531 | { | |
2532 | Dwarf_Die type; | |
2533 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2534 | const char *name = dwarf_diename (&type); | |
2535 | if (name && (name == string("char") || name == string("unsigned char"))) | |
2536 | { | |
2537 | functioncall* fcall = new functioncall; | |
2538 | fcall->tok = e->tok; | |
2539 | fcall->function = userspace_p ? "user_string2" : "kernel_string2"; | |
2540 | fcall->args.push_back (deref (e)); | |
2541 | expression *err_msg = new literal_string ("<unknown>"); | |
2542 | err_msg->tok = e->tok; | |
2543 | fcall->args.push_back (err_msg); | |
2544 | ||
2545 | pf->raw_components.append ("\"%s\""); | |
2546 | pf->args.push_back (fcall); | |
2547 | return true; | |
2548 | } | |
2549 | return false; | |
2550 | } | |
2551 | ||
2552 | ||
5f36109e JS |
2553 | expression* |
2554 | dwarf_pretty_print::deref (target_symbol* e) | |
2555 | { | |
2556 | static unsigned tick = 0; | |
2557 | ||
2558 | // Synthesize a function to dereference the dwarf fields, | |
2559 | // with a pointer parameter that is the base tracepoint variable | |
2560 | functiondecl *fdecl = new functiondecl; | |
2561 | fdecl->synthetic = true; | |
2562 | fdecl->tok = e->tok; | |
2563 | embeddedcode *ec = new embeddedcode; | |
2564 | ec->tok = e->tok; | |
2565 | ||
2566 | fdecl->name = "_dwarf_pretty_print_deref_" + lex_cast(tick++); | |
2567 | fdecl->body = ec; | |
2568 | ||
2569 | // Synthesize a functioncall. | |
2570 | functioncall* fcall = new functioncall; | |
2571 | fcall->tok = e->tok; | |
2572 | fcall->function = fdecl->name; | |
5f36109e JS |
2573 | |
2574 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime | |
2575 | ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n"; | |
2576 | ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n"; | |
2577 | ||
2578 | if (pointer) | |
2579 | { | |
2580 | ec->code += dw.literal_stmt_for_pointer (&pointer_type, e, | |
2581 | false, fdecl->type); | |
2582 | ||
2583 | vardecl *v = new vardecl; | |
2584 | v->type = pe_long; | |
2585 | v->name = "pointer"; | |
2586 | v->tok = e->tok; | |
2587 | fdecl->formal_args.push_back(v); | |
2588 | fcall->args.push_back(pointer); | |
2589 | } | |
2590 | else if (!local.empty()) | |
2591 | ec->code += dw.literal_stmt_for_local (scopes, pc, local, e, | |
2592 | false, fdecl->type); | |
2593 | else | |
2594 | ec->code += dw.literal_stmt_for_return (&scopes[0], pc, e, | |
2595 | false, fdecl->type); | |
2596 | ||
2597 | // Any non-literal indexes need to be passed in too. | |
2598 | for (unsigned i = 0; i < e->components.size(); ++i) | |
2599 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
2600 | { | |
2601 | vardecl *v = new vardecl; | |
2602 | v->type = pe_long; | |
2603 | v->name = "index" + lex_cast(i); | |
2604 | v->tok = e->tok; | |
2605 | fdecl->formal_args.push_back(v); | |
2606 | fcall->args.push_back(e->components[i].expr_index); | |
2607 | } | |
2608 | ||
2609 | ec->code += "/* pure */"; | |
2610 | ec->code += "/* unprivileged */"; | |
2611 | ||
2612 | // PR10601 | |
2613 | ec->code += "\n#undef fetch_register\n"; | |
2614 | ec->code += "\n#undef store_register\n"; | |
2615 | ||
f8809d54 | 2616 | fdecl->join (dw.sess); |
5f36109e JS |
2617 | return fcall; |
2618 | } | |
2619 | ||
2620 | ||
e57b735a | 2621 | void |
a7999c82 | 2622 | dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e) |
e57b735a | 2623 | { |
a7999c82 JS |
2624 | // Get the full name of the target symbol. |
2625 | stringstream ts_name_stream; | |
2626 | e->print(ts_name_stream); | |
2627 | string ts_name = ts_name_stream.str(); | |
2628 | ||
2629 | // Check and make sure we haven't already seen this target | |
2630 | // variable in this return probe. If we have, just return our | |
2631 | // last replacement. | |
af234c40 | 2632 | map<string, expression *>::iterator i = return_ts_map.find(ts_name); |
a7999c82 | 2633 | if (i != return_ts_map.end()) |
85ecf79a | 2634 | { |
a7999c82 JS |
2635 | provide (i->second); |
2636 | return; | |
2637 | } | |
85ecf79a | 2638 | |
70208613 JS |
2639 | // Attempt the expansion directly first, so if there's a problem with the |
2640 | // variable we won't have a bogus entry probe lying around. Like in | |
2641 | // saveargs(), we pretend for a moment that we're not in a .return. | |
2642 | bool saved_has_return = q.has_return; | |
2643 | q.has_return = false; | |
2644 | expression *repl = e; | |
2645 | replace (repl); | |
2646 | q.has_return = saved_has_return; | |
2647 | target_symbol* n = dynamic_cast<target_symbol*>(repl); | |
2648 | if (n && n->saved_conversion_error) | |
2649 | { | |
2650 | provide (repl); | |
2651 | return; | |
2652 | } | |
2653 | ||
af234c40 JS |
2654 | expression *exp; |
2655 | if (!q.has_process && | |
2656 | strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0) | |
277c21bc | 2657 | exp = gen_kretprobe_saved_return(repl, e->name); |
af234c40 | 2658 | else |
277c21bc | 2659 | exp = gen_mapped_saved_return(repl, e->name); |
af234c40 JS |
2660 | |
2661 | // Provide the variable to our parent so it can be used as a | |
2662 | // substitute for the target symbol. | |
2663 | provide (exp); | |
2664 | ||
2665 | // Remember this replacement since we might be able to reuse | |
2666 | // it later if the same return probe references this target | |
2667 | // symbol again. | |
2668 | return_ts_map[ts_name] = exp; | |
2669 | } | |
2670 | ||
2671 | expression* | |
70208613 | 2672 | dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e, |
277c21bc | 2673 | const string& name) |
af234c40 | 2674 | { |
a7999c82 JS |
2675 | // We've got to do several things here to handle target |
2676 | // variables in return probes. | |
85ecf79a | 2677 | |
a7999c82 JS |
2678 | // (1) Synthesize two global arrays. One is the cache of the |
2679 | // target variable and the other contains a thread specific | |
2680 | // nesting level counter. The arrays will look like | |
2681 | // this: | |
2682 | // | |
2683 | // _dwarf_tvar_{name}_{num} | |
2684 | // _dwarf_tvar_{name}_{num}_ctr | |
2685 | ||
2686 | string aname = (string("_dwarf_tvar_") | |
277c21bc | 2687 | + name.substr(1) |
aca66a36 | 2688 | + "_" + lex_cast(tick++)); |
a7999c82 JS |
2689 | vardecl* vd = new vardecl; |
2690 | vd->name = aname; | |
2691 | vd->tok = e->tok; | |
2692 | q.sess.globals.push_back (vd); | |
2693 | ||
2694 | string ctrname = aname + "_ctr"; | |
2695 | vd = new vardecl; | |
2696 | vd->name = ctrname; | |
2697 | vd->tok = e->tok; | |
2698 | q.sess.globals.push_back (vd); | |
2699 | ||
2700 | // (2) Create a new code block we're going to insert at the | |
2701 | // beginning of this probe to get the cached value into a | |
2702 | // temporary variable. We'll replace the target variable | |
2703 | // reference with the temporary variable reference. The code | |
2704 | // will look like this: | |
2705 | // | |
2706 | // _dwarf_tvar_tid = tid() | |
2707 | // _dwarf_tvar_{name}_{num}_tmp | |
2708 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2709 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2710 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2711 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
2712 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
2713 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
2714 | ||
2715 | // (2a) Synthesize the tid temporary expression, which will look | |
2716 | // like this: | |
2717 | // | |
2718 | // _dwarf_tvar_tid = tid() | |
2719 | symbol* tidsym = new symbol; | |
2720 | tidsym->name = string("_dwarf_tvar_tid"); | |
2721 | tidsym->tok = e->tok; | |
85ecf79a | 2722 | |
a7999c82 JS |
2723 | if (add_block == NULL) |
2724 | { | |
2725 | add_block = new block; | |
2726 | add_block->tok = e->tok; | |
8c819921 | 2727 | |
a7999c82 JS |
2728 | // Synthesize a functioncall to grab the thread id. |
2729 | functioncall* fc = new functioncall; | |
2730 | fc->tok = e->tok; | |
2731 | fc->function = string("tid"); | |
8c819921 | 2732 | |
a7999c82 | 2733 | // Assign the tid to '_dwarf_tvar_tid'. |
8c819921 DS |
2734 | assignment* a = new assignment; |
2735 | a->tok = e->tok; | |
2736 | a->op = "="; | |
a7999c82 JS |
2737 | a->left = tidsym; |
2738 | a->right = fc; | |
8c819921 DS |
2739 | |
2740 | expr_statement* es = new expr_statement; | |
2741 | es->tok = e->tok; | |
2742 | es->value = a; | |
8c819921 | 2743 | add_block->statements.push_back (es); |
a7999c82 | 2744 | } |
8c819921 | 2745 | |
a7999c82 JS |
2746 | // (2b) Synthesize an array reference and assign it to a |
2747 | // temporary variable (that we'll use as replacement for the | |
2748 | // target variable reference). It will look like this: | |
2749 | // | |
2750 | // _dwarf_tvar_{name}_{num}_tmp | |
2751 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2752 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2753 | ||
2754 | arrayindex* ai_tvar_base = new arrayindex; | |
2755 | ai_tvar_base->tok = e->tok; | |
2756 | ||
2757 | symbol* sym = new symbol; | |
2758 | sym->name = aname; | |
2759 | sym->tok = e->tok; | |
2760 | ai_tvar_base->base = sym; | |
2761 | ||
2762 | ai_tvar_base->indexes.push_back(tidsym); | |
2763 | ||
2764 | // We need to create a copy of the array index in its current | |
2765 | // state so we can have 2 variants of it (the original and one | |
2766 | // that post-decrements the second index). | |
2767 | arrayindex* ai_tvar = new arrayindex; | |
2768 | arrayindex* ai_tvar_postdec = new arrayindex; | |
2769 | *ai_tvar = *ai_tvar_base; | |
2770 | *ai_tvar_postdec = *ai_tvar_base; | |
2771 | ||
2772 | // Synthesize the | |
2773 | // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the | |
2774 | // second index into the array. | |
2775 | arrayindex* ai_ctr = new arrayindex; | |
2776 | ai_ctr->tok = e->tok; | |
2777 | ||
2778 | sym = new symbol; | |
2779 | sym->name = ctrname; | |
2780 | sym->tok = e->tok; | |
2781 | ai_ctr->base = sym; | |
2782 | ai_ctr->indexes.push_back(tidsym); | |
2783 | ai_tvar->indexes.push_back(ai_ctr); | |
2784 | ||
2785 | symbol* tmpsym = new symbol; | |
2786 | tmpsym->name = aname + "_tmp"; | |
2787 | tmpsym->tok = e->tok; | |
2788 | ||
2789 | assignment* a = new assignment; | |
2790 | a->tok = e->tok; | |
2791 | a->op = "="; | |
2792 | a->left = tmpsym; | |
2793 | a->right = ai_tvar; | |
2794 | ||
2795 | expr_statement* es = new expr_statement; | |
2796 | es->tok = e->tok; | |
2797 | es->value = a; | |
2798 | ||
2799 | add_block->statements.push_back (es); | |
2800 | ||
2801 | // (2c) Add a post-decrement to the second array index and | |
2802 | // delete the array value. It will look like this: | |
2803 | // | |
2804 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2805 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
2806 | ||
2807 | post_crement* pc = new post_crement; | |
2808 | pc->tok = e->tok; | |
2809 | pc->op = "--"; | |
2810 | pc->operand = ai_ctr; | |
2811 | ai_tvar_postdec->indexes.push_back(pc); | |
2812 | ||
2813 | delete_statement* ds = new delete_statement; | |
2814 | ds->tok = e->tok; | |
2815 | ds->value = ai_tvar_postdec; | |
2816 | ||
2817 | add_block->statements.push_back (ds); | |
2818 | ||
2819 | // (2d) Delete the counter value if it is 0. It will look like | |
2820 | // this: | |
2821 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
2822 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
2823 | ||
2824 | ds = new delete_statement; | |
2825 | ds->tok = e->tok; | |
2826 | ds->value = ai_ctr; | |
2827 | ||
2828 | unary_expression *ue = new unary_expression; | |
2829 | ue->tok = e->tok; | |
2830 | ue->op = "!"; | |
2831 | ue->operand = ai_ctr; | |
2832 | ||
2833 | if_statement *ifs = new if_statement; | |
2834 | ifs->tok = e->tok; | |
2835 | ifs->condition = ue; | |
2836 | ifs->thenblock = ds; | |
2837 | ifs->elseblock = NULL; | |
2838 | ||
2839 | add_block->statements.push_back (ifs); | |
2840 | ||
2841 | // (3) We need an entry probe that saves the value for us in the | |
2842 | // global array we created. Create the entry probe, which will | |
2843 | // look like this: | |
2844 | // | |
2260f4e3 | 2845 | // probe kernel.function("{function}").call { |
a7999c82 JS |
2846 | // _dwarf_tvar_tid = tid() |
2847 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2848 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2849 | // = ${param} | |
2850 | // } | |
2851 | ||
2260f4e3 | 2852 | if (add_call_probe == NULL) |
a7999c82 | 2853 | { |
2260f4e3 FCE |
2854 | add_call_probe = new block; |
2855 | add_call_probe->tok = e->tok; | |
4baf0e53 | 2856 | |
a7999c82 JS |
2857 | // Synthesize a functioncall to grab the thread id. |
2858 | functioncall* fc = new functioncall; | |
2859 | fc->tok = e->tok; | |
2860 | fc->function = string("tid"); | |
4baf0e53 | 2861 | |
a7999c82 JS |
2862 | // Assign the tid to '_dwarf_tvar_tid'. |
2863 | assignment* a = new assignment; | |
8fc05e57 DS |
2864 | a->tok = e->tok; |
2865 | a->op = "="; | |
a7999c82 JS |
2866 | a->left = tidsym; |
2867 | a->right = fc; | |
8fc05e57 | 2868 | |
a7999c82 | 2869 | expr_statement* es = new expr_statement; |
8fc05e57 DS |
2870 | es->tok = e->tok; |
2871 | es->value = a; | |
2260f4e3 | 2872 | add_call_probe = new block(add_call_probe, es); |
85ecf79a | 2873 | } |
cf2a1f85 | 2874 | |
a7999c82 JS |
2875 | // Save the value, like this: |
2876 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
2877 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
2878 | // = ${param} | |
2879 | arrayindex* ai_tvar_preinc = new arrayindex; | |
2880 | *ai_tvar_preinc = *ai_tvar_base; | |
2881 | ||
2882 | pre_crement* preinc = new pre_crement; | |
2883 | preinc->tok = e->tok; | |
2884 | preinc->op = "++"; | |
2885 | preinc->operand = ai_ctr; | |
2886 | ai_tvar_preinc->indexes.push_back(preinc); | |
2887 | ||
2888 | a = new assignment; | |
2889 | a->tok = e->tok; | |
2890 | a->op = "="; | |
2891 | a->left = ai_tvar_preinc; | |
2892 | a->right = e; | |
2893 | ||
2894 | es = new expr_statement; | |
2895 | es->tok = e->tok; | |
2896 | es->value = a; | |
2897 | ||
2260f4e3 | 2898 | add_call_probe = new block(add_call_probe, es); |
a7999c82 JS |
2899 | |
2900 | // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to | |
2901 | // our parent so it can be used as a substitute for the target | |
2902 | // symbol. | |
af234c40 JS |
2903 | return tmpsym; |
2904 | } | |
a7999c82 | 2905 | |
af234c40 JS |
2906 | |
2907 | expression* | |
70208613 | 2908 | dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e, |
277c21bc | 2909 | const string& name) |
af234c40 JS |
2910 | { |
2911 | // The code for this is simple. | |
2912 | // | |
2913 | // .call: | |
2914 | // _set_kretprobe_long(index, $value) | |
2915 | // | |
2916 | // .return: | |
2917 | // _get_kretprobe_long(index) | |
2918 | // | |
2919 | // (or s/long/string/ for things like $$parms) | |
2920 | ||
2921 | unsigned index; | |
2922 | string setfn, getfn; | |
2923 | ||
2924 | // Cheesy way to predetermine that this is a string -- if the second | |
2925 | // character is a '$', then we're looking at a $$vars, $$parms, or $$locals. | |
2926 | // XXX We need real type resolution here, especially if we are ever to | |
2927 | // support an @entry construct. | |
277c21bc | 2928 | if (name[1] == '$') |
af234c40 JS |
2929 | { |
2930 | index = saved_strings++; | |
2931 | setfn = "_set_kretprobe_string"; | |
2932 | getfn = "_get_kretprobe_string"; | |
2933 | } | |
2934 | else | |
2935 | { | |
2936 | index = saved_longs++; | |
2937 | setfn = "_set_kretprobe_long"; | |
2938 | getfn = "_get_kretprobe_long"; | |
2939 | } | |
2940 | ||
2941 | // Create the entry code | |
2942 | // _set_kretprobe_{long|string}(index, $value) | |
2943 | ||
2944 | if (add_call_probe == NULL) | |
2945 | { | |
2946 | add_call_probe = new block; | |
2947 | add_call_probe->tok = e->tok; | |
2948 | } | |
2949 | ||
2950 | functioncall* set_fc = new functioncall; | |
2951 | set_fc->tok = e->tok; | |
2952 | set_fc->function = setfn; | |
2953 | set_fc->args.push_back(new literal_number(index)); | |
2954 | set_fc->args.back()->tok = e->tok; | |
2955 | set_fc->args.push_back(e); | |
2956 | ||
2957 | expr_statement* set_es = new expr_statement; | |
2958 | set_es->tok = e->tok; | |
2959 | set_es->value = set_fc; | |
2960 | ||
2961 | add_call_probe->statements.push_back(set_es); | |
2962 | ||
2963 | // Create the return code | |
2964 | // _get_kretprobe_{long|string}(index) | |
2965 | ||
2966 | functioncall* get_fc = new functioncall; | |
2967 | get_fc->tok = e->tok; | |
2968 | get_fc->function = getfn; | |
2969 | get_fc->args.push_back(new literal_number(index)); | |
2970 | get_fc->args.back()->tok = e->tok; | |
2971 | ||
2972 | return get_fc; | |
a7999c82 | 2973 | } |
a43ba433 | 2974 | |
2cb3fe26 | 2975 | |
a7999c82 JS |
2976 | void |
2977 | dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
2978 | { | |
9aa8ffce | 2979 | if (null_die(scope_die)) |
a7999c82 | 2980 | return; |
2cb3fe26 | 2981 | |
5f36109e JS |
2982 | target_symbol *tsym = new target_symbol(*e); |
2983 | ||
2984 | string format = "=%#x"; | |
2985 | if (!e->components.empty() && | |
2986 | e->components[0].type == target_symbol::comp_pretty_print) | |
2987 | format = "=%s"; | |
a43ba433 | 2988 | |
a7999c82 JS |
2989 | // Convert $$parms to sprintf of a list of parms and active local vars |
2990 | // which we recursively evaluate | |
a43ba433 | 2991 | |
a7999c82 JS |
2992 | // NB: we synthesize a new token here rather than reusing |
2993 | // e->tok, because print_format::print likes to use | |
2994 | // its tok->content. | |
5f36109e | 2995 | token* pf_tok = new token(*e->tok); |
a7999c82 | 2996 | pf_tok->type = tok_identifier; |
b393f6f2 | 2997 | pf_tok->content = "sprintf"; |
2cb3fe26 | 2998 | |
d5e178c1 | 2999 | print_format* pf = print_format::create(pf_tok); |
a7999c82 | 3000 | |
277c21bc | 3001 | if (q.has_return && (e->name == "$$return")) |
a7999c82 | 3002 | { |
277c21bc | 3003 | tsym->name = "$return"; |
a7999c82 JS |
3004 | |
3005 | // Ignore any variable that isn't accessible. | |
3006 | tsym->saved_conversion_error = 0; | |
3007 | expression *texp = tsym; | |
8b095b45 | 3008 | replace (texp); // NB: throws nothing ... |
a7999c82 | 3009 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
a43ba433 | 3010 | { |
2cb3fe26 | 3011 | |
a43ba433 FCE |
3012 | } |
3013 | else | |
3014 | { | |
a7999c82 | 3015 | pf->raw_components += "return"; |
5f36109e | 3016 | pf->raw_components += format; |
a7999c82 JS |
3017 | pf->args.push_back(texp); |
3018 | } | |
3019 | } | |
3020 | else | |
3021 | { | |
3022 | // non-.return probe: support $$parms, $$vars, $$locals | |
345bbb3d | 3023 | bool first = true; |
a7999c82 | 3024 | Dwarf_Die result; |
9aa8ffce | 3025 | vector<Dwarf_Die> scopes = q.dw.getscopes_die(scope_die); |
a7999c82 JS |
3026 | if (dwarf_child (&scopes[0], &result) == 0) |
3027 | do | |
3028 | { | |
3029 | switch (dwarf_tag (&result)) | |
00cf3709 | 3030 | { |
a7999c82 | 3031 | case DW_TAG_variable: |
277c21bc | 3032 | if (e->name == "$$parms") |
a7999c82 JS |
3033 | continue; |
3034 | break; | |
3035 | case DW_TAG_formal_parameter: | |
277c21bc | 3036 | if (e->name == "$$locals") |
a7999c82 JS |
3037 | continue; |
3038 | break; | |
3039 | ||
3040 | default: | |
3041 | continue; | |
3042 | } | |
41c262f3 | 3043 | |
a7999c82 JS |
3044 | const char *diename = dwarf_diename (&result); |
3045 | if (! diename) continue; | |
f76427a2 | 3046 | |
345bbb3d MW |
3047 | if (! first) |
3048 | pf->raw_components += " "; | |
3049 | pf->raw_components += diename; | |
3050 | ||
277c21bc JS |
3051 | tsym->name = "$"; |
3052 | tsym->name += diename; | |
41c262f3 | 3053 | |
a7999c82 JS |
3054 | // Ignore any variable that isn't accessible. |
3055 | tsym->saved_conversion_error = 0; | |
3056 | expression *texp = tsym; | |
8b095b45 | 3057 | replace (texp); // NB: throws nothing ... |
a7999c82 JS |
3058 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
3059 | { | |
3060 | if (q.sess.verbose>2) | |
a43ba433 | 3061 | { |
a7999c82 JS |
3062 | for (semantic_error *c = tsym->saved_conversion_error; |
3063 | c != 0; | |
3064 | c = c->chain) { | |
3065 | clog << "variable location problem: " << c->what() << endl; | |
3066 | } | |
a43ba433 | 3067 | } |
a7999c82 | 3068 | |
345bbb3d | 3069 | pf->raw_components += "=?"; |
00cf3709 | 3070 | } |
a7999c82 JS |
3071 | else |
3072 | { | |
5f36109e | 3073 | pf->raw_components += format; |
a7999c82 JS |
3074 | pf->args.push_back(texp); |
3075 | } | |
345bbb3d | 3076 | first = false; |
a7999c82 JS |
3077 | } |
3078 | while (dwarf_siblingof (&result, &result) == 0); | |
3079 | } | |
2cb3fe26 | 3080 | |
a7999c82 JS |
3081 | pf->components = print_format::string_to_components(pf->raw_components); |
3082 | provide (pf); | |
3083 | } | |
3084 | ||
2cb3fe26 | 3085 | |
a7999c82 JS |
3086 | void |
3087 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
3088 | { | |
277c21bc | 3089 | assert(e->name.size() > 0 && e->name[0] == '$'); |
a7999c82 | 3090 | visited = true; |
30263a73 FCE |
3091 | bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e)); |
3092 | // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars | |
a7999c82 | 3093 | |
70208613 | 3094 | try |
a7999c82 | 3095 | { |
c69a87e0 FCE |
3096 | bool lvalue = is_active_lvalue(e); |
3097 | if (lvalue && !q.sess.guru_mode) | |
3098 | throw semantic_error("write to target variable not permitted", e->tok); | |
2cb3fe26 | 3099 | |
c69a87e0 | 3100 | // XXX: process $context vars should be writeable |
70208613 | 3101 | |
c69a87e0 FCE |
3102 | // See if we need to generate a new probe to save/access function |
3103 | // parameters from a return probe. PR 1382. | |
3104 | if (q.has_return | |
3105 | && !defined_being_checked | |
277c21bc JS |
3106 | && e->name != "$return" // not the special return-value variable handled below |
3107 | && e->name != "$$return") // nor the other special variable handled below | |
c69a87e0 FCE |
3108 | { |
3109 | if (lvalue) | |
3110 | throw semantic_error("write to target variable not permitted in .return probes", e->tok); | |
3111 | visit_target_symbol_saved_return(e); | |
3112 | return; | |
3113 | } | |
e57b735a | 3114 | |
277c21bc JS |
3115 | if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals" |
3116 | || (q.has_return && (e->name == "$$return"))) | |
c69a87e0 FCE |
3117 | { |
3118 | if (lvalue) | |
3119 | throw semantic_error("cannot write to context variable", e->tok); | |
70208613 | 3120 | |
c69a87e0 FCE |
3121 | if (e->addressof) |
3122 | throw semantic_error("cannot take address of context variable", e->tok); | |
70208613 | 3123 | |
5f36109e JS |
3124 | e->assert_no_components("dwarf", true); |
3125 | ||
c69a87e0 FCE |
3126 | visit_target_symbol_context(e); |
3127 | return; | |
3128 | } | |
70208613 | 3129 | |
5f36109e JS |
3130 | if (!e->components.empty() && |
3131 | e->components.back().type == target_symbol::comp_pretty_print) | |
3132 | { | |
3133 | if (lvalue) | |
3134 | throw semantic_error("cannot write to pretty-printed variable", e->tok); | |
3135 | ||
277c21bc | 3136 | if (q.has_return && (e->name == "$return")) |
5f36109e JS |
3137 | { |
3138 | dwarf_pretty_print dpp (q.dw, scope_die, addr, | |
3139 | q.has_process, *e); | |
3140 | dpp.expand()->visit(this); | |
3141 | } | |
3142 | else | |
3143 | { | |
3144 | dwarf_pretty_print dpp (q.dw, getscopes(e), addr, | |
277c21bc | 3145 | e->name.substr(1), |
5f36109e JS |
3146 | q.has_process, *e); |
3147 | dpp.expand()->visit(this); | |
3148 | } | |
3149 | return; | |
3150 | } | |
3151 | ||
c69a87e0 FCE |
3152 | // Synthesize a function. |
3153 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 3154 | fdecl->synthetic = true; |
c69a87e0 FCE |
3155 | fdecl->tok = e->tok; |
3156 | embeddedcode *ec = new embeddedcode; | |
3157 | ec->tok = e->tok; | |
70208613 | 3158 | |
c69a87e0 | 3159 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
277c21bc | 3160 | + "_" + e->name.substr(1) |
c69a87e0 | 3161 | + "_" + lex_cast(tick++)); |
70208613 | 3162 | |
b5a0dd41 FCE |
3163 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
3164 | ec->code += "\n#define fetch_register " + string(q.has_process?"u":"k") + "_fetch_register\n"; | |
3165 | ec->code += "#define store_register " + string(q.has_process?"u":"k") + "_store_register\n"; | |
70208613 | 3166 | |
277c21bc | 3167 | if (q.has_return && (e->name == "$return")) |
e19fda4e | 3168 | { |
b5a0dd41 | 3169 | ec->code += q.dw.literal_stmt_for_return (scope_die, |
e19fda4e | 3170 | addr, |
b4c34c26 | 3171 | e, |
e19fda4e DS |
3172 | lvalue, |
3173 | fdecl->type); | |
3174 | } | |
3175 | else | |
3176 | { | |
b5a0dd41 | 3177 | ec->code += q.dw.literal_stmt_for_local (getscopes(e), |
e19fda4e | 3178 | addr, |
277c21bc | 3179 | e->name.substr(1), |
b4c34c26 | 3180 | e, |
e19fda4e DS |
3181 | lvalue, |
3182 | fdecl->type); | |
3183 | } | |
3184 | ||
1b07c728 FCE |
3185 | if (! lvalue) |
3186 | ec->code += "/* pure */"; | |
64211010 DB |
3187 | |
3188 | ec->code += "/* unprivileged */"; | |
b5a0dd41 FCE |
3189 | |
3190 | // PR10601 | |
3191 | ec->code += "\n#undef fetch_register\n"; | |
3192 | ec->code += "\n#undef store_register\n"; | |
c69a87e0 FCE |
3193 | |
3194 | fdecl->name = fname; | |
3195 | fdecl->body = ec; | |
70208613 | 3196 | |
c69a87e0 FCE |
3197 | // Any non-literal indexes need to be passed in too. |
3198 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3199 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3200 | { | |
3201 | vardecl *v = new vardecl; | |
3202 | v->type = pe_long; | |
3203 | v->name = "index" + lex_cast(i); | |
3204 | v->tok = e->tok; | |
3205 | fdecl->formal_args.push_back(v); | |
3206 | } | |
70208613 | 3207 | |
c69a87e0 FCE |
3208 | if (lvalue) |
3209 | { | |
3210 | // Modify the fdecl so it carries a single pe_long formal | |
3211 | // argument called "value". | |
70208613 | 3212 | |
c69a87e0 FCE |
3213 | // FIXME: For the time being we only support setting target |
3214 | // variables which have base types; these are 'pe_long' in | |
3215 | // stap's type vocabulary. Strings and pointers might be | |
3216 | // reasonable, some day, but not today. | |
70208613 | 3217 | |
c69a87e0 FCE |
3218 | vardecl *v = new vardecl; |
3219 | v->type = pe_long; | |
3220 | v->name = "value"; | |
3221 | v->tok = e->tok; | |
3222 | fdecl->formal_args.push_back(v); | |
3223 | } | |
f8809d54 | 3224 | fdecl->join (q.sess); |
70208613 | 3225 | |
c69a87e0 FCE |
3226 | // Synthesize a functioncall. |
3227 | functioncall* n = new functioncall; | |
3228 | n->tok = e->tok; | |
3229 | n->function = fname; | |
70208613 | 3230 | |
c69a87e0 FCE |
3231 | // Any non-literal indexes need to be passed in too. |
3232 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3233 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3234 | n->args.push_back(require(e->components[i].expr_index)); | |
70208613 | 3235 | |
c69a87e0 FCE |
3236 | if (lvalue) |
3237 | { | |
3238 | // Provide the functioncall to our parent, so that it can be | |
3239 | // used to substitute for the assignment node immediately above | |
3240 | // us. | |
3241 | assert(!target_symbol_setter_functioncalls.empty()); | |
3242 | *(target_symbol_setter_functioncalls.top()) = n; | |
3243 | } | |
70208613 | 3244 | |
c69a87e0 | 3245 | provide (n); |
66d284f4 FCE |
3246 | } |
3247 | catch (const semantic_error& er) | |
3248 | { | |
9fab2262 JS |
3249 | // We suppress this error message, and pass the unresolved |
3250 | // target_symbol to the next pass. We hope that this value ends | |
3251 | // up not being referenced after all, so it can be optimized out | |
3252 | // quietly. | |
1af1e62d | 3253 | e->chain (er); |
9fab2262 | 3254 | provide (e); |
66d284f4 | 3255 | } |
77de5e9e GH |
3256 | } |
3257 | ||
3258 | ||
c24447be JS |
3259 | void |
3260 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
3261 | { | |
3262 | // Fill in our current module context if needed | |
3263 | if (e->module.empty()) | |
3264 | e->module = q.dw.module_name; | |
3265 | ||
3266 | var_expanding_visitor::visit_cast_op(e); | |
3267 | } | |
3268 | ||
3269 | ||
729455a7 JS |
3270 | vector<Dwarf_Die>& |
3271 | dwarf_var_expanding_visitor::getscopes(target_symbol *e) | |
3272 | { | |
3273 | if (scopes.empty()) | |
3274 | { | |
3275 | // If the address is at the beginning of the scope_die, we can do a fast | |
3276 | // getscopes from there. Otherwise we need to look it up by address. | |
3277 | Dwarf_Addr entrypc; | |
3278 | if (q.dw.die_entrypc(scope_die, &entrypc) && entrypc == addr) | |
3279 | scopes = q.dw.getscopes(scope_die); | |
3280 | else | |
3281 | scopes = q.dw.getscopes(addr); | |
3282 | ||
3283 | if (scopes.empty()) | |
3284 | throw semantic_error ("unable to find any scopes containing " | |
3285 | + lex_cast_hex(addr) | |
3286 | + ((scope_die == NULL) ? "" | |
3287 | : (string (" in ") | |
3288 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
3289 | + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") | |
3290 | + ")")) | |
3291 | + " while searching for local '" | |
277c21bc | 3292 | + e->name.substr(1) + "'", |
729455a7 JS |
3293 | e->tok); |
3294 | } | |
3295 | return scopes; | |
3296 | } | |
3297 | ||
3298 | ||
5f36109e JS |
3299 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor |
3300 | { | |
3301 | systemtap_session& s; | |
3302 | dwarf_builder& db; | |
3303 | ||
3304 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
3305 | s(s), db(db) {} | |
3306 | void visit_cast_op (cast_op* e); | |
3307 | void filter_special_modules(string& module); | |
3308 | }; | |
3309 | ||
3310 | ||
c4ce66a1 JS |
3311 | struct dwarf_cast_query : public base_query |
3312 | { | |
946e1a48 | 3313 | cast_op& e; |
c4ce66a1 | 3314 | const bool lvalue; |
5f36109e JS |
3315 | const bool userspace_p; |
3316 | functioncall*& result; | |
c4ce66a1 | 3317 | |
5f36109e JS |
3318 | dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue, |
3319 | const bool userspace_p, functioncall*& result): | |
abb41d92 | 3320 | base_query(dw, module), e(e), lvalue(lvalue), |
5f36109e | 3321 | userspace_p(userspace_p), result(result) {} |
c4ce66a1 JS |
3322 | |
3323 | void handle_query_module(); | |
c4ce66a1 JS |
3324 | }; |
3325 | ||
3326 | ||
c4ce66a1 JS |
3327 | void |
3328 | dwarf_cast_query::handle_query_module() | |
3329 | { | |
5f36109e JS |
3330 | static unsigned tick = 0; |
3331 | ||
3332 | if (result) | |
c4ce66a1 JS |
3333 | return; |
3334 | ||
ea1e477a | 3335 | // look for the type in any CU |
7f6b80bd | 3336 | Dwarf_Die* type_die = dw.declaration_resolve_other_cus(e.type_name.c_str()); |
ea1e477a JS |
3337 | if (!type_die) |
3338 | return; | |
c4ce66a1 | 3339 | |
5f36109e JS |
3340 | string code; |
3341 | exp_type type = pe_long; | |
3342 | ||
ea1e477a | 3343 | try |
c4ce66a1 | 3344 | { |
ea1e477a JS |
3345 | Dwarf_Die cu_mem; |
3346 | dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL)); | |
5f36109e JS |
3347 | |
3348 | if (!e.components.empty() && | |
3349 | e.components.back().type == target_symbol::comp_pretty_print) | |
3350 | { | |
3351 | if (lvalue) | |
3352 | throw semantic_error("cannot write to pretty-printed variable", e.tok); | |
3353 | ||
3354 | dwarf_pretty_print dpp(dw, type_die, e.operand, userspace_p, e); | |
3355 | result = dpp.expand(); | |
3356 | return; | |
3357 | } | |
3358 | ||
3359 | code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type); | |
ea1e477a JS |
3360 | } |
3361 | catch (const semantic_error& er) | |
3362 | { | |
3363 | // NB: we can have multiple errors, since a @cast | |
1af1e62d JS |
3364 | // may be attempted using several different modules: |
3365 | // @cast(ptr, "type", "module1:module2:...") | |
3366 | e.chain (er); | |
c4ce66a1 | 3367 | } |
c4ce66a1 | 3368 | |
5f36109e JS |
3369 | if (code.empty()) |
3370 | return; | |
c4ce66a1 | 3371 | |
5f36109e | 3372 | string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get") |
277c21bc | 3373 | + "_" + e.name.substr(1) |
5f36109e | 3374 | + "_" + lex_cast(tick++)); |
c4ce66a1 | 3375 | |
5f36109e JS |
3376 | // Synthesize a function. |
3377 | functiondecl *fdecl = new functiondecl; | |
3378 | fdecl->synthetic = true; | |
3379 | fdecl->tok = e.tok; | |
3380 | fdecl->type = type; | |
3381 | fdecl->name = fname; | |
3382 | ||
3383 | embeddedcode *ec = new embeddedcode; | |
3384 | ec->tok = e.tok; | |
3385 | fdecl->body = ec; | |
3386 | ||
3387 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime | |
3388 | ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n"; | |
3389 | ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n"; | |
3390 | ||
3391 | ec->code += code; | |
3392 | ||
3393 | // Give the fdecl an argument for the pointer we're trying to cast | |
3394 | vardecl *v1 = new vardecl; | |
3395 | v1->type = pe_long; | |
3396 | v1->name = "pointer"; | |
3397 | v1->tok = e.tok; | |
3398 | fdecl->formal_args.push_back(v1); | |
3399 | ||
3400 | // Any non-literal indexes need to be passed in too. | |
3401 | for (unsigned i = 0; i < e.components.size(); ++i) | |
3402 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
3403 | { | |
3404 | vardecl *v = new vardecl; | |
3405 | v->type = pe_long; | |
3406 | v->name = "index" + lex_cast(i); | |
3407 | v->tok = e.tok; | |
3408 | fdecl->formal_args.push_back(v); | |
3409 | } | |
3410 | ||
3411 | if (lvalue) | |
3412 | { | |
3413 | // Modify the fdecl so it carries a second pe_long formal | |
3414 | // argument called "value". | |
3415 | ||
3416 | // FIXME: For the time being we only support setting target | |
3417 | // variables which have base types; these are 'pe_long' in | |
3418 | // stap's type vocabulary. Strings and pointers might be | |
3419 | // reasonable, some day, but not today. | |
3420 | ||
3421 | vardecl *v2 = new vardecl; | |
3422 | v2->type = pe_long; | |
3423 | v2->name = "value"; | |
3424 | v2->tok = e.tok; | |
3425 | fdecl->formal_args.push_back(v2); | |
3426 | } | |
3427 | else | |
3428 | ec->code += "/* pure */"; | |
3429 | ||
3430 | ec->code += "/* unprivileged */"; | |
3431 | ||
3432 | // PR10601 | |
3433 | ec->code += "\n#undef fetch_register\n"; | |
3434 | ec->code += "\n#undef store_register\n"; | |
3435 | ||
f8809d54 | 3436 | fdecl->join (dw.sess); |
5f36109e JS |
3437 | |
3438 | // Synthesize a functioncall. | |
3439 | functioncall* n = new functioncall; | |
3440 | n->tok = e.tok; | |
3441 | n->function = fname; | |
5f36109e JS |
3442 | n->args.push_back(e.operand); |
3443 | ||
3444 | // Any non-literal indexes need to be passed in too. | |
3445 | for (unsigned i = 0; i < e.components.size(); ++i) | |
3446 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
3447 | n->args.push_back(e.components[i].expr_index); | |
3448 | ||
3449 | result = n; | |
3450 | } | |
c4ce66a1 JS |
3451 | |
3452 | ||
fb0274bc JS |
3453 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
3454 | { | |
d90053e7 | 3455 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 3456 | // for those cases, build a module including that header |
d90053e7 | 3457 | if (module[module.size() - 1] == '>' && |
60d98537 | 3458 | (module[0] == '<' || startswith(module, "kernel<"))) |
fb0274bc JS |
3459 | { |
3460 | string cached_module; | |
3461 | if (s.use_cache) | |
3462 | { | |
3463 | // see if the cached module exists | |
a2639cb7 | 3464 | cached_module = find_typequery_hash(s, module); |
d105f664 | 3465 | if (!cached_module.empty() && !s.poison_cache) |
fb0274bc JS |
3466 | { |
3467 | int fd = open(cached_module.c_str(), O_RDONLY); | |
3468 | if (fd != -1) | |
3469 | { | |
3470 | if (s.verbose > 2) | |
3471 | clog << "Pass 2: using cached " << cached_module << endl; | |
3472 | module = cached_module; | |
3473 | close(fd); | |
3474 | return; | |
3475 | } | |
3476 | } | |
3477 | } | |
3478 | ||
3479 | // no cached module, time to make it | |
d90053e7 | 3480 | if (make_typequery(s, module) == 0) |
fb0274bc | 3481 | { |
e16dc041 | 3482 | // try to save typequery in the cache |
fb0274bc | 3483 | if (s.use_cache) |
e16dc041 | 3484 | copy_file(module, cached_module, s.verbose > 2); |
fb0274bc JS |
3485 | } |
3486 | } | |
3487 | } | |
3488 | ||
3489 | ||
c4ce66a1 JS |
3490 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
3491 | { | |
3492 | bool lvalue = is_active_lvalue(e); | |
3493 | if (lvalue && !s.guru_mode) | |
3494 | throw semantic_error("write to typecast value not permitted", e->tok); | |
3495 | ||
3496 | if (e->module.empty()) | |
3497 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
3498 | ||
5f36109e | 3499 | functioncall* result = NULL; |
8b31197b JS |
3500 | |
3501 | // split the module string by ':' for alternatives | |
3502 | vector<string> modules; | |
3503 | tokenize(e->module, modules, ":"); | |
b5a0dd41 | 3504 | bool userspace_p=false; // PR10601 |
5f36109e | 3505 | for (unsigned i = 0; !result && i < modules.size(); ++i) |
c4ce66a1 | 3506 | { |
8b31197b | 3507 | string& module = modules[i]; |
fb0274bc | 3508 | filter_special_modules(module); |
abb41d92 | 3509 | |
c4ce66a1 JS |
3510 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
3511 | // which means that userspace modules won't get any PATH searching. | |
3512 | dwflpp* dw; | |
707bf35e JS |
3513 | try |
3514 | { | |
b5a0dd41 FCE |
3515 | userspace_p=is_user_module (module); |
3516 | if (! userspace_p) | |
707bf35e JS |
3517 | { |
3518 | // kernel or kernel module target | |
ae2552da | 3519 | dw = db.get_kern_dw(s, module); |
707bf35e JS |
3520 | } |
3521 | else | |
3522 | { | |
3523 | module = find_executable (module); // canonicalize it | |
3524 | dw = db.get_user_dw(s, module); | |
3525 | } | |
3526 | } | |
3527 | catch (const semantic_error& er) | |
3528 | { | |
3529 | /* ignore and go to the next module */ | |
3530 | continue; | |
3531 | } | |
c4ce66a1 | 3532 | |
5f36109e | 3533 | dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result); |
51178501 | 3534 | dw->iterate_over_modules(&query_module, &q); |
c4ce66a1 | 3535 | } |
abb41d92 | 3536 | |
5f36109e | 3537 | if (!result) |
c4ce66a1 | 3538 | { |
946e1a48 JS |
3539 | // We pass the unresolved cast_op to the next pass, and hope |
3540 | // that this value ends up not being referenced after all, so | |
3541 | // it can be optimized out quietly. | |
c4ce66a1 JS |
3542 | provide (e); |
3543 | return; | |
3544 | } | |
3545 | ||
c4ce66a1 JS |
3546 | if (lvalue) |
3547 | { | |
3548 | // Provide the functioncall to our parent, so that it can be | |
3549 | // used to substitute for the assignment node immediately above | |
3550 | // us. | |
3551 | assert(!target_symbol_setter_functioncalls.empty()); | |
5f36109e | 3552 | *(target_symbol_setter_functioncalls.top()) = result; |
c4ce66a1 JS |
3553 | } |
3554 | ||
5f36109e | 3555 | result->visit (this); |
77de5e9e GH |
3556 | } |
3557 | ||
3558 | ||
b8da0ad1 FCE |
3559 | void |
3560 | dwarf_derived_probe::printsig (ostream& o) const | |
3561 | { | |
3562 | // Instead of just printing the plain locations, we add a PC value | |
3563 | // as a comment as a way of telling e.g. apart multiple inlined | |
3564 | // function instances. This is distinct from the verbose/clog | |
3565 | // output, since this part goes into the cache hash calculations. | |
3566 | sole_location()->print (o); | |
6d0f3f0c | 3567 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
3568 | printsig_nested (o); |
3569 | } | |
3570 | ||
3571 | ||
3572 | ||
dc38c0ae | 3573 | void |
b20febf3 FCE |
3574 | dwarf_derived_probe::join_group (systemtap_session& s) |
3575 | { | |
af234c40 JS |
3576 | // skip probes which are paired entry-handlers |
3577 | if (!has_return && (saved_longs || saved_strings)) | |
3578 | return; | |
3579 | ||
b20febf3 FCE |
3580 | if (! s.dwarf_derived_probes) |
3581 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
3582 | s.dwarf_derived_probes->enroll (this); | |
b642c901 SC |
3583 | |
3584 | enable_task_finder(s); | |
b20febf3 FCE |
3585 | } |
3586 | ||
3587 | ||
3588 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, | |
3589 | const string& filename, | |
3590 | int line, | |
91af0778 | 3591 | // module & section specify a relocation |
b20febf3 FCE |
3592 | // base for <addr>, unless section=="" |
3593 | // (equivalently module=="kernel") | |
3594 | const string& module, | |
3595 | const string& section, | |
3596 | // NB: dwfl_addr is the virtualized | |
3597 | // address for this symbol. | |
3598 | Dwarf_Addr dwfl_addr, | |
3599 | // addr is the section-offset for | |
3600 | // actual relocation. | |
3601 | Dwarf_Addr addr, | |
3602 | dwarf_query& q, | |
37ebca01 | 3603 | Dwarf_Die* scope_die /* may be null */) |
1939ea32 | 3604 | : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ), |
b20febf3 | 3605 | module (module), section (section), addr (addr), |
63b4fd14 | 3606 | path (q.path), |
27dc09b1 | 3607 | has_process (q.has_process), |
c9bad430 DS |
3608 | has_return (q.has_return), |
3609 | has_maxactive (q.has_maxactive), | |
63b4fd14 | 3610 | has_library (q.has_library), |
6b66b9f7 | 3611 | maxactive_val (q.maxactive_val), |
b642c901 SC |
3612 | user_path (q.user_path), |
3613 | user_lib (q.user_lib), | |
af234c40 | 3614 | access_vars(false), |
63b4fd14 | 3615 | saved_longs(0), saved_strings(0), |
af234c40 | 3616 | entry_handler(0) |
bd2b1e68 | 3617 | { |
b642c901 SC |
3618 | if (user_lib.size() != 0) |
3619 | has_library = true; | |
3620 | ||
6b66b9f7 JS |
3621 | if (q.has_process) |
3622 | { | |
3623 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. | |
3624 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
3625 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
3626 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
3627 | // XXX Assert invariants here too? | |
3628 | } | |
3629 | else | |
3630 | { | |
3631 | // Assert kernel relocation invariants | |
3632 | if (section == "" && dwfl_addr != addr) // addr should be absolute | |
3633 | throw semantic_error ("missing relocation base against", tok); | |
3634 | if (section != "" && dwfl_addr == addr) // addr should be an offset | |
3635 | throw semantic_error ("inconsistent relocation address", tok); | |
3636 | } | |
2930abc7 | 3637 | |
21beacc9 FCE |
3638 | // XXX: hack for strange g++/gcc's |
3639 | #ifndef USHRT_MAX | |
3640 | #define USHRT_MAX 32767 | |
3641 | #endif | |
3642 | ||
606fd9c8 | 3643 | // Range limit maxactive() value |
6b66b9f7 | 3644 | if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX)) |
606fd9c8 | 3645 | throw semantic_error ("maxactive value out of range [0," |
aca66a36 | 3646 | + lex_cast(USHRT_MAX) + "]", |
f1a0157a | 3647 | q.base_loc->components.front()->tok); |
606fd9c8 | 3648 | |
de688825 | 3649 | // Expand target variables in the probe body |
5f0a03a6 | 3650 | if (!null_die(scope_die)) |
8fc05e57 | 3651 | { |
6b66b9f7 | 3652 | // XXX: user-space deref's for q.has_process! |
de688825 | 3653 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
8b095b45 | 3654 | v.replace (this->body); |
6b66b9f7 JS |
3655 | if (!q.has_process) |
3656 | access_vars = v.visited; | |
37ebca01 FCE |
3657 | |
3658 | // If during target-variable-expanding the probe, we added a new block | |
3659 | // of code, add it to the start of the probe. | |
3660 | if (v.add_block) | |
ba6f838d | 3661 | this->body = new block(v.add_block, this->body); |
2260f4e3 FCE |
3662 | |
3663 | // If when target-variable-expanding the probe, we need to synthesize a | |
3664 | // sibling function-entry probe. We don't go through the whole probe derivation | |
3665 | // business (PR10642) that could lead to wildcard/alias resolution, or for that | |
3666 | // dwarf-induced duplication. | |
3667 | if (v.add_call_probe) | |
37ebca01 | 3668 | { |
2260f4e3 FCE |
3669 | assert (q.has_return && !q.has_call); |
3670 | ||
3671 | // We temporarily replace q.base_probe. | |
3672 | statement* old_body = q.base_probe->body; | |
3673 | q.base_probe->body = v.add_call_probe; | |
3674 | q.has_return = false; | |
3675 | q.has_call = true; | |
af234c40 | 3676 | |
da23eceb | 3677 | if (q.has_process) |
af234c40 JS |
3678 | entry_handler = new uprobe_derived_probe (funcname, filename, line, |
3679 | module, section, dwfl_addr, | |
3680 | addr, q, scope_die); | |
da23eceb | 3681 | else |
af234c40 JS |
3682 | entry_handler = new dwarf_derived_probe (funcname, filename, line, |
3683 | module, section, dwfl_addr, | |
3684 | addr, q, scope_die); | |
3685 | ||
3686 | saved_longs = entry_handler->saved_longs = v.saved_longs; | |
3687 | saved_strings = entry_handler->saved_strings = v.saved_strings; | |
3688 | ||
3689 | q.results.push_back (entry_handler); | |
2260f4e3 FCE |
3690 | |
3691 | q.has_return = true; | |
3692 | q.has_call = false; | |
3693 | q.base_probe->body = old_body; | |
37ebca01 | 3694 | } |
f10534c6 WH |
3695 | // Save the local variables for listing mode |
3696 | if (q.sess.listing_mode_vars) | |
3697 | saveargs(q, scope_die, v); | |
8fc05e57 | 3698 | } |
37ebca01 | 3699 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 3700 | |
f10534c6 | 3701 | // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs |
0a98fd42 | 3702 | |
5d23847d | 3703 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
3704 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
3705 | // point. This allows a user to see what function / file / line | |
3706 | // number any particular match of the wildcards. | |
2930abc7 | 3707 | |
a229fcd7 | 3708 | vector<probe_point::component*> comps; |
91af0778 FCE |
3709 | if (q.has_kernel) |
3710 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
3711 | else if(q.has_module) | |
3712 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
3713 | else if(q.has_process) | |
3714 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
3715 | else | |
3716 | assert (0); | |
b5d77020 | 3717 | |
db520b00 FCE |
3718 | string fn_or_stmt; |
3719 | if (q.has_function_str || q.has_function_num) | |
3720 | fn_or_stmt = "function"; | |
3721 | else | |
3722 | fn_or_stmt = "statement"; | |
a229fcd7 | 3723 | |
b8da0ad1 | 3724 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 3725 | { |
4cd232e4 | 3726 | string retro_name = funcname; |
b20febf3 | 3727 | if (filename != "") |
cee35f73 | 3728 | { |
fb84c077 | 3729 | retro_name += ("@" + string (filename)); |
cee35f73 | 3730 | if (line > 0) |
aca66a36 | 3731 | retro_name += (":" + lex_cast (line)); |
cee35f73 | 3732 | } |
db520b00 FCE |
3733 | comps.push_back |
3734 | (new probe_point::component | |
3735 | (fn_or_stmt, new literal_string (retro_name))); | |
3736 | } | |
b8da0ad1 | 3737 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
3738 | { |
3739 | Dwarf_Addr retro_addr; | |
3740 | if (q.has_function_num) | |
3741 | retro_addr = q.function_num_val; | |
3742 | else | |
3743 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
3744 | comps.push_back (new probe_point::component |
3745 | (fn_or_stmt, | |
9ea68eb9 | 3746 | new literal_number(retro_addr, true))); |
37ebca01 FCE |
3747 | |
3748 | if (q.has_absolute) | |
3749 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
3750 | } |
3751 | ||
b8da0ad1 FCE |
3752 | if (q.has_call) |
3753 | comps.push_back (new probe_point::component(TOK_CALL)); | |
3754 | if (q.has_inline) | |
3755 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 3756 | if (has_return) |
b8da0ad1 FCE |
3757 | comps.push_back (new probe_point::component(TOK_RETURN)); |
3758 | if (has_maxactive) | |
3759 | comps.push_back (new probe_point::component | |
3760 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 3761 | |
5d23847d FCE |
3762 | // Overwrite it. |
3763 | this->sole_location()->components = comps; | |
2930abc7 FCE |
3764 | } |
3765 | ||
bd2b1e68 | 3766 | |
0a98fd42 | 3767 | void |
f10534c6 | 3768 | dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die, dwarf_var_expanding_visitor& v) |
0a98fd42 | 3769 | { |
9aa8ffce | 3770 | if (null_die(scope_die)) |
0a98fd42 | 3771 | return; |
0a98fd42 | 3772 | |
0a98fd42 | 3773 | string type_name; |
0a98fd42 JS |
3774 | Dwarf_Die type_die; |
3775 | ||
3776 | if (has_return && | |
3d1ad340 | 3777 | dwarf_attr_die (scope_die, DW_AT_type, &type_die) && |
f1c8f8a5 | 3778 | dwarf_type_name(&type_die, type_name)) |
d0bfd2ac | 3779 | args.push_back("$return:"+type_name); |
0a98fd42 | 3780 | |
36b155fa | 3781 | /* Pretend that we aren't in a .return for a moment, just so we can |
e29b2f5a | 3782 | * check whether variables are accessible. We don't want any of the |
36b155fa FCE |
3783 | * entry-saving code generated during listing mode. This works |
3784 | * because the set of $context variables available in a .return | |
3785 | * probe (apart from $return) is the same set as available for the | |
3786 | * corresponding .call probe, since we collect those variables at | |
3787 | * .call time. */ | |
d87623a1 JS |
3788 | bool saved_has_return = has_return; |
3789 | q.has_return = has_return = false; | |
3790 | ||
0a98fd42 | 3791 | Dwarf_Die arg; |
9aa8ffce | 3792 | vector<Dwarf_Die> scopes = q.dw.getscopes_die(scope_die); |
0a98fd42 JS |
3793 | if (dwarf_child (&scopes[0], &arg) == 0) |
3794 | do | |
3795 | { | |
3796 | switch (dwarf_tag (&arg)) | |
3797 | { | |
3798 | case DW_TAG_variable: | |
3799 | case DW_TAG_formal_parameter: | |
3800 | break; | |
3801 | ||
3802 | default: | |
3803 | continue; | |
3804 | } | |
3805 | ||
3806 | const char *arg_name = dwarf_diename (&arg); | |
3807 | if (!arg_name) | |
3808 | continue; | |
3809 | ||
3810 | type_name.clear(); | |
3d1ad340 | 3811 | if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) || |
f1c8f8a5 | 3812 | !dwarf_type_name(&type_die, type_name)) |
0a98fd42 JS |
3813 | continue; |
3814 | ||
f10534c6 WH |
3815 | /* trick from visit_target_symbol_context */ |
3816 | target_symbol *tsym = new target_symbol; | |
f1a0157a | 3817 | tsym->tok = q.base_loc->components.front()->tok; |
277c21bc JS |
3818 | tsym->name = "$"; |
3819 | tsym->name += arg_name; | |
f10534c6 WH |
3820 | |
3821 | /* Ignore any variable that isn't accessible */ | |
3822 | tsym->saved_conversion_error = 0; | |
3823 | v.require (tsym); | |
3824 | if (!tsym->saved_conversion_error) | |
d0bfd2ac | 3825 | args.push_back("$"+string(arg_name)+":"+type_name); |
0a98fd42 JS |
3826 | } |
3827 | while (dwarf_siblingof (&arg, &arg) == 0); | |
d87623a1 JS |
3828 | |
3829 | /* restore the .return status of the probe */ | |
3830 | q.has_return = has_return = saved_has_return; | |
0a98fd42 JS |
3831 | } |
3832 | ||
3833 | ||
3834 | void | |
d0bfd2ac | 3835 | dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const |
0a98fd42 | 3836 | { |
d0bfd2ac | 3837 | arg_set.insert(arg_set.end(), args.begin(), args.end()); |
0a98fd42 JS |
3838 | } |
3839 | ||
3840 | ||
27dc09b1 DB |
3841 | void |
3842 | dwarf_derived_probe::emit_unprivileged_assertion (translator_output* o) | |
3843 | { | |
3844 | if (has_process) | |
3845 | { | |
3846 | // These probes are allowed for unprivileged users, but only in the | |
3847 | // context of processes which they own. | |
3848 | emit_process_owner_assertion (o); | |
3849 | return; | |
3850 | } | |
3851 | ||
3852 | // Other probes must contain the default assertion which aborts | |
3853 | // if executed by an unprivileged user. | |
3854 | derived_probe::emit_unprivileged_assertion (o); | |
3855 | } | |
3856 | ||
3857 | ||
3858 | void | |
3859 | dwarf_derived_probe::print_dupe_stamp(ostream& o) | |
3860 | { | |
3861 | if (has_process) | |
3862 | { | |
3863 | // These probes are allowed for unprivileged users, but only in the | |
3864 | // context of processes which they own. | |
3865 | print_dupe_stamp_unprivileged_process_owner (o); | |
3866 | return; | |
3867 | } | |
3868 | ||
3869 | // Other probes must contain the default dupe stamp | |
3870 | derived_probe::print_dupe_stamp (o); | |
3871 | } | |
3872 | ||
64211010 | 3873 | |
7a053d3b | 3874 | void |
20c6c071 | 3875 | dwarf_derived_probe::register_statement_variants(match_node * root, |
27dc09b1 DB |
3876 | dwarf_builder * dw, |
3877 | bool bind_unprivileged_p) | |
bd2b1e68 | 3878 | { |
27dc09b1 DB |
3879 | root |
3880 | ->bind_unprivileged(bind_unprivileged_p) | |
3881 | ->bind(dw); | |
54efe513 GH |
3882 | } |
3883 | ||
7a053d3b | 3884 | void |
fd6602a0 | 3885 | dwarf_derived_probe::register_function_variants(match_node * root, |
27dc09b1 DB |
3886 | dwarf_builder * dw, |
3887 | bool bind_unprivileged_p) | |
2865d17a | 3888 | { |
27dc09b1 DB |
3889 | root |
3890 | ->bind_unprivileged(bind_unprivileged_p) | |
3891 | ->bind(dw); | |
3892 | root->bind(TOK_INLINE) | |
3893 | ->bind_unprivileged(bind_unprivileged_p) | |
3894 | ->bind(dw); | |
3895 | root->bind(TOK_CALL) | |
3896 | ->bind_unprivileged(bind_unprivileged_p) | |
3897 | ->bind(dw); | |
3898 | root->bind(TOK_RETURN) | |
3899 | ->bind_unprivileged(bind_unprivileged_p) | |
3900 | ->bind(dw); | |
3901 | root->bind(TOK_RETURN) | |
3902 | ->bind_unprivileged(bind_unprivileged_p) | |
3903 | ->bind_num(TOK_MAXACTIVE)->bind(dw); | |
bd2b1e68 GH |
3904 | } |
3905 | ||
7a053d3b | 3906 | void |
27dc09b1 DB |
3907 | dwarf_derived_probe::register_function_and_statement_variants( |
3908 | match_node * root, | |
3909 | dwarf_builder * dw, | |
3910 | bool bind_unprivileged_p | |
3911 | ) | |
bd2b1e68 GH |
3912 | { |
3913 | // Here we match 4 forms: | |
3914 | // | |
3915 | // .function("foo") | |
3916 | // .function(0xdeadbeef) | |
3917 | // .statement("foo") | |
3918 | // .statement(0xdeadbeef) | |
3919 | ||
27dc09b1 DB |
3920 | register_function_variants(root->bind_str(TOK_FUNCTION), dw, bind_unprivileged_p); |
3921 | register_function_variants(root->bind_num(TOK_FUNCTION), dw, bind_unprivileged_p); | |
3922 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw, bind_unprivileged_p); | |
3923 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw, bind_unprivileged_p); | |
bd2b1e68 GH |
3924 | } |
3925 | ||
3926 | void | |
c4ce66a1 | 3927 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 3928 | { |
c4ce66a1 | 3929 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
3930 | dwarf_builder *dw = new dwarf_builder(); |
3931 | ||
c4ce66a1 JS |
3932 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
3933 | s.code_filters.push_back(filter); | |
3934 | ||
27dc09b1 DB |
3935 | register_function_and_statement_variants(root->bind(TOK_KERNEL), dw); |
3936 | register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw); | |
3937 | ||
3938 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
3939 | ->bind(dw); | |
3940 | root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL) | |
3941 | ->bind(dw); | |
d2c9ec9b | 3942 | |
27dc09b1 DB |
3943 | register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw, |
3944 | true/*bind_unprivileged*/); | |
d2c9ec9b | 3945 | root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL) |
27dc09b1 | 3946 | ->bind_unprivileged() |
d2c9ec9b | 3947 | ->bind(dw); |
63b4fd14 | 3948 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_MARK) |
27dc09b1 | 3949 | ->bind_unprivileged() |
63b4fd14 | 3950 | ->bind(dw); |
a794dbeb FCE |
3951 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) |
3952 | ->bind_unprivileged() | |
3953 | ->bind(dw); | |
d2c9ec9b | 3954 | root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK) |
27dc09b1 | 3955 | ->bind_unprivileged() |
d2c9ec9b | 3956 | ->bind(dw); |
a794dbeb FCE |
3957 | root->bind_str(TOK_PROCESS)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) |
3958 | ->bind_unprivileged() | |
3959 | ->bind(dw); | |
3960 | // XXX what uses this? | |
d2c9ec9b | 3961 | root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK) |
27dc09b1 | 3962 | ->bind_unprivileged() |
d2c9ec9b | 3963 | ->bind(dw); |
bd2b1e68 GH |
3964 | } |
3965 | ||
9020300d FCE |
3966 | void |
3967 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
3968 | { | |
b95e2b79 MH |
3969 | if (access_vars) |
3970 | { | |
3971 | // if accessing $variables, emit bsp cache setup for speeding up | |
3972 | o->newline() << "bspcache(c->unwaddr, c->regs);"; | |
3973 | } | |
9020300d | 3974 | } |
2930abc7 | 3975 | |
b20febf3 | 3976 | // ------------------------------------------------------------------------ |
46b84a80 DS |
3977 | |
3978 | void | |
b20febf3 | 3979 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 3980 | { |
b20febf3 | 3981 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
3982 | |
3983 | // XXX: probes put at the same address should all share a | |
3984 | // single kprobe/kretprobe, and have their handlers executed | |
3985 | // sequentially. | |
b55bc428 FCE |
3986 | } |
3987 | ||
7a053d3b | 3988 | void |
775d51e5 | 3989 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 3990 | { |
b20febf3 | 3991 | if (probes_by_module.empty()) return; |
2930abc7 | 3992 | |
775d51e5 DS |
3993 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
3994 | ||
3995 | // Warn of misconfigured kernels | |
f41595cc FCE |
3996 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
3997 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
3998 | s.op->newline() << "#endif"; | |
775d51e5 | 3999 | s.op->newline(); |
f41595cc | 4000 | |
f07c3b68 | 4001 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 4002 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
4003 | s.op->newline() << "#endif"; |
4004 | ||
14cf7e42 SC |
4005 | // Forward decls |
4006 | s.op->newline() << "#include \"kprobes-common.h\""; | |
4007 | ||
b20febf3 FCE |
4008 | // Forward declare the master entry functions |
4009 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4010 | s.op->line() << " struct pt_regs *regs);"; | |
4011 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4012 | s.op->line() << " struct pt_regs *regs);"; | |
4013 | ||
42cb22bd MH |
4014 | // Emit an array of kprobe/kretprobe pointers |
4015 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4016 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
4017 | s.op->newline() << "#endif"; | |
4018 | ||
b20febf3 | 4019 | // Emit the actual probe list. |
606fd9c8 FCE |
4020 | |
4021 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
4022 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
4023 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
14cf7e42 | 4024 | s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
606fd9c8 FCE |
4025 | // NB: bss! |
4026 | ||
4c2732a1 | 4027 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
4028 | s.op->newline(1) << "const unsigned return_p:1;"; |
4029 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 4030 | s.op->newline() << "const unsigned optional_p:1;"; |
b20febf3 | 4031 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 4032 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 | 4033 | |
af234c40 JS |
4034 | // data saved in the kretprobe_instance packet |
4035 | s.op->newline() << "const unsigned short saved_longs;"; | |
4036 | s.op->newline() << "const unsigned short saved_strings;"; | |
4037 | ||
faea5e16 | 4038 | // Let's find some stats for the embedded strings. Maybe they |
606fd9c8 FCE |
4039 | // are small and uniform enough to justify putting char[MAX]'s into |
4040 | // the array instead of relocated char*'s. | |
faea5e16 JS |
4041 | size_t module_name_max = 0, section_name_max = 0; |
4042 | size_t module_name_tot = 0, section_name_tot = 0; | |
606fd9c8 FCE |
4043 | size_t all_name_cnt = probes_by_module.size(); // for average |
4044 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4045 | { | |
4046 | dwarf_derived_probe* p = it->second; | |
4047 | #define DOIT(var,expr) do { \ | |
4048 | size_t var##_size = (expr) + 1; \ | |
4049 | var##_max = max (var##_max, var##_size); \ | |
4050 | var##_tot += var##_size; } while (0) | |
4051 | DOIT(module_name, p->module.size()); | |
4052 | DOIT(section_name, p->section.size()); | |
606fd9c8 FCE |
4053 | #undef DOIT |
4054 | } | |
4055 | ||
4056 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
4057 | // the amount of average waste (max - avg) to the relocation data size | |
4058 | // (3 native long words). | |
4059 | #define CALCIT(var) \ | |
4060 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
4061 | { \ | |
4062 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
4063 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
4064 | << "[" << var##_name_max << "]" << endl; \ | |
4065 | } \ | |
4066 | else \ | |
4067 | { \ | |
b0986e7a | 4068 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
4069 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
4070 | } | |
4071 | ||
4072 | CALCIT(module); | |
4073 | CALCIT(section); | |
e6fe60e7 | 4074 | #undef CALCIT |
606fd9c8 | 4075 | |
b0986e7a | 4076 | s.op->newline() << "const unsigned long address;"; |
faea5e16 | 4077 | s.op->newline() << "struct stap_probe probe;"; |
af234c40 | 4078 | s.op->newline() << "void (* const entry_ph) (struct context*);"; |
b642c901 SC |
4079 | s.op->newline() << "const unsigned long sdt_sem_offset;"; |
4080 | s.op->newline() << "unsigned long sdt_sem_address;"; | |
4081 | s.op->newline() << "struct task_struct *tsk;"; | |
4082 | s.op->newline() << "const char *pathname;"; | |
4083 | s.op->newline() << "struct stap_task_finder_target finder;"; | |
b20febf3 FCE |
4084 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
4085 | s.op->indent(1); | |
4086 | ||
4087 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 4088 | { |
b20febf3 FCE |
4089 | dwarf_derived_probe* p = it->second; |
4090 | s.op->newline() << "{"; | |
4091 | if (p->has_return) | |
4092 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 4093 | if (p->has_maxactive) |
606fd9c8 FCE |
4094 | { |
4095 | s.op->line() << " .maxactive_p=1,"; | |
4096 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
4097 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
4098 | } | |
af234c40 JS |
4099 | if (p->saved_longs || p->saved_strings) |
4100 | { | |
4101 | if (p->saved_longs) | |
4102 | s.op->line() << " .saved_longs=" << p->saved_longs << ","; | |
4103 | if (p->saved_strings) | |
4104 | s.op->line() << " .saved_strings=" << p->saved_strings << ","; | |
4105 | if (p->entry_handler) | |
4106 | s.op->line() << " .entry_ph=&" << p->entry_handler->name << ","; | |
4107 | } | |
b350f56b JS |
4108 | if (p->locations[0]->optional) |
4109 | s.op->line() << " .optional_p=1,"; | |
dc38c256 | 4110 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
4111 | s.op->line() << " .module=\"" << p->module << "\","; |
4112 | s.op->line() << " .section=\"" << p->section << "\","; | |
faea5e16 | 4113 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
b642c901 SC |
4114 | if (p->sdt_semaphore_addr != 0) |
4115 | { | |
4116 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" << hex << p->sdt_semaphore_addr << dec << "ULL,"; | |
4117 | s.op->line() << " .sdt_sem_address=0,"; | |
4118 | if (p->has_library) | |
4119 | { | |
4120 | s.op->line() << " .finder={"; | |
4121 | s.op->line() << " .pid=0,"; | |
4122 | s.op->line() << " .procname=\"" << p->user_path << "\","; | |
4123 | s.op->line() << " .mmap_callback=&stap_kprobe_mmap_found,"; | |
4124 | s.op->line() << " },"; | |
4125 | s.op->line() << " .pathname=\"" << p->user_lib << "\","; | |
4126 | } | |
4127 | else | |
4128 | { | |
4129 | s.op->line() << " .finder={"; | |
4130 | s.op->line() << " .pid=0,"; | |
4131 | s.op->line() << " .procname=\"" << p->user_path << "\","; | |
4132 | s.op->line() << " .callback=&stap_kprobe_process_found,"; | |
4133 | s.op->line() << " },"; | |
4134 | s.op->line() << " .pathname=\"" << p->user_path << "\","; | |
4135 | } | |
4136 | ||
4137 | } | |
4138 | else | |
4139 | s.op->line() << " .sdt_sem_offset=0,"; | |
b642c901 | 4140 | |
b20febf3 | 4141 | s.op->line() << " },"; |
2930abc7 | 4142 | } |
2930abc7 | 4143 | |
b20febf3 FCE |
4144 | s.op->newline(-1) << "};"; |
4145 | ||
4146 | // Emit the kprobes callback function | |
4147 | s.op->newline(); | |
4148 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4149 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
4150 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
4151 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
4152 | // Check that the index is plausible | |
4153 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4154 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4155 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4156 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4157 | s.op->line() << "];"; | |
faea5e16 | 4158 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe"); |
b20febf3 | 4159 | s.op->newline() << "c->regs = regs;"; |
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);"; | |
259d54c0 | 4167 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
faea5e16 | 4168 | s.op->newline() << "(*sdp->probe.ph) (c);"; |
259d54c0 | 4169 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4170 | s.op->newline(-1) << "}"; |
4171 | ||
b20febf3 FCE |
4172 | common_probe_entryfn_epilogue (s.op); |
4173 | s.op->newline() << "return 0;"; | |
4174 | s.op->newline(-1) << "}"; | |
4175 | ||
4176 | // Same for kretprobes | |
4177 | s.op->newline(); | |
af234c40 JS |
4178 | s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,"; |
4179 | s.op->line() << " struct pt_regs *regs, int entry) {"; | |
b20febf3 | 4180 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; |
606fd9c8 FCE |
4181 | |
4182 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 4183 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
4184 | // Check that the index is plausible |
4185 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4186 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4187 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4188 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4189 | s.op->line() << "];"; | |
4190 | ||
faea5e16 | 4191 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe"); |
b20febf3 | 4192 | s.op->newline() << "c->regs = regs;"; |
af234c40 JS |
4193 | |
4194 | // for assisting runtime's backtrace logic and accessing kretprobe data packets | |
4195 | s.op->newline() << "c->pi = inst;"; | |
4196 | s.op->newline() << "c->pi_longs = sdp->saved_longs;"; | |
6415ddde MW |
4197 | |
4198 | // Make it look like the IP is set as it wouldn't have been replaced | |
4199 | // by a breakpoint instruction when calling real probe handler. Reset | |
4200 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
4201 | s.op->newline() << "{"; | |
4202 | s.op->indent(1); | |
4203 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);"; | |
5e562a69 TM |
4204 | s.op->newline() << "if (entry) {"; |
4205 | s.op->indent(1); | |
259d54c0 | 4206 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
5e562a69 TM |
4207 | s.op->newline() << "(sdp->entry_ph) (c);"; |
4208 | s.op->newline(-1) << "} else {"; | |
4209 | s.op->indent(1); | |
4210 | s.op->newline() << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);"; | |
faea5e16 | 4211 | s.op->newline() << "(sdp->probe.ph) (c);"; |
5e562a69 | 4212 | s.op->newline(-1) << "}"; |
259d54c0 | 4213 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4214 | s.op->newline(-1) << "}"; |
4215 | ||
b20febf3 FCE |
4216 | common_probe_entryfn_epilogue (s.op); |
4217 | s.op->newline() << "return 0;"; | |
4218 | s.op->newline(-1) << "}"; | |
af234c40 JS |
4219 | |
4220 | s.op->newline(); | |
4221 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4222 | s.op->line() << " struct pt_regs *regs) {"; | |
4223 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);"; | |
4224 | s.op->newline(-1) << "}"; | |
4225 | ||
4226 | s.op->newline(); | |
4227 | s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,"; | |
4228 | s.op->line() << " struct pt_regs *regs) {"; | |
4229 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);"; | |
4230 | s.op->newline(-1) << "}"; | |
b642c901 | 4231 | |
14cf7e42 SC |
4232 | s.op->newline(); |
4233 | s.op->newline() << "#include \"kprobes-common.c\""; | |
4234 | s.op->newline(); | |
20c6c071 | 4235 | } |
ec4373ff | 4236 | |
20c6c071 | 4237 | |
dc38c0ae | 4238 | void |
b20febf3 FCE |
4239 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
4240 | { | |
b642c901 SC |
4241 | p_b_m_iterator it; |
4242 | for (it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4243 | { | |
4244 | dwarf_derived_probe* p = it->second; | |
4245 | if (p->sdt_semaphore_addr != 0) | |
4246 | break; | |
4247 | } | |
4248 | if (it != probes_by_module.end()) // Ignore if there are no semaphores | |
4249 | { | |
4250 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_dwarf_probes); i++) {"; | |
4251 | s.op->newline(1) << "int rc;"; | |
4252 | s.op->newline() << "struct stap_dwarf_probe *p = &stap_dwarf_probes[i];"; | |
faea5e16 | 4253 | s.op->newline() << "probe_point = p->probe.pp;"; // for error messages |
b642c901 SC |
4254 | s.op->newline() << "if (p->sdt_sem_offset) {"; |
4255 | s.op->newline(1) << "rc = stap_register_task_finder_target(&p->finder);"; | |
4256 | s.op->newline(-1) << "}"; | |
4257 | s.op->newline() << "if (rc) break;"; | |
4258 | s.op->newline(-1) << "}"; | |
4259 | } | |
4260 | ||
b20febf3 FCE |
4261 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
4262 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 4263 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
a049e342 | 4264 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 4265 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
faea5e16 | 4266 | s.op->newline() << "probe_point = sdp->probe.pp;"; // for error messages |
b20febf3 | 4267 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 4268 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 4269 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 4270 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 4271 | s.op->newline(-1) << "} else {"; |
f07c3b68 | 4272 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
c9bad430 | 4273 | s.op->newline(-1) << "}"; |
606fd9c8 | 4274 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
af234c40 JS |
4275 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; |
4276 | s.op->newline() << "if (sdp->entry_ph) {"; | |
4277 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; | |
4278 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
4279 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
4280 | s.op->newline(-1) << "}"; | |
4281 | s.op->newline() << "#endif"; | |
e4cb375f MH |
4282 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
4283 | s.op->newline() << "#ifdef __ia64__"; | |
4284 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
4285 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4286 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4287 | s.op->newline() << "if (rc == 0) {"; | |
4288 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
4289 | s.op->newline() << "if (rc != 0)"; | |
4290 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4291 | s.op->newline(-2) << "}"; | |
4292 | s.op->newline() << "#else"; | |
606fd9c8 | 4293 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 4294 | s.op->newline() << "#endif"; |
b20febf3 | 4295 | s.op->newline(-1) << "} else {"; |
e4cb375f | 4296 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
4297 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
4298 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
4299 | s.op->newline() << "#ifdef __ia64__"; |
4300 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
4301 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4302 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4303 | s.op->newline() << "if (rc == 0) {"; | |
4304 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
4305 | s.op->newline() << "if (rc != 0)"; | |
4306 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4307 | s.op->newline(-2) << "}"; | |
4308 | s.op->newline() << "#else"; | |
606fd9c8 | 4309 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 4310 | s.op->newline() << "#endif"; |
b20febf3 | 4311 | s.op->newline(-1) << "}"; |
9063462a FCE |
4312 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
4313 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 4314 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 4315 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);"; |
b350f56b | 4316 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
9063462a FCE |
4317 | // XXX: shall we increment numskipped? |
4318 | s.op->newline(-1) << "}"; | |
4319 | ||
4320 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 4321 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 4322 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
4323 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
4324 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
4325 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
4326 | s.op->newline() << "#ifdef __ia64__"; |
4327 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
4328 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
4329 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
4330 | // not run for this early-abort case. | |
4331 | s.op->newline(-1) << "}"; | |
4332 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 4333 | s.op->newline(-1) << "}"; |
9063462a FCE |
4334 | #endif |
4335 | ||
b20febf3 FCE |
4336 | s.op->newline() << "else sdp->registered_p = 1;"; |
4337 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
4338 | } |
4339 | ||
4340 | ||
46b84a80 | 4341 | void |
b20febf3 | 4342 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 4343 | { |
b642c901 SC |
4344 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
4345 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4346 | s.op->newline() << "unsigned short sdt_semaphore = 0;"; // NB: fixed size | |
4347 | s.op->newline() << "if (sdp->sdt_sem_address && __access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 0)) {"; | |
4348 | s.op->newline(1) << "sdt_semaphore --;"; | |
4349 | s.op->newline() << "__access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 1);"; | |
4350 | s.op->newline(-1) << "}"; | |
4351 | s.op->newline(-1) << "}"; | |
4352 | ||
42cb22bd MH |
4353 | //Unregister kprobes by batch interfaces. |
4354 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4355 | s.op->newline() << "j = 0;"; | |
4356 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4357 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4358 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
4359 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4360 | s.op->newline() << "if (!sdp->return_p)"; | |
4361 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
4362 | s.op->newline(-2) << "}"; | |
4363 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
4364 | s.op->newline() << "j = 0;"; | |
4365 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4366 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4367 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
4368 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4369 | s.op->newline() << "if (sdp->return_p)"; | |
4370 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
4371 | s.op->newline(-2) << "}"; | |
4372 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
4373 | s.op->newline() << "#ifdef __ia64__"; |
4374 | s.op->newline() << "j = 0;"; | |
4375 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4376 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4377 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
4378 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
4379 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
4380 | s.op->newline(-1) << "}"; | |
4381 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
4382 | s.op->newline() << "#endif"; | |
42cb22bd MH |
4383 | s.op->newline() << "#endif"; |
4384 | ||
b20febf3 FCE |
4385 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
4386 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 4387 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
4388 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
4389 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 4390 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 4391 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 4392 | s.op->newline() << "#endif"; |
606fd9c8 | 4393 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; |
73209876 FCE |
4394 | s.op->newline() << "#ifdef STP_TIMING"; |
4395 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
faea5e16 | 4396 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe.pp, kp->u.krp.nmissed);"; |
73209876 | 4397 | s.op->newline(-1) << "#endif"; |
606fd9c8 | 4398 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; |
73209876 FCE |
4399 | s.op->newline() << "#ifdef STP_TIMING"; |
4400 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
faea5e16 | 4401 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe.pp, kp->u.krp.kp.nmissed);"; |
73209876 | 4402 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 4403 | s.op->newline(-1) << "} else {"; |
42cb22bd | 4404 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 4405 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 4406 | s.op->newline() << "#endif"; |
606fd9c8 | 4407 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; |
73209876 FCE |
4408 | s.op->newline() << "#ifdef STP_TIMING"; |
4409 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
faea5e16 | 4410 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe.pp, kp->u.kp.nmissed);"; |
73209876 | 4411 | s.op->newline(-1) << "#endif"; |
b20febf3 | 4412 | s.op->newline(-1) << "}"; |
e4cb375f MH |
4413 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
4414 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
4415 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
4416 | s.op->newline() << "sdp->registered_p = 0;"; |
4417 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
4418 | } |
4419 | ||
aff5d390 | 4420 | struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor |
7a05f484 | 4421 | { |
f83336a5 FCE |
4422 | sdt_uprobe_var_expanding_visitor(systemtap_session& s, |
4423 | int elf_machine, | |
4424 | const string & process_name, | |
a794dbeb | 4425 | const string & provider_name, |
aff5d390 SC |
4426 | const string & probe_name, |
4427 | const string & arg_string, | |
4428 | int arg_count): | |
f83336a5 | 4429 | session (s), process_name (process_name), |
a794dbeb | 4430 | provider_name (provider_name), probe_name (probe_name), arg_count (arg_count) |
a8ec7719 | 4431 | { |
f83336a5 FCE |
4432 | /* Register name mapping table depends on the elf machine of this particular |
4433 | probe target process/file, not upon the host. So we can't just | |
4434 | #ifdef _i686_ etc. */ | |
4435 | if (elf_machine == EM_X86_64) { | |
4436 | dwarf_regs["rax"] = dwarf_regs["eax"] = dwarf_regs["ax"] = dwarf_regs["al"] = 0; | |
4437 | dwarf_regs["rdx"] = dwarf_regs["edx"] = dwarf_regs["dx"] = dwarf_regs["dl"] = 1; | |
4438 | dwarf_regs["rcx"] = dwarf_regs["ecx"] = dwarf_regs["cx"] = dwarf_regs["cl"] = 2; | |
4439 | dwarf_regs["rbx"] = dwarf_regs["ebx"] = dwarf_regs["bx"] = dwarf_regs["bl"] = 3; | |
4440 | dwarf_regs["rsi"] = dwarf_regs["esi"] = dwarf_regs["si"] = dwarf_regs["sil"] = 4; | |
4441 | dwarf_regs["rdi"] = dwarf_regs["edi"] = dwarf_regs["di"] = dwarf_regs["dil"] = 5; | |
4442 | dwarf_regs["rbp"] = dwarf_regs["ebp"] = dwarf_regs["bp"] = 6; | |
4443 | dwarf_regs["rsp"] = dwarf_regs["esp"] = dwarf_regs["sp"] = 7; | |
4444 | dwarf_regs["r8"] = dwarf_regs["r8d"] = dwarf_regs["r8w"] = dwarf_regs["r8b"] = 8; | |
4445 | dwarf_regs["r9"] = dwarf_regs["r9d"] = dwarf_regs["r9w"] = dwarf_regs["r9b"] = 9; | |
4446 | dwarf_regs["r10"] = dwarf_regs["r10d"] = dwarf_regs["r10w"] = dwarf_regs["r10b"] = 10; | |
4447 | dwarf_regs["r11"] = dwarf_regs["r11d"] = dwarf_regs["r11w"] = dwarf_regs["r11b"] = 11; | |
4448 | dwarf_regs["r12"] = dwarf_regs["r12d"] = dwarf_regs["r12w"] = dwarf_regs["r12b"] = 12; | |
4449 | dwarf_regs["r13"] = dwarf_regs["r13d"] = dwarf_regs["r13w"] = dwarf_regs["r13b"] = 13; | |
4450 | dwarf_regs["r14"] = dwarf_regs["r14d"] = dwarf_regs["r14w"] = dwarf_regs["r14b"] = 14; | |
4451 | dwarf_regs["r15"] = dwarf_regs["r15d"] = dwarf_regs["r15w"] = dwarf_regs["r15b"] = 15; | |
4452 | } else if (elf_machine == EM_386) { | |
4453 | dwarf_regs["eax"] = dwarf_regs["ax"] = dwarf_regs["al"] = 0; | |
4454 | dwarf_regs["ecx"] = dwarf_regs["cx"] = dwarf_regs["cl"] = 1; | |
4455 | dwarf_regs["edx"] = dwarf_regs["dx"] = dwarf_regs["dl"] = 2; | |
4456 | dwarf_regs["ebx"] = dwarf_regs["bx"] = dwarf_regs["bl"] = 3; | |
4457 | dwarf_regs["esp"] = dwarf_regs["sp"] = 4; | |
4458 | dwarf_regs["ebp"] = dwarf_regs["bp"] = 5; | |
4459 | dwarf_regs["esi"] = dwarf_regs["si"] = dwarf_regs["sil"] = 6; | |
4460 | dwarf_regs["edi"] = dwarf_regs["di"] = dwarf_regs["dil"] = 7; | |
c8bc2044 | 4461 | } else if (arg_count) { |
f83336a5 FCE |
4462 | throw semantic_error (string("Unsupported architecture ") |
4463 | + "(" + process_name + " ELF code " + lex_cast(elf_machine) + ")" | |
4464 | + "for dwarfless sdt probes."); | |
4465 | } | |
4466 | ||
aff5d390 SC |
4467 | |
4468 | tokenize(arg_string, arg_tokens, " "); | |
4469 | assert(arg_count >= 0 && arg_count <= 10); | |
a8ec7719 | 4470 | } |
f83336a5 | 4471 | systemtap_session& session; |
aff5d390 | 4472 | const string & process_name; |
a794dbeb | 4473 | const string & provider_name; |
aff5d390 | 4474 | const string & probe_name; |
7a05f484 | 4475 | int arg_count; |
aff5d390 SC |
4476 | vector<string> arg_tokens; |
4477 | map<string,int> dwarf_regs; | |
4478 | ||
4479 | void visit_target_symbol (target_symbol* e); | |
4480 | void visit_defined_op (defined_op* e); | |
4481 | }; | |
4482 | ||
4483 | struct sdt_kprobe_var_expanding_visitor: public var_expanding_visitor | |
4484 | { | |
4485 | sdt_kprobe_var_expanding_visitor(const string & process_name, | |
a794dbeb | 4486 | const string & provider_name, |
aff5d390 SC |
4487 | const string & probe_name, |
4488 | const string & arg_string, | |
4489 | int arg_count): | |
a794dbeb | 4490 | process_name (process_name), provider_name (provider_name), probe_name (probe_name), |
aff5d390 SC |
4491 | arg_count (arg_count) |
4492 | { | |
4493 | tokenize(arg_string, arg_tokens, " "); | |
4494 | assert(arg_count >= 0 && arg_count <= 10); | |
4495 | } | |
4496 | const string & process_name; | |
a794dbeb | 4497 | const string & provider_name; |
aff5d390 SC |
4498 | const string & probe_name; |
4499 | int arg_count; | |
4500 | vector<string> arg_tokens; | |
7a05f484 SC |
4501 | |
4502 | void visit_target_symbol (target_symbol* e); | |
30263a73 | 4503 | void visit_defined_op (defined_op* e); |
7a05f484 SC |
4504 | }; |
4505 | ||
4506 | void | |
aff5d390 | 4507 | sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
7a05f484 | 4508 | { |
63ea4244 | 4509 | try |
7a05f484 | 4510 | { |
277c21bc | 4511 | if (e->name == "$$name") |
aff5d390 SC |
4512 | { |
4513 | if (e->addressof) | |
4514 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
7a05f484 | 4515 | |
aff5d390 SC |
4516 | literal_string *myname = new literal_string (probe_name); |
4517 | myname->tok = e->tok; | |
4518 | provide(myname); | |
4519 | return; | |
4520 | } | |
277c21bc | 4521 | if (e->name == "$$provider") |
a794dbeb FCE |
4522 | { |
4523 | if (e->addressof) | |
4524 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
4525 | ||
4526 | literal_string *myname = new literal_string (provider_name); | |
4527 | myname->tok = e->tok; | |
4528 | provide(myname); | |
4529 | return; | |
4530 | } | |
aff5d390 SC |
4531 | if (arg_count == 0) |
4532 | { | |
277c21bc | 4533 | if (startswith(e->name, "$")) |
aff5d390 | 4534 | { |
277c21bc | 4535 | // NB: Either |
aff5d390 SC |
4536 | // 1) uprobe1_type $argN or $FOO (we don't know the arg_count) |
4537 | // 2) uprobe2_type $FOO (no probe args) | |
4538 | // both of which get resolved later. | |
4539 | provide(e); | |
4540 | return; | |
4541 | } | |
4542 | else // uprobe2_type with no args | |
4543 | return; | |
4544 | } | |
63ea4244 | 4545 | |
c69a87e0 | 4546 | int argno = 0; |
a794dbeb | 4547 | // XXX: check for $arg prefix! |
c69a87e0 | 4548 | try |
aff5d390 | 4549 | { |
277c21bc | 4550 | argno = lex_cast<int>(e->name.substr(4)); |
aff5d390 | 4551 | } |
c69a87e0 | 4552 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF |
aff5d390 SC |
4553 | { |
4554 | throw semantic_error("invalid argument number", e->tok); | |
4555 | } | |
c69a87e0 | 4556 | if (argno < 1 || argno > arg_count) |
aff5d390 | 4557 | throw semantic_error("invalid argument number", e->tok); |
305a3c44 SC |
4558 | enum arg_type |
4559 | { | |
4560 | literal_arg, | |
4561 | register_arg, | |
4562 | memory_arg | |
4563 | } arg_type; | |
aff5d390 SC |
4564 | functioncall *fc = new functioncall; |
4565 | binary_expression *be = new binary_expression; | |
fc7dfc08 | 4566 | literal_number* ln = NULL; |
aff5d390 SC |
4567 | string reg; |
4568 | string disp_str; | |
4569 | int disp = 0; | |
4570 | // NB: uprobes-based sdt.h; $argFOO gets resolved later. | |
4571 | ||
4572 | string tok = arg_tokens[argno-1]; | |
4573 | ||
4574 | regex_t preg; | |
4575 | // this pattern matches 0xD(%R) | (%R) | %R | |
4576 | const char *pattern = "\\([0x]*-*[0-9]*\\)\\([(]*\\)\\(%[0-9a-z][0-9a-z][0-9a-z]*\\)\\([)]*\\)"; | |
4577 | int rc; | |
4578 | size_t nmatch = 5; | |
4579 | regmatch_t pmatch[5]; | |
277c21bc | 4580 | |
aff5d390 SC |
4581 | if (0 != (rc = regcomp(&preg, pattern, 0))) |
4582 | throw semantic_error("Failed to parse probe operand"); | |
4583 | if (0 != (rc = regexec(&preg, arg_tokens[argno-1].c_str(), nmatch, pmatch, 0))) | |
aff5d390 | 4584 | { |
305a3c44 SC |
4585 | // Do we have a numeric literal? |
4586 | // Check this separately instead of complicating the above pattern. | |
4587 | if (0 != (rc = regcomp(&preg, "\\$[0-9][0-9]*", 0))) | |
4588 | throw semantic_error("Failed to parse probe operand"); | |
4589 | if (0 != (rc = regexec(&preg, arg_tokens[argno-1].c_str(), nmatch, pmatch, 0))) | |
4590 | throw semantic_error("Unsupported assembler operand while accessing " | |
4591 | + probe_name + " " + e->name + " " + arg_tokens[argno-1], | |
4592 | e->tok); | |
4593 | arg_type = literal_arg; | |
dc4c488a | 4594 | ln = new literal_number(lex_cast<int>(arg_tokens[argno-1].substr(pmatch[0].rm_so+1))); |
305a3c44 | 4595 | ln->tok = e->tok; |
aff5d390 | 4596 | } |
305a3c44 | 4597 | else |
aff5d390 | 4598 | { |
305a3c44 SC |
4599 | // Is there a displacement? |
4600 | if (pmatch[2].rm_so > pmatch[1].rm_so) | |
4601 | { | |
dc4c488a | 4602 | string disp_str = arg_tokens[argno-1].substr(pmatch[1].rm_so); |
305a3c44 SC |
4603 | disp = lex_cast<int>(disp_str.substr(0,pmatch[1].rm_eo - pmatch[1].rm_so)); |
4604 | } | |
4605 | // Is there an indirect register? | |
4606 | if ((arg_tokens[argno-1][pmatch[2].rm_so]) == '(') | |
4607 | arg_type = memory_arg; | |
4608 | else | |
4609 | arg_type = register_arg; | |
4610 | // Is there a register? | |
4611 | if (pmatch[3].rm_eo >= pmatch[3].rm_so) | |
4612 | { | |
dc4c488a | 4613 | reg = arg_tokens[argno-1].substr(pmatch[3].rm_so+1); |
305a3c44 SC |
4614 | reg.erase(pmatch[3].rm_eo - pmatch[3].rm_so - 1); |
4615 | } | |
4616 | if (reg.length() == 0) | |
4617 | throw semantic_error("Unsupported assembler operand while accessing " | |
4618 | + probe_name + " " + e->name, e->tok); | |
4619 | // synthesize user_long(%{fetch_register(R)%} + D) | |
4620 | fc->function = "user_long"; | |
4621 | fc->tok = e->tok; | |
4622 | be->tok = e->tok; | |
4623 | ||
4624 | embedded_expr *get_arg1 = new embedded_expr; | |
4625 | get_arg1->tok = e->tok; | |
4626 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
4627 | + (is_user_module (process_name) | |
4628 | ? string("u_fetch_register(") | |
4629 | : string("k_fetch_register(")) | |
4630 | + lex_cast(dwarf_regs[reg]) + string(")"); | |
4631 | // XXX: may we ever need to cast that to a narrower type? | |
4632 | ||
4633 | be->left = get_arg1; | |
4634 | be->op = "+"; | |
4635 | literal_number* inc = new literal_number(disp); | |
4636 | inc->tok = e->tok; | |
4637 | be->right = inc; | |
4638 | fc->args.push_back(be); | |
aff5d390 | 4639 | } |
277c21bc | 4640 | |
aff5d390 SC |
4641 | if (e->components.empty()) // We have a scalar |
4642 | { | |
4643 | if (e->addressof) | |
4644 | throw semantic_error("cannot take address of sdt variable", e->tok); | |
63ea4244 | 4645 | |
305a3c44 | 4646 | if (arg_type == memory_arg) |
aff5d390 | 4647 | provide(fc); |
305a3c44 | 4648 | else if (arg_type == register_arg) |
aff5d390 | 4649 | provide(be); |
305a3c44 SC |
4650 | else if (arg_type == literal_arg) |
4651 | provide(ln); | |
aff5d390 SC |
4652 | return; |
4653 | } | |
4654 | cast_op *cast = new cast_op; | |
277c21bc | 4655 | cast->name = "@cast"; |
aff5d390 | 4656 | cast->tok = e->tok; |
305a3c44 | 4657 | if (arg_type == memory_arg) |
aff5d390 | 4658 | cast->operand = fc; |
305a3c44 | 4659 | else if (arg_type == register_arg) |
aff5d390 | 4660 | cast->operand = be; |
305a3c44 SC |
4661 | else if (arg_type == literal_arg) |
4662 | provide(ln); | |
aff5d390 | 4663 | cast->components = e->components; |
7f6b80bd | 4664 | cast->type_name = probe_name + "_arg" + lex_cast(argno); |
aff5d390 SC |
4665 | cast->module = process_name; |
4666 | ||
4667 | cast->visit(this); | |
4668 | } | |
4669 | catch (const semantic_error &er) | |
4670 | { | |
4671 | e->chain (er); | |
4672 | provide (e); | |
4673 | } | |
4674 | } | |
4675 | ||
4676 | ||
4677 | void | |
4678 | sdt_kprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
4679 | { | |
4680 | try | |
4681 | { | |
277c21bc | 4682 | if (e->name == "$$name") |
aff5d390 SC |
4683 | { |
4684 | if (e->addressof) | |
4685 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
4686 | ||
4687 | literal_string *myname = new literal_string (probe_name); | |
4688 | myname->tok = e->tok; | |
4689 | provide(myname); | |
4690 | return; | |
4691 | } | |
277c21bc | 4692 | if (e->name == "$$provider") |
a794dbeb FCE |
4693 | { |
4694 | if (e->addressof) | |
4695 | throw semantic_error("cannot take address of sdt context variable", e->tok); | |
4696 | ||
4697 | literal_string *myname = new literal_string (provider_name); | |
4698 | myname->tok = e->tok; | |
4699 | provide(myname); | |
4700 | return; | |
4701 | } | |
aff5d390 SC |
4702 | |
4703 | int argno = 0; | |
4704 | try | |
4705 | { | |
277c21bc | 4706 | argno = lex_cast<int>(e->name.substr(4)); |
aff5d390 SC |
4707 | } |
4708 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF | |
4709 | { | |
4710 | throw semantic_error("invalid argument number", e->tok); | |
4711 | } | |
4712 | if (argno < 1 || argno > arg_count) | |
4713 | throw semantic_error("invalid argument number", e->tok); | |
c69a87e0 FCE |
4714 | bool lvalue = is_active_lvalue(e); |
4715 | functioncall *fc = new functioncall; | |
63ea4244 | 4716 | |
c69a87e0 FCE |
4717 | // First two args are hidden: 1. pointer to probe name 2. task id |
4718 | if (arg_count < 2) | |
aff5d390 SC |
4719 | { |
4720 | fc->function = "ulong_arg"; | |
4721 | fc->type = pe_long; | |
4722 | fc->tok = e->tok; | |
4723 | // skip the hidden args | |
4724 | literal_number* num = new literal_number(argno + 2); | |
4725 | num->tok = e->tok; | |
4726 | fc->args.push_back(num); | |
4727 | } | |
4728 | else | |
4729 | { | |
4730 | // args are passed in arg3 as members of a struct | |
4731 | fc->function = "user_long"; | |
4732 | fc->tok = e->tok; | |
4733 | binary_expression *be = new binary_expression; | |
4734 | be->tok = e->tok; | |
4735 | functioncall *get_arg1 = new functioncall; | |
4736 | get_arg1->function = "pointer_arg"; | |
4737 | get_arg1->tok = e->tok; | |
4738 | // arg3 is the pointer to a struct of arguments | |
4739 | literal_number* num = new literal_number(3); | |
4740 | num->tok = e->tok; | |
4741 | get_arg1->args.push_back(num); | |
4742 | ||
4743 | be->left = get_arg1; | |
4744 | be->op = "+"; | |
4745 | // offset in struct to the desired arg | |
4746 | literal_number* inc = new literal_number((argno - 1) * 8); | |
4747 | inc->tok = e->tok; | |
4748 | be->right = inc; | |
4749 | fc->args.push_back(be); | |
4750 | } | |
c69a87e0 | 4751 | if (lvalue) |
aff5d390 | 4752 | *(target_symbol_setter_functioncalls.top()) = fc; |
63ea4244 | 4753 | |
aff5d390 SC |
4754 | if (e->components.empty()) // We have a scalar |
4755 | { | |
4756 | if (e->addressof) | |
4757 | throw semantic_error("cannot take address of sdt variable", e->tok); | |
63ea4244 | 4758 | |
aff5d390 SC |
4759 | provide(fc); |
4760 | return; | |
4761 | } | |
c69a87e0 | 4762 | cast_op *cast = new cast_op; |
277c21bc | 4763 | cast->name = "@cast"; |
c69a87e0 FCE |
4764 | cast->tok = e->tok; |
4765 | cast->operand = fc; | |
4766 | cast->components = e->components; | |
7f6b80bd | 4767 | cast->type_name = probe_name + "_arg" + lex_cast(argno); |
c69a87e0 | 4768 | cast->module = process_name; |
63ea4244 | 4769 | |
c69a87e0 | 4770 | cast->visit(this); |
7a05f484 | 4771 | } |
c69a87e0 | 4772 | catch (const semantic_error &er) |
ad002306 | 4773 | { |
1af1e62d | 4774 | e->chain (er); |
c69a87e0 | 4775 | provide (e); |
ad002306 | 4776 | } |
7a05f484 | 4777 | } |
46b84a80 | 4778 | |
277c21bc | 4779 | |
30263a73 | 4780 | // See var_expanding_visitor::visit_defined_op for a background on |
aff5d390 | 4781 | // this callback, |
30263a73 | 4782 | void |
aff5d390 | 4783 | sdt_uprobe_var_expanding_visitor::visit_defined_op (defined_op *e) |
30263a73 | 4784 | { |
aff5d390 SC |
4785 | provide (e); |
4786 | } | |
4787 | ||
4788 | void | |
4789 | sdt_kprobe_var_expanding_visitor::visit_defined_op (defined_op *e) | |
4790 | { | |
4791 | var_expanding_visitor::visit_defined_op (e); | |
30263a73 FCE |
4792 | } |
4793 | ||
4794 | ||
edce5b67 JS |
4795 | struct sdt_query : public base_query |
4796 | { | |
4797 | sdt_query(probe * base_probe, probe_point * base_loc, | |
4798 | dwflpp & dw, literal_map_t const & params, | |
4799 | vector<derived_probe *> & results); | |
4800 | ||
4801 | void handle_query_module(); | |
4802 | ||
4803 | private: | |
15284963 | 4804 | stap_sdt_probe_type probe_type; |
edce5b67 JS |
4805 | probe * base_probe; |
4806 | probe_point * base_loc; | |
6846cfc8 | 4807 | literal_map_t const & params; |
edce5b67 | 4808 | vector<derived_probe *> & results; |
a794dbeb FCE |
4809 | string pp_mark; |
4810 | string pp_provider; | |
edce5b67 JS |
4811 | |
4812 | set<string> probes_handled; | |
4813 | ||
4814 | Elf_Data *pdata; | |
4815 | size_t probe_scn_offset; | |
4816 | size_t probe_scn_addr; | |
aff5d390 SC |
4817 | uint64_t arg_count; |
4818 | uint64_t pc; | |
4819 | string arg_string; | |
edce5b67 | 4820 | string probe_name; |
a794dbeb | 4821 | string provider_name; |
edce5b67 JS |
4822 | |
4823 | bool init_probe_scn(); | |
4824 | bool get_next_probe(); | |
4825 | ||
4826 | void convert_probe(probe *base); | |
4ddb6dd0 | 4827 | void record_semaphore(vector<derived_probe *> & results, unsigned start); |
c72aa911 | 4828 | probe* convert_location(); |
aff5d390 SC |
4829 | bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type;} |
4830 | bool have_kprobe() {return probe_type == kprobe1_type || probe_type == kprobe2_type;} | |
edce5b67 JS |
4831 | }; |
4832 | ||
4833 | ||
4834 | sdt_query::sdt_query(probe * base_probe, probe_point * base_loc, | |
4835 | dwflpp & dw, literal_map_t const & params, | |
4836 | vector<derived_probe *> & results): | |
4837 | base_query(dw, params), base_probe(base_probe), | |
6846cfc8 | 4838 | base_loc(base_loc), params(params), results(results) |
edce5b67 | 4839 | { |
a794dbeb FCE |
4840 | assert(get_string_param(params, TOK_MARK, pp_mark)); |
4841 | get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified | |
4842 | ||
ef428667 FCE |
4843 | // PR10245: permit usage of dtrace-y "-" separator in marker name; |
4844 | // map it to double-underscores. | |
4845 | size_t pos = 0; | |
4846 | while (1) // there may be more than one | |
4847 | { | |
a794dbeb | 4848 | size_t i = pp_mark.find("-", pos); |
ef428667 | 4849 | if (i == string::npos) break; |
a794dbeb | 4850 | pp_mark.replace (i, 1, "__"); |
ef428667 FCE |
4851 | pos = i+1; // resume searching after the inserted __ |
4852 | } | |
a794dbeb FCE |
4853 | |
4854 | // XXX: same for pp_provider? | |
edce5b67 JS |
4855 | } |
4856 | ||
4857 | ||
4858 | void | |
4859 | sdt_query::handle_query_module() | |
4860 | { | |
4861 | if (!init_probe_scn()) | |
4862 | return; | |
4863 | ||
4864 | if (sess.verbose > 3) | |
a794dbeb | 4865 | clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl; |
edce5b67 JS |
4866 | |
4867 | while (get_next_probe()) | |
4868 | { | |
aff5d390 | 4869 | if (! have_uprobe() |
696ec154 | 4870 | && !probes_handled.insert(probe_name).second) |
edce5b67 JS |
4871 | continue; |
4872 | ||
39a3e397 SC |
4873 | if (sess.verbose > 3) |
4874 | { | |
4875 | clog << "matched probe_name " << probe_name << " probe_type "; | |
4876 | switch (probe_type) | |
4877 | { | |
aff5d390 SC |
4878 | case uprobe1_type: |
4879 | clog << "uprobe1 at 0x" << hex << pc << dec << endl; | |
4880 | break; | |
4881 | case uprobe2_type: | |
4882 | clog << "uprobe2 at 0x" << hex << pc << dec << endl; | |
4883 | break; | |
4884 | case kprobe1_type: | |
4885 | clog << "kprobe1" << endl; | |
39a3e397 | 4886 | break; |
aff5d390 SC |
4887 | case kprobe2_type: |
4888 | clog << "kprobe2" << endl; | |
39a3e397 | 4889 | break; |
39a3e397 SC |
4890 | } |
4891 | } | |
30263a73 | 4892 | |
c72aa911 JS |
4893 | // Extend the derivation chain |
4894 | probe *new_base = convert_location(); | |
4895 | probe_point *new_location = new_base->locations[0]; | |
edce5b67 | 4896 | |
aff5d390 SC |
4897 | bool kprobe_found = false; |
4898 | if (have_kprobe()) | |
edce5b67 JS |
4899 | { |
4900 | convert_probe(new_base); | |
aff5d390 SC |
4901 | kprobe_found = true; |
4902 | // Expand the local variables in the probe body | |
4903 | sdt_kprobe_var_expanding_visitor svv (module_val, | |
a794dbeb | 4904 | provider_name, |
aff5d390 SC |
4905 | probe_name, |
4906 | arg_string, | |
4907 | arg_count); | |
4908 | svv.replace (new_base->body); | |
edce5b67 | 4909 | } |
aff5d390 SC |
4910 | else |
4911 | { | |
f83336a5 FCE |
4912 | /* Figure out the architecture of this particular ELF file. |
4913 | The dwarfless register-name mappings depend on it. */ | |
4914 | Dwarf_Addr bias; | |
4915 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.mod_info->mod, &bias)) | |
4916 | ?: dwfl_module_getelf (dw.mod_info->mod, &bias)); | |
4917 | GElf_Ehdr ehdr_mem; | |
4918 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
4919 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } | |
4920 | int elf_machine = em->e_machine; | |
4921 | sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, | |
4922 | module_val, | |
a794dbeb | 4923 | provider_name, |
aff5d390 SC |
4924 | probe_name, |
4925 | arg_string, | |
4926 | arg_count); | |
4927 | svv.replace (new_base->body); | |
4928 | } | |
4929 | ||
edce5b67 JS |
4930 | unsigned i = results.size(); |
4931 | ||
aff5d390 | 4932 | if (have_kprobe()) |
4ddb6dd0 JS |
4933 | derive_probes(sess, new_base, results); |
4934 | ||
edce5b67 JS |
4935 | else |
4936 | { | |
a794dbeb | 4937 | // XXX: why not derive_probes() in the uprobes case case too? |
edce5b67 JS |
4938 | literal_map_t params; |
4939 | for (unsigned i = 0; i < new_location->components.size(); ++i) | |
4940 | { | |
4941 | probe_point::component *c = new_location->components[i]; | |
4942 | params[c->functor] = c->arg; | |
4943 | } | |
4944 | ||
aff5d390 SC |
4945 | dwarf_query q(new_base, new_location, dw, params, results, "", ""); |
4946 | q.has_mark = true; // enables mid-statement probing | |
edce5b67 | 4947 | |
aff5d390 SC |
4948 | if (probe_type == uprobe1_type) |
4949 | dw.iterate_over_modules(&query_module, &q); | |
4950 | else if (probe_type == uprobe2_type) | |
4951 | { | |
4952 | Dwarf_Addr bias; | |
4953 | string section; | |
4954 | Elf* elf = dwfl_module_getelf (q.dw.mod_info->mod, &bias); | |
4955 | assert(elf); | |
4956 | Dwarf_Addr reloc_addr = q.statement_num_val + bias; | |
4957 | if (dwfl_module_relocations (q.dw.mod_info->mod) > 0) | |
4958 | { | |
4959 | dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr); | |
4960 | section = ".dynamic"; | |
4961 | } | |
4962 | else | |
4963 | section = ".absolute"; | |
4964 | ||
4965 | uprobe_derived_probe* p = | |
4966 | new uprobe_derived_probe ("", "", 0, q.module_val, section, | |
4967 | q.statement_num_val, reloc_addr, q, 0); | |
4968 | results.push_back (p); | |
4969 | } | |
4970 | } | |
4ddb6dd0 | 4971 | record_semaphore(results, i); |
edce5b67 JS |
4972 | } |
4973 | } | |
4974 | ||
4975 | ||
4976 | bool | |
4977 | sdt_query::init_probe_scn() | |
4978 | { | |
4979 | Elf* elf; | |
4980 | GElf_Shdr shdr_mem; | |
4981 | GElf_Shdr *shdr = NULL; | |
4982 | Dwarf_Addr bias; | |
4983 | size_t shstrndx; | |
4984 | ||
4985 | // Explicitly look in the main elf file first. | |
4986 | elf = dwfl_module_getelf (dw.module, &bias); | |
4987 | Elf_Scn *probe_scn = NULL; | |
4988 | ||
4989 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); | |
4990 | ||
4991 | bool have_probes = false; | |
4992 | ||
4993 | // Is there a .probes section? | |
4994 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
4995 | { | |
4996 | shdr = gelf_getshdr (probe_scn, &shdr_mem); | |
4997 | assert (shdr != NULL); | |
4998 | ||
4999 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0) | |
5000 | { | |
5001 | have_probes = true; | |
5002 | break; | |
5003 | } | |
5004 | } | |
5005 | ||
5006 | // Older versions put .probes section in the debuginfo dwarf file, | |
5007 | // so check if it actually exists, if not take a look in the debuginfo file | |
5008 | if (! have_probes || (have_probes && shdr->sh_type == SHT_NOBITS)) | |
5009 | { | |
5010 | elf = dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias)); | |
5011 | if (! elf) | |
5012 | return false; | |
5013 | dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx)); | |
5014 | probe_scn = NULL; | |
5015 | while ((probe_scn = elf_nextscn (elf, probe_scn))) | |
5016 | { | |
5017 | shdr = gelf_getshdr (probe_scn, &shdr_mem); | |
5018 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), | |
5019 | ".probes") == 0) | |
aff5d390 SC |
5020 | { |
5021 | have_probes = true; | |
5022 | break; | |
5023 | } | |
edce5b67 JS |
5024 | } |
5025 | } | |
5026 | ||
5027 | if (!have_probes) | |
5028 | return false; | |
5029 | ||
5030 | pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); | |
5031 | probe_scn_offset = 0; | |
5032 | probe_scn_addr = shdr->sh_addr; | |
5033 | assert (pdata != NULL); | |
5034 | if (sess.verbose > 4) | |
5035 | clog << "got .probes elf scn_addr@0x" << probe_scn_addr << dec | |
5036 | << ", size: " << pdata->d_size << endl; | |
5037 | return true; | |
5038 | } | |
5039 | ||
5040 | bool | |
5041 | sdt_query::get_next_probe() | |
5042 | { | |
3a31e709 SC |
5043 | // Extract probe info from the .probes section, e.g. |
5044 | // 74657374 5f70726f 62655f32 00000000 test_probe_2.... | |
5045 | // 50524233 00000000 980c2000 00000000 PRB3...... ..... | |
5046 | // 01000000 00000000 00000000 00000000 ................ | |
5047 | // test_probe_2 is probe_name, probe_type is 50524233, | |
5048 | // *probe_name (pbe->name) is 980c2000, probe_arg (pbe->arg) is 1 | |
5049 | // probe_scn_offset is position currently being scanned in .probes | |
edce5b67 JS |
5050 | |
5051 | while (probe_scn_offset < pdata->d_size) | |
5052 | { | |
aff5d390 SC |
5053 | stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset); |
5054 | stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset); | |
15284963 | 5055 | probe_type = (stap_sdt_probe_type)(pbe_v1->type_a); |
aff5d390 | 5056 | if (! have_uprobe() && ! have_kprobe()) |
edce5b67 JS |
5057 | { |
5058 | // Unless this is a mangled .probes section, this happens | |
5059 | // because the name of the probe comes first, followed by | |
5060 | // the sentinel. | |
5061 | if (sess.verbose > 5) | |
5062 | clog << "got unknown probe_type: 0x" << hex << probe_type | |
5063 | << dec << endl; | |
5064 | probe_scn_offset += sizeof(__uint32_t); | |
5065 | continue; | |
5066 | } | |
aff5d390 SC |
5067 | if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b |
5068 | { | |
5069 | pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t)); | |
5070 | if (pbe_v1->type_b != uprobe1_type && pbe_v1->type_b != kprobe1_type) | |
5071 | continue; | |
5072 | } | |
5073 | ||
5074 | if (probe_type == uprobe1_type || probe_type == kprobe1_type) | |
5075 | { | |
5076 | probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr); | |
a794dbeb | 5077 | provider_name = ""; // unknown |
aff5d390 SC |
5078 | if (probe_type == uprobe1_type) |
5079 | { | |
5080 | pc = pbe_v1->arg; | |
5081 | arg_count = 0; | |
5082 | } | |
5083 | else if (probe_type == kprobe1_type) | |
5084 | arg_count = pbe_v1->arg; | |
5085 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v1); | |
5086 | } | |
5087 | else if (probe_type == uprobe2_type || probe_type == kprobe2_type) | |
5088 | { | |
5089 | probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr); | |
a794dbeb | 5090 | provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr); |
aff5d390 SC |
5091 | arg_count = pbe_v2->arg_count; |
5092 | pc = pbe_v2->pc; | |
5093 | if (pbe_v2->arg_string) | |
5094 | arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr); | |
5095 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v2); | |
5096 | } | |
edce5b67 | 5097 | if (sess.verbose > 4) |
a794dbeb | 5098 | clog << "saw .probes " << probe_name << (provider_name != "" ? " (provider "+provider_name+") " : "") |
aff5d390 | 5099 | << "@0x" << hex << pc << dec << endl; |
edce5b67 | 5100 | |
a794dbeb FCE |
5101 | if (dw.function_name_matches_pattern (probe_name, pp_mark) |
5102 | && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider))) | |
39a3e397 | 5103 | return true; |
edce5b67 JS |
5104 | else |
5105 | continue; | |
5106 | } | |
5107 | return false; | |
5108 | } | |
5109 | ||
5110 | ||
6846cfc8 | 5111 | void |
4ddb6dd0 | 5112 | sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start) |
6846cfc8 | 5113 | { |
a794dbeb FCE |
5114 | for (unsigned i=0; i<2; i++) { |
5115 | // prefer with-provider symbol; look without provider prefix for backward compatibility only | |
5116 | string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore"; | |
5117 | // XXX: multiple addresses? | |
5118 | if (sess.verbose > 2) | |
5119 | clog << "looking for semaphore symbol " << semaphore; | |
5120 | ||
5121 | Dwarf_Addr addr = lookup_symbol_address(dw.module, semaphore.c_str()); | |
5122 | if (addr) | |
5123 | { | |
5124 | if (dwfl_module_relocations (dw.module) > 0) | |
5125 | dwfl_module_relocate_address (dw.module, &addr); | |
5126 | // XXX: relocation basis? | |
5127 | for (unsigned i = start; i < results.size(); ++i) | |
5128 | results[i]->sdt_semaphore_addr = addr; | |
5129 | if (sess.verbose > 2) | |
5130 | clog << ", found at 0x" << hex << addr << dec << endl; | |
5131 | return; | |
5132 | } | |
5133 | else | |
5134 | if (sess.verbose > 2) | |
5135 | clog << ", not found" << endl; | |
5136 | } | |
6846cfc8 SC |
5137 | } |
5138 | ||
5139 | ||
edce5b67 JS |
5140 | void |
5141 | sdt_query::convert_probe (probe *base) | |
5142 | { | |
5143 | block *b = new block; | |
5144 | b->tok = base->body->tok; | |
5145 | ||
5146 | // XXX: Does this also need to happen for i386 under x86_64 stap? | |
a794dbeb | 5147 | #ifdef __i386__ /* XXX: probably s.architecture[] instead */ |
aff5d390 | 5148 | if (have_kprobe()) |
edce5b67 JS |
5149 | { |
5150 | functioncall *rp = new functioncall; | |
edce5b67 JS |
5151 | rp->function = "regparm"; |
5152 | rp->tok = b->tok; | |
5153 | literal_number* littid = new literal_number(0); | |
5154 | littid->tok = b->tok; | |
5155 | rp->args.push_back(littid); | |
5156 | expr_statement* es = new expr_statement; | |
5157 | es->tok = b->tok; | |
5158 | es->value = rp; | |
5159 | b->statements.push_back(es); | |
5160 | } | |
5161 | #endif | |
5162 | ||
aff5d390 | 5163 | if (have_kprobe()) |
edce5b67 | 5164 | { |
aff5d390 | 5165 | // Generate: if (arg2 != kprobe2_type) next; |
edce5b67 JS |
5166 | if_statement *istid = new if_statement; |
5167 | istid->thenblock = new next_statement; | |
5168 | istid->elseblock = NULL; | |
5169 | istid->tok = b->tok; | |
63ea4244 | 5170 | istid->thenblock->tok = b->tok; |
edce5b67 JS |
5171 | comparison *betid = new comparison; |
5172 | betid->op = "!="; | |
5173 | betid->tok = b->tok; | |
5174 | ||
5175 | functioncall *arg2 = new functioncall; | |
5176 | arg2->function = "ulong_arg"; | |
5177 | arg2->tok = b->tok; | |
5178 | literal_number* num = new literal_number(2); | |
5179 | num->tok = b->tok; | |
5180 | arg2->args.push_back(num); | |
5181 | ||
5182 | betid->left = arg2; | |
aff5d390 | 5183 | literal_number* littid = new literal_number(probe_type); |
edce5b67 JS |
5184 | littid->tok = b->tok; |
5185 | betid->right = littid; | |
5186 | istid->condition = betid; | |
5187 | b->statements.push_back(istid); | |
5188 | } | |
5189 | ||
5190 | // Generate: if (arg1 != mark("label")) next; | |
5191 | functioncall *fc = new functioncall; | |
bbafcb1e | 5192 | fc->function = "ulong_arg"; |
edce5b67 | 5193 | fc->tok = b->tok; |
bbafcb1e | 5194 | literal_number* num = new literal_number(1); |
edce5b67 JS |
5195 | num->tok = b->tok; |
5196 | fc->args.push_back(num); | |
5197 | ||
5198 | functioncall *fcus = new functioncall; | |
5199 | fcus->function = "user_string"; | |
5200 | fcus->type = pe_string; | |
5201 | fcus->tok = b->tok; | |
5202 | fcus->args.push_back(fc); | |
5203 | ||
5204 | if_statement *is = new if_statement; | |
5205 | is->thenblock = new next_statement; | |
5206 | is->elseblock = NULL; | |
5207 | is->tok = b->tok; | |
63ea4244 | 5208 | is->thenblock->tok = b->tok; |
edce5b67 JS |
5209 | comparison *be = new comparison; |
5210 | be->op = "!="; | |
5211 | be->tok = b->tok; | |
5212 | be->left = fcus; | |
5213 | be->right = new literal_string(probe_name); | |
63ea4244 | 5214 | be->right->tok = b->tok; |
edce5b67 JS |
5215 | is->condition = be; |
5216 | b->statements.push_back(is); | |
5217 | ||
5218 | // Now replace the body | |
5219 | b->statements.push_back(base->body); | |
5220 | base->body = b; | |
5221 | } | |
5222 | ||
5223 | ||
c72aa911 JS |
5224 | probe* |
5225 | sdt_query::convert_location () | |
edce5b67 | 5226 | { |
c72aa911 JS |
5227 | probe_point* specific_loc = new probe_point(*base_loc); |
5228 | probe_point* derived_loc = new probe_point(*base_loc); | |
edce5b67 | 5229 | |
c72aa911 JS |
5230 | for (unsigned i = 0; i < derived_loc->components.size(); ++i) |
5231 | if (derived_loc->components[i]->functor == TOK_MARK) | |
5232 | { | |
5233 | // replace the possibly wildcarded arg with the specific marker name | |
5234 | specific_loc->components[i] = | |
5235 | new probe_point::component(TOK_MARK, new literal_string(probe_name)); | |
edce5b67 | 5236 | |
a794dbeb FCE |
5237 | // XXX: similarly, fill in a TOK_PROVIDER() on the base_loc if |
5238 | // one was missing/unspecified/wildcarded. | |
5239 | ||
aff5d390 SC |
5240 | if (sess.verbose > 3) |
5241 | switch (probe_type) | |
5242 | { | |
5243 | case uprobe1_type: | |
5244 | clog << "probe_type == uprobe1, use statement addr: 0x" | |
5245 | << hex << pc << dec << endl; | |
5246 | break; | |
5247 | case uprobe2_type: | |
5248 | clog << "probe_type == uprobe2, use statement addr: 0x" | |
5249 | << hex << pc << dec << endl; | |
5250 | break; | |
5251 | ||
5252 | case kprobe1_type: | |
5253 | clog << "probe_type == kprobe1" << endl; | |
5254 | break; | |
5255 | case kprobe2_type: | |
5256 | clog << "probe_type == kprobe2" << endl; | |
5257 | break; | |
5258 | default: | |
5259 | clog << "probe_type == use_uprobe_no_dwarf, use label name: " | |
a794dbeb | 5260 | << "_stapprobe1_" << pp_mark << endl; |
aff5d390 SC |
5261 | } |
5262 | ||
c72aa911 JS |
5263 | switch (probe_type) |
5264 | { | |
aff5d390 SC |
5265 | case uprobe1_type: |
5266 | case uprobe2_type: | |
c72aa911 JS |
5267 | // process("executable").statement(probe_arg) |
5268 | derived_loc->components[i] = | |
9ea68eb9 | 5269 | new probe_point::component(TOK_STATEMENT, |
aff5d390 | 5270 | new literal_number(pc, true)); |
c72aa911 JS |
5271 | break; |
5272 | ||
aff5d390 SC |
5273 | case kprobe1_type: |
5274 | case kprobe2_type: | |
b642c901 SC |
5275 | // kernel.function("*getegid*") |
5276 | derived_loc->components[i] = | |
5277 | new probe_point::component(TOK_FUNCTION, new literal_string("*getegid*")); | |
5278 | if (derived_loc->components[i - 1]->functor == TOK_LIBRARY) | |
5279 | derived_loc->components.erase (derived_loc->components.begin() + i - 1); | |
5280 | break; | |
c72aa911 | 5281 | |
a794dbeb | 5282 | default: // deprecated |
c72aa911 JS |
5283 | // process("executable").function("*").label("_stapprobe1_MARK_NAME") |
5284 | derived_loc->components[i] = | |
5285 | new probe_point::component(TOK_FUNCTION, new literal_string("*")); | |
5286 | derived_loc->components.push_back | |
5287 | (new probe_point::component(TOK_LABEL, | |
a794dbeb | 5288 | new literal_string("_stapprobe1_" + pp_mark))); |
c72aa911 JS |
5289 | break; |
5290 | } | |
5291 | } | |
5292 | else if (derived_loc->components[i]->functor == TOK_PROCESS | |
aff5d390 | 5293 | && have_kprobe()) |
63b4fd14 | 5294 | { |
c72aa911 | 5295 | derived_loc->components[i] = new probe_point::component(TOK_KERNEL); |
63b4fd14 | 5296 | } |
edce5b67 | 5297 | |
c72aa911 | 5298 | return base_probe->create_alias(derived_loc, specific_loc); |
edce5b67 JS |
5299 | } |
5300 | ||
5301 | ||
20c6c071 | 5302 | void |
5227f1ea | 5303 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 5304 | probe * base, |
20c6c071 | 5305 | probe_point * location, |
86bf665e | 5306 | literal_map_t const & parameters, |
20c6c071 GH |
5307 | vector<derived_probe *> & finished_results) |
5308 | { | |
b20febf3 FCE |
5309 | // NB: the kernel/user dwlfpp objects are long-lived. |
5310 | // XXX: but they should be per-session, as this builder object | |
5311 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 5312 | |
7a24d422 FCE |
5313 | dwflpp* dw = 0; |
5314 | ||
7a24d422 | 5315 | string module_name; |
ae2552da FCE |
5316 | if (has_null_param (parameters, TOK_KERNEL)) |
5317 | { | |
5318 | dw = get_kern_dw(sess, "kernel"); | |
5319 | } | |
5320 | else if (get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 5321 | { |
ae2552da | 5322 | dw = get_kern_dw(sess, module_name); |
b8da0ad1 | 5323 | } |
7a24d422 | 5324 | else if (get_param (parameters, TOK_PROCESS, module_name)) |
b8da0ad1 | 5325 | { |
63b4fd14 | 5326 | string library_name; |
b642c901 | 5327 | user_path = find_executable (module_name); // canonicalize it |
63b4fd14 | 5328 | if (get_param (parameters, TOK_LIBRARY, library_name)) |
b642c901 SC |
5329 | { |
5330 | module_name = find_executable (library_name, "LD_LIBRARY_PATH"); | |
5331 | user_lib = module_name; | |
5332 | } | |
63b4fd14 | 5333 | else |
b642c901 | 5334 | module_name = user_path; // canonicalize it |
d0a7f5a9 | 5335 | |
e34d5d13 FCE |
5336 | if (sess.kernel_config["CONFIG_UTRACE"] != string("y")) |
5337 | throw semantic_error ("process probes not available without kernel CONFIG_UTRACE"); | |
5338 | ||
7a24d422 FCE |
5339 | // user-space target; we use one dwflpp instance per module name |
5340 | // (= program or shared library) | |
707bf35e | 5341 | dw = get_user_dw(sess, module_name); |
c8959a29 | 5342 | } |
20c6c071 | 5343 | |
5896cd05 MW |
5344 | if (sess.verbose > 3) |
5345 | clog << "dwarf_builder::build for " << module_name << endl; | |
5346 | ||
a794dbeb FCE |
5347 | string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __ |
5348 | if (get_param(parameters, TOK_MARK, dummy_mark_name)) | |
f28a8c28 | 5349 | { |
edce5b67 JS |
5350 | sdt_query sdtq(base, location, *dw, parameters, finished_results); |
5351 | dw->iterate_over_modules(&query_module, &sdtq); | |
5352 | return; | |
7a05f484 | 5353 | } |
20c6c071 | 5354 | |
8f14e444 | 5355 | unsigned results_pre = finished_results.size(); |
b642c901 | 5356 | dwarf_query q(base, location, *dw, parameters, finished_results, user_path, user_lib); |
7a24d422 FCE |
5357 | |
5358 | // XXX: kernel.statement.absolute is a special case that requires no | |
5359 | // dwfl processing. This code should be in a separate builder. | |
7a24d422 | 5360 | if (q.has_kernel && q.has_absolute) |
37ebca01 | 5361 | { |
4baf0e53 | 5362 | // assert guru mode for absolute probes |
37ebca01 FCE |
5363 | if (! q.base_probe->privileged) |
5364 | { | |
edce5b67 JS |
5365 | throw semantic_error ("absolute statement probe in unprivileged script", |
5366 | q.base_probe->tok); | |
37ebca01 FCE |
5367 | } |
5368 | ||
5369 | // For kernel.statement(NUM).absolute probe points, we bypass | |
5370 | // all the debuginfo stuff: We just wire up a | |
5371 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 5372 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
5373 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
5374 | q.statement_num_val, q.statement_num_val, | |
5375 | q, 0); | |
37ebca01 | 5376 | finished_results.push_back (p); |
1a0dbc5a | 5377 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
5378 | return; |
5379 | } | |
5380 | ||
51178501 | 5381 | dw->iterate_over_modules(&query_module, &q); |
8f14e444 FCE |
5382 | |
5383 | ||
5384 | // PR11553 special processing: .return probes requested, but | |
5385 | // some inlined function instances matched. | |
5386 | unsigned i_n_r = q.inlined_non_returnable.size(); | |
5387 | unsigned results_post = finished_results.size(); | |
5388 | if (i_n_r > 0) | |
5389 | { | |
5390 | if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning | |
5391 | { | |
5392 | string quicklist; | |
5393 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
5394 | it != q.inlined_non_returnable.end(); | |
5395 | it++) | |
5396 | { | |
5397 | quicklist += " " + (*it); | |
5398 | if (quicklist.size() > 80) // heuristic, don't make an overlong report line | |
5399 | { | |
5400 | quicklist += " ..."; | |
5401 | break; | |
5402 | } | |
5403 | } | |
5404 | ||
5405 | sess.print_warning ("cannot probe .return of " + lex_cast(i_n_r) + " inlined function(s):" + quicklist); | |
5406 | // There will be also a "no matches" semantic error generated. | |
5407 | } | |
5408 | if (sess.verbose > 1) | |
5409 | clog << "skipped .return probe of " + lex_cast(i_n_r) + " inlined function(s)" << endl; | |
5410 | if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity | |
5411 | { | |
5412 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
5413 | it != q.inlined_non_returnable.end(); | |
5414 | it++) | |
5415 | clog << (*it) << " "; | |
5416 | clog << endl; | |
5417 | } | |
5418 | } // i_n_r > 0 | |
5f0a03a6 JK |
5419 | } |
5420 | ||
5421 | symbol_table::~symbol_table() | |
5422 | { | |
c9efa5c9 | 5423 | delete_map(map_by_addr); |
5f0a03a6 JK |
5424 | } |
5425 | ||
5426 | void | |
2867a2a1 JS |
5427 | symbol_table::add_symbol(const char *name, bool weak, bool descriptor, |
5428 | Dwarf_Addr addr, Dwarf_Addr *high_addr) | |
5f0a03a6 | 5429 | { |
ab91b232 JK |
5430 | #ifdef __powerpc__ |
5431 | // Map ".sys_foo" to "sys_foo". | |
5432 | if (name[0] == '.') | |
5433 | name++; | |
5434 | #endif | |
5f0a03a6 JK |
5435 | func_info *fi = new func_info(); |
5436 | fi->addr = addr; | |
5437 | fi->name = name; | |
ab91b232 | 5438 | fi->weak = weak; |
2867a2a1 | 5439 | fi->descriptor = descriptor; |
5f0a03a6 JK |
5440 | map_by_name[fi->name] = fi; |
5441 | // TODO: Use a multimap in case there are multiple static | |
5442 | // functions with the same name? | |
1c6b77e5 | 5443 | map_by_addr.insert(make_pair(addr, fi)); |
5f0a03a6 JK |
5444 | } |
5445 | ||
5446 | enum info_status | |
5447 | symbol_table::read_symbols(FILE *f, const string& path) | |
5448 | { | |
5449 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
5450 | int ret; | |
2e67a43b TM |
5451 | char *name = 0; |
5452 | char *mod = 0; | |
5f0a03a6 JK |
5453 | char type; |
5454 | unsigned long long addr; | |
5455 | Dwarf_Addr high_addr = 0; | |
5456 | int line = 0; | |
5457 | ||
5458 | // %as (non-POSIX) mallocs space for the string and stores its address. | |
5459 | while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0) | |
5460 | { | |
2e67a43b TM |
5461 | auto_free free_name(name); |
5462 | auto_free free_mod(mod); | |
5f0a03a6 JK |
5463 | line++; |
5464 | if (ret < 3) | |
5465 | { | |
41c262f3 | 5466 | cerr << "Symbol table error: Line " |
5f0a03a6 JK |
5467 | << line |
5468 | << " of symbol list from " | |
5469 | << path | |
5470 | << " is not in correct format: address type name [module]"; | |
5471 | // Caller should delete symbol_table object. | |
5472 | return info_absent; | |
5473 | } | |
2e67a43b | 5474 | else if (ret > 3) |
5f0a03a6 JK |
5475 | { |
5476 | // Modules are loaded above the kernel, so if we're getting | |
5477 | // modules, we're done. | |
2e67a43b | 5478 | break; |
5f0a03a6 | 5479 | } |
ab91b232 | 5480 | if (type == 'T' || type == 't' || type == 'W') |
2867a2a1 | 5481 | add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr); |
5f0a03a6 JK |
5482 | } |
5483 | ||
1c6b77e5 | 5484 | if (map_by_addr.size() < 1) |
5f0a03a6 JK |
5485 | { |
5486 | cerr << "Symbol table error: " | |
5487 | << path << " contains no function symbols." << endl; | |
5488 | return info_absent; | |
5489 | } | |
5490 | return info_present; | |
5491 | } | |
5492 | ||
5493 | // NB: This currently unused. We use get_from_elf() instead because | |
5494 | // that gives us raw addresses -- which we need for modules -- whereas | |
5495 | // nm provides the address relative to the beginning of the section. | |
5496 | enum info_status | |
83ca3872 MW |
5497 | symbol_table::read_from_elf_file(const string &path, |
5498 | const systemtap_session &sess) | |
5f0a03a6 JK |
5499 | { |
5500 | FILE *f; | |
5501 | string cmd = string("/usr/bin/nm -n --defined-only ") + path; | |
5502 | f = popen(cmd.c_str(), "r"); | |
5503 | if (!f) | |
5504 | { | |
5505 | // nm failures are detected by pclose, not popen. | |
5506 | cerr << "Internal error reading symbol table from " | |
5507 | << path << " -- " << strerror (errno); | |
5508 | return info_absent; | |
5509 | } | |
5510 | enum info_status status = read_symbols(f, path); | |
5511 | if (pclose(f) != 0) | |
5512 | { | |
83ca3872 | 5513 | if (status == info_present && ! sess.suppress_warnings) |
5f0a03a6 JK |
5514 | cerr << "Warning: nm cannot read symbol table from " << path; |
5515 | return info_absent; | |
5516 | } | |
5517 | return status; | |
5518 | } | |
5519 | ||
5520 | enum info_status | |
83ca3872 MW |
5521 | symbol_table::read_from_text_file(const string& path, |
5522 | const systemtap_session &sess) | |
5f0a03a6 JK |
5523 | { |
5524 | FILE *f = fopen(path.c_str(), "r"); | |
5525 | if (!f) | |
5526 | { | |
83ca3872 MW |
5527 | if (! sess.suppress_warnings) |
5528 | cerr << "Warning: cannot read symbol table from " | |
5529 | << path << " -- " << strerror (errno); | |
5f0a03a6 JK |
5530 | return info_absent; |
5531 | } | |
5532 | enum info_status status = read_symbols(f, path); | |
5533 | (void) fclose(f); | |
5534 | return status; | |
5535 | } | |
5536 | ||
46f7b6be JK |
5537 | void |
5538 | symbol_table::prepare_section_rejection(Dwfl_Module *mod) | |
5539 | { | |
5540 | #ifdef __powerpc__ | |
5541 | /* | |
5542 | * The .opd section contains function descriptors that can look | |
5543 | * just like function entry points. For example, there's a function | |
5544 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
5545 | * Reject all symbols in .opd. | |
5546 | */ | |
5547 | opd_section = SHN_UNDEF; | |
5548 | Dwarf_Addr bias; | |
5549 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
5550 | ?: dwfl_module_getelf (mod, &bias)); | |
5551 | Elf_Scn* scn = 0; | |
5552 | size_t shstrndx; | |
5553 | ||
5554 | if (!elf) | |
5555 | return; | |
fcc30d6d | 5556 | if (elf_getshdrstrndx (elf, &shstrndx) != 0) |
46f7b6be JK |
5557 | return; |
5558 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
5559 | { | |
5560 | GElf_Shdr shdr_mem; | |
5561 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
5562 | if (!shdr) | |
5563 | continue; | |
5564 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
5565 | if (!strcmp(name, ".opd")) | |
5566 | { | |
5567 | opd_section = elf_ndxscn(scn); | |
5568 | return; | |
5569 | } | |
5570 | } | |
5571 | #endif | |
5572 | } | |
5573 | ||
5574 | bool | |
5575 | symbol_table::reject_section(GElf_Word section) | |
5576 | { | |
5577 | if (section == SHN_UNDEF) | |
5578 | return true; | |
5579 | #ifdef __powerpc__ | |
5580 | if (section == opd_section) | |
5581 | return true; | |
5582 | #endif | |
5583 | return false; | |
5584 | } | |
5585 | ||
5f0a03a6 JK |
5586 | enum info_status |
5587 | symbol_table::get_from_elf() | |
5588 | { | |
5589 | Dwarf_Addr high_addr = 0; | |
5590 | Dwfl_Module *mod = mod_info->mod; | |
5591 | int syments = dwfl_module_getsymtab(mod); | |
5592 | assert(syments); | |
46f7b6be | 5593 | prepare_section_rejection(mod); |
5f0a03a6 JK |
5594 | for (int i = 1; i < syments; ++i) |
5595 | { | |
5596 | GElf_Sym sym; | |
ab91b232 JK |
5597 | GElf_Word section; |
5598 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
2867a2a1 | 5599 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC) |
ab91b232 | 5600 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
2867a2a1 | 5601 | reject_section(section), sym.st_value, &high_addr); |
5f0a03a6 JK |
5602 | } |
5603 | return info_present; | |
5604 | } | |
5605 | ||
5f0a03a6 JK |
5606 | func_info * |
5607 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
5608 | { | |
1c6b77e5 JS |
5609 | iterator_t iter = map_by_addr.upper_bound(addr); |
5610 | if (iter == map_by_addr.begin()) | |
5f0a03a6 | 5611 | return NULL; |
2e67a43b | 5612 | else |
1c6b77e5 | 5613 | return (--iter)->second; |
5f0a03a6 JK |
5614 | } |
5615 | ||
5616 | func_info * | |
5617 | symbol_table::lookup_symbol(const string& name) | |
5618 | { | |
5619 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
5620 | if (i == map_by_name.end()) | |
5621 | return NULL; | |
5622 | return i->second; | |
5623 | } | |
5624 | ||
5625 | Dwarf_Addr | |
5626 | symbol_table::lookup_symbol_address(const string& name) | |
5627 | { | |
5628 | func_info *fi = lookup_symbol(name); | |
5629 | if (fi) | |
5630 | return fi->addr; | |
5631 | return 0; | |
5632 | } | |
5633 | ||
ab91b232 JK |
5634 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
5635 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
5636 | // For system calls not implemented elsewhere, this weak symbol shows up | |
5637 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
5638 | // we refuse to consider such symbols. Here we delete them from our | |
5639 | // symbol table. | |
5640 | // TODO: Consider generalizing this and/or making it part of blacklist | |
5641 | // processing. | |
5642 | void | |
5643 | symbol_table::purge_syscall_stubs() | |
5644 | { | |
5645 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
5646 | if (stub_addr == 0) | |
5647 | return; | |
1c6b77e5 | 5648 | range_t purge_range = map_by_addr.equal_range(stub_addr); |
2e67a43b TM |
5649 | for (iterator_t iter = purge_range.first; |
5650 | iter != purge_range.second; | |
1c6b77e5 | 5651 | ) |
ab91b232 | 5652 | { |
1c6b77e5 | 5653 | func_info *fi = iter->second; |
2e67a43b | 5654 | if (fi->weak && fi->name != "sys_ni_syscall") |
ab91b232 | 5655 | { |
2e67a43b | 5656 | map_by_name.erase(fi->name); |
1c6b77e5 | 5657 | map_by_addr.erase(iter++); |
2e67a43b | 5658 | delete fi; |
2e67a43b | 5659 | } |
1c6b77e5 JS |
5660 | else |
5661 | iter++; | |
ab91b232 JK |
5662 | } |
5663 | } | |
5664 | ||
5f0a03a6 JK |
5665 | void |
5666 | module_info::get_symtab(dwarf_query *q) | |
5667 | { | |
5668 | systemtap_session &sess = q->sess; | |
5669 | ||
1c6b77e5 JS |
5670 | if (symtab_status != info_unknown) |
5671 | return; | |
5672 | ||
5f0a03a6 JK |
5673 | sym_table = new symbol_table(this); |
5674 | if (!elf_path.empty()) | |
5675 | { | |
83ca3872 MW |
5676 | if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty() |
5677 | && ! sess.suppress_warnings) | |
5f0a03a6 JK |
5678 | cerr << "Warning: reading symbol table from " |
5679 | << elf_path | |
5680 | << " -- ignoring " | |
5681 | << sess.kernel_symtab_path | |
83ca3872 | 5682 | << endl; |
5f0a03a6 JK |
5683 | symtab_status = sym_table->get_from_elf(); |
5684 | } | |
5685 | else | |
5686 | { | |
5687 | assert(name == TOK_KERNEL); | |
5688 | if (sess.kernel_symtab_path.empty()) | |
5689 | { | |
5690 | symtab_status = info_absent; | |
5691 | cerr << "Error: Cannot find vmlinux." | |
5692 | << " Consider using --kmap instead of --kelf." | |
5693 | << endl;; | |
5694 | } | |
5695 | else | |
5696 | { | |
5697 | symtab_status = | |
83ca3872 | 5698 | sym_table->read_from_text_file(sess.kernel_symtab_path, sess); |
5f0a03a6 JK |
5699 | if (symtab_status == info_present) |
5700 | { | |
5701 | sess.sym_kprobes_text_start = | |
5702 | sym_table->lookup_symbol_address("__kprobes_text_start"); | |
5703 | sess.sym_kprobes_text_end = | |
5704 | sym_table->lookup_symbol_address("__kprobes_text_end"); | |
5705 | sess.sym_stext = sym_table->lookup_symbol_address("_stext"); | |
5f0a03a6 JK |
5706 | } |
5707 | } | |
5708 | } | |
5709 | if (symtab_status == info_absent) | |
5710 | { | |
5711 | delete sym_table; | |
5712 | sym_table = NULL; | |
5713 | return; | |
5714 | } | |
5715 | ||
ab91b232 JK |
5716 | if (name == TOK_KERNEL) |
5717 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
5718 | } |
5719 | ||
1c6b77e5 JS |
5720 | // update_symtab reconciles data between the elf symbol table and the dwarf |
5721 | // function enumeration. It updates the symbol table entries with the dwarf | |
5722 | // die that describes the function, which also signals to query_module_symtab | |
5723 | // that a statement probe isn't needed. In return, it also adds aliases to the | |
5724 | // function table for names that share the same addr/die. | |
5725 | void | |
5726 | module_info::update_symtab(cu_function_cache_t *funcs) | |
5727 | { | |
5728 | if (!sym_table) | |
5729 | return; | |
5730 | ||
5731 | cu_function_cache_t new_funcs; | |
5732 | ||
5733 | for (cu_function_cache_t::iterator func = funcs->begin(); | |
5734 | func != funcs->end(); func++) | |
5735 | { | |
5736 | // optimization: inlines will never be in the symbol table | |
5737 | if (dwarf_func_inline(&func->second) != 0) | |
5738 | continue; | |
5739 | ||
5740 | func_info *fi = sym_table->lookup_symbol(func->first); | |
5741 | if (!fi) | |
5742 | continue; | |
5743 | ||
5744 | // iterate over all functions at the same address | |
5745 | symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr); | |
5746 | for (symbol_table::iterator_t it = er.first; it != er.second; ++it) | |
5747 | { | |
5748 | // update this function with the dwarf die | |
5749 | it->second->die = func->second; | |
5750 | ||
5751 | // if this function is a new alias, then | |
5752 | // save it to merge into the function cache | |
5753 | if (it->second != fi) | |
b7478964 | 5754 | new_funcs.insert(make_pair(it->second->name, it->second->die)); |
1c6b77e5 JS |
5755 | } |
5756 | } | |
5757 | ||
5758 | // add all discovered aliases back into the function cache | |
5759 | // NB: this won't replace any names that dwarf may have already found | |
5760 | funcs->insert(new_funcs.begin(), new_funcs.end()); | |
5761 | } | |
5762 | ||
5f0a03a6 JK |
5763 | module_info::~module_info() |
5764 | { | |
5765 | if (sym_table) | |
5766 | delete sym_table; | |
b55bc428 FCE |
5767 | } |
5768 | ||
935447c8 | 5769 | // ------------------------------------------------------------------------ |
888af770 | 5770 | // user-space probes |
935447c8 DS |
5771 | // ------------------------------------------------------------------------ |
5772 | ||
935447c8 | 5773 | |
888af770 | 5774 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> |
935447c8 | 5775 | { |
89ba3085 FCE |
5776 | private: |
5777 | string make_pbm_key (uprobe_derived_probe* p) { | |
5778 | return p->module + "|" + p->section + "|" + lex_cast(p->pid); | |
5779 | } | |
5780 | ||
935447c8 | 5781 | public: |
888af770 | 5782 | void emit_module_decls (systemtap_session& s); |
935447c8 DS |
5783 | void emit_module_init (systemtap_session& s); |
5784 | void emit_module_exit (systemtap_session& s); | |
5785 | }; | |
5786 | ||
5787 | ||
888af770 FCE |
5788 | void |
5789 | uprobe_derived_probe::join_group (systemtap_session& s) | |
5790 | { | |
5791 | if (! s.uprobe_derived_probes) | |
5792 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
5793 | s.uprobe_derived_probes->enroll (this); | |
93646f4d | 5794 | enable_task_finder(s); |
a96d1db0 | 5795 | |
8a03658e JS |
5796 | // Ask buildrun.cxx to build extra module if needed, and |
5797 | // signal staprun to load that module | |
5798 | s.need_uprobes = true; | |
a96d1db0 DN |
5799 | } |
5800 | ||
888af770 | 5801 | |
2865d17a DB |
5802 | void |
5803 | uprobe_derived_probe::emit_unprivileged_assertion (translator_output* o) | |
5804 | { | |
5805 | // These probes are allowed for unprivileged users, but only in the | |
5806 | // context of processes which they own. | |
5807 | emit_process_owner_assertion (o); | |
5808 | } | |
5809 | ||
5810 | ||
888af770 | 5811 | struct uprobe_builder: public derived_probe_builder |
a96d1db0 | 5812 | { |
888af770 | 5813 | uprobe_builder() {} |
a96d1db0 DN |
5814 | virtual void build(systemtap_session & sess, |
5815 | probe * base, | |
5816 | probe_point * location, | |
86bf665e | 5817 | literal_map_t const & parameters, |
a96d1db0 DN |
5818 | vector<derived_probe *> & finished_results) |
5819 | { | |
888af770 | 5820 | int64_t process, address; |
a96d1db0 | 5821 | |
888af770 | 5822 | bool b1 = get_param (parameters, TOK_PROCESS, process); |
ced347a9 | 5823 | (void) b1; |
888af770 | 5824 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 5825 | (void) b2; |
888af770 FCE |
5826 | bool rr = has_null_param (parameters, TOK_RETURN); |
5827 | assert (b1 && b2); // by pattern_root construction | |
a96d1db0 | 5828 | |
0973d815 | 5829 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
a96d1db0 DN |
5830 | } |
5831 | }; | |
5832 | ||
5833 | ||
5834 | void | |
775d51e5 | 5835 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) |
a96d1db0 | 5836 | { |
888af770 | 5837 | if (probes.empty()) return; |
775d51e5 | 5838 | s.op->newline() << "/* ---- user probes ---- */"; |
471fca5e TM |
5839 | // If uprobes isn't in the kernel, pull it in from the runtime. |
5840 | ||
5841 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; | |
5842 | s.op->newline() << "#include <linux/uprobes.h>"; | |
5843 | s.op->newline() << "#else"; | |
5844 | s.op->newline() << "#include \"uprobes/uprobes.h\""; | |
5845 | s.op->newline() << "#endif"; | |
5846 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; | |
5847 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
5848 | s.op->newline() << "#endif"; | |
a96d1db0 | 5849 | |
43241c44 FCE |
5850 | // We'll probably need at least this many: |
5851 | unsigned minuprobes = probes.size(); | |
5852 | // .. but we don't want so many that .bss is inflated (PR10507): | |
5853 | unsigned uprobesize = 64; | |
5854 | unsigned maxuprobesmem = 10*1024*1024; // 10 MB | |
5855 | unsigned maxuprobes = maxuprobesmem / uprobesize; | |
5856 | ||
aaf7ffe8 FCE |
5857 | // Let's choose a value on the geometric middle. This should end up |
5858 | // between minuprobes and maxuprobes. It's OK if this number turns | |
5859 | // out to be < minuprobes or > maxuprobes. At worst, we get a | |
5860 | // run-time error of one kind (too few: missed uprobe registrations) | |
5861 | // or another (too many: vmalloc errors at module load time). | |
5862 | unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes); | |
43241c44 | 5863 | |
6d0f3f0c | 5864 | s.op->newline() << "#ifndef MAXUPROBES"; |
43241c44 | 5865 | s.op->newline() << "#define MAXUPROBES " << default_maxuprobes; |
6d0f3f0c | 5866 | s.op->newline() << "#endif"; |
a96d1db0 | 5867 | |
cc52276b WC |
5868 | // Forward decls |
5869 | s.op->newline() << "#include \"uprobes-common.h\""; | |
5870 | ||
5e112f92 FCE |
5871 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
5872 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
cc52276b WC |
5873 | // XXX: consider a slab cache or somesuch for stap_uprobes |
5874 | s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];"; | |
5e112f92 | 5875 | s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
a96d1db0 | 5876 | |
89ba3085 FCE |
5877 | s.op->assert_0_indent(); |
5878 | ||
89ba3085 FCE |
5879 | // Assign task-finder numbers as we build up the stap_uprobe_tf table. |
5880 | // This means we process probes[] in two passes. | |
5881 | map <string,unsigned> module_index; | |
5882 | unsigned module_index_ctr = 0; | |
5883 | ||
cc52276b WC |
5884 | // not const since embedded task_finder_target struct changes |
5885 | s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {"; | |
89ba3085 FCE |
5886 | s.op->indent(1); |
5887 | for (unsigned i=0; i<probes.size(); i++) | |
5888 | { | |
5889 | uprobe_derived_probe *p = probes[i]; | |
5890 | string pbmkey = make_pbm_key (p); | |
5891 | if (module_index.find (pbmkey) == module_index.end()) | |
5892 | { | |
5893 | module_index[pbmkey] = module_index_ctr++; | |
5894 | ||
5895 | s.op->newline() << "{"; | |
5896 | // NB: it's essential that make_pbm_key() use all of and | |
5897 | // only the same fields as we're about to emit. | |
5898 | s.op->line() << " .finder={"; | |
5899 | if (p->pid != 0) | |
5900 | s.op->line() << " .pid=" << p->pid; | |
5901 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program | |
5902 | { | |
5903 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
5904 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
5905 | } | |
aff5d390 | 5906 | if (p->section != ".absolute") // ET_DYN |
89ba3085 | 5907 | { |
63b4fd14 SC |
5908 | if (p->has_library && p->sdt_semaphore_addr != 0) |
5909 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
89ba3085 FCE |
5910 | s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, "; |
5911 | s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, "; | |
19d91f6c | 5912 | s.op->line() << " .callback=&stap_uprobe_process_munmap,"; |
89ba3085 FCE |
5913 | } |
5914 | ||
5915 | s.op->line() << " },"; | |
5916 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
5917 | s.op->line() << " },"; | |
5918 | } | |
5919 | else | |
5920 | ; // skip it in this pass, already have a suitable stap_uprobe_tf slot for it. | |
5921 | } | |
5922 | s.op->newline(-1) << "};"; | |
5923 | ||
5924 | s.op->assert_0_indent(); | |
5925 | ||
cc52276b WC |
5926 | // NB: read-only structure |
5927 | s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {"; | |
a96d1db0 | 5928 | s.op->indent(1); |
888af770 FCE |
5929 | for (unsigned i =0; i<probes.size(); i++) |
5930 | { | |
5931 | uprobe_derived_probe* p = probes[i]; | |
5932 | s.op->newline() << "{"; | |
89ba3085 FCE |
5933 | string key = make_pbm_key (p); |
5934 | unsigned value = module_index[key]; | |
759e1d76 FCE |
5935 | if (value != 0) |
5936 | s.op->line() << " .tfi=" << value << ","; | |
6b66b9f7 | 5937 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
faea5e16 | 5938 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
4ddb6dd0 | 5939 | |
038c38c6 | 5940 | if (p->sdt_semaphore_addr != 0) |
63b4fd14 | 5941 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" |
038c38c6 | 5942 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; |
4ddb6dd0 JS |
5943 | |
5944 | if (p->has_return) | |
5945 | s.op->line() << " .return_p=1,"; | |
888af770 FCE |
5946 | s.op->line() << " },"; |
5947 | } | |
5948 | s.op->newline(-1) << "};"; | |
a96d1db0 | 5949 | |
89ba3085 FCE |
5950 | s.op->assert_0_indent(); |
5951 | ||
48e685da | 5952 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 5953 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
89ba3085 | 5954 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
faea5e16 | 5955 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe"); |
6d0f3f0c FCE |
5956 | s.op->newline() << "if (sup->spec_index < 0 ||" |
5957 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
a96d1db0 | 5958 | s.op->newline() << "c->regs = regs;"; |
fd9187da | 5959 | s.op->newline() << "c->ri = GET_PC_URETPROBE_NONE;"; |
6415ddde MW |
5960 | |
5961 | // Make it look like the IP is set as it would in the actual user | |
5962 | // task when calling real probe handler. Reset IP regs on return, so | |
5963 | // we don't confuse uprobes. PR10458 | |
5964 | s.op->newline() << "{"; | |
5965 | s.op->indent(1); | |
5966 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);"; | |
259d54c0 | 5967 | s.op->newline() << "SET_REG_IP(regs, inst->vaddr);"; |
faea5e16 | 5968 | s.op->newline() << "(*sups->probe.ph) (c);"; |
259d54c0 | 5969 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
5970 | s.op->newline(-1) << "}"; |
5971 | ||
a96d1db0 | 5972 | common_probe_entryfn_epilogue (s.op); |
888af770 | 5973 | s.op->newline(-1) << "}"; |
a96d1db0 | 5974 | |
48e685da | 5975 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 5976 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
89ba3085 | 5977 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
faea5e16 | 5978 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe"); |
5e562a69 | 5979 | s.op->newline() << "c->ri = inst;"; |
6d0f3f0c FCE |
5980 | s.op->newline() << "if (sup->spec_index < 0 ||" |
5981 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 FCE |
5982 | // XXX: kretprobes saves "c->pi = inst;" too |
5983 | s.op->newline() << "c->regs = regs;"; | |
6415ddde MW |
5984 | |
5985 | // Make it look like the IP is set as it would in the actual user | |
5986 | // task when calling real probe handler. Reset IP regs on return, so | |
5987 | // we don't confuse uprobes. PR10458 | |
5988 | s.op->newline() << "{"; | |
5989 | s.op->indent(1); | |
5990 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);"; | |
5e562a69 | 5991 | s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);"; |
faea5e16 | 5992 | s.op->newline() << "(*sups->probe.ph) (c);"; |
259d54c0 | 5993 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
5994 | s.op->newline(-1) << "}"; |
5995 | ||
888af770 | 5996 | common_probe_entryfn_epilogue (s.op); |
a96d1db0 DN |
5997 | s.op->newline(-1) << "}"; |
5998 | ||
89ba3085 | 5999 | s.op->newline(); |
cc52276b | 6000 | s.op->newline() << "#include \"uprobes-common.c\""; |
6d0f3f0c | 6001 | s.op->newline(); |
888af770 | 6002 | } |
935447c8 DS |
6003 | |
6004 | ||
888af770 FCE |
6005 | void |
6006 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
935447c8 | 6007 | { |
888af770 | 6008 | if (probes.empty()) return; |
935447c8 | 6009 | |
5e112f92 | 6010 | s.op->newline() << "/* ---- user probes ---- */"; |
935447c8 | 6011 | |
01b05e2e | 6012 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
6013 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
6014 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
6015 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
6016 | // initialized to zero. This is so that we can use | |
6017 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
6018 | s.op->newline(-1) << "}"; |
6019 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
935447c8 | 6020 | |
89ba3085 FCE |
6021 | // Set up the task_finders |
6022 | s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {"; | |
6023 | s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];"; | |
6024 | s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better | |
6025 | s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);"; | |
935447c8 | 6026 | |
5e112f92 FCE |
6027 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any |
6028 | // finders already registered, since mere registration does not | |
6029 | // cause any utrace or memory allocation actions. That happens only | |
6030 | // later, once the task finder engine starts running. So, for a | |
6031 | // partial initialization requiring unwind, we need do nothing. | |
6032 | s.op->newline() << "if (rc) break;"; | |
a7a68293 | 6033 | |
888af770 FCE |
6034 | s.op->newline(-1) << "}"; |
6035 | } | |
d0ea46ce | 6036 | |
d0a7f5a9 | 6037 | |
888af770 FCE |
6038 | void |
6039 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
6040 | { | |
6041 | if (probes.empty()) return; | |
6042 | s.op->newline() << "/* ---- user probes ---- */"; | |
e56e51c9 | 6043 | |
6d0f3f0c FCE |
6044 | // NB: there is no stap_unregister_task_finder_target call; |
6045 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
6046 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
6047 | // | |
6048 | // This function blocks until all callbacks are completed, so there | |
6049 | // is supposed to be no possibility of any registration-related code starting | |
6050 | // to run in parallel with our shutdown here. So we don't need to protect the | |
6051 | // stap_uprobes[] array with the mutex. | |
d0a7f5a9 | 6052 | |
01b05e2e | 6053 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 | 6054 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
89ba3085 | 6055 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6d0f3f0c | 6056 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd | 6057 | |
8faa1fc5 | 6058 | // PR10655: decrement that ENABLED semaphore |
c116c31b | 6059 | s.op->newline() << "if (sup->sdt_sem_address) {"; |
8faa1fc5 FCE |
6060 | s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size |
6061 | s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);"; | |
6062 | s.op->newline() << "struct task_struct *tsk;"; | |
6063 | s.op->newline() << "rcu_read_lock();"; | |
6846cfc8 | 6064 | |
8faa1fc5 FCE |
6065 | // XXX: what a gross cut & paste job from tapset/task.stp, just for a lousy pid->task_struct* lookup |
6066 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)"; | |
6067 | s.op->newline() << " { struct pid *p_pid = find_get_pid(pid);"; | |
6068 | s.op->newline() << " tsk = pid_task(p_pid, PIDTYPE_PID);"; | |
6069 | s.op->newline() << " put_pid(p_pid); }"; | |
6070 | s.op->newline() << "#else"; | |
6071 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)"; | |
6072 | s.op->newline() << " tsk = find_task_by_vpid (pid);"; | |
6073 | s.op->newline() << "#else"; | |
6074 | s.op->newline() << " tsk = find_task_by_pid (pid);"; | |
6075 | s.op->newline() << "#endif /* 2.6.24 */"; | |
6076 | s.op->newline() << "#endif /* 2.6.31 */"; | |
6077 | ||
6078 | s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching | |
3c5b8e2b | 6079 | s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {"; |
63b4fd14 | 6080 | s.op->newline(1) << "sdt_semaphore --;"; |
903b9fcd | 6081 | s.op->newline() << "#ifdef DEBUG_UPROBES"; |
c116c31b | 6082 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);"; |
903b9fcd | 6083 | s.op->newline() << "#endif"; |
3c5b8e2b | 6084 | s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);"; |
93c84191 | 6085 | s.op->newline(-1) << "}"; |
8faa1fc5 FCE |
6086 | // XXX: need to analyze possibility of race condition |
6087 | s.op->newline(-1) << "}"; | |
6088 | s.op->newline() << "rcu_read_unlock();"; | |
6089 | s.op->newline(-1) << "}"; | |
6846cfc8 | 6090 | |
3568f1dd FCE |
6091 | s.op->newline() << "if (sups->return_p) {"; |
6092 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
89ba3085 | 6093 | 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 | 6094 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
6095 | // NB: PR6829 does not change that we still need to unregister at |
6096 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
6097 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
6098 | s.op->newline(-1) << "} else {"; | |
6099 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
8faa1fc5 | 6100 | 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 |
6101 | s.op->newline() << "#endif"; |
6102 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
6103 | s.op->newline(-1) << "}"; | |
935447c8 | 6104 | |
6d0f3f0c | 6105 | s.op->newline() << "sup->spec_index = -1;"; |
935447c8 | 6106 | |
3568f1dd FCE |
6107 | // XXX: uprobe missed counts? |
6108 | ||
6d0f3f0c | 6109 | s.op->newline(-1) << "}"; |
935447c8 | 6110 | |
5e112f92 | 6111 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; |
935447c8 DS |
6112 | } |
6113 | ||
e6fe60e7 AM |
6114 | // ------------------------------------------------------------------------ |
6115 | // Kprobe derived probes | |
6116 | // ------------------------------------------------------------------------ | |
6117 | ||
4627ed58 | 6118 | static const string TOK_KPROBE("kprobe"); |
935447c8 | 6119 | |
bae55db9 | 6120 | struct kprobe_derived_probe: public derived_probe |
d0ea46ce | 6121 | { |
bae55db9 JS |
6122 | kprobe_derived_probe (probe *base, |
6123 | probe_point *location, | |
6124 | const string& name, | |
6125 | int64_t stmt_addr, | |
6126 | bool has_return, | |
6127 | bool has_statement, | |
6128 | bool has_maxactive, | |
b642c901 SC |
6129 | bool has_path, |
6130 | bool has_library, | |
6131 | long maxactive_val, | |
6132 | const string& path, | |
6133 | const string& library | |
bae55db9 JS |
6134 | ); |
6135 | string symbol_name; | |
6136 | Dwarf_Addr addr; | |
6137 | bool has_return; | |
6138 | bool has_statement; | |
6139 | bool has_maxactive; | |
b642c901 SC |
6140 | bool has_path; |
6141 | bool has_library; | |
bae55db9 | 6142 | long maxactive_val; |
b642c901 SC |
6143 | string path; |
6144 | string library; | |
bae55db9 JS |
6145 | bool access_var; |
6146 | void printsig (std::ostream &o) const; | |
6147 | void join_group (systemtap_session& s); | |
6148 | }; | |
d0ea46ce | 6149 | |
bae55db9 JS |
6150 | struct kprobe_derived_probe_group: public derived_probe_group |
6151 | { | |
6152 | private: | |
6153 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
6154 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
d0ea46ce | 6155 | |
bae55db9 JS |
6156 | public: |
6157 | void enroll (kprobe_derived_probe* probe); | |
6158 | void emit_module_decls (systemtap_session& s); | |
6159 | void emit_module_init (systemtap_session& s); | |
6160 | void emit_module_exit (systemtap_session& s); | |
6161 | }; | |
d0ea46ce | 6162 | |
e6fe60e7 AM |
6163 | kprobe_derived_probe::kprobe_derived_probe (probe *base, |
6164 | probe_point *location, | |
b6371390 | 6165 | const string& name, |
e6fe60e7 | 6166 | int64_t stmt_addr, |
b6371390 JS |
6167 | bool has_return, |
6168 | bool has_statement, | |
6169 | bool has_maxactive, | |
b642c901 SC |
6170 | bool has_path, |
6171 | bool has_library, | |
6172 | long maxactive_val, | |
6173 | const string& path, | |
6174 | const string& library | |
b6371390 | 6175 | ): |
e6fe60e7 AM |
6176 | derived_probe (base, location), |
6177 | symbol_name (name), addr (stmt_addr), | |
b6371390 | 6178 | has_return (has_return), has_statement (has_statement), |
b642c901 SC |
6179 | has_maxactive (has_maxactive), has_path (has_path), |
6180 | has_library (has_library), | |
6181 | maxactive_val (maxactive_val), | |
6182 | path (path), library (library) | |
e6fe60e7 AM |
6183 | { |
6184 | this->tok = base->tok; | |
6185 | this->access_var = false; | |
d0ea46ce | 6186 | |
e6fe60e7 AM |
6187 | #ifndef USHRT_MAX |
6188 | #define USHRT_MAX 32767 | |
6189 | #endif | |
d0ea46ce | 6190 | |
46856d8d JS |
6191 | // Expansion of $target variables in the probe body produces an error during |
6192 | // translate phase, since we're not using debuginfo | |
d0ea46ce | 6193 | |
e6fe60e7 | 6194 | vector<probe_point::component*> comps; |
46856d8d | 6195 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 6196 | |
46856d8d JS |
6197 | if (has_statement) |
6198 | { | |
9ea68eb9 JS |
6199 | comps.push_back (new probe_point::component(TOK_STATEMENT, |
6200 | new literal_number(addr, true))); | |
46856d8d JS |
6201 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); |
6202 | } | |
6203 | else | |
6204 | { | |
6205 | size_t pos = name.find(':'); | |
6206 | if (pos != string::npos) | |
d0ea46ce | 6207 | { |
46856d8d JS |
6208 | string module = name.substr(0, pos); |
6209 | string function = name.substr(pos + 1); | |
6210 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
6211 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
6212 | } | |
6213 | else | |
6214 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 6215 | } |
d0ea46ce | 6216 | |
b6371390 JS |
6217 | if (has_return) |
6218 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
6219 | if (has_maxactive) | |
6220 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d0ea46ce | 6221 | |
e6fe60e7 AM |
6222 | this->sole_location()->components = comps; |
6223 | } | |
d0ea46ce | 6224 | |
e6fe60e7 AM |
6225 | void kprobe_derived_probe::printsig (ostream& o) const |
6226 | { | |
6227 | sole_location()->print (o); | |
6228 | o << " /* " << " name = " << symbol_name << "*/"; | |
6229 | printsig_nested (o); | |
6230 | } | |
d0ea46ce | 6231 | |
e6fe60e7 AM |
6232 | void kprobe_derived_probe::join_group (systemtap_session& s) |
6233 | { | |
d0ea46ce | 6234 | |
e6fe60e7 AM |
6235 | if (! s.kprobe_derived_probes) |
6236 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
6237 | s.kprobe_derived_probes->enroll (this); | |
d0ea46ce | 6238 | |
e6fe60e7 | 6239 | } |
d0ea46ce | 6240 | |
e6fe60e7 AM |
6241 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) |
6242 | { | |
6243 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
6244 | // probes of same symbol should share single kprobe/kretprobe | |
6245 | } | |
d0ea46ce | 6246 | |
e6fe60e7 AM |
6247 | void |
6248 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6249 | { | |
6250 | if (probes_by_module.empty()) return; | |
d0ea46ce | 6251 | |
e6fe60e7 | 6252 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; |
d0ea46ce | 6253 | |
e6fe60e7 AM |
6254 | // Warn of misconfigured kernels |
6255 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
6256 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
6257 | s.op->newline() << "#endif"; | |
6258 | s.op->newline(); | |
d0ea46ce | 6259 | |
f07c3b68 | 6260 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 6261 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
6262 | s.op->newline() << "#endif"; |
6263 | ||
e6fe60e7 | 6264 | // Forward declare the master entry functions |
88747011 | 6265 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 6266 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 6267 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 | 6268 | s.op->line() << " struct pt_regs *regs);"; |
d0ea46ce | 6269 | |
e6fe60e7 AM |
6270 | // Emit an array of kprobe/kretprobe pointers |
6271 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
c9116e99 | 6272 | s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];"; |
e6fe60e7 | 6273 | s.op->newline() << "#endif"; |
d0ea46ce | 6274 | |
e6fe60e7 | 6275 | // Emit the actual probe list. |
d0ea46ce | 6276 | |
e6fe60e7 AM |
6277 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; |
6278 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
6279 | s.op->newline() << "#ifdef __ia64__"; | |
6280 | s.op->newline() << "struct kprobe dummy;"; | |
6281 | s.op->newline() << "#endif"; | |
6282 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
6283 | // NB: bss! | |
d0ea46ce | 6284 | |
e6fe60e7 AM |
6285 | s.op->newline() << "static struct stap_dwarfless_probe {"; |
6286 | s.op->newline(1) << "const unsigned return_p:1;"; | |
6287 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 6288 | s.op->newline() << "const unsigned optional_p:1;"; |
e6fe60e7 AM |
6289 | s.op->newline() << "unsigned registered_p:1;"; |
6290 | s.op->newline() << "const unsigned short maxactive_val;"; | |
935447c8 | 6291 | |
e6fe60e7 AM |
6292 | // Function Names are mostly small and uniform enough to justify putting |
6293 | // char[MAX]'s into the array instead of relocated char*'s. | |
935447c8 | 6294 | |
faea5e16 JS |
6295 | size_t symbol_string_name_max = 0; |
6296 | size_t symbol_string_name_tot = 0; | |
e6fe60e7 | 6297 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
6270adc1 | 6298 | { |
e6fe60e7 AM |
6299 | kprobe_derived_probe* p = it->second; |
6300 | #define DOIT(var,expr) do { \ | |
6301 | size_t var##_size = (expr) + 1; \ | |
6302 | var##_max = max (var##_max, var##_size); \ | |
6303 | var##_tot += var##_size; } while (0) | |
e6fe60e7 AM |
6304 | DOIT(symbol_string_name, p->symbol_name.size()); |
6305 | #undef DOIT | |
6270adc1 MH |
6306 | } |
6307 | ||
e6fe60e7 AM |
6308 | #define CALCIT(var) \ |
6309 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
935447c8 | 6310 | |
e6fe60e7 AM |
6311 | CALCIT(symbol_string); |
6312 | #undef CALCIT | |
6270adc1 | 6313 | |
bd659351 | 6314 | s.op->newline() << "unsigned long address;"; |
faea5e16 | 6315 | s.op->newline() << "struct stap_probe probe;"; |
e6fe60e7 AM |
6316 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; |
6317 | s.op->indent(1); | |
6270adc1 | 6318 | |
e6fe60e7 AM |
6319 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
6320 | { | |
6321 | kprobe_derived_probe* p = it->second; | |
6322 | s.op->newline() << "{"; | |
6323 | if (p->has_return) | |
6324 | s.op->line() << " .return_p=1,"; | |
6270adc1 | 6325 | |
e6fe60e7 AM |
6326 | if (p->has_maxactive) |
6327 | { | |
6328 | s.op->line() << " .maxactive_p=1,"; | |
6329 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
6330 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
6331 | } | |
6270adc1 | 6332 | |
b350f56b JS |
6333 | if (p->locations[0]->optional) |
6334 | s.op->line() << " .optional_p=1,"; | |
6335 | ||
e6fe60e7 | 6336 | if (p->has_statement) |
c8d9d15e | 6337 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 6338 | else |
c8d9d15e | 6339 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
5d67b47c | 6340 | |
faea5e16 | 6341 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
e6fe60e7 | 6342 | s.op->line() << " },"; |
935447c8 DS |
6343 | } |
6344 | ||
e6fe60e7 | 6345 | s.op->newline(-1) << "};"; |
5d67b47c | 6346 | |
e6fe60e7 AM |
6347 | // Emit the kprobes callback function |
6348 | s.op->newline(); | |
88747011 | 6349 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
6350 | s.op->line() << " struct pt_regs *regs) {"; |
6351 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
6352 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
6353 | // Check that the index is plausible | |
6354 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
6355 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
6356 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
6357 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
6358 | s.op->line() << "];"; | |
faea5e16 | 6359 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe"); |
e6fe60e7 | 6360 | s.op->newline() << "c->regs = regs;"; |
6415ddde MW |
6361 | |
6362 | // Make it look like the IP is set as it wouldn't have been replaced | |
6363 | // by a breakpoint instruction when calling real probe handler. Reset | |
6364 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
6365 | s.op->newline() << "{"; | |
6366 | s.op->indent(1); | |
6367 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);"; | |
259d54c0 | 6368 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
faea5e16 | 6369 | s.op->newline() << "(*sdp->probe.ph) (c);"; |
259d54c0 | 6370 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
6371 | s.op->newline(-1) << "}"; |
6372 | ||
e6fe60e7 AM |
6373 | common_probe_entryfn_epilogue (s.op); |
6374 | s.op->newline() << "return 0;"; | |
6375 | s.op->newline(-1) << "}"; | |
935447c8 | 6376 | |
e6fe60e7 AM |
6377 | // Same for kretprobes |
6378 | s.op->newline(); | |
88747011 | 6379 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
6380 | s.op->line() << " struct pt_regs *regs) {"; |
6381 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
935447c8 | 6382 | |
e6fe60e7 AM |
6383 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
6384 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
6385 | // Check that the index is plausible | |
6386 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
6387 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
6388 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
6389 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
6390 | s.op->line() << "];"; | |
935447c8 | 6391 | |
faea5e16 | 6392 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe"); |
e6fe60e7 AM |
6393 | s.op->newline() << "c->regs = regs;"; |
6394 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
6415ddde MW |
6395 | |
6396 | // Make it look like the IP is set as it wouldn't have been replaced | |
6397 | // by a breakpoint instruction when calling real probe handler. Reset | |
6398 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
6399 | s.op->newline() << "{"; | |
6400 | s.op->indent(1); | |
6401 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);"; | |
259d54c0 | 6402 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
faea5e16 | 6403 | s.op->newline() << "(*sdp->probe.ph) (c);"; |
259d54c0 | 6404 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
6405 | s.op->newline(-1) << "}"; |
6406 | ||
e6fe60e7 AM |
6407 | common_probe_entryfn_epilogue (s.op); |
6408 | s.op->newline() << "return 0;"; | |
6409 | s.op->newline(-1) << "}"; | |
bd659351 | 6410 | |
03a4ec63 | 6411 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
bd659351 MW |
6412 | s.op->newline() << "static int kprobe_resolve(void *data, const char *name,"; |
6413 | s.op->newline() << " struct module *owner,"; | |
6414 | s.op->newline() << " unsigned long val) {"; | |
6415 | s.op->newline(1) << "int i;"; | |
fc1d2aa2 MW |
6416 | s.op->newline() << "int *p = (int *) data;"; |
6417 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() | |
6418 | << " && *p > 0; i++) {"; | |
bd659351 MW |
6419 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
6420 | s.op->newline() << "if (! sdp->address)"; | |
fc1d2aa2 | 6421 | s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {"; |
bd659351 | 6422 | s.op->newline(1) << "sdp->address = val;"; |
fc1d2aa2 MW |
6423 | s.op->newline() << "(*p)--;"; |
6424 | s.op->newline(-1) << "}"; | |
6425 | s.op->newline(-2) << "}"; | |
6426 | s.op->newline() << "return (p > 0) ? 0 : -1;"; | |
bd659351 | 6427 | s.op->newline(-1) << "}"; |
03a4ec63 | 6428 | s.op->newline() << "#endif"; |
935447c8 DS |
6429 | } |
6430 | ||
e6fe60e7 | 6431 | |
6270adc1 | 6432 | void |
e6fe60e7 | 6433 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) |
6270adc1 | 6434 | { |
03a4ec63 | 6435 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
fc1d2aa2 MW |
6436 | s.op->newline() << "{"; |
6437 | s.op->newline(1) << "int p = 0;"; | |
6438 | s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {"; | |
6439 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6440 | s.op->newline() << "if (! sdp->address)"; | |
6441 | s.op->newline(1) << "p++;"; | |
6442 | s.op->newline(-2) << "}"; | |
6443 | s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);"; | |
6444 | s.op->newline(-1) << "}"; | |
03a4ec63 | 6445 | s.op->newline() << "#endif"; |
bd659351 | 6446 | |
e6fe60e7 | 6447 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 6448 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 6449 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e | 6450 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
03a4ec63 MW |
6451 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; |
6452 | ||
6453 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; | |
bd659351 MW |
6454 | s.op->newline() << "if (! addr) {"; |
6455 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
9319b767 MW |
6456 | s.op->newline() << "if (!sdp->optional_p)"; |
6457 | s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);"; | |
6458 | s.op->newline(-1) << "continue;"; | |
bd659351 | 6459 | s.op->newline(-1) << "}"; |
03a4ec63 MW |
6460 | s.op->newline() << "#endif"; |
6461 | ||
faea5e16 | 6462 | s.op->newline() << "probe_point = sdp->probe.pp;"; // for error messages |
e6fe60e7 | 6463 | s.op->newline() << "if (sdp->return_p) {"; |
c8d9d15e | 6464 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
03a4ec63 | 6465 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
e6fe60e7 AM |
6466 | s.op->newline() << "if (sdp->maxactive_p) {"; |
6467 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
6468 | s.op->newline(-1) << "} else {"; | |
f07c3b68 | 6469 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
e6fe60e7 | 6470 | s.op->newline(-1) << "}"; |
88747011 | 6471 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
6472 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
6473 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 6474 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
c8d9d15e JS |
6475 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
6476 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
e6fe60e7 AM |
6477 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
6478 | s.op->newline() << "if (rc == 0) {"; | |
6479 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
6480 | s.op->newline() << "if (rc != 0)"; | |
6481 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
6482 | s.op->newline(-2) << "}"; | |
6483 | s.op->newline() << "#else"; | |
6484 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
6485 | s.op->newline() << "#endif"; | |
6486 | s.op->newline(-1) << "} else {"; | |
6487 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 6488 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
03a4ec63 | 6489 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
88747011 | 6490 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 6491 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 6492 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e JS |
6493 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
6494 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; | |
e6fe60e7 AM |
6495 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
6496 | s.op->newline() << "if (rc == 0) {"; | |
6497 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
6498 | s.op->newline() << "if (rc != 0)"; | |
6499 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
6500 | s.op->newline(-2) << "}"; | |
6501 | s.op->newline() << "#else"; | |
6502 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
6503 | s.op->newline() << "#endif"; | |
6504 | s.op->newline(-1) << "}"; | |
6505 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
6506 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 6507 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 6508 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);"; |
b350f56b | 6509 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
e6fe60e7 AM |
6510 | // XXX: shall we increment numskipped? |
6511 | s.op->newline(-1) << "}"; | |
6270adc1 | 6512 | |
e6fe60e7 AM |
6513 | s.op->newline() << "else sdp->registered_p = 1;"; |
6514 | s.op->newline(-1) << "}"; // for loop | |
6270adc1 MH |
6515 | } |
6516 | ||
e6fe60e7 AM |
6517 | void |
6518 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
935447c8 | 6519 | { |
e6fe60e7 AM |
6520 | //Unregister kprobes by batch interfaces. |
6521 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
6522 | s.op->newline() << "j = 0;"; | |
6523 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
6524 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6525 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6526 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
6527 | s.op->newline() << "if (!sdp->return_p)"; | |
c9116e99 | 6528 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;"; |
e6fe60e7 | 6529 | s.op->newline(-2) << "}"; |
c9116e99 | 6530 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
6531 | s.op->newline() << "j = 0;"; |
6532 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
6533 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6534 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6535 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
6536 | s.op->newline() << "if (sdp->return_p)"; | |
c9116e99 | 6537 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;"; |
e6fe60e7 | 6538 | s.op->newline(-2) << "}"; |
c9116e99 | 6539 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
6540 | s.op->newline() << "#ifdef __ia64__"; |
6541 | s.op->newline() << "j = 0;"; | |
6542 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
6543 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6544 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6545 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
c9116e99 | 6546 | s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;"; |
e6fe60e7 | 6547 | s.op->newline(-1) << "}"; |
c9116e99 | 6548 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
6549 | s.op->newline() << "#endif"; |
6550 | s.op->newline() << "#endif"; | |
3e3bd7b6 | 6551 | |
e6fe60e7 AM |
6552 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
6553 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
6554 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
6555 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
6556 | s.op->newline() << "if (sdp->return_p) {"; | |
6557 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
6558 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
6559 | s.op->newline() << "#endif"; | |
6560 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; | |
6561 | s.op->newline() << "#ifdef STP_TIMING"; | |
6562 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
faea5e16 | 6563 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe.pp, kp->u.krp.nmissed);"; |
e6fe60e7 AM |
6564 | s.op->newline(-1) << "#endif"; |
6565 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; | |
6566 | s.op->newline() << "#ifdef STP_TIMING"; | |
6567 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
faea5e16 | 6568 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe.pp, kp->u.krp.kp.nmissed);"; |
e6fe60e7 AM |
6569 | s.op->newline(-1) << "#endif"; |
6570 | s.op->newline(-1) << "} else {"; | |
6571 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
6572 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
6573 | s.op->newline() << "#endif"; | |
6574 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; | |
6575 | s.op->newline() << "#ifdef STP_TIMING"; | |
6576 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
faea5e16 | 6577 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe.pp, kp->u.kp.nmissed);"; |
e6fe60e7 AM |
6578 | s.op->newline(-1) << "#endif"; |
6579 | s.op->newline(-1) << "}"; | |
6580 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
6581 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
6582 | s.op->newline() << "#endif"; | |
6583 | s.op->newline() << "sdp->registered_p = 0;"; | |
6584 | s.op->newline(-1) << "}"; | |
f8a968bc JS |
6585 | } |
6586 | ||
e6fe60e7 | 6587 | struct kprobe_builder: public derived_probe_builder |
3c1b3d06 | 6588 | { |
e6fe60e7 AM |
6589 | kprobe_builder() {} |
6590 | virtual void build(systemtap_session & sess, | |
6591 | probe * base, | |
6592 | probe_point * location, | |
6593 | literal_map_t const & parameters, | |
6594 | vector<derived_probe *> & finished_results); | |
6595 | }; | |
3c1b3d06 FCE |
6596 | |
6597 | ||
79189b84 | 6598 | void |
e6fe60e7 AM |
6599 | kprobe_builder::build(systemtap_session & sess, |
6600 | probe * base, | |
6601 | probe_point * location, | |
6602 | literal_map_t const & parameters, | |
6603 | vector<derived_probe *> & finished_results) | |
79189b84 | 6604 | { |
e6fe60e7 | 6605 | string function_string_val, module_string_val; |
b642c901 | 6606 | string path, library; |
b6371390 JS |
6607 | int64_t statement_num_val = 0, maxactive_val = 0; |
6608 | bool has_function_str, has_module_str, has_statement_num; | |
6609 | bool has_absolute, has_return, has_maxactive; | |
b642c901 | 6610 | bool has_path, has_library; |
79189b84 | 6611 | |
b6371390 JS |
6612 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
6613 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
6614 | has_return = has_null_param (parameters, TOK_RETURN); | |
6615 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
6616 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
6617 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
b642c901 SC |
6618 | has_path = get_param (parameters, TOK_PROCESS, path); |
6619 | has_library = get_param (parameters, TOK_LIBRARY, library); | |
6620 | ||
6621 | if (has_path) | |
6622 | path = find_executable (path); | |
6623 | if (has_library) | |
6624 | library = find_executable (library, "LD_LIBRARY_PATH"); | |
6625 | ||
b6371390 | 6626 | if (has_function_str) |
6fb70fb7 | 6627 | { |
b6371390 JS |
6628 | if (has_module_str) |
6629 | function_string_val = module_string_val + ":" + function_string_val; | |
86758d5f | 6630 | |
b6371390 JS |
6631 | finished_results.push_back (new kprobe_derived_probe (base, |
6632 | location, function_string_val, | |
6633 | 0, has_return, | |
6634 | has_statement_num, | |
6635 | has_maxactive, | |
b642c901 SC |
6636 | has_path, |
6637 | has_library, | |
6638 | maxactive_val, | |
6639 | path, | |
6640 | library)); | |
6fb70fb7 | 6641 | } |
e6fe60e7 | 6642 | else |
b6371390 JS |
6643 | { |
6644 | // assert guru mode for absolute probes | |
6645 | if ( has_statement_num && has_absolute && !base->privileged ) | |
6646 | throw semantic_error ("absolute statement probe in unprivileged script", base->tok); | |
6647 | ||
6648 | finished_results.push_back (new kprobe_derived_probe (base, | |
6649 | location, "", | |
6650 | statement_num_val, | |
6651 | has_return, | |
6652 | has_statement_num, | |
6653 | has_maxactive, | |
b642c901 SC |
6654 | has_path, |
6655 | has_library, | |
6656 | maxactive_val, | |
6657 | path, | |
6658 | library)); | |
96b030fe | 6659 | } |
79189b84 JS |
6660 | } |
6661 | ||
dd225250 PS |
6662 | // ------------------------------------------------------------------------ |
6663 | // Hardware breakpoint based probes. | |
6664 | // ------------------------------------------------------------------------ | |
6665 | ||
6666 | static const string TOK_HWBKPT("data"); | |
6667 | static const string TOK_HWBKPT_WRITE("write"); | |
6668 | static const string TOK_HWBKPT_RW("rw"); | |
6669 | static const string TOK_LENGTH("length"); | |
6670 | ||
6671 | #define HWBKPT_READ 0 | |
6672 | #define HWBKPT_WRITE 1 | |
6673 | #define HWBKPT_RW 2 | |
6674 | struct hwbkpt_derived_probe: public derived_probe | |
6675 | { | |
6676 | hwbkpt_derived_probe (probe *base, | |
6677 | probe_point *location, | |
6678 | uint64_t addr, | |
6679 | string symname, | |
6680 | unsigned int len, | |
6681 | bool has_only_read_access, | |
6682 | bool has_only_write_access, | |
6683 | bool has_rw_access | |
6684 | ); | |
6685 | Dwarf_Addr hwbkpt_addr; | |
6686 | string symbol_name; | |
6687 | unsigned int hwbkpt_access,hwbkpt_len; | |
6688 | ||
6689 | void printsig (std::ostream &o) const; | |
6690 | void join_group (systemtap_session& s); | |
6691 | }; | |
6692 | ||
6693 | struct hwbkpt_derived_probe_group: public derived_probe_group | |
6694 | { | |
dd225250 | 6695 | private: |
dac77b80 | 6696 | vector<hwbkpt_derived_probe*> hwbkpt_probes; |
dd225250 PS |
6697 | |
6698 | public: | |
6699 | void enroll (hwbkpt_derived_probe* probe, systemtap_session& s); | |
6700 | void emit_module_decls (systemtap_session& s); | |
6701 | void emit_module_init (systemtap_session& s); | |
6702 | void emit_module_exit (systemtap_session& s); | |
6703 | }; | |
6704 | ||
6705 | hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base, | |
9ea68eb9 JS |
6706 | probe_point *location, |
6707 | uint64_t addr, | |
6708 | string symname, | |
6709 | unsigned int len, | |
6710 | bool has_only_read_access, | |
6711 | bool has_only_write_access, | |
6712 | bool has_rw_access | |
6713 | ): | |
dd225250 PS |
6714 | derived_probe (base, location), |
6715 | hwbkpt_addr (addr), | |
6716 | symbol_name (symname), | |
6717 | hwbkpt_len (len) | |
6718 | { | |
6719 | this->tok = base->tok; | |
6720 | ||
6721 | vector<probe_point::component*> comps; | |
6722 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
6723 | ||
6724 | if (hwbkpt_addr) | |
9ea68eb9 JS |
6725 | comps.push_back (new probe_point::component (TOK_HWBKPT, |
6726 | new literal_number(hwbkpt_addr, true))); | |
6727 | else if (symbol_name.size()) | |
6728 | comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name))); | |
dd225250 PS |
6729 | |
6730 | comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len))); | |
6731 | ||
6732 | if (has_only_read_access) | |
9ea68eb9 | 6733 | this->hwbkpt_access = HWBKPT_READ ; |
dd225250 PS |
6734 | //TODO add code for comps.push_back for read, since this flag is not for x86 |
6735 | ||
6736 | else | |
9ea68eb9 JS |
6737 | { |
6738 | if (has_only_write_access) | |
6739 | { | |
6740 | this->hwbkpt_access = HWBKPT_WRITE ; | |
6741 | comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE)); | |
6742 | } | |
6743 | else | |
6744 | { | |
6745 | this->hwbkpt_access = HWBKPT_RW ; | |
6746 | comps.push_back (new probe_point::component(TOK_HWBKPT_RW)); | |
6747 | } | |
6748 | } | |
dd225250 PS |
6749 | |
6750 | this->sole_location()->components = comps; | |
6751 | } | |
6752 | ||
6753 | void hwbkpt_derived_probe::printsig (ostream& o) const | |
6754 | { | |
6755 | sole_location()->print (o); | |
6756 | printsig_nested (o); | |
6757 | } | |
6758 | ||
6759 | void hwbkpt_derived_probe::join_group (systemtap_session& s) | |
6760 | { | |
dac77b80 FCE |
6761 | if (! s.hwbkpt_derived_probes) |
6762 | s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group (); | |
dd225250 PS |
6763 | s.hwbkpt_derived_probes->enroll (this, s); |
6764 | } | |
6765 | ||
6766 | void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s) | |
6767 | { | |
dac77b80 FCE |
6768 | hwbkpt_probes.push_back (p); |
6769 | ||
6770 | unsigned max_hwbkpt_probes_by_arch = 0; | |
6771 | if (s.architecture == "i386" || s.architecture == "x86_64") | |
6772 | max_hwbkpt_probes_by_arch = 4; | |
6773 | else if (s.architecture == "s390") | |
6774 | max_hwbkpt_probes_by_arch = 1; | |
6775 | ||
6776 | if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch) | |
6777 | if (! s.suppress_warnings) | |
6778 | s.print_warning ("Too many hardware breakpoint probes requested for " + s.architecture | |
6779 | + "(" + lex_cast(hwbkpt_probes.size()) + | |
6780 | " vs. " + lex_cast(max_hwbkpt_probes_by_arch) + ")"); | |
dd225250 PS |
6781 | } |
6782 | ||
6783 | void | |
6784 | hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6785 | { | |
dac77b80 | 6786 | if (hwbkpt_probes.empty()) return; |
dd225250 PS |
6787 | |
6788 | s.op->newline() << "/* ---- hwbkpt-based probes ---- */"; | |
6789 | ||
6790 | s.op->newline() << "#include <linux/perf_event.h>"; | |
6791 | s.op->newline() << "#include <linux/hw_breakpoint.h>"; | |
6792 | s.op->newline(); | |
6793 | ||
6794 | // Forward declare the master entry functions | |
6795 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
6796 | s.op->line() << " int nmi,"; | |
6797 | s.op->line() << " struct perf_sample_data *data,"; | |
6798 | s.op->line() << " struct pt_regs *regs);"; | |
79189b84 | 6799 | |
dd225250 PS |
6800 | // Emit the actual probe list. |
6801 | ||
6802 | s.op->newline() << "static struct perf_event_attr "; | |
dac77b80 | 6803 | s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
6804 | |
6805 | s.op->newline() << "static struct perf_event **"; | |
dac77b80 | 6806 | s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
6807 | s.op->newline() << "static struct stap_hwbkpt_probe {"; |
6808 | s.op->newline() << "int registered_p:1;"; | |
6809 | // registered_p = 0 signifies a probe that failed registration | |
6810 | // registered_p = 1 signifies a probe that got registered successfully | |
6811 | ||
faea5e16 | 6812 | // Symbol Names are mostly small and uniform enough |
dd225250 | 6813 | // to justify putting const char*. |
dac77b80 | 6814 | s.op->newline() << "const char * const symbol;"; |
dd225250 PS |
6815 | |
6816 | s.op->newline() << "const unsigned long address;"; | |
6817 | s.op->newline() << "uint8_t atype;"; | |
bb0a4e12 | 6818 | s.op->newline() << "unsigned int len;"; |
faea5e16 | 6819 | s.op->newline() << "struct stap_probe probe;"; |
dd225250 PS |
6820 | s.op->newline() << "} stap_hwbkpt_probes[] = {"; |
6821 | s.op->indent(1); | |
6822 | ||
dac77b80 | 6823 | for (unsigned int it = 0; it < hwbkpt_probes.size(); it++) |
dd225250 | 6824 | { |
dac77b80 | 6825 | hwbkpt_derived_probe* p = hwbkpt_probes.at(it); |
dd225250 PS |
6826 | s.op->newline() << "{"; |
6827 | s.op->line() << " .registered_p=1,"; | |
6828 | if (p->symbol_name.size()) | |
6829 | s.op->line() << " .address=(unsigned long)0x0" << "ULL,"; | |
6830 | else | |
6831 | s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,"; | |
6832 | switch(p->hwbkpt_access){ | |
6833 | case HWBKPT_READ: | |
6834 | s.op->line() << " .atype=HW_BREAKPOINT_R ,"; | |
bb0a4e12 | 6835 | break; |
dd225250 PS |
6836 | case HWBKPT_WRITE: |
6837 | s.op->line() << " .atype=HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 6838 | break; |
dd225250 PS |
6839 | case HWBKPT_RW: |
6840 | s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 6841 | break; |
dd225250 PS |
6842 | }; |
6843 | s.op->line() << " .len=" << p->hwbkpt_len << ","; | |
faea5e16 | 6844 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
dd225250 | 6845 | s.op->line() << " .symbol=\"" << p->symbol_name << "\","; |
dd225250 PS |
6846 | s.op->line() << " },"; |
6847 | } | |
dac77b80 | 6848 | s.op->newline(-1) << "};"; |
dd225250 PS |
6849 | |
6850 | // Emit the hwbkpt callback function | |
6851 | s.op->newline() ; | |
6852 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
6853 | s.op->line() << " int nmi,"; | |
6854 | s.op->line() << " struct perf_sample_data *data,"; | |
6855 | s.op->line() << " struct pt_regs *regs) {"; | |
dac77b80 FCE |
6856 | s.op->newline(1) << "unsigned int i;"; |
6857 | s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;"; | |
6858 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; | |
6859 | s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
6860 | // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead? | |
6861 | 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) {"; | |
6862 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];"; | |
faea5e16 | 6863 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe"); |
dd225250 | 6864 | s.op->newline() << "c->regs = regs;"; |
faea5e16 | 6865 | s.op->newline() << "(*sdp->probe.ph) (c);"; |
dd225250 | 6866 | common_probe_entryfn_epilogue (s.op); |
dac77b80 | 6867 | s.op->newline(-1) << "}"; |
dd225250 PS |
6868 | s.op->newline(-1) << "}"; |
6869 | s.op->newline() << "return 0;"; | |
dac77b80 | 6870 | s.op->newline(-1) << "}"; |
dd225250 PS |
6871 | } |
6872 | ||
6873 | void | |
6874 | hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s) | |
6875 | { | |
dac77b80 | 6876 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 PS |
6877 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
6878 | s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
6879 | s.op->newline() << "void *addr = (void *) sdp->address;"; | |
6880 | s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;"; | |
dac77b80 FCE |
6881 | s.op->newline() << "hw_breakpoint_init(hp);"; |
6882 | s.op->newline() << "if (addr)"; | |
6883 | s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;"; | |
6884 | s.op->newline(-1) << "else { "; | |
6885 | s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);"; | |
6886 | s.op->newline() << "if (!hp->bp_addr) { "; | |
faea5e16 | 6887 | s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe.pp,hwbkpt_symbol_name);"; |
dac77b80 FCE |
6888 | s.op->newline() << "continue;"; |
6889 | s.op->newline(-1) << "}"; | |
6890 | s.op->newline(-1) << "}"; | |
6891 | s.op->newline() << "hp->bp_type = sdp->atype;"; | |
6892 | ||
6893 | // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!). | |
6894 | if (s.architecture == "i386" || s.architecture == "x86_64" ) | |
6895 | { | |
6896 | s.op->newline() << "switch(sdp->len) {"; | |
6897 | s.op->newline() << "case 1:"; | |
6898 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;"; | |
6899 | s.op->newline() << "break;"; | |
6900 | s.op->newline(-1) << "case 2:"; | |
6901 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;"; | |
6902 | s.op->newline() << "break;"; | |
6903 | s.op->newline(-1) << "case 3:"; | |
6904 | s.op->newline() << "case 4:"; | |
6905 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;"; | |
6906 | s.op->newline() << "break;"; | |
6907 | s.op->newline(-1) << "case 5:"; | |
6908 | s.op->newline() << "case 6:"; | |
6909 | s.op->newline() << "case 7:"; | |
6910 | s.op->newline() << "case 8:"; | |
6911 | s.op->newline() << "default:"; // XXX: could instead reject | |
6912 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;"; | |
6913 | s.op->newline() << "break;"; | |
6914 | s.op->newline(-1) << "}"; | |
6915 | } | |
6916 | else // other architectures presumed straightforward | |
6917 | s.op->newline() << "hp->bp_len = sdp->len;"; | |
6918 | ||
faea5e16 | 6919 | s.op->newline() << "probe_point = sdp->probe.pp;"; // for error messages |
dac77b80 FCE |
6920 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);"; |
6921 | s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {"; | |
6922 | s.op->newline(1) << "int err_code = PTR_ERR(stap_hwbkpt_ret_array[i]);"; | |
6923 | s.op->newline(0) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, err_code, addr, hwbkpt_symbol_name);"; | |
6924 | s.op->newline(-1) << "}"; | |
dd225250 | 6925 | s.op->newline() << " else sdp->registered_p = 1;"; |
dd225250 PS |
6926 | s.op->newline(-1) << "}"; // for loop |
6927 | } | |
6928 | ||
6929 | void | |
6930 | hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s) | |
6931 | { | |
6932 | //Unregister hwbkpt probes. | |
dac77b80 | 6933 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 | 6934 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
dac77b80 FCE |
6935 | s.op->newline() << "if (sdp->registered_p == 0) continue;"; |
6936 | s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);"; | |
dd225250 PS |
6937 | s.op->newline() << "sdp->registered_p = 0;"; |
6938 | s.op->newline(-1) << "}"; | |
6939 | } | |
6940 | ||
6941 | struct hwbkpt_builder: public derived_probe_builder | |
6942 | { | |
6943 | hwbkpt_builder() {} | |
6944 | virtual void build(systemtap_session & sess, | |
6945 | probe * base, | |
6946 | probe_point * location, | |
6947 | literal_map_t const & parameters, | |
6948 | vector<derived_probe *> & finished_results); | |
6949 | }; | |
6950 | ||
6951 | void | |
6952 | hwbkpt_builder::build(systemtap_session & sess, | |
6953 | probe * base, | |
6954 | probe_point * location, | |
6955 | literal_map_t const & parameters, | |
6956 | vector<derived_probe *> & finished_results) | |
6957 | { | |
6958 | string symbol_str_val; | |
6959 | int64_t hwbkpt_address, len; | |
6960 | bool has_addr, has_symbol_str, has_write, has_rw, has_len; | |
6961 | ||
b47f3a55 FCE |
6962 | if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y"))) |
6963 | throw semantic_error ("CONFIG_PERF_EVENTS not available on this kernel", | |
6964 | location->components[0]->tok); | |
6965 | if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y"))) | |
6966 | throw semantic_error ("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel", | |
6967 | location->components[0]->tok); | |
6968 | ||
dd225250 PS |
6969 | has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address); |
6970 | has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val); | |
6971 | has_len = get_param (parameters, TOK_LENGTH, len); | |
6972 | has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end()); | |
6973 | has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end()); | |
6974 | ||
6975 | if (!has_len) | |
6976 | len = 1; | |
6977 | ||
6978 | if (has_addr) | |
6979 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
6980 | location, | |
6981 | hwbkpt_address, | |
6982 | "",len,0, | |
6983 | has_write, | |
6984 | has_rw)); | |
6985 | else // has symbol_str | |
6986 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
6987 | location, | |
6988 | 0, | |
6989 | symbol_str_val,len,0, | |
6990 | has_write, | |
6991 | has_rw)); | |
6992 | } | |
342d3f96 | 6993 | |
0a6f5a3f JS |
6994 | // ------------------------------------------------------------------------ |
6995 | // statically inserted kernel-tracepoint derived probes | |
6996 | // ------------------------------------------------------------------------ | |
6997 | ||
6fb70fb7 | 6998 | struct tracepoint_arg |
79189b84 | 6999 | { |
ad370dcc | 7000 | string name, c_type, typecast; |
dcaa1a65 | 7001 | bool usable, used, isptr; |
f8a968bc | 7002 | Dwarf_Die type_die; |
dcaa1a65 | 7003 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 7004 | }; |
79189b84 | 7005 | |
0a6f5a3f JS |
7006 | struct tracepoint_derived_probe: public derived_probe |
7007 | { | |
79189b84 JS |
7008 | tracepoint_derived_probe (systemtap_session& s, |
7009 | dwflpp& dw, Dwarf_Die& func_die, | |
7010 | const string& tracepoint_name, | |
7011 | probe* base_probe, probe_point* location); | |
bc9a523d | 7012 | |
79189b84 | 7013 | systemtap_session& sess; |
6fb70fb7 JS |
7014 | string tracepoint_name, header; |
7015 | vector <struct tracepoint_arg> args; | |
bc9a523d | 7016 | |
6fb70fb7 | 7017 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
d0bfd2ac | 7018 | void getargs (std::list<std::string> &arg_set) const; |
79189b84 | 7019 | void join_group (systemtap_session& s); |
3e3bd7b6 | 7020 | void print_dupe_stamp(ostream& o); |
0a6f5a3f | 7021 | }; |
79189b84 JS |
7022 | |
7023 | ||
0a6f5a3f | 7024 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> |
79189b84 | 7025 | { |
79189b84 JS |
7026 | void emit_module_decls (systemtap_session& s); |
7027 | void emit_module_init (systemtap_session& s); | |
7028 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f | 7029 | }; |
79189b84 | 7030 | |
bc9a523d | 7031 | |
f8a968bc JS |
7032 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
7033 | { | |
7034 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
7035 | vector <struct tracepoint_arg>& args): | |
7036 | dw (dw), probe_name (probe_name), args (args) {} | |
7037 | dwflpp& dw; | |
7038 | const string& probe_name; | |
7039 | vector <struct tracepoint_arg>& args; | |
bc9a523d | 7040 | |
f8a968bc JS |
7041 | void visit_target_symbol (target_symbol* e); |
7042 | void visit_target_symbol_arg (target_symbol* e); | |
7043 | void visit_target_symbol_context (target_symbol* e); | |
7044 | }; | |
79189b84 JS |
7045 | |
7046 | ||
f8a968bc JS |
7047 | void |
7048 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
75ead1f7 | 7049 | { |
277c21bc | 7050 | string argname = e->name.substr(1); |
75ead1f7 | 7051 | |
f8a968bc JS |
7052 | // search for a tracepoint parameter matching this name |
7053 | tracepoint_arg *arg = NULL; | |
7054 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 7055 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
7056 | { |
7057 | arg = &args[i]; | |
7058 | arg->used = true; | |
7059 | break; | |
7060 | } | |
75ead1f7 | 7061 | |
f8a968bc JS |
7062 | if (arg == NULL) |
7063 | { | |
7064 | stringstream alternatives; | |
7065 | for (unsigned i = 0; i < args.size(); ++i) | |
7066 | alternatives << " $" << args[i].name; | |
046e7190 | 7067 | alternatives << " $$name $$parms $$vars"; |
75ead1f7 | 7068 | |
f8a968bc JS |
7069 | // We hope that this value ends up not being referenced after all, so it |
7070 | // can be optimized out quietly. | |
277c21bc | 7071 | throw semantic_error("unable to find tracepoint variable '" + e->name |
f8a968bc JS |
7072 | + "' (alternatives:" + alternatives.str () + ")", e->tok); |
7073 | // NB: we can have multiple errors, since a target variable | |
7074 | // may be expanded in several different contexts: | |
7075 | // trace ("*") { $foo->bar } | |
f8a968bc | 7076 | } |
75ead1f7 | 7077 | |
f8a968bc | 7078 | // make sure we're not dereferencing base types |
dc5a09fc JS |
7079 | if (!arg->isptr) |
7080 | e->assert_no_components("tracepoint"); | |
75ead1f7 | 7081 | |
f8a968bc JS |
7082 | // we can only write to dereferenced fields, and only if guru mode is on |
7083 | bool lvalue = is_active_lvalue(e); | |
7084 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
277c21bc | 7085 | throw semantic_error("write to tracepoint variable '" + e->name |
f8a968bc | 7086 | + "' not permitted", e->tok); |
c69a87e0 | 7087 | |
ad370dcc JS |
7088 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
7089 | // is also meaningless until you dereference past a pointer member. It's | |
7090 | // harder to detect and prevent that though... | |
75ead1f7 | 7091 | |
f8a968bc JS |
7092 | if (e->components.empty()) |
7093 | { | |
03c75a4a JS |
7094 | if (e->addressof) |
7095 | throw semantic_error("cannot take address of tracepoint variable", e->tok); | |
a45664f4 | 7096 | |
3e3bd7b6 | 7097 | // Just grab the value from the probe locals |
a45664f4 JS |
7098 | symbol* sym = new symbol; |
7099 | sym->tok = e->tok; | |
7100 | sym->name = "__tracepoint_arg_" + arg->name; | |
7101 | provide (sym); | |
f8a968bc JS |
7102 | } |
7103 | else | |
7104 | { | |
5f36109e JS |
7105 | // make a copy of the original as a bare target symbol for the tracepoint |
7106 | // value, which will be passed into the dwarf dereferencing code | |
7107 | target_symbol* e2 = deep_copy_visitor::deep_copy(e); | |
7108 | e2->components.clear(); | |
7109 | ||
7110 | if (e->components.back().type == target_symbol::comp_pretty_print) | |
7111 | { | |
7112 | if (lvalue) | |
7113 | throw semantic_error("cannot write to pretty-printed variable", e->tok); | |
7114 | ||
7115 | dwarf_pretty_print dpp(dw, &arg->type_die, e2, false, *e); | |
7116 | dpp.expand()->visit (this); | |
7117 | return; | |
7118 | } | |
7119 | ||
f8a968bc JS |
7120 | // Synthesize a function to dereference the dwarf fields, |
7121 | // with a pointer parameter that is the base tracepoint variable | |
7122 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 7123 | fdecl->synthetic = true; |
f8a968bc JS |
7124 | fdecl->tok = e->tok; |
7125 | embeddedcode *ec = new embeddedcode; | |
7126 | ec->tok = e->tok; | |
75ead1f7 | 7127 | |
f8a968bc | 7128 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") |
277c21bc | 7129 | + "_" + e->name.substr(1) |
aca66a36 | 7130 | + "_" + lex_cast(tick++)); |
75ead1f7 | 7131 | |
f8a968bc JS |
7132 | fdecl->name = fname; |
7133 | fdecl->body = ec; | |
75ead1f7 | 7134 | |
b5a0dd41 FCE |
7135 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
7136 | ec->code += "\n#define fetch_register k_fetch_register\n"; | |
7137 | ec->code += "#define store_register k_store_register\n"; | |
c69a87e0 FCE |
7138 | |
7139 | ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e, | |
f8a968bc | 7140 | lvalue, fdecl->type); |
75ead1f7 | 7141 | |
f8a968bc JS |
7142 | // Give the fdecl an argument for the raw tracepoint value |
7143 | vardecl *v1 = new vardecl; | |
7144 | v1->type = pe_long; | |
7145 | v1->name = "pointer"; | |
7146 | v1->tok = e->tok; | |
7147 | fdecl->formal_args.push_back(v1); | |
75ead1f7 | 7148 | |
6fda2dff JS |
7149 | // Any non-literal indexes need to be passed in too. |
7150 | for (unsigned i = 0; i < e->components.size(); ++i) | |
7151 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
7152 | { | |
7153 | vardecl *v = new vardecl; | |
7154 | v->type = pe_long; | |
aca66a36 | 7155 | v->name = "index" + lex_cast(i); |
6fda2dff JS |
7156 | v->tok = e->tok; |
7157 | fdecl->formal_args.push_back(v); | |
7158 | } | |
7159 | ||
f8a968bc JS |
7160 | if (lvalue) |
7161 | { | |
7162 | // Modify the fdecl so it carries a pe_long formal | |
7163 | // argument called "value". | |
75ead1f7 | 7164 | |
f8a968bc JS |
7165 | // FIXME: For the time being we only support setting target |
7166 | // variables which have base types; these are 'pe_long' in | |
7167 | // stap's type vocabulary. Strings and pointers might be | |
7168 | // reasonable, some day, but not today. | |
7169 | ||
7170 | vardecl *v2 = new vardecl; | |
7171 | v2->type = pe_long; | |
7172 | v2->name = "value"; | |
7173 | v2->tok = e->tok; | |
7174 | fdecl->formal_args.push_back(v2); | |
7175 | } | |
7176 | else | |
7177 | ec->code += "/* pure */"; | |
7178 | ||
64211010 DB |
7179 | ec->code += "/* unprivileged */"; |
7180 | ||
b5a0dd41 FCE |
7181 | // PR10601 |
7182 | ec->code += "\n#undef fetch_register\n"; | |
7183 | ec->code += "\n#undef store_register\n"; | |
aff5d390 | 7184 | |
f8809d54 | 7185 | fdecl->join (dw.sess); |
75ead1f7 | 7186 | |
f8a968bc JS |
7187 | // Synthesize a functioncall. |
7188 | functioncall* n = new functioncall; | |
7189 | n->tok = e->tok; | |
7190 | n->function = fname; | |
6fda2dff JS |
7191 | n->args.push_back(require(e2)); |
7192 | ||
7193 | // Any non-literal indexes need to be passed in too. | |
7194 | for (unsigned i = 0; i < e->components.size(); ++i) | |
7195 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
7196 | n->args.push_back(require(e->components[i].expr_index)); | |
75ead1f7 | 7197 | |
f8a968bc JS |
7198 | if (lvalue) |
7199 | { | |
7200 | // Provide the functioncall to our parent, so that it can be | |
7201 | // used to substitute for the assignment node immediately above | |
7202 | // us. | |
7203 | assert(!target_symbol_setter_functioncalls.empty()); | |
7204 | *(target_symbol_setter_functioncalls.top()) = n; | |
7205 | } | |
75ead1f7 | 7206 | |
f8a968bc JS |
7207 | provide (n); |
7208 | } | |
75ead1f7 JS |
7209 | } |
7210 | ||
7211 | ||
f8a968bc JS |
7212 | void |
7213 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
0a6f5a3f | 7214 | { |
03c75a4a JS |
7215 | if (e->addressof) |
7216 | throw semantic_error("cannot take address of context variable", e->tok); | |
7217 | ||
f8a968bc | 7218 | if (is_active_lvalue (e)) |
277c21bc | 7219 | throw semantic_error("write to tracepoint '" + e->name + "' not permitted", e->tok); |
0a6f5a3f | 7220 | |
277c21bc | 7221 | if (e->name == "$$name") |
f8a968bc | 7222 | { |
5f36109e JS |
7223 | e->assert_no_components("tracepoint"); |
7224 | ||
f8a968bc JS |
7225 | // Synthesize a functioncall. |
7226 | functioncall* n = new functioncall; | |
7227 | n->tok = e->tok; | |
7228 | n->function = "_mark_name_get"; | |
f8a968bc JS |
7229 | provide (n); |
7230 | } | |
277c21bc | 7231 | else if (e->name == "$$vars" || e->name == "$$parms") |
f8a968bc | 7232 | { |
5f36109e JS |
7233 | e->assert_no_components("tracepoint", true); |
7234 | ||
f8a968bc JS |
7235 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate |
7236 | // NB: we synthesize a new token here rather than reusing | |
7237 | // e->tok, because print_format::print likes to use | |
7238 | // its tok->content. | |
7239 | token* pf_tok = new token(*e->tok); | |
7240 | pf_tok->content = "sprintf"; | |
0a6f5a3f | 7241 | |
d5e178c1 | 7242 | print_format* pf = print_format::create(pf_tok); |
0a6f5a3f | 7243 | |
f8a968bc | 7244 | for (unsigned i = 0; i < args.size(); ++i) |
b278033a | 7245 | { |
dcaa1a65 JS |
7246 | if (!args[i].usable) |
7247 | continue; | |
f8a968bc JS |
7248 | if (i > 0) |
7249 | pf->raw_components += " "; | |
7250 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 7251 | target_symbol *tsym = new target_symbol; |
f8a968bc | 7252 | tsym->tok = e->tok; |
277c21bc | 7253 | tsym->name = "$" + args[i].name; |
5f36109e | 7254 | tsym->components = e->components; |
b278033a | 7255 | |
f8a968bc JS |
7256 | // every variable should always be accessible! |
7257 | tsym->saved_conversion_error = 0; | |
7258 | expression *texp = require (tsym); // NB: throws nothing ... | |
7259 | assert (!tsym->saved_conversion_error); // ... but this is how we know it happened. | |
b278033a | 7260 | |
5f36109e JS |
7261 | if (!e->components.empty() && |
7262 | e->components[0].type == target_symbol::comp_pretty_print) | |
7263 | pf->raw_components += "=%s"; | |
7264 | else | |
7265 | pf->raw_components += args[i].isptr ? "=%p" : "=%#x"; | |
f8a968bc JS |
7266 | pf->args.push_back(texp); |
7267 | } | |
0a6f5a3f | 7268 | |
f8a968bc JS |
7269 | pf->components = print_format::string_to_components(pf->raw_components); |
7270 | provide (pf); | |
b278033a | 7271 | } |
f8a968bc JS |
7272 | else |
7273 | assert(0); // shouldn't get here | |
0a6f5a3f JS |
7274 | } |
7275 | ||
0a6f5a3f | 7276 | void |
f8a968bc | 7277 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a6f5a3f | 7278 | { |
aff5d390 | 7279 | try |
c69a87e0 | 7280 | { |
277c21bc | 7281 | assert(e->name.size() > 0 && e->name[0] == '$'); |
aff5d390 | 7282 | |
277c21bc | 7283 | if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars") |
c69a87e0 FCE |
7284 | visit_target_symbol_context (e); |
7285 | else | |
7286 | visit_target_symbol_arg (e); | |
7287 | } | |
7288 | catch (const semantic_error &er) | |
7289 | { | |
1af1e62d | 7290 | e->chain (er); |
c69a87e0 FCE |
7291 | provide (e); |
7292 | } | |
0a6f5a3f JS |
7293 | } |
7294 | ||
7295 | ||
7296 | ||
79189b84 JS |
7297 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
7298 | dwflpp& dw, Dwarf_Die& func_die, | |
7299 | const string& tracepoint_name, | |
7300 | probe* base, probe_point* loc): | |
7301 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), | |
7302 | sess (s), tracepoint_name (tracepoint_name) | |
56894e91 | 7303 | { |
79189b84 JS |
7304 | // create synthetic probe point name; preserve condition |
7305 | vector<probe_point::component*> comps; | |
7306 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
7307 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
7308 | this->sole_location()->components = comps; | |
7309 | ||
6fb70fb7 JS |
7310 | // fill out the available arguments in this tracepoint |
7311 | build_args(dw, func_die); | |
56894e91 | 7312 | |
6fb70fb7 JS |
7313 | // determine which header defined this tracepoint |
7314 | string decl_file = dwarf_decl_file(&func_die); | |
7315 | size_t header_pos = decl_file.rfind("trace/"); | |
7316 | if (header_pos == string::npos) | |
7317 | throw semantic_error ("cannot parse header location for tracepoint '" | |
7318 | + tracepoint_name + "' in '" | |
7319 | + decl_file + "'"); | |
7320 | header = decl_file.substr(header_pos); | |
56894e91 | 7321 | |
6fb70fb7 JS |
7322 | // tracepoints from FOO_event_types.h should really be included from FOO.h |
7323 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
7324 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 7325 | // XXX: see also PR9993. |
6fb70fb7 JS |
7326 | header_pos = header.find("_event_types"); |
7327 | if (header_pos != string::npos) | |
7328 | header.erase(header_pos, 12); | |
56894e91 | 7329 | |
f8a968bc JS |
7330 | // Now expand the local variables in the probe body |
7331 | tracepoint_var_expanding_visitor v (dw, name, args); | |
8b095b45 | 7332 | v.replace (this->body); |
a45664f4 JS |
7333 | for (unsigned i = 0; i < args.size(); i++) |
7334 | if (args[i].used) | |
7335 | { | |
7336 | vardecl* v = new vardecl; | |
7337 | v->name = "__tracepoint_arg_" + args[i].name; | |
7338 | v->tok = this->tok; | |
58701b78 | 7339 | v->set_arity(0, this->tok); |
a45664f4 JS |
7340 | v->type = pe_long; |
7341 | v->skip_init = true; | |
7342 | this->locals.push_back (v); | |
7343 | } | |
56894e91 | 7344 | |
79189b84 JS |
7345 | if (sess.verbose > 2) |
7346 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name | |
7347 | << "'" << endl; | |
7348 | } | |
dc38c0ae | 7349 | |
56894e91 | 7350 | |
f8a968bc | 7351 | static bool |
dcaa1a65 | 7352 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
46b84a80 | 7353 | { |
dcaa1a65 | 7354 | switch (dwarf_tag(&arg.type_die)) |
b20febf3 | 7355 | { |
f8a968bc JS |
7356 | case DW_TAG_typedef: |
7357 | case DW_TAG_const_type: | |
7358 | case DW_TAG_volatile_type: | |
7359 | // iterate on the referent type | |
3d1ad340 | 7360 | return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die) |
dcaa1a65 | 7361 | && resolve_tracepoint_arg_type(arg)); |
f8a968bc JS |
7362 | case DW_TAG_base_type: |
7363 | // base types will simply be treated as script longs | |
dcaa1a65 | 7364 | arg.isptr = false; |
f8a968bc JS |
7365 | return true; |
7366 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
7367 | // pointers can be treated as script longs, |
7368 | // and if we know their type, they can also be dereferenced | |
3d1ad340 | 7369 | if (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)) |
dcaa1a65 | 7370 | arg.isptr = true; |
ad370dcc JS |
7371 | arg.typecast = "(intptr_t)"; |
7372 | return true; | |
7373 | case DW_TAG_structure_type: | |
7374 | case DW_TAG_union_type: | |
7375 | // for structs/unions which are passed by value, we turn it into | |
7376 | // a pointer that can be dereferenced. | |
7377 | arg.isptr = true; | |
7378 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 7379 | return true; |
f8a968bc JS |
7380 | default: |
7381 | // should we consider other types too? | |
7382 | return false; | |
b20febf3 | 7383 | } |
56894e91 JS |
7384 | } |
7385 | ||
7386 | ||
7387 | void | |
6fb70fb7 | 7388 | tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die) |
56894e91 | 7389 | { |
6fb70fb7 JS |
7390 | Dwarf_Die arg; |
7391 | if (dwarf_child(&func_die, &arg) == 0) | |
7392 | do | |
7393 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
7394 | { | |
7395 | // build a tracepoint_arg for this parameter | |
7396 | tracepoint_arg tparg; | |
23d106b9 | 7397 | tparg.name = dwarf_diename(&arg); |
56894e91 | 7398 | |
6fb70fb7 | 7399 | // read the type of this parameter |
3d1ad340 | 7400 | if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die) |
f1c8f8a5 | 7401 | || !dwarf_type_name(&tparg.type_die, tparg.c_type)) |
6fb70fb7 JS |
7402 | throw semantic_error ("cannot get type of tracepoint '" |
7403 | + tracepoint_name + "' parameter '" | |
7404 | + tparg.name + "'"); | |
a68f81a2 | 7405 | |
dcaa1a65 | 7406 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
7407 | args.push_back(tparg); |
7408 | if (sess.verbose > 4) | |
7409 | clog << "found parameter for tracepoint '" << tracepoint_name | |
7410 | << "': type:'" << tparg.c_type | |
7411 | << "' name:'" << tparg.name << "'" << endl; | |
7412 | } | |
7413 | while (dwarf_siblingof(&arg, &arg) == 0); | |
56894e91 JS |
7414 | } |
7415 | ||
dc38c0ae | 7416 | void |
d0bfd2ac | 7417 | tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const |
dc38c0ae | 7418 | { |
dcaa1a65 JS |
7419 | for (unsigned i = 0; i < args.size(); ++i) |
7420 | if (args[i].usable) | |
d0bfd2ac | 7421 | arg_set.push_back("$"+args[i].name+":"+args[i].c_type); |
dc38c0ae DS |
7422 | } |
7423 | ||
79189b84 JS |
7424 | void |
7425 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
197a4d62 | 7426 | { |
79189b84 JS |
7427 | if (! s.tracepoint_derived_probes) |
7428 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
7429 | s.tracepoint_derived_probes->enroll (this); | |
7430 | } | |
e38d6504 | 7431 | |
56894e91 | 7432 | |
197a4d62 | 7433 | void |
3e3bd7b6 | 7434 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) |
56894e91 | 7435 | { |
3e3bd7b6 JS |
7436 | for (unsigned i = 0; i < args.size(); i++) |
7437 | if (args[i].used) | |
7438 | o << "__tracepoint_arg_" << args[i].name << endl; | |
197a4d62 | 7439 | } |
56894e91 | 7440 | |
3e3bd7b6 | 7441 | |
9e0cd21a | 7442 | static vector<string> tracepoint_extra_headers (systemtap_session& s) |
47dd066d | 7443 | { |
3c1b3d06 FCE |
7444 | vector<string> they_live; |
7445 | // PR 9993 | |
7446 | // XXX: may need this to be configurable | |
7447 | they_live.push_back ("linux/skbuff.h"); | |
9e0cd21a FCE |
7448 | |
7449 | // PR11649: conditional extra header | |
7450 | // for kvm tracepoints in 2.6.33ish | |
7451 | if (s.kernel_config["CONFIG_KVM"] != string("")) { | |
7452 | they_live.push_back ("linux/kvm_host.h"); | |
7453 | } | |
3c1b3d06 FCE |
7454 | return they_live; |
7455 | } | |
47dd066d WC |
7456 | |
7457 | ||
7458 | void | |
79189b84 | 7459 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) |
47dd066d | 7460 | { |
79189b84 JS |
7461 | if (probes.empty()) |
7462 | return; | |
47dd066d | 7463 | |
96b030fe JS |
7464 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
7465 | s.op->newline(); | |
79189b84 | 7466 | |
3c1b3d06 FCE |
7467 | // PR9993: Add extra headers to work around undeclared types in individual |
7468 | // include/trace/foo.h files | |
9e0cd21a | 7469 | const vector<string>& extra_headers = tracepoint_extra_headers (s); |
3c1b3d06 FCE |
7470 | for (unsigned z=0; z<extra_headers.size(); z++) |
7471 | s.op->newline() << "#include <" << extra_headers[z] << ">\n"; | |
47dd066d | 7472 | |
6fb70fb7 JS |
7473 | for (unsigned i = 0; i < probes.size(); ++i) |
7474 | { | |
7475 | tracepoint_derived_probe *p = probes[i]; | |
47dd066d | 7476 | |
96b030fe JS |
7477 | // emit a separate entry function for each probe, since tracepoints |
7478 | // don't provide any sort of context pointer. | |
392e08b7 | 7479 | s.op->newline() << "#undef TRACE_INCLUDE_FILE"; |
6fb70fb7 | 7480 | s.op->newline() << "#include <" << p->header << ">"; |
5f73a260 JS |
7481 | |
7482 | // Starting in 2.6.35, at the same time NOARGS was added, the callback | |
7483 | // always has a void* as the first parameter. PR11599 | |
7484 | s.op->newline() << "#ifdef DECLARE_TRACE_NOARGS"; | |
7485 | s.op->newline() << "#define STAP_TP_DATA , NULL"; | |
7486 | s.op->newline() << "#define STAP_TP_PROTO void *cb_data" | |
7487 | << " __attribute__ ((unused))"; | |
7488 | if (!p->args.empty()) | |
7489 | s.op->line() << ","; | |
7490 | s.op->newline() << "#else"; | |
7491 | s.op->newline() << "#define STAP_TP_DATA"; | |
7492 | s.op->newline() << "#define STAP_TP_PROTO"; | |
7493 | if (p->args.empty()) | |
7494 | s.op->line() << " void"; | |
7495 | s.op->newline() << "#endif"; | |
7496 | ||
6fb70fb7 | 7497 | s.op->newline() << "static void enter_tracepoint_probe_" << i << "("; |
5f73a260 JS |
7498 | s.op->newline(2) << "STAP_TP_PROTO"; |
7499 | ||
6fb70fb7 JS |
7500 | for (unsigned j = 0; j < p->args.size(); ++j) |
7501 | { | |
7502 | if (j > 0) | |
5f73a260 JS |
7503 | s.op->line() << ","; |
7504 | s.op->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
6fb70fb7 | 7505 | } |
5f73a260 JS |
7506 | s.op->newline() << ")"; |
7507 | s.op->newline(-2) << "{"; | |
7508 | ||
faea5e16 JS |
7509 | s.op->newline(1) << "static const struct stap_probe probe = " |
7510 | << common_probe_init (p) << ";"; | |
7511 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "probe"); | |
f8a968bc | 7512 | s.op->newline() << "c->marker_name = " |
c12d974f FCE |
7513 | << lex_cast_qstring (p->tracepoint_name) |
7514 | << ";"; | |
f8a968bc JS |
7515 | for (unsigned j = 0; j < p->args.size(); ++j) |
7516 | if (p->args[j].used) | |
7517 | { | |
66671fd8 | 7518 | s.op->newline() << "c->probe_locals." << p->name << ".__tracepoint_arg_" |
f8a968bc | 7519 | << p->args[j].name << " = (int64_t)"; |
ad370dcc | 7520 | s.op->line() << p->args[j].typecast; |
f8a968bc JS |
7521 | s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";"; |
7522 | } | |
faea5e16 | 7523 | s.op->newline() << "(*probe.ph) (c);"; |
6fb70fb7 JS |
7524 | common_probe_entryfn_epilogue (s.op); |
7525 | s.op->newline(-1) << "}"; | |
47dd066d | 7526 | |
96b030fe JS |
7527 | // emit normalized registration functions |
7528 | s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {"; | |
7529 | s.op->newline(1) << "return register_trace_" << p->tracepoint_name | |
5f73a260 | 7530 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
96b030fe | 7531 | s.op->newline(-1) << "}"; |
47dd066d | 7532 | |
86758d5f JS |
7533 | // NB: we're not prepared to deal with unreg failures. However, failures |
7534 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
7535 | // weren't even registered. The former should be OKed by the initial | |
7536 | // registration call, and the latter is safe to ignore. | |
7537 | s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {"; | |
7538 | s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name | |
5f73a260 | 7539 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
96b030fe | 7540 | s.op->newline(-1) << "}"; |
6fb70fb7 | 7541 | s.op->newline(); |
5f73a260 JS |
7542 | |
7543 | s.op->newline() << "#undef STAP_TP_DATA"; | |
7544 | s.op->newline() << "#undef STAP_TP_PROTO"; | |
7545 | s.op->newline(); | |
af304783 DS |
7546 | } |
7547 | ||
96b030fe JS |
7548 | // emit an array of registration functions for easy init/shutdown |
7549 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
7550 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 7551 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
7552 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
7553 | s.op->indent(1); | |
7554 | for (unsigned i = 0; i < probes.size(); ++i) | |
7555 | { | |
7556 | s.op->newline () << "{"; | |
7557 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
7558 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
7559 | s.op->line() << " },"; | |
7560 | } | |
7561 | s.op->newline(-1) << "};"; | |
7562 | s.op->newline(); | |
47dd066d WC |
7563 | } |
7564 | ||
7565 | ||
79189b84 JS |
7566 | void |
7567 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
47dd066d | 7568 | { |
79189b84 JS |
7569 | if (probes.size () == 0) |
7570 | return; | |
47dd066d | 7571 | |
79189b84 | 7572 | s.op->newline() << "/* init tracepoint probes */"; |
96b030fe JS |
7573 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
7574 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
7575 | s.op->newline() << "if (rc) {"; | |
7576 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
7577 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
7578 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
7579 | s.op->newline(-1) << "}"; | |
7580 | s.op->newline(-1) << "}"; | |
47dd066d | 7581 | |
bc9a523d FCE |
7582 | // This would be technically proper (on those autoconf-detectable |
7583 | // kernels that include this function in tracepoint.h), however we | |
7584 | // already make several calls to synchronze_sched() during our | |
7585 | // shutdown processes. | |
47dd066d | 7586 | |
bc9a523d FCE |
7587 | // s.op->newline() << "if (rc)"; |
7588 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
7589 | // s.op->indent(-1); | |
79189b84 | 7590 | } |
47dd066d WC |
7591 | |
7592 | ||
79189b84 JS |
7593 | void |
7594 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
47dd066d | 7595 | { |
79189b84 JS |
7596 | if (probes.empty()) |
7597 | return; | |
47dd066d | 7598 | |
96b030fe JS |
7599 | s.op->newline() << "/* deregister tracepoint probes */"; |
7600 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
7601 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
7602 | s.op->indent(-1); | |
47dd066d | 7603 | |
bc9a523d | 7604 | // Not necessary: see above. |
47dd066d | 7605 | |
bc9a523d | 7606 | // s.op->newline() << "tracepoint_synchronize_unregister();"; |
79189b84 | 7607 | } |
b20febf3 | 7608 | |
47dd066d | 7609 | |
75ead1f7 | 7610 | struct tracepoint_query : public base_query |
47dd066d | 7611 | { |
75ead1f7 JS |
7612 | tracepoint_query(dwflpp & dw, const string & tracepoint, |
7613 | probe * base_probe, probe_point * base_loc, | |
7614 | vector<derived_probe *> & results): | |
7615 | base_query(dw, "*"), tracepoint(tracepoint), | |
7616 | base_probe(base_probe), base_loc(base_loc), | |
7617 | results(results) {} | |
47dd066d | 7618 | |
75ead1f7 | 7619 | const string& tracepoint; |
47dd066d | 7620 | |
75ead1f7 JS |
7621 | probe * base_probe; |
7622 | probe_point * base_loc; | |
7623 | vector<derived_probe *> & results; | |
f982c59b | 7624 | set<string> probed_names; |
47dd066d | 7625 | |
75ead1f7 JS |
7626 | void handle_query_module(); |
7627 | int handle_query_cu(Dwarf_Die * cudie); | |
7628 | int handle_query_func(Dwarf_Die * func); | |
b20febf3 | 7629 | |
75ead1f7 JS |
7630 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); |
7631 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
7632 | }; | |
47dd066d WC |
7633 | |
7634 | ||
7635 | void | |
75ead1f7 | 7636 | tracepoint_query::handle_query_module() |
47dd066d | 7637 | { |
75ead1f7 JS |
7638 | // look for the tracepoints in each CU |
7639 | dw.iterate_over_cus(tracepoint_query_cu, this); | |
47dd066d WC |
7640 | } |
7641 | ||
7642 | ||
75ead1f7 JS |
7643 | int |
7644 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
47dd066d | 7645 | { |
75ead1f7 | 7646 | dw.focus_on_cu (cudie); |
47dd066d | 7647 | |
75ead1f7 JS |
7648 | // look at each function to see if it's a tracepoint |
7649 | string function = "stapprobe_" + tracepoint; | |
7650 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
47dd066d WC |
7651 | } |
7652 | ||
7653 | ||
75ead1f7 JS |
7654 | int |
7655 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
47dd066d | 7656 | { |
75ead1f7 | 7657 | dw.focus_on_function (func); |
47dd066d | 7658 | |
60d98537 | 7659 | assert(startswith(dw.function_name, "stapprobe_")); |
75ead1f7 | 7660 | string tracepoint_instance = dw.function_name.substr(10); |
f982c59b JS |
7661 | |
7662 | // check for duplicates -- sometimes tracepoint headers may be indirectly | |
7663 | // included in more than one of our tracequery modules. | |
7664 | if (!probed_names.insert(tracepoint_instance).second) | |
7665 | return DWARF_CB_OK; | |
7666 | ||
79189b84 JS |
7667 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
7668 | tracepoint_instance, | |
7669 | base_probe, base_loc); | |
7670 | results.push_back (dp); | |
75ead1f7 | 7671 | return DWARF_CB_OK; |
47dd066d WC |
7672 | } |
7673 | ||
7674 | ||
75ead1f7 JS |
7675 | int |
7676 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
47dd066d | 7677 | { |
75ead1f7 | 7678 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); |
aa4d21c0 | 7679 | if (systemtap_session::pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 7680 | return q->handle_query_cu(cudie); |
47dd066d WC |
7681 | } |
7682 | ||
7683 | ||
75ead1f7 JS |
7684 | int |
7685 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
47dd066d | 7686 | { |
75ead1f7 | 7687 | tracepoint_query * q = static_cast<tracepoint_query *>(query); |
aa4d21c0 | 7688 | if (systemtap_session::pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 7689 | return q->handle_query_func(func); |
47dd066d WC |
7690 | } |
7691 | ||
7692 | ||
0a6f5a3f | 7693 | struct tracepoint_builder: public derived_probe_builder |
47dd066d | 7694 | { |
0a6f5a3f JS |
7695 | private: |
7696 | dwflpp *dw; | |
7697 | bool init_dw(systemtap_session& s); | |
55e50c24 JS |
7698 | string get_tracequery_module(systemtap_session& s, |
7699 | const vector<string>& headers); | |
47dd066d | 7700 | |
0a6f5a3f | 7701 | public: |
47dd066d | 7702 | |
0a6f5a3f JS |
7703 | tracepoint_builder(): dw(0) {} |
7704 | ~tracepoint_builder() { delete dw; } | |
47dd066d | 7705 | |
0a6f5a3f JS |
7706 | void build_no_more (systemtap_session& s) |
7707 | { | |
7708 | if (dw && s.verbose > 3) | |
7709 | clog << "tracepoint_builder releasing dwflpp" << endl; | |
7710 | delete dw; | |
7711 | dw = NULL; | |
7712 | } | |
47dd066d | 7713 | |
0a6f5a3f JS |
7714 | void build(systemtap_session& s, |
7715 | probe *base, probe_point *location, | |
7716 | literal_map_t const& parameters, | |
7717 | vector<derived_probe*>& finished_results); | |
7718 | }; | |
47dd066d | 7719 | |
47dd066d | 7720 | |
f982c59b JS |
7721 | string |
7722 | tracepoint_builder::get_tracequery_module(systemtap_session& s, | |
55e50c24 | 7723 | const vector<string>& headers) |
0a6f5a3f | 7724 | { |
c95eddf7 | 7725 | if (s.verbose > 2) |
55e50c24 JS |
7726 | { |
7727 | clog << "Pass 2: getting a tracequery for " | |
7728 | << headers.size() << " headers:" << endl; | |
7729 | for (size_t i = 0; i < headers.size(); ++i) | |
7730 | clog << " " << headers[i] << endl; | |
7731 | } | |
c95eddf7 | 7732 | |
a2639cb7 | 7733 | string tracequery_path; |
b278033a JS |
7734 | if (s.use_cache) |
7735 | { | |
7736 | // see if the cached module exists | |
55e50c24 | 7737 | tracequery_path = find_tracequery_hash(s, headers); |
d105f664 | 7738 | if (!tracequery_path.empty() && !s.poison_cache) |
b278033a | 7739 | { |
a2639cb7 | 7740 | int fd = open(tracequery_path.c_str(), O_RDONLY); |
b278033a JS |
7741 | if (fd != -1) |
7742 | { | |
7743 | if (s.verbose > 2) | |
a2639cb7 | 7744 | clog << "Pass 2: using cached " << tracequery_path << endl; |
b278033a | 7745 | close(fd); |
f982c59b | 7746 | return tracequery_path; |
b278033a JS |
7747 | } |
7748 | } | |
7749 | } | |
47dd066d | 7750 | |
b278033a | 7751 | // no cached module, time to make it |
f982c59b | 7752 | |
55e50c24 JS |
7753 | // PR9993: Add extra headers to work around undeclared types in individual |
7754 | // include/trace/foo.h files | |
9e0cd21a | 7755 | vector<string> short_headers = tracepoint_extra_headers(s); |
55e50c24 JS |
7756 | |
7757 | // add each requested tracepoint header | |
7758 | for (size_t i = 0; i < headers.size(); ++i) | |
7759 | { | |
7760 | const string &header = headers[i]; | |
7761 | size_t root_pos = header.rfind("/include/"); | |
7762 | short_headers.push_back((root_pos != string::npos) ? | |
7763 | header.substr(root_pos + 9) : | |
7764 | header); | |
7765 | } | |
f982c59b | 7766 | |
0a6f5a3f | 7767 | string tracequery_ko; |
55e50c24 | 7768 | int rc = make_tracequery(s, tracequery_ko, short_headers); |
0a6f5a3f | 7769 | if (rc != 0) |
c95eddf7 | 7770 | tracequery_ko = "/dev/null"; |
47dd066d | 7771 | |
e16dc041 | 7772 | // try to save tracequery in the cache |
b278033a | 7773 | if (s.use_cache) |
e16dc041 JS |
7774 | copy_file(tracequery_ko, tracequery_path, s.verbose > 2); |
7775 | ||
f982c59b JS |
7776 | return tracequery_ko; |
7777 | } | |
7778 | ||
7779 | ||
7780 | bool | |
7781 | tracepoint_builder::init_dw(systemtap_session& s) | |
7782 | { | |
7783 | if (dw != NULL) | |
7784 | return true; | |
7785 | ||
7786 | vector<string> tracequery_modules; | |
55e50c24 | 7787 | vector<string> system_headers; |
f982c59b JS |
7788 | |
7789 | glob_t trace_glob; | |
7790 | string globs[] = { | |
f982c59b | 7791 | "/include/trace/events/*.h", |
f982c59b | 7792 | "/source/include/trace/events/*.h", |
508968a7 JS |
7793 | "/include/trace/*.h", |
7794 | "/source/include/trace/*.h", | |
f982c59b JS |
7795 | }; |
7796 | for (unsigned z = 0; z < sizeof(globs) / sizeof(globs[0]); z++) | |
7797 | { | |
7798 | string glob_str(s.kernel_build_tree + globs[z]); | |
7799 | glob(glob_str.c_str(), 0, NULL, &trace_glob); | |
7800 | for (unsigned i = 0; i < trace_glob.gl_pathc; ++i) | |
7801 | { | |
7802 | string header(trace_glob.gl_pathv[i]); | |
7803 | ||
7804 | // filter out a few known "internal-only" headers | |
60d98537 JS |
7805 | if (endswith(header, "/define_trace.h") || |
7806 | endswith(header, "/ftrace.h") || | |
7807 | endswith(header, "/trace_events.h") || | |
7808 | endswith(header, "_event_types.h")) | |
f982c59b JS |
7809 | continue; |
7810 | ||
55e50c24 | 7811 | system_headers.push_back(header); |
f982c59b JS |
7812 | } |
7813 | globfree(&trace_glob); | |
7814 | } | |
7815 | ||
55e50c24 JS |
7816 | // First attempt to do all system headers in one go |
7817 | string tracequery_path = get_tracequery_module(s, system_headers); | |
7818 | // NB: An empty tracequery means that the header didn't compile correctly | |
7819 | if (get_file_size(tracequery_path)) | |
7820 | tracequery_modules.push_back(tracequery_path); | |
7821 | else | |
7822 | // Otherwise try to do them one at a time (PR10424) | |
7823 | for (size_t i = 0; i < system_headers.size(); ++i) | |
7824 | { | |
aa4d21c0 | 7825 | if (systemtap_session::pending_interrupts) return false; |
392e08b7 | 7826 | |
55e50c24 JS |
7827 | vector<string> one_header(1, system_headers[i]); |
7828 | tracequery_path = get_tracequery_module(s, one_header); | |
7829 | if (get_file_size(tracequery_path)) | |
7830 | tracequery_modules.push_back(tracequery_path); | |
7831 | } | |
7832 | ||
f982c59b JS |
7833 | // TODO: consider other sources of tracepoint headers too, like from |
7834 | // a command-line parameter or some environment or .systemtaprc | |
47dd066d | 7835 | |
59c11f91 | 7836 | dw = new dwflpp(s, tracequery_modules, true); |
0a6f5a3f JS |
7837 | return true; |
7838 | } | |
47dd066d | 7839 | |
0a6f5a3f JS |
7840 | void |
7841 | tracepoint_builder::build(systemtap_session& s, | |
7842 | probe *base, probe_point *location, | |
7843 | literal_map_t const& parameters, | |
7844 | vector<derived_probe*>& finished_results) | |
7845 | { | |
7846 | if (!init_dw(s)) | |
7847 | return; | |
47dd066d | 7848 | |
75ead1f7 JS |
7849 | string tracepoint; |
7850 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
47dd066d | 7851 | |
75ead1f7 | 7852 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); |
51178501 | 7853 | dw->iterate_over_modules(&query_module, &q); |
47dd066d | 7854 | } |
47dd066d | 7855 | |
e6fe60e7 | 7856 | |
b55bc428 | 7857 | // ------------------------------------------------------------------------ |
bd2b1e68 | 7858 | // Standard tapset registry. |
b55bc428 FCE |
7859 | // ------------------------------------------------------------------------ |
7860 | ||
7a053d3b | 7861 | void |
f8220a7b | 7862 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 7863 | { |
47e0478e | 7864 | register_tapset_been(s); |
93646f4d | 7865 | register_tapset_itrace(s); |
dd0e4fa7 | 7866 | register_tapset_mark(s); |
7a212aa8 | 7867 | register_tapset_procfs(s); |
912e8c59 | 7868 | register_tapset_timers(s); |
b84779a5 | 7869 | register_tapset_utrace(s); |
b98a8d73 | 7870 | |
7a24d422 | 7871 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 7872 | dwarf_derived_probe::register_patterns(s); |
30a279be | 7873 | |
888af770 FCE |
7874 | // XXX: user-space starter set |
7875 | s.pattern_root->bind_num(TOK_PROCESS) | |
7876 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
27dc09b1 | 7877 | ->bind_unprivileged() |
888af770 FCE |
7878 | ->bind(new uprobe_builder ()); |
7879 | s.pattern_root->bind_num(TOK_PROCESS) | |
7880 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
27dc09b1 | 7881 | ->bind_unprivileged() |
888af770 FCE |
7882 | ->bind(new uprobe_builder ()); |
7883 | ||
0a6f5a3f JS |
7884 | // kernel tracepoint probes |
7885 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
7886 | ->bind(new tracepoint_builder()); | |
7887 | ||
e6fe60e7 AM |
7888 | // Kprobe based probe |
7889 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
7890 | ->bind(new kprobe_builder()); | |
7891 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) | |
7892 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
7893 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
7894 | ->bind(new kprobe_builder()); | |
b6371390 JS |
7895 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
7896 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
7897 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
7898 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
7899 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
7900 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
7901 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
7902 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
7903 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
dd225250 PS |
7904 | |
7905 | //Hwbkpt based probe | |
b47f3a55 FCE |
7906 | // NB: we formerly registered the probe point types only if the kernel configuration |
7907 | // allowed it. However, we get better error messages if we allow probes to resolve. | |
7908 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7909 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
7910 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
7911 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
7912 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7913 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
7914 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
7915 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
7916 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7917 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
7918 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
7919 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
7920 | // length supported with address only, not symbol names | |
83ea76b1 WC |
7921 | |
7922 | //perf event based probe | |
4763f713 | 7923 | register_tapset_perf(s); |
b55bc428 | 7924 | } |
dc38c0ae DS |
7925 | |
7926 | ||
b20febf3 FCE |
7927 | vector<derived_probe_group*> |
7928 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 7929 | { |
b20febf3 | 7930 | vector<derived_probe_group*> g; |
912e8c59 JS |
7931 | |
7932 | #define DOONE(x) \ | |
7933 | if (s. x##_derived_probes) \ | |
7934 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
7935 | |
7936 | // Note that order *is* important here. We want to make sure we | |
7937 | // register (actually run) begin probes before any other probe type | |
7938 | // is run. Similarly, when unregistering probes, we want to | |
7939 | // unregister (actually run) end probes after every other probe type | |
7940 | // has be unregistered. To do the latter, | |
7941 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
7942 | DOONE(be); |
7943 | DOONE(dwarf); | |
888af770 | 7944 | DOONE(uprobe); |
b20febf3 FCE |
7945 | DOONE(timer); |
7946 | DOONE(profile); | |
7947 | DOONE(mark); | |
0a6f5a3f | 7948 | DOONE(tracepoint); |
e6fe60e7 | 7949 | DOONE(kprobe); |
dd225250 | 7950 | DOONE(hwbkpt); |
83ea76b1 | 7951 | DOONE(perf); |
b20febf3 | 7952 | DOONE(hrtimer); |
ce82316f | 7953 | DOONE(procfs); |
935447c8 DS |
7954 | |
7955 | // Another "order is important" item. We want to make sure we | |
7956 | // "register" the dummy task_finder probe group after all probe | |
7957 | // groups that use the task_finder. | |
7958 | DOONE(utrace); | |
a96d1db0 | 7959 | DOONE(itrace); |
935447c8 | 7960 | DOONE(task_finder); |
b20febf3 FCE |
7961 | #undef DOONE |
7962 | return g; | |
46b84a80 | 7963 | } |
73267b89 JS |
7964 | |
7965 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |