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