]>
Commit | Line | Data |
---|---|---|
56e12059 | 1 | // tapset resolution |
12b44fb3 | 2 | // Copyright (C) 2005-2009 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" |
bd2b1e68 | 23 | |
3b579393 FCE |
24 | #include <cstdlib> |
25 | #include <algorithm> | |
bd2b1e68 | 26 | #include <deque> |
56e12059 | 27 | #include <iostream> |
bd2b1e68 | 28 | #include <map> |
3bf6ac45 TM |
29 | #ifdef HAVE_TR1_UNORDERED_MAP |
30 | #include <tr1/unordered_map> | |
31 | #else | |
2171f774 | 32 | #include <ext/hash_map> |
3bf6ac45 | 33 | #endif |
ec4373ff | 34 | #include <set> |
56e12059 | 35 | #include <sstream> |
bd2b1e68 | 36 | #include <stdexcept> |
b55bc428 | 37 | #include <vector> |
e36387d7 | 38 | #include <cstdarg> |
29e64872 | 39 | #include <cassert> |
1969b5bc | 40 | #include <iomanip> |
f781f849 | 41 | #include <cerrno> |
bd2b1e68 GH |
42 | |
43 | extern "C" { | |
df8fadee | 44 | #include <fcntl.h> |
bd2b1e68 | 45 | #include <elfutils/libdwfl.h> |
7a053d3b | 46 | #include <elfutils/libdw.h> |
77de5e9e GH |
47 | #include <dwarf.h> |
48 | #include <elf.h> | |
49 | #include <obstack.h> | |
30a279be | 50 | #include <regex.h> |
b20febf3 | 51 | #include <glob.h> |
30a279be | 52 | #include <fnmatch.h> |
5f0a03a6 | 53 | #include <stdio.h> |
349dc70e | 54 | #include <sys/types.h> |
4b1ad75e | 55 | |
30a279be | 56 | #include "loc2c.h" |
4b1ad75e RM |
57 | #define __STDC_FORMAT_MACROS |
58 | #include <inttypes.h> | |
bd2b1e68 | 59 | } |
77de5e9e | 60 | |
56e12059 FCE |
61 | |
62 | using namespace std; | |
2171f774 | 63 | using namespace __gnu_cxx; |
56e12059 | 64 | |
46b84a80 | 65 | |
b20febf3 | 66 | |
b20febf3 FCE |
67 | // ------------------------------------------------------------------------ |
68 | void | |
a58d79d0 | 69 | common_probe_entryfn_prologue (translator_output* o, string statestr, |
c12d974f | 70 | string new_pp, |
912e8c59 | 71 | bool overload_processing) |
b20febf3 | 72 | { |
72d18b98 | 73 | o->newline() << "struct context* __restrict__ c;"; |
e0a17418 JS |
74 | o->newline() << "#if !INTERRUPTIBLE"; |
75 | o->newline() << "unsigned long flags;"; | |
76 | o->newline() << "#endif"; | |
b20febf3 | 77 | |
a58d79d0 DS |
78 | if (overload_processing) |
79 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
80 | else | |
81 | o->newline() << "#ifdef STP_TIMING"; | |
82 | o->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
b20febf3 | 83 | o->newline() << "#endif"; |
b20febf3 | 84 | |
e0a17418 JS |
85 | o->newline() << "#if INTERRUPTIBLE"; |
86 | o->newline() << "preempt_disable ();"; | |
87 | o->newline() << "#else"; | |
88 | o->newline() << "local_irq_save (flags);"; | |
89 | o->newline() << "#endif"; | |
b20febf3 | 90 | |
c931ec8a | 91 | // Check for enough free enough stack space |
d05a1d00 | 92 | o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space |
a63401b1 | 93 | o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space |
d05a1d00 FCE |
94 | // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack |
95 | // NB: see also CONFIG_DEBUG_STACKOVERFLOW | |
b3c3ca7c FCE |
96 | o->newline() << "atomic_inc (& skipped_count);"; |
97 | o->newline() << "#ifdef STP_TIMING"; | |
98 | o->newline() << "atomic_inc (& skipped_count_lowstack);"; | |
99 | o->newline() << "#endif"; | |
c931ec8a FCE |
100 | o->newline() << "goto probe_epilogue;"; |
101 | o->newline(-1) << "}"; | |
102 | ||
b20febf3 FCE |
103 | o->newline() << "if (atomic_read (&session_state) != " << statestr << ")"; |
104 | o->newline(1) << "goto probe_epilogue;"; | |
105 | o->indent(-1); | |
9a604fac | 106 | |
a44a0785 | 107 | o->newline() << "c = per_cpu_ptr (contexts, smp_processor_id());"; |
b3c3ca7c | 108 | o->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
9c736061 FCE |
109 | o->newline(1) << "#if !INTERRUPTIBLE"; |
110 | o->newline() << "atomic_inc (& skipped_count);"; | |
111 | o->newline() << "#endif"; | |
b3c3ca7c FCE |
112 | o->newline() << "#ifdef STP_TIMING"; |
113 | o->newline() << "atomic_inc (& skipped_count_reentrant);"; | |
c12d974f FCE |
114 | o->newline() << "#ifdef DEBUG_REENTRANCY"; |
115 | o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
116 | << new_pp << ", c->probe_point ?: \"?\", smp_processor_id());"; | |
117 | // NB: There is a conceivable race condition here with reading | |
118 | // c->probe_point, knowing that this other probe is sort of running. | |
119 | // However, in reality, it's interrupted. Plus even if it were able | |
120 | // to somehow start again, and stop before we read c->probe_point, | |
121 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
122 | o->newline() << "#endif"; | |
b3c3ca7c | 123 | o->newline() << "#endif"; |
9a604fac | 124 | o->newline() << "atomic_dec (& c->busy);"; |
b20febf3 | 125 | o->newline() << "goto probe_epilogue;"; |
9a604fac FCE |
126 | o->newline(-1) << "}"; |
127 | o->newline(); | |
1e00cfb1 | 128 | o->newline() << "c->last_stmt = 0;"; |
9a604fac | 129 | o->newline() << "c->last_error = 0;"; |
9a604fac | 130 | o->newline() << "c->nesting = 0;"; |
22f8b401 | 131 | o->newline() << "c->regs = 0;"; |
b916df9c | 132 | o->newline() << "c->unwaddr = 0;"; |
c12d974f | 133 | o->newline() << "c->probe_point = " << new_pp << ";"; |
b916df9c | 134 | // reset unwound address cache |
fcff848e | 135 | o->newline() << "c->pi = 0;"; |
9addf322 | 136 | o->newline() << "c->regparm = 0;"; |
bc54e71c MH |
137 | o->newline() << "c->marker_name = NULL;"; |
138 | o->newline() << "c->marker_format = NULL;"; | |
e0a17418 JS |
139 | |
140 | o->newline() << "#if INTERRUPTIBLE"; | |
141 | o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
142 | o->newline() << "#else"; | |
143 | o->newline() << "c->actionremaining = MAXACTION;"; | |
144 | o->newline() << "#endif"; | |
dbb68664 FCE |
145 | o->newline() << "#ifdef STP_TIMING"; |
146 | o->newline() << "c->statp = 0;"; | |
147 | o->newline() << "#endif"; | |
9915575b FCE |
148 | // NB: The following would actually be incorrect. |
149 | // That's because cycles_sum/cycles_base values are supposed to survive | |
150 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
151 | // cycles), the values will be reset. | |
152 | /* | |
f0e6dc63 FCE |
153 | o->newline() << "#ifdef STP_OVERLOAD"; |
154 | o->newline() << "c->cycles_sum = 0;"; | |
155 | o->newline() << "c->cycles_base = 0;"; | |
41c262f3 | 156 | o->newline() << "#endif"; |
9915575b | 157 | */ |
b20febf3 | 158 | } |
9a604fac | 159 | |
a44a0785 | 160 | |
b20febf3 | 161 | void |
a58d79d0 | 162 | common_probe_entryfn_epilogue (translator_output* o, |
912e8c59 | 163 | bool overload_processing) |
b20febf3 | 164 | { |
a58d79d0 DS |
165 | if (overload_processing) |
166 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
167 | else | |
168 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 169 | o->newline() << "{"; |
a58d79d0 DS |
170 | o->newline(1) << "cycles_t cycles_atend = get_cycles ();"; |
171 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
172 | // fewer, if the hardware counter rolls over really quickly. We | |
173 | // handle 32-bit wraparound here. | |
174 | o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
175 | o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
176 | o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
177 | o->indent(-1); | |
dbb68664 | 178 | |
a58d79d0 | 179 | o->newline() << "#ifdef STP_TIMING"; |
dbb68664 | 180 | o->newline() << "if (likely (c->statp)) _stp_stat_add(*c->statp, cycles_elapsed);"; |
a58d79d0 DS |
181 | o->newline() << "#endif"; |
182 | ||
183 | if (overload_processing) | |
184 | { | |
185 | o->newline() << "#ifdef STP_OVERLOAD"; | |
186 | o->newline() << "{"; | |
187 | // If the cycle count has wrapped (cycles_atend > cycles_base), | |
188 | // let's go ahead and pretend the interval has been reached. | |
189 | // This should reset cycles_base and cycles_sum. | |
190 | o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; | |
191 | o->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
192 | o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
193 | o->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
194 | ||
195 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
196 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
197 | // has overloaded the system and we need to quit. | |
198 | o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; | |
199 | o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
200 | o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
201 | o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);"; | |
551e9f14 | 202 | o->newline() << "atomic_inc (&error_count);"; |
a58d79d0 DS |
203 | o->newline(-1) << "}"; |
204 | ||
205 | o->newline() << "c->cycles_base = cycles_atend;"; | |
206 | o->newline() << "c->cycles_sum = 0;"; | |
207 | o->newline(-1) << "}"; | |
208 | o->newline(-1) << "}"; | |
209 | o->newline() << "#endif"; | |
210 | } | |
211 | ||
dbb68664 FCE |
212 | o->newline(-1) << "}"; |
213 | o->newline() << "#endif"; | |
214 | ||
c3add01f | 215 | o->newline() << "c->probe_point = 0;"; // vacated |
9a604fac FCE |
216 | o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; |
217 | o->newline(1) << "if (c->last_stmt != NULL)"; | |
218 | o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
219 | o->newline(-1) << "else"; | |
220 | o->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
221 | o->indent(-1); | |
222 | o->newline() << "atomic_inc (& error_count);"; | |
9a604fac FCE |
223 | o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {"; |
224 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
225 | o->newline() << "_stp_exit ();"; | |
226 | o->newline(-1) << "}"; | |
9a604fac | 227 | o->newline(-1) << "}"; |
9a604fac | 228 | o->newline() << "atomic_dec (&c->busy);"; |
a44a0785 | 229 | |
b20febf3 FCE |
230 | o->newline(-1) << "probe_epilogue:"; // context is free |
231 | o->indent(1); | |
a44a0785 | 232 | |
b3c3ca7c FCE |
233 | // Check for excessive skip counts. |
234 | o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {"; | |
235 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
236 | o->newline() << "_stp_exit ();"; | |
237 | o->newline(-1) << "}"; | |
238 | ||
e0a17418 JS |
239 | o->newline() << "#if INTERRUPTIBLE"; |
240 | o->newline() << "preempt_enable_no_resched ();"; | |
241 | o->newline() << "#else"; | |
242 | o->newline() << "local_irq_restore (flags);"; | |
243 | o->newline() << "#endif"; | |
9a604fac FCE |
244 | } |
245 | ||
246 | ||
56e12059 FCE |
247 | // ------------------------------------------------------------------------ |
248 | ||
56e12059 | 249 | // ------------------------------------------------------------------------ |
b20febf3 | 250 | // Dwarf derived probes. "We apologize for the inconvience." |
b55bc428 | 251 | // ------------------------------------------------------------------------ |
bd2b1e68 | 252 | |
c239d28c GH |
253 | static string TOK_KERNEL("kernel"); |
254 | static string TOK_MODULE("module"); | |
c239d28c | 255 | static string TOK_FUNCTION("function"); |
54efe513 | 256 | static string TOK_INLINE("inline"); |
b8da0ad1 | 257 | static string TOK_CALL("call"); |
c239d28c | 258 | static string TOK_RETURN("return"); |
c9bad430 | 259 | static string TOK_MAXACTIVE("maxactive"); |
c239d28c | 260 | static string TOK_STATEMENT("statement"); |
37ebca01 | 261 | static string TOK_ABSOLUTE("absolute"); |
888af770 | 262 | static string TOK_PROCESS("process"); |
f28a8c28 | 263 | static string TOK_MARK("mark"); |
0a6f5a3f | 264 | static string TOK_TRACE("trace"); |
0f336e95 | 265 | static string TOK_LABEL("label"); |
59ff2773 | 266 | |
5f0a03a6 JK |
267 | // Can we handle this query with just symbol-table info? |
268 | enum dbinfo_reqt | |
269 | { | |
270 | dbr_unknown, | |
271 | dbr_none, // kernel.statement(NUM).absolute | |
272 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
273 | dbr_need_dwarf | |
274 | }; | |
275 | ||
276 | enum info_status | |
277 | { | |
278 | info_unknown, | |
279 | info_present, | |
280 | info_absent | |
281 | }; | |
b8da0ad1 | 282 | |
20e4a32c | 283 | struct |
7e1279ea FCE |
284 | func_info |
285 | { | |
20e4a32c | 286 | func_info() |
ab91b232 | 287 | : decl_file(NULL), decl_line(-1), addr(0), prologue_end(0), weak(false) |
b6581717 GH |
288 | { |
289 | memset(&die, 0, sizeof(die)); | |
290 | } | |
7e1279ea | 291 | string name; |
4cd232e4 GH |
292 | char const * decl_file; |
293 | int decl_line; | |
7e1279ea | 294 | Dwarf_Die die; |
5f0a03a6 | 295 | Dwarf_Addr addr; |
3e961ba6 | 296 | Dwarf_Addr entrypc; |
7e1279ea | 297 | Dwarf_Addr prologue_end; |
ab91b232 | 298 | bool weak; |
2e67a43b TM |
299 | // Comparison functor for list of functions sorted by address. The |
300 | // two versions that take a Dwarf_Addr let us use the STL algorithms | |
301 | // upper_bound, equal_range et al., but we don't know whether the | |
302 | // searched-for value will be passed as the first or the second | |
303 | // argument. | |
304 | struct Compare | |
305 | { | |
306 | bool operator() (const func_info* f1, const func_info* f2) const | |
307 | { | |
308 | return f1->addr < f2->addr; | |
309 | } | |
310 | // For doing lookups by address. | |
311 | bool operator() (Dwarf_Addr addr, const func_info* f2) const | |
312 | { | |
313 | return addr < f2->addr; | |
314 | } | |
315 | bool operator() (const func_info* f1, Dwarf_Addr addr) const | |
316 | { | |
317 | return f1->addr < addr; | |
318 | } | |
319 | }; | |
7e1279ea FCE |
320 | }; |
321 | ||
322 | struct | |
323 | inline_instance_info | |
324 | { | |
20e4a32c | 325 | inline_instance_info() |
b6581717 GH |
326 | : decl_file(NULL), decl_line(-1) |
327 | { | |
328 | memset(&die, 0, sizeof(die)); | |
329 | } | |
7e1279ea | 330 | string name; |
4cd232e4 GH |
331 | char const * decl_file; |
332 | int decl_line; | |
3e961ba6 | 333 | Dwarf_Addr entrypc; |
7e1279ea FCE |
334 | Dwarf_Die die; |
335 | }; | |
336 | ||
c8959a29 | 337 | |
c4ce66a1 JS |
338 | struct base_query; // forward decls |
339 | struct dwarf_query; | |
5f0a03a6 JK |
340 | struct dwflpp; |
341 | struct symbol_table; | |
342 | ||
405b71b8 | 343 | |
5f0a03a6 JK |
344 | struct |
345 | module_info | |
346 | { | |
347 | Dwfl_Module* mod; | |
348 | const char* name; | |
349 | string elf_path; | |
350 | Dwarf_Addr addr; | |
351 | Dwarf_Addr bias; | |
352 | symbol_table *sym_table; | |
353 | info_status dwarf_status; // module has dwarf info? | |
354 | info_status symtab_status; // symbol table cached? | |
355 | ||
356 | void get_symtab(dwarf_query *q); | |
357 | ||
358 | module_info(const char *name) : | |
359 | mod(NULL), | |
360 | name(name), | |
361 | addr(0), | |
362 | bias(0), | |
363 | sym_table(NULL), | |
364 | dwarf_status(info_unknown), | |
365 | symtab_status(info_unknown) | |
366 | {} | |
367 | ||
368 | ~module_info(); | |
369 | }; | |
370 | ||
371 | struct | |
372 | module_cache | |
373 | { | |
374 | map<string, module_info*> cache; | |
375 | bool paths_collected; | |
376 | bool dwarf_collected; | |
377 | ||
378 | module_cache() : paths_collected(false), dwarf_collected(false) {} | |
379 | }; | |
380 | typedef struct module_cache module_cache_t; | |
381 | ||
3bf6ac45 TM |
382 | #ifdef HAVE_TR1_UNORDERED_MAP |
383 | typedef tr1::unordered_map<string,Dwarf_Die> cu_function_cache_t; | |
384 | typedef tr1::unordered_map<string,cu_function_cache_t*> mod_cu_function_cache_t; // module:cu -> function -> die | |
385 | #else | |
2171f774 | 386 | struct stringhash { |
30369ac1 FCE |
387 | // __gnu_cxx:: is needed because our own hash.h has an ambiguous hash<> decl too. |
388 | size_t operator() (const string& s) const { __gnu_cxx::hash<const char*> h; return h(s.c_str()); } | |
2171f774 FCE |
389 | }; |
390 | ||
391 | typedef hash_map<string,Dwarf_Die,stringhash> cu_function_cache_t; | |
392 | typedef hash_map<string,cu_function_cache_t*,stringhash> mod_cu_function_cache_t; // module:cu -> function -> die | |
3bf6ac45 | 393 | #endif |
5f0a03a6 JK |
394 | |
395 | struct | |
396 | symbol_table | |
397 | { | |
398 | module_info *mod_info; // associated module | |
399 | map<string, func_info*> map_by_name; | |
400 | vector<func_info*> list_by_addr; | |
2e67a43b TM |
401 | typedef vector<func_info*>::iterator iterator_t; |
402 | typedef pair<iterator_t, iterator_t> range_t; | |
46f7b6be JK |
403 | #ifdef __powerpc__ |
404 | GElf_Word opd_section; | |
405 | #endif | |
2e67a43b TM |
406 | // add_symbol doesn't leave symbol table in order; call |
407 | // symbol_table::sort() when done adding symbols. | |
ab91b232 JK |
408 | void add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
409 | Dwarf_Addr *high_addr); | |
2e67a43b | 410 | void sort(); |
5f0a03a6 JK |
411 | enum info_status read_symbols(FILE *f, const string& path); |
412 | enum info_status read_from_elf_file(const string& path); | |
413 | enum info_status read_from_text_file(const string& path); | |
414 | enum info_status get_from_elf(); | |
46f7b6be JK |
415 | void prepare_section_rejection(Dwfl_Module *mod); |
416 | bool reject_section(GElf_Word section); | |
5f0a03a6 | 417 | void mark_dwarf_redundancies(dwflpp *dw); |
ab91b232 | 418 | void purge_syscall_stubs(); |
5f0a03a6 JK |
419 | func_info *lookup_symbol(const string& name); |
420 | Dwarf_Addr lookup_symbol_address(const string& name); | |
421 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
5f0a03a6 JK |
422 | |
423 | symbol_table(module_info *mi) : mod_info(mi) {} | |
424 | ~symbol_table(); | |
425 | }; | |
426 | ||
427 | static bool null_die(Dwarf_Die *die) | |
428 | { | |
429 | static Dwarf_Die null = { 0 }; | |
430 | return (!die || !memcmp(die, &null, sizeof(null))); | |
431 | } | |
432 | ||
7e1279ea FCE |
433 | static int |
434 | query_cu (Dwarf_Die * cudie, void * arg); | |
59ff2773 FCE |
435 | |
436 | ||
bd2b1e68 GH |
437 | // Helper for dealing with selected portions of libdwfl in a more readable |
438 | // fashion, and with specific cleanup / checking / logging options. | |
439 | ||
91eefb1c GH |
440 | static const char * |
441 | dwarf_diename_integrate (Dwarf_Die *die) | |
442 | { | |
443 | Dwarf_Attribute attr_mem; | |
444 | return dwarf_formstring (dwarf_attr_integrate (die, DW_AT_name, &attr_mem)); | |
445 | } | |
446 | ||
879eb9e9 SC |
447 | enum line_t { ABSOLUTE, RELATIVE, RANGE, WILDCARD }; |
448 | ||
3e961ba6 JB |
449 | typedef vector<inline_instance_info> inline_instance_map_t; |
450 | typedef vector<func_info> func_info_map_t; | |
451 | ||
a12a8a3f RA |
452 | |
453 | // PR 9941 introduces the need for a predicate | |
454 | ||
455 | int dwfl_report_offline_predicate (const char* modname, const char* filename) | |
456 | { | |
457 | if (pending_interrupts) { return -1; } | |
458 | return 1; | |
459 | } | |
460 | ||
b20febf3 | 461 | struct dwflpp |
bd2b1e68 | 462 | { |
5227f1ea | 463 | systemtap_session & sess; |
bd2b1e68 GH |
464 | Dwfl * dwfl; |
465 | ||
466 | // These are "current" values we focus on. | |
467 | Dwfl_Module * module; | |
468 | Dwarf * module_dwarf; | |
469 | Dwarf_Addr module_bias; | |
5f0a03a6 | 470 | module_info * mod_info; |
50e0d793 GH |
471 | |
472 | // These describe the current module's PC address range | |
473 | Dwarf_Addr module_start; | |
474 | Dwarf_Addr module_end; | |
475 | ||
bd2b1e68 | 476 | Dwarf_Die * cu; |
20e4a32c | 477 | Dwarf_Die * function; |
bd2b1e68 GH |
478 | |
479 | string module_name; | |
480 | string cu_name; | |
481 | string function_name; | |
482 | ||
7a053d3b | 483 | string const default_name(char const * in, |
78f6bba6 | 484 | char const *) |
bd2b1e68 | 485 | { |
7a053d3b | 486 | if (in) |
bd2b1e68 | 487 | return in; |
a229fcd7 | 488 | return string(""); |
bd2b1e68 GH |
489 | } |
490 | ||
50e0d793 | 491 | |
5f0a03a6 | 492 | void get_module_dwarf(bool required = false, bool report = true) |
5227f1ea | 493 | { |
91af0778 FCE |
494 | module_dwarf = dwfl_module_getdwarf(module, &module_bias); |
495 | mod_info->dwarf_status = (module_dwarf ? info_present : info_absent); | |
5f0a03a6 JK |
496 | if (!module_dwarf && report) |
497 | { | |
498 | string msg = "cannot find "; | |
499 | if (module_name == "") | |
500 | msg += "kernel"; | |
501 | else | |
502 | msg += string("module ") + module_name; | |
503 | msg += " debuginfo"; | |
504 | ||
505 | int i = dwfl_errno(); | |
506 | if (i) | |
507 | msg += string(": ") + dwfl_errmsg (i); | |
508 | ||
509 | if (required) | |
510 | throw semantic_error (msg); | |
511 | else | |
512 | cerr << "WARNING: " << msg << "\n"; | |
0ce64fb8 | 513 | } |
5227f1ea GH |
514 | } |
515 | ||
5f0a03a6 | 516 | void focus_on_module(Dwfl_Module * m, module_info * mi) |
bd2b1e68 | 517 | { |
bd2b1e68 | 518 | module = m; |
5f0a03a6 JK |
519 | mod_info = mi; |
520 | if (m) | |
521 | { | |
522 | module_name = default_name(dwfl_module_info(module, NULL, | |
50e0d793 | 523 | &module_start, &module_end, |
bd2b1e68 GH |
524 | NULL, NULL, |
525 | NULL, NULL), | |
526 | "module"); | |
5f0a03a6 JK |
527 | } |
528 | else | |
529 | { | |
530 | assert(mi && mi->name && mi->name == TOK_KERNEL); | |
531 | module_name = mi->name; | |
532 | module_start = 0; | |
533 | module_end = 0; | |
534 | module_bias = mi->bias; | |
535 | } | |
50e0d793 GH |
536 | |
537 | // Reset existing pointers and names | |
538 | ||
539 | module_dwarf = NULL; | |
540 | ||
a229fcd7 | 541 | cu_name.clear(); |
50e0d793 GH |
542 | cu = NULL; |
543 | ||
a229fcd7 | 544 | function_name.clear(); |
50e0d793 | 545 | function = NULL; |
bd2b1e68 GH |
546 | } |
547 | ||
50e0d793 | 548 | |
bd2b1e68 GH |
549 | void focus_on_cu(Dwarf_Die * c) |
550 | { | |
551 | assert(c); | |
50e0d793 GH |
552 | assert(module); |
553 | ||
bd2b1e68 | 554 | cu = c; |
50e0d793 GH |
555 | cu_name = default_name(dwarf_diename(c), "CU"); |
556 | ||
557 | // Reset existing pointers and names | |
a229fcd7 | 558 | function_name.clear(); |
50e0d793 | 559 | function = NULL; |
bd2b1e68 GH |
560 | } |
561 | ||
50e0d793 | 562 | |
20e4a32c | 563 | void focus_on_function(Dwarf_Die * f) |
bd2b1e68 GH |
564 | { |
565 | assert(f); | |
50e0d793 GH |
566 | assert(module); |
567 | assert(cu); | |
568 | ||
bd2b1e68 | 569 | function = f; |
20e4a32c | 570 | function_name = default_name(dwarf_diename(function), |
bd2b1e68 | 571 | "function"); |
bd2b1e68 GH |
572 | } |
573 | ||
50e0d793 | 574 | |
bd2b1e68 GH |
575 | void focus_on_module_containing_global_address(Dwarf_Addr a) |
576 | { | |
577 | assert(dwfl); | |
50e0d793 | 578 | cu = NULL; |
0ce64fb8 FCE |
579 | Dwfl_Module* mod = dwfl_addrmodule(dwfl, a); |
580 | if (mod) // address could be wildly out of range | |
5f0a03a6 | 581 | focus_on_module(mod, NULL); |
bd2b1e68 GH |
582 | } |
583 | ||
50e0d793 | 584 | |
7e1279ea | 585 | void query_cu_containing_global_address(Dwarf_Addr a, void *arg) |
bd2b1e68 | 586 | { |
bd2b1e68 | 587 | Dwarf_Addr bias; |
50e0d793 | 588 | assert(dwfl); |
5227f1ea | 589 | get_module_dwarf(); |
ab55a5ae FCE |
590 | Dwarf_Die* cudie = dwfl_module_addrdie(module, a, &bias); |
591 | if (cudie) // address could be wildly out of range | |
592 | query_cu (cudie, arg); | |
bd2b1e68 GH |
593 | assert(bias == module_bias); |
594 | } | |
595 | ||
50e0d793 | 596 | |
7e1279ea | 597 | void query_cu_containing_module_address(Dwarf_Addr a, void *arg) |
bd2b1e68 | 598 | { |
7e1279ea | 599 | query_cu_containing_global_address(module_address_to_global(a), arg); |
bd2b1e68 GH |
600 | } |
601 | ||
50e0d793 | 602 | |
bd2b1e68 GH |
603 | Dwarf_Addr module_address_to_global(Dwarf_Addr a) |
604 | { | |
50e0d793 | 605 | assert(dwfl); |
bd2b1e68 | 606 | assert(module); |
5227f1ea | 607 | get_module_dwarf(); |
76e954ca | 608 | if (module_name == TOK_KERNEL || dwfl_module_relocations (module) <= 0) |
c239d28c | 609 | return a; |
50e0d793 | 610 | return a + module_start; |
bd2b1e68 GH |
611 | } |
612 | ||
50e0d793 | 613 | |
bd2b1e68 GH |
614 | Dwarf_Addr global_address_to_module(Dwarf_Addr a) |
615 | { | |
616 | assert(module); | |
5227f1ea | 617 | get_module_dwarf(); |
bd2b1e68 GH |
618 | return a - module_bias; |
619 | } | |
620 | ||
621 | ||
622 | bool module_name_matches(string pattern) | |
623 | { | |
bd2b1e68 | 624 | bool t = (fnmatch(pattern.c_str(), module_name.c_str(), 0) == 0); |
b8da0ad1 | 625 | if (t && sess.verbose>3) |
bd2b1e68 | 626 | clog << "pattern '" << pattern << "' " |
24cb178f | 627 | << "matches " |
db22e55f | 628 | << "module '" << module_name << "'" << "\n"; |
bd2b1e68 GH |
629 | return t; |
630 | } | |
5f0a03a6 JK |
631 | bool name_has_wildcard(string pattern) |
632 | { | |
633 | return (pattern.find('*') != string::npos || | |
634 | pattern.find('?') != string::npos || | |
635 | pattern.find('[') != string::npos); | |
636 | } | |
b8da0ad1 FCE |
637 | bool module_name_final_match(string pattern) |
638 | { | |
639 | // Assume module_name_matches(). Can there be any more matches? | |
640 | // Not unless the pattern is a wildcard, since module names are | |
641 | // presumed unique. | |
5f0a03a6 | 642 | return !name_has_wildcard(pattern); |
b8da0ad1 | 643 | } |
bd2b1e68 | 644 | |
50e0d793 | 645 | |
5f0a03a6 | 646 | bool function_name_matches_pattern(string name, string pattern) |
bd2b1e68 | 647 | { |
5f0a03a6 | 648 | bool t = (fnmatch(pattern.c_str(), name.c_str(), 0) == 0); |
b8da0ad1 | 649 | if (t && sess.verbose>3) |
bd2b1e68 | 650 | clog << "pattern '" << pattern << "' " |
24cb178f | 651 | << "matches " |
5f0a03a6 | 652 | << "function '" << name << "'" << "\n"; |
bd2b1e68 GH |
653 | return t; |
654 | } | |
5f0a03a6 JK |
655 | bool function_name_matches(string pattern) |
656 | { | |
657 | assert(function); | |
658 | return function_name_matches_pattern(function_name, pattern); | |
659 | } | |
275f40a6 FCE |
660 | bool function_name_final_match(string pattern) |
661 | { | |
662 | return module_name_final_match (pattern); | |
663 | } | |
bd2b1e68 | 664 | |
50e0d793 | 665 | |
bd2b1e68 GH |
666 | bool cu_name_matches(string pattern) |
667 | { | |
668 | assert(cu); | |
3213d089 FCE |
669 | |
670 | // PR 5049: implicit * in front of given path pattern. | |
671 | // NB: fnmatch() is used without FNM_PATHNAME. | |
79640c29 | 672 | string prefixed_pattern = string("*/") + pattern; |
3213d089 | 673 | |
79640c29 FCE |
674 | bool t = (fnmatch(pattern.c_str(), cu_name.c_str(), 0) == 0 || |
675 | fnmatch(prefixed_pattern.c_str(), cu_name.c_str(), 0) == 0); | |
b8da0ad1 | 676 | if (t && sess.verbose>3) |
3213d089 | 677 | clog << "pattern '" << prefixed_pattern << "' " |
24cb178f | 678 | << "matches " |
db22e55f | 679 | << "CU '" << cu_name << "'" << "\n"; |
bd2b1e68 GH |
680 | return t; |
681 | } | |
682 | ||
5f0a03a6 | 683 | dwflpp(systemtap_session & session) |
bd2b1e68 | 684 | : |
5f0a03a6 | 685 | sess(session), |
bd2b1e68 GH |
686 | dwfl(NULL), |
687 | module(NULL), | |
688 | module_dwarf(NULL), | |
689 | module_bias(0), | |
5f0a03a6 | 690 | mod_info(NULL), |
50e0d793 GH |
691 | module_start(0), |
692 | module_end(0), | |
bd2b1e68 GH |
693 | cu(NULL), |
694 | function(NULL) | |
5f0a03a6 | 695 | { |
5f0a03a6 | 696 | } |
7a053d3b | 697 | |
50e0d793 | 698 | |
b5e66ada FCE |
699 | // XXX: See also translate.cxx:emit_symbol_data |
700 | ||
7a24d422 | 701 | void setup_kernel(bool debuginfo_needed = true) |
bd2b1e68 | 702 | { |
405b71b8 FCE |
703 | if (! sess.module_cache) |
704 | sess.module_cache = new module_cache (); | |
705 | ||
0dc34322 | 706 | static const char *debuginfo_path_arr = "+:.debug:/usr/lib/debug:build"; |
b5e66ada FCE |
707 | static const char *debuginfo_env_arr = getenv("SYSTEMTAP_DEBUGINFO_PATH"); |
708 | static const char *debuginfo_path = (debuginfo_env_arr ?: debuginfo_path_arr ); | |
b5d77020 | 709 | |
bd2b1e68 GH |
710 | static const Dwfl_Callbacks kernel_callbacks = |
711 | { | |
712 | dwfl_linux_kernel_find_elf, | |
713 | dwfl_standard_find_debuginfo, | |
b20febf3 | 714 | dwfl_offline_section_address, |
b5e66ada | 715 | (char **) & debuginfo_path |
bd2b1e68 GH |
716 | }; |
717 | ||
7a24d422 FCE |
718 | dwfl = dwfl_begin (&kernel_callbacks); |
719 | if (!dwfl) | |
720 | throw semantic_error ("cannot open dwfl"); | |
721 | dwfl_report_begin (dwfl); | |
06aca46a | 722 | |
b5e66ada FCE |
723 | // We have a problem with -r REVISION vs -r BUILDDIR here. If |
724 | // we're running against a fedora/rhel style kernel-debuginfo | |
725 | // tree, s.kernel_build_tree is not the place where the unstripped | |
726 | // vmlinux will be installed. Rather, it's over yonder at | |
727 | // /usr/lib/debug/lib/modules/$REVISION/. It seems that there is | |
728 | // no way to set the dwfl_callback.debuginfo_path and always | |
729 | // passs the plain kernel_release here. So instead we have to | |
730 | // hard-code this magic here. | |
731 | string elfutils_kernel_path; | |
732 | if (sess.kernel_build_tree == string("/lib/modules/" + sess.kernel_release + "/build")) | |
733 | elfutils_kernel_path = sess.kernel_release; | |
734 | else | |
735 | elfutils_kernel_path = sess.kernel_build_tree; | |
736 | ||
7a24d422 | 737 | int rc = dwfl_linux_kernel_report_offline (dwfl, |
b5e66ada | 738 | elfutils_kernel_path.c_str(), |
a12a8a3f | 739 | &dwfl_report_offline_predicate); |
06aca46a | 740 | |
7a24d422 | 741 | if (debuginfo_needed) |
b5e66ada FCE |
742 | dwfl_assert (string("missing ") + sess.architecture + |
743 | string(" kernel/module debuginfo under '") + | |
744 | sess.kernel_build_tree + string("'"), | |
7a24d422 | 745 | rc); |
06aca46a | 746 | |
7a24d422 FCE |
747 | // XXX: it would be nice if we could do a single |
748 | // ..._report_offline call for an entire systemtap script, so | |
749 | // that a selection predicate would filter out modules outside | |
750 | // the union of all the requested wildcards. But we build | |
751 | // derived_probes one-by-one and we don't have lookahead. | |
752 | // PR 3498. | |
06aca46a | 753 | |
7a24d422 FCE |
754 | // XXX: a special case: if we have only kernel.* probe points, |
755 | // we shouldn't waste time looking for module debug-info (and | |
756 | // vice versa). | |
06aca46a | 757 | |
7a24d422 FCE |
758 | // NB: the result of an _offline call is the assignment of |
759 | // virtualized addresses to relocatable objects such as | |
760 | // modules. These have to be converted to real addresses at | |
761 | // run time. See the dwarf_derived_probe ctor and its caller. | |
762 | ||
763 | dwfl_assert ("dwfl_report_end", dwfl_report_end(dwfl, NULL, NULL)); | |
764 | } | |
765 | ||
766 | void setup_user(string module_name, bool debuginfo_needed = true) | |
767 | { | |
dd22832a JS |
768 | if (! sess.module_cache) |
769 | sess.module_cache = new module_cache (); | |
770 | ||
0dc34322 | 771 | static const char *debuginfo_path_arr = "+:.debug:/usr/lib/debug:build"; |
b5e66ada FCE |
772 | static const char *debuginfo_env_arr = getenv("SYSTEMTAP_DEBUGINFO_PATH"); |
773 | // NB: kernel_build_tree doesn't enter into this, as it's for | |
774 | // kernel-side modules only. | |
775 | static const char *debuginfo_path = (debuginfo_env_arr ?: debuginfo_path_arr); | |
7a24d422 FCE |
776 | |
777 | static const Dwfl_Callbacks user_callbacks = | |
bd2b1e68 | 778 | { |
7a24d422 FCE |
779 | NULL, /* dwfl_linux_kernel_find_elf, */ |
780 | dwfl_standard_find_debuginfo, | |
781 | dwfl_offline_section_address, | |
b5e66ada | 782 | (char **) & debuginfo_path |
7a24d422 | 783 | }; |
b20febf3 | 784 | |
7a24d422 FCE |
785 | dwfl = dwfl_begin (&user_callbacks); |
786 | if (!dwfl) | |
787 | throw semantic_error ("cannot open dwfl"); | |
788 | dwfl_report_begin (dwfl); | |
789 | ||
7a24d422 | 790 | // XXX: should support buildid-based naming |
405b71b8 | 791 | |
7a24d422 FCE |
792 | Dwfl_Module *mod = dwfl_report_offline (dwfl, |
793 | module_name.c_str(), | |
794 | module_name.c_str(), | |
795 | -1); | |
796 | // XXX: save mod! | |
5f0a03a6 | 797 | |
7a24d422 FCE |
798 | if (debuginfo_needed) |
799 | dwfl_assert (string("missing process ") + | |
800 | module_name + | |
801 | string(" ") + | |
802 | sess.architecture + | |
803 | string(" debuginfo"), | |
804 | mod); | |
b20febf3 | 805 | |
f28a8c28 SC |
806 | if (!module) |
807 | module = mod; | |
808 | ||
7a24d422 FCE |
809 | // NB: the result of an _offline call is the assignment of |
810 | // virtualized addresses to relocatable objects such as | |
811 | // modules. These have to be converted to real addresses at | |
812 | // run time. See the dwarf_derived_probe ctor and its caller. | |
bd2b1e68 | 813 | |
7e1279ea | 814 | dwfl_assert ("dwfl_report_end", dwfl_report_end(dwfl, NULL, NULL)); |
bd2b1e68 GH |
815 | } |
816 | ||
b8da0ad1 FCE |
817 | |
818 | ||
819 | // ----------------------------------------------------------------- | |
820 | ||
91af0778 | 821 | void iterate_over_modules(int (* callback)(Dwfl_Module *, void **, |
5f0a03a6 JK |
822 | const char *, Dwarf_Addr, |
823 | void *), | |
c4ce66a1 | 824 | base_query *data) |
5f0a03a6 | 825 | { |
91af0778 FCE |
826 | ptrdiff_t off = 0; |
827 | do | |
b8da0ad1 | 828 | { |
49abf162 | 829 | if (pending_interrupts) return; |
91af0778 | 830 | off = dwfl_getmodules (dwfl, callback, data, off); |
bd2b1e68 | 831 | } |
91af0778 | 832 | while (off > 0); |
432f054f FCE |
833 | // Don't complain if we exited dwfl_getmodules early. |
834 | // This could be a $target variable error that will be | |
835 | // reported soon anyway. | |
836 | // dwfl_assert("dwfl_getmodules", off == 0); | |
6f4c1275 FCE |
837 | |
838 | // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the | |
839 | // "kernel" module is not reported in the loop above. However, we | |
840 | // may be able to make do with symbol table data. | |
bd2b1e68 GH |
841 | } |
842 | ||
91af0778 | 843 | |
5f0a03a6 | 844 | // Defined after dwarf_query |
c4ce66a1 | 845 | void query_modules(base_query *q); |
7e1279ea | 846 | |
b8da0ad1 FCE |
847 | |
848 | // ----------------------------------------------------------------- | |
849 | ||
850 | typedef map<Dwarf*, vector<Dwarf_Die>*> module_cu_cache_t; | |
851 | module_cu_cache_t module_cu_cache; | |
852 | ||
7a053d3b | 853 | void iterate_over_cus (int (*callback)(Dwarf_Die * die, void * arg), |
bd2b1e68 GH |
854 | void * data) |
855 | { | |
0ce64fb8 | 856 | get_module_dwarf(false); |
b8da0ad1 FCE |
857 | Dwarf *dw = module_dwarf; |
858 | if (!dw) return; | |
5227f1ea | 859 | |
b8da0ad1 FCE |
860 | vector<Dwarf_Die>* v = module_cu_cache[dw]; |
861 | if (v == 0) | |
862 | { | |
863 | v = new vector<Dwarf_Die>; | |
864 | module_cu_cache[dw] = v; | |
bd2b1e68 | 865 | |
b8da0ad1 FCE |
866 | Dwarf_Off off = 0; |
867 | size_t cuhl; | |
868 | Dwarf_Off noff; | |
869 | while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) | |
870 | { | |
49abf162 | 871 | if (pending_interrupts) return; |
b8da0ad1 FCE |
872 | Dwarf_Die die_mem; |
873 | Dwarf_Die *die; | |
874 | die = dwarf_offdie (dw, off + cuhl, &die_mem); | |
875 | v->push_back (*die); /* copy */ | |
876 | off = noff; | |
877 | } | |
878 | } | |
879 | ||
880 | for (unsigned i = 0; i < v->size(); i++) | |
7a053d3b | 881 | { |
49abf162 | 882 | if (pending_interrupts) return; |
b8da0ad1 FCE |
883 | Dwarf_Die die = v->at(i); |
884 | int rc = (*callback)(& die, data); | |
885 | if (rc != DWARF_CB_OK) break; | |
bd2b1e68 GH |
886 | } |
887 | } | |
888 | ||
bd2b1e68 | 889 | |
b8da0ad1 FCE |
890 | // ----------------------------------------------------------------- |
891 | ||
7e1279ea | 892 | bool func_is_inline() |
bd2b1e68 | 893 | { |
7e1279ea FCE |
894 | assert (function); |
895 | return dwarf_func_inline (function) != 0; | |
bd2b1e68 GH |
896 | } |
897 | ||
b8da0ad1 FCE |
898 | |
899 | typedef map<string, vector<Dwarf_Die>*> cu_inl_function_cache_t; | |
900 | cu_inl_function_cache_t cu_inl_function_cache; | |
901 | ||
902 | static int cu_inl_function_caching_callback (Dwarf_Die* func, void *arg) | |
903 | { | |
904 | vector<Dwarf_Die>* v = static_cast<vector<Dwarf_Die>*>(arg); | |
905 | v->push_back (* func); | |
906 | return DWARF_CB_OK; | |
907 | } | |
908 | ||
7e1279ea FCE |
909 | void iterate_over_inline_instances (int (* callback)(Dwarf_Die * die, void * arg), |
910 | void * data) | |
bd2b1e68 | 911 | { |
7e1279ea FCE |
912 | assert (function); |
913 | assert (func_is_inline ()); | |
b8da0ad1 FCE |
914 | |
915 | string key = module_name + ":" + cu_name + ":" + function_name; | |
916 | vector<Dwarf_Die>* v = cu_inl_function_cache[key]; | |
917 | if (v == 0) | |
918 | { | |
919 | v = new vector<Dwarf_Die>; | |
920 | cu_inl_function_cache[key] = v; | |
921 | dwarf_func_inline_instances (function, cu_inl_function_caching_callback, v); | |
922 | } | |
923 | ||
924 | for (unsigned i=0; i<v->size(); i++) | |
925 | { | |
f76427a2 | 926 | if (pending_interrupts) return; |
b8da0ad1 FCE |
927 | Dwarf_Die die = v->at(i); |
928 | int rc = (*callback)(& die, data); | |
929 | if (rc != DWARF_CB_OK) break; | |
930 | } | |
4fa7b22b | 931 | } |
bd2b1e68 | 932 | |
50e0d793 | 933 | |
b8da0ad1 FCE |
934 | // ----------------------------------------------------------------- |
935 | ||
7bb73781 | 936 | /* The global alias cache is used to resolve any DIE found in a |
0b8f6579 JB |
937 | * module that is stubbed out with DW_AT_declaration with a defining |
938 | * DIE found in a different module. The current assumption is that | |
939 | * this only applies to structures and unions, which have a global | |
940 | * namespace (it deliberately only traverses program scope), so this | |
941 | * cache is indexed by name. If other declaration lookups were | |
942 | * added to it, it would have to be indexed by name and tag | |
943 | */ | |
7bb73781 | 944 | mod_cu_function_cache_t global_alias_cache; |
0b8f6579 JB |
945 | static int global_alias_caching_callback(Dwarf_Die *die, void *arg) |
946 | { | |
7bb73781 | 947 | cu_function_cache_t *cache = static_cast<cu_function_cache_t*>(arg); |
0b8f6579 JB |
948 | const char *name = dwarf_diename(die); |
949 | ||
950 | if (!name) | |
951 | return DWARF_CB_OK; | |
952 | ||
953 | string structure_name = name; | |
954 | ||
955 | if (!dwarf_hasattr(die, DW_AT_declaration) && | |
7bb73781 FCE |
956 | cache->find(structure_name) == cache->end()) |
957 | (*cache)[structure_name] = *die; | |
0b8f6579 JB |
958 | |
959 | return DWARF_CB_OK; | |
960 | } | |
961 | ||
c4ce66a1 | 962 | Dwarf_Die *declaration_resolve(const char *name) |
0b8f6579 | 963 | { |
0b8f6579 JB |
964 | if (!name) |
965 | return NULL; | |
966 | ||
7bb73781 FCE |
967 | string key = module_name + ":" + cu_name; |
968 | cu_function_cache_t *v = global_alias_cache[key]; | |
969 | if (v == 0) // need to build the cache, just once per encountered module/cu | |
970 | { | |
971 | v = new cu_function_cache_t; | |
972 | global_alias_cache[key] = v; | |
973 | iterate_over_globals(global_alias_caching_callback, v); | |
974 | if (sess.verbose > 4) | |
975 | clog << "global alias cache " << key << " size " << v->size() << endl; | |
976 | } | |
977 | ||
978 | // XXX: it may be desirable to search other modules' declarations | |
979 | // too, in case a module/shared-library processes a | |
980 | // forward-declared pointer type only, where the actual definition | |
981 | // may only be in vmlinux or the application. | |
982 | ||
983 | // XXX: it is probably desirable to search other CU's declarations | |
984 | // in the same module. | |
41c262f3 | 985 | |
7bb73781 | 986 | if (v->find(name) == v->end()) |
0b8f6579 JB |
987 | return NULL; |
988 | ||
7bb73781 | 989 | return & ((*v)[name]); |
0b8f6579 JB |
990 | } |
991 | ||
6561773f | 992 | mod_cu_function_cache_t cu_function_cache; |
b8da0ad1 FCE |
993 | |
994 | static int cu_function_caching_callback (Dwarf_Die* func, void *arg) | |
995 | { | |
6561773f | 996 | cu_function_cache_t* v = static_cast<cu_function_cache_t*>(arg); |
0e68eaaa DS |
997 | const char *name = dwarf_diename(func); |
998 | if (!name) | |
999 | return DWARF_CB_OK; | |
1000 | ||
1001 | string function_name = name; | |
6561773f | 1002 | (*v)[function_name] = * func; |
b8da0ad1 FCE |
1003 | return DWARF_CB_OK; |
1004 | } | |
1005 | ||
2da9cedb JS |
1006 | int iterate_over_functions (int (* callback)(Dwarf_Die * func, base_query * q), |
1007 | base_query * q, const string& function, | |
1008 | bool has_statement_num=false); | |
0b8f6579 JB |
1009 | int iterate_over_globals (int (* callback)(Dwarf_Die *, void *), |
1010 | void * data); | |
1011 | ||
fedd4090 | 1012 | bool has_single_line_record (dwarf_query * q, char const * srcfile, int lineno); |
897820ca | 1013 | |
7e1279ea | 1014 | void iterate_over_srcfile_lines (char const * srcfile, |
879eb9e9 | 1015 | int lines[2], |
897820ca | 1016 | bool need_single_match, |
879eb9e9 | 1017 | enum line_t line_type, |
86bf665e TM |
1018 | void (* callback) (const dwarf_line_t& line, |
1019 | void * arg), | |
7e1279ea FCE |
1020 | void *data) |
1021 | { | |
6315bd76 GH |
1022 | Dwarf_Line **srcsp = NULL; |
1023 | size_t nsrcs = 0; | |
fedd4090 | 1024 | dwarf_query * q = static_cast<dwarf_query *>(data); |
879eb9e9 | 1025 | int lineno = lines[0]; |
558982c5 | 1026 | auto_free_ref<Dwarf_Line**> free_srcsp(srcsp); |
41c262f3 | 1027 | |
7e1279ea | 1028 | get_module_dwarf(); |
bb788f9f | 1029 | |
41c262f3 | 1030 | if (line_type == RELATIVE) |
0c8b7d37 SC |
1031 | { |
1032 | Dwarf_Addr addr; | |
1033 | Dwarf_Line *line; | |
1034 | int line_number; | |
41c262f3 | 1035 | |
0c8b7d37 SC |
1036 | dwarf_assert ("dwarf_entrypc", dwarf_entrypc (this->function, &addr)); |
1037 | line = dwarf_getsrc_die (this->cu, addr); | |
1038 | dwarf_assert ("dwarf_getsrc_die", line == NULL); | |
1039 | dwarf_assert ("dwarf_lineno", dwarf_lineno (line, &line_number)); | |
1040 | lineno += line_number; | |
1041 | } | |
879eb9e9 SC |
1042 | else if (line_type == WILDCARD) |
1043 | function_line (&lineno); | |
41c262f3 | 1044 | |
879eb9e9 SC |
1045 | for (int l = lineno; ; l = l + 1) |
1046 | { | |
2ffc7958 SC |
1047 | set<int> lines_probed; |
1048 | pair<set<int>::iterator,bool> line_probed; | |
879eb9e9 SC |
1049 | dwarf_assert ("dwarf_getsrc_file", |
1050 | dwarf_getsrc_file (module_dwarf, | |
1051 | srcfile, l, 0, | |
1052 | &srcsp, &nsrcs)); | |
879eb9e9 SC |
1053 | if (line_type == WILDCARD || line_type == RANGE) |
1054 | { | |
1055 | Dwarf_Addr line_addr; | |
1056 | dwarf_lineno (srcsp [0], &lineno); | |
2ffc7958 | 1057 | line_probed = lines_probed.insert(lineno); |
69087111 | 1058 | if (lineno != l || line_probed.second == false || nsrcs > 1) |
879eb9e9 SC |
1059 | continue; |
1060 | dwarf_lineaddr (srcsp [0], &line_addr); | |
1061 | if (dwarf_haspc (function, line_addr) != 1) | |
1062 | break; | |
1063 | } | |
fedd4090 | 1064 | |
879eb9e9 | 1065 | // NB: Formerly, we used to filter, because: |
20e4a32c | 1066 | |
879eb9e9 SC |
1067 | // dwarf_getsrc_file gets one *near hits* for line numbers, not |
1068 | // exact matches. For example, an existing file but a nonexistent | |
1069 | // line number will be rounded up to the next definition in that | |
1070 | // file. This may be similar to the GDB breakpoint algorithm, but | |
1071 | // we don't want to be so fuzzy in systemtap land. So we filter. | |
847bf07f | 1072 | |
879eb9e9 | 1073 | // But we now see the error of our ways, and skip this filtering. |
fedd4090 | 1074 | |
879eb9e9 SC |
1075 | // XXX: the code also fails to match e.g. inline function |
1076 | // definitions when the srcfile is a header file rather than the | |
1077 | // CU name. | |
847bf07f | 1078 | |
879eb9e9 | 1079 | size_t remaining_nsrcs = nsrcs; |
847bf07f | 1080 | |
879eb9e9 | 1081 | if (need_single_match && remaining_nsrcs > 1) |
897820ca | 1082 | { |
879eb9e9 SC |
1083 | // We wanted a single line record (a unique address for the |
1084 | // line) and we got a bunch of line records. We're going to | |
1085 | // skip this probe (throw an exception) but before we throw | |
1086 | // we're going to look around a bit to see if there's a low or | |
1087 | // high line number nearby which *doesn't* have this problem, | |
1088 | // so we can give the user some advice. | |
1089 | ||
1090 | int lo_try = -1; | |
1091 | int hi_try = -1; | |
1092 | for (size_t i = 1; i < 6; ++i) | |
1093 | { | |
1094 | if (lo_try == -1 && has_single_line_record(q, srcfile, lineno - i)) | |
1095 | lo_try = lineno - i; | |
897820ca | 1096 | |
879eb9e9 SC |
1097 | if (hi_try == -1 && has_single_line_record(q, srcfile, lineno + i)) |
1098 | hi_try = lineno + i; | |
1099 | } | |
897820ca | 1100 | |
879eb9e9 SC |
1101 | stringstream advice; |
1102 | advice << "multiple addresses for " << srcfile << ":" << lineno; | |
1103 | if (lo_try > 0 || hi_try > 0) | |
1104 | { | |
1105 | advice << " (try "; | |
1106 | if (lo_try > 0) | |
1107 | advice << srcfile << ":" << lo_try; | |
1108 | if (lo_try > 0 && hi_try > 0) | |
1109 | advice << " or "; | |
1110 | if (hi_try > 0) | |
1111 | advice << srcfile << ":" << hi_try; | |
1112 | advice << ")"; | |
1113 | } | |
1114 | throw semantic_error (advice.str()); | |
1115 | } | |
897820ca | 1116 | |
2e67a43b TM |
1117 | for (size_t i = 0; i < nsrcs; ++i) |
1118 | { | |
1119 | if (pending_interrupts) return; | |
1120 | if (srcsp [i]) // skip over mismatched lines | |
1121 | callback (dwarf_line_t(srcsp[i]), data); | |
1122 | } | |
69087111 SC |
1123 | |
1124 | if (line_type == ABSOLUTE || line_type == RELATIVE) | |
1125 | break; | |
1126 | else if (line_type == RANGE && l == lines[1]) | |
1127 | break; | |
20e4a32c | 1128 | } |
50e0d793 GH |
1129 | } |
1130 | ||
0f336e95 | 1131 | void |
bd4b874d SC |
1132 | iterate_over_labels (Dwarf_Die *begin_die, |
1133 | void *data, | |
1134 | void (* callback)(const string &, | |
1135 | const char *, | |
1136 | int, | |
1137 | Dwarf_Die *, | |
1138 | Dwarf_Addr, | |
1139 | dwarf_query *)); | |
0f336e95 | 1140 | |
7e1279ea FCE |
1141 | void collect_srcfiles_matching (string const & pattern, |
1142 | set<char const *> & filtered_srcfiles) | |
50e0d793 | 1143 | { |
7e1279ea FCE |
1144 | assert (module); |
1145 | assert (cu); | |
bb788f9f | 1146 | |
7e1279ea FCE |
1147 | size_t nfiles; |
1148 | Dwarf_Files *srcfiles; | |
bb788f9f | 1149 | |
3213d089 FCE |
1150 | // PR 5049: implicit * in front of given path pattern. |
1151 | // NB: fnmatch() is used without FNM_PATHNAME. | |
79640c29 | 1152 | string prefixed_pattern = string("*/") + pattern; |
3213d089 | 1153 | |
20e4a32c | 1154 | dwarf_assert ("dwarf_getsrcfiles", |
7e1279ea FCE |
1155 | dwarf_getsrcfiles (cu, &srcfiles, &nfiles)); |
1156 | { | |
1157 | for (size_t i = 0; i < nfiles; ++i) | |
50e0d793 | 1158 | { |
7e1279ea | 1159 | char const * fname = dwarf_filesrc (srcfiles, i, NULL, NULL); |
79640c29 FCE |
1160 | if (fnmatch (pattern.c_str(), fname, 0) == 0 || |
1161 | fnmatch (prefixed_pattern.c_str(), fname, 0) == 0) | |
50e0d793 | 1162 | { |
7e1279ea | 1163 | filtered_srcfiles.insert (fname); |
b0ee93c4 | 1164 | if (sess.verbose>2) |
db22e55f | 1165 | clog << "selected source file '" << fname << "'\n"; |
50e0d793 GH |
1166 | } |
1167 | } | |
7e1279ea | 1168 | } |
20e4a32c | 1169 | } |
50e0d793 | 1170 | |
3e961ba6 | 1171 | void resolve_prologue_endings (func_info_map_t & funcs) |
7d71e1d5 FCE |
1172 | { |
1173 | // This heuristic attempts to pick the first address that has a | |
34ca7d84 FCE |
1174 | // source line distinct from the function declaration's. In a |
1175 | // perfect world, this would be the first statement *past* the | |
1176 | // prologue. | |
1177 | ||
7d71e1d5 FCE |
1178 | assert(module); |
1179 | assert(cu); | |
1180 | ||
456aa31c FCE |
1181 | size_t nlines = 0; |
1182 | Dwarf_Lines *lines = NULL; | |
7d71e1d5 | 1183 | |
dc223023 FCE |
1184 | /* trouble cases: |
1185 | malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue | |
1186 | sys_get?id in kernel/timer.c no-prologue | |
1187 | sys_exit_group tail-recursive | |
1188 | {do_,}sys_open extra-long-prologue (gcc 3.4) | |
1189 | cpu_to_logical_apicid NULL-decl_file | |
1190 | */ | |
1191 | ||
1192 | // Fetch all srcline records, sorted by address. | |
20e4a32c RM |
1193 | dwarf_assert ("dwarf_getsrclines", |
1194 | dwarf_getsrclines(cu, &lines, &nlines)); | |
dc223023 | 1195 | // XXX: free lines[] later, but how? |
7d71e1d5 | 1196 | |
3e961ba6 | 1197 | for(func_info_map_t::iterator it = funcs.begin(); it != funcs.end(); it++) |
7d71e1d5 | 1198 | { |
dc223023 FCE |
1199 | #if 0 /* someday */ |
1200 | Dwarf_Addr* bkpts = 0; | |
3e961ba6 | 1201 | int n = dwarf_entry_breakpoints (& it->die, & bkpts); |
dc223023 FCE |
1202 | // ... |
1203 | free (bkpts); | |
1204 | #endif | |
20e4a32c | 1205 | |
3e961ba6 | 1206 | Dwarf_Addr entrypc = it->entrypc; |
dc223023 | 1207 | Dwarf_Addr highpc; // NB: highpc is exclusive: [entrypc,highpc) |
3e961ba6 | 1208 | dwfl_assert ("dwarf_highpc", dwarf_highpc (& it->die, |
dc223023 FCE |
1209 | & highpc)); |
1210 | ||
3e961ba6 | 1211 | if (it->decl_file == 0) it->decl_file = ""; |
e38d6504 | 1212 | |
dc223023 | 1213 | unsigned entrypc_srcline_idx = 0; |
86bf665e | 1214 | dwarf_line_t entrypc_srcline; |
dc223023 FCE |
1215 | // open-code binary search for exact match |
1216 | { | |
1217 | unsigned l = 0, h = nlines; | |
1218 | while (l < h) | |
1219 | { | |
1220 | entrypc_srcline_idx = (l + h) / 2; | |
86bf665e TM |
1221 | const dwarf_line_t lr(dwarf_onesrcline(lines, |
1222 | entrypc_srcline_idx)); | |
1223 | Dwarf_Addr addr = lr.addr(); | |
dc223023 FCE |
1224 | if (addr == entrypc) { entrypc_srcline = lr; break; } |
1225 | else if (l + 1 == h) { break; } // ran off bottom of tree | |
1226 | else if (addr < entrypc) { l = entrypc_srcline_idx; } | |
1227 | else { h = entrypc_srcline_idx; } | |
e38d6504 | 1228 | } |
dc223023 | 1229 | } |
86bf665e | 1230 | if (!entrypc_srcline) |
dfa11ddb FCE |
1231 | { |
1232 | if (sess.verbose > 2) | |
1233 | clog << "missing entrypc dwarf line record for function '" | |
1234 | << it->name << "'\n"; | |
1235 | // This is probably an inlined function. We'll end up using | |
1236 | // its lowpc as a probe address. | |
1237 | continue; | |
1238 | } | |
dc223023 FCE |
1239 | |
1240 | if (sess.verbose>2) | |
3e961ba6 | 1241 | clog << "prologue searching function '" << it->name << "'" |
5fe3e97f | 1242 | << " 0x" << hex << entrypc << "-0x" << highpc << dec |
3e961ba6 | 1243 | << "@" << it->decl_file << ":" << it->decl_line |
dc223023 FCE |
1244 | << "\n"; |
1245 | ||
1246 | // Now we go searching for the first line record that has a | |
1247 | // file/line different from the one in the declaration. | |
1248 | // Normally, this will be the next one. BUT: | |
1249 | // | |
1250 | // We may have to skip a few because some old compilers plop | |
1251 | // in dummy line records for longer prologues. If we go too | |
1252 | // far (addr >= highpc), we take the previous one. Or, it may | |
1253 | // be the first one, if the function had no prologue, and thus | |
1254 | // the entrypc maps to a statement in the body rather than the | |
1255 | // declaration. | |
1256 | ||
1257 | unsigned postprologue_srcline_idx = entrypc_srcline_idx; | |
1258 | bool ranoff_end = false; | |
35f5f091 | 1259 | while (postprologue_srcline_idx < nlines) |
7d71e1d5 | 1260 | { |
86bf665e TM |
1261 | dwarf_line_t lr(dwarf_onesrcline(lines, postprologue_srcline_idx)); |
1262 | Dwarf_Addr postprologue_addr = lr.addr(); | |
1263 | const char* postprologue_file = lr.linesrc(); | |
1264 | int postprologue_lineno = lr.lineno(); | |
456aa31c | 1265 | |
b0ee93c4 | 1266 | if (sess.verbose>2) |
dc223023 FCE |
1267 | clog << "checking line record 0x" << hex << postprologue_addr << dec |
1268 | << "@" << postprologue_file << ":" << postprologue_lineno << "\n"; | |
1269 | ||
1270 | if (postprologue_addr >= highpc) | |
e38d6504 RM |
1271 | { |
1272 | ranoff_end = true; | |
1273 | postprologue_srcline_idx --; | |
dc223023 FCE |
1274 | continue; |
1275 | } | |
1276 | if (ranoff_end || | |
3e961ba6 JB |
1277 | (strcmp (postprologue_file, it->decl_file) || // We have a winner! |
1278 | (postprologue_lineno != it->decl_line))) | |
dc223023 | 1279 | { |
3e961ba6 | 1280 | it->prologue_end = postprologue_addr; |
dc223023 FCE |
1281 | |
1282 | if (sess.verbose>2) | |
1283 | { | |
3e961ba6 | 1284 | clog << "prologue found function '" << it->name << "'"; |
dc223023 FCE |
1285 | // Add a little classification datum |
1286 | if (postprologue_srcline_idx == entrypc_srcline_idx) clog << " (naked)"; | |
1287 | if (ranoff_end) clog << " (tail-call?)"; | |
1288 | clog << " = 0x" << hex << postprologue_addr << dec << "\n"; | |
1289 | } | |
1290 | ||
1291 | break; | |
1292 | } | |
e38d6504 | 1293 | |
dc223023 FCE |
1294 | // Let's try the next srcline. |
1295 | postprologue_srcline_idx ++; | |
1296 | } // loop over srclines | |
7d71e1d5 | 1297 | |
3e961ba6 | 1298 | // if (strlen(it->decl_file) == 0) it->decl_file = NULL; |
dc223023 FCE |
1299 | |
1300 | } // loop over functions | |
b20febf3 FCE |
1301 | |
1302 | // XXX: how to free lines? | |
bd2b1e68 GH |
1303 | } |
1304 | ||
7e1279ea FCE |
1305 | |
1306 | bool function_entrypc (Dwarf_Addr * addr) | |
1307 | { | |
1308 | assert (function); | |
20e4a32c | 1309 | return (dwarf_entrypc (function, addr) == 0); |
b8da0ad1 | 1310 | // XXX: see also _lowpc ? |
7e1279ea FCE |
1311 | } |
1312 | ||
1313 | ||
1314 | bool die_entrypc (Dwarf_Die * die, Dwarf_Addr * addr) | |
1315 | { | |
5bc4ac10 FCE |
1316 | int rc = 0; |
1317 | string lookup_method; | |
7e1279ea | 1318 | |
5bc4ac10 FCE |
1319 | * addr = 0; |
1320 | ||
1321 | lookup_method = "dwarf_entrypc"; | |
1322 | rc = dwarf_entrypc (die, addr); | |
1323 | ||
4baf0e53 | 1324 | if (rc) |
5bc4ac10 FCE |
1325 | { |
1326 | lookup_method = "dwarf_lowpc"; | |
1327 | rc = dwarf_lowpc (die, addr); | |
1328 | } | |
1329 | ||
1330 | if (rc) | |
1331 | { | |
1332 | lookup_method = "dwarf_ranges"; | |
1333 | ||
1334 | Dwarf_Addr base; | |
1335 | Dwarf_Addr begin; | |
1336 | Dwarf_Addr end; | |
1337 | ptrdiff_t offset = dwarf_ranges (die, 0, &base, &begin, &end); | |
1338 | if (offset < 0) rc = -1; | |
1339 | else if (offset > 0) | |
1340 | { | |
1341 | * addr = begin; | |
1342 | rc = 0; | |
1343 | ||
1344 | // Now we need to check that there are no more ranges | |
1345 | // associated with this function, which could conceivably | |
1346 | // happen if a function is inlined, then pieces of it are | |
1347 | // split amongst different conditional branches. It's not | |
1348 | // obvious which of them to favour. As a heuristic, we | |
1349 | // pick the beginning of the first range, and ignore the | |
1350 | // others (but with a warning). | |
1351 | ||
1352 | unsigned extra = 0; | |
1353 | while ((offset = dwarf_ranges (die, offset, &base, &begin, &end)) > 0) | |
1354 | extra ++; | |
1355 | if (extra) | |
1356 | lookup_method += ", ignored " + lex_cast<string>(extra) + " more"; | |
1357 | } | |
1358 | } | |
4baf0e53 | 1359 | |
5bc4ac10 FCE |
1360 | if (sess.verbose > 2) |
1361 | clog << "entry-pc lookup (" << lookup_method << ") = 0x" << hex << *addr << dec | |
4baf0e53 | 1362 | << " (rc " << rc << ")" |
5bc4ac10 FCE |
1363 | << endl; |
1364 | return (rc == 0); | |
7e1279ea FCE |
1365 | } |
1366 | ||
4cd232e4 GH |
1367 | void function_die (Dwarf_Die *d) |
1368 | { | |
1369 | assert (function); | |
20e4a32c | 1370 | *d = *function; |
4cd232e4 | 1371 | } |
7e1279ea | 1372 | |
4cd232e4 | 1373 | void function_file (char const ** c) |
7e1279ea FCE |
1374 | { |
1375 | assert (function); | |
4cd232e4 | 1376 | assert (c); |
20e4a32c | 1377 | *c = dwarf_decl_file (function); |
7e1279ea FCE |
1378 | } |
1379 | ||
4cd232e4 | 1380 | void function_line (int *linep) |
7e1279ea FCE |
1381 | { |
1382 | assert (function); | |
20e4a32c | 1383 | dwarf_decl_line (function, linep); |
7e1279ea FCE |
1384 | } |
1385 | ||
86bf665e | 1386 | bool die_has_pc (Dwarf_Die & die, Dwarf_Addr pc) |
7e1279ea | 1387 | { |
86bf665e | 1388 | int res = dwarf_haspc (&die, pc); |
a688cff2 SC |
1389 | // dwarf_ranges will return -1 if a function die has no DW_AT_ranges |
1390 | // if (res == -1) | |
1391 | // dwarf_assert ("dwarf_haspc", res); | |
7e1279ea FCE |
1392 | return res == 1; |
1393 | } | |
1394 | ||
1395 | ||
78f6bba6 | 1396 | static void loc2c_error (void *, const char *fmt, ...) |
e36387d7 | 1397 | { |
78f6bba6 FCE |
1398 | const char *msg = "?"; |
1399 | char *tmp = NULL; | |
5ce20b7a | 1400 | int rc; |
e36387d7 RM |
1401 | va_list ap; |
1402 | va_start (ap, fmt); | |
78f6bba6 FCE |
1403 | rc = vasprintf (& tmp, fmt, ap); |
1404 | if (rc < 0) | |
1405 | msg = "?"; | |
1406 | else | |
1407 | msg = tmp; | |
e36387d7 RM |
1408 | va_end (ap); |
1409 | throw semantic_error (msg); | |
1410 | } | |
bd2b1e68 | 1411 | |
e664cf5b FCE |
1412 | // This function generates code used for addressing computations of |
1413 | // target variables. | |
e38d6504 RM |
1414 | void emit_address (struct obstack *pool, Dwarf_Addr address) |
1415 | { | |
e664cf5b FCE |
1416 | #if 0 |
1417 | // The easy but incorrect way is to just print a hard-wired | |
1418 | // constant. | |
e38d6504 | 1419 | obstack_printf (pool, "%#" PRIx64 "UL", address); |
e664cf5b | 1420 | #endif |
e38d6504 RM |
1421 | |
1422 | // Turn this address into a section-relative offset if it should be one. | |
1423 | // We emit a comment approximating the variable+offset expression that | |
1424 | // relocatable module probing code will need to have. | |
1425 | Dwfl_Module *mod = dwfl_addrmodule (dwfl, address); | |
86bf665e | 1426 | dwfl_assert ("dwfl_addrmodule", mod); |
432f054f FCE |
1427 | const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, |
1428 | NULL, NULL, NULL, NULL); | |
e38d6504 | 1429 | int n = dwfl_module_relocations (mod); |
ba53ea9f | 1430 | dwfl_assert ("dwfl_module_relocations", n >= 0); |
432f054f FCE |
1431 | Dwarf_Addr reloc_address = address; |
1432 | int i = dwfl_module_relocate_address (mod, &reloc_address); | |
ba53ea9f | 1433 | dwfl_assert ("dwfl_module_relocate_address", i >= 0); |
86bf665e | 1434 | dwfl_assert ("dwfl_module_info", modname); |
d64e82b1 SD |
1435 | const char *secname = dwfl_module_relocation_info (mod, i, NULL); |
1436 | ||
432f054f FCE |
1437 | if (sess.verbose > 2) |
1438 | { | |
1439 | clog << "emit dwarf addr 0x" << hex << address << dec | |
1440 | << " => module " << modname | |
1441 | << " section " << (secname ?: "null") | |
1442 | << " relocaddr 0x" << hex << reloc_address << dec | |
1443 | << endl; | |
1444 | } | |
1445 | ||
d64e82b1 SD |
1446 | if (n > 0 && !(n == 1 && secname == NULL)) |
1447 | { | |
ba53ea9f | 1448 | dwfl_assert ("dwfl_module_relocation_info", secname); |
e38d6504 | 1449 | if (n > 1 || secname[0] != '\0') |
7e41d3dc FCE |
1450 | { |
1451 | // This gives us the module name, and section name within the | |
1452 | // module, for a kernel module (or other ET_REL module object). | |
1453 | obstack_printf (pool, "({ static unsigned long addr = 0; "); | |
1454 | obstack_printf (pool, "if (addr==0) addr = _stp_module_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", | |
432f054f | 1455 | modname, secname, reloc_address); |
7e41d3dc FCE |
1456 | obstack_printf (pool, "addr; })"); |
1457 | } | |
4baf0e53 | 1458 | else if (n == 1 && module_name == TOK_KERNEL && secname[0] == '\0') |
21beacc9 FCE |
1459 | { |
1460 | // elfutils' way of telling us that this is a relocatable kernel address, which we | |
1461 | // need to treat the same way here as dwarf_query::add_probe_point does: _stext. | |
1462 | address -= sess.sym_stext; | |
1463 | secname = "_stext"; | |
1464 | obstack_printf (pool, "({ static unsigned long addr = 0; "); | |
1465 | obstack_printf (pool, "if (addr==0) addr = _stp_module_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", | |
432f054f | 1466 | modname, secname, address); // PR10000 NB: not reloc_address |
21beacc9 FCE |
1467 | obstack_printf (pool, "addr; })"); |
1468 | } | |
e38d6504 | 1469 | else |
e664cf5b FCE |
1470 | { |
1471 | throw semantic_error ("cannot relocate user-space dso (?) address"); | |
1472 | #if 0 | |
1473 | // This would happen for a Dwfl_Module that's a user-level DSO. | |
1474 | obstack_printf (pool, " /* %s+%#" PRIx64 " */", | |
1475 | modname, address); | |
1476 | #endif | |
1477 | } | |
e38d6504 | 1478 | } |
e664cf5b FCE |
1479 | else |
1480 | obstack_printf (pool, "%#" PRIx64 "UL", address); // assume as constant | |
e38d6504 | 1481 | } |
7e1279ea | 1482 | |
4b1ad75e RM |
1483 | static void loc2c_emit_address (void *arg, struct obstack *pool, |
1484 | Dwarf_Addr address) | |
1485 | { | |
1486 | dwflpp *dwfl = (dwflpp *) arg; | |
e38d6504 | 1487 | dwfl->emit_address (pool, address); |
4b1ad75e RM |
1488 | } |
1489 | ||
82e72903 DS |
1490 | void print_locals(Dwarf_Die *die, ostream &o) |
1491 | { | |
1492 | // Try to get the first child of die. | |
82e72903 DS |
1493 | Dwarf_Die child; |
1494 | if (dwarf_child (die, &child) == 0) | |
1495 | { | |
1496 | do | |
1497 | { | |
0e68eaaa | 1498 | const char *name; |
82e72903 DS |
1499 | // Output each sibling's name (that is a variable or |
1500 | // parameter) to 'o'. | |
1501 | switch (dwarf_tag (&child)) | |
1502 | { | |
1503 | case DW_TAG_variable: | |
1504 | case DW_TAG_formal_parameter: | |
0e68eaaa DS |
1505 | name = dwarf_diename (&child); |
1506 | if (name) | |
1507 | o << " " << name; | |
82e72903 DS |
1508 | break; |
1509 | default: | |
1510 | break; | |
1511 | } | |
1512 | } | |
1513 | while (dwarf_siblingof (&child, &child) == 0); | |
1514 | } | |
82e72903 DS |
1515 | } |
1516 | ||
e57b735a GH |
1517 | Dwarf_Attribute * |
1518 | find_variable_and_frame_base (Dwarf_Die *scope_die, | |
20e4a32c | 1519 | Dwarf_Addr pc, |
91eefb1c | 1520 | string const & local, |
b4c34c26 | 1521 | const target_symbol *e, |
e57b735a GH |
1522 | Dwarf_Die *vardie, |
1523 | Dwarf_Attribute *fb_attr_mem) | |
77de5e9e | 1524 | { |
77de5e9e | 1525 | Dwarf_Die *scopes; |
bcc12710 | 1526 | int nscopes = 0; |
e57b735a GH |
1527 | Dwarf_Attribute *fb_attr = NULL; |
1528 | ||
1529 | assert (cu); | |
bcc12710 | 1530 | |
f46d1b8a SC |
1531 | nscopes = dwarf_getscopes (cu, pc, &scopes); |
1532 | int sidx; | |
1533 | // if pc and scope_die are disjoint then we need dwarf_getscopes_die | |
1534 | for (sidx = 0; sidx < nscopes; sidx++) | |
1535 | if (scopes[sidx].addr == scope_die->addr) | |
1536 | break; | |
1537 | if (sidx == nscopes) | |
1538 | nscopes = dwarf_getscopes_die (scope_die, &scopes); | |
77de5e9e | 1539 | |
955925b7 | 1540 | if (nscopes <= 0) |
77de5e9e | 1541 | { |
7a053d3b | 1542 | throw semantic_error ("unable to find any scopes containing " |
59ff2773 | 1543 | + lex_cast_hex<string>(pc) |
1c7643a9 | 1544 | + ((scope_die == NULL) ? "" |
0e68eaaa DS |
1545 | : (string (" in ") |
1546 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
1547 | + "(" + (dwarf_diename(cu) ?: "<unknown>") | |
1548 | + ")")) | |
b4c34c26 FCE |
1549 | + " while searching for local '" + local + "'", |
1550 | e->tok); | |
77de5e9e | 1551 | } |
7a053d3b | 1552 | |
77de5e9e | 1553 | int declaring_scope = dwarf_getscopevar (scopes, nscopes, |
7a053d3b RM |
1554 | local.c_str(), |
1555 | 0, NULL, 0, 0, | |
e57b735a | 1556 | vardie); |
77de5e9e GH |
1557 | if (declaring_scope < 0) |
1558 | { | |
82e72903 DS |
1559 | stringstream alternatives; |
1560 | print_locals (scopes, alternatives); | |
77de5e9e | 1561 | throw semantic_error ("unable to find local '" + local + "'" |
82e72903 | 1562 | + " near pc " + lex_cast_hex<string>(pc) |
1c7643a9 | 1563 | + ((scope_die == NULL) ? "" |
0e68eaaa DS |
1564 | : (string (" in ") |
1565 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
1566 | + "(" + (dwarf_diename(cu) ?: "<unknown>") | |
1567 | + ")")) | |
b4c34c26 FCE |
1568 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")")), |
1569 | e->tok); | |
77de5e9e | 1570 | } |
7a053d3b | 1571 | |
77de5e9e GH |
1572 | for (int inner = 0; inner < nscopes; ++inner) |
1573 | { | |
1574 | switch (dwarf_tag (&scopes[inner])) | |
1575 | { | |
1576 | default: | |
1577 | continue; | |
1578 | case DW_TAG_subprogram: | |
1579 | case DW_TAG_entry_point: | |
1580 | case DW_TAG_inlined_subroutine: /* XXX */ | |
1581 | if (inner >= declaring_scope) | |
1582 | fb_attr = dwarf_attr_integrate (&scopes[inner], | |
1583 | DW_AT_frame_base, | |
e57b735a | 1584 | fb_attr_mem); |
77de5e9e GH |
1585 | break; |
1586 | } | |
1587 | } | |
e57b735a GH |
1588 | return fb_attr; |
1589 | } | |
77de5e9e | 1590 | |
77de5e9e | 1591 | |
d1531387 RM |
1592 | struct location * |
1593 | translate_location(struct obstack *pool, | |
1594 | Dwarf_Attribute *attr, Dwarf_Addr pc, | |
1595 | Dwarf_Attribute *fb_attr, | |
b4c34c26 FCE |
1596 | struct location **tail, |
1597 | const target_symbol *e) | |
d1531387 RM |
1598 | { |
1599 | Dwarf_Op *expr; | |
1600 | size_t len; | |
1601 | ||
67982217 FCE |
1602 | /* PR9768: formerly, we added pc+module_bias here. However, that bias value |
1603 | is not present in the pc value by the time we get it, so adding it would | |
1604 | result in false negatives of variable reachibility. In other instances | |
1605 | further below, the c_translate_FOO functions, the module_bias value used | |
1606 | to be passed in, but instead should now be zero for the same reason. */ | |
1607 | ||
1608 | switch (dwarf_getlocation_addr (attr, pc /*+ module_bias*/, &expr, &len, 1)) | |
d1531387 RM |
1609 | { |
1610 | case 1: /* Should always happen. */ | |
1611 | if (len > 0) | |
1612 | break; | |
1613 | /* Fall through. */ | |
1614 | ||
1615 | case 0: /* Shouldn't happen. */ | |
b4c34c26 | 1616 | throw semantic_error ("not accessible at this address", e->tok); |
d1531387 RM |
1617 | |
1618 | default: /* Shouldn't happen. */ | |
1619 | case -1: | |
1620 | throw semantic_error (string ("dwarf_getlocation_addr failed") + | |
b4c34c26 FCE |
1621 | string (dwarf_errmsg (-1)), |
1622 | e->tok); | |
d1531387 RM |
1623 | } |
1624 | ||
1625 | return c_translate_location (pool, &loc2c_error, this, | |
1626 | &loc2c_emit_address, | |
67982217 | 1627 | 1, 0 /* PR9768 */, |
d1531387 RM |
1628 | pc, expr, len, tail, fb_attr); |
1629 | } | |
1630 | ||
82e72903 DS |
1631 | void |
1632 | print_members(Dwarf_Die *vardie, ostream &o) | |
1633 | { | |
1634 | const int typetag = dwarf_tag (vardie); | |
1635 | ||
1636 | if (typetag != DW_TAG_structure_type && typetag != DW_TAG_union_type) | |
1637 | { | |
1638 | o << " Error: " | |
1639 | << (dwarf_diename_integrate (vardie) ?: "<anonymous>") | |
1640 | << " isn't a struct/union"; | |
1641 | return; | |
1642 | } | |
1643 | ||
1644 | // Try to get the first child of vardie. | |
1645 | Dwarf_Die die_mem; | |
1646 | Dwarf_Die *die = &die_mem; | |
1647 | switch (dwarf_child (vardie, die)) | |
1648 | { | |
1649 | case 1: // No children. | |
1650 | o << ((typetag == DW_TAG_union_type) ? " union " : " struct ") | |
1651 | << (dwarf_diename_integrate (die) ?: "<anonymous>") | |
1652 | << " is empty"; | |
1653 | break; | |
1654 | ||
1655 | case -1: // Error. | |
1656 | default: // Shouldn't happen. | |
1657 | o << ((typetag == DW_TAG_union_type) ? " union " : " struct ") | |
1658 | << (dwarf_diename_integrate (die) ?: "<anonymous>") | |
1659 | << ": " << dwarf_errmsg (-1); | |
1660 | break; | |
1661 | ||
1662 | case 0: // Success. | |
1663 | break; | |
1664 | } | |
1665 | ||
1666 | // Output each sibling's name to 'o'. | |
1667 | while (dwarf_tag (die) == DW_TAG_member) | |
1668 | { | |
4b3b2cc7 PS |
1669 | const char *member = dwarf_diename_integrate (die) ; |
1670 | ||
1671 | if ( member != NULL ) | |
4baf0e53 | 1672 | |
4b3b2cc7 PS |
1673 | o << " " << member; |
1674 | ||
1675 | else | |
1676 | { | |
1677 | Dwarf_Die temp_die = *die; | |
1678 | Dwarf_Attribute temp_attr ; | |
1679 | ||
1680 | if (!dwarf_attr_integrate (&temp_die, DW_AT_type, &temp_attr)) | |
1681 | { | |
1682 | clog<<"\n Error in obtaining type attribute for " | |
1683 | <<(dwarf_diename(&temp_die)?:"<anonymous>"); | |
1684 | return ; | |
1685 | } | |
1686 | ||
1687 | if ( ! dwarf_formref_die (&temp_attr,&temp_die)) | |
1688 | { | |
1689 | clog<<"\n Error in decoding type attribute for " | |
1690 | <<(dwarf_diename(&temp_die)?:"<anonymous>"); | |
1691 | return ; | |
1692 | } | |
1693 | print_members(&temp_die,o); | |
1694 | ||
1695 | } | |
82e72903 DS |
1696 | |
1697 | if (dwarf_siblingof (die, &die_mem) != 0) | |
1698 | break; | |
1699 | } | |
1700 | } | |
1701 | ||
e57b735a GH |
1702 | Dwarf_Die * |
1703 | translate_components(struct obstack *pool, | |
20e4a32c RM |
1704 | struct location **tail, |
1705 | Dwarf_Addr pc, | |
b4c34c26 | 1706 | const target_symbol *e, |
e57b735a GH |
1707 | Dwarf_Die *vardie, |
1708 | Dwarf_Die *die_mem, | |
1709 | Dwarf_Attribute *attr_mem) | |
1710 | { | |
b9c2e81c | 1711 | Dwarf_Die *die = NULL; |
82e72903 | 1712 | Dwarf_Die struct_die; |
4b3b2cc7 PS |
1713 | Dwarf_Attribute temp_attr; |
1714 | ||
d9b516ca | 1715 | unsigned i = 0; |
4b3b2cc7 | 1716 | |
c4ce66a1 JS |
1717 | if (vardie) |
1718 | *die_mem = *vardie; | |
1719 | ||
b9c2e81c JS |
1720 | if (e->components.empty()) |
1721 | return die_mem; | |
1722 | ||
4b3b2cc7 PS |
1723 | static unsigned int func_call_level ; |
1724 | static unsigned int dwarf_error_flag ; // indicates current error is dwarf error | |
1725 | static unsigned int dwarf_error_count ; // keeps track of no of dwarf errors | |
1726 | static semantic_error saved_dwarf_error(""); | |
1727 | ||
b4c34c26 | 1728 | while (i < e->components.size()) |
d9b516ca | 1729 | { |
0301cfe7 FCE |
1730 | /* XXX: This would be desirable, but we don't get the target_symbol token, |
1731 | and printing that gives us the file:line number too early anyway. */ | |
1732 | #if 0 | |
1733 | // Emit a marker to note which field is being access-attempted, to give | |
1734 | // better error messages if deref() fails. | |
1735 | string piece = string(...target_symbol token...) + string ("#") + stringify(components[i].second); | |
1736 | obstack_printf (pool, "c->last_stmt = %s;", lex_cast_qstring(piece).c_str()); | |
1737 | #endif | |
1738 | ||
b9c2e81c | 1739 | die = die ? dwarf_formref_die (attr_mem, die_mem) : die_mem; |
d9b516ca RM |
1740 | const int typetag = dwarf_tag (die); |
1741 | switch (typetag) | |
1742 | { | |
1743 | case DW_TAG_typedef: | |
fdfbe4f7 GH |
1744 | case DW_TAG_const_type: |
1745 | case DW_TAG_volatile_type: | |
d9b516ca RM |
1746 | /* Just iterate on the referent type. */ |
1747 | break; | |
91eefb1c | 1748 | |
d9b516ca | 1749 | case DW_TAG_pointer_type: |
b4c34c26 FCE |
1750 | if (e->components[i].first == target_symbol::comp_literal_array_index) |
1751 | throw semantic_error ("cannot index pointer", e->tok); | |
2302c47e | 1752 | // XXX: of course, we should support this the same way C does, |
b0be9bdb | 1753 | // by explicit pointer arithmetic etc. PR4166. |
91eefb1c | 1754 | |
67982217 | 1755 | c_translate_pointer (pool, 1, 0 /* PR9768*/, die, tail); |
d9b516ca | 1756 | break; |
91eefb1c | 1757 | |
d9b516ca | 1758 | case DW_TAG_array_type: |
b4c34c26 | 1759 | if (e->components[i].first == target_symbol::comp_literal_array_index) |
d9b516ca | 1760 | { |
67982217 | 1761 | c_translate_array (pool, 1, 0 /* PR9768 */, die, tail, |
b4c34c26 | 1762 | NULL, lex_cast<Dwarf_Word>(e->components[i].second)); |
d9b516ca RM |
1763 | ++i; |
1764 | } | |
1765 | else | |
1766 | throw semantic_error("bad field '" | |
b4c34c26 FCE |
1767 | + e->components[i].second |
1768 | + "' for array type", | |
1769 | e->tok); | |
d9b516ca | 1770 | break; |
91eefb1c | 1771 | |
d9b516ca RM |
1772 | case DW_TAG_structure_type: |
1773 | case DW_TAG_union_type: | |
82e72903 | 1774 | struct_die = *die; |
0b8f6579 JB |
1775 | if (dwarf_hasattr(die, DW_AT_declaration)) |
1776 | { | |
c4ce66a1 | 1777 | Dwarf_Die *tmpdie = dwflpp::declaration_resolve(dwarf_diename(die)); |
0b8f6579 JB |
1778 | if (tmpdie == NULL) |
1779 | throw semantic_error ("unresolved struct " | |
b4c34c26 FCE |
1780 | + string (dwarf_diename_integrate (die) ?: "<anonymous>"), |
1781 | e->tok); | |
0b8f6579 JB |
1782 | *die_mem = *tmpdie; |
1783 | } | |
e57b735a | 1784 | switch (dwarf_child (die, die_mem)) |
d9b516ca RM |
1785 | { |
1786 | case 1: /* No children. */ | |
4b3b2cc7 | 1787 | return NULL; |
d9b516ca RM |
1788 | case -1: /* Error. */ |
1789 | default: /* Shouldn't happen */ | |
1790 | throw semantic_error (string (typetag == DW_TAG_union_type ? "union" : "struct") | |
1791 | + string (dwarf_diename_integrate (die) ?: "<anonymous>") | |
b4c34c26 FCE |
1792 | + string (dwarf_errmsg (-1)), |
1793 | e->tok); | |
d9b516ca RM |
1794 | break; |
1795 | ||
1796 | case 0: | |
1797 | break; | |
1798 | } | |
1799 | ||
1800 | while (dwarf_tag (die) != DW_TAG_member | |
1801 | || ({ const char *member = dwarf_diename_integrate (die); | |
b4c34c26 | 1802 | member == NULL || string(member) != e->components[i].second; })) |
4b3b2cc7 PS |
1803 | { |
1804 | if ( dwarf_diename (die) == NULL ) // handling Anonymous structs/unions | |
1805 | { | |
1806 | Dwarf_Die temp_die = *die; | |
1807 | Dwarf_Die temp_die_2; | |
1808 | ||
1809 | try | |
1810 | { | |
1811 | if (!dwarf_attr_integrate (&temp_die, DW_AT_type, &temp_attr)) | |
1812 | { | |
1813 | dwarf_error_flag ++ ; | |
1814 | dwarf_error_count ++; | |
b4c34c26 | 1815 | throw semantic_error(" Error in obtaining type attribute for "+ string(dwarf_diename(&temp_die)?:"<anonymous>"), e->tok); |
4b3b2cc7 PS |
1816 | } |
1817 | ||
1818 | if ( !dwarf_formref_die (&temp_attr, &temp_die)) | |
1819 | { | |
1820 | dwarf_error_flag ++ ; | |
1821 | dwarf_error_count ++; | |
b4c34c26 | 1822 | throw semantic_error(" Error in decoding DW_AT_type attribute for " + string(dwarf_diename(&temp_die)?:"<anonymous>"), e->tok); |
4b3b2cc7 PS |
1823 | } |
1824 | ||
1825 | func_call_level ++ ; | |
1826 | ||
b4c34c26 | 1827 | Dwarf_Die *result_die = translate_components(pool, tail, pc, e, &temp_die, &temp_die_2, &temp_attr); |
4b3b2cc7 PS |
1828 | |
1829 | func_call_level -- ; | |
1830 | ||
1831 | if (result_die != NULL) | |
1832 | { | |
1833 | memcpy(die_mem, &temp_die_2, sizeof(Dwarf_Die)); | |
1834 | memcpy(attr_mem, &temp_attr, sizeof(Dwarf_Attribute)); | |
1835 | return die_mem; | |
1836 | } | |
1837 | } | |
1838 | catch (const semantic_error& e) | |
1839 | { | |
1840 | if ( !dwarf_error_flag ) //not a dwarf error | |
1841 | throw; | |
1842 | else | |
1843 | { | |
1844 | dwarf_error_flag = 0 ; | |
1845 | saved_dwarf_error = e ; | |
1846 | } | |
1847 | } | |
1848 | } | |
e57b735a | 1849 | if (dwarf_siblingof (die, die_mem) != 0) |
4b3b2cc7 PS |
1850 | { |
1851 | if ( func_call_level == 0 && dwarf_error_count ) // this is parent call & a dwarf error has been reported in a branch somewhere | |
1852 | throw semantic_error( saved_dwarf_error ); | |
1853 | else | |
1854 | return NULL; | |
1855 | } | |
1856 | } | |
d9b516ca RM |
1857 | |
1858 | if (dwarf_attr_integrate (die, DW_AT_data_member_location, | |
e57b735a | 1859 | attr_mem) == NULL) |
d9b516ca RM |
1860 | { |
1861 | /* Union members don't usually have a location, | |
1862 | but just use the containing union's location. */ | |
1863 | if (typetag != DW_TAG_union_type) | |
9f36b77f | 1864 | throw semantic_error ("no location for field '" |
b4c34c26 FCE |
1865 | + e->components[i].second |
1866 | + "' :" + string(dwarf_errmsg (-1)), | |
1867 | e->tok); | |
d9b516ca RM |
1868 | } |
1869 | else | |
b4c34c26 | 1870 | translate_location (pool, attr_mem, pc, NULL, tail, e); |
d9b516ca RM |
1871 | ++i; |
1872 | break; | |
1873 | ||
27805741 JS |
1874 | case DW_TAG_enumeration_type: |
1875 | throw semantic_error ("field '" | |
1876 | + e->components[i].second | |
1877 | + "' vs. enum type " | |
1878 | + string(dwarf_diename_integrate (die) ?: "<anonymous type>"), | |
1879 | e->tok); | |
1880 | break; | |
d9b516ca | 1881 | case DW_TAG_base_type: |
9f36b77f | 1882 | throw semantic_error ("field '" |
b4c34c26 | 1883 | + e->components[i].second |
9f36b77f | 1884 | + "' vs. base type " |
b4c34c26 FCE |
1885 | + string(dwarf_diename_integrate (die) ?: "<anonymous type>"), |
1886 | e->tok); | |
d9b516ca RM |
1887 | break; |
1888 | case -1: | |
b4c34c26 FCE |
1889 | throw semantic_error ("cannot find type: " + string(dwarf_errmsg (-1)), |
1890 | e->tok); | |
d9b516ca RM |
1891 | break; |
1892 | ||
1893 | default: | |
1894 | throw semantic_error (string(dwarf_diename_integrate (die) ?: "<anonymous type>") | |
1895 | + ": unexpected type tag " | |
b4c34c26 FCE |
1896 | + lex_cast<string>(dwarf_tag (die)), |
1897 | e->tok); | |
d9b516ca RM |
1898 | break; |
1899 | } | |
1900 | ||
1901 | /* Now iterate on the type in DIE's attribute. */ | |
e57b735a | 1902 | if (dwarf_attr_integrate (die, DW_AT_type, attr_mem) == NULL) |
b4c34c26 | 1903 | throw semantic_error ("cannot get type of field: " + string(dwarf_errmsg (-1)), e->tok); |
d9b516ca | 1904 | } |
e57b735a GH |
1905 | return die; |
1906 | } | |
91eefb1c | 1907 | |
d9b516ca | 1908 | |
e57b735a GH |
1909 | Dwarf_Die * |
1910 | resolve_unqualified_inner_typedie (Dwarf_Die *typedie_mem, | |
b4c34c26 FCE |
1911 | Dwarf_Attribute *attr_mem, |
1912 | const target_symbol *e) | |
e57b735a | 1913 | { |
d9b516ca | 1914 | Dwarf_Die *typedie; |
e57b735a | 1915 | int typetag = 0; |
d9b516ca | 1916 | while (1) |
20e4a32c | 1917 | { |
e57b735a | 1918 | typedie = dwarf_formref_die (attr_mem, typedie_mem); |
d9b516ca | 1919 | if (typedie == NULL) |
b4c34c26 | 1920 | throw semantic_error ("cannot get type: " + string(dwarf_errmsg (-1)), e->tok); |
d9b516ca | 1921 | typetag = dwarf_tag (typedie); |
20e4a32c | 1922 | if (typetag != DW_TAG_typedef && |
fdfbe4f7 GH |
1923 | typetag != DW_TAG_const_type && |
1924 | typetag != DW_TAG_volatile_type) | |
91eefb1c | 1925 | break; |
e57b735a | 1926 | if (dwarf_attr_integrate (typedie, DW_AT_type, attr_mem) == NULL) |
b4c34c26 | 1927 | throw semantic_error ("cannot get type of pointee: " + string(dwarf_errmsg (-1)), e->tok); |
d9b516ca | 1928 | } |
e57b735a GH |
1929 | return typedie; |
1930 | } | |
91eefb1c | 1931 | |
91eefb1c | 1932 | |
20e4a32c | 1933 | void |
e57b735a GH |
1934 | translate_final_fetch_or_store (struct obstack *pool, |
1935 | struct location **tail, | |
1936 | Dwarf_Addr module_bias, | |
1937 | Dwarf_Die *die, | |
1938 | Dwarf_Attribute *attr_mem, | |
1939 | bool lvalue, | |
b4c34c26 | 1940 | const target_symbol *e, |
b8da0ad1 FCE |
1941 | string &, |
1942 | string &, | |
e57b735a GH |
1943 | exp_type & ty) |
1944 | { | |
1945 | /* First boil away any qualifiers associated with the type DIE of | |
1946 | the final location to be accessed. */ | |
fdfbe4f7 | 1947 | |
e57b735a GH |
1948 | Dwarf_Die typedie_mem; |
1949 | Dwarf_Die *typedie; | |
1950 | int typetag; | |
022b623f DS |
1951 | char const *dname; |
1952 | string diestr; | |
e57b735a | 1953 | |
b4c34c26 | 1954 | typedie = resolve_unqualified_inner_typedie (&typedie_mem, attr_mem, e); |
e57b735a GH |
1955 | typetag = dwarf_tag (typedie); |
1956 | ||
1957 | /* Then switch behavior depending on the type of fetch/store we | |
1958 | want, and the type and pointer-ness of the final location. */ | |
20e4a32c | 1959 | |
fdfbe4f7 GH |
1960 | switch (typetag) |
1961 | { | |
fdfbe4f7 | 1962 | default: |
022b623f DS |
1963 | dname = dwarf_diename(die); |
1964 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
66d284f4 | 1965 | throw semantic_error ("unsupported type tag " |
022b623f | 1966 | + lex_cast<string>(typetag) |
b4c34c26 | 1967 | + " for " + diestr, e->tok); |
022b623f DS |
1968 | break; |
1969 | ||
1970 | case DW_TAG_structure_type: | |
1971 | case DW_TAG_union_type: | |
1972 | dname = dwarf_diename(die); | |
1973 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
1974 | throw semantic_error ("struct/union '" + diestr | |
b4c34c26 | 1975 | + "' is being accessed instead of a member of the struct/union", e->tok); |
fdfbe4f7 | 1976 | break; |
66d284f4 | 1977 | |
e7a012f0 | 1978 | case DW_TAG_enumeration_type: |
fdfbe4f7 | 1979 | case DW_TAG_base_type: |
00cf3709 FCE |
1980 | |
1981 | // Reject types we can't handle in systemtap | |
1982 | { | |
1983 | dname = dwarf_diename(die); | |
1984 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
1985 | ||
1986 | Dwarf_Attribute encoding_attr; | |
6503a1cc | 1987 | Dwarf_Word encoding = (Dwarf_Word) -1; |
00cf3709 FCE |
1988 | dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding, &encoding_attr), |
1989 | & encoding); | |
1990 | if (encoding < 0) | |
1991 | { | |
1992 | // clog << "bad type1 " << encoding << " diestr" << endl; | |
1993 | throw semantic_error ("unsupported type (mystery encoding " + lex_cast<string>(encoding) + ")" + | |
b4c34c26 | 1994 | " for " + diestr, e->tok); |
00cf3709 FCE |
1995 | } |
1996 | ||
1997 | if (encoding == DW_ATE_float | |
1998 | || encoding == DW_ATE_complex_float | |
1999 | /* XXX || many others? */) | |
2000 | { | |
2001 | // clog << "bad type " << encoding << " diestr" << endl; | |
2002 | throw semantic_error ("unsupported type (encoding " + lex_cast<string>(encoding) + ")" + | |
b4c34c26 | 2003 | " for " + diestr, e->tok); |
00cf3709 FCE |
2004 | } |
2005 | } | |
2006 | ||
fdfbe4f7 | 2007 | ty = pe_long; |
e57b735a | 2008 | if (lvalue) |
67982217 | 2009 | c_translate_store (pool, 1, 0 /* PR9768 */, die, typedie, tail, |
e57b735a | 2010 | "THIS->value"); |
20e4a32c | 2011 | else |
67982217 | 2012 | c_translate_fetch (pool, 1, 0 /* PR9768 */, die, typedie, tail, |
e57b735a | 2013 | "THIS->__retvalue"); |
fdfbe4f7 GH |
2014 | break; |
2015 | ||
2016 | case DW_TAG_array_type: | |
2017 | case DW_TAG_pointer_type: | |
e57b735a | 2018 | |
b0be9bdb | 2019 | { |
fdfbe4f7 GH |
2020 | Dwarf_Die pointee_typedie_mem; |
2021 | Dwarf_Die *pointee_typedie; | |
2022 | Dwarf_Word pointee_encoding; | |
246b383e | 2023 | Dwarf_Word pointee_byte_size = 0; |
fdfbe4f7 | 2024 | |
b4c34c26 | 2025 | pointee_typedie = resolve_unqualified_inner_typedie (&pointee_typedie_mem, attr_mem, e); |
e57b735a GH |
2026 | |
2027 | if (dwarf_attr_integrate (pointee_typedie, DW_AT_byte_size, attr_mem)) | |
2028 | dwarf_formudata (attr_mem, &pointee_byte_size); | |
20e4a32c RM |
2029 | |
2030 | dwarf_formudata (dwarf_attr_integrate (pointee_typedie, DW_AT_encoding, attr_mem), | |
fdfbe4f7 GH |
2031 | &pointee_encoding); |
2032 | ||
b0be9bdb FCE |
2033 | if (lvalue) |
2034 | { | |
2035 | ty = pe_long; | |
2036 | if (typetag == DW_TAG_array_type) | |
b4c34c26 | 2037 | throw semantic_error ("cannot write to array address", e->tok); |
b0be9bdb | 2038 | assert (typetag == DW_TAG_pointer_type); |
67982217 | 2039 | c_translate_pointer_store (pool, 1, 0 /* PR9768 */, typedie, tail, |
b0be9bdb FCE |
2040 | "THIS->value"); |
2041 | } | |
2042 | else | |
2043 | { | |
2044 | // We have the pointer: cast it to an integral type via &(*(...)) | |
41c262f3 | 2045 | |
b0be9bdb FCE |
2046 | // NB: per bug #1187, at one point char*-like types were |
2047 | // automagically converted here to systemtap string values. | |
2048 | // For several reasons, this was taken back out, leaving | |
2049 | // pointer-to-string "conversion" (copying) to tapset functions. | |
41c262f3 | 2050 | |
b0be9bdb FCE |
2051 | ty = pe_long; |
2052 | if (typetag == DW_TAG_array_type) | |
67982217 | 2053 | c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail, NULL, 0); |
b0be9bdb | 2054 | else |
67982217 FCE |
2055 | c_translate_pointer (pool, 1, 0 /* PR9768 */, typedie, tail); |
2056 | c_translate_addressof (pool, 1, 0 /* PR9768 */, NULL, pointee_typedie, tail, | |
b0be9bdb FCE |
2057 | "THIS->__retvalue"); |
2058 | } | |
2059 | } | |
20e4a32c | 2060 | break; |
fdfbe4f7 | 2061 | } |
20e4a32c | 2062 | } |
e57b735a | 2063 | |
e19fda4e DS |
2064 | string |
2065 | express_as_string (string prelude, | |
2066 | string postlude, | |
2067 | struct location *head) | |
2068 | { | |
2069 | size_t bufsz = 1024; | |
2070 | char *buf = static_cast<char*>(malloc(bufsz)); | |
2071 | assert(buf); | |
2072 | ||
2073 | FILE *memstream = open_memstream (&buf, &bufsz); | |
2074 | assert(memstream); | |
2075 | ||
2076 | fprintf(memstream, "{\n"); | |
bec508de | 2077 | fprintf(memstream, "%s", prelude.c_str()); |
e19fda4e | 2078 | bool deref = c_emit_location (memstream, head, 1); |
bec508de | 2079 | fprintf(memstream, "%s", postlude.c_str()); |
e19fda4e DS |
2080 | fprintf(memstream, " goto out;\n"); |
2081 | ||
2082 | // dummy use of deref_fault label, to disable warning if deref() not used | |
2083 | fprintf(memstream, "if (0) goto deref_fault;\n"); | |
2084 | ||
2085 | // XXX: deref flag not reliable; emit fault label unconditionally | |
78f6bba6 | 2086 | (void) deref; |
e19fda4e DS |
2087 | fprintf(memstream, |
2088 | "deref_fault:\n" | |
e19fda4e DS |
2089 | " goto out;\n"); |
2090 | fprintf(memstream, "}\n"); | |
2091 | ||
2092 | fclose (memstream); | |
2093 | string result(buf); | |
2094 | free (buf); | |
2095 | return result; | |
2096 | } | |
e57b735a | 2097 | |
20e4a32c | 2098 | string |
e57b735a | 2099 | literal_stmt_for_local (Dwarf_Die *scope_die, |
20e4a32c | 2100 | Dwarf_Addr pc, |
e57b735a | 2101 | string const & local, |
b4c34c26 | 2102 | const target_symbol *e, |
e57b735a GH |
2103 | bool lvalue, |
2104 | exp_type & ty) | |
2105 | { | |
2106 | Dwarf_Die vardie; | |
2107 | Dwarf_Attribute fb_attr_mem, *fb_attr = NULL; | |
2108 | ||
b4c34c26 | 2109 | fb_attr = find_variable_and_frame_base (scope_die, pc, local, e, |
e57b735a GH |
2110 | &vardie, &fb_attr_mem); |
2111 | ||
b0ee93c4 | 2112 | if (sess.verbose>2) |
e57b735a | 2113 | clog << "finding location for local '" << local |
12b44fb3 FCE |
2114 | << "' near address 0x" << hex << pc |
2115 | << ", module bias 0x" << module_bias << dec | |
db22e55f | 2116 | << "\n"; |
e57b735a GH |
2117 | |
2118 | Dwarf_Attribute attr_mem; | |
2119 | if (dwarf_attr_integrate (&vardie, DW_AT_location, &attr_mem) == NULL) | |
2120 | { | |
2121 | throw semantic_error("failed to retrieve location " | |
20e4a32c RM |
2122 | "attribute for local '" + local |
2123 | + "' (dieoffset: " | |
2124 | + lex_cast_hex<string>(dwarf_dieoffset (&vardie)) | |
b4c34c26 FCE |
2125 | + ")", |
2126 | e->tok); | |
e57b735a GH |
2127 | } |
2128 | ||
2129 | #define obstack_chunk_alloc malloc | |
2130 | #define obstack_chunk_free free | |
2131 | ||
2132 | struct obstack pool; | |
2133 | obstack_init (&pool); | |
2134 | struct location *tail = NULL; | |
2135 | ||
2136 | /* Given $foo->bar->baz[NN], translate the location of foo. */ | |
2137 | ||
d1531387 | 2138 | struct location *head = translate_location (&pool, |
b4c34c26 FCE |
2139 | &attr_mem, pc, fb_attr, &tail, |
2140 | e); | |
e57b735a GH |
2141 | |
2142 | if (dwarf_attr_integrate (&vardie, DW_AT_type, &attr_mem) == NULL) | |
2143 | throw semantic_error("failed to retrieve type " | |
b4c34c26 FCE |
2144 | "attribute for local '" + local + "'", |
2145 | e->tok); | |
e57b735a | 2146 | |
e57b735a GH |
2147 | /* Translate the ->bar->baz[NN] parts. */ |
2148 | ||
675e4d2a | 2149 | Dwarf_Die die_mem, *die = dwarf_formref_die (&attr_mem, &die_mem); |
b4c34c26 | 2150 | die = translate_components (&pool, &tail, pc, e, |
c4ce66a1 | 2151 | die, &die_mem, &attr_mem); |
4b3b2cc7 PS |
2152 | if(!die) |
2153 | { | |
675e4d2a | 2154 | die = dwarf_formref_die (&attr_mem, &die_mem); |
4b3b2cc7 | 2155 | stringstream alternatives; |
a3de5d6e MW |
2156 | if (die != NULL) |
2157 | print_members(die,alternatives); | |
b487a14d FCE |
2158 | throw semantic_error("unable to find local '" + local + "'" |
2159 | + " near pc " + lex_cast_hex<string>(pc) | |
b4c34c26 FCE |
2160 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")")), |
2161 | e->tok); | |
4b3b2cc7 | 2162 | } |
e57b735a | 2163 | |
20e4a32c RM |
2164 | /* Translate the assignment part, either |
2165 | x = $foo->bar->baz[NN] | |
2166 | or | |
e57b735a GH |
2167 | $foo->bar->baz[NN] = x |
2168 | */ | |
2169 | ||
2170 | string prelude, postlude; | |
20e4a32c | 2171 | translate_final_fetch_or_store (&pool, &tail, module_bias, |
b4c34c26 | 2172 | die, &attr_mem, lvalue, e, |
e57b735a GH |
2173 | prelude, postlude, ty); |
2174 | ||
2175 | /* Write the translation to a string. */ | |
e19fda4e DS |
2176 | return express_as_string(prelude, postlude, head); |
2177 | } | |
20e4a32c | 2178 | |
20e4a32c | 2179 | |
e19fda4e DS |
2180 | string |
2181 | literal_stmt_for_return (Dwarf_Die *scope_die, | |
2182 | Dwarf_Addr pc, | |
b4c34c26 | 2183 | const target_symbol *e, |
e19fda4e DS |
2184 | bool lvalue, |
2185 | exp_type & ty) | |
2186 | { | |
2187 | if (sess.verbose>2) | |
2188 | clog << "literal_stmt_for_return: finding return value for " | |
0e68eaaa | 2189 | << (dwarf_diename(scope_die) ?: "<unknown>") |
e19fda4e | 2190 | << "(" |
0e68eaaa | 2191 | << (dwarf_diename(cu) ?: "<unknown>") |
e19fda4e | 2192 | << ")\n"; |
7a053d3b | 2193 | |
e19fda4e DS |
2194 | struct obstack pool; |
2195 | obstack_init (&pool); | |
2196 | struct location *tail = NULL; | |
7a053d3b | 2197 | |
e19fda4e DS |
2198 | /* Given $return->bar->baz[NN], translate the location of return. */ |
2199 | const Dwarf_Op *locops; | |
2200 | int nlocops = dwfl_module_return_value_location (module, scope_die, | |
2201 | &locops); | |
2202 | if (nlocops < 0) | |
2203 | { | |
194c6687 | 2204 | throw semantic_error("failed to retrieve return value location" |
0e68eaaa DS |
2205 | " for " |
2206 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2207 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
b4c34c26 FCE |
2208 | + ")", |
2209 | e->tok); | |
e19fda4e DS |
2210 | } |
2211 | // the function has no return value (e.g. "void" in C) | |
2212 | else if (nlocops == 0) | |
2213 | { | |
0e68eaaa DS |
2214 | throw semantic_error("function " |
2215 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2216 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
b4c34c26 FCE |
2217 | + ") has no return value", |
2218 | e->tok); | |
e19fda4e | 2219 | } |
a781f401 | 2220 | |
e19fda4e DS |
2221 | struct location *head = c_translate_location (&pool, &loc2c_error, this, |
2222 | &loc2c_emit_address, | |
67982217 | 2223 | 1, 0 /* PR9768 */, |
e19fda4e DS |
2224 | pc, locops, nlocops, |
2225 | &tail, NULL); | |
7a053d3b | 2226 | |
e19fda4e | 2227 | /* Translate the ->bar->baz[NN] parts. */ |
7a053d3b | 2228 | |
e19fda4e | 2229 | Dwarf_Attribute attr_mem; |
675e4d2a JS |
2230 | if (dwarf_attr_integrate (scope_die, DW_AT_type, &attr_mem) == NULL) |
2231 | throw semantic_error("failed to retrieve return value type attribute for " | |
2232 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2233 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
2234 | + ")", | |
2235 | e->tok); | |
e19fda4e | 2236 | |
675e4d2a | 2237 | Dwarf_Die die_mem, *die = dwarf_formref_die (&attr_mem, &die_mem); |
b4c34c26 | 2238 | die = translate_components (&pool, &tail, pc, e, |
675e4d2a | 2239 | die, &die_mem, &attr_mem); |
4b3b2cc7 PS |
2240 | if(!die) |
2241 | { | |
675e4d2a | 2242 | die = dwarf_formref_die (&attr_mem, &die_mem); |
4b3b2cc7 | 2243 | stringstream alternatives; |
a3de5d6e MW |
2244 | if (die != NULL) |
2245 | print_members(die,alternatives); | |
b487a14d FCE |
2246 | throw semantic_error("unable to find return value" |
2247 | " near pc " + lex_cast_hex<string>(pc) | |
0e68eaaa DS |
2248 | + " for " |
2249 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2250 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
2251 | + ")" | |
b4c34c26 FCE |
2252 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")")), |
2253 | e->tok); | |
4b3b2cc7 PS |
2254 | } |
2255 | ||
e19fda4e DS |
2256 | |
2257 | /* Translate the assignment part, either | |
2258 | x = $return->bar->baz[NN] | |
2259 | or | |
2260 | $return->bar->baz[NN] = x | |
2261 | */ | |
2262 | ||
2263 | string prelude, postlude; | |
2264 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
b4c34c26 | 2265 | die, &attr_mem, lvalue, e, |
e19fda4e DS |
2266 | prelude, postlude, ty); |
2267 | ||
2268 | /* Write the translation to a string. */ | |
2269 | return express_as_string(prelude, postlude, head); | |
2270 | } | |
7a053d3b | 2271 | |
77de5e9e | 2272 | |
c4ce66a1 JS |
2273 | string |
2274 | literal_stmt_for_pointer (Dwarf_Die *type_die, | |
b4c34c26 | 2275 | const target_symbol *e, |
c4ce66a1 JS |
2276 | bool lvalue, |
2277 | exp_type & ty) | |
2278 | { | |
2279 | if (sess.verbose>2) | |
2280 | clog << "literal_stmt_for_pointer: finding value for " | |
2281 | << (dwarf_diename(type_die) ?: "<unknown>") | |
2282 | << "(" | |
2283 | << (dwarf_diename(cu) ?: "<unknown>") | |
2284 | << ")\n"; | |
2285 | ||
2286 | struct obstack pool; | |
2287 | obstack_init (&pool); | |
2288 | struct location *head = c_translate_argument (&pool, &loc2c_error, this, | |
2289 | &loc2c_emit_address, | |
2290 | 1, "THIS->pointer"); | |
2291 | struct location *tail = head; | |
2292 | ||
2293 | /* Translate the ->bar->baz[NN] parts. */ | |
2294 | ||
2295 | Dwarf_Attribute attr_mem; | |
2296 | Dwarf_Die die_mem, *die = NULL; | |
b4c34c26 | 2297 | die = translate_components (&pool, &tail, 0, e, |
c4ce66a1 JS |
2298 | type_die, &die_mem, &attr_mem); |
2299 | if(!die) | |
2300 | { | |
2301 | die = dwarf_formref_die (&attr_mem, &die_mem); | |
2302 | stringstream alternatives; | |
2303 | print_members(die ?: type_die, alternatives); | |
2304 | throw semantic_error("unable to find member for struct " | |
2305 | + string(dwarf_diename(die ?: type_die) ?: "<unknown>") | |
b4c34c26 FCE |
2306 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")")), |
2307 | e->tok); | |
c4ce66a1 JS |
2308 | } |
2309 | ||
2310 | ||
2311 | /* Translate the assignment part, either | |
2312 | x = (THIS->pointer)->bar->baz[NN] | |
2313 | or | |
2314 | (THIS->pointer)->bar->baz[NN] = x | |
2315 | */ | |
2316 | ||
2317 | string prelude, postlude; | |
2318 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
b4c34c26 | 2319 | die, &attr_mem, lvalue, e, |
c4ce66a1 JS |
2320 | prelude, postlude, ty); |
2321 | ||
2322 | /* Write the translation to a string. */ | |
2323 | return express_as_string(prelude, postlude, head); | |
2324 | } | |
2325 | ||
2326 | ||
bd2b1e68 GH |
2327 | ~dwflpp() |
2328 | { | |
2329 | if (dwfl) | |
2330 | dwfl_end(dwfl); | |
2331 | } | |
2332 | }; | |
2333 | ||
405b71b8 | 2334 | |
20c6c071 | 2335 | |
7a053d3b | 2336 | enum |
bd2b1e68 | 2337 | function_spec_type |
7a053d3b | 2338 | { |
bd2b1e68 GH |
2339 | function_alone, |
2340 | function_and_file, | |
7a053d3b | 2341 | function_file_and_line |
bd2b1e68 GH |
2342 | }; |
2343 | ||
ec4373ff | 2344 | |
bd2b1e68 | 2345 | struct dwarf_builder; |
77de5e9e | 2346 | |
2930abc7 | 2347 | |
b20febf3 FCE |
2348 | // XXX: This class is a candidate for subclassing to separate |
2349 | // the relocation vs non-relocation variants. Likewise for | |
2350 | // kprobe vs kretprobe variants. | |
2351 | ||
2352 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 2353 | { |
b20febf3 FCE |
2354 | dwarf_derived_probe (const string& function, |
2355 | const string& filename, | |
2356 | int line, | |
2357 | const string& module, | |
2358 | const string& section, | |
2359 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 2360 | Dwarf_Addr addr, |
b20febf3 FCE |
2361 | dwarf_query & q, |
2362 | Dwarf_Die* scope_die); | |
20e4a32c | 2363 | |
b20febf3 FCE |
2364 | string module; |
2365 | string section; | |
2366 | Dwarf_Addr addr; | |
2930abc7 | 2367 | bool has_return; |
c9bad430 DS |
2368 | bool has_maxactive; |
2369 | long maxactive_val; | |
b95e2b79 | 2370 | bool access_vars; |
2930abc7 | 2371 | |
b8da0ad1 | 2372 | void printsig (std::ostream &o) const; |
b20febf3 | 2373 | void join_group (systemtap_session& s); |
9020300d FCE |
2374 | void emit_probe_local_init(translator_output * o); |
2375 | ||
bd2b1e68 | 2376 | // Pattern registration helpers. |
7a053d3b | 2377 | static void register_statement_variants(match_node * root, |
bd2b1e68 | 2378 | dwarf_builder * dw); |
fd6602a0 FCE |
2379 | static void register_function_variants(match_node * root, |
2380 | dwarf_builder * dw); | |
7a053d3b | 2381 | static void register_function_and_statement_variants(match_node * root, |
bd2b1e68 | 2382 | dwarf_builder * dw); |
c4ce66a1 | 2383 | static void register_patterns(systemtap_session& s); |
20c6c071 GH |
2384 | }; |
2385 | ||
dc38c0ae | 2386 | |
6d0f3f0c FCE |
2387 | struct uprobe_derived_probe: public derived_probe |
2388 | { | |
2389 | bool return_p; | |
2390 | string module; // * => unrestricted | |
2391 | int pid; // 0 => unrestricted | |
2392 | string section; // empty => absolute address | |
2393 | Dwarf_Addr address; | |
2394 | // bool has_maxactive; | |
2395 | // long maxactive_val; | |
0973d815 | 2396 | |
6d0f3f0c FCE |
2397 | uprobe_derived_probe (const string& function, |
2398 | const string& filename, | |
2399 | int line, | |
2400 | const string& module, | |
2401 | int pid, | |
2402 | const string& section, | |
2403 | Dwarf_Addr dwfl_addr, | |
2404 | Dwarf_Addr addr, | |
2405 | dwarf_query & q, | |
2406 | Dwarf_Die* scope_die); | |
2407 | ||
0973d815 FCE |
2408 | // alternate constructor for process(PID).statement(ADDR).absolute |
2409 | uprobe_derived_probe (probe *base, | |
2410 | probe_point *location, | |
2411 | int pid, | |
2412 | Dwarf_Addr addr, | |
2413 | bool return_p); | |
2414 | ||
6d0f3f0c FCE |
2415 | void printsig (std::ostream &o) const; |
2416 | void join_group (systemtap_session& s); | |
2417 | }; | |
2418 | ||
dc38c0ae DS |
2419 | struct dwarf_derived_probe_group: public derived_probe_group |
2420 | { | |
2421 | private: | |
b20febf3 FCE |
2422 | multimap<string,dwarf_derived_probe*> probes_by_module; |
2423 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
2424 | |
2425 | public: | |
b20febf3 FCE |
2426 | void enroll (dwarf_derived_probe* probe); |
2427 | void emit_module_decls (systemtap_session& s); | |
2428 | void emit_module_init (systemtap_session& s); | |
2429 | void emit_module_exit (systemtap_session& s); | |
dc38c0ae DS |
2430 | }; |
2431 | ||
2432 | ||
20c6c071 | 2433 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 2434 | struct base_query |
20c6c071 | 2435 | { |
c4ce66a1 JS |
2436 | base_query(dwflpp & dw, literal_map_t const & params); |
2437 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 2438 | virtual ~base_query() {} |
bd2b1e68 | 2439 | |
5227f1ea | 2440 | systemtap_session & sess; |
2c384610 | 2441 | dwflpp & dw; |
5227f1ea | 2442 | |
bd2b1e68 | 2443 | // Parameter extractors. |
86bf665e | 2444 | static bool has_null_param(literal_map_t const & params, |
888af770 | 2445 | string const & k); |
86bf665e | 2446 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 2447 | string const & k, string & v); |
86bf665e | 2448 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 2449 | string const & k, long & v); |
86bf665e | 2450 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 2451 | string const & k, Dwarf_Addr & v); |
b55bc428 | 2452 | |
2c384610 DS |
2453 | // Extracted parameters. |
2454 | bool has_kernel; | |
91af0778 FCE |
2455 | bool has_module; |
2456 | bool has_process; | |
2c384610 DS |
2457 | string module_val; // has_kernel => module_val = "kernel" |
2458 | ||
2459 | virtual void handle_query_module() = 0; | |
2460 | }; | |
2461 | ||
2462 | ||
c4ce66a1 JS |
2463 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
2464 | sess(dw.sess), dw(dw) | |
2c384610 | 2465 | { |
91af0778 | 2466 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
2467 | if (has_kernel) |
2468 | module_val = "kernel"; | |
91af0778 FCE |
2469 | |
2470 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
2471 | if (has_module) | |
2472 | has_process = false; | |
4baf0e53 | 2473 | else |
d0a7f5a9 FCE |
2474 | { |
2475 | has_process = get_string_param(params, TOK_PROCESS, module_val); | |
06aca46a | 2476 | if (has_process) |
d0a7f5a9 FCE |
2477 | module_val = find_executable (module_val); |
2478 | } | |
91af0778 FCE |
2479 | |
2480 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
2481 | } |
2482 | ||
c4ce66a1 JS |
2483 | base_query::base_query(dwflpp & dw, const string & module_val) |
2484 | : sess(dw.sess), dw(dw), module_val(module_val) | |
2485 | { | |
2486 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
2487 | // which means that userspace modules won't get any PATH searching. | |
2488 | if (module_val.find('/') == string::npos) | |
2489 | { | |
2490 | has_kernel = (module_val == TOK_KERNEL); | |
2491 | has_module = !has_kernel; | |
2492 | has_process = false; | |
2493 | } | |
2494 | else | |
2495 | { | |
2496 | has_kernel = has_module = false; | |
2497 | has_process = true; | |
2498 | } | |
2499 | } | |
2500 | ||
2c384610 | 2501 | bool |
86bf665e | 2502 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
2503 | string const & k) |
2504 | { | |
888af770 | 2505 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
2506 | } |
2507 | ||
2508 | ||
2509 | bool | |
86bf665e | 2510 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
2511 | string const & k, string & v) |
2512 | { | |
2513 | return derived_probe_builder::get_param (params, k, v); | |
2514 | } | |
2515 | ||
2516 | ||
2517 | bool | |
86bf665e | 2518 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
2519 | string const & k, long & v) |
2520 | { | |
2521 | int64_t value; | |
2522 | bool present = derived_probe_builder::get_param (params, k, value); | |
2523 | v = (long) value; | |
2524 | return present; | |
2525 | } | |
2526 | ||
2527 | ||
2528 | bool | |
86bf665e | 2529 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
2530 | string const & k, Dwarf_Addr & v) |
2531 | { | |
2532 | int64_t value; | |
2533 | bool present = derived_probe_builder::get_param (params, k, value); | |
2534 | v = (Dwarf_Addr) value; | |
2535 | return present; | |
2536 | } | |
2537 | ||
2c384610 DS |
2538 | struct dwarf_query : public base_query |
2539 | { | |
2540 | dwarf_query(systemtap_session & sess, | |
2541 | probe * base_probe, | |
2542 | probe_point * base_loc, | |
2543 | dwflpp & dw, | |
86bf665e | 2544 | literal_map_t const & params, |
2c384610 DS |
2545 | vector<derived_probe *> & results); |
2546 | ||
c4ce66a1 JS |
2547 | vector<derived_probe *> & results; |
2548 | probe * base_probe; | |
2549 | probe_point * base_loc; | |
2550 | ||
2c384610 | 2551 | virtual void handle_query_module(); |
5f0a03a6 JK |
2552 | void query_module_dwarf(); |
2553 | void query_module_symtab(); | |
2c384610 | 2554 | |
2930abc7 FCE |
2555 | void add_probe_point(string const & funcname, |
2556 | char const * filename, | |
2557 | int line, | |
2558 | Dwarf_Die *scope_die, | |
2559 | Dwarf_Addr addr); | |
d64e82b1 | 2560 | string get_blacklist_section(Dwarf_Addr addr); |
20c6c071 | 2561 | |
a7301475 FCE |
2562 | regex_t blacklist_func; // function/statement probes |
2563 | regex_t blacklist_func_ret; // only for .return probes | |
2564 | regex_t blacklist_file; // file name | |
0daad364 JS |
2565 | void build_blacklist(); |
2566 | ||
b20febf3 FCE |
2567 | bool blacklisted_p(const string& funcname, |
2568 | const string& filename, | |
36f9dd1d | 2569 | int line, |
b20febf3 FCE |
2570 | const string& module, |
2571 | const string& section, | |
36f9dd1d FCE |
2572 | Dwarf_Addr addr); |
2573 | ||
2930abc7 | 2574 | // Extracted parameters. |
7a053d3b | 2575 | string function_val; |
20c6c071 GH |
2576 | |
2577 | bool has_function_str; | |
2578 | bool has_statement_str; | |
2579 | bool has_function_num; | |
2580 | bool has_statement_num; | |
7a053d3b RM |
2581 | string statement_str_val; |
2582 | string function_str_val; | |
c239d28c GH |
2583 | Dwarf_Addr statement_num_val; |
2584 | Dwarf_Addr function_num_val; | |
20c6c071 | 2585 | |
b8da0ad1 FCE |
2586 | bool has_call; |
2587 | bool has_inline; | |
20c6c071 GH |
2588 | bool has_return; |
2589 | ||
c9bad430 DS |
2590 | bool has_maxactive; |
2591 | long maxactive_val; | |
2592 | ||
20c6c071 GH |
2593 | bool has_label; |
2594 | string label_val; | |
2595 | ||
2596 | bool has_relative; | |
2597 | long relative_val; | |
2598 | ||
37ebca01 FCE |
2599 | bool has_absolute; |
2600 | ||
467bea43 SC |
2601 | bool has_mark; |
2602 | ||
5f0a03a6 JK |
2603 | enum dbinfo_reqt dbinfo_reqt; |
2604 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
2605 | ||
20c6c071 GH |
2606 | function_spec_type parse_function_spec(string & spec); |
2607 | function_spec_type spec_type; | |
2608 | string function; | |
2609 | string file; | |
0c8b7d37 | 2610 | line_t line_type; |
879eb9e9 | 2611 | int line[2]; |
5f0a03a6 | 2612 | bool query_done; // Found exact match |
20c6c071 | 2613 | |
7e1279ea FCE |
2614 | set<char const *> filtered_srcfiles; |
2615 | ||
2616 | // Map official entrypc -> func_info object | |
86bf665e TM |
2617 | inline_instance_map_t filtered_inlines; |
2618 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
2619 | bool choose_next_line; |
2620 | Dwarf_Addr entrypc_for_next_line; | |
b55bc428 FCE |
2621 | }; |
2622 | ||
98afd80e | 2623 | |
fedd4090 FCE |
2624 | // This little test routine represents an unfortunate breakdown in |
2625 | // abstraction between dwflpp (putatively, a layer right on top of | |
2626 | // elfutils), and dwarf_query (interpreting a systemtap probe point). | |
2627 | // It arises because we sometimes try to fix up slightly-off | |
2628 | // .statement() probes (something we find out in fairly low-level). | |
2629 | // | |
2cb3fe26 | 2630 | // An alternative would be to put some more intelligence into query_cu(), |
4baf0e53 | 2631 | // and have it print additional suggestions after finding that |
fedd4090 FCE |
2632 | // q->dw.iterate_over_srcfile_lines resulted in no new finished_results. |
2633 | ||
2634 | bool | |
2635 | dwflpp::has_single_line_record (dwarf_query * q, char const * srcfile, int lineno) | |
2636 | { | |
2637 | if (lineno < 0) | |
2638 | return false; | |
2639 | ||
2640 | Dwarf_Line **srcsp = NULL; | |
2641 | size_t nsrcs = 0; | |
2642 | ||
2643 | dwarf_assert ("dwarf_getsrc_file", | |
2644 | dwarf_getsrc_file (module_dwarf, | |
2645 | srcfile, lineno, 0, | |
2646 | &srcsp, &nsrcs)); | |
2647 | ||
4baf0e53 | 2648 | if (nsrcs != 1) |
fedd4090 FCE |
2649 | { |
2650 | if (sess.verbose>4) | |
2651 | clog << "alternative line " << lineno << " rejected: nsrcs=" << nsrcs << endl; | |
2652 | return false; | |
2653 | } | |
2654 | ||
2655 | // We also try to filter out lines that leave the selected | |
2656 | // functions (if any). | |
2657 | ||
86bf665e TM |
2658 | dwarf_line_t line(srcsp[0]); |
2659 | Dwarf_Addr addr = line.addr(); | |
fedd4090 | 2660 | |
86bf665e | 2661 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
fedd4090 FCE |
2662 | i != q->filtered_functions.end(); ++i) |
2663 | { | |
3e961ba6 | 2664 | if (q->dw.die_has_pc (i->die, addr)) |
fedd4090 FCE |
2665 | { |
2666 | if (q->sess.verbose>4) | |
3e961ba6 | 2667 | clog << "alternative line " << lineno << " accepted: fn=" << i->name << endl; |
fedd4090 FCE |
2668 | return true; |
2669 | } | |
2670 | } | |
2671 | ||
86bf665e | 2672 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); |
fedd4090 FCE |
2673 | i != q->filtered_inlines.end(); ++i) |
2674 | { | |
3e961ba6 | 2675 | if (q->dw.die_has_pc (i->die, addr)) |
fedd4090 FCE |
2676 | { |
2677 | if (sess.verbose>4) | |
3e961ba6 | 2678 | clog << "alternative line " << lineno << " accepted: ifn=" << i->name << endl; |
fedd4090 FCE |
2679 | return true; |
2680 | } | |
2681 | } | |
2682 | ||
2683 | if (sess.verbose>4) | |
2684 | clog << "alternative line " << lineno << " rejected: leaves selected fns" << endl; | |
2685 | return false; | |
2686 | } | |
2687 | ||
0b8f6579 JB |
2688 | /* This basically only goes one level down from the compile unit so it |
2689 | * only picks up top level stuff (i.e. nothing in a lower scope) */ | |
2690 | int | |
2691 | dwflpp::iterate_over_globals (int (* callback)(Dwarf_Die *, void *), | |
56eb8456 | 2692 | void * data) |
0b8f6579 JB |
2693 | { |
2694 | int rc = DWARF_CB_OK; | |
2695 | Dwarf_Die die; | |
2696 | ||
2697 | assert (module); | |
2698 | assert (cu); | |
2699 | assert (dwarf_tag(cu) == DW_TAG_compile_unit); | |
2700 | ||
2701 | if (dwarf_child(cu, &die) != 0) | |
2702 | return rc; | |
2703 | ||
56eb8456 JS |
2704 | do |
2705 | /* We're only currently looking for named types, | |
0b8f6579 | 2706 | * although other types of declarations exist */ |
56eb8456 JS |
2707 | switch (dwarf_tag(&die)) |
2708 | { | |
2709 | case DW_TAG_base_type: | |
2710 | case DW_TAG_enumeration_type: | |
2711 | case DW_TAG_structure_type: | |
2712 | case DW_TAG_typedef: | |
2713 | case DW_TAG_union_type: | |
2714 | rc = (*callback)(&die, data); | |
2715 | break; | |
2716 | } | |
2717 | while (rc == DWARF_CB_OK && dwarf_siblingof(&die, &die) == 0); | |
0b8f6579 JB |
2718 | |
2719 | return rc; | |
2720 | } | |
fedd4090 | 2721 | |
6561773f | 2722 | int |
2da9cedb JS |
2723 | dwflpp::iterate_over_functions (int (* callback)(Dwarf_Die * func, base_query * q), |
2724 | base_query * q, const string& function, | |
2725 | bool has_statement_num) | |
6561773f FCE |
2726 | { |
2727 | int rc = DWARF_CB_OK; | |
2728 | assert (module); | |
2729 | assert (cu); | |
41c262f3 | 2730 | |
6561773f FCE |
2731 | string key = module_name + ":" + cu_name; |
2732 | cu_function_cache_t *v = cu_function_cache[key]; | |
2733 | if (v == 0) | |
2734 | { | |
2735 | v = new cu_function_cache_t; | |
2736 | cu_function_cache[key] = v; | |
2737 | dwarf_getfuncs (cu, cu_function_caching_callback, v, 0); | |
2738 | if (q->sess.verbose > 4) | |
2739 | clog << "function cache " << key << " size " << v->size() << endl; | |
2740 | } | |
41c262f3 | 2741 | |
2da9cedb | 2742 | string subkey = function; |
6561773f FCE |
2743 | if (v->find(subkey) != v->end()) |
2744 | { | |
2171f774 | 2745 | Dwarf_Die die = v->find(subkey)->second; |
6561773f FCE |
2746 | if (q->sess.verbose > 4) |
2747 | clog << "function cache " << key << " hit " << subkey << endl; | |
2da9cedb | 2748 | return (*callback)(& die, q); |
6561773f FCE |
2749 | } |
2750 | else if (name_has_wildcard (subkey)) | |
2751 | { | |
2752 | for (cu_function_cache_t::iterator it = v->begin(); it != v->end(); it++) | |
2753 | { | |
f76427a2 | 2754 | if (pending_interrupts) return DWARF_CB_ABORT; |
6561773f FCE |
2755 | string func_name = it->first; |
2756 | Dwarf_Die die = it->second; | |
2757 | if (function_name_matches_pattern (func_name, subkey)) | |
2758 | { | |
2759 | if (q->sess.verbose > 4) | |
2760 | clog << "function cache " << key << " match " << func_name << " vs " << subkey << endl; | |
41c262f3 | 2761 | |
2da9cedb | 2762 | rc = (*callback)(& die, q); |
6561773f FCE |
2763 | if (rc != DWARF_CB_OK) break; |
2764 | } | |
2765 | } | |
2766 | } | |
2da9cedb | 2767 | else if (has_statement_num) // searching all for kernel.statement |
cf314c0f WH |
2768 | { |
2769 | for (cu_function_cache_t::iterator it = v->begin(); it != v->end(); it++) | |
2770 | { | |
2771 | Dwarf_Die die = it->second; | |
2da9cedb | 2772 | rc = (*callback)(& die, q); |
cf314c0f WH |
2773 | if (rc != DWARF_CB_OK) break; |
2774 | } | |
2775 | } | |
6561773f FCE |
2776 | else // not a wildcard and no match in this CU |
2777 | { | |
2778 | // do nothing | |
2779 | } | |
2780 | return rc; | |
2781 | } | |
2782 | ||
2783 | ||
bc33c23b SC |
2784 | void |
2785 | dwflpp::iterate_over_labels (Dwarf_Die *begin_die, | |
2786 | void *data, | |
2787 | void (* callback)(const string &, | |
2788 | const char *, | |
2789 | int, | |
2790 | Dwarf_Die *, | |
2791 | Dwarf_Addr, | |
2792 | dwarf_query *)) | |
2793 | { | |
2794 | dwarf_query * q __attribute__ ((unused)) = static_cast<dwarf_query *>(data) ; | |
bd4b874d | 2795 | |
bc33c23b | 2796 | get_module_dwarf(); |
bd4b874d | 2797 | |
bc33c23b SC |
2798 | const char * sym = q->label_val.c_str(); |
2799 | Dwarf_Die die; | |
2800 | int res = dwarf_child (begin_die, &die); | |
2801 | if (res != 0) | |
2802 | return; // die without children, bail out. | |
bd4b874d | 2803 | |
bc33c23b SC |
2804 | static string function_name = dwarf_diename (begin_die); |
2805 | do | |
2806 | { | |
2807 | Dwarf_Attribute attr_mem; | |
2808 | Dwarf_Attribute *attr = dwarf_attr (&die, DW_AT_name, &attr_mem); | |
2809 | int tag = dwarf_tag(&die); | |
2810 | const char *name = dwarf_formstring (attr); | |
2811 | if (name == 0) | |
2812 | continue; | |
2813 | switch (tag) | |
2814 | { | |
2815 | case DW_TAG_label: | |
2816 | break; | |
2817 | case DW_TAG_subprogram: | |
c60a55f2 MW |
2818 | if (!dwarf_hasattr(&die, DW_AT_declaration)) |
2819 | function_name = name; | |
2820 | else | |
2821 | continue; | |
bc33c23b SC |
2822 | default: |
2823 | if (dwarf_haschildren (&die)) | |
2824 | iterate_over_labels (&die, q, callback); | |
bd4b874d | 2825 | continue; |
bc33c23b | 2826 | } |
bd4b874d | 2827 | |
bc33c23b SC |
2828 | if (strcmp(function_name.c_str(), q->function.c_str()) == 0 |
2829 | || (name_has_wildcard(q->function) | |
2830 | && function_name_matches_pattern (function_name, q->function))) | |
2831 | { | |
2832 | } | |
2833 | else | |
2834 | continue; | |
2835 | if (strcmp(name, sym) == 0 | |
2836 | || (name_has_wildcard(sym) | |
2837 | && function_name_matches_pattern (name, sym))) | |
2838 | { | |
2839 | const char *file = dwarf_decl_file (&die); | |
2840 | // Get the line number for this label | |
2841 | Dwarf_Attribute attr; | |
2842 | dwarf_attr (&die,DW_AT_decl_line, &attr); | |
2843 | Dwarf_Sword dline; | |
2844 | dwarf_formsdata (&attr, &dline); | |
2845 | Dwarf_Addr stmt_addr; | |
2846 | if (dwarf_lowpc (&die, &stmt_addr) != 0) | |
2847 | { | |
2848 | // There is no lowpc so figure out the address | |
2849 | // Get the real die for this cu | |
2850 | Dwarf_Die cudie; | |
2851 | dwarf_diecu (q->dw.cu, &cudie, NULL, NULL); | |
2852 | size_t nlines = 0; | |
2853 | // Get the line for this label | |
2854 | Dwarf_Line **aline; | |
2855 | dwarf_getsrc_file (module_dwarf, file, (int)dline, 0, &aline, &nlines); | |
2856 | // Get the address | |
2857 | for (size_t i = 0; i < nlines; i++) | |
2858 | { | |
2859 | dwarf_lineaddr (*aline, &stmt_addr); | |
2860 | if ((dwarf_haspc (&die, stmt_addr))) | |
2861 | break; | |
2862 | } | |
2863 | } | |
bd4b874d | 2864 | |
bc33c23b SC |
2865 | Dwarf_Die *scopes; |
2866 | int nscopes = 0; | |
2867 | nscopes = dwarf_getscopes_die (&die, &scopes); | |
2868 | if (nscopes > 1) | |
2869 | { | |
2870 | callback(function_name.c_str(), file, | |
2871 | (int)dline, &scopes[1], stmt_addr, q); | |
2872 | if (sess.listing_mode) | |
2873 | q->results.back()->locations[0]->components.push_back | |
2874 | (new probe_point::component(TOK_LABEL, new literal_string (name))); | |
2875 | } | |
2876 | } | |
2877 | } | |
2878 | while (dwarf_siblingof (&die, &die) == 0); | |
2879 | } | |
bd4b874d | 2880 | |
fedd4090 | 2881 | |
98afd80e | 2882 | struct dwarf_builder: public derived_probe_builder |
b55bc428 | 2883 | { |
e38d6504 | 2884 | dwflpp *kern_dw; |
7a24d422 | 2885 | map <string,dwflpp*> user_dw; |
b8da0ad1 | 2886 | dwarf_builder(): kern_dw(0) {} |
aa30ccd3 | 2887 | |
7a24d422 FCE |
2888 | |
2889 | /* NB: not virtual, so can be called from dtor too: */ | |
06aca46a | 2890 | void dwarf_build_no_more (bool verbose) |
aa30ccd3 FCE |
2891 | { |
2892 | if (kern_dw) | |
2893 | { | |
7a24d422 FCE |
2894 | if (verbose) |
2895 | clog << "dwarf_builder releasing kernel dwflpp" << endl; | |
aa30ccd3 FCE |
2896 | delete kern_dw; |
2897 | kern_dw = 0; | |
2898 | } | |
7a24d422 FCE |
2899 | |
2900 | for (map<string,dwflpp*>::iterator udi = user_dw.begin(); | |
2901 | udi != user_dw.end(); | |
2902 | udi ++) | |
2903 | { | |
2904 | if (verbose) | |
2905 | clog << "dwarf_builder releasing user dwflpp " << udi->first << endl; | |
2906 | delete udi->second; | |
2907 | } | |
2908 | user_dw.erase (user_dw.begin(), user_dw.end()); | |
2909 | } | |
2910 | ||
2911 | void build_no_more (systemtap_session &s) | |
2912 | { | |
2913 | dwarf_build_no_more (s.verbose > 3); | |
aa30ccd3 FCE |
2914 | } |
2915 | ||
e38d6504 RM |
2916 | ~dwarf_builder() |
2917 | { | |
7a24d422 | 2918 | dwarf_build_no_more (false); |
c8959a29 | 2919 | } |
aa30ccd3 | 2920 | |
5227f1ea | 2921 | virtual void build(systemtap_session & sess, |
7a053d3b | 2922 | probe * base, |
20c6c071 | 2923 | probe_point * location, |
86bf665e | 2924 | literal_map_t const & parameters, |
20c6c071 | 2925 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
2926 | }; |
2927 | ||
888af770 | 2928 | |
5227f1ea GH |
2929 | dwarf_query::dwarf_query(systemtap_session & sess, |
2930 | probe * base_probe, | |
20c6c071 GH |
2931 | probe_point * base_loc, |
2932 | dwflpp & dw, | |
86bf665e | 2933 | literal_map_t const & params, |
20c6c071 | 2934 | vector<derived_probe *> & results) |
c4ce66a1 JS |
2935 | : base_query(dw, params), results(results), |
2936 | base_probe(base_probe), base_loc(base_loc) | |
bd2b1e68 GH |
2937 | { |
2938 | // Reduce the query to more reasonable semantic values (booleans, | |
2939 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
2940 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
2941 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
2942 | ||
2943 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
2944 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
2945 | ||
0f336e95 SC |
2946 | has_label = get_string_param(params, TOK_LABEL, label_val); |
2947 | ||
b8da0ad1 FCE |
2948 | has_call = has_null_param(params, TOK_CALL); |
2949 | has_inline = has_null_param(params, TOK_INLINE); | |
bd2b1e68 | 2950 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 2951 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 2952 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 2953 | has_mark = false; |
37ebca01 | 2954 | |
bd2b1e68 GH |
2955 | if (has_function_str) |
2956 | spec_type = parse_function_spec(function_str_val); | |
2957 | else if (has_statement_str) | |
2958 | spec_type = parse_function_spec(statement_str_val); | |
0daad364 | 2959 | |
b8da0ad1 | 2960 | build_blacklist(); // XXX: why not reuse amongst dwarf_query instances? |
5f0a03a6 JK |
2961 | dbinfo_reqt = assess_dbinfo_reqt(); |
2962 | query_done = false; | |
0daad364 JS |
2963 | } |
2964 | ||
2965 | ||
2c384610 | 2966 | void |
5f0a03a6 | 2967 | dwarf_query::query_module_dwarf() |
2c384610 DS |
2968 | { |
2969 | if (has_function_num || has_statement_num) | |
2970 | { | |
2971 | // If we have module("foo").function(0xbeef) or | |
2972 | // module("foo").statement(0xbeef), the address is relative | |
2973 | // to the start of the module, so we seek the function | |
2974 | // number plus the module's bias. | |
2975 | ||
2976 | Dwarf_Addr addr; | |
2977 | if (has_function_num) | |
2978 | addr = function_num_val; | |
2979 | else | |
2980 | addr = statement_num_val; | |
4baf0e53 | 2981 | |
2c384610 DS |
2982 | // NB: we don't need to add the module base address or bias |
2983 | // value here (for reasons that may be coincidental). | |
2984 | dw.query_cu_containing_module_address(addr, this); | |
2985 | } | |
2986 | else | |
2987 | { | |
2988 | // Otherwise if we have a function("foo") or statement("foo") | |
2989 | // specifier, we have to scan over all the CUs looking for | |
2990 | // the function(s) in question | |
2991 | assert(has_function_str || has_statement_str); | |
2992 | dw.iterate_over_cus(&query_cu, this); | |
2993 | } | |
2994 | } | |
2995 | ||
5f0a03a6 JK |
2996 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
2997 | dwarf_query * q); | |
2998 | ||
2999 | void | |
3000 | dwarf_query::query_module_symtab() | |
3001 | { | |
3002 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
3003 | if (dbinfo_reqt == dbr_need_dwarf) | |
3004 | return; | |
3005 | ||
3006 | module_info *mi = dw.mod_info; | |
3007 | if (dbinfo_reqt == dbr_need_symtab) | |
3008 | { | |
3009 | if (mi->symtab_status == info_unknown) | |
3010 | mi->get_symtab(this); | |
3011 | if (mi->symtab_status == info_absent) | |
3012 | return; | |
3013 | } | |
3014 | ||
3015 | func_info *fi = NULL; | |
3016 | symbol_table *sym_table = mi->sym_table; | |
3017 | ||
3018 | if (has_function_str) | |
3019 | { | |
3020 | // Per dwarf_query::assess_dbinfo_reqt()... | |
3021 | assert(spec_type == function_alone); | |
3022 | if (dw.name_has_wildcard(function_str_val)) | |
3023 | { | |
3024 | // Until we augment the blacklist sufficently... | |
3025 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
3026 | { | |
3027 | // e.g., kernel.function("*") | |
3028 | cerr << "Error: Pattern '" | |
3029 | << function_str_val | |
3030 | << "' matches every instruction address in the symbol table," | |
3031 | << endl | |
3032 | << "some of which aren't even functions." | |
3033 | << " Please be more precise." | |
3034 | << endl; | |
3035 | return; | |
3036 | } | |
2e67a43b TM |
3037 | symbol_table::iterator_t iter; |
3038 | for (iter = sym_table->list_by_addr.begin(); | |
3039 | iter != sym_table->list_by_addr.end(); | |
3040 | ++iter) | |
5f0a03a6 | 3041 | { |
2e67a43b | 3042 | fi = *iter; |
5f0a03a6 JK |
3043 | if (!null_die(&fi->die)) |
3044 | continue; // already handled in query_module_dwarf() | |
3045 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
3046 | query_func_info(fi->addr, *fi, this); | |
3047 | } | |
3048 | } | |
3049 | else | |
3050 | { | |
3051 | fi = sym_table->lookup_symbol(function_str_val); | |
3052 | if (fi && null_die(&fi->die)) | |
3053 | query_func_info(fi->addr, *fi, this); | |
3054 | } | |
3055 | } | |
3056 | else | |
3057 | { | |
3058 | assert(has_function_num || has_statement_num); | |
3059 | // Find the "function" in which the indicated address resides. | |
3060 | Dwarf_Addr addr = | |
3061 | (has_function_num ? function_num_val : statement_num_val); | |
3062 | fi = sym_table->get_func_containing_address(addr); | |
3063 | if (!fi) | |
3064 | { | |
3065 | cerr << "Warning: address " | |
3066 | << hex << addr << dec | |
3067 | << " out of range for module " | |
3068 | << dw.module_name; | |
3069 | return; | |
3070 | } | |
3071 | if (!null_die(&fi->die)) | |
3072 | { | |
3073 | // addr looks like it's in the compilation unit containing | |
3074 | // the indicated function, but query_module_dwarf() didn't | |
3075 | // match addr to any compilation unit, so addr must be | |
3076 | // above that cu's address range. | |
3077 | cerr << "Warning: address " | |
3078 | << hex << addr << dec | |
3079 | << " maps to no known compilation unit in module " | |
3080 | << dw.module_name; | |
3081 | return; | |
3082 | } | |
3083 | query_func_info(fi->addr, *fi, this); | |
3084 | } | |
3085 | } | |
3086 | ||
3087 | void | |
3088 | dwarf_query::handle_query_module() | |
3089 | { | |
3090 | dw.get_module_dwarf(false, | |
3091 | (dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab)); | |
3092 | if (dw.mod_info->dwarf_status == info_present) | |
3093 | query_module_dwarf(); | |
3094 | // Consult the symbol table if we haven't found all we're looking for. | |
3095 | // asm functions can show up in the symbol table but not in dwarf. | |
3096 | if (sess.consult_symtab && !query_done) | |
3097 | query_module_symtab(); | |
3098 | } | |
3099 | ||
2c384610 | 3100 | |
0daad364 JS |
3101 | void |
3102 | dwarf_query::build_blacklist() | |
3103 | { | |
91af0778 FCE |
3104 | // No blacklist for userspace. |
3105 | if (has_process) | |
3106 | return; | |
3107 | ||
a7301475 FCE |
3108 | // We build up the regexps in these strings |
3109 | ||
3110 | // Add ^ anchors at the front; $ will be added just before regcomp. | |
3111 | ||
a7301475 FCE |
3112 | string blfn = "^("; |
3113 | string blfn_ret = "^("; | |
3114 | string blfile = "^("; | |
3115 | ||
e4c58386 | 3116 | blfile += "kernel/kprobes.c"; // first alternative, no "|" |
a7301475 | 3117 | blfile += "|arch/.*/kernel/kprobes.c"; |
275a898f | 3118 | // Older kernels need ... |
02a929d1 | 3119 | blfile += "|include/asm/io.h"; |
275a898f AM |
3120 | blfile += "|include/asm/bitops.h"; |
3121 | // While newer ones need ... | |
3122 | blfile += "|arch/.*/include/asm/io.h"; | |
3123 | blfile += "|arch/.*/include/asm/bitops.h"; | |
02a929d1 | 3124 | blfile += "|drivers/ide/ide-iops.c"; |
a7301475 FCE |
3125 | |
3126 | // XXX: it would be nice if these blacklisted functions were pulled | |
3127 | // in dynamically, instead of being statically defined here. | |
49f426d9 FCE |
3128 | // Perhaps it could be populated from script files. A "noprobe |
3129 | // kernel.function("...")" construct might do the trick. | |
0daad364 | 3130 | |
b20febf3 | 3131 | // Most of these are marked __kprobes in newer kernels. We list |
a7301475 FCE |
3132 | // them here (anyway) so the translator can block them on older |
3133 | // kernels that don't have the __kprobes function decorator. This | |
3134 | // also allows detection of problems at translate- rather than | |
3135 | // run-time. | |
3136 | ||
e4c58386 | 3137 | blfn += "atomic_notifier_call_chain"; // first blfn; no "|" |
a7301475 FCE |
3138 | blfn += "|default_do_nmi"; |
3139 | blfn += "|__die"; | |
3140 | blfn += "|die_nmi"; | |
3141 | blfn += "|do_debug"; | |
3142 | blfn += "|do_general_protection"; | |
3143 | blfn += "|do_int3"; | |
3144 | blfn += "|do_IRQ"; | |
3145 | blfn += "|do_page_fault"; | |
3146 | blfn += "|do_sparc64_fault"; | |
3147 | blfn += "|do_trap"; | |
3148 | blfn += "|dummy_nmi_callback"; | |
3149 | blfn += "|flush_icache_range"; | |
3150 | blfn += "|ia64_bad_break"; | |
3151 | blfn += "|ia64_do_page_fault"; | |
3152 | blfn += "|ia64_fault"; | |
3153 | blfn += "|io_check_error"; | |
3154 | blfn += "|mem_parity_error"; | |
3155 | blfn += "|nmi_watchdog_tick"; | |
3156 | blfn += "|notifier_call_chain"; | |
3157 | blfn += "|oops_begin"; | |
3158 | blfn += "|oops_end"; | |
3159 | blfn += "|program_check_exception"; | |
3160 | blfn += "|single_step_exception"; | |
3161 | blfn += "|sync_regs"; | |
3162 | blfn += "|unhandled_fault"; | |
3163 | blfn += "|unknown_nmi_error"; | |
3164 | ||
3165 | // Lots of locks | |
3166 | blfn += "|.*raw_.*lock.*"; | |
3167 | blfn += "|.*read_.*lock.*"; | |
3168 | blfn += "|.*write_.*lock.*"; | |
3169 | blfn += "|.*spin_.*lock.*"; | |
3170 | blfn += "|.*rwlock_.*lock.*"; | |
3171 | blfn += "|.*rwsem_.*lock.*"; | |
3172 | blfn += "|.*mutex_.*lock.*"; | |
3173 | blfn += "|raw_.*"; | |
3174 | blfn += "|.*seq_.*lock.*"; | |
3175 | ||
a11f4bae | 3176 | // atomic functions |
2ca12712 JS |
3177 | blfn += "|atomic_.*"; |
3178 | blfn += "|atomic64_.*"; | |
a11f4bae SD |
3179 | |
3180 | // few other problematic cases | |
3181 | blfn += "|get_bh"; | |
3182 | blfn += "|put_bh"; | |
3183 | ||
a7301475 FCE |
3184 | // Experimental |
3185 | blfn += "|.*apic.*|.*APIC.*"; | |
3186 | blfn += "|.*softirq.*"; | |
3187 | blfn += "|.*IRQ.*"; | |
3188 | blfn += "|.*_intr.*"; | |
3189 | blfn += "|__delay"; | |
3190 | blfn += "|.*kernel_text.*"; | |
3191 | blfn += "|get_current"; | |
3192 | blfn += "|current_.*"; | |
3193 | blfn += "|.*exception_tables.*"; | |
3194 | blfn += "|.*setup_rt_frame.*"; | |
c931ec8a | 3195 | |
a8c9be6f | 3196 | // PR 5759, CONFIG_PREEMPT kernels |
a7301475 FCE |
3197 | blfn += "|.*preempt_count.*"; |
3198 | blfn += "|preempt_schedule"; | |
a8c9be6f | 3199 | |
e4c58386 FCE |
3200 | // These functions don't return, so return probes would never be recovered |
3201 | blfn_ret += "do_exit"; // no "|" | |
3202 | blfn_ret += "|sys_exit"; | |
3203 | blfn_ret += "|sys_exit_group"; | |
3204 | ||
a721fbcf MH |
3205 | // __switch_to changes "current" on x86_64 and i686, so return probes |
3206 | // would cause kernel panic, and it is marked as "__kprobes" on x86_64 | |
0daad364 | 3207 | if (sess.architecture == "x86_64") |
a7301475 | 3208 | blfn += "|__switch_to"; |
a721fbcf | 3209 | if (sess.architecture == "i686") |
a7301475 | 3210 | blfn_ret += "|__switch_to"; |
0daad364 | 3211 | |
a7301475 FCE |
3212 | blfn += ")$"; |
3213 | blfn_ret += ")$"; | |
3214 | blfile += ")$"; | |
3215 | ||
41c262f3 | 3216 | if (sess.verbose > 2) |
e4c58386 FCE |
3217 | { |
3218 | clog << "blacklist regexps:" << endl; | |
3219 | clog << "blfn: " << blfn << endl; | |
3220 | clog << "blfn_ret: " << blfn_ret << endl; | |
3221 | clog << "blfile: " << blfile << endl; | |
3222 | } | |
3223 | ||
a7301475 FCE |
3224 | int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED); |
3225 | if (rc) throw semantic_error ("blacklist_func regcomp failed"); | |
3226 | rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED); | |
3227 | if (rc) throw semantic_error ("blacklist_func_ret regcomp failed"); | |
3228 | rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED); | |
3229 | if (rc) throw semantic_error ("blacklist_file regcomp failed"); | |
7a053d3b | 3230 | } |
bd2b1e68 GH |
3231 | |
3232 | ||
bd2b1e68 | 3233 | function_spec_type |
20c6c071 | 3234 | dwarf_query::parse_function_spec(string & spec) |
bd2b1e68 GH |
3235 | { |
3236 | string::const_iterator i = spec.begin(), e = spec.end(); | |
7a053d3b | 3237 | |
bd2b1e68 GH |
3238 | function.clear(); |
3239 | file.clear(); | |
879eb9e9 SC |
3240 | line[0] = 0; |
3241 | line[1] = 0; | |
bd2b1e68 GH |
3242 | |
3243 | while (i != e && *i != '@') | |
3244 | { | |
0c8b7d37 | 3245 | if (*i == ':' || *i == '+') |
bd2b1e68 GH |
3246 | goto bad; |
3247 | function += *i++; | |
3248 | } | |
3249 | ||
3250 | if (i == e) | |
3251 | { | |
b0ee93c4 | 3252 | if (sess.verbose>2) |
7a053d3b RM |
3253 | clog << "parsed '" << spec |
3254 | << "' -> func '" << function | |
db22e55f | 3255 | << "'\n"; |
bd2b1e68 GH |
3256 | return function_alone; |
3257 | } | |
3258 | ||
3259 | if (i++ == e) | |
3260 | goto bad; | |
3261 | ||
0c8b7d37 | 3262 | while (i != e && *i != ':' && *i != '+') |
bd2b1e68 | 3263 | file += *i++; |
41c262f3 | 3264 | if (*i == ':') |
879eb9e9 SC |
3265 | { |
3266 | if (*(i + 1) == '*') | |
3267 | line_type = WILDCARD; | |
3268 | else | |
3269 | line_type = ABSOLUTE; | |
3270 | } | |
0c8b7d37 SC |
3271 | else if (*i == '+') |
3272 | line_type = RELATIVE; | |
7a053d3b | 3273 | |
bd2b1e68 GH |
3274 | if (i == e) |
3275 | { | |
b0ee93c4 | 3276 | if (sess.verbose>2) |
7a053d3b RM |
3277 | clog << "parsed '" << spec |
3278 | << "' -> func '"<< function | |
3279 | << "', file '" << file | |
db22e55f | 3280 | << "'\n"; |
bd2b1e68 GH |
3281 | return function_and_file; |
3282 | } | |
3283 | ||
3284 | if (i++ == e) | |
3285 | goto bad; | |
3286 | ||
3287 | try | |
3288 | { | |
879eb9e9 SC |
3289 | if (line_type != WILDCARD) |
3290 | { | |
3291 | string::const_iterator dash = i; | |
41c262f3 | 3292 | |
879eb9e9 SC |
3293 | while (dash != e && *dash != '-') |
3294 | dash++; | |
3295 | if (dash == e) | |
3296 | line[0] = line[1] = lex_cast<int>(string(i, e)); | |
3297 | else | |
3298 | { | |
3299 | line_type = RANGE; | |
3300 | line[0] = lex_cast<int>(string(i, dash)); | |
3301 | line[1] = lex_cast<int>(string(dash + 1, e)); | |
3302 | } | |
3303 | } | |
41c262f3 | 3304 | |
b0ee93c4 | 3305 | if (sess.verbose>2) |
7a053d3b RM |
3306 | clog << "parsed '" << spec |
3307 | << "' -> func '"<< function | |
3308 | << "', file '" << file | |
db22e55f | 3309 | << "', line " << line << "\n"; |
bd2b1e68 GH |
3310 | return function_file_and_line; |
3311 | } | |
3312 | catch (runtime_error & exn) | |
3313 | { | |
3314 | goto bad; | |
3315 | } | |
3316 | ||
3317 | bad: | |
7a053d3b | 3318 | throw semantic_error("malformed specification '" + spec + "'", |
20c6c071 | 3319 | base_probe->tok); |
bd2b1e68 GH |
3320 | } |
3321 | ||
3322 | ||
91af0778 | 3323 | #if 0 |
b20febf3 | 3324 | // Forward declaration. |
91af0778 | 3325 | static int query_kernel_module (Dwfl_Module *, void **, const char *, |
b20febf3 | 3326 | Dwarf_Addr, void *); |
91af0778 | 3327 | #endif |
7e1279ea | 3328 | |
2930abc7 | 3329 | |
b8da0ad1 | 3330 | // XXX: pull this into dwflpp |
b20febf3 FCE |
3331 | static bool |
3332 | in_kprobes_function(systemtap_session& sess, Dwarf_Addr addr) | |
2930abc7 | 3333 | { |
84048984 | 3334 | if (sess.sym_kprobes_text_start != 0 && sess.sym_kprobes_text_end != 0) |
1d3a40b6 DS |
3335 | { |
3336 | // If the probe point address is anywhere in the __kprobes | |
3337 | // address range, we can't use this probe point. | |
84048984 | 3338 | if (addr >= sess.sym_kprobes_text_start && addr < sess.sym_kprobes_text_end) |
1d3a40b6 DS |
3339 | return true; |
3340 | } | |
3341 | return false; | |
3342 | } | |
3343 | ||
20e4a32c | 3344 | |
36f9dd1d | 3345 | bool |
b20febf3 FCE |
3346 | dwarf_query::blacklisted_p(const string& funcname, |
3347 | const string& filename, | |
78f6bba6 | 3348 | int, |
b20febf3 FCE |
3349 | const string& module, |
3350 | const string& section, | |
36f9dd1d FCE |
3351 | Dwarf_Addr addr) |
3352 | { | |
91af0778 FCE |
3353 | if (has_process) |
3354 | return false; // no blacklist for userspace | |
3355 | ||
b20febf3 | 3356 | if (section.substr(0, 6) == string(".init.") || |
f90f9261 SD |
3357 | section.substr(0, 6) == string(".exit.") || |
3358 | section.substr(0, 9) == string(".devinit.") || | |
3359 | section.substr(0, 9) == string(".devexit.") || | |
3360 | section.substr(0, 9) == string(".cpuinit.") || | |
3361 | section.substr(0, 9) == string(".cpuexit.") || | |
3362 | section.substr(0, 9) == string(".meminit.") || | |
3363 | section.substr(0, 9) == string(".memexit.")) | |
703621ae | 3364 | { |
b8da0ad1 FCE |
3365 | // NB: module .exit. routines could be probed in theory: |
3366 | // if the exit handler in "struct module" is diverted, | |
3367 | // first inserting the kprobes | |
3368 | // then allowing the exit code to run | |
3369 | // then removing these kprobes | |
b20febf3 FCE |
3370 | if (sess.verbose>1) |
3371 | clog << " skipping - init/exit"; | |
3372 | return true; | |
703621ae FCE |
3373 | } |
3374 | ||
1d3a40b6 | 3375 | // Check for function marked '__kprobes'. |
b20febf3 | 3376 | if (module == TOK_KERNEL && in_kprobes_function(sess, addr)) |
1d3a40b6 DS |
3377 | { |
3378 | if (sess.verbose>1) | |
b20febf3 | 3379 | clog << " skipping - __kprobes"; |
1d3a40b6 DS |
3380 | return true; |
3381 | } | |
4baf0e53 | 3382 | |
a7301475 FCE |
3383 | // Check probe point against blacklist. |
3384 | int goodfn = regexec (&blacklist_func, funcname.c_str(), 0, NULL, 0); | |
3385 | if (has_return) | |
3386 | goodfn = goodfn && regexec (&blacklist_func_ret, funcname.c_str(), 0, NULL, 0); | |
3387 | int goodfile = regexec (&blacklist_file, filename.c_str(), 0, NULL, 0); | |
3388 | ||
3389 | if (! (goodfn && goodfile)) | |
36f9dd1d | 3390 | { |
e2ae0696 LR |
3391 | if (sess.guru_mode) |
3392 | { | |
3393 | if (sess.verbose>1) | |
3394 | clog << " guru mode enabled - ignoring blacklist"; | |
3395 | } | |
3396 | else | |
3397 | { | |
3398 | if (sess.verbose>1) | |
3399 | clog << " skipping - blacklisted"; | |
3400 | return true; | |
3401 | } | |
36f9dd1d FCE |
3402 | } |
3403 | ||
3404 | // This probe point is not blacklisted. | |
3405 | return false; | |
3406 | } | |
3407 | ||
d64e82b1 SD |
3408 | string dwarf_query::get_blacklist_section(Dwarf_Addr addr) |
3409 | { | |
d64e82b1 | 3410 | string blacklist_section; |
f9331b29 RM |
3411 | Dwarf_Addr bias; |
3412 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
3413 | // because dwfl_module_getelf can force costly section relocations | |
3414 | // we don't really need, while either will do for this purpose. | |
3415 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias)) | |
3416 | ?: dwfl_module_getelf (dw.module, &bias)); | |
3417 | ||
3418 | Dwarf_Addr offset = addr - bias; | |
d64e82b1 SD |
3419 | if (elf) |
3420 | { | |
3421 | Elf_Scn* scn = 0; | |
3422 | size_t shstrndx; | |
86bf665e | 3423 | dwfl_assert ("getshstrndx", elf_getshstrndx (elf, &shstrndx)); |
d64e82b1 SD |
3424 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
3425 | { | |
3426 | GElf_Shdr shdr_mem; | |
3427 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); | |
3428 | if (! shdr) continue; // XXX error? | |
3429 | ||
f9331b29 RM |
3430 | if (!(shdr->sh_flags & SHF_ALLOC)) |
3431 | continue; | |
3432 | ||
d64e82b1 SD |
3433 | GElf_Addr start = shdr->sh_addr; |
3434 | GElf_Addr end = start + shdr->sh_size; | |
3435 | if (! (offset >= start && offset < end)) | |
3436 | continue; | |
3437 | ||
3438 | blacklist_section = elf_strptr (elf, shstrndx, shdr->sh_name); | |
3439 | break; | |
3440 | } | |
3441 | } | |
3442 | return blacklist_section; | |
3443 | } | |
36f9dd1d | 3444 | |
b20febf3 | 3445 | |
36f9dd1d | 3446 | void |
b20febf3 FCE |
3447 | dwarf_query::add_probe_point(const string& funcname, |
3448 | const char* filename, | |
36f9dd1d | 3449 | int line, |
b20febf3 | 3450 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
3451 | Dwarf_Addr addr) |
3452 | { | |
b20febf3 FCE |
3453 | string reloc_section; // base section for relocation purposes |
3454 | Dwarf_Addr reloc_addr = addr; // relocated | |
3455 | string blacklist_section; // linking section for blacklist purposes | |
3456 | const string& module = dw.module_name; // "kernel" or other | |
36f9dd1d | 3457 | |
37ebca01 FCE |
3458 | assert (! has_absolute); // already handled in dwarf_builder::build() |
3459 | ||
5f0a03a6 JK |
3460 | if (!dw.module) |
3461 | { | |
3462 | assert(module == TOK_KERNEL); | |
3463 | reloc_section = ""; | |
3464 | blacklist_section = ""; | |
3465 | } | |
3466 | else if (dwfl_module_relocations (dw.module) > 0) | |
2930abc7 | 3467 | { |
17c128f2 | 3468 | // This is a relocatable module; libdwfl already knows its |
b20febf3 FCE |
3469 | // sections, so we can relativize addr. |
3470 | int idx = dwfl_module_relocate_address (dw.module, &reloc_addr); | |
3471 | const char* r_s = dwfl_module_relocation_info (dw.module, idx, NULL); | |
3472 | if (r_s) | |
3473 | reloc_section = r_s; | |
3474 | blacklist_section = reloc_section; | |
d64e82b1 SD |
3475 | |
3476 | if(reloc_section == "" && dwfl_module_relocations (dw.module) == 1) | |
17c128f2 FCE |
3477 | { |
3478 | blacklist_section = this->get_blacklist_section(addr); | |
3479 | reloc_section = ".dynamic"; | |
4b0eb118 | 3480 | reloc_addr = addr; |
17c128f2 | 3481 | } |
2930abc7 FCE |
3482 | } |
3483 | else | |
3484 | { | |
d64e82b1 | 3485 | blacklist_section = this->get_blacklist_section(addr); |
17c128f2 | 3486 | reloc_section = ".absolute"; |
2930abc7 FCE |
3487 | } |
3488 | ||
7f9f3386 FCE |
3489 | if (sess.verbose > 1) |
3490 | { | |
b20febf3 FCE |
3491 | clog << "probe " << funcname << "@" << filename << ":" << line; |
3492 | if (string(module) == TOK_KERNEL) | |
3493 | clog << " kernel"; | |
91af0778 | 3494 | else if (has_module) |
b20febf3 | 3495 | clog << " module=" << module; |
91af0778 FCE |
3496 | else if (has_process) |
3497 | clog << " process=" << module; | |
b20febf3 FCE |
3498 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
3499 | if (blacklist_section != "") clog << " section=" << blacklist_section; | |
3500 | clog << " pc=0x" << hex << addr << dec; | |
7f9f3386 | 3501 | } |
4baf0e53 | 3502 | |
b20febf3 FCE |
3503 | bool bad = blacklisted_p (funcname, filename, line, module, blacklist_section, addr); |
3504 | if (sess.verbose > 1) | |
3505 | clog << endl; | |
7f9f3386 | 3506 | |
84048984 FCE |
3507 | if (module == TOK_KERNEL) |
3508 | { | |
3509 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
3510 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 3511 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
3512 | } |
3513 | ||
b20febf3 FCE |
3514 | if (! bad) |
3515 | { | |
1a0dbc5a | 3516 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
3517 | |
3518 | if (has_process) | |
3519 | { | |
3520 | results.push_back (new uprobe_derived_probe(funcname, filename, line, | |
3521 | module, 0, reloc_section, addr, reloc_addr, | |
3522 | *this, scope_die)); | |
3523 | } | |
3524 | else | |
3525 | { | |
3526 | assert (has_kernel || has_module); | |
3527 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 3528 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
3529 | *this, scope_die)); |
3530 | } | |
b20febf3 | 3531 | } |
2930abc7 FCE |
3532 | } |
3533 | ||
5f0a03a6 JK |
3534 | enum dbinfo_reqt |
3535 | dwarf_query::assess_dbinfo_reqt() | |
3536 | { | |
3537 | if (has_absolute) | |
3538 | { | |
3539 | // kernel.statement(NUM).absolute | |
3540 | return dbr_none; | |
3541 | } | |
3542 | if (has_inline) | |
3543 | { | |
3544 | // kernel.function("f").inline or module("m").function("f").inline | |
3545 | return dbr_need_dwarf; | |
3546 | } | |
3547 | if (has_function_str && spec_type == function_alone) | |
3548 | { | |
3549 | // kernel.function("f") or module("m").function("f") | |
3550 | return dbr_need_symtab; | |
3551 | } | |
3552 | if (has_statement_num) | |
3553 | { | |
3554 | // kernel.statement(NUM) or module("m").statement(NUM) | |
3555 | // Technically, all we need is the module offset (or _stext, for | |
3556 | // the kernel). But for that we need either the ELF file or (for | |
3557 | // _stext) the symbol table. In either case, the symbol table | |
3558 | // is available, and that allows us to map the NUM (address) | |
3559 | // to a function, which is goodness. | |
3560 | return dbr_need_symtab; | |
3561 | } | |
3562 | if (has_function_num) | |
3563 | { | |
3564 | // kernel.function(NUM) or module("m").function(NUM) | |
3565 | // Need the symbol table so we can back up from NUM to the | |
3566 | // start of the function. | |
3567 | return dbr_need_symtab; | |
3568 | } | |
3569 | // Symbol table tells us nothing about source files or line numbers. | |
3570 | return dbr_need_dwarf; | |
3571 | } | |
2930abc7 FCE |
3572 | |
3573 | ||
b8da0ad1 FCE |
3574 | // The critical determining factor when interpreting a pattern |
3575 | // string is, perhaps surprisingly: "presence of a lineno". The | |
3576 | // presence of a lineno changes the search strategy completely. | |
3577 | // | |
3578 | // Compare the two cases: | |
3579 | // | |
3580 | // 1. {statement,function}(foo@file.c:lineno) | |
3581 | // - find the files matching file.c | |
3582 | // - in each file, find the functions matching foo | |
3583 | // - query the file for line records matching lineno | |
3584 | // - iterate over the line records, | |
3585 | // - and iterate over the functions, | |
3586 | // - if(haspc(function.DIE, line.addr)) | |
3587 | // - if looking for statements: probe(lineno.addr) | |
3588 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
3589 | // | |
3590 | // 2. {statement,function}(foo@file.c) | |
3591 | // - find the files matching file.c | |
3592 | // - in each file, find the functions matching foo | |
3593 | // - probe(function.{entrypc,return,etc.}) | |
3594 | // | |
3595 | // Thus the first decision we make is based on the presence of a | |
3596 | // lineno, and we enter entirely different sets of callbacks | |
3597 | // depending on that decision. | |
3598 | // | |
3599 | // Note that the first case is a generalization fo the second, in that | |
3600 | // we could theoretically search through line records for matching | |
3601 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
3602 | // are already cached elsewhere, so we can do an "index scan" as an | |
3603 | // optimization. | |
7e1279ea | 3604 | |
bd2b1e68 | 3605 | static void |
4cd232e4 | 3606 | query_statement (string const & func, |
20e4a32c | 3607 | char const * file, |
4cd232e4 | 3608 | int line, |
bcc12710 | 3609 | Dwarf_Die *scope_die, |
20e4a32c | 3610 | Dwarf_Addr stmt_addr, |
4cd232e4 | 3611 | dwarf_query * q) |
bd2b1e68 | 3612 | { |
39bcd429 FCE |
3613 | try |
3614 | { | |
cee35f73 | 3615 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 3616 | line, scope_die, stmt_addr); |
39bcd429 FCE |
3617 | } |
3618 | catch (const semantic_error& e) | |
3619 | { | |
3620 | q->sess.print_error (e); | |
3621 | } | |
bd2b1e68 GH |
3622 | } |
3623 | ||
7e1279ea | 3624 | static void |
3e961ba6 | 3625 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
3626 | dwarf_query * q) |
3627 | { | |
b6581717 | 3628 | try |
7e1279ea | 3629 | { |
b6581717 GH |
3630 | if (q->has_return) |
3631 | { | |
3632 | throw semantic_error ("cannot probe .return of inline function '" + ii.name + "'"); | |
3633 | } | |
3634 | else | |
3635 | { | |
b0ee93c4 | 3636 | if (q->sess.verbose>2) |
20e4a32c | 3637 | clog << "querying entrypc " |
3e961ba6 | 3638 | << hex << ii.entrypc << dec |
db22e55f | 3639 | << " of instance of inline '" << ii.name << "'\n"; |
20e4a32c | 3640 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
3e961ba6 | 3641 | &ii.die, ii.entrypc, q); |
b6581717 | 3642 | } |
7e1279ea | 3643 | } |
b6581717 | 3644 | catch (semantic_error &e) |
7e1279ea | 3645 | { |
b6581717 | 3646 | q->sess.print_error (e); |
7e1279ea FCE |
3647 | } |
3648 | } | |
3649 | ||
3650 | static void | |
3651 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 3652 | func_info & fi, |
7e1279ea FCE |
3653 | dwarf_query * q) |
3654 | { | |
b6581717 | 3655 | try |
7e1279ea | 3656 | { |
b6581717 GH |
3657 | if (q->has_return) |
3658 | { | |
3659 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
3660 | // kretprobe based on the entrypc in this case. | |
20e4a32c | 3661 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
3662 | &fi.die, entrypc, q); |
3663 | } | |
3664 | else | |
3665 | { | |
35dc8b04 | 3666 | if (fi.prologue_end != 0) |
44f75386 | 3667 | { |
44f75386 FCE |
3668 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
3669 | &fi.die, fi.prologue_end, q); | |
3670 | } | |
3671 | else | |
3672 | { | |
3673 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
3674 | &fi.die, entrypc, q); | |
3675 | } | |
b6581717 | 3676 | } |
7e1279ea | 3677 | } |
b6581717 | 3678 | catch (semantic_error &e) |
7e1279ea | 3679 | { |
b6581717 | 3680 | q->sess.print_error (e); |
7e1279ea FCE |
3681 | } |
3682 | } | |
3683 | ||
3684 | ||
bd4b874d SC |
3685 | static void |
3686 | query_srcfile_label (const dwarf_line_t& line, void * arg) | |
3687 | { | |
3688 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
3689 | ||
3690 | Dwarf_Addr addr = line.addr(); | |
3691 | ||
3692 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
3693 | i != q->filtered_functions.end(); ++i) | |
3694 | if (q->dw.die_has_pc (i->die, addr)) | |
3695 | q->dw.iterate_over_labels (&i->die, q, query_statement); | |
3696 | } | |
3697 | ||
7e1279ea | 3698 | static void |
86bf665e | 3699 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
3700 | { |
3701 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
3702 | ||
86bf665e | 3703 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 3704 | |
86bf665e | 3705 | int lineno = line.lineno(); |
847bf07f | 3706 | |
86bf665e | 3707 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
3708 | i != q->filtered_functions.end(); ++i) |
3709 | { | |
3e961ba6 | 3710 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 3711 | { |
b0ee93c4 | 3712 | if (q->sess.verbose>3) |
db22e55f | 3713 | clog << "function DIE lands on srcfile\n"; |
4cd232e4 | 3714 | if (q->has_statement_str) |
3e961ba6 | 3715 | query_statement (i->name, i->decl_file, |
847bf07f | 3716 | lineno, // NB: not q->line ! |
3e961ba6 | 3717 | &(i->die), addr, q); |
4cd232e4 | 3718 | else |
3e961ba6 | 3719 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 3720 | } |
20e4a32c RM |
3721 | } |
3722 | ||
86bf665e | 3723 | for (inline_instance_map_t::iterator i |
897820ca GH |
3724 | = q->filtered_inlines.begin(); |
3725 | i != q->filtered_inlines.end(); ++i) | |
3726 | { | |
3e961ba6 | 3727 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 3728 | { |
b0ee93c4 | 3729 | if (q->sess.verbose>3) |
db22e55f | 3730 | clog << "inline instance DIE lands on srcfile\n"; |
897820ca | 3731 | if (q->has_statement_str) |
3e961ba6 JB |
3732 | query_statement (i->name, i->decl_file, |
3733 | q->line[0], &(i->die), addr, q); | |
897820ca | 3734 | else |
3e961ba6 | 3735 | query_inline_instance_info (*i, q); |
897820ca | 3736 | } |
20e4a32c | 3737 | } |
7e1279ea FCE |
3738 | } |
3739 | ||
3740 | ||
4fa7b22b | 3741 | static int |
7e1279ea | 3742 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
3743 | { |
3744 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
7e1279ea | 3745 | assert (!q->has_statement_num); |
bd2b1e68 | 3746 | |
39bcd429 | 3747 | try |
7a053d3b | 3748 | { |
b0ee93c4 | 3749 | if (q->sess.verbose>2) |
db22e55f | 3750 | clog << "examining inline instance of " << q->dw.function_name << "\n"; |
7e1279ea | 3751 | |
4baf0e53 | 3752 | if ((q->has_function_str && ! q->has_call) |
b8da0ad1 | 3753 | || q->has_statement_str) |
7e1279ea | 3754 | { |
b0ee93c4 | 3755 | if (q->sess.verbose>2) |
db22e55f FCE |
3756 | clog << "selected inline instance of " << q->dw.function_name |
3757 | << "\n"; | |
7e1279ea FCE |
3758 | |
3759 | Dwarf_Addr entrypc; | |
3760 | if (q->dw.die_entrypc (die, &entrypc)) | |
3761 | { | |
3762 | inline_instance_info inl; | |
3763 | inl.die = *die; | |
3764 | inl.name = q->dw.function_name; | |
3e961ba6 | 3765 | inl.entrypc = entrypc; |
4cd232e4 GH |
3766 | q->dw.function_file (&inl.decl_file); |
3767 | q->dw.function_line (&inl.decl_line); | |
3e961ba6 | 3768 | q->filtered_inlines.push_back(inl); |
7e1279ea FCE |
3769 | } |
3770 | } | |
3771 | return DWARF_CB_OK; | |
3772 | } | |
3773 | catch (const semantic_error& e) | |
3774 | { | |
3775 | q->sess.print_error (e); | |
3776 | return DWARF_CB_ABORT; | |
3777 | } | |
3778 | } | |
bb788f9f | 3779 | |
7e1279ea | 3780 | static int |
2da9cedb | 3781 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 3782 | { |
2da9cedb | 3783 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
bb788f9f | 3784 | |
7e1279ea FCE |
3785 | try |
3786 | { | |
7e1279ea FCE |
3787 | q->dw.focus_on_function (func); |
3788 | ||
20e4a32c | 3789 | if (q->dw.func_is_inline () |
b8da0ad1 FCE |
3790 | && (! q->has_call) && (! q->has_return) |
3791 | && (((q->has_statement_str || q->has_function_str) | |
3792 | && q->dw.function_name_matches(q->function)))) | |
7e1279ea | 3793 | { |
b0ee93c4 | 3794 | if (q->sess.verbose>3) |
db22e55f FCE |
3795 | clog << "checking instances of inline " << q->dw.function_name |
3796 | << "\n"; | |
2da9cedb | 3797 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
275f40a6 FCE |
3798 | |
3799 | if (q->dw.function_name_final_match (q->function)) | |
3800 | return DWARF_CB_ABORT; | |
7e1279ea | 3801 | } |
396afcee | 3802 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 3803 | { |
7e1279ea FCE |
3804 | bool record_this_function = false; |
3805 | ||
3806 | if ((q->has_statement_str || q->has_function_str) | |
3807 | && q->dw.function_name_matches(q->function)) | |
3808 | { | |
3809 | record_this_function = true; | |
3810 | } | |
e4c58386 | 3811 | else if (q->has_function_num || q->has_statement_num) |
7e1279ea | 3812 | { |
e4c58386 | 3813 | Dwarf_Addr query_addr = |
9b692b91 SC |
3814 | (q->has_function_num ? q->function_num_val : |
3815 | q->has_statement_num ? q->statement_num_val : | |
3816 | (assert(0) , 0)); | |
7e1279ea FCE |
3817 | Dwarf_Die d; |
3818 | q->dw.function_die (&d); | |
20e4a32c | 3819 | |
86bf665e | 3820 | if (q->dw.die_has_pc (d, query_addr)) |
7e1279ea FCE |
3821 | record_this_function = true; |
3822 | } | |
3823 | ||
3824 | if (record_this_function) | |
3825 | { | |
b0ee93c4 | 3826 | if (q->sess.verbose>2) |
db22e55f | 3827 | clog << "selected function " << q->dw.function_name << "\n"; |
7e1279ea | 3828 | |
e4c58386 FCE |
3829 | func_info func; |
3830 | q->dw.function_die (&func.die); | |
3831 | func.name = q->dw.function_name; | |
3832 | q->dw.function_file (&func.decl_file); | |
3833 | q->dw.function_line (&func.decl_line); | |
3834 | ||
3835 | if (q->has_function_num || q->has_function_str || q->has_statement_str) | |
3836 | { | |
3837 | Dwarf_Addr entrypc; | |
3838 | if (q->dw.function_entrypc (&entrypc)) | |
3e961ba6 JB |
3839 | { |
3840 | func.entrypc = entrypc; | |
3841 | q->filtered_functions.push_back (func); | |
3842 | } | |
e4c58386 | 3843 | else |
552fdd9f JB |
3844 | /* this function just be fully inlined, just ignore it */ |
3845 | return DWARF_CB_OK; | |
e4c58386 FCE |
3846 | } |
3847 | else if (q->has_statement_num) | |
3848 | { | |
3e961ba6 JB |
3849 | func.entrypc = q->statement_num_val; |
3850 | q->filtered_functions.push_back (func); | |
275f40a6 FCE |
3851 | if (q->dw.function_name_final_match (q->function)) |
3852 | return DWARF_CB_ABORT; | |
e4c58386 FCE |
3853 | } |
3854 | else | |
3855 | assert(0); | |
3856 | ||
3857 | if (q->dw.function_name_final_match (q->function)) | |
3858 | return DWARF_CB_ABORT; | |
7e1279ea FCE |
3859 | } |
3860 | } | |
39bcd429 | 3861 | return DWARF_CB_OK; |
bd2b1e68 | 3862 | } |
39bcd429 | 3863 | catch (const semantic_error& e) |
bd2b1e68 | 3864 | { |
39bcd429 FCE |
3865 | q->sess.print_error (e); |
3866 | return DWARF_CB_ABORT; | |
bd2b1e68 | 3867 | } |
bd2b1e68 GH |
3868 | } |
3869 | ||
3870 | static int | |
3871 | query_cu (Dwarf_Die * cudie, void * arg) | |
3872 | { | |
20c6c071 | 3873 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
49abf162 | 3874 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 3875 | |
39bcd429 | 3876 | try |
bd2b1e68 | 3877 | { |
7e1279ea | 3878 | q->dw.focus_on_cu (cudie); |
b5d77020 | 3879 | |
b0ee93c4 | 3880 | if (false && q->sess.verbose>2) |
b5d77020 | 3881 | clog << "focused on CU '" << q->dw.cu_name |
db22e55f | 3882 | << "', in module '" << q->dw.module_name << "'\n"; |
d9b516ca | 3883 | |
e4c58386 | 3884 | if (q->has_statement_str || q->has_statement_num |
54efe513 | 3885 | || q->has_function_str || q->has_function_num) |
7e1279ea FCE |
3886 | { |
3887 | q->filtered_srcfiles.clear(); | |
3888 | q->filtered_functions.clear(); | |
3889 | q->filtered_inlines.clear(); | |
3890 | ||
3891 | // In this path, we find "abstract functions", record | |
3892 | // information about them, and then (depending on lineno | |
3893 | // matching) possibly emit one or more of the function's | |
3894 | // associated addresses. Unfortunately the control of this | |
3895 | // cannot easily be turned inside out. | |
3896 | ||
b8da0ad1 | 3897 | if ((q->has_statement_str || q->has_function_str) |
7e1279ea FCE |
3898 | && (q->spec_type != function_alone)) |
3899 | { | |
3900 | // If we have a pattern string with a filename, we need | |
3901 | // to elaborate the srcfile mask in question first. | |
3902 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
3903 | ||
3904 | // If we have a file pattern and *no* srcfile matches, there's | |
3905 | // no need to look further into this CU, so skip. | |
3906 | if (q->filtered_srcfiles.empty()) | |
3907 | return DWARF_CB_OK; | |
3908 | } | |
86bf665e TM |
3909 | // Verify that a raw address matches the beginning of a |
3910 | // statement. This is a somewhat lame check that the address | |
467bea43 SC |
3911 | // is at the start of an assembly instruction. Mark probes are in the |
3912 | // middle of a macro and thus not strictly at a statement beginning. | |
3913 | if (q->has_statement_num && ! q->has_mark) | |
86bf665e TM |
3914 | { |
3915 | Dwarf_Addr queryaddr = q->statement_num_val; | |
3916 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, queryaddr)); | |
3917 | Dwarf_Addr lineaddr = 0; | |
3918 | if (address_line) | |
3919 | lineaddr = address_line.addr(); | |
3920 | if (!address_line || lineaddr != queryaddr) | |
3921 | { | |
3922 | stringstream msg; | |
3923 | msg << "address 0x" << hex << queryaddr | |
1b1b4ceb | 3924 | << " does not match the beginning of a statement"; |
86bf665e TM |
3925 | throw semantic_error(msg.str()); |
3926 | } | |
3927 | } | |
7e1279ea FCE |
3928 | // Pick up [entrypc, name, DIE] tuples for all the functions |
3929 | // matching the query, and fill in the prologue endings of them | |
3930 | // all in a single pass. | |
2da9cedb JS |
3931 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, |
3932 | q->function, | |
3933 | q->has_statement_num); | |
5f0a03a6 JK |
3934 | if (rc != DWARF_CB_OK) |
3935 | q->query_done = true; | |
44f75386 | 3936 | |
35dc8b04 | 3937 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 |
e4c58386 | 3938 | && !q->has_statement_str && !q->has_statement_num) // PR 2608 |
44f75386 FCE |
3939 | if (! q->filtered_functions.empty()) |
3940 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
7e1279ea | 3941 | |
bd4b874d SC |
3942 | if (q->has_label) |
3943 | { | |
3944 | if (q->line[0] == 0) // No line number specified | |
3945 | q->dw.iterate_over_labels (q->dw.cu, q, query_statement); | |
3946 | else | |
3947 | for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin(); | |
3948 | i != q->filtered_srcfiles.end(); ++i) | |
3949 | q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str, | |
3950 | q->line_type, query_srcfile_label, q); | |
3951 | } | |
3952 | else if ((q->has_statement_str || q->has_function_str) | |
7e1279ea FCE |
3953 | && (q->spec_type == function_file_and_line)) |
3954 | { | |
3955 | // If we have a pattern string with target *line*, we | |
20e4a32c | 3956 | // have to look at lines in all the matched srcfiles. |
7e1279ea FCE |
3957 | for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin(); |
3958 | i != q->filtered_srcfiles.end(); ++i) | |
897820ca | 3959 | q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str, |
0c8b7d37 | 3960 | q->line_type, query_srcfile_line, q); |
7e1279ea FCE |
3961 | } |
3962 | else | |
3963 | { | |
e4c58386 | 3964 | // Otherwise, simply probe all resolved functions. |
86bf665e | 3965 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
e4c58386 | 3966 | i != q->filtered_functions.end(); ++i) |
3e961ba6 | 3967 | query_func_info (i->entrypc, *i, q); |
e4c58386 FCE |
3968 | |
3969 | // And all inline instances (if we're not excluding inlines with ".call") | |
3970 | if (! q->has_call) | |
86bf665e | 3971 | for (inline_instance_map_t::iterator i |
54efe513 | 3972 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) |
3e961ba6 | 3973 | query_inline_instance_info (*i, q); |
7e1279ea FCE |
3974 | } |
3975 | } | |
39bcd429 FCE |
3976 | else |
3977 | { | |
e4c58386 FCE |
3978 | // Before PR 5787, we used to have this: |
3979 | #if 0 | |
7e1279ea FCE |
3980 | // Otherwise we have a statement number, and we can just |
3981 | // query it directly within this module. | |
7e1279ea FCE |
3982 | assert (q->has_statement_num); |
3983 | Dwarf_Addr query_addr = q->statement_num_val; | |
b8da0ad1 | 3984 | query_addr = q->dw.module_address_to_global(query_addr); |
7e1279ea | 3985 | |
bcc12710 | 3986 | query_statement ("", "", -1, NULL, query_addr, q); |
e4c58386 FCE |
3987 | #endif |
3988 | // But now, we traverse CUs/functions even for | |
3989 | // statement_num's, for blacklist sensitivity and $var | |
3990 | // resolution purposes. | |
3991 | ||
3992 | assert (0); // NOTREACHED | |
39bcd429 FCE |
3993 | } |
3994 | return DWARF_CB_OK; | |
bd2b1e68 | 3995 | } |
39bcd429 | 3996 | catch (const semantic_error& e) |
bd2b1e68 | 3997 | { |
39bcd429 FCE |
3998 | q->sess.print_error (e); |
3999 | return DWARF_CB_ABORT; | |
bd2b1e68 | 4000 | } |
bd2b1e68 GH |
4001 | } |
4002 | ||
0ce64fb8 | 4003 | |
91af0778 | 4004 | #if 0 |
1d3a40b6 DS |
4005 | static int |
4006 | query_kernel_module (Dwfl_Module *mod, | |
91af0778 | 4007 | void **, |
1d3a40b6 | 4008 | const char *name, |
b8da0ad1 | 4009 | Dwarf_Addr, |
1d3a40b6 DS |
4010 | void *arg) |
4011 | { | |
4012 | if (TOK_KERNEL == name) | |
4013 | { | |
4014 | Dwfl_Module **m = (Dwfl_Module **)arg; | |
4015 | ||
4016 | *m = mod; | |
4017 | return DWARF_CB_ABORT; | |
4018 | } | |
4019 | return DWARF_CB_OK; | |
4020 | } | |
91af0778 FCE |
4021 | #endif |
4022 | ||
1d3a40b6 | 4023 | |
5f0a03a6 JK |
4024 | static void |
4025 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
4026 | { | |
4027 | // Validate the machine code in this elf file against the | |
4028 | // session machine. This is important, in case the wrong kind | |
4029 | // of debuginfo is being automagically processed by elfutils. | |
4030 | // While we can tell i686 apart from x86-64, unfortunately | |
4031 | // we can't help confusing i586 vs i686 (both EM_386). | |
4032 | ||
4033 | Dwarf_Addr bias; | |
4034 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
4035 | // because dwfl_module_getelf can force costly section relocations | |
4036 | // we don't really need, while either will do for this purpose. | |
4037 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
4038 | ?: dwfl_module_getelf (mod, &bias)); | |
4039 | ||
4040 | GElf_Ehdr ehdr_mem; | |
4041 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 4042 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
5f0a03a6 JK |
4043 | int elf_machine = em->e_machine; |
4044 | const char* debug_filename = ""; | |
4045 | const char* main_filename = ""; | |
4046 | (void) dwfl_module_info (mod, NULL, NULL, | |
4047 | NULL, NULL, NULL, | |
4048 | & main_filename, | |
4049 | & debug_filename); | |
4050 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
4051 | |
4052 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
4053 | string expect_machine2; | |
5f0a03a6 JK |
4054 | |
4055 | switch (elf_machine) | |
4056 | { | |
756c9462 FCE |
4057 | // x86 and ppc are bi-architecture; a 64-bit kernel |
4058 | // can normally run either 32-bit or 64-bit *userspace*. | |
4059 | case EM_386: | |
4060 | expect_machine = "i?86"; | |
4061 | if (! q->has_process) break; // 32-bit kernel/module | |
4062 | /* FALLSTHROUGH */ | |
4063 | case EM_X86_64: | |
4064 | expect_machine2 = "x86_64"; | |
4065 | break; | |
4066 | case EM_PPC: | |
4067 | expect_machine = "ppc"; | |
4068 | if (! q->has_process) break; // 32-bit kernel/module | |
4069 | /* FALLSTHROUGH */ | |
4070 | case EM_PPC64: | |
4071 | expect_machine2 = "ppc64"; | |
4072 | break; | |
5f0a03a6 JK |
4073 | case EM_S390: expect_machine = "s390x"; break; |
4074 | case EM_IA_64: expect_machine = "ia64"; break; | |
4075 | case EM_ARM: expect_machine = "armv*"; break; | |
4076 | // XXX: fill in some more of these | |
4077 | default: expect_machine = "?"; break; | |
4078 | } | |
4079 | ||
4080 | if (! debug_filename) debug_filename = main_filename; | |
4081 | if (! debug_filename) debug_filename = name; | |
4082 | ||
756c9462 FCE |
4083 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
4084 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
4085 | { |
4086 | stringstream msg; | |
756c9462 FCE |
4087 | msg << "ELF machine " << expect_machine << "|" << expect_machine2 |
4088 | << " (code " << elf_machine | |
5f0a03a6 JK |
4089 | << ") mismatch with target " << sess_machine |
4090 | << " in '" << debug_filename << "'"; | |
4091 | throw semantic_error(msg.str ()); | |
4092 | } | |
4093 | ||
4094 | if (q->sess.verbose>2) | |
4095 | clog << "focused on module '" << q->dw.module_name | |
4096 | << " = [0x" << hex << q->dw.module_start | |
4097 | << "-0x" << q->dw.module_end | |
4098 | << ", bias 0x" << q->dw.module_bias << "]" << dec | |
4099 | << " file " << debug_filename | |
756c9462 | 4100 | << " ELF machine " << expect_machine << "|" << expect_machine2 |
5f0a03a6 JK |
4101 | << " (code " << elf_machine << ")" |
4102 | << "\n"; | |
4103 | } | |
1d3a40b6 | 4104 | |
91af0778 FCE |
4105 | |
4106 | ||
4107 | static Dwarf_Addr | |
4108 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
4109 | { | |
4110 | int syments = dwfl_module_getsymtab(m); | |
4111 | assert(syments); | |
4112 | for (int i = 1; i < syments; ++i) | |
4113 | { | |
4114 | GElf_Sym sym; | |
4115 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
4116 | if (name != NULL && strcmp(name, wanted) == 0) | |
4117 | return sym.st_value; | |
4118 | } | |
4119 | ||
4120 | return 0; | |
4121 | } | |
4122 | ||
4123 | ||
4124 | ||
bd2b1e68 | 4125 | static int |
b8da0ad1 | 4126 | query_module (Dwfl_Module *mod, |
91af0778 | 4127 | void **, |
b8da0ad1 | 4128 | const char *name, |
6f4c1275 | 4129 | Dwarf_Addr addr, |
b8da0ad1 | 4130 | void *arg) |
bd2b1e68 | 4131 | { |
91af0778 | 4132 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 4133 | |
39bcd429 | 4134 | try |
e38d6504 | 4135 | { |
91af0778 FCE |
4136 | module_info* mi = q->sess.module_cache->cache[name]; |
4137 | if (mi == 0) | |
4138 | { | |
4139 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
4140 | ||
6f4c1275 FCE |
4141 | mi->mod = mod; |
4142 | mi->addr = addr; | |
91af0778 | 4143 | |
6f4c1275 FCE |
4144 | const char* debug_filename = ""; |
4145 | const char* main_filename = ""; | |
4146 | (void) dwfl_module_info (mod, NULL, NULL, | |
4147 | NULL, NULL, NULL, | |
4148 | & main_filename, | |
4149 | & debug_filename); | |
4150 | ||
4151 | if (q->sess.ignore_vmlinux && name == TOK_KERNEL) | |
91af0778 FCE |
4152 | { |
4153 | // report_kernel() in elfutils found vmlinux, but pretend it didn't. | |
4154 | // Given a non-null path, returning 1 means keep reporting modules. | |
4155 | mi->dwarf_status = info_absent; | |
4156 | } | |
6f4c1275 | 4157 | else if (debug_filename || main_filename) |
91af0778 | 4158 | { |
6f4c1275 FCE |
4159 | mi->elf_path = debug_filename ?: main_filename; |
4160 | } | |
4161 | else if (name == TOK_KERNEL) | |
4162 | { | |
4163 | mi->dwarf_status = info_absent; | |
91af0778 | 4164 | } |
91af0778 FCE |
4165 | } |
4166 | // OK, enough of that module_info caching business. | |
4167 | ||
5f0a03a6 | 4168 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 4169 | |
39bcd429 FCE |
4170 | // If we have enough information in the pattern to skip a module and |
4171 | // the module does not match that information, return early. | |
b8da0ad1 | 4172 | if (!q->dw.module_name_matches(q->module_val)) |
39bcd429 | 4173 | return DWARF_CB_OK; |
0cbbf9d1 FCE |
4174 | |
4175 | // Don't allow module("*kernel*") type expressions to match the | |
4176 | // elfutils module "kernel", which we refer to in the probe | |
4177 | // point syntax exclusively as "kernel.*". | |
4178 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
4179 | return DWARF_CB_OK; | |
b5d77020 | 4180 | |
5f0a03a6 JK |
4181 | if (mod) |
4182 | validate_module_elf(mod, name, q); | |
4183 | else | |
91af0778 FCE |
4184 | assert(q->has_kernel); // and no vmlinux to examine |
4185 | ||
4186 | if (q->sess.verbose>2) | |
4187 | cerr << "focused on module '" << q->dw.module_name << "'\n"; | |
4188 | ||
4189 | ||
4190 | // Collect a few kernel addresses. XXX: these belong better | |
4191 | // to the sess.module_info["kernel"] struct. | |
4192 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 4193 | { |
91af0778 FCE |
4194 | if (! q->sess.sym_kprobes_text_start) |
4195 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
4196 | if (! q->sess.sym_kprobes_text_end) | |
4197 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
4198 | if (! q->sess.sym_stext) | |
4199 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
4200 | } |
4201 | ||
91af0778 | 4202 | // Finally, search the module for matches of the probe point. |
2c384610 | 4203 | q->handle_query_module(); |
bb788f9f | 4204 | |
91af0778 | 4205 | |
b8da0ad1 FCE |
4206 | // If we know that there will be no more matches, abort early. |
4207 | if (q->dw.module_name_final_match(q->module_val)) | |
4208 | return DWARF_CB_ABORT; | |
4209 | else | |
4210 | return DWARF_CB_OK; | |
7a053d3b | 4211 | } |
39bcd429 | 4212 | catch (const semantic_error& e) |
bd2b1e68 | 4213 | { |
39bcd429 FCE |
4214 | q->sess.print_error (e); |
4215 | return DWARF_CB_ABORT; | |
bd2b1e68 | 4216 | } |
bd2b1e68 GH |
4217 | } |
4218 | ||
5f0a03a6 | 4219 | void |
c4ce66a1 | 4220 | dwflpp::query_modules(base_query *q) |
5f0a03a6 | 4221 | { |
91af0778 | 4222 | iterate_over_modules(&query_module, q); |
5f0a03a6 | 4223 | } |
2930abc7 | 4224 | |
35d4ab18 | 4225 | |
de688825 | 4226 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 4227 | { |
77de5e9e | 4228 | dwarf_query & q; |
bcc12710 | 4229 | Dwarf_Die *scope_die; |
77de5e9e | 4230 | Dwarf_Addr addr; |
8c819921 | 4231 | block *add_block; |
8fc05e57 | 4232 | probe *add_probe; |
cd5b28b2 | 4233 | std::map<std::string, symbol *> return_ts_map; |
b95e2b79 | 4234 | bool visited; |
77de5e9e | 4235 | |
de688825 | 4236 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
b95e2b79 | 4237 | q(q), scope_die(sd), addr(a), add_block(NULL), add_probe(NULL), visited(false) {} |
d7f3e0c5 | 4238 | void visit_target_symbol (target_symbol* e); |
c24447be | 4239 | void visit_cast_op (cast_op* e); |
77de5e9e GH |
4240 | }; |
4241 | ||
4242 | ||
de688825 | 4243 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 4244 | |
77de5e9e | 4245 | void |
de688825 | 4246 | var_expanding_visitor::visit_assignment (assignment* e) |
77de5e9e | 4247 | { |
e57b735a GH |
4248 | // Our job would normally be to require() the left and right sides |
4249 | // into a new assignment. What we're doing is slightly trickier: | |
4250 | // we're pushing a functioncall** onto a stack, and if our left | |
4251 | // child sets the functioncall* for that value, we're going to | |
4252 | // assume our left child was a target symbol -- transformed into a | |
4253 | // set_target_foo(value) call, and it wants to take our right child | |
4254 | // as the argument "value". | |
4255 | // | |
4256 | // This is why some people claim that languages with | |
4257 | // constructor-decomposing case expressions have a leg up on | |
4258 | // visitors. | |
4259 | ||
4260 | functioncall *fcall = NULL; | |
4261 | expression *new_left, *new_right; | |
d9b516ca | 4262 | |
e57b735a | 4263 | target_symbol_setter_functioncalls.push (&fcall); |
4ed05b15 | 4264 | new_left = require (e->left); |
e57b735a | 4265 | target_symbol_setter_functioncalls.pop (); |
4ed05b15 | 4266 | new_right = require (e->right); |
e57b735a GH |
4267 | |
4268 | if (fcall != NULL) | |
77de5e9e | 4269 | { |
e57b735a GH |
4270 | // Our left child is informing us that it was a target variable |
4271 | // and it has been replaced with a set_target_foo() function | |
4272 | // call; we are going to provide that function call -- with the | |
4273 | // right child spliced in as sole argument -- in place of | |
de688825 | 4274 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a GH |
4275 | |
4276 | // FIXME: for the time being, we only support plan $foo = bar, | |
4277 | // not += or any other op= variant. This is fixable, but a bit | |
4278 | // ugly. | |
4279 | if (e->op != "=") | |
4280 | throw semantic_error ("Operator-assign expressions on target " | |
4281 | "variables not implemented", e->tok); | |
4282 | ||
4283 | assert (new_left == fcall); | |
4284 | fcall->args.push_back (new_right); | |
4ed05b15 | 4285 | provide (fcall); |
77de5e9e | 4286 | } |
e57b735a GH |
4287 | else |
4288 | { | |
de688825 JS |
4289 | e->left = new_left; |
4290 | e->right = new_right; | |
4291 | provide (e); | |
e57b735a GH |
4292 | } |
4293 | } | |
d9b516ca | 4294 | |
d7f3e0c5 | 4295 | |
e57b735a | 4296 | void |
de688825 | 4297 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
e57b735a GH |
4298 | { |
4299 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
b95e2b79 | 4300 | visited = true; |
e57b735a | 4301 | |
cf2a1f85 DS |
4302 | bool lvalue = is_active_lvalue(e); |
4303 | if (lvalue && !q.sess.guru_mode) | |
4304 | throw semantic_error("write to target variable not permitted", e->tok); | |
4305 | ||
a43ba433 FCE |
4306 | // See if we need to generate a new probe to save/access function |
4307 | // parameters from a return probe. PR 1382. | |
4308 | if (q.has_return | |
4309 | && e->base_name != "$return" // not the special return-value variable handled below | |
4310 | && e->base_name != "$$return") // nor the other special variable handled below | |
85ecf79a DS |
4311 | { |
4312 | if (lvalue) | |
4313 | throw semantic_error("write to target variable not permitted in .return probes", e->tok); | |
4314 | ||
cd5b28b2 DS |
4315 | // Get the full name of the target symbol. |
4316 | stringstream ts_name_stream; | |
4317 | e->print(ts_name_stream); | |
4318 | string ts_name = ts_name_stream.str(); | |
4319 | ||
4320 | // Check and make sure we haven't already seen this target | |
4321 | // variable in this return probe. If we have, just return our | |
4322 | // last replacement. | |
4323 | map<string, symbol *>::iterator i = return_ts_map.find(ts_name); | |
4324 | if (i != return_ts_map.end()) | |
4325 | { | |
4ed05b15 | 4326 | provide (i->second); |
cd5b28b2 DS |
4327 | return; |
4328 | } | |
4329 | ||
85ecf79a DS |
4330 | // We've got to do several things here to handle target |
4331 | // variables in return probes. | |
4332 | ||
5e600bd6 DS |
4333 | // (1) Synthesize two global arrays. One is the cache of the |
4334 | // target variable and the other contains a thread specific | |
4335 | // nesting level counter. The arrays will look like | |
4336 | // this: | |
85ecf79a DS |
4337 | // |
4338 | // _dwarf_tvar_{name}_{num} | |
85ecf79a DS |
4339 | // _dwarf_tvar_{name}_{num}_ctr |
4340 | ||
4341 | string aname = (string("_dwarf_tvar_") | |
4342 | + e->base_name.substr(1) | |
4343 | + "_" + lex_cast<string>(tick++)); | |
4344 | vardecl* vd = new vardecl; | |
4345 | vd->name = aname; | |
4346 | vd->tok = e->tok; | |
4347 | q.sess.globals.push_back (vd); | |
4348 | ||
4349 | string ctrname = aname + "_ctr"; | |
4350 | vd = new vardecl; | |
4351 | vd->name = ctrname; | |
4352 | vd->tok = e->tok; | |
4353 | q.sess.globals.push_back (vd); | |
4354 | ||
8c819921 DS |
4355 | // (2) Create a new code block we're going to insert at the |
4356 | // beginning of this probe to get the cached value into a | |
4357 | // temporary variable. We'll replace the target variable | |
4358 | // reference with the temporary variable reference. The code | |
4359 | // will look like this: | |
4360 | // | |
4361 | // _dwarf_tvar_tid = tid() | |
4362 | // _dwarf_tvar_{name}_{num}_tmp | |
4363 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4364 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4365 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4366 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
46392da3 DS |
4367 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) |
4368 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
8c819921 DS |
4369 | |
4370 | // (2a) Synthesize the tid temporary expression, which will look | |
85ecf79a DS |
4371 | // like this: |
4372 | // | |
8c819921 DS |
4373 | // _dwarf_tvar_tid = tid() |
4374 | symbol* tidsym = new symbol; | |
4375 | tidsym->name = string("_dwarf_tvar_tid"); | |
4376 | tidsym->tok = e->tok; | |
85ecf79a | 4377 | |
8c819921 DS |
4378 | if (add_block == NULL) |
4379 | { | |
8fc05e57 DS |
4380 | add_block = new block; |
4381 | add_block->tok = e->tok; | |
4382 | ||
4383 | // Synthesize a functioncall to grab the thread id. | |
4384 | functioncall* fc = new functioncall; | |
4385 | fc->tok = e->tok; | |
4386 | fc->function = string("tid"); | |
4387 | ||
4388 | // Assign the tid to '_dwarf_tvar_tid'. | |
4389 | assignment* a = new assignment; | |
4390 | a->tok = e->tok; | |
4391 | a->op = "="; | |
4392 | a->left = tidsym; | |
4393 | a->right = fc; | |
4394 | ||
4395 | expr_statement* es = new expr_statement; | |
4396 | es->tok = e->tok; | |
4397 | es->value = a; | |
4398 | add_block->statements.push_back (es); | |
8c819921 DS |
4399 | } |
4400 | ||
4401 | // (2b) Synthesize an array reference and assign it to a | |
4402 | // temporary variable (that we'll use as replacement for the | |
4403 | // target variable reference). It will look like this: | |
4404 | // | |
4405 | // _dwarf_tvar_{name}_{num}_tmp | |
4406 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4407 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4408 | ||
8fc05e57 DS |
4409 | arrayindex* ai_tvar_base = new arrayindex; |
4410 | ai_tvar_base->tok = e->tok; | |
85ecf79a DS |
4411 | |
4412 | symbol* sym = new symbol; | |
4413 | sym->name = aname; | |
4414 | sym->tok = e->tok; | |
8fc05e57 | 4415 | ai_tvar_base->base = sym; |
8c819921 | 4416 | |
8fc05e57 | 4417 | ai_tvar_base->indexes.push_back(tidsym); |
85ecf79a | 4418 | |
8c819921 DS |
4419 | // We need to create a copy of the array index in its current |
4420 | // state so we can have 2 variants of it (the original and one | |
4421 | // that post-decrements the second index). | |
8fc05e57 DS |
4422 | arrayindex* ai_tvar = new arrayindex; |
4423 | arrayindex* ai_tvar_postdec = new arrayindex; | |
4424 | *ai_tvar = *ai_tvar_base; | |
4425 | *ai_tvar_postdec = *ai_tvar_base; | |
85ecf79a | 4426 | |
8c819921 DS |
4427 | // Synthesize the |
4428 | // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the | |
85ecf79a | 4429 | // second index into the array. |
8c819921 DS |
4430 | arrayindex* ai_ctr = new arrayindex; |
4431 | ai_ctr->tok = e->tok; | |
5e600bd6 | 4432 | |
85ecf79a DS |
4433 | sym = new symbol; |
4434 | sym->name = ctrname; | |
4435 | sym->tok = e->tok; | |
8c819921 DS |
4436 | ai_ctr->base = sym; |
4437 | ai_ctr->indexes.push_back(tidsym); | |
4438 | ai_tvar->indexes.push_back(ai_ctr); | |
4439 | ||
4440 | symbol* tmpsym = new symbol; | |
4441 | tmpsym->name = aname + "_tmp"; | |
4442 | tmpsym->tok = e->tok; | |
4443 | ||
4444 | assignment* a = new assignment; | |
4445 | a->tok = e->tok; | |
4446 | a->op = "="; | |
4447 | a->left = tmpsym; | |
4448 | a->right = ai_tvar; | |
4449 | ||
4450 | expr_statement* es = new expr_statement; | |
4451 | es->tok = e->tok; | |
4452 | es->value = a; | |
4453 | ||
4454 | add_block->statements.push_back (es); | |
4455 | ||
4456 | // (2c) Add a post-decrement to the second array index and | |
4457 | // delete the array value. It will look like this: | |
4458 | // | |
4459 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4460 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
5e600bd6 | 4461 | |
85ecf79a DS |
4462 | post_crement* pc = new post_crement; |
4463 | pc->tok = e->tok; | |
4464 | pc->op = "--"; | |
8c819921 | 4465 | pc->operand = ai_ctr; |
8fc05e57 | 4466 | ai_tvar_postdec->indexes.push_back(pc); |
8c819921 DS |
4467 | |
4468 | delete_statement* ds = new delete_statement; | |
4469 | ds->tok = e->tok; | |
8fc05e57 | 4470 | ds->value = ai_tvar_postdec; |
8c819921 DS |
4471 | |
4472 | add_block->statements.push_back (ds); | |
85ecf79a | 4473 | |
46392da3 DS |
4474 | // (2d) Delete the counter value if it is 0. It will look like |
4475 | // this: | |
4476 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
4477 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
4baf0e53 | 4478 | |
46392da3 DS |
4479 | ds = new delete_statement; |
4480 | ds->tok = e->tok; | |
4481 | ds->value = ai_ctr; | |
4482 | ||
4483 | unary_expression *ue = new unary_expression; | |
4484 | ue->tok = e->tok; | |
4485 | ue->op = "!"; | |
4486 | ue->operand = ai_ctr; | |
4487 | ||
4488 | if_statement *ifs = new if_statement; | |
4489 | ifs->tok = e->tok; | |
4490 | ifs->condition = ue; | |
4491 | ifs->thenblock = ds; | |
4492 | ifs->elseblock = NULL; | |
4baf0e53 | 4493 | |
46392da3 DS |
4494 | add_block->statements.push_back (ifs); |
4495 | ||
85ecf79a | 4496 | // (3) We need an entry probe that saves the value for us in the |
8fc05e57 DS |
4497 | // global array we created. Create the entry probe, which will |
4498 | // look like this: | |
85ecf79a | 4499 | // |
85ecf79a | 4500 | // probe kernel.function("{function}") { |
8fc05e57 DS |
4501 | // _dwarf_tvar_tid = tid() |
4502 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4503 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
85ecf79a DS |
4504 | // = ${param} |
4505 | // } | |
4506 | ||
8fc05e57 | 4507 | if (add_probe == NULL) |
85ecf79a | 4508 | { |
8fc05e57 DS |
4509 | add_probe = new probe; |
4510 | add_probe->tok = e->tok; | |
4511 | ||
4512 | // We need the name of the current probe point, minus the | |
4513 | // ".return" (or anything after it, such as ".maxactive(N)"). | |
4514 | // Create a new probe point, copying all the components, | |
4515 | // stopping when we see the ".return" component. | |
4516 | probe_point* pp = new probe_point; | |
4517 | for (unsigned c = 0; c < q.base_loc->components.size(); c++) | |
4518 | { | |
4519 | if (q.base_loc->components[c]->functor == "return") | |
4520 | break; | |
4521 | else | |
4522 | pp->components.push_back(q.base_loc->components[c]); | |
4523 | } | |
4524 | pp->tok = e->tok; | |
4525 | pp->optional = q.base_loc->optional; | |
4526 | add_probe->locations.push_back(pp); | |
4527 | ||
4528 | add_probe->body = new block; | |
4529 | add_probe->body->tok = e->tok; | |
4530 | ||
4531 | // Synthesize a functioncall to grab the thread id. | |
4532 | functioncall* fc = new functioncall; | |
4533 | fc->tok = e->tok; | |
4534 | fc->function = string("tid"); | |
4535 | ||
4536 | // Assign the tid to '_dwarf_tvar_tid'. | |
4537 | assignment* a = new assignment; | |
4538 | a->tok = e->tok; | |
4539 | a->op = "="; | |
4540 | a->left = tidsym; | |
4541 | a->right = fc; | |
4542 | ||
4543 | expr_statement* es = new expr_statement; | |
4544 | es->tok = e->tok; | |
4545 | es->value = a; | |
ba6f838d | 4546 | add_probe->body = new block(add_probe->body, es); |
8fc05e57 DS |
4547 | |
4548 | vardecl* vd = new vardecl; | |
4549 | vd->tok = e->tok; | |
4550 | vd->name = tidsym->name; | |
4551 | vd->type = pe_long; | |
4552 | vd->set_arity(0); | |
4553 | add_probe->locals.push_back(vd); | |
85ecf79a | 4554 | } |
8fc05e57 DS |
4555 | |
4556 | // Save the value, like this: | |
4557 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4558 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4559 | // = ${param} | |
4560 | arrayindex* ai_tvar_preinc = new arrayindex; | |
4561 | *ai_tvar_preinc = *ai_tvar_base; | |
4baf0e53 | 4562 | |
8fc05e57 DS |
4563 | pre_crement* preinc = new pre_crement; |
4564 | preinc->tok = e->tok; | |
4565 | preinc->op = "++"; | |
4566 | preinc->operand = ai_ctr; | |
4567 | ai_tvar_preinc->indexes.push_back(preinc); | |
4baf0e53 | 4568 | |
8fc05e57 DS |
4569 | a = new assignment; |
4570 | a->tok = e->tok; | |
4571 | a->op = "="; | |
4572 | a->left = ai_tvar_preinc; | |
4573 | a->right = e; | |
4574 | ||
4575 | es = new expr_statement; | |
4576 | es->tok = e->tok; | |
4577 | es->value = a; | |
4578 | ||
ba6f838d | 4579 | add_probe->body = new block(add_probe->body, es); |
85ecf79a | 4580 | |
8c819921 DS |
4581 | // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to |
4582 | // our parent so it can be used as a substitute for the target | |
4583 | // symbol. | |
4ed05b15 | 4584 | provide (tmpsym); |
cd5b28b2 DS |
4585 | |
4586 | // (5) Remember this replacement since we might be able to reuse | |
4587 | // it later if the same return probe references this target | |
4588 | // symbol again. | |
4589 | return_ts_map[ts_name] = tmpsym; | |
85ecf79a DS |
4590 | return; |
4591 | } | |
cf2a1f85 | 4592 | |
2cb3fe26 SC |
4593 | if (e->base_name == "$$vars" |
4594 | || e->base_name == "$$parms" | |
a43ba433 FCE |
4595 | || e->base_name == "$$locals" |
4596 | || (q.has_return && (e->base_name == "$$return"))) | |
2cb3fe26 SC |
4597 | { |
4598 | Dwarf_Die *scopes; | |
4599 | if (dwarf_getscopes_die (scope_die, &scopes) == 0) | |
4600 | return; | |
41c262f3 | 4601 | |
2cb3fe26 SC |
4602 | target_symbol *tsym = new target_symbol; |
4603 | print_format* pf = new print_format; | |
4604 | ||
4605 | // Convert $$parms to sprintf of a list of parms and active local vars | |
4606 | // which we recursively evaluate | |
a43ba433 FCE |
4607 | |
4608 | // NB: we synthesize a new token here rather than reusing | |
4609 | // e->tok, because print_format::print likes to use | |
4610 | // its tok->content. | |
4611 | token* pf_tok = new token; | |
4612 | pf_tok->location = e->tok->location; | |
4613 | pf_tok->type = tok_identifier; | |
4614 | pf_tok->content = "sprint"; | |
4615 | ||
4616 | pf->tok = pf_tok; | |
2cb3fe26 SC |
4617 | pf->print_to_stream = false; |
4618 | pf->print_with_format = true; | |
4619 | pf->print_with_delim = false; | |
4620 | pf->print_with_newline = false; | |
4621 | pf->print_char = false; | |
4622 | ||
a43ba433 FCE |
4623 | if (q.has_return && (e->base_name == "$$return")) |
4624 | { | |
4625 | tsym->tok = e->tok; | |
4626 | tsym->base_name = "$return"; | |
41c262f3 | 4627 | |
a43ba433 FCE |
4628 | // Ignore any variable that isn't accessible. |
4629 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
4630 | expression *texp = tsym; |
4631 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
4632 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
4633 | { | |
2cb3fe26 | 4634 | |
a43ba433 FCE |
4635 | } |
4636 | else | |
4637 | { | |
fd574705 | 4638 | pf->raw_components += "return"; |
a43ba433 | 4639 | pf->raw_components += "=%#x "; |
4ed05b15 | 4640 | pf->args.push_back(texp); |
a43ba433 FCE |
4641 | } |
4642 | } | |
4643 | else | |
4644 | { | |
4645 | // non-.return probe: support $$parms, $$vars, $$locals | |
4646 | Dwarf_Die result; | |
4647 | if (dwarf_child (&scopes[0], &result) == 0) | |
4648 | do | |
00cf3709 | 4649 | { |
a43ba433 FCE |
4650 | switch (dwarf_tag (&result)) |
4651 | { | |
4652 | case DW_TAG_variable: | |
4653 | if (e->base_name == "$$parms") | |
4654 | continue; | |
4655 | break; | |
4656 | case DW_TAG_formal_parameter: | |
4657 | if (e->base_name == "$$locals") | |
4658 | continue; | |
4659 | break; | |
41c262f3 | 4660 | |
a43ba433 FCE |
4661 | default: |
4662 | continue; | |
4663 | } | |
41c262f3 | 4664 | |
a43ba433 | 4665 | const char *diename = dwarf_diename (&result); |
f76427a2 FCE |
4666 | if (! diename) continue; |
4667 | ||
a43ba433 FCE |
4668 | tsym->tok = e->tok; |
4669 | tsym->base_name = "$"; | |
4670 | tsym->base_name += diename; | |
41c262f3 | 4671 | |
a43ba433 FCE |
4672 | // Ignore any variable that isn't accessible. |
4673 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
4674 | expression *texp = tsym; |
4675 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
4676 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
4677 | { | |
12b44fb3 FCE |
4678 | if (q.sess.verbose>2) |
4679 | { | |
4680 | for (semantic_error *c = tsym->saved_conversion_error; | |
4681 | c != 0; | |
4682 | c = c->chain) { | |
4683 | clog << "variable location problem: " << c->what() << endl; | |
4684 | } | |
4685 | } | |
4686 | ||
a43ba433 FCE |
4687 | pf->raw_components += diename; |
4688 | pf->raw_components += "=? "; | |
4689 | } | |
4690 | else | |
4691 | { | |
4692 | pf->raw_components += diename; | |
4693 | pf->raw_components += "=%#x "; | |
4ed05b15 | 4694 | pf->args.push_back(texp); |
a43ba433 | 4695 | } |
00cf3709 | 4696 | } |
a43ba433 FCE |
4697 | while (dwarf_siblingof (&result, &result) == 0); |
4698 | } | |
2cb3fe26 | 4699 | |
2cb3fe26 | 4700 | pf->components = print_format::string_to_components(pf->raw_components); |
4ed05b15 | 4701 | provide (pf); |
2cb3fe26 SC |
4702 | |
4703 | return; | |
4704 | } | |
4705 | ||
e57b735a | 4706 | // Synthesize a function. |
d7f3e0c5 | 4707 | functiondecl *fdecl = new functiondecl; |
7b99c7d3 | 4708 | fdecl->tok = e->tok; |
d7f3e0c5 | 4709 | embeddedcode *ec = new embeddedcode; |
5e309481 | 4710 | ec->tok = e->tok; |
e8fbc5e8 | 4711 | |
1b07c728 | 4712 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
20e4a32c | 4713 | + "_" + e->base_name.substr(1) |
e57b735a GH |
4714 | + "_" + lex_cast<string>(tick++)); |
4715 | ||
66d284f4 | 4716 | try |
e57b735a | 4717 | { |
a43ba433 | 4718 | if (q.has_return && (e->base_name == "$return")) |
e19fda4e DS |
4719 | { |
4720 | ec->code = q.dw.literal_stmt_for_return (scope_die, | |
4721 | addr, | |
b4c34c26 | 4722 | e, |
e19fda4e DS |
4723 | lvalue, |
4724 | fdecl->type); | |
4725 | } | |
4726 | else | |
4727 | { | |
4728 | ec->code = q.dw.literal_stmt_for_local (scope_die, | |
4729 | addr, | |
4730 | e->base_name.substr(1), | |
b4c34c26 | 4731 | e, |
e19fda4e DS |
4732 | lvalue, |
4733 | fdecl->type); | |
4734 | } | |
4735 | ||
1b07c728 FCE |
4736 | if (! lvalue) |
4737 | ec->code += "/* pure */"; | |
66d284f4 FCE |
4738 | } |
4739 | catch (const semantic_error& er) | |
4740 | { | |
3bd0d4df RA |
4741 | if (!q.sess.skip_badvars) |
4742 | { | |
4743 | // We suppress this error message, and pass the unresolved | |
4744 | // target_symbol to the next pass. We hope that this value ends | |
4745 | // up not being referenced after all, so it can be optimized out | |
4746 | // quietly. | |
4747 | provide (e); | |
4748 | semantic_error* saveme = new semantic_error (er); // copy it | |
4749 | saveme->tok1 = e->tok; // XXX: token not passed to q.dw code generation routines | |
4750 | // NB: we can have multiple errors, since a $target variable | |
4751 | // may be expanded in several different contexts: | |
4752 | // function ("*") { $var } | |
4753 | saveme->chain = e->saved_conversion_error; | |
4754 | e->saved_conversion_error = saveme; | |
4755 | } | |
4756 | else | |
4757 | { | |
4758 | // Upon user request for ignoring context, the symbol is replaced | |
4759 | // with a literal 0 and a warning message displayed | |
4760 | literal_number* ln_zero = new literal_number (0); | |
4761 | ln_zero->tok = e->tok; | |
4762 | provide (ln_zero); | |
8c1b9e27 FCE |
4763 | if (!q.sess.suppress_warnings) |
4764 | q.sess.print_warning ("Bad $context variable being substituted with literal 0", | |
4765 | e->tok); | |
3bd0d4df | 4766 | } |
1cde5ba5 JS |
4767 | delete fdecl; |
4768 | delete ec; | |
cbfbbf69 | 4769 | return; |
66d284f4 | 4770 | } |
e57b735a | 4771 | |
d7f3e0c5 GH |
4772 | fdecl->name = fname; |
4773 | fdecl->body = ec; | |
e57b735a GH |
4774 | if (lvalue) |
4775 | { | |
4776 | // Modify the fdecl so it carries a single pe_long formal | |
4777 | // argument called "value". | |
4778 | ||
4779 | // FIXME: For the time being we only support setting target | |
4780 | // variables which have base types; these are 'pe_long' in | |
4781 | // stap's type vocabulary. Strings and pointers might be | |
4782 | // reasonable, some day, but not today. | |
4783 | ||
4784 | vardecl *v = new vardecl; | |
4785 | v->type = pe_long; | |
4786 | v->name = "value"; | |
4787 | v->tok = e->tok; | |
4788 | fdecl->formal_args.push_back(v); | |
4789 | } | |
f76427a2 | 4790 | q.sess.functions[fdecl->name]=fdecl; |
d9b516ca | 4791 | |
e57b735a | 4792 | // Synthesize a functioncall. |
d7f3e0c5 GH |
4793 | functioncall* n = new functioncall; |
4794 | n->tok = e->tok; | |
4795 | n->function = fname; | |
35d4ab18 | 4796 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session |
e57b735a GH |
4797 | |
4798 | if (lvalue) | |
4799 | { | |
4800 | // Provide the functioncall to our parent, so that it can be | |
4801 | // used to substitute for the assignment node immediately above | |
4802 | // us. | |
4803 | assert(!target_symbol_setter_functioncalls.empty()); | |
4804 | *(target_symbol_setter_functioncalls.top()) = n; | |
4805 | } | |
4806 | ||
4ed05b15 | 4807 | provide (n); |
77de5e9e GH |
4808 | } |
4809 | ||
4810 | ||
c24447be JS |
4811 | void |
4812 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
4813 | { | |
4814 | // Fill in our current module context if needed | |
4815 | if (e->module.empty()) | |
4816 | e->module = q.dw.module_name; | |
4817 | ||
4818 | var_expanding_visitor::visit_cast_op(e); | |
4819 | } | |
4820 | ||
4821 | ||
c4ce66a1 JS |
4822 | struct dwarf_cast_query : public base_query |
4823 | { | |
4824 | const cast_op& e; | |
4825 | const bool lvalue; | |
c4ce66a1 | 4826 | |
abb41d92 JS |
4827 | exp_type& pe_type; |
4828 | string& code; | |
c4ce66a1 | 4829 | |
abb41d92 JS |
4830 | dwarf_cast_query(dwflpp& dw, const string& module, const cast_op& e, |
4831 | bool lvalue, exp_type& pe_type, string& code): | |
4832 | base_query(dw, module), e(e), lvalue(lvalue), | |
4833 | pe_type(pe_type), code(code) {} | |
c4ce66a1 JS |
4834 | |
4835 | void handle_query_module(); | |
4836 | int handle_query_cu(Dwarf_Die * cudie); | |
4837 | ||
4838 | static int cast_query_cu (Dwarf_Die * cudie, void * arg); | |
4839 | }; | |
4840 | ||
4841 | ||
c4ce66a1 JS |
4842 | void |
4843 | dwarf_cast_query::handle_query_module() | |
4844 | { | |
abb41d92 | 4845 | if (!code.empty()) |
c4ce66a1 JS |
4846 | return; |
4847 | ||
4848 | // look for the type in each CU | |
4849 | dw.iterate_over_cus(cast_query_cu, this); | |
4850 | } | |
4851 | ||
4852 | ||
4853 | int | |
4854 | dwarf_cast_query::handle_query_cu(Dwarf_Die * cudie) | |
4855 | { | |
abb41d92 | 4856 | if (!code.empty()) |
c4ce66a1 JS |
4857 | return DWARF_CB_ABORT; |
4858 | ||
4859 | dw.focus_on_cu (cudie); | |
4860 | Dwarf_Die* type_die = dw.declaration_resolve(e.type.c_str()); | |
4861 | if (type_die) | |
4862 | { | |
4863 | try | |
4864 | { | |
b4c34c26 | 4865 | code = dw.literal_stmt_for_pointer (type_die, &e, |
c4ce66a1 JS |
4866 | lvalue, pe_type); |
4867 | } | |
4868 | catch (const semantic_error& e) | |
4869 | { | |
4870 | // XXX might be better to save the error | |
4871 | // and try again in another CU | |
4872 | sess.print_error (e); | |
c4ce66a1 | 4873 | } |
c4ce66a1 JS |
4874 | return DWARF_CB_ABORT; |
4875 | } | |
4876 | return DWARF_CB_OK; | |
4877 | } | |
4878 | ||
4879 | ||
4880 | int | |
4881 | dwarf_cast_query::cast_query_cu (Dwarf_Die * cudie, void * arg) | |
4882 | { | |
4883 | dwarf_cast_query * q = static_cast<dwarf_cast_query *>(arg); | |
4884 | if (pending_interrupts) return DWARF_CB_ABORT; | |
4885 | return q->handle_query_cu(cudie); | |
4886 | } | |
4887 | ||
4888 | ||
4889 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor | |
4890 | { | |
4891 | systemtap_session& s; | |
4892 | dwarf_builder& db; | |
4893 | ||
4894 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
4895 | s(s), db(db) {} | |
4896 | void visit_cast_op (cast_op* e); | |
fb0274bc | 4897 | void filter_special_modules(string& module); |
c4ce66a1 JS |
4898 | }; |
4899 | ||
4900 | ||
fb0274bc JS |
4901 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
4902 | { | |
d90053e7 | 4903 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 4904 | // for those cases, build a module including that header |
d90053e7 JS |
4905 | if (module[module.size() - 1] == '>' && |
4906 | (module[0] == '<' || module.compare(0, 7, "kernel<") == 0)) | |
fb0274bc JS |
4907 | { |
4908 | string cached_module; | |
4909 | if (s.use_cache) | |
4910 | { | |
4911 | // see if the cached module exists | |
4912 | find_typequery_hash(s, module, cached_module); | |
4913 | if (!cached_module.empty()) | |
4914 | { | |
4915 | int fd = open(cached_module.c_str(), O_RDONLY); | |
4916 | if (fd != -1) | |
4917 | { | |
4918 | if (s.verbose > 2) | |
4919 | clog << "Pass 2: using cached " << cached_module << endl; | |
4920 | module = cached_module; | |
4921 | close(fd); | |
4922 | return; | |
4923 | } | |
4924 | } | |
4925 | } | |
4926 | ||
4927 | // no cached module, time to make it | |
d90053e7 | 4928 | if (make_typequery(s, module) == 0) |
fb0274bc | 4929 | { |
fb0274bc JS |
4930 | if (s.use_cache) |
4931 | { | |
4932 | // try to save typequery in the cache | |
4933 | if (s.verbose > 2) | |
d90053e7 | 4934 | clog << "Copying " << module |
fb0274bc | 4935 | << " to " << cached_module << endl; |
d90053e7 | 4936 | if (copy_file(module.c_str(), |
fb0274bc | 4937 | cached_module.c_str()) != 0) |
d90053e7 | 4938 | cerr << "Copy failed (\"" << module << "\" to \"" |
fb0274bc JS |
4939 | << cached_module << "\"): " << strerror(errno) << endl; |
4940 | } | |
4941 | } | |
4942 | } | |
4943 | } | |
4944 | ||
4945 | ||
c4ce66a1 JS |
4946 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
4947 | { | |
4948 | bool lvalue = is_active_lvalue(e); | |
4949 | if (lvalue && !s.guru_mode) | |
4950 | throw semantic_error("write to typecast value not permitted", e->tok); | |
4951 | ||
4952 | if (e->module.empty()) | |
4953 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
4954 | ||
c4ce66a1 JS |
4955 | string code; |
4956 | exp_type type = pe_long; | |
8b31197b JS |
4957 | |
4958 | // split the module string by ':' for alternatives | |
4959 | vector<string> modules; | |
4960 | tokenize(e->module, modules, ":"); | |
4961 | for (unsigned i = 0; code.empty() && i < modules.size(); ++i) | |
c4ce66a1 | 4962 | { |
8b31197b | 4963 | string& module = modules[i]; |
fb0274bc | 4964 | filter_special_modules(module); |
abb41d92 | 4965 | |
c4ce66a1 JS |
4966 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
4967 | // which means that userspace modules won't get any PATH searching. | |
4968 | dwflpp* dw; | |
abb41d92 | 4969 | if (module.find('/') == string::npos) |
c4ce66a1 JS |
4970 | { |
4971 | // kernel or kernel module target | |
4972 | if (! db.kern_dw) | |
4973 | { | |
7a2ee7a2 JS |
4974 | dw = new dwflpp(s); |
4975 | try | |
4976 | { | |
4977 | dw->setup_kernel(true); | |
4978 | } | |
4979 | catch (const semantic_error& er) | |
4980 | { | |
4981 | /* ignore and go to the next module */ | |
4982 | delete dw; | |
4983 | continue; | |
4984 | } | |
4985 | db.kern_dw = dw; | |
c4ce66a1 | 4986 | } |
7a2ee7a2 JS |
4987 | else |
4988 | dw = db.kern_dw; | |
c4ce66a1 JS |
4989 | } |
4990 | else | |
4991 | { | |
abb41d92 | 4992 | module = find_executable (module); // canonicalize it |
c4ce66a1 JS |
4993 | |
4994 | // user-space target; we use one dwflpp instance per module name | |
4995 | // (= program or shared library) | |
abb41d92 | 4996 | if (db.user_dw.find(module) == db.user_dw.end()) |
c4ce66a1 JS |
4997 | { |
4998 | dw = new dwflpp(s); | |
462c90c3 JS |
4999 | try |
5000 | { | |
5001 | dw->setup_user(module); | |
5002 | } | |
5003 | catch (const semantic_error& er) | |
5004 | { | |
5005 | /* ignore and go to the next module */ | |
5006 | delete dw; | |
5007 | continue; | |
5008 | } | |
abb41d92 | 5009 | db.user_dw[module] = dw; |
c4ce66a1 JS |
5010 | } |
5011 | else | |
abb41d92 | 5012 | dw = db.user_dw[module]; |
c4ce66a1 JS |
5013 | } |
5014 | ||
abb41d92 JS |
5015 | dwarf_cast_query q (*dw, module, *e, lvalue, type, code); |
5016 | dw->query_modules(&q); | |
c4ce66a1 | 5017 | } |
abb41d92 JS |
5018 | |
5019 | if (code.empty()) | |
c4ce66a1 | 5020 | { |
abb41d92 | 5021 | // We generate an error message, and pass the unresolved |
c4ce66a1 JS |
5022 | // cast_op to the next pass. We hope that this value ends |
5023 | // up not being referenced after all, so it can be optimized out | |
5024 | // quietly. | |
d0cd971e MW |
5025 | string msg = "type definition '" + e->type + "' not found"; |
5026 | semantic_error* er = new semantic_error (msg, e->tok); | |
c4ce66a1 JS |
5027 | // NB: we can have multiple errors, since a @cast |
5028 | // may be expanded in several different contexts: | |
5029 | // function ("*") { @cast(...) } | |
abb41d92 JS |
5030 | er->chain = e->saved_conversion_error; |
5031 | e->saved_conversion_error = er; | |
c4ce66a1 JS |
5032 | provide (e); |
5033 | return; | |
5034 | } | |
5035 | ||
5036 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") | |
5037 | + "_" + e->base_name.substr(1) | |
5038 | + "_" + lex_cast<string>(tick++)); | |
5039 | ||
5040 | // Synthesize a function. | |
5041 | functiondecl *fdecl = new functiondecl; | |
5042 | fdecl->tok = e->tok; | |
5043 | fdecl->type = type; | |
5044 | fdecl->name = fname; | |
5045 | ||
5046 | embeddedcode *ec = new embeddedcode; | |
5047 | ec->tok = e->tok; | |
5048 | ec->code = code; | |
5049 | fdecl->body = ec; | |
5050 | ||
5051 | // Give the fdecl an argument for the pointer we're trying to cast | |
5052 | vardecl *v1 = new vardecl; | |
5053 | v1->type = pe_long; | |
5054 | v1->name = "pointer"; | |
5055 | v1->tok = e->tok; | |
5056 | fdecl->formal_args.push_back(v1); | |
5057 | ||
5058 | if (lvalue) | |
5059 | { | |
5060 | // Modify the fdecl so it carries a second pe_long formal | |
5061 | // argument called "value". | |
5062 | ||
5063 | // FIXME: For the time being we only support setting target | |
5064 | // variables which have base types; these are 'pe_long' in | |
5065 | // stap's type vocabulary. Strings and pointers might be | |
5066 | // reasonable, some day, but not today. | |
5067 | ||
5068 | vardecl *v2 = new vardecl; | |
5069 | v2->type = pe_long; | |
5070 | v2->name = "value"; | |
5071 | v2->tok = e->tok; | |
5072 | fdecl->formal_args.push_back(v2); | |
5073 | } | |
5074 | else | |
5075 | ec->code += "/* pure */"; | |
5076 | ||
5077 | s.functions[fdecl->name] = fdecl; | |
5078 | ||
5079 | // Synthesize a functioncall. | |
5080 | functioncall* n = new functioncall; | |
5081 | n->tok = e->tok; | |
5082 | n->function = fname; | |
5083 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
5084 | n->args.push_back(e->operand); | |
5085 | ||
5086 | if (lvalue) | |
5087 | { | |
5088 | // Provide the functioncall to our parent, so that it can be | |
5089 | // used to substitute for the assignment node immediately above | |
5090 | // us. | |
5091 | assert(!target_symbol_setter_functioncalls.empty()); | |
5092 | *(target_symbol_setter_functioncalls.top()) = n; | |
5093 | } | |
5094 | ||
5095 | provide (n); | |
77de5e9e GH |
5096 | } |
5097 | ||
5098 | ||
b8da0ad1 FCE |
5099 | void |
5100 | dwarf_derived_probe::printsig (ostream& o) const | |
5101 | { | |
5102 | // Instead of just printing the plain locations, we add a PC value | |
5103 | // as a comment as a way of telling e.g. apart multiple inlined | |
5104 | // function instances. This is distinct from the verbose/clog | |
5105 | // output, since this part goes into the cache hash calculations. | |
5106 | sole_location()->print (o); | |
6d0f3f0c | 5107 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
5108 | printsig_nested (o); |
5109 | } | |
5110 | ||
5111 | ||
5112 | ||
dc38c0ae | 5113 | void |
b20febf3 FCE |
5114 | dwarf_derived_probe::join_group (systemtap_session& s) |
5115 | { | |
5116 | if (! s.dwarf_derived_probes) | |
5117 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
5118 | s.dwarf_derived_probes->enroll (this); | |
5119 | } | |
5120 | ||
5121 | ||
5122 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, | |
5123 | const string& filename, | |
5124 | int line, | |
91af0778 | 5125 | // module & section specify a relocation |
b20febf3 FCE |
5126 | // base for <addr>, unless section=="" |
5127 | // (equivalently module=="kernel") | |
5128 | const string& module, | |
5129 | const string& section, | |
5130 | // NB: dwfl_addr is the virtualized | |
5131 | // address for this symbol. | |
5132 | Dwarf_Addr dwfl_addr, | |
5133 | // addr is the section-offset for | |
5134 | // actual relocation. | |
5135 | Dwarf_Addr addr, | |
5136 | dwarf_query& q, | |
37ebca01 | 5137 | Dwarf_Die* scope_die /* may be null */) |
1939ea32 | 5138 | : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ), |
b20febf3 | 5139 | module (module), section (section), addr (addr), |
c9bad430 DS |
5140 | has_return (q.has_return), |
5141 | has_maxactive (q.has_maxactive), | |
5142 | maxactive_val (q.maxactive_val) | |
bd2b1e68 | 5143 | { |
b20febf3 | 5144 | // Assert relocation invariants |
4baf0e53 | 5145 | if (section == "" && dwfl_addr != addr) // addr should be absolute |
84048984 FCE |
5146 | throw semantic_error ("missing relocation base against", q.base_loc->tok); |
5147 | if (section != "" && dwfl_addr == addr) // addr should be an offset | |
b20febf3 | 5148 | throw semantic_error ("inconsistent relocation address", q.base_loc->tok); |
df8fadee | 5149 | |
b20febf3 | 5150 | this->tok = q.base_probe->tok; |
b95e2b79 | 5151 | this->access_vars = false; |
2930abc7 | 5152 | |
21beacc9 FCE |
5153 | // XXX: hack for strange g++/gcc's |
5154 | #ifndef USHRT_MAX | |
5155 | #define USHRT_MAX 32767 | |
5156 | #endif | |
5157 | ||
606fd9c8 FCE |
5158 | // Range limit maxactive() value |
5159 | if (q.has_maxactive && (q.maxactive_val < 0 || q.maxactive_val > USHRT_MAX)) | |
5160 | throw semantic_error ("maxactive value out of range [0," | |
5161 | + lex_cast<string>(USHRT_MAX) + "]", | |
5162 | q.base_loc->tok); | |
5163 | ||
de688825 | 5164 | // Expand target variables in the probe body |
5f0a03a6 | 5165 | if (!null_die(scope_die)) |
8fc05e57 | 5166 | { |
de688825 | 5167 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
4ed05b15 | 5168 | this->body = v.require (this->body); |
b95e2b79 | 5169 | this->access_vars = v.visited; |
37ebca01 FCE |
5170 | |
5171 | // If during target-variable-expanding the probe, we added a new block | |
5172 | // of code, add it to the start of the probe. | |
5173 | if (v.add_block) | |
ba6f838d | 5174 | this->body = new block(v.add_block, this->body); |
37ebca01 FCE |
5175 | // If when target-variable-expanding the probe, we added a new |
5176 | // probe, add it in a new file to the list of files to be processed. | |
5177 | if (v.add_probe) | |
5178 | { | |
5179 | stapfile *f = new stapfile; | |
5180 | f->probes.push_back(v.add_probe); | |
5181 | q.sess.files.push_back(f); | |
5182 | } | |
8fc05e57 | 5183 | } |
37ebca01 | 5184 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 5185 | |
5d23847d | 5186 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
5187 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
5188 | // point. This allows a user to see what function / file / line | |
5189 | // number any particular match of the wildcards. | |
2930abc7 | 5190 | |
a229fcd7 | 5191 | vector<probe_point::component*> comps; |
91af0778 FCE |
5192 | if (q.has_kernel) |
5193 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
5194 | else if(q.has_module) | |
5195 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
5196 | else if(q.has_process) | |
5197 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
5198 | else | |
5199 | assert (0); | |
b5d77020 | 5200 | |
db520b00 FCE |
5201 | string fn_or_stmt; |
5202 | if (q.has_function_str || q.has_function_num) | |
5203 | fn_or_stmt = "function"; | |
5204 | else | |
5205 | fn_or_stmt = "statement"; | |
a229fcd7 | 5206 | |
b8da0ad1 | 5207 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 5208 | { |
4cd232e4 | 5209 | string retro_name = funcname; |
b20febf3 | 5210 | if (filename != "") |
cee35f73 | 5211 | { |
fb84c077 | 5212 | retro_name += ("@" + string (filename)); |
cee35f73 | 5213 | if (line > 0) |
fb84c077 | 5214 | retro_name += (":" + lex_cast<string> (line)); |
cee35f73 | 5215 | } |
db520b00 FCE |
5216 | comps.push_back |
5217 | (new probe_point::component | |
5218 | (fn_or_stmt, new literal_string (retro_name))); | |
5219 | } | |
b8da0ad1 | 5220 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
5221 | { |
5222 | Dwarf_Addr retro_addr; | |
5223 | if (q.has_function_num) | |
5224 | retro_addr = q.function_num_val; | |
5225 | else | |
5226 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
5227 | comps.push_back (new probe_point::component |
5228 | (fn_or_stmt, | |
5229 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
37ebca01 FCE |
5230 | |
5231 | if (q.has_absolute) | |
5232 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
5233 | } |
5234 | ||
b8da0ad1 FCE |
5235 | if (q.has_call) |
5236 | comps.push_back (new probe_point::component(TOK_CALL)); | |
5237 | if (q.has_inline) | |
5238 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 5239 | if (has_return) |
b8da0ad1 FCE |
5240 | comps.push_back (new probe_point::component(TOK_RETURN)); |
5241 | if (has_maxactive) | |
5242 | comps.push_back (new probe_point::component | |
5243 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 5244 | |
5d23847d FCE |
5245 | // Overwrite it. |
5246 | this->sole_location()->components = comps; | |
2930abc7 FCE |
5247 | } |
5248 | ||
bd2b1e68 | 5249 | |
7a053d3b | 5250 | void |
20c6c071 | 5251 | dwarf_derived_probe::register_statement_variants(match_node * root, |
bd2b1e68 GH |
5252 | dwarf_builder * dw) |
5253 | { | |
54efe513 | 5254 | root->bind(dw); |
54efe513 GH |
5255 | } |
5256 | ||
7a053d3b | 5257 | void |
fd6602a0 | 5258 | dwarf_derived_probe::register_function_variants(match_node * root, |
c9bad430 | 5259 | dwarf_builder * dw) |
bd2b1e68 | 5260 | { |
fd6602a0 | 5261 | root->bind(dw); |
b8da0ad1 FCE |
5262 | root->bind(TOK_INLINE)->bind(dw); |
5263 | root->bind(TOK_CALL)->bind(dw); | |
fd6602a0 | 5264 | root->bind(TOK_RETURN)->bind(dw); |
c9bad430 | 5265 | root->bind(TOK_RETURN)->bind_num(TOK_MAXACTIVE)->bind(dw); |
bd2b1e68 GH |
5266 | } |
5267 | ||
7a053d3b | 5268 | void |
20c6c071 | 5269 | dwarf_derived_probe::register_function_and_statement_variants(match_node * root, |
bd2b1e68 GH |
5270 | dwarf_builder * dw) |
5271 | { | |
5272 | // Here we match 4 forms: | |
5273 | // | |
5274 | // .function("foo") | |
5275 | // .function(0xdeadbeef) | |
5276 | // .statement("foo") | |
5277 | // .statement(0xdeadbeef) | |
5278 | ||
fd6602a0 FCE |
5279 | register_function_variants(root->bind_str(TOK_FUNCTION), dw); |
5280 | register_function_variants(root->bind_num(TOK_FUNCTION), dw); | |
20c6c071 GH |
5281 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw); |
5282 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw); | |
bd2b1e68 GH |
5283 | } |
5284 | ||
5285 | void | |
c4ce66a1 | 5286 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 5287 | { |
c4ce66a1 | 5288 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
5289 | dwarf_builder *dw = new dwarf_builder(); |
5290 | ||
c4ce66a1 JS |
5291 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
5292 | s.code_filters.push_back(filter); | |
5293 | ||
20c6c071 GH |
5294 | register_function_and_statement_variants(root->bind(TOK_KERNEL), dw); |
5295 | register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw); | |
37ebca01 | 5296 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(dw); |
0f336e95 SC |
5297 | root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); |
5298 | root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); | |
37ebca01 | 5299 | |
7a24d422 | 5300 | register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw); |
f28a8c28 SC |
5301 | root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)->bind(dw); |
5302 | root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK)->bind(dw); | |
bd2b1e68 GH |
5303 | } |
5304 | ||
9020300d FCE |
5305 | void |
5306 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
5307 | { | |
b95e2b79 MH |
5308 | if (access_vars) |
5309 | { | |
5310 | // if accessing $variables, emit bsp cache setup for speeding up | |
5311 | o->newline() << "bspcache(c->unwaddr, c->regs);"; | |
5312 | } | |
9020300d | 5313 | } |
2930abc7 | 5314 | |
b20febf3 | 5315 | // ------------------------------------------------------------------------ |
46b84a80 DS |
5316 | |
5317 | void | |
b20febf3 | 5318 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 5319 | { |
b20febf3 | 5320 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
5321 | |
5322 | // XXX: probes put at the same address should all share a | |
5323 | // single kprobe/kretprobe, and have their handlers executed | |
5324 | // sequentially. | |
b55bc428 FCE |
5325 | } |
5326 | ||
7a053d3b | 5327 | void |
775d51e5 | 5328 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 5329 | { |
b20febf3 | 5330 | if (probes_by_module.empty()) return; |
2930abc7 | 5331 | |
775d51e5 DS |
5332 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
5333 | ||
5334 | // Warn of misconfigured kernels | |
f41595cc FCE |
5335 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
5336 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
5337 | s.op->newline() << "#endif"; | |
775d51e5 | 5338 | s.op->newline(); |
f41595cc | 5339 | |
b20febf3 FCE |
5340 | // Forward declare the master entry functions |
5341 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5342 | s.op->line() << " struct pt_regs *regs);"; | |
5343 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5344 | s.op->line() << " struct pt_regs *regs);"; | |
5345 | ||
42cb22bd MH |
5346 | // Emit an array of kprobe/kretprobe pointers |
5347 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5348 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
5349 | s.op->newline() << "#endif"; | |
5350 | ||
b20febf3 | 5351 | // Emit the actual probe list. |
606fd9c8 FCE |
5352 | |
5353 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
5354 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
5355 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
4c2732a1 | 5356 | s.op->newline() << "static struct stap_dwarf_kprobe {"; |
b20febf3 | 5357 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; |
e4cb375f MH |
5358 | s.op->newline() << "#ifdef __ia64__"; |
5359 | s.op->newline() << "struct kprobe dummy;"; | |
5360 | s.op->newline() << "#endif"; | |
606fd9c8 FCE |
5361 | s.op->newline(-1) << "} stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
5362 | // NB: bss! | |
5363 | ||
4c2732a1 | 5364 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
5365 | s.op->newline(1) << "const unsigned return_p:1;"; |
5366 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b20febf3 | 5367 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 5368 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 FCE |
5369 | |
5370 | // Let's find some stats for the three embedded strings. Maybe they | |
5371 | // are small and uniform enough to justify putting char[MAX]'s into | |
5372 | // the array instead of relocated char*'s. | |
5373 | size_t module_name_max = 0, section_name_max = 0, pp_name_max = 0; | |
5374 | size_t module_name_tot = 0, section_name_tot = 0, pp_name_tot = 0; | |
5375 | size_t all_name_cnt = probes_by_module.size(); // for average | |
5376 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
5377 | { | |
5378 | dwarf_derived_probe* p = it->second; | |
5379 | #define DOIT(var,expr) do { \ | |
5380 | size_t var##_size = (expr) + 1; \ | |
5381 | var##_max = max (var##_max, var##_size); \ | |
5382 | var##_tot += var##_size; } while (0) | |
5383 | DOIT(module_name, p->module.size()); | |
5384 | DOIT(section_name, p->section.size()); | |
5385 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
5386 | #undef DOIT | |
5387 | } | |
5388 | ||
5389 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
5390 | // the amount of average waste (max - avg) to the relocation data size | |
5391 | // (3 native long words). | |
5392 | #define CALCIT(var) \ | |
5393 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
5394 | { \ | |
5395 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
5396 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
5397 | << "[" << var##_name_max << "]" << endl; \ | |
5398 | } \ | |
5399 | else \ | |
5400 | { \ | |
b0986e7a | 5401 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
5402 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
5403 | } | |
5404 | ||
5405 | CALCIT(module); | |
5406 | CALCIT(section); | |
5407 | CALCIT(pp); | |
e6fe60e7 | 5408 | #undef CALCIT |
606fd9c8 | 5409 | |
b0986e7a DS |
5410 | s.op->newline() << "const unsigned long address;"; |
5411 | s.op->newline() << "void (* const ph) (struct context*);"; | |
b20febf3 FCE |
5412 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
5413 | s.op->indent(1); | |
5414 | ||
5415 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 5416 | { |
b20febf3 FCE |
5417 | dwarf_derived_probe* p = it->second; |
5418 | s.op->newline() << "{"; | |
5419 | if (p->has_return) | |
5420 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 5421 | if (p->has_maxactive) |
606fd9c8 FCE |
5422 | { |
5423 | s.op->line() << " .maxactive_p=1,"; | |
5424 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
5425 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
5426 | } | |
dc38c256 | 5427 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
5428 | s.op->line() << " .module=\"" << p->module << "\","; |
5429 | s.op->line() << " .section=\"" << p->section << "\","; | |
b20febf3 FCE |
5430 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
5431 | s.op->line() << " .ph=&" << p->name; | |
5432 | s.op->line() << " },"; | |
2930abc7 | 5433 | } |
2930abc7 | 5434 | |
b20febf3 FCE |
5435 | s.op->newline(-1) << "};"; |
5436 | ||
5437 | // Emit the kprobes callback function | |
5438 | s.op->newline(); | |
5439 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5440 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
5441 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
5442 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
5443 | // Check that the index is plausible | |
5444 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5445 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5446 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5447 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5448 | s.op->line() << "];"; | |
c12d974f | 5449 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
5450 | s.op->newline() << "c->regs = regs;"; |
5451 | s.op->newline() << "(*sdp->ph) (c);"; | |
5452 | common_probe_entryfn_epilogue (s.op); | |
5453 | s.op->newline() << "return 0;"; | |
5454 | s.op->newline(-1) << "}"; | |
5455 | ||
5456 | // Same for kretprobes | |
5457 | s.op->newline(); | |
5458 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5459 | s.op->line() << " struct pt_regs *regs) {"; | |
5460 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
606fd9c8 FCE |
5461 | |
5462 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 5463 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
5464 | // Check that the index is plausible |
5465 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5466 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5467 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5468 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5469 | s.op->line() << "];"; | |
5470 | ||
c12d974f | 5471 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
5472 | s.op->newline() << "c->regs = regs;"; |
5473 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
5474 | s.op->newline() << "(*sdp->ph) (c);"; | |
5475 | common_probe_entryfn_epilogue (s.op); | |
5476 | s.op->newline() << "return 0;"; | |
5477 | s.op->newline(-1) << "}"; | |
20c6c071 | 5478 | } |
ec4373ff | 5479 | |
20c6c071 | 5480 | |
dc38c0ae | 5481 | void |
b20febf3 FCE |
5482 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
5483 | { | |
5484 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5485 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5486 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
f1bad60c | 5487 | s.op->newline() << "unsigned long relocated_addr = _stp_module_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 5488 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
6d0f3f0c | 5489 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
b20febf3 | 5490 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 5491 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 5492 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 5493 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 5494 | s.op->newline(-1) << "} else {"; |
606fd9c8 | 5495 | s.op->newline(1) << "kp->u.krp.maxactive = max(10, 4*NR_CPUS);"; |
c9bad430 | 5496 | s.op->newline(-1) << "}"; |
606fd9c8 | 5497 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
e4cb375f MH |
5498 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
5499 | s.op->newline() << "#ifdef __ia64__"; | |
5500 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5501 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5502 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5503 | s.op->newline() << "if (rc == 0) {"; | |
5504 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5505 | s.op->newline() << "if (rc != 0)"; | |
5506 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5507 | s.op->newline(-2) << "}"; | |
5508 | s.op->newline() << "#else"; | |
606fd9c8 | 5509 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 5510 | s.op->newline() << "#endif"; |
b20febf3 | 5511 | s.op->newline(-1) << "} else {"; |
e4cb375f | 5512 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
5513 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
5514 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
5515 | s.op->newline() << "#ifdef __ia64__"; |
5516 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5517 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5518 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5519 | s.op->newline() << "if (rc == 0) {"; | |
5520 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5521 | s.op->newline() << "if (rc != 0)"; | |
5522 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5523 | s.op->newline(-2) << "}"; | |
5524 | s.op->newline() << "#else"; | |
606fd9c8 | 5525 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 5526 | s.op->newline() << "#endif"; |
b20febf3 | 5527 | s.op->newline(-1) << "}"; |
9063462a FCE |
5528 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
5529 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
5530 | s.op->newline() << "_stp_warn (\"probe %s registration error (rc %d)\", probe_point, rc);"; | |
5531 | s.op->newline() << "rc = 0;"; // continue with other probes | |
5532 | // XXX: shall we increment numskipped? | |
5533 | s.op->newline(-1) << "}"; | |
5534 | ||
5535 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 5536 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 5537 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
5538 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
5539 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
5540 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
5541 | s.op->newline() << "#ifdef __ia64__"; |
5542 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
5543 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
5544 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
5545 | // not run for this early-abort case. | |
5546 | s.op->newline(-1) << "}"; | |
5547 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 5548 | s.op->newline(-1) << "}"; |
9063462a FCE |
5549 | #endif |
5550 | ||
b20febf3 FCE |
5551 | s.op->newline() << "else sdp->registered_p = 1;"; |
5552 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
5553 | } |
5554 | ||
5555 | ||
46b84a80 | 5556 | void |
b20febf3 | 5557 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 5558 | { |
42cb22bd MH |
5559 | //Unregister kprobes by batch interfaces. |
5560 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5561 | s.op->newline() << "j = 0;"; | |
5562 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5563 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5564 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5565 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5566 | s.op->newline() << "if (!sdp->return_p)"; | |
5567 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
5568 | s.op->newline(-2) << "}"; | |
5569 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5570 | s.op->newline() << "j = 0;"; | |
5571 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5572 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5573 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5574 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5575 | s.op->newline() << "if (sdp->return_p)"; | |
5576 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
5577 | s.op->newline(-2) << "}"; | |
5578 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
5579 | s.op->newline() << "#ifdef __ia64__"; |
5580 | s.op->newline() << "j = 0;"; | |
5581 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5582 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5583 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5584 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5585 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
5586 | s.op->newline(-1) << "}"; | |
5587 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5588 | s.op->newline() << "#endif"; | |
42cb22bd MH |
5589 | s.op->newline() << "#endif"; |
5590 | ||
b20febf3 FCE |
5591 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
5592 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5593 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
5594 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
5595 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 5596 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5597 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 5598 | s.op->newline() << "#endif"; |
606fd9c8 | 5599 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; |
73209876 FCE |
5600 | s.op->newline() << "#ifdef STP_TIMING"; |
5601 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
d01eaa30 | 5602 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; |
73209876 | 5603 | s.op->newline(-1) << "#endif"; |
606fd9c8 | 5604 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5605 | s.op->newline() << "#ifdef STP_TIMING"; |
5606 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
d01eaa30 | 5607 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; |
73209876 | 5608 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 5609 | s.op->newline(-1) << "} else {"; |
42cb22bd | 5610 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5611 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 5612 | s.op->newline() << "#endif"; |
606fd9c8 | 5613 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5614 | s.op->newline() << "#ifdef STP_TIMING"; |
5615 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
d01eaa30 | 5616 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);"; |
73209876 | 5617 | s.op->newline(-1) << "#endif"; |
b20febf3 | 5618 | s.op->newline(-1) << "}"; |
e4cb375f MH |
5619 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
5620 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5621 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
5622 | s.op->newline() << "sdp->registered_p = 0;"; |
5623 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
5624 | } |
5625 | ||
5626 | ||
20c6c071 | 5627 | void |
5227f1ea | 5628 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 5629 | probe * base, |
20c6c071 | 5630 | probe_point * location, |
86bf665e | 5631 | literal_map_t const & parameters, |
20c6c071 GH |
5632 | vector<derived_probe *> & finished_results) |
5633 | { | |
b20febf3 FCE |
5634 | // NB: the kernel/user dwlfpp objects are long-lived. |
5635 | // XXX: but they should be per-session, as this builder object | |
5636 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 5637 | |
7a24d422 FCE |
5638 | dwflpp* dw = 0; |
5639 | ||
7a24d422 FCE |
5640 | string module_name; |
5641 | if (has_null_param (parameters, TOK_KERNEL) | |
5642 | || get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 5643 | { |
7a24d422 FCE |
5644 | // kernel or kernel module target |
5645 | if (! kern_dw) | |
5646 | { | |
5647 | kern_dw = new dwflpp(sess); | |
28d29bd3 FCE |
5648 | // XXX: PR 3498, PR 6864 |
5649 | kern_dw->setup_kernel(true); | |
7a24d422 FCE |
5650 | } |
5651 | dw = kern_dw; | |
b8da0ad1 | 5652 | } |
7a24d422 | 5653 | else if (get_param (parameters, TOK_PROCESS, module_name)) |
b8da0ad1 | 5654 | { |
d0a7f5a9 FCE |
5655 | module_name = find_executable (module_name); // canonicalize it |
5656 | ||
7a24d422 FCE |
5657 | // user-space target; we use one dwflpp instance per module name |
5658 | // (= program or shared library) | |
5659 | if (user_dw.find(module_name) == user_dw.end()) | |
84048984 | 5660 | { |
7a24d422 FCE |
5661 | dw = new dwflpp(sess); |
5662 | // XXX: PR 3498 | |
5663 | dw->setup_user(module_name); | |
5664 | user_dw[module_name] = dw; | |
84048984 | 5665 | } |
7a24d422 FCE |
5666 | else |
5667 | dw = user_dw[module_name]; | |
c8959a29 | 5668 | } |
20c6c071 | 5669 | |
f28a8c28 | 5670 | if (((probe_point::component*)(location->components[1]))->functor == TOK_MARK) |
349dc70e SC |
5671 | { |
5672 | enum probe_types | |
f28a8c28 | 5673 | { |
7b534f48 SC |
5674 | probes_and_dwarf = 0, // Use statement address |
5675 | dwarf_no_probes = 1, // Use label name | |
5676 | probes_no_dwarf = 2 | |
349dc70e | 5677 | }; |
f28a8c28 | 5678 | |
7b534f48 SC |
5679 | int probe_type = dwarf_no_probes; |
5680 | string probe_name = (char*) location->components[1]->arg->tok->content.c_str(); | |
5681 | __uint64_t probe_arg = 0; | |
349dc70e | 5682 | Dwarf_Addr bias; |
1f0cfd98 | 5683 | Elf* elf = dwfl_module_getelf (dw->module, &bias); |
349dc70e | 5684 | size_t shstrndx; |
349dc70e | 5685 | Elf_Scn *probe_scn = NULL; |
7b534f48 | 5686 | |
349dc70e | 5687 | dwfl_assert ("getshstrndx", elf_getshstrndx (elf, &shstrndx)); |
7b534f48 SC |
5688 | GElf_Shdr *shdr = NULL; |
5689 | ||
5690 | // Is there a .probes section? | |
349dc70e SC |
5691 | while ((probe_scn = elf_nextscn (elf, probe_scn))) |
5692 | { | |
5693 | GElf_Shdr shdr_mem; | |
7b534f48 | 5694 | shdr = gelf_getshdr (probe_scn, &shdr_mem); |
349dc70e SC |
5695 | assert (shdr != NULL); |
5696 | ||
7b534f48 SC |
5697 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0) |
5698 | { | |
5699 | probe_type = probes_and_dwarf; | |
5700 | break; | |
5701 | } | |
5702 | } | |
5703 | ||
5704 | if (probe_type == probes_and_dwarf) | |
5705 | { | |
1f0cfd98 | 5706 | Elf_Data *pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
349dc70e SC |
5707 | assert (pdata != NULL); |
5708 | size_t probe_scn_offset = 0; | |
c1008fd0 | 5709 | size_t probe_scn_addr = shdr->sh_addr; |
349dc70e SC |
5710 | while (probe_scn_offset < pdata->d_size) |
5711 | { | |
c1008fd0 | 5712 | const int stap_sentinel = 0x31425250; |
1f0cfd98 | 5713 | probe_type = *((int*)((char*)pdata->d_buf + probe_scn_offset)); |
c1008fd0 SC |
5714 | if (probe_type != stap_sentinel) |
5715 | { | |
5716 | probe_scn_offset += sizeof(int); | |
5717 | continue; | |
5718 | } | |
349dc70e | 5719 | probe_scn_offset += sizeof(int); |
c1008fd0 SC |
5720 | if (probe_scn_offset % (sizeof(__uint64_t))) |
5721 | probe_scn_offset += sizeof(__uint64_t) - (probe_scn_offset % sizeof(__uint64_t)); | |
5722 | ||
247f1e1f | 5723 | probe_name = ((char*)((long)(pdata->d_buf) + (long)(*((long*)((char*)pdata->d_buf + probe_scn_offset)) - probe_scn_addr))); |
c1008fd0 | 5724 | probe_scn_offset += sizeof(void*); |
1f0cfd98 SC |
5725 | if (probe_scn_offset % (sizeof(__uint64_t))) |
5726 | probe_scn_offset += sizeof(__uint64_t) - (probe_scn_offset % sizeof(__uint64_t)); | |
5727 | probe_arg = *((__uint64_t*)((char*)pdata->d_buf + probe_scn_offset)); | |
1f0cfd98 SC |
5728 | if (probe_scn_offset % (sizeof(__uint64_t)*2)) |
5729 | probe_scn_offset = (probe_scn_offset + sizeof(__uint64_t)*2) - (probe_scn_offset % (sizeof(__uint64_t)*2)); | |
c5746f91 SC |
5730 | if ((strcmp (location->components[1]->arg->tok->content.c_str(), |
5731 | probe_name.c_str()) == 0) | |
5732 | || (dw->name_has_wildcard (location->components[1]->arg->tok->content.c_str()) | |
5733 | && dw->function_name_matches_pattern | |
5734 | (probe_name.c_str(), | |
5735 | location->components[1]->arg->tok->content.c_str()))) | |
952ce18c SC |
5736 | { |
5737 | } | |
9e67aff9 SC |
5738 | else |
5739 | continue; | |
5740 | const token* sv_tok = location->components[1]->arg->tok; | |
5741 | location->components[1]->functor = TOK_STATEMENT; | |
247f1e1f | 5742 | location->components[1]->arg = new literal_number((long)probe_arg); |
9e67aff9 SC |
5743 | location->components[1]->arg->tok = sv_tok; |
5744 | ((literal_map_t&)parameters)[TOK_STATEMENT] = location->components[1]->arg; | |
c5746f91 | 5745 | |
9e67aff9 | 5746 | dwarf_query q(sess, base, location, *dw, parameters, finished_results); |
467bea43 | 5747 | q.has_mark = true; |
9e67aff9 | 5748 | dw->query_modules(&q); |
c5746f91 SC |
5749 | if (sess.listing_mode) |
5750 | { | |
5751 | finished_results.back()->locations[0]->components[1]->functor = TOK_MARK; | |
5752 | finished_results.back()->locations[0]->components[1]->arg = new literal_string (probe_name.c_str()); | |
5753 | } | |
349dc70e | 5754 | } |
64c6aab0 | 5755 | return; |
349dc70e | 5756 | } |
59b2ec52 | 5757 | |
952ce18c | 5758 | else if (probe_type == dwarf_no_probes) |
349dc70e | 5759 | { |
7b534f48 SC |
5760 | location->components[1]->functor = TOK_FUNCTION; |
5761 | location->components[1]->arg = new literal_string("*"); | |
5762 | ((literal_map_t&)parameters)[TOK_FUNCTION] = location->components[1]->arg; | |
5763 | location->components.push_back(new probe_point::component(TOK_LABEL)); | |
5764 | location->components[2]->arg = new literal_string("_stapprobe1_" + probe_name); | |
5765 | ((literal_map_t&)parameters).erase(TOK_MARK); | |
5766 | ((literal_map_t&)parameters).insert(pair<string,literal*>(TOK_LABEL, location->components[2]->arg)); | |
349dc70e | 5767 | } |
1f0cfd98 | 5768 | |
349dc70e SC |
5769 | dw->module = 0; |
5770 | } | |
5771 | ||
c8959a29 | 5772 | dwarf_query q(sess, base, location, *dw, parameters, finished_results); |
20c6c071 | 5773 | |
7a24d422 FCE |
5774 | |
5775 | // XXX: kernel.statement.absolute is a special case that requires no | |
5776 | // dwfl processing. This code should be in a separate builder. | |
5777 | ||
5778 | if (q.has_kernel && q.has_absolute) | |
37ebca01 | 5779 | { |
4baf0e53 | 5780 | // assert guru mode for absolute probes |
37ebca01 FCE |
5781 | if (! q.base_probe->privileged) |
5782 | { | |
5783 | throw semantic_error ("absolute statement probe in unprivileged script", q.base_probe->tok); | |
5784 | } | |
5785 | ||
5786 | // For kernel.statement(NUM).absolute probe points, we bypass | |
5787 | // all the debuginfo stuff: We just wire up a | |
5788 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 5789 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
5790 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
5791 | q.statement_num_val, q.statement_num_val, | |
5792 | q, 0); | |
37ebca01 | 5793 | finished_results.push_back (p); |
1a0dbc5a | 5794 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
5795 | return; |
5796 | } | |
5797 | ||
5f0a03a6 JK |
5798 | dw->query_modules(&q); |
5799 | } | |
5800 | ||
5801 | symbol_table::~symbol_table() | |
5802 | { | |
2e67a43b TM |
5803 | for (iterator_t i = list_by_addr.begin(); i != list_by_addr.end(); ++i) |
5804 | delete *i; | |
5f0a03a6 JK |
5805 | } |
5806 | ||
5807 | void | |
ab91b232 JK |
5808 | symbol_table::add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
5809 | Dwarf_Addr *high_addr) | |
5f0a03a6 | 5810 | { |
ab91b232 JK |
5811 | #ifdef __powerpc__ |
5812 | // Map ".sys_foo" to "sys_foo". | |
5813 | if (name[0] == '.') | |
5814 | name++; | |
5815 | #endif | |
5f0a03a6 JK |
5816 | func_info *fi = new func_info(); |
5817 | fi->addr = addr; | |
5818 | fi->name = name; | |
ab91b232 | 5819 | fi->weak = weak; |
5f0a03a6 JK |
5820 | map_by_name[fi->name] = fi; |
5821 | // TODO: Use a multimap in case there are multiple static | |
5822 | // functions with the same name? | |
2e67a43b | 5823 | list_by_addr.push_back(fi); |
5f0a03a6 JK |
5824 | } |
5825 | ||
5826 | enum info_status | |
5827 | symbol_table::read_symbols(FILE *f, const string& path) | |
5828 | { | |
5829 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
5830 | int ret; | |
2e67a43b TM |
5831 | char *name = 0; |
5832 | char *mod = 0; | |
5f0a03a6 JK |
5833 | char type; |
5834 | unsigned long long addr; | |
5835 | Dwarf_Addr high_addr = 0; | |
5836 | int line = 0; | |
5837 | ||
5838 | // %as (non-POSIX) mallocs space for the string and stores its address. | |
5839 | while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0) | |
5840 | { | |
2e67a43b TM |
5841 | auto_free free_name(name); |
5842 | auto_free free_mod(mod); | |
5f0a03a6 JK |
5843 | line++; |
5844 | if (ret < 3) | |
5845 | { | |
41c262f3 | 5846 | cerr << "Symbol table error: Line " |
5f0a03a6 JK |
5847 | << line |
5848 | << " of symbol list from " | |
5849 | << path | |
5850 | << " is not in correct format: address type name [module]"; | |
5851 | // Caller should delete symbol_table object. | |
5852 | return info_absent; | |
5853 | } | |
2e67a43b | 5854 | else if (ret > 3) |
5f0a03a6 JK |
5855 | { |
5856 | // Modules are loaded above the kernel, so if we're getting | |
5857 | // modules, we're done. | |
2e67a43b | 5858 | break; |
5f0a03a6 | 5859 | } |
ab91b232 JK |
5860 | if (type == 'T' || type == 't' || type == 'W') |
5861 | add_symbol(name, (type == 'W'), (Dwarf_Addr) addr, &high_addr); | |
5f0a03a6 JK |
5862 | } |
5863 | ||
5f0a03a6 JK |
5864 | if (list_by_addr.size() < 1) |
5865 | { | |
5866 | cerr << "Symbol table error: " | |
5867 | << path << " contains no function symbols." << endl; | |
5868 | return info_absent; | |
5869 | } | |
2e67a43b | 5870 | sort(); |
5f0a03a6 JK |
5871 | return info_present; |
5872 | } | |
5873 | ||
5874 | // NB: This currently unused. We use get_from_elf() instead because | |
5875 | // that gives us raw addresses -- which we need for modules -- whereas | |
5876 | // nm provides the address relative to the beginning of the section. | |
5877 | enum info_status | |
5878 | symbol_table::read_from_elf_file(const string &path) | |
5879 | { | |
5880 | FILE *f; | |
5881 | string cmd = string("/usr/bin/nm -n --defined-only ") + path; | |
5882 | f = popen(cmd.c_str(), "r"); | |
5883 | if (!f) | |
5884 | { | |
5885 | // nm failures are detected by pclose, not popen. | |
5886 | cerr << "Internal error reading symbol table from " | |
5887 | << path << " -- " << strerror (errno); | |
5888 | return info_absent; | |
5889 | } | |
5890 | enum info_status status = read_symbols(f, path); | |
5891 | if (pclose(f) != 0) | |
5892 | { | |
5893 | if (status == info_present) | |
5894 | cerr << "Warning: nm cannot read symbol table from " << path; | |
5895 | return info_absent; | |
5896 | } | |
5897 | return status; | |
5898 | } | |
5899 | ||
5900 | enum info_status | |
5901 | symbol_table::read_from_text_file(const string& path) | |
5902 | { | |
5903 | FILE *f = fopen(path.c_str(), "r"); | |
5904 | if (!f) | |
5905 | { | |
5906 | cerr << "Warning: cannot read symbol table from " | |
5907 | << path << " -- " << strerror (errno); | |
5908 | return info_absent; | |
5909 | } | |
5910 | enum info_status status = read_symbols(f, path); | |
5911 | (void) fclose(f); | |
5912 | return status; | |
5913 | } | |
5914 | ||
46f7b6be JK |
5915 | void |
5916 | symbol_table::prepare_section_rejection(Dwfl_Module *mod) | |
5917 | { | |
5918 | #ifdef __powerpc__ | |
5919 | /* | |
5920 | * The .opd section contains function descriptors that can look | |
5921 | * just like function entry points. For example, there's a function | |
5922 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
5923 | * Reject all symbols in .opd. | |
5924 | */ | |
5925 | opd_section = SHN_UNDEF; | |
5926 | Dwarf_Addr bias; | |
5927 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
5928 | ?: dwfl_module_getelf (mod, &bias)); | |
5929 | Elf_Scn* scn = 0; | |
5930 | size_t shstrndx; | |
5931 | ||
5932 | if (!elf) | |
5933 | return; | |
5934 | if (elf_getshstrndx(elf, &shstrndx) != 0) | |
5935 | return; | |
5936 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
5937 | { | |
5938 | GElf_Shdr shdr_mem; | |
5939 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
5940 | if (!shdr) | |
5941 | continue; | |
5942 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
5943 | if (!strcmp(name, ".opd")) | |
5944 | { | |
5945 | opd_section = elf_ndxscn(scn); | |
5946 | return; | |
5947 | } | |
5948 | } | |
5949 | #endif | |
5950 | } | |
5951 | ||
5952 | bool | |
5953 | symbol_table::reject_section(GElf_Word section) | |
5954 | { | |
5955 | if (section == SHN_UNDEF) | |
5956 | return true; | |
5957 | #ifdef __powerpc__ | |
5958 | if (section == opd_section) | |
5959 | return true; | |
5960 | #endif | |
5961 | return false; | |
5962 | } | |
5963 | ||
5f0a03a6 JK |
5964 | enum info_status |
5965 | symbol_table::get_from_elf() | |
5966 | { | |
5967 | Dwarf_Addr high_addr = 0; | |
5968 | Dwfl_Module *mod = mod_info->mod; | |
5969 | int syments = dwfl_module_getsymtab(mod); | |
5970 | assert(syments); | |
46f7b6be | 5971 | prepare_section_rejection(mod); |
5f0a03a6 JK |
5972 | for (int i = 1; i < syments; ++i) |
5973 | { | |
5974 | GElf_Sym sym; | |
ab91b232 JK |
5975 | GElf_Word section; |
5976 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
46f7b6be JK |
5977 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC && |
5978 | !reject_section(section)) | |
ab91b232 JK |
5979 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
5980 | sym.st_value, &high_addr); | |
5f0a03a6 | 5981 | } |
2e67a43b | 5982 | sort(); |
5f0a03a6 JK |
5983 | return info_present; |
5984 | } | |
5985 | ||
5986 | void | |
5987 | symbol_table::mark_dwarf_redundancies(dwflpp *dw) | |
5988 | { | |
5989 | // dwflpp.cu_function_cache maps each module_name:cu_name to a | |
5990 | // vector of Dwarf_Dies, one per function. | |
5991 | string module_prefix = string(mod_info->name) + ":"; | |
5992 | ||
73f289b2 | 5993 | for (mod_cu_function_cache_t::iterator cu = dw->cu_function_cache.begin(); |
6561773f | 5994 | cu != dw->cu_function_cache.end(); cu++) |
5f0a03a6 JK |
5995 | { |
5996 | string key = cu->first; | |
5997 | if (key.find(module_prefix) == 0) | |
5998 | { | |
5999 | // Found a compilation unit in the module of interest. | |
6000 | // Mark all its functions in the symbol table. | |
6561773f | 6001 | cu_function_cache_t* v = cu->second; |
5f0a03a6 | 6002 | assert(v); |
6561773f | 6003 | for (cu_function_cache_t::iterator fc = v->begin(); fc != v->end(); fc++) |
5f0a03a6 | 6004 | { |
6561773f FCE |
6005 | Dwarf_Die func = fc->second; |
6006 | string func_name = fc->first; // == dwarf_diename(&func); | |
5f0a03a6 JK |
6007 | // map_by_name[func_name]->die = func; |
6008 | map<string, func_info*>::iterator i = map_by_name.find(func_name); | |
6009 | // Func names can show up in the dwarf but not the symtab (!). | |
6010 | if (i != map_by_name.end()) | |
6011 | { | |
6012 | func_info *fi = i->second; | |
6013 | fi->die = func; | |
6014 | } | |
6015 | } | |
6016 | } | |
6017 | } | |
6018 | } | |
6019 | ||
6020 | func_info * | |
6021 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
6022 | { | |
2e67a43b TM |
6023 | iterator_t iter = upper_bound(list_by_addr.begin(), list_by_addr.end(), addr, |
6024 | func_info::Compare()); | |
6025 | if (iter == list_by_addr.begin()) | |
5f0a03a6 | 6026 | return NULL; |
2e67a43b TM |
6027 | else |
6028 | return *(iter - 1); | |
5f0a03a6 JK |
6029 | } |
6030 | ||
6031 | func_info * | |
6032 | symbol_table::lookup_symbol(const string& name) | |
6033 | { | |
6034 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
6035 | if (i == map_by_name.end()) | |
6036 | return NULL; | |
6037 | return i->second; | |
6038 | } | |
6039 | ||
6040 | Dwarf_Addr | |
6041 | symbol_table::lookup_symbol_address(const string& name) | |
6042 | { | |
6043 | func_info *fi = lookup_symbol(name); | |
6044 | if (fi) | |
6045 | return fi->addr; | |
6046 | return 0; | |
6047 | } | |
6048 | ||
ab91b232 JK |
6049 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
6050 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
6051 | // For system calls not implemented elsewhere, this weak symbol shows up | |
6052 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
6053 | // we refuse to consider such symbols. Here we delete them from our | |
6054 | // symbol table. | |
6055 | // TODO: Consider generalizing this and/or making it part of blacklist | |
6056 | // processing. | |
6057 | void | |
6058 | symbol_table::purge_syscall_stubs() | |
6059 | { | |
6060 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
6061 | if (stub_addr == 0) | |
6062 | return; | |
2e67a43b TM |
6063 | range_t purge_range = equal_range(list_by_addr.begin(), list_by_addr.end(), |
6064 | stub_addr, func_info::Compare()); | |
6065 | for (iterator_t iter = purge_range.first; | |
6066 | iter != purge_range.second; | |
6067 | ++iter) | |
ab91b232 | 6068 | { |
2e67a43b TM |
6069 | func_info *fi = *iter; |
6070 | if (fi->weak && fi->name != "sys_ni_syscall") | |
ab91b232 | 6071 | { |
2e67a43b TM |
6072 | map_by_name.erase(fi->name); |
6073 | delete fi; | |
6074 | *iter = 0; | |
6075 | } | |
ab91b232 | 6076 | } |
2e67a43b TM |
6077 | // Range might have null pointer entries that should be erased. |
6078 | list_by_addr.erase(remove(purge_range.first, purge_range.second, | |
6079 | (func_info*)0), | |
6080 | purge_range.second); | |
6081 | } | |
6082 | ||
6083 | void | |
6084 | symbol_table::sort() | |
6085 | { | |
6086 | stable_sort(list_by_addr.begin(), list_by_addr.end(), func_info::Compare()); | |
ab91b232 JK |
6087 | } |
6088 | ||
5f0a03a6 JK |
6089 | void |
6090 | module_info::get_symtab(dwarf_query *q) | |
6091 | { | |
6092 | systemtap_session &sess = q->sess; | |
6093 | ||
6094 | sym_table = new symbol_table(this); | |
6095 | if (!elf_path.empty()) | |
6096 | { | |
6097 | if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()) | |
6098 | cerr << "Warning: reading symbol table from " | |
6099 | << elf_path | |
6100 | << " -- ignoring " | |
6101 | << sess.kernel_symtab_path | |
6102 | << endl ;; | |
6103 | symtab_status = sym_table->get_from_elf(); | |
6104 | } | |
6105 | else | |
6106 | { | |
6107 | assert(name == TOK_KERNEL); | |
6108 | if (sess.kernel_symtab_path.empty()) | |
6109 | { | |
6110 | symtab_status = info_absent; | |
6111 | cerr << "Error: Cannot find vmlinux." | |
6112 | << " Consider using --kmap instead of --kelf." | |
6113 | << endl;; | |
6114 | } | |
6115 | else | |
6116 | { | |
6117 | symtab_status = | |
6118 | sym_table->read_from_text_file(sess.kernel_symtab_path); | |
6119 | if (symtab_status == info_present) | |
6120 | { | |
6121 | sess.sym_kprobes_text_start = | |
6122 | sym_table->lookup_symbol_address("__kprobes_text_start"); | |
6123 | sess.sym_kprobes_text_end = | |
6124 | sym_table->lookup_symbol_address("__kprobes_text_end"); | |
6125 | sess.sym_stext = sym_table->lookup_symbol_address("_stext"); | |
5f0a03a6 JK |
6126 | } |
6127 | } | |
6128 | } | |
6129 | if (symtab_status == info_absent) | |
6130 | { | |
6131 | delete sym_table; | |
6132 | sym_table = NULL; | |
6133 | return; | |
6134 | } | |
6135 | ||
6136 | // If we have dwarf for the same module, mark the redundant symtab | |
6137 | // entries. | |
6138 | // | |
6139 | // In dwarf_query::handle_query_module(), the call to query_module_dwarf() | |
6140 | // precedes the call to query_module_symtab(). So we should never read | |
6141 | // a module's symbol table without first having tried to get its dwarf. | |
6142 | sym_table->mark_dwarf_redundancies(&q->dw); | |
ab91b232 JK |
6143 | |
6144 | if (name == TOK_KERNEL) | |
6145 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
6146 | } |
6147 | ||
6148 | module_info::~module_info() | |
6149 | { | |
6150 | if (sym_table) | |
6151 | delete sym_table; | |
b55bc428 FCE |
6152 | } |
6153 | ||
935447c8 DS |
6154 | // ------------------------------------------------------------------------ |
6155 | // utrace user-space probes | |
6156 | // ------------------------------------------------------------------------ | |
6157 | ||
47e0478e JS |
6158 | static string TOK_BEGIN("begin"); |
6159 | static string TOK_END("end"); | |
eff6ac72 | 6160 | static string TOK_THREAD("thread"); |
12b21830 DS |
6161 | static string TOK_SYSCALL("syscall"); |
6162 | ||
eff6ac72 DS |
6163 | // Note that these flags don't match up exactly with UTRACE_EVENT |
6164 | // flags (and that's OK). | |
935447c8 DS |
6165 | enum utrace_derived_probe_flags { |
6166 | UDPF_NONE, | |
eff6ac72 DS |
6167 | UDPF_BEGIN, // process begin |
6168 | UDPF_END, // process end | |
6169 | UDPF_THREAD_BEGIN, // thread begin | |
6170 | UDPF_THREAD_END, // thread end | |
6171 | UDPF_SYSCALL, // syscall entry | |
6172 | UDPF_SYSCALL_RETURN, // syscall exit | |
935447c8 DS |
6173 | UDPF_NFLAGS |
6174 | }; | |
6175 | ||
6176 | struct utrace_derived_probe: public derived_probe | |
6177 | { | |
6178 | bool has_path; | |
6179 | string path; | |
6180 | int64_t pid; | |
6181 | enum utrace_derived_probe_flags flags; | |
6182 | bool target_symbol_seen; | |
6183 | ||
6184 | utrace_derived_probe (systemtap_session &s, probe* p, probe_point* l, | |
6185 | bool hp, string &pn, int64_t pd, | |
6186 | enum utrace_derived_probe_flags f); | |
6187 | void join_group (systemtap_session& s); | |
6188 | }; | |
6189 | ||
6190 | ||
6191 | struct utrace_derived_probe_group: public generic_dpg<utrace_derived_probe> | |
6192 | { | |
6193 | private: | |
6194 | map<string, vector<utrace_derived_probe*> > probes_by_path; | |
6195 | typedef map<string, vector<utrace_derived_probe*> >::iterator p_b_path_iterator; | |
6196 | map<int64_t, vector<utrace_derived_probe*> > probes_by_pid; | |
6197 | typedef map<int64_t, vector<utrace_derived_probe*> >::iterator p_b_pid_iterator; | |
6198 | unsigned num_probes; | |
6199 | bool flags_seen[UDPF_NFLAGS]; | |
6200 | ||
6201 | void emit_probe_decl (systemtap_session& s, utrace_derived_probe *p); | |
6202 | ||
6203 | public: | |
6204 | utrace_derived_probe_group(): num_probes(0), flags_seen() { } | |
6205 | ||
6206 | void enroll (utrace_derived_probe* probe); | |
6207 | void emit_module_decls (systemtap_session& s); | |
6208 | void emit_module_init (systemtap_session& s); | |
6209 | void emit_module_exit (systemtap_session& s); | |
6210 | }; | |
6211 | ||
6212 | ||
de688825 | 6213 | struct utrace_var_expanding_visitor: public var_expanding_visitor |
935447c8 | 6214 | { |
d0ea46ce DS |
6215 | utrace_var_expanding_visitor(systemtap_session& s, probe_point* l, |
6216 | const string& pn, | |
de688825 | 6217 | enum utrace_derived_probe_flags f): |
d0ea46ce DS |
6218 | sess (s), base_loc (l), probe_name (pn), flags (f), |
6219 | target_symbol_seen (false), add_block(NULL), add_probe(NULL) {} | |
935447c8 DS |
6220 | |
6221 | systemtap_session& sess; | |
d0ea46ce | 6222 | probe_point* base_loc; |
935447c8 DS |
6223 | string probe_name; |
6224 | enum utrace_derived_probe_flags flags; | |
6225 | bool target_symbol_seen; | |
d0ea46ce DS |
6226 | block *add_block; |
6227 | probe *add_probe; | |
6228 | std::map<std::string, symbol *> return_ts_map; | |
935447c8 | 6229 | |
6270adc1 | 6230 | void visit_target_symbol_arg (target_symbol* e); |
5d67b47c | 6231 | void visit_target_symbol_context (target_symbol* e); |
d0ea46ce | 6232 | void visit_target_symbol_cached (target_symbol* e); |
935447c8 DS |
6233 | void visit_target_symbol (target_symbol* e); |
6234 | }; | |
6235 | ||
6236 | ||
a7a68293 | 6237 | |
935447c8 DS |
6238 | utrace_derived_probe::utrace_derived_probe (systemtap_session &s, |
6239 | probe* p, probe_point* l, | |
6240 | bool hp, string &pn, int64_t pd, | |
6241 | enum utrace_derived_probe_flags f): | |
d0a7f5a9 FCE |
6242 | derived_probe (p, new probe_point (*l) /* .components soon rewritten */ ), |
6243 | has_path(hp), path(pn), pid(pd), flags(f), | |
935447c8 DS |
6244 | target_symbol_seen(false) |
6245 | { | |
de688825 | 6246 | // Expand local variables in the probe body |
d0ea46ce | 6247 | utrace_var_expanding_visitor v (s, l, name, flags); |
de688825 | 6248 | this->body = v.require (this->body); |
935447c8 | 6249 | target_symbol_seen = v.target_symbol_seen; |
d0a7f5a9 | 6250 | |
d0ea46ce DS |
6251 | // If during target-variable-expanding the probe, we added a new block |
6252 | // of code, add it to the start of the probe. | |
6253 | if (v.add_block) | |
6254 | this->body = new block(v.add_block, this->body); | |
6255 | // If when target-variable-expanding the probe, we added a new | |
6256 | // probe, add it in a new file to the list of files to be processed. | |
6257 | if (v.add_probe) | |
6258 | { | |
6259 | stapfile *f = new stapfile; | |
6260 | f->probes.push_back(v.add_probe); | |
6261 | s.files.push_back(f); | |
6262 | } | |
6263 | ||
d0a7f5a9 FCE |
6264 | // Reset the sole element of the "locations" vector as a |
6265 | // "reverse-engineered" form of the incoming (q.base_loc) probe | |
6266 | // point. This allows a user to see what program etc. | |
6267 | // number any particular match of the wildcards. | |
6268 | ||
6269 | vector<probe_point::component*> comps; | |
6270 | if (hp) | |
6271 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(path))); | |
e56e51c9 | 6272 | else if (pid != 0) |
06aca46a | 6273 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_number(pid))); |
e56e51c9 FCE |
6274 | else |
6275 | comps.push_back (new probe_point::component(TOK_PROCESS)); | |
6276 | ||
d0a7f5a9 FCE |
6277 | switch (flags) |
6278 | { | |
6279 | case UDPF_THREAD_BEGIN: | |
6280 | comps.push_back (new probe_point::component(TOK_THREAD)); | |
6281 | comps.push_back (new probe_point::component(TOK_BEGIN)); | |
6282 | break; | |
6283 | case UDPF_THREAD_END: | |
6284 | comps.push_back (new probe_point::component(TOK_THREAD)); | |
6285 | comps.push_back (new probe_point::component(TOK_END)); | |
6286 | break; | |
6287 | case UDPF_SYSCALL: | |
6288 | comps.push_back (new probe_point::component(TOK_SYSCALL)); | |
6289 | break; | |
6290 | case UDPF_SYSCALL_RETURN: | |
6291 | comps.push_back (new probe_point::component(TOK_SYSCALL)); | |
6292 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
6293 | break; | |
6294 | case UDPF_BEGIN: | |
6295 | comps.push_back (new probe_point::component(TOK_BEGIN)); | |
6296 | break; | |
6297 | case UDPF_END: | |
6298 | comps.push_back (new probe_point::component(TOK_END)); | |
6299 | break; | |
06aca46a | 6300 | default: |
d0a7f5a9 FCE |
6301 | assert (0); |
6302 | } | |
6303 | ||
6304 | // Overwrite it. | |
6305 | this->sole_location()->components = comps; | |
935447c8 DS |
6306 | } |
6307 | ||
6308 | ||
6309 | void | |
6310 | utrace_derived_probe::join_group (systemtap_session& s) | |
6311 | { | |
6312 | if (! s.utrace_derived_probes) | |
14cdaa0b | 6313 | { |
935447c8 | 6314 | s.utrace_derived_probes = new utrace_derived_probe_group (); |
14cdaa0b | 6315 | } |
935447c8 DS |
6316 | s.utrace_derived_probes->enroll (this); |
6317 | ||
93646f4d | 6318 | enable_task_finder(s); |
935447c8 DS |
6319 | } |
6320 | ||
6321 | ||
d0ea46ce DS |
6322 | void |
6323 | utrace_var_expanding_visitor::visit_target_symbol_cached (target_symbol* e) | |
6324 | { | |
6325 | // Get the full name of the target symbol. | |
6326 | stringstream ts_name_stream; | |
6327 | e->print(ts_name_stream); | |
6328 | string ts_name = ts_name_stream.str(); | |
6329 | ||
6330 | // Check and make sure we haven't already seen this target | |
6331 | // variable in this return probe. If we have, just return our | |
6332 | // last replacement. | |
6333 | map<string, symbol *>::iterator i = return_ts_map.find(ts_name); | |
6334 | if (i != return_ts_map.end()) | |
6335 | { | |
6336 | provide (i->second); | |
6337 | return; | |
6338 | } | |
6339 | ||
6340 | // We've got to do several things here to handle target | |
6341 | // variables in return probes. | |
6342 | ||
6343 | // (1) Synthesize a global array which is the cache of the | |
6344 | // target variable value. We don't need a nesting level counter | |
6345 | // like the dwarf_var_expanding_visitor::visit_target_symbol() | |
6346 | // does since a particular thread can only be in one system | |
6347 | // calls at a time. The array will look like this: | |
6348 | // | |
6349 | // _utrace_tvar_{name}_{num} | |
6350 | string aname = (string("_utrace_tvar_") | |
6351 | + e->base_name.substr(1) | |
6352 | + "_" + lex_cast<string>(tick++)); | |
6353 | vardecl* vd = new vardecl; | |
6354 | vd->name = aname; | |
6355 | vd->tok = e->tok; | |
6356 | sess.globals.push_back (vd); | |
6357 | ||
6358 | // (2) Create a new code block we're going to insert at the | |
6359 | // beginning of this probe to get the cached value into a | |
6360 | // temporary variable. We'll replace the target variable | |
6361 | // reference with the temporary variable reference. The code | |
6362 | // will look like this: | |
6363 | // | |
6364 | // _utrace_tvar_tid = tid() | |
6365 | // _utrace_tvar_{name}_{num}_tmp | |
6366 | // = _utrace_tvar_{name}_{num}[_utrace_tvar_tid] | |
6367 | // delete _utrace_tvar_{name}_{num}[_utrace_tvar_tid] | |
6368 | ||
6369 | // (2a) Synthesize the tid temporary expression, which will look | |
6370 | // like this: | |
6371 | // | |
6372 | // _utrace_tvar_tid = tid() | |
6373 | symbol* tidsym = new symbol; | |
6374 | tidsym->name = string("_utrace_tvar_tid"); | |
6375 | tidsym->tok = e->tok; | |
6376 | ||
6377 | if (add_block == NULL) | |
6378 | { | |
6379 | add_block = new block; | |
6380 | add_block->tok = e->tok; | |
6381 | ||
6382 | // Synthesize a functioncall to grab the thread id. | |
6383 | functioncall* fc = new functioncall; | |
6384 | fc->tok = e->tok; | |
6385 | fc->function = string("tid"); | |
6386 | ||
6387 | // Assign the tid to '_utrace_tvar_tid'. | |
6388 | assignment* a = new assignment; | |
6389 | a->tok = e->tok; | |
6390 | a->op = "="; | |
6391 | a->left = tidsym; | |
6392 | a->right = fc; | |
6393 | ||
6394 | expr_statement* es = new expr_statement; | |
6395 | es->tok = e->tok; | |
6396 | es->value = a; | |
6397 | add_block->statements.push_back (es); | |
6398 | } | |
6399 | ||
6400 | // (2b) Synthesize an array reference and assign it to a | |
6401 | // temporary variable (that we'll use as replacement for the | |
6402 | // target variable reference). It will look like this: | |
6403 | // | |
6404 | // _utrace_tvar_{name}_{num}_tmp | |
6405 | // = _utrace_tvar_{name}_{num}[_utrace_tvar_tid] | |
6406 | ||
6407 | arrayindex* ai_tvar = new arrayindex; | |
6408 | ai_tvar->tok = e->tok; | |
6409 | ||
6410 | symbol* sym = new symbol; | |
6411 | sym->name = aname; | |
6412 | sym->tok = e->tok; | |
6413 | ai_tvar->base = sym; | |
6414 | ||
6415 | ai_tvar->indexes.push_back(tidsym); | |
6416 | ||
6417 | symbol* tmpsym = new symbol; | |
6418 | tmpsym->name = aname + "_tmp"; | |
6419 | tmpsym->tok = e->tok; | |
6420 | ||
6421 | assignment* a = new assignment; | |
6422 | a->tok = e->tok; | |
6423 | a->op = "="; | |
6424 | a->left = tmpsym; | |
6425 | a->right = ai_tvar; | |
6426 | ||
6427 | expr_statement* es = new expr_statement; | |
6428 | es->tok = e->tok; | |
6429 | es->value = a; | |
6430 | ||
6431 | add_block->statements.push_back (es); | |
6432 | ||
6433 | // (2c) Delete the array value. It will look like this: | |
6434 | // | |
6435 | // delete _utrace_tvar_{name}_{num}[_utrace_tvar_tid] | |
6436 | ||
6437 | delete_statement* ds = new delete_statement; | |
6438 | ds->tok = e->tok; | |
6439 | ds->value = ai_tvar; | |
6440 | add_block->statements.push_back (ds); | |
6441 | ||
6442 | // (3) We need an entry probe that saves the value for us in the | |
6443 | // global array we created. Create the entry probe, which will | |
6444 | // look like this: | |
6445 | // | |
6446 | // probe process(PATH_OR_PID).syscall { | |
6447 | // _utrace_tvar_tid = tid() | |
6448 | // _utrace_tvar_{name}_{num}[_utrace_tvar_tid] = ${param} | |
6449 | // } | |
6450 | // | |
6451 | // Why the temporary for tid()? If we end up caching more | |
6452 | // than one target variable, we can reuse the temporary instead | |
6453 | // of calling tid() multiple times. | |
6454 | ||
6455 | if (add_probe == NULL) | |
6456 | { | |
6457 | add_probe = new probe; | |
6458 | add_probe->tok = e->tok; | |
6459 | ||
6460 | // We need the name of the current probe point, minus the | |
6461 | // ".return". Create a new probe point, copying all the | |
6462 | // components, stopping when we see the ".return" | |
6463 | // component. | |
6464 | probe_point* pp = new probe_point; | |
6465 | for (unsigned c = 0; c < base_loc->components.size(); c++) | |
6466 | { | |
6467 | if (base_loc->components[c]->functor == "return") | |
6468 | break; | |
6469 | else | |
6470 | pp->components.push_back(base_loc->components[c]); | |
6471 | } | |
6472 | pp->tok = e->tok; | |
6473 | pp->optional = base_loc->optional; | |
6474 | add_probe->locations.push_back(pp); | |
6475 | ||
6476 | add_probe->body = new block; | |
6477 | add_probe->body->tok = e->tok; | |
6478 | ||
6479 | // Synthesize a functioncall to grab the thread id. | |
6480 | functioncall* fc = new functioncall; | |
6481 | fc->tok = e->tok; | |
6482 | fc->function = string("tid"); | |
6483 | ||
6484 | // Assign the tid to '_utrace_tvar_tid'. | |
6485 | assignment* a = new assignment; | |
6486 | a->tok = e->tok; | |
6487 | a->op = "="; | |
6488 | a->left = tidsym; | |
6489 | a->right = fc; | |
6490 | ||
6491 | expr_statement* es = new expr_statement; | |
6492 | es->tok = e->tok; | |
6493 | es->value = a; | |
6494 | add_probe->body = new block(add_probe->body, es); | |
6495 | ||
6496 | vardecl* vd = new vardecl; | |
6497 | vd->tok = e->tok; | |
6498 | vd->name = tidsym->name; | |
6499 | vd->type = pe_long; | |
6500 | vd->set_arity(0); | |
6501 | add_probe->locals.push_back(vd); | |
6502 | } | |
6503 | ||
6504 | // Save the value, like this: | |
6505 | // | |
6506 | // _utrace_tvar_{name}_{num}[_utrace_tvar_tid] = ${param} | |
6507 | a = new assignment; | |
6508 | a->tok = e->tok; | |
6509 | a->op = "="; | |
6510 | a->left = ai_tvar; | |
6511 | a->right = e; | |
6512 | ||
6513 | es = new expr_statement; | |
6514 | es->tok = e->tok; | |
6515 | es->value = a; | |
6516 | ||
6517 | add_probe->body = new block(add_probe->body, es); | |
6518 | ||
6519 | // (4) Provide the '_utrace_tvar_{name}_{num}_tmp' variable to | |
6520 | // our parent so it can be used as a substitute for the target | |
6521 | // symbol. | |
6522 | provide (tmpsym); | |
6523 | ||
6524 | // (5) Remember this replacement since we might be able to reuse | |
6525 | // it later if the same return probe references this target | |
6526 | // symbol again. | |
6527 | return_ts_map[ts_name] = tmpsym; | |
6528 | return; | |
6529 | } | |
6530 | ||
6531 | ||
935447c8 | 6532 | void |
de688825 | 6533 | utrace_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) |
935447c8 | 6534 | { |
6270adc1 | 6535 | string argnum_s = e->base_name.substr(4,e->base_name.length()-4); |
3a4e19b8 | 6536 | int argnum = lex_cast<int>(argnum_s); |
935447c8 | 6537 | |
6270adc1 MH |
6538 | if (flags != UDPF_SYSCALL) |
6539 | throw semantic_error ("only \"process(PATH_OR_PID).syscall\" support $argN.", e->tok); | |
935447c8 | 6540 | |
6270adc1 MH |
6541 | if (e->components.size() > 0) |
6542 | { | |
6543 | switch (e->components[0].first) | |
6544 | { | |
6545 | case target_symbol::comp_literal_array_index: | |
6546 | throw semantic_error("utrace target variable '$argN' may not be used as array", | |
6547 | e->tok); | |
6548 | break; | |
6549 | case target_symbol::comp_struct_member: | |
6550 | throw semantic_error("utrace target variable '$argN' may not be used as a structure", | |
6551 | e->tok); | |
6552 | break; | |
6553 | default: | |
6554 | throw semantic_error ("invalid use of utrace target variable '$argN'", | |
6555 | e->tok); | |
6556 | break; | |
6557 | } | |
6558 | } | |
6559 | ||
6560 | // FIXME: max argnument number should not be hardcoded. | |
6561 | if (argnum < 1 || argnum > 6) | |
6562 | throw semantic_error ("invalid syscall argument number (1-6)", e->tok); | |
6563 | ||
6564 | bool lvalue = is_active_lvalue(e); | |
6565 | if (lvalue) | |
6566 | throw semantic_error("utrace '$argN' variable is read-only", e->tok); | |
935447c8 | 6567 | |
6270adc1 MH |
6568 | // Remember that we've seen a target variable. |
6569 | target_symbol_seen = true; | |
6570 | ||
6571 | // We're going to substitute a synthesized '_utrace_syscall_arg' | |
6572 | // function call for the '$argN' reference. | |
6573 | functioncall* n = new functioncall; | |
6574 | n->tok = e->tok; | |
6575 | n->function = "_utrace_syscall_arg"; | |
6576 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
6577 | ||
6578 | literal_number *num = new literal_number(argnum - 1); | |
6579 | num->tok = e->tok; | |
6580 | n->args.push_back(num); | |
6581 | ||
4ed05b15 | 6582 | provide (n); |
6270adc1 MH |
6583 | } |
6584 | ||
6585 | void | |
de688825 | 6586 | utrace_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
6270adc1 | 6587 | { |
5d67b47c MH |
6588 | string sname = e->base_name; |
6589 | ||
935447c8 DS |
6590 | if (e->components.size() > 0) |
6591 | { | |
6592 | switch (e->components[0].first) | |
6593 | { | |
6594 | case target_symbol::comp_literal_array_index: | |
5d67b47c | 6595 | throw semantic_error("utrace target variable '" + sname + "' may not be used as array", |
935447c8 DS |
6596 | e->tok); |
6597 | break; | |
6598 | case target_symbol::comp_struct_member: | |
5d67b47c | 6599 | throw semantic_error("utrace target variable '" + sname + "' may not be used as a structure", |
935447c8 DS |
6600 | e->tok); |
6601 | break; | |
6602 | default: | |
5d67b47c | 6603 | throw semantic_error ("invalid use of utrace target variable '" + sname + "'", |
935447c8 DS |
6604 | e->tok); |
6605 | break; | |
6606 | } | |
6607 | } | |
6608 | ||
6609 | bool lvalue = is_active_lvalue(e); | |
6610 | if (lvalue) | |
5d67b47c MH |
6611 | throw semantic_error("utrace '" + sname + "' variable is read-only", e->tok); |
6612 | ||
6613 | string fname; | |
6614 | if (sname == "$return") | |
6615 | { | |
6616 | if (flags != UDPF_SYSCALL_RETURN) | |
6617 | throw semantic_error ("only \"process(PATH_OR_PID).syscall.return\" support $return.", e->tok); | |
6618 | fname = "_utrace_syscall_return"; | |
6619 | } | |
d0ea46ce DS |
6620 | else if (sname == "$syscall") |
6621 | { | |
6622 | // If we've got a syscall entry probe, we can just call the | |
6623 | // right function. | |
6624 | if (flags == UDPF_SYSCALL) { | |
6625 | fname = "_utrace_syscall_nr"; | |
6626 | } | |
6627 | // If we're in a syscal return probe, we can't really access | |
6628 | // $syscall. So, similar to what | |
6629 | // dwarf_var_expanding_visitor::visit_target_symbol() does, | |
6630 | // we'll create an syscall entry probe to cache $syscall, then | |
6631 | // we'll access the cached value in the syscall return probe. | |
6632 | else { | |
6633 | visit_target_symbol_cached (e); | |
6634 | ||
6635 | // Remember that we've seen a target variable. | |
6636 | target_symbol_seen = true; | |
6637 | return; | |
6638 | } | |
6639 | } | |
5d67b47c | 6640 | else |
d0ea46ce DS |
6641 | { |
6642 | throw semantic_error ("unknown target variable", e->tok); | |
6643 | } | |
935447c8 DS |
6644 | |
6645 | // Remember that we've seen a target variable. | |
6646 | target_symbol_seen = true; | |
6647 | ||
6270adc1 | 6648 | // We're going to substitute a synthesized '_utrace_syscall_nr' |
a7a68293 | 6649 | // function call for the '$syscall' reference. |
935447c8 DS |
6650 | functioncall* n = new functioncall; |
6651 | n->tok = e->tok; | |
5d67b47c | 6652 | n->function = fname; |
935447c8 DS |
6653 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session |
6654 | ||
4ed05b15 | 6655 | provide (n); |
935447c8 DS |
6656 | } |
6657 | ||
6270adc1 | 6658 | void |
de688825 | 6659 | utrace_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
6270adc1 MH |
6660 | { |
6661 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
6662 | ||
6663 | if (flags != UDPF_SYSCALL && flags != UDPF_SYSCALL_RETURN) | |
6664 | throw semantic_error ("only \"process(PATH_OR_PID).syscall\" and \"process(PATH_OR_PID).syscall.return\" probes support target symbols", | |
6665 | e->tok); | |
6666 | ||
6667 | if (e->base_name.substr(0,4) == "$arg") | |
6668 | visit_target_symbol_arg(e); | |
5d67b47c MH |
6669 | else if (e->base_name == "$syscall" || e->base_name == "$return") |
6670 | visit_target_symbol_context(e); | |
6270adc1 | 6671 | else |
5d67b47c | 6672 | throw semantic_error ("invalid target symbol for utrace probe, $syscall, $return or $argN expected", |
6270adc1 MH |
6673 | e->tok); |
6674 | } | |
6675 | ||
935447c8 DS |
6676 | |
6677 | struct utrace_builder: public derived_probe_builder | |
6678 | { | |
6679 | utrace_builder() {} | |
6680 | virtual void build(systemtap_session & sess, | |
6681 | probe * base, | |
6682 | probe_point * location, | |
86bf665e | 6683 | literal_map_t const & parameters, |
935447c8 DS |
6684 | vector<derived_probe *> & finished_results) |
6685 | { | |
6686 | string path; | |
6687 | int64_t pid; | |
6688 | ||
6689 | bool has_path = get_param (parameters, TOK_PROCESS, path); | |
6690 | bool has_pid = get_param (parameters, TOK_PROCESS, pid); | |
6691 | enum utrace_derived_probe_flags flags = UDPF_NONE; | |
d0a7f5a9 | 6692 | |
eff6ac72 DS |
6693 | if (has_null_param (parameters, TOK_THREAD)) |
6694 | { | |
6695 | if (has_null_param (parameters, TOK_BEGIN)) | |
6696 | flags = UDPF_THREAD_BEGIN; | |
6697 | else if (has_null_param (parameters, TOK_END)) | |
6698 | flags = UDPF_THREAD_END; | |
6699 | } | |
12b21830 | 6700 | else if (has_null_param (parameters, TOK_SYSCALL)) |
935447c8 DS |
6701 | { |
6702 | if (has_null_param (parameters, TOK_RETURN)) | |
eff6ac72 | 6703 | flags = UDPF_SYSCALL_RETURN; |
935447c8 | 6704 | else |
eff6ac72 | 6705 | flags = UDPF_SYSCALL; |
935447c8 | 6706 | } |
eff6ac72 DS |
6707 | else if (has_null_param (parameters, TOK_BEGIN)) |
6708 | flags = UDPF_BEGIN; | |
6709 | else if (has_null_param (parameters, TOK_END)) | |
6710 | flags = UDPF_END; | |
935447c8 | 6711 | |
986e98de DS |
6712 | // If we didn't get a path or pid, this means to probe everything. |
6713 | // Convert this to a pid-based probe. | |
6714 | if (! has_path && ! has_pid) | |
6715 | { | |
6716 | has_path = false; | |
6717 | path.clear(); | |
6718 | has_pid = true; | |
6719 | pid = 0; | |
6720 | } | |
c569c2e4 FCE |
6721 | else if (has_path) |
6722 | { | |
6723 | path = find_executable (path); | |
6724 | sess.unwindsym_modules.insert (path); | |
6725 | } | |
986e98de | 6726 | else if (has_pid) |
6cdf2889 | 6727 | { |
c569c2e4 | 6728 | // We can't probe 'init' (pid 1). XXX: where does this limitation come from? |
6cdf2889 DS |
6729 | if (pid < 2) |
6730 | throw semantic_error ("process pid must be greater than 1", | |
6731 | location->tok); | |
28d29bd3 FCE |
6732 | |
6733 | // XXX: could we use /proc/$pid/exe in unwindsym_modules and elsewhere? | |
6cdf2889 | 6734 | } |
0744f531 | 6735 | |
935447c8 DS |
6736 | finished_results.push_back(new utrace_derived_probe(sess, base, location, |
6737 | has_path, path, pid, | |
6738 | flags)); | |
6739 | } | |
6740 | }; | |
6741 | ||
6742 | ||
6743 | void | |
6744 | utrace_derived_probe_group::enroll (utrace_derived_probe* p) | |
6745 | { | |
6746 | if (p->has_path) | |
6747 | probes_by_path[p->path].push_back(p); | |
6748 | else | |
6749 | probes_by_pid[p->pid].push_back(p); | |
6750 | num_probes++; | |
6751 | flags_seen[p->flags] = true; | |
6752 | ||
6753 | // XXX: multiple exec probes (for instance) for the same path (or | |
6754 | // pid) should all share a utrace report function, and have their | |
6755 | // handlers executed sequentially. | |
6756 | } | |
6757 | ||
6758 | ||
6759 | void | |
6760 | utrace_derived_probe_group::emit_probe_decl (systemtap_session& s, | |
6761 | utrace_derived_probe *p) | |
6762 | { | |
6763 | s.op->newline() << "{"; | |
6764 | s.op->line() << " .tgt={"; | |
6765 | ||
6766 | if (p->has_path) | |
6767 | { | |
6768 | s.op->line() << " .pathname=\"" << p->path << "\","; | |
6769 | s.op->line() << " .pid=0,"; | |
6770 | } | |
6771 | else | |
6772 | { | |
6773 | s.op->line() << " .pathname=NULL,"; | |
6774 | s.op->line() << " .pid=" << p->pid << ","; | |
6775 | } | |
6776 | ||
6777 | s.op->line() << " .callback=&_stp_utrace_probe_cb,"; | |
782040b3 DS |
6778 | s.op->line() << " .mmap_callback=NULL,"; |
6779 | s.op->line() << " .munmap_callback=NULL,"; | |
6780 | s.op->line() << " .mprotect_callback=NULL,"; | |
935447c8 DS |
6781 | s.op->line() << " },"; |
6782 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
6783 | s.op->line() << " .ph=&" << p->name << ","; | |
6784 | ||
6785 | // Handle flags | |
6786 | switch (p->flags) | |
6787 | { | |
b20bac3a DS |
6788 | // Notice that we'll just call the probe directly when we get |
6789 | // notified, since the task_finder layer stops the thread for us. | |
eff6ac72 DS |
6790 | case UDPF_BEGIN: // process begin |
6791 | s.op->line() << " .flags=(UDPF_BEGIN),"; | |
935447c8 | 6792 | break; |
eff6ac72 DS |
6793 | case UDPF_THREAD_BEGIN: // thread begin |
6794 | s.op->line() << " .flags=(UDPF_THREAD_BEGIN),"; | |
159cb109 | 6795 | break; |
eff6ac72 DS |
6796 | |
6797 | // Notice we're not setting up a .ops/.report_death handler for | |
6798 | // either UDPF_END or UDPF_THREAD_END. Instead, we'll just call | |
6799 | // the probe directly when we get notified. | |
6800 | case UDPF_END: // process end | |
6801 | s.op->line() << " .flags=(UDPF_END),"; | |
935447c8 | 6802 | break; |
eff6ac72 DS |
6803 | case UDPF_THREAD_END: // thread end |
6804 | s.op->line() << " .flags=(UDPF_THREAD_END),"; | |
6805 | break; | |
6806 | ||
6807 | // For UDPF_SYSCALL/UDPF_SYSCALL_RETURN probes, the .report_death | |
6808 | // handler isn't strictly necessary. However, it helps to keep | |
b20bac3a DS |
6809 | // our attaches/detaches symmetrical. Since the task_finder layer |
6810 | // stops the thread, that works around bug 6841. | |
eff6ac72 DS |
6811 | case UDPF_SYSCALL: |
6812 | s.op->line() << " .flags=(UDPF_SYSCALL),"; | |
b20bac3a DS |
6813 | s.op->line() << " .ops={ .report_syscall_entry=stap_utrace_probe_syscall, .report_death=stap_utrace_task_finder_report_death },"; |
6814 | s.op->line() << " .events=(UTRACE_EVENT(SYSCALL_ENTRY)|UTRACE_EVENT(DEATH)),"; | |
935447c8 | 6815 | break; |
eff6ac72 DS |
6816 | case UDPF_SYSCALL_RETURN: |
6817 | s.op->line() << " .flags=(UDPF_SYSCALL_RETURN),"; | |
b20bac3a DS |
6818 | s.op->line() << " .ops={ .report_syscall_exit=stap_utrace_probe_syscall, .report_death=stap_utrace_task_finder_report_death },"; |
6819 | s.op->line() << " .events=(UTRACE_EVENT(SYSCALL_EXIT)|UTRACE_EVENT(DEATH)),"; | |
935447c8 | 6820 | break; |
930a1798 | 6821 | |
a21d81ec DS |
6822 | case UDPF_NONE: |
6823 | s.op->line() << " .flags=(UDPF_NONE),"; | |
6824 | s.op->line() << " .ops={ },"; | |
6825 | s.op->line() << " .events=0,"; | |
6826 | break; | |
935447c8 DS |
6827 | default: |
6828 | throw semantic_error ("bad utrace probe flag"); | |
6829 | break; | |
6830 | } | |
6831 | s.op->line() << " .engine_attached=0,"; | |
6832 | s.op->line() << " },"; | |
6833 | } | |
6834 | ||
6835 | ||
6836 | void | |
6837 | utrace_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6838 | { | |
6839 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
6840 | return; | |
6841 | ||
6842 | s.op->newline(); | |
6843 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
104c6237 | 6844 | |
eff6ac72 DS |
6845 | s.op->newline() << "enum utrace_derived_probe_flags {"; |
6846 | s.op->indent(1); | |
6847 | s.op->newline() << "UDPF_NONE,"; | |
6848 | s.op->newline() << "UDPF_BEGIN,"; | |
6849 | s.op->newline() << "UDPF_END,"; | |
6850 | s.op->newline() << "UDPF_THREAD_BEGIN,"; | |
6851 | s.op->newline() << "UDPF_THREAD_END,"; | |
6852 | s.op->newline() << "UDPF_SYSCALL,"; | |
6853 | s.op->newline() << "UDPF_SYSCALL_RETURN,"; | |
6854 | s.op->newline() << "UDPF_NFLAGS"; | |
6855 | s.op->newline(-1) << "};"; | |
6856 | ||
935447c8 DS |
6857 | s.op->newline() << "struct stap_utrace_probe {"; |
6858 | s.op->indent(1); | |
6859 | s.op->newline() << "struct stap_task_finder_target tgt;"; | |
6860 | s.op->newline() << "const char *pp;"; | |
6861 | s.op->newline() << "void (*ph) (struct context*);"; | |
eff6ac72 | 6862 | s.op->newline() << "enum utrace_derived_probe_flags flags;"; |
935447c8 | 6863 | s.op->newline() << "struct utrace_engine_ops ops;"; |
eff6ac72 | 6864 | s.op->newline() << "unsigned long events;"; |
935447c8 DS |
6865 | s.op->newline() << "int engine_attached;"; |
6866 | s.op->newline(-1) << "};"; | |
6867 | ||
eff6ac72 | 6868 | |
b20bac3a DS |
6869 | // Output handler function for UDPF_BEGIN, UDPF_THREAD_BEGIN, |
6870 | // UDPF_END, and UDPF_THREAD_END | |
6871 | if (flags_seen[UDPF_BEGIN] || flags_seen[UDPF_THREAD_BEGIN] | |
6872 | || flags_seen[UDPF_END] || flags_seen[UDPF_THREAD_END]) | |
159cb109 | 6873 | { |
935447c8 DS |
6874 | s.op->newline() << "static void stap_utrace_probe_handler(struct task_struct *tsk, struct stap_utrace_probe *p) {"; |
6875 | s.op->indent(1); | |
6876 | ||
c12d974f | 6877 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
935447c8 DS |
6878 | |
6879 | // call probe function | |
6880 | s.op->newline() << "(*p->ph) (c);"; | |
6881 | common_probe_entryfn_epilogue (s.op); | |
6882 | ||
6883 | s.op->newline() << "return;"; | |
6884 | s.op->newline(-1) << "}"; | |
159cb109 | 6885 | } |
935447c8 DS |
6886 | |
6887 | // Output handler function for SYSCALL_ENTRY and SYSCALL_EXIT events | |
eff6ac72 | 6888 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
935447c8 | 6889 | { |
4550733e | 6890 | s.op->newline() << "#ifdef UTRACE_ORIG_VERSION"; |
935447c8 | 6891 | s.op->newline() << "static u32 stap_utrace_probe_syscall(struct utrace_attached_engine *engine, struct task_struct *tsk, struct pt_regs *regs) {"; |
4550733e DS |
6892 | s.op->newline() << "#else"; |
6893 | s.op->newline() << "static u32 stap_utrace_probe_syscall(enum utrace_resume_action action, struct utrace_attached_engine *engine, struct task_struct *tsk, struct pt_regs *regs) {"; | |
6894 | s.op->newline() << "#endif"; | |
6895 | ||
935447c8 DS |
6896 | s.op->indent(1); |
6897 | s.op->newline() << "struct stap_utrace_probe *p = (struct stap_utrace_probe *)engine->data;"; | |
6898 | ||
c12d974f | 6899 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
935447c8 DS |
6900 | s.op->newline() << "c->regs = regs;"; |
6901 | ||
6902 | // call probe function | |
6903 | s.op->newline() << "(*p->ph) (c);"; | |
6904 | common_probe_entryfn_epilogue (s.op); | |
6905 | ||
b20bac3a DS |
6906 | s.op->newline() << "if ((atomic_read (&session_state) != STAP_SESSION_STARTING) && (atomic_read (&session_state) != STAP_SESSION_RUNNING)) {"; |
6907 | s.op->indent(1); | |
6908 | s.op->newline() << "debug_task_finder_detach();"; | |
6909 | s.op->newline() << "return UTRACE_DETACH;"; | |
6910 | s.op->newline(-1) << "}"; | |
4550733e | 6911 | s.op->newline() << "return UTRACE_RESUME;"; |
935447c8 DS |
6912 | s.op->newline(-1) << "}"; |
6913 | } | |
6914 | ||
eff6ac72 | 6915 | // Output task_finder callback routine that gets called for all |
935447c8 | 6916 | // utrace probe types. |
46b3c6cd | 6917 | s.op->newline() << "static int _stp_utrace_probe_cb(struct stap_task_finder_target *tgt, struct task_struct *tsk, int register_p, int process_p) {"; |
935447c8 DS |
6918 | s.op->indent(1); |
6919 | s.op->newline() << "int rc = 0;"; | |
6920 | s.op->newline() << "struct stap_utrace_probe *p = container_of(tgt, struct stap_utrace_probe, tgt);"; | |
6921 | s.op->newline() << "struct utrace_attached_engine *engine;"; | |
6922 | ||
935447c8 DS |
6923 | s.op->newline() << "if (register_p) {"; |
6924 | s.op->indent(1); | |
6925 | ||
0744f531 | 6926 | s.op->newline() << "switch (p->flags) {"; |
935447c8 | 6927 | s.op->indent(1); |
eff6ac72 DS |
6928 | |
6929 | // When receiving a UTRACE_EVENT(CLONE) event, we can't call the | |
6930 | // begin/thread.begin probe directly. So, we'll just attach an | |
6931 | // engine that waits for the thread to quiesce. When the thread | |
6932 | // quiesces, then call the probe. | |
6933 | if (flags_seen[UDPF_BEGIN]) | |
6934 | { | |
6935 | s.op->newline() << "case UDPF_BEGIN:"; | |
37e24fbe | 6936 | s.op->indent(1); |
eff6ac72 | 6937 | s.op->newline() << "if (process_p) {"; |
37e24fbe | 6938 | s.op->indent(1); |
b20bac3a | 6939 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
37e24fbe DS |
6940 | s.op->newline(-1) << "}"; |
6941 | s.op->newline() << "break;"; | |
6942 | s.op->indent(-1); | |
eff6ac72 DS |
6943 | } |
6944 | if (flags_seen[UDPF_THREAD_BEGIN]) | |
6945 | { | |
6946 | s.op->newline() << "case UDPF_THREAD_BEGIN:"; | |
0744f531 | 6947 | s.op->indent(1); |
eff6ac72 | 6948 | s.op->newline() << "if (! process_p) {"; |
cfac4b1f | 6949 | s.op->indent(1); |
b20bac3a | 6950 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
cfac4b1f | 6951 | s.op->newline(-1) << "}"; |
0744f531 DS |
6952 | s.op->newline() << "break;"; |
6953 | s.op->indent(-1); | |
eff6ac72 DS |
6954 | } |
6955 | ||
6956 | // For end/thread_end probes, do nothing at registration time. | |
6957 | // We'll handle these in the 'register_p == 0' case. | |
6958 | if (flags_seen[UDPF_END] || flags_seen[UDPF_THREAD_END]) | |
0744f531 | 6959 | { |
eff6ac72 DS |
6960 | s.op->newline() << "case UDPF_END:"; |
6961 | s.op->newline() << "case UDPF_THREAD_END:"; | |
0744f531 DS |
6962 | s.op->indent(1); |
6963 | s.op->newline() << "break;"; | |
6964 | s.op->indent(-1); | |
6965 | } | |
eff6ac72 | 6966 | |
37e24fbe | 6967 | // Attach an engine for SYSCALL_ENTRY and SYSCALL_EXIT events. |
eff6ac72 | 6968 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
0744f531 | 6969 | { |
eff6ac72 DS |
6970 | s.op->newline() << "case UDPF_SYSCALL:"; |
6971 | s.op->newline() << "case UDPF_SYSCALL_RETURN:"; | |
0744f531 | 6972 | s.op->indent(1); |
eff6ac72 DS |
6973 | s.op->newline() << "rc = stap_utrace_attach(tsk, &p->ops, p, p->events);"; |
6974 | s.op->newline() << "if (rc == 0) {"; | |
0744f531 | 6975 | s.op->indent(1); |
0744f531 DS |
6976 | s.op->newline() << "p->engine_attached = 1;"; |
6977 | s.op->newline(-1) << "}"; | |
6978 | s.op->newline() << "break;"; | |
6979 | s.op->indent(-1); | |
6980 | } | |
eff6ac72 DS |
6981 | |
6982 | s.op->newline() << "default:"; | |
6983 | s.op->indent(1); | |
6984 | s.op->newline() << "_stp_error(\"unhandled flag value %d at %s:%d\", p->flags, __FUNCTION__, __LINE__);"; | |
6985 | s.op->newline() << "break;"; | |
6986 | s.op->indent(-1); | |
935447c8 DS |
6987 | s.op->newline(-1) << "}"; |
6988 | s.op->newline(-1) << "}"; | |
eff6ac72 | 6989 | |
935447c8 | 6990 | // Since this engine could be attached to multiple threads, don't |
b20bac3a DS |
6991 | // call stap_utrace_detach_ops() here, only call |
6992 | // stap_utrace_detach() as necessary. | |
0744f531 | 6993 | s.op->newline() << "else {"; |
935447c8 | 6994 | s.op->indent(1); |
37e24fbe DS |
6995 | s.op->newline() << "switch (p->flags) {"; |
6996 | s.op->indent(1); | |
d0ea46ce | 6997 | // For end probes, go ahead and call the probe directly. |
eff6ac72 | 6998 | if (flags_seen[UDPF_END]) |
0744f531 | 6999 | { |
eff6ac72 | 7000 | s.op->newline() << "case UDPF_END:"; |
37e24fbe | 7001 | s.op->indent(1); |
eff6ac72 | 7002 | s.op->newline() << "if (process_p) {"; |
0744f531 | 7003 | s.op->indent(1); |
0744f531 DS |
7004 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
7005 | s.op->newline(-1) << "}"; | |
37e24fbe DS |
7006 | s.op->newline() << "break;"; |
7007 | s.op->indent(-1); | |
0744f531 | 7008 | } |
eff6ac72 | 7009 | if (flags_seen[UDPF_THREAD_END]) |
37e24fbe | 7010 | { |
eff6ac72 | 7011 | s.op->newline() << "case UDPF_THREAD_END:"; |
37e24fbe | 7012 | s.op->indent(1); |
eff6ac72 DS |
7013 | s.op->newline() << "if (! process_p) {"; |
7014 | s.op->indent(1); | |
7015 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; | |
7016 | s.op->newline(-1) << "}"; | |
7017 | s.op->newline() << "break;"; | |
7018 | s.op->indent(-1); | |
7019 | } | |
7020 | ||
b20bac3a DS |
7021 | // For begin/thread_begin probes, we don't need to do anything. |
7022 | if (flags_seen[UDPF_BEGIN] || flags_seen[UDPF_THREAD_BEGIN]) | |
7023 | { | |
eff6ac72 DS |
7024 | s.op->newline() << "case UDPF_BEGIN:"; |
7025 | s.op->newline() << "case UDPF_THREAD_BEGIN:"; | |
b20bac3a DS |
7026 | s.op->indent(1); |
7027 | s.op->newline() << "break;"; | |
7028 | s.op->indent(-1); | |
7029 | } | |
28d29bd3 | 7030 | |
b20bac3a DS |
7031 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
7032 | { | |
eff6ac72 DS |
7033 | s.op->newline() << "case UDPF_SYSCALL:"; |
7034 | s.op->newline() << "case UDPF_SYSCALL_RETURN:"; | |
37e24fbe | 7035 | s.op->indent(1); |
41211ba3 | 7036 | s.op->newline() << "stap_utrace_detach(tsk, &p->ops);"; |
37e24fbe DS |
7037 | s.op->newline() << "break;"; |
7038 | s.op->indent(-1); | |
7039 | } | |
eff6ac72 DS |
7040 | |
7041 | s.op->newline() << "default:"; | |
7042 | s.op->indent(1); | |
7043 | s.op->newline() << "_stp_error(\"unhandled flag value %d at %s:%d\", p->flags, __FUNCTION__, __LINE__);"; | |
7044 | s.op->newline() << "break;"; | |
7045 | s.op->indent(-1); | |
37e24fbe | 7046 | s.op->newline(-1) << "}"; |
935447c8 DS |
7047 | s.op->newline(-1) << "}"; |
7048 | s.op->newline() << "return rc;"; | |
7049 | s.op->newline(-1) << "}"; | |
7050 | ||
feec037d | 7051 | // Emit vma callbacks. |
dd163639 | 7052 | s.op->newline() << "#ifdef STP_NEED_VMA_TRACKER"; |
feec037d MW |
7053 | s.op->newline() << "static struct stap_task_finder_target stap_utrace_vmcbs[] = {"; |
7054 | s.op->indent(1); | |
7055 | if (! probes_by_path.empty()) | |
7056 | { | |
7057 | for (p_b_path_iterator it = probes_by_path.begin(); | |
7058 | it != probes_by_path.end(); it++) | |
7059 | emit_vma_callback_probe_decl (s, it->first, (int64_t)0); | |
7060 | } | |
7061 | if (! probes_by_pid.empty()) | |
7062 | { | |
7063 | for (p_b_pid_iterator it = probes_by_pid.begin(); | |
7064 | it != probes_by_pid.end(); it++) | |
7065 | emit_vma_callback_probe_decl (s, "", it->first); | |
7066 | } | |
7067 | s.op->newline(-1) << "};"; | |
dd163639 | 7068 | s.op->newline() << "#endif"; |
feec037d | 7069 | |
4c2732a1 | 7070 | s.op->newline() << "static struct stap_utrace_probe stap_utrace_probes[] = {"; |
935447c8 DS |
7071 | s.op->indent(1); |
7072 | ||
7073 | // Set up 'process(PATH)' probes | |
7074 | if (! probes_by_path.empty()) | |
7075 | { | |
7076 | for (p_b_path_iterator it = probes_by_path.begin(); | |
7077 | it != probes_by_path.end(); it++) | |
7078 | { | |
7079 | for (unsigned i = 0; i < it->second.size(); i++) | |
7080 | { | |
7081 | utrace_derived_probe *p = it->second[i]; | |
7082 | emit_probe_decl(s, p); | |
7083 | } | |
7084 | } | |
7085 | } | |
7086 | ||
7087 | // Set up 'process(PID)' probes | |
7088 | if (! probes_by_pid.empty()) | |
7089 | { | |
7090 | for (p_b_pid_iterator it = probes_by_pid.begin(); | |
7091 | it != probes_by_pid.end(); it++) | |
7092 | { | |
7093 | for (unsigned i = 0; i < it->second.size(); i++) | |
7094 | { | |
7095 | utrace_derived_probe *p = it->second[i]; | |
7096 | emit_probe_decl(s, p); | |
7097 | } | |
7098 | } | |
7099 | } | |
7100 | s.op->newline(-1) << "};"; | |
7101 | } | |
7102 | ||
7103 | ||
7104 | void | |
7105 | utrace_derived_probe_group::emit_module_init (systemtap_session& s) | |
7106 | { | |
7107 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
7108 | return; | |
7109 | ||
7110 | s.op->newline(); | |
dd163639 | 7111 | s.op->newline() << "#ifdef STP_NEED_VMA_TRACKER"; |
782040b3 | 7112 | s.op->newline() << "_stp_sym_init();"; |
feec037d MW |
7113 | s.op->newline() << "/* ---- utrace vma callbacks ---- */"; |
7114 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_utrace_vmcbs); i++) {"; | |
7115 | s.op->indent(1); | |
7116 | s.op->newline() << "struct stap_task_finder_target *r = &stap_utrace_vmcbs[i];"; | |
7117 | s.op->newline() << "rc = stap_register_task_finder_target(r);"; | |
7118 | s.op->newline(-1) << "}"; | |
110b061e | 7119 | s.op->newline() << "#endif"; |
feec037d | 7120 | |
935447c8 | 7121 | s.op->newline() << "/* ---- utrace probes ---- */"; |
278def10 | 7122 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_utrace_probes); i++) {"; |
935447c8 DS |
7123 | s.op->indent(1); |
7124 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[i];"; | |
7125 | s.op->newline() << "rc = stap_register_task_finder_target(&p->tgt);"; | |
7126 | s.op->newline(-1) << "}"; | |
7127 | ||
7128 | // rollback all utrace probes | |
7129 | s.op->newline() << "if (rc) {"; | |
7130 | s.op->indent(1); | |
7131 | s.op->newline() << "for (j=i-1; j>=0; j--) {"; | |
7132 | s.op->indent(1); | |
7133 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[j];"; | |
7134 | ||
7135 | s.op->newline() << "if (p->engine_attached) {"; | |
7136 | s.op->indent(1); | |
7137 | s.op->newline() << "stap_utrace_detach_ops(&p->ops);"; | |
7138 | s.op->newline(-1) << "}"; | |
7139 | s.op->newline(-1) << "}"; | |
7140 | ||
7141 | s.op->newline(-1) << "}"; | |
7142 | } | |
7143 | ||
7144 | ||
7145 | void | |
7146 | utrace_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7147 | { | |
7148 | if (probes_by_path.empty() && probes_by_pid.empty()) return; | |
7149 | ||
7150 | s.op->newline(); | |
7151 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
278def10 | 7152 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_utrace_probes); i++) {"; |
935447c8 DS |
7153 | s.op->indent(1); |
7154 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[i];"; | |
7155 | ||
7156 | s.op->newline() << "if (p->engine_attached) {"; | |
7157 | s.op->indent(1); | |
7158 | s.op->newline() << "stap_utrace_detach_ops(&p->ops);"; | |
7159 | s.op->newline(-1) << "}"; | |
7160 | s.op->newline(-1) << "}"; | |
7161 | } | |
7162 | ||
7163 | ||
888af770 FCE |
7164 | // ------------------------------------------------------------------------ |
7165 | // user-space probes | |
7166 | // ------------------------------------------------------------------------ | |
7167 | ||
888af770 FCE |
7168 | |
7169 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> | |
7170 | { | |
7171 | public: | |
7172 | void emit_module_decls (systemtap_session& s); | |
7173 | void emit_module_init (systemtap_session& s); | |
7174 | void emit_module_exit (systemtap_session& s); | |
7175 | }; | |
7176 | ||
7177 | ||
6d0f3f0c FCE |
7178 | uprobe_derived_probe::uprobe_derived_probe (const string& function, |
7179 | const string& filename, | |
7180 | int line, | |
7181 | const string& module, | |
7182 | int pid, | |
7183 | const string& section, | |
7184 | Dwarf_Addr dwfl_addr, | |
7185 | Dwarf_Addr addr, | |
7186 | dwarf_query & q, | |
7187 | Dwarf_Die* scope_die /* may be null */): | |
7188 | derived_probe (q.base_probe, new probe_point (*q.base_loc) /* .components soon rewritten */ ), | |
7189 | return_p (q.has_return), module (module), pid (pid), section (section), address (addr) | |
4baf0e53 | 7190 | { |
17c128f2 FCE |
7191 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. |
7192 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
7193 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
7194 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
6d0f3f0c FCE |
7195 | |
7196 | this->tok = q.base_probe->tok; | |
7197 | ||
de688825 | 7198 | // Expand target variables in the probe body |
6d0f3f0c FCE |
7199 | if (!null_die(scope_die)) |
7200 | { | |
de688825 | 7201 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); // XXX: user-space deref's! |
4ed05b15 | 7202 | this->body = v.require (this->body); |
6d0f3f0c FCE |
7203 | |
7204 | // If during target-variable-expanding the probe, we added a new block | |
7205 | // of code, add it to the start of the probe. | |
7206 | if (v.add_block) | |
7207 | this->body = new block(v.add_block, this->body); | |
7208 | ||
7209 | // If when target-variable-expanding the probe, we added a new | |
7210 | // probe, add it in a new file to the list of files to be processed. | |
7211 | if (v.add_probe) | |
7212 | { | |
7213 | stapfile *f = new stapfile; | |
7214 | f->probes.push_back(v.add_probe); | |
7215 | q.sess.files.push_back(f); | |
7216 | } | |
7217 | } | |
7218 | // else - null scope_die - $target variables will produce an error during translate phase | |
7219 | ||
7220 | // Reset the sole element of the "locations" vector as a | |
7221 | // "reverse-engineered" form of the incoming (q.base_loc) probe | |
7222 | // point. This allows a user to see what function / file / line | |
7223 | // number any particular match of the wildcards. | |
7224 | ||
7225 | vector<probe_point::component*> comps; | |
7226 | if(q.has_process) | |
7227 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
7228 | else | |
7229 | assert (0); | |
06aca46a | 7230 | |
6d0f3f0c FCE |
7231 | string fn_or_stmt; |
7232 | if (q.has_function_str || q.has_function_num) | |
7233 | fn_or_stmt = "function"; | |
7234 | else | |
7235 | fn_or_stmt = "statement"; | |
7236 | ||
7237 | if (q.has_function_str || q.has_statement_str) | |
7238 | { | |
7239 | string retro_name = function; | |
7240 | if (filename != "") | |
fb84c077 FCE |
7241 | { |
7242 | retro_name += ("@" + string (filename)); | |
7243 | if (line > 0) | |
7244 | retro_name += (":" + lex_cast<string> (line)); | |
7245 | } | |
6d0f3f0c FCE |
7246 | comps.push_back |
7247 | (new probe_point::component | |
7248 | (fn_or_stmt, new literal_string (retro_name))); | |
7249 | } | |
7250 | else if (q.has_function_num || q.has_statement_num) | |
7251 | { | |
7252 | Dwarf_Addr retro_addr; | |
7253 | if (q.has_function_num) | |
7254 | retro_addr = q.function_num_val; | |
7255 | else | |
7256 | retro_addr = q.statement_num_val; | |
7257 | comps.push_back (new probe_point::component | |
7258 | (fn_or_stmt, | |
7259 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
7260 | ||
7261 | if (q.has_absolute) | |
7262 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
7263 | } | |
7264 | ||
7265 | if (q.has_call) | |
7266 | comps.push_back (new probe_point::component(TOK_CALL)); | |
7267 | if (q.has_inline) | |
7268 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
7269 | if (return_p) | |
7270 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
7271 | /* | |
7272 | if (has_maxactive) | |
7273 | comps.push_back (new probe_point::component | |
7274 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
7275 | */ | |
7276 | ||
7277 | // Overwrite it. | |
7278 | this->sole_location()->components = comps; | |
7279 | } | |
7280 | ||
7281 | ||
0973d815 FCE |
7282 | uprobe_derived_probe::uprobe_derived_probe (probe *base, |
7283 | probe_point *location, | |
7284 | int pid, | |
7285 | Dwarf_Addr addr, | |
7286 | bool has_return): | |
7287 | derived_probe (base, location), // location is not rewritten here | |
7288 | return_p (has_return), pid (pid), address (addr) | |
7289 | { | |
7290 | } | |
7291 | ||
7292 | ||
6d0f3f0c FCE |
7293 | void |
7294 | uprobe_derived_probe::printsig (ostream& o) const | |
7295 | { | |
7296 | // Same as dwarf_derived_probe. | |
7297 | sole_location()->print (o); | |
7298 | o << " /* pc=" << section << "+0x" << hex << address << dec << " */"; | |
7299 | printsig_nested (o); | |
888af770 FCE |
7300 | } |
7301 | ||
7302 | ||
7303 | void | |
7304 | uprobe_derived_probe::join_group (systemtap_session& s) | |
7305 | { | |
7306 | if (! s.uprobe_derived_probes) | |
7307 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
7308 | s.uprobe_derived_probes->enroll (this); | |
93646f4d | 7309 | enable_task_finder(s); |
8a03658e JS |
7310 | |
7311 | // Ask buildrun.cxx to build extra module if needed, and | |
7312 | // signal staprun to load that module | |
7313 | s.need_uprobes = true; | |
888af770 FCE |
7314 | } |
7315 | ||
7316 | ||
7317 | struct uprobe_builder: public derived_probe_builder | |
7318 | { | |
7319 | uprobe_builder() {} | |
7320 | virtual void build(systemtap_session & sess, | |
7321 | probe * base, | |
7322 | probe_point * location, | |
86bf665e | 7323 | literal_map_t const & parameters, |
888af770 FCE |
7324 | vector<derived_probe *> & finished_results) |
7325 | { | |
7326 | int64_t process, address; | |
7327 | ||
7328 | bool b1 = get_param (parameters, TOK_PROCESS, process); | |
ced347a9 | 7329 | (void) b1; |
888af770 | 7330 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 7331 | (void) b2; |
888af770 FCE |
7332 | bool rr = has_null_param (parameters, TOK_RETURN); |
7333 | assert (b1 && b2); // by pattern_root construction | |
4baf0e53 | 7334 | |
0973d815 | 7335 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
888af770 FCE |
7336 | } |
7337 | }; | |
7338 | ||
7339 | ||
7340 | void | |
775d51e5 | 7341 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) |
888af770 FCE |
7342 | { |
7343 | if (probes.empty()) return; | |
775d51e5 | 7344 | s.op->newline() << "/* ---- user probes ---- */"; |
4baf0e53 | 7345 | |
c480bf3d | 7346 | // If uprobes isn't in the kernel, pull it in from the runtime. |
6274464e | 7347 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; |
888af770 | 7348 | s.op->newline() << "#include <linux/uprobes.h>"; |
c480bf3d | 7349 | s.op->newline() << "#else"; |
6274464e | 7350 | s.op->newline() << "#include \"uprobes/uprobes.h\""; |
c480bf3d | 7351 | s.op->newline() << "#endif"; |
9d451878 JK |
7352 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; |
7353 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
7354 | s.op->newline() << "#endif"; | |
6d0f3f0c | 7355 | |
01b05e2e | 7356 | s.op->newline() << "#ifndef MULTIPLE_UPROBES"; |
478c850f FCE |
7357 | s.op->newline() << "#define MULTIPLE_UPROBES 256"; // maximum possible armed uprobes per process() probe point |
7358 | // or apprx. max number of processes mapping a shared library | |
01b05e2e | 7359 | s.op->newline() << "#endif"; |
6d0f3f0c | 7360 | s.op->newline() << "#ifndef MAXUPROBES"; |
01b05e2e | 7361 | s.op->newline() << "#define MAXUPROBES (MULTIPLE_UPROBES * " << probes.size() << ")"; |
6d0f3f0c | 7362 | s.op->newline() << "#endif"; |
888af770 | 7363 | |
5e112f92 FCE |
7364 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
7365 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
4c2732a1 | 7366 | s.op->newline() << "static struct stap_uprobe {"; |
888af770 | 7367 | s.op->newline(1) << "union { struct uprobe up; struct uretprobe urp; };"; |
6d0f3f0c | 7368 | s.op->newline() << "int spec_index;"; // index into stap_uprobe_specs; <0 == free && unregistered |
01b05e2e | 7369 | s.op->newline(-1) << "} stap_uprobes [MAXUPROBES];"; |
5e112f92 | 7370 | s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
6d0f3f0c | 7371 | |
c9a05b1c | 7372 | // Emit vma callbacks. |
dd163639 | 7373 | s.op->newline() << "#ifdef STP_NEED_VMA_TRACKER"; |
c9a05b1c MW |
7374 | s.op->newline() << "static struct stap_task_finder_target stap_uprobe_vmcbs[] = {"; |
7375 | s.op->indent(1); | |
7376 | for (unsigned i = 0; i < probes.size(); i++) | |
7377 | { | |
7378 | uprobe_derived_probe* p = probes[i]; | |
7379 | if (p->pid != 0) | |
7380 | emit_vma_callback_probe_decl (s, "", p->pid); | |
7381 | else | |
7382 | emit_vma_callback_probe_decl (s, p->module, (int64_t)0); | |
7383 | } | |
7384 | s.op->newline(-1) << "};"; | |
dd163639 | 7385 | s.op->newline() << "#endif"; |
c9a05b1c | 7386 | |
4c2732a1 | 7387 | s.op->newline() << "static struct stap_uprobe_spec {"; |
5e112f92 | 7388 | s.op->newline(1) << "struct stap_task_finder_target finder;"; |
888af770 | 7389 | s.op->newline() << "unsigned long address;"; |
17c128f2 | 7390 | s.op->newline() << "const char *pathname;"; |
888af770 FCE |
7391 | s.op->newline() << "const char *pp;"; |
7392 | s.op->newline() << "void (*ph) (struct context*);"; | |
6d0f3f0c FCE |
7393 | s.op->newline() << "unsigned return_p:1;"; |
7394 | s.op->newline(-1) << "} stap_uprobe_specs [] = {"; | |
888af770 FCE |
7395 | s.op->indent(1); |
7396 | for (unsigned i =0; i<probes.size(); i++) | |
7397 | { | |
7398 | uprobe_derived_probe* p = probes[i]; | |
7399 | s.op->newline() << "{"; | |
0973d815 FCE |
7400 | s.op->line() << " .finder = {"; |
7401 | if (p->pid != 0) | |
17c128f2 FCE |
7402 | s.op->line() << " .pid=" << p->pid; |
7403 | else if (p->section == ".absolute") | |
0973d815 | 7404 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; |
06aca46a | 7405 | // else ".dynamic" gets pathname=0, pid=0, activating task_finder "global tracing" |
0973d815 | 7406 | s.op->line() << "},"; |
17c128f2 FCE |
7407 | if (p->section != ".absolute") |
7408 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
dc38c256 | 7409 | s.op->line() << " .address=(unsigned long)0x" << hex << p->address << dec << "ULL,"; |
888af770 FCE |
7410 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
7411 | s.op->line() << " .ph=&" << p->name << ","; | |
7412 | if (p->return_p) s.op->line() << " .return_p=1,"; | |
7413 | s.op->line() << " },"; | |
7414 | } | |
7415 | s.op->newline(-1) << "};"; | |
7416 | ||
48e685da | 7417 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 7418 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
6d0f3f0c | 7419 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 7420 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
7421 | s.op->newline() << "if (sup->spec_index < 0 ||" |
7422 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 | 7423 | s.op->newline() << "c->regs = regs;"; |
6d0f3f0c | 7424 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
7425 | common_probe_entryfn_epilogue (s.op); |
7426 | s.op->newline(-1) << "}"; | |
7427 | ||
48e685da | 7428 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 7429 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
6d0f3f0c | 7430 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 7431 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
7432 | s.op->newline() << "if (sup->spec_index < 0 ||" |
7433 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 FCE |
7434 | // XXX: kretprobes saves "c->pi = inst;" too |
7435 | s.op->newline() << "c->regs = regs;"; | |
6d0f3f0c | 7436 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
7437 | common_probe_entryfn_epilogue (s.op); |
7438 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7439 | |
17c128f2 FCE |
7440 | |
7441 | ||
6d0f3f0c FCE |
7442 | // NB: Because these utrace callbacks only occur before / after |
7443 | // userspace instructions run, there is no concurrency control issue | |
7444 | // between active uprobe callbacks and these registration / | |
06aca46a | 7445 | // unregistration pieces. |
6d0f3f0c FCE |
7446 | |
7447 | // We protect the stap_uprobe->spec_index (which also serves as a | |
d41d451c FCE |
7448 | // free/busy flag) value with the outer protective stap_probes_lock |
7449 | // spinlock, to protect it against concurrent registration / | |
7450 | // unregistration. | |
6d0f3f0c FCE |
7451 | |
7452 | s.op->newline(); | |
17c128f2 FCE |
7453 | s.op->newline() << "static int stap_uprobe_change (struct task_struct *tsk, int register_p, unsigned long relocation, struct stap_uprobe_spec *sups) {"; |
7454 | s.op->newline(1) << "int spec_index = (sups - stap_uprobe_specs);"; | |
6d0f3f0c | 7455 | s.op->newline() << "int handled_p = 0;"; |
d41d451c | 7456 | s.op->newline() << "int slotted_p = 0;"; |
6d0f3f0c FCE |
7457 | s.op->newline() << "int rc = 0;"; |
7458 | s.op->newline() << "int i;"; | |
3568f1dd | 7459 | |
d41d451c | 7460 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; |
01b05e2e | 7461 | s.op->newline() << "for (i=0; i<MAXUPROBES; i++) {"; // XXX: slow linear search |
5e112f92 | 7462 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; |
6d0f3f0c FCE |
7463 | |
7464 | // register new uprobe | |
7465 | s.op->newline() << "if (register_p && sup->spec_index < 0) {"; | |
9d451878 JK |
7466 | s.op->newline(1) << "#if (UPROBES_API_VERSION < 2)"; |
7467 | // See PR6829 comment. | |
7468 | s.op->newline() << "if (sup->spec_index == -1 && sup->up.kdata != NULL) continue;"; | |
80b4ad8b | 7469 | s.op->newline() << "else if (sup->spec_index == -2 && sup->urp.u.kdata != NULL) continue;"; |
9d451878 | 7470 | s.op->newline() << "#endif"; |
80b4ad8b | 7471 | s.op->newline() << "sup->spec_index = spec_index;"; |
d41d451c FCE |
7472 | s.op->newline() << "slotted_p = 1;"; |
7473 | s.op->newline() << "break;"; | |
7474 | s.op->newline(-1) << "} else if (!register_p && " | |
7475 | << "sup->spec_index == spec_index && " // a u[ret]probe set up for this probe point | |
7476 | << "((sups->return_p && sup->urp.u.pid == tsk->tgid && sup->urp.u.vaddr == relocation + sups->address) ||" // dying uretprobe | |
7477 | << "(!sups->return_p && sup->up.pid == tsk->tgid && sup->up.vaddr == relocation + sups->address))) {"; // dying uprobe | |
7478 | s.op->newline(1) << "slotted_p = 1;"; | |
7479 | s.op->newline() << "break;"; // exit to-free slot search | |
7480 | s.op->newline(-1) << "}"; | |
7481 | ||
7482 | s.op->newline(-1) << "}"; | |
7483 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; | |
7484 | ||
7485 | s.op->newline() << "#ifdef DEBUG_UPROBES"; | |
7486 | s.op->newline() << "printk (KERN_INFO \"%cuprobe spec %d idx %d process %s[%d] reloc %p pp '%s'\\n\", "; | |
7487 | s.op->line() << "(register_p ? '+' : '-'), spec_index, (slotted_p ? i : -1), tsk->comm, tsk->tgid, (void*) relocation, sups->pp);"; | |
7488 | s.op->newline() << "#endif"; | |
7489 | ||
7490 | // Here, slotted_p implies that `i' points to the single | |
7491 | // stap_uprobes[] element that has been slotted in for registration | |
7492 | // or unregistration processing. !slotted_p implies that the table | |
7493 | // was full (registration; MAXUPROBES) or that no matching entry was | |
7494 | // found (unregistration; should not happen). | |
7495 | ||
7496 | s.op->newline() << "if (register_p && slotted_p) {"; | |
7497 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
6d0f3f0c FCE |
7498 | s.op->newline() << "if (sups->return_p) {"; |
7499 | s.op->newline(1) << "sup->urp.u.pid = tsk->tgid;"; | |
17c128f2 | 7500 | s.op->newline() << "sup->urp.u.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
7501 | s.op->newline() << "sup->urp.handler = &enter_uretprobe_probe;"; |
7502 | s.op->newline() << "rc = register_uretprobe (& sup->urp);"; | |
7503 | s.op->newline(-1) << "} else {"; | |
7504 | s.op->newline(1) << "sup->up.pid = tsk->tgid;"; | |
17c128f2 | 7505 | s.op->newline() << "sup->up.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
7506 | s.op->newline() << "sup->up.handler = &enter_uprobe_probe;"; |
7507 | s.op->newline() << "rc = register_uprobe (& sup->up);"; | |
7508 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7509 | s.op->newline() << "if (rc) {"; // failed to register |
d41d451c FCE |
7510 | s.op->newline(1) << "printk (KERN_WARNING \"uprobe failed %s[%d] '%s' addr %p rc %d\\n\", tsk->comm, tsk->tgid, sups->pp, (void*)(relocation + sups->address), rc);"; |
7511 | // NB: we need to release this slot, so we need to borrow the mutex temporarily. | |
7512 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; | |
3568f1dd | 7513 | s.op->newline() << "sup->spec_index = -1;"; |
d41d451c | 7514 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
6d0f3f0c FCE |
7515 | s.op->newline(-1) << "} else {"; |
7516 | s.op->newline(1) << "handled_p = 1;"; // success | |
7517 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7518 | |
d41d451c FCE |
7519 | s.op->newline(-1) << "} else if (!register_p && slotted_p) {"; |
7520 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
9d451878 JK |
7521 | s.op->newline() << "int unregistered_flag;"; |
7522 | // PR6829, PR9940: | |
7523 | // Here we're unregistering for one of two reasons: | |
7524 | // 1. the process image is going away (or gone) due to exit or exec; or | |
7525 | // 2. the vma containing the probepoint has been unmapped. | |
7526 | // In case 1, it's sort of a nop, because uprobes will notice the event | |
7527 | // and dispose of the probes eventually, if it hasn't already. But by | |
7528 | // calling unmap_u[ret]probe() ourselves, we free up sup right away. | |
7529 | // | |
7530 | // In both cases, we must use unmap_u[ret]probe instead of | |
7531 | // unregister_u[ret]probe, so uprobes knows not to try to restore the | |
7532 | // original opcode. | |
7533 | s.op->newline() << "#if (UPROBES_API_VERSION >= 2)"; | |
7534 | s.op->newline() << "if (sups->return_p)"; | |
7535 | s.op->newline(1) << "unmap_uretprobe (& sup->urp);"; | |
7536 | s.op->newline(-1) << "else"; | |
7537 | s.op->newline(1) << "unmap_uprobe (& sup->up);"; | |
7538 | s.op->newline(-1) << "unregistered_flag = -1;"; | |
7539 | s.op->newline() << "#else"; | |
7540 | // Uprobes lacks unmap_u[ret]probe. Before reusing sup, we must wait | |
7541 | // until uprobes turns loose of the u[ret]probe on its own, as indicated | |
7542 | // by uprobe.kdata = NULL. | |
7543 | s.op->newline() << "unregistered_flag = (sups->return_p ? -2 : -1);"; | |
7544 | s.op->newline() << "#endif"; | |
d41d451c | 7545 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; |
9d451878 | 7546 | s.op->newline() << "sup->spec_index = unregistered_flag;"; |
5e112f92 | 7547 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
d41d451c FCE |
7548 | s.op->newline() << "handled_p = 1;"; |
7549 | s.op->newline(-1) << "}"; // if slotted_p | |
7550 | ||
7551 | // NB: handled_p implies slotted_p | |
7552 | ||
6d0f3f0c | 7553 | s.op->newline() << "if (! handled_p) {"; |
73209876 FCE |
7554 | s.op->newline(1) << "#ifdef STP_TIMING"; |
7555 | s.op->newline() << "atomic_inc (register_p ? & skipped_count_uprobe_reg : & skipped_count_uprobe_unreg);"; | |
7556 | s.op->newline() << "#endif"; | |
7557 | // NB: duplicates common_entryfn_epilogue, but then this is not a probe entry fn epilogue. | |
7558 | s.op->newline() << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
6d0f3f0c FCE |
7559 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; |
7560 | s.op->newline() << "_stp_exit ();"; | |
7561 | s.op->newline(-1) << "}"; | |
7562 | s.op->newline(-1) << "}"; | |
06aca46a | 7563 | |
6d0f3f0c FCE |
7564 | s.op->newline() << "return 0;"; // XXX: or rc? |
7565 | s.op->newline(-1) << "}"; | |
7566 | s.op->assert_0_indent(); | |
7567 | ||
17c128f2 FCE |
7568 | |
7569 | // The task_finder_callback we use for ET_EXEC targets. | |
7570 | s.op->newline(); | |
7571 | s.op->newline() << "static int stap_uprobe_process_found (struct stap_task_finder_target *tgt, struct task_struct *tsk, int register_p, int process_p) {"; | |
7572 | ||
7573 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; | |
7574 | s.op->newline() << "if (! process_p) return 0;"; | |
7575 | s.op->newline(0) << "return stap_uprobe_change (tsk, register_p, 0, sups);"; | |
7576 | s.op->newline(-1) << "}"; | |
7577 | ||
782040b3 | 7578 | // The task_finder_mmap_callback we use for ET_DYN targets. |
17c128f2 | 7579 | s.op->newline(); |
782040b3 | 7580 | s.op->newline() << "static int stap_uprobe_mmap_found (struct stap_task_finder_target *tgt, struct task_struct *tsk, char *path, unsigned long addr, unsigned long length, unsigned long offset, unsigned long vm_flags) {"; |
17c128f2 FCE |
7581 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; |
7582 | // 1 - shared libraries' executable segments load from offset 0 - ld.so convention | |
782040b3 | 7583 | s.op->newline() << "if (offset != 0) return 0;"; |
17c128f2 | 7584 | // 2 - the shared library we're interested in |
782040b3 | 7585 | s.op->newline() << "if (path == NULL || strcmp (path, sups->pathname)) return 0;"; |
17c128f2 | 7586 | // 3 - probe address within the mapping limits; test should not fail |
782040b3 DS |
7587 | s.op->newline() << "if (sups->address >= addr && sups->address < (addr + length)) return 0;"; |
7588 | // 4 - mapping should be executable | |
7589 | s.op->newline() << "if (!(vm_flags & VM_EXEC)) return 0;"; | |
06aca46a | 7590 | |
c16d425a | 7591 | s.op->newline() << "#ifdef DEBUG_TASK_FINDER_VMA"; |
782040b3 | 7592 | s.op->newline() << "printk (KERN_INFO \"vmchange pid %d path %s addr %p length %lu offset %p\\n\", tsk->tgid, path, (void *) addr, length, (void*) offset);"; |
c16d425a FCE |
7593 | s.op->newline() << "printk (KERN_INFO \"sups %p pp %s path %s address %p\\n\", sups, sups->pp, sups->pathname ?: \"\", (void*) sups->address);"; |
7594 | s.op->newline() << "#endif"; | |
7595 | ||
782040b3 | 7596 | s.op->newline(0) << "return stap_uprobe_change (tsk, 1, addr, sups);"; |
17c128f2 FCE |
7597 | s.op->newline(-1) << "}"; |
7598 | s.op->assert_0_indent(); | |
7599 | ||
7600 | ||
6d0f3f0c | 7601 | s.op->newline(); |
888af770 FCE |
7602 | } |
7603 | ||
7604 | ||
7605 | void | |
7606 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
7607 | { | |
7608 | if (probes.empty()) return; | |
dd163639 | 7609 | s.op->newline() << "#ifdef STP_NEED_VMA_TRACKER"; |
782040b3 | 7610 | s.op->newline() << "_stp_sym_init();"; |
c9a05b1c MW |
7611 | s.op->newline() << "/* ---- uprobe vma callbacks ---- */"; |
7612 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_uprobe_vmcbs); i++) {"; | |
7613 | s.op->indent(1); | |
7614 | s.op->newline() << "struct stap_task_finder_target *r = &stap_uprobe_vmcbs[i];"; | |
7615 | s.op->newline() << "rc = stap_register_task_finder_target(r);"; | |
7616 | s.op->newline(-1) << "}"; | |
dd163639 | 7617 | s.op->newline() << "#endif"; |
c9a05b1c | 7618 | |
5e112f92 FCE |
7619 | s.op->newline() << "/* ---- user probes ---- */"; |
7620 | ||
01b05e2e | 7621 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7622 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7623 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
7624 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
7625 | // initialized to zero. This is so that we can use | |
7626 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
7627 | s.op->newline(-1) << "}"; |
7628 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
888af770 FCE |
7629 | |
7630 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
6d0f3f0c FCE |
7631 | s.op->newline(1) << "struct stap_uprobe_spec *sups = & stap_uprobe_specs[i];"; |
7632 | s.op->newline() << "probe_point = sups->pp;"; // for error messages | |
17c128f2 | 7633 | s.op->newline() << "if (sups->finder.pathname) sups->finder.callback = & stap_uprobe_process_found;"; |
782040b3 | 7634 | s.op->newline() << "else if (sups->pathname) sups->finder.mmap_callback = & stap_uprobe_mmap_found;"; |
6d0f3f0c | 7635 | s.op->newline() << "rc = stap_register_task_finder_target (& sups->finder);"; |
5e112f92 FCE |
7636 | |
7637 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any | |
7638 | // finders already registered, since mere registration does not | |
7639 | // cause any utrace or memory allocation actions. That happens only | |
7640 | // later, once the task finder engine starts running. So, for a | |
7641 | // partial initialization requiring unwind, we need do nothing. | |
7642 | s.op->newline() << "if (rc) break;"; | |
7643 | ||
888af770 FCE |
7644 | s.op->newline(-1) << "}"; |
7645 | } | |
7646 | ||
7647 | ||
7648 | void | |
7649 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7650 | { | |
7651 | if (probes.empty()) return; | |
7652 | s.op->newline() << "/* ---- user probes ---- */"; | |
7653 | ||
6d0f3f0c FCE |
7654 | // NB: there is no stap_unregister_task_finder_target call; |
7655 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
7656 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
7657 | // | |
7658 | // This function blocks until all callbacks are completed, so there | |
7659 | // is supposed to be no possibility of any registration-related code starting | |
7660 | // to run in parallel with our shutdown here. So we don't need to protect the | |
7661 | // stap_uprobes[] array with the mutex. | |
5e112f92 | 7662 | |
01b05e2e | 7663 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7664 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7665 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; | |
6d0f3f0c | 7666 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd FCE |
7667 | |
7668 | s.op->newline() << "if (sups->return_p) {"; | |
7669 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 7670 | s.op->newline() << "printk (KERN_INFO \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);"; |
3568f1dd | 7671 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
7672 | // NB: PR6829 does not change that we still need to unregister at |
7673 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
7674 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
7675 | s.op->newline(-1) << "} else {"; | |
7676 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 7677 | s.op->newline() << "printk (KERN_INFO \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->urp.u.pid, (void*) sup->urp.u.vaddr);"; |
3568f1dd FCE |
7678 | s.op->newline() << "#endif"; |
7679 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
7680 | s.op->newline(-1) << "}"; | |
7681 | ||
6d0f3f0c | 7682 | s.op->newline() << "sup->spec_index = -1;"; |
3568f1dd FCE |
7683 | |
7684 | // XXX: uprobe missed counts? | |
7685 | ||
6d0f3f0c | 7686 | s.op->newline(-1) << "}"; |
5e112f92 FCE |
7687 | |
7688 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; | |
888af770 FCE |
7689 | } |
7690 | ||
e6fe60e7 AM |
7691 | // ------------------------------------------------------------------------ |
7692 | // Kprobe derived probes | |
7693 | // ------------------------------------------------------------------------ | |
7694 | ||
7695 | static string TOK_KPROBE("kprobe"); | |
7696 | ||
bae55db9 JS |
7697 | struct kprobe_derived_probe: public derived_probe |
7698 | { | |
7699 | kprobe_derived_probe (probe *base, | |
7700 | probe_point *location, | |
7701 | const string& name, | |
7702 | int64_t stmt_addr, | |
7703 | bool has_return, | |
7704 | bool has_statement, | |
7705 | bool has_maxactive, | |
7706 | long maxactive_val | |
7707 | ); | |
7708 | string symbol_name; | |
7709 | Dwarf_Addr addr; | |
7710 | bool has_return; | |
7711 | bool has_statement; | |
7712 | bool has_maxactive; | |
7713 | long maxactive_val; | |
7714 | bool access_var; | |
7715 | void printsig (std::ostream &o) const; | |
7716 | void join_group (systemtap_session& s); | |
7717 | }; | |
7718 | ||
7719 | struct kprobe_derived_probe_group: public derived_probe_group | |
7720 | { | |
7721 | private: | |
7722 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
7723 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
7724 | ||
7725 | public: | |
7726 | void enroll (kprobe_derived_probe* probe); | |
7727 | void emit_module_decls (systemtap_session& s); | |
7728 | void emit_module_init (systemtap_session& s); | |
7729 | void emit_module_exit (systemtap_session& s); | |
7730 | }; | |
7731 | ||
e6fe60e7 AM |
7732 | kprobe_derived_probe::kprobe_derived_probe (probe *base, |
7733 | probe_point *location, | |
b6371390 | 7734 | const string& name, |
e6fe60e7 | 7735 | int64_t stmt_addr, |
b6371390 JS |
7736 | bool has_return, |
7737 | bool has_statement, | |
7738 | bool has_maxactive, | |
7739 | long maxactive_val | |
7740 | ): | |
e6fe60e7 AM |
7741 | derived_probe (base, location), |
7742 | symbol_name (name), addr (stmt_addr), | |
b6371390 JS |
7743 | has_return (has_return), has_statement (has_statement), |
7744 | has_maxactive (has_maxactive), maxactive_val (maxactive_val) | |
e6fe60e7 AM |
7745 | { |
7746 | this->tok = base->tok; | |
7747 | this->access_var = false; | |
7748 | ||
7749 | #ifndef USHRT_MAX | |
7750 | #define USHRT_MAX 32767 | |
7751 | #endif | |
7752 | ||
46856d8d JS |
7753 | // Expansion of $target variables in the probe body produces an error during |
7754 | // translate phase, since we're not using debuginfo | |
7755 | ||
e6fe60e7 | 7756 | vector<probe_point::component*> comps; |
46856d8d | 7757 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 7758 | |
46856d8d JS |
7759 | if (has_statement) |
7760 | { | |
7761 | comps.push_back (new probe_point::component(TOK_STATEMENT, new literal_number(addr))); | |
7762 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); | |
7763 | } | |
7764 | else | |
7765 | { | |
7766 | size_t pos = name.find(':'); | |
7767 | if (pos != string::npos) | |
7768 | { | |
7769 | string module = name.substr(0, pos); | |
7770 | string function = name.substr(pos + 1); | |
7771 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
7772 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
7773 | } | |
7774 | else | |
7775 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 7776 | } |
888af770 | 7777 | |
b6371390 JS |
7778 | if (has_return) |
7779 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
7780 | if (has_maxactive) | |
7781 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
7782 | ||
e6fe60e7 AM |
7783 | this->sole_location()->components = comps; |
7784 | } | |
888af770 | 7785 | |
e6fe60e7 AM |
7786 | void kprobe_derived_probe::printsig (ostream& o) const |
7787 | { | |
7788 | sole_location()->print (o); | |
7789 | o << " /* " << " name = " << symbol_name << "*/"; | |
7790 | printsig_nested (o); | |
7791 | } | |
7792 | ||
7793 | void kprobe_derived_probe::join_group (systemtap_session& s) | |
7794 | { | |
7795 | ||
7796 | if (! s.kprobe_derived_probes) | |
7797 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
7798 | s.kprobe_derived_probes->enroll (this); | |
7799 | ||
7800 | } | |
7801 | ||
7802 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) | |
7803 | { | |
7804 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
7805 | // probes of same symbol should share single kprobe/kretprobe | |
7806 | } | |
7807 | ||
7808 | void | |
7809 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
7810 | { | |
7811 | if (probes_by_module.empty()) return; | |
7812 | ||
7813 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; | |
7814 | ||
7815 | // Warn of misconfigured kernels | |
7816 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
7817 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
7818 | s.op->newline() << "#endif"; | |
7819 | s.op->newline(); | |
7820 | ||
7821 | // Forward declare the master entry functions | |
88747011 | 7822 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 7823 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 7824 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
7825 | s.op->line() << " struct pt_regs *regs);"; |
7826 | ||
7827 | // Emit an array of kprobe/kretprobe pointers | |
7828 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
7829 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
7830 | s.op->newline() << "#endif"; | |
7831 | ||
7832 | // Emit the actual probe list. | |
7833 | ||
7834 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; | |
7835 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
7836 | s.op->newline() << "#ifdef __ia64__"; | |
7837 | s.op->newline() << "struct kprobe dummy;"; | |
7838 | s.op->newline() << "#endif"; | |
7839 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
7840 | // NB: bss! | |
7841 | ||
7842 | s.op->newline() << "static struct stap_dwarfless_probe {"; | |
7843 | s.op->newline(1) << "const unsigned return_p:1;"; | |
7844 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
e6fe60e7 AM |
7845 | s.op->newline() << "unsigned registered_p:1;"; |
7846 | s.op->newline() << "const unsigned short maxactive_val;"; | |
7847 | ||
7848 | // Function Names are mostly small and uniform enough to justify putting | |
7849 | // char[MAX]'s into the array instead of relocated char*'s. | |
7850 | ||
7851 | size_t pp_name_max = 0, symbol_string_name_max = 0; | |
7852 | size_t pp_name_tot = 0, symbol_string_name_tot = 0; | |
7853 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
7854 | { | |
7855 | kprobe_derived_probe* p = it->second; | |
7856 | #define DOIT(var,expr) do { \ | |
7857 | size_t var##_size = (expr) + 1; \ | |
7858 | var##_max = max (var##_max, var##_size); \ | |
7859 | var##_tot += var##_size; } while (0) | |
7860 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
7861 | DOIT(symbol_string_name, p->symbol_name.size()); | |
7862 | #undef DOIT | |
7863 | } | |
7864 | ||
7865 | #define CALCIT(var) \ | |
7866 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
7867 | ||
7868 | CALCIT(pp); | |
7869 | CALCIT(symbol_string); | |
7870 | #undef CALCIT | |
7871 | ||
7872 | s.op->newline() << "const unsigned long address;"; | |
7873 | s.op->newline() << "void (* const ph) (struct context*);"; | |
7874 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; | |
7875 | s.op->indent(1); | |
7876 | ||
7877 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
7878 | { | |
7879 | kprobe_derived_probe* p = it->second; | |
7880 | s.op->newline() << "{"; | |
7881 | if (p->has_return) | |
7882 | s.op->line() << " .return_p=1,"; | |
7883 | ||
7884 | if (p->has_maxactive) | |
7885 | { | |
7886 | s.op->line() << " .maxactive_p=1,"; | |
7887 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
7888 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
7889 | } | |
c8d9d15e | 7890 | |
e6fe60e7 | 7891 | if (p->has_statement) |
c8d9d15e | 7892 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 7893 | else |
c8d9d15e | 7894 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
e6fe60e7 AM |
7895 | |
7896 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
7897 | s.op->line() << " .ph=&" << p->name; | |
7898 | s.op->line() << " },"; | |
7899 | } | |
7900 | ||
7901 | s.op->newline(-1) << "};"; | |
7902 | ||
7903 | // Emit the kprobes callback function | |
7904 | s.op->newline(); | |
88747011 | 7905 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
7906 | s.op->line() << " struct pt_regs *regs) {"; |
7907 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
7908 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
7909 | // Check that the index is plausible | |
7910 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
7911 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
7912 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
7913 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
7914 | s.op->line() << "];"; | |
7915 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); | |
7916 | s.op->newline() << "c->regs = regs;"; | |
7917 | s.op->newline() << "(*sdp->ph) (c);"; | |
7918 | common_probe_entryfn_epilogue (s.op); | |
7919 | s.op->newline() << "return 0;"; | |
7920 | s.op->newline(-1) << "}"; | |
7921 | ||
7922 | // Same for kretprobes | |
7923 | s.op->newline(); | |
88747011 | 7924 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
7925 | s.op->line() << " struct pt_regs *regs) {"; |
7926 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
7927 | ||
7928 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
7929 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
7930 | // Check that the index is plausible | |
7931 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
7932 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
7933 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
7934 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
7935 | s.op->line() << "];"; | |
7936 | ||
7937 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); | |
7938 | s.op->newline() << "c->regs = regs;"; | |
7939 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
7940 | s.op->newline() << "(*sdp->ph) (c);"; | |
7941 | common_probe_entryfn_epilogue (s.op); | |
7942 | s.op->newline() << "return 0;"; | |
7943 | s.op->newline(-1) << "}"; | |
7944 | } | |
7945 | ||
7946 | ||
7947 | void | |
7948 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
7949 | { | |
e6fe60e7 | 7950 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 7951 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 7952 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e JS |
7953 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
7954 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; | |
e6fe60e7 AM |
7955 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
7956 | s.op->newline() << "if (sdp->return_p) {"; | |
c8d9d15e | 7957 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
9f38e653 | 7958 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
e6fe60e7 AM |
7959 | s.op->newline() << "if (sdp->maxactive_p) {"; |
7960 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
7961 | s.op->newline(-1) << "} else {"; | |
7962 | s.op->newline(1) << "kp->u.krp.maxactive = max(10, 4*NR_CPUS);"; | |
7963 | s.op->newline(-1) << "}"; | |
88747011 | 7964 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
7965 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
7966 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 7967 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
c8d9d15e JS |
7968 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
7969 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
e6fe60e7 AM |
7970 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
7971 | s.op->newline() << "if (rc == 0) {"; | |
7972 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
7973 | s.op->newline() << "if (rc != 0)"; | |
7974 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
7975 | s.op->newline(-2) << "}"; | |
7976 | s.op->newline() << "#else"; | |
7977 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
7978 | s.op->newline() << "#endif"; | |
7979 | s.op->newline(-1) << "} else {"; | |
7980 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 7981 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
9f38e653 | 7982 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
88747011 | 7983 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 7984 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 7985 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e JS |
7986 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
7987 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; | |
e6fe60e7 AM |
7988 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
7989 | s.op->newline() << "if (rc == 0) {"; | |
7990 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
7991 | s.op->newline() << "if (rc != 0)"; | |
7992 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
7993 | s.op->newline(-2) << "}"; | |
7994 | s.op->newline() << "#else"; | |
7995 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
7996 | s.op->newline() << "#endif"; | |
7997 | s.op->newline(-1) << "}"; | |
7998 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
7999 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
5badd4d0 | 8000 | s.op->newline() << "_stp_warn (\"probe %s registration error (rc %d)\", probe_point, rc);"; |
e6fe60e7 AM |
8001 | s.op->newline() << "rc = 0;"; // continue with other probes |
8002 | // XXX: shall we increment numskipped? | |
8003 | s.op->newline(-1) << "}"; | |
8004 | ||
8005 | s.op->newline() << "else sdp->registered_p = 1;"; | |
8006 | s.op->newline(-1) << "}"; // for loop | |
e6fe60e7 AM |
8007 | } |
8008 | ||
8009 | void | |
8010 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8011 | { | |
8012 | //Unregister kprobes by batch interfaces. | |
8013 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8014 | s.op->newline() << "j = 0;"; | |
8015 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8016 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8017 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8018 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8019 | s.op->newline() << "if (!sdp->return_p)"; | |
8020 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
8021 | s.op->newline(-2) << "}"; | |
8022 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
8023 | s.op->newline() << "j = 0;"; | |
8024 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8025 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8026 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8027 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8028 | s.op->newline() << "if (sdp->return_p)"; | |
8029 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
8030 | s.op->newline(-2) << "}"; | |
8031 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
8032 | s.op->newline() << "#ifdef __ia64__"; | |
8033 | s.op->newline() << "j = 0;"; | |
8034 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8035 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8036 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8037 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8038 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
8039 | s.op->newline(-1) << "}"; | |
8040 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
8041 | s.op->newline() << "#endif"; | |
8042 | s.op->newline() << "#endif"; | |
8043 | ||
8044 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8045 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8046 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8047 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8048 | s.op->newline() << "if (sdp->return_p) {"; | |
8049 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8050 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
8051 | s.op->newline() << "#endif"; | |
8052 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; | |
8053 | s.op->newline() << "#ifdef STP_TIMING"; | |
8054 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
8055 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; | |
8056 | s.op->newline(-1) << "#endif"; | |
8057 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; | |
8058 | s.op->newline() << "#ifdef STP_TIMING"; | |
8059 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
8060 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; | |
8061 | s.op->newline(-1) << "#endif"; | |
8062 | s.op->newline(-1) << "} else {"; | |
8063 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8064 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
8065 | s.op->newline() << "#endif"; | |
8066 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; | |
8067 | s.op->newline() << "#ifdef STP_TIMING"; | |
8068 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
8069 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);"; | |
8070 | s.op->newline(-1) << "#endif"; | |
8071 | s.op->newline(-1) << "}"; | |
8072 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
8073 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
8074 | s.op->newline() << "#endif"; | |
8075 | s.op->newline() << "sdp->registered_p = 0;"; | |
8076 | s.op->newline(-1) << "}"; | |
8077 | } | |
8078 | ||
8079 | struct kprobe_builder: public derived_probe_builder | |
8080 | { | |
8081 | kprobe_builder() {} | |
8082 | virtual void build(systemtap_session & sess, | |
8083 | probe * base, | |
8084 | probe_point * location, | |
8085 | literal_map_t const & parameters, | |
8086 | vector<derived_probe *> & finished_results); | |
8087 | }; | |
8088 | ||
8089 | ||
8090 | void | |
8091 | kprobe_builder::build(systemtap_session & sess, | |
8092 | probe * base, | |
8093 | probe_point * location, | |
8094 | literal_map_t const & parameters, | |
8095 | vector<derived_probe *> & finished_results) | |
8096 | { | |
8097 | string function_string_val, module_string_val; | |
b6371390 JS |
8098 | int64_t statement_num_val = 0, maxactive_val = 0; |
8099 | bool has_function_str, has_module_str, has_statement_num; | |
8100 | bool has_absolute, has_return, has_maxactive; | |
e6fe60e7 | 8101 | |
b6371390 JS |
8102 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
8103 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
8104 | has_return = has_null_param (parameters, TOK_RETURN); | |
8105 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
8106 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
8107 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
e6fe60e7 | 8108 | |
b6371390 JS |
8109 | if (has_function_str) |
8110 | { | |
8111 | if (has_module_str) | |
8112 | function_string_val = module_string_val + ":" + function_string_val; | |
8113 | ||
8114 | finished_results.push_back (new kprobe_derived_probe (base, | |
8115 | location, function_string_val, | |
8116 | 0, has_return, | |
8117 | has_statement_num, | |
8118 | has_maxactive, | |
8119 | maxactive_val)); | |
8120 | } | |
e6fe60e7 | 8121 | else |
b6371390 JS |
8122 | { |
8123 | // assert guru mode for absolute probes | |
8124 | if ( has_statement_num && has_absolute && !base->privileged ) | |
8125 | throw semantic_error ("absolute statement probe in unprivileged script", base->tok); | |
8126 | ||
8127 | finished_results.push_back (new kprobe_derived_probe (base, | |
8128 | location, "", | |
8129 | statement_num_val, | |
8130 | has_return, | |
8131 | has_statement_num, | |
8132 | has_maxactive, | |
8133 | maxactive_val)); | |
8134 | } | |
e6fe60e7 | 8135 | } |
b6371390 JS |
8136 | |
8137 | ||
342d3f96 | 8138 | |
0a6f5a3f JS |
8139 | // ------------------------------------------------------------------------ |
8140 | // statically inserted kernel-tracepoint derived probes | |
8141 | // ------------------------------------------------------------------------ | |
8142 | ||
6fb70fb7 JS |
8143 | struct tracepoint_arg |
8144 | { | |
ad370dcc | 8145 | string name, c_type, typecast; |
dcaa1a65 | 8146 | bool usable, used, isptr; |
f8a968bc | 8147 | Dwarf_Die type_die; |
dcaa1a65 | 8148 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 8149 | }; |
0a6f5a3f JS |
8150 | |
8151 | struct tracepoint_derived_probe: public derived_probe | |
8152 | { | |
79189b84 JS |
8153 | tracepoint_derived_probe (systemtap_session& s, |
8154 | dwflpp& dw, Dwarf_Die& func_die, | |
8155 | const string& tracepoint_name, | |
8156 | probe* base_probe, probe_point* location); | |
8157 | ||
8158 | systemtap_session& sess; | |
6fb70fb7 JS |
8159 | string tracepoint_name, header; |
8160 | vector <struct tracepoint_arg> args; | |
79189b84 | 8161 | |
6fb70fb7 | 8162 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
e2086848 | 8163 | void printargs (std::ostream &o) const; |
79189b84 | 8164 | void join_group (systemtap_session& s); |
3e3bd7b6 | 8165 | void print_dupe_stamp(ostream& o); |
f8a968bc | 8166 | void emit_probe_context_vars (translator_output* o); |
0a6f5a3f JS |
8167 | }; |
8168 | ||
8169 | ||
8170 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> | |
8171 | { | |
79189b84 JS |
8172 | void emit_module_decls (systemtap_session& s); |
8173 | void emit_module_init (systemtap_session& s); | |
8174 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f JS |
8175 | }; |
8176 | ||
8177 | ||
f8a968bc JS |
8178 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
8179 | { | |
8180 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
8181 | vector <struct tracepoint_arg>& args): | |
8182 | dw (dw), probe_name (probe_name), args (args) {} | |
8183 | dwflpp& dw; | |
8184 | const string& probe_name; | |
8185 | vector <struct tracepoint_arg>& args; | |
8186 | ||
8187 | void visit_target_symbol (target_symbol* e); | |
8188 | void visit_target_symbol_arg (target_symbol* e); | |
8189 | void visit_target_symbol_context (target_symbol* e); | |
8190 | }; | |
8191 | ||
8192 | ||
8193 | void | |
8194 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
8195 | { | |
8196 | string argname = e->base_name.substr(1); | |
8197 | ||
8198 | // search for a tracepoint parameter matching this name | |
8199 | tracepoint_arg *arg = NULL; | |
8200 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 8201 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
8202 | { |
8203 | arg = &args[i]; | |
8204 | arg->used = true; | |
8205 | break; | |
8206 | } | |
8207 | ||
8208 | if (arg == NULL) | |
8209 | { | |
8210 | stringstream alternatives; | |
8211 | for (unsigned i = 0; i < args.size(); ++i) | |
8212 | alternatives << " $" << args[i].name; | |
046e7190 | 8213 | alternatives << " $$name $$parms $$vars"; |
f8a968bc JS |
8214 | |
8215 | // We hope that this value ends up not being referenced after all, so it | |
8216 | // can be optimized out quietly. | |
8217 | semantic_error* saveme = | |
8218 | new semantic_error("unable to find tracepoint variable '" + e->base_name | |
8219 | + "' (alternatives:" + alternatives.str () + ")", e->tok); | |
8220 | // NB: we can have multiple errors, since a target variable | |
8221 | // may be expanded in several different contexts: | |
8222 | // trace ("*") { $foo->bar } | |
8223 | saveme->chain = e->saved_conversion_error; | |
8224 | e->saved_conversion_error = saveme; | |
8225 | provide (e); | |
8226 | return; | |
8227 | } | |
8228 | ||
8229 | // make sure we're not dereferencing base types | |
8230 | if (!e->components.empty() && !arg->isptr) | |
8231 | switch (e->components[0].first) | |
8232 | { | |
8233 | case target_symbol::comp_literal_array_index: | |
8234 | throw semantic_error("tracepoint variable '" + e->base_name | |
8235 | + "' may not be used as array", e->tok); | |
8236 | case target_symbol::comp_struct_member: | |
8237 | throw semantic_error("tracepoint variable '" + e->base_name | |
8238 | + "' may not be used as a structure", e->tok); | |
8239 | default: | |
8240 | throw semantic_error("invalid use of tracepoint variable '" | |
8241 | + e->base_name + "'", e->tok); | |
8242 | } | |
8243 | ||
8244 | // we can only write to dereferenced fields, and only if guru mode is on | |
8245 | bool lvalue = is_active_lvalue(e); | |
8246 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
8247 | throw semantic_error("write to tracepoint variable '" + e->base_name | |
8248 | + "' not permitted", e->tok); | |
ad370dcc JS |
8249 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
8250 | // is also meaningless until you dereference past a pointer member. It's | |
8251 | // harder to detect and prevent that though... | |
f8a968bc JS |
8252 | |
8253 | if (e->components.empty()) | |
8254 | { | |
3e3bd7b6 JS |
8255 | // Just grab the value from the probe locals |
8256 | e->probe_context_var = "__tracepoint_arg_" + arg->name; | |
8257 | e->type = pe_long; | |
8258 | provide (e); | |
f8a968bc JS |
8259 | } |
8260 | else | |
8261 | { | |
8262 | // Synthesize a function to dereference the dwarf fields, | |
8263 | // with a pointer parameter that is the base tracepoint variable | |
8264 | functiondecl *fdecl = new functiondecl; | |
8265 | fdecl->tok = e->tok; | |
8266 | embeddedcode *ec = new embeddedcode; | |
8267 | ec->tok = e->tok; | |
8268 | ||
8269 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") | |
8270 | + "_" + e->base_name.substr(1) | |
8271 | + "_" + lex_cast<string>(tick++)); | |
8272 | ||
8273 | fdecl->name = fname; | |
8274 | fdecl->body = ec; | |
8275 | ||
8276 | try | |
8277 | { | |
b4c34c26 | 8278 | ec->code = dw.literal_stmt_for_pointer (&arg->type_die, e, |
f8a968bc JS |
8279 | lvalue, fdecl->type); |
8280 | } | |
8281 | catch (const semantic_error& er) | |
8282 | { | |
8283 | // We suppress this error message, and pass the unresolved | |
8284 | // variable to the next pass. We hope that this value ends | |
8285 | // up not being referenced after all, so it can be optimized out | |
8286 | // quietly. | |
8287 | semantic_error* saveme = new semantic_error (er); // copy it | |
8288 | saveme->tok1 = e->tok; // XXX: token not passed to dw code generation routines | |
8289 | // NB: we can have multiple errors, since a target variable | |
8290 | // may be expanded in several different contexts: | |
8291 | // trace ("*") { $foo->bar } | |
8292 | saveme->chain = e->saved_conversion_error; | |
8293 | e->saved_conversion_error = saveme; | |
8294 | provide (e); | |
8295 | return; | |
8296 | } | |
8297 | ||
8298 | // Give the fdecl an argument for the raw tracepoint value | |
8299 | vardecl *v1 = new vardecl; | |
8300 | v1->type = pe_long; | |
8301 | v1->name = "pointer"; | |
8302 | v1->tok = e->tok; | |
8303 | fdecl->formal_args.push_back(v1); | |
8304 | ||
8305 | if (lvalue) | |
8306 | { | |
8307 | // Modify the fdecl so it carries a pe_long formal | |
8308 | // argument called "value". | |
8309 | ||
8310 | // FIXME: For the time being we only support setting target | |
8311 | // variables which have base types; these are 'pe_long' in | |
8312 | // stap's type vocabulary. Strings and pointers might be | |
8313 | // reasonable, some day, but not today. | |
8314 | ||
8315 | vardecl *v2 = new vardecl; | |
8316 | v2->type = pe_long; | |
8317 | v2->name = "value"; | |
8318 | v2->tok = e->tok; | |
8319 | fdecl->formal_args.push_back(v2); | |
8320 | } | |
8321 | else | |
8322 | ec->code += "/* pure */"; | |
8323 | ||
8324 | dw.sess.functions[fdecl->name] = fdecl; | |
8325 | ||
8326 | // Synthesize a functioncall. | |
8327 | functioncall* n = new functioncall; | |
8328 | n->tok = e->tok; | |
8329 | n->function = fname; | |
8330 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
8331 | ||
8332 | // make the original a bare target symbol for the tracepoint value, | |
8333 | // which will be passed into the dwarf dereferencing code | |
8334 | e->components.clear(); | |
8335 | n->args.push_back(require(e)); | |
8336 | ||
8337 | if (lvalue) | |
8338 | { | |
8339 | // Provide the functioncall to our parent, so that it can be | |
8340 | // used to substitute for the assignment node immediately above | |
8341 | // us. | |
8342 | assert(!target_symbol_setter_functioncalls.empty()); | |
8343 | *(target_symbol_setter_functioncalls.top()) = n; | |
8344 | } | |
8345 | ||
8346 | provide (n); | |
8347 | } | |
8348 | } | |
8349 | ||
8350 | ||
8351 | void | |
8352 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
8353 | { | |
8354 | if (is_active_lvalue (e)) | |
8355 | throw semantic_error("write to tracepoint '" + e->base_name + "' not permitted", e->tok); | |
8356 | ||
8357 | if (!e->components.empty()) | |
8358 | switch (e->components[0].first) | |
8359 | { | |
8360 | case target_symbol::comp_literal_array_index: | |
8361 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as array", | |
8362 | e->tok); | |
8363 | case target_symbol::comp_struct_member: | |
8364 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as a structure", | |
8365 | e->tok); | |
8366 | default: | |
8367 | throw semantic_error("invalid tracepoint '" + e->base_name + "' use", e->tok); | |
8368 | } | |
8369 | ||
8370 | if (e->base_name == "$$name") | |
8371 | { | |
8372 | // Synthesize a functioncall. | |
8373 | functioncall* n = new functioncall; | |
8374 | n->tok = e->tok; | |
8375 | n->function = "_mark_name_get"; | |
8376 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
8377 | provide (n); | |
8378 | } | |
046e7190 | 8379 | else if (e->base_name == "$$vars" || e->base_name == "$$parms") |
f8a968bc | 8380 | { |
f8a968bc JS |
8381 | print_format* pf = new print_format; |
8382 | ||
8383 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
8384 | // NB: we synthesize a new token here rather than reusing | |
8385 | // e->tok, because print_format::print likes to use | |
8386 | // its tok->content. | |
8387 | token* pf_tok = new token(*e->tok); | |
8388 | pf_tok->content = "sprintf"; | |
8389 | ||
8390 | pf->tok = pf_tok; | |
8391 | pf->print_to_stream = false; | |
8392 | pf->print_with_format = true; | |
8393 | pf->print_with_delim = false; | |
8394 | pf->print_with_newline = false; | |
8395 | pf->print_char = false; | |
8396 | ||
8397 | for (unsigned i = 0; i < args.size(); ++i) | |
8398 | { | |
dcaa1a65 JS |
8399 | if (!args[i].usable) |
8400 | continue; | |
f8a968bc JS |
8401 | if (i > 0) |
8402 | pf->raw_components += " "; | |
8403 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 8404 | target_symbol *tsym = new target_symbol; |
f8a968bc JS |
8405 | tsym->tok = e->tok; |
8406 | tsym->base_name = "$" + args[i].name; | |
8407 | ||
8408 | // every variable should always be accessible! | |
8409 | tsym->saved_conversion_error = 0; | |
8410 | expression *texp = require (tsym); // NB: throws nothing ... | |
8411 | assert (!tsym->saved_conversion_error); // ... but this is how we know it happened. | |
8412 | ||
8413 | pf->raw_components += "=%#x"; | |
8414 | pf->args.push_back(texp); | |
8415 | } | |
8416 | ||
8417 | pf->components = print_format::string_to_components(pf->raw_components); | |
8418 | provide (pf); | |
8419 | } | |
8420 | else | |
8421 | assert(0); // shouldn't get here | |
8422 | } | |
8423 | ||
8424 | void | |
8425 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
8426 | { | |
8427 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
8428 | ||
046e7190 JS |
8429 | if (e->base_name == "$$name" || |
8430 | e->base_name == "$$parms" || | |
8431 | e->base_name == "$$vars") | |
f8a968bc JS |
8432 | visit_target_symbol_context (e); |
8433 | else | |
8434 | visit_target_symbol_arg (e); | |
8435 | } | |
8436 | ||
8437 | ||
8438 | ||
79189b84 JS |
8439 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
8440 | dwflpp& dw, Dwarf_Die& func_die, | |
8441 | const string& tracepoint_name, | |
8442 | probe* base, probe_point* loc): | |
8443 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), | |
8444 | sess (s), tracepoint_name (tracepoint_name) | |
8445 | { | |
8446 | // create synthetic probe point name; preserve condition | |
8447 | vector<probe_point::component*> comps; | |
8448 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
8449 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
8450 | this->sole_location()->components = comps; | |
8451 | ||
6fb70fb7 JS |
8452 | // fill out the available arguments in this tracepoint |
8453 | build_args(dw, func_die); | |
8454 | ||
8455 | // determine which header defined this tracepoint | |
8456 | string decl_file = dwarf_decl_file(&func_die); | |
8457 | size_t header_pos = decl_file.rfind("trace/"); | |
8458 | if (header_pos == string::npos) | |
8459 | throw semantic_error ("cannot parse header location for tracepoint '" | |
8460 | + tracepoint_name + "' in '" | |
8461 | + decl_file + "'"); | |
8462 | header = decl_file.substr(header_pos); | |
8463 | ||
8464 | // tracepoints from FOO_event_types.h should really be included from FOO.h | |
8465 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
8466 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 8467 | // XXX: see also PR9993. |
6fb70fb7 JS |
8468 | header_pos = header.find("_event_types"); |
8469 | if (header_pos != string::npos) | |
8470 | header.erase(header_pos, 12); | |
8471 | ||
f8a968bc JS |
8472 | // Now expand the local variables in the probe body |
8473 | tracepoint_var_expanding_visitor v (dw, name, args); | |
8474 | this->body = v.require (this->body); | |
8475 | ||
79189b84 JS |
8476 | if (sess.verbose > 2) |
8477 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name | |
8478 | << "'" << endl; | |
8479 | } | |
8480 | ||
8481 | ||
6fb70fb7 JS |
8482 | static bool |
8483 | dwarf_type_name(Dwarf_Die& type_die, string& c_type) | |
8484 | { | |
219e62c7 JS |
8485 | // if we've gotten down to a basic type, then we're done |
8486 | bool done = true; | |
8487 | switch (dwarf_tag(&type_die)) | |
6fb70fb7 | 8488 | { |
219e62c7 JS |
8489 | case DW_TAG_structure_type: |
8490 | c_type.append("struct "); | |
8491 | break; | |
8492 | case DW_TAG_union_type: | |
8493 | c_type.append("union "); | |
8494 | break; | |
8495 | case DW_TAG_typedef: | |
8496 | case DW_TAG_base_type: | |
8497 | break; | |
8498 | default: | |
8499 | done = false; | |
8500 | break; | |
8501 | } | |
8502 | if (done) | |
8503 | { | |
8504 | c_type.append(dwarf_diename_integrate(&type_die)); | |
6fb70fb7 JS |
8505 | return true; |
8506 | } | |
8507 | ||
8508 | // otherwise, this die is a type modifier. | |
8509 | ||
8510 | // recurse into the referent type | |
219e62c7 | 8511 | // if it can't be named, just call it "void" |
6fb70fb7 JS |
8512 | Dwarf_Attribute subtype_attr; |
8513 | Dwarf_Die subtype_die; | |
8514 | if (!dwarf_attr_integrate(&type_die, DW_AT_type, &subtype_attr) | |
8515 | || !dwarf_formref_die(&subtype_attr, &subtype_die) | |
8516 | || !dwarf_type_name(subtype_die, c_type)) | |
219e62c7 | 8517 | c_type = "void"; |
6fb70fb7 JS |
8518 | |
8519 | const char *suffix = NULL; | |
8520 | switch (dwarf_tag(&type_die)) | |
8521 | { | |
8522 | case DW_TAG_pointer_type: | |
8523 | suffix = "*"; | |
8524 | break; | |
8525 | case DW_TAG_array_type: | |
8526 | suffix = "[]"; | |
8527 | break; | |
8528 | case DW_TAG_const_type: | |
8529 | suffix = " const"; | |
8530 | break; | |
8531 | case DW_TAG_volatile_type: | |
8532 | suffix = " volatile"; | |
8533 | break; | |
8534 | default: | |
8535 | return false; | |
8536 | } | |
8537 | c_type.append(suffix); | |
219e62c7 JS |
8538 | |
8539 | // XXX HACK! The va_list isn't usable as found in the debuginfo... | |
8540 | if (c_type == "struct __va_list_tag*") | |
8541 | c_type = "va_list"; | |
8542 | ||
6fb70fb7 JS |
8543 | return true; |
8544 | } | |
8545 | ||
8546 | ||
f8a968bc | 8547 | static bool |
dcaa1a65 | 8548 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
f8a968bc JS |
8549 | { |
8550 | Dwarf_Attribute type_attr; | |
dcaa1a65 | 8551 | switch (dwarf_tag(&arg.type_die)) |
f8a968bc JS |
8552 | { |
8553 | case DW_TAG_typedef: | |
8554 | case DW_TAG_const_type: | |
8555 | case DW_TAG_volatile_type: | |
8556 | // iterate on the referent type | |
dcaa1a65 JS |
8557 | return (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) |
8558 | && dwarf_formref_die(&type_attr, &arg.type_die) | |
8559 | && resolve_tracepoint_arg_type(arg)); | |
f8a968bc JS |
8560 | case DW_TAG_base_type: |
8561 | // base types will simply be treated as script longs | |
dcaa1a65 | 8562 | arg.isptr = false; |
f8a968bc JS |
8563 | return true; |
8564 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
8565 | // pointers can be treated as script longs, |
8566 | // and if we know their type, they can also be dereferenced | |
8567 | if (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) | |
8568 | && dwarf_formref_die(&type_attr, &arg.type_die)) | |
8569 | arg.isptr = true; | |
ad370dcc JS |
8570 | arg.typecast = "(intptr_t)"; |
8571 | return true; | |
8572 | case DW_TAG_structure_type: | |
8573 | case DW_TAG_union_type: | |
8574 | // for structs/unions which are passed by value, we turn it into | |
8575 | // a pointer that can be dereferenced. | |
8576 | arg.isptr = true; | |
8577 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 8578 | return true; |
f8a968bc JS |
8579 | default: |
8580 | // should we consider other types too? | |
8581 | return false; | |
8582 | } | |
8583 | } | |
8584 | ||
8585 | ||
6fb70fb7 JS |
8586 | void |
8587 | tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die) | |
8588 | { | |
8589 | Dwarf_Die arg; | |
8590 | if (dwarf_child(&func_die, &arg) == 0) | |
8591 | do | |
8592 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
8593 | { | |
8594 | // build a tracepoint_arg for this parameter | |
8595 | tracepoint_arg tparg; | |
8596 | tparg.name = dwarf_diename_integrate(&arg); | |
8597 | ||
8598 | // read the type of this parameter | |
8599 | Dwarf_Attribute type_attr; | |
6fb70fb7 | 8600 | if (!dwarf_attr_integrate (&arg, DW_AT_type, &type_attr) |
f8a968bc | 8601 | || !dwarf_formref_die (&type_attr, &tparg.type_die) |
dcaa1a65 | 8602 | || !dwarf_type_name(tparg.type_die, tparg.c_type)) |
6fb70fb7 JS |
8603 | throw semantic_error ("cannot get type of tracepoint '" |
8604 | + tracepoint_name + "' parameter '" | |
8605 | + tparg.name + "'"); | |
8606 | ||
dcaa1a65 | 8607 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
8608 | args.push_back(tparg); |
8609 | if (sess.verbose > 4) | |
8610 | clog << "found parameter for tracepoint '" << tracepoint_name | |
8611 | << "': type:'" << tparg.c_type | |
8612 | << "' name:'" << tparg.name << "'" << endl; | |
8613 | } | |
8614 | while (dwarf_siblingof(&arg, &arg) == 0); | |
8615 | } | |
8616 | ||
e2086848 WH |
8617 | void |
8618 | tracepoint_derived_probe::printargs(std::ostream &o) const | |
8619 | { | |
dcaa1a65 JS |
8620 | for (unsigned i = 0; i < args.size(); ++i) |
8621 | if (args[i].usable) | |
8622 | o << " $" << args[i].name << ":" << args[i].c_type; | |
e2086848 | 8623 | } |
6fb70fb7 | 8624 | |
79189b84 JS |
8625 | void |
8626 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
8627 | { | |
8628 | if (! s.tracepoint_derived_probes) | |
8629 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
8630 | s.tracepoint_derived_probes->enroll (this); | |
8631 | } | |
8632 | ||
8633 | ||
3e3bd7b6 JS |
8634 | void |
8635 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) | |
8636 | { | |
8637 | for (unsigned i = 0; i < args.size(); i++) | |
8638 | if (args[i].used) | |
8639 | o << "__tracepoint_arg_" << args[i].name << endl; | |
8640 | } | |
8641 | ||
8642 | ||
f8a968bc JS |
8643 | void |
8644 | tracepoint_derived_probe::emit_probe_context_vars (translator_output* o) | |
8645 | { | |
8646 | for (unsigned i = 0; i < args.size(); i++) | |
8647 | if (args[i].used) | |
8648 | o->newline() << "int64_t __tracepoint_arg_" << args[i].name << ";"; | |
8649 | } | |
8650 | ||
8651 | ||
3c1b3d06 FCE |
8652 | static vector<string> tracepoint_extra_headers () |
8653 | { | |
8654 | vector<string> they_live; | |
8655 | // PR 9993 | |
8656 | // XXX: may need this to be configurable | |
8657 | they_live.push_back ("linux/skbuff.h"); | |
8658 | return they_live; | |
8659 | } | |
8660 | ||
8661 | ||
79189b84 JS |
8662 | void |
8663 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8664 | { | |
8665 | if (probes.empty()) | |
8666 | return; | |
8667 | ||
96b030fe JS |
8668 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
8669 | s.op->newline(); | |
79189b84 | 8670 | |
3c1b3d06 FCE |
8671 | // PR9993: Add extra headers to work around undeclared types in individual |
8672 | // include/trace/foo.h files | |
8673 | const vector<string>& extra_headers = tracepoint_extra_headers (); | |
8674 | for (unsigned z=0; z<extra_headers.size(); z++) | |
8675 | s.op->newline() << "#include <" << extra_headers[z] << ">\n"; | |
8676 | ||
6fb70fb7 JS |
8677 | for (unsigned i = 0; i < probes.size(); ++i) |
8678 | { | |
8679 | tracepoint_derived_probe *p = probes[i]; | |
96b030fe JS |
8680 | |
8681 | // emit a separate entry function for each probe, since tracepoints | |
8682 | // don't provide any sort of context pointer. | |
6fb70fb7 JS |
8683 | s.op->newline() << "#include <" << p->header << ">"; |
8684 | s.op->newline() << "static void enter_tracepoint_probe_" << i << "("; | |
8df306c4 JS |
8685 | if (p->args.size() == 0) |
8686 | s.op->line() << "void"; | |
6fb70fb7 JS |
8687 | for (unsigned j = 0; j < p->args.size(); ++j) |
8688 | { | |
8689 | if (j > 0) | |
8690 | s.op->line() << ", "; | |
8691 | s.op->line() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
8692 | } | |
8693 | s.op->line() << ") {"; | |
8694 | s.op->indent(1); | |
c12d974f FCE |
8695 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", |
8696 | lex_cast_qstring (*p->sole_location())); | |
f8a968bc | 8697 | s.op->newline() << "c->marker_name = " |
c12d974f FCE |
8698 | << lex_cast_qstring (p->tracepoint_name) |
8699 | << ";"; | |
f8a968bc JS |
8700 | for (unsigned j = 0; j < p->args.size(); ++j) |
8701 | if (p->args[j].used) | |
8702 | { | |
8703 | s.op->newline() << "c->locals[0]." << p->name << ".__tracepoint_arg_" | |
8704 | << p->args[j].name << " = (int64_t)"; | |
ad370dcc | 8705 | s.op->line() << p->args[j].typecast; |
f8a968bc JS |
8706 | s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";"; |
8707 | } | |
6fb70fb7 JS |
8708 | s.op->newline() << p->name << " (c);"; |
8709 | common_probe_entryfn_epilogue (s.op); | |
8710 | s.op->newline(-1) << "}"; | |
96b030fe JS |
8711 | |
8712 | // emit normalized registration functions | |
8713 | s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {"; | |
8714 | s.op->newline(1) << "return register_trace_" << p->tracepoint_name | |
8715 | << "(enter_tracepoint_probe_" << i << ");"; | |
8716 | s.op->newline(-1) << "}"; | |
86758d5f JS |
8717 | |
8718 | // NB: we're not prepared to deal with unreg failures. However, failures | |
8719 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
8720 | // weren't even registered. The former should be OKed by the initial | |
8721 | // registration call, and the latter is safe to ignore. | |
8722 | s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {"; | |
8723 | s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name | |
96b030fe JS |
8724 | << "(enter_tracepoint_probe_" << i << ");"; |
8725 | s.op->newline(-1) << "}"; | |
6fb70fb7 JS |
8726 | s.op->newline(); |
8727 | } | |
96b030fe JS |
8728 | |
8729 | // emit an array of registration functions for easy init/shutdown | |
8730 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
8731 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 8732 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
8733 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
8734 | s.op->indent(1); | |
8735 | for (unsigned i = 0; i < probes.size(); ++i) | |
8736 | { | |
8737 | s.op->newline () << "{"; | |
8738 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
8739 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
8740 | s.op->line() << " },"; | |
8741 | } | |
8742 | s.op->newline(-1) << "};"; | |
8743 | s.op->newline(); | |
79189b84 JS |
8744 | } |
8745 | ||
8746 | ||
8747 | void | |
8748 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
8749 | { | |
8750 | if (probes.size () == 0) | |
8751 | return; | |
8752 | ||
8753 | s.op->newline() << "/* init tracepoint probes */"; | |
96b030fe JS |
8754 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
8755 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
8756 | s.op->newline() << "if (rc) {"; | |
8757 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
8758 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
8759 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
8760 | s.op->newline(-1) << "}"; | |
8761 | s.op->newline(-1) << "}"; | |
bc9a523d FCE |
8762 | |
8763 | // This would be technically proper (on those autoconf-detectable | |
8764 | // kernels that include this function in tracepoint.h), however we | |
8765 | // already make several calls to synchronze_sched() during our | |
8766 | // shutdown processes. | |
8767 | ||
8768 | // s.op->newline() << "if (rc)"; | |
8769 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
8770 | // s.op->indent(-1); | |
79189b84 JS |
8771 | } |
8772 | ||
8773 | ||
8774 | void | |
8775 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8776 | { | |
8777 | if (probes.empty()) | |
8778 | return; | |
8779 | ||
96b030fe JS |
8780 | s.op->newline() << "/* deregister tracepoint probes */"; |
8781 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
8782 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
8783 | s.op->indent(-1); | |
bc9a523d FCE |
8784 | |
8785 | // Not necessary: see above. | |
8786 | ||
8787 | // s.op->newline() << "tracepoint_synchronize_unregister();"; | |
79189b84 JS |
8788 | } |
8789 | ||
8790 | ||
75ead1f7 JS |
8791 | struct tracepoint_query : public base_query |
8792 | { | |
8793 | tracepoint_query(dwflpp & dw, const string & tracepoint, | |
8794 | probe * base_probe, probe_point * base_loc, | |
8795 | vector<derived_probe *> & results): | |
8796 | base_query(dw, "*"), tracepoint(tracepoint), | |
8797 | base_probe(base_probe), base_loc(base_loc), | |
8798 | results(results) {} | |
8799 | ||
8800 | const string& tracepoint; | |
8801 | ||
8802 | probe * base_probe; | |
8803 | probe_point * base_loc; | |
8804 | vector<derived_probe *> & results; | |
8805 | ||
8806 | void handle_query_module(); | |
8807 | int handle_query_cu(Dwarf_Die * cudie); | |
8808 | int handle_query_func(Dwarf_Die * func); | |
8809 | ||
8810 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); | |
8811 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
8812 | }; | |
8813 | ||
8814 | ||
8815 | void | |
8816 | tracepoint_query::handle_query_module() | |
8817 | { | |
8818 | // look for the tracepoints in each CU | |
8819 | dw.iterate_over_cus(tracepoint_query_cu, this); | |
8820 | } | |
8821 | ||
8822 | ||
8823 | int | |
8824 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
8825 | { | |
8826 | dw.focus_on_cu (cudie); | |
8827 | ||
8828 | // look at each function to see if it's a tracepoint | |
8829 | string function = "stapprobe_" + tracepoint; | |
8830 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
8831 | } | |
8832 | ||
8833 | ||
8834 | int | |
8835 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
8836 | { | |
8837 | dw.focus_on_function (func); | |
8838 | ||
8839 | assert(dw.function_name.compare(0, 10, "stapprobe_") == 0); | |
8840 | string tracepoint_instance = dw.function_name.substr(10); | |
79189b84 JS |
8841 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
8842 | tracepoint_instance, | |
8843 | base_probe, base_loc); | |
8844 | results.push_back (dp); | |
75ead1f7 JS |
8845 | return DWARF_CB_OK; |
8846 | } | |
8847 | ||
8848 | ||
8849 | int | |
8850 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
8851 | { | |
8852 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); | |
8853 | if (pending_interrupts) return DWARF_CB_ABORT; | |
8854 | return q->handle_query_cu(cudie); | |
8855 | } | |
8856 | ||
8857 | ||
8858 | int | |
8859 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
8860 | { | |
8861 | tracepoint_query * q = static_cast<tracepoint_query *>(query); | |
8862 | if (pending_interrupts) return DWARF_CB_ABORT; | |
8863 | return q->handle_query_func(func); | |
8864 | } | |
8865 | ||
8866 | ||
0a6f5a3f JS |
8867 | struct tracepoint_builder: public derived_probe_builder |
8868 | { | |
8869 | private: | |
8870 | dwflpp *dw; | |
8871 | bool init_dw(systemtap_session& s); | |
8872 | ||
8873 | public: | |
3c1b3d06 | 8874 | |
0a6f5a3f JS |
8875 | tracepoint_builder(): dw(0) {} |
8876 | ~tracepoint_builder() { delete dw; } | |
8877 | ||
8878 | void build_no_more (systemtap_session& s) | |
8879 | { | |
8880 | if (dw && s.verbose > 3) | |
8881 | clog << "tracepoint_builder releasing dwflpp" << endl; | |
8882 | delete dw; | |
8883 | dw = NULL; | |
8884 | } | |
8885 | ||
8886 | void build(systemtap_session& s, | |
8887 | probe *base, probe_point *location, | |
8888 | literal_map_t const& parameters, | |
8889 | vector<derived_probe*>& finished_results); | |
8890 | }; | |
8891 | ||
8892 | ||
8893 | bool | |
8894 | tracepoint_builder::init_dw(systemtap_session& s) | |
8895 | { | |
8896 | if (dw != NULL) | |
8897 | return true; | |
8898 | ||
b278033a JS |
8899 | if (s.use_cache) |
8900 | { | |
8901 | // see if the cached module exists | |
8902 | find_tracequery_hash(s); | |
8903 | if (!s.tracequery_path.empty()) | |
8904 | { | |
8905 | int fd = open(s.tracequery_path.c_str(), O_RDONLY); | |
8906 | if (fd != -1) | |
8907 | { | |
8908 | if (s.verbose > 2) | |
8909 | clog << "Pass 2: using cached " << s.tracequery_path << endl; | |
8910 | ||
8911 | dw = new dwflpp(s); | |
8912 | dw->setup_user(s.tracequery_path); | |
8913 | close(fd); | |
8914 | return true; | |
8915 | } | |
8916 | } | |
8917 | } | |
8918 | ||
8919 | // no cached module, time to make it | |
0a6f5a3f | 8920 | string tracequery_ko; |
3c1b3d06 | 8921 | int rc = make_tracequery(s, tracequery_ko, tracepoint_extra_headers()); |
0a6f5a3f JS |
8922 | if (rc != 0) |
8923 | return false; | |
8924 | ||
b278033a JS |
8925 | if (s.use_cache) |
8926 | { | |
8927 | // try to save tracequery in the cache | |
8928 | if (s.verbose > 2) | |
8929 | clog << "Copying " << tracequery_ko | |
8930 | << " to " << s.tracequery_path << endl; | |
8931 | if (copy_file(tracequery_ko.c_str(), | |
8932 | s.tracequery_path.c_str()) != 0) | |
8933 | cerr << "Copy failed (\"" << tracequery_ko << "\" to \"" | |
8934 | << s.tracequery_path << "\"): " << strerror(errno) << endl; | |
8935 | } | |
0a6f5a3f JS |
8936 | |
8937 | dw = new dwflpp(s); | |
8938 | dw->setup_user(tracequery_ko); | |
8939 | return true; | |
8940 | } | |
8941 | ||
8942 | ||
8943 | void | |
8944 | tracepoint_builder::build(systemtap_session& s, | |
8945 | probe *base, probe_point *location, | |
8946 | literal_map_t const& parameters, | |
8947 | vector<derived_probe*>& finished_results) | |
8948 | { | |
8949 | if (!init_dw(s)) | |
8950 | return; | |
8951 | ||
75ead1f7 JS |
8952 | string tracepoint; |
8953 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
8954 | ||
8955 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); | |
8956 | dw->query_modules(&q); | |
0a6f5a3f JS |
8957 | } |
8958 | ||
8959 | ||
8960 | ||
b55bc428 | 8961 | // ------------------------------------------------------------------------ |
bd2b1e68 | 8962 | // Standard tapset registry. |
b55bc428 FCE |
8963 | // ------------------------------------------------------------------------ |
8964 | ||
7a053d3b | 8965 | void |
f8220a7b | 8966 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 8967 | { |
47e0478e | 8968 | register_tapset_been(s); |
93646f4d | 8969 | register_tapset_itrace(s); |
dd0e4fa7 | 8970 | register_tapset_mark(s); |
01c2eefe | 8971 | register_tapset_perfmon(s); |
7a212aa8 | 8972 | register_tapset_procfs(s); |
912e8c59 JS |
8973 | register_tapset_timers(s); |
8974 | ||
b98a8d73 | 8975 | |
7a24d422 | 8976 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 8977 | dwarf_derived_probe::register_patterns(s); |
30a279be | 8978 | |
888af770 FCE |
8979 | // XXX: user-space starter set |
8980 | s.pattern_root->bind_num(TOK_PROCESS) | |
8981 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
8982 | ->bind(new uprobe_builder ()); | |
8983 | s.pattern_root->bind_num(TOK_PROCESS) | |
8984 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
8985 | ->bind(new uprobe_builder ()); | |
8986 | ||
935447c8 | 8987 | // utrace user-space probes |
eff6ac72 DS |
8988 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_BEGIN) |
8989 | ->bind(new utrace_builder ()); | |
8990 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_BEGIN) | |
8991 | ->bind(new utrace_builder ()); | |
986e98de DS |
8992 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_BEGIN) |
8993 | ->bind(new utrace_builder ()); | |
eff6ac72 | 8994 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_END) |
935447c8 | 8995 | ->bind(new utrace_builder ()); |
eff6ac72 | 8996 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_END) |
935447c8 | 8997 | ->bind(new utrace_builder ()); |
986e98de DS |
8998 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_END) |
8999 | ->bind(new utrace_builder ()); | |
eff6ac72 | 9000 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
159cb109 | 9001 | ->bind(new utrace_builder ()); |
eff6ac72 DS |
9002 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
9003 | ->bind(new utrace_builder ()); | |
986e98de DS |
9004 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
9005 | ->bind(new utrace_builder ()); | |
eff6ac72 DS |
9006 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) |
9007 | ->bind(new utrace_builder ()); | |
9008 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) | |
159cb109 | 9009 | ->bind(new utrace_builder ()); |
986e98de DS |
9010 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) |
9011 | ->bind(new utrace_builder ()); | |
12b21830 | 9012 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_SYSCALL) |
935447c8 | 9013 | ->bind(new utrace_builder ()); |
12b21830 | 9014 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_SYSCALL) |
935447c8 | 9015 | ->bind(new utrace_builder ()); |
480f38d3 DS |
9016 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_SYSCALL) |
9017 | ->bind(new utrace_builder ()); | |
12b21830 | 9018 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
935447c8 | 9019 | ->bind(new utrace_builder ()); |
12b21830 | 9020 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
935447c8 | 9021 | ->bind(new utrace_builder ()); |
480f38d3 DS |
9022 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
9023 | ->bind(new utrace_builder ()); | |
9024 | ||
0a6f5a3f JS |
9025 | // kernel tracepoint probes |
9026 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
9027 | ->bind(new tracepoint_builder()); | |
9028 | ||
e6fe60e7 AM |
9029 | // Kprobe based probe |
9030 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
9031 | ->bind(new kprobe_builder()); | |
9032 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) | |
9033 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
9034 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
9035 | ->bind(new kprobe_builder()); | |
b6371390 JS |
9036 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
9037 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
9038 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
9039 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
9040 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
9041 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
9042 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
9043 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
9044 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
b55bc428 | 9045 | } |
dc38c0ae DS |
9046 | |
9047 | ||
b20febf3 FCE |
9048 | vector<derived_probe_group*> |
9049 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 9050 | { |
b20febf3 | 9051 | vector<derived_probe_group*> g; |
912e8c59 JS |
9052 | |
9053 | #define DOONE(x) \ | |
9054 | if (s. x##_derived_probes) \ | |
9055 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
9056 | |
9057 | // Note that order *is* important here. We want to make sure we | |
9058 | // register (actually run) begin probes before any other probe type | |
9059 | // is run. Similarly, when unregistering probes, we want to | |
9060 | // unregister (actually run) end probes after every other probe type | |
9061 | // has be unregistered. To do the latter, | |
9062 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
9063 | DOONE(be); |
9064 | DOONE(dwarf); | |
888af770 | 9065 | DOONE(uprobe); |
b20febf3 FCE |
9066 | DOONE(timer); |
9067 | DOONE(profile); | |
9068 | DOONE(mark); | |
0a6f5a3f | 9069 | DOONE(tracepoint); |
e6fe60e7 | 9070 | DOONE(kprobe); |
b20febf3 FCE |
9071 | DOONE(hrtimer); |
9072 | DOONE(perfmon); | |
ce82316f | 9073 | DOONE(procfs); |
935447c8 DS |
9074 | |
9075 | // Another "order is important" item. We want to make sure we | |
9076 | // "register" the dummy task_finder probe group after all probe | |
9077 | // groups that use the task_finder. | |
9078 | DOONE(utrace); | |
a96d1db0 | 9079 | DOONE(itrace); |
935447c8 | 9080 | DOONE(task_finder); |
b20febf3 FCE |
9081 | #undef DOONE |
9082 | return g; | |
46b84a80 | 9083 | } |
73267b89 JS |
9084 | |
9085 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |