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