]>
Commit | Line | Data |
---|---|---|
56e12059 | 1 | // tapset resolution |
e3bbc038 | 2 | // Copyright (C) 2005-2012 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" |
6b51ee12 | 25 | #include <gelf.h> |
22d737e8 | 26 | |
4d0d9e8a | 27 | #include "sdt_types.h" |
bd2b1e68 | 28 | |
3b579393 FCE |
29 | #include <cstdlib> |
30 | #include <algorithm> | |
bd2b1e68 | 31 | #include <deque> |
56e12059 | 32 | #include <iostream> |
d1bcbe71 | 33 | #include <fstream> |
bd2b1e68 | 34 | #include <map> |
ec4373ff | 35 | #include <set> |
56e12059 | 36 | #include <sstream> |
bd2b1e68 | 37 | #include <stdexcept> |
b55bc428 | 38 | #include <vector> |
e36387d7 | 39 | #include <cstdarg> |
29e64872 | 40 | #include <cassert> |
1969b5bc | 41 | #include <iomanip> |
f781f849 | 42 | #include <cerrno> |
bd2b1e68 GH |
43 | |
44 | extern "C" { | |
df8fadee | 45 | #include <fcntl.h> |
bd2b1e68 | 46 | #include <elfutils/libdwfl.h> |
7a053d3b | 47 | #include <elfutils/libdw.h> |
77de5e9e GH |
48 | #include <dwarf.h> |
49 | #include <elf.h> | |
50 | #include <obstack.h> | |
b20febf3 | 51 | #include <glob.h> |
30a279be | 52 | #include <fnmatch.h> |
5f0a03a6 | 53 | #include <stdio.h> |
349dc70e | 54 | #include <sys/types.h> |
37001baa | 55 | #include <sys/stat.h> |
aaf7ffe8 | 56 | #include <math.h> |
aff5d390 | 57 | #include <regex.h> |
37001baa | 58 | #include <unistd.h> |
6d5d594e | 59 | #include <wordexp.h> |
4b1ad75e RM |
60 | |
61 | #define __STDC_FORMAT_MACROS | |
62 | #include <inttypes.h> | |
bd2b1e68 | 63 | } |
77de5e9e | 64 | |
56e12059 FCE |
65 | |
66 | using namespace std; | |
2171f774 | 67 | using namespace __gnu_cxx; |
56e12059 | 68 | |
47dd066d | 69 | |
b20febf3 FCE |
70 | |
71 | // ------------------------------------------------------------------------ | |
faea5e16 JS |
72 | |
73 | string | |
74 | common_probe_init (derived_probe* p) | |
75 | { | |
26e63673 JS |
76 | assert(p->session_index != (unsigned)-1); |
77 | return "(&stap_probes[" + lex_cast(p->session_index) + "])"; | |
faea5e16 JS |
78 | } |
79 | ||
80 | ||
b20febf3 | 81 | void |
a58d79d0 | 82 | common_probe_entryfn_prologue (translator_output* o, string statestr, |
6eefe942 MW |
83 | string probe, string probe_type, |
84 | bool overload_processing) | |
b20febf3 | 85 | { |
653e6a9a JS |
86 | o->newline() << "#ifdef STP_ALIBI"; |
87 | o->newline() << "atomic_inc(&(" << probe << "->alibi));"; | |
88 | o->newline() << "#else"; | |
89 | ||
72d18b98 | 90 | o->newline() << "struct context* __restrict__ c;"; |
e0a17418 JS |
91 | o->newline() << "#if !INTERRUPTIBLE"; |
92 | o->newline() << "unsigned long flags;"; | |
93 | o->newline() << "#endif"; | |
b20febf3 | 94 | |
a58d79d0 DS |
95 | if (overload_processing) |
96 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
97 | else | |
98 | o->newline() << "#ifdef STP_TIMING"; | |
99 | o->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
b20febf3 | 100 | o->newline() << "#endif"; |
b20febf3 | 101 | |
994aac0e | 102 | o->newline() << "#ifdef STP_TIMING"; |
26e63673 | 103 | o->newline() << "Stat stat = " << probe << "->timing;"; |
994aac0e JS |
104 | o->newline() << "#endif"; |
105 | ||
e0a17418 JS |
106 | o->newline() << "#if INTERRUPTIBLE"; |
107 | o->newline() << "preempt_disable ();"; | |
108 | o->newline() << "#else"; | |
109 | o->newline() << "local_irq_save (flags);"; | |
110 | o->newline() << "#endif"; | |
b20febf3 | 111 | |
c931ec8a | 112 | // Check for enough free enough stack space |
4441e344 | 113 | o->newline() << "#ifdef __KERNEL__"; // XXX needed in dyninst mode? |
d05a1d00 | 114 | o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space |
a63401b1 | 115 | o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space |
d05a1d00 FCE |
116 | // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack |
117 | // NB: see also CONFIG_DEBUG_STACKOVERFLOW | |
b3c3ca7c FCE |
118 | o->newline() << "atomic_inc (& skipped_count);"; |
119 | o->newline() << "#ifdef STP_TIMING"; | |
120 | o->newline() << "atomic_inc (& skipped_count_lowstack);"; | |
121 | o->newline() << "#endif"; | |
c931ec8a FCE |
122 | o->newline() << "goto probe_epilogue;"; |
123 | o->newline(-1) << "}"; | |
4441e344 | 124 | o->newline() << "#endif // __KERNEL__"; |
c931ec8a | 125 | |
b20febf3 FCE |
126 | o->newline() << "if (atomic_read (&session_state) != " << statestr << ")"; |
127 | o->newline(1) << "goto probe_epilogue;"; | |
128 | o->indent(-1); | |
9a604fac | 129 | |
4a0ae64c | 130 | o->newline() << "c = contexts[smp_processor_id()];"; |
b3c3ca7c | 131 | o->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
9c736061 FCE |
132 | o->newline(1) << "#if !INTERRUPTIBLE"; |
133 | o->newline() << "atomic_inc (& skipped_count);"; | |
134 | o->newline() << "#endif"; | |
b3c3ca7c FCE |
135 | o->newline() << "#ifdef STP_TIMING"; |
136 | o->newline() << "atomic_inc (& skipped_count_reentrant);"; | |
c12d974f FCE |
137 | o->newline() << "#ifdef DEBUG_REENTRANCY"; |
138 | o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
26e63673 | 139 | << probe << "->pp, c->probe_point ?: \"?\", smp_processor_id());"; |
c12d974f FCE |
140 | // NB: There is a conceivable race condition here with reading |
141 | // c->probe_point, knowing that this other probe is sort of running. | |
142 | // However, in reality, it's interrupted. Plus even if it were able | |
143 | // to somehow start again, and stop before we read c->probe_point, | |
144 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
145 | o->newline() << "#endif"; | |
b3c3ca7c | 146 | o->newline() << "#endif"; |
9a604fac | 147 | o->newline() << "atomic_dec (& c->busy);"; |
b20febf3 | 148 | o->newline() << "goto probe_epilogue;"; |
9a604fac FCE |
149 | o->newline(-1) << "}"; |
150 | o->newline(); | |
1e00cfb1 | 151 | o->newline() << "c->last_stmt = 0;"; |
9a604fac | 152 | o->newline() << "c->last_error = 0;"; |
a7ed0d3e | 153 | o->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter |
d9aed31e MW |
154 | o->newline() << "c->uregs = 0;"; |
155 | o->newline() << "c->kregs = 0;"; | |
d4670309 | 156 | o->newline() << "#if defined __ia64__"; |
b916df9c | 157 | o->newline() << "c->unwaddr = 0;"; |
d4670309 | 158 | o->newline() << "#endif"; |
26e63673 | 159 | o->newline() << "c->probe_point = " << probe << "->pp;"; |
d48df0cf | 160 | o->newline() << "#ifdef STP_NEED_PROBE_NAME"; |
26e63673 | 161 | o->newline() << "c->probe_name = " << probe << "->pn;"; |
2d767770 | 162 | o->newline() << "#endif"; |
6eefe942 | 163 | o->newline() << "c->probe_type = " << probe_type << ";"; |
6dceb5c9 MW |
164 | // reset Individual Probe State union |
165 | o->newline() << "memset(&c->ips, 0, sizeof(c->ips));"; | |
92c25572 | 166 | o->newline() << "c->probe_flags = 0;"; |
309d67d8 | 167 | o->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp |
9addf322 | 168 | o->newline() << "c->regparm = 0;"; |
309d67d8 | 169 | o->newline() << "#endif"; |
e0a17418 JS |
170 | |
171 | o->newline() << "#if INTERRUPTIBLE"; | |
172 | o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
173 | o->newline() << "#else"; | |
174 | o->newline() << "c->actionremaining = MAXACTION;"; | |
175 | o->newline() << "#endif"; | |
9915575b FCE |
176 | // NB: The following would actually be incorrect. |
177 | // That's because cycles_sum/cycles_base values are supposed to survive | |
178 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
179 | // cycles), the values will be reset. | |
180 | /* | |
f0e6dc63 FCE |
181 | o->newline() << "#ifdef STP_OVERLOAD"; |
182 | o->newline() << "c->cycles_sum = 0;"; | |
183 | o->newline() << "c->cycles_base = 0;"; | |
41c262f3 | 184 | o->newline() << "#endif"; |
9915575b | 185 | */ |
b20febf3 | 186 | } |
9a604fac | 187 | |
a44a0785 | 188 | |
b20febf3 | 189 | void |
a58d79d0 | 190 | common_probe_entryfn_epilogue (translator_output* o, |
7baf48e9 FCE |
191 | bool overload_processing, |
192 | bool suppress_handler_errors) | |
b20febf3 | 193 | { |
a58d79d0 DS |
194 | if (overload_processing) |
195 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
196 | else | |
197 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 198 | o->newline() << "{"; |
a58d79d0 DS |
199 | o->newline(1) << "cycles_t cycles_atend = get_cycles ();"; |
200 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
201 | // fewer, if the hardware counter rolls over really quickly. We | |
202 | // handle 32-bit wraparound here. | |
203 | o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
204 | o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
205 | o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
206 | o->indent(-1); | |
dbb68664 | 207 | |
a58d79d0 | 208 | o->newline() << "#ifdef STP_TIMING"; |
994aac0e | 209 | o->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);"; |
a58d79d0 DS |
210 | o->newline() << "#endif"; |
211 | ||
212 | if (overload_processing) | |
213 | { | |
214 | o->newline() << "#ifdef STP_OVERLOAD"; | |
215 | o->newline() << "{"; | |
216 | // If the cycle count has wrapped (cycles_atend > cycles_base), | |
217 | // let's go ahead and pretend the interval has been reached. | |
218 | // This should reset cycles_base and cycles_sum. | |
219 | o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; | |
220 | o->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
221 | o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
222 | o->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
223 | ||
224 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
225 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
226 | // has overloaded the system and we need to quit. | |
7baf48e9 FCE |
227 | // NB: this is not suppressible via --suppress-runtime-errors, |
228 | // because this is a system safety metric that we cannot trust | |
229 | // unprivileged users to override. | |
a58d79d0 DS |
230 | o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; |
231 | o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
232 | o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
233 | o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);"; | |
551e9f14 | 234 | o->newline() << "atomic_inc (&error_count);"; |
a58d79d0 | 235 | o->newline(-1) << "}"; |
e57b735a | 236 | |
a58d79d0 DS |
237 | o->newline() << "c->cycles_base = cycles_atend;"; |
238 | o->newline() << "c->cycles_sum = 0;"; | |
239 | o->newline(-1) << "}"; | |
240 | o->newline(-1) << "}"; | |
241 | o->newline() << "#endif"; | |
242 | } | |
e57b735a | 243 | |
440f755a JS |
244 | o->newline(-1) << "}"; |
245 | o->newline() << "#endif"; | |
e57b735a | 246 | |
440f755a | 247 | o->newline() << "c->probe_point = 0;"; // vacated |
dc575eac | 248 | o->newline() << "#ifdef STP_NEED_PROBE_NAME"; |
d48df0cf | 249 | o->newline() << "c->probe_name = 0;"; |
dc575eac | 250 | o->newline() << "#endif"; |
6eefe942 | 251 | o->newline() << "c->probe_type = 0;"; |
7baf48e9 FCE |
252 | |
253 | ||
440f755a | 254 | o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; |
7baf48e9 FCE |
255 | o->indent(1); |
256 | if (suppress_handler_errors) // PR 13306 | |
257 | { | |
258 | o->newline() << "atomic_inc (& error_count);"; | |
259 | } | |
260 | else | |
261 | { | |
262 | o->newline() << "if (c->last_stmt != NULL)"; | |
263 | o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
264 | o->newline(-1) << "else"; | |
265 | o->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
266 | o->indent(-1); | |
267 | o->newline() << "atomic_inc (& error_count);"; | |
268 | o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {"; | |
269 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
270 | o->newline() << "_stp_exit ();"; | |
271 | o->newline(-1) << "}"; | |
272 | } | |
273 | ||
440f755a | 274 | o->newline(-1) << "}"; |
7baf48e9 FCE |
275 | |
276 | ||
440f755a | 277 | o->newline() << "atomic_dec (&c->busy);"; |
e57b735a | 278 | |
440f755a JS |
279 | o->newline(-1) << "probe_epilogue:"; // context is free |
280 | o->indent(1); | |
e57b735a | 281 | |
7baf48e9 FCE |
282 | if (! suppress_handler_errors) // PR 13306 |
283 | { | |
284 | // Check for excessive skip counts. | |
285 | o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {"; | |
286 | o->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(& session_state, STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))"; | |
287 | o->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");"; | |
288 | o->newline(-1) << "}"; | |
289 | } | |
e57b735a | 290 | |
440f755a JS |
291 | o->newline() << "#if INTERRUPTIBLE"; |
292 | o->newline() << "preempt_enable_no_resched ();"; | |
293 | o->newline() << "#else"; | |
294 | o->newline() << "local_irq_restore (flags);"; | |
295 | o->newline() << "#endif"; | |
653e6a9a JS |
296 | |
297 | o->newline() << "#endif // STP_ALIBI"; | |
440f755a | 298 | } |
e57b735a | 299 | |
e57b735a | 300 | |
440f755a | 301 | // ------------------------------------------------------------------------ |
e57b735a | 302 | |
440f755a JS |
303 | // ------------------------------------------------------------------------ |
304 | // Dwarf derived probes. "We apologize for the inconvience." | |
305 | // ------------------------------------------------------------------------ | |
e57b735a | 306 | |
4627ed58 JS |
307 | static const string TOK_KERNEL("kernel"); |
308 | static const string TOK_MODULE("module"); | |
309 | static const string TOK_FUNCTION("function"); | |
310 | static const string TOK_INLINE("inline"); | |
311 | static const string TOK_CALL("call"); | |
4bda987e | 312 | static const string TOK_EXPORTED("exported"); |
4627ed58 JS |
313 | static const string TOK_RETURN("return"); |
314 | static const string TOK_MAXACTIVE("maxactive"); | |
315 | static const string TOK_STATEMENT("statement"); | |
316 | static const string TOK_ABSOLUTE("absolute"); | |
317 | static const string TOK_PROCESS("process"); | |
a794dbeb | 318 | static const string TOK_PROVIDER("provider"); |
4627ed58 JS |
319 | static const string TOK_MARK("mark"); |
320 | static const string TOK_TRACE("trace"); | |
321 | static const string TOK_LABEL("label"); | |
63b4fd14 | 322 | static const string TOK_LIBRARY("library"); |
576eaefe | 323 | static const string TOK_PLT("plt"); |
e57b735a | 324 | |
1adf8ef1 | 325 | static int query_cu (Dwarf_Die * cudie, void * arg); |
6b517475 | 326 | static void query_addr(Dwarf_Addr addr, dwarf_query *q); |
e57b735a | 327 | |
440f755a JS |
328 | // Can we handle this query with just symbol-table info? |
329 | enum dbinfo_reqt | |
330 | { | |
331 | dbr_unknown, | |
332 | dbr_none, // kernel.statement(NUM).absolute | |
333 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
334 | dbr_need_dwarf | |
335 | }; | |
e57b735a | 336 | |
20e4a32c | 337 | |
440f755a JS |
338 | struct base_query; // forward decls |
339 | struct dwarf_query; | |
340 | struct dwflpp; | |
341 | struct symbol_table; | |
20e4a32c | 342 | |
a781f401 | 343 | |
440f755a JS |
344 | struct |
345 | symbol_table | |
346 | { | |
347 | module_info *mod_info; // associated module | |
348 | map<string, func_info*> map_by_name; | |
1c6b77e5 JS |
349 | multimap<Dwarf_Addr, func_info*> map_by_addr; |
350 | typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t; | |
440f755a JS |
351 | typedef pair<iterator_t, iterator_t> range_t; |
352 | #ifdef __powerpc__ | |
353 | GElf_Word opd_section; | |
354 | #endif | |
2867a2a1 JS |
355 | void add_symbol(const char *name, bool weak, bool descriptor, |
356 | Dwarf_Addr addr, Dwarf_Addr *high_addr); | |
440f755a | 357 | enum info_status read_symbols(FILE *f, const string& path); |
83ca3872 | 358 | enum info_status read_from_elf_file(const string& path, |
2713ea24 | 359 | systemtap_session &sess); |
83ca3872 | 360 | enum info_status read_from_text_file(const string& path, |
2713ea24 | 361 | systemtap_session &sess); |
440f755a JS |
362 | enum info_status get_from_elf(); |
363 | void prepare_section_rejection(Dwfl_Module *mod); | |
364 | bool reject_section(GElf_Word section); | |
440f755a JS |
365 | void purge_syscall_stubs(); |
366 | func_info *lookup_symbol(const string& name); | |
367 | Dwarf_Addr lookup_symbol_address(const string& name); | |
368 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
3d372d6b | 369 | func_info *get_first_func(); |
7a053d3b | 370 | |
440f755a JS |
371 | symbol_table(module_info *mi) : mod_info(mi) {} |
372 | ~symbol_table(); | |
373 | }; | |
77de5e9e | 374 | |
440f755a JS |
375 | static bool null_die(Dwarf_Die *die) |
376 | { | |
822a6a3d | 377 | static Dwarf_Die null; |
440f755a JS |
378 | return (!die || !memcmp(die, &null, sizeof(null))); |
379 | } | |
c4ce66a1 JS |
380 | |
381 | ||
7a053d3b | 382 | enum |
bd2b1e68 | 383 | function_spec_type |
7a053d3b | 384 | { |
bd2b1e68 GH |
385 | function_alone, |
386 | function_and_file, | |
7a053d3b | 387 | function_file_and_line |
bd2b1e68 GH |
388 | }; |
389 | ||
ec4373ff | 390 | |
bd2b1e68 | 391 | struct dwarf_builder; |
f10534c6 | 392 | struct dwarf_var_expanding_visitor; |
77de5e9e | 393 | |
2930abc7 | 394 | |
b20febf3 FCE |
395 | // XXX: This class is a candidate for subclassing to separate |
396 | // the relocation vs non-relocation variants. Likewise for | |
397 | // kprobe vs kretprobe variants. | |
398 | ||
399 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 400 | { |
b20febf3 FCE |
401 | dwarf_derived_probe (const string& function, |
402 | const string& filename, | |
403 | int line, | |
404 | const string& module, | |
405 | const string& section, | |
406 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 407 | Dwarf_Addr addr, |
b20febf3 FCE |
408 | dwarf_query & q, |
409 | Dwarf_Die* scope_die); | |
20e4a32c | 410 | |
b20febf3 FCE |
411 | string module; |
412 | string section; | |
413 | Dwarf_Addr addr; | |
63b4fd14 | 414 | string path; |
27dc09b1 | 415 | bool has_process; |
2930abc7 | 416 | bool has_return; |
c9bad430 | 417 | bool has_maxactive; |
63b4fd14 | 418 | bool has_library; |
c9bad430 | 419 | long maxactive_val; |
4ad95bbc | 420 | // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition |
b642c901 SC |
421 | string user_path; |
422 | string user_lib; | |
b95e2b79 | 423 | bool access_vars; |
2930abc7 | 424 | |
af234c40 JS |
425 | unsigned saved_longs, saved_strings; |
426 | dwarf_derived_probe* entry_handler; | |
427 | ||
b8da0ad1 | 428 | void printsig (std::ostream &o) const; |
6b66b9f7 | 429 | virtual void join_group (systemtap_session& s); |
9020300d | 430 | void emit_probe_local_init(translator_output * o); |
d0bfd2ac | 431 | void getargs(std::list<std::string> &arg_set) const; |
0a98fd42 | 432 | |
42e38653 | 433 | void emit_privilege_assertion (translator_output*); |
27dc09b1 DB |
434 | void print_dupe_stamp(ostream& o); |
435 | ||
bd2b1e68 | 436 | // Pattern registration helpers. |
7a053d3b | 437 | static void register_statement_variants(match_node * root, |
27dc09b1 | 438 | dwarf_builder * dw, |
42e38653 | 439 | privilege_t privilege); |
fd6602a0 | 440 | static void register_function_variants(match_node * root, |
27dc09b1 | 441 | dwarf_builder * dw, |
42e38653 | 442 | privilege_t privilege); |
440d9b00 DB |
443 | static void register_function_and_statement_variants(systemtap_session& s, |
444 | match_node * root, | |
27dc09b1 | 445 | dwarf_builder * dw, |
42e38653 | 446 | privilege_t privilege); |
b1615c74 JS |
447 | static void register_sdt_variants(systemtap_session& s, |
448 | match_node * root, | |
449 | dwarf_builder * dw); | |
450 | static void register_plt_variants(systemtap_session& s, | |
451 | match_node * root, | |
452 | dwarf_builder * dw); | |
c4ce66a1 | 453 | static void register_patterns(systemtap_session& s); |
6b66b9f7 JS |
454 | |
455 | protected: | |
456 | dwarf_derived_probe(probe *base, | |
457 | probe_point *location, | |
458 | Dwarf_Addr addr, | |
459 | bool has_return): | |
74fe61bc LB |
460 | derived_probe(base, location), addr(addr), has_process(0), |
461 | has_return(has_return), has_maxactive(0), has_library(0), | |
462 | maxactive_val(0), access_vars(false), saved_longs(0), | |
463 | saved_strings(0), entry_handler(0) | |
6b66b9f7 JS |
464 | {} |
465 | ||
466 | private: | |
d0bfd2ac | 467 | list<string> args; |
8c67c337 | 468 | void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr); |
20c6c071 GH |
469 | }; |
470 | ||
dc38c0ae | 471 | |
6b66b9f7 | 472 | struct uprobe_derived_probe: public dwarf_derived_probe |
6d0f3f0c | 473 | { |
6d0f3f0c | 474 | int pid; // 0 => unrestricted |
0973d815 | 475 | |
6d0f3f0c FCE |
476 | uprobe_derived_probe (const string& function, |
477 | const string& filename, | |
478 | int line, | |
479 | const string& module, | |
6d0f3f0c FCE |
480 | const string& section, |
481 | Dwarf_Addr dwfl_addr, | |
482 | Dwarf_Addr addr, | |
483 | dwarf_query & q, | |
6b66b9f7 JS |
484 | Dwarf_Die* scope_die): |
485 | dwarf_derived_probe(function, filename, line, module, section, | |
486 | dwfl_addr, addr, q, scope_die), pid(0) | |
487 | {} | |
6d0f3f0c | 488 | |
0973d815 FCE |
489 | // alternate constructor for process(PID).statement(ADDR).absolute |
490 | uprobe_derived_probe (probe *base, | |
491 | probe_point *location, | |
492 | int pid, | |
493 | Dwarf_Addr addr, | |
6b66b9f7 JS |
494 | bool has_return): |
495 | dwarf_derived_probe(base, location, addr, has_return), pid(pid) | |
496 | {} | |
9ace370f | 497 | |
6d0f3f0c | 498 | void join_group (systemtap_session& s); |
2865d17a | 499 | |
42e38653 | 500 | void emit_privilege_assertion (translator_output*); |
8f6d8c2b | 501 | void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); } |
c0f84e7b SC |
502 | void getargs(std::list<std::string> &arg_set) const; |
503 | void saveargs(int nargs); | |
504 | private: | |
505 | list<string> args; | |
6d0f3f0c FCE |
506 | }; |
507 | ||
dc38c0ae DS |
508 | struct dwarf_derived_probe_group: public derived_probe_group |
509 | { | |
510 | private: | |
b20febf3 FCE |
511 | multimap<string,dwarf_derived_probe*> probes_by_module; |
512 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
513 | |
514 | public: | |
08b5a50c | 515 | dwarf_derived_probe_group() {} |
b20febf3 FCE |
516 | void enroll (dwarf_derived_probe* probe); |
517 | void emit_module_decls (systemtap_session& s); | |
518 | void emit_module_init (systemtap_session& s); | |
b4be7cbc | 519 | void emit_module_refresh (systemtap_session& s); |
b20febf3 | 520 | void emit_module_exit (systemtap_session& s); |
dc38c0ae DS |
521 | }; |
522 | ||
523 | ||
20c6c071 | 524 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 525 | struct base_query |
20c6c071 | 526 | { |
c4ce66a1 JS |
527 | base_query(dwflpp & dw, literal_map_t const & params); |
528 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 529 | virtual ~base_query() {} |
bd2b1e68 | 530 | |
5227f1ea | 531 | systemtap_session & sess; |
2c384610 | 532 | dwflpp & dw; |
5227f1ea | 533 | |
bd2b1e68 | 534 | // Parameter extractors. |
86bf665e | 535 | static bool has_null_param(literal_map_t const & params, |
888af770 | 536 | string const & k); |
86bf665e | 537 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 538 | string const & k, string & v); |
86bf665e | 539 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 540 | string const & k, long & v); |
86bf665e | 541 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 542 | string const & k, Dwarf_Addr & v); |
f301a9ff | 543 | static void query_library_callback (void *object, const char *data); |
576eaefe | 544 | static void query_plt_callback (void *object, const char *link, size_t addr); |
f301a9ff | 545 | virtual void query_library (const char *data) = 0; |
576eaefe | 546 | virtual void query_plt (const char *link, size_t addr) = 0; |
84c84ac4 | 547 | |
b55bc428 | 548 | |
2c384610 DS |
549 | // Extracted parameters. |
550 | bool has_kernel; | |
91af0778 FCE |
551 | bool has_module; |
552 | bool has_process; | |
63b4fd14 | 553 | bool has_library; |
576eaefe SC |
554 | bool has_plt; |
555 | bool has_statement; | |
2c384610 | 556 | string module_val; // has_kernel => module_val = "kernel" |
63b4fd14 | 557 | string path; // executable path if module is a .so |
576eaefe | 558 | string plt_val; // has_plt => plt wildcard |
2c384610 DS |
559 | |
560 | virtual void handle_query_module() = 0; | |
561 | }; | |
562 | ||
563 | ||
c4ce66a1 | 564 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
576eaefe | 565 | sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false) |
2c384610 | 566 | { |
91af0778 | 567 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
568 | if (has_kernel) |
569 | module_val = "kernel"; | |
91af0778 FCE |
570 | |
571 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
572 | if (has_module) | |
573 | has_process = false; | |
4baf0e53 | 574 | else |
d0a7f5a9 | 575 | { |
63b4fd14 | 576 | string library_name; |
576eaefe | 577 | long statement_num_val; |
d0a7f5a9 | 578 | has_process = get_string_param(params, TOK_PROCESS, module_val); |
63b4fd14 | 579 | has_library = get_string_param (params, TOK_LIBRARY, library_name); |
576eaefe SC |
580 | if ((has_plt = has_null_param (params, TOK_PLT))) |
581 | plt_val = "*"; | |
582 | else has_plt = get_string_param (params, TOK_PLT, plt_val); | |
18418d34 SC |
583 | if (has_plt) |
584 | sess.consult_symtab = true; | |
576eaefe SC |
585 | has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val); |
586 | ||
84c84ac4 | 587 | if (has_process) |
05fb3e0c | 588 | module_val = find_executable (module_val, sess.sysroot, sess.sysenv); |
84c84ac4 SC |
589 | if (has_library) |
590 | { | |
591 | if (! contains_glob_chars (library_name)) | |
592 | { | |
05fb3e0c WF |
593 | path = path_remove_sysroot(sess, module_val); |
594 | module_val = find_executable (library_name, sess.sysroot, | |
595 | sess.sysenv, "LD_LIBRARY_PATH"); | |
47e226ed SC |
596 | if (module_val.find('/') == string::npos) |
597 | { | |
598 | // We didn't find library_name so use iterate_over_libraries | |
599 | module_val = path; | |
600 | path = library_name; | |
601 | } | |
84c84ac4 SC |
602 | } |
603 | else | |
604 | path = library_name; | |
605 | } | |
d0a7f5a9 | 606 | } |
91af0778 FCE |
607 | |
608 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
609 | } |
610 | ||
c4ce66a1 | 611 | base_query::base_query(dwflpp & dw, const string & module_val) |
576eaefe SC |
612 | : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false), |
613 | module_val(module_val) | |
c4ce66a1 JS |
614 | { |
615 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
616 | // which means that userspace modules won't get any PATH searching. | |
617 | if (module_val.find('/') == string::npos) | |
618 | { | |
619 | has_kernel = (module_val == TOK_KERNEL); | |
620 | has_module = !has_kernel; | |
621 | has_process = false; | |
622 | } | |
623 | else | |
624 | { | |
625 | has_kernel = has_module = false; | |
626 | has_process = true; | |
627 | } | |
628 | } | |
629 | ||
2c384610 | 630 | bool |
86bf665e | 631 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
632 | string const & k) |
633 | { | |
888af770 | 634 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
635 | } |
636 | ||
637 | ||
638 | bool | |
86bf665e | 639 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
640 | string const & k, string & v) |
641 | { | |
642 | return derived_probe_builder::get_param (params, k, v); | |
643 | } | |
644 | ||
645 | ||
646 | bool | |
86bf665e | 647 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
648 | string const & k, long & v) |
649 | { | |
650 | int64_t value; | |
651 | bool present = derived_probe_builder::get_param (params, k, value); | |
652 | v = (long) value; | |
653 | return present; | |
654 | } | |
655 | ||
656 | ||
657 | bool | |
86bf665e | 658 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
659 | string const & k, Dwarf_Addr & v) |
660 | { | |
661 | int64_t value; | |
662 | bool present = derived_probe_builder::get_param (params, k, value); | |
663 | v = (Dwarf_Addr) value; | |
664 | return present; | |
665 | } | |
666 | ||
2c384610 DS |
667 | struct dwarf_query : public base_query |
668 | { | |
e1278bd4 | 669 | dwarf_query(probe * base_probe, |
2c384610 DS |
670 | probe_point * base_loc, |
671 | dwflpp & dw, | |
86bf665e | 672 | literal_map_t const & params, |
b642c901 SC |
673 | vector<derived_probe *> & results, |
674 | const string user_path, | |
675 | const string user_lib); | |
2c384610 | 676 | |
c4ce66a1 | 677 | vector<derived_probe *> & results; |
8f14e444 | 678 | set<string> inlined_non_returnable; // function names |
c4ce66a1 JS |
679 | probe * base_probe; |
680 | probe_point * base_loc; | |
b642c901 SC |
681 | string user_path; |
682 | string user_lib; | |
c4ce66a1 | 683 | |
2c384610 | 684 | virtual void handle_query_module(); |
5f0a03a6 JK |
685 | void query_module_dwarf(); |
686 | void query_module_symtab(); | |
5d5bd369 | 687 | void query_library (const char *data); |
576eaefe | 688 | void query_plt (const char *entry, size_t addr); |
2c384610 | 689 | |
2930abc7 FCE |
690 | void add_probe_point(string const & funcname, |
691 | char const * filename, | |
692 | int line, | |
693 | Dwarf_Die *scope_die, | |
694 | Dwarf_Addr addr); | |
36f9dd1d | 695 | |
857bdfd1 JS |
696 | // Track addresses we've already seen in a given module |
697 | set<Dwarf_Addr> alias_dupes; | |
698 | ||
7fdd3e2c JS |
699 | // Track inlines we've already seen as well |
700 | // NB: this can't be compared just by entrypc, as inlines can overlap | |
701 | set<inline_instance_info> inline_dupes; | |
702 | ||
2930abc7 | 703 | // Extracted parameters. |
7a053d3b | 704 | string function_val; |
20c6c071 GH |
705 | |
706 | bool has_function_str; | |
707 | bool has_statement_str; | |
708 | bool has_function_num; | |
709 | bool has_statement_num; | |
7a053d3b RM |
710 | string statement_str_val; |
711 | string function_str_val; | |
c239d28c GH |
712 | Dwarf_Addr statement_num_val; |
713 | Dwarf_Addr function_num_val; | |
20c6c071 | 714 | |
b8da0ad1 | 715 | bool has_call; |
4bda987e | 716 | bool has_exported; |
b8da0ad1 | 717 | bool has_inline; |
20c6c071 GH |
718 | bool has_return; |
719 | ||
c9bad430 DS |
720 | bool has_maxactive; |
721 | long maxactive_val; | |
722 | ||
20c6c071 GH |
723 | bool has_label; |
724 | string label_val; | |
725 | ||
726 | bool has_relative; | |
727 | long relative_val; | |
728 | ||
37ebca01 FCE |
729 | bool has_absolute; |
730 | ||
467bea43 SC |
731 | bool has_mark; |
732 | ||
5f0a03a6 JK |
733 | enum dbinfo_reqt dbinfo_reqt; |
734 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
735 | ||
7d6d0afc | 736 | void parse_function_spec(const string & spec); |
20c6c071 | 737 | function_spec_type spec_type; |
7d6d0afc | 738 | vector<string> scopes; |
20c6c071 GH |
739 | string function; |
740 | string file; | |
0c8b7d37 | 741 | line_t line_type; |
879eb9e9 | 742 | int line[2]; |
5f0a03a6 | 743 | bool query_done; // Found exact match |
20c6c071 | 744 | |
bd25380d | 745 | set<string> filtered_srcfiles; |
7e1279ea FCE |
746 | |
747 | // Map official entrypc -> func_info object | |
86bf665e TM |
748 | inline_instance_map_t filtered_inlines; |
749 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
750 | bool choose_next_line; |
751 | Dwarf_Addr entrypc_for_next_line; | |
4df79aaf JS |
752 | |
753 | void query_module_functions (); | |
b55bc428 FCE |
754 | }; |
755 | ||
98afd80e | 756 | |
435f53a7 FCE |
757 | static void delete_session_module_cache (systemtap_session& s); // forward decl |
758 | ||
759 | ||
98afd80e | 760 | struct dwarf_builder: public derived_probe_builder |
b55bc428 | 761 | { |
665e1256 | 762 | map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */ |
7a24d422 | 763 | map <string,dwflpp*> user_dw; |
b642c901 SC |
764 | string user_path; |
765 | string user_lib; | |
ae2552da | 766 | dwarf_builder() {} |
aa30ccd3 | 767 | |
ae2552da | 768 | dwflpp *get_kern_dw(systemtap_session& sess, const string& module) |
707bf35e | 769 | { |
ea14cf67 FCE |
770 | if (kern_dw[module] == 0) |
771 | kern_dw[module] = new dwflpp(sess, module, true); // might throw | |
ae2552da | 772 | return kern_dw[module]; |
707bf35e JS |
773 | } |
774 | ||
775 | dwflpp *get_user_dw(systemtap_session& sess, const string& module) | |
776 | { | |
ea14cf67 FCE |
777 | if (user_dw[module] == 0) |
778 | user_dw[module] = new dwflpp(sess, module, false); // might throw | |
707bf35e JS |
779 | return user_dw[module]; |
780 | } | |
7a24d422 FCE |
781 | |
782 | /* NB: not virtual, so can be called from dtor too: */ | |
822a6a3d | 783 | void dwarf_build_no_more (bool) |
aa30ccd3 | 784 | { |
435f53a7 FCE |
785 | delete_map(kern_dw); |
786 | delete_map(user_dw); | |
7a24d422 FCE |
787 | } |
788 | ||
789 | void build_no_more (systemtap_session &s) | |
790 | { | |
791 | dwarf_build_no_more (s.verbose > 3); | |
435f53a7 | 792 | delete_session_module_cache (s); |
aa30ccd3 FCE |
793 | } |
794 | ||
e38d6504 RM |
795 | ~dwarf_builder() |
796 | { | |
7a24d422 | 797 | dwarf_build_no_more (false); |
c8959a29 | 798 | } |
aa30ccd3 | 799 | |
5227f1ea | 800 | virtual void build(systemtap_session & sess, |
7a053d3b | 801 | probe * base, |
20c6c071 | 802 | probe_point * location, |
86bf665e | 803 | literal_map_t const & parameters, |
20c6c071 | 804 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
805 | }; |
806 | ||
5111fc3e | 807 | |
e1278bd4 | 808 | dwarf_query::dwarf_query(probe * base_probe, |
20c6c071 GH |
809 | probe_point * base_loc, |
810 | dwflpp & dw, | |
86bf665e | 811 | literal_map_t const & params, |
b642c901 SC |
812 | vector<derived_probe *> & results, |
813 | const string user_path, | |
814 | const string user_lib) | |
c4ce66a1 | 815 | : base_query(dw, params), results(results), |
b642c901 | 816 | base_probe(base_probe), base_loc(base_loc), |
74fe61bc LB |
817 | user_path(user_path), user_lib(user_lib), has_relative(false), |
818 | relative_val(0), choose_next_line(false), entrypc_for_next_line(0) | |
bd2b1e68 GH |
819 | { |
820 | // Reduce the query to more reasonable semantic values (booleans, | |
821 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
822 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
823 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
824 | ||
825 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
826 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
827 | ||
0f336e95 SC |
828 | has_label = get_string_param(params, TOK_LABEL, label_val); |
829 | ||
b8da0ad1 | 830 | has_call = has_null_param(params, TOK_CALL); |
4bda987e | 831 | has_exported = has_null_param(params, TOK_EXPORTED); |
b8da0ad1 | 832 | has_inline = has_null_param(params, TOK_INLINE); |
bd2b1e68 | 833 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 834 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 835 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 836 | has_mark = false; |
37ebca01 | 837 | |
bd2b1e68 | 838 | if (has_function_str) |
7d6d0afc | 839 | parse_function_spec(function_str_val); |
bd2b1e68 | 840 | else if (has_statement_str) |
7d6d0afc | 841 | parse_function_spec(statement_str_val); |
0daad364 | 842 | |
5f0a03a6 JK |
843 | dbinfo_reqt = assess_dbinfo_reqt(); |
844 | query_done = false; | |
0daad364 JS |
845 | } |
846 | ||
847 | ||
440f755a JS |
848 | func_info_map_t * |
849 | get_filtered_functions(dwarf_query *q) | |
850 | { | |
851 | return &q->filtered_functions; | |
852 | } | |
853 | ||
854 | ||
855 | inline_instance_map_t * | |
856 | get_filtered_inlines(dwarf_query *q) | |
857 | { | |
858 | return &q->filtered_inlines; | |
859 | } | |
860 | ||
861 | ||
2c384610 | 862 | void |
5f0a03a6 | 863 | dwarf_query::query_module_dwarf() |
2c384610 DS |
864 | { |
865 | if (has_function_num || has_statement_num) | |
866 | { | |
867 | // If we have module("foo").function(0xbeef) or | |
868 | // module("foo").statement(0xbeef), the address is relative | |
869 | // to the start of the module, so we seek the function | |
870 | // number plus the module's bias. | |
6b517475 JS |
871 | Dwarf_Addr addr = has_function_num ? |
872 | function_num_val : statement_num_val; | |
08d1d520 MW |
873 | |
874 | // These are raw addresses, we need to know what the elf_bias | |
875 | // is to feed it to libdwfl based functions. | |
876 | Dwarf_Addr elf_bias; | |
877 | Elf *elf = dwfl_module_getelf (dw.module, &elf_bias); | |
878 | assert(elf); | |
879 | addr += elf_bias; | |
6b517475 | 880 | query_addr(addr, this); |
2c384610 DS |
881 | } |
882 | else | |
883 | { | |
884 | // Otherwise if we have a function("foo") or statement("foo") | |
885 | // specifier, we have to scan over all the CUs looking for | |
886 | // the function(s) in question | |
887 | assert(has_function_str || has_statement_str); | |
4df79aaf JS |
888 | |
889 | // For simple cases, no wildcard and no source:line, we can do a very | |
890 | // quick function lookup in a module-wide cache. | |
1ffb8bd1 JS |
891 | if (spec_type == function_alone && |
892 | !dw.name_has_wildcard(function) && | |
893 | !startswith(function, "_Z")) | |
4df79aaf JS |
894 | query_module_functions(); |
895 | else | |
337b7c44 | 896 | dw.iterate_over_cus(&query_cu, this, false); |
2c384610 DS |
897 | } |
898 | } | |
899 | ||
5f0a03a6 JK |
900 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
901 | dwarf_query * q); | |
902 | ||
903 | void | |
904 | dwarf_query::query_module_symtab() | |
905 | { | |
906 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
907 | if (dbinfo_reqt == dbr_need_dwarf) | |
908 | return; | |
909 | ||
910 | module_info *mi = dw.mod_info; | |
911 | if (dbinfo_reqt == dbr_need_symtab) | |
912 | { | |
913 | if (mi->symtab_status == info_unknown) | |
914 | mi->get_symtab(this); | |
915 | if (mi->symtab_status == info_absent) | |
916 | return; | |
917 | } | |
918 | ||
919 | func_info *fi = NULL; | |
920 | symbol_table *sym_table = mi->sym_table; | |
921 | ||
922 | if (has_function_str) | |
923 | { | |
924 | // Per dwarf_query::assess_dbinfo_reqt()... | |
925 | assert(spec_type == function_alone); | |
926 | if (dw.name_has_wildcard(function_str_val)) | |
927 | { | |
928 | // Until we augment the blacklist sufficently... | |
929 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
930 | { | |
931 | // e.g., kernel.function("*") | |
b530b5b3 LB |
932 | cerr << _F("Error: Pattern '%s' matches every single " |
933 | "instruction address in the symbol table,\n" | |
934 | "some of which aren't even functions.\n", function_str_val.c_str()) << endl; | |
5f0a03a6 JK |
935 | return; |
936 | } | |
2e67a43b | 937 | symbol_table::iterator_t iter; |
1c6b77e5 JS |
938 | for (iter = sym_table->map_by_addr.begin(); |
939 | iter != sym_table->map_by_addr.end(); | |
2e67a43b | 940 | ++iter) |
5f0a03a6 | 941 | { |
1c6b77e5 | 942 | fi = iter->second; |
5f0a03a6 JK |
943 | if (!null_die(&fi->die)) |
944 | continue; // already handled in query_module_dwarf() | |
945 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
946 | query_func_info(fi->addr, *fi, this); | |
947 | } | |
948 | } | |
949 | else | |
950 | { | |
951 | fi = sym_table->lookup_symbol(function_str_val); | |
2867a2a1 | 952 | if (fi && !fi->descriptor && null_die(&fi->die)) |
5f0a03a6 JK |
953 | query_func_info(fi->addr, *fi, this); |
954 | } | |
955 | } | |
956 | else | |
957 | { | |
958 | assert(has_function_num || has_statement_num); | |
959 | // Find the "function" in which the indicated address resides. | |
960 | Dwarf_Addr addr = | |
961 | (has_function_num ? function_num_val : statement_num_val); | |
576eaefe | 962 | if (has_plt) |
3d372d6b SC |
963 | { |
964 | // Use the raw address from the .plt | |
965 | fi = sym_table->get_first_func(); | |
966 | fi->addr = addr; | |
967 | } | |
968 | else | |
969 | fi = sym_table->get_func_containing_address(addr); | |
970 | ||
5f0a03a6 JK |
971 | if (!fi) |
972 | { | |
2713ea24 CM |
973 | sess.print_warning(_F("address %#" PRIx64 " out of range for module %s", |
974 | addr, dw.module_name.c_str())); | |
975 | return; | |
5f0a03a6 JK |
976 | } |
977 | if (!null_die(&fi->die)) | |
978 | { | |
979 | // addr looks like it's in the compilation unit containing | |
980 | // the indicated function, but query_module_dwarf() didn't | |
981 | // match addr to any compilation unit, so addr must be | |
982 | // above that cu's address range. | |
2713ea24 CM |
983 | sess.print_warning(_F("address %#" PRIx64 " maps to no known compilation unit in module %s", |
984 | addr, dw.module_name.c_str())); | |
5f0a03a6 JK |
985 | return; |
986 | } | |
987 | query_func_info(fi->addr, *fi, this); | |
988 | } | |
989 | } | |
990 | ||
991 | void | |
992 | dwarf_query::handle_query_module() | |
993 | { | |
1c6b77e5 JS |
994 | bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab; |
995 | dw.get_module_dwarf(false, report); | |
996 | ||
997 | // prebuild the symbol table to resolve aliases | |
998 | dw.mod_info->get_symtab(this); | |
999 | ||
857bdfd1 JS |
1000 | // reset the dupe-checking for each new module |
1001 | alias_dupes.clear(); | |
7fdd3e2c | 1002 | inline_dupes.clear(); |
857bdfd1 | 1003 | |
5f0a03a6 JK |
1004 | if (dw.mod_info->dwarf_status == info_present) |
1005 | query_module_dwarf(); | |
1c6b77e5 | 1006 | |
5f0a03a6 JK |
1007 | // Consult the symbol table if we haven't found all we're looking for. |
1008 | // asm functions can show up in the symbol table but not in dwarf. | |
1009 | if (sess.consult_symtab && !query_done) | |
1010 | query_module_symtab(); | |
1011 | } | |
1012 | ||
2c384610 | 1013 | |
7d6d0afc JS |
1014 | void |
1015 | dwarf_query::parse_function_spec(const string & spec) | |
bd2b1e68 | 1016 | { |
1d12a9b2 JS |
1017 | line_type = ABSOLUTE; |
1018 | line[0] = line[1] = 0; | |
1019 | ||
91699a70 | 1020 | size_t src_pos, line_pos, dash_pos, scope_pos; |
bd2b1e68 | 1021 | |
7d6d0afc | 1022 | // look for named scopes |
91699a70 JS |
1023 | scope_pos = spec.rfind("::"); |
1024 | if (scope_pos != string::npos) | |
bd2b1e68 | 1025 | { |
91699a70 JS |
1026 | tokenize_cxx(spec.substr(0, scope_pos), scopes); |
1027 | scope_pos += 2; | |
bd2b1e68 | 1028 | } |
91699a70 JS |
1029 | else |
1030 | scope_pos = 0; | |
bd2b1e68 | 1031 | |
7d6d0afc JS |
1032 | // look for a source separator |
1033 | src_pos = spec.find('@', scope_pos); | |
1034 | if (src_pos == string::npos) | |
bd2b1e68 | 1035 | { |
7d6d0afc JS |
1036 | function = spec.substr(scope_pos); |
1037 | spec_type = function_alone; | |
bd2b1e68 | 1038 | } |
7d6d0afc | 1039 | else |
879eb9e9 | 1040 | { |
7d6d0afc | 1041 | function = spec.substr(scope_pos, src_pos - scope_pos); |
7a053d3b | 1042 | |
7d6d0afc JS |
1043 | // look for a line-number separator |
1044 | line_pos = spec.find_first_of(":+", src_pos); | |
1045 | if (line_pos == string::npos) | |
1046 | { | |
1047 | file = spec.substr(src_pos + 1); | |
1048 | spec_type = function_and_file; | |
1049 | } | |
1050 | else | |
1051 | { | |
1052 | file = spec.substr(src_pos + 1, line_pos - src_pos - 1); | |
1053 | ||
1054 | // classify the line spec | |
1055 | spec_type = function_file_and_line; | |
1056 | if (spec[line_pos] == '+') | |
1057 | line_type = RELATIVE; | |
1058 | else if (spec[line_pos + 1] == '*' && | |
1059 | spec.length() == line_pos + 2) | |
1060 | line_type = WILDCARD; | |
1061 | else | |
1062 | line_type = ABSOLUTE; | |
1063 | ||
1064 | if (line_type != WILDCARD) | |
1065 | try | |
1066 | { | |
1067 | // try to parse either N or N-M | |
1068 | dash_pos = spec.find('-', line_pos + 1); | |
1069 | if (dash_pos == string::npos) | |
1070 | line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1)); | |
1071 | else | |
1072 | { | |
1073 | line_type = RANGE; | |
1074 | line[0] = lex_cast<int>(spec.substr(line_pos + 1, | |
1075 | dash_pos - line_pos - 1)); | |
1076 | line[1] = lex_cast<int>(spec.substr(dash_pos + 1)); | |
1077 | } | |
1078 | } | |
1079 | catch (runtime_error & exn) | |
1080 | { | |
1081 | goto bad; | |
1082 | } | |
1083 | } | |
bd2b1e68 GH |
1084 | } |
1085 | ||
7d6d0afc JS |
1086 | if (function.empty() || |
1087 | (spec_type != function_alone && file.empty())) | |
bd2b1e68 GH |
1088 | goto bad; |
1089 | ||
7d6d0afc | 1090 | if (sess.verbose > 2) |
bd2b1e68 | 1091 | { |
b530b5b3 LB |
1092 | //clog << "parsed '" << spec << "'"; |
1093 | clog << _F("parse '%s'", spec.c_str()); | |
41c262f3 | 1094 | |
7d6d0afc JS |
1095 | if (!scopes.empty()) |
1096 | clog << ", scope '" << scopes[0] << "'"; | |
1097 | for (unsigned i = 1; i < scopes.size(); ++i) | |
1098 | clog << "::'" << scopes[i] << "'"; | |
41c262f3 | 1099 | |
7d6d0afc JS |
1100 | clog << ", func '" << function << "'"; |
1101 | ||
1102 | if (spec_type != function_alone) | |
1103 | clog << ", file '" << file << "'"; | |
1104 | ||
1105 | if (spec_type == function_file_and_line) | |
1106 | { | |
1107 | clog << ", line "; | |
1108 | switch (line_type) | |
1109 | { | |
1110 | case ABSOLUTE: | |
1111 | clog << line[0]; | |
1112 | break; | |
1113 | ||
1114 | case RELATIVE: | |
1115 | clog << "+" << line[0]; | |
1116 | break; | |
1117 | ||
1118 | case RANGE: | |
1119 | clog << line[0] << " - " << line[1]; | |
1120 | break; | |
1121 | ||
1122 | case WILDCARD: | |
1123 | clog << "*"; | |
1124 | break; | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | clog << endl; | |
bd2b1e68 GH |
1129 | } |
1130 | ||
7d6d0afc JS |
1131 | return; |
1132 | ||
1133 | bad: | |
b530b5b3 | 1134 | throw semantic_error(_F("malformed specification '%s'", spec.c_str()), |
7d6d0afc | 1135 | base_probe->tok); |
bd2b1e68 GH |
1136 | } |
1137 | ||
05fb3e0c WF |
1138 | string path_remove_sysroot(const systemtap_session& sess, const string& path) |
1139 | { | |
1140 | size_t pos; | |
1141 | string retval = path; | |
1142 | if (!sess.sysroot.empty() && | |
1143 | (pos = retval.find(sess.sysroot)) != string::npos) | |
1144 | retval.replace(pos, sess.sysroot.length(), "/"); | |
1145 | return retval; | |
1146 | } | |
bd2b1e68 | 1147 | |
36f9dd1d | 1148 | void |
1ffb8bd1 | 1149 | dwarf_query::add_probe_point(const string& dw_funcname, |
b20febf3 | 1150 | const char* filename, |
36f9dd1d | 1151 | int line, |
b20febf3 | 1152 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
1153 | Dwarf_Addr addr) |
1154 | { | |
b20febf3 | 1155 | string reloc_section; // base section for relocation purposes |
27646582 | 1156 | Dwarf_Addr reloc_addr; // relocated |
b20febf3 | 1157 | const string& module = dw.module_name; // "kernel" or other |
1ffb8bd1 | 1158 | string funcname = dw_funcname; |
36f9dd1d | 1159 | |
37ebca01 FCE |
1160 | assert (! has_absolute); // already handled in dwarf_builder::build() |
1161 | ||
576eaefe SC |
1162 | if (!has_plt) |
1163 | reloc_addr = dw.relocate_address(addr, reloc_section); | |
1164 | else | |
1165 | { | |
3d372d6b | 1166 | // Set the reloc_section but use the plt entry for reloc_addr |
576eaefe SC |
1167 | dw.relocate_address(addr, reloc_section); |
1168 | reloc_addr = addr; | |
1169 | } | |
2930abc7 | 1170 | |
1ffb8bd1 | 1171 | // If we originally used the linkage name, then let's call it that way |
1ffb8bd1 JS |
1172 | const char* linkage_name; |
1173 | if (scope_die && startswith (this->function, "_Z") | |
f450a7e3 | 1174 | && (linkage_name = dwarf_linkage_name (scope_die))) |
1ffb8bd1 JS |
1175 | funcname = linkage_name; |
1176 | ||
7f9f3386 FCE |
1177 | if (sess.verbose > 1) |
1178 | { | |
b530b5b3 | 1179 | clog << _("probe ") << funcname << "@" << filename << ":" << line; |
b20febf3 | 1180 | if (string(module) == TOK_KERNEL) |
b530b5b3 | 1181 | clog << _(" kernel"); |
91af0778 | 1182 | else if (has_module) |
b530b5b3 | 1183 | clog << _(" module=") << module; |
91af0778 | 1184 | else if (has_process) |
b530b5b3 | 1185 | clog << _(" process=") << module; |
b20febf3 | 1186 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
b20febf3 | 1187 | clog << " pc=0x" << hex << addr << dec; |
7f9f3386 | 1188 | } |
4baf0e53 | 1189 | |
27646582 | 1190 | bool bad = dw.blacklisted_p (funcname, filename, line, module, |
789448a3 | 1191 | addr, has_return); |
b20febf3 FCE |
1192 | if (sess.verbose > 1) |
1193 | clog << endl; | |
7f9f3386 | 1194 | |
84048984 FCE |
1195 | if (module == TOK_KERNEL) |
1196 | { | |
1197 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
1198 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 1199 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
1200 | } |
1201 | ||
b20febf3 FCE |
1202 | if (! bad) |
1203 | { | |
1a0dbc5a | 1204 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
1205 | |
1206 | if (has_process) | |
1207 | { | |
05fb3e0c | 1208 | string module_tgt = path_remove_sysroot(sess, module); |
6d0f3f0c | 1209 | results.push_back (new uprobe_derived_probe(funcname, filename, line, |
05fb3e0c | 1210 | module_tgt, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1211 | *this, scope_die)); |
1212 | } | |
1213 | else | |
1214 | { | |
1215 | assert (has_kernel || has_module); | |
1216 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 1217 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1218 | *this, scope_die)); |
1219 | } | |
b20febf3 | 1220 | } |
2930abc7 FCE |
1221 | } |
1222 | ||
5f0a03a6 JK |
1223 | enum dbinfo_reqt |
1224 | dwarf_query::assess_dbinfo_reqt() | |
1225 | { | |
1226 | if (has_absolute) | |
1227 | { | |
1228 | // kernel.statement(NUM).absolute | |
1229 | return dbr_none; | |
1230 | } | |
1231 | if (has_inline) | |
1232 | { | |
1233 | // kernel.function("f").inline or module("m").function("f").inline | |
1234 | return dbr_need_dwarf; | |
1235 | } | |
1236 | if (has_function_str && spec_type == function_alone) | |
1237 | { | |
1238 | // kernel.function("f") or module("m").function("f") | |
1239 | return dbr_need_symtab; | |
1240 | } | |
1241 | if (has_statement_num) | |
1242 | { | |
1243 | // kernel.statement(NUM) or module("m").statement(NUM) | |
1244 | // Technically, all we need is the module offset (or _stext, for | |
1245 | // the kernel). But for that we need either the ELF file or (for | |
1246 | // _stext) the symbol table. In either case, the symbol table | |
1247 | // is available, and that allows us to map the NUM (address) | |
1248 | // to a function, which is goodness. | |
1249 | return dbr_need_symtab; | |
1250 | } | |
1251 | if (has_function_num) | |
1252 | { | |
1253 | // kernel.function(NUM) or module("m").function(NUM) | |
1254 | // Need the symbol table so we can back up from NUM to the | |
1255 | // start of the function. | |
1256 | return dbr_need_symtab; | |
1257 | } | |
1258 | // Symbol table tells us nothing about source files or line numbers. | |
1259 | return dbr_need_dwarf; | |
1260 | } | |
2930abc7 FCE |
1261 | |
1262 | ||
b8da0ad1 FCE |
1263 | // The critical determining factor when interpreting a pattern |
1264 | // string is, perhaps surprisingly: "presence of a lineno". The | |
1265 | // presence of a lineno changes the search strategy completely. | |
1266 | // | |
1267 | // Compare the two cases: | |
1268 | // | |
1269 | // 1. {statement,function}(foo@file.c:lineno) | |
1270 | // - find the files matching file.c | |
1271 | // - in each file, find the functions matching foo | |
1272 | // - query the file for line records matching lineno | |
1273 | // - iterate over the line records, | |
1274 | // - and iterate over the functions, | |
1275 | // - if(haspc(function.DIE, line.addr)) | |
1276 | // - if looking for statements: probe(lineno.addr) | |
1277 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
1278 | // | |
1279 | // 2. {statement,function}(foo@file.c) | |
1280 | // - find the files matching file.c | |
1281 | // - in each file, find the functions matching foo | |
1282 | // - probe(function.{entrypc,return,etc.}) | |
1283 | // | |
1284 | // Thus the first decision we make is based on the presence of a | |
1285 | // lineno, and we enter entirely different sets of callbacks | |
1286 | // depending on that decision. | |
1287 | // | |
1288 | // Note that the first case is a generalization fo the second, in that | |
1289 | // we could theoretically search through line records for matching | |
1290 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
1291 | // are already cached elsewhere, so we can do an "index scan" as an | |
1292 | // optimization. | |
7e1279ea | 1293 | |
bd2b1e68 | 1294 | static void |
4cd232e4 | 1295 | query_statement (string const & func, |
20e4a32c | 1296 | char const * file, |
4cd232e4 | 1297 | int line, |
bcc12710 | 1298 | Dwarf_Die *scope_die, |
20e4a32c | 1299 | Dwarf_Addr stmt_addr, |
4cd232e4 | 1300 | dwarf_query * q) |
bd2b1e68 | 1301 | { |
39bcd429 FCE |
1302 | try |
1303 | { | |
cee35f73 | 1304 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 1305 | line, scope_die, stmt_addr); |
39bcd429 FCE |
1306 | } |
1307 | catch (const semantic_error& e) | |
1308 | { | |
1309 | q->sess.print_error (e); | |
1310 | } | |
bd2b1e68 GH |
1311 | } |
1312 | ||
6b517475 JS |
1313 | static void |
1314 | query_addr(Dwarf_Addr addr, dwarf_query *q) | |
1315 | { | |
1316 | dwflpp &dw = q->dw; | |
1317 | ||
08d1d520 MW |
1318 | if (q->sess.verbose > 2) |
1319 | clog << "query_addr 0x" << hex << addr << dec << endl; | |
6b517475 JS |
1320 | |
1321 | // First pick which CU contains this address | |
1322 | Dwarf_Die* cudie = dw.query_cu_containing_address(addr); | |
1323 | if (!cudie) // address could be wildly out of range | |
1324 | return; | |
1325 | dw.focus_on_cu(cudie); | |
1326 | ||
1327 | // Now compensate for the dw bias | |
1328 | addr -= dw.module_bias; | |
1329 | ||
1330 | // Per PR5787, we look up the scope die even for | |
1331 | // statement_num's, for blacklist sensitivity and $var | |
1332 | // resolution purposes. | |
1333 | ||
1334 | // Find the scopes containing this address | |
1335 | vector<Dwarf_Die> scopes = dw.getscopes(addr); | |
1336 | if (scopes.empty()) | |
1337 | return; | |
1338 | ||
1339 | // Look for the innermost containing function | |
1340 | Dwarf_Die *fnscope = NULL; | |
1341 | for (size_t i = 0; i < scopes.size(); ++i) | |
1342 | { | |
1343 | int tag = dwarf_tag(&scopes[i]); | |
1344 | if ((tag == DW_TAG_subprogram && !q->has_inline) || | |
1345 | (tag == DW_TAG_inlined_subroutine && | |
4bda987e | 1346 | !q->has_call && !q->has_return && !q->has_exported)) |
6b517475 JS |
1347 | { |
1348 | fnscope = &scopes[i]; | |
1349 | break; | |
1350 | } | |
1351 | } | |
1352 | if (!fnscope) | |
1353 | return; | |
1354 | dw.focus_on_function(fnscope); | |
1355 | ||
1356 | Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0]; | |
1357 | ||
1358 | const char *file = dwarf_decl_file(fnscope); | |
1359 | int line; | |
1360 | dwarf_decl_line(fnscope, &line); | |
1361 | ||
1362 | // Function probes should reset the addr to the function entry | |
1363 | // and possibly perform prologue searching | |
1364 | if (q->has_function_num) | |
1365 | { | |
1366 | dw.die_entrypc(fnscope, &addr); | |
1367 | if (dwarf_tag(fnscope) == DW_TAG_subprogram && | |
1368 | (q->sess.prologue_searching || q->has_process)) // PR 6871 | |
1369 | { | |
1370 | func_info func; | |
1371 | func.die = *fnscope; | |
1372 | func.name = dw.function_name; | |
1373 | func.decl_file = file; | |
1374 | func.decl_line = line; | |
1375 | func.entrypc = addr; | |
1376 | ||
1377 | func_info_map_t funcs(1, func); | |
1378 | dw.resolve_prologue_endings (funcs); | |
464379bb FCE |
1379 | if (q->has_return) // PR13200 |
1380 | { | |
1381 | if (q->sess.verbose > 2) | |
1382 | clog << "ignoring prologue for .return probes" << endl; | |
1383 | } | |
1384 | else | |
1385 | { | |
1386 | if (funcs[0].prologue_end) | |
1387 | addr = funcs[0].prologue_end; | |
1388 | } | |
6b517475 JS |
1389 | } |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr)); | |
1394 | if (address_line) | |
1395 | { | |
1396 | file = address_line.linesrc(); | |
1397 | line = address_line.lineno(); | |
1398 | } | |
1399 | ||
1400 | // Verify that a raw address matches the beginning of a | |
1401 | // statement. This is a somewhat lame check that the address | |
1402 | // is at the start of an assembly instruction. Mark probes are in the | |
1403 | // middle of a macro and thus not strictly at a statement beginning. | |
1404 | // Guru mode may override this check. | |
1405 | if (!q->has_mark && (!address_line || address_line.addr() != addr)) | |
1406 | { | |
1407 | stringstream msg; | |
2a97f50b | 1408 | msg << _F("address %#" PRIx64 " does not match the beginning of a statement", |
b530b5b3 | 1409 | addr); |
6b517475 | 1410 | if (address_line) |
2a97f50b | 1411 | msg << _F(" (try %#" PRIx64 ")", address_line.addr()); |
6b517475 | 1412 | else |
b530b5b3 LB |
1413 | msg << _F(" (no line info found for '%s', in module '%s')", |
1414 | dw.cu_name().c_str(), dw.module_name.c_str()); | |
6b517475 JS |
1415 | if (! q->sess.guru_mode) |
1416 | throw semantic_error(msg.str()); | |
2713ea24 | 1417 | else |
6b517475 JS |
1418 | q->sess.print_warning(msg.str()); |
1419 | } | |
1420 | } | |
1421 | ||
1422 | // Build a probe at this point | |
1423 | query_statement(dw.function_name, file, line, scope, addr, q); | |
1424 | } | |
1425 | ||
8096dd7d JS |
1426 | static void |
1427 | query_label (string const & func, | |
1428 | char const * label, | |
1429 | char const * file, | |
1430 | int line, | |
1431 | Dwarf_Die *scope_die, | |
1432 | Dwarf_Addr stmt_addr, | |
1433 | dwarf_query * q) | |
1434 | { | |
6b517475 JS |
1435 | assert (q->has_statement_str || q->has_function_str); |
1436 | ||
8096dd7d JS |
1437 | size_t i = q->results.size(); |
1438 | ||
1439 | // weed out functions whose decl_file isn't one of | |
1440 | // the source files that we actually care about | |
6b517475 | 1441 | if (q->spec_type != function_alone && |
8096dd7d JS |
1442 | q->filtered_srcfiles.count(file) == 0) |
1443 | return; | |
1444 | ||
1445 | query_statement(func, file, line, scope_die, stmt_addr, q); | |
1446 | ||
c72aa911 JS |
1447 | // after the fact, insert the label back into the derivation chain |
1448 | probe_point::component* ppc = | |
1449 | new probe_point::component(TOK_LABEL, new literal_string (label)); | |
1450 | for (; i < q->results.size(); ++i) | |
1451 | { | |
1452 | derived_probe* p = q->results[i]; | |
1453 | probe_point* pp = new probe_point(*p->locations[0]); | |
1454 | pp->components.push_back (ppc); | |
1455 | p->base = p->base->create_alias(p->locations[0], pp); | |
1456 | } | |
8096dd7d JS |
1457 | } |
1458 | ||
7e1279ea | 1459 | static void |
3e961ba6 | 1460 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
1461 | dwarf_query * q) |
1462 | { | |
b6581717 | 1463 | try |
7e1279ea | 1464 | { |
8f14e444 FCE |
1465 | assert (! q->has_return); // checked by caller already |
1466 | if (q->sess.verbose>2) | |
b530b5b3 LB |
1467 | clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n", |
1468 | ii.entrypc, ii.name.c_str()); | |
8f14e444 FCE |
1469 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
1470 | &ii.die, ii.entrypc, q); | |
7e1279ea | 1471 | } |
b6581717 | 1472 | catch (semantic_error &e) |
7e1279ea | 1473 | { |
b6581717 | 1474 | q->sess.print_error (e); |
7e1279ea FCE |
1475 | } |
1476 | } | |
1477 | ||
1478 | static void | |
1479 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 1480 | func_info & fi, |
7e1279ea FCE |
1481 | dwarf_query * q) |
1482 | { | |
b6581717 | 1483 | try |
7e1279ea | 1484 | { |
b6581717 GH |
1485 | if (q->has_return) |
1486 | { | |
1487 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
1488 | // kretprobe based on the entrypc in this case. | |
464379bb FCE |
1489 | if (fi.prologue_end != 0 && q->has_return) // PR13200 |
1490 | { | |
1491 | if (q->sess.verbose > 2) | |
1492 | clog << "ignoring prologue for .return probes" << endl; | |
1493 | } | |
20e4a32c | 1494 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
1495 | &fi.die, entrypc, q); |
1496 | } | |
1497 | else | |
1498 | { | |
35dc8b04 | 1499 | if (fi.prologue_end != 0) |
44f75386 | 1500 | { |
44f75386 FCE |
1501 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1502 | &fi.die, fi.prologue_end, q); | |
1503 | } | |
1504 | else | |
1505 | { | |
1506 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
1507 | &fi.die, entrypc, q); | |
1508 | } | |
b6581717 | 1509 | } |
7e1279ea | 1510 | } |
b6581717 | 1511 | catch (semantic_error &e) |
7e1279ea | 1512 | { |
b6581717 | 1513 | q->sess.print_error (e); |
7e1279ea FCE |
1514 | } |
1515 | } | |
1516 | ||
1517 | ||
bd4b874d SC |
1518 | static void |
1519 | query_srcfile_label (const dwarf_line_t& line, void * arg) | |
1520 | { | |
1521 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1522 | ||
1523 | Dwarf_Addr addr = line.addr(); | |
1524 | ||
1525 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1526 | i != q->filtered_functions.end(); ++i) | |
1527 | if (q->dw.die_has_pc (i->die, addr)) | |
f09d0d1e JS |
1528 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, |
1529 | q, query_label); | |
1530 | ||
1531 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1532 | i != q->filtered_inlines.end(); ++i) | |
1533 | if (q->dw.die_has_pc (i->die, addr)) | |
1534 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1535 | q, query_label); | |
bd4b874d SC |
1536 | } |
1537 | ||
7e1279ea | 1538 | static void |
86bf665e | 1539 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
1540 | { |
1541 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1542 | ||
86bf665e | 1543 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 1544 | |
86bf665e | 1545 | int lineno = line.lineno(); |
847bf07f | 1546 | |
86bf665e | 1547 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
1548 | i != q->filtered_functions.end(); ++i) |
1549 | { | |
3e961ba6 | 1550 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1551 | { |
b0ee93c4 | 1552 | if (q->sess.verbose>3) |
b530b5b3 | 1553 | clog << _("function DIE lands on srcfile\n"); |
4cd232e4 | 1554 | if (q->has_statement_str) |
f5958c8f JS |
1555 | { |
1556 | Dwarf_Die scope; | |
1557 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1558 | query_statement (i->name, i->decl_file, | |
1559 | lineno, // NB: not q->line ! | |
1560 | &scope, addr, q); | |
1561 | } | |
4cd232e4 | 1562 | else |
3e961ba6 | 1563 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 1564 | } |
20e4a32c RM |
1565 | } |
1566 | ||
86bf665e | 1567 | for (inline_instance_map_t::iterator i |
897820ca GH |
1568 | = q->filtered_inlines.begin(); |
1569 | i != q->filtered_inlines.end(); ++i) | |
1570 | { | |
3e961ba6 | 1571 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1572 | { |
b0ee93c4 | 1573 | if (q->sess.verbose>3) |
b530b5b3 | 1574 | clog << _("inline instance DIE lands on srcfile\n"); |
897820ca | 1575 | if (q->has_statement_str) |
f5958c8f JS |
1576 | { |
1577 | Dwarf_Die scope; | |
1578 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1579 | query_statement (i->name, i->decl_file, | |
1580 | q->line[0], &scope, addr, q); | |
1581 | } | |
897820ca | 1582 | else |
3e961ba6 | 1583 | query_inline_instance_info (*i, q); |
897820ca | 1584 | } |
20e4a32c | 1585 | } |
7e1279ea FCE |
1586 | } |
1587 | ||
1588 | ||
7fdd3e2c JS |
1589 | bool |
1590 | inline_instance_info::operator<(const inline_instance_info& other) const | |
1591 | { | |
1592 | if (entrypc != other.entrypc) | |
1593 | return entrypc < other.entrypc; | |
1594 | ||
1595 | if (decl_line != other.decl_line) | |
1596 | return decl_line < other.decl_line; | |
1597 | ||
1598 | int cmp = name.compare(other.name); | |
1599 | if (!cmp) | |
1600 | cmp = strcmp(decl_file, other.decl_file); | |
1601 | return cmp < 0; | |
1602 | } | |
1603 | ||
1604 | ||
4fa7b22b | 1605 | static int |
7e1279ea | 1606 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
1607 | { |
1608 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
6b517475 | 1609 | assert (q->has_statement_str || q->has_function_str); |
4bda987e | 1610 | assert (!q->has_call && !q->has_return && !q->has_exported); |
bd2b1e68 | 1611 | |
39bcd429 | 1612 | try |
7a053d3b | 1613 | { |
b0ee93c4 | 1614 | if (q->sess.verbose>2) |
b530b5b3 | 1615 | clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str()); |
7e1279ea | 1616 | |
6b517475 JS |
1617 | Dwarf_Addr entrypc; |
1618 | if (q->dw.die_entrypc (die, &entrypc)) | |
1619 | { | |
1620 | inline_instance_info inl; | |
1621 | inl.die = *die; | |
1622 | inl.name = q->dw.function_name; | |
1623 | inl.entrypc = entrypc; | |
1624 | q->dw.function_file (&inl.decl_file); | |
1625 | q->dw.function_line (&inl.decl_line); | |
1626 | ||
1627 | // make sure that this inline hasn't already | |
1628 | // been matched from a different CU | |
1629 | if (q->inline_dupes.insert(inl).second) | |
1630 | q->filtered_inlines.push_back(inl); | |
1631 | } | |
7e1279ea FCE |
1632 | return DWARF_CB_OK; |
1633 | } | |
1634 | catch (const semantic_error& e) | |
1635 | { | |
1636 | q->sess.print_error (e); | |
1637 | return DWARF_CB_ABORT; | |
1638 | } | |
1639 | } | |
bb788f9f | 1640 | |
7e1279ea | 1641 | static int |
2da9cedb | 1642 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 1643 | { |
2da9cedb | 1644 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
6b517475 | 1645 | assert (q->has_statement_str || q->has_function_str); |
bb788f9f | 1646 | |
bd25380d JS |
1647 | // weed out functions whose decl_file isn't one of |
1648 | // the source files that we actually care about | |
6b517475 | 1649 | if (q->spec_type != function_alone && |
bd25380d | 1650 | q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0) |
8096dd7d | 1651 | return DWARF_CB_OK; |
bd25380d | 1652 | |
7e1279ea FCE |
1653 | try |
1654 | { | |
7e1279ea FCE |
1655 | q->dw.focus_on_function (func); |
1656 | ||
7d6d0afc JS |
1657 | if (!q->dw.function_scope_matches(q->scopes)) |
1658 | return DWARF_CB_OK; | |
1659 | ||
857bdfd1 JS |
1660 | // make sure that this function address hasn't |
1661 | // already been matched under an aliased name | |
1662 | Dwarf_Addr addr; | |
1663 | if (!q->dw.func_is_inline() && | |
1664 | dwarf_entrypc(func, &addr) == 0 && | |
1665 | !q->alias_dupes.insert(addr).second) | |
1666 | return DWARF_CB_OK; | |
1667 | ||
4bda987e | 1668 | if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported)) |
7e1279ea | 1669 | { |
4bda987e | 1670 | if (q->sess.verbose>3) |
b530b5b3 | 1671 | clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str()); |
4bda987e | 1672 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
7e1279ea | 1673 | } |
8f14e444 FCE |
1674 | else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553 |
1675 | { | |
1676 | q->inlined_non_returnable.insert (q->dw.function_name); | |
1677 | } | |
396afcee | 1678 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 1679 | { |
4bda987e SC |
1680 | if (q->has_exported && !q->dw.func_is_exported ()) |
1681 | return DWARF_CB_OK; | |
6b517475 | 1682 | if (q->sess.verbose>2) |
b530b5b3 | 1683 | clog << _F("selected function %s\n", q->dw.function_name.c_str()); |
6b517475 JS |
1684 | |
1685 | func_info func; | |
1686 | q->dw.function_die (&func.die); | |
1687 | func.name = q->dw.function_name; | |
1688 | q->dw.function_file (&func.decl_file); | |
1689 | q->dw.function_line (&func.decl_line); | |
1690 | ||
1691 | Dwarf_Addr entrypc; | |
1692 | if (q->dw.function_entrypc (&entrypc)) | |
1693 | { | |
1694 | func.entrypc = entrypc; | |
1695 | q->filtered_functions.push_back (func); | |
1696 | } | |
1697 | /* else this function is fully inlined, just ignore it */ | |
7e1279ea | 1698 | } |
39bcd429 | 1699 | return DWARF_CB_OK; |
bd2b1e68 | 1700 | } |
39bcd429 | 1701 | catch (const semantic_error& e) |
bd2b1e68 | 1702 | { |
39bcd429 FCE |
1703 | q->sess.print_error (e); |
1704 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1705 | } |
bd2b1e68 GH |
1706 | } |
1707 | ||
1708 | static int | |
1709 | query_cu (Dwarf_Die * cudie, void * arg) | |
1710 | { | |
20c6c071 | 1711 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
6b517475 JS |
1712 | assert (q->has_statement_str || q->has_function_str); |
1713 | ||
85007c04 | 1714 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 1715 | |
39bcd429 | 1716 | try |
bd2b1e68 | 1717 | { |
7e1279ea | 1718 | q->dw.focus_on_cu (cudie); |
b5d77020 | 1719 | |
b0ee93c4 | 1720 | if (false && q->sess.verbose>2) |
b530b5b3 LB |
1721 | clog << _F("focused on CU '%s', in module '%s'\n", |
1722 | q->dw.cu_name().c_str(), q->dw.module_name.c_str()); | |
d9b516ca | 1723 | |
6b517475 JS |
1724 | q->filtered_srcfiles.clear(); |
1725 | q->filtered_functions.clear(); | |
1726 | q->filtered_inlines.clear(); | |
1727 | ||
1728 | // In this path, we find "abstract functions", record | |
1729 | // information about them, and then (depending on lineno | |
1730 | // matching) possibly emit one or more of the function's | |
1731 | // associated addresses. Unfortunately the control of this | |
1732 | // cannot easily be turned inside out. | |
1733 | ||
1734 | if (q->spec_type != function_alone) | |
39bcd429 | 1735 | { |
6b517475 JS |
1736 | // If we have a pattern string with a filename, we need |
1737 | // to elaborate the srcfile mask in question first. | |
1738 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
1739 | ||
1740 | // If we have a file pattern and *no* srcfile matches, there's | |
1741 | // no need to look further into this CU, so skip. | |
1742 | if (q->filtered_srcfiles.empty()) | |
1743 | return DWARF_CB_OK; | |
1744 | } | |
e4c58386 | 1745 | |
6b517475 JS |
1746 | // Pick up [entrypc, name, DIE] tuples for all the functions |
1747 | // matching the query, and fill in the prologue endings of them | |
1748 | // all in a single pass. | |
5898b6e1 | 1749 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function); |
6b517475 JS |
1750 | if (rc != DWARF_CB_OK) |
1751 | q->query_done = true; | |
1752 | ||
1753 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 | |
1754 | && !q->has_statement_str) // PR 2608 | |
1755 | if (! q->filtered_functions.empty()) | |
1756 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
464379bb FCE |
1757 | // NB: we could skip the resolve_prologue_endings() call here for has_return case (PR13200), |
1758 | // but don't have to. We can resolve the prologue, just not actually use it in query_addr(). | |
6b517475 JS |
1759 | |
1760 | if (q->spec_type == function_file_and_line) | |
1761 | { | |
58b070fb | 1762 | // .statement(...:NN) often gets mixed up with .function(...:NN) |
2713ea24 | 1763 | if (q->has_function_str) |
b530b5b3 LB |
1764 | q->sess.print_warning (_("For probing a particular line, use a " |
1765 | ".statement() probe, not .function()"), | |
af2e341f | 1766 | q->base_probe->tok); |
58b070fb | 1767 | |
6b517475 JS |
1768 | // If we have a pattern string with target *line*, we |
1769 | // have to look at lines in all the matched srcfiles. | |
1770 | void (* callback) (const dwarf_line_t&, void*) = | |
1771 | q->has_label ? query_srcfile_label : query_srcfile_line; | |
1772 | for (set<string>::const_iterator i = q->filtered_srcfiles.begin(); | |
1773 | i != q->filtered_srcfiles.end(); ++i) | |
1774 | q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str, | |
1775 | q->line_type, callback, q->function, q); | |
1776 | } | |
1777 | else if (q->has_label) | |
1778 | { | |
1779 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1780 | i != q->filtered_functions.end(); ++i) | |
1781 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1782 | q, query_label); | |
1783 | ||
1784 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1785 | i != q->filtered_inlines.end(); ++i) | |
1786 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1787 | q, query_label); | |
39bcd429 | 1788 | } |
6b517475 JS |
1789 | else |
1790 | { | |
1791 | // Otherwise, simply probe all resolved functions. | |
1792 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1793 | i != q->filtered_functions.end(); ++i) | |
1794 | query_func_info (i->entrypc, *i, q); | |
1795 | ||
1796 | // And all inline instances (if we're not excluding inlines with ".call") | |
1797 | if (! q->has_call) | |
1798 | for (inline_instance_map_t::iterator i | |
1799 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) | |
1800 | query_inline_instance_info (*i, q); | |
1801 | } | |
39bcd429 | 1802 | return DWARF_CB_OK; |
bd2b1e68 | 1803 | } |
39bcd429 | 1804 | catch (const semantic_error& e) |
bd2b1e68 | 1805 | { |
39bcd429 FCE |
1806 | q->sess.print_error (e); |
1807 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1808 | } |
bd2b1e68 GH |
1809 | } |
1810 | ||
0ce64fb8 | 1811 | |
4df79aaf JS |
1812 | void |
1813 | dwarf_query::query_module_functions () | |
1814 | { | |
1815 | try | |
1816 | { | |
1817 | filtered_srcfiles.clear(); | |
1818 | filtered_functions.clear(); | |
1819 | filtered_inlines.clear(); | |
1820 | ||
1821 | // Collect all module functions so we know which CUs are interesting | |
1822 | int rc = dw.iterate_single_function(query_dwarf_func, this, function); | |
1823 | if (rc != DWARF_CB_OK) | |
1824 | { | |
1825 | query_done = true; | |
1826 | return; | |
1827 | } | |
1828 | ||
1829 | set<void*> used_cus; // by cu->addr | |
1830 | vector<Dwarf_Die> cus; | |
1831 | Dwarf_Die cu_mem; | |
1832 | ||
1833 | for (func_info_map_t::iterator i = filtered_functions.begin(); | |
1834 | i != filtered_functions.end(); ++i) | |
1835 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1836 | used_cus.insert(cu_mem.addr).second) | |
1837 | cus.push_back(cu_mem); | |
1838 | ||
1839 | for (inline_instance_map_t::iterator i = filtered_inlines.begin(); | |
1840 | i != filtered_inlines.end(); ++i) | |
1841 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1842 | used_cus.insert(cu_mem.addr).second) | |
1843 | cus.push_back(cu_mem); | |
1844 | ||
1845 | // Reset the dupes since we didn't actually collect them the first time | |
1846 | alias_dupes.clear(); | |
1847 | inline_dupes.clear(); | |
1848 | ||
1849 | // Run the query again on the individual CUs | |
1850 | for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i) | |
1851 | query_cu(&*i, this); | |
1852 | } | |
1853 | catch (const semantic_error& e) | |
1854 | { | |
1855 | sess.print_error (e); | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | ||
5f0a03a6 JK |
1860 | static void |
1861 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
1862 | { | |
1863 | // Validate the machine code in this elf file against the | |
1864 | // session machine. This is important, in case the wrong kind | |
1865 | // of debuginfo is being automagically processed by elfutils. | |
1866 | // While we can tell i686 apart from x86-64, unfortunately | |
1867 | // we can't help confusing i586 vs i686 (both EM_386). | |
1868 | ||
1869 | Dwarf_Addr bias; | |
1870 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
1871 | // because dwfl_module_getelf can force costly section relocations | |
1872 | // we don't really need, while either will do for this purpose. | |
1873 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
1874 | ?: dwfl_module_getelf (mod, &bias)); | |
1875 | ||
1876 | GElf_Ehdr ehdr_mem; | |
1877 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 1878 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
d8f31d0a | 1879 | assert(em); |
5f0a03a6 JK |
1880 | int elf_machine = em->e_machine; |
1881 | const char* debug_filename = ""; | |
1882 | const char* main_filename = ""; | |
1883 | (void) dwfl_module_info (mod, NULL, NULL, | |
1884 | NULL, NULL, NULL, | |
1885 | & main_filename, | |
1886 | & debug_filename); | |
1887 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
1888 | |
1889 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
1890 | string expect_machine2; | |
5f0a03a6 | 1891 | |
d27e6fd5 | 1892 | // NB: See also the 'uname -m' squashing done in main.cxx. |
5f0a03a6 JK |
1893 | switch (elf_machine) |
1894 | { | |
756c9462 FCE |
1895 | // x86 and ppc are bi-architecture; a 64-bit kernel |
1896 | // can normally run either 32-bit or 64-bit *userspace*. | |
1897 | case EM_386: | |
1898 | expect_machine = "i?86"; | |
1899 | if (! q->has_process) break; // 32-bit kernel/module | |
1900 | /* FALLSTHROUGH */ | |
1901 | case EM_X86_64: | |
1902 | expect_machine2 = "x86_64"; | |
1903 | break; | |
1904 | case EM_PPC: | |
756c9462 | 1905 | case EM_PPC64: |
5a1c472e | 1906 | expect_machine = "powerpc"; |
756c9462 | 1907 | break; |
3fe7d888 | 1908 | case EM_S390: expect_machine = "s390"; break; |
5f0a03a6 | 1909 | case EM_IA_64: expect_machine = "ia64"; break; |
d27e6fd5 | 1910 | case EM_ARM: expect_machine = "arm*"; break; |
5f0a03a6 JK |
1911 | // XXX: fill in some more of these |
1912 | default: expect_machine = "?"; break; | |
1913 | } | |
1914 | ||
1915 | if (! debug_filename) debug_filename = main_filename; | |
1916 | if (! debug_filename) debug_filename = name; | |
1917 | ||
756c9462 FCE |
1918 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
1919 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
1920 | { |
1921 | stringstream msg; | |
b530b5b3 LB |
1922 | msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'", |
1923 | expect_machine.c_str(), expect_machine2.c_str(), elf_machine, | |
1924 | sess_machine.c_str(), debug_filename); | |
5f0a03a6 JK |
1925 | throw semantic_error(msg.str ()); |
1926 | } | |
1927 | ||
b57082de | 1928 | if (q->sess.verbose>1) |
2a97f50b | 1929 | clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64 |
b530b5b3 LB |
1930 | " file %s ELF machine %s|%s (code %d)\n", |
1931 | q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end, | |
1932 | q->dw.module_bias, debug_filename, expect_machine.c_str(), | |
1933 | expect_machine2.c_str(), elf_machine); | |
5f0a03a6 | 1934 | } |
1d3a40b6 | 1935 | |
91af0778 FCE |
1936 | |
1937 | ||
1938 | static Dwarf_Addr | |
1939 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
1940 | { | |
1941 | int syments = dwfl_module_getsymtab(m); | |
1942 | assert(syments); | |
1943 | for (int i = 1; i < syments; ++i) | |
1944 | { | |
1945 | GElf_Sym sym; | |
1946 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
1947 | if (name != NULL && strcmp(name, wanted) == 0) | |
1948 | return sym.st_value; | |
1949 | } | |
1950 | ||
1951 | return 0; | |
1952 | } | |
1953 | ||
1954 | ||
1955 | ||
bd2b1e68 | 1956 | static int |
b8da0ad1 | 1957 | query_module (Dwfl_Module *mod, |
91af0778 | 1958 | void **, |
b8da0ad1 | 1959 | const char *name, |
6f4c1275 | 1960 | Dwarf_Addr addr, |
b8da0ad1 | 1961 | void *arg) |
bd2b1e68 | 1962 | { |
91af0778 | 1963 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 1964 | |
39bcd429 | 1965 | try |
e38d6504 | 1966 | { |
91af0778 FCE |
1967 | module_info* mi = q->sess.module_cache->cache[name]; |
1968 | if (mi == 0) | |
1969 | { | |
1970 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
1971 | ||
6f4c1275 FCE |
1972 | mi->mod = mod; |
1973 | mi->addr = addr; | |
91af0778 | 1974 | |
6f4c1275 FCE |
1975 | const char* debug_filename = ""; |
1976 | const char* main_filename = ""; | |
1977 | (void) dwfl_module_info (mod, NULL, NULL, | |
1978 | NULL, NULL, NULL, | |
1979 | & main_filename, | |
1980 | & debug_filename); | |
1981 | ||
1982 | if (q->sess.ignore_vmlinux && name == TOK_KERNEL) | |
91af0778 FCE |
1983 | { |
1984 | // report_kernel() in elfutils found vmlinux, but pretend it didn't. | |
1985 | // Given a non-null path, returning 1 means keep reporting modules. | |
1986 | mi->dwarf_status = info_absent; | |
1987 | } | |
6f4c1275 | 1988 | else if (debug_filename || main_filename) |
91af0778 | 1989 | { |
6f4c1275 FCE |
1990 | mi->elf_path = debug_filename ?: main_filename; |
1991 | } | |
1992 | else if (name == TOK_KERNEL) | |
1993 | { | |
1994 | mi->dwarf_status = info_absent; | |
91af0778 | 1995 | } |
91af0778 FCE |
1996 | } |
1997 | // OK, enough of that module_info caching business. | |
1998 | ||
5f0a03a6 | 1999 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 2000 | |
39bcd429 FCE |
2001 | // If we have enough information in the pattern to skip a module and |
2002 | // the module does not match that information, return early. | |
b8da0ad1 | 2003 | if (!q->dw.module_name_matches(q->module_val)) |
85007c04 | 2004 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
0cbbf9d1 FCE |
2005 | |
2006 | // Don't allow module("*kernel*") type expressions to match the | |
2007 | // elfutils module "kernel", which we refer to in the probe | |
2008 | // point syntax exclusively as "kernel.*". | |
2009 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
85007c04 | 2010 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
b5d77020 | 2011 | |
5f0a03a6 JK |
2012 | if (mod) |
2013 | validate_module_elf(mod, name, q); | |
2014 | else | |
91af0778 FCE |
2015 | assert(q->has_kernel); // and no vmlinux to examine |
2016 | ||
2017 | if (q->sess.verbose>2) | |
b530b5b3 | 2018 | cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str()); |
91af0778 FCE |
2019 | |
2020 | ||
2021 | // Collect a few kernel addresses. XXX: these belong better | |
2022 | // to the sess.module_info["kernel"] struct. | |
2023 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 2024 | { |
91af0778 FCE |
2025 | if (! q->sess.sym_kprobes_text_start) |
2026 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
2027 | if (! q->sess.sym_kprobes_text_end) | |
2028 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
2029 | if (! q->sess.sym_stext) | |
2030 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
2031 | } |
2032 | ||
47e226ed SC |
2033 | // We either have a wildcard or an unresolved library |
2034 | if (q->has_library && (contains_glob_chars (q->path) | |
2035 | || q->path.find('/') == string::npos)) | |
84c84ac4 SC |
2036 | // handle .library(GLOB) |
2037 | q->dw.iterate_over_libraries (&q->query_library_callback, q); | |
576eaefe SC |
2038 | // .plt is translated to .plt.statement(N). We only want to iterate for the |
2039 | // .plt case | |
2040 | else if (q->has_plt && ! q->has_statement) | |
2041 | q->dw.iterate_over_plt (q, &q->query_plt_callback); | |
84c84ac4 SC |
2042 | else |
2043 | // search the module for matches of the probe point. | |
2044 | q->handle_query_module(); | |
bb788f9f | 2045 | |
b8da0ad1 | 2046 | // If we know that there will be no more matches, abort early. |
85007c04 | 2047 | if (q->dw.module_name_final_match(q->module_val) || pending_interrupts) |
b8da0ad1 FCE |
2048 | return DWARF_CB_ABORT; |
2049 | else | |
2050 | return DWARF_CB_OK; | |
7a053d3b | 2051 | } |
39bcd429 | 2052 | catch (const semantic_error& e) |
bd2b1e68 | 2053 | { |
39bcd429 FCE |
2054 | q->sess.print_error (e); |
2055 | return DWARF_CB_ABORT; | |
bd2b1e68 | 2056 | } |
bd2b1e68 GH |
2057 | } |
2058 | ||
35d4ab18 | 2059 | |
84c84ac4 | 2060 | void |
5d5bd369 | 2061 | base_query::query_library_callback (void *q, const char *data) |
84c84ac4 SC |
2062 | { |
2063 | base_query *me = (base_query*)q; | |
5d5bd369 | 2064 | me->query_library (data); |
84c84ac4 SC |
2065 | } |
2066 | ||
2067 | ||
2068 | void | |
51d6bda3 SC |
2069 | query_one_library (const char *library, dwflpp & dw, |
2070 | const string user_lib, probe * base_probe, probe_point *base_loc, | |
2071 | vector<derived_probe *> & results) | |
84c84ac4 | 2072 | { |
47e226ed | 2073 | if (dw.function_name_matches_pattern(library, "*" + user_lib)) |
84c84ac4 | 2074 | { |
05fb3e0c WF |
2075 | string library_path = find_executable (library, "", dw.sess.sysenv, |
2076 | "LD_LIBRARY_PATH"); | |
84c84ac4 SC |
2077 | probe_point* specific_loc = new probe_point(*base_loc); |
2078 | specific_loc->optional = true; | |
2079 | vector<probe_point::component*> derived_comps; | |
2080 | ||
2081 | vector<probe_point::component*>::iterator it; | |
2082 | for (it = specific_loc->components.begin(); | |
2083 | it != specific_loc->components.end(); ++it) | |
2084 | if ((*it)->functor == TOK_LIBRARY) | |
2085 | derived_comps.push_back(new probe_point::component(TOK_LIBRARY, | |
2086 | new literal_string(library_path))); | |
2087 | else | |
2088 | derived_comps.push_back(*it); | |
2089 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2090 | derived_loc->components = derived_comps; | |
2091 | probe *new_base = base_probe->create_alias(derived_loc, specific_loc); | |
51d6bda3 SC |
2092 | derive_probes(dw.sess, new_base, results); |
2093 | if (dw.sess.verbose > 2) | |
84c84ac4 SC |
2094 | clog << _("module=") << library_path; |
2095 | } | |
2096 | } | |
2097 | ||
2098 | ||
51d6bda3 SC |
2099 | void |
2100 | dwarf_query::query_library (const char *library) | |
2101 | { | |
2102 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
2103 | } | |
2104 | ||
576eaefe SC |
2105 | struct plt_expanding_visitor: public var_expanding_visitor |
2106 | { | |
2107 | plt_expanding_visitor(const string & entry): | |
2108 | entry (entry) | |
2109 | { | |
2110 | } | |
2111 | const string & entry; | |
2112 | ||
2113 | void visit_target_symbol (target_symbol* e); | |
2114 | }; | |
2115 | ||
2116 | ||
2117 | void | |
2118 | base_query::query_plt_callback (void *q, const char *entry, size_t address) | |
2119 | { | |
2120 | base_query *me = (base_query*)q; | |
2121 | if (me->dw.function_name_matches_pattern (entry, me->plt_val)) | |
2122 | me->query_plt (entry, address); | |
2123 | } | |
2124 | ||
2125 | ||
2126 | void | |
2127 | query_one_plt (const char *entry, long addr, dwflpp & dw, | |
2128 | probe * base_probe, probe_point *base_loc, | |
2129 | vector<derived_probe *> & results) | |
2130 | { | |
2131 | probe_point* specific_loc = new probe_point(*base_loc); | |
2132 | specific_loc->optional = true; | |
2133 | vector<probe_point::component*> derived_comps; | |
2134 | ||
2135 | if (dw.sess.verbose > 2) | |
2136 | clog << _F("plt entry=%s\n", entry); | |
2137 | ||
2138 | // query_module_symtab requires .plt to recognize that it can set the probe at | |
2139 | // a plt entry so we convert process.plt to process.plt.statement | |
2140 | vector<probe_point::component*>::iterator it; | |
2141 | for (it = specific_loc->components.begin(); | |
2142 | it != specific_loc->components.end(); ++it) | |
2143 | if ((*it)->functor == TOK_PLT) | |
3d372d6b SC |
2144 | { |
2145 | derived_comps.push_back(*it); | |
2146 | derived_comps.push_back(new probe_point::component(TOK_STATEMENT, | |
2147 | new literal_number(addr))); | |
2148 | } | |
576eaefe SC |
2149 | else |
2150 | derived_comps.push_back(*it); | |
2151 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2152 | derived_loc->components = derived_comps; | |
2153 | probe *new_base = base_probe->create_alias(derived_loc, specific_loc); | |
2154 | string e = string(entry); | |
2155 | plt_expanding_visitor pltv (e); | |
2156 | pltv.replace (new_base->body); | |
2157 | derive_probes(dw.sess, new_base, results); | |
2158 | } | |
2159 | ||
2160 | ||
2161 | void | |
2162 | dwarf_query::query_plt (const char *entry, size_t address) | |
2163 | { | |
2164 | query_one_plt (entry, address, dw, base_probe, base_loc, results); | |
2165 | } | |
51d6bda3 | 2166 | |
435f53a7 FCE |
2167 | // This would more naturally fit into elaborate.cxx:semantic_pass_symbols, |
2168 | // but the needed declaration for module_cache is not available there. | |
2169 | // Nor for that matter in session.cxx. Only in this CU is that field ever | |
2170 | // set (in query_module() above), so we clean it up here too. | |
2171 | static void | |
2172 | delete_session_module_cache (systemtap_session& s) | |
2173 | { | |
2174 | if (s.module_cache) { | |
2175 | if (s.verbose > 3) | |
b530b5b3 | 2176 | clog << _("deleting module_cache") << endl; |
435f53a7 FCE |
2177 | delete s.module_cache; |
2178 | s.module_cache = 0; | |
2179 | } | |
2180 | } | |
2181 | ||
2182 | ||
de688825 | 2183 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 2184 | { |
77de5e9e | 2185 | dwarf_query & q; |
bcc12710 | 2186 | Dwarf_Die *scope_die; |
77de5e9e | 2187 | Dwarf_Addr addr; |
8c819921 | 2188 | block *add_block; |
2260f4e3 | 2189 | block *add_call_probe; // synthesized from .return probes with saved $vars |
1630966a JS |
2190 | // NB: tids are not always collected in add_block & add_call_probe, because |
2191 | // gen_kretprobe_saved_return doesn't need them. Thus we need these extra | |
2192 | // *_tid bools for gen_mapped_saved_return to tell what's there. | |
8cc799a5 | 2193 | bool add_block_tid, add_call_probe_tid; |
af234c40 JS |
2194 | unsigned saved_longs, saved_strings; // data saved within kretprobes |
2195 | map<std::string, expression *> return_ts_map; | |
729455a7 | 2196 | vector<Dwarf_Die> scopes; |
b95e2b79 | 2197 | bool visited; |
77de5e9e | 2198 | |
de688825 | 2199 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
af234c40 | 2200 | q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL), |
8cc799a5 | 2201 | add_block_tid(false), add_call_probe_tid(false), |
af234c40 | 2202 | saved_longs(0), saved_strings(0), visited(false) {} |
277c21bc | 2203 | expression* gen_mapped_saved_return(expression* e, const string& name); |
140be17a | 2204 | expression* gen_kretprobe_saved_return(expression* e); |
a7999c82 JS |
2205 | void visit_target_symbol_saved_return (target_symbol* e); |
2206 | void visit_target_symbol_context (target_symbol* e); | |
d7f3e0c5 | 2207 | void visit_target_symbol (target_symbol* e); |
c24447be | 2208 | void visit_cast_op (cast_op* e); |
8cc799a5 | 2209 | void visit_entry_op (entry_op* e); |
729455a7 | 2210 | private: |
bfa7e523 | 2211 | vector<Dwarf_Die>& getcuscope(target_symbol *e); |
729455a7 | 2212 | vector<Dwarf_Die>& getscopes(target_symbol *e); |
77de5e9e GH |
2213 | }; |
2214 | ||
2215 | ||
de688825 | 2216 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 2217 | |
a50de939 | 2218 | |
74fe61bc | 2219 | var_expanding_visitor::var_expanding_visitor (): op() |
a50de939 DS |
2220 | { |
2221 | // FIXME: for the time being, by default we only support plain '$foo | |
2222 | // = bar', not '+=' or any other op= variant. This is fixable, but a | |
2223 | // bit ugly. | |
2224 | // | |
2225 | // If derived classes desire to add additional operator support, add | |
2226 | // new operators to this list in the derived class constructor. | |
2227 | valid_ops.insert ("="); | |
2228 | } | |
2229 | ||
2230 | ||
87214add JS |
2231 | bool |
2232 | var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop, | |
2233 | expression*& lvalue, expression*& rvalue) | |
77de5e9e | 2234 | { |
e57b735a GH |
2235 | // Our job would normally be to require() the left and right sides |
2236 | // into a new assignment. What we're doing is slightly trickier: | |
2237 | // we're pushing a functioncall** onto a stack, and if our left | |
2238 | // child sets the functioncall* for that value, we're going to | |
2239 | // assume our left child was a target symbol -- transformed into a | |
2240 | // set_target_foo(value) call, and it wants to take our right child | |
2241 | // as the argument "value". | |
2242 | // | |
2243 | // This is why some people claim that languages with | |
2244 | // constructor-decomposing case expressions have a leg up on | |
2245 | // visitors. | |
2246 | ||
2247 | functioncall *fcall = NULL; | |
d9b516ca | 2248 | |
a50de939 | 2249 | // Let visit_target_symbol know what operator it should handle. |
87214add JS |
2250 | const string* old_op = op; |
2251 | op = &eop; | |
a50de939 | 2252 | |
e57b735a | 2253 | target_symbol_setter_functioncalls.push (&fcall); |
87214add | 2254 | replace (lvalue); |
e57b735a | 2255 | target_symbol_setter_functioncalls.pop (); |
87214add JS |
2256 | replace (rvalue); |
2257 | ||
2258 | op = old_op; | |
e57b735a GH |
2259 | |
2260 | if (fcall != NULL) | |
77de5e9e | 2261 | { |
e57b735a GH |
2262 | // Our left child is informing us that it was a target variable |
2263 | // and it has been replaced with a set_target_foo() function | |
2264 | // call; we are going to provide that function call -- with the | |
2265 | // right child spliced in as sole argument -- in place of | |
de688825 | 2266 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a | 2267 | |
87214add | 2268 | if (valid_ops.find (eop) == valid_ops.end ()) |
a50de939 DS |
2269 | { |
2270 | // Build up a list of supported operators. | |
2271 | string ops; | |
2272 | std::set<string>::iterator i; | |
b530b5b3 | 2273 | int valid_ops_size = 0; |
a50de939 | 2274 | for (i = valid_ops.begin(); i != valid_ops.end(); i++) |
b530b5b3 | 2275 | { |
a50de939 | 2276 | ops += " " + *i + ","; |
b530b5b3 LB |
2277 | valid_ops_size++; |
2278 | } | |
a50de939 DS |
2279 | ops.resize(ops.size() - 1); // chop off the last ',' |
2280 | ||
2281 | // Throw the error. | |
1e41115c LB |
2282 | throw semantic_error (_F(ngettext("Only the following assign operator is implemented on target variables: %s", |
2283 | "Only the following assign operators are implemented on target variables: %s", | |
b530b5b3 LB |
2284 | valid_ops_size), ops.c_str()), tok); |
2285 | ||
a50de939 | 2286 | } |
e57b735a | 2287 | |
87214add JS |
2288 | assert (lvalue == fcall); |
2289 | if (rvalue) | |
2290 | fcall->args.push_back (rvalue); | |
4ed05b15 | 2291 | provide (fcall); |
87214add | 2292 | return true; |
77de5e9e | 2293 | } |
e57b735a | 2294 | else |
87214add JS |
2295 | return false; |
2296 | } | |
2297 | ||
2298 | ||
2299 | void | |
2300 | var_expanding_visitor::visit_assignment (assignment* e) | |
2301 | { | |
2302 | if (!rewrite_lvalue (e->tok, e->op, e->left, e->right)) | |
2303 | provide (e); | |
2304 | } | |
2305 | ||
2306 | ||
2307 | void | |
2308 | var_expanding_visitor::visit_pre_crement (pre_crement* e) | |
2309 | { | |
2310 | expression *dummy = NULL; | |
2311 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2312 | provide (e); | |
2313 | } | |
2314 | ||
2315 | ||
2316 | void | |
2317 | var_expanding_visitor::visit_post_crement (post_crement* e) | |
2318 | { | |
2319 | expression *dummy = NULL; | |
2320 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2321 | provide (e); | |
2322 | } | |
2323 | ||
2324 | ||
2325 | void | |
2326 | var_expanding_visitor::visit_delete_statement (delete_statement* s) | |
2327 | { | |
2328 | string fakeop = "delete"; | |
2329 | expression *dummy = NULL; | |
2330 | if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy)) | |
2331 | provide (s); | |
e57b735a | 2332 | } |
d9b516ca | 2333 | |
d7f3e0c5 | 2334 | |
30263a73 FCE |
2335 | void |
2336 | var_expanding_visitor::visit_defined_op (defined_op* e) | |
2337 | { | |
2338 | bool resolved = true; | |
2339 | ||
2340 | defined_ops.push (e); | |
2341 | try { | |
2342 | // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is | |
2343 | // defined with a target_symbol* operand, a subsidiary call may attempt to | |
2344 | // rewrite it to a general expression* instead, and require<> happily | |
2345 | // casts to/from void*, causing possible memory corruption. We use | |
2346 | // expression* here, being the general case of rewritten $variable. | |
2347 | expression *foo1 = e->operand; | |
2348 | foo1 = require (foo1); | |
2349 | ||
c69a87e0 | 2350 | // NB: Formerly, we had some curious cases to consider here, depending on what |
30263a73 | 2351 | // various visit_target_symbol() implementations do for successful or |
c69a87e0 FCE |
2352 | // erroneous resolutions. Some would signal a visit_target_symbol failure |
2353 | // with an exception, with a set flag within the target_symbol, or nothing | |
2354 | // at all. | |
30263a73 | 2355 | // |
c69a87e0 FCE |
2356 | // Now, failures always have to be signalled with a |
2357 | // saved_conversion_error being chained to the target_symbol. | |
2358 | // Successes have to result in an attempted rewrite of the | |
850bfddd | 2359 | // target_symbol (via provide()). |
780f11ff | 2360 | // |
c69a87e0 FCE |
2361 | // Edna Mode: "no capes". fche: "no exceptions". |
2362 | ||
30263a73 FCE |
2363 | // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error |
2364 | // | |
2365 | // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error | |
2366 | // | |
2367 | // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic | |
2368 | // dwarf probe to take care of it. | |
2369 | // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor | |
2370 | // to pass through @defined() to the synthetic dwarf probe. | |
780f11ff | 2371 | // |
30263a73 FCE |
2372 | // utrace: success: rewrites to function; failure: semantic_error |
2373 | // | |
850bfddd | 2374 | // procfs: success: rewrites to function; failure: semantic_error |
30263a73 FCE |
2375 | |
2376 | target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1); | |
c69a87e0 | 2377 | if (foo2 && foo2->saved_conversion_error) // failing |
30263a73 | 2378 | resolved = false; |
a45664f4 | 2379 | else if (foo2) // unresolved but not marked failing |
b7aedf26 | 2380 | { |
780f11ff JS |
2381 | // There are some visitors that won't touch certain target_symbols, |
2382 | // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should | |
2383 | // leave it for now so some other visitor can have a chance. | |
b7aedf26 JS |
2384 | e->operand = foo2; |
2385 | provide (e); | |
2386 | return; | |
2387 | } | |
30263a73 FCE |
2388 | else // resolved, rewritten to some other expression type |
2389 | resolved = true; | |
780f11ff | 2390 | } catch (const semantic_error& e) { |
c69a87e0 | 2391 | assert (0); // should not happen |
30263a73 FCE |
2392 | } |
2393 | defined_ops.pop (); | |
2394 | ||
2395 | literal_number* ln = new literal_number (resolved ? 1 : 0); | |
2396 | ln->tok = e->tok; | |
2397 | provide (ln); | |
2398 | } | |
2399 | ||
2400 | ||
5f36109e JS |
2401 | struct dwarf_pretty_print |
2402 | { | |
2403 | dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc, | |
2404 | const string& local, bool userspace_p, | |
2405 | const target_symbol& e): | |
d19a9a82 JS |
2406 | dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL), |
2407 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2408 | { |
2409 | init_ts (e); | |
2410 | dw.type_die_for_local (scopes, pc, local, ts, &base_type); | |
2411 | } | |
2412 | ||
2413 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc, | |
2414 | bool userspace_p, const target_symbol& e): | |
d19a9a82 JS |
2415 | dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL), |
2416 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2417 | { |
2418 | init_ts (e); | |
2419 | dw.type_die_for_return (&scopes[0], pc, ts, &base_type); | |
2420 | } | |
2421 | ||
2422 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer, | |
d19a9a82 | 2423 | bool deref_p, bool userspace_p, const target_symbol& e): |
5f36109e | 2424 | dw(dw), pc(0), pointer(pointer), pointer_type(*type_die), |
d19a9a82 | 2425 | userspace_p(userspace_p), deref_p(deref_p) |
5f36109e JS |
2426 | { |
2427 | init_ts (e); | |
2428 | dw.type_die_for_pointer (type_die, ts, &base_type); | |
2429 | } | |
2430 | ||
2431 | functioncall* expand (); | |
ce83ff57 | 2432 | ~dwarf_pretty_print () { delete ts; } |
5f36109e JS |
2433 | |
2434 | private: | |
2435 | dwflpp& dw; | |
2436 | target_symbol* ts; | |
7d11d8c9 | 2437 | bool print_full; |
5f36109e JS |
2438 | Dwarf_Die base_type; |
2439 | ||
2440 | string local; | |
2441 | vector<Dwarf_Die> scopes; | |
2442 | Dwarf_Addr pc; | |
2443 | ||
2444 | expression* pointer; | |
2445 | Dwarf_Die pointer_type; | |
2446 | ||
d19a9a82 | 2447 | const bool userspace_p, deref_p; |
5f36109e JS |
2448 | |
2449 | void recurse (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2450 | print_format* pf, bool top=false); |
600551ca JS |
2451 | void recurse_bitfield (Dwarf_Die* type, target_symbol* e, |
2452 | print_format* pf); | |
5f36109e | 2453 | void recurse_base (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2454 | print_format* pf); |
5f36109e | 2455 | void recurse_array (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2456 | print_format* pf, bool top); |
5f36109e | 2457 | void recurse_pointer (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2458 | print_format* pf, bool top); |
5f36109e | 2459 | void recurse_struct (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2460 | print_format* pf, bool top); |
5f36109e | 2461 | void recurse_struct_members (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2462 | print_format* pf, int& count); |
bbee5bb8 | 2463 | bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf); |
5f36109e JS |
2464 | |
2465 | void init_ts (const target_symbol& e); | |
2466 | expression* deref (target_symbol* e); | |
c55ea10d | 2467 | bool push_deref (print_format* pf, const string& fmt, target_symbol* e); |
5f36109e JS |
2468 | }; |
2469 | ||
2470 | ||
2471 | void | |
2472 | dwarf_pretty_print::init_ts (const target_symbol& e) | |
2473 | { | |
2474 | // Work with a new target_symbol so we can modify arguments | |
2475 | ts = new target_symbol (e); | |
2476 | ||
2477 | if (ts->addressof) | |
b530b5b3 | 2478 | throw semantic_error(_("cannot take address of pretty-printed variable"), ts->tok); |
5f36109e JS |
2479 | |
2480 | if (ts->components.empty() || | |
2481 | ts->components.back().type != target_symbol::comp_pretty_print) | |
b530b5b3 | 2482 | throw semantic_error(_("invalid target_symbol for pretty-print"), ts->tok); |
7d11d8c9 | 2483 | print_full = ts->components.back().member.length() > 1; |
5f36109e JS |
2484 | ts->components.pop_back(); |
2485 | } | |
2486 | ||
2487 | ||
2488 | functioncall* | |
2489 | dwarf_pretty_print::expand () | |
2490 | { | |
2491 | static unsigned tick = 0; | |
2492 | ||
2493 | // function pretty_print_X([pointer], [arg1, arg2, ...]) { | |
7d11d8c9 JS |
2494 | // try { |
2495 | // return sprintf("{.foo=...}", (ts)->foo, ...) | |
2496 | // } catch { | |
2497 | // return "ERROR" | |
2498 | // } | |
5f36109e JS |
2499 | // } |
2500 | ||
2501 | // Create the function decl and call. | |
2502 | ||
2503 | functiondecl *fdecl = new functiondecl; | |
2504 | fdecl->tok = ts->tok; | |
2505 | fdecl->synthetic = true; | |
2506 | fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++); | |
2507 | fdecl->type = pe_string; | |
2508 | ||
2509 | functioncall* fcall = new functioncall; | |
2510 | fcall->tok = ts->tok; | |
2511 | fcall->function = fdecl->name; | |
140be17a | 2512 | fcall->type = pe_string; |
5f36109e JS |
2513 | |
2514 | // If there's a <pointer>, replace it with a new var and make that | |
2515 | // the first function argument. | |
2516 | if (pointer) | |
2517 | { | |
2518 | vardecl *v = new vardecl; | |
2519 | v->type = pe_long; | |
2520 | v->name = "pointer"; | |
2521 | v->tok = ts->tok; | |
2522 | fdecl->formal_args.push_back (v); | |
2523 | fcall->args.push_back (pointer); | |
2524 | ||
2525 | symbol* sym = new symbol; | |
2526 | sym->tok = ts->tok; | |
2527 | sym->name = v->name; | |
5f36109e JS |
2528 | pointer = sym; |
2529 | } | |
2530 | ||
2531 | // For each expression argument, replace it with a function argument. | |
2532 | for (unsigned i = 0; i < ts->components.size(); ++i) | |
2533 | if (ts->components[i].type == target_symbol::comp_expression_array_index) | |
2534 | { | |
2535 | vardecl *v = new vardecl; | |
2536 | v->type = pe_long; | |
2537 | v->name = "index" + lex_cast(i); | |
2538 | v->tok = ts->tok; | |
2539 | fdecl->formal_args.push_back (v); | |
2540 | fcall->args.push_back (ts->components[i].expr_index); | |
2541 | ||
2542 | symbol* sym = new symbol; | |
2543 | sym->tok = ts->tok; | |
2544 | sym->name = v->name; | |
5f36109e JS |
2545 | ts->components[i].expr_index = sym; |
2546 | } | |
2547 | ||
2548 | // Create the return sprintf. | |
2549 | token* pf_tok = new token(*ts->tok); | |
2550 | pf_tok->content = "sprintf"; | |
2551 | print_format* pf = print_format::create(pf_tok); | |
2552 | return_statement* rs = new return_statement; | |
2553 | rs->tok = ts->tok; | |
2554 | rs->value = pf; | |
5f36109e JS |
2555 | |
2556 | // Recurse into the actual values. | |
7d11d8c9 | 2557 | recurse (&base_type, ts, pf, true); |
5f36109e JS |
2558 | pf->components = print_format::string_to_components(pf->raw_components); |
2559 | ||
7d11d8c9 JS |
2560 | // Create the try-catch net |
2561 | try_block* tb = new try_block; | |
2562 | tb->tok = ts->tok; | |
2563 | tb->try_block = rs; | |
2564 | tb->catch_error_var = 0; | |
2565 | return_statement* rs2 = new return_statement; | |
2566 | rs2->tok = ts->tok; | |
2567 | rs2->value = new literal_string ("ERROR"); | |
2568 | rs2->value->tok = ts->tok; | |
2569 | tb->catch_block = rs2; | |
2570 | fdecl->body = tb; | |
2571 | ||
f8809d54 | 2572 | fdecl->join (dw.sess); |
5f36109e JS |
2573 | return fcall; |
2574 | } | |
2575 | ||
2576 | ||
2577 | void | |
2578 | dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e, | |
7d11d8c9 | 2579 | print_format* pf, bool top) |
5f36109e JS |
2580 | { |
2581 | Dwarf_Die type; | |
2582 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2583 | ||
2584 | switch (dwarf_tag(&type)) | |
2585 | { | |
2586 | default: | |
2587 | // XXX need a warning? | |
2588 | // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type)) | |
2589 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2590 | pf->raw_components.append("?"); | |
2591 | break; | |
2592 | ||
2593 | case DW_TAG_enumeration_type: | |
2594 | case DW_TAG_base_type: | |
7d11d8c9 | 2595 | recurse_base (&type, e, pf); |
5f36109e JS |
2596 | break; |
2597 | ||
2598 | case DW_TAG_array_type: | |
7d11d8c9 | 2599 | recurse_array (&type, e, pf, top); |
5f36109e JS |
2600 | break; |
2601 | ||
2602 | case DW_TAG_pointer_type: | |
2603 | case DW_TAG_reference_type: | |
2604 | case DW_TAG_rvalue_reference_type: | |
7d11d8c9 | 2605 | recurse_pointer (&type, e, pf, top); |
5f36109e JS |
2606 | break; |
2607 | ||
2608 | case DW_TAG_subroutine_type: | |
c55ea10d | 2609 | push_deref (pf, "<function>:%p", e); |
5f36109e JS |
2610 | break; |
2611 | ||
2612 | case DW_TAG_union_type: | |
5f36109e JS |
2613 | case DW_TAG_structure_type: |
2614 | case DW_TAG_class_type: | |
7d11d8c9 | 2615 | recurse_struct (&type, e, pf, top); |
5f36109e JS |
2616 | break; |
2617 | } | |
2618 | } | |
2619 | ||
2620 | ||
600551ca JS |
2621 | // Bit fields are handled as a special-case combination of recurse() and |
2622 | // recurse_base(), only called from recurse_struct_members(). The main | |
2623 | // difference is that the value is always printed numerically, even if the | |
2624 | // underlying type is a char. | |
2625 | void | |
2626 | dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e, | |
2627 | print_format* pf) | |
2628 | { | |
2629 | Dwarf_Die type; | |
2630 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2631 | ||
2632 | int tag = dwarf_tag(&type); | |
2633 | if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type) | |
2634 | { | |
2635 | // XXX need a warning? | |
2636 | // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag) | |
2637 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2638 | pf->raw_components.append("?"); | |
2639 | return; | |
2640 | } | |
2641 | ||
2642 | Dwarf_Attribute attr; | |
2643 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2644 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr), | |
2645 | &encoding); | |
2646 | switch (encoding) | |
2647 | { | |
2648 | case DW_ATE_float: | |
2649 | case DW_ATE_complex_float: | |
2650 | // XXX need a warning? | |
2651 | // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding) | |
2652 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2653 | pf->raw_components.append("?"); | |
2654 | break; | |
2655 | ||
2656 | case DW_ATE_unsigned: | |
2657 | case DW_ATE_unsigned_char: | |
2658 | push_deref (pf, "%u", e); | |
2659 | break; | |
2660 | ||
2661 | case DW_ATE_signed: | |
2662 | case DW_ATE_signed_char: | |
2663 | default: | |
2664 | push_deref (pf, "%i", e); | |
2665 | break; | |
2666 | } | |
2667 | } | |
2668 | ||
2669 | ||
5f36109e JS |
2670 | void |
2671 | dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2672 | print_format* pf) |
5f36109e JS |
2673 | { |
2674 | Dwarf_Attribute attr; | |
2675 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2676 | dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr), | |
2677 | &encoding); | |
5f36109e JS |
2678 | switch (encoding) |
2679 | { | |
2680 | case DW_ATE_float: | |
2681 | case DW_ATE_complex_float: | |
2682 | // XXX need a warning? | |
2683 | // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding) | |
2684 | // + ") for " + dwarf_type_name(type), e->tok); | |
2685 | pf->raw_components.append("?"); | |
5f36109e JS |
2686 | break; |
2687 | ||
2688 | case DW_ATE_signed_char: | |
2689 | case DW_ATE_unsigned_char: | |
941101c1 JS |
2690 | // Use escapes to make sure that non-printable characters |
2691 | // don't interrupt our stream (especially '\0' values). | |
2692 | push_deref (pf, "'%#c'", e); | |
5f36109e JS |
2693 | break; |
2694 | ||
2695 | case DW_ATE_unsigned: | |
c55ea10d | 2696 | push_deref (pf, "%u", e); |
5f36109e JS |
2697 | break; |
2698 | ||
600551ca | 2699 | case DW_ATE_signed: |
5f36109e | 2700 | default: |
c55ea10d | 2701 | push_deref (pf, "%i", e); |
5f36109e JS |
2702 | break; |
2703 | } | |
5f36109e JS |
2704 | } |
2705 | ||
2706 | ||
2707 | void | |
2708 | dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2709 | print_format* pf, bool top) |
5f36109e | 2710 | { |
7d11d8c9 JS |
2711 | if (!top && !print_full) |
2712 | { | |
2713 | pf->raw_components.append("[...]"); | |
2714 | return; | |
2715 | } | |
2716 | ||
5f36109e JS |
2717 | Dwarf_Die childtype; |
2718 | dwarf_attr_die (type, DW_AT_type, &childtype); | |
bbee5bb8 JS |
2719 | |
2720 | if (print_chars (&childtype, e, pf)) | |
2721 | return; | |
2722 | ||
5f36109e JS |
2723 | pf->raw_components.append("["); |
2724 | ||
2725 | // We print the array up to the first 5 elements. | |
2726 | // XXX how can we determine the array size? | |
2727 | // ... for now, just print the first element | |
64cddf39 | 2728 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
5f36109e | 2729 | unsigned i, size = 1; |
64cddf39 | 2730 | for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i) |
5f36109e JS |
2731 | { |
2732 | if (i > 0) | |
2733 | pf->raw_components.append(", "); | |
2734 | target_symbol* e2 = new target_symbol(*e); | |
2735 | e2->components.push_back (target_symbol::component(e->tok, i)); | |
7d11d8c9 | 2736 | recurse (&childtype, e2, pf); |
5f36109e JS |
2737 | } |
2738 | if (i < size || 1/*XXX until real size is known */) | |
2739 | pf->raw_components.append(", ..."); | |
2740 | pf->raw_components.append("]"); | |
2741 | } | |
2742 | ||
2743 | ||
2744 | void | |
2745 | dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2746 | print_format* pf, bool top) |
5f36109e | 2747 | { |
7d11d8c9 | 2748 | // We chase to top-level pointers, but leave the rest alone |
d19a9a82 | 2749 | bool void_p = true; |
7d11d8c9 | 2750 | Dwarf_Die pointee; |
bbee5bb8 | 2751 | if (dwarf_attr_die (type, DW_AT_type, &pointee)) |
d19a9a82 JS |
2752 | { |
2753 | try | |
2754 | { | |
2755 | dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e); | |
2756 | void_p = false; | |
2757 | } | |
2758 | catch (const semantic_error&) {} | |
2759 | } | |
2760 | ||
2761 | if (!void_p) | |
5f36109e | 2762 | { |
bbee5bb8 JS |
2763 | if (print_chars (&pointee, e, pf)) |
2764 | return; | |
2765 | ||
2766 | if (top) | |
2767 | { | |
2768 | recurse (&pointee, e, pf, top); | |
2769 | return; | |
2770 | } | |
5f36109e | 2771 | } |
bbee5bb8 | 2772 | |
c55ea10d | 2773 | push_deref (pf, "%p", e); |
5f36109e JS |
2774 | } |
2775 | ||
2776 | ||
2777 | void | |
2778 | dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2779 | print_format* pf, bool top) |
5f36109e | 2780 | { |
bdec0e18 JS |
2781 | if (dwarf_hasattr(type, DW_AT_declaration)) |
2782 | { | |
a44a7cb5 | 2783 | Dwarf_Die *resolved = dw.declaration_resolve(type); |
bdec0e18 JS |
2784 | if (!resolved) |
2785 | { | |
2786 | // could be an error, but for now just stub it | |
2787 | // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok); | |
2788 | pf->raw_components.append("{...}"); | |
2789 | return; | |
2790 | } | |
2791 | type = resolved; | |
2792 | } | |
2793 | ||
5f36109e JS |
2794 | int count = 0; |
2795 | pf->raw_components.append("{"); | |
7d11d8c9 JS |
2796 | if (top || print_full) |
2797 | recurse_struct_members (type, e, pf, count); | |
2798 | else | |
2799 | pf->raw_components.append("..."); | |
5f36109e JS |
2800 | pf->raw_components.append("}"); |
2801 | } | |
2802 | ||
2803 | ||
2804 | void | |
2805 | dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2806 | print_format* pf, int& count) |
5f36109e | 2807 | { |
a80f28d8 JS |
2808 | /* With inheritance, a subclass may mask member names of parent classes, so |
2809 | * our search among the inheritance tree must be breadth-first rather than | |
2810 | * depth-first (recursive). The type die is still our starting point. When | |
2811 | * we encounter a masked name, just skip it. */ | |
2812 | set<string> dupes; | |
2813 | deque<Dwarf_Die> inheritees(1, *type); | |
2814 | for (; !inheritees.empty(); inheritees.pop_front()) | |
2815 | { | |
2816 | Dwarf_Die child, childtype; | |
2817 | if (dwarf_child (&inheritees.front(), &child) == 0) | |
2818 | do | |
2819 | { | |
2820 | target_symbol* e2 = e; | |
5f36109e | 2821 | |
a80f28d8 JS |
2822 | // skip static members |
2823 | if (dwarf_hasattr(&child, DW_AT_declaration)) | |
2824 | continue; | |
5f36109e | 2825 | |
a80f28d8 | 2826 | int tag = dwarf_tag (&child); |
5f36109e | 2827 | |
a80f28d8 JS |
2828 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) |
2829 | continue; | |
5f36109e | 2830 | |
a80f28d8 | 2831 | dwarf_attr_die (&child, DW_AT_type, &childtype); |
5f36109e | 2832 | |
a80f28d8 JS |
2833 | if (tag == DW_TAG_inheritance) |
2834 | { | |
2835 | inheritees.push_back(childtype); | |
2836 | continue; | |
2837 | } | |
5f36109e | 2838 | |
a80f28d8 JS |
2839 | int childtag = dwarf_tag (&childtype); |
2840 | const char *member = dwarf_diename (&child); | |
3a147004 | 2841 | |
a80f28d8 JS |
2842 | // "_vptr.foo" members are C++ virtual function tables, |
2843 | // which (generally?) aren't interesting for users. | |
2844 | if (member && startswith(member, "_vptr.")) | |
2845 | continue; | |
3a147004 | 2846 | |
a80f28d8 JS |
2847 | // skip inheritance-masked duplicates |
2848 | if (member && !dupes.insert(member).second) | |
2849 | continue; | |
64cddf39 | 2850 | |
a80f28d8 JS |
2851 | if (++count > 1) |
2852 | pf->raw_components.append(", "); | |
64cddf39 | 2853 | |
a80f28d8 JS |
2854 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
2855 | if (pf->args.size() >= 32) | |
2856 | { | |
2857 | pf->raw_components.append("..."); | |
2858 | break; | |
2859 | } | |
2860 | ||
2861 | if (member) | |
2862 | { | |
2863 | pf->raw_components.append("."); | |
2864 | pf->raw_components.append(member); | |
5f36109e | 2865 | |
a80f28d8 JS |
2866 | e2 = new target_symbol(*e); |
2867 | e2->components.push_back (target_symbol::component(e->tok, member)); | |
2868 | } | |
2869 | else if (childtag == DW_TAG_union_type) | |
2870 | pf->raw_components.append("<union>"); | |
2871 | else if (childtag == DW_TAG_structure_type) | |
2872 | pf->raw_components.append("<class>"); | |
2873 | else if (childtag == DW_TAG_class_type) | |
2874 | pf->raw_components.append("<struct>"); | |
2875 | pf->raw_components.append("="); | |
600551ca JS |
2876 | |
2877 | if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset)) | |
2878 | recurse_bitfield (&childtype, e2, pf); | |
2879 | else | |
2880 | recurse (&childtype, e2, pf); | |
5f36109e | 2881 | } |
a80f28d8 JS |
2882 | while (dwarf_siblingof (&child, &child) == 0); |
2883 | } | |
5f36109e JS |
2884 | } |
2885 | ||
2886 | ||
bbee5bb8 JS |
2887 | bool |
2888 | dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e, | |
2889 | print_format* pf) | |
2890 | { | |
2891 | Dwarf_Die type; | |
2892 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2893 | const char *name = dwarf_diename (&type); | |
2894 | if (name && (name == string("char") || name == string("unsigned char"))) | |
2895 | { | |
c55ea10d JS |
2896 | if (push_deref (pf, "\"%s\"", e)) |
2897 | { | |
2898 | // steal the last arg for a string access | |
2899 | assert (!pf->args.empty()); | |
2900 | functioncall* fcall = new functioncall; | |
2901 | fcall->tok = e->tok; | |
2902 | fcall->function = userspace_p ? "user_string2" : "kernel_string2"; | |
2903 | fcall->args.push_back (pf->args.back()); | |
2904 | expression *err_msg = new literal_string ("<unknown>"); | |
2905 | err_msg->tok = e->tok; | |
2906 | fcall->args.push_back (err_msg); | |
2907 | pf->args.back() = fcall; | |
2908 | } | |
bbee5bb8 JS |
2909 | return true; |
2910 | } | |
2911 | return false; | |
2912 | } | |
2913 | ||
a5ce5211 MW |
2914 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
2915 | static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n") | |
f1e8e7e0 MW |
2916 | + "#define fetch_register k_fetch_register\n" |
2917 | + "#define store_register k_store_register\n" | |
2918 | + "#define deref kderef\n" | |
2919 | + "#define store_deref store_kderef\n"; | |
a5ce5211 MW |
2920 | |
2921 | static const string EMBEDDED_FETCH_DEREF_USER = string("\n") | |
f1e8e7e0 MW |
2922 | + "#define fetch_register u_fetch_register\n" |
2923 | + "#define store_register u_store_register\n" | |
2924 | + "#define deref uderef\n" | |
2925 | + "#define store_deref store_uderef\n"; | |
a5ce5211 MW |
2926 | |
2927 | #define EMBEDDED_FETCH_DEREF(U) \ | |
f1e8e7e0 | 2928 | (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL) |
a5ce5211 MW |
2929 | |
2930 | static const string EMBEDDED_FETCH_DEREF_DONE = string("\n") | |
f1e8e7e0 MW |
2931 | + "#undef fetch_register\n" |
2932 | + "#undef store_register\n" | |
2933 | + "#undef deref\n" | |
2934 | + "#undef store_deref\n"; | |
bbee5bb8 | 2935 | |
5f36109e JS |
2936 | expression* |
2937 | dwarf_pretty_print::deref (target_symbol* e) | |
2938 | { | |
2939 | static unsigned tick = 0; | |
2940 | ||
d19a9a82 JS |
2941 | if (!deref_p) |
2942 | { | |
2943 | assert (pointer && e->components.empty()); | |
2944 | return pointer; | |
2945 | } | |
2946 | ||
5f36109e JS |
2947 | // Synthesize a function to dereference the dwarf fields, |
2948 | // with a pointer parameter that is the base tracepoint variable | |
2949 | functiondecl *fdecl = new functiondecl; | |
2950 | fdecl->synthetic = true; | |
2951 | fdecl->tok = e->tok; | |
2952 | embeddedcode *ec = new embeddedcode; | |
2953 | ec->tok = e->tok; | |
2954 | ||
2955 | fdecl->name = "_dwarf_pretty_print_deref_" + lex_cast(tick++); | |
2956 | fdecl->body = ec; | |
2957 | ||
2958 | // Synthesize a functioncall. | |
2959 | functioncall* fcall = new functioncall; | |
2960 | fcall->tok = e->tok; | |
2961 | fcall->function = fdecl->name; | |
5f36109e | 2962 | |
a5ce5211 | 2963 | ec->code += EMBEDDED_FETCH_DEREF(userspace_p); |
5f36109e JS |
2964 | |
2965 | if (pointer) | |
2966 | { | |
2967 | ec->code += dw.literal_stmt_for_pointer (&pointer_type, e, | |
2968 | false, fdecl->type); | |
2969 | ||
2970 | vardecl *v = new vardecl; | |
2971 | v->type = pe_long; | |
2972 | v->name = "pointer"; | |
2973 | v->tok = e->tok; | |
2974 | fdecl->formal_args.push_back(v); | |
2975 | fcall->args.push_back(pointer); | |
2976 | } | |
2977 | else if (!local.empty()) | |
2978 | ec->code += dw.literal_stmt_for_local (scopes, pc, local, e, | |
2979 | false, fdecl->type); | |
2980 | else | |
2981 | ec->code += dw.literal_stmt_for_return (&scopes[0], pc, e, | |
2982 | false, fdecl->type); | |
2983 | ||
2984 | // Any non-literal indexes need to be passed in too. | |
2985 | for (unsigned i = 0; i < e->components.size(); ++i) | |
2986 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
2987 | { | |
2988 | vardecl *v = new vardecl; | |
2989 | v->type = pe_long; | |
2990 | v->name = "index" + lex_cast(i); | |
2991 | v->tok = e->tok; | |
2992 | fdecl->formal_args.push_back(v); | |
2993 | fcall->args.push_back(e->components[i].expr_index); | |
2994 | } | |
2995 | ||
2996 | ec->code += "/* pure */"; | |
2997 | ec->code += "/* unprivileged */"; | |
2998 | ||
a5ce5211 | 2999 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
5f36109e | 3000 | |
f8809d54 | 3001 | fdecl->join (dw.sess); |
5f36109e JS |
3002 | return fcall; |
3003 | } | |
3004 | ||
3005 | ||
c55ea10d JS |
3006 | bool |
3007 | dwarf_pretty_print::push_deref (print_format* pf, const string& fmt, | |
3008 | target_symbol* e) | |
3009 | { | |
3010 | expression* e2 = NULL; | |
3011 | try | |
3012 | { | |
3013 | e2 = deref (e); | |
3014 | } | |
3015 | catch (const semantic_error&) | |
3016 | { | |
3017 | pf->raw_components.append ("?"); | |
3018 | return false; | |
3019 | } | |
3020 | pf->raw_components.append (fmt); | |
3021 | pf->args.push_back (e2); | |
3022 | return true; | |
3023 | } | |
3024 | ||
3025 | ||
e57b735a | 3026 | void |
a7999c82 | 3027 | dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e) |
e57b735a | 3028 | { |
a7999c82 JS |
3029 | // Get the full name of the target symbol. |
3030 | stringstream ts_name_stream; | |
3031 | e->print(ts_name_stream); | |
3032 | string ts_name = ts_name_stream.str(); | |
3033 | ||
3034 | // Check and make sure we haven't already seen this target | |
3035 | // variable in this return probe. If we have, just return our | |
3036 | // last replacement. | |
af234c40 | 3037 | map<string, expression *>::iterator i = return_ts_map.find(ts_name); |
a7999c82 | 3038 | if (i != return_ts_map.end()) |
85ecf79a | 3039 | { |
a7999c82 JS |
3040 | provide (i->second); |
3041 | return; | |
3042 | } | |
85ecf79a | 3043 | |
70208613 JS |
3044 | // Attempt the expansion directly first, so if there's a problem with the |
3045 | // variable we won't have a bogus entry probe lying around. Like in | |
3046 | // saveargs(), we pretend for a moment that we're not in a .return. | |
3047 | bool saved_has_return = q.has_return; | |
3048 | q.has_return = false; | |
3049 | expression *repl = e; | |
3050 | replace (repl); | |
3051 | q.has_return = saved_has_return; | |
3052 | target_symbol* n = dynamic_cast<target_symbol*>(repl); | |
3053 | if (n && n->saved_conversion_error) | |
3054 | { | |
3055 | provide (repl); | |
3056 | return; | |
3057 | } | |
3058 | ||
af234c40 JS |
3059 | expression *exp; |
3060 | if (!q.has_process && | |
3061 | strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0) | |
140be17a | 3062 | exp = gen_kretprobe_saved_return(repl); |
af234c40 | 3063 | else |
cc9001af | 3064 | exp = gen_mapped_saved_return(repl, e->sym_name()); |
af234c40 JS |
3065 | |
3066 | // Provide the variable to our parent so it can be used as a | |
3067 | // substitute for the target symbol. | |
3068 | provide (exp); | |
3069 | ||
3070 | // Remember this replacement since we might be able to reuse | |
3071 | // it later if the same return probe references this target | |
3072 | // symbol again. | |
3073 | return_ts_map[ts_name] = exp; | |
3074 | } | |
3075 | ||
4a2970a3 | 3076 | static expression* |
23dc94f6 DS |
3077 | gen_mapped_saved_return(systemtap_session &sess, expression* e, |
3078 | const string& name, | |
3079 | block *& add_block, bool& add_block_tid, | |
3080 | block *& add_call_probe, bool& add_call_probe_tid) | |
af234c40 | 3081 | { |
23dc94f6 DS |
3082 | static unsigned tick = 0; |
3083 | ||
a7999c82 JS |
3084 | // We've got to do several things here to handle target |
3085 | // variables in return probes. | |
85ecf79a | 3086 | |
a7999c82 JS |
3087 | // (1) Synthesize two global arrays. One is the cache of the |
3088 | // target variable and the other contains a thread specific | |
3089 | // nesting level counter. The arrays will look like | |
3090 | // this: | |
3091 | // | |
23dc94f6 DS |
3092 | // _entry_tvar_{name}_{num} |
3093 | // _entry_tvar_{name}_{num}_ctr | |
a7999c82 | 3094 | |
23dc94f6 | 3095 | string aname = (string("_entry_tvar_") |
cc9001af | 3096 | + name |
aca66a36 | 3097 | + "_" + lex_cast(tick++)); |
a7999c82 JS |
3098 | vardecl* vd = new vardecl; |
3099 | vd->name = aname; | |
3100 | vd->tok = e->tok; | |
23dc94f6 | 3101 | sess.globals.push_back (vd); |
a7999c82 JS |
3102 | |
3103 | string ctrname = aname + "_ctr"; | |
3104 | vd = new vardecl; | |
3105 | vd->name = ctrname; | |
3106 | vd->tok = e->tok; | |
23dc94f6 | 3107 | sess.globals.push_back (vd); |
a7999c82 JS |
3108 | |
3109 | // (2) Create a new code block we're going to insert at the | |
3110 | // beginning of this probe to get the cached value into a | |
3111 | // temporary variable. We'll replace the target variable | |
3112 | // reference with the temporary variable reference. The code | |
3113 | // will look like this: | |
3114 | // | |
23dc94f6 DS |
3115 | // _entry_tvar_tid = tid() |
3116 | // _entry_tvar_{name}_{num}_tmp | |
3117 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3118 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
3119 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3120 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
3121 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) | |
3122 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3123 | |
3124 | // (2a) Synthesize the tid temporary expression, which will look | |
3125 | // like this: | |
3126 | // | |
23dc94f6 | 3127 | // _entry_tvar_tid = tid() |
a7999c82 | 3128 | symbol* tidsym = new symbol; |
23dc94f6 | 3129 | tidsym->name = string("_entry_tvar_tid"); |
a7999c82 | 3130 | tidsym->tok = e->tok; |
85ecf79a | 3131 | |
a7999c82 JS |
3132 | if (add_block == NULL) |
3133 | { | |
3134 | add_block = new block; | |
3135 | add_block->tok = e->tok; | |
8cc799a5 | 3136 | } |
8c819921 | 3137 | |
8cc799a5 JS |
3138 | if (!add_block_tid) |
3139 | { | |
a7999c82 JS |
3140 | // Synthesize a functioncall to grab the thread id. |
3141 | functioncall* fc = new functioncall; | |
3142 | fc->tok = e->tok; | |
3143 | fc->function = string("tid"); | |
8c819921 | 3144 | |
23dc94f6 | 3145 | // Assign the tid to '_entry_tvar_tid'. |
8c819921 DS |
3146 | assignment* a = new assignment; |
3147 | a->tok = e->tok; | |
3148 | a->op = "="; | |
a7999c82 JS |
3149 | a->left = tidsym; |
3150 | a->right = fc; | |
8c819921 DS |
3151 | |
3152 | expr_statement* es = new expr_statement; | |
3153 | es->tok = e->tok; | |
3154 | es->value = a; | |
8c819921 | 3155 | add_block->statements.push_back (es); |
8cc799a5 | 3156 | add_block_tid = true; |
a7999c82 | 3157 | } |
8c819921 | 3158 | |
a7999c82 JS |
3159 | // (2b) Synthesize an array reference and assign it to a |
3160 | // temporary variable (that we'll use as replacement for the | |
3161 | // target variable reference). It will look like this: | |
3162 | // | |
23dc94f6 DS |
3163 | // _entry_tvar_{name}_{num}_tmp |
3164 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3165 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3166 | |
3167 | arrayindex* ai_tvar_base = new arrayindex; | |
3168 | ai_tvar_base->tok = e->tok; | |
3169 | ||
3170 | symbol* sym = new symbol; | |
3171 | sym->name = aname; | |
3172 | sym->tok = e->tok; | |
3173 | ai_tvar_base->base = sym; | |
3174 | ||
3175 | ai_tvar_base->indexes.push_back(tidsym); | |
3176 | ||
3177 | // We need to create a copy of the array index in its current | |
3178 | // state so we can have 2 variants of it (the original and one | |
3179 | // that post-decrements the second index). | |
3180 | arrayindex* ai_tvar = new arrayindex; | |
3181 | arrayindex* ai_tvar_postdec = new arrayindex; | |
3182 | *ai_tvar = *ai_tvar_base; | |
3183 | *ai_tvar_postdec = *ai_tvar_base; | |
3184 | ||
3185 | // Synthesize the | |
23dc94f6 | 3186 | // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the |
a7999c82 JS |
3187 | // second index into the array. |
3188 | arrayindex* ai_ctr = new arrayindex; | |
3189 | ai_ctr->tok = e->tok; | |
3190 | ||
3191 | sym = new symbol; | |
3192 | sym->name = ctrname; | |
3193 | sym->tok = e->tok; | |
3194 | ai_ctr->base = sym; | |
3195 | ai_ctr->indexes.push_back(tidsym); | |
3196 | ai_tvar->indexes.push_back(ai_ctr); | |
3197 | ||
3198 | symbol* tmpsym = new symbol; | |
3199 | tmpsym->name = aname + "_tmp"; | |
3200 | tmpsym->tok = e->tok; | |
3201 | ||
3202 | assignment* a = new assignment; | |
3203 | a->tok = e->tok; | |
3204 | a->op = "="; | |
3205 | a->left = tmpsym; | |
3206 | a->right = ai_tvar; | |
3207 | ||
3208 | expr_statement* es = new expr_statement; | |
3209 | es->tok = e->tok; | |
3210 | es->value = a; | |
3211 | ||
3212 | add_block->statements.push_back (es); | |
3213 | ||
3214 | // (2c) Add a post-decrement to the second array index and | |
3215 | // delete the array value. It will look like this: | |
3216 | // | |
23dc94f6 DS |
3217 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3218 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
a7999c82 JS |
3219 | |
3220 | post_crement* pc = new post_crement; | |
3221 | pc->tok = e->tok; | |
3222 | pc->op = "--"; | |
3223 | pc->operand = ai_ctr; | |
3224 | ai_tvar_postdec->indexes.push_back(pc); | |
3225 | ||
3226 | delete_statement* ds = new delete_statement; | |
3227 | ds->tok = e->tok; | |
3228 | ds->value = ai_tvar_postdec; | |
3229 | ||
3230 | add_block->statements.push_back (ds); | |
3231 | ||
3232 | // (2d) Delete the counter value if it is 0. It will look like | |
3233 | // this: | |
23dc94f6 DS |
3234 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) |
3235 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3236 | |
3237 | ds = new delete_statement; | |
3238 | ds->tok = e->tok; | |
3239 | ds->value = ai_ctr; | |
3240 | ||
3241 | unary_expression *ue = new unary_expression; | |
3242 | ue->tok = e->tok; | |
3243 | ue->op = "!"; | |
3244 | ue->operand = ai_ctr; | |
3245 | ||
3246 | if_statement *ifs = new if_statement; | |
3247 | ifs->tok = e->tok; | |
3248 | ifs->condition = ue; | |
3249 | ifs->thenblock = ds; | |
3250 | ifs->elseblock = NULL; | |
3251 | ||
3252 | add_block->statements.push_back (ifs); | |
3253 | ||
3254 | // (3) We need an entry probe that saves the value for us in the | |
3255 | // global array we created. Create the entry probe, which will | |
3256 | // look like this: | |
3257 | // | |
2260f4e3 | 3258 | // probe kernel.function("{function}").call { |
23dc94f6 DS |
3259 | // _entry_tvar_tid = tid() |
3260 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3261 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3262 | // = ${param} |
3263 | // } | |
3264 | ||
2260f4e3 | 3265 | if (add_call_probe == NULL) |
a7999c82 | 3266 | { |
2260f4e3 FCE |
3267 | add_call_probe = new block; |
3268 | add_call_probe->tok = e->tok; | |
8cc799a5 | 3269 | } |
4baf0e53 | 3270 | |
8cc799a5 JS |
3271 | if (!add_call_probe_tid) |
3272 | { | |
a7999c82 JS |
3273 | // Synthesize a functioncall to grab the thread id. |
3274 | functioncall* fc = new functioncall; | |
3275 | fc->tok = e->tok; | |
3276 | fc->function = string("tid"); | |
4baf0e53 | 3277 | |
23dc94f6 | 3278 | // Assign the tid to '_entry_tvar_tid'. |
a7999c82 | 3279 | assignment* a = new assignment; |
8fc05e57 DS |
3280 | a->tok = e->tok; |
3281 | a->op = "="; | |
a7999c82 JS |
3282 | a->left = tidsym; |
3283 | a->right = fc; | |
8fc05e57 | 3284 | |
a7999c82 | 3285 | expr_statement* es = new expr_statement; |
8fc05e57 DS |
3286 | es->tok = e->tok; |
3287 | es->value = a; | |
2260f4e3 | 3288 | add_call_probe = new block(add_call_probe, es); |
8cc799a5 | 3289 | add_call_probe_tid = true; |
85ecf79a | 3290 | } |
cf2a1f85 | 3291 | |
a7999c82 | 3292 | // Save the value, like this: |
23dc94f6 DS |
3293 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3294 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3295 | // = ${param} |
3296 | arrayindex* ai_tvar_preinc = new arrayindex; | |
3297 | *ai_tvar_preinc = *ai_tvar_base; | |
3298 | ||
3299 | pre_crement* preinc = new pre_crement; | |
3300 | preinc->tok = e->tok; | |
3301 | preinc->op = "++"; | |
3302 | preinc->operand = ai_ctr; | |
3303 | ai_tvar_preinc->indexes.push_back(preinc); | |
3304 | ||
3305 | a = new assignment; | |
3306 | a->tok = e->tok; | |
3307 | a->op = "="; | |
3308 | a->left = ai_tvar_preinc; | |
3309 | a->right = e; | |
3310 | ||
3311 | es = new expr_statement; | |
3312 | es->tok = e->tok; | |
3313 | es->value = a; | |
3314 | ||
2260f4e3 | 3315 | add_call_probe = new block(add_call_probe, es); |
a7999c82 | 3316 | |
23dc94f6 | 3317 | // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to |
a7999c82 JS |
3318 | // our parent so it can be used as a substitute for the target |
3319 | // symbol. | |
3f803f9e | 3320 | delete ai_tvar_base; |
af234c40 JS |
3321 | return tmpsym; |
3322 | } | |
a7999c82 | 3323 | |
af234c40 | 3324 | |
23dc94f6 DS |
3325 | expression* |
3326 | dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e, | |
3327 | const string& name) | |
3328 | { | |
3329 | return ::gen_mapped_saved_return(q.sess, e, name, add_block, | |
3330 | add_block_tid, add_call_probe, | |
3331 | add_call_probe_tid); | |
3332 | } | |
3333 | ||
3334 | ||
af234c40 | 3335 | expression* |
140be17a | 3336 | dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e) |
af234c40 JS |
3337 | { |
3338 | // The code for this is simple. | |
3339 | // | |
3340 | // .call: | |
3341 | // _set_kretprobe_long(index, $value) | |
3342 | // | |
3343 | // .return: | |
3344 | // _get_kretprobe_long(index) | |
3345 | // | |
3346 | // (or s/long/string/ for things like $$parms) | |
3347 | ||
3348 | unsigned index; | |
3349 | string setfn, getfn; | |
3350 | ||
140be17a JS |
3351 | // We need the caller to predetermine the type of the expression! |
3352 | switch (e->type) | |
af234c40 | 3353 | { |
140be17a | 3354 | case pe_string: |
af234c40 JS |
3355 | index = saved_strings++; |
3356 | setfn = "_set_kretprobe_string"; | |
3357 | getfn = "_get_kretprobe_string"; | |
140be17a JS |
3358 | break; |
3359 | case pe_long: | |
af234c40 JS |
3360 | index = saved_longs++; |
3361 | setfn = "_set_kretprobe_long"; | |
3362 | getfn = "_get_kretprobe_long"; | |
140be17a JS |
3363 | break; |
3364 | default: | |
b530b5b3 | 3365 | throw semantic_error(_("unknown type to save in kretprobe"), e->tok); |
af234c40 JS |
3366 | } |
3367 | ||
3368 | // Create the entry code | |
3369 | // _set_kretprobe_{long|string}(index, $value) | |
3370 | ||
3371 | if (add_call_probe == NULL) | |
3372 | { | |
3373 | add_call_probe = new block; | |
3374 | add_call_probe->tok = e->tok; | |
3375 | } | |
3376 | ||
3377 | functioncall* set_fc = new functioncall; | |
3378 | set_fc->tok = e->tok; | |
3379 | set_fc->function = setfn; | |
3380 | set_fc->args.push_back(new literal_number(index)); | |
3381 | set_fc->args.back()->tok = e->tok; | |
3382 | set_fc->args.push_back(e); | |
3383 | ||
3384 | expr_statement* set_es = new expr_statement; | |
3385 | set_es->tok = e->tok; | |
3386 | set_es->value = set_fc; | |
3387 | ||
3388 | add_call_probe->statements.push_back(set_es); | |
3389 | ||
3390 | // Create the return code | |
3391 | // _get_kretprobe_{long|string}(index) | |
3392 | ||
3393 | functioncall* get_fc = new functioncall; | |
3394 | get_fc->tok = e->tok; | |
3395 | get_fc->function = getfn; | |
3396 | get_fc->args.push_back(new literal_number(index)); | |
3397 | get_fc->args.back()->tok = e->tok; | |
3398 | ||
3399 | return get_fc; | |
a7999c82 | 3400 | } |
a43ba433 | 3401 | |
2cb3fe26 | 3402 | |
a7999c82 JS |
3403 | void |
3404 | dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
3405 | { | |
9aa8ffce | 3406 | if (null_die(scope_die)) |
a7999c82 | 3407 | return; |
2cb3fe26 | 3408 | |
5f36109e JS |
3409 | target_symbol *tsym = new target_symbol(*e); |
3410 | ||
fde50242 JS |
3411 | bool pretty = (!e->components.empty() && |
3412 | e->components[0].type == target_symbol::comp_pretty_print); | |
3413 | string format = pretty ? "=%s" : "=%#x"; | |
a43ba433 | 3414 | |
a7999c82 JS |
3415 | // Convert $$parms to sprintf of a list of parms and active local vars |
3416 | // which we recursively evaluate | |
a43ba433 | 3417 | |
a7999c82 JS |
3418 | // NB: we synthesize a new token here rather than reusing |
3419 | // e->tok, because print_format::print likes to use | |
3420 | // its tok->content. | |
5f36109e | 3421 | token* pf_tok = new token(*e->tok); |
a7999c82 | 3422 | pf_tok->type = tok_identifier; |
b393f6f2 | 3423 | pf_tok->content = "sprintf"; |
2cb3fe26 | 3424 | |
d5e178c1 | 3425 | print_format* pf = print_format::create(pf_tok); |
a7999c82 | 3426 | |
277c21bc | 3427 | if (q.has_return && (e->name == "$$return")) |
a7999c82 | 3428 | { |
277c21bc | 3429 | tsym->name = "$return"; |
a7999c82 JS |
3430 | |
3431 | // Ignore any variable that isn't accessible. | |
3432 | tsym->saved_conversion_error = 0; | |
3433 | expression *texp = tsym; | |
8b095b45 | 3434 | replace (texp); // NB: throws nothing ... |
a7999c82 | 3435 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
a43ba433 | 3436 | { |
2cb3fe26 | 3437 | |
a43ba433 FCE |
3438 | } |
3439 | else | |
3440 | { | |
a7999c82 | 3441 | pf->raw_components += "return"; |
5f36109e | 3442 | pf->raw_components += format; |
a7999c82 JS |
3443 | pf->args.push_back(texp); |
3444 | } | |
3445 | } | |
3446 | else | |
3447 | { | |
3448 | // non-.return probe: support $$parms, $$vars, $$locals | |
345bbb3d | 3449 | bool first = true; |
a7999c82 | 3450 | Dwarf_Die result; |
d48bc7eb JS |
3451 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
3452 | for (unsigned i = 0; i < scopes.size(); ++i) | |
3453 | { | |
3454 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
3455 | break; // we don't want file-level variables | |
3456 | if (dwarf_child (&scopes[i], &result) == 0) | |
3457 | do | |
00cf3709 | 3458 | { |
d48bc7eb JS |
3459 | switch (dwarf_tag (&result)) |
3460 | { | |
3461 | case DW_TAG_variable: | |
3462 | if (e->name == "$$parms") | |
3463 | continue; | |
3464 | break; | |
3465 | case DW_TAG_formal_parameter: | |
3466 | if (e->name == "$$locals") | |
3467 | continue; | |
3468 | break; | |
3469 | ||
3470 | default: | |
3471 | continue; | |
3472 | } | |
41c262f3 | 3473 | |
d48bc7eb JS |
3474 | const char *diename = dwarf_diename (&result); |
3475 | if (! diename) continue; | |
f76427a2 | 3476 | |
d48bc7eb JS |
3477 | if (! first) |
3478 | pf->raw_components += " "; | |
3479 | pf->raw_components += diename; | |
fde50242 JS |
3480 | first = false; |
3481 | ||
3482 | // Write a placeholder for ugly aggregates | |
3483 | Dwarf_Die type; | |
3484 | if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type)) | |
3485 | { | |
3486 | q.dw.resolve_unqualified_inner_typedie(&type, &type, e); | |
3487 | switch (dwarf_tag(&type)) | |
3488 | { | |
3489 | case DW_TAG_union_type: | |
3490 | case DW_TAG_structure_type: | |
3491 | case DW_TAG_class_type: | |
3492 | pf->raw_components += "={...}"; | |
3493 | continue; | |
3494 | ||
3495 | case DW_TAG_array_type: | |
3496 | pf->raw_components += "=[...]"; | |
3497 | continue; | |
3498 | } | |
3499 | } | |
345bbb3d | 3500 | |
d48bc7eb JS |
3501 | tsym->name = "$"; |
3502 | tsym->name += diename; | |
41c262f3 | 3503 | |
d48bc7eb JS |
3504 | // Ignore any variable that isn't accessible. |
3505 | tsym->saved_conversion_error = 0; | |
3506 | expression *texp = tsym; | |
3507 | replace (texp); // NB: throws nothing ... | |
3508 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. | |
a43ba433 | 3509 | { |
d48bc7eb JS |
3510 | if (q.sess.verbose>2) |
3511 | { | |
e26c2f83 | 3512 | for (const semantic_error *c = tsym->saved_conversion_error; |
d48bc7eb JS |
3513 | c != 0; |
3514 | c = c->chain) { | |
b530b5b3 | 3515 | clog << _("variable location problem: ") << c->what() << endl; |
d48bc7eb JS |
3516 | } |
3517 | } | |
3518 | ||
3519 | pf->raw_components += "=?"; | |
a43ba433 | 3520 | } |
d48bc7eb JS |
3521 | else |
3522 | { | |
3523 | pf->raw_components += format; | |
3524 | pf->args.push_back(texp); | |
3525 | } | |
a7999c82 | 3526 | } |
d48bc7eb JS |
3527 | while (dwarf_siblingof (&result, &result) == 0); |
3528 | } | |
a7999c82 | 3529 | } |
2cb3fe26 | 3530 | |
a7999c82 | 3531 | pf->components = print_format::string_to_components(pf->raw_components); |
140be17a | 3532 | pf->type = pe_string; |
a7999c82 JS |
3533 | provide (pf); |
3534 | } | |
3535 | ||
2cb3fe26 | 3536 | |
a7999c82 JS |
3537 | void |
3538 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
3539 | { | |
cc9001af MW |
3540 | assert(e->name.size() > 0 |
3541 | && ((e->name[0] == '$' && e->target_name == "") | |
3542 | || (e->name == "@var" && e->target_name != ""))); | |
a7999c82 | 3543 | visited = true; |
30263a73 FCE |
3544 | bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e)); |
3545 | // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars | |
a7999c82 | 3546 | |
70208613 | 3547 | try |
a7999c82 | 3548 | { |
c69a87e0 FCE |
3549 | bool lvalue = is_active_lvalue(e); |
3550 | if (lvalue && !q.sess.guru_mode) | |
b3741c9d | 3551 | throw semantic_error(_("write to target variable not permitted; need stap -g"), e->tok); |
2cb3fe26 | 3552 | |
100a540e | 3553 | // XXX: process $context vars should be writable |
70208613 | 3554 | |
c69a87e0 FCE |
3555 | // See if we need to generate a new probe to save/access function |
3556 | // parameters from a return probe. PR 1382. | |
3557 | if (q.has_return | |
3558 | && !defined_being_checked | |
277c21bc JS |
3559 | && e->name != "$return" // not the special return-value variable handled below |
3560 | && e->name != "$$return") // nor the other special variable handled below | |
c69a87e0 FCE |
3561 | { |
3562 | if (lvalue) | |
b530b5b3 | 3563 | throw semantic_error(_("write to target variable not permitted in .return probes"), e->tok); |
c69a87e0 FCE |
3564 | visit_target_symbol_saved_return(e); |
3565 | return; | |
3566 | } | |
e57b735a | 3567 | |
277c21bc JS |
3568 | if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals" |
3569 | || (q.has_return && (e->name == "$$return"))) | |
c69a87e0 FCE |
3570 | { |
3571 | if (lvalue) | |
b530b5b3 | 3572 | throw semantic_error(_("cannot write to context variable"), e->tok); |
70208613 | 3573 | |
c69a87e0 | 3574 | if (e->addressof) |
b530b5b3 | 3575 | throw semantic_error(_("cannot take address of context variable"), e->tok); |
70208613 | 3576 | |
5f36109e JS |
3577 | e->assert_no_components("dwarf", true); |
3578 | ||
c69a87e0 FCE |
3579 | visit_target_symbol_context(e); |
3580 | return; | |
3581 | } | |
70208613 | 3582 | |
5f36109e JS |
3583 | if (!e->components.empty() && |
3584 | e->components.back().type == target_symbol::comp_pretty_print) | |
3585 | { | |
3586 | if (lvalue) | |
b530b5b3 | 3587 | throw semantic_error(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 3588 | |
277c21bc | 3589 | if (q.has_return && (e->name == "$return")) |
5f36109e JS |
3590 | { |
3591 | dwarf_pretty_print dpp (q.dw, scope_die, addr, | |
3592 | q.has_process, *e); | |
3593 | dpp.expand()->visit(this); | |
3594 | } | |
3595 | else | |
3596 | { | |
3597 | dwarf_pretty_print dpp (q.dw, getscopes(e), addr, | |
cc9001af | 3598 | e->sym_name(), |
5f36109e JS |
3599 | q.has_process, *e); |
3600 | dpp.expand()->visit(this); | |
3601 | } | |
3602 | return; | |
3603 | } | |
3604 | ||
c69a87e0 FCE |
3605 | // Synthesize a function. |
3606 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 3607 | fdecl->synthetic = true; |
c69a87e0 FCE |
3608 | fdecl->tok = e->tok; |
3609 | embeddedcode *ec = new embeddedcode; | |
3610 | ec->tok = e->tok; | |
70208613 | 3611 | |
c69a87e0 | 3612 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
cc9001af | 3613 | + "_" + e->sym_name() |
c69a87e0 | 3614 | + "_" + lex_cast(tick++)); |
70208613 | 3615 | |
a5ce5211 | 3616 | ec->code += EMBEDDED_FETCH_DEREF(q.has_process); |
70208613 | 3617 | |
277c21bc | 3618 | if (q.has_return && (e->name == "$return")) |
e19fda4e | 3619 | { |
b5a0dd41 | 3620 | ec->code += q.dw.literal_stmt_for_return (scope_die, |
e19fda4e | 3621 | addr, |
b4c34c26 | 3622 | e, |
e19fda4e DS |
3623 | lvalue, |
3624 | fdecl->type); | |
3625 | } | |
3626 | else | |
3627 | { | |
b5a0dd41 | 3628 | ec->code += q.dw.literal_stmt_for_local (getscopes(e), |
e19fda4e | 3629 | addr, |
cc9001af | 3630 | e->sym_name(), |
b4c34c26 | 3631 | e, |
e19fda4e DS |
3632 | lvalue, |
3633 | fdecl->type); | |
3634 | } | |
3635 | ||
1b07c728 FCE |
3636 | if (! lvalue) |
3637 | ec->code += "/* pure */"; | |
64211010 DB |
3638 | |
3639 | ec->code += "/* unprivileged */"; | |
a5ce5211 | 3640 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
c69a87e0 FCE |
3641 | |
3642 | fdecl->name = fname; | |
3643 | fdecl->body = ec; | |
70208613 | 3644 | |
c69a87e0 FCE |
3645 | // Any non-literal indexes need to be passed in too. |
3646 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3647 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3648 | { | |
3649 | vardecl *v = new vardecl; | |
3650 | v->type = pe_long; | |
3651 | v->name = "index" + lex_cast(i); | |
3652 | v->tok = e->tok; | |
3653 | fdecl->formal_args.push_back(v); | |
3654 | } | |
70208613 | 3655 | |
c69a87e0 FCE |
3656 | if (lvalue) |
3657 | { | |
3658 | // Modify the fdecl so it carries a single pe_long formal | |
3659 | // argument called "value". | |
70208613 | 3660 | |
c69a87e0 FCE |
3661 | // FIXME: For the time being we only support setting target |
3662 | // variables which have base types; these are 'pe_long' in | |
3663 | // stap's type vocabulary. Strings and pointers might be | |
3664 | // reasonable, some day, but not today. | |
70208613 | 3665 | |
c69a87e0 FCE |
3666 | vardecl *v = new vardecl; |
3667 | v->type = pe_long; | |
3668 | v->name = "value"; | |
3669 | v->tok = e->tok; | |
3670 | fdecl->formal_args.push_back(v); | |
3671 | } | |
f8809d54 | 3672 | fdecl->join (q.sess); |
70208613 | 3673 | |
c69a87e0 FCE |
3674 | // Synthesize a functioncall. |
3675 | functioncall* n = new functioncall; | |
3676 | n->tok = e->tok; | |
3677 | n->function = fname; | |
140be17a | 3678 | n->type = fdecl->type; |
70208613 | 3679 | |
c69a87e0 FCE |
3680 | // Any non-literal indexes need to be passed in too. |
3681 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3682 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3683 | n->args.push_back(require(e->components[i].expr_index)); | |
70208613 | 3684 | |
c69a87e0 FCE |
3685 | if (lvalue) |
3686 | { | |
3687 | // Provide the functioncall to our parent, so that it can be | |
3688 | // used to substitute for the assignment node immediately above | |
3689 | // us. | |
3690 | assert(!target_symbol_setter_functioncalls.empty()); | |
3691 | *(target_symbol_setter_functioncalls.top()) = n; | |
3692 | } | |
70208613 | 3693 | |
c69a87e0 | 3694 | provide (n); |
66d284f4 FCE |
3695 | } |
3696 | catch (const semantic_error& er) | |
3697 | { | |
9fab2262 JS |
3698 | // We suppress this error message, and pass the unresolved |
3699 | // target_symbol to the next pass. We hope that this value ends | |
3700 | // up not being referenced after all, so it can be optimized out | |
3701 | // quietly. | |
1af1e62d | 3702 | e->chain (er); |
9fab2262 | 3703 | provide (e); |
66d284f4 | 3704 | } |
77de5e9e GH |
3705 | } |
3706 | ||
3707 | ||
c24447be JS |
3708 | void |
3709 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
3710 | { | |
3711 | // Fill in our current module context if needed | |
3712 | if (e->module.empty()) | |
3713 | e->module = q.dw.module_name; | |
3714 | ||
3715 | var_expanding_visitor::visit_cast_op(e); | |
3716 | } | |
3717 | ||
3718 | ||
8cc799a5 JS |
3719 | void |
3720 | dwarf_var_expanding_visitor::visit_entry_op (entry_op *e) | |
3721 | { | |
3722 | expression *repl = e; | |
3723 | if (q.has_return) | |
3724 | { | |
3725 | // expand the operand as if it weren't a return probe | |
3726 | q.has_return = false; | |
3727 | replace (e->operand); | |
3728 | q.has_return = true; | |
3729 | ||
3730 | // XXX it would be nice to use gen_kretprobe_saved_return when available, | |
3731 | // but it requires knowing the types already, which is problematic for | |
3732 | // arbitrary expressons. | |
cc9001af | 3733 | repl = gen_mapped_saved_return (e->operand, "entry"); |
8cc799a5 JS |
3734 | } |
3735 | provide (repl); | |
3736 | } | |
3737 | ||
bfa7e523 MW |
3738 | vector<Dwarf_Die>& |
3739 | dwarf_var_expanding_visitor::getcuscope(target_symbol *e) | |
3740 | { | |
54e9f062 MW |
3741 | Dwarf_Off cu_off = 0; |
3742 | const char *cu_name = NULL; | |
bfa7e523 MW |
3743 | |
3744 | string prefixed_srcfile = string("*/") + e->cu_name; | |
3745 | ||
3746 | Dwarf_Off off = 0; | |
3747 | size_t cuhl; | |
3748 | Dwarf_Off noff; | |
3749 | Dwarf_Off module_bias; | |
3750 | Dwarf *dw = dwfl_module_getdwarf(q.dw.module, &module_bias); | |
54e9f062 | 3751 | while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) |
bfa7e523 MW |
3752 | { |
3753 | Dwarf_Die die_mem; | |
3754 | Dwarf_Die *die; | |
3755 | die = dwarf_offdie (dw, off + cuhl, &die_mem); | |
54e9f062 MW |
3756 | const char *die_name = dwarf_diename (die); |
3757 | ||
3758 | if (strcmp (die_name, e->cu_name.c_str()) == 0) // Perfect match. | |
3759 | { | |
3760 | cu_name = die_name; | |
3761 | cu_off = off + cuhl; | |
3762 | break; | |
3763 | } | |
3764 | ||
3765 | if (fnmatch(prefixed_srcfile.c_str(), die_name, 0) == 0) | |
3766 | if (cu_name == NULL || strlen (die_name) < strlen (cu_name)) | |
3767 | { | |
3768 | cu_name = die_name; | |
3769 | cu_off = off + cuhl; | |
3770 | } | |
bfa7e523 MW |
3771 | off = noff; |
3772 | } | |
3773 | ||
54e9f062 | 3774 | if (cu_name == NULL) |
bfa7e523 MW |
3775 | throw semantic_error ("unable to find CU '" + e->cu_name + "'" |
3776 | + " while searching for '" + e->target_name + "'", | |
3777 | e->tok); | |
3778 | ||
3779 | vector<Dwarf_Die> *cu_scope = new vector<Dwarf_Die>; | |
54e9f062 MW |
3780 | Dwarf_Die cu_die; |
3781 | dwarf_offdie (dw, cu_off, &cu_die); | |
3782 | cu_scope->push_back(cu_die); | |
bfa7e523 MW |
3783 | return *cu_scope; |
3784 | } | |
8cc799a5 | 3785 | |
729455a7 JS |
3786 | vector<Dwarf_Die>& |
3787 | dwarf_var_expanding_visitor::getscopes(target_symbol *e) | |
3788 | { | |
bfa7e523 MW |
3789 | // "static globals" can only be found in the top-level CU. |
3790 | if (e->name == "@var" && e->cu_name != "") | |
3791 | return this->getcuscope(e); | |
3792 | ||
729455a7 JS |
3793 | if (scopes.empty()) |
3794 | { | |
f25a9197 CM |
3795 | if(scope_die != NULL) |
3796 | scopes = q.dw.getscopes(scope_die); | |
729455a7 | 3797 | if (scopes.empty()) |
b530b5b3 LB |
3798 | //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok); |
3799 | // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" )) | |
729455a7 JS |
3800 | throw semantic_error ("unable to find any scopes containing " |
3801 | + lex_cast_hex(addr) | |
3802 | + ((scope_die == NULL) ? "" | |
3803 | : (string (" in ") | |
3804 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
3805 | + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") | |
3806 | + ")")) | |
3807 | + " while searching for local '" | |
cc9001af | 3808 | + e->sym_name() + "'", |
729455a7 JS |
3809 | e->tok); |
3810 | } | |
3811 | return scopes; | |
3812 | } | |
3813 | ||
3814 | ||
5f36109e JS |
3815 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor |
3816 | { | |
3817 | systemtap_session& s; | |
3818 | dwarf_builder& db; | |
3819 | ||
3820 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
3821 | s(s), db(db) {} | |
3822 | void visit_cast_op (cast_op* e); | |
3823 | void filter_special_modules(string& module); | |
3824 | }; | |
3825 | ||
3826 | ||
c4ce66a1 JS |
3827 | struct dwarf_cast_query : public base_query |
3828 | { | |
946e1a48 | 3829 | cast_op& e; |
c4ce66a1 | 3830 | const bool lvalue; |
5f36109e JS |
3831 | const bool userspace_p; |
3832 | functioncall*& result; | |
c4ce66a1 | 3833 | |
5f36109e JS |
3834 | dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue, |
3835 | const bool userspace_p, functioncall*& result): | |
abb41d92 | 3836 | base_query(dw, module), e(e), lvalue(lvalue), |
5f36109e | 3837 | userspace_p(userspace_p), result(result) {} |
c4ce66a1 JS |
3838 | |
3839 | void handle_query_module(); | |
822a6a3d | 3840 | void query_library (const char *) {} |
576eaefe | 3841 | void query_plt (const char *entry, size_t addr) {} |
c4ce66a1 JS |
3842 | }; |
3843 | ||
3844 | ||
c4ce66a1 JS |
3845 | void |
3846 | dwarf_cast_query::handle_query_module() | |
3847 | { | |
5f36109e JS |
3848 | static unsigned tick = 0; |
3849 | ||
3850 | if (result) | |
c4ce66a1 JS |
3851 | return; |
3852 | ||
ea1e477a | 3853 | // look for the type in any CU |
a44a7cb5 JS |
3854 | Dwarf_Die* type_die = NULL; |
3855 | if (startswith(e.type_name, "class ")) | |
3856 | { | |
3857 | // normalize to match dwflpp::global_alias_caching_callback | |
3858 | string struct_name = "struct " + e.type_name.substr(6); | |
3859 | type_die = dw.declaration_resolve_other_cus(struct_name); | |
3860 | } | |
3861 | else | |
3862 | type_die = dw.declaration_resolve_other_cus(e.type_name); | |
3863 | ||
3864 | // NB: We now index the types as "struct name"/"union name"/etc. instead of | |
3865 | // just "name". But since we didn't require users to be explicit before, and | |
3866 | // actually sort of discouraged it, we must be flexible now. So if a lookup | |
3867 | // fails with a bare name, try augmenting it. | |
3868 | if (!type_die && | |
3869 | !startswith(e.type_name, "class ") && | |
3870 | !startswith(e.type_name, "struct ") && | |
3871 | !startswith(e.type_name, "union ") && | |
3872 | !startswith(e.type_name, "enum ")) | |
3873 | { | |
3874 | type_die = dw.declaration_resolve_other_cus("struct " + e.type_name); | |
3875 | if (!type_die) | |
3876 | type_die = dw.declaration_resolve_other_cus("union " + e.type_name); | |
3877 | if (!type_die) | |
3878 | type_die = dw.declaration_resolve_other_cus("enum " + e.type_name); | |
3879 | } | |
3880 | ||
ea1e477a JS |
3881 | if (!type_die) |
3882 | return; | |
c4ce66a1 | 3883 | |
5f36109e JS |
3884 | string code; |
3885 | exp_type type = pe_long; | |
3886 | ||
ea1e477a | 3887 | try |
c4ce66a1 | 3888 | { |
ea1e477a JS |
3889 | Dwarf_Die cu_mem; |
3890 | dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL)); | |
5f36109e JS |
3891 | |
3892 | if (!e.components.empty() && | |
3893 | e.components.back().type == target_symbol::comp_pretty_print) | |
3894 | { | |
3895 | if (lvalue) | |
b530b5b3 | 3896 | throw semantic_error(_("cannot write to pretty-printed variable"), e.tok); |
5f36109e | 3897 | |
d19a9a82 | 3898 | dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e); |
5f36109e JS |
3899 | result = dpp.expand(); |
3900 | return; | |
3901 | } | |
3902 | ||
3903 | code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type); | |
ea1e477a JS |
3904 | } |
3905 | catch (const semantic_error& er) | |
3906 | { | |
3907 | // NB: we can have multiple errors, since a @cast | |
1af1e62d JS |
3908 | // may be attempted using several different modules: |
3909 | // @cast(ptr, "type", "module1:module2:...") | |
3910 | e.chain (er); | |
c4ce66a1 | 3911 | } |
c4ce66a1 | 3912 | |
5f36109e JS |
3913 | if (code.empty()) |
3914 | return; | |
c4ce66a1 | 3915 | |
5f36109e | 3916 | string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get") |
cc9001af | 3917 | + "_" + e.sym_name() |
5f36109e | 3918 | + "_" + lex_cast(tick++)); |
c4ce66a1 | 3919 | |
5f36109e JS |
3920 | // Synthesize a function. |
3921 | functiondecl *fdecl = new functiondecl; | |
3922 | fdecl->synthetic = true; | |
3923 | fdecl->tok = e.tok; | |
3924 | fdecl->type = type; | |
3925 | fdecl->name = fname; | |
3926 | ||
3927 | embeddedcode *ec = new embeddedcode; | |
3928 | ec->tok = e.tok; | |
3929 | fdecl->body = ec; | |
3930 | ||
a5ce5211 | 3931 | ec->code += EMBEDDED_FETCH_DEREF(userspace_p); |
5f36109e JS |
3932 | ec->code += code; |
3933 | ||
3934 | // Give the fdecl an argument for the pointer we're trying to cast | |
3935 | vardecl *v1 = new vardecl; | |
3936 | v1->type = pe_long; | |
3937 | v1->name = "pointer"; | |
3938 | v1->tok = e.tok; | |
3939 | fdecl->formal_args.push_back(v1); | |
3940 | ||
3941 | // Any non-literal indexes need to be passed in too. | |
3942 | for (unsigned i = 0; i < e.components.size(); ++i) | |
3943 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
3944 | { | |
3945 | vardecl *v = new vardecl; | |
3946 | v->type = pe_long; | |
3947 | v->name = "index" + lex_cast(i); | |
3948 | v->tok = e.tok; | |
3949 | fdecl->formal_args.push_back(v); | |
3950 | } | |
3951 | ||
3952 | if (lvalue) | |
3953 | { | |
3954 | // Modify the fdecl so it carries a second pe_long formal | |
3955 | // argument called "value". | |
3956 | ||
3957 | // FIXME: For the time being we only support setting target | |
3958 | // variables which have base types; these are 'pe_long' in | |
3959 | // stap's type vocabulary. Strings and pointers might be | |
3960 | // reasonable, some day, but not today. | |
3961 | ||
3962 | vardecl *v2 = new vardecl; | |
3963 | v2->type = pe_long; | |
3964 | v2->name = "value"; | |
3965 | v2->tok = e.tok; | |
3966 | fdecl->formal_args.push_back(v2); | |
3967 | } | |
3968 | else | |
3969 | ec->code += "/* pure */"; | |
3970 | ||
3971 | ec->code += "/* unprivileged */"; | |
a5ce5211 | 3972 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
5f36109e | 3973 | |
f8809d54 | 3974 | fdecl->join (dw.sess); |
5f36109e JS |
3975 | |
3976 | // Synthesize a functioncall. | |
3977 | functioncall* n = new functioncall; | |
3978 | n->tok = e.tok; | |
3979 | n->function = fname; | |
5f36109e JS |
3980 | n->args.push_back(e.operand); |
3981 | ||
3982 | // Any non-literal indexes need to be passed in too. | |
3983 | for (unsigned i = 0; i < e.components.size(); ++i) | |
3984 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
3985 | n->args.push_back(e.components[i].expr_index); | |
3986 | ||
3987 | result = n; | |
3988 | } | |
c4ce66a1 JS |
3989 | |
3990 | ||
fb0274bc JS |
3991 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
3992 | { | |
d90053e7 | 3993 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 3994 | // for those cases, build a module including that header |
d90053e7 | 3995 | if (module[module.size() - 1] == '>' && |
60d98537 | 3996 | (module[0] == '<' || startswith(module, "kernel<"))) |
fb0274bc JS |
3997 | { |
3998 | string cached_module; | |
3999 | if (s.use_cache) | |
4000 | { | |
4001 | // see if the cached module exists | |
a2639cb7 | 4002 | cached_module = find_typequery_hash(s, module); |
d105f664 | 4003 | if (!cached_module.empty() && !s.poison_cache) |
fb0274bc JS |
4004 | { |
4005 | int fd = open(cached_module.c_str(), O_RDONLY); | |
4006 | if (fd != -1) | |
4007 | { | |
4008 | if (s.verbose > 2) | |
b530b5b3 LB |
4009 | //TRANSLATORS: Here we're using a cached module. |
4010 | clog << _("Pass 2: using cached ") << cached_module << endl; | |
fb0274bc JS |
4011 | module = cached_module; |
4012 | close(fd); | |
4013 | return; | |
4014 | } | |
4015 | } | |
4016 | } | |
4017 | ||
4018 | // no cached module, time to make it | |
d90053e7 | 4019 | if (make_typequery(s, module) == 0) |
fb0274bc | 4020 | { |
e16dc041 | 4021 | // try to save typequery in the cache |
fb0274bc | 4022 | if (s.use_cache) |
e16dc041 | 4023 | copy_file(module, cached_module, s.verbose > 2); |
fb0274bc JS |
4024 | } |
4025 | } | |
4026 | } | |
4027 | ||
4028 | ||
c4ce66a1 JS |
4029 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
4030 | { | |
4031 | bool lvalue = is_active_lvalue(e); | |
4032 | if (lvalue && !s.guru_mode) | |
b3741c9d | 4033 | throw semantic_error(_("write to @cast context variable not permitted; need stap -g"), e->tok); |
c4ce66a1 JS |
4034 | |
4035 | if (e->module.empty()) | |
4036 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
4037 | ||
5f36109e | 4038 | functioncall* result = NULL; |
8b31197b JS |
4039 | |
4040 | // split the module string by ':' for alternatives | |
4041 | vector<string> modules; | |
4042 | tokenize(e->module, modules, ":"); | |
b5a0dd41 | 4043 | bool userspace_p=false; // PR10601 |
5f36109e | 4044 | for (unsigned i = 0; !result && i < modules.size(); ++i) |
c4ce66a1 | 4045 | { |
8b31197b | 4046 | string& module = modules[i]; |
fb0274bc | 4047 | filter_special_modules(module); |
abb41d92 | 4048 | |
c4ce66a1 JS |
4049 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
4050 | // which means that userspace modules won't get any PATH searching. | |
4051 | dwflpp* dw; | |
707bf35e JS |
4052 | try |
4053 | { | |
b5a0dd41 FCE |
4054 | userspace_p=is_user_module (module); |
4055 | if (! userspace_p) | |
707bf35e JS |
4056 | { |
4057 | // kernel or kernel module target | |
ae2552da | 4058 | dw = db.get_kern_dw(s, module); |
707bf35e JS |
4059 | } |
4060 | else | |
4061 | { | |
05fb3e0c | 4062 | module = find_executable (module, "", s.sysenv); // canonicalize it |
707bf35e JS |
4063 | dw = db.get_user_dw(s, module); |
4064 | } | |
4065 | } | |
4066 | catch (const semantic_error& er) | |
4067 | { | |
4068 | /* ignore and go to the next module */ | |
4069 | continue; | |
4070 | } | |
c4ce66a1 | 4071 | |
5f36109e | 4072 | dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result); |
51178501 | 4073 | dw->iterate_over_modules(&query_module, &q); |
c4ce66a1 | 4074 | } |
abb41d92 | 4075 | |
5f36109e | 4076 | if (!result) |
c4ce66a1 | 4077 | { |
946e1a48 JS |
4078 | // We pass the unresolved cast_op to the next pass, and hope |
4079 | // that this value ends up not being referenced after all, so | |
4080 | // it can be optimized out quietly. | |
c4ce66a1 JS |
4081 | provide (e); |
4082 | return; | |
4083 | } | |
4084 | ||
c4ce66a1 JS |
4085 | if (lvalue) |
4086 | { | |
4087 | // Provide the functioncall to our parent, so that it can be | |
4088 | // used to substitute for the assignment node immediately above | |
4089 | // us. | |
4090 | assert(!target_symbol_setter_functioncalls.empty()); | |
5f36109e | 4091 | *(target_symbol_setter_functioncalls.top()) = result; |
c4ce66a1 JS |
4092 | } |
4093 | ||
5f36109e | 4094 | result->visit (this); |
77de5e9e GH |
4095 | } |
4096 | ||
4097 | ||
b8da0ad1 FCE |
4098 | void |
4099 | dwarf_derived_probe::printsig (ostream& o) const | |
4100 | { | |
4101 | // Instead of just printing the plain locations, we add a PC value | |
4102 | // as a comment as a way of telling e.g. apart multiple inlined | |
4103 | // function instances. This is distinct from the verbose/clog | |
4104 | // output, since this part goes into the cache hash calculations. | |
4105 | sole_location()->print (o); | |
6d0f3f0c | 4106 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
4107 | printsig_nested (o); |
4108 | } | |
4109 | ||
4110 | ||
4111 | ||
dc38c0ae | 4112 | void |
b20febf3 FCE |
4113 | dwarf_derived_probe::join_group (systemtap_session& s) |
4114 | { | |
af234c40 JS |
4115 | // skip probes which are paired entry-handlers |
4116 | if (!has_return && (saved_longs || saved_strings)) | |
4117 | return; | |
4118 | ||
b20febf3 FCE |
4119 | if (! s.dwarf_derived_probes) |
4120 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
4121 | s.dwarf_derived_probes->enroll (this); | |
4122 | } | |
4123 | ||
4124 | ||
2b69faaf JS |
4125 | static bool |
4126 | kernel_supports_inode_uprobes(systemtap_session& s) | |
4127 | { | |
4128 | // The arch-supports is new to the builtin inode-uprobes, so it makes a | |
4129 | // reasonable indicator of the new API. Else we'll need an autoconf... | |
64e807c2 | 4130 | // see also buildrun.cxx:kernel_built_uprobs() |
2b69faaf JS |
4131 | return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y" |
4132 | && s.kernel_config["CONFIG_UPROBES"] == "y"); | |
4133 | } | |
4134 | ||
4135 | ||
5261f7ab DS |
4136 | void |
4137 | check_process_probe_kernel_support(systemtap_session& s) | |
4138 | { | |
4139 | // If we've got utrace, we're good to go. | |
4140 | if (s.kernel_config["CONFIG_UTRACE"] == "y") | |
4141 | return; | |
4142 | ||
8c021542 DS |
4143 | // We don't have utrace. For process probes that aren't |
4144 | // uprobes-based, we just need the task_finder. The task_finder | |
b266d318 DS |
4145 | // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a |
4146 | // specific autoconf test for its needs. | |
8c021542 DS |
4147 | // |
4148 | // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty | |
4149 | // approximation. | |
4150 | if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y") | |
4151 | return; | |
4152 | ||
d3e959b0 DS |
4153 | // For uprobes-based process probes, we need the task_finder plus |
4154 | // the builtin inode-uprobes. | |
8c021542 DS |
4155 | if (s.need_uprobes |
4156 | && s.kernel_config["CONFIG_TRACEPOINTS"] == "y" | |
d3e959b0 | 4157 | && kernel_supports_inode_uprobes(s)) |
8c021542 DS |
4158 | return; |
4159 | ||
4160 | throw semantic_error (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES")); | |
5261f7ab DS |
4161 | } |
4162 | ||
4163 | ||
b20febf3 FCE |
4164 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, |
4165 | const string& filename, | |
4166 | int line, | |
91af0778 | 4167 | // module & section specify a relocation |
b20febf3 FCE |
4168 | // base for <addr>, unless section=="" |
4169 | // (equivalently module=="kernel") | |
4170 | const string& module, | |
4171 | const string& section, | |
4172 | // NB: dwfl_addr is the virtualized | |
4173 | // address for this symbol. | |
4174 | Dwarf_Addr dwfl_addr, | |
4175 | // addr is the section-offset for | |
4176 | // actual relocation. | |
4177 | Dwarf_Addr addr, | |
4178 | dwarf_query& q, | |
37ebca01 | 4179 | Dwarf_Die* scope_die /* may be null */) |
4c5d1300 | 4180 | : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ), |
b20febf3 | 4181 | module (module), section (section), addr (addr), |
63b4fd14 | 4182 | path (q.path), |
27dc09b1 | 4183 | has_process (q.has_process), |
c9bad430 DS |
4184 | has_return (q.has_return), |
4185 | has_maxactive (q.has_maxactive), | |
c57ea854 | 4186 | has_library (q.has_library), |
6b66b9f7 | 4187 | maxactive_val (q.maxactive_val), |
b642c901 SC |
4188 | user_path (q.user_path), |
4189 | user_lib (q.user_lib), | |
af234c40 | 4190 | access_vars(false), |
c57ea854 | 4191 | saved_longs(0), saved_strings(0), |
af234c40 | 4192 | entry_handler(0) |
bd2b1e68 | 4193 | { |
b642c901 SC |
4194 | if (user_lib.size() != 0) |
4195 | has_library = true; | |
4196 | ||
6b66b9f7 JS |
4197 | if (q.has_process) |
4198 | { | |
4199 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. | |
4200 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
4201 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
4202 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
4203 | // XXX Assert invariants here too? | |
2b69faaf JS |
4204 | |
4205 | // inode-uprobes needs an offset rather than an absolute VM address. | |
4441e344 JS |
4206 | // ditto for userspace runtimes (dyninst) |
4207 | if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.is_usermode()) && | |
2b69faaf JS |
4208 | section == ".absolute" && addr == dwfl_addr && |
4209 | addr >= q.dw.module_start && addr < q.dw.module_end) | |
4210 | this->addr = addr - q.dw.module_start; | |
6b66b9f7 JS |
4211 | } |
4212 | else | |
4213 | { | |
4214 | // Assert kernel relocation invariants | |
4215 | if (section == "" && dwfl_addr != addr) // addr should be absolute | |
ce0f6648 | 4216 | throw semantic_error (_("missing relocation basis"), tok); |
6b66b9f7 | 4217 | if (section != "" && dwfl_addr == addr) // addr should be an offset |
b530b5b3 | 4218 | throw semantic_error (_("inconsistent relocation address"), tok); |
6b66b9f7 | 4219 | } |
2930abc7 | 4220 | |
21beacc9 FCE |
4221 | // XXX: hack for strange g++/gcc's |
4222 | #ifndef USHRT_MAX | |
4223 | #define USHRT_MAX 32767 | |
4224 | #endif | |
4225 | ||
606fd9c8 | 4226 | // Range limit maxactive() value |
6b66b9f7 | 4227 | if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX)) |
b530b5b3 LB |
4228 | throw semantic_error (_F("maxactive value out of range [0,%s]", |
4229 | lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok); | |
606fd9c8 | 4230 | |
de688825 | 4231 | // Expand target variables in the probe body |
5f0a03a6 | 4232 | if (!null_die(scope_die)) |
8fc05e57 | 4233 | { |
6b66b9f7 | 4234 | // XXX: user-space deref's for q.has_process! |
de688825 | 4235 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
8b095b45 | 4236 | v.replace (this->body); |
6b66b9f7 JS |
4237 | if (!q.has_process) |
4238 | access_vars = v.visited; | |
37ebca01 FCE |
4239 | |
4240 | // If during target-variable-expanding the probe, we added a new block | |
4241 | // of code, add it to the start of the probe. | |
4242 | if (v.add_block) | |
ba6f838d | 4243 | this->body = new block(v.add_block, this->body); |
2260f4e3 FCE |
4244 | |
4245 | // If when target-variable-expanding the probe, we need to synthesize a | |
4246 | // sibling function-entry probe. We don't go through the whole probe derivation | |
4247 | // business (PR10642) that could lead to wildcard/alias resolution, or for that | |
4248 | // dwarf-induced duplication. | |
4249 | if (v.add_call_probe) | |
37ebca01 | 4250 | { |
2260f4e3 FCE |
4251 | assert (q.has_return && !q.has_call); |
4252 | ||
4253 | // We temporarily replace q.base_probe. | |
4254 | statement* old_body = q.base_probe->body; | |
4255 | q.base_probe->body = v.add_call_probe; | |
4256 | q.has_return = false; | |
4257 | q.has_call = true; | |
af234c40 | 4258 | |
da23eceb | 4259 | if (q.has_process) |
af234c40 JS |
4260 | entry_handler = new uprobe_derived_probe (funcname, filename, line, |
4261 | module, section, dwfl_addr, | |
4262 | addr, q, scope_die); | |
da23eceb | 4263 | else |
af234c40 JS |
4264 | entry_handler = new dwarf_derived_probe (funcname, filename, line, |
4265 | module, section, dwfl_addr, | |
4266 | addr, q, scope_die); | |
4267 | ||
4268 | saved_longs = entry_handler->saved_longs = v.saved_longs; | |
4269 | saved_strings = entry_handler->saved_strings = v.saved_strings; | |
4270 | ||
4271 | q.results.push_back (entry_handler); | |
2260f4e3 FCE |
4272 | |
4273 | q.has_return = true; | |
4274 | q.has_call = false; | |
4275 | q.base_probe->body = old_body; | |
37ebca01 | 4276 | } |
f10534c6 WH |
4277 | // Save the local variables for listing mode |
4278 | if (q.sess.listing_mode_vars) | |
8c67c337 | 4279 | saveargs(q, scope_die, dwfl_addr); |
8fc05e57 | 4280 | } |
37ebca01 | 4281 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 4282 | |
f10534c6 | 4283 | // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs |
0a98fd42 | 4284 | |
5d23847d | 4285 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
4286 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
4287 | // point. This allows a user to see what function / file / line | |
4288 | // number any particular match of the wildcards. | |
2930abc7 | 4289 | |
a229fcd7 | 4290 | vector<probe_point::component*> comps; |
91af0778 FCE |
4291 | if (q.has_kernel) |
4292 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
4293 | else if(q.has_module) | |
4294 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
4295 | else if(q.has_process) | |
4296 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
4297 | else | |
4298 | assert (0); | |
b5d77020 | 4299 | |
db520b00 FCE |
4300 | string fn_or_stmt; |
4301 | if (q.has_function_str || q.has_function_num) | |
4302 | fn_or_stmt = "function"; | |
4303 | else | |
4304 | fn_or_stmt = "statement"; | |
a229fcd7 | 4305 | |
b8da0ad1 | 4306 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 4307 | { |
4cd232e4 | 4308 | string retro_name = funcname; |
b20febf3 | 4309 | if (filename != "") |
cee35f73 | 4310 | { |
fb84c077 | 4311 | retro_name += ("@" + string (filename)); |
cee35f73 | 4312 | if (line > 0) |
aca66a36 | 4313 | retro_name += (":" + lex_cast (line)); |
cee35f73 | 4314 | } |
db520b00 FCE |
4315 | comps.push_back |
4316 | (new probe_point::component | |
4317 | (fn_or_stmt, new literal_string (retro_name))); | |
4318 | } | |
b8da0ad1 | 4319 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
4320 | { |
4321 | Dwarf_Addr retro_addr; | |
4322 | if (q.has_function_num) | |
4323 | retro_addr = q.function_num_val; | |
4324 | else | |
4325 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
4326 | comps.push_back (new probe_point::component |
4327 | (fn_or_stmt, | |
9ea68eb9 | 4328 | new literal_number(retro_addr, true))); |
37ebca01 FCE |
4329 | |
4330 | if (q.has_absolute) | |
4331 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
4332 | } |
4333 | ||
b8da0ad1 FCE |
4334 | if (q.has_call) |
4335 | comps.push_back (new probe_point::component(TOK_CALL)); | |
4bda987e SC |
4336 | if (q.has_exported) |
4337 | comps.push_back (new probe_point::component(TOK_EXPORTED)); | |
b8da0ad1 FCE |
4338 | if (q.has_inline) |
4339 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 4340 | if (has_return) |
b8da0ad1 FCE |
4341 | comps.push_back (new probe_point::component(TOK_RETURN)); |
4342 | if (has_maxactive) | |
4343 | comps.push_back (new probe_point::component | |
4344 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 4345 | |
5d23847d FCE |
4346 | // Overwrite it. |
4347 | this->sole_location()->components = comps; | |
2930abc7 FCE |
4348 | } |
4349 | ||
bd2b1e68 | 4350 | |
0a98fd42 | 4351 | void |
8c67c337 JS |
4352 | dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die, |
4353 | Dwarf_Addr dwfl_addr) | |
0a98fd42 | 4354 | { |
9aa8ffce | 4355 | if (null_die(scope_die)) |
0a98fd42 | 4356 | return; |
0a98fd42 | 4357 | |
8c67c337 | 4358 | bool verbose = q.sess.verbose > 2; |
0a98fd42 | 4359 | |
8c67c337 | 4360 | if (verbose) |
b530b5b3 | 4361 | clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die)); |
0a98fd42 | 4362 | |
8c67c337 JS |
4363 | if (has_return) |
4364 | { | |
4365 | /* Only save the return value if it has a type. */ | |
4366 | string type_name; | |
4367 | Dwarf_Die type_die; | |
4368 | if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) && | |
4369 | dwarf_type_name(&type_die, type_name)) | |
4370 | args.push_back("$return:"+type_name); | |
4371 | ||
4372 | else if (verbose) | |
b530b5b3 LB |
4373 | clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n", |
4374 | lex_cast_hex(dwarf_dieoffset(scope_die)).c_str()); | |
8c67c337 | 4375 | } |
d87623a1 | 4376 | |
0a98fd42 | 4377 | Dwarf_Die arg; |
4ef35696 JS |
4378 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
4379 | for (unsigned i = 0; i < scopes.size(); ++i) | |
4380 | { | |
4381 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
4382 | break; // we don't want file-level variables | |
4383 | if (dwarf_child (&scopes[i], &arg) == 0) | |
4384 | do | |
0a98fd42 | 4385 | { |
4ef35696 JS |
4386 | switch (dwarf_tag (&arg)) |
4387 | { | |
4388 | case DW_TAG_variable: | |
4389 | case DW_TAG_formal_parameter: | |
4390 | break; | |
0a98fd42 | 4391 | |
4ef35696 JS |
4392 | default: |
4393 | continue; | |
4394 | } | |
0a98fd42 | 4395 | |
4ef35696 JS |
4396 | /* Ignore this local if it has no name. */ |
4397 | const char *arg_name = dwarf_diename (&arg); | |
4398 | if (!arg_name) | |
8c67c337 JS |
4399 | { |
4400 | if (verbose) | |
b530b5b3 LB |
4401 | clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n", |
4402 | lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4403 | continue; |
4404 | } | |
4ef35696 JS |
4405 | |
4406 | if (verbose) | |
b530b5b3 LB |
4407 | clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n", |
4408 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4409 | |
4410 | /* Ignore this local if it has no location (or not at this PC). */ | |
4411 | /* NB: It still may not be directly accessible, e.g. if it is an | |
4412 | * aggregate type, implicit_pointer, etc., but the user can later | |
4413 | * figure out how to access the interesting parts. */ | |
4414 | Dwarf_Attribute attr_mem; | |
4415 | if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem)) | |
4416 | { | |
4417 | Dwarf_Op *expr; | |
4418 | size_t len; | |
4419 | if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem)) | |
4420 | { | |
4421 | if (verbose) | |
b530b5b3 LB |
4422 | clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n", |
4423 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4424 | continue; |
4425 | } | |
4426 | else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr, | |
4427 | &len, 1) == 1 && len > 0)) | |
4428 | { | |
4429 | if (verbose) | |
b530b5b3 LB |
4430 | clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n", |
4431 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str()); | |
4ef35696 JS |
4432 | continue; |
4433 | } | |
4434 | } | |
4435 | ||
4436 | /* Ignore this local if it has no type. */ | |
4437 | string type_name; | |
4438 | Dwarf_Die type_die; | |
4439 | if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) || | |
4440 | !dwarf_type_name(&type_die, type_name)) | |
8c67c337 JS |
4441 | { |
4442 | if (verbose) | |
b530b5b3 LB |
4443 | clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n", |
4444 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4445 | continue; |
4446 | } | |
8c67c337 | 4447 | |
4ef35696 JS |
4448 | /* This local looks good -- save it! */ |
4449 | args.push_back("$"+string(arg_name)+":"+type_name); | |
8c67c337 | 4450 | } |
4ef35696 JS |
4451 | while (dwarf_siblingof (&arg, &arg) == 0); |
4452 | } | |
0a98fd42 JS |
4453 | } |
4454 | ||
4455 | ||
4456 | void | |
d0bfd2ac | 4457 | dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const |
0a98fd42 | 4458 | { |
d0bfd2ac | 4459 | arg_set.insert(arg_set.end(), args.begin(), args.end()); |
0a98fd42 JS |
4460 | } |
4461 | ||
4462 | ||
27dc09b1 | 4463 | void |
42e38653 | 4464 | dwarf_derived_probe::emit_privilege_assertion (translator_output* o) |
27dc09b1 DB |
4465 | { |
4466 | if (has_process) | |
4467 | { | |
4468 | // These probes are allowed for unprivileged users, but only in the | |
4469 | // context of processes which they own. | |
4470 | emit_process_owner_assertion (o); | |
4471 | return; | |
4472 | } | |
4473 | ||
4474 | // Other probes must contain the default assertion which aborts | |
4475 | // if executed by an unprivileged user. | |
42e38653 | 4476 | derived_probe::emit_privilege_assertion (o); |
27dc09b1 DB |
4477 | } |
4478 | ||
4479 | ||
4480 | void | |
4481 | dwarf_derived_probe::print_dupe_stamp(ostream& o) | |
4482 | { | |
4483 | if (has_process) | |
4484 | { | |
4485 | // These probes are allowed for unprivileged users, but only in the | |
4486 | // context of processes which they own. | |
4487 | print_dupe_stamp_unprivileged_process_owner (o); | |
4488 | return; | |
4489 | } | |
4490 | ||
4491 | // Other probes must contain the default dupe stamp | |
4492 | derived_probe::print_dupe_stamp (o); | |
4493 | } | |
4494 | ||
64211010 | 4495 | |
7a053d3b | 4496 | void |
20c6c071 | 4497 | dwarf_derived_probe::register_statement_variants(match_node * root, |
27dc09b1 | 4498 | dwarf_builder * dw, |
42e38653 | 4499 | privilege_t privilege) |
bd2b1e68 | 4500 | { |
27dc09b1 | 4501 | root |
42e38653 | 4502 | ->bind_privilege(privilege) |
27dc09b1 | 4503 | ->bind(dw); |
54efe513 GH |
4504 | } |
4505 | ||
7a053d3b | 4506 | void |
fd6602a0 | 4507 | dwarf_derived_probe::register_function_variants(match_node * root, |
27dc09b1 | 4508 | dwarf_builder * dw, |
42e38653 | 4509 | privilege_t privilege) |
2865d17a | 4510 | { |
27dc09b1 | 4511 | root |
42e38653 | 4512 | ->bind_privilege(privilege) |
27dc09b1 | 4513 | ->bind(dw); |
27dc09b1 | 4514 | root->bind(TOK_CALL) |
42e38653 | 4515 | ->bind_privilege(privilege) |
27dc09b1 | 4516 | ->bind(dw); |
4bda987e SC |
4517 | root->bind(TOK_EXPORTED) |
4518 | ->bind_privilege(privilege) | |
4519 | ->bind(dw); | |
27dc09b1 | 4520 | root->bind(TOK_RETURN) |
42e38653 | 4521 | ->bind_privilege(privilege) |
27dc09b1 | 4522 | ->bind(dw); |
1e035395 | 4523 | |
f6be7c06 DB |
4524 | // For process probes / uprobes, .maxactive() is unused. |
4525 | if (! pr_contains (privilege, pr_stapusr)) | |
1e035395 FCE |
4526 | { |
4527 | root->bind(TOK_RETURN) | |
1e035395 FCE |
4528 | ->bind_num(TOK_MAXACTIVE)->bind(dw); |
4529 | } | |
bd2b1e68 GH |
4530 | } |
4531 | ||
7a053d3b | 4532 | void |
27dc09b1 | 4533 | dwarf_derived_probe::register_function_and_statement_variants( |
440d9b00 | 4534 | systemtap_session& s, |
27dc09b1 DB |
4535 | match_node * root, |
4536 | dwarf_builder * dw, | |
42e38653 | 4537 | privilege_t privilege |
27dc09b1 | 4538 | ) |
bd2b1e68 GH |
4539 | { |
4540 | // Here we match 4 forms: | |
4541 | // | |
4542 | // .function("foo") | |
4543 | // .function(0xdeadbeef) | |
4544 | // .statement("foo") | |
4545 | // .statement(0xdeadbeef) | |
4546 | ||
440d9b00 | 4547 | match_node *fv_root = root->bind_str(TOK_FUNCTION); |
42e38653 | 4548 | register_function_variants(fv_root, dw, privilege); |
7f02ca94 | 4549 | // ROOT.function("STRING") always gets the .inline and .label variants. |
440d9b00 | 4550 | fv_root->bind(TOK_INLINE) |
42e38653 | 4551 | ->bind_privilege(privilege) |
440d9b00 | 4552 | ->bind(dw); |
7f02ca94 JS |
4553 | fv_root->bind_str(TOK_LABEL) |
4554 | ->bind_privilege(privilege) | |
440d9b00 DB |
4555 | ->bind(dw); |
4556 | ||
4557 | fv_root = root->bind_num(TOK_FUNCTION); | |
42e38653 | 4558 | register_function_variants(fv_root, dw, privilege); |
440d9b00 DB |
4559 | // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter. |
4560 | if (strverscmp(s.compatible.c_str(), "1.7") <= 0) | |
4561 | { | |
4562 | fv_root->bind(TOK_INLINE) | |
42e38653 | 4563 | ->bind_privilege(privilege) |
440d9b00 DB |
4564 | ->bind(dw); |
4565 | } | |
4566 | ||
42e38653 DB |
4567 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege); |
4568 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege); | |
bd2b1e68 GH |
4569 | } |
4570 | ||
b1615c74 JS |
4571 | void |
4572 | dwarf_derived_probe::register_sdt_variants(systemtap_session& s, | |
4573 | match_node * root, | |
4574 | dwarf_builder * dw) | |
4575 | { | |
4576 | root->bind_str(TOK_MARK) | |
f66bb29a | 4577 | ->bind_privilege(pr_all) |
b1615c74 JS |
4578 | ->bind(dw); |
4579 | root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) | |
f66bb29a | 4580 | ->bind_privilege(pr_all) |
b1615c74 JS |
4581 | ->bind(dw); |
4582 | } | |
4583 | ||
4584 | void | |
4585 | dwarf_derived_probe::register_plt_variants(systemtap_session& s, | |
4586 | match_node * root, | |
4587 | dwarf_builder * dw) | |
4588 | { | |
4589 | root->bind(TOK_PLT) | |
f66bb29a | 4590 | ->bind_privilege(pr_all) |
b1615c74 JS |
4591 | ->bind(dw); |
4592 | root->bind_str(TOK_PLT) | |
f66bb29a | 4593 | ->bind_privilege(pr_all) |
b1615c74 JS |
4594 | ->bind(dw); |
4595 | root->bind(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 4596 | ->bind_privilege(pr_all) |
b1615c74 JS |
4597 | ->bind(dw); |
4598 | root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 4599 | ->bind_privilege(pr_all) |
b1615c74 | 4600 | ->bind(dw); |
bd2b1e68 GH |
4601 | } |
4602 | ||
4603 | void | |
c4ce66a1 | 4604 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 4605 | { |
c4ce66a1 | 4606 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
4607 | dwarf_builder *dw = new dwarf_builder(); |
4608 | ||
c4ce66a1 JS |
4609 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
4610 | s.code_filters.push_back(filter); | |
4611 | ||
73f52eb4 DB |
4612 | register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged); |
4613 | register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged); | |
27dc09b1 DB |
4614 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) |
4615 | ->bind(dw); | |
2cab6244 | 4616 | |
7f02ca94 JS |
4617 | match_node* uprobes[] = { |
4618 | root->bind(TOK_PROCESS), | |
4619 | root->bind_str(TOK_PROCESS), | |
4620 | root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
4621 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
4622 | }; | |
4623 | for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i) | |
4624 | { | |
f66bb29a | 4625 | register_function_and_statement_variants(s, uprobes[i], dw, pr_all); |
7f02ca94 JS |
4626 | register_sdt_variants(s, uprobes[i], dw); |
4627 | register_plt_variants(s, uprobes[i], dw); | |
4628 | } | |
bd2b1e68 GH |
4629 | } |
4630 | ||
9020300d FCE |
4631 | void |
4632 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
4633 | { | |
b95e2b79 MH |
4634 | if (access_vars) |
4635 | { | |
4636 | // if accessing $variables, emit bsp cache setup for speeding up | |
d4670309 | 4637 | o->newline() << "#if defined __ia64__"; |
d9aed31e | 4638 | o->newline() << "bspcache(c->unwaddr, c->kregs);"; |
d4670309 | 4639 | o->newline() << "#endif"; |
b95e2b79 | 4640 | } |
9020300d | 4641 | } |
2930abc7 | 4642 | |
b20febf3 | 4643 | // ------------------------------------------------------------------------ |
46b84a80 DS |
4644 | |
4645 | void | |
b20febf3 | 4646 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 4647 | { |
b20febf3 | 4648 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
4649 | |
4650 | // XXX: probes put at the same address should all share a | |
4651 | // single kprobe/kretprobe, and have their handlers executed | |
4652 | // sequentially. | |
b55bc428 FCE |
4653 | } |
4654 | ||
7a053d3b | 4655 | void |
775d51e5 | 4656 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 4657 | { |
b20febf3 | 4658 | if (probes_by_module.empty()) return; |
2930abc7 | 4659 | |
775d51e5 DS |
4660 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
4661 | ||
4662 | // Warn of misconfigured kernels | |
f41595cc FCE |
4663 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
4664 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
4665 | s.op->newline() << "#endif"; | |
775d51e5 | 4666 | s.op->newline(); |
f41595cc | 4667 | |
f07c3b68 | 4668 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 4669 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
4670 | s.op->newline() << "#endif"; |
4671 | ||
14cf7e42 | 4672 | // Forward decls |
2ba1736a | 4673 | s.op->newline() << "#include \"linux/kprobes-common.h\""; |
14cf7e42 | 4674 | |
b20febf3 FCE |
4675 | // Forward declare the master entry functions |
4676 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4677 | s.op->line() << " struct pt_regs *regs);"; | |
4678 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4679 | s.op->line() << " struct pt_regs *regs);"; | |
4680 | ||
42cb22bd MH |
4681 | // Emit an array of kprobe/kretprobe pointers |
4682 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4683 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
4684 | s.op->newline() << "#endif"; | |
4685 | ||
b20febf3 | 4686 | // Emit the actual probe list. |
606fd9c8 FCE |
4687 | |
4688 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
4689 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
4690 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
14cf7e42 | 4691 | s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
606fd9c8 FCE |
4692 | // NB: bss! |
4693 | ||
4c2732a1 | 4694 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
4695 | s.op->newline(1) << "const unsigned return_p:1;"; |
4696 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 4697 | s.op->newline() << "const unsigned optional_p:1;"; |
b20febf3 | 4698 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 4699 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 | 4700 | |
af234c40 JS |
4701 | // data saved in the kretprobe_instance packet |
4702 | s.op->newline() << "const unsigned short saved_longs;"; | |
4703 | s.op->newline() << "const unsigned short saved_strings;"; | |
4704 | ||
faea5e16 | 4705 | // Let's find some stats for the embedded strings. Maybe they |
606fd9c8 FCE |
4706 | // are small and uniform enough to justify putting char[MAX]'s into |
4707 | // the array instead of relocated char*'s. | |
faea5e16 JS |
4708 | size_t module_name_max = 0, section_name_max = 0; |
4709 | size_t module_name_tot = 0, section_name_tot = 0; | |
606fd9c8 FCE |
4710 | size_t all_name_cnt = probes_by_module.size(); // for average |
4711 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4712 | { | |
4713 | dwarf_derived_probe* p = it->second; | |
4714 | #define DOIT(var,expr) do { \ | |
4715 | size_t var##_size = (expr) + 1; \ | |
4716 | var##_max = max (var##_max, var##_size); \ | |
4717 | var##_tot += var##_size; } while (0) | |
4718 | DOIT(module_name, p->module.size()); | |
4719 | DOIT(section_name, p->section.size()); | |
606fd9c8 FCE |
4720 | #undef DOIT |
4721 | } | |
4722 | ||
4723 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
4724 | // the amount of average waste (max - avg) to the relocation data size | |
4725 | // (3 native long words). | |
4726 | #define CALCIT(var) \ | |
4727 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
4728 | { \ | |
4729 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
4730 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
4731 | << "[" << var##_name_max << "]" << endl; \ | |
4732 | } \ | |
4733 | else \ | |
4734 | { \ | |
b0986e7a | 4735 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
4736 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
4737 | } | |
4738 | ||
4739 | CALCIT(module); | |
4740 | CALCIT(section); | |
e6fe60e7 | 4741 | #undef CALCIT |
606fd9c8 | 4742 | |
b0986e7a | 4743 | s.op->newline() << "const unsigned long address;"; |
26e63673 | 4744 | s.op->newline() << "struct stap_probe * const probe;"; |
c87ae2c1 | 4745 | s.op->newline() << "struct stap_probe * const entry_probe;"; |
b20febf3 FCE |
4746 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
4747 | s.op->indent(1); | |
4748 | ||
4749 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 4750 | { |
b20febf3 FCE |
4751 | dwarf_derived_probe* p = it->second; |
4752 | s.op->newline() << "{"; | |
4753 | if (p->has_return) | |
4754 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 4755 | if (p->has_maxactive) |
606fd9c8 FCE |
4756 | { |
4757 | s.op->line() << " .maxactive_p=1,"; | |
4758 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
4759 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
4760 | } | |
af234c40 JS |
4761 | if (p->saved_longs || p->saved_strings) |
4762 | { | |
4763 | if (p->saved_longs) | |
4764 | s.op->line() << " .saved_longs=" << p->saved_longs << ","; | |
4765 | if (p->saved_strings) | |
4766 | s.op->line() << " .saved_strings=" << p->saved_strings << ","; | |
4767 | if (p->entry_handler) | |
c87ae2c1 | 4768 | s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ","; |
af234c40 | 4769 | } |
b350f56b JS |
4770 | if (p->locations[0]->optional) |
4771 | s.op->line() << " .optional_p=1,"; | |
dc38c256 | 4772 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
4773 | s.op->line() << " .module=\"" << p->module << "\","; |
4774 | s.op->line() << " .section=\"" << p->section << "\","; | |
faea5e16 | 4775 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
b20febf3 | 4776 | s.op->line() << " },"; |
2930abc7 | 4777 | } |
2930abc7 | 4778 | |
b20febf3 FCE |
4779 | s.op->newline(-1) << "};"; |
4780 | ||
4781 | // Emit the kprobes callback function | |
4782 | s.op->newline(); | |
4783 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4784 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
4785 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
4786 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
4787 | // Check that the index is plausible | |
4788 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4789 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4790 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4791 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4792 | s.op->line() << "];"; | |
6eefe942 MW |
4793 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe", |
4794 | "_STP_PROBE_HANDLER_KPROBE"); | |
d9aed31e | 4795 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
4796 | |
4797 | // Make it look like the IP is set as it wouldn't have been replaced | |
4798 | // by a breakpoint instruction when calling real probe handler. Reset | |
4799 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
4800 | s.op->newline() << "{"; | |
4801 | s.op->indent(1); | |
d9aed31e | 4802 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 4803 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 4804 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 4805 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4806 | s.op->newline(-1) << "}"; |
4807 | ||
7baf48e9 | 4808 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
b20febf3 FCE |
4809 | s.op->newline() << "return 0;"; |
4810 | s.op->newline(-1) << "}"; | |
4811 | ||
4812 | // Same for kretprobes | |
4813 | s.op->newline(); | |
af234c40 JS |
4814 | s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,"; |
4815 | s.op->line() << " struct pt_regs *regs, int entry) {"; | |
b20febf3 | 4816 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; |
606fd9c8 FCE |
4817 | |
4818 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 4819 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
4820 | // Check that the index is plausible |
4821 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4822 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4823 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4824 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4825 | s.op->line() << "];"; | |
4826 | ||
c87ae2c1 JS |
4827 | s.op->newline() << "struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;"; |
4828 | s.op->newline() << "if (sp) {"; | |
4829 | s.op->indent(1); | |
6eefe942 MW |
4830 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sp", |
4831 | "_STP_PROBE_HANDLER_KRETPROBE"); | |
d9aed31e | 4832 | s.op->newline() << "c->kregs = regs;"; |
af234c40 JS |
4833 | |
4834 | // for assisting runtime's backtrace logic and accessing kretprobe data packets | |
6dceb5c9 MW |
4835 | s.op->newline() << "c->ips.krp.pi = inst;"; |
4836 | s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;"; | |
6415ddde MW |
4837 | |
4838 | // Make it look like the IP is set as it wouldn't have been replaced | |
4839 | // by a breakpoint instruction when calling real probe handler. Reset | |
4840 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
4841 | s.op->newline() << "{"; | |
d9aed31e | 4842 | s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
c87ae2c1 JS |
4843 | s.op->newline() << "if (entry)"; |
4844 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; | |
4845 | s.op->newline(-1) << "else"; | |
4846 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);"; | |
4847 | s.op->newline(-1) << "(sp->ph) (c);"; | |
259d54c0 | 4848 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4849 | s.op->newline(-1) << "}"; |
4850 | ||
7baf48e9 | 4851 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
c87ae2c1 | 4852 | s.op->newline(-1) << "}"; |
b20febf3 FCE |
4853 | s.op->newline() << "return 0;"; |
4854 | s.op->newline(-1) << "}"; | |
af234c40 JS |
4855 | |
4856 | s.op->newline(); | |
4857 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4858 | s.op->line() << " struct pt_regs *regs) {"; | |
4859 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);"; | |
4860 | s.op->newline(-1) << "}"; | |
4861 | ||
4862 | s.op->newline(); | |
4863 | s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,"; | |
4864 | s.op->line() << " struct pt_regs *regs) {"; | |
4865 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);"; | |
4866 | s.op->newline(-1) << "}"; | |
b642c901 | 4867 | |
14cf7e42 | 4868 | s.op->newline(); |
20c6c071 | 4869 | } |
ec4373ff | 4870 | |
20c6c071 | 4871 | |
dc38c0ae | 4872 | void |
b20febf3 FCE |
4873 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
4874 | { | |
4875 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4876 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 4877 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
a049e342 | 4878 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 4879 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
26e63673 | 4880 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b20febf3 | 4881 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 4882 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 4883 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 4884 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 4885 | s.op->newline(-1) << "} else {"; |
f07c3b68 | 4886 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
c9bad430 | 4887 | s.op->newline(-1) << "}"; |
606fd9c8 | 4888 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
af234c40 | 4889 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; |
c87ae2c1 | 4890 | s.op->newline() << "if (sdp->entry_probe) {"; |
af234c40 JS |
4891 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; |
4892 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
4893 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
4894 | s.op->newline(-1) << "}"; | |
4895 | s.op->newline() << "#endif"; | |
e4cb375f MH |
4896 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
4897 | s.op->newline() << "#ifdef __ia64__"; | |
4898 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
4899 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4900 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4901 | s.op->newline() << "if (rc == 0) {"; | |
4902 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
4903 | s.op->newline() << "if (rc != 0)"; | |
4904 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4905 | s.op->newline(-2) << "}"; | |
4906 | s.op->newline() << "#else"; | |
606fd9c8 | 4907 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 4908 | s.op->newline() << "#endif"; |
b20febf3 | 4909 | s.op->newline(-1) << "} else {"; |
e4cb375f | 4910 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
4911 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
4912 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
4913 | s.op->newline() << "#ifdef __ia64__"; |
4914 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
4915 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4916 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4917 | s.op->newline() << "if (rc == 0) {"; | |
4918 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
4919 | s.op->newline() << "if (rc != 0)"; | |
4920 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4921 | s.op->newline(-2) << "}"; | |
4922 | s.op->newline() << "#else"; | |
606fd9c8 | 4923 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 4924 | s.op->newline() << "#endif"; |
b20febf3 | 4925 | s.op->newline(-1) << "}"; |
9063462a FCE |
4926 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
4927 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 4928 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 4929 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);"; |
b350f56b | 4930 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
9063462a FCE |
4931 | // XXX: shall we increment numskipped? |
4932 | s.op->newline(-1) << "}"; | |
4933 | ||
4934 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 4935 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 4936 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
4937 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
4938 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
4939 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
4940 | s.op->newline() << "#ifdef __ia64__"; |
4941 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
4942 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
4943 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
4944 | // not run for this early-abort case. | |
4945 | s.op->newline(-1) << "}"; | |
4946 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 4947 | s.op->newline(-1) << "}"; |
9063462a FCE |
4948 | #endif |
4949 | ||
b20febf3 FCE |
4950 | s.op->newline() << "else sdp->registered_p = 1;"; |
4951 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
4952 | } |
4953 | ||
4954 | ||
b4be7cbc FCE |
4955 | void |
4956 | dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s) | |
4957 | { | |
4958 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4959 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
4960 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
4961 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; | |
4962 | s.op->newline() << "int rc;"; | |
4963 | ||
4964 | // new module arrived? | |
4965 | s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {"; | |
4966 | s.op->newline(1) << "if (sdp->return_p) {"; | |
4967 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; | |
4968 | s.op->newline() << "if (sdp->maxactive_p) {"; | |
4969 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
4970 | s.op->newline(-1) << "} else {"; | |
4971 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; | |
4972 | s.op->newline(-1) << "}"; | |
4973 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; | |
4974 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; | |
4975 | s.op->newline() << "if (sdp->entry_probe) {"; | |
4976 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; | |
4977 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
4978 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
4979 | s.op->newline(-1) << "}"; | |
4980 | s.op->newline() << "#endif"; | |
4981 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
4982 | s.op->newline() << "#ifdef __ia64__"; | |
4983 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
4984 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4985 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4986 | s.op->newline() << "if (rc == 0) {"; | |
4987 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
4988 | s.op->newline() << "if (rc != 0)"; | |
4989 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4990 | s.op->newline(-2) << "}"; | |
4991 | s.op->newline() << "#else"; | |
4992 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
4993 | s.op->newline() << "#endif"; | |
4994 | s.op->newline(-1) << "} else {"; | |
4995 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
4996 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; | |
4997 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
4998 | s.op->newline() << "#ifdef __ia64__"; | |
4999 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5000 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5001 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5002 | s.op->newline() << "if (rc == 0) {"; | |
5003 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5004 | s.op->newline() << "if (rc != 0)"; | |
5005 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5006 | s.op->newline(-2) << "}"; | |
5007 | s.op->newline() << "#else"; | |
5008 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
5009 | s.op->newline() << "#endif"; | |
5010 | s.op->newline(-1) << "}"; | |
5011 | s.op->newline() << "if (rc == 0) sdp->registered_p = 1;"; | |
5012 | ||
5013 | // old module disappeared? | |
5014 | s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {"; | |
5015 | s.op->newline(1) << "if (sdp->return_p) {"; | |
5016 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
5017 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; | |
5018 | s.op->newline() << "#ifdef STP_TIMING"; | |
5019 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
5020 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; | |
5021 | s.op->newline(-1) << "#endif"; | |
5022 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; | |
5023 | s.op->newline() << "#ifdef STP_TIMING"; | |
5024 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
5025 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; | |
5026 | s.op->newline(-1) << "#endif"; | |
5027 | s.op->newline(-1) << "} else {"; | |
5028 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
5029 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; | |
5030 | s.op->newline() << "#ifdef STP_TIMING"; | |
5031 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
5032 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; | |
5033 | s.op->newline(-1) << "#endif"; | |
5034 | s.op->newline(-1) << "}"; | |
5035 | s.op->newline() << "#if defined(__ia64__)"; | |
5036 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5037 | s.op->newline() << "#endif"; | |
5038 | s.op->newline() << "sdp->registered_p = 0;"; | |
5039 | s.op->newline(-1) << "}"; | |
5040 | ||
5041 | s.op->newline(-1) << "}"; // for loop | |
5042 | } | |
5043 | ||
5044 | ||
5045 | ||
5046 | ||
46b84a80 | 5047 | void |
b20febf3 | 5048 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 5049 | { |
42cb22bd MH |
5050 | //Unregister kprobes by batch interfaces. |
5051 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5052 | s.op->newline() << "j = 0;"; | |
5053 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5054 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5055 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5056 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5057 | s.op->newline() << "if (!sdp->return_p)"; | |
5058 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
5059 | s.op->newline(-2) << "}"; | |
5060 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5061 | s.op->newline() << "j = 0;"; | |
5062 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5063 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5064 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5065 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5066 | s.op->newline() << "if (sdp->return_p)"; | |
5067 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
5068 | s.op->newline(-2) << "}"; | |
5069 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
5070 | s.op->newline() << "#ifdef __ia64__"; |
5071 | s.op->newline() << "j = 0;"; | |
5072 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5073 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5074 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5075 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5076 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
5077 | s.op->newline(-1) << "}"; | |
5078 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5079 | s.op->newline() << "#endif"; | |
42cb22bd MH |
5080 | s.op->newline() << "#endif"; |
5081 | ||
b20febf3 FCE |
5082 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
5083 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5084 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
5085 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
5086 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 5087 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5088 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 5089 | s.op->newline() << "#endif"; |
606fd9c8 | 5090 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; |
73209876 FCE |
5091 | s.op->newline() << "#ifdef STP_TIMING"; |
5092 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 5093 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
73209876 | 5094 | s.op->newline(-1) << "#endif"; |
606fd9c8 | 5095 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5096 | s.op->newline() << "#ifdef STP_TIMING"; |
5097 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 5098 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; |
73209876 | 5099 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 5100 | s.op->newline(-1) << "} else {"; |
42cb22bd | 5101 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5102 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 5103 | s.op->newline() << "#endif"; |
606fd9c8 | 5104 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5105 | s.op->newline() << "#ifdef STP_TIMING"; |
5106 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 5107 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
73209876 | 5108 | s.op->newline(-1) << "#endif"; |
b20febf3 | 5109 | s.op->newline(-1) << "}"; |
e4cb375f MH |
5110 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
5111 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5112 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
5113 | s.op->newline() << "sdp->registered_p = 0;"; |
5114 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
5115 | } |
5116 | ||
272c9036 WF |
5117 | static void sdt_v3_tokenize(const string& str, vector<string>& tokens) |
5118 | { | |
5119 | string::size_type pos; | |
5120 | string::size_type lastPos = str.find_first_not_of(" ", 0); | |
5121 | string::size_type nextAt = str.find("@", lastPos); | |
e9a90eee JS |
5122 | |
5123 | if (nextAt == string::npos) | |
5124 | { | |
5125 | // PR13934: Assembly probes are not forced to use the N@OP form. | |
5126 | // In this case, N is inferred to be the native word size. Since we | |
5127 | // don't have a nice delimiter, just split it on spaces. SDT-asm authors | |
5128 | // then must not put any spaces in arguments, to avoid ambiguity. | |
5129 | tokenize(str, tokens, " "); | |
5130 | return; | |
5131 | } | |
5132 | ||
272c9036 WF |
5133 | while (lastPos != string::npos) |
5134 | { | |
5135 | pos = nextAt + 1; | |
5136 | nextAt = str.find("@", pos); | |
5137 | if (nextAt == string::npos) | |
5138 | pos = string::npos; | |
5139 | else | |
5140 | pos = str.rfind(" ", nextAt); | |
5141 | ||
5142 | tokens.push_back(str.substr(lastPos, pos - lastPos)); | |
5143 | lastPos = str.find_first_not_of(" ", pos); | |
5144 | } | |
5145 | } | |
8aabf152 | 5146 | |
8aabf152 | 5147 | |
aff5d390 | 5148 | struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor |
7a05f484 | 5149 | { |
ae1418f0 | 5150 | enum regwidths {QI, QIh, HI, SI, DI}; |
f83336a5 FCE |
5151 | sdt_uprobe_var_expanding_visitor(systemtap_session& s, |
5152 | int elf_machine, | |
5153 | const string & process_name, | |
a794dbeb | 5154 | const string & provider_name, |
aff5d390 | 5155 | const string & probe_name, |
71e5e13d | 5156 | stap_sdt_probe_type probe_type, |
aff5d390 | 5157 | const string & arg_string, |
8aabf152 | 5158 | int ac): |
332ba7e7 | 5159 | session (s), elf_machine (elf_machine), process_name (process_name), |
71e5e13d SC |
5160 | provider_name (provider_name), probe_name (probe_name), |
5161 | probe_type (probe_type), arg_count ((unsigned) ac) | |
a8ec7719 | 5162 | { |
f83336a5 FCE |
5163 | /* Register name mapping table depends on the elf machine of this particular |
5164 | probe target process/file, not upon the host. So we can't just | |
5165 | #ifdef _i686_ etc. */ | |
ae1418f0 FCE |
5166 | |
5167 | #define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width) | |
f83336a5 | 5168 | if (elf_machine == EM_X86_64) { |
46a94997 SC |
5169 | DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); |
5170 | DRI ("%al", 0, QI); DRI ("%ah", 0, QIh); | |
5171 | DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI); | |
5172 | DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh); | |
5173 | DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI); | |
5174 | DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh); | |
5175 | DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); | |
5176 | DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh); | |
5177 | DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI); | |
5178 | DRI ("%sil", 4, QI); | |
5179 | DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI); | |
5180 | DRI ("%dil", 5, QI); | |
5181 | DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI); | |
5182 | DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI); | |
5183 | DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI); | |
5184 | DRI ("%r8b", 8, QI); | |
5185 | DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI); | |
5186 | DRI ("%r9b", 9, QI); | |
5187 | DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI); | |
5188 | DRI ("%r10b", 10, QI); | |
5189 | DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI); | |
5190 | DRI ("%r11b", 11, QI); | |
5191 | DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI); | |
5192 | DRI ("%r12b", 12, QI); | |
5193 | DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI); | |
5194 | DRI ("%r13b", 13, QI); | |
5195 | DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI); | |
5196 | DRI ("%r14b", 14, QI); | |
5197 | DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI); | |
5198 | DRI ("%r15b", 15, QI); | |
f83336a5 | 5199 | } else if (elf_machine == EM_386) { |
46a94997 SC |
5200 | DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI); |
5201 | DRI ("%ah", 0, QIh); | |
5202 | DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI); | |
5203 | DRI ("%ch", 1, QIh); | |
5204 | DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI); | |
5205 | DRI ("%dh", 2, QIh); | |
5206 | DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI); | |
5207 | DRI ("%bh", 3, QIh); | |
5208 | DRI ("%esp", 4, SI); DRI ("%sp", 4, HI); | |
5209 | DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI); | |
5210 | DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI); | |
5211 | DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI); | |
0491c523 | 5212 | } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) { |
46a94997 SC |
5213 | DRI ("%r0", 0, DI); |
5214 | DRI ("%r1", 1, DI); | |
5215 | DRI ("%r2", 2, DI); | |
5216 | DRI ("%r3", 3, DI); | |
5217 | DRI ("%r4", 4, DI); | |
5218 | DRI ("%r5", 5, DI); | |
5219 | DRI ("%r6", 6, DI); | |
5220 | DRI ("%r7", 7, DI); | |
5221 | DRI ("%r8", 8, DI); | |
5222 | DRI ("%r9", 9, DI); | |
5223 | DRI ("%r10", 10, DI); | |
5224 | DRI ("%r11", 11, DI); | |
5225 | DRI ("%r12", 12, DI); | |
5226 | DRI ("%r13", 13, DI); | |
5227 | DRI ("%r14", 14, DI); | |
5228 | DRI ("%r15", 15, DI); | |
5229 | DRI ("%r16", 16, DI); | |
5230 | DRI ("%r17", 17, DI); | |
5231 | DRI ("%r18", 18, DI); | |
5232 | DRI ("%r19", 19, DI); | |
5233 | DRI ("%r20", 20, DI); | |
5234 | DRI ("%r21", 21, DI); | |
5235 | DRI ("%r22", 22, DI); | |
5236 | DRI ("%r23", 23, DI); | |
5237 | DRI ("%r24", 24, DI); | |
5238 | DRI ("%r25", 25, DI); | |
5239 | DRI ("%r26", 26, DI); | |
5240 | DRI ("%r27", 27, DI); | |
5241 | DRI ("%r28", 28, DI); | |
5242 | DRI ("%r29", 29, DI); | |
5243 | DRI ("%r30", 30, DI); | |
5244 | DRI ("%r31", 31, DI); | |
8aabf152 | 5245 | // PR11821: unadorned register "names" without -mregnames |
46a94997 SC |
5246 | DRI ("0", 0, DI); |
5247 | DRI ("1", 1, DI); | |
5248 | DRI ("2", 2, DI); | |
5249 | DRI ("3", 3, DI); | |
5250 | DRI ("4", 4, DI); | |
5251 | DRI ("5", 5, DI); | |
5252 | DRI ("6", 6, DI); | |
5253 | DRI ("7", 7, DI); | |
5254 | DRI ("8", 8, DI); | |
5255 | DRI ("9", 9, DI); | |
5256 | DRI ("10", 10, DI); | |
5257 | DRI ("11", 11, DI); | |
5258 | DRI ("12", 12, DI); | |
5259 | DRI ("13", 13, DI); | |
5260 | DRI ("14", 14, DI); | |
5261 | DRI ("15", 15, DI); | |
5262 | DRI ("16", 16, DI); | |
5263 | DRI ("17", 17, DI); | |
5264 | DRI ("18", 18, DI); | |
5265 | DRI ("19", 19, DI); | |
5266 | DRI ("20", 20, DI); | |
5267 | DRI ("21", 21, DI); | |
5268 | DRI ("22", 22, DI); | |
5269 | DRI ("23", 23, DI); | |
5270 | DRI ("24", 24, DI); | |
5271 | DRI ("25", 25, DI); | |
5272 | DRI ("26", 26, DI); | |
5273 | DRI ("27", 27, DI); | |
5274 | DRI ("28", 28, DI); | |
5275 | DRI ("29", 29, DI); | |
5276 | DRI ("30", 30, DI); | |
5277 | DRI ("31", 31, DI); | |
14900130 | 5278 | } else if (elf_machine == EM_S390) { |
46a94997 SC |
5279 | DRI ("%r0", 0, DI); |
5280 | DRI ("%r1", 1, DI); | |
5281 | DRI ("%r2", 2, DI); | |
5282 | DRI ("%r3", 3, DI); | |
5283 | DRI ("%r4", 4, DI); | |
5284 | DRI ("%r5", 5, DI); | |
5285 | DRI ("%r6", 6, DI); | |
5286 | DRI ("%r7", 7, DI); | |
5287 | DRI ("%r8", 8, DI); | |
5288 | DRI ("%r9", 9, DI); | |
5289 | DRI ("%r10", 10, DI); | |
5290 | DRI ("%r11", 11, DI); | |
5291 | DRI ("%r12", 12, DI); | |
5292 | DRI ("%r13", 13, DI); | |
5293 | DRI ("%r14", 14, DI); | |
5294 | DRI ("%r15", 15, DI); | |
272c9036 WF |
5295 | } else if (elf_machine == EM_ARM) { |
5296 | DRI ("r0", 0, SI); | |
5297 | DRI ("r1", 1, SI); | |
5298 | DRI ("r2", 2, SI); | |
5299 | DRI ("r3", 3, SI); | |
5300 | DRI ("r4", 4, SI); | |
5301 | DRI ("r5", 5, SI); | |
5302 | DRI ("r6", 6, SI); | |
5303 | DRI ("r7", 7, SI); | |
5304 | DRI ("r8", 8, SI); | |
5305 | DRI ("r9", 9, SI); | |
5306 | DRI ("sl", 10, SI); | |
5307 | DRI ("fp", 11, SI); | |
5308 | DRI ("ip", 12, SI); | |
5309 | DRI ("sp", 13, SI); | |
5310 | DRI ("lr", 14, SI); | |
5311 | DRI ("pc", 15, SI); | |
14900130 | 5312 | } else if (arg_count) { |
8aabf152 | 5313 | /* permit this case; just fall back to dwarf */ |
f83336a5 | 5314 | } |
ae1418f0 | 5315 | #undef DRI |
f83336a5 | 5316 | |
ebbd2b45 | 5317 | need_debug_info = false; |
88e39987 | 5318 | if (probe_type == uprobe3_type) |
272c9036 WF |
5319 | { |
5320 | sdt_v3_tokenize(arg_string, arg_tokens); | |
5321 | assert(arg_count <= 12); | |
5322 | } | |
88e39987 | 5323 | else |
272c9036 WF |
5324 | { |
5325 | tokenize(arg_string, arg_tokens, " "); | |
5326 | assert(arg_count <= 10); | |
5327 | } | |
a8ec7719 | 5328 | } |
8aabf152 | 5329 | |
f83336a5 | 5330 | systemtap_session& session; |
332ba7e7 | 5331 | int elf_machine; |
aff5d390 | 5332 | const string & process_name; |
a794dbeb | 5333 | const string & provider_name; |
aff5d390 | 5334 | const string & probe_name; |
71e5e13d | 5335 | stap_sdt_probe_type probe_type; |
8aabf152 | 5336 | unsigned arg_count; |
aff5d390 | 5337 | vector<string> arg_tokens; |
46a94997 | 5338 | map<string, pair<unsigned,int> > dwarf_regs; |
ebbd2b45 | 5339 | bool need_debug_info; |
aff5d390 SC |
5340 | |
5341 | void visit_target_symbol (target_symbol* e); | |
6ef331c8 SC |
5342 | void visit_target_symbol_arg (target_symbol* e); |
5343 | void visit_target_symbol_context (target_symbol* e); | |
40a0c64e | 5344 | void visit_cast_op (cast_op* e); |
aff5d390 SC |
5345 | }; |
5346 | ||
7a05f484 SC |
5347 | |
5348 | void | |
6ef331c8 | 5349 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
7a05f484 | 5350 | { |
6ef331c8 | 5351 | if (e->addressof) |
b530b5b3 | 5352 | throw semantic_error(_("cannot take address of context variable"), e->tok); |
6ef331c8 SC |
5353 | |
5354 | if (e->name == "$$name") | |
7a05f484 | 5355 | { |
6ef331c8 SC |
5356 | literal_string *myname = new literal_string (probe_name); |
5357 | myname->tok = e->tok; | |
5358 | provide(myname); | |
5359 | return; | |
5360 | } | |
7a05f484 | 5361 | |
6ef331c8 SC |
5362 | else if (e->name == "$$provider") |
5363 | { | |
5364 | literal_string *myname = new literal_string (provider_name); | |
5365 | myname->tok = e->tok; | |
5366 | provide(myname); | |
5367 | return; | |
5368 | } | |
a794dbeb | 5369 | |
6ef331c8 SC |
5370 | else if (e->name == "$$vars" || e->name == "$$parms") |
5371 | { | |
5372 | e->assert_no_components("sdt", true); | |
6ef331c8 SC |
5373 | |
5374 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
5375 | // NB: we synthesize a new token here rather than reusing | |
5376 | // e->tok, because print_format::print likes to use | |
5377 | // its tok->content. | |
5378 | token* pf_tok = new token(*e->tok); | |
5379 | pf_tok->content = "sprintf"; | |
63ea4244 | 5380 | |
6ef331c8 SC |
5381 | print_format* pf = print_format::create(pf_tok); |
5382 | ||
5383 | for (unsigned i = 1; i <= arg_count; ++i) | |
5384 | { | |
5385 | if (i > 1) | |
5386 | pf->raw_components += " "; | |
5387 | target_symbol *tsym = new target_symbol; | |
5388 | tsym->tok = e->tok; | |
5389 | tsym->name = "$arg" + lex_cast(i); | |
5390 | pf->raw_components += tsym->name; | |
5391 | tsym->components = e->components; | |
5392 | ||
5393 | expression *texp = require (tsym); | |
5394 | if (!e->components.empty() && | |
5395 | e->components[0].type == target_symbol::comp_pretty_print) | |
5396 | pf->raw_components += "=%s"; | |
5397 | else | |
5398 | pf->raw_components += "=%#x"; | |
5399 | pf->args.push_back(texp); | |
5400 | } | |
5401 | ||
5402 | pf->components = print_format::string_to_components(pf->raw_components); | |
5403 | provide (pf); | |
5404 | } | |
5405 | else | |
5406 | assert(0); // shouldn't get here | |
5407 | } | |
5408 | ||
5409 | ||
5410 | void | |
5411 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e) | |
5412 | { | |
5413 | try | |
5414 | { | |
8aabf152 | 5415 | unsigned argno = 0; // the N in $argN |
c69a87e0 | 5416 | try |
aff5d390 | 5417 | { |
5ecaa5a7 | 5418 | if (startswith(e->name, "$arg")) |
8aabf152 | 5419 | argno = lex_cast<unsigned>(e->name.substr(4)); |
aff5d390 | 5420 | } |
c69a87e0 | 5421 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF |
aff5d390 | 5422 | { |
8aabf152 | 5423 | argno = 0; |
aff5d390 | 5424 | } |
5ecaa5a7 | 5425 | |
8aabf152 FCE |
5426 | if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count |
5427 | argno < 1 || argno > arg_count) // a $argN with out-of-range N | |
aff5d390 | 5428 | { |
8aabf152 FCE |
5429 | // NB: Either |
5430 | // 1) uprobe1_type $argN or $FOO (we don't know the arg_count) | |
5431 | // 2) uprobe2_type $FOO (no probe args) | |
5432 | // both of which get resolved later. | |
73d53dd2 | 5433 | // Throw it now, and it might be resolved by DWARF later. |
8aabf152 | 5434 | need_debug_info = true; |
73d53dd2 | 5435 | throw semantic_error(_("target-symbol requires debuginfo"), e->tok); |
aff5d390 | 5436 | } |
277c21bc | 5437 | |
8aabf152 FCE |
5438 | assert (arg_tokens.size() >= argno); |
5439 | string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0] | |
c57ea854 | 5440 | |
8aabf152 FCE |
5441 | // Now we try to parse this thing, which is an assembler operand |
5442 | // expression. If we can't, we warn, back down to need_debug_info | |
b874bd52 | 5443 | // and hope for the best. Here is the syntax for a few architectures. |
9859b766 | 5444 | // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i. |
8095a157 FCE |
5445 | // |
5446 | // literal reg reg reg + base+index*size+offset | |
b874bd52 | 5447 | // indirect offset |
8095a157 | 5448 | // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) |
b874bd52 SC |
5449 | // power iN R (R) N(R) |
5450 | // ia64 N rR [r16] | |
5451 | // s390 N %rR 0(rR) N(r15) | |
5452 | // arm #N rR [rR] [rR, #N] | |
5453 | ||
8aabf152 FCE |
5454 | expression* argexpr = 0; // filled in in case of successful parse |
5455 | ||
5456 | string percent_regnames; | |
5457 | string regnames; | |
5458 | vector<string> matches; | |
71e5e13d | 5459 | long precision; |
8aabf152 FCE |
5460 | int rc; |
5461 | ||
40fe32e0 SC |
5462 | // Parse the leading length |
5463 | ||
5464 | if (asmarg.find('@') != string::npos) | |
5465 | { | |
5466 | precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@'))); | |
5467 | asmarg = asmarg.substr(asmarg.find('@')+1); | |
5468 | } | |
71e5e13d SC |
5469 | else |
5470 | { | |
5471 | // V1/V2 do not have precision field so default to signed long | |
5472 | // V3 asm does not have precision field so default to unsigned long | |
5473 | if (probe_type == uprobe3_type) | |
5474 | precision = sizeof(long); // this is an asm probe | |
5475 | else | |
5476 | precision = -sizeof(long); | |
5477 | } | |
40fe32e0 | 5478 | |
8aabf152 FCE |
5479 | // test for a numeric literal. |
5480 | // Only accept (signed) decimals throughout. XXX | |
5481 | ||
5482 | // PR11821. NB: on powerpc, literals are not prefixed with $, | |
5483 | // so this regex does not match. But that's OK, since without | |
5484 | // -mregnames, we can't tell them apart from register numbers | |
5485 | // anyway. With -mregnames, we could, if gcc somehow | |
5486 | // communicated to us the presence of that option, but alas it | |
5487 | // doesn't. http://gcc.gnu.org/PR44995. | |
272c9036 | 5488 | rc = regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches); |
8aabf152 FCE |
5489 | if (! rc) |
5490 | { | |
75a371ce JS |
5491 | string sn = matches[0].substr(1); |
5492 | int64_t n; | |
5493 | try | |
5494 | { | |
5495 | // We have to pay attention to the size & sign, as gcc sometimes | |
5496 | // propagates constants that don't quite match, like a negative | |
5497 | // value to fill an unsigned type. | |
5498 | switch (precision) | |
5499 | { | |
5500 | case -1: n = lex_cast< int8_t>(sn); break; | |
5501 | case 1: n = lex_cast< uint8_t>(sn); break; | |
5502 | case -2: n = lex_cast< int16_t>(sn); break; | |
5503 | case 2: n = lex_cast<uint16_t>(sn); break; | |
5504 | case -4: n = lex_cast< int32_t>(sn); break; | |
5505 | case 4: n = lex_cast<uint32_t>(sn); break; | |
5506 | default: | |
5507 | case -8: n = lex_cast< int64_t>(sn); break; | |
5508 | case 8: n = lex_cast<uint64_t>(sn); break; | |
5509 | } | |
5510 | } | |
5511 | catch (std::runtime_error&) | |
5512 | { | |
5513 | goto not_matched; | |
5514 | } | |
5515 | literal_number* ln = new literal_number(n); | |
8aabf152 FCE |
5516 | ln->tok = e->tok; |
5517 | argexpr = ln; | |
5518 | goto matched; | |
5519 | } | |
5520 | ||
14900130 SC |
5521 | if (dwarf_regs.empty()) |
5522 | goto not_matched; | |
d5b83cee | 5523 | |
8aabf152 FCE |
5524 | // Build regex pieces out of the known dwarf_regs. We keep two separate |
5525 | // lists: ones with the % prefix (and thus unambigiuous even despite PR11821), | |
5526 | // and ones with no prefix (and thus only usable in unambiguous contexts). | |
46a94997 | 5527 | for (map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++) |
8aabf152 FCE |
5528 | { |
5529 | string regname = ri->first; | |
5530 | assert (regname != ""); | |
5531 | regnames += string("|")+regname; | |
5532 | if (regname[0]=='%') | |
5533 | percent_regnames += string("|")+regname; | |
5534 | } | |
5535 | // clip off leading | | |
5536 | regnames = regnames.substr(1); | |
272c9036 WF |
5537 | if (percent_regnames != "") |
5538 | percent_regnames = percent_regnames.substr(1); | |
8aabf152 FCE |
5539 | |
5540 | // test for REGISTER | |
5541 | // NB: Because PR11821, we must use percent_regnames here. | |
272c9036 | 5542 | if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM) |
9109f487 SC |
5543 | rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches); |
5544 | else | |
332ba7e7 | 5545 | rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches); |
8aabf152 FCE |
5546 | if (! rc) |
5547 | { | |
5548 | string regname = matches[1]; | |
46a94997 SC |
5549 | map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname); |
5550 | if (ri != dwarf_regs.end()) // known register | |
8aabf152 FCE |
5551 | { |
5552 | embedded_expr *get_arg1 = new embedded_expr; | |
19c22e1f | 5553 | string width_adjust; |
46a94997 | 5554 | switch (ri->second.second) |
19c22e1f | 5555 | { |
892ec39a SC |
5556 | case QI: width_adjust = ") & 0xff)"; break; |
5557 | case QIh: width_adjust = ">>8) & 0xff)"; break; | |
46a94997 | 5558 | case HI: |
71e5e13d | 5559 | // preserve 16 bit register signness |
892ec39a SC |
5560 | width_adjust = ") & 0xffff)"; |
5561 | if (precision < 0) | |
55b377f4 | 5562 | width_adjust += " << 48 >> 48"; |
ac8a78aa SC |
5563 | break; |
5564 | case SI: | |
5565 | // preserve 32 bit register signness | |
892ec39a SC |
5566 | width_adjust = ") & 0xffffffff)"; |
5567 | if (precision < 0) | |
55b377f4 | 5568 | width_adjust += " << 32 >> 32"; |
19c22e1f | 5569 | break; |
892ec39a | 5570 | default: width_adjust = "))"; |
19c22e1f | 5571 | } |
55b377f4 SC |
5572 | string type = ""; |
5573 | if (probe_type == uprobe3_type) | |
5574 | type = (precision < 0 | |
5575 | ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)"; | |
5576 | type = type + "(("; | |
8aabf152 FCE |
5577 | get_arg1->tok = e->tok; |
5578 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
892ec39a | 5579 | + string(" ((int64_t)") + type |
8aabf152 FCE |
5580 | + (is_user_module (process_name) |
5581 | ? string("u_fetch_register(") | |
5582 | : string("k_fetch_register(")) | |
46a94997 | 5583 | + lex_cast(dwarf_regs[regname].first) + string("))") |
19c22e1f | 5584 | + width_adjust; |
8aabf152 FCE |
5585 | argexpr = get_arg1; |
5586 | goto matched; | |
5587 | } | |
5588 | // invalid register name, fall through | |
5589 | } | |
40fe32e0 | 5590 | |
272c9036 | 5591 | int reg, offset1; |
e5b7b83f | 5592 | // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N |
40fe32e0 | 5593 | // NB: Despite PR11821, we can use regnames here, since the parentheses |
e5b7b83f | 5594 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) |
272c9036 WF |
5595 | // On ARM test for [REGISTER, OFFSET] |
5596 | if (elf_machine == EM_ARM) | |
5597 | { | |
5598 | rc = regexp_match (asmarg, string("^\\[(")+regnames+string("), #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?\\]$"), matches); | |
5599 | reg = 1; | |
5600 | offset1 = 2; | |
5601 | } | |
5602 | else | |
5603 | { | |
5604 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string(")[)]$"), matches); | |
5605 | reg = 4; | |
5606 | offset1 = 1; | |
5607 | } | |
8aabf152 FCE |
5608 | if (! rc) |
5609 | { | |
e5b7b83f | 5610 | string regname; |
8aabf152 | 5611 | int64_t disp = 0; |
272c9036 WF |
5612 | if (matches[reg].length()) |
5613 | regname = matches[reg]; | |
8095a157 FCE |
5614 | if (dwarf_regs.find (regname) == dwarf_regs.end()) |
5615 | goto not_matched; | |
5616 | ||
272c9036 | 5617 | for (int i=offset1; i <= (offset1 + 2); i++) |
e5b7b83f SC |
5618 | if (matches[i].length()) |
5619 | try | |
5620 | { | |
5621 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
5622 | } | |
8aabf152 FCE |
5623 | catch (const runtime_error& f) // unparseable offset |
5624 | { | |
5625 | goto not_matched; // can't just 'break' out of | |
5626 | // this case or use a sentinel | |
5627 | // value, unfortunately | |
5628 | } | |
5629 | ||
8aabf152 | 5630 | // synthesize user_long(%{fetch_register(R)%} + D) |
8aabf152 FCE |
5631 | embedded_expr *get_arg1 = new embedded_expr; |
5632 | get_arg1->tok = e->tok; | |
5633 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
5634 | + (is_user_module (process_name) | |
5635 | ? string("u_fetch_register(") | |
5636 | : string("k_fetch_register(")) | |
46a94997 | 5637 | + lex_cast(dwarf_regs[regname].first) + string(")"); |
8aabf152 | 5638 | // XXX: may we ever need to cast that to a narrower type? |
40fe32e0 | 5639 | |
8aabf152 FCE |
5640 | literal_number* inc = new literal_number(disp); |
5641 | inc->tok = e->tok; | |
40fe32e0 | 5642 | |
8aabf152 FCE |
5643 | binary_expression *be = new binary_expression; |
5644 | be->tok = e->tok; | |
5645 | be->left = get_arg1; | |
5646 | be->op = "+"; | |
5647 | be->right = inc; | |
40fe32e0 | 5648 | |
8aabf152 | 5649 | functioncall *fc = new functioncall; |
40fe32e0 SC |
5650 | switch (precision) |
5651 | { | |
7f6ce9ab SC |
5652 | case 1: case -1: |
5653 | fc->function = "user_int8"; break; | |
5654 | case 2: | |
5655 | fc->function = "user_uint16"; break; | |
5656 | case -2: | |
5657 | fc->function = "user_int16"; break; | |
5658 | case 4: | |
5659 | fc->function = "user_uint32"; break; | |
5660 | case -4: | |
5661 | fc->function = "user_int32"; break; | |
5662 | case 8: case -8: | |
5663 | fc->function = "user_int64"; break; | |
40fe32e0 SC |
5664 | default: fc->function = "user_long"; |
5665 | } | |
8aabf152 FCE |
5666 | fc->tok = e->tok; |
5667 | fc->args.push_back(be); | |
366af4e7 | 5668 | |
8aabf152 FCE |
5669 | argexpr = fc; |
5670 | goto matched; | |
5671 | } | |
8095a157 FCE |
5672 | |
5673 | // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N | |
5674 | // NB: Despite PR11821, we can use regnames here, since the parentheses | |
5675 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) | |
0f7b51d6 | 5676 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string("),(")+regnames+string(")(,[1248])?[)]$"), matches); |
8095a157 FCE |
5677 | if (! rc) |
5678 | { | |
5679 | string baseregname; | |
5680 | string indexregname; | |
5681 | int64_t disp = 0; | |
5682 | short scale = 1; | |
5683 | ||
5684 | if (matches[6].length()) | |
5685 | try | |
5686 | { | |
5687 | scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma! | |
5688 | // We could verify that scale is one of 1,2,4,8, | |
5689 | // but it doesn't really matter. An erroneous | |
5690 | // address merely results in run-time errors. | |
8aabf152 | 5691 | } |
8095a157 FCE |
5692 | catch (const runtime_error &f) // unparseable scale |
5693 | { | |
5694 | goto not_matched; | |
5695 | } | |
5696 | ||
5697 | if (matches[4].length()) | |
5698 | baseregname = matches[4]; | |
5699 | if (dwarf_regs.find (baseregname) == dwarf_regs.end()) | |
5700 | goto not_matched; | |
5701 | ||
5702 | if (matches[5].length()) | |
5703 | indexregname = matches[5]; | |
5704 | if (dwarf_regs.find (indexregname) == dwarf_regs.end()) | |
5705 | goto not_matched; | |
5706 | ||
f7719b3d | 5707 | for (int i = 1; i <= 3; i++) // up to three OFFSET terms |
8095a157 FCE |
5708 | if (matches[i].length()) |
5709 | try | |
5710 | { | |
5711 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
5712 | } | |
5713 | catch (const runtime_error& f) // unparseable offset | |
5714 | { | |
5715 | goto not_matched; // can't just 'break' out of | |
5716 | // this case or use a sentinel | |
5717 | // value, unfortunately | |
5718 | } | |
5719 | ||
5720 | // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D) | |
5721 | ||
5722 | embedded_expr *get_arg1 = new embedded_expr; | |
5723 | string regfn = is_user_module (process_name) | |
5724 | ? string("u_fetch_register") | |
5725 | : string("k_fetch_register"); // NB: in practice sdt.h probes are for userspace only | |
5726 | ||
5727 | get_arg1->tok = e->tok; | |
5728 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
5729 | + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")") | |
5730 | + string("+(") | |
5731 | + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")") | |
5732 | + string("*") | |
5733 | + lex_cast(scale) | |
5734 | + string(")"); | |
5735 | ||
5736 | // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too | |
5737 | literal_number* inc = new literal_number(disp); | |
5738 | inc->tok = e->tok; | |
5739 | ||
5740 | binary_expression *be = new binary_expression; | |
5741 | be->tok = e->tok; | |
5742 | be->left = get_arg1; | |
5743 | be->op = "+"; | |
5744 | be->right = inc; | |
5745 | ||
5746 | functioncall *fc = new functioncall; | |
5747 | switch (precision) | |
5748 | { | |
5749 | case 1: case -1: | |
5750 | fc->function = "user_int8"; break; | |
5751 | case 2: | |
5752 | fc->function = "user_uint16"; break; | |
5753 | case -2: | |
5754 | fc->function = "user_int16"; break; | |
5755 | case 4: | |
5756 | fc->function = "user_uint32"; break; | |
5757 | case -4: | |
5758 | fc->function = "user_int32"; break; | |
5759 | case 8: case -8: | |
5760 | fc->function = "user_int64"; break; | |
5761 | default: fc->function = "user_long"; | |
5762 | } | |
5763 | fc->tok = e->tok; | |
5764 | fc->args.push_back(be); | |
5765 | ||
5766 | argexpr = fc; | |
5767 | goto matched; | |
8aabf152 FCE |
5768 | } |
5769 | ||
8aabf152 FCE |
5770 | |
5771 | not_matched: | |
5772 | // The asmarg operand was not recognized. Back down to dwarf. | |
5773 | if (! session.suppress_warnings) | |
84fef8ee FCE |
5774 | { |
5775 | if (probe_type == UPROBE3_TYPE) | |
5776 | session.print_warning (_F("Can't parse SDT_V3 operand '%s'", asmarg.c_str()), e->tok); | |
5777 | else // must be *PROBE2; others don't get asm operands | |
5778 | session.print_warning (_F("Downgrading SDT_V2 probe argument to dwarf, can't parse '%s'", | |
5779 | asmarg.c_str()), e->tok); | |
5780 | } | |
8aabf152 FCE |
5781 | assert (argexpr == 0); |
5782 | need_debug_info = true; | |
73d53dd2 | 5783 | throw semantic_error(_("SDT asm not understood, requires debuginfo"), e->tok); |
366af4e7 | 5784 | |
8aabf152 FCE |
5785 | matched: |
5786 | assert (argexpr != 0); | |
366af4e7 RM |
5787 | |
5788 | if (session.verbose > 2) | |
1e41115c | 5789 | //TRANSLATORS: We're mapping the operand to a new expression*. |
b530b5b3 | 5790 | clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl; |
366af4e7 | 5791 | |
aff5d390 | 5792 | if (e->components.empty()) // We have a scalar |
8aabf152 FCE |
5793 | { |
5794 | if (e->addressof) | |
b530b5b3 | 5795 | throw semantic_error(_("cannot take address of sdt variable"), e->tok); |
8aabf152 FCE |
5796 | provide (argexpr); |
5797 | return; | |
5798 | } | |
5799 | else // $var->foo | |
5800 | { | |
5801 | cast_op *cast = new cast_op; | |
5802 | cast->name = "@cast"; | |
5803 | cast->tok = e->tok; | |
5804 | cast->operand = argexpr; | |
5805 | cast->components = e->components; | |
5806 | cast->type_name = probe_name + "_arg" + lex_cast(argno); | |
5807 | cast->module = process_name; | |
5808 | cast->visit(this); | |
5809 | return; | |
5810 | } | |
366af4e7 | 5811 | |
8aabf152 | 5812 | /* NOTREACHED */ |
aff5d390 SC |
5813 | } |
5814 | catch (const semantic_error &er) | |
5815 | { | |
5816 | e->chain (er); | |
5817 | provide (e); | |
5818 | } | |
5819 | } | |
5820 | ||
5821 | ||
6ef331c8 SC |
5822 | void |
5823 | sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
5824 | { | |
5825 | try | |
5826 | { | |
49131a6d MW |
5827 | assert(e->name.size() > 0 |
5828 | && ((e->name[0] == '$' && e->target_name == "") | |
5829 | || (e->name == "@var" && e->target_name != ""))); | |
6ef331c8 SC |
5830 | |
5831 | if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars") | |
5832 | visit_target_symbol_context (e); | |
5833 | else | |
5834 | visit_target_symbol_arg (e); | |
5835 | } | |
5836 | catch (const semantic_error &er) | |
5837 | { | |
5838 | e->chain (er); | |
5839 | provide (e); | |
5840 | } | |
5841 | } | |
5842 | ||
5843 | ||
40a0c64e JS |
5844 | void |
5845 | sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e) | |
5846 | { | |
5847 | // Fill in our current module context if needed | |
5848 | if (e->module.empty()) | |
5849 | e->module = process_name; | |
5850 | ||
5851 | var_expanding_visitor::visit_cast_op(e); | |
5852 | } | |
5853 | ||
5854 | ||
576eaefe SC |
5855 | void |
5856 | plt_expanding_visitor::visit_target_symbol (target_symbol *e) | |
5857 | { | |
5858 | try | |
5859 | { | |
5860 | if (e->name == "$$name") | |
5861 | { | |
5862 | literal_string *myname = new literal_string (entry); | |
5863 | myname->tok = e->tok; | |
5864 | provide(myname); | |
5865 | return; | |
5866 | } | |
3d69c03f JS |
5867 | |
5868 | // variable not found -> throw a semantic error | |
5869 | // (only to be caught right away, but this may be more complex later...) | |
5870 | string alternatives = "$$name"; | |
5871 | throw semantic_error(_F("unable to find plt variable '%s' (alternatives: %s)", | |
5872 | e->name.c_str(), alternatives.c_str()), e->tok); | |
576eaefe SC |
5873 | } |
5874 | catch (const semantic_error &er) | |
5875 | { | |
5876 | e->chain (er); | |
5877 | provide (e); | |
5878 | } | |
5879 | } | |
5880 | ||
5881 | ||
edce5b67 JS |
5882 | struct sdt_query : public base_query |
5883 | { | |
5884 | sdt_query(probe * base_probe, probe_point * base_loc, | |
5885 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 5886 | vector<derived_probe *> & results, const string user_lib); |
edce5b67 | 5887 | |
51d6bda3 | 5888 | void query_library (const char *data); |
576eaefe | 5889 | void query_plt (const char *entry, size_t addr) {} |
edce5b67 JS |
5890 | void handle_query_module(); |
5891 | ||
5892 | private: | |
15284963 | 5893 | stap_sdt_probe_type probe_type; |
d61ea602 | 5894 | enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc; |
edce5b67 JS |
5895 | probe * base_probe; |
5896 | probe_point * base_loc; | |
6846cfc8 | 5897 | literal_map_t const & params; |
edce5b67 | 5898 | vector<derived_probe *> & results; |
a794dbeb FCE |
5899 | string pp_mark; |
5900 | string pp_provider; | |
51d6bda3 | 5901 | string user_lib; |
edce5b67 JS |
5902 | |
5903 | set<string> probes_handled; | |
5904 | ||
5905 | Elf_Data *pdata; | |
5906 | size_t probe_scn_offset; | |
5907 | size_t probe_scn_addr; | |
aff5d390 | 5908 | uint64_t arg_count; |
40fe32e0 | 5909 | GElf_Addr base; |
c57ea854 | 5910 | GElf_Addr pc; |
aff5d390 | 5911 | string arg_string; |
edce5b67 | 5912 | string probe_name; |
a794dbeb | 5913 | string provider_name; |
79a0ca08 | 5914 | Dwarf_Addr semaphore; |
edce5b67 JS |
5915 | |
5916 | bool init_probe_scn(); | |
6b51ee12 | 5917 | bool get_next_probe(); |
c57ea854 SC |
5918 | void iterate_over_probe_entries(); |
5919 | void handle_probe_entry(); | |
edce5b67 | 5920 | |
40fe32e0 SC |
5921 | static void setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len); |
5922 | void setup_note_probe_entry (int type, const char *data, size_t len); | |
5923 | ||
edce5b67 | 5924 | void convert_probe(probe *base); |
4ddb6dd0 | 5925 | void record_semaphore(vector<derived_probe *> & results, unsigned start); |
c72aa911 | 5926 | probe* convert_location(); |
40fe32e0 | 5927 | bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;} |
c57ea854 SC |
5928 | bool have_debuginfo_uprobe(bool need_debug_info) |
5929 | {return probe_type == uprobe1_type | |
40fe32e0 | 5930 | || ((probe_type == uprobe2_type || probe_type == uprobe3_type) |
c57ea854 | 5931 | && need_debug_info);} |
40fe32e0 | 5932 | bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;} |
edce5b67 JS |
5933 | }; |
5934 | ||
5935 | ||
5936 | sdt_query::sdt_query(probe * base_probe, probe_point * base_loc, | |
5937 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 5938 | vector<derived_probe *> & results, const string user_lib): |
d61ea602 JS |
5939 | base_query(dw, params), probe_type(unknown_probe_type), |
5940 | probe_loc(unknown_section), base_probe(base_probe), | |
74fe61bc LB |
5941 | base_loc(base_loc), params(params), results(results), user_lib(user_lib), |
5942 | probe_scn_offset(0), probe_scn_addr(0), arg_count(0), base(0), pc(0), | |
5943 | semaphore(0) | |
edce5b67 | 5944 | { |
a794dbeb FCE |
5945 | assert(get_string_param(params, TOK_MARK, pp_mark)); |
5946 | get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified | |
5947 | ||
ef428667 FCE |
5948 | // PR10245: permit usage of dtrace-y "-" separator in marker name; |
5949 | // map it to double-underscores. | |
5950 | size_t pos = 0; | |
5951 | while (1) // there may be more than one | |
5952 | { | |
a794dbeb | 5953 | size_t i = pp_mark.find("-", pos); |
ef428667 | 5954 | if (i == string::npos) break; |
a794dbeb | 5955 | pp_mark.replace (i, 1, "__"); |
ef428667 FCE |
5956 | pos = i+1; // resume searching after the inserted __ |
5957 | } | |
a794dbeb FCE |
5958 | |
5959 | // XXX: same for pp_provider? | |
edce5b67 JS |
5960 | } |
5961 | ||
5962 | ||
5963 | void | |
c57ea854 | 5964 | sdt_query::handle_probe_entry() |
edce5b67 | 5965 | { |
c57ea854 SC |
5966 | if (! have_uprobe() |
5967 | && !probes_handled.insert(probe_name).second) | |
edce5b67 JS |
5968 | return; |
5969 | ||
5970 | if (sess.verbose > 3) | |
c57ea854 | 5971 | { |
b530b5b3 LB |
5972 | //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe |
5973 | //TRANSLATORS: is matched to. | |
5974 | clog << _F("matched probe_name %s probe type ", probe_name.c_str()); | |
c57ea854 SC |
5975 | switch (probe_type) |
5976 | { | |
5977 | case uprobe1_type: | |
5978 | clog << "uprobe1 at 0x" << hex << pc << dec << endl; | |
5979 | break; | |
5980 | case uprobe2_type: | |
5981 | clog << "uprobe2 at 0x" << hex << pc << dec << endl; | |
5982 | break; | |
40fe32e0 SC |
5983 | case uprobe3_type: |
5984 | clog << "uprobe3 at 0x" << hex << pc << dec << endl; | |
5985 | break; | |
d61ea602 JS |
5986 | default: |
5987 | clog << "unknown!" << endl; | |
5988 | break; | |
c57ea854 SC |
5989 | } |
5990 | } | |
edce5b67 | 5991 | |
c57ea854 SC |
5992 | // Extend the derivation chain |
5993 | probe *new_base = convert_location(); | |
5994 | probe_point *new_location = new_base->locations[0]; | |
5995 | ||
c57ea854 SC |
5996 | bool need_debug_info = false; |
5997 | ||
7d395255 JS |
5998 | // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...)) |
5999 | // or dwfl_module_getelf(...). We only need it for the machine type, which | |
6000 | // should be the same. The bias is used for relocating debuginfoless probes, | |
6001 | // though, so that must come from the possibly-prelinked ELF file, not DWARF. | |
c57ea854 | 6002 | Dwarf_Addr bias; |
7d395255 | 6003 | Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias); |
c57ea854 | 6004 | |
1cc41cd6 DS |
6005 | /* Figure out the architecture of this particular ELF file. The |
6006 | dwarfless register-name mappings depend on it. */ | |
6007 | GElf_Ehdr ehdr_mem; | |
6008 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
6009 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } | |
d8f31d0a | 6010 | assert(em); |
1cc41cd6 DS |
6011 | int elf_machine = em->e_machine; |
6012 | sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, module_val, | |
6013 | provider_name, probe_name, | |
6014 | probe_type, arg_string, arg_count); | |
6015 | svv.replace (new_base->body); | |
6016 | need_debug_info = svv.need_debug_info; | |
c57ea854 | 6017 | |
1cc41cd6 DS |
6018 | // XXX: why not derive_probes() in the uprobes case too? |
6019 | literal_map_t params; | |
6020 | for (unsigned i = 0; i < new_location->components.size(); ++i) | |
6021 | { | |
6022 | probe_point::component *c = new_location->components[i]; | |
6023 | params[c->functor] = c->arg; | |
6024 | } | |
c57ea854 | 6025 | |
73d53dd2 | 6026 | unsigned prior_results_size = results.size(); |
1cc41cd6 DS |
6027 | dwarf_query q(new_base, new_location, dw, params, results, "", ""); |
6028 | q.has_mark = true; // enables mid-statement probing | |
30263a73 | 6029 | |
73d53dd2 JS |
6030 | // V1 probes always need dwarf info |
6031 | // V2+ probes need dwarf info in case of a variable reference | |
1cc41cd6 DS |
6032 | if (have_debuginfo_uprobe(need_debug_info)) |
6033 | dw.iterate_over_modules(&query_module, &q); | |
73d53dd2 JS |
6034 | |
6035 | // For V2+ probes, if variable references weren't used or failed (PR14369), | |
6036 | // then try with the more direct approach. Unresolved $vars might still | |
6037 | // cause their own error, but this gives them a chance to be optimized out. | |
6038 | if (have_debuginfoless_uprobe() && results.size() == prior_results_size) | |
1cc41cd6 DS |
6039 | { |
6040 | string section; | |
6041 | Dwarf_Addr reloc_addr = q.statement_num_val + bias; | |
6042 | if (dwfl_module_relocations (q.dw.mod_info->mod) > 0) | |
6043 | { | |
6044 | dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr); | |
6045 | section = ".dynamic"; | |
6046 | } | |
6047 | else | |
6048 | section = ".absolute"; | |
edce5b67 | 6049 | |
1cc41cd6 DS |
6050 | uprobe_derived_probe* p = |
6051 | new uprobe_derived_probe ("", "", 0, | |
6052 | path_remove_sysroot(sess,q.module_val), | |
6053 | section, | |
6054 | q.statement_num_val, reloc_addr, q, 0); | |
6055 | p->saveargs (arg_count); | |
6056 | results.push_back (p); | |
c57ea854 | 6057 | } |
487bf4e2 | 6058 | sess.unwindsym_modules.insert (dw.module_name); |
73d53dd2 | 6059 | record_semaphore(results, prior_results_size); |
c57ea854 | 6060 | } |
edce5b67 | 6061 | |
4ddb6dd0 | 6062 | |
c57ea854 SC |
6063 | void |
6064 | sdt_query::handle_query_module() | |
6065 | { | |
6066 | if (!init_probe_scn()) | |
6067 | return; | |
edce5b67 | 6068 | |
c57ea854 SC |
6069 | if (sess.verbose > 3) |
6070 | clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl; | |
edce5b67 | 6071 | |
40fe32e0 SC |
6072 | if (probe_loc == note_section) |
6073 | { | |
6074 | GElf_Shdr shdr_mem; | |
6075 | GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem); | |
6076 | ||
6077 | if (shdr) | |
6078 | base = shdr->sh_addr; | |
6079 | else | |
6080 | base = 0; | |
6081 | dw.iterate_over_notes ((void*) this, &sdt_query::setup_note_probe_entry_callback); | |
6082 | } | |
d61ea602 | 6083 | else if (probe_loc == probe_section) |
40fe32e0 | 6084 | iterate_over_probe_entries (); |
edce5b67 JS |
6085 | } |
6086 | ||
6087 | ||
6088 | bool | |
6089 | sdt_query::init_probe_scn() | |
6090 | { | |
448a86b7 | 6091 | Elf* elf; |
edce5b67 | 6092 | GElf_Shdr shdr_mem; |
40fe32e0 SC |
6093 | |
6094 | GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem); | |
6095 | if (shdr) | |
6096 | { | |
6097 | probe_loc = note_section; | |
6098 | return true; | |
6099 | } | |
edce5b67 | 6100 | |
448a86b7 | 6101 | shdr = dw.get_section (".probes", &shdr_mem, &elf); |
fea74777 | 6102 | if (shdr) |
edce5b67 | 6103 | { |
fea74777 SC |
6104 | pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
6105 | probe_scn_offset = 0; | |
6106 | probe_scn_addr = shdr->sh_addr; | |
6107 | assert (pdata != NULL); | |
6108 | if (sess.verbose > 4) | |
ce0f6648 LB |
6109 | clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: " |
6110 | << pdata->d_size << endl; | |
40fe32e0 | 6111 | probe_loc = probe_section; |
fea74777 | 6112 | return true; |
edce5b67 | 6113 | } |
fea74777 | 6114 | else |
edce5b67 | 6115 | return false; |
edce5b67 JS |
6116 | } |
6117 | ||
40fe32e0 SC |
6118 | void |
6119 | sdt_query::setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len) | |
6120 | { | |
6121 | sdt_query *me = (sdt_query*)object; | |
6122 | me->setup_note_probe_entry (type, data, len); | |
6123 | } | |
6124 | ||
6125 | ||
6126 | void | |
6127 | sdt_query::setup_note_probe_entry (int type, const char *data, size_t len) | |
6128 | { | |
6129 | // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME | |
6130 | // && !memcmp (data->d_buf + name_off, | |
6131 | // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME)) | |
6132 | ||
6133 | // probes are in the .note.stapsdt section | |
6134 | #define _SDT_NOTE_TYPE 3 | |
6135 | if (type != _SDT_NOTE_TYPE) | |
6136 | return; | |
6137 | ||
6138 | union | |
6139 | { | |
6140 | Elf64_Addr a64[3]; | |
6141 | Elf32_Addr a32[3]; | |
6142 | } buf; | |
6143 | Dwarf_Addr bias; | |
6144 | Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias)); | |
6145 | Elf_Data dst = | |
6146 | { | |
6147 | &buf, ELF_T_ADDR, EV_CURRENT, | |
6148 | gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0 | |
6149 | }; | |
6150 | assert (dst.d_size <= sizeof buf); | |
6151 | ||
6152 | if (len < dst.d_size + 3) | |
6153 | return; | |
6154 | ||
6155 | Elf_Data src = | |
6156 | { | |
6157 | (void *) data, ELF_T_ADDR, EV_CURRENT, | |
6158 | dst.d_size, 0, 0 | |
6159 | }; | |
6160 | ||
6161 | if (gelf_xlatetom (elf, &dst, &src, | |
6162 | elf_getident (elf, NULL)[EI_DATA]) == NULL) | |
6163 | printf ("gelf_xlatetom: %s", elf_errmsg (-1)); | |
6164 | ||
6165 | probe_type = uprobe3_type; | |
6166 | const char * provider = data + dst.d_size; | |
3f803f9e | 6167 | |
40fe32e0 | 6168 | const char *name = (const char*)memchr (provider, '\0', data + len - provider); |
3f803f9e CM |
6169 | if(name++ == NULL) |
6170 | return; | |
6171 | ||
6172 | const char *args = (const char*)memchr (name, '\0', data + len - name); | |
6173 | if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1) | |
6174 | return; | |
6175 | ||
6176 | provider_name = provider; | |
6177 | probe_name = name; | |
6178 | arg_string = args; | |
40fe32e0 SC |
6179 | |
6180 | // Did we find a matching probe? | |
6181 | if (! (dw.function_name_matches_pattern (probe_name, pp_mark) | |
6182 | && ((pp_provider == "") | |
6183 | || dw.function_name_matches_pattern (provider_name, pp_provider)))) | |
6184 | return; | |
6185 | ||
e9a90eee JS |
6186 | // PR13934: Assembly probes are not forced to use the N@OP form. |
6187 | // If we have '@' then great, else count based on space-delimiters. | |
6188 | arg_count = count(arg_string.begin(), arg_string.end(), '@'); | |
6189 | if (!arg_count && !arg_string.empty()) | |
6190 | arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' '); | |
6191 | ||
40fe32e0 SC |
6192 | GElf_Addr base_ref; |
6193 | if (gelf_getclass (elf) == ELFCLASS32) | |
6194 | { | |
6195 | pc = buf.a32[0]; | |
6196 | base_ref = buf.a32[1]; | |
6197 | semaphore = buf.a32[2]; | |
6198 | } | |
6199 | else | |
6200 | { | |
6201 | pc = buf.a64[0]; | |
6202 | base_ref = buf.a64[1]; | |
6203 | semaphore = buf.a64[2]; | |
6204 | } | |
6205 | ||
6206 | semaphore += base - base_ref; | |
6207 | pc += base - base_ref; | |
6208 | ||
7d395255 JS |
6209 | // The semaphore also needs the ELF bias added now, so |
6210 | // record_semaphore can properly relocate it later. | |
6211 | semaphore += bias; | |
6212 | ||
40fe32e0 | 6213 | if (sess.verbose > 4) |
b530b5b3 | 6214 | clog << _F(" saw .note.stapsdt %s%s ", probe_name.c_str(), (provider_name != "" ? _(" (provider ")+provider_name+") " : "").c_str()) << "@0x" << hex << pc << dec << endl; |
40fe32e0 SC |
6215 | |
6216 | handle_probe_entry(); | |
6217 | } | |
6218 | ||
6219 | ||
c57ea854 SC |
6220 | void |
6221 | sdt_query::iterate_over_probe_entries() | |
edce5b67 | 6222 | { |
c57ea854 | 6223 | // probes are in the .probe section |
edce5b67 JS |
6224 | while (probe_scn_offset < pdata->d_size) |
6225 | { | |
aff5d390 SC |
6226 | stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset); |
6227 | stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset); | |
15284963 | 6228 | probe_type = (stap_sdt_probe_type)(pbe_v1->type_a); |
1cc41cd6 | 6229 | if (! have_uprobe()) |
edce5b67 JS |
6230 | { |
6231 | // Unless this is a mangled .probes section, this happens | |
6232 | // because the name of the probe comes first, followed by | |
6233 | // the sentinel. | |
6234 | if (sess.verbose > 5) | |
b530b5b3 | 6235 | clog << _F("got unknown probe_type : 0x%x", probe_type) << endl; |
edce5b67 JS |
6236 | probe_scn_offset += sizeof(__uint32_t); |
6237 | continue; | |
6238 | } | |
aff5d390 SC |
6239 | if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b |
6240 | { | |
6241 | pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t)); | |
1cc41cd6 | 6242 | if (pbe_v1->type_b != uprobe1_type) |
aff5d390 SC |
6243 | continue; |
6244 | } | |
6245 | ||
1cc41cd6 | 6246 | if (probe_type == uprobe1_type) |
aff5d390 | 6247 | { |
79a0ca08 | 6248 | if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6249 | return; |
79a0ca08 | 6250 | semaphore = 0; |
aff5d390 | 6251 | probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr); |
a794dbeb | 6252 | provider_name = ""; // unknown |
1cc41cd6 DS |
6253 | pc = pbe_v1->arg; |
6254 | arg_count = 0; | |
aff5d390 SC |
6255 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v1); |
6256 | } | |
08b5a50c | 6257 | else if (probe_type == uprobe2_type) |
aff5d390 | 6258 | { |
79a0ca08 | 6259 | if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6260 | return; |
79a0ca08 | 6261 | semaphore = pbe_v2->semaphore; |
aff5d390 | 6262 | probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr); |
a794dbeb | 6263 | provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr); |
aff5d390 SC |
6264 | arg_count = pbe_v2->arg_count; |
6265 | pc = pbe_v2->pc; | |
6266 | if (pbe_v2->arg_string) | |
6267 | arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr); | |
79a0ca08 SC |
6268 | // skip over pbe_v2, probe_name text and provider text |
6269 | probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length(); | |
6270 | probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t); | |
aff5d390 | 6271 | } |
edce5b67 | 6272 | if (sess.verbose > 4) |
b530b5b3 | 6273 | clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "") |
aff5d390 | 6274 | << "@0x" << hex << pc << dec << endl; |
edce5b67 | 6275 | |
a794dbeb FCE |
6276 | if (dw.function_name_matches_pattern (probe_name, pp_mark) |
6277 | && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider))) | |
c57ea854 | 6278 | handle_probe_entry (); |
edce5b67 | 6279 | } |
edce5b67 JS |
6280 | } |
6281 | ||
6282 | ||
6846cfc8 | 6283 | void |
4ddb6dd0 | 6284 | sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start) |
6846cfc8 | 6285 | { |
a794dbeb FCE |
6286 | for (unsigned i=0; i<2; i++) { |
6287 | // prefer with-provider symbol; look without provider prefix for backward compatibility only | |
6288 | string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore"; | |
6289 | // XXX: multiple addresses? | |
6290 | if (sess.verbose > 2) | |
b530b5b3 | 6291 | clog << _F("looking for semaphore symbol %s ", semaphore.c_str()); |
a794dbeb | 6292 | |
79a0ca08 SC |
6293 | Dwarf_Addr addr; |
6294 | if (this->semaphore) | |
6295 | addr = this->semaphore; | |
6296 | else | |
6297 | addr = lookup_symbol_address(dw.module, semaphore.c_str()); | |
a794dbeb FCE |
6298 | if (addr) |
6299 | { | |
7d395255 | 6300 | if (dwfl_module_relocations (dw.module) > 0) |
a794dbeb FCE |
6301 | dwfl_module_relocate_address (dw.module, &addr); |
6302 | // XXX: relocation basis? | |
6303 | for (unsigned i = start; i < results.size(); ++i) | |
6304 | results[i]->sdt_semaphore_addr = addr; | |
6305 | if (sess.verbose > 2) | |
b530b5b3 | 6306 | clog << _(", found at 0x") << hex << addr << dec << endl; |
a794dbeb FCE |
6307 | return; |
6308 | } | |
6309 | else | |
6310 | if (sess.verbose > 2) | |
b530b5b3 | 6311 | clog << _(", not found") << endl; |
a794dbeb | 6312 | } |
6846cfc8 SC |
6313 | } |
6314 | ||
6315 | ||
edce5b67 JS |
6316 | void |
6317 | sdt_query::convert_probe (probe *base) | |
6318 | { | |
6319 | block *b = new block; | |
6320 | b->tok = base->body->tok; | |
6321 | ||
edce5b67 JS |
6322 | // Generate: if (arg1 != mark("label")) next; |
6323 | functioncall *fc = new functioncall; | |
bbafcb1e | 6324 | fc->function = "ulong_arg"; |
edce5b67 | 6325 | fc->tok = b->tok; |
bbafcb1e | 6326 | literal_number* num = new literal_number(1); |
edce5b67 JS |
6327 | num->tok = b->tok; |
6328 | fc->args.push_back(num); | |
6329 | ||
6330 | functioncall *fcus = new functioncall; | |
6331 | fcus->function = "user_string"; | |
6332 | fcus->type = pe_string; | |
6333 | fcus->tok = b->tok; | |
6334 | fcus->args.push_back(fc); | |
6335 | ||
6336 | if_statement *is = new if_statement; | |
6337 | is->thenblock = new next_statement; | |
6338 | is->elseblock = NULL; | |
6339 | is->tok = b->tok; | |
63ea4244 | 6340 | is->thenblock->tok = b->tok; |
edce5b67 JS |
6341 | comparison *be = new comparison; |
6342 | be->op = "!="; | |
6343 | be->tok = b->tok; | |
6344 | be->left = fcus; | |
6345 | be->right = new literal_string(probe_name); | |
63ea4244 | 6346 | be->right->tok = b->tok; |
edce5b67 JS |
6347 | is->condition = be; |
6348 | b->statements.push_back(is); | |
6349 | ||
6350 | // Now replace the body | |
6351 | b->statements.push_back(base->body); | |
6352 | base->body = b; | |
6353 | } | |
6354 | ||
6355 | ||
c72aa911 JS |
6356 | probe* |
6357 | sdt_query::convert_location () | |
edce5b67 | 6358 | { |
c72aa911 | 6359 | probe_point* specific_loc = new probe_point(*base_loc); |
662539d9 | 6360 | vector<probe_point::component*> derived_comps; |
edce5b67 | 6361 | |
662539d9 JS |
6362 | vector<probe_point::component*>::iterator it; |
6363 | for (it = specific_loc->components.begin(); | |
6364 | it != specific_loc->components.end(); ++it) | |
6365 | if ((*it)->functor == TOK_PROCESS) | |
6366 | { | |
1cc41cd6 DS |
6367 | // copy the process name |
6368 | derived_comps.push_back(*it); | |
662539d9 JS |
6369 | } |
6370 | else if ((*it)->functor == TOK_LIBRARY) | |
6371 | { | |
1cc41cd6 DS |
6372 | // copy the library name for process probes |
6373 | derived_comps.push_back(*it); | |
662539d9 JS |
6374 | } |
6375 | else if ((*it)->functor == TOK_PROVIDER) | |
6376 | { | |
6377 | // replace the possibly wildcarded arg with the specific provider name | |
6378 | *it = new probe_point::component(TOK_PROVIDER, | |
6379 | new literal_string(provider_name)); | |
6380 | } | |
6381 | else if ((*it)->functor == TOK_MARK) | |
c72aa911 JS |
6382 | { |
6383 | // replace the possibly wildcarded arg with the specific marker name | |
662539d9 JS |
6384 | *it = new probe_point::component(TOK_MARK, |
6385 | new literal_string(probe_name)); | |
a794dbeb | 6386 | |
aff5d390 SC |
6387 | if (sess.verbose > 3) |
6388 | switch (probe_type) | |
6389 | { | |
6390 | case uprobe1_type: | |
b530b5b3 | 6391 | clog << _("probe_type == uprobe1, use statement addr: 0x") |
aff5d390 SC |
6392 | << hex << pc << dec << endl; |
6393 | break; | |
6394 | case uprobe2_type: | |
b530b5b3 | 6395 | clog << _("probe_type == uprobe2, use statement addr: 0x") |
aff5d390 SC |
6396 | << hex << pc << dec << endl; |
6397 | break; | |
40fe32e0 | 6398 | case uprobe3_type: |
b530b5b3 | 6399 | clog << _("probe_type == uprobe3, use statement addr: 0x") |
40fe32e0 SC |
6400 | << hex << pc << dec << endl; |
6401 | break; | |
aff5d390 | 6402 | default: |
b530b5b3 LB |
6403 | clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s", |
6404 | pp_mark.c_str()) << endl; | |
aff5d390 SC |
6405 | } |
6406 | ||
c72aa911 JS |
6407 | switch (probe_type) |
6408 | { | |
aff5d390 SC |
6409 | case uprobe1_type: |
6410 | case uprobe2_type: | |
40fe32e0 | 6411 | case uprobe3_type: |
c72aa911 | 6412 | // process("executable").statement(probe_arg) |
662539d9 JS |
6413 | derived_comps.push_back |
6414 | (new probe_point::component(TOK_STATEMENT, | |
6415 | new literal_number(pc, true))); | |
c72aa911 JS |
6416 | break; |
6417 | ||
a794dbeb | 6418 | default: // deprecated |
c72aa911 | 6419 | // process("executable").function("*").label("_stapprobe1_MARK_NAME") |
662539d9 JS |
6420 | derived_comps.push_back |
6421 | (new probe_point::component(TOK_FUNCTION, | |
6422 | new literal_string("*"))); | |
6423 | derived_comps.push_back | |
c72aa911 | 6424 | (new probe_point::component(TOK_LABEL, |
a794dbeb | 6425 | new literal_string("_stapprobe1_" + pp_mark))); |
c72aa911 JS |
6426 | break; |
6427 | } | |
6428 | } | |
edce5b67 | 6429 | |
662539d9 JS |
6430 | probe_point* derived_loc = new probe_point(*specific_loc); |
6431 | derived_loc->components = derived_comps; | |
c72aa911 | 6432 | return base_probe->create_alias(derived_loc, specific_loc); |
edce5b67 JS |
6433 | } |
6434 | ||
6435 | ||
51d6bda3 SC |
6436 | void |
6437 | sdt_query::query_library (const char *library) | |
6438 | { | |
6439 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
6440 | } | |
6441 | ||
6442 | ||
20c6c071 | 6443 | void |
5227f1ea | 6444 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 6445 | probe * base, |
20c6c071 | 6446 | probe_point * location, |
86bf665e | 6447 | literal_map_t const & parameters, |
20c6c071 GH |
6448 | vector<derived_probe *> & finished_results) |
6449 | { | |
b20febf3 FCE |
6450 | // NB: the kernel/user dwlfpp objects are long-lived. |
6451 | // XXX: but they should be per-session, as this builder object | |
6452 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 6453 | |
7a24d422 | 6454 | dwflpp* dw = 0; |
6d5d594e | 6455 | literal_map_t filled_parameters = parameters; |
7a24d422 | 6456 | |
7a24d422 | 6457 | string module_name; |
ae2552da FCE |
6458 | if (has_null_param (parameters, TOK_KERNEL)) |
6459 | { | |
6460 | dw = get_kern_dw(sess, "kernel"); | |
6461 | } | |
6462 | else if (get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 6463 | { |
c523a015 LB |
6464 | size_t dash_pos = 0; |
6465 | while((dash_pos=module_name.find('-'))!=string::npos) | |
6466 | module_name.replace(int(dash_pos),1,"_"); | |
6467 | filled_parameters[TOK_MODULE] = new literal_string(module_name); | |
37001baa FCE |
6468 | // NB: glob patterns get expanded later, during the offline |
6469 | // elfutils module listing. | |
ae2552da | 6470 | dw = get_kern_dw(sess, module_name); |
b8da0ad1 | 6471 | } |
6d5d594e | 6472 | else if (get_param (parameters, TOK_PROCESS, module_name) || has_null_param(parameters, TOK_PROCESS)) |
84c84ac4 | 6473 | { |
05fb3e0c | 6474 | module_name = sess.sysroot + module_name; |
6d5d594e LB |
6475 | if(has_null_param(filled_parameters, TOK_PROCESS)) |
6476 | { | |
6477 | wordexp_t words; | |
6478 | int rc = wordexp(sess.cmd.c_str(), &words, WRDE_NOCMD|WRDE_UNDEF); | |
6479 | if(rc || words.we_wordc <= 0) | |
6480 | throw semantic_error(_("unspecified process probe is invalid without a -c COMMAND")); | |
05fb3e0c | 6481 | module_name = sess.sysroot + words.we_wordv[0]; |
6d5d594e LB |
6482 | filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map |
6483 | // in the case of TOK_MARK we need to modify locations as well | |
6484 | if(location->components[0]->functor==TOK_PROCESS && | |
6485 | location->components[0]->arg == 0) | |
6486 | location->components[0]->arg = new literal_string(module_name); | |
6487 | wordfree (& words); | |
6488 | } | |
5750ecc6 | 6489 | |
37001baa FCE |
6490 | // PR6456 process("/bin/*") glob handling |
6491 | if (contains_glob_chars (module_name)) | |
6492 | { | |
6493 | // Expand glob via rewriting the probe-point process("....") | |
6494 | // parameter, asserted to be the first one. | |
6495 | ||
6496 | assert (location->components.size() > 0); | |
6497 | assert (location->components[0]->functor == TOK_PROCESS); | |
6498 | assert (location->components[0]->arg); | |
6499 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
6500 | assert (lit); | |
6501 | ||
6502 | // Evaluate glob here, and call derive_probes recursively with each match. | |
6503 | glob_t the_blob; | |
6504 | int rc = glob (module_name.c_str(), 0, NULL, & the_blob); | |
b530b5b3 LB |
6505 | if (rc) |
6506 | throw semantic_error (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() )); | |
37001baa FCE |
6507 | for (unsigned i = 0; i < the_blob.gl_pathc; ++i) |
6508 | { | |
e19ebcf7 | 6509 | assert_no_interrupts(); |
37001baa FCE |
6510 | |
6511 | const char* globbed = the_blob.gl_pathv[i]; | |
6512 | struct stat st; | |
6513 | ||
6514 | if (access (globbed, X_OK) == 0 | |
6515 | && stat (globbed, &st) == 0 | |
6516 | && S_ISREG (st.st_mode)) // see find_executable() | |
6517 | { | |
7977a734 FCE |
6518 | // Need to call canonicalize here, in order to path-expand |
6519 | // patterns like process("stap*"). Otherwise it may go through | |
6520 | // to the next round of expansion as ("stap"), leading to a $PATH | |
6521 | // search that's not consistent with the glob search already done. | |
6522 | ||
6523 | char *cf = canonicalize_file_name (globbed); | |
6524 | if (cf) globbed = cf; | |
6525 | ||
37001baa FCE |
6526 | // synthesize a new probe_point, with the glob-expanded string |
6527 | probe_point *pp = new probe_point (*location); | |
5750ecc6 FCE |
6528 | // PR13338: quote results to prevent recursion |
6529 | string eglobbed = escape_glob_chars (globbed); | |
6530 | ||
6531 | if (sess.verbose > 1) | |
6532 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", | |
6533 | module_name.c_str(), eglobbed.c_str()) << endl; | |
05fb3e0c | 6534 | string eglobbed_tgt = path_remove_sysroot(sess, eglobbed); |
5750ecc6 | 6535 | |
37001baa | 6536 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 6537 | new literal_string (eglobbed_tgt)); |
37001baa FCE |
6538 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
6539 | pp->components[0] = ppc; | |
6540 | ||
7977a734 FCE |
6541 | probe* new_probe = new probe (*base, pp); |
6542 | ||
6543 | // We override "optional = true" here, as if the | |
6544 | // wildcarded probe point was given a "?" suffix. | |
6545 | ||
6546 | // This is because wildcard probes will be expected | |
6547 | // by users to apply only to some subset of the | |
6548 | // matching binaries, in the sense of "any", rather | |
6549 | // than "all", sort of similarly how | |
6550 | // module("*").function("...") patterns work. | |
6551 | ||
6552 | derive_probes (sess, new_probe, finished_results, | |
6553 | true /* NB: not location->optional */ ); | |
37001baa FCE |
6554 | } |
6555 | } | |
6556 | ||
6557 | globfree (& the_blob); | |
6558 | return; // avoid falling through | |
6559 | } | |
6560 | ||
5750ecc6 FCE |
6561 | // PR13338: unquote glob results |
6562 | module_name = unescape_glob_chars (module_name); | |
05fb3e0c | 6563 | user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it |
d1bcbe71 RH |
6564 | |
6565 | // if the executable starts with "#!", we look for the interpreter of the script | |
6566 | { | |
6567 | ifstream script_file (user_path.c_str () ); | |
6568 | ||
6569 | if (script_file.good ()) | |
6570 | { | |
6571 | string line; | |
6572 | ||
6573 | getline (script_file, line); | |
6574 | ||
6575 | if (line.compare (0, 2, "#!") == 0) | |
6576 | { | |
6577 | string path_head = line.substr(2); | |
6578 | ||
6579 | // remove white spaces at the beginning of the string | |
6580 | size_t p2 = path_head.find_first_not_of(" \t"); | |
6581 | ||
6582 | if (p2 != string::npos) | |
6583 | { | |
6584 | string path = path_head.substr(p2); | |
6585 | ||
6586 | // remove white spaces at the end of the string | |
6587 | p2 = path.find_last_not_of(" \t\n"); | |
6588 | if (string::npos != p2) | |
6589 | path.erase(p2+1); | |
6590 | ||
8e13c1a1 RH |
6591 | // handle "#!/usr/bin/env" redirect |
6592 | size_t offset = 0; | |
6593 | if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0) | |
6594 | { | |
6595 | offset = sizeof("/bin/env")-1; | |
6596 | } | |
6597 | else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0) | |
6598 | { | |
6599 | offset = sizeof("/usr/bin/env")-1; | |
6600 | } | |
6601 | ||
6602 | if (offset != 0) | |
6603 | { | |
6604 | size_t p3 = path.find_first_not_of(" \t", offset); | |
6605 | ||
6606 | if (p3 != string::npos) | |
6607 | { | |
6608 | string env_path = path.substr(p3); | |
05fb3e0c WF |
6609 | user_path = find_executable (env_path, sess.sysroot, |
6610 | sess.sysenv); | |
8e13c1a1 RH |
6611 | } |
6612 | } | |
6613 | else | |
6614 | { | |
05fb3e0c | 6615 | user_path = find_executable (path, sess.sysroot, sess.sysenv); |
8e13c1a1 | 6616 | } |
d1bcbe71 RH |
6617 | |
6618 | struct stat st; | |
6619 | ||
6620 | if (access (user_path.c_str(), X_OK) == 0 | |
6621 | && stat (user_path.c_str(), &st) == 0 | |
6622 | && S_ISREG (st.st_mode)) // see find_executable() | |
6623 | { | |
6624 | if (sess.verbose > 1) | |
b530b5b3 LB |
6625 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", |
6626 | module_name.c_str(), user_path.c_str()) << endl; | |
d1bcbe71 RH |
6627 | |
6628 | assert (location->components.size() > 0); | |
6629 | assert (location->components[0]->functor == TOK_PROCESS); | |
6630 | assert (location->components[0]->arg); | |
6631 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
6632 | assert (lit); | |
6633 | ||
6634 | // synthesize a new probe_point, with the expanded string | |
6635 | probe_point *pp = new probe_point (*location); | |
05fb3e0c | 6636 | string user_path_tgt = path_remove_sysroot(sess, user_path); |
d1bcbe71 | 6637 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 6638 | new literal_string (user_path_tgt.c_str())); |
d1bcbe71 RH |
6639 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
6640 | pp->components[0] = ppc; | |
6641 | ||
6642 | probe* new_probe = new probe (*base, pp); | |
6643 | ||
6644 | derive_probes (sess, new_probe, finished_results); | |
6645 | ||
6646 | script_file.close(); | |
6647 | return; | |
6648 | } | |
6649 | } | |
6650 | } | |
6651 | } | |
6652 | script_file.close(); | |
6653 | } | |
6654 | ||
47e226ed | 6655 | if (get_param (parameters, TOK_LIBRARY, user_lib) |
378d78b5 | 6656 | && user_lib.length() && ! contains_glob_chars (user_lib)) |
47e226ed SC |
6657 | { |
6658 | module_name = find_executable (user_lib, sess.sysroot, sess.sysenv, | |
6659 | "LD_LIBRARY_PATH"); | |
6660 | if (module_name.find('/') == string::npos) | |
6661 | // We didn't find user_lib so use iterate_over_libraries | |
6662 | module_name = user_path; | |
6663 | } | |
63b4fd14 | 6664 | else |
b642c901 | 6665 | module_name = user_path; // canonicalize it |
d0a7f5a9 | 6666 | |
2b69faaf JS |
6667 | if (kernel_supports_inode_uprobes(sess)) |
6668 | { | |
64e807c2 | 6669 | // XXX: autoconf this? |
2b69faaf JS |
6670 | if (has_null_param(parameters, TOK_RETURN)) |
6671 | throw semantic_error | |
6672 | (_("process return probes not available with inode-based uprobes")); | |
6673 | } | |
f4000852 MW |
6674 | // There is a similar check in pass 4 (buildrun), but it is |
6675 | // needed here too to make sure alternatives for optional | |
6676 | // (? or !) process probes are disposed and/or alternatives | |
6677 | // are selected. | |
5261f7ab | 6678 | check_process_probe_kernel_support(sess); |
e34d5d13 | 6679 | |
7a24d422 FCE |
6680 | // user-space target; we use one dwflpp instance per module name |
6681 | // (= program or shared library) | |
707bf35e | 6682 | dw = get_user_dw(sess, module_name); |
c8959a29 | 6683 | } |
20c6c071 | 6684 | |
5896cd05 | 6685 | if (sess.verbose > 3) |
b530b5b3 | 6686 | clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl; |
5896cd05 | 6687 | |
a794dbeb FCE |
6688 | string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __ |
6689 | if (get_param(parameters, TOK_MARK, dummy_mark_name)) | |
f28a8c28 | 6690 | { |
d8f31d0a | 6691 | assert(dw); |
51d6bda3 | 6692 | sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib); |
edce5b67 JS |
6693 | dw->iterate_over_modules(&query_module, &sdtq); |
6694 | return; | |
7a05f484 | 6695 | } |
20c6c071 | 6696 | |
8f14e444 | 6697 | unsigned results_pre = finished_results.size(); |
6d5d594e | 6698 | dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib); |
7a24d422 FCE |
6699 | |
6700 | // XXX: kernel.statement.absolute is a special case that requires no | |
6701 | // dwfl processing. This code should be in a separate builder. | |
7a24d422 | 6702 | if (q.has_kernel && q.has_absolute) |
37ebca01 | 6703 | { |
4baf0e53 | 6704 | // assert guru mode for absolute probes |
37ebca01 FCE |
6705 | if (! q.base_probe->privileged) |
6706 | { | |
e3bbc038 | 6707 | throw semantic_error (_("absolute statement probe in unprivileged script; need stap -g"), |
edce5b67 | 6708 | q.base_probe->tok); |
37ebca01 FCE |
6709 | } |
6710 | ||
6711 | // For kernel.statement(NUM).absolute probe points, we bypass | |
6712 | // all the debuginfo stuff: We just wire up a | |
6713 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 6714 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
6715 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
6716 | q.statement_num_val, q.statement_num_val, | |
6717 | q, 0); | |
37ebca01 | 6718 | finished_results.push_back (p); |
1a0dbc5a | 6719 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
6720 | return; |
6721 | } | |
6722 | ||
51178501 | 6723 | dw->iterate_over_modules(&query_module, &q); |
8f14e444 FCE |
6724 | |
6725 | ||
6726 | // PR11553 special processing: .return probes requested, but | |
6727 | // some inlined function instances matched. | |
6728 | unsigned i_n_r = q.inlined_non_returnable.size(); | |
6729 | unsigned results_post = finished_results.size(); | |
6730 | if (i_n_r > 0) | |
6731 | { | |
6732 | if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning | |
6733 | { | |
6734 | string quicklist; | |
6735 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
6736 | it != q.inlined_non_returnable.end(); | |
6737 | it++) | |
6738 | { | |
6739 | quicklist += " " + (*it); | |
6740 | if (quicklist.size() > 80) // heuristic, don't make an overlong report line | |
6741 | { | |
6742 | quicklist += " ..."; | |
6743 | break; | |
6744 | } | |
6745 | } | |
c57ea854 | 6746 | |
b530b5b3 LB |
6747 | sess.print_warning (_F(ngettext("cannot probe .return of %u inlined function %s", |
6748 | "cannot probe .return of %u inlined functions %s", | |
6749 | quicklist.size()), i_n_r, quicklist.c_str())); | |
8f14e444 FCE |
6750 | // There will be also a "no matches" semantic error generated. |
6751 | } | |
6752 | if (sess.verbose > 1) | |
b530b5b3 LB |
6753 | clog << _F(ngettext("skipped .return probe of %u inlined function", |
6754 | "skipped .return probe of %u inlined functions", i_n_r), i_n_r) << endl; | |
8f14e444 FCE |
6755 | if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity |
6756 | { | |
6757 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
6758 | it != q.inlined_non_returnable.end(); | |
6759 | it++) | |
6760 | clog << (*it) << " "; | |
6761 | clog << endl; | |
6762 | } | |
6763 | } // i_n_r > 0 | |
5f0a03a6 JK |
6764 | } |
6765 | ||
6766 | symbol_table::~symbol_table() | |
6767 | { | |
c9efa5c9 | 6768 | delete_map(map_by_addr); |
5f0a03a6 JK |
6769 | } |
6770 | ||
6771 | void | |
2867a2a1 | 6772 | symbol_table::add_symbol(const char *name, bool weak, bool descriptor, |
822a6a3d | 6773 | Dwarf_Addr addr, Dwarf_Addr */*high_addr*/) |
5f0a03a6 | 6774 | { |
ab91b232 JK |
6775 | #ifdef __powerpc__ |
6776 | // Map ".sys_foo" to "sys_foo". | |
6777 | if (name[0] == '.') | |
6778 | name++; | |
6779 | #endif | |
5f0a03a6 JK |
6780 | func_info *fi = new func_info(); |
6781 | fi->addr = addr; | |
6782 | fi->name = name; | |
ab91b232 | 6783 | fi->weak = weak; |
2867a2a1 | 6784 | fi->descriptor = descriptor; |
5f0a03a6 JK |
6785 | map_by_name[fi->name] = fi; |
6786 | // TODO: Use a multimap in case there are multiple static | |
6787 | // functions with the same name? | |
1c6b77e5 | 6788 | map_by_addr.insert(make_pair(addr, fi)); |
5f0a03a6 JK |
6789 | } |
6790 | ||
6791 | enum info_status | |
6792 | symbol_table::read_symbols(FILE *f, const string& path) | |
6793 | { | |
6794 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
6795 | int ret; | |
2e67a43b TM |
6796 | char *name = 0; |
6797 | char *mod = 0; | |
5f0a03a6 JK |
6798 | char type; |
6799 | unsigned long long addr; | |
6800 | Dwarf_Addr high_addr = 0; | |
6801 | int line = 0; | |
6802 | ||
6803 | // %as (non-POSIX) mallocs space for the string and stores its address. | |
6804 | while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0) | |
6805 | { | |
2e67a43b TM |
6806 | auto_free free_name(name); |
6807 | auto_free free_mod(mod); | |
5f0a03a6 JK |
6808 | line++; |
6809 | if (ret < 3) | |
6810 | { | |
3d372d6b | 6811 | cerr << _F("Symbol table error: Line %d of symbol list from %s is not in correct format: address type name [module]\n", |
b530b5b3 | 6812 | line, path.c_str()); |
5f0a03a6 JK |
6813 | // Caller should delete symbol_table object. |
6814 | return info_absent; | |
6815 | } | |
2e67a43b | 6816 | else if (ret > 3) |
5f0a03a6 JK |
6817 | { |
6818 | // Modules are loaded above the kernel, so if we're getting | |
6819 | // modules, we're done. | |
2e67a43b | 6820 | break; |
5f0a03a6 | 6821 | } |
ab91b232 | 6822 | if (type == 'T' || type == 't' || type == 'W') |
2867a2a1 | 6823 | add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr); |
5f0a03a6 JK |
6824 | } |
6825 | ||
1c6b77e5 | 6826 | if (map_by_addr.size() < 1) |
5f0a03a6 | 6827 | { |
3d372d6b | 6828 | cerr << _F("Symbol table error: %s contains no function symbols.\n", |
b530b5b3 | 6829 | path.c_str()) << endl; |
5f0a03a6 JK |
6830 | return info_absent; |
6831 | } | |
6832 | return info_present; | |
6833 | } | |
6834 | ||
6835 | // NB: This currently unused. We use get_from_elf() instead because | |
6836 | // that gives us raw addresses -- which we need for modules -- whereas | |
6837 | // nm provides the address relative to the beginning of the section. | |
6838 | enum info_status | |
83ca3872 | 6839 | symbol_table::read_from_elf_file(const string &path, |
2713ea24 | 6840 | systemtap_session &sess) |
5f0a03a6 | 6841 | { |
58502ae4 JS |
6842 | vector<string> cmd; |
6843 | cmd.push_back("/usr/bin/nm"); | |
6844 | cmd.push_back("-n"); | |
6845 | cmd.push_back("--defined-only"); | |
6846 | cmd.push_back("path"); | |
6847 | ||
5f0a03a6 | 6848 | FILE *f; |
58502ae4 JS |
6849 | int child_fd; |
6850 | pid_t child = stap_spawn_piped(sess.verbose, cmd, NULL, &child_fd); | |
6851 | if (child <= 0 || !(f = fdopen(child_fd, "r"))) | |
5f0a03a6 | 6852 | { |
58502ae4 | 6853 | // nm failures are detected by stap_waitpid |
3d372d6b | 6854 | cerr << _F("Internal error reading symbol table from %s -- %s\n", |
b530b5b3 | 6855 | path.c_str(), strerror(errno)); |
5f0a03a6 JK |
6856 | return info_absent; |
6857 | } | |
6858 | enum info_status status = read_symbols(f, path); | |
58502ae4 | 6859 | if (fclose(f) || stap_waitpid(sess.verbose, child)) |
5f0a03a6 | 6860 | { |
2713ea24 CM |
6861 | if (status == info_present) |
6862 | sess.print_warning("nm cannot read symbol table from " + path); | |
5f0a03a6 JK |
6863 | return info_absent; |
6864 | } | |
6865 | return status; | |
6866 | } | |
6867 | ||
6868 | enum info_status | |
83ca3872 | 6869 | symbol_table::read_from_text_file(const string& path, |
2713ea24 | 6870 | systemtap_session &sess) |
5f0a03a6 JK |
6871 | { |
6872 | FILE *f = fopen(path.c_str(), "r"); | |
6873 | if (!f) | |
6874 | { | |
2713ea24 | 6875 | sess.print_warning("cannot read symbol table from " + path + " -- " + strerror(errno)); |
5f0a03a6 JK |
6876 | return info_absent; |
6877 | } | |
6878 | enum info_status status = read_symbols(f, path); | |
6879 | (void) fclose(f); | |
6880 | return status; | |
6881 | } | |
6882 | ||
46f7b6be | 6883 | void |
f98c6346 | 6884 | symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused))) |
46f7b6be JK |
6885 | { |
6886 | #ifdef __powerpc__ | |
6887 | /* | |
6888 | * The .opd section contains function descriptors that can look | |
6889 | * just like function entry points. For example, there's a function | |
6890 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
6891 | * Reject all symbols in .opd. | |
6892 | */ | |
6893 | opd_section = SHN_UNDEF; | |
6894 | Dwarf_Addr bias; | |
6895 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
6896 | ?: dwfl_module_getelf (mod, &bias)); | |
6897 | Elf_Scn* scn = 0; | |
6898 | size_t shstrndx; | |
6899 | ||
6900 | if (!elf) | |
6901 | return; | |
fcc30d6d | 6902 | if (elf_getshdrstrndx (elf, &shstrndx) != 0) |
46f7b6be JK |
6903 | return; |
6904 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
6905 | { | |
6906 | GElf_Shdr shdr_mem; | |
6907 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
6908 | if (!shdr) | |
6909 | continue; | |
6910 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
6911 | if (!strcmp(name, ".opd")) | |
6912 | { | |
6913 | opd_section = elf_ndxscn(scn); | |
6914 | return; | |
6915 | } | |
6916 | } | |
6917 | #endif | |
6918 | } | |
6919 | ||
6920 | bool | |
6921 | symbol_table::reject_section(GElf_Word section) | |
6922 | { | |
6923 | if (section == SHN_UNDEF) | |
6924 | return true; | |
6925 | #ifdef __powerpc__ | |
6926 | if (section == opd_section) | |
6927 | return true; | |
6928 | #endif | |
6929 | return false; | |
6930 | } | |
6931 | ||
5f0a03a6 JK |
6932 | enum info_status |
6933 | symbol_table::get_from_elf() | |
6934 | { | |
6935 | Dwarf_Addr high_addr = 0; | |
6936 | Dwfl_Module *mod = mod_info->mod; | |
6937 | int syments = dwfl_module_getsymtab(mod); | |
6938 | assert(syments); | |
46f7b6be | 6939 | prepare_section_rejection(mod); |
5f0a03a6 JK |
6940 | for (int i = 1; i < syments; ++i) |
6941 | { | |
6942 | GElf_Sym sym; | |
ab91b232 JK |
6943 | GElf_Word section; |
6944 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
2867a2a1 | 6945 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC) |
ab91b232 | 6946 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
2867a2a1 | 6947 | reject_section(section), sym.st_value, &high_addr); |
5f0a03a6 JK |
6948 | } |
6949 | return info_present; | |
6950 | } | |
6951 | ||
5f0a03a6 JK |
6952 | func_info * |
6953 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
6954 | { | |
1c6b77e5 JS |
6955 | iterator_t iter = map_by_addr.upper_bound(addr); |
6956 | if (iter == map_by_addr.begin()) | |
5f0a03a6 | 6957 | return NULL; |
2e67a43b | 6958 | else |
1c6b77e5 | 6959 | return (--iter)->second; |
5f0a03a6 JK |
6960 | } |
6961 | ||
3d372d6b SC |
6962 | func_info * |
6963 | symbol_table::get_first_func() | |
6964 | { | |
6965 | iterator_t iter = map_by_addr.begin(); | |
6966 | return (iter)->second; | |
6967 | } | |
6968 | ||
5f0a03a6 JK |
6969 | func_info * |
6970 | symbol_table::lookup_symbol(const string& name) | |
6971 | { | |
6972 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
6973 | if (i == map_by_name.end()) | |
6974 | return NULL; | |
6975 | return i->second; | |
6976 | } | |
6977 | ||
6978 | Dwarf_Addr | |
6979 | symbol_table::lookup_symbol_address(const string& name) | |
6980 | { | |
6981 | func_info *fi = lookup_symbol(name); | |
6982 | if (fi) | |
6983 | return fi->addr; | |
6984 | return 0; | |
6985 | } | |
6986 | ||
ab91b232 JK |
6987 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
6988 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
6989 | // For system calls not implemented elsewhere, this weak symbol shows up | |
6990 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
6991 | // we refuse to consider such symbols. Here we delete them from our | |
6992 | // symbol table. | |
6993 | // TODO: Consider generalizing this and/or making it part of blacklist | |
6994 | // processing. | |
6995 | void | |
6996 | symbol_table::purge_syscall_stubs() | |
6997 | { | |
6998 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
6999 | if (stub_addr == 0) | |
7000 | return; | |
1c6b77e5 | 7001 | range_t purge_range = map_by_addr.equal_range(stub_addr); |
2e67a43b TM |
7002 | for (iterator_t iter = purge_range.first; |
7003 | iter != purge_range.second; | |
1c6b77e5 | 7004 | ) |
ab91b232 | 7005 | { |
1c6b77e5 | 7006 | func_info *fi = iter->second; |
2e67a43b | 7007 | if (fi->weak && fi->name != "sys_ni_syscall") |
ab91b232 | 7008 | { |
2e67a43b | 7009 | map_by_name.erase(fi->name); |
1c6b77e5 | 7010 | map_by_addr.erase(iter++); |
2e67a43b | 7011 | delete fi; |
2e67a43b | 7012 | } |
1c6b77e5 JS |
7013 | else |
7014 | iter++; | |
ab91b232 JK |
7015 | } |
7016 | } | |
7017 | ||
5f0a03a6 JK |
7018 | void |
7019 | module_info::get_symtab(dwarf_query *q) | |
7020 | { | |
7021 | systemtap_session &sess = q->sess; | |
7022 | ||
1c6b77e5 JS |
7023 | if (symtab_status != info_unknown) |
7024 | return; | |
7025 | ||
5f0a03a6 JK |
7026 | sym_table = new symbol_table(this); |
7027 | if (!elf_path.empty()) | |
7028 | { | |
2713ea24 CM |
7029 | if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()) |
7030 | sess.print_warning("reading symbol table from " + elf_path + " -- ignoring " + sess.kernel_symtab_path.c_str()); | |
5f0a03a6 JK |
7031 | symtab_status = sym_table->get_from_elf(); |
7032 | } | |
7033 | else | |
7034 | { | |
7035 | assert(name == TOK_KERNEL); | |
7036 | if (sess.kernel_symtab_path.empty()) | |
7037 | { | |
7038 | symtab_status = info_absent; | |
3d372d6b | 7039 | cerr << _("Error: Cannot find vmlinux.\n" |
b530b5b3 | 7040 | " Consider using --kmap instead of --kelf.") |
5f0a03a6 JK |
7041 | << endl;; |
7042 | } | |
7043 | else | |
7044 | { | |
7045 | symtab_status = | |
83ca3872 | 7046 | sym_table->read_from_text_file(sess.kernel_symtab_path, sess); |
5f0a03a6 JK |
7047 | if (symtab_status == info_present) |
7048 | { | |
7049 | sess.sym_kprobes_text_start = | |
7050 | sym_table->lookup_symbol_address("__kprobes_text_start"); | |
7051 | sess.sym_kprobes_text_end = | |
7052 | sym_table->lookup_symbol_address("__kprobes_text_end"); | |
7053 | sess.sym_stext = sym_table->lookup_symbol_address("_stext"); | |
5f0a03a6 JK |
7054 | } |
7055 | } | |
7056 | } | |
7057 | if (symtab_status == info_absent) | |
7058 | { | |
7059 | delete sym_table; | |
7060 | sym_table = NULL; | |
7061 | return; | |
7062 | } | |
7063 | ||
ab91b232 JK |
7064 | if (name == TOK_KERNEL) |
7065 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
7066 | } |
7067 | ||
1c6b77e5 JS |
7068 | // update_symtab reconciles data between the elf symbol table and the dwarf |
7069 | // function enumeration. It updates the symbol table entries with the dwarf | |
7070 | // die that describes the function, which also signals to query_module_symtab | |
7071 | // that a statement probe isn't needed. In return, it also adds aliases to the | |
7072 | // function table for names that share the same addr/die. | |
7073 | void | |
7074 | module_info::update_symtab(cu_function_cache_t *funcs) | |
7075 | { | |
7076 | if (!sym_table) | |
7077 | return; | |
7078 | ||
7079 | cu_function_cache_t new_funcs; | |
7080 | ||
7081 | for (cu_function_cache_t::iterator func = funcs->begin(); | |
7082 | func != funcs->end(); func++) | |
7083 | { | |
7084 | // optimization: inlines will never be in the symbol table | |
7085 | if (dwarf_func_inline(&func->second) != 0) | |
7086 | continue; | |
7087 | ||
1ffb8bd1 JS |
7088 | // XXX We may want to make additional efforts to match mangled elf names |
7089 | // to dwarf too. MIPS_linkage_name can help, but that's sometimes | |
7090 | // missing, so we may also need to try matching by address. See also the | |
7091 | // notes about _Z in dwflpp::iterate_over_functions(). | |
7092 | ||
1c6b77e5 JS |
7093 | func_info *fi = sym_table->lookup_symbol(func->first); |
7094 | if (!fi) | |
7095 | continue; | |
7096 | ||
7097 | // iterate over all functions at the same address | |
7098 | symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr); | |
7099 | for (symbol_table::iterator_t it = er.first; it != er.second; ++it) | |
7100 | { | |
7101 | // update this function with the dwarf die | |
7102 | it->second->die = func->second; | |
7103 | ||
7104 | // if this function is a new alias, then | |
7105 | // save it to merge into the function cache | |
7106 | if (it->second != fi) | |
b7478964 | 7107 | new_funcs.insert(make_pair(it->second->name, it->second->die)); |
1c6b77e5 JS |
7108 | } |
7109 | } | |
7110 | ||
7111 | // add all discovered aliases back into the function cache | |
7112 | // NB: this won't replace any names that dwarf may have already found | |
7113 | funcs->insert(new_funcs.begin(), new_funcs.end()); | |
7114 | } | |
7115 | ||
5f0a03a6 JK |
7116 | module_info::~module_info() |
7117 | { | |
7118 | if (sym_table) | |
7119 | delete sym_table; | |
b55bc428 FCE |
7120 | } |
7121 | ||
935447c8 | 7122 | // ------------------------------------------------------------------------ |
888af770 | 7123 | // user-space probes |
935447c8 DS |
7124 | // ------------------------------------------------------------------------ |
7125 | ||
935447c8 | 7126 | |
888af770 | 7127 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> |
935447c8 | 7128 | { |
89ba3085 FCE |
7129 | private: |
7130 | string make_pbm_key (uprobe_derived_probe* p) { | |
cfcab6c7 | 7131 | return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid); |
89ba3085 FCE |
7132 | } |
7133 | ||
cfcab6c7 JS |
7134 | void emit_module_maxuprobes (systemtap_session& s); |
7135 | ||
2b69faaf JS |
7136 | // Using our own utrace-based uprobes |
7137 | void emit_module_utrace_decls (systemtap_session& s); | |
7138 | void emit_module_utrace_init (systemtap_session& s); | |
7139 | void emit_module_utrace_exit (systemtap_session& s); | |
7140 | ||
7141 | // Using the upstream inode-based uprobes | |
7142 | void emit_module_inode_decls (systemtap_session& s); | |
7143 | void emit_module_inode_init (systemtap_session& s); | |
7144 | void emit_module_inode_exit (systemtap_session& s); | |
7145 | ||
3a894f7e JS |
7146 | // Using the dyninst backend (via stapdyn) |
7147 | void emit_module_dyninst_decls (systemtap_session& s); | |
7148 | void emit_module_dyninst_init (systemtap_session& s); | |
7149 | void emit_module_dyninst_exit (systemtap_session& s); | |
7150 | ||
935447c8 | 7151 | public: |
888af770 | 7152 | void emit_module_decls (systemtap_session& s); |
935447c8 DS |
7153 | void emit_module_init (systemtap_session& s); |
7154 | void emit_module_exit (systemtap_session& s); | |
7155 | }; | |
7156 | ||
7157 | ||
888af770 FCE |
7158 | void |
7159 | uprobe_derived_probe::join_group (systemtap_session& s) | |
7160 | { | |
7161 | if (! s.uprobe_derived_probes) | |
7162 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
7163 | s.uprobe_derived_probes->enroll (this); | |
4441e344 JS |
7164 | if (!s.is_usermode()) |
7165 | enable_task_finder(s); | |
a96d1db0 | 7166 | |
8a03658e | 7167 | // Ask buildrun.cxx to build extra module if needed, and |
d3e959b0 DS |
7168 | // signal staprun to load that module. If we're using the builtin |
7169 | // inode-uprobes, we still need to know that it is required. | |
8a03658e | 7170 | s.need_uprobes = true; |
a96d1db0 DN |
7171 | } |
7172 | ||
888af770 | 7173 | |
c0f84e7b SC |
7174 | void |
7175 | uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const | |
7176 | { | |
7177 | dwarf_derived_probe::getargs(arg_set); | |
7178 | arg_set.insert(arg_set.end(), args.begin(), args.end()); | |
7179 | } | |
7180 | ||
7181 | ||
7182 | void | |
7183 | uprobe_derived_probe::saveargs(int nargs) | |
7184 | { | |
7185 | for (int i = 1; i <= nargs; i++) | |
7186 | args.push_back("$arg" + lex_cast (i) + ":long"); | |
7187 | } | |
7188 | ||
7189 | ||
2865d17a | 7190 | void |
42e38653 | 7191 | uprobe_derived_probe::emit_privilege_assertion (translator_output* o) |
2865d17a DB |
7192 | { |
7193 | // These probes are allowed for unprivileged users, but only in the | |
7194 | // context of processes which they own. | |
7195 | emit_process_owner_assertion (o); | |
7196 | } | |
7197 | ||
7198 | ||
888af770 | 7199 | struct uprobe_builder: public derived_probe_builder |
a96d1db0 | 7200 | { |
888af770 | 7201 | uprobe_builder() {} |
2b69faaf | 7202 | virtual void build(systemtap_session & sess, |
a96d1db0 DN |
7203 | probe * base, |
7204 | probe_point * location, | |
86bf665e | 7205 | literal_map_t const & parameters, |
a96d1db0 DN |
7206 | vector<derived_probe *> & finished_results) |
7207 | { | |
888af770 | 7208 | int64_t process, address; |
a96d1db0 | 7209 | |
2b69faaf JS |
7210 | if (kernel_supports_inode_uprobes(sess)) |
7211 | throw semantic_error (_("absolute process probes not available with inode-based uprobes")); | |
7212 | ||
888af770 | 7213 | bool b1 = get_param (parameters, TOK_PROCESS, process); |
ced347a9 | 7214 | (void) b1; |
888af770 | 7215 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 7216 | (void) b2; |
888af770 FCE |
7217 | bool rr = has_null_param (parameters, TOK_RETURN); |
7218 | assert (b1 && b2); // by pattern_root construction | |
a96d1db0 | 7219 | |
0973d815 | 7220 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
a96d1db0 DN |
7221 | } |
7222 | }; | |
7223 | ||
7224 | ||
7225 | void | |
cfcab6c7 | 7226 | uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s) |
a96d1db0 | 7227 | { |
43241c44 FCE |
7228 | // We'll probably need at least this many: |
7229 | unsigned minuprobes = probes.size(); | |
7230 | // .. but we don't want so many that .bss is inflated (PR10507): | |
7231 | unsigned uprobesize = 64; | |
7232 | unsigned maxuprobesmem = 10*1024*1024; // 10 MB | |
7233 | unsigned maxuprobes = maxuprobesmem / uprobesize; | |
7234 | ||
aaf7ffe8 FCE |
7235 | // Let's choose a value on the geometric middle. This should end up |
7236 | // between minuprobes and maxuprobes. It's OK if this number turns | |
7237 | // out to be < minuprobes or > maxuprobes. At worst, we get a | |
7238 | // run-time error of one kind (too few: missed uprobe registrations) | |
7239 | // or another (too many: vmalloc errors at module load time). | |
7240 | unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes); | |
43241c44 | 7241 | |
6d0f3f0c | 7242 | s.op->newline() << "#ifndef MAXUPROBES"; |
43241c44 | 7243 | s.op->newline() << "#define MAXUPROBES " << default_maxuprobes; |
6d0f3f0c | 7244 | s.op->newline() << "#endif"; |
cfcab6c7 JS |
7245 | } |
7246 | ||
7247 | ||
7248 | void | |
7249 | uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s) | |
7250 | { | |
7251 | if (probes.empty()) return; | |
7252 | s.op->newline() << "/* ---- utrace uprobes ---- */"; | |
7253 | // If uprobes isn't in the kernel, pull it in from the runtime. | |
7254 | ||
7255 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; | |
7256 | s.op->newline() << "#include <linux/uprobes.h>"; | |
7257 | s.op->newline() << "#else"; | |
2ba1736a | 7258 | s.op->newline() << "#include \"linux/uprobes/uprobes.h\""; |
cfcab6c7 JS |
7259 | s.op->newline() << "#endif"; |
7260 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; | |
7261 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
7262 | s.op->newline() << "#endif"; | |
7263 | ||
7264 | emit_module_maxuprobes (s); | |
a96d1db0 | 7265 | |
cc52276b | 7266 | // Forward decls |
2ba1736a | 7267 | s.op->newline() << "#include \"linux/uprobes-common.h\""; |
cc52276b | 7268 | |
5e112f92 FCE |
7269 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
7270 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
cc52276b WC |
7271 | // XXX: consider a slab cache or somesuch for stap_uprobes |
7272 | s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];"; | |
5e112f92 | 7273 | s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
a96d1db0 | 7274 | |
89ba3085 FCE |
7275 | s.op->assert_0_indent(); |
7276 | ||
89ba3085 FCE |
7277 | // Assign task-finder numbers as we build up the stap_uprobe_tf table. |
7278 | // This means we process probes[] in two passes. | |
7279 | map <string,unsigned> module_index; | |
7280 | unsigned module_index_ctr = 0; | |
7281 | ||
cc52276b WC |
7282 | // not const since embedded task_finder_target struct changes |
7283 | s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {"; | |
89ba3085 FCE |
7284 | s.op->indent(1); |
7285 | for (unsigned i=0; i<probes.size(); i++) | |
7286 | { | |
7287 | uprobe_derived_probe *p = probes[i]; | |
7288 | string pbmkey = make_pbm_key (p); | |
7289 | if (module_index.find (pbmkey) == module_index.end()) | |
7290 | { | |
7291 | module_index[pbmkey] = module_index_ctr++; | |
7292 | ||
7293 | s.op->newline() << "{"; | |
7294 | // NB: it's essential that make_pbm_key() use all of and | |
7295 | // only the same fields as we're about to emit. | |
7296 | s.op->line() << " .finder={"; | |
7297 | if (p->pid != 0) | |
68910c97 JK |
7298 | s.op->line() << " .pid=" << p->pid << ","; |
7299 | ||
7300 | if (p->section == "") // .statement(addr).absolute | |
7301 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
89ba3085 FCE |
7302 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program |
7303 | { | |
7304 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
7305 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
7306 | } | |
68910c97 | 7307 | else if (p->section != ".absolute") // ET_DYN |
89ba3085 | 7308 | { |
4ad95bbc SC |
7309 | if (p->has_library) |
7310 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
89ba3085 FCE |
7311 | s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, "; |
7312 | s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, "; | |
19d91f6c | 7313 | s.op->line() << " .callback=&stap_uprobe_process_munmap,"; |
89ba3085 | 7314 | } |
89ba3085 | 7315 | s.op->line() << " },"; |
68910c97 JK |
7316 | if (p->module != "") |
7317 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
89ba3085 FCE |
7318 | s.op->line() << " },"; |
7319 | } | |
c57ea854 | 7320 | else |
822a6a3d | 7321 | { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it. |
89ba3085 FCE |
7322 | } |
7323 | s.op->newline(-1) << "};"; | |
7324 | ||
7325 | s.op->assert_0_indent(); | |
7326 | ||
cc52276b WC |
7327 | // NB: read-only structure |
7328 | s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {"; | |
a96d1db0 | 7329 | s.op->indent(1); |
888af770 FCE |
7330 | for (unsigned i =0; i<probes.size(); i++) |
7331 | { | |
7332 | uprobe_derived_probe* p = probes[i]; | |
7333 | s.op->newline() << "{"; | |
89ba3085 FCE |
7334 | string key = make_pbm_key (p); |
7335 | unsigned value = module_index[key]; | |
759e1d76 FCE |
7336 | if (value != 0) |
7337 | s.op->line() << " .tfi=" << value << ","; | |
6b66b9f7 | 7338 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
faea5e16 | 7339 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
4ddb6dd0 | 7340 | |
038c38c6 | 7341 | if (p->sdt_semaphore_addr != 0) |
63b4fd14 | 7342 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" |
038c38c6 | 7343 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; |
4ddb6dd0 JS |
7344 | |
7345 | if (p->has_return) | |
7346 | s.op->line() << " .return_p=1,"; | |
888af770 FCE |
7347 | s.op->line() << " },"; |
7348 | } | |
7349 | s.op->newline(-1) << "};"; | |
a96d1db0 | 7350 | |
89ba3085 FCE |
7351 | s.op->assert_0_indent(); |
7352 | ||
48e685da | 7353 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 7354 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
89ba3085 | 7355 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6eefe942 MW |
7356 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe", |
7357 | "_STP_PROBE_HANDLER_UPROBE"); | |
0e090c74 | 7358 | s.op->newline() << "if (sup->spec_index < 0 || " |
6e895029 DS |
7359 | << "sup->spec_index >= " << probes.size() << ") {"; |
7360 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 7361 | << "): %s\", sup->spec_index, c->probe_point);"; |
6e895029 DS |
7362 | s.op->newline() << "atomic_dec (&c->busy);"; |
7363 | s.op->newline() << "goto probe_epilogue;"; | |
7364 | s.op->newline(-1) << "}"; | |
d9aed31e | 7365 | s.op->newline() << "c->uregs = regs;"; |
92c25572 | 7366 | s.op->newline() << "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;"; |
6415ddde MW |
7367 | |
7368 | // Make it look like the IP is set as it would in the actual user | |
7369 | // task when calling real probe handler. Reset IP regs on return, so | |
7370 | // we don't confuse uprobes. PR10458 | |
7371 | s.op->newline() << "{"; | |
7372 | s.op->indent(1); | |
d9aed31e | 7373 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
259d54c0 | 7374 | s.op->newline() << "SET_REG_IP(regs, inst->vaddr);"; |
26e63673 | 7375 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 7376 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
7377 | s.op->newline(-1) << "}"; |
7378 | ||
7baf48e9 | 7379 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
888af770 | 7380 | s.op->newline(-1) << "}"; |
a96d1db0 | 7381 | |
48e685da | 7382 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 7383 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
89ba3085 | 7384 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6eefe942 MW |
7385 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe", |
7386 | "_STP_PROBE_HANDLER_URETPROBE"); | |
6dceb5c9 | 7387 | s.op->newline() << "c->ips.ri = inst;"; |
0e090c74 | 7388 | s.op->newline() << "if (sup->spec_index < 0 || " |
0d5561a5 DS |
7389 | << "sup->spec_index >= " << probes.size() << ") {"; |
7390 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 7391 | << "): %s\", sup->spec_index, c->probe_point);"; |
0d5561a5 DS |
7392 | s.op->newline() << "atomic_dec (&c->busy);"; |
7393 | s.op->newline() << "goto probe_epilogue;"; | |
7394 | s.op->newline(-1) << "}"; | |
7395 | ||
d9aed31e | 7396 | s.op->newline() << "c->uregs = regs;"; |
92c25572 | 7397 | s.op->newline() << "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;"; |
6415ddde MW |
7398 | |
7399 | // Make it look like the IP is set as it would in the actual user | |
7400 | // task when calling real probe handler. Reset IP regs on return, so | |
7401 | // we don't confuse uprobes. PR10458 | |
7402 | s.op->newline() << "{"; | |
7403 | s.op->indent(1); | |
d9aed31e | 7404 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
5e562a69 | 7405 | s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);"; |
26e63673 | 7406 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 7407 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
7408 | s.op->newline(-1) << "}"; |
7409 | ||
7baf48e9 | 7410 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
a96d1db0 DN |
7411 | s.op->newline(-1) << "}"; |
7412 | ||
89ba3085 | 7413 | s.op->newline(); |
2ba1736a | 7414 | s.op->newline() << "#include \"linux/uprobes-common.c\""; |
6d0f3f0c | 7415 | s.op->newline(); |
888af770 | 7416 | } |
935447c8 DS |
7417 | |
7418 | ||
888af770 | 7419 | void |
2b69faaf | 7420 | uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s) |
935447c8 | 7421 | { |
888af770 | 7422 | if (probes.empty()) return; |
935447c8 | 7423 | |
2b69faaf | 7424 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
935447c8 | 7425 | |
01b05e2e | 7426 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7427 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7428 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
7429 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
7430 | // initialized to zero. This is so that we can use | |
7431 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
7432 | s.op->newline(-1) << "}"; |
7433 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
935447c8 | 7434 | |
89ba3085 FCE |
7435 | // Set up the task_finders |
7436 | s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {"; | |
7437 | s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];"; | |
c57ea854 | 7438 | s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better |
89ba3085 | 7439 | s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);"; |
935447c8 | 7440 | |
5e112f92 FCE |
7441 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any |
7442 | // finders already registered, since mere registration does not | |
7443 | // cause any utrace or memory allocation actions. That happens only | |
7444 | // later, once the task finder engine starts running. So, for a | |
7445 | // partial initialization requiring unwind, we need do nothing. | |
7446 | s.op->newline() << "if (rc) break;"; | |
a7a68293 | 7447 | |
888af770 FCE |
7448 | s.op->newline(-1) << "}"; |
7449 | } | |
d0ea46ce | 7450 | |
d0a7f5a9 | 7451 | |
888af770 | 7452 | void |
2b69faaf | 7453 | uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s) |
888af770 FCE |
7454 | { |
7455 | if (probes.empty()) return; | |
2b69faaf | 7456 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
e56e51c9 | 7457 | |
6d0f3f0c FCE |
7458 | // NB: there is no stap_unregister_task_finder_target call; |
7459 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
7460 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
7461 | // | |
7462 | // This function blocks until all callbacks are completed, so there | |
7463 | // is supposed to be no possibility of any registration-related code starting | |
7464 | // to run in parallel with our shutdown here. So we don't need to protect the | |
7465 | // stap_uprobes[] array with the mutex. | |
d0a7f5a9 | 7466 | |
01b05e2e | 7467 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 | 7468 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
89ba3085 | 7469 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6d0f3f0c | 7470 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd | 7471 | |
8faa1fc5 | 7472 | // PR10655: decrement that ENABLED semaphore |
c116c31b | 7473 | s.op->newline() << "if (sup->sdt_sem_address) {"; |
8faa1fc5 FCE |
7474 | s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size |
7475 | s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);"; | |
7476 | s.op->newline() << "struct task_struct *tsk;"; | |
7477 | s.op->newline() << "rcu_read_lock();"; | |
6846cfc8 | 7478 | |
86229a55 DS |
7479 | // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes |
7480 | // that the pid is always in the global namespace, not in any | |
7481 | // private namespace. | |
8faa1fc5 | 7482 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)"; |
86229a55 DS |
7483 | // We'd like to call find_task_by_pid_ns() here, but it isn't |
7484 | // exported. So, we call what it calls... | |
7485 | s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);"; | |
8faa1fc5 FCE |
7486 | s.op->newline() << "#else"; |
7487 | s.op->newline() << " tsk = find_task_by_pid (pid);"; | |
7488 | s.op->newline() << "#endif /* 2.6.24 */"; | |
8faa1fc5 FCE |
7489 | |
7490 | s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching | |
3c5b8e2b | 7491 | s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {"; |
63b4fd14 | 7492 | s.op->newline(1) << "sdt_semaphore --;"; |
903b9fcd | 7493 | s.op->newline() << "#ifdef DEBUG_UPROBES"; |
c116c31b | 7494 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);"; |
903b9fcd | 7495 | s.op->newline() << "#endif"; |
3c5b8e2b | 7496 | s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);"; |
93c84191 | 7497 | s.op->newline(-1) << "}"; |
8faa1fc5 FCE |
7498 | // XXX: need to analyze possibility of race condition |
7499 | s.op->newline(-1) << "}"; | |
7500 | s.op->newline() << "rcu_read_unlock();"; | |
7501 | s.op->newline(-1) << "}"; | |
6846cfc8 | 7502 | |
3568f1dd FCE |
7503 | s.op->newline() << "if (sups->return_p) {"; |
7504 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
89ba3085 | 7505 | 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 | 7506 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
7507 | // NB: PR6829 does not change that we still need to unregister at |
7508 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
7509 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
7510 | s.op->newline(-1) << "} else {"; | |
7511 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
8faa1fc5 | 7512 | 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 |
7513 | s.op->newline() << "#endif"; |
7514 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
7515 | s.op->newline(-1) << "}"; | |
935447c8 | 7516 | |
6d0f3f0c | 7517 | s.op->newline() << "sup->spec_index = -1;"; |
935447c8 | 7518 | |
3568f1dd FCE |
7519 | // XXX: uprobe missed counts? |
7520 | ||
6d0f3f0c | 7521 | s.op->newline(-1) << "}"; |
935447c8 | 7522 | |
5e112f92 | 7523 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; |
935447c8 DS |
7524 | } |
7525 | ||
2b69faaf JS |
7526 | |
7527 | void | |
7528 | uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s) | |
7529 | { | |
7530 | if (probes.empty()) return; | |
7531 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 7532 | emit_module_maxuprobes (s); |
2ba1736a | 7533 | s.op->newline() << "#include \"linux/uprobes-inode.c\""; |
2b69faaf JS |
7534 | |
7535 | // Write the probe handler. | |
7536 | s.op->newline() << "static int enter_inode_uprobe " | |
7537 | << "(struct uprobe_consumer *inst, struct pt_regs *regs) {"; | |
cfcab6c7 JS |
7538 | s.op->newline(1) << "struct stapiu_consumer *sup = " |
7539 | << "container_of(inst, struct stapiu_consumer, consumer);"; | |
fe5acded JS |
7540 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sup->probe", |
7541 | "_STP_PROBE_HANDLER_UPROBE"); | |
3bff6634 JS |
7542 | s.op->newline() << "c->uregs = regs;"; |
7543 | s.op->newline() << "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;"; | |
2b69faaf JS |
7544 | // XXX: Can't set SET_REG_IP; we don't actually know the relocated address. |
7545 | // ... In some error cases, uprobes itself calls uprobes_get_bkpt_addr(). | |
7546 | s.op->newline() << "(*sup->probe->ph) (c);"; | |
7ee95b6b | 7547 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
2b69faaf JS |
7548 | s.op->newline() << "return 0;"; |
7549 | s.op->newline(-1) << "}"; | |
7550 | s.op->assert_0_indent(); | |
7551 | ||
7552 | // Index of all the modules for which we need inodes. | |
7553 | map<string, unsigned> module_index; | |
7554 | unsigned module_index_ctr = 0; | |
7555 | ||
7556 | // Discover and declare targets for each unique path. | |
cfcab6c7 | 7557 | s.op->newline() << "static struct stapiu_target " |
2b69faaf JS |
7558 | << "stap_inode_uprobe_targets[] = {"; |
7559 | s.op->indent(1); | |
7560 | for (unsigned i=0; i<probes.size(); i++) | |
7561 | { | |
7562 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
7563 | const string key = make_pbm_key(p); |
7564 | if (module_index.find (key) == module_index.end()) | |
2b69faaf | 7565 | { |
cfcab6c7 JS |
7566 | module_index[key] = module_index_ctr++; |
7567 | s.op->newline() << "{"; | |
7568 | s.op->line() << " .finder={"; | |
7569 | if (p->pid != 0) | |
7570 | s.op->line() << " .pid=" << p->pid << ","; | |
7571 | ||
7572 | if (p->section == "") // .statement(addr).absolute XXX? | |
7573 | s.op->line() << " .callback=&stapiu_process_found,"; | |
7574 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program | |
7575 | { | |
7576 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
7577 | s.op->line() << " .callback=&stapiu_process_found,"; | |
7578 | } | |
7579 | else if (p->section != ".absolute") // ET_DYN | |
7580 | { | |
7581 | if (p->has_library) | |
7582 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
7583 | s.op->line() << " .mmap_callback=&stapiu_mmap_found, "; | |
7584 | s.op->line() << " .munmap_callback=&stapiu_munmap_found, "; | |
7585 | s.op->line() << " .callback=&stapiu_process_munmap,"; | |
7586 | } | |
7587 | s.op->line() << " },"; | |
7588 | s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ","; | |
7589 | s.op->line() << " },"; | |
2b69faaf JS |
7590 | } |
7591 | } | |
7592 | s.op->newline(-1) << "};"; | |
7593 | s.op->assert_0_indent(); | |
7594 | ||
7595 | // Declare the actual probes. | |
cfcab6c7 | 7596 | s.op->newline() << "static struct stapiu_consumer " |
2b69faaf JS |
7597 | << "stap_inode_uprobe_consumers[] = {"; |
7598 | s.op->indent(1); | |
7599 | for (unsigned i=0; i<probes.size(); i++) | |
7600 | { | |
7601 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
7602 | unsigned index = module_index[make_pbm_key(p)]; |
7603 | s.op->newline() << "{"; | |
7604 | s.op->line() << " .consumer={ .handler=enter_inode_uprobe },"; | |
7605 | s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],"; | |
7606 | s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,"; | |
7607 | if (p->sdt_semaphore_addr) | |
7608 | s.op->line() << " .sdt_sem_offset=(loff_t)0x" | |
7609 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; | |
7610 | s.op->line() << " .probe=" << common_probe_init (p) << ","; | |
7611 | s.op->line() << " },"; | |
2b69faaf JS |
7612 | } |
7613 | s.op->newline(-1) << "};"; | |
7614 | s.op->assert_0_indent(); | |
7615 | } | |
7616 | ||
7617 | ||
7618 | void | |
7619 | uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s) | |
7620 | { | |
7621 | if (probes.empty()) return; | |
7622 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
b0f614a0 DS |
7623 | // Let stapiu_init() handle reporting errors by setting probe_point |
7624 | // to NULL. | |
7625 | s.op->newline() << "probe_point = NULL;"; | |
cfcab6c7 | 7626 | s.op->newline() << "rc = stapiu_init (" |
2b69faaf JS |
7627 | << "stap_inode_uprobe_targets, " |
7628 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
7629 | << "stap_inode_uprobe_consumers, " | |
7630 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
7631 | } | |
7632 | ||
7633 | ||
7634 | void | |
7635 | uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s) | |
7636 | { | |
7637 | if (probes.empty()) return; | |
7638 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 7639 | s.op->newline() << "stapiu_exit (" |
2b69faaf JS |
7640 | << "stap_inode_uprobe_targets, " |
7641 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
7642 | << "stap_inode_uprobe_consumers, " | |
7643 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
7644 | } | |
7645 | ||
7646 | ||
3a894f7e JS |
7647 | void |
7648 | uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s) | |
7649 | { | |
7650 | if (probes.empty()) return; | |
7651 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
7652 | emit_module_maxuprobes (s); | |
2466bf70 | 7653 | s.op->newline() << "#include \"dyninst/uprobes.c\""; |
3a894f7e JS |
7654 | |
7655 | // Discover and declare targets for dyninst to use | |
7656 | s.op->newline() << "const struct stapdu_target " | |
7657 | << "stap_dyninst_uprobe_targets[] " | |
7658 | << "__attribute__ ((section (\".stap_dyninst\"))) " | |
7659 | << "= {"; | |
7660 | s.op->indent(1); | |
7661 | for (unsigned i=0; i<probes.size(); i++) | |
7662 | { | |
7663 | uprobe_derived_probe *p = probes[i]; | |
7664 | s.op->newline() << "{"; | |
7665 | // XXX pid/procname filtering? | |
7666 | s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ","; | |
7667 | s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,"; | |
7668 | if (p->sdt_semaphore_addr) | |
7669 | s.op->line() << " .sdt_sem_offset=(loff_t)0x" | |
7670 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; | |
7671 | s.op->line() << " },"; | |
7672 | } | |
7673 | s.op->newline(-1) << "};"; | |
7674 | s.op->assert_0_indent(); | |
7675 | ||
7676 | // Declare the actual probes for the runtime to use | |
7677 | s.op->newline() << "static struct stapdu_consumer " | |
7678 | << "stap_dyninst_uprobe_consumers[] = {"; | |
7679 | s.op->indent(1); | |
7680 | for (unsigned i=0; i<probes.size(); i++) | |
7681 | { | |
7682 | uprobe_derived_probe *p = probes[i]; | |
7683 | s.op->newline() << "{"; | |
7684 | s.op->line() << " .probe=" << common_probe_init (p) << ","; | |
7685 | s.op->line() << " },"; | |
7686 | } | |
7687 | s.op->newline(-1) << "};"; | |
7688 | s.op->assert_0_indent(); | |
7689 | ||
7690 | // Write the probe handler. | |
7691 | // NB: not static, so dyninst can find it | |
7692 | s.op->newline() << "int enter_dyninst_uprobe " | |
7693 | << "(uint64_t index, struct pt_regs *regs) {"; | |
7694 | s.op->newline(1) << "struct stapdu_consumer *sup = " | |
7695 | << "&stap_dyninst_uprobe_consumers[index];"; | |
7696 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sup->probe", | |
7697 | "_STP_PROBE_HANDLER_UPROBE"); | |
7698 | s.op->newline() << "c->uregs = regs;"; | |
7699 | s.op->newline() << "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;"; | |
7700 | // XXX: once we have regs, check how dyninst sets the IP | |
7701 | // XXX: the way that dyninst rewrites stuff is probably going to be | |
7702 | // ... very confusing to our backtracer (at least if we stay in process) | |
7703 | s.op->newline() << "(*sup->probe->ph) (c);"; | |
7704 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); | |
7705 | s.op->newline() << "return 0;"; | |
7706 | s.op->newline(-1) << "}"; | |
7707 | s.op->assert_0_indent(); | |
7708 | } | |
7709 | ||
7710 | ||
7711 | void | |
7712 | uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s) | |
7713 | { | |
7714 | if (probes.empty()) return; | |
7715 | ||
7716 | /* stapdyn handles the dirty work via dyninst */ | |
7717 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
7718 | s.op->newline() << "/* this section left intentionally blank */"; | |
7719 | } | |
7720 | ||
7721 | ||
7722 | void | |
7723 | uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s) | |
7724 | { | |
7725 | if (probes.empty()) return; | |
7726 | ||
7727 | /* stapdyn handles the dirty work via dyninst */ | |
7728 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
7729 | s.op->newline() << "/* this section left intentionally blank */"; | |
7730 | } | |
7731 | ||
7732 | ||
2b69faaf JS |
7733 | void |
7734 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
7735 | { | |
4441e344 JS |
7736 | if (s.is_usermode()) |
7737 | emit_module_dyninst_decls (s); | |
7738 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
7739 | emit_module_inode_decls (s); |
7740 | else | |
7741 | emit_module_utrace_decls (s); | |
7742 | } | |
7743 | ||
7744 | ||
7745 | void | |
7746 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
7747 | { | |
4441e344 JS |
7748 | if (s.is_usermode()) |
7749 | emit_module_dyninst_init (s); | |
7750 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
7751 | emit_module_inode_init (s); |
7752 | else | |
7753 | emit_module_utrace_init (s); | |
7754 | } | |
7755 | ||
7756 | ||
7757 | void | |
7758 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7759 | { | |
4441e344 JS |
7760 | if (s.is_usermode()) |
7761 | emit_module_dyninst_exit (s); | |
7762 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
7763 | emit_module_inode_exit (s); |
7764 | else | |
7765 | emit_module_utrace_exit (s); | |
7766 | } | |
7767 | ||
7768 | ||
e6fe60e7 AM |
7769 | // ------------------------------------------------------------------------ |
7770 | // Kprobe derived probes | |
7771 | // ------------------------------------------------------------------------ | |
7772 | ||
4627ed58 | 7773 | static const string TOK_KPROBE("kprobe"); |
935447c8 | 7774 | |
bae55db9 | 7775 | struct kprobe_derived_probe: public derived_probe |
d0ea46ce | 7776 | { |
23dc94f6 DS |
7777 | kprobe_derived_probe (systemtap_session& sess, |
7778 | vector<derived_probe *> & results, | |
7779 | probe *base, | |
bae55db9 JS |
7780 | probe_point *location, |
7781 | const string& name, | |
7782 | int64_t stmt_addr, | |
7783 | bool has_return, | |
7784 | bool has_statement, | |
7785 | bool has_maxactive, | |
b642c901 SC |
7786 | bool has_path, |
7787 | bool has_library, | |
7788 | long maxactive_val, | |
7789 | const string& path, | |
7790 | const string& library | |
bae55db9 JS |
7791 | ); |
7792 | string symbol_name; | |
7793 | Dwarf_Addr addr; | |
7794 | bool has_return; | |
7795 | bool has_statement; | |
7796 | bool has_maxactive; | |
b642c901 SC |
7797 | bool has_path; |
7798 | bool has_library; | |
bae55db9 | 7799 | long maxactive_val; |
b642c901 SC |
7800 | string path; |
7801 | string library; | |
bae55db9 JS |
7802 | bool access_var; |
7803 | void printsig (std::ostream &o) const; | |
7804 | void join_group (systemtap_session& s); | |
7805 | }; | |
d0ea46ce | 7806 | |
bae55db9 JS |
7807 | struct kprobe_derived_probe_group: public derived_probe_group |
7808 | { | |
7809 | private: | |
7810 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
7811 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
d0ea46ce | 7812 | |
bae55db9 JS |
7813 | public: |
7814 | void enroll (kprobe_derived_probe* probe); | |
7815 | void emit_module_decls (systemtap_session& s); | |
7816 | void emit_module_init (systemtap_session& s); | |
7817 | void emit_module_exit (systemtap_session& s); | |
7818 | }; | |
d0ea46ce | 7819 | |
23dc94f6 DS |
7820 | struct kprobe_var_expanding_visitor: public var_expanding_visitor |
7821 | { | |
7822 | systemtap_session& sess; | |
7823 | block *add_block; | |
7824 | block *add_call_probe; // synthesized from .return probes with saved $vars | |
7825 | bool add_block_tid, add_call_probe_tid; | |
7826 | ||
7827 | kprobe_var_expanding_visitor(systemtap_session& sess): | |
7828 | sess(sess), add_block(NULL), add_call_probe(NULL), | |
7829 | add_block_tid(false), add_call_probe_tid(false) {} | |
7830 | ||
7831 | void visit_entry_op (entry_op* e); | |
7832 | }; | |
7833 | ||
7834 | ||
7835 | kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess, | |
7836 | vector<derived_probe *> & results, | |
7837 | probe *base, | |
e6fe60e7 | 7838 | probe_point *location, |
b6371390 | 7839 | const string& name, |
e6fe60e7 | 7840 | int64_t stmt_addr, |
b6371390 JS |
7841 | bool has_return, |
7842 | bool has_statement, | |
7843 | bool has_maxactive, | |
b642c901 SC |
7844 | bool has_path, |
7845 | bool has_library, | |
7846 | long maxactive_val, | |
7847 | const string& path, | |
7848 | const string& library | |
b6371390 | 7849 | ): |
4c5d1300 | 7850 | derived_probe (base, location, true /* .components soon rewritten */ ), |
e6fe60e7 | 7851 | symbol_name (name), addr (stmt_addr), |
b6371390 | 7852 | has_return (has_return), has_statement (has_statement), |
b642c901 SC |
7853 | has_maxactive (has_maxactive), has_path (has_path), |
7854 | has_library (has_library), | |
7855 | maxactive_val (maxactive_val), | |
7856 | path (path), library (library) | |
e6fe60e7 AM |
7857 | { |
7858 | this->tok = base->tok; | |
7859 | this->access_var = false; | |
d0ea46ce | 7860 | |
e6fe60e7 AM |
7861 | #ifndef USHRT_MAX |
7862 | #define USHRT_MAX 32767 | |
7863 | #endif | |
d0ea46ce | 7864 | |
46856d8d JS |
7865 | // Expansion of $target variables in the probe body produces an error during |
7866 | // translate phase, since we're not using debuginfo | |
d0ea46ce | 7867 | |
e6fe60e7 | 7868 | vector<probe_point::component*> comps; |
46856d8d | 7869 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 7870 | |
46856d8d JS |
7871 | if (has_statement) |
7872 | { | |
9ea68eb9 JS |
7873 | comps.push_back (new probe_point::component(TOK_STATEMENT, |
7874 | new literal_number(addr, true))); | |
46856d8d JS |
7875 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); |
7876 | } | |
7877 | else | |
7878 | { | |
7879 | size_t pos = name.find(':'); | |
7880 | if (pos != string::npos) | |
d0ea46ce | 7881 | { |
46856d8d JS |
7882 | string module = name.substr(0, pos); |
7883 | string function = name.substr(pos + 1); | |
7884 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
7885 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
7886 | } | |
7887 | else | |
7888 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 7889 | } |
d0ea46ce | 7890 | |
b6371390 JS |
7891 | if (has_return) |
7892 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
7893 | if (has_maxactive) | |
7894 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d0ea46ce | 7895 | |
23dc94f6 DS |
7896 | kprobe_var_expanding_visitor v (sess); |
7897 | v.replace (this->body); | |
7898 | ||
7899 | // If during target-variable-expanding the probe, we added a new block | |
7900 | // of code, add it to the start of the probe. | |
7901 | if (v.add_block) | |
7902 | this->body = new block(v.add_block, this->body); | |
7903 | ||
7904 | // If when target-variable-expanding the probe, we need to | |
7905 | // synthesize a sibling function-entry probe. We don't go through | |
7906 | // the whole probe derivation business (PR10642) that could lead to | |
7907 | // wildcard/alias resolution, or for that dwarf-induced duplication. | |
7908 | if (v.add_call_probe) | |
7909 | { | |
7910 | assert (has_return); | |
7911 | ||
7912 | // We temporarily replace base. | |
7913 | statement* old_body = base->body; | |
7914 | base->body = v.add_call_probe; | |
7915 | ||
7916 | derived_probe *entry_handler | |
7917 | = new kprobe_derived_probe (sess, results, base, location, name, 0, | |
7918 | false, has_statement, has_maxactive, | |
7919 | has_path, has_library, maxactive_val, | |
7920 | path, library); | |
7921 | results.push_back (entry_handler); | |
7922 | ||
7923 | base->body = old_body; | |
7924 | } | |
7925 | ||
e6fe60e7 AM |
7926 | this->sole_location()->components = comps; |
7927 | } | |
d0ea46ce | 7928 | |
e6fe60e7 AM |
7929 | void kprobe_derived_probe::printsig (ostream& o) const |
7930 | { | |
7931 | sole_location()->print (o); | |
7932 | o << " /* " << " name = " << symbol_name << "*/"; | |
7933 | printsig_nested (o); | |
7934 | } | |
d0ea46ce | 7935 | |
e6fe60e7 AM |
7936 | void kprobe_derived_probe::join_group (systemtap_session& s) |
7937 | { | |
d0ea46ce | 7938 | |
e6fe60e7 AM |
7939 | if (! s.kprobe_derived_probes) |
7940 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
7941 | s.kprobe_derived_probes->enroll (this); | |
d0ea46ce | 7942 | |
e6fe60e7 | 7943 | } |
d0ea46ce | 7944 | |
e6fe60e7 AM |
7945 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) |
7946 | { | |
7947 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
7948 | // probes of same symbol should share single kprobe/kretprobe | |
7949 | } | |
d0ea46ce | 7950 | |
e6fe60e7 AM |
7951 | void |
7952 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
7953 | { | |
7954 | if (probes_by_module.empty()) return; | |
d0ea46ce | 7955 | |
e6fe60e7 | 7956 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; |
d0ea46ce | 7957 | |
e6fe60e7 AM |
7958 | // Warn of misconfigured kernels |
7959 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
7960 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
7961 | s.op->newline() << "#endif"; | |
7962 | s.op->newline(); | |
d0ea46ce | 7963 | |
f07c3b68 | 7964 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 7965 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
7966 | s.op->newline() << "#endif"; |
7967 | ||
e6fe60e7 | 7968 | // Forward declare the master entry functions |
88747011 | 7969 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 7970 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 7971 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 | 7972 | s.op->line() << " struct pt_regs *regs);"; |
d0ea46ce | 7973 | |
e6fe60e7 AM |
7974 | // Emit an array of kprobe/kretprobe pointers |
7975 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
c9116e99 | 7976 | s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];"; |
e6fe60e7 | 7977 | s.op->newline() << "#endif"; |
d0ea46ce | 7978 | |
e6fe60e7 | 7979 | // Emit the actual probe list. |
d0ea46ce | 7980 | |
e6fe60e7 AM |
7981 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; |
7982 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
7983 | s.op->newline() << "#ifdef __ia64__"; | |
7984 | s.op->newline() << "struct kprobe dummy;"; | |
7985 | s.op->newline() << "#endif"; | |
7986 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
7987 | // NB: bss! | |
d0ea46ce | 7988 | |
e6fe60e7 AM |
7989 | s.op->newline() << "static struct stap_dwarfless_probe {"; |
7990 | s.op->newline(1) << "const unsigned return_p:1;"; | |
7991 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 7992 | s.op->newline() << "const unsigned optional_p:1;"; |
e6fe60e7 AM |
7993 | s.op->newline() << "unsigned registered_p:1;"; |
7994 | s.op->newline() << "const unsigned short maxactive_val;"; | |
935447c8 | 7995 | |
e6fe60e7 AM |
7996 | // Function Names are mostly small and uniform enough to justify putting |
7997 | // char[MAX]'s into the array instead of relocated char*'s. | |
935447c8 | 7998 | |
faea5e16 JS |
7999 | size_t symbol_string_name_max = 0; |
8000 | size_t symbol_string_name_tot = 0; | |
e6fe60e7 | 8001 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
6270adc1 | 8002 | { |
e6fe60e7 AM |
8003 | kprobe_derived_probe* p = it->second; |
8004 | #define DOIT(var,expr) do { \ | |
8005 | size_t var##_size = (expr) + 1; \ | |
8006 | var##_max = max (var##_max, var##_size); \ | |
8007 | var##_tot += var##_size; } while (0) | |
e6fe60e7 AM |
8008 | DOIT(symbol_string_name, p->symbol_name.size()); |
8009 | #undef DOIT | |
6270adc1 MH |
8010 | } |
8011 | ||
e6fe60e7 AM |
8012 | #define CALCIT(var) \ |
8013 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
935447c8 | 8014 | |
e6fe60e7 AM |
8015 | CALCIT(symbol_string); |
8016 | #undef CALCIT | |
6270adc1 | 8017 | |
bd659351 | 8018 | s.op->newline() << "unsigned long address;"; |
26e63673 | 8019 | s.op->newline() << "struct stap_probe * const probe;"; |
e6fe60e7 AM |
8020 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; |
8021 | s.op->indent(1); | |
6270adc1 | 8022 | |
e6fe60e7 AM |
8023 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
8024 | { | |
8025 | kprobe_derived_probe* p = it->second; | |
8026 | s.op->newline() << "{"; | |
8027 | if (p->has_return) | |
8028 | s.op->line() << " .return_p=1,"; | |
6270adc1 | 8029 | |
e6fe60e7 AM |
8030 | if (p->has_maxactive) |
8031 | { | |
8032 | s.op->line() << " .maxactive_p=1,"; | |
8033 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
8034 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
8035 | } | |
6270adc1 | 8036 | |
b350f56b JS |
8037 | if (p->locations[0]->optional) |
8038 | s.op->line() << " .optional_p=1,"; | |
8039 | ||
e6fe60e7 | 8040 | if (p->has_statement) |
c8d9d15e | 8041 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 8042 | else |
c8d9d15e | 8043 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
5d67b47c | 8044 | |
faea5e16 | 8045 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
e6fe60e7 | 8046 | s.op->line() << " },"; |
935447c8 DS |
8047 | } |
8048 | ||
e6fe60e7 | 8049 | s.op->newline(-1) << "};"; |
5d67b47c | 8050 | |
e6fe60e7 AM |
8051 | // Emit the kprobes callback function |
8052 | s.op->newline(); | |
88747011 | 8053 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
8054 | s.op->line() << " struct pt_regs *regs) {"; |
8055 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
8056 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8057 | // Check that the index is plausible | |
8058 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8059 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8060 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8061 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8062 | s.op->line() << "];"; | |
6eefe942 MW |
8063 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe", |
8064 | "_STP_PROBE_HANDLER_KPROBE"); | |
d9aed31e | 8065 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
8066 | |
8067 | // Make it look like the IP is set as it wouldn't have been replaced | |
8068 | // by a breakpoint instruction when calling real probe handler. Reset | |
8069 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
8070 | s.op->newline() << "{"; | |
8071 | s.op->indent(1); | |
d9aed31e | 8072 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 8073 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 8074 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 8075 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
8076 | s.op->newline(-1) << "}"; |
8077 | ||
7baf48e9 | 8078 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
e6fe60e7 AM |
8079 | s.op->newline() << "return 0;"; |
8080 | s.op->newline(-1) << "}"; | |
935447c8 | 8081 | |
e6fe60e7 AM |
8082 | // Same for kretprobes |
8083 | s.op->newline(); | |
88747011 | 8084 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
8085 | s.op->line() << " struct pt_regs *regs) {"; |
8086 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
935447c8 | 8087 | |
e6fe60e7 AM |
8088 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
8089 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8090 | // Check that the index is plausible | |
8091 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8092 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8093 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8094 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8095 | s.op->line() << "];"; | |
935447c8 | 8096 | |
6eefe942 MW |
8097 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe", |
8098 | "_STP_PROBE_HANDLER_KRETPROBE"); | |
d9aed31e | 8099 | s.op->newline() << "c->kregs = regs;"; |
6dceb5c9 | 8100 | s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic |
6415ddde MW |
8101 | |
8102 | // Make it look like the IP is set as it wouldn't have been replaced | |
8103 | // by a breakpoint instruction when calling real probe handler. Reset | |
8104 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
8105 | s.op->newline() << "{"; | |
8106 | s.op->indent(1); | |
d9aed31e | 8107 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 8108 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
26e63673 | 8109 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 8110 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
8111 | s.op->newline(-1) << "}"; |
8112 | ||
7baf48e9 | 8113 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
e6fe60e7 AM |
8114 | s.op->newline() << "return 0;"; |
8115 | s.op->newline(-1) << "}"; | |
bd659351 | 8116 | |
03a4ec63 | 8117 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
bd659351 MW |
8118 | s.op->newline() << "static int kprobe_resolve(void *data, const char *name,"; |
8119 | s.op->newline() << " struct module *owner,"; | |
8120 | s.op->newline() << " unsigned long val) {"; | |
8121 | s.op->newline(1) << "int i;"; | |
fc1d2aa2 MW |
8122 | s.op->newline() << "int *p = (int *) data;"; |
8123 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() | |
8124 | << " && *p > 0; i++) {"; | |
bd659351 MW |
8125 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
8126 | s.op->newline() << "if (! sdp->address)"; | |
fc1d2aa2 | 8127 | s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {"; |
bd659351 | 8128 | s.op->newline(1) << "sdp->address = val;"; |
fc1d2aa2 MW |
8129 | s.op->newline() << "(*p)--;"; |
8130 | s.op->newline(-1) << "}"; | |
8131 | s.op->newline(-2) << "}"; | |
8132 | s.op->newline() << "return (p > 0) ? 0 : -1;"; | |
bd659351 | 8133 | s.op->newline(-1) << "}"; |
03a4ec63 | 8134 | s.op->newline() << "#endif"; |
935447c8 DS |
8135 | } |
8136 | ||
e6fe60e7 | 8137 | |
6270adc1 | 8138 | void |
e6fe60e7 | 8139 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) |
6270adc1 | 8140 | { |
03a4ec63 | 8141 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
fc1d2aa2 MW |
8142 | s.op->newline() << "{"; |
8143 | s.op->newline(1) << "int p = 0;"; | |
8144 | s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {"; | |
8145 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8146 | s.op->newline() << "if (! sdp->address)"; | |
8147 | s.op->newline(1) << "p++;"; | |
8148 | s.op->newline(-2) << "}"; | |
8149 | s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);"; | |
8150 | s.op->newline(-1) << "}"; | |
03a4ec63 | 8151 | s.op->newline() << "#endif"; |
bd659351 | 8152 | |
e6fe60e7 | 8153 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 8154 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 8155 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e | 8156 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
03a4ec63 MW |
8157 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; |
8158 | ||
8159 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; | |
bd659351 MW |
8160 | s.op->newline() << "if (! addr) {"; |
8161 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
9319b767 MW |
8162 | s.op->newline() << "if (!sdp->optional_p)"; |
8163 | s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);"; | |
8164 | s.op->newline(-1) << "continue;"; | |
bd659351 | 8165 | s.op->newline(-1) << "}"; |
03a4ec63 MW |
8166 | s.op->newline() << "#endif"; |
8167 | ||
26e63673 | 8168 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
e6fe60e7 | 8169 | s.op->newline() << "if (sdp->return_p) {"; |
c8d9d15e | 8170 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
766cee5f | 8171 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 8172 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 8173 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
8174 | s.op->newline() << "if (sdp->maxactive_p) {"; |
8175 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
8176 | s.op->newline(-1) << "} else {"; | |
f07c3b68 | 8177 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
e6fe60e7 | 8178 | s.op->newline(-1) << "}"; |
88747011 | 8179 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
8180 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
8181 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 8182 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
766cee5f | 8183 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 8184 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
766cee5f | 8185 | s.op->newline() << "#endif"; |
c8d9d15e | 8186 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
e6fe60e7 AM |
8187 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
8188 | s.op->newline() << "if (rc == 0) {"; | |
8189 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
8190 | s.op->newline() << "if (rc != 0)"; | |
8191 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
8192 | s.op->newline(-2) << "}"; | |
8193 | s.op->newline() << "#else"; | |
8194 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
8195 | s.op->newline() << "#endif"; | |
8196 | s.op->newline(-1) << "} else {"; | |
8197 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 8198 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
766cee5f | 8199 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 8200 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 8201 | s.op->newline() << "#endif"; |
88747011 | 8202 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 8203 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 8204 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e | 8205 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
766cee5f | 8206 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 8207 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; |
766cee5f | 8208 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
8209 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
8210 | s.op->newline() << "if (rc == 0) {"; | |
8211 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
8212 | s.op->newline() << "if (rc != 0)"; | |
8213 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
8214 | s.op->newline(-2) << "}"; | |
8215 | s.op->newline() << "#else"; | |
8216 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
8217 | s.op->newline() << "#endif"; | |
8218 | s.op->newline(-1) << "}"; | |
8219 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
8220 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 8221 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 8222 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);"; |
b350f56b | 8223 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
e6fe60e7 AM |
8224 | // XXX: shall we increment numskipped? |
8225 | s.op->newline(-1) << "}"; | |
6270adc1 | 8226 | |
e6fe60e7 AM |
8227 | s.op->newline() << "else sdp->registered_p = 1;"; |
8228 | s.op->newline(-1) << "}"; // for loop | |
6270adc1 MH |
8229 | } |
8230 | ||
b4be7cbc | 8231 | |
e6fe60e7 AM |
8232 | void |
8233 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
935447c8 | 8234 | { |
e6fe60e7 AM |
8235 | //Unregister kprobes by batch interfaces. |
8236 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8237 | s.op->newline() << "j = 0;"; | |
8238 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8239 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8240 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8241 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8242 | s.op->newline() << "if (!sdp->return_p)"; | |
c9116e99 | 8243 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;"; |
e6fe60e7 | 8244 | s.op->newline(-2) << "}"; |
c9116e99 | 8245 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8246 | s.op->newline() << "j = 0;"; |
8247 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8248 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8249 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8250 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8251 | s.op->newline() << "if (sdp->return_p)"; | |
c9116e99 | 8252 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;"; |
e6fe60e7 | 8253 | s.op->newline(-2) << "}"; |
c9116e99 | 8254 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8255 | s.op->newline() << "#ifdef __ia64__"; |
8256 | s.op->newline() << "j = 0;"; | |
8257 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8258 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8259 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8260 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
c9116e99 | 8261 | s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;"; |
e6fe60e7 | 8262 | s.op->newline(-1) << "}"; |
c9116e99 | 8263 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8264 | s.op->newline() << "#endif"; |
8265 | s.op->newline() << "#endif"; | |
3e3bd7b6 | 8266 | |
e6fe60e7 AM |
8267 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
8268 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8269 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8270 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8271 | s.op->newline() << "if (sdp->return_p) {"; | |
8272 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8273 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
8274 | s.op->newline() << "#endif"; | |
8275 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; | |
8276 | s.op->newline() << "#ifdef STP_TIMING"; | |
8277 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 8278 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
e6fe60e7 AM |
8279 | s.op->newline(-1) << "#endif"; |
8280 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; | |
8281 | s.op->newline() << "#ifdef STP_TIMING"; | |
8282 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 8283 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; |
e6fe60e7 AM |
8284 | s.op->newline(-1) << "#endif"; |
8285 | s.op->newline(-1) << "} else {"; | |
8286 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8287 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
8288 | s.op->newline() << "#endif"; | |
8289 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; | |
8290 | s.op->newline() << "#ifdef STP_TIMING"; | |
8291 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 8292 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
e6fe60e7 AM |
8293 | s.op->newline(-1) << "#endif"; |
8294 | s.op->newline(-1) << "}"; | |
8295 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
8296 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
8297 | s.op->newline() << "#endif"; | |
8298 | s.op->newline() << "sdp->registered_p = 0;"; | |
8299 | s.op->newline(-1) << "}"; | |
f8a968bc JS |
8300 | } |
8301 | ||
e6fe60e7 | 8302 | struct kprobe_builder: public derived_probe_builder |
3c1b3d06 | 8303 | { |
9fdf787d DS |
8304 | private: |
8305 | bool cache_initialized; | |
8306 | vector<string> function_name_cache; | |
8307 | ||
8308 | void load_function_name_cache(systemtap_session & sess); | |
8309 | ||
8310 | public: | |
8311 | kprobe_builder(): cache_initialized(false) {} | |
8312 | ||
8313 | void build_no_more (systemtap_session &s) | |
8314 | { | |
8315 | if (! function_name_cache.empty()) | |
8316 | { | |
8317 | if (s.verbose > 3) | |
8318 | clog << _("kprobe_builder releasing cache") << endl; | |
8319 | function_name_cache.clear(); | |
8320 | } | |
8321 | } | |
8322 | ||
e6fe60e7 AM |
8323 | virtual void build(systemtap_session & sess, |
8324 | probe * base, | |
8325 | probe_point * location, | |
8326 | literal_map_t const & parameters, | |
8327 | vector<derived_probe *> & finished_results); | |
8328 | }; | |
3c1b3d06 FCE |
8329 | |
8330 | ||
9fdf787d DS |
8331 | void |
8332 | kprobe_builder::load_function_name_cache(systemtap_session &sess) | |
8333 | { | |
8334 | if (! cache_initialized) | |
8335 | { | |
8336 | cache_initialized = true; | |
8337 | string system_map_path = sess.kernel_build_tree + "/System.map"; | |
8338 | ifstream system_map; | |
8339 | ||
8340 | system_map.open(system_map_path.c_str(), ifstream::in); | |
8341 | if (! system_map.is_open()) | |
8342 | { | |
8343 | string system_map_path2 = "/boot/System.map-" + sess.kernel_release; | |
8344 | ||
8345 | system_map.clear(); | |
8346 | system_map.open(system_map_path2.c_str(), ifstream::in); | |
8347 | if (! system_map.is_open()) | |
8348 | { | |
8349 | if (sess.verbose > 3) | |
8350 | //TRANSLATORS: specific path cannot be opened | |
8351 | clog << system_map_path << _(" and ") | |
8352 | << system_map_path2 << _(" cannot be opened: ") | |
8353 | << strerror(errno) << endl; | |
8354 | return; | |
8355 | } | |
8356 | } | |
8357 | ||
8358 | string address, type, name; | |
8359 | do | |
8360 | { | |
8361 | system_map >> address >> type >> name; | |
8362 | ||
8363 | if (sess.verbose > 3) | |
8364 | clog << "'" << address << "' '" << type << "' '" << name | |
8365 | << "'" << endl; | |
8366 | ||
8367 | // 'T'/'t' are text code symbols | |
8368 | if (type != "t" && type != "T") | |
8369 | continue; | |
8370 | ||
a648015f DS |
8371 | #ifdef __powerpc__ |
8372 | // Map ".sys_foo" to "sys_foo". | |
8373 | if (name[0] == '.') | |
8374 | name.erase(0, 1); | |
8375 | #endif | |
8376 | ||
9fdf787d DS |
8377 | // FIXME: better things to do here - look for _stext before |
8378 | // remembering symbols. Also: | |
8379 | // - stop remembering names at ??? | |
8380 | // - what about __kprobes_text_start/__kprobes_text_end? | |
8381 | function_name_cache.push_back(name); | |
8382 | } | |
8383 | while (! system_map.eof()); | |
8384 | system_map.close(); | |
8385 | } | |
8386 | } | |
8387 | ||
79189b84 | 8388 | void |
05fb3e0c | 8389 | kprobe_builder::build(systemtap_session & sess, |
e6fe60e7 AM |
8390 | probe * base, |
8391 | probe_point * location, | |
8392 | literal_map_t const & parameters, | |
8393 | vector<derived_probe *> & finished_results) | |
79189b84 | 8394 | { |
e6fe60e7 | 8395 | string function_string_val, module_string_val; |
05fb3e0c | 8396 | string path, library, path_tgt, library_tgt; |
b6371390 JS |
8397 | int64_t statement_num_val = 0, maxactive_val = 0; |
8398 | bool has_function_str, has_module_str, has_statement_num; | |
8399 | bool has_absolute, has_return, has_maxactive; | |
b642c901 | 8400 | bool has_path, has_library; |
79189b84 | 8401 | |
b6371390 JS |
8402 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
8403 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
8404 | has_return = has_null_param (parameters, TOK_RETURN); | |
8405 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
8406 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
8407 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
b642c901 SC |
8408 | has_path = get_param (parameters, TOK_PROCESS, path); |
8409 | has_library = get_param (parameters, TOK_LIBRARY, library); | |
8410 | ||
8411 | if (has_path) | |
05fb3e0c WF |
8412 | { |
8413 | path = find_executable (path, sess.sysroot, sess.sysenv); | |
8414 | path_tgt = path_remove_sysroot(sess, path); | |
8415 | } | |
b642c901 | 8416 | if (has_library) |
05fb3e0c WF |
8417 | { |
8418 | library = find_executable (library, sess.sysroot, sess.sysenv, | |
8419 | "LD_LIBRARY_PATH"); | |
8420 | library_tgt = path_remove_sysroot(sess, library); | |
8421 | } | |
c57ea854 | 8422 | |
b6371390 | 8423 | if (has_function_str) |
6fb70fb7 | 8424 | { |
9fdf787d DS |
8425 | if (has_module_str) |
8426 | { | |
8427 | function_string_val = module_string_val + ":" + function_string_val; | |
8428 | derived_probe *dp | |
23dc94f6 DS |
8429 | = new kprobe_derived_probe (sess, finished_results, base, |
8430 | location, function_string_val, | |
9fdf787d DS |
8431 | 0, has_return, has_statement_num, |
8432 | has_maxactive, has_path, has_library, | |
8433 | maxactive_val, path_tgt, library_tgt); | |
8434 | finished_results.push_back (dp); | |
8435 | } | |
8436 | else | |
8437 | { | |
8438 | load_function_name_cache(sess); | |
86758d5f | 8439 | |
9fdf787d DS |
8440 | // Search function name list for matching names |
8441 | for (vector<string>::const_iterator it = function_name_cache.begin(); | |
8442 | it != function_name_cache.end(); it++) | |
8443 | { | |
8444 | // Below, "rc" has negative polarity: zero iff matching. | |
8445 | int rc = fnmatch(function_string_val.c_str(), (*it).c_str(), 0); | |
8446 | if (! rc) | |
8447 | { | |
8448 | derived_probe *dp | |
23dc94f6 DS |
8449 | = new kprobe_derived_probe (sess, finished_results, base, |
8450 | location, *it, 0, has_return, | |
9fdf787d DS |
8451 | has_statement_num, |
8452 | has_maxactive, has_path, | |
8453 | has_library, maxactive_val, | |
8454 | path_tgt, library_tgt); | |
8455 | finished_results.push_back (dp); | |
8456 | } | |
8457 | } | |
8458 | } | |
6fb70fb7 | 8459 | } |
e6fe60e7 | 8460 | else |
b6371390 JS |
8461 | { |
8462 | // assert guru mode for absolute probes | |
8463 | if ( has_statement_num && has_absolute && !base->privileged ) | |
e3bbc038 | 8464 | throw semantic_error (_("absolute statement probe in unprivileged script; need stap -g"), base->tok); |
b6371390 | 8465 | |
23dc94f6 DS |
8466 | finished_results.push_back (new kprobe_derived_probe (sess, |
8467 | finished_results, | |
8468 | base, | |
b6371390 JS |
8469 | location, "", |
8470 | statement_num_val, | |
8471 | has_return, | |
8472 | has_statement_num, | |
8473 | has_maxactive, | |
b642c901 SC |
8474 | has_path, |
8475 | has_library, | |
8476 | maxactive_val, | |
05fb3e0c WF |
8477 | path_tgt, |
8478 | library_tgt)); | |
96b030fe | 8479 | } |
79189b84 JS |
8480 | } |
8481 | ||
23dc94f6 DS |
8482 | |
8483 | void | |
8484 | kprobe_var_expanding_visitor::visit_entry_op (entry_op *e) | |
8485 | { | |
8486 | expression *repl = e; | |
8487 | ||
8488 | // expand the operand as if it weren't a return probe | |
8489 | replace (e->operand); | |
8490 | ||
8491 | // XXX it would be nice to use gen_kretprobe_saved_return when | |
8492 | // available, but it requires knowing the types already, which is | |
8493 | // problematic for arbitrary expressons. | |
8494 | repl = gen_mapped_saved_return (sess, e->operand, "entry", | |
8495 | add_block, add_block_tid, | |
8496 | add_call_probe, add_call_probe_tid); | |
8497 | provide (repl); | |
8498 | } | |
8499 | ||
8500 | ||
dd225250 PS |
8501 | // ------------------------------------------------------------------------ |
8502 | // Hardware breakpoint based probes. | |
8503 | // ------------------------------------------------------------------------ | |
8504 | ||
8505 | static const string TOK_HWBKPT("data"); | |
8506 | static const string TOK_HWBKPT_WRITE("write"); | |
8507 | static const string TOK_HWBKPT_RW("rw"); | |
8508 | static const string TOK_LENGTH("length"); | |
8509 | ||
8510 | #define HWBKPT_READ 0 | |
8511 | #define HWBKPT_WRITE 1 | |
8512 | #define HWBKPT_RW 2 | |
8513 | struct hwbkpt_derived_probe: public derived_probe | |
8514 | { | |
8515 | hwbkpt_derived_probe (probe *base, | |
8516 | probe_point *location, | |
8517 | uint64_t addr, | |
8518 | string symname, | |
8519 | unsigned int len, | |
8520 | bool has_only_read_access, | |
8521 | bool has_only_write_access, | |
8522 | bool has_rw_access | |
8523 | ); | |
8524 | Dwarf_Addr hwbkpt_addr; | |
8525 | string symbol_name; | |
8526 | unsigned int hwbkpt_access,hwbkpt_len; | |
8527 | ||
8528 | void printsig (std::ostream &o) const; | |
8529 | void join_group (systemtap_session& s); | |
8530 | }; | |
8531 | ||
8532 | struct hwbkpt_derived_probe_group: public derived_probe_group | |
8533 | { | |
dd225250 | 8534 | private: |
dac77b80 | 8535 | vector<hwbkpt_derived_probe*> hwbkpt_probes; |
dd225250 PS |
8536 | |
8537 | public: | |
8538 | void enroll (hwbkpt_derived_probe* probe, systemtap_session& s); | |
8539 | void emit_module_decls (systemtap_session& s); | |
8540 | void emit_module_init (systemtap_session& s); | |
8541 | void emit_module_exit (systemtap_session& s); | |
8542 | }; | |
8543 | ||
8544 | hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base, | |
9ea68eb9 JS |
8545 | probe_point *location, |
8546 | uint64_t addr, | |
8547 | string symname, | |
8548 | unsigned int len, | |
8549 | bool has_only_read_access, | |
8550 | bool has_only_write_access, | |
822a6a3d | 8551 | bool): |
4c5d1300 | 8552 | derived_probe (base, location, true /* .components soon rewritten */ ), |
dd225250 PS |
8553 | hwbkpt_addr (addr), |
8554 | symbol_name (symname), | |
8555 | hwbkpt_len (len) | |
8556 | { | |
8557 | this->tok = base->tok; | |
8558 | ||
8559 | vector<probe_point::component*> comps; | |
8560 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
8561 | ||
8562 | if (hwbkpt_addr) | |
9ea68eb9 JS |
8563 | comps.push_back (new probe_point::component (TOK_HWBKPT, |
8564 | new literal_number(hwbkpt_addr, true))); | |
8565 | else if (symbol_name.size()) | |
8566 | comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name))); | |
dd225250 PS |
8567 | |
8568 | comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len))); | |
8569 | ||
8570 | if (has_only_read_access) | |
9ea68eb9 | 8571 | this->hwbkpt_access = HWBKPT_READ ; |
dd225250 PS |
8572 | //TODO add code for comps.push_back for read, since this flag is not for x86 |
8573 | ||
8574 | else | |
9ea68eb9 JS |
8575 | { |
8576 | if (has_only_write_access) | |
8577 | { | |
8578 | this->hwbkpt_access = HWBKPT_WRITE ; | |
8579 | comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE)); | |
8580 | } | |
8581 | else | |
8582 | { | |
8583 | this->hwbkpt_access = HWBKPT_RW ; | |
8584 | comps.push_back (new probe_point::component(TOK_HWBKPT_RW)); | |
8585 | } | |
8586 | } | |
dd225250 PS |
8587 | |
8588 | this->sole_location()->components = comps; | |
8589 | } | |
8590 | ||
8591 | void hwbkpt_derived_probe::printsig (ostream& o) const | |
8592 | { | |
8593 | sole_location()->print (o); | |
8594 | printsig_nested (o); | |
8595 | } | |
8596 | ||
8597 | void hwbkpt_derived_probe::join_group (systemtap_session& s) | |
8598 | { | |
dac77b80 FCE |
8599 | if (! s.hwbkpt_derived_probes) |
8600 | s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group (); | |
dd225250 PS |
8601 | s.hwbkpt_derived_probes->enroll (this, s); |
8602 | } | |
8603 | ||
8604 | void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s) | |
8605 | { | |
dac77b80 FCE |
8606 | hwbkpt_probes.push_back (p); |
8607 | ||
8608 | unsigned max_hwbkpt_probes_by_arch = 0; | |
8609 | if (s.architecture == "i386" || s.architecture == "x86_64") | |
8610 | max_hwbkpt_probes_by_arch = 4; | |
8611 | else if (s.architecture == "s390") | |
8612 | max_hwbkpt_probes_by_arch = 1; | |
8613 | ||
c57ea854 | 8614 | if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch) |
2713ea24 | 8615 | s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)", |
b530b5b3 | 8616 | s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch)); |
dd225250 PS |
8617 | } |
8618 | ||
8619 | void | |
8620 | hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8621 | { | |
dac77b80 | 8622 | if (hwbkpt_probes.empty()) return; |
dd225250 PS |
8623 | |
8624 | s.op->newline() << "/* ---- hwbkpt-based probes ---- */"; | |
8625 | ||
8626 | s.op->newline() << "#include <linux/perf_event.h>"; | |
8627 | s.op->newline() << "#include <linux/hw_breakpoint.h>"; | |
8628 | s.op->newline(); | |
8629 | ||
8630 | // Forward declare the master entry functions | |
8631 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
8632 | s.op->line() << " int nmi,"; | |
8633 | s.op->line() << " struct perf_sample_data *data,"; | |
8634 | s.op->line() << " struct pt_regs *regs);"; | |
79189b84 | 8635 | |
dd225250 PS |
8636 | // Emit the actual probe list. |
8637 | ||
8638 | s.op->newline() << "static struct perf_event_attr "; | |
dac77b80 | 8639 | s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
8640 | |
8641 | s.op->newline() << "static struct perf_event **"; | |
dac77b80 | 8642 | s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
8643 | s.op->newline() << "static struct stap_hwbkpt_probe {"; |
8644 | s.op->newline() << "int registered_p:1;"; | |
43650b10 | 8645 | // registered_p = 0 signifies a probe that is unregistered (or failed) |
dd225250 PS |
8646 | // registered_p = 1 signifies a probe that got registered successfully |
8647 | ||
faea5e16 | 8648 | // Symbol Names are mostly small and uniform enough |
dd225250 | 8649 | // to justify putting const char*. |
dac77b80 | 8650 | s.op->newline() << "const char * const symbol;"; |
dd225250 PS |
8651 | |
8652 | s.op->newline() << "const unsigned long address;"; | |
8653 | s.op->newline() << "uint8_t atype;"; | |
bb0a4e12 | 8654 | s.op->newline() << "unsigned int len;"; |
26e63673 | 8655 | s.op->newline() << "struct stap_probe * const probe;"; |
dd225250 PS |
8656 | s.op->newline() << "} stap_hwbkpt_probes[] = {"; |
8657 | s.op->indent(1); | |
8658 | ||
dac77b80 | 8659 | for (unsigned int it = 0; it < hwbkpt_probes.size(); it++) |
dd225250 | 8660 | { |
dac77b80 | 8661 | hwbkpt_derived_probe* p = hwbkpt_probes.at(it); |
dd225250 | 8662 | s.op->newline() << "{"; |
dd225250 PS |
8663 | if (p->symbol_name.size()) |
8664 | s.op->line() << " .address=(unsigned long)0x0" << "ULL,"; | |
8665 | else | |
8666 | s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,"; | |
8667 | switch(p->hwbkpt_access){ | |
8668 | case HWBKPT_READ: | |
8669 | s.op->line() << " .atype=HW_BREAKPOINT_R ,"; | |
bb0a4e12 | 8670 | break; |
dd225250 PS |
8671 | case HWBKPT_WRITE: |
8672 | s.op->line() << " .atype=HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 8673 | break; |
dd225250 PS |
8674 | case HWBKPT_RW: |
8675 | s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 8676 | break; |
dd225250 PS |
8677 | }; |
8678 | s.op->line() << " .len=" << p->hwbkpt_len << ","; | |
faea5e16 | 8679 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
dd225250 | 8680 | s.op->line() << " .symbol=\"" << p->symbol_name << "\","; |
dd225250 PS |
8681 | s.op->line() << " },"; |
8682 | } | |
dac77b80 | 8683 | s.op->newline(-1) << "};"; |
dd225250 PS |
8684 | |
8685 | // Emit the hwbkpt callback function | |
8686 | s.op->newline() ; | |
8687 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
8688 | s.op->line() << " int nmi,"; | |
8689 | s.op->line() << " struct perf_sample_data *data,"; | |
8690 | s.op->line() << " struct pt_regs *regs) {"; | |
dac77b80 FCE |
8691 | s.op->newline(1) << "unsigned int i;"; |
8692 | s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;"; | |
8693 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; | |
8694 | s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
8695 | // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead? | |
8696 | 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) {"; | |
8697 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];"; | |
6eefe942 MW |
8698 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe", |
8699 | "_STP_PROBE_HANDLER_HWBKPT"); | |
d9aed31e MW |
8700 | s.op->newline() << "if (user_mode(regs)) {"; |
8701 | s.op->newline(1)<< "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;"; | |
8702 | s.op->newline() << "c->uregs = regs;"; | |
8703 | s.op->newline(-1) << "} else {"; | |
8704 | s.op->newline(1) << "c->kregs = regs;"; | |
8705 | s.op->newline(-1) << "}"; | |
26e63673 | 8706 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
7baf48e9 | 8707 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
dac77b80 | 8708 | s.op->newline(-1) << "}"; |
dd225250 PS |
8709 | s.op->newline(-1) << "}"; |
8710 | s.op->newline() << "return 0;"; | |
dac77b80 | 8711 | s.op->newline(-1) << "}"; |
dd225250 PS |
8712 | } |
8713 | ||
8714 | void | |
8715 | hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s) | |
8716 | { | |
dac77b80 | 8717 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 PS |
8718 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
8719 | s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
8720 | s.op->newline() << "void *addr = (void *) sdp->address;"; | |
8721 | s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;"; | |
dac77b80 FCE |
8722 | s.op->newline() << "hw_breakpoint_init(hp);"; |
8723 | s.op->newline() << "if (addr)"; | |
8724 | s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;"; | |
8725 | s.op->newline(-1) << "else { "; | |
8726 | s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);"; | |
8727 | s.op->newline() << "if (!hp->bp_addr) { "; | |
26e63673 | 8728 | s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);"; |
dac77b80 FCE |
8729 | s.op->newline() << "continue;"; |
8730 | s.op->newline(-1) << "}"; | |
8731 | s.op->newline(-1) << "}"; | |
8732 | s.op->newline() << "hp->bp_type = sdp->atype;"; | |
8733 | ||
8734 | // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!). | |
c57ea854 | 8735 | if (s.architecture == "i386" || s.architecture == "x86_64" ) |
dac77b80 FCE |
8736 | { |
8737 | s.op->newline() << "switch(sdp->len) {"; | |
8738 | s.op->newline() << "case 1:"; | |
8739 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;"; | |
8740 | s.op->newline() << "break;"; | |
8741 | s.op->newline(-1) << "case 2:"; | |
8742 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;"; | |
8743 | s.op->newline() << "break;"; | |
8744 | s.op->newline(-1) << "case 3:"; | |
8745 | s.op->newline() << "case 4:"; | |
8746 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;"; | |
8747 | s.op->newline() << "break;"; | |
8748 | s.op->newline(-1) << "case 5:"; | |
8749 | s.op->newline() << "case 6:"; | |
8750 | s.op->newline() << "case 7:"; | |
8751 | s.op->newline() << "case 8:"; | |
8752 | s.op->newline() << "default:"; // XXX: could instead reject | |
8753 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;"; | |
8754 | s.op->newline() << "break;"; | |
8755 | s.op->newline(-1) << "}"; | |
8756 | } | |
8757 | else // other architectures presumed straightforward | |
8758 | s.op->newline() << "hp->bp_len = sdp->len;"; | |
8759 | ||
26e63673 | 8760 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b273669e MW |
8761 | s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT"; |
8762 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);"; | |
8763 | s.op->newline() << "#else"; | |
dac77b80 | 8764 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);"; |
b273669e | 8765 | s.op->newline() << "#endif"; |
43650b10 | 8766 | s.op->newline() << "rc = 0;"; |
dac77b80 | 8767 | s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {"; |
43650b10 WC |
8768 | s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);"; |
8769 | s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;"; | |
8770 | s.op->newline(-1) << "}"; | |
217ef1f4 WC |
8771 | s.op->newline() << "if (rc) {"; |
8772 | s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);"; | |
43650b10 | 8773 | s.op->newline() << "sdp->registered_p = 0;"; |
dac77b80 | 8774 | s.op->newline(-1) << "}"; |
dd225250 | 8775 | s.op->newline() << " else sdp->registered_p = 1;"; |
dd225250 PS |
8776 | s.op->newline(-1) << "}"; // for loop |
8777 | } | |
8778 | ||
8779 | void | |
8780 | hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8781 | { | |
8782 | //Unregister hwbkpt probes. | |
dac77b80 | 8783 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 | 8784 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
dac77b80 FCE |
8785 | s.op->newline() << "if (sdp->registered_p == 0) continue;"; |
8786 | s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);"; | |
dd225250 PS |
8787 | s.op->newline() << "sdp->registered_p = 0;"; |
8788 | s.op->newline(-1) << "}"; | |
8789 | } | |
8790 | ||
8791 | struct hwbkpt_builder: public derived_probe_builder | |
8792 | { | |
8793 | hwbkpt_builder() {} | |
8794 | virtual void build(systemtap_session & sess, | |
8795 | probe * base, | |
8796 | probe_point * location, | |
8797 | literal_map_t const & parameters, | |
8798 | vector<derived_probe *> & finished_results); | |
8799 | }; | |
8800 | ||
8801 | void | |
8802 | hwbkpt_builder::build(systemtap_session & sess, | |
8803 | probe * base, | |
8804 | probe_point * location, | |
8805 | literal_map_t const & parameters, | |
8806 | vector<derived_probe *> & finished_results) | |
8807 | { | |
8808 | string symbol_str_val; | |
8809 | int64_t hwbkpt_address, len; | |
8810 | bool has_addr, has_symbol_str, has_write, has_rw, has_len; | |
8811 | ||
b47f3a55 | 8812 | if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y"))) |
b530b5b3 | 8813 | throw semantic_error (_("CONFIG_PERF_EVENTS not available on this kernel"), |
b47f3a55 FCE |
8814 | location->components[0]->tok); |
8815 | if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y"))) | |
b530b5b3 | 8816 | throw semantic_error (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"), |
b47f3a55 FCE |
8817 | location->components[0]->tok); |
8818 | ||
dd225250 PS |
8819 | has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address); |
8820 | has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val); | |
8821 | has_len = get_param (parameters, TOK_LENGTH, len); | |
8822 | has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end()); | |
8823 | has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end()); | |
8824 | ||
8825 | if (!has_len) | |
8826 | len = 1; | |
8827 | ||
8828 | if (has_addr) | |
8829 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
8830 | location, | |
8831 | hwbkpt_address, | |
8832 | "",len,0, | |
8833 | has_write, | |
8834 | has_rw)); | |
5d8a0aea | 8835 | else if (has_symbol_str) |
dd225250 PS |
8836 | finished_results.push_back (new hwbkpt_derived_probe (base, |
8837 | location, | |
8838 | 0, | |
8839 | symbol_str_val,len,0, | |
8840 | has_write, | |
8841 | has_rw)); | |
5d8a0aea FCE |
8842 | else |
8843 | assert (0); | |
dd225250 | 8844 | } |
342d3f96 | 8845 | |
0a6f5a3f JS |
8846 | // ------------------------------------------------------------------------ |
8847 | // statically inserted kernel-tracepoint derived probes | |
8848 | // ------------------------------------------------------------------------ | |
8849 | ||
6fb70fb7 | 8850 | struct tracepoint_arg |
79189b84 | 8851 | { |
ad370dcc | 8852 | string name, c_type, typecast; |
dcaa1a65 | 8853 | bool usable, used, isptr; |
f8a968bc | 8854 | Dwarf_Die type_die; |
dcaa1a65 | 8855 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 8856 | }; |
79189b84 | 8857 | |
0a6f5a3f JS |
8858 | struct tracepoint_derived_probe: public derived_probe |
8859 | { | |
79189b84 JS |
8860 | tracepoint_derived_probe (systemtap_session& s, |
8861 | dwflpp& dw, Dwarf_Die& func_die, | |
8862 | const string& tracepoint_name, | |
8863 | probe* base_probe, probe_point* location); | |
bc9a523d | 8864 | |
79189b84 | 8865 | systemtap_session& sess; |
6fb70fb7 JS |
8866 | string tracepoint_name, header; |
8867 | vector <struct tracepoint_arg> args; | |
bc9a523d | 8868 | |
6fb70fb7 | 8869 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
d0bfd2ac | 8870 | void getargs (std::list<std::string> &arg_set) const; |
79189b84 | 8871 | void join_group (systemtap_session& s); |
3e3bd7b6 | 8872 | void print_dupe_stamp(ostream& o); |
0a6f5a3f | 8873 | }; |
79189b84 JS |
8874 | |
8875 | ||
0a6f5a3f | 8876 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> |
79189b84 | 8877 | { |
79189b84 JS |
8878 | void emit_module_decls (systemtap_session& s); |
8879 | void emit_module_init (systemtap_session& s); | |
8880 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f | 8881 | }; |
79189b84 | 8882 | |
bc9a523d | 8883 | |
f8a968bc JS |
8884 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
8885 | { | |
8886 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
8887 | vector <struct tracepoint_arg>& args): | |
8888 | dw (dw), probe_name (probe_name), args (args) {} | |
8889 | dwflpp& dw; | |
8890 | const string& probe_name; | |
8891 | vector <struct tracepoint_arg>& args; | |
bc9a523d | 8892 | |
f8a968bc JS |
8893 | void visit_target_symbol (target_symbol* e); |
8894 | void visit_target_symbol_arg (target_symbol* e); | |
8895 | void visit_target_symbol_context (target_symbol* e); | |
8896 | }; | |
79189b84 JS |
8897 | |
8898 | ||
f8a968bc JS |
8899 | void |
8900 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
75ead1f7 | 8901 | { |
cc9001af | 8902 | string argname = e->sym_name(); |
75ead1f7 | 8903 | |
f8a968bc JS |
8904 | // search for a tracepoint parameter matching this name |
8905 | tracepoint_arg *arg = NULL; | |
8906 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 8907 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
8908 | { |
8909 | arg = &args[i]; | |
8910 | arg->used = true; | |
8911 | break; | |
8912 | } | |
75ead1f7 | 8913 | |
f8a968bc JS |
8914 | if (arg == NULL) |
8915 | { | |
8916 | stringstream alternatives; | |
8917 | for (unsigned i = 0; i < args.size(); ++i) | |
8918 | alternatives << " $" << args[i].name; | |
046e7190 | 8919 | alternatives << " $$name $$parms $$vars"; |
75ead1f7 | 8920 | |
f8a968bc JS |
8921 | // We hope that this value ends up not being referenced after all, so it |
8922 | // can be optimized out quietly. | |
b530b5b3 LB |
8923 | throw semantic_error(_F("unable to find tracepoint variable '%s' (alternatives: %s)", |
8924 | e->name.c_str(), alternatives.str().c_str()), e->tok); | |
f8a968bc JS |
8925 | // NB: we can have multiple errors, since a target variable |
8926 | // may be expanded in several different contexts: | |
8927 | // trace ("*") { $foo->bar } | |
f8a968bc | 8928 | } |
75ead1f7 | 8929 | |
f8a968bc | 8930 | // make sure we're not dereferencing base types |
dc5a09fc | 8931 | if (!arg->isptr) |
d19a9a82 | 8932 | e->assert_no_components("tracepoint", true); |
75ead1f7 | 8933 | |
f8a968bc JS |
8934 | // we can only write to dereferenced fields, and only if guru mode is on |
8935 | bool lvalue = is_active_lvalue(e); | |
8936 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
b3741c9d | 8937 | throw semantic_error(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok); |
c69a87e0 | 8938 | |
ad370dcc JS |
8939 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
8940 | // is also meaningless until you dereference past a pointer member. It's | |
8941 | // harder to detect and prevent that though... | |
75ead1f7 | 8942 | |
f8a968bc JS |
8943 | if (e->components.empty()) |
8944 | { | |
03c75a4a | 8945 | if (e->addressof) |
b530b5b3 | 8946 | throw semantic_error(_("cannot take address of tracepoint variable"), e->tok); |
a45664f4 | 8947 | |
3e3bd7b6 | 8948 | // Just grab the value from the probe locals |
a45664f4 JS |
8949 | symbol* sym = new symbol; |
8950 | sym->tok = e->tok; | |
8951 | sym->name = "__tracepoint_arg_" + arg->name; | |
8952 | provide (sym); | |
f8a968bc JS |
8953 | } |
8954 | else | |
8955 | { | |
5f36109e JS |
8956 | // make a copy of the original as a bare target symbol for the tracepoint |
8957 | // value, which will be passed into the dwarf dereferencing code | |
8958 | target_symbol* e2 = deep_copy_visitor::deep_copy(e); | |
8959 | e2->components.clear(); | |
8960 | ||
8961 | if (e->components.back().type == target_symbol::comp_pretty_print) | |
8962 | { | |
8963 | if (lvalue) | |
b530b5b3 | 8964 | throw semantic_error(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 8965 | |
d19a9a82 | 8966 | dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e); |
5f36109e JS |
8967 | dpp.expand()->visit (this); |
8968 | return; | |
8969 | } | |
8970 | ||
f8a968bc JS |
8971 | // Synthesize a function to dereference the dwarf fields, |
8972 | // with a pointer parameter that is the base tracepoint variable | |
8973 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 8974 | fdecl->synthetic = true; |
f8a968bc JS |
8975 | fdecl->tok = e->tok; |
8976 | embeddedcode *ec = new embeddedcode; | |
8977 | ec->tok = e->tok; | |
75ead1f7 | 8978 | |
f8a968bc | 8979 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") |
cc9001af | 8980 | + "_" + e->sym_name() |
aca66a36 | 8981 | + "_" + lex_cast(tick++)); |
75ead1f7 | 8982 | |
f8a968bc JS |
8983 | fdecl->name = fname; |
8984 | fdecl->body = ec; | |
75ead1f7 | 8985 | |
a5ce5211 | 8986 | ec->code += EMBEDDED_FETCH_DEREF(false); |
c69a87e0 | 8987 | ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e, |
f8a968bc | 8988 | lvalue, fdecl->type); |
75ead1f7 | 8989 | |
f8a968bc JS |
8990 | // Give the fdecl an argument for the raw tracepoint value |
8991 | vardecl *v1 = new vardecl; | |
8992 | v1->type = pe_long; | |
8993 | v1->name = "pointer"; | |
8994 | v1->tok = e->tok; | |
8995 | fdecl->formal_args.push_back(v1); | |
75ead1f7 | 8996 | |
6fda2dff JS |
8997 | // Any non-literal indexes need to be passed in too. |
8998 | for (unsigned i = 0; i < e->components.size(); ++i) | |
8999 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
9000 | { | |
9001 | vardecl *v = new vardecl; | |
9002 | v->type = pe_long; | |
aca66a36 | 9003 | v->name = "index" + lex_cast(i); |
6fda2dff JS |
9004 | v->tok = e->tok; |
9005 | fdecl->formal_args.push_back(v); | |
9006 | } | |
9007 | ||
f8a968bc JS |
9008 | if (lvalue) |
9009 | { | |
9010 | // Modify the fdecl so it carries a pe_long formal | |
9011 | // argument called "value". | |
75ead1f7 | 9012 | |
f8a968bc JS |
9013 | // FIXME: For the time being we only support setting target |
9014 | // variables which have base types; these are 'pe_long' in | |
9015 | // stap's type vocabulary. Strings and pointers might be | |
9016 | // reasonable, some day, but not today. | |
9017 | ||
9018 | vardecl *v2 = new vardecl; | |
9019 | v2->type = pe_long; | |
9020 | v2->name = "value"; | |
9021 | v2->tok = e->tok; | |
9022 | fdecl->formal_args.push_back(v2); | |
9023 | } | |
9024 | else | |
9025 | ec->code += "/* pure */"; | |
9026 | ||
64211010 | 9027 | ec->code += "/* unprivileged */"; |
a5ce5211 | 9028 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
aff5d390 | 9029 | |
f8809d54 | 9030 | fdecl->join (dw.sess); |
75ead1f7 | 9031 | |
f8a968bc JS |
9032 | // Synthesize a functioncall. |
9033 | functioncall* n = new functioncall; | |
9034 | n->tok = e->tok; | |
9035 | n->function = fname; | |
6fda2dff JS |
9036 | n->args.push_back(require(e2)); |
9037 | ||
9038 | // Any non-literal indexes need to be passed in too. | |
9039 | for (unsigned i = 0; i < e->components.size(); ++i) | |
9040 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
9041 | n->args.push_back(require(e->components[i].expr_index)); | |
75ead1f7 | 9042 | |
f8a968bc JS |
9043 | if (lvalue) |
9044 | { | |
9045 | // Provide the functioncall to our parent, so that it can be | |
9046 | // used to substitute for the assignment node immediately above | |
9047 | // us. | |
9048 | assert(!target_symbol_setter_functioncalls.empty()); | |
9049 | *(target_symbol_setter_functioncalls.top()) = n; | |
9050 | } | |
75ead1f7 | 9051 | |
f8a968bc JS |
9052 | provide (n); |
9053 | } | |
75ead1f7 JS |
9054 | } |
9055 | ||
9056 | ||
f8a968bc JS |
9057 | void |
9058 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
0a6f5a3f | 9059 | { |
03c75a4a | 9060 | if (e->addressof) |
b530b5b3 | 9061 | throw semantic_error(_("cannot take address of context variable"), e->tok); |
03c75a4a | 9062 | |
f8a968bc | 9063 | if (is_active_lvalue (e)) |
b530b5b3 | 9064 | throw semantic_error(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok); |
0a6f5a3f | 9065 | |
277c21bc | 9066 | if (e->name == "$$name") |
f8a968bc | 9067 | { |
5f36109e JS |
9068 | e->assert_no_components("tracepoint"); |
9069 | ||
bfdaad1e DS |
9070 | // Synthesize an embedded expression. |
9071 | embedded_expr *expr = new embedded_expr; | |
9072 | expr->tok = e->tok; | |
9073 | expr->code = string("/* string */ /* pure */ ") | |
6dceb5c9 | 9074 | + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\""); |
bfdaad1e | 9075 | provide (expr); |
f8a968bc | 9076 | } |
277c21bc | 9077 | else if (e->name == "$$vars" || e->name == "$$parms") |
f8a968bc | 9078 | { |
5f36109e JS |
9079 | e->assert_no_components("tracepoint", true); |
9080 | ||
f8a968bc JS |
9081 | token* pf_tok = new token(*e->tok); |
9082 | pf_tok->content = "sprintf"; | |
0a6f5a3f | 9083 | |
d5e178c1 | 9084 | print_format* pf = print_format::create(pf_tok); |
0a6f5a3f | 9085 | |
f8a968bc | 9086 | for (unsigned i = 0; i < args.size(); ++i) |
b278033a | 9087 | { |
dcaa1a65 JS |
9088 | if (!args[i].usable) |
9089 | continue; | |
f8a968bc JS |
9090 | if (i > 0) |
9091 | pf->raw_components += " "; | |
9092 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 9093 | target_symbol *tsym = new target_symbol; |
f8a968bc | 9094 | tsym->tok = e->tok; |
277c21bc | 9095 | tsym->name = "$" + args[i].name; |
5f36109e | 9096 | tsym->components = e->components; |
b278033a | 9097 | |
f8a968bc JS |
9098 | // every variable should always be accessible! |
9099 | tsym->saved_conversion_error = 0; | |
9100 | expression *texp = require (tsym); // NB: throws nothing ... | |
14a97852 JS |
9101 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
9102 | { | |
9103 | if (dw.sess.verbose>2) | |
e26c2f83 | 9104 | for (const semantic_error *c = tsym->saved_conversion_error; |
14a97852 | 9105 | c != 0; c = c->chain) |
b530b5b3 | 9106 | clog << _("variable location problem: ") << c->what() << endl; |
14a97852 JS |
9107 | pf->raw_components += "=?"; |
9108 | continue; | |
9109 | } | |
b278033a | 9110 | |
5f36109e JS |
9111 | if (!e->components.empty() && |
9112 | e->components[0].type == target_symbol::comp_pretty_print) | |
9113 | pf->raw_components += "=%s"; | |
9114 | else | |
9115 | pf->raw_components += args[i].isptr ? "=%p" : "=%#x"; | |
f8a968bc JS |
9116 | pf->args.push_back(texp); |
9117 | } | |
0a6f5a3f | 9118 | |
f8a968bc JS |
9119 | pf->components = print_format::string_to_components(pf->raw_components); |
9120 | provide (pf); | |
b278033a | 9121 | } |
f8a968bc JS |
9122 | else |
9123 | assert(0); // shouldn't get here | |
0a6f5a3f JS |
9124 | } |
9125 | ||
0a6f5a3f | 9126 | void |
f8a968bc | 9127 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a6f5a3f | 9128 | { |
aff5d390 | 9129 | try |
c69a87e0 | 9130 | { |
8fa5f271 MW |
9131 | assert(e->name.size() > 0 |
9132 | && ((e->name[0] == '$' && e->target_name == "") | |
9133 | || (e->name == "@var" && e->target_name != ""))); | |
aff5d390 | 9134 | |
277c21bc | 9135 | if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars") |
c69a87e0 | 9136 | visit_target_symbol_context (e); |
8fa5f271 MW |
9137 | else if (e->name == "@var") |
9138 | throw semantic_error(_("cannot use @var DWARF variables in tracepoints"), e->tok); | |
c69a87e0 FCE |
9139 | else |
9140 | visit_target_symbol_arg (e); | |
9141 | } | |
9142 | catch (const semantic_error &er) | |
9143 | { | |
1af1e62d | 9144 | e->chain (er); |
c69a87e0 FCE |
9145 | provide (e); |
9146 | } | |
0a6f5a3f JS |
9147 | } |
9148 | ||
9149 | ||
9150 | ||
79189b84 JS |
9151 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
9152 | dwflpp& dw, Dwarf_Die& func_die, | |
9153 | const string& tracepoint_name, | |
9154 | probe* base, probe_point* loc): | |
4c5d1300 | 9155 | derived_probe (base, loc, true /* .components soon rewritten */), |
79189b84 | 9156 | sess (s), tracepoint_name (tracepoint_name) |
56894e91 | 9157 | { |
79189b84 JS |
9158 | // create synthetic probe point name; preserve condition |
9159 | vector<probe_point::component*> comps; | |
9160 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
9161 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
9162 | this->sole_location()->components = comps; | |
9163 | ||
6fb70fb7 JS |
9164 | // fill out the available arguments in this tracepoint |
9165 | build_args(dw, func_die); | |
56894e91 | 9166 | |
6fb70fb7 JS |
9167 | // determine which header defined this tracepoint |
9168 | string decl_file = dwarf_decl_file(&func_die); | |
d4393459 FCE |
9169 | header = decl_file; |
9170 | ||
9171 | #if 0 /* This convention is not enforced. */ | |
6fb70fb7 JS |
9172 | size_t header_pos = decl_file.rfind("trace/"); |
9173 | if (header_pos == string::npos) | |
9174 | throw semantic_error ("cannot parse header location for tracepoint '" | |
9175 | + tracepoint_name + "' in '" | |
9176 | + decl_file + "'"); | |
9177 | header = decl_file.substr(header_pos); | |
d4393459 | 9178 | #endif |
56894e91 | 9179 | |
6fb70fb7 JS |
9180 | // tracepoints from FOO_event_types.h should really be included from FOO.h |
9181 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
9182 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 9183 | // XXX: see also PR9993. |
d4393459 | 9184 | size_t header_pos = header.find("_event_types"); |
6fb70fb7 JS |
9185 | if (header_pos != string::npos) |
9186 | header.erase(header_pos, 12); | |
56894e91 | 9187 | |
f8a968bc JS |
9188 | // Now expand the local variables in the probe body |
9189 | tracepoint_var_expanding_visitor v (dw, name, args); | |
8b095b45 | 9190 | v.replace (this->body); |
a45664f4 JS |
9191 | for (unsigned i = 0; i < args.size(); i++) |
9192 | if (args[i].used) | |
9193 | { | |
9194 | vardecl* v = new vardecl; | |
9195 | v->name = "__tracepoint_arg_" + args[i].name; | |
9196 | v->tok = this->tok; | |
58701b78 | 9197 | v->set_arity(0, this->tok); |
a45664f4 | 9198 | v->type = pe_long; |
69aa668e | 9199 | v->synthetic = true; |
a45664f4 JS |
9200 | this->locals.push_back (v); |
9201 | } | |
56894e91 | 9202 | |
79189b84 | 9203 | if (sess.verbose > 2) |
ce0f6648 | 9204 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl; |
79189b84 | 9205 | } |
dc38c0ae | 9206 | |
56894e91 | 9207 | |
f8a968bc | 9208 | static bool |
dcaa1a65 | 9209 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
46b84a80 | 9210 | { |
d19a9a82 | 9211 | Dwarf_Die type; |
dcaa1a65 | 9212 | switch (dwarf_tag(&arg.type_die)) |
b20febf3 | 9213 | { |
f8a968bc JS |
9214 | case DW_TAG_typedef: |
9215 | case DW_TAG_const_type: | |
9216 | case DW_TAG_volatile_type: | |
9217 | // iterate on the referent type | |
3d1ad340 | 9218 | return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die) |
dcaa1a65 | 9219 | && resolve_tracepoint_arg_type(arg)); |
f8a968bc | 9220 | case DW_TAG_base_type: |
a52d2ac0 | 9221 | case DW_TAG_enumeration_type: |
f8a968bc | 9222 | // base types will simply be treated as script longs |
dcaa1a65 | 9223 | arg.isptr = false; |
f8a968bc JS |
9224 | return true; |
9225 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
9226 | // pointers can be treated as script longs, |
9227 | // and if we know their type, they can also be dereferenced | |
d19a9a82 JS |
9228 | type = arg.type_die; |
9229 | while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)) | |
9230 | { | |
9231 | // It still might be a non-type, e.g. const void, | |
9232 | // so we need to strip away all qualifiers. | |
9233 | int tag = dwarf_tag(&arg.type_die); | |
9234 | if (tag != DW_TAG_typedef && | |
9235 | tag != DW_TAG_const_type && | |
9236 | tag != DW_TAG_volatile_type) | |
9237 | { | |
9238 | arg.isptr = true; | |
9239 | break; | |
9240 | } | |
9241 | } | |
9242 | if (!arg.isptr) | |
9243 | arg.type_die = type; | |
ad370dcc JS |
9244 | arg.typecast = "(intptr_t)"; |
9245 | return true; | |
9246 | case DW_TAG_structure_type: | |
9247 | case DW_TAG_union_type: | |
9248 | // for structs/unions which are passed by value, we turn it into | |
9249 | // a pointer that can be dereferenced. | |
9250 | arg.isptr = true; | |
9251 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 9252 | return true; |
f8a968bc JS |
9253 | default: |
9254 | // should we consider other types too? | |
9255 | return false; | |
b20febf3 | 9256 | } |
56894e91 JS |
9257 | } |
9258 | ||
9259 | ||
9260 | void | |
822a6a3d | 9261 | tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die) |
56894e91 | 9262 | { |
6fb70fb7 JS |
9263 | Dwarf_Die arg; |
9264 | if (dwarf_child(&func_die, &arg) == 0) | |
9265 | do | |
9266 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
9267 | { | |
9268 | // build a tracepoint_arg for this parameter | |
9269 | tracepoint_arg tparg; | |
23d106b9 | 9270 | tparg.name = dwarf_diename(&arg); |
56894e91 | 9271 | |
6fb70fb7 | 9272 | // read the type of this parameter |
3d1ad340 | 9273 | if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die) |
f1c8f8a5 | 9274 | || !dwarf_type_name(&tparg.type_die, tparg.c_type)) |
ce0f6648 LB |
9275 | throw semantic_error (_F("cannot get type of parameter '%s' of tracepoint '%s'", |
9276 | tparg.name.c_str(), tracepoint_name.c_str())); | |
a68f81a2 | 9277 | |
dcaa1a65 | 9278 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
9279 | args.push_back(tparg); |
9280 | if (sess.verbose > 4) | |
a52d2ac0 JS |
9281 | clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s", |
9282 | tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(), | |
9283 | tparg.usable ? "ok" : "unavailable") << endl; | |
6fb70fb7 JS |
9284 | } |
9285 | while (dwarf_siblingof(&arg, &arg) == 0); | |
56894e91 JS |
9286 | } |
9287 | ||
dc38c0ae | 9288 | void |
d0bfd2ac | 9289 | tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const |
dc38c0ae | 9290 | { |
dcaa1a65 JS |
9291 | for (unsigned i = 0; i < args.size(); ++i) |
9292 | if (args[i].usable) | |
d0bfd2ac | 9293 | arg_set.push_back("$"+args[i].name+":"+args[i].c_type); |
dc38c0ae DS |
9294 | } |
9295 | ||
79189b84 JS |
9296 | void |
9297 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
197a4d62 | 9298 | { |
79189b84 JS |
9299 | if (! s.tracepoint_derived_probes) |
9300 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
9301 | s.tracepoint_derived_probes->enroll (this); | |
9302 | } | |
e38d6504 | 9303 | |
56894e91 | 9304 | |
197a4d62 | 9305 | void |
3e3bd7b6 | 9306 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) |
56894e91 | 9307 | { |
3e3bd7b6 JS |
9308 | for (unsigned i = 0; i < args.size(); i++) |
9309 | if (args[i].used) | |
9310 | o << "__tracepoint_arg_" << args[i].name << endl; | |
197a4d62 | 9311 | } |
56894e91 | 9312 | |
3e3bd7b6 | 9313 | |
c9ccb642 | 9314 | static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header) |
47dd066d | 9315 | { |
3c1b3d06 FCE |
9316 | vector<string> they_live; |
9317 | // PR 9993 | |
9318 | // XXX: may need this to be configurable | |
d4393459 | 9319 | they_live.push_back ("#include <linux/skbuff.h>"); |
9e0cd21a FCE |
9320 | |
9321 | // PR11649: conditional extra header | |
9322 | // for kvm tracepoints in 2.6.33ish | |
9323 | if (s.kernel_config["CONFIG_KVM"] != string("")) { | |
d4393459 FCE |
9324 | they_live.push_back ("#include <linux/kvm_host.h>"); |
9325 | } | |
9326 | ||
50b72692 | 9327 | if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) { |
d4393459 | 9328 | they_live.push_back ("#define XFS_BIG_BLKNOS 1"); |
88637c31 FCE |
9329 | if (s.kernel_source_tree != "") |
9330 | they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree | |
d4393459 FCE |
9331 | they_live.push_back ("struct xfs_mount;"); |
9332 | they_live.push_back ("struct xfs_inode;"); | |
9333 | they_live.push_back ("struct xfs_buf;"); | |
9334 | they_live.push_back ("struct xfs_bmbt_irec;"); | |
c2cf1b87 | 9335 | they_live.push_back ("struct xfs_trans;"); |
9e0cd21a | 9336 | } |
d4393459 | 9337 | |
50b72692 | 9338 | if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) { |
d4393459 FCE |
9339 | they_live.push_back ("struct rpc_task;"); |
9340 | } | |
9341 | ||
9342 | they_live.push_back ("#include <asm/cputime.h>"); | |
9343 | ||
c2cf1b87 FCE |
9344 | // linux 3.0 |
9345 | they_live.push_back ("struct cpu_workqueue_struct;"); | |
9346 | ||
50b72692 | 9347 | if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string("")) |
c2cf1b87 FCE |
9348 | if (s.kernel_source_tree != "") |
9349 | they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree | |
9350 | ||
50b72692 HP |
9351 | if (header.find("ext3") != string::npos && s.kernel_config["CONFIG_EXT3_FS"] != string("")) |
9352 | they_live.push_back ("struct ext3_reserve_window_node;"); | |
9353 | ||
3c1b3d06 FCE |
9354 | return they_live; |
9355 | } | |
47dd066d WC |
9356 | |
9357 | ||
9358 | void | |
79189b84 | 9359 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) |
47dd066d | 9360 | { |
79189b84 JS |
9361 | if (probes.empty()) |
9362 | return; | |
47dd066d | 9363 | |
96b030fe JS |
9364 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
9365 | s.op->newline(); | |
79189b84 | 9366 | |
47dd066d | 9367 | |
a4b9c3b3 FCE |
9368 | // We create a MODULE_aux_N.c file for each tracepoint header, to allow them |
9369 | // to be separately compiled. That's because kernel tracepoint headers sometimes | |
9370 | // conflict. PR13155. | |
9371 | ||
9372 | map<string,translator_output*> per_header_aux; | |
9373 | // GC NB: the translator_output* structs are owned/retained by the systemtap_session. | |
47dd066d | 9374 | |
6fb70fb7 JS |
9375 | for (unsigned i = 0; i < probes.size(); ++i) |
9376 | { | |
9377 | tracepoint_derived_probe *p = probes[i]; | |
75ae2ec9 | 9378 | string header = p->header; |
5f73a260 | 9379 | |
a4b9c3b3 FCE |
9380 | // We cache the auxiliary output files on a per-header basis. We don't |
9381 | // need one aux file per tracepoint, only one per tracepoint-header. | |
9382 | translator_output *tpop = per_header_aux[header]; | |
9383 | if (tpop == 0) | |
9384 | { | |
9385 | tpop = s.op_create_auxiliary(); | |
9386 | per_header_aux[header] = tpop; | |
9387 | ||
9388 | // PR9993: Add extra headers to work around undeclared types in individual | |
9389 | // include/trace/foo.h files | |
9390 | const vector<string>& extra_decls = tracepoint_extra_decls (s, header); | |
9391 | for (unsigned z=0; z<extra_decls.size(); z++) | |
9392 | tpop->newline() << extra_decls[z] << "\n"; | |
720c435f | 9393 | |
a4b9c3b3 FCE |
9394 | // strip include/ substring, the same way as done in get_tracequery_module() |
9395 | size_t root_pos = header.rfind("include/"); | |
9396 | header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header); | |
bfffa443 FCE |
9397 | |
9398 | tpop->newline() << "#include <linux/tracepoint.h>" << endl; | |
a4b9c3b3 | 9399 | tpop->newline() << "#include <" << header << ">"; |
720c435f | 9400 | |
a4b9c3b3 FCE |
9401 | // Starting in 2.6.35, at the same time NOARGS was added, the callback |
9402 | // always has a void* as the first parameter. PR11599 | |
9403 | tpop->newline() << "#ifdef DECLARE_TRACE_NOARGS"; | |
9404 | tpop->newline() << "#define STAP_TP_DATA , NULL"; | |
9405 | tpop->newline() << "#define STAP_TP_PROTO void *cb_data" | |
9406 | << " __attribute__ ((unused))"; | |
9407 | if (!p->args.empty()) | |
9408 | tpop->line() << ","; | |
9409 | tpop->newline() << "#else"; | |
9410 | tpop->newline() << "#define STAP_TP_DATA"; | |
9411 | tpop->newline() << "#define STAP_TP_PROTO"; | |
9412 | if (p->args.empty()) | |
9413 | tpop->line() << " void"; | |
9414 | tpop->newline() << "#endif"; | |
720c435f JS |
9415 | |
9416 | tpop->newline() << "#define intptr_t long"; | |
a4b9c3b3 FCE |
9417 | } |
9418 | ||
720c435f JS |
9419 | // collect the args that are actually in use |
9420 | vector<const tracepoint_arg*> used_args; | |
6fb70fb7 | 9421 | for (unsigned j = 0; j < p->args.size(); ++j) |
720c435f JS |
9422 | if (p->args[j].used) |
9423 | used_args.push_back(&p->args[j]); | |
9424 | ||
a4b9c3b3 FCE |
9425 | // forward-declare the generated-side tracepoint callback |
9426 | tpop->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
9427 | tpop->indent(2); |
9428 | if (used_args.empty()) | |
9429 | tpop->line() << "void"; | |
9430 | for (unsigned j = 0; j < used_args.size(); ++j) | |
6fb70fb7 JS |
9431 | { |
9432 | if (j > 0) | |
a4b9c3b3 FCE |
9433 | tpop->line() << ", "; |
9434 | tpop->line() << "int64_t"; | |
6fb70fb7 | 9435 | } |
720c435f JS |
9436 | tpop->line() << ");"; |
9437 | tpop->indent(-2); | |
5f73a260 | 9438 | |
a4b9c3b3 FCE |
9439 | // define the generated-side tracepoint callback - in the main translator-output |
9440 | s.op->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
9441 | s.op->indent(2); |
9442 | if (used_args.empty()) | |
9443 | s.op->newline() << "void"; | |
9444 | for (unsigned j = 0; j < used_args.size(); ++j) | |
a4b9c3b3 | 9445 | { |
a4b9c3b3 FCE |
9446 | if (j > 0) |
9447 | s.op->line() << ", "; | |
720c435f | 9448 | s.op->newline() << "int64_t __tracepoint_arg_" << used_args[j]->name; |
6fb70fb7 | 9449 | } |
5f73a260 JS |
9450 | s.op->newline() << ")"; |
9451 | s.op->newline(-2) << "{"; | |
26e63673 | 9452 | s.op->newline(1) << "struct stap_probe * const probe = " |
faea5e16 | 9453 | << common_probe_init (p) << ";"; |
6eefe942 MW |
9454 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "probe", |
9455 | "_STP_PROBE_HANDLER_TRACEPOINT"); | |
6dceb5c9 | 9456 | s.op->newline() << "c->ips.tracepoint_name = " |
c12d974f FCE |
9457 | << lex_cast_qstring (p->tracepoint_name) |
9458 | << ";"; | |
720c435f JS |
9459 | for (unsigned j = 0; j < used_args.size(); ++j) |
9460 | { | |
9461 | s.op->newline() << "c->probe_locals." << p->name | |
0386bbcf | 9462 | << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name) |
720c435f JS |
9463 | << " = __tracepoint_arg_" << used_args[j]->name << ";"; |
9464 | } | |
26e63673 | 9465 | s.op->newline() << "(*probe->ph) (c);"; |
7baf48e9 | 9466 | common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors); |
6fb70fb7 | 9467 | s.op->newline(-1) << "}"; |
47dd066d | 9468 | |
a4b9c3b3 | 9469 | // define the real tracepoint callback function |
720c435f | 9470 | tpop->newline() << "static void enter_tracepoint_probe_" << i << "("; |
a4b9c3b3 FCE |
9471 | tpop->newline(2) << "STAP_TP_PROTO"; |
9472 | for (unsigned j = 0; j < p->args.size(); ++j) | |
9473 | { | |
9474 | if (j > 0) | |
9475 | tpop->line() << ", "; | |
9476 | tpop->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
9477 | } | |
9478 | tpop->newline() << ")"; | |
9479 | tpop->newline(-2) << "{"; | |
720c435f JS |
9480 | tpop->newline(1) << "enter_real_tracepoint_probe_" << i << "("; |
9481 | tpop->indent(2); | |
9482 | for (unsigned j = 0; j < used_args.size(); ++j) | |
9483 | { | |
9484 | if (j > 0) | |
9485 | tpop->line() << ", "; | |
9486 | tpop->newline() << "(int64_t)" << used_args[j]->typecast | |
9487 | << "__tracepoint_arg_" << used_args[j]->name; | |
9488 | } | |
9489 | tpop->newline() << ");"; | |
9490 | tpop->newline(-3) << "}"; | |
a4b9c3b3 FCE |
9491 | |
9492 | ||
96b030fe | 9493 | // emit normalized registration functions |
720c435f | 9494 | tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 9495 | tpop->newline(1) << "return register_trace_" << p->tracepoint_name |
5f73a260 | 9496 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 | 9497 | tpop->newline(-1) << "}"; |
47dd066d | 9498 | |
86758d5f JS |
9499 | // NB: we're not prepared to deal with unreg failures. However, failures |
9500 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
9501 | // weren't even registered. The former should be OKed by the initial | |
9502 | // registration call, and the latter is safe to ignore. | |
720c435f | 9503 | tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 9504 | tpop->newline(1) << "(void) unregister_trace_" << p->tracepoint_name |
5f73a260 | 9505 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 FCE |
9506 | tpop->newline(-1) << "}"; |
9507 | tpop->newline(); | |
5f73a260 | 9508 | |
720c435f JS |
9509 | // declare normalized registration functions |
9510 | s.op->newline() << "int register_tracepoint_probe_" << i << "(void);"; | |
9511 | s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);"; | |
5f73a260 | 9512 | |
a4b9c3b3 | 9513 | tpop->assert_0_indent(); |
af304783 DS |
9514 | } |
9515 | ||
96b030fe JS |
9516 | // emit an array of registration functions for easy init/shutdown |
9517 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
9518 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 9519 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
9520 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
9521 | s.op->indent(1); | |
9522 | for (unsigned i = 0; i < probes.size(); ++i) | |
9523 | { | |
9524 | s.op->newline () << "{"; | |
9525 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
9526 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
9527 | s.op->line() << " },"; | |
9528 | } | |
9529 | s.op->newline(-1) << "};"; | |
9530 | s.op->newline(); | |
47dd066d WC |
9531 | } |
9532 | ||
9533 | ||
79189b84 JS |
9534 | void |
9535 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
47dd066d | 9536 | { |
79189b84 JS |
9537 | if (probes.size () == 0) |
9538 | return; | |
47dd066d | 9539 | |
79189b84 | 9540 | s.op->newline() << "/* init tracepoint probes */"; |
96b030fe JS |
9541 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
9542 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
9543 | s.op->newline() << "if (rc) {"; | |
9544 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
9545 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
9546 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
9547 | s.op->newline(-1) << "}"; | |
9548 | s.op->newline(-1) << "}"; | |
47dd066d | 9549 | |
bc9a523d FCE |
9550 | // This would be technically proper (on those autoconf-detectable |
9551 | // kernels that include this function in tracepoint.h), however we | |
9552 | // already make several calls to synchronze_sched() during our | |
9553 | // shutdown processes. | |
47dd066d | 9554 | |
bc9a523d FCE |
9555 | // s.op->newline() << "if (rc)"; |
9556 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
9557 | // s.op->indent(-1); | |
79189b84 | 9558 | } |
47dd066d WC |
9559 | |
9560 | ||
79189b84 JS |
9561 | void |
9562 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
47dd066d | 9563 | { |
79189b84 JS |
9564 | if (probes.empty()) |
9565 | return; | |
47dd066d | 9566 | |
96b030fe JS |
9567 | s.op->newline() << "/* deregister tracepoint probes */"; |
9568 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
9569 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
9570 | s.op->indent(-1); | |
47dd066d | 9571 | |
bc9a523d | 9572 | // Not necessary: see above. |
47dd066d | 9573 | |
bc9a523d | 9574 | // s.op->newline() << "tracepoint_synchronize_unregister();"; |
79189b84 | 9575 | } |
b20febf3 | 9576 | |
47dd066d | 9577 | |
75ead1f7 | 9578 | struct tracepoint_query : public base_query |
47dd066d | 9579 | { |
75ead1f7 JS |
9580 | tracepoint_query(dwflpp & dw, const string & tracepoint, |
9581 | probe * base_probe, probe_point * base_loc, | |
9582 | vector<derived_probe *> & results): | |
9583 | base_query(dw, "*"), tracepoint(tracepoint), | |
9584 | base_probe(base_probe), base_loc(base_loc), | |
9585 | results(results) {} | |
47dd066d | 9586 | |
75ead1f7 | 9587 | const string& tracepoint; |
47dd066d | 9588 | |
75ead1f7 JS |
9589 | probe * base_probe; |
9590 | probe_point * base_loc; | |
9591 | vector<derived_probe *> & results; | |
f982c59b | 9592 | set<string> probed_names; |
47dd066d | 9593 | |
75ead1f7 JS |
9594 | void handle_query_module(); |
9595 | int handle_query_cu(Dwarf_Die * cudie); | |
9596 | int handle_query_func(Dwarf_Die * func); | |
822a6a3d | 9597 | void query_library (const char *) {} |
576eaefe | 9598 | void query_plt (const char *entry, size_t addr) {} |
b20febf3 | 9599 | |
75ead1f7 JS |
9600 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); |
9601 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
9602 | }; | |
47dd066d WC |
9603 | |
9604 | ||
9605 | void | |
75ead1f7 | 9606 | tracepoint_query::handle_query_module() |
47dd066d | 9607 | { |
75ead1f7 | 9608 | // look for the tracepoints in each CU |
337b7c44 | 9609 | dw.iterate_over_cus(tracepoint_query_cu, this, false); |
47dd066d WC |
9610 | } |
9611 | ||
9612 | ||
75ead1f7 JS |
9613 | int |
9614 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
47dd066d | 9615 | { |
75ead1f7 | 9616 | dw.focus_on_cu (cudie); |
47dd066d | 9617 | |
75ead1f7 JS |
9618 | // look at each function to see if it's a tracepoint |
9619 | string function = "stapprobe_" + tracepoint; | |
9620 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
47dd066d WC |
9621 | } |
9622 | ||
9623 | ||
75ead1f7 JS |
9624 | int |
9625 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
47dd066d | 9626 | { |
75ead1f7 | 9627 | dw.focus_on_function (func); |
47dd066d | 9628 | |
60d98537 | 9629 | assert(startswith(dw.function_name, "stapprobe_")); |
75ead1f7 | 9630 | string tracepoint_instance = dw.function_name.substr(10); |
f982c59b JS |
9631 | |
9632 | // check for duplicates -- sometimes tracepoint headers may be indirectly | |
9633 | // included in more than one of our tracequery modules. | |
9634 | if (!probed_names.insert(tracepoint_instance).second) | |
9635 | return DWARF_CB_OK; | |
9636 | ||
79189b84 JS |
9637 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
9638 | tracepoint_instance, | |
9639 | base_probe, base_loc); | |
9640 | results.push_back (dp); | |
75ead1f7 | 9641 | return DWARF_CB_OK; |
47dd066d WC |
9642 | } |
9643 | ||
9644 | ||
75ead1f7 JS |
9645 | int |
9646 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
47dd066d | 9647 | { |
75ead1f7 | 9648 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); |
85007c04 | 9649 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 9650 | return q->handle_query_cu(cudie); |
47dd066d WC |
9651 | } |
9652 | ||
9653 | ||
75ead1f7 JS |
9654 | int |
9655 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
47dd066d | 9656 | { |
75ead1f7 | 9657 | tracepoint_query * q = static_cast<tracepoint_query *>(query); |
85007c04 | 9658 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 9659 | return q->handle_query_func(func); |
47dd066d WC |
9660 | } |
9661 | ||
9662 | ||
0a6f5a3f | 9663 | struct tracepoint_builder: public derived_probe_builder |
47dd066d | 9664 | { |
0a6f5a3f JS |
9665 | private: |
9666 | dwflpp *dw; | |
9667 | bool init_dw(systemtap_session& s); | |
c9ccb642 FCE |
9668 | void get_tracequery_modules(systemtap_session& s, |
9669 | const vector<string>& headers, | |
9670 | vector<string>& modules); | |
47dd066d | 9671 | |
0a6f5a3f | 9672 | public: |
47dd066d | 9673 | |
0a6f5a3f JS |
9674 | tracepoint_builder(): dw(0) {} |
9675 | ~tracepoint_builder() { delete dw; } | |
47dd066d | 9676 | |
0a6f5a3f JS |
9677 | void build_no_more (systemtap_session& s) |
9678 | { | |
9679 | if (dw && s.verbose > 3) | |
b530b5b3 | 9680 | clog << _("tracepoint_builder releasing dwflpp") << endl; |
0a6f5a3f JS |
9681 | delete dw; |
9682 | dw = NULL; | |
435f53a7 FCE |
9683 | |
9684 | delete_session_module_cache (s); | |
0a6f5a3f | 9685 | } |
47dd066d | 9686 | |
0a6f5a3f JS |
9687 | void build(systemtap_session& s, |
9688 | probe *base, probe_point *location, | |
9689 | literal_map_t const& parameters, | |
9690 | vector<derived_probe*>& finished_results); | |
9691 | }; | |
47dd066d | 9692 | |
47dd066d | 9693 | |
c9ccb642 | 9694 | |
2a0e62a8 | 9695 | // Create (or cache) one or more tracequery .o modules, based upon the |
c9ccb642 FCE |
9696 | // tracepoint-related header files given. Return the generated or cached |
9697 | // modules[]. | |
9698 | ||
9699 | void | |
9700 | tracepoint_builder::get_tracequery_modules(systemtap_session& s, | |
9701 | const vector<string>& headers, | |
9702 | vector<string>& modules) | |
0a6f5a3f | 9703 | { |
c95eddf7 | 9704 | if (s.verbose > 2) |
55e50c24 | 9705 | { |
ce0f6648 | 9706 | clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl; |
55e50c24 JS |
9707 | for (size_t i = 0; i < headers.size(); ++i) |
9708 | clog << " " << headers[i] << endl; | |
9709 | } | |
c95eddf7 | 9710 | |
2a0e62a8 JS |
9711 | map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name |
9712 | // Map the headers to cache .o names. Note that this has side-effects of | |
c9ccb642 FCE |
9713 | // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file, |
9714 | // so we prefer not to repeat this. | |
9715 | vector<string> uncached_headers; | |
9716 | for (size_t i=0; i<headers.size(); i++) | |
2a0e62a8 | 9717 | headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]); |
c9ccb642 FCE |
9718 | |
9719 | // They may be in the cache already. | |
9720 | if (s.use_cache && !s.poison_cache) | |
9721 | for (size_t i=0; i<headers.size(); i++) | |
9722 | { | |
9723 | // see if the cached module exists | |
2a0e62a8 | 9724 | const string& tracequery_path = headers_cache_obj[headers[i]]; |
c9ccb642 FCE |
9725 | if (!tracequery_path.empty() && file_exists(tracequery_path)) |
9726 | { | |
9727 | if (s.verbose > 2) | |
9728 | clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl; | |
47dd066d | 9729 | |
c252fca2 JS |
9730 | // an empty file is a cached failure |
9731 | if (get_file_size(tracequery_path) > 0) | |
9732 | modules.push_back (tracequery_path); | |
c9ccb642 FCE |
9733 | } |
9734 | else | |
9735 | uncached_headers.push_back(headers[i]); | |
9736 | } | |
9737 | else | |
9738 | uncached_headers = headers; | |
f982c59b | 9739 | |
c9ccb642 FCE |
9740 | // If we have nothing left to search for, quit |
9741 | if (uncached_headers.empty()) return; | |
55e50c24 | 9742 | |
c9ccb642 | 9743 | map<string,string> headers_tracequery_src; // header -> C-source code mapping |
55e50c24 | 9744 | |
c9ccb642 FCE |
9745 | // We could query several subsets of headers[] to make this go |
9746 | // faster, but let's KISS and do one at a time. | |
9747 | for (size_t i=0; i<uncached_headers.size(); i++) | |
55e50c24 | 9748 | { |
c9ccb642 FCE |
9749 | const string& header = uncached_headers[i]; |
9750 | ||
9751 | // create a tracequery source file | |
9752 | ostringstream osrc; | |
9753 | ||
9754 | // PR9993: Add extra headers to work around undeclared types in individual | |
9755 | // include/trace/foo.h files | |
9756 | vector<string> short_decls = tracepoint_extra_decls(s, header); | |
9757 | ||
9758 | // add each requested tracepoint header | |
75ae2ec9 | 9759 | size_t root_pos = header.rfind("include/"); |
832f100d | 9760 | short_decls.push_back(string("#include <") + |
75ae2ec9 | 9761 | ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) + |
d4393459 | 9762 | string(">")); |
f982c59b | 9763 | |
c9ccb642 FCE |
9764 | osrc << "#ifdef CONFIG_TRACEPOINTS" << endl; |
9765 | osrc << "#include <linux/tracepoint.h>" << endl; | |
832f100d JS |
9766 | |
9767 | // the kernel has changed this naming a few times, previously TPPROTO, | |
9768 | // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest. | |
9769 | osrc << "#ifndef PARAMS" << endl; | |
9770 | osrc << "#define PARAMS(args...) args" << endl; | |
9771 | osrc << "#endif" << endl; | |
9772 | ||
c9ccb642 FCE |
9773 | // override DECLARE_TRACE to synthesize probe functions for us |
9774 | osrc << "#undef DECLARE_TRACE" << endl; | |
9775 | osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl; | |
9776 | osrc << " void stapprobe_##name(proto) {}" << endl; | |
832f100d | 9777 | |
c9ccb642 FCE |
9778 | // 2.6.35 added the NOARGS variant, but it's the same for us |
9779 | osrc << "#undef DECLARE_TRACE_NOARGS" << endl; | |
9780 | osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl; | |
9781 | osrc << " DECLARE_TRACE(name, void, )" << endl; | |
832f100d JS |
9782 | |
9783 | // 2.6.38 added the CONDITION variant, which can also just redirect | |
9784 | osrc << "#undef DECLARE_TRACE_CONDITION" << endl; | |
9785 | osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl; | |
9786 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; | |
9787 | ||
c9ccb642 FCE |
9788 | // older tracepoints used DEFINE_TRACE, so redirect that too |
9789 | osrc << "#undef DEFINE_TRACE" << endl; | |
9790 | osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl; | |
832f100d JS |
9791 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; |
9792 | ||
c9ccb642 FCE |
9793 | // add the specified decls/#includes |
9794 | for (unsigned z=0; z<short_decls.size(); z++) | |
9795 | osrc << "#undef TRACE_INCLUDE_FILE\n" | |
9796 | << "#undef TRACE_INCLUDE_PATH\n" | |
9797 | << short_decls[z] << "\n"; | |
9798 | ||
9799 | // finish up the module source | |
9800 | osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl; | |
47dd066d | 9801 | |
c9ccb642 FCE |
9802 | // save the source file away |
9803 | headers_tracequery_src[header] = osrc.str(); | |
55e50c24 | 9804 | } |
f982c59b | 9805 | |
c9ccb642 | 9806 | // now build them all together |
2a0e62a8 | 9807 | map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src); |
47dd066d | 9808 | |
c9ccb642 | 9809 | // now plop them into the cache |
b278033a | 9810 | if (s.use_cache) |
c9ccb642 FCE |
9811 | for (size_t i=0; i<uncached_headers.size(); i++) |
9812 | { | |
9813 | const string& header = uncached_headers[i]; | |
2a0e62a8 JS |
9814 | const string& tracequery_obj = tracequery_objs[header]; |
9815 | const string& tracequery_path = headers_cache_obj[header]; | |
9816 | if (tracequery_obj !="" && file_exists(tracequery_obj)) | |
c9ccb642 | 9817 | { |
2a0e62a8 | 9818 | copy_file(tracequery_obj, tracequery_path, s.verbose > 2); |
c9ccb642 FCE |
9819 | modules.push_back (tracequery_path); |
9820 | } | |
c252fca2 JS |
9821 | else |
9822 | // cache an empty file for failures | |
9823 | copy_file("/dev/null", tracequery_path, s.verbose > 2); | |
c9ccb642 | 9824 | } |
f982c59b JS |
9825 | } |
9826 | ||
9827 | ||
d4393459 | 9828 | |
f982c59b JS |
9829 | bool |
9830 | tracepoint_builder::init_dw(systemtap_session& s) | |
9831 | { | |
9832 | if (dw != NULL) | |
9833 | return true; | |
9834 | ||
9835 | vector<string> tracequery_modules; | |
55e50c24 | 9836 | vector<string> system_headers; |
f982c59b JS |
9837 | |
9838 | glob_t trace_glob; | |
d4393459 FCE |
9839 | |
9840 | // find kernel_source_tree | |
9841 | if (s.kernel_source_tree == "") | |
f982c59b | 9842 | { |
d4393459 FCE |
9843 | unsigned found; |
9844 | DwflPtr dwfl_ptr = setup_dwfl_kernel ("kernel", &found, s); | |
9845 | Dwfl *dwfl = dwfl_ptr.get()->dwfl; | |
9846 | if (found) | |
9847 | { | |
9848 | Dwarf_Die *cudie = 0; | |
9849 | Dwarf_Addr bias; | |
9850 | while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL) | |
9851 | { | |
e19ebcf7 | 9852 | assert_no_interrupts(); |
d4393459 FCE |
9853 | Dwarf_Attribute attr; |
9854 | const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr)); | |
9855 | if (name) | |
9856 | { | |
61f1a63b | 9857 | if (s.verbose > 2) |
b530b5b3 | 9858 | clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl; |
61f1a63b | 9859 | |
d4393459 FCE |
9860 | s.kernel_source_tree = name; |
9861 | break; // skip others; modern Kbuild uses same comp_dir for them all | |
9862 | } | |
9863 | } | |
9864 | } | |
9865 | } | |
9866 | ||
9867 | // prefixes | |
9868 | vector<string> glob_prefixes; | |
9869 | glob_prefixes.push_back (s.kernel_build_tree); | |
9870 | if (s.kernel_source_tree != "") | |
9871 | glob_prefixes.push_back (s.kernel_source_tree); | |
9872 | ||
9873 | // suffixes | |
9874 | vector<string> glob_suffixes; | |
9875 | glob_suffixes.push_back("include/trace/events/*.h"); | |
9876 | glob_suffixes.push_back("include/trace/*.h"); | |
9877 | glob_suffixes.push_back("arch/x86/kvm/*trace.h"); | |
f89ff3e2 | 9878 | glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h"); |
d4393459 FCE |
9879 | |
9880 | // compute cartesian product | |
9881 | vector<string> globs; | |
9882 | for (unsigned i=0; i<glob_prefixes.size(); i++) | |
9883 | for (unsigned j=0; j<glob_suffixes.size(); j++) | |
9884 | globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]); | |
9885 | ||
8aa43b8d | 9886 | set<string> duped_headers; |
d4393459 FCE |
9887 | for (unsigned z = 0; z < globs.size(); z++) |
9888 | { | |
9889 | string glob_str = globs[z]; | |
9890 | if (s.verbose > 3) | |
b530b5b3 | 9891 | clog << _("Checking tracepoint glob ") << glob_str << endl; |
d4393459 | 9892 | |
067cc66f CM |
9893 | int r = glob(glob_str.c_str(), 0, NULL, &trace_glob); |
9894 | if (r == GLOB_NOSPACE || r == GLOB_ABORTED) | |
9895 | throw runtime_error("Error globbing tracepoint"); | |
9896 | ||
f982c59b JS |
9897 | for (unsigned i = 0; i < trace_glob.gl_pathc; ++i) |
9898 | { | |
9899 | string header(trace_glob.gl_pathv[i]); | |
9900 | ||
9901 | // filter out a few known "internal-only" headers | |
60d98537 JS |
9902 | if (endswith(header, "/define_trace.h") || |
9903 | endswith(header, "/ftrace.h") || | |
9904 | endswith(header, "/trace_events.h") || | |
9905 | endswith(header, "_event_types.h")) | |
f982c59b JS |
9906 | continue; |
9907 | ||
8aa43b8d JS |
9908 | // skip identical headers from the build and source trees. |
9909 | size_t root_pos = header.rfind("include/"); | |
9910 | if (root_pos != string::npos && | |
9911 | !duped_headers.insert(header.substr(root_pos + 8)).second) | |
9912 | continue; | |
9913 | ||
55e50c24 | 9914 | system_headers.push_back(header); |
f982c59b JS |
9915 | } |
9916 | globfree(&trace_glob); | |
9917 | } | |
9918 | ||
c9ccb642 FCE |
9919 | // Build tracequery modules |
9920 | get_tracequery_modules(s, system_headers, tracequery_modules); | |
55e50c24 | 9921 | |
f982c59b JS |
9922 | // TODO: consider other sources of tracepoint headers too, like from |
9923 | // a command-line parameter or some environment or .systemtaprc | |
47dd066d | 9924 | |
59c11f91 | 9925 | dw = new dwflpp(s, tracequery_modules, true); |
0a6f5a3f JS |
9926 | return true; |
9927 | } | |
47dd066d | 9928 | |
0a6f5a3f JS |
9929 | void |
9930 | tracepoint_builder::build(systemtap_session& s, | |
9931 | probe *base, probe_point *location, | |
9932 | literal_map_t const& parameters, | |
9933 | vector<derived_probe*>& finished_results) | |
9934 | { | |
9935 | if (!init_dw(s)) | |
9936 | return; | |
47dd066d | 9937 | |
75ead1f7 JS |
9938 | string tracepoint; |
9939 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
47dd066d | 9940 | |
75ead1f7 | 9941 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); |
51178501 | 9942 | dw->iterate_over_modules(&query_module, &q); |
47dd066d | 9943 | } |
47dd066d | 9944 | |
e6fe60e7 | 9945 | |
b55bc428 | 9946 | // ------------------------------------------------------------------------ |
bd2b1e68 | 9947 | // Standard tapset registry. |
b55bc428 FCE |
9948 | // ------------------------------------------------------------------------ |
9949 | ||
7a053d3b | 9950 | void |
f8220a7b | 9951 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 9952 | { |
47e0478e | 9953 | register_tapset_been(s); |
93646f4d | 9954 | register_tapset_itrace(s); |
dd0e4fa7 | 9955 | register_tapset_mark(s); |
7a212aa8 | 9956 | register_tapset_procfs(s); |
912e8c59 | 9957 | register_tapset_timers(s); |
8d9609f5 | 9958 | register_tapset_netfilter(s); |
b84779a5 | 9959 | register_tapset_utrace(s); |
b98a8d73 | 9960 | |
7a24d422 | 9961 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 9962 | dwarf_derived_probe::register_patterns(s); |
30a279be | 9963 | |
888af770 FCE |
9964 | // XXX: user-space starter set |
9965 | s.pattern_root->bind_num(TOK_PROCESS) | |
9966 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
f66bb29a | 9967 | ->bind_privilege(pr_all) |
888af770 FCE |
9968 | ->bind(new uprobe_builder ()); |
9969 | s.pattern_root->bind_num(TOK_PROCESS) | |
9970 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
f66bb29a | 9971 | ->bind_privilege(pr_all) |
888af770 FCE |
9972 | ->bind(new uprobe_builder ()); |
9973 | ||
0a6f5a3f JS |
9974 | // kernel tracepoint probes |
9975 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
9976 | ->bind(new tracepoint_builder()); | |
9977 | ||
e6fe60e7 AM |
9978 | // Kprobe based probe |
9979 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
9980 | ->bind(new kprobe_builder()); | |
9981 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) | |
9982 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
9983 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
9984 | ->bind(new kprobe_builder()); | |
b6371390 JS |
9985 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
9986 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
9987 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
9988 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
9989 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
9990 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
9991 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
9992 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
9993 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
dd225250 PS |
9994 | |
9995 | //Hwbkpt based probe | |
b47f3a55 FCE |
9996 | // NB: we formerly registered the probe point types only if the kernel configuration |
9997 | // allowed it. However, we get better error messages if we allow probes to resolve. | |
9998 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
9999 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10000 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10001 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10002 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10003 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10004 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10005 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10006 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10007 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10008 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10009 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10010 | // length supported with address only, not symbol names | |
83ea76b1 WC |
10011 | |
10012 | //perf event based probe | |
4763f713 | 10013 | register_tapset_perf(s); |
b55bc428 | 10014 | } |
dc38c0ae DS |
10015 | |
10016 | ||
b20febf3 FCE |
10017 | vector<derived_probe_group*> |
10018 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 10019 | { |
b20febf3 | 10020 | vector<derived_probe_group*> g; |
912e8c59 JS |
10021 | |
10022 | #define DOONE(x) \ | |
10023 | if (s. x##_derived_probes) \ | |
10024 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
10025 | |
10026 | // Note that order *is* important here. We want to make sure we | |
10027 | // register (actually run) begin probes before any other probe type | |
10028 | // is run. Similarly, when unregistering probes, we want to | |
10029 | // unregister (actually run) end probes after every other probe type | |
10030 | // has be unregistered. To do the latter, | |
10031 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
10032 | DOONE(be); |
10033 | DOONE(dwarf); | |
888af770 | 10034 | DOONE(uprobe); |
b20febf3 FCE |
10035 | DOONE(timer); |
10036 | DOONE(profile); | |
10037 | DOONE(mark); | |
0a6f5a3f | 10038 | DOONE(tracepoint); |
e6fe60e7 | 10039 | DOONE(kprobe); |
dd225250 | 10040 | DOONE(hwbkpt); |
83ea76b1 | 10041 | DOONE(perf); |
b20febf3 | 10042 | DOONE(hrtimer); |
ce82316f | 10043 | DOONE(procfs); |
8d9609f5 | 10044 | DOONE(netfilter); |
935447c8 DS |
10045 | |
10046 | // Another "order is important" item. We want to make sure we | |
10047 | // "register" the dummy task_finder probe group after all probe | |
10048 | // groups that use the task_finder. | |
10049 | DOONE(utrace); | |
a96d1db0 | 10050 | DOONE(itrace); |
935447c8 | 10051 | DOONE(task_finder); |
b20febf3 FCE |
10052 | #undef DOONE |
10053 | return g; | |
46b84a80 | 10054 | } |
73267b89 JS |
10055 | |
10056 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |