]>
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" |
56e12059 | 15 | #include "translate.h" |
dc38c0ae | 16 | #include "session.h" |
72dbc915 | 17 | #include "util.h" |
0a6f5a3f | 18 | #include "buildrun.h" |
86bf665e | 19 | #include "dwarf_wrappers.h" |
2e67a43b | 20 | #include "auto_free.h" |
b278033a | 21 | #include "hash.h" |
bd2b1e68 | 22 | |
3b579393 FCE |
23 | #include <cstdlib> |
24 | #include <algorithm> | |
bd2b1e68 | 25 | #include <deque> |
56e12059 | 26 | #include <iostream> |
bd2b1e68 | 27 | #include <map> |
3bf6ac45 TM |
28 | #ifdef HAVE_TR1_UNORDERED_MAP |
29 | #include <tr1/unordered_map> | |
30 | #else | |
2171f774 | 31 | #include <ext/hash_map> |
3bf6ac45 | 32 | #endif |
ec4373ff | 33 | #include <set> |
56e12059 | 34 | #include <sstream> |
bd2b1e68 | 35 | #include <stdexcept> |
b55bc428 | 36 | #include <vector> |
e36387d7 | 37 | #include <cstdarg> |
29e64872 | 38 | #include <cassert> |
1969b5bc | 39 | #include <iomanip> |
f781f849 | 40 | #include <cerrno> |
bd2b1e68 GH |
41 | |
42 | extern "C" { | |
df8fadee | 43 | #include <fcntl.h> |
bd2b1e68 | 44 | #include <elfutils/libdwfl.h> |
7a053d3b | 45 | #include <elfutils/libdw.h> |
77de5e9e GH |
46 | #include <dwarf.h> |
47 | #include <elf.h> | |
48 | #include <obstack.h> | |
30a279be | 49 | #include <regex.h> |
b20febf3 | 50 | #include <glob.h> |
30a279be | 51 | #include <fnmatch.h> |
5f0a03a6 | 52 | #include <stdio.h> |
349dc70e | 53 | #include <sys/types.h> |
4b1ad75e | 54 | |
30a279be | 55 | #include "loc2c.h" |
4b1ad75e RM |
56 | #define __STDC_FORMAT_MACROS |
57 | #include <inttypes.h> | |
bd2b1e68 | 58 | } |
77de5e9e | 59 | |
56e12059 | 60 | |
47dd066d WC |
61 | #ifdef PERFMON |
62 | #include <perfmon/pfmlib.h> | |
63 | #include <perfmon/perfmon.h> | |
64 | #endif | |
65 | ||
56e12059 | 66 | using namespace std; |
2171f774 | 67 | using namespace __gnu_cxx; |
56e12059 | 68 | |
b20febf3 FCE |
69 | // ------------------------------------------------------------------------ |
70 | // Generic derived_probe_group: contains an ordinary vector of the | |
71 | // given type. It provides only the enrollment function. | |
72 | ||
73 | template <class DP> struct generic_dpg: public derived_probe_group | |
74 | { | |
75 | protected: | |
76 | vector <DP*> probes; | |
77 | public: | |
78 | generic_dpg () {} | |
79 | void enroll (DP* probe) { probes.push_back (probe); } | |
80 | }; | |
46b84a80 DS |
81 | |
82 | ||
b20febf3 FCE |
83 | |
84 | // ------------------------------------------------------------------------ | |
65aeaea0 | 85 | // begin/end/error probes are run right during registration / deregistration |
9a604fac FCE |
86 | // ------------------------------------------------------------------------ |
87 | ||
12b21830 DS |
88 | static string TOK_BEGIN("begin"); |
89 | static string TOK_END("end"); | |
90 | static string TOK_ERROR("error"); | |
91 | ||
65aeaea0 FCE |
92 | enum be_t { BEGIN, END, ERROR }; |
93 | ||
b20febf3 FCE |
94 | struct be_derived_probe: public derived_probe |
95 | { | |
65aeaea0 | 96 | be_t type; |
16e8f21f JS |
97 | int64_t priority; |
98 | ||
65aeaea0 FCE |
99 | be_derived_probe (probe* p, probe_point* l, be_t t, int64_t pr): |
100 | derived_probe (p, l), type (t), priority (pr) {} | |
b20febf3 FCE |
101 | |
102 | void join_group (systemtap_session& s); | |
16e8f21f JS |
103 | |
104 | static inline bool comp(be_derived_probe const *a, | |
105 | be_derived_probe const *b) | |
65aeaea0 FCE |
106 | { |
107 | // This allows the BEGIN/END/ERROR probes to intermingle. | |
108 | // But that's OK - they're always treversed with a nested | |
109 | // "if (type==FOO)" conditional. | |
4baf0e53 | 110 | return a->priority < b->priority; |
65aeaea0 | 111 | } |
90f98cc3 DS |
112 | |
113 | bool needs_global_locks () { return false; } | |
114 | // begin/end probes don't need locks around global variables, since | |
115 | // they aren't run concurrently with any other probes | |
b20febf3 FCE |
116 | }; |
117 | ||
118 | ||
119 | struct be_derived_probe_group: public generic_dpg<be_derived_probe> | |
120 | { | |
121 | public: | |
122 | void emit_module_decls (systemtap_session& s); | |
123 | void emit_module_init (systemtap_session& s); | |
124 | void emit_module_exit (systemtap_session& s); | |
125 | }; | |
126 | ||
b20febf3 FCE |
127 | struct be_builder: public derived_probe_builder |
128 | { | |
65aeaea0 FCE |
129 | be_t type; |
130 | ||
131 | be_builder(be_t t) : type(t) {} | |
132 | ||
78f6bba6 | 133 | virtual void build(systemtap_session &, |
b20febf3 FCE |
134 | probe * base, |
135 | probe_point * location, | |
86bf665e | 136 | literal_map_t const & parameters, |
b20febf3 FCE |
137 | vector<derived_probe *> & finished_results) |
138 | { | |
16e8f21f | 139 | int64_t priority; |
12b21830 DS |
140 | if ((type == BEGIN && !get_param(parameters, TOK_BEGIN, priority)) || |
141 | (type == END && !get_param(parameters, TOK_END, priority)) || | |
142 | (type == ERROR && !get_param(parameters, TOK_ERROR, priority))) | |
16e8f21f JS |
143 | priority = 0; |
144 | finished_results.push_back( | |
65aeaea0 | 145 | new be_derived_probe(base, location, type, priority)); |
b20febf3 FCE |
146 | } |
147 | }; | |
148 | ||
149 | ||
9a604fac | 150 | void |
b20febf3 | 151 | be_derived_probe::join_group (systemtap_session& s) |
9a604fac | 152 | { |
b20febf3 FCE |
153 | if (! s.be_derived_probes) |
154 | s.be_derived_probes = new be_derived_probe_group (); | |
155 | s.be_derived_probes->enroll (this); | |
156 | } | |
47dd066d | 157 | |
b20febf3 FCE |
158 | |
159 | // ------------------------------------------------------------------------ | |
160 | void | |
a58d79d0 | 161 | common_probe_entryfn_prologue (translator_output* o, string statestr, |
c12d974f | 162 | string new_pp, |
e0a17418 | 163 | bool overload_processing = true) |
b20febf3 | 164 | { |
72d18b98 | 165 | o->newline() << "struct context* __restrict__ c;"; |
e0a17418 JS |
166 | o->newline() << "#if !INTERRUPTIBLE"; |
167 | o->newline() << "unsigned long flags;"; | |
168 | o->newline() << "#endif"; | |
b20febf3 | 169 | |
a58d79d0 DS |
170 | if (overload_processing) |
171 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
172 | else | |
173 | o->newline() << "#ifdef STP_TIMING"; | |
174 | o->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
b20febf3 | 175 | o->newline() << "#endif"; |
b20febf3 FCE |
176 | |
177 | #if 0 /* XXX: PERFMON */ | |
47dd066d WC |
178 | o->newline() << "static struct pfarg_ctx _pfm_context;"; |
179 | o->newline() << "static void *_pfm_desc;"; | |
180 | o->newline() << "static struct pfarg_pmc *_pfm_pmc_x;"; | |
181 | o->newline() << "static int _pfm_num_pmc_x;"; | |
182 | o->newline() << "static struct pfarg_pmd *_pfm_pmd_x;"; | |
183 | o->newline() << "static int _pfm_num_pmd_x;"; | |
184 | #endif | |
185 | ||
e0a17418 JS |
186 | o->newline() << "#if INTERRUPTIBLE"; |
187 | o->newline() << "preempt_disable ();"; | |
188 | o->newline() << "#else"; | |
189 | o->newline() << "local_irq_save (flags);"; | |
190 | o->newline() << "#endif"; | |
b20febf3 | 191 | |
c931ec8a | 192 | // Check for enough free enough stack space |
d05a1d00 | 193 | o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space |
a63401b1 | 194 | o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space |
d05a1d00 FCE |
195 | // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack |
196 | // NB: see also CONFIG_DEBUG_STACKOVERFLOW | |
b3c3ca7c FCE |
197 | o->newline() << "atomic_inc (& skipped_count);"; |
198 | o->newline() << "#ifdef STP_TIMING"; | |
199 | o->newline() << "atomic_inc (& skipped_count_lowstack);"; | |
200 | o->newline() << "#endif"; | |
c931ec8a FCE |
201 | o->newline() << "goto probe_epilogue;"; |
202 | o->newline(-1) << "}"; | |
203 | ||
b20febf3 FCE |
204 | o->newline() << "if (atomic_read (&session_state) != " << statestr << ")"; |
205 | o->newline(1) << "goto probe_epilogue;"; | |
206 | o->indent(-1); | |
9a604fac | 207 | |
a44a0785 | 208 | o->newline() << "c = per_cpu_ptr (contexts, smp_processor_id());"; |
b3c3ca7c | 209 | o->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
9c736061 FCE |
210 | o->newline(1) << "#if !INTERRUPTIBLE"; |
211 | o->newline() << "atomic_inc (& skipped_count);"; | |
212 | o->newline() << "#endif"; | |
b3c3ca7c FCE |
213 | o->newline() << "#ifdef STP_TIMING"; |
214 | o->newline() << "atomic_inc (& skipped_count_reentrant);"; | |
c12d974f FCE |
215 | o->newline() << "#ifdef DEBUG_REENTRANCY"; |
216 | o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
217 | << new_pp << ", c->probe_point ?: \"?\", smp_processor_id());"; | |
218 | // NB: There is a conceivable race condition here with reading | |
219 | // c->probe_point, knowing that this other probe is sort of running. | |
220 | // However, in reality, it's interrupted. Plus even if it were able | |
221 | // to somehow start again, and stop before we read c->probe_point, | |
222 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
223 | o->newline() << "#endif"; | |
b3c3ca7c | 224 | o->newline() << "#endif"; |
9a604fac | 225 | o->newline() << "atomic_dec (& c->busy);"; |
b20febf3 | 226 | o->newline() << "goto probe_epilogue;"; |
9a604fac FCE |
227 | o->newline(-1) << "}"; |
228 | o->newline(); | |
1e00cfb1 | 229 | o->newline() << "c->last_stmt = 0;"; |
9a604fac | 230 | o->newline() << "c->last_error = 0;"; |
9a604fac | 231 | o->newline() << "c->nesting = 0;"; |
22f8b401 | 232 | o->newline() << "c->regs = 0;"; |
b916df9c | 233 | o->newline() << "c->unwaddr = 0;"; |
c12d974f | 234 | o->newline() << "c->probe_point = " << new_pp << ";"; |
b916df9c | 235 | // reset unwound address cache |
fcff848e | 236 | o->newline() << "c->pi = 0;"; |
9addf322 | 237 | o->newline() << "c->regparm = 0;"; |
bc54e71c MH |
238 | o->newline() << "c->marker_name = NULL;"; |
239 | o->newline() << "c->marker_format = NULL;"; | |
e0a17418 JS |
240 | |
241 | o->newline() << "#if INTERRUPTIBLE"; | |
242 | o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
243 | o->newline() << "#else"; | |
244 | o->newline() << "c->actionremaining = MAXACTION;"; | |
245 | o->newline() << "#endif"; | |
dbb68664 FCE |
246 | o->newline() << "#ifdef STP_TIMING"; |
247 | o->newline() << "c->statp = 0;"; | |
248 | o->newline() << "#endif"; | |
9915575b FCE |
249 | // NB: The following would actually be incorrect. |
250 | // That's because cycles_sum/cycles_base values are supposed to survive | |
251 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
252 | // cycles), the values will be reset. | |
253 | /* | |
f0e6dc63 FCE |
254 | o->newline() << "#ifdef STP_OVERLOAD"; |
255 | o->newline() << "c->cycles_sum = 0;"; | |
256 | o->newline() << "c->cycles_base = 0;"; | |
41c262f3 | 257 | o->newline() << "#endif"; |
9915575b | 258 | */ |
b20febf3 | 259 | } |
9a604fac | 260 | |
a44a0785 | 261 | |
b20febf3 | 262 | void |
a58d79d0 | 263 | common_probe_entryfn_epilogue (translator_output* o, |
e0a17418 | 264 | bool overload_processing = true) |
b20febf3 | 265 | { |
a58d79d0 DS |
266 | if (overload_processing) |
267 | o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
268 | else | |
269 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 270 | o->newline() << "{"; |
a58d79d0 DS |
271 | o->newline(1) << "cycles_t cycles_atend = get_cycles ();"; |
272 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
273 | // fewer, if the hardware counter rolls over really quickly. We | |
274 | // handle 32-bit wraparound here. | |
275 | o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
276 | o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
277 | o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
278 | o->indent(-1); | |
dbb68664 | 279 | |
a58d79d0 | 280 | o->newline() << "#ifdef STP_TIMING"; |
dbb68664 | 281 | o->newline() << "if (likely (c->statp)) _stp_stat_add(*c->statp, cycles_elapsed);"; |
a58d79d0 DS |
282 | o->newline() << "#endif"; |
283 | ||
284 | if (overload_processing) | |
285 | { | |
286 | o->newline() << "#ifdef STP_OVERLOAD"; | |
287 | o->newline() << "{"; | |
288 | // If the cycle count has wrapped (cycles_atend > cycles_base), | |
289 | // let's go ahead and pretend the interval has been reached. | |
290 | // This should reset cycles_base and cycles_sum. | |
291 | o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; | |
292 | o->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
293 | o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
294 | o->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
295 | ||
296 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
297 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
298 | // has overloaded the system and we need to quit. | |
299 | o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; | |
300 | o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
301 | o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
302 | o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);"; | |
551e9f14 | 303 | o->newline() << "atomic_inc (&error_count);"; |
a58d79d0 DS |
304 | o->newline(-1) << "}"; |
305 | ||
306 | o->newline() << "c->cycles_base = cycles_atend;"; | |
307 | o->newline() << "c->cycles_sum = 0;"; | |
308 | o->newline(-1) << "}"; | |
309 | o->newline(-1) << "}"; | |
310 | o->newline() << "#endif"; | |
311 | } | |
312 | ||
dbb68664 FCE |
313 | o->newline(-1) << "}"; |
314 | o->newline() << "#endif"; | |
315 | ||
c3add01f | 316 | o->newline() << "c->probe_point = 0;"; // vacated |
9a604fac FCE |
317 | o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; |
318 | o->newline(1) << "if (c->last_stmt != NULL)"; | |
319 | o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
320 | o->newline(-1) << "else"; | |
321 | o->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
322 | o->indent(-1); | |
323 | o->newline() << "atomic_inc (& error_count);"; | |
9a604fac FCE |
324 | o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {"; |
325 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
326 | o->newline() << "_stp_exit ();"; | |
327 | o->newline(-1) << "}"; | |
9a604fac | 328 | o->newline(-1) << "}"; |
9a604fac | 329 | o->newline() << "atomic_dec (&c->busy);"; |
a44a0785 | 330 | |
b20febf3 FCE |
331 | o->newline(-1) << "probe_epilogue:"; // context is free |
332 | o->indent(1); | |
a44a0785 | 333 | |
b3c3ca7c FCE |
334 | // Check for excessive skip counts. |
335 | o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {"; | |
336 | o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
337 | o->newline() << "_stp_exit ();"; | |
338 | o->newline(-1) << "}"; | |
339 | ||
e0a17418 JS |
340 | o->newline() << "#if INTERRUPTIBLE"; |
341 | o->newline() << "preempt_enable_no_resched ();"; | |
342 | o->newline() << "#else"; | |
343 | o->newline() << "local_irq_restore (flags);"; | |
344 | o->newline() << "#endif"; | |
9a604fac FCE |
345 | } |
346 | ||
347 | ||
56e12059 FCE |
348 | // ------------------------------------------------------------------------ |
349 | ||
56e12059 | 350 | void |
b20febf3 FCE |
351 | be_derived_probe_group::emit_module_decls (systemtap_session& s) |
352 | { | |
353 | if (probes.empty()) return; | |
354 | ||
355 | s.op->newline() << "/* ---- begin/end probes ---- */"; | |
4c2732a1 | 356 | s.op->newline() << "static void enter_begin_probe (void (*fn)(struct context*), const char* pp) {"; |
b20febf3 | 357 | s.op->indent(1); |
e0a17418 | 358 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_STARTING", "pp", false); |
b20febf3 | 359 | s.op->newline() << "(*fn) (c);"; |
e0a17418 | 360 | common_probe_entryfn_epilogue (s.op, false); |
b20febf3 | 361 | s.op->newline(-1) << "}"; |
65aeaea0 | 362 | |
4c2732a1 | 363 | s.op->newline() << "static void enter_end_probe (void (*fn)(struct context*), const char* pp) {"; |
b20febf3 | 364 | s.op->indent(1); |
e0a17418 | 365 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_STOPPING", "pp", false); |
65aeaea0 | 366 | s.op->newline() << "(*fn) (c);"; |
e0a17418 | 367 | common_probe_entryfn_epilogue (s.op, false); |
65aeaea0 FCE |
368 | s.op->newline(-1) << "}"; |
369 | ||
4c2732a1 | 370 | s.op->newline() << "static void enter_error_probe (void (*fn)(struct context*), const char* pp) {"; |
65aeaea0 | 371 | s.op->indent(1); |
e0a17418 | 372 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_ERROR", "pp", false); |
b20febf3 | 373 | s.op->newline() << "(*fn) (c);"; |
e0a17418 | 374 | common_probe_entryfn_epilogue (s.op, false); |
b20febf3 | 375 | s.op->newline(-1) << "}"; |
65aeaea0 | 376 | |
4c2732a1 | 377 | s.op->newline() << "static struct stap_be_probe {"; |
65aeaea0 FCE |
378 | s.op->newline(1) << "void (*ph)(struct context*);"; |
379 | s.op->newline() << "const char* pp;"; | |
380 | s.op->newline() << "int type;"; | |
381 | s.op->newline(-1) << "} stap_be_probes[] = {"; | |
382 | s.op->indent(1); | |
383 | ||
384 | // NB: We emit the table in sorted order here, so we don't have to | |
385 | // store the priority numbers as integers and sort at run time. | |
386 | ||
387 | sort(probes.begin(), probes.end(), be_derived_probe::comp); | |
388 | ||
389 | for (unsigned i=0; i < probes.size(); i++) | |
390 | { | |
4baf0e53 RM |
391 | s.op->newline () << "{"; |
392 | s.op->line() << " .pp=" | |
65aeaea0 FCE |
393 | << lex_cast_qstring (*probes[i]->sole_location()) << ","; |
394 | s.op->line() << " .ph=&" << probes[i]->name << ","; | |
395 | s.op->line() << " .type=" << probes[i]->type; | |
396 | s.op->line() << " },"; | |
397 | } | |
398 | s.op->newline(-1) << "};"; | |
56e12059 FCE |
399 | } |
400 | ||
dc38c0ae | 401 | void |
b20febf3 | 402 | be_derived_probe_group::emit_module_init (systemtap_session& s) |
dc38c0ae | 403 | { |
b8da0ad1 FCE |
404 | if (probes.empty()) return; |
405 | ||
65aeaea0 FCE |
406 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
407 | s.op->newline(1) << "struct stap_be_probe* stp = & stap_be_probes [i];"; | |
408 | s.op->newline() << "if (stp->type != " << BEGIN << ") continue;"; | |
409 | s.op->newline() << "enter_begin_probe (stp->ph, stp->pp);"; | |
410 | s.op->newline() << "/* rc = 0; */"; | |
411 | // NB: begin probes that cause errors do not constitute registration | |
412 | // failures. An error message will probably get printed and if | |
413 | // MAXERRORS was left at 1, we'll get an stp_exit. The | |
414 | // error-handling probes will be run during the ordinary | |
415 | // unregistration phase. | |
416 | s.op->newline(-1) << "}"; | |
dc38c0ae DS |
417 | } |
418 | ||
46b84a80 | 419 | void |
b20febf3 | 420 | be_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 421 | { |
b8da0ad1 FCE |
422 | if (probes.empty()) return; |
423 | ||
65aeaea0 FCE |
424 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
425 | s.op->newline(1) << "struct stap_be_probe* stp = & stap_be_probes [i];"; | |
426 | s.op->newline() << "if (stp->type != " << END << ") continue;"; | |
427 | s.op->newline() << "enter_end_probe (stp->ph, stp->pp);"; | |
428 | s.op->newline(-1) << "}"; | |
429 | ||
430 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
431 | s.op->newline(1) << "struct stap_be_probe* stp = & stap_be_probes [i];"; | |
432 | s.op->newline() << "if (stp->type != " << ERROR << ") continue;"; | |
433 | s.op->newline() << "enter_error_probe (stp->ph, stp->pp);"; | |
434 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
435 | } |
436 | ||
dc38c0ae | 437 | |
b20febf3 | 438 | |
6e3347a9 FCE |
439 | // ------------------------------------------------------------------------ |
440 | // never probes are never run | |
441 | // ------------------------------------------------------------------------ | |
442 | ||
12b21830 DS |
443 | static string TOK_NEVER("never"); |
444 | ||
6e3347a9 FCE |
445 | struct never_derived_probe: public derived_probe |
446 | { | |
447 | never_derived_probe (probe* p): derived_probe (p) {} | |
448 | never_derived_probe (probe* p, probe_point* l): derived_probe (p, l) {} | |
78f6bba6 | 449 | void join_group (systemtap_session&) { /* thus no probe_group */ } |
dc38c0ae DS |
450 | }; |
451 | ||
452 | ||
6e3347a9 FCE |
453 | struct never_builder: public derived_probe_builder |
454 | { | |
455 | never_builder() {} | |
78f6bba6 | 456 | virtual void build(systemtap_session &, |
6e3347a9 FCE |
457 | probe * base, |
458 | probe_point * location, | |
86bf665e | 459 | literal_map_t const &, |
6e3347a9 FCE |
460 | vector<derived_probe *> & finished_results) |
461 | { | |
462 | finished_results.push_back(new never_derived_probe(base, location)); | |
463 | } | |
464 | }; | |
465 | ||
466 | ||
b20febf3 | 467 | |
56e12059 | 468 | // ------------------------------------------------------------------------ |
b20febf3 | 469 | // Dwarf derived probes. "We apologize for the inconvience." |
b55bc428 | 470 | // ------------------------------------------------------------------------ |
bd2b1e68 | 471 | |
c239d28c GH |
472 | static string TOK_KERNEL("kernel"); |
473 | static string TOK_MODULE("module"); | |
c239d28c | 474 | static string TOK_FUNCTION("function"); |
54efe513 | 475 | static string TOK_INLINE("inline"); |
b8da0ad1 | 476 | static string TOK_CALL("call"); |
c239d28c | 477 | static string TOK_RETURN("return"); |
c9bad430 | 478 | static string TOK_MAXACTIVE("maxactive"); |
c239d28c | 479 | static string TOK_STATEMENT("statement"); |
37ebca01 | 480 | static string TOK_ABSOLUTE("absolute"); |
888af770 | 481 | static string TOK_PROCESS("process"); |
f28a8c28 | 482 | static string TOK_MARK("mark"); |
0a6f5a3f | 483 | static string TOK_TRACE("trace"); |
0f336e95 | 484 | static string TOK_LABEL("label"); |
59ff2773 | 485 | |
5f0a03a6 JK |
486 | // Can we handle this query with just symbol-table info? |
487 | enum dbinfo_reqt | |
488 | { | |
489 | dbr_unknown, | |
490 | dbr_none, // kernel.statement(NUM).absolute | |
491 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
492 | dbr_need_dwarf | |
493 | }; | |
494 | ||
495 | enum info_status | |
496 | { | |
497 | info_unknown, | |
498 | info_present, | |
499 | info_absent | |
500 | }; | |
b8da0ad1 | 501 | |
20e4a32c | 502 | struct |
7e1279ea FCE |
503 | func_info |
504 | { | |
20e4a32c | 505 | func_info() |
ab91b232 | 506 | : decl_file(NULL), decl_line(-1), addr(0), prologue_end(0), weak(false) |
b6581717 GH |
507 | { |
508 | memset(&die, 0, sizeof(die)); | |
509 | } | |
7e1279ea | 510 | string name; |
4cd232e4 GH |
511 | char const * decl_file; |
512 | int decl_line; | |
7e1279ea | 513 | Dwarf_Die die; |
5f0a03a6 | 514 | Dwarf_Addr addr; |
3e961ba6 | 515 | Dwarf_Addr entrypc; |
7e1279ea | 516 | Dwarf_Addr prologue_end; |
ab91b232 | 517 | bool weak; |
2e67a43b TM |
518 | // Comparison functor for list of functions sorted by address. The |
519 | // two versions that take a Dwarf_Addr let us use the STL algorithms | |
520 | // upper_bound, equal_range et al., but we don't know whether the | |
521 | // searched-for value will be passed as the first or the second | |
522 | // argument. | |
523 | struct Compare | |
524 | { | |
525 | bool operator() (const func_info* f1, const func_info* f2) const | |
526 | { | |
527 | return f1->addr < f2->addr; | |
528 | } | |
529 | // For doing lookups by address. | |
530 | bool operator() (Dwarf_Addr addr, const func_info* f2) const | |
531 | { | |
532 | return addr < f2->addr; | |
533 | } | |
534 | bool operator() (const func_info* f1, Dwarf_Addr addr) const | |
535 | { | |
536 | return f1->addr < addr; | |
537 | } | |
538 | }; | |
7e1279ea FCE |
539 | }; |
540 | ||
541 | struct | |
542 | inline_instance_info | |
543 | { | |
20e4a32c | 544 | inline_instance_info() |
b6581717 GH |
545 | : decl_file(NULL), decl_line(-1) |
546 | { | |
547 | memset(&die, 0, sizeof(die)); | |
548 | } | |
7e1279ea | 549 | string name; |
4cd232e4 GH |
550 | char const * decl_file; |
551 | int decl_line; | |
3e961ba6 | 552 | Dwarf_Addr entrypc; |
7e1279ea FCE |
553 | Dwarf_Die die; |
554 | }; | |
555 | ||
c8959a29 | 556 | |
c4ce66a1 JS |
557 | struct base_query; // forward decls |
558 | struct dwarf_query; | |
5f0a03a6 JK |
559 | struct dwflpp; |
560 | struct symbol_table; | |
561 | ||
405b71b8 | 562 | |
5f0a03a6 JK |
563 | struct |
564 | module_info | |
565 | { | |
566 | Dwfl_Module* mod; | |
567 | const char* name; | |
568 | string elf_path; | |
569 | Dwarf_Addr addr; | |
570 | Dwarf_Addr bias; | |
571 | symbol_table *sym_table; | |
572 | info_status dwarf_status; // module has dwarf info? | |
573 | info_status symtab_status; // symbol table cached? | |
574 | ||
575 | void get_symtab(dwarf_query *q); | |
576 | ||
577 | module_info(const char *name) : | |
578 | mod(NULL), | |
579 | name(name), | |
580 | addr(0), | |
581 | bias(0), | |
582 | sym_table(NULL), | |
583 | dwarf_status(info_unknown), | |
584 | symtab_status(info_unknown) | |
585 | {} | |
586 | ||
587 | ~module_info(); | |
588 | }; | |
589 | ||
590 | struct | |
591 | module_cache | |
592 | { | |
593 | map<string, module_info*> cache; | |
594 | bool paths_collected; | |
595 | bool dwarf_collected; | |
596 | ||
597 | module_cache() : paths_collected(false), dwarf_collected(false) {} | |
598 | }; | |
599 | typedef struct module_cache module_cache_t; | |
600 | ||
3bf6ac45 TM |
601 | #ifdef HAVE_TR1_UNORDERED_MAP |
602 | typedef tr1::unordered_map<string,Dwarf_Die> cu_function_cache_t; | |
603 | typedef tr1::unordered_map<string,cu_function_cache_t*> mod_cu_function_cache_t; // module:cu -> function -> die | |
604 | #else | |
2171f774 | 605 | struct stringhash { |
30369ac1 FCE |
606 | // __gnu_cxx:: is needed because our own hash.h has an ambiguous hash<> decl too. |
607 | size_t operator() (const string& s) const { __gnu_cxx::hash<const char*> h; return h(s.c_str()); } | |
2171f774 FCE |
608 | }; |
609 | ||
610 | typedef hash_map<string,Dwarf_Die,stringhash> cu_function_cache_t; | |
611 | typedef hash_map<string,cu_function_cache_t*,stringhash> mod_cu_function_cache_t; // module:cu -> function -> die | |
3bf6ac45 | 612 | #endif |
5f0a03a6 JK |
613 | |
614 | struct | |
615 | symbol_table | |
616 | { | |
617 | module_info *mod_info; // associated module | |
618 | map<string, func_info*> map_by_name; | |
619 | vector<func_info*> list_by_addr; | |
2e67a43b TM |
620 | typedef vector<func_info*>::iterator iterator_t; |
621 | typedef pair<iterator_t, iterator_t> range_t; | |
46f7b6be JK |
622 | #ifdef __powerpc__ |
623 | GElf_Word opd_section; | |
624 | #endif | |
2e67a43b TM |
625 | // add_symbol doesn't leave symbol table in order; call |
626 | // symbol_table::sort() when done adding symbols. | |
ab91b232 JK |
627 | void add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
628 | Dwarf_Addr *high_addr); | |
2e67a43b | 629 | void sort(); |
5f0a03a6 JK |
630 | enum info_status read_symbols(FILE *f, const string& path); |
631 | enum info_status read_from_elf_file(const string& path); | |
632 | enum info_status read_from_text_file(const string& path); | |
633 | enum info_status get_from_elf(); | |
46f7b6be JK |
634 | void prepare_section_rejection(Dwfl_Module *mod); |
635 | bool reject_section(GElf_Word section); | |
5f0a03a6 | 636 | void mark_dwarf_redundancies(dwflpp *dw); |
ab91b232 | 637 | void purge_syscall_stubs(); |
5f0a03a6 JK |
638 | func_info *lookup_symbol(const string& name); |
639 | Dwarf_Addr lookup_symbol_address(const string& name); | |
640 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
5f0a03a6 JK |
641 | |
642 | symbol_table(module_info *mi) : mod_info(mi) {} | |
643 | ~symbol_table(); | |
644 | }; | |
645 | ||
646 | static bool null_die(Dwarf_Die *die) | |
647 | { | |
648 | static Dwarf_Die null = { 0 }; | |
649 | return (!die || !memcmp(die, &null, sizeof(null))); | |
650 | } | |
651 | ||
7e1279ea FCE |
652 | static int |
653 | query_cu (Dwarf_Die * cudie, void * arg); | |
59ff2773 FCE |
654 | |
655 | ||
bd2b1e68 GH |
656 | // Helper for dealing with selected portions of libdwfl in a more readable |
657 | // fashion, and with specific cleanup / checking / logging options. | |
658 | ||
91eefb1c GH |
659 | static const char * |
660 | dwarf_diename_integrate (Dwarf_Die *die) | |
661 | { | |
662 | Dwarf_Attribute attr_mem; | |
663 | return dwarf_formstring (dwarf_attr_integrate (die, DW_AT_name, &attr_mem)); | |
664 | } | |
665 | ||
879eb9e9 SC |
666 | enum line_t { ABSOLUTE, RELATIVE, RANGE, WILDCARD }; |
667 | ||
3e961ba6 JB |
668 | typedef vector<inline_instance_info> inline_instance_map_t; |
669 | typedef vector<func_info> func_info_map_t; | |
670 | ||
b20febf3 | 671 | struct dwflpp |
bd2b1e68 | 672 | { |
5227f1ea | 673 | systemtap_session & sess; |
bd2b1e68 GH |
674 | Dwfl * dwfl; |
675 | ||
676 | // These are "current" values we focus on. | |
677 | Dwfl_Module * module; | |
678 | Dwarf * module_dwarf; | |
679 | Dwarf_Addr module_bias; | |
5f0a03a6 | 680 | module_info * mod_info; |
50e0d793 GH |
681 | |
682 | // These describe the current module's PC address range | |
683 | Dwarf_Addr module_start; | |
684 | Dwarf_Addr module_end; | |
685 | ||
bd2b1e68 | 686 | Dwarf_Die * cu; |
20e4a32c | 687 | Dwarf_Die * function; |
bd2b1e68 GH |
688 | |
689 | string module_name; | |
690 | string cu_name; | |
691 | string function_name; | |
692 | ||
7a053d3b | 693 | string const default_name(char const * in, |
78f6bba6 | 694 | char const *) |
bd2b1e68 | 695 | { |
7a053d3b | 696 | if (in) |
bd2b1e68 | 697 | return in; |
a229fcd7 | 698 | return string(""); |
bd2b1e68 GH |
699 | } |
700 | ||
50e0d793 | 701 | |
5f0a03a6 | 702 | void get_module_dwarf(bool required = false, bool report = true) |
5227f1ea | 703 | { |
91af0778 FCE |
704 | module_dwarf = dwfl_module_getdwarf(module, &module_bias); |
705 | mod_info->dwarf_status = (module_dwarf ? info_present : info_absent); | |
5f0a03a6 JK |
706 | if (!module_dwarf && report) |
707 | { | |
708 | string msg = "cannot find "; | |
709 | if (module_name == "") | |
710 | msg += "kernel"; | |
711 | else | |
712 | msg += string("module ") + module_name; | |
713 | msg += " debuginfo"; | |
714 | ||
715 | int i = dwfl_errno(); | |
716 | if (i) | |
717 | msg += string(": ") + dwfl_errmsg (i); | |
718 | ||
719 | if (required) | |
720 | throw semantic_error (msg); | |
721 | else | |
722 | cerr << "WARNING: " << msg << "\n"; | |
0ce64fb8 | 723 | } |
5227f1ea GH |
724 | } |
725 | ||
5f0a03a6 | 726 | void focus_on_module(Dwfl_Module * m, module_info * mi) |
bd2b1e68 | 727 | { |
bd2b1e68 | 728 | module = m; |
5f0a03a6 JK |
729 | mod_info = mi; |
730 | if (m) | |
731 | { | |
732 | module_name = default_name(dwfl_module_info(module, NULL, | |
50e0d793 | 733 | &module_start, &module_end, |
bd2b1e68 GH |
734 | NULL, NULL, |
735 | NULL, NULL), | |
736 | "module"); | |
5f0a03a6 JK |
737 | } |
738 | else | |
739 | { | |
740 | assert(mi && mi->name && mi->name == TOK_KERNEL); | |
741 | module_name = mi->name; | |
742 | module_start = 0; | |
743 | module_end = 0; | |
744 | module_bias = mi->bias; | |
745 | } | |
50e0d793 GH |
746 | |
747 | // Reset existing pointers and names | |
748 | ||
749 | module_dwarf = NULL; | |
750 | ||
a229fcd7 | 751 | cu_name.clear(); |
50e0d793 GH |
752 | cu = NULL; |
753 | ||
a229fcd7 | 754 | function_name.clear(); |
50e0d793 | 755 | function = NULL; |
bd2b1e68 GH |
756 | } |
757 | ||
50e0d793 | 758 | |
bd2b1e68 GH |
759 | void focus_on_cu(Dwarf_Die * c) |
760 | { | |
761 | assert(c); | |
50e0d793 GH |
762 | assert(module); |
763 | ||
bd2b1e68 | 764 | cu = c; |
50e0d793 GH |
765 | cu_name = default_name(dwarf_diename(c), "CU"); |
766 | ||
767 | // Reset existing pointers and names | |
a229fcd7 | 768 | function_name.clear(); |
50e0d793 | 769 | function = NULL; |
bd2b1e68 GH |
770 | } |
771 | ||
50e0d793 | 772 | |
20e4a32c | 773 | void focus_on_function(Dwarf_Die * f) |
bd2b1e68 GH |
774 | { |
775 | assert(f); | |
50e0d793 GH |
776 | assert(module); |
777 | assert(cu); | |
778 | ||
bd2b1e68 | 779 | function = f; |
20e4a32c | 780 | function_name = default_name(dwarf_diename(function), |
bd2b1e68 | 781 | "function"); |
bd2b1e68 GH |
782 | } |
783 | ||
50e0d793 | 784 | |
bd2b1e68 GH |
785 | void focus_on_module_containing_global_address(Dwarf_Addr a) |
786 | { | |
787 | assert(dwfl); | |
50e0d793 | 788 | cu = NULL; |
0ce64fb8 FCE |
789 | Dwfl_Module* mod = dwfl_addrmodule(dwfl, a); |
790 | if (mod) // address could be wildly out of range | |
5f0a03a6 | 791 | focus_on_module(mod, NULL); |
bd2b1e68 GH |
792 | } |
793 | ||
50e0d793 | 794 | |
7e1279ea | 795 | void query_cu_containing_global_address(Dwarf_Addr a, void *arg) |
bd2b1e68 | 796 | { |
bd2b1e68 | 797 | Dwarf_Addr bias; |
50e0d793 | 798 | assert(dwfl); |
5227f1ea | 799 | get_module_dwarf(); |
ab55a5ae FCE |
800 | Dwarf_Die* cudie = dwfl_module_addrdie(module, a, &bias); |
801 | if (cudie) // address could be wildly out of range | |
802 | query_cu (cudie, arg); | |
bd2b1e68 GH |
803 | assert(bias == module_bias); |
804 | } | |
805 | ||
50e0d793 | 806 | |
7e1279ea | 807 | void query_cu_containing_module_address(Dwarf_Addr a, void *arg) |
bd2b1e68 | 808 | { |
7e1279ea | 809 | query_cu_containing_global_address(module_address_to_global(a), arg); |
bd2b1e68 GH |
810 | } |
811 | ||
50e0d793 | 812 | |
bd2b1e68 GH |
813 | Dwarf_Addr module_address_to_global(Dwarf_Addr a) |
814 | { | |
50e0d793 | 815 | assert(dwfl); |
bd2b1e68 | 816 | assert(module); |
5227f1ea | 817 | get_module_dwarf(); |
76e954ca | 818 | if (module_name == TOK_KERNEL || dwfl_module_relocations (module) <= 0) |
c239d28c | 819 | return a; |
50e0d793 | 820 | return a + module_start; |
bd2b1e68 GH |
821 | } |
822 | ||
50e0d793 | 823 | |
bd2b1e68 GH |
824 | Dwarf_Addr global_address_to_module(Dwarf_Addr a) |
825 | { | |
826 | assert(module); | |
5227f1ea | 827 | get_module_dwarf(); |
bd2b1e68 GH |
828 | return a - module_bias; |
829 | } | |
830 | ||
831 | ||
832 | bool module_name_matches(string pattern) | |
833 | { | |
bd2b1e68 | 834 | bool t = (fnmatch(pattern.c_str(), module_name.c_str(), 0) == 0); |
b8da0ad1 | 835 | if (t && sess.verbose>3) |
bd2b1e68 | 836 | clog << "pattern '" << pattern << "' " |
24cb178f | 837 | << "matches " |
db22e55f | 838 | << "module '" << module_name << "'" << "\n"; |
bd2b1e68 GH |
839 | return t; |
840 | } | |
5f0a03a6 JK |
841 | bool name_has_wildcard(string pattern) |
842 | { | |
843 | return (pattern.find('*') != string::npos || | |
844 | pattern.find('?') != string::npos || | |
845 | pattern.find('[') != string::npos); | |
846 | } | |
b8da0ad1 FCE |
847 | bool module_name_final_match(string pattern) |
848 | { | |
849 | // Assume module_name_matches(). Can there be any more matches? | |
850 | // Not unless the pattern is a wildcard, since module names are | |
851 | // presumed unique. | |
5f0a03a6 | 852 | return !name_has_wildcard(pattern); |
b8da0ad1 | 853 | } |
bd2b1e68 | 854 | |
50e0d793 | 855 | |
5f0a03a6 | 856 | bool function_name_matches_pattern(string name, string pattern) |
bd2b1e68 | 857 | { |
5f0a03a6 | 858 | bool t = (fnmatch(pattern.c_str(), name.c_str(), 0) == 0); |
b8da0ad1 | 859 | if (t && sess.verbose>3) |
bd2b1e68 | 860 | clog << "pattern '" << pattern << "' " |
24cb178f | 861 | << "matches " |
5f0a03a6 | 862 | << "function '" << name << "'" << "\n"; |
bd2b1e68 GH |
863 | return t; |
864 | } | |
5f0a03a6 JK |
865 | bool function_name_matches(string pattern) |
866 | { | |
867 | assert(function); | |
868 | return function_name_matches_pattern(function_name, pattern); | |
869 | } | |
275f40a6 FCE |
870 | bool function_name_final_match(string pattern) |
871 | { | |
872 | return module_name_final_match (pattern); | |
873 | } | |
bd2b1e68 | 874 | |
50e0d793 | 875 | |
bd2b1e68 GH |
876 | bool cu_name_matches(string pattern) |
877 | { | |
878 | assert(cu); | |
3213d089 FCE |
879 | |
880 | // PR 5049: implicit * in front of given path pattern. | |
881 | // NB: fnmatch() is used without FNM_PATHNAME. | |
79640c29 | 882 | string prefixed_pattern = string("*/") + pattern; |
3213d089 | 883 | |
79640c29 FCE |
884 | bool t = (fnmatch(pattern.c_str(), cu_name.c_str(), 0) == 0 || |
885 | fnmatch(prefixed_pattern.c_str(), cu_name.c_str(), 0) == 0); | |
b8da0ad1 | 886 | if (t && sess.verbose>3) |
3213d089 | 887 | clog << "pattern '" << prefixed_pattern << "' " |
24cb178f | 888 | << "matches " |
db22e55f | 889 | << "CU '" << cu_name << "'" << "\n"; |
bd2b1e68 GH |
890 | return t; |
891 | } | |
892 | ||
5f0a03a6 | 893 | dwflpp(systemtap_session & session) |
bd2b1e68 | 894 | : |
5f0a03a6 | 895 | sess(session), |
bd2b1e68 GH |
896 | dwfl(NULL), |
897 | module(NULL), | |
898 | module_dwarf(NULL), | |
899 | module_bias(0), | |
5f0a03a6 | 900 | mod_info(NULL), |
50e0d793 GH |
901 | module_start(0), |
902 | module_end(0), | |
bd2b1e68 GH |
903 | cu(NULL), |
904 | function(NULL) | |
5f0a03a6 | 905 | { |
5f0a03a6 | 906 | } |
7a053d3b | 907 | |
50e0d793 | 908 | |
b5e66ada FCE |
909 | // XXX: See also translate.cxx:emit_symbol_data |
910 | ||
7a24d422 | 911 | void setup_kernel(bool debuginfo_needed = true) |
bd2b1e68 | 912 | { |
405b71b8 FCE |
913 | if (! sess.module_cache) |
914 | sess.module_cache = new module_cache (); | |
915 | ||
0dc34322 | 916 | static const char *debuginfo_path_arr = "+:.debug:/usr/lib/debug:build"; |
b5e66ada FCE |
917 | static const char *debuginfo_env_arr = getenv("SYSTEMTAP_DEBUGINFO_PATH"); |
918 | static const char *debuginfo_path = (debuginfo_env_arr ?: debuginfo_path_arr ); | |
b5d77020 | 919 | |
bd2b1e68 GH |
920 | static const Dwfl_Callbacks kernel_callbacks = |
921 | { | |
922 | dwfl_linux_kernel_find_elf, | |
923 | dwfl_standard_find_debuginfo, | |
b20febf3 | 924 | dwfl_offline_section_address, |
b5e66ada | 925 | (char **) & debuginfo_path |
bd2b1e68 GH |
926 | }; |
927 | ||
7a24d422 FCE |
928 | dwfl = dwfl_begin (&kernel_callbacks); |
929 | if (!dwfl) | |
930 | throw semantic_error ("cannot open dwfl"); | |
931 | dwfl_report_begin (dwfl); | |
06aca46a | 932 | |
b5e66ada FCE |
933 | // We have a problem with -r REVISION vs -r BUILDDIR here. If |
934 | // we're running against a fedora/rhel style kernel-debuginfo | |
935 | // tree, s.kernel_build_tree is not the place where the unstripped | |
936 | // vmlinux will be installed. Rather, it's over yonder at | |
937 | // /usr/lib/debug/lib/modules/$REVISION/. It seems that there is | |
938 | // no way to set the dwfl_callback.debuginfo_path and always | |
939 | // passs the plain kernel_release here. So instead we have to | |
940 | // hard-code this magic here. | |
941 | string elfutils_kernel_path; | |
942 | if (sess.kernel_build_tree == string("/lib/modules/" + sess.kernel_release + "/build")) | |
943 | elfutils_kernel_path = sess.kernel_release; | |
944 | else | |
945 | elfutils_kernel_path = sess.kernel_build_tree; | |
946 | ||
7a24d422 | 947 | int rc = dwfl_linux_kernel_report_offline (dwfl, |
b5e66ada | 948 | elfutils_kernel_path.c_str(), |
91af0778 | 949 | NULL); |
06aca46a | 950 | |
7a24d422 | 951 | if (debuginfo_needed) |
b5e66ada FCE |
952 | dwfl_assert (string("missing ") + sess.architecture + |
953 | string(" kernel/module debuginfo under '") + | |
954 | sess.kernel_build_tree + string("'"), | |
7a24d422 | 955 | rc); |
06aca46a | 956 | |
7a24d422 FCE |
957 | // XXX: it would be nice if we could do a single |
958 | // ..._report_offline call for an entire systemtap script, so | |
959 | // that a selection predicate would filter out modules outside | |
960 | // the union of all the requested wildcards. But we build | |
961 | // derived_probes one-by-one and we don't have lookahead. | |
962 | // PR 3498. | |
06aca46a | 963 | |
7a24d422 FCE |
964 | // XXX: a special case: if we have only kernel.* probe points, |
965 | // we shouldn't waste time looking for module debug-info (and | |
966 | // vice versa). | |
06aca46a | 967 | |
7a24d422 FCE |
968 | // NB: the result of an _offline call is the assignment of |
969 | // virtualized addresses to relocatable objects such as | |
970 | // modules. These have to be converted to real addresses at | |
971 | // run time. See the dwarf_derived_probe ctor and its caller. | |
972 | ||
973 | dwfl_assert ("dwfl_report_end", dwfl_report_end(dwfl, NULL, NULL)); | |
974 | } | |
975 | ||
976 | void setup_user(string module_name, bool debuginfo_needed = true) | |
977 | { | |
dd22832a JS |
978 | if (! sess.module_cache) |
979 | sess.module_cache = new module_cache (); | |
980 | ||
0dc34322 | 981 | static const char *debuginfo_path_arr = "+:.debug:/usr/lib/debug:build"; |
b5e66ada FCE |
982 | static const char *debuginfo_env_arr = getenv("SYSTEMTAP_DEBUGINFO_PATH"); |
983 | // NB: kernel_build_tree doesn't enter into this, as it's for | |
984 | // kernel-side modules only. | |
985 | static const char *debuginfo_path = (debuginfo_env_arr ?: debuginfo_path_arr); | |
7a24d422 FCE |
986 | |
987 | static const Dwfl_Callbacks user_callbacks = | |
bd2b1e68 | 988 | { |
7a24d422 FCE |
989 | NULL, /* dwfl_linux_kernel_find_elf, */ |
990 | dwfl_standard_find_debuginfo, | |
991 | dwfl_offline_section_address, | |
b5e66ada | 992 | (char **) & debuginfo_path |
7a24d422 | 993 | }; |
b20febf3 | 994 | |
7a24d422 FCE |
995 | dwfl = dwfl_begin (&user_callbacks); |
996 | if (!dwfl) | |
997 | throw semantic_error ("cannot open dwfl"); | |
998 | dwfl_report_begin (dwfl); | |
999 | ||
7a24d422 | 1000 | // XXX: should support buildid-based naming |
405b71b8 | 1001 | |
7a24d422 FCE |
1002 | Dwfl_Module *mod = dwfl_report_offline (dwfl, |
1003 | module_name.c_str(), | |
1004 | module_name.c_str(), | |
1005 | -1); | |
1006 | // XXX: save mod! | |
5f0a03a6 | 1007 | |
7a24d422 FCE |
1008 | if (debuginfo_needed) |
1009 | dwfl_assert (string("missing process ") + | |
1010 | module_name + | |
1011 | string(" ") + | |
1012 | sess.architecture + | |
1013 | string(" debuginfo"), | |
1014 | mod); | |
b20febf3 | 1015 | |
f28a8c28 SC |
1016 | if (!module) |
1017 | module = mod; | |
1018 | ||
7a24d422 FCE |
1019 | // NB: the result of an _offline call is the assignment of |
1020 | // virtualized addresses to relocatable objects such as | |
1021 | // modules. These have to be converted to real addresses at | |
1022 | // run time. See the dwarf_derived_probe ctor and its caller. | |
bd2b1e68 | 1023 | |
7e1279ea | 1024 | dwfl_assert ("dwfl_report_end", dwfl_report_end(dwfl, NULL, NULL)); |
bd2b1e68 GH |
1025 | } |
1026 | ||
b8da0ad1 FCE |
1027 | |
1028 | ||
1029 | // ----------------------------------------------------------------- | |
1030 | ||
91af0778 | 1031 | void iterate_over_modules(int (* callback)(Dwfl_Module *, void **, |
5f0a03a6 JK |
1032 | const char *, Dwarf_Addr, |
1033 | void *), | |
c4ce66a1 | 1034 | base_query *data) |
5f0a03a6 | 1035 | { |
91af0778 FCE |
1036 | ptrdiff_t off = 0; |
1037 | do | |
b8da0ad1 | 1038 | { |
49abf162 | 1039 | if (pending_interrupts) return; |
91af0778 | 1040 | off = dwfl_getmodules (dwfl, callback, data, off); |
bd2b1e68 | 1041 | } |
91af0778 | 1042 | while (off > 0); |
432f054f FCE |
1043 | // Don't complain if we exited dwfl_getmodules early. |
1044 | // This could be a $target variable error that will be | |
1045 | // reported soon anyway. | |
1046 | // dwfl_assert("dwfl_getmodules", off == 0); | |
6f4c1275 FCE |
1047 | |
1048 | // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the | |
1049 | // "kernel" module is not reported in the loop above. However, we | |
1050 | // may be able to make do with symbol table data. | |
bd2b1e68 GH |
1051 | } |
1052 | ||
91af0778 | 1053 | |
5f0a03a6 | 1054 | // Defined after dwarf_query |
c4ce66a1 | 1055 | void query_modules(base_query *q); |
7e1279ea | 1056 | |
b8da0ad1 FCE |
1057 | |
1058 | // ----------------------------------------------------------------- | |
1059 | ||
1060 | typedef map<Dwarf*, vector<Dwarf_Die>*> module_cu_cache_t; | |
1061 | module_cu_cache_t module_cu_cache; | |
1062 | ||
7a053d3b | 1063 | void iterate_over_cus (int (*callback)(Dwarf_Die * die, void * arg), |
bd2b1e68 GH |
1064 | void * data) |
1065 | { | |
0ce64fb8 | 1066 | get_module_dwarf(false); |
b8da0ad1 FCE |
1067 | Dwarf *dw = module_dwarf; |
1068 | if (!dw) return; | |
5227f1ea | 1069 | |
b8da0ad1 FCE |
1070 | vector<Dwarf_Die>* v = module_cu_cache[dw]; |
1071 | if (v == 0) | |
1072 | { | |
1073 | v = new vector<Dwarf_Die>; | |
1074 | module_cu_cache[dw] = v; | |
bd2b1e68 | 1075 | |
b8da0ad1 FCE |
1076 | Dwarf_Off off = 0; |
1077 | size_t cuhl; | |
1078 | Dwarf_Off noff; | |
1079 | while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) | |
1080 | { | |
49abf162 | 1081 | if (pending_interrupts) return; |
b8da0ad1 FCE |
1082 | Dwarf_Die die_mem; |
1083 | Dwarf_Die *die; | |
1084 | die = dwarf_offdie (dw, off + cuhl, &die_mem); | |
1085 | v->push_back (*die); /* copy */ | |
1086 | off = noff; | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | for (unsigned i = 0; i < v->size(); i++) | |
7a053d3b | 1091 | { |
49abf162 | 1092 | if (pending_interrupts) return; |
b8da0ad1 FCE |
1093 | Dwarf_Die die = v->at(i); |
1094 | int rc = (*callback)(& die, data); | |
1095 | if (rc != DWARF_CB_OK) break; | |
bd2b1e68 GH |
1096 | } |
1097 | } | |
1098 | ||
bd2b1e68 | 1099 | |
b8da0ad1 FCE |
1100 | // ----------------------------------------------------------------- |
1101 | ||
7e1279ea | 1102 | bool func_is_inline() |
bd2b1e68 | 1103 | { |
7e1279ea FCE |
1104 | assert (function); |
1105 | return dwarf_func_inline (function) != 0; | |
bd2b1e68 GH |
1106 | } |
1107 | ||
b8da0ad1 FCE |
1108 | |
1109 | typedef map<string, vector<Dwarf_Die>*> cu_inl_function_cache_t; | |
1110 | cu_inl_function_cache_t cu_inl_function_cache; | |
1111 | ||
1112 | static int cu_inl_function_caching_callback (Dwarf_Die* func, void *arg) | |
1113 | { | |
1114 | vector<Dwarf_Die>* v = static_cast<vector<Dwarf_Die>*>(arg); | |
1115 | v->push_back (* func); | |
1116 | return DWARF_CB_OK; | |
1117 | } | |
1118 | ||
7e1279ea FCE |
1119 | void iterate_over_inline_instances (int (* callback)(Dwarf_Die * die, void * arg), |
1120 | void * data) | |
bd2b1e68 | 1121 | { |
7e1279ea FCE |
1122 | assert (function); |
1123 | assert (func_is_inline ()); | |
b8da0ad1 FCE |
1124 | |
1125 | string key = module_name + ":" + cu_name + ":" + function_name; | |
1126 | vector<Dwarf_Die>* v = cu_inl_function_cache[key]; | |
1127 | if (v == 0) | |
1128 | { | |
1129 | v = new vector<Dwarf_Die>; | |
1130 | cu_inl_function_cache[key] = v; | |
1131 | dwarf_func_inline_instances (function, cu_inl_function_caching_callback, v); | |
1132 | } | |
1133 | ||
1134 | for (unsigned i=0; i<v->size(); i++) | |
1135 | { | |
f76427a2 | 1136 | if (pending_interrupts) return; |
b8da0ad1 FCE |
1137 | Dwarf_Die die = v->at(i); |
1138 | int rc = (*callback)(& die, data); | |
1139 | if (rc != DWARF_CB_OK) break; | |
1140 | } | |
4fa7b22b | 1141 | } |
bd2b1e68 | 1142 | |
50e0d793 | 1143 | |
b8da0ad1 FCE |
1144 | // ----------------------------------------------------------------- |
1145 | ||
7bb73781 | 1146 | /* The global alias cache is used to resolve any DIE found in a |
0b8f6579 JB |
1147 | * module that is stubbed out with DW_AT_declaration with a defining |
1148 | * DIE found in a different module. The current assumption is that | |
1149 | * this only applies to structures and unions, which have a global | |
1150 | * namespace (it deliberately only traverses program scope), so this | |
1151 | * cache is indexed by name. If other declaration lookups were | |
1152 | * added to it, it would have to be indexed by name and tag | |
1153 | */ | |
7bb73781 | 1154 | mod_cu_function_cache_t global_alias_cache; |
0b8f6579 JB |
1155 | static int global_alias_caching_callback(Dwarf_Die *die, void *arg) |
1156 | { | |
7bb73781 | 1157 | cu_function_cache_t *cache = static_cast<cu_function_cache_t*>(arg); |
0b8f6579 JB |
1158 | const char *name = dwarf_diename(die); |
1159 | ||
1160 | if (!name) | |
1161 | return DWARF_CB_OK; | |
1162 | ||
1163 | string structure_name = name; | |
1164 | ||
1165 | if (!dwarf_hasattr(die, DW_AT_declaration) && | |
7bb73781 FCE |
1166 | cache->find(structure_name) == cache->end()) |
1167 | (*cache)[structure_name] = *die; | |
0b8f6579 JB |
1168 | |
1169 | return DWARF_CB_OK; | |
1170 | } | |
1171 | ||
c4ce66a1 | 1172 | Dwarf_Die *declaration_resolve(const char *name) |
0b8f6579 | 1173 | { |
0b8f6579 JB |
1174 | if (!name) |
1175 | return NULL; | |
1176 | ||
7bb73781 FCE |
1177 | string key = module_name + ":" + cu_name; |
1178 | cu_function_cache_t *v = global_alias_cache[key]; | |
1179 | if (v == 0) // need to build the cache, just once per encountered module/cu | |
1180 | { | |
1181 | v = new cu_function_cache_t; | |
1182 | global_alias_cache[key] = v; | |
1183 | iterate_over_globals(global_alias_caching_callback, v); | |
1184 | if (sess.verbose > 4) | |
1185 | clog << "global alias cache " << key << " size " << v->size() << endl; | |
1186 | } | |
1187 | ||
1188 | // XXX: it may be desirable to search other modules' declarations | |
1189 | // too, in case a module/shared-library processes a | |
1190 | // forward-declared pointer type only, where the actual definition | |
1191 | // may only be in vmlinux or the application. | |
1192 | ||
1193 | // XXX: it is probably desirable to search other CU's declarations | |
1194 | // in the same module. | |
41c262f3 | 1195 | |
7bb73781 | 1196 | if (v->find(name) == v->end()) |
0b8f6579 JB |
1197 | return NULL; |
1198 | ||
7bb73781 | 1199 | return & ((*v)[name]); |
0b8f6579 JB |
1200 | } |
1201 | ||
6561773f | 1202 | mod_cu_function_cache_t cu_function_cache; |
b8da0ad1 FCE |
1203 | |
1204 | static int cu_function_caching_callback (Dwarf_Die* func, void *arg) | |
1205 | { | |
6561773f | 1206 | cu_function_cache_t* v = static_cast<cu_function_cache_t*>(arg); |
0e68eaaa DS |
1207 | const char *name = dwarf_diename(func); |
1208 | if (!name) | |
1209 | return DWARF_CB_OK; | |
1210 | ||
1211 | string function_name = name; | |
6561773f | 1212 | (*v)[function_name] = * func; |
b8da0ad1 FCE |
1213 | return DWARF_CB_OK; |
1214 | } | |
1215 | ||
2da9cedb JS |
1216 | int iterate_over_functions (int (* callback)(Dwarf_Die * func, base_query * q), |
1217 | base_query * q, const string& function, | |
1218 | bool has_statement_num=false); | |
0b8f6579 JB |
1219 | int iterate_over_globals (int (* callback)(Dwarf_Die *, void *), |
1220 | void * data); | |
1221 | ||
fedd4090 | 1222 | bool has_single_line_record (dwarf_query * q, char const * srcfile, int lineno); |
897820ca | 1223 | |
7e1279ea | 1224 | void iterate_over_srcfile_lines (char const * srcfile, |
879eb9e9 | 1225 | int lines[2], |
897820ca | 1226 | bool need_single_match, |
879eb9e9 | 1227 | enum line_t line_type, |
86bf665e TM |
1228 | void (* callback) (const dwarf_line_t& line, |
1229 | void * arg), | |
7e1279ea FCE |
1230 | void *data) |
1231 | { | |
6315bd76 GH |
1232 | Dwarf_Line **srcsp = NULL; |
1233 | size_t nsrcs = 0; | |
fedd4090 | 1234 | dwarf_query * q = static_cast<dwarf_query *>(data); |
879eb9e9 | 1235 | int lineno = lines[0]; |
558982c5 | 1236 | auto_free_ref<Dwarf_Line**> free_srcsp(srcsp); |
41c262f3 | 1237 | |
7e1279ea | 1238 | get_module_dwarf(); |
bb788f9f | 1239 | |
41c262f3 | 1240 | if (line_type == RELATIVE) |
0c8b7d37 SC |
1241 | { |
1242 | Dwarf_Addr addr; | |
1243 | Dwarf_Line *line; | |
1244 | int line_number; | |
41c262f3 | 1245 | |
0c8b7d37 SC |
1246 | dwarf_assert ("dwarf_entrypc", dwarf_entrypc (this->function, &addr)); |
1247 | line = dwarf_getsrc_die (this->cu, addr); | |
1248 | dwarf_assert ("dwarf_getsrc_die", line == NULL); | |
1249 | dwarf_assert ("dwarf_lineno", dwarf_lineno (line, &line_number)); | |
1250 | lineno += line_number; | |
1251 | } | |
879eb9e9 SC |
1252 | else if (line_type == WILDCARD) |
1253 | function_line (&lineno); | |
41c262f3 | 1254 | |
879eb9e9 SC |
1255 | for (int l = lineno; ; l = l + 1) |
1256 | { | |
2ffc7958 SC |
1257 | set<int> lines_probed; |
1258 | pair<set<int>::iterator,bool> line_probed; | |
879eb9e9 SC |
1259 | dwarf_assert ("dwarf_getsrc_file", |
1260 | dwarf_getsrc_file (module_dwarf, | |
1261 | srcfile, l, 0, | |
1262 | &srcsp, &nsrcs)); | |
879eb9e9 SC |
1263 | if (line_type == WILDCARD || line_type == RANGE) |
1264 | { | |
1265 | Dwarf_Addr line_addr; | |
1266 | dwarf_lineno (srcsp [0], &lineno); | |
2ffc7958 | 1267 | line_probed = lines_probed.insert(lineno); |
69087111 | 1268 | if (lineno != l || line_probed.second == false || nsrcs > 1) |
879eb9e9 SC |
1269 | continue; |
1270 | dwarf_lineaddr (srcsp [0], &line_addr); | |
1271 | if (dwarf_haspc (function, line_addr) != 1) | |
1272 | break; | |
1273 | } | |
fedd4090 | 1274 | |
879eb9e9 | 1275 | // NB: Formerly, we used to filter, because: |
20e4a32c | 1276 | |
879eb9e9 SC |
1277 | // dwarf_getsrc_file gets one *near hits* for line numbers, not |
1278 | // exact matches. For example, an existing file but a nonexistent | |
1279 | // line number will be rounded up to the next definition in that | |
1280 | // file. This may be similar to the GDB breakpoint algorithm, but | |
1281 | // we don't want to be so fuzzy in systemtap land. So we filter. | |
847bf07f | 1282 | |
879eb9e9 | 1283 | // But we now see the error of our ways, and skip this filtering. |
fedd4090 | 1284 | |
879eb9e9 SC |
1285 | // XXX: the code also fails to match e.g. inline function |
1286 | // definitions when the srcfile is a header file rather than the | |
1287 | // CU name. | |
847bf07f | 1288 | |
879eb9e9 | 1289 | size_t remaining_nsrcs = nsrcs; |
847bf07f | 1290 | |
879eb9e9 | 1291 | if (need_single_match && remaining_nsrcs > 1) |
897820ca | 1292 | { |
879eb9e9 SC |
1293 | // We wanted a single line record (a unique address for the |
1294 | // line) and we got a bunch of line records. We're going to | |
1295 | // skip this probe (throw an exception) but before we throw | |
1296 | // we're going to look around a bit to see if there's a low or | |
1297 | // high line number nearby which *doesn't* have this problem, | |
1298 | // so we can give the user some advice. | |
1299 | ||
1300 | int lo_try = -1; | |
1301 | int hi_try = -1; | |
1302 | for (size_t i = 1; i < 6; ++i) | |
1303 | { | |
1304 | if (lo_try == -1 && has_single_line_record(q, srcfile, lineno - i)) | |
1305 | lo_try = lineno - i; | |
897820ca | 1306 | |
879eb9e9 SC |
1307 | if (hi_try == -1 && has_single_line_record(q, srcfile, lineno + i)) |
1308 | hi_try = lineno + i; | |
1309 | } | |
897820ca | 1310 | |
879eb9e9 SC |
1311 | stringstream advice; |
1312 | advice << "multiple addresses for " << srcfile << ":" << lineno; | |
1313 | if (lo_try > 0 || hi_try > 0) | |
1314 | { | |
1315 | advice << " (try "; | |
1316 | if (lo_try > 0) | |
1317 | advice << srcfile << ":" << lo_try; | |
1318 | if (lo_try > 0 && hi_try > 0) | |
1319 | advice << " or "; | |
1320 | if (hi_try > 0) | |
1321 | advice << srcfile << ":" << hi_try; | |
1322 | advice << ")"; | |
1323 | } | |
1324 | throw semantic_error (advice.str()); | |
1325 | } | |
897820ca | 1326 | |
2e67a43b TM |
1327 | for (size_t i = 0; i < nsrcs; ++i) |
1328 | { | |
1329 | if (pending_interrupts) return; | |
1330 | if (srcsp [i]) // skip over mismatched lines | |
1331 | callback (dwarf_line_t(srcsp[i]), data); | |
1332 | } | |
69087111 SC |
1333 | |
1334 | if (line_type == ABSOLUTE || line_type == RELATIVE) | |
1335 | break; | |
1336 | else if (line_type == RANGE && l == lines[1]) | |
1337 | break; | |
20e4a32c | 1338 | } |
50e0d793 GH |
1339 | } |
1340 | ||
0f336e95 SC |
1341 | void |
1342 | iterate_over_cu_labels (string label_val, Dwarf_Die *cu, void *data, | |
1343 | void (* callback)(const string &, | |
1344 | const char *, | |
1345 | int, | |
1346 | Dwarf_Die *, | |
1347 | Dwarf_Addr, | |
1348 | dwarf_query *)) | |
1349 | { | |
1350 | dwarf_query * q __attribute__ ((unused)) = static_cast<dwarf_query *>(data) ; | |
1351 | ||
1352 | get_module_dwarf(); | |
1353 | ||
1354 | const char * sym = label_val.c_str(); | |
1355 | Dwarf_Die die; | |
3c1f71d5 MW |
1356 | int res = dwarf_child (cu, &die); |
1357 | if (res != 0) | |
1358 | return; // die without children, bail out. | |
1359 | ||
0f336e95 SC |
1360 | static string function_name; |
1361 | do | |
1362 | { | |
1363 | Dwarf_Attribute attr_mem; | |
1364 | Dwarf_Attribute *attr = dwarf_attr (&die, DW_AT_name, &attr_mem); | |
1365 | int tag = dwarf_tag(&die); | |
1366 | const char *name = dwarf_formstring (attr); | |
9e67aff9 | 1367 | if (tag == DW_TAG_subprogram && name != 0) |
0f336e95 SC |
1368 | { |
1369 | function_name = name; | |
1370 | } | |
9e67aff9 | 1371 | else if (tag == DW_TAG_label && name != 0 |
7b534f48 | 1372 | && ((strncmp(name, sym, strlen(sym)) == 0) |
0f336e95 SC |
1373 | || (name_has_wildcard (sym) |
1374 | && function_name_matches_pattern (name, sym)))) | |
1375 | { | |
1376 | const char *file = dwarf_decl_file (&die); | |
7b534f48 SC |
1377 | // Get the line number for this label |
1378 | Dwarf_Attribute attr; | |
1379 | dwarf_attr (&die,DW_AT_decl_line, &attr); | |
1380 | Dwarf_Sword dline; | |
1381 | dwarf_formsdata (&attr, &dline); | |
0f336e95 SC |
1382 | Dwarf_Addr stmt_addr; |
1383 | if (dwarf_lowpc (&die, &stmt_addr) != 0) | |
7b534f48 SC |
1384 | { |
1385 | // There is no lowpc so figure out the address | |
1386 | // Get the real die for this cu | |
1387 | Dwarf_Die cudie; | |
1388 | dwarf_diecu (cu, &cudie, NULL, NULL); | |
1389 | size_t nlines = 0; | |
1390 | // Get the line for this label | |
1391 | Dwarf_Line **aline; | |
1392 | dwarf_getsrc_file (module_dwarf, file, (int)dline, 0, &aline, &nlines); | |
1393 | // Get the address | |
1394 | for (size_t i = 0; i < nlines; i++) | |
1395 | { | |
1396 | dwarf_lineaddr (*aline, &stmt_addr); | |
1397 | if ((dwarf_haspc (&die, stmt_addr))) | |
1398 | break; | |
1399 | } | |
1400 | } | |
1401 | ||
0f336e95 SC |
1402 | Dwarf_Die *scopes; |
1403 | int nscopes = 0; | |
1404 | nscopes = dwarf_getscopes_die (&die, &scopes); | |
1405 | if (nscopes > 1) | |
1406 | callback(function_name.c_str(), file, | |
7b534f48 | 1407 | (int)dline, &scopes[1], stmt_addr, q); |
0f336e95 SC |
1408 | } |
1409 | if (dwarf_haschildren (&die) && tag != DW_TAG_structure_type | |
1410 | && tag != DW_TAG_union_type) | |
1411 | { | |
1412 | iterate_over_cu_labels (label_val, &die, q, callback); | |
1413 | } | |
1414 | } | |
1415 | while (dwarf_siblingof (&die, &die) == 0); | |
1416 | } | |
1417 | ||
50e0d793 | 1418 | |
7e1279ea FCE |
1419 | void collect_srcfiles_matching (string const & pattern, |
1420 | set<char const *> & filtered_srcfiles) | |
50e0d793 | 1421 | { |
7e1279ea FCE |
1422 | assert (module); |
1423 | assert (cu); | |
bb788f9f | 1424 | |
7e1279ea FCE |
1425 | size_t nfiles; |
1426 | Dwarf_Files *srcfiles; | |
bb788f9f | 1427 | |
3213d089 FCE |
1428 | // PR 5049: implicit * in front of given path pattern. |
1429 | // NB: fnmatch() is used without FNM_PATHNAME. | |
79640c29 | 1430 | string prefixed_pattern = string("*/") + pattern; |
3213d089 | 1431 | |
20e4a32c | 1432 | dwarf_assert ("dwarf_getsrcfiles", |
7e1279ea FCE |
1433 | dwarf_getsrcfiles (cu, &srcfiles, &nfiles)); |
1434 | { | |
1435 | for (size_t i = 0; i < nfiles; ++i) | |
50e0d793 | 1436 | { |
7e1279ea | 1437 | char const * fname = dwarf_filesrc (srcfiles, i, NULL, NULL); |
79640c29 FCE |
1438 | if (fnmatch (pattern.c_str(), fname, 0) == 0 || |
1439 | fnmatch (prefixed_pattern.c_str(), fname, 0) == 0) | |
50e0d793 | 1440 | { |
7e1279ea | 1441 | filtered_srcfiles.insert (fname); |
b0ee93c4 | 1442 | if (sess.verbose>2) |
db22e55f | 1443 | clog << "selected source file '" << fname << "'\n"; |
50e0d793 GH |
1444 | } |
1445 | } | |
7e1279ea | 1446 | } |
20e4a32c | 1447 | } |
50e0d793 | 1448 | |
3e961ba6 | 1449 | void resolve_prologue_endings (func_info_map_t & funcs) |
7d71e1d5 FCE |
1450 | { |
1451 | // This heuristic attempts to pick the first address that has a | |
34ca7d84 FCE |
1452 | // source line distinct from the function declaration's. In a |
1453 | // perfect world, this would be the first statement *past* the | |
1454 | // prologue. | |
1455 | ||
7d71e1d5 FCE |
1456 | assert(module); |
1457 | assert(cu); | |
1458 | ||
456aa31c FCE |
1459 | size_t nlines = 0; |
1460 | Dwarf_Lines *lines = NULL; | |
7d71e1d5 | 1461 | |
dc223023 FCE |
1462 | /* trouble cases: |
1463 | malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue | |
1464 | sys_get?id in kernel/timer.c no-prologue | |
1465 | sys_exit_group tail-recursive | |
1466 | {do_,}sys_open extra-long-prologue (gcc 3.4) | |
1467 | cpu_to_logical_apicid NULL-decl_file | |
1468 | */ | |
1469 | ||
1470 | // Fetch all srcline records, sorted by address. | |
20e4a32c RM |
1471 | dwarf_assert ("dwarf_getsrclines", |
1472 | dwarf_getsrclines(cu, &lines, &nlines)); | |
dc223023 | 1473 | // XXX: free lines[] later, but how? |
7d71e1d5 | 1474 | |
3e961ba6 | 1475 | for(func_info_map_t::iterator it = funcs.begin(); it != funcs.end(); it++) |
7d71e1d5 | 1476 | { |
dc223023 FCE |
1477 | #if 0 /* someday */ |
1478 | Dwarf_Addr* bkpts = 0; | |
3e961ba6 | 1479 | int n = dwarf_entry_breakpoints (& it->die, & bkpts); |
dc223023 FCE |
1480 | // ... |
1481 | free (bkpts); | |
1482 | #endif | |
20e4a32c | 1483 | |
3e961ba6 | 1484 | Dwarf_Addr entrypc = it->entrypc; |
dc223023 | 1485 | Dwarf_Addr highpc; // NB: highpc is exclusive: [entrypc,highpc) |
3e961ba6 | 1486 | dwfl_assert ("dwarf_highpc", dwarf_highpc (& it->die, |
dc223023 FCE |
1487 | & highpc)); |
1488 | ||
3e961ba6 | 1489 | if (it->decl_file == 0) it->decl_file = ""; |
e38d6504 | 1490 | |
dc223023 | 1491 | unsigned entrypc_srcline_idx = 0; |
86bf665e | 1492 | dwarf_line_t entrypc_srcline; |
dc223023 FCE |
1493 | // open-code binary search for exact match |
1494 | { | |
1495 | unsigned l = 0, h = nlines; | |
1496 | while (l < h) | |
1497 | { | |
1498 | entrypc_srcline_idx = (l + h) / 2; | |
86bf665e TM |
1499 | const dwarf_line_t lr(dwarf_onesrcline(lines, |
1500 | entrypc_srcline_idx)); | |
1501 | Dwarf_Addr addr = lr.addr(); | |
dc223023 FCE |
1502 | if (addr == entrypc) { entrypc_srcline = lr; break; } |
1503 | else if (l + 1 == h) { break; } // ran off bottom of tree | |
1504 | else if (addr < entrypc) { l = entrypc_srcline_idx; } | |
1505 | else { h = entrypc_srcline_idx; } | |
e38d6504 | 1506 | } |
dc223023 | 1507 | } |
86bf665e | 1508 | if (!entrypc_srcline) |
dfa11ddb FCE |
1509 | { |
1510 | if (sess.verbose > 2) | |
1511 | clog << "missing entrypc dwarf line record for function '" | |
1512 | << it->name << "'\n"; | |
1513 | // This is probably an inlined function. We'll end up using | |
1514 | // its lowpc as a probe address. | |
1515 | continue; | |
1516 | } | |
dc223023 FCE |
1517 | |
1518 | if (sess.verbose>2) | |
3e961ba6 | 1519 | clog << "prologue searching function '" << it->name << "'" |
5fe3e97f | 1520 | << " 0x" << hex << entrypc << "-0x" << highpc << dec |
3e961ba6 | 1521 | << "@" << it->decl_file << ":" << it->decl_line |
dc223023 FCE |
1522 | << "\n"; |
1523 | ||
1524 | // Now we go searching for the first line record that has a | |
1525 | // file/line different from the one in the declaration. | |
1526 | // Normally, this will be the next one. BUT: | |
1527 | // | |
1528 | // We may have to skip a few because some old compilers plop | |
1529 | // in dummy line records for longer prologues. If we go too | |
1530 | // far (addr >= highpc), we take the previous one. Or, it may | |
1531 | // be the first one, if the function had no prologue, and thus | |
1532 | // the entrypc maps to a statement in the body rather than the | |
1533 | // declaration. | |
1534 | ||
1535 | unsigned postprologue_srcline_idx = entrypc_srcline_idx; | |
1536 | bool ranoff_end = false; | |
35f5f091 | 1537 | while (postprologue_srcline_idx < nlines) |
7d71e1d5 | 1538 | { |
86bf665e TM |
1539 | dwarf_line_t lr(dwarf_onesrcline(lines, postprologue_srcline_idx)); |
1540 | Dwarf_Addr postprologue_addr = lr.addr(); | |
1541 | const char* postprologue_file = lr.linesrc(); | |
1542 | int postprologue_lineno = lr.lineno(); | |
456aa31c | 1543 | |
b0ee93c4 | 1544 | if (sess.verbose>2) |
dc223023 FCE |
1545 | clog << "checking line record 0x" << hex << postprologue_addr << dec |
1546 | << "@" << postprologue_file << ":" << postprologue_lineno << "\n"; | |
1547 | ||
1548 | if (postprologue_addr >= highpc) | |
e38d6504 RM |
1549 | { |
1550 | ranoff_end = true; | |
1551 | postprologue_srcline_idx --; | |
dc223023 FCE |
1552 | continue; |
1553 | } | |
1554 | if (ranoff_end || | |
3e961ba6 JB |
1555 | (strcmp (postprologue_file, it->decl_file) || // We have a winner! |
1556 | (postprologue_lineno != it->decl_line))) | |
dc223023 | 1557 | { |
3e961ba6 | 1558 | it->prologue_end = postprologue_addr; |
dc223023 FCE |
1559 | |
1560 | if (sess.verbose>2) | |
1561 | { | |
3e961ba6 | 1562 | clog << "prologue found function '" << it->name << "'"; |
dc223023 FCE |
1563 | // Add a little classification datum |
1564 | if (postprologue_srcline_idx == entrypc_srcline_idx) clog << " (naked)"; | |
1565 | if (ranoff_end) clog << " (tail-call?)"; | |
1566 | clog << " = 0x" << hex << postprologue_addr << dec << "\n"; | |
1567 | } | |
1568 | ||
1569 | break; | |
1570 | } | |
e38d6504 | 1571 | |
dc223023 FCE |
1572 | // Let's try the next srcline. |
1573 | postprologue_srcline_idx ++; | |
1574 | } // loop over srclines | |
7d71e1d5 | 1575 | |
3e961ba6 | 1576 | // if (strlen(it->decl_file) == 0) it->decl_file = NULL; |
dc223023 FCE |
1577 | |
1578 | } // loop over functions | |
b20febf3 FCE |
1579 | |
1580 | // XXX: how to free lines? | |
bd2b1e68 GH |
1581 | } |
1582 | ||
7e1279ea FCE |
1583 | |
1584 | bool function_entrypc (Dwarf_Addr * addr) | |
1585 | { | |
1586 | assert (function); | |
20e4a32c | 1587 | return (dwarf_entrypc (function, addr) == 0); |
b8da0ad1 | 1588 | // XXX: see also _lowpc ? |
7e1279ea FCE |
1589 | } |
1590 | ||
1591 | ||
1592 | bool die_entrypc (Dwarf_Die * die, Dwarf_Addr * addr) | |
1593 | { | |
5bc4ac10 FCE |
1594 | int rc = 0; |
1595 | string lookup_method; | |
7e1279ea | 1596 | |
5bc4ac10 FCE |
1597 | * addr = 0; |
1598 | ||
1599 | lookup_method = "dwarf_entrypc"; | |
1600 | rc = dwarf_entrypc (die, addr); | |
1601 | ||
4baf0e53 | 1602 | if (rc) |
5bc4ac10 FCE |
1603 | { |
1604 | lookup_method = "dwarf_lowpc"; | |
1605 | rc = dwarf_lowpc (die, addr); | |
1606 | } | |
1607 | ||
1608 | if (rc) | |
1609 | { | |
1610 | lookup_method = "dwarf_ranges"; | |
1611 | ||
1612 | Dwarf_Addr base; | |
1613 | Dwarf_Addr begin; | |
1614 | Dwarf_Addr end; | |
1615 | ptrdiff_t offset = dwarf_ranges (die, 0, &base, &begin, &end); | |
1616 | if (offset < 0) rc = -1; | |
1617 | else if (offset > 0) | |
1618 | { | |
1619 | * addr = begin; | |
1620 | rc = 0; | |
1621 | ||
1622 | // Now we need to check that there are no more ranges | |
1623 | // associated with this function, which could conceivably | |
1624 | // happen if a function is inlined, then pieces of it are | |
1625 | // split amongst different conditional branches. It's not | |
1626 | // obvious which of them to favour. As a heuristic, we | |
1627 | // pick the beginning of the first range, and ignore the | |
1628 | // others (but with a warning). | |
1629 | ||
1630 | unsigned extra = 0; | |
1631 | while ((offset = dwarf_ranges (die, offset, &base, &begin, &end)) > 0) | |
1632 | extra ++; | |
1633 | if (extra) | |
1634 | lookup_method += ", ignored " + lex_cast<string>(extra) + " more"; | |
1635 | } | |
1636 | } | |
4baf0e53 | 1637 | |
5bc4ac10 FCE |
1638 | if (sess.verbose > 2) |
1639 | clog << "entry-pc lookup (" << lookup_method << ") = 0x" << hex << *addr << dec | |
4baf0e53 | 1640 | << " (rc " << rc << ")" |
5bc4ac10 FCE |
1641 | << endl; |
1642 | return (rc == 0); | |
7e1279ea FCE |
1643 | } |
1644 | ||
4cd232e4 GH |
1645 | void function_die (Dwarf_Die *d) |
1646 | { | |
1647 | assert (function); | |
20e4a32c | 1648 | *d = *function; |
4cd232e4 | 1649 | } |
7e1279ea | 1650 | |
4cd232e4 | 1651 | void function_file (char const ** c) |
7e1279ea FCE |
1652 | { |
1653 | assert (function); | |
4cd232e4 | 1654 | assert (c); |
20e4a32c | 1655 | *c = dwarf_decl_file (function); |
7e1279ea FCE |
1656 | } |
1657 | ||
4cd232e4 | 1658 | void function_line (int *linep) |
7e1279ea FCE |
1659 | { |
1660 | assert (function); | |
20e4a32c | 1661 | dwarf_decl_line (function, linep); |
7e1279ea FCE |
1662 | } |
1663 | ||
86bf665e | 1664 | bool die_has_pc (Dwarf_Die & die, Dwarf_Addr pc) |
7e1279ea | 1665 | { |
86bf665e | 1666 | int res = dwarf_haspc (&die, pc); |
a688cff2 SC |
1667 | // dwarf_ranges will return -1 if a function die has no DW_AT_ranges |
1668 | // if (res == -1) | |
1669 | // dwarf_assert ("dwarf_haspc", res); | |
7e1279ea FCE |
1670 | return res == 1; |
1671 | } | |
1672 | ||
1673 | ||
78f6bba6 | 1674 | static void loc2c_error (void *, const char *fmt, ...) |
e36387d7 | 1675 | { |
78f6bba6 FCE |
1676 | const char *msg = "?"; |
1677 | char *tmp = NULL; | |
5ce20b7a | 1678 | int rc; |
e36387d7 RM |
1679 | va_list ap; |
1680 | va_start (ap, fmt); | |
78f6bba6 FCE |
1681 | rc = vasprintf (& tmp, fmt, ap); |
1682 | if (rc < 0) | |
1683 | msg = "?"; | |
1684 | else | |
1685 | msg = tmp; | |
e36387d7 RM |
1686 | va_end (ap); |
1687 | throw semantic_error (msg); | |
1688 | } | |
bd2b1e68 | 1689 | |
e664cf5b FCE |
1690 | // This function generates code used for addressing computations of |
1691 | // target variables. | |
e38d6504 RM |
1692 | void emit_address (struct obstack *pool, Dwarf_Addr address) |
1693 | { | |
e664cf5b FCE |
1694 | #if 0 |
1695 | // The easy but incorrect way is to just print a hard-wired | |
1696 | // constant. | |
e38d6504 | 1697 | obstack_printf (pool, "%#" PRIx64 "UL", address); |
e664cf5b | 1698 | #endif |
e38d6504 RM |
1699 | |
1700 | // Turn this address into a section-relative offset if it should be one. | |
1701 | // We emit a comment approximating the variable+offset expression that | |
1702 | // relocatable module probing code will need to have. | |
1703 | Dwfl_Module *mod = dwfl_addrmodule (dwfl, address); | |
86bf665e | 1704 | dwfl_assert ("dwfl_addrmodule", mod); |
432f054f FCE |
1705 | const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, |
1706 | NULL, NULL, NULL, NULL); | |
e38d6504 | 1707 | int n = dwfl_module_relocations (mod); |
ba53ea9f | 1708 | dwfl_assert ("dwfl_module_relocations", n >= 0); |
432f054f FCE |
1709 | Dwarf_Addr reloc_address = address; |
1710 | int i = dwfl_module_relocate_address (mod, &reloc_address); | |
ba53ea9f | 1711 | dwfl_assert ("dwfl_module_relocate_address", i >= 0); |
86bf665e | 1712 | dwfl_assert ("dwfl_module_info", modname); |
d64e82b1 SD |
1713 | const char *secname = dwfl_module_relocation_info (mod, i, NULL); |
1714 | ||
432f054f FCE |
1715 | if (sess.verbose > 2) |
1716 | { | |
1717 | clog << "emit dwarf addr 0x" << hex << address << dec | |
1718 | << " => module " << modname | |
1719 | << " section " << (secname ?: "null") | |
1720 | << " relocaddr 0x" << hex << reloc_address << dec | |
1721 | << endl; | |
1722 | } | |
1723 | ||
d64e82b1 SD |
1724 | if (n > 0 && !(n == 1 && secname == NULL)) |
1725 | { | |
ba53ea9f | 1726 | dwfl_assert ("dwfl_module_relocation_info", secname); |
e38d6504 | 1727 | if (n > 1 || secname[0] != '\0') |
7e41d3dc FCE |
1728 | { |
1729 | // This gives us the module name, and section name within the | |
1730 | // module, for a kernel module (or other ET_REL module object). | |
1731 | obstack_printf (pool, "({ static unsigned long addr = 0; "); | |
1732 | obstack_printf (pool, "if (addr==0) addr = _stp_module_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", | |
432f054f | 1733 | modname, secname, reloc_address); |
7e41d3dc FCE |
1734 | obstack_printf (pool, "addr; })"); |
1735 | } | |
4baf0e53 | 1736 | else if (n == 1 && module_name == TOK_KERNEL && secname[0] == '\0') |
21beacc9 FCE |
1737 | { |
1738 | // elfutils' way of telling us that this is a relocatable kernel address, which we | |
1739 | // need to treat the same way here as dwarf_query::add_probe_point does: _stext. | |
1740 | address -= sess.sym_stext; | |
1741 | secname = "_stext"; | |
1742 | obstack_printf (pool, "({ static unsigned long addr = 0; "); | |
1743 | obstack_printf (pool, "if (addr==0) addr = _stp_module_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", | |
432f054f | 1744 | modname, secname, address); // PR10000 NB: not reloc_address |
21beacc9 FCE |
1745 | obstack_printf (pool, "addr; })"); |
1746 | } | |
e38d6504 | 1747 | else |
e664cf5b FCE |
1748 | { |
1749 | throw semantic_error ("cannot relocate user-space dso (?) address"); | |
1750 | #if 0 | |
1751 | // This would happen for a Dwfl_Module that's a user-level DSO. | |
1752 | obstack_printf (pool, " /* %s+%#" PRIx64 " */", | |
1753 | modname, address); | |
1754 | #endif | |
1755 | } | |
e38d6504 | 1756 | } |
e664cf5b FCE |
1757 | else |
1758 | obstack_printf (pool, "%#" PRIx64 "UL", address); // assume as constant | |
e38d6504 | 1759 | } |
7e1279ea | 1760 | |
4b1ad75e RM |
1761 | static void loc2c_emit_address (void *arg, struct obstack *pool, |
1762 | Dwarf_Addr address) | |
1763 | { | |
1764 | dwflpp *dwfl = (dwflpp *) arg; | |
e38d6504 | 1765 | dwfl->emit_address (pool, address); |
4b1ad75e RM |
1766 | } |
1767 | ||
82e72903 DS |
1768 | void print_locals(Dwarf_Die *die, ostream &o) |
1769 | { | |
1770 | // Try to get the first child of die. | |
82e72903 DS |
1771 | Dwarf_Die child; |
1772 | if (dwarf_child (die, &child) == 0) | |
1773 | { | |
1774 | do | |
1775 | { | |
0e68eaaa | 1776 | const char *name; |
82e72903 DS |
1777 | // Output each sibling's name (that is a variable or |
1778 | // parameter) to 'o'. | |
1779 | switch (dwarf_tag (&child)) | |
1780 | { | |
1781 | case DW_TAG_variable: | |
1782 | case DW_TAG_formal_parameter: | |
0e68eaaa DS |
1783 | name = dwarf_diename (&child); |
1784 | if (name) | |
1785 | o << " " << name; | |
82e72903 DS |
1786 | break; |
1787 | default: | |
1788 | break; | |
1789 | } | |
1790 | } | |
1791 | while (dwarf_siblingof (&child, &child) == 0); | |
1792 | } | |
82e72903 DS |
1793 | } |
1794 | ||
e57b735a GH |
1795 | Dwarf_Attribute * |
1796 | find_variable_and_frame_base (Dwarf_Die *scope_die, | |
20e4a32c | 1797 | Dwarf_Addr pc, |
91eefb1c | 1798 | string const & local, |
e57b735a GH |
1799 | Dwarf_Die *vardie, |
1800 | Dwarf_Attribute *fb_attr_mem) | |
77de5e9e | 1801 | { |
77de5e9e | 1802 | Dwarf_Die *scopes; |
bcc12710 | 1803 | int nscopes = 0; |
e57b735a GH |
1804 | Dwarf_Attribute *fb_attr = NULL; |
1805 | ||
1806 | assert (cu); | |
bcc12710 | 1807 | |
f46d1b8a SC |
1808 | nscopes = dwarf_getscopes (cu, pc, &scopes); |
1809 | int sidx; | |
1810 | // if pc and scope_die are disjoint then we need dwarf_getscopes_die | |
1811 | for (sidx = 0; sidx < nscopes; sidx++) | |
1812 | if (scopes[sidx].addr == scope_die->addr) | |
1813 | break; | |
1814 | if (sidx == nscopes) | |
1815 | nscopes = dwarf_getscopes_die (scope_die, &scopes); | |
77de5e9e | 1816 | |
955925b7 | 1817 | if (nscopes <= 0) |
77de5e9e | 1818 | { |
7a053d3b | 1819 | throw semantic_error ("unable to find any scopes containing " |
59ff2773 | 1820 | + lex_cast_hex<string>(pc) |
1c7643a9 | 1821 | + ((scope_die == NULL) ? "" |
0e68eaaa DS |
1822 | : (string (" in ") |
1823 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
1824 | + "(" + (dwarf_diename(cu) ?: "<unknown>") | |
1825 | + ")")) | |
77de5e9e GH |
1826 | + " while searching for local '" + local + "'"); |
1827 | } | |
7a053d3b | 1828 | |
77de5e9e | 1829 | int declaring_scope = dwarf_getscopevar (scopes, nscopes, |
7a053d3b RM |
1830 | local.c_str(), |
1831 | 0, NULL, 0, 0, | |
e57b735a | 1832 | vardie); |
77de5e9e GH |
1833 | if (declaring_scope < 0) |
1834 | { | |
82e72903 DS |
1835 | stringstream alternatives; |
1836 | print_locals (scopes, alternatives); | |
77de5e9e | 1837 | throw semantic_error ("unable to find local '" + local + "'" |
82e72903 | 1838 | + " near pc " + lex_cast_hex<string>(pc) |
1c7643a9 | 1839 | + ((scope_die == NULL) ? "" |
0e68eaaa DS |
1840 | : (string (" in ") |
1841 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
1842 | + "(" + (dwarf_diename(cu) ?: "<unknown>") | |
1843 | + ")")) | |
8215ea9a | 1844 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); |
77de5e9e | 1845 | } |
7a053d3b | 1846 | |
77de5e9e GH |
1847 | for (int inner = 0; inner < nscopes; ++inner) |
1848 | { | |
1849 | switch (dwarf_tag (&scopes[inner])) | |
1850 | { | |
1851 | default: | |
1852 | continue; | |
1853 | case DW_TAG_subprogram: | |
1854 | case DW_TAG_entry_point: | |
1855 | case DW_TAG_inlined_subroutine: /* XXX */ | |
1856 | if (inner >= declaring_scope) | |
1857 | fb_attr = dwarf_attr_integrate (&scopes[inner], | |
1858 | DW_AT_frame_base, | |
e57b735a | 1859 | fb_attr_mem); |
77de5e9e GH |
1860 | break; |
1861 | } | |
1862 | } | |
e57b735a GH |
1863 | return fb_attr; |
1864 | } | |
77de5e9e | 1865 | |
77de5e9e | 1866 | |
d1531387 RM |
1867 | struct location * |
1868 | translate_location(struct obstack *pool, | |
1869 | Dwarf_Attribute *attr, Dwarf_Addr pc, | |
1870 | Dwarf_Attribute *fb_attr, | |
1871 | struct location **tail) | |
1872 | { | |
1873 | Dwarf_Op *expr; | |
1874 | size_t len; | |
1875 | ||
67982217 FCE |
1876 | /* PR9768: formerly, we added pc+module_bias here. However, that bias value |
1877 | is not present in the pc value by the time we get it, so adding it would | |
1878 | result in false negatives of variable reachibility. In other instances | |
1879 | further below, the c_translate_FOO functions, the module_bias value used | |
1880 | to be passed in, but instead should now be zero for the same reason. */ | |
1881 | ||
1882 | switch (dwarf_getlocation_addr (attr, pc /*+ module_bias*/, &expr, &len, 1)) | |
d1531387 RM |
1883 | { |
1884 | case 1: /* Should always happen. */ | |
1885 | if (len > 0) | |
1886 | break; | |
1887 | /* Fall through. */ | |
1888 | ||
1889 | case 0: /* Shouldn't happen. */ | |
1890 | throw semantic_error ("not accessible at this address"); | |
1891 | ||
1892 | default: /* Shouldn't happen. */ | |
1893 | case -1: | |
1894 | throw semantic_error (string ("dwarf_getlocation_addr failed") + | |
1895 | string (dwarf_errmsg (-1))); | |
1896 | } | |
1897 | ||
1898 | return c_translate_location (pool, &loc2c_error, this, | |
1899 | &loc2c_emit_address, | |
67982217 | 1900 | 1, 0 /* PR9768 */, |
d1531387 RM |
1901 | pc, expr, len, tail, fb_attr); |
1902 | } | |
1903 | ||
82e72903 DS |
1904 | void |
1905 | print_members(Dwarf_Die *vardie, ostream &o) | |
1906 | { | |
1907 | const int typetag = dwarf_tag (vardie); | |
1908 | ||
1909 | if (typetag != DW_TAG_structure_type && typetag != DW_TAG_union_type) | |
1910 | { | |
1911 | o << " Error: " | |
1912 | << (dwarf_diename_integrate (vardie) ?: "<anonymous>") | |
1913 | << " isn't a struct/union"; | |
1914 | return; | |
1915 | } | |
1916 | ||
1917 | // Try to get the first child of vardie. | |
1918 | Dwarf_Die die_mem; | |
1919 | Dwarf_Die *die = &die_mem; | |
1920 | switch (dwarf_child (vardie, die)) | |
1921 | { | |
1922 | case 1: // No children. | |
1923 | o << ((typetag == DW_TAG_union_type) ? " union " : " struct ") | |
1924 | << (dwarf_diename_integrate (die) ?: "<anonymous>") | |
1925 | << " is empty"; | |
1926 | break; | |
1927 | ||
1928 | case -1: // Error. | |
1929 | default: // Shouldn't happen. | |
1930 | o << ((typetag == DW_TAG_union_type) ? " union " : " struct ") | |
1931 | << (dwarf_diename_integrate (die) ?: "<anonymous>") | |
1932 | << ": " << dwarf_errmsg (-1); | |
1933 | break; | |
1934 | ||
1935 | case 0: // Success. | |
1936 | break; | |
1937 | } | |
1938 | ||
1939 | // Output each sibling's name to 'o'. | |
1940 | while (dwarf_tag (die) == DW_TAG_member) | |
1941 | { | |
4b3b2cc7 PS |
1942 | const char *member = dwarf_diename_integrate (die) ; |
1943 | ||
1944 | if ( member != NULL ) | |
4baf0e53 | 1945 | |
4b3b2cc7 PS |
1946 | o << " " << member; |
1947 | ||
1948 | else | |
1949 | { | |
1950 | Dwarf_Die temp_die = *die; | |
1951 | Dwarf_Attribute temp_attr ; | |
1952 | ||
1953 | if (!dwarf_attr_integrate (&temp_die, DW_AT_type, &temp_attr)) | |
1954 | { | |
1955 | clog<<"\n Error in obtaining type attribute for " | |
1956 | <<(dwarf_diename(&temp_die)?:"<anonymous>"); | |
1957 | return ; | |
1958 | } | |
1959 | ||
1960 | if ( ! dwarf_formref_die (&temp_attr,&temp_die)) | |
1961 | { | |
1962 | clog<<"\n Error in decoding type attribute for " | |
1963 | <<(dwarf_diename(&temp_die)?:"<anonymous>"); | |
1964 | return ; | |
1965 | } | |
1966 | print_members(&temp_die,o); | |
1967 | ||
1968 | } | |
82e72903 DS |
1969 | |
1970 | if (dwarf_siblingof (die, &die_mem) != 0) | |
1971 | break; | |
1972 | } | |
1973 | } | |
1974 | ||
e57b735a GH |
1975 | Dwarf_Die * |
1976 | translate_components(struct obstack *pool, | |
20e4a32c RM |
1977 | struct location **tail, |
1978 | Dwarf_Addr pc, | |
e57b735a GH |
1979 | vector<pair<target_symbol::component_type, |
1980 | std::string> > const & components, | |
1981 | Dwarf_Die *vardie, | |
1982 | Dwarf_Die *die_mem, | |
1983 | Dwarf_Attribute *attr_mem) | |
1984 | { | |
c4ce66a1 | 1985 | Dwarf_Die *die = die_mem; |
82e72903 | 1986 | Dwarf_Die struct_die; |
4b3b2cc7 PS |
1987 | Dwarf_Attribute temp_attr; |
1988 | ||
d9b516ca | 1989 | unsigned i = 0; |
4b3b2cc7 | 1990 | |
c4ce66a1 JS |
1991 | if (vardie) |
1992 | *die_mem = *vardie; | |
1993 | ||
4b3b2cc7 PS |
1994 | static unsigned int func_call_level ; |
1995 | static unsigned int dwarf_error_flag ; // indicates current error is dwarf error | |
1996 | static unsigned int dwarf_error_count ; // keeps track of no of dwarf errors | |
1997 | static semantic_error saved_dwarf_error(""); | |
1998 | ||
d9b516ca RM |
1999 | while (i < components.size()) |
2000 | { | |
0301cfe7 FCE |
2001 | /* XXX: This would be desirable, but we don't get the target_symbol token, |
2002 | and printing that gives us the file:line number too early anyway. */ | |
2003 | #if 0 | |
2004 | // Emit a marker to note which field is being access-attempted, to give | |
2005 | // better error messages if deref() fails. | |
2006 | string piece = string(...target_symbol token...) + string ("#") + stringify(components[i].second); | |
2007 | obstack_printf (pool, "c->last_stmt = %s;", lex_cast_qstring(piece).c_str()); | |
2008 | #endif | |
2009 | ||
d9b516ca RM |
2010 | const int typetag = dwarf_tag (die); |
2011 | switch (typetag) | |
2012 | { | |
2013 | case DW_TAG_typedef: | |
fdfbe4f7 GH |
2014 | case DW_TAG_const_type: |
2015 | case DW_TAG_volatile_type: | |
d9b516ca RM |
2016 | /* Just iterate on the referent type. */ |
2017 | break; | |
91eefb1c | 2018 | |
d9b516ca RM |
2019 | case DW_TAG_pointer_type: |
2020 | if (components[i].first == target_symbol::comp_literal_array_index) | |
2302c47e FCE |
2021 | throw semantic_error ("cannot index pointer"); |
2022 | // XXX: of course, we should support this the same way C does, | |
b0be9bdb | 2023 | // by explicit pointer arithmetic etc. PR4166. |
91eefb1c | 2024 | |
67982217 | 2025 | c_translate_pointer (pool, 1, 0 /* PR9768*/, die, tail); |
d9b516ca | 2026 | break; |
91eefb1c | 2027 | |
d9b516ca RM |
2028 | case DW_TAG_array_type: |
2029 | if (components[i].first == target_symbol::comp_literal_array_index) | |
2030 | { | |
67982217 | 2031 | c_translate_array (pool, 1, 0 /* PR9768 */, die, tail, |
d9b516ca RM |
2032 | NULL, lex_cast<Dwarf_Word>(components[i].second)); |
2033 | ++i; | |
2034 | } | |
2035 | else | |
2036 | throw semantic_error("bad field '" | |
2037 | + components[i].second | |
2038 | + "' for array type"); | |
2039 | break; | |
91eefb1c | 2040 | |
d9b516ca RM |
2041 | case DW_TAG_structure_type: |
2042 | case DW_TAG_union_type: | |
82e72903 | 2043 | struct_die = *die; |
0b8f6579 JB |
2044 | if (dwarf_hasattr(die, DW_AT_declaration)) |
2045 | { | |
c4ce66a1 | 2046 | Dwarf_Die *tmpdie = dwflpp::declaration_resolve(dwarf_diename(die)); |
0b8f6579 JB |
2047 | if (tmpdie == NULL) |
2048 | throw semantic_error ("unresolved struct " | |
2049 | + string (dwarf_diename_integrate (die) ?: "<anonymous>")); | |
2050 | *die_mem = *tmpdie; | |
2051 | } | |
e57b735a | 2052 | switch (dwarf_child (die, die_mem)) |
d9b516ca RM |
2053 | { |
2054 | case 1: /* No children. */ | |
4b3b2cc7 | 2055 | return NULL; |
d9b516ca RM |
2056 | case -1: /* Error. */ |
2057 | default: /* Shouldn't happen */ | |
2058 | throw semantic_error (string (typetag == DW_TAG_union_type ? "union" : "struct") | |
2059 | + string (dwarf_diename_integrate (die) ?: "<anonymous>") | |
2060 | + string (dwarf_errmsg (-1))); | |
2061 | break; | |
2062 | ||
2063 | case 0: | |
2064 | break; | |
2065 | } | |
2066 | ||
2067 | while (dwarf_tag (die) != DW_TAG_member | |
2068 | || ({ const char *member = dwarf_diename_integrate (die); | |
2069 | member == NULL || string(member) != components[i].second; })) | |
4b3b2cc7 PS |
2070 | { |
2071 | if ( dwarf_diename (die) == NULL ) // handling Anonymous structs/unions | |
2072 | { | |
2073 | Dwarf_Die temp_die = *die; | |
2074 | Dwarf_Die temp_die_2; | |
2075 | ||
2076 | try | |
2077 | { | |
2078 | if (!dwarf_attr_integrate (&temp_die, DW_AT_type, &temp_attr)) | |
2079 | { | |
2080 | dwarf_error_flag ++ ; | |
2081 | dwarf_error_count ++; | |
2082 | throw semantic_error(" Error in obtaining type attribute for "+ string(dwarf_diename(&temp_die)?:"<anonymous>")); | |
2083 | } | |
2084 | ||
2085 | if ( !dwarf_formref_die (&temp_attr, &temp_die)) | |
2086 | { | |
2087 | dwarf_error_flag ++ ; | |
2088 | dwarf_error_count ++; | |
2089 | throw semantic_error(" Error in decoding DW_AT_type attribute for " + string(dwarf_diename(&temp_die)?:"<anonymous>")); | |
2090 | } | |
2091 | ||
2092 | func_call_level ++ ; | |
2093 | ||
2094 | Dwarf_Die *result_die = translate_components(pool, tail, pc, components, &temp_die, &temp_die_2, &temp_attr ); | |
2095 | ||
2096 | func_call_level -- ; | |
2097 | ||
2098 | if (result_die != NULL) | |
2099 | { | |
2100 | memcpy(die_mem, &temp_die_2, sizeof(Dwarf_Die)); | |
2101 | memcpy(attr_mem, &temp_attr, sizeof(Dwarf_Attribute)); | |
2102 | return die_mem; | |
2103 | } | |
2104 | } | |
2105 | catch (const semantic_error& e) | |
2106 | { | |
2107 | if ( !dwarf_error_flag ) //not a dwarf error | |
2108 | throw; | |
2109 | else | |
2110 | { | |
2111 | dwarf_error_flag = 0 ; | |
2112 | saved_dwarf_error = e ; | |
2113 | } | |
2114 | } | |
2115 | } | |
e57b735a | 2116 | if (dwarf_siblingof (die, die_mem) != 0) |
4b3b2cc7 PS |
2117 | { |
2118 | if ( func_call_level == 0 && dwarf_error_count ) // this is parent call & a dwarf error has been reported in a branch somewhere | |
2119 | throw semantic_error( saved_dwarf_error ); | |
2120 | else | |
2121 | return NULL; | |
2122 | } | |
2123 | } | |
d9b516ca RM |
2124 | |
2125 | if (dwarf_attr_integrate (die, DW_AT_data_member_location, | |
e57b735a | 2126 | attr_mem) == NULL) |
d9b516ca RM |
2127 | { |
2128 | /* Union members don't usually have a location, | |
2129 | but just use the containing union's location. */ | |
2130 | if (typetag != DW_TAG_union_type) | |
9f36b77f | 2131 | throw semantic_error ("no location for field '" |
d9b516ca | 2132 | + components[i].second |
9f36b77f | 2133 | + "' :" + string(dwarf_errmsg (-1))); |
d9b516ca RM |
2134 | } |
2135 | else | |
d1531387 | 2136 | translate_location (pool, attr_mem, pc, NULL, tail); |
d9b516ca RM |
2137 | ++i; |
2138 | break; | |
2139 | ||
2140 | case DW_TAG_base_type: | |
9f36b77f | 2141 | throw semantic_error ("field '" |
d9b516ca | 2142 | + components[i].second |
9f36b77f | 2143 | + "' vs. base type " |
d9b516ca RM |
2144 | + string(dwarf_diename_integrate (die) ?: "<anonymous type>")); |
2145 | break; | |
2146 | case -1: | |
2147 | throw semantic_error ("cannot find type: " + string(dwarf_errmsg (-1))); | |
2148 | break; | |
2149 | ||
2150 | default: | |
2151 | throw semantic_error (string(dwarf_diename_integrate (die) ?: "<anonymous type>") | |
2152 | + ": unexpected type tag " | |
2153 | + lex_cast<string>(dwarf_tag (die))); | |
2154 | break; | |
2155 | } | |
2156 | ||
2157 | /* Now iterate on the type in DIE's attribute. */ | |
e57b735a | 2158 | if (dwarf_attr_integrate (die, DW_AT_type, attr_mem) == NULL) |
d9b516ca | 2159 | throw semantic_error ("cannot get type of field: " + string(dwarf_errmsg (-1))); |
c4ce66a1 | 2160 | die = dwarf_formref_die (attr_mem, die_mem); |
d9b516ca | 2161 | } |
e57b735a GH |
2162 | return die; |
2163 | } | |
91eefb1c | 2164 | |
d9b516ca | 2165 | |
e57b735a GH |
2166 | Dwarf_Die * |
2167 | resolve_unqualified_inner_typedie (Dwarf_Die *typedie_mem, | |
2168 | Dwarf_Attribute *attr_mem) | |
2169 | { | |
2170 | ; | |
d9b516ca | 2171 | Dwarf_Die *typedie; |
e57b735a | 2172 | int typetag = 0; |
d9b516ca | 2173 | while (1) |
20e4a32c | 2174 | { |
e57b735a | 2175 | typedie = dwarf_formref_die (attr_mem, typedie_mem); |
d9b516ca | 2176 | if (typedie == NULL) |
e57b735a | 2177 | throw semantic_error ("cannot get type: " + string(dwarf_errmsg (-1))); |
d9b516ca | 2178 | typetag = dwarf_tag (typedie); |
20e4a32c | 2179 | if (typetag != DW_TAG_typedef && |
fdfbe4f7 GH |
2180 | typetag != DW_TAG_const_type && |
2181 | typetag != DW_TAG_volatile_type) | |
91eefb1c | 2182 | break; |
e57b735a GH |
2183 | if (dwarf_attr_integrate (typedie, DW_AT_type, attr_mem) == NULL) |
2184 | throw semantic_error ("cannot get type of pointee: " + string(dwarf_errmsg (-1))); | |
d9b516ca | 2185 | } |
e57b735a GH |
2186 | return typedie; |
2187 | } | |
91eefb1c | 2188 | |
91eefb1c | 2189 | |
20e4a32c | 2190 | void |
e57b735a GH |
2191 | translate_final_fetch_or_store (struct obstack *pool, |
2192 | struct location **tail, | |
2193 | Dwarf_Addr module_bias, | |
2194 | Dwarf_Die *die, | |
2195 | Dwarf_Attribute *attr_mem, | |
2196 | bool lvalue, | |
b8da0ad1 FCE |
2197 | string &, |
2198 | string &, | |
e57b735a GH |
2199 | exp_type & ty) |
2200 | { | |
2201 | /* First boil away any qualifiers associated with the type DIE of | |
2202 | the final location to be accessed. */ | |
fdfbe4f7 | 2203 | |
e57b735a GH |
2204 | Dwarf_Die typedie_mem; |
2205 | Dwarf_Die *typedie; | |
2206 | int typetag; | |
022b623f DS |
2207 | char const *dname; |
2208 | string diestr; | |
e57b735a GH |
2209 | |
2210 | typedie = resolve_unqualified_inner_typedie (&typedie_mem, attr_mem); | |
2211 | typetag = dwarf_tag (typedie); | |
2212 | ||
2213 | /* Then switch behavior depending on the type of fetch/store we | |
2214 | want, and the type and pointer-ness of the final location. */ | |
20e4a32c | 2215 | |
fdfbe4f7 GH |
2216 | switch (typetag) |
2217 | { | |
fdfbe4f7 | 2218 | default: |
022b623f DS |
2219 | dname = dwarf_diename(die); |
2220 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
66d284f4 | 2221 | throw semantic_error ("unsupported type tag " |
022b623f DS |
2222 | + lex_cast<string>(typetag) |
2223 | + " for " + diestr); | |
2224 | break; | |
2225 | ||
2226 | case DW_TAG_structure_type: | |
2227 | case DW_TAG_union_type: | |
2228 | dname = dwarf_diename(die); | |
2229 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
2230 | throw semantic_error ("struct/union '" + diestr | |
2231 | + "' is being accessed instead of a member of the struct/union"); | |
fdfbe4f7 | 2232 | break; |
66d284f4 | 2233 | |
e7a012f0 | 2234 | case DW_TAG_enumeration_type: |
fdfbe4f7 | 2235 | case DW_TAG_base_type: |
00cf3709 FCE |
2236 | |
2237 | // Reject types we can't handle in systemtap | |
2238 | { | |
2239 | dname = dwarf_diename(die); | |
2240 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
2241 | ||
2242 | Dwarf_Attribute encoding_attr; | |
6503a1cc | 2243 | Dwarf_Word encoding = (Dwarf_Word) -1; |
00cf3709 FCE |
2244 | dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding, &encoding_attr), |
2245 | & encoding); | |
2246 | if (encoding < 0) | |
2247 | { | |
2248 | // clog << "bad type1 " << encoding << " diestr" << endl; | |
2249 | throw semantic_error ("unsupported type (mystery encoding " + lex_cast<string>(encoding) + ")" + | |
2250 | " for " + diestr); | |
2251 | } | |
2252 | ||
2253 | if (encoding == DW_ATE_float | |
2254 | || encoding == DW_ATE_complex_float | |
2255 | /* XXX || many others? */) | |
2256 | { | |
2257 | // clog << "bad type " << encoding << " diestr" << endl; | |
2258 | throw semantic_error ("unsupported type (encoding " + lex_cast<string>(encoding) + ")" + | |
2259 | " for " + diestr); | |
2260 | } | |
2261 | } | |
2262 | ||
fdfbe4f7 | 2263 | ty = pe_long; |
e57b735a | 2264 | if (lvalue) |
67982217 | 2265 | c_translate_store (pool, 1, 0 /* PR9768 */, die, typedie, tail, |
e57b735a | 2266 | "THIS->value"); |
20e4a32c | 2267 | else |
67982217 | 2268 | c_translate_fetch (pool, 1, 0 /* PR9768 */, die, typedie, tail, |
e57b735a | 2269 | "THIS->__retvalue"); |
fdfbe4f7 GH |
2270 | break; |
2271 | ||
2272 | case DW_TAG_array_type: | |
2273 | case DW_TAG_pointer_type: | |
e57b735a | 2274 | |
b0be9bdb | 2275 | { |
fdfbe4f7 GH |
2276 | Dwarf_Die pointee_typedie_mem; |
2277 | Dwarf_Die *pointee_typedie; | |
2278 | Dwarf_Word pointee_encoding; | |
246b383e | 2279 | Dwarf_Word pointee_byte_size = 0; |
fdfbe4f7 | 2280 | |
e57b735a GH |
2281 | pointee_typedie = resolve_unqualified_inner_typedie (&pointee_typedie_mem, attr_mem); |
2282 | ||
2283 | if (dwarf_attr_integrate (pointee_typedie, DW_AT_byte_size, attr_mem)) | |
2284 | dwarf_formudata (attr_mem, &pointee_byte_size); | |
20e4a32c RM |
2285 | |
2286 | dwarf_formudata (dwarf_attr_integrate (pointee_typedie, DW_AT_encoding, attr_mem), | |
fdfbe4f7 GH |
2287 | &pointee_encoding); |
2288 | ||
b0be9bdb FCE |
2289 | if (lvalue) |
2290 | { | |
2291 | ty = pe_long; | |
2292 | if (typetag == DW_TAG_array_type) | |
2293 | throw semantic_error ("cannot write to array address"); | |
2294 | assert (typetag == DW_TAG_pointer_type); | |
67982217 | 2295 | c_translate_pointer_store (pool, 1, 0 /* PR9768 */, typedie, tail, |
b0be9bdb FCE |
2296 | "THIS->value"); |
2297 | } | |
2298 | else | |
2299 | { | |
2300 | // We have the pointer: cast it to an integral type via &(*(...)) | |
41c262f3 | 2301 | |
b0be9bdb FCE |
2302 | // NB: per bug #1187, at one point char*-like types were |
2303 | // automagically converted here to systemtap string values. | |
2304 | // For several reasons, this was taken back out, leaving | |
2305 | // pointer-to-string "conversion" (copying) to tapset functions. | |
41c262f3 | 2306 | |
b0be9bdb FCE |
2307 | ty = pe_long; |
2308 | if (typetag == DW_TAG_array_type) | |
67982217 | 2309 | c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail, NULL, 0); |
b0be9bdb | 2310 | else |
67982217 FCE |
2311 | c_translate_pointer (pool, 1, 0 /* PR9768 */, typedie, tail); |
2312 | c_translate_addressof (pool, 1, 0 /* PR9768 */, NULL, pointee_typedie, tail, | |
b0be9bdb FCE |
2313 | "THIS->__retvalue"); |
2314 | } | |
2315 | } | |
20e4a32c | 2316 | break; |
fdfbe4f7 | 2317 | } |
20e4a32c | 2318 | } |
e57b735a | 2319 | |
e19fda4e DS |
2320 | string |
2321 | express_as_string (string prelude, | |
2322 | string postlude, | |
2323 | struct location *head) | |
2324 | { | |
2325 | size_t bufsz = 1024; | |
2326 | char *buf = static_cast<char*>(malloc(bufsz)); | |
2327 | assert(buf); | |
2328 | ||
2329 | FILE *memstream = open_memstream (&buf, &bufsz); | |
2330 | assert(memstream); | |
2331 | ||
2332 | fprintf(memstream, "{\n"); | |
bec508de | 2333 | fprintf(memstream, "%s", prelude.c_str()); |
e19fda4e | 2334 | bool deref = c_emit_location (memstream, head, 1); |
bec508de | 2335 | fprintf(memstream, "%s", postlude.c_str()); |
e19fda4e DS |
2336 | fprintf(memstream, " goto out;\n"); |
2337 | ||
2338 | // dummy use of deref_fault label, to disable warning if deref() not used | |
2339 | fprintf(memstream, "if (0) goto deref_fault;\n"); | |
2340 | ||
2341 | // XXX: deref flag not reliable; emit fault label unconditionally | |
78f6bba6 | 2342 | (void) deref; |
e19fda4e DS |
2343 | fprintf(memstream, |
2344 | "deref_fault:\n" | |
e19fda4e DS |
2345 | " goto out;\n"); |
2346 | fprintf(memstream, "}\n"); | |
2347 | ||
2348 | fclose (memstream); | |
2349 | string result(buf); | |
2350 | free (buf); | |
2351 | return result; | |
2352 | } | |
e57b735a | 2353 | |
20e4a32c | 2354 | string |
e57b735a | 2355 | literal_stmt_for_local (Dwarf_Die *scope_die, |
20e4a32c | 2356 | Dwarf_Addr pc, |
e57b735a GH |
2357 | string const & local, |
2358 | vector<pair<target_symbol::component_type, | |
2359 | std::string> > const & components, | |
2360 | bool lvalue, | |
2361 | exp_type & ty) | |
2362 | { | |
2363 | Dwarf_Die vardie; | |
2364 | Dwarf_Attribute fb_attr_mem, *fb_attr = NULL; | |
2365 | ||
20e4a32c | 2366 | fb_attr = find_variable_and_frame_base (scope_die, pc, local, |
e57b735a GH |
2367 | &vardie, &fb_attr_mem); |
2368 | ||
b0ee93c4 | 2369 | if (sess.verbose>2) |
e57b735a | 2370 | clog << "finding location for local '" << local |
12b44fb3 FCE |
2371 | << "' near address 0x" << hex << pc |
2372 | << ", module bias 0x" << module_bias << dec | |
db22e55f | 2373 | << "\n"; |
e57b735a GH |
2374 | |
2375 | Dwarf_Attribute attr_mem; | |
2376 | if (dwarf_attr_integrate (&vardie, DW_AT_location, &attr_mem) == NULL) | |
2377 | { | |
2378 | throw semantic_error("failed to retrieve location " | |
20e4a32c RM |
2379 | "attribute for local '" + local |
2380 | + "' (dieoffset: " | |
2381 | + lex_cast_hex<string>(dwarf_dieoffset (&vardie)) | |
e57b735a GH |
2382 | + ")"); |
2383 | } | |
2384 | ||
2385 | #define obstack_chunk_alloc malloc | |
2386 | #define obstack_chunk_free free | |
2387 | ||
2388 | struct obstack pool; | |
2389 | obstack_init (&pool); | |
2390 | struct location *tail = NULL; | |
2391 | ||
2392 | /* Given $foo->bar->baz[NN], translate the location of foo. */ | |
2393 | ||
d1531387 RM |
2394 | struct location *head = translate_location (&pool, |
2395 | &attr_mem, pc, fb_attr, &tail); | |
e57b735a GH |
2396 | |
2397 | if (dwarf_attr_integrate (&vardie, DW_AT_type, &attr_mem) == NULL) | |
2398 | throw semantic_error("failed to retrieve type " | |
2399 | "attribute for local '" + local + "'"); | |
2400 | ||
e57b735a GH |
2401 | /* Translate the ->bar->baz[NN] parts. */ |
2402 | ||
2403 | Dwarf_Die die_mem, *die = NULL; | |
c4ce66a1 | 2404 | die = dwarf_formref_die (&attr_mem, &die_mem); |
20e4a32c | 2405 | die = translate_components (&pool, &tail, pc, components, |
c4ce66a1 | 2406 | die, &die_mem, &attr_mem); |
4b3b2cc7 PS |
2407 | if(!die) |
2408 | { | |
2409 | die = dwarf_formref_die (&attr_mem, &vardie); | |
2410 | stringstream alternatives; | |
a3de5d6e MW |
2411 | if (die != NULL) |
2412 | print_members(die,alternatives); | |
b487a14d FCE |
2413 | throw semantic_error("unable to find local '" + local + "'" |
2414 | + " near pc " + lex_cast_hex<string>(pc) | |
2415 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); | |
4b3b2cc7 | 2416 | } |
e57b735a | 2417 | |
20e4a32c RM |
2418 | /* Translate the assignment part, either |
2419 | x = $foo->bar->baz[NN] | |
2420 | or | |
e57b735a GH |
2421 | $foo->bar->baz[NN] = x |
2422 | */ | |
2423 | ||
2424 | string prelude, postlude; | |
20e4a32c | 2425 | translate_final_fetch_or_store (&pool, &tail, module_bias, |
e57b735a GH |
2426 | die, &attr_mem, lvalue, |
2427 | prelude, postlude, ty); | |
2428 | ||
2429 | /* Write the translation to a string. */ | |
e19fda4e DS |
2430 | return express_as_string(prelude, postlude, head); |
2431 | } | |
20e4a32c | 2432 | |
20e4a32c | 2433 | |
e19fda4e DS |
2434 | string |
2435 | literal_stmt_for_return (Dwarf_Die *scope_die, | |
2436 | Dwarf_Addr pc, | |
2437 | vector<pair<target_symbol::component_type, | |
2438 | std::string> > const & components, | |
2439 | bool lvalue, | |
2440 | exp_type & ty) | |
2441 | { | |
2442 | if (sess.verbose>2) | |
2443 | clog << "literal_stmt_for_return: finding return value for " | |
0e68eaaa | 2444 | << (dwarf_diename(scope_die) ?: "<unknown>") |
e19fda4e | 2445 | << "(" |
0e68eaaa | 2446 | << (dwarf_diename(cu) ?: "<unknown>") |
e19fda4e | 2447 | << ")\n"; |
7a053d3b | 2448 | |
e19fda4e DS |
2449 | struct obstack pool; |
2450 | obstack_init (&pool); | |
2451 | struct location *tail = NULL; | |
7a053d3b | 2452 | |
e19fda4e DS |
2453 | /* Given $return->bar->baz[NN], translate the location of return. */ |
2454 | const Dwarf_Op *locops; | |
2455 | int nlocops = dwfl_module_return_value_location (module, scope_die, | |
2456 | &locops); | |
2457 | if (nlocops < 0) | |
2458 | { | |
194c6687 | 2459 | throw semantic_error("failed to retrieve return value location" |
0e68eaaa DS |
2460 | " for " |
2461 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2462 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
2463 | + ")"); | |
e19fda4e DS |
2464 | } |
2465 | // the function has no return value (e.g. "void" in C) | |
2466 | else if (nlocops == 0) | |
2467 | { | |
0e68eaaa DS |
2468 | throw semantic_error("function " |
2469 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2470 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
194c6687 | 2471 | + ") has no return value"); |
e19fda4e | 2472 | } |
a781f401 | 2473 | |
e19fda4e DS |
2474 | struct location *head = c_translate_location (&pool, &loc2c_error, this, |
2475 | &loc2c_emit_address, | |
67982217 | 2476 | 1, 0 /* PR9768 */, |
e19fda4e DS |
2477 | pc, locops, nlocops, |
2478 | &tail, NULL); | |
7a053d3b | 2479 | |
e19fda4e | 2480 | /* Translate the ->bar->baz[NN] parts. */ |
7a053d3b | 2481 | |
e19fda4e DS |
2482 | Dwarf_Attribute attr_mem; |
2483 | Dwarf_Attribute *attr = dwarf_attr (scope_die, DW_AT_type, &attr_mem); | |
2484 | ||
2485 | Dwarf_Die vardie_mem; | |
2486 | Dwarf_Die *vardie = dwarf_formref_die (attr, &vardie_mem); | |
2487 | ||
2488 | Dwarf_Die die_mem, *die = NULL; | |
2489 | die = translate_components (&pool, &tail, pc, components, | |
2490 | vardie, &die_mem, &attr_mem); | |
4b3b2cc7 PS |
2491 | if(!die) |
2492 | { | |
2493 | die = dwarf_formref_die (&attr_mem, vardie); | |
2494 | stringstream alternatives; | |
a3de5d6e MW |
2495 | if (die != NULL) |
2496 | print_members(die,alternatives); | |
b487a14d FCE |
2497 | throw semantic_error("unable to find return value" |
2498 | " near pc " + lex_cast_hex<string>(pc) | |
0e68eaaa DS |
2499 | + " for " |
2500 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2501 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
2502 | + ")" | |
b487a14d | 2503 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); |
4b3b2cc7 PS |
2504 | } |
2505 | ||
e19fda4e DS |
2506 | |
2507 | /* Translate the assignment part, either | |
2508 | x = $return->bar->baz[NN] | |
2509 | or | |
2510 | $return->bar->baz[NN] = x | |
2511 | */ | |
2512 | ||
2513 | string prelude, postlude; | |
2514 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
2515 | die, &attr_mem, lvalue, | |
2516 | prelude, postlude, ty); | |
2517 | ||
2518 | /* Write the translation to a string. */ | |
2519 | return express_as_string(prelude, postlude, head); | |
2520 | } | |
7a053d3b | 2521 | |
77de5e9e | 2522 | |
c4ce66a1 JS |
2523 | string |
2524 | literal_stmt_for_pointer (Dwarf_Die *type_die, | |
2525 | vector<pair<target_symbol::component_type, | |
2526 | std::string> > const & components, | |
2527 | bool lvalue, | |
2528 | exp_type & ty) | |
2529 | { | |
2530 | if (sess.verbose>2) | |
2531 | clog << "literal_stmt_for_pointer: finding value for " | |
2532 | << (dwarf_diename(type_die) ?: "<unknown>") | |
2533 | << "(" | |
2534 | << (dwarf_diename(cu) ?: "<unknown>") | |
2535 | << ")\n"; | |
2536 | ||
2537 | struct obstack pool; | |
2538 | obstack_init (&pool); | |
2539 | struct location *head = c_translate_argument (&pool, &loc2c_error, this, | |
2540 | &loc2c_emit_address, | |
2541 | 1, "THIS->pointer"); | |
2542 | struct location *tail = head; | |
2543 | ||
2544 | /* Translate the ->bar->baz[NN] parts. */ | |
2545 | ||
2546 | Dwarf_Attribute attr_mem; | |
2547 | Dwarf_Die die_mem, *die = NULL; | |
2548 | die = translate_components (&pool, &tail, 0, components, | |
2549 | type_die, &die_mem, &attr_mem); | |
2550 | if(!die) | |
2551 | { | |
2552 | die = dwarf_formref_die (&attr_mem, &die_mem); | |
2553 | stringstream alternatives; | |
2554 | print_members(die ?: type_die, alternatives); | |
2555 | throw semantic_error("unable to find member for struct " | |
2556 | + string(dwarf_diename(die ?: type_die) ?: "<unknown>") | |
2557 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); | |
2558 | } | |
2559 | ||
2560 | ||
2561 | /* Translate the assignment part, either | |
2562 | x = (THIS->pointer)->bar->baz[NN] | |
2563 | or | |
2564 | (THIS->pointer)->bar->baz[NN] = x | |
2565 | */ | |
2566 | ||
2567 | string prelude, postlude; | |
2568 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
2569 | die, &attr_mem, lvalue, | |
2570 | prelude, postlude, ty); | |
2571 | ||
2572 | /* Write the translation to a string. */ | |
2573 | return express_as_string(prelude, postlude, head); | |
2574 | } | |
2575 | ||
2576 | ||
bd2b1e68 GH |
2577 | ~dwflpp() |
2578 | { | |
2579 | if (dwfl) | |
2580 | dwfl_end(dwfl); | |
2581 | } | |
2582 | }; | |
2583 | ||
405b71b8 | 2584 | |
20c6c071 | 2585 | |
7a053d3b | 2586 | enum |
bd2b1e68 | 2587 | function_spec_type |
7a053d3b | 2588 | { |
bd2b1e68 GH |
2589 | function_alone, |
2590 | function_and_file, | |
7a053d3b | 2591 | function_file_and_line |
bd2b1e68 GH |
2592 | }; |
2593 | ||
ec4373ff | 2594 | |
bd2b1e68 | 2595 | struct dwarf_builder; |
77de5e9e | 2596 | |
2930abc7 | 2597 | |
b20febf3 FCE |
2598 | // XXX: This class is a candidate for subclassing to separate |
2599 | // the relocation vs non-relocation variants. Likewise for | |
2600 | // kprobe vs kretprobe variants. | |
2601 | ||
2602 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 2603 | { |
b20febf3 FCE |
2604 | dwarf_derived_probe (const string& function, |
2605 | const string& filename, | |
2606 | int line, | |
2607 | const string& module, | |
2608 | const string& section, | |
2609 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 2610 | Dwarf_Addr addr, |
b20febf3 FCE |
2611 | dwarf_query & q, |
2612 | Dwarf_Die* scope_die); | |
20e4a32c | 2613 | |
b20febf3 FCE |
2614 | string module; |
2615 | string section; | |
2616 | Dwarf_Addr addr; | |
2930abc7 | 2617 | bool has_return; |
c9bad430 DS |
2618 | bool has_maxactive; |
2619 | long maxactive_val; | |
b95e2b79 | 2620 | bool access_vars; |
2930abc7 | 2621 | |
b8da0ad1 | 2622 | void printsig (std::ostream &o) const; |
b20febf3 | 2623 | void join_group (systemtap_session& s); |
9020300d FCE |
2624 | void emit_probe_local_init(translator_output * o); |
2625 | ||
bd2b1e68 | 2626 | // Pattern registration helpers. |
7a053d3b | 2627 | static void register_statement_variants(match_node * root, |
bd2b1e68 | 2628 | dwarf_builder * dw); |
fd6602a0 FCE |
2629 | static void register_function_variants(match_node * root, |
2630 | dwarf_builder * dw); | |
7a053d3b | 2631 | static void register_function_and_statement_variants(match_node * root, |
bd2b1e68 | 2632 | dwarf_builder * dw); |
c4ce66a1 | 2633 | static void register_patterns(systemtap_session& s); |
20c6c071 GH |
2634 | }; |
2635 | ||
dc38c0ae | 2636 | |
6d0f3f0c FCE |
2637 | struct uprobe_derived_probe: public derived_probe |
2638 | { | |
2639 | bool return_p; | |
2640 | string module; // * => unrestricted | |
2641 | int pid; // 0 => unrestricted | |
2642 | string section; // empty => absolute address | |
2643 | Dwarf_Addr address; | |
2644 | // bool has_maxactive; | |
2645 | // long maxactive_val; | |
0973d815 | 2646 | |
6d0f3f0c FCE |
2647 | uprobe_derived_probe (const string& function, |
2648 | const string& filename, | |
2649 | int line, | |
2650 | const string& module, | |
2651 | int pid, | |
2652 | const string& section, | |
2653 | Dwarf_Addr dwfl_addr, | |
2654 | Dwarf_Addr addr, | |
2655 | dwarf_query & q, | |
2656 | Dwarf_Die* scope_die); | |
2657 | ||
0973d815 FCE |
2658 | // alternate constructor for process(PID).statement(ADDR).absolute |
2659 | uprobe_derived_probe (probe *base, | |
2660 | probe_point *location, | |
2661 | int pid, | |
2662 | Dwarf_Addr addr, | |
2663 | bool return_p); | |
2664 | ||
6d0f3f0c FCE |
2665 | void printsig (std::ostream &o) const; |
2666 | void join_group (systemtap_session& s); | |
2667 | }; | |
2668 | ||
2669 | ||
2670 | ||
dc38c0ae DS |
2671 | struct dwarf_derived_probe_group: public derived_probe_group |
2672 | { | |
2673 | private: | |
b20febf3 FCE |
2674 | multimap<string,dwarf_derived_probe*> probes_by_module; |
2675 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
2676 | |
2677 | public: | |
b20febf3 FCE |
2678 | void enroll (dwarf_derived_probe* probe); |
2679 | void emit_module_decls (systemtap_session& s); | |
2680 | void emit_module_init (systemtap_session& s); | |
2681 | void emit_module_exit (systemtap_session& s); | |
dc38c0ae DS |
2682 | }; |
2683 | ||
2684 | ||
20c6c071 | 2685 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 2686 | struct base_query |
20c6c071 | 2687 | { |
c4ce66a1 JS |
2688 | base_query(dwflpp & dw, literal_map_t const & params); |
2689 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 2690 | virtual ~base_query() {} |
bd2b1e68 | 2691 | |
5227f1ea | 2692 | systemtap_session & sess; |
2c384610 | 2693 | dwflpp & dw; |
5227f1ea | 2694 | |
bd2b1e68 | 2695 | // Parameter extractors. |
86bf665e | 2696 | static bool has_null_param(literal_map_t const & params, |
888af770 | 2697 | string const & k); |
86bf665e | 2698 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 2699 | string const & k, string & v); |
86bf665e | 2700 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 2701 | string const & k, long & v); |
86bf665e | 2702 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 2703 | string const & k, Dwarf_Addr & v); |
b55bc428 | 2704 | |
2c384610 DS |
2705 | // Extracted parameters. |
2706 | bool has_kernel; | |
91af0778 FCE |
2707 | bool has_module; |
2708 | bool has_process; | |
2c384610 DS |
2709 | string module_val; // has_kernel => module_val = "kernel" |
2710 | ||
2711 | virtual void handle_query_module() = 0; | |
2712 | }; | |
2713 | ||
2714 | ||
c4ce66a1 JS |
2715 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
2716 | sess(dw.sess), dw(dw) | |
2c384610 | 2717 | { |
91af0778 | 2718 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
2719 | if (has_kernel) |
2720 | module_val = "kernel"; | |
91af0778 FCE |
2721 | |
2722 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
2723 | if (has_module) | |
2724 | has_process = false; | |
4baf0e53 | 2725 | else |
d0a7f5a9 FCE |
2726 | { |
2727 | has_process = get_string_param(params, TOK_PROCESS, module_val); | |
06aca46a | 2728 | if (has_process) |
d0a7f5a9 FCE |
2729 | module_val = find_executable (module_val); |
2730 | } | |
91af0778 FCE |
2731 | |
2732 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
2733 | } |
2734 | ||
c4ce66a1 JS |
2735 | base_query::base_query(dwflpp & dw, const string & module_val) |
2736 | : sess(dw.sess), dw(dw), module_val(module_val) | |
2737 | { | |
2738 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
2739 | // which means that userspace modules won't get any PATH searching. | |
2740 | if (module_val.find('/') == string::npos) | |
2741 | { | |
2742 | has_kernel = (module_val == TOK_KERNEL); | |
2743 | has_module = !has_kernel; | |
2744 | has_process = false; | |
2745 | } | |
2746 | else | |
2747 | { | |
2748 | has_kernel = has_module = false; | |
2749 | has_process = true; | |
2750 | } | |
2751 | } | |
2752 | ||
2c384610 | 2753 | bool |
86bf665e | 2754 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
2755 | string const & k) |
2756 | { | |
888af770 | 2757 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
2758 | } |
2759 | ||
2760 | ||
2761 | bool | |
86bf665e | 2762 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
2763 | string const & k, string & v) |
2764 | { | |
2765 | return derived_probe_builder::get_param (params, k, v); | |
2766 | } | |
2767 | ||
2768 | ||
2769 | bool | |
86bf665e | 2770 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
2771 | string const & k, long & v) |
2772 | { | |
2773 | int64_t value; | |
2774 | bool present = derived_probe_builder::get_param (params, k, value); | |
2775 | v = (long) value; | |
2776 | return present; | |
2777 | } | |
2778 | ||
2779 | ||
2780 | bool | |
86bf665e | 2781 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
2782 | string const & k, Dwarf_Addr & v) |
2783 | { | |
2784 | int64_t value; | |
2785 | bool present = derived_probe_builder::get_param (params, k, value); | |
2786 | v = (Dwarf_Addr) value; | |
2787 | return present; | |
2788 | } | |
2789 | ||
2c384610 DS |
2790 | struct dwarf_query : public base_query |
2791 | { | |
2792 | dwarf_query(systemtap_session & sess, | |
2793 | probe * base_probe, | |
2794 | probe_point * base_loc, | |
2795 | dwflpp & dw, | |
86bf665e | 2796 | literal_map_t const & params, |
2c384610 DS |
2797 | vector<derived_probe *> & results); |
2798 | ||
c4ce66a1 JS |
2799 | vector<derived_probe *> & results; |
2800 | probe * base_probe; | |
2801 | probe_point * base_loc; | |
2802 | ||
2c384610 | 2803 | virtual void handle_query_module(); |
5f0a03a6 JK |
2804 | void query_module_dwarf(); |
2805 | void query_module_symtab(); | |
2c384610 | 2806 | |
2930abc7 FCE |
2807 | void add_probe_point(string const & funcname, |
2808 | char const * filename, | |
2809 | int line, | |
2810 | Dwarf_Die *scope_die, | |
2811 | Dwarf_Addr addr); | |
d64e82b1 | 2812 | string get_blacklist_section(Dwarf_Addr addr); |
20c6c071 | 2813 | |
a7301475 FCE |
2814 | regex_t blacklist_func; // function/statement probes |
2815 | regex_t blacklist_func_ret; // only for .return probes | |
2816 | regex_t blacklist_file; // file name | |
0daad364 JS |
2817 | void build_blacklist(); |
2818 | ||
b20febf3 FCE |
2819 | bool blacklisted_p(const string& funcname, |
2820 | const string& filename, | |
36f9dd1d | 2821 | int line, |
b20febf3 FCE |
2822 | const string& module, |
2823 | const string& section, | |
36f9dd1d FCE |
2824 | Dwarf_Addr addr); |
2825 | ||
2930abc7 | 2826 | // Extracted parameters. |
7a053d3b | 2827 | string function_val; |
20c6c071 GH |
2828 | |
2829 | bool has_function_str; | |
2830 | bool has_statement_str; | |
2831 | bool has_function_num; | |
2832 | bool has_statement_num; | |
7a053d3b RM |
2833 | string statement_str_val; |
2834 | string function_str_val; | |
c239d28c GH |
2835 | Dwarf_Addr statement_num_val; |
2836 | Dwarf_Addr function_num_val; | |
20c6c071 | 2837 | |
b8da0ad1 FCE |
2838 | bool has_call; |
2839 | bool has_inline; | |
20c6c071 GH |
2840 | bool has_return; |
2841 | ||
c9bad430 DS |
2842 | bool has_maxactive; |
2843 | long maxactive_val; | |
2844 | ||
20c6c071 GH |
2845 | bool has_label; |
2846 | string label_val; | |
2847 | ||
2848 | bool has_relative; | |
2849 | long relative_val; | |
2850 | ||
37ebca01 FCE |
2851 | bool has_absolute; |
2852 | ||
5f0a03a6 JK |
2853 | enum dbinfo_reqt dbinfo_reqt; |
2854 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
2855 | ||
20c6c071 GH |
2856 | function_spec_type parse_function_spec(string & spec); |
2857 | function_spec_type spec_type; | |
2858 | string function; | |
2859 | string file; | |
0c8b7d37 | 2860 | line_t line_type; |
879eb9e9 | 2861 | int line[2]; |
5f0a03a6 | 2862 | bool query_done; // Found exact match |
20c6c071 | 2863 | |
7e1279ea FCE |
2864 | set<char const *> filtered_srcfiles; |
2865 | ||
2866 | // Map official entrypc -> func_info object | |
86bf665e TM |
2867 | inline_instance_map_t filtered_inlines; |
2868 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
2869 | bool choose_next_line; |
2870 | Dwarf_Addr entrypc_for_next_line; | |
b55bc428 FCE |
2871 | }; |
2872 | ||
98afd80e | 2873 | |
fedd4090 FCE |
2874 | // This little test routine represents an unfortunate breakdown in |
2875 | // abstraction between dwflpp (putatively, a layer right on top of | |
2876 | // elfutils), and dwarf_query (interpreting a systemtap probe point). | |
2877 | // It arises because we sometimes try to fix up slightly-off | |
2878 | // .statement() probes (something we find out in fairly low-level). | |
2879 | // | |
2cb3fe26 | 2880 | // An alternative would be to put some more intelligence into query_cu(), |
4baf0e53 | 2881 | // and have it print additional suggestions after finding that |
fedd4090 FCE |
2882 | // q->dw.iterate_over_srcfile_lines resulted in no new finished_results. |
2883 | ||
2884 | bool | |
2885 | dwflpp::has_single_line_record (dwarf_query * q, char const * srcfile, int lineno) | |
2886 | { | |
2887 | if (lineno < 0) | |
2888 | return false; | |
2889 | ||
2890 | Dwarf_Line **srcsp = NULL; | |
2891 | size_t nsrcs = 0; | |
2892 | ||
2893 | dwarf_assert ("dwarf_getsrc_file", | |
2894 | dwarf_getsrc_file (module_dwarf, | |
2895 | srcfile, lineno, 0, | |
2896 | &srcsp, &nsrcs)); | |
2897 | ||
4baf0e53 | 2898 | if (nsrcs != 1) |
fedd4090 FCE |
2899 | { |
2900 | if (sess.verbose>4) | |
2901 | clog << "alternative line " << lineno << " rejected: nsrcs=" << nsrcs << endl; | |
2902 | return false; | |
2903 | } | |
2904 | ||
2905 | // We also try to filter out lines that leave the selected | |
2906 | // functions (if any). | |
2907 | ||
86bf665e TM |
2908 | dwarf_line_t line(srcsp[0]); |
2909 | Dwarf_Addr addr = line.addr(); | |
fedd4090 | 2910 | |
86bf665e | 2911 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
fedd4090 FCE |
2912 | i != q->filtered_functions.end(); ++i) |
2913 | { | |
3e961ba6 | 2914 | if (q->dw.die_has_pc (i->die, addr)) |
fedd4090 FCE |
2915 | { |
2916 | if (q->sess.verbose>4) | |
3e961ba6 | 2917 | clog << "alternative line " << lineno << " accepted: fn=" << i->name << endl; |
fedd4090 FCE |
2918 | return true; |
2919 | } | |
2920 | } | |
2921 | ||
86bf665e | 2922 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); |
fedd4090 FCE |
2923 | i != q->filtered_inlines.end(); ++i) |
2924 | { | |
3e961ba6 | 2925 | if (q->dw.die_has_pc (i->die, addr)) |
fedd4090 FCE |
2926 | { |
2927 | if (sess.verbose>4) | |
3e961ba6 | 2928 | clog << "alternative line " << lineno << " accepted: ifn=" << i->name << endl; |
fedd4090 FCE |
2929 | return true; |
2930 | } | |
2931 | } | |
2932 | ||
2933 | if (sess.verbose>4) | |
2934 | clog << "alternative line " << lineno << " rejected: leaves selected fns" << endl; | |
2935 | return false; | |
2936 | } | |
2937 | ||
0b8f6579 JB |
2938 | /* This basically only goes one level down from the compile unit so it |
2939 | * only picks up top level stuff (i.e. nothing in a lower scope) */ | |
2940 | int | |
2941 | dwflpp::iterate_over_globals (int (* callback)(Dwarf_Die *, void *), | |
2942 | void * data) | |
2943 | { | |
2944 | int rc = DWARF_CB_OK; | |
2945 | Dwarf_Die die; | |
2946 | ||
2947 | assert (module); | |
2948 | assert (cu); | |
2949 | assert (dwarf_tag(cu) == DW_TAG_compile_unit); | |
2950 | ||
2951 | if (dwarf_child(cu, &die) != 0) | |
2952 | return rc; | |
2953 | ||
2954 | do { | |
2955 | /* We're only currently looking for structures and unions, | |
2956 | * although other types of declarations exist */ | |
2957 | if (dwarf_tag(&die) != DW_TAG_structure_type && | |
2958 | dwarf_tag(&die) != DW_TAG_union_type) | |
2959 | continue; | |
2960 | ||
2961 | rc = (*callback)(&die, data); | |
2962 | if (rc != DWARF_CB_OK) | |
2963 | break; | |
2964 | ||
2965 | } while (dwarf_siblingof(&die, &die) == 0); | |
2966 | ||
2967 | return rc; | |
2968 | } | |
fedd4090 | 2969 | |
6561773f | 2970 | int |
2da9cedb JS |
2971 | dwflpp::iterate_over_functions (int (* callback)(Dwarf_Die * func, base_query * q), |
2972 | base_query * q, const string& function, | |
2973 | bool has_statement_num) | |
6561773f FCE |
2974 | { |
2975 | int rc = DWARF_CB_OK; | |
2976 | assert (module); | |
2977 | assert (cu); | |
41c262f3 | 2978 | |
6561773f FCE |
2979 | string key = module_name + ":" + cu_name; |
2980 | cu_function_cache_t *v = cu_function_cache[key]; | |
2981 | if (v == 0) | |
2982 | { | |
2983 | v = new cu_function_cache_t; | |
2984 | cu_function_cache[key] = v; | |
2985 | dwarf_getfuncs (cu, cu_function_caching_callback, v, 0); | |
2986 | if (q->sess.verbose > 4) | |
2987 | clog << "function cache " << key << " size " << v->size() << endl; | |
2988 | } | |
41c262f3 | 2989 | |
2da9cedb | 2990 | string subkey = function; |
6561773f FCE |
2991 | if (v->find(subkey) != v->end()) |
2992 | { | |
2171f774 | 2993 | Dwarf_Die die = v->find(subkey)->second; |
6561773f FCE |
2994 | if (q->sess.verbose > 4) |
2995 | clog << "function cache " << key << " hit " << subkey << endl; | |
2da9cedb | 2996 | return (*callback)(& die, q); |
6561773f FCE |
2997 | } |
2998 | else if (name_has_wildcard (subkey)) | |
2999 | { | |
3000 | for (cu_function_cache_t::iterator it = v->begin(); it != v->end(); it++) | |
3001 | { | |
f76427a2 | 3002 | if (pending_interrupts) return DWARF_CB_ABORT; |
6561773f FCE |
3003 | string func_name = it->first; |
3004 | Dwarf_Die die = it->second; | |
3005 | if (function_name_matches_pattern (func_name, subkey)) | |
3006 | { | |
3007 | if (q->sess.verbose > 4) | |
3008 | clog << "function cache " << key << " match " << func_name << " vs " << subkey << endl; | |
41c262f3 | 3009 | |
2da9cedb | 3010 | rc = (*callback)(& die, q); |
6561773f FCE |
3011 | if (rc != DWARF_CB_OK) break; |
3012 | } | |
3013 | } | |
3014 | } | |
2da9cedb | 3015 | else if (has_statement_num) // searching all for kernel.statement |
cf314c0f WH |
3016 | { |
3017 | for (cu_function_cache_t::iterator it = v->begin(); it != v->end(); it++) | |
3018 | { | |
3019 | Dwarf_Die die = it->second; | |
2da9cedb | 3020 | rc = (*callback)(& die, q); |
cf314c0f WH |
3021 | if (rc != DWARF_CB_OK) break; |
3022 | } | |
3023 | } | |
6561773f FCE |
3024 | else // not a wildcard and no match in this CU |
3025 | { | |
3026 | // do nothing | |
3027 | } | |
3028 | return rc; | |
3029 | } | |
3030 | ||
3031 | ||
fedd4090 | 3032 | |
98afd80e | 3033 | struct dwarf_builder: public derived_probe_builder |
b55bc428 | 3034 | { |
e38d6504 | 3035 | dwflpp *kern_dw; |
7a24d422 | 3036 | map <string,dwflpp*> user_dw; |
b8da0ad1 | 3037 | dwarf_builder(): kern_dw(0) {} |
aa30ccd3 | 3038 | |
7a24d422 FCE |
3039 | |
3040 | /* NB: not virtual, so can be called from dtor too: */ | |
06aca46a | 3041 | void dwarf_build_no_more (bool verbose) |
aa30ccd3 FCE |
3042 | { |
3043 | if (kern_dw) | |
3044 | { | |
7a24d422 FCE |
3045 | if (verbose) |
3046 | clog << "dwarf_builder releasing kernel dwflpp" << endl; | |
aa30ccd3 FCE |
3047 | delete kern_dw; |
3048 | kern_dw = 0; | |
3049 | } | |
7a24d422 FCE |
3050 | |
3051 | for (map<string,dwflpp*>::iterator udi = user_dw.begin(); | |
3052 | udi != user_dw.end(); | |
3053 | udi ++) | |
3054 | { | |
3055 | if (verbose) | |
3056 | clog << "dwarf_builder releasing user dwflpp " << udi->first << endl; | |
3057 | delete udi->second; | |
3058 | } | |
3059 | user_dw.erase (user_dw.begin(), user_dw.end()); | |
3060 | } | |
3061 | ||
3062 | void build_no_more (systemtap_session &s) | |
3063 | { | |
3064 | dwarf_build_no_more (s.verbose > 3); | |
aa30ccd3 FCE |
3065 | } |
3066 | ||
e38d6504 RM |
3067 | ~dwarf_builder() |
3068 | { | |
7a24d422 | 3069 | dwarf_build_no_more (false); |
c8959a29 | 3070 | } |
aa30ccd3 | 3071 | |
5227f1ea | 3072 | virtual void build(systemtap_session & sess, |
7a053d3b | 3073 | probe * base, |
20c6c071 | 3074 | probe_point * location, |
86bf665e | 3075 | literal_map_t const & parameters, |
20c6c071 | 3076 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
3077 | }; |
3078 | ||
888af770 | 3079 | |
5227f1ea GH |
3080 | dwarf_query::dwarf_query(systemtap_session & sess, |
3081 | probe * base_probe, | |
20c6c071 GH |
3082 | probe_point * base_loc, |
3083 | dwflpp & dw, | |
86bf665e | 3084 | literal_map_t const & params, |
20c6c071 | 3085 | vector<derived_probe *> & results) |
c4ce66a1 JS |
3086 | : base_query(dw, params), results(results), |
3087 | base_probe(base_probe), base_loc(base_loc) | |
bd2b1e68 GH |
3088 | { |
3089 | // Reduce the query to more reasonable semantic values (booleans, | |
3090 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
3091 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
3092 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
3093 | ||
3094 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
3095 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
3096 | ||
0f336e95 SC |
3097 | has_label = get_string_param(params, TOK_LABEL, label_val); |
3098 | ||
b8da0ad1 FCE |
3099 | has_call = has_null_param(params, TOK_CALL); |
3100 | has_inline = has_null_param(params, TOK_INLINE); | |
bd2b1e68 | 3101 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 3102 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 FCE |
3103 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
3104 | ||
bd2b1e68 GH |
3105 | if (has_function_str) |
3106 | spec_type = parse_function_spec(function_str_val); | |
3107 | else if (has_statement_str) | |
3108 | spec_type = parse_function_spec(statement_str_val); | |
0daad364 | 3109 | |
b8da0ad1 | 3110 | build_blacklist(); // XXX: why not reuse amongst dwarf_query instances? |
5f0a03a6 JK |
3111 | dbinfo_reqt = assess_dbinfo_reqt(); |
3112 | query_done = false; | |
0daad364 JS |
3113 | } |
3114 | ||
3115 | ||
2c384610 | 3116 | void |
5f0a03a6 | 3117 | dwarf_query::query_module_dwarf() |
2c384610 DS |
3118 | { |
3119 | if (has_function_num || has_statement_num) | |
3120 | { | |
3121 | // If we have module("foo").function(0xbeef) or | |
3122 | // module("foo").statement(0xbeef), the address is relative | |
3123 | // to the start of the module, so we seek the function | |
3124 | // number plus the module's bias. | |
3125 | ||
3126 | Dwarf_Addr addr; | |
3127 | if (has_function_num) | |
3128 | addr = function_num_val; | |
3129 | else | |
3130 | addr = statement_num_val; | |
4baf0e53 | 3131 | |
2c384610 DS |
3132 | // NB: we don't need to add the module base address or bias |
3133 | // value here (for reasons that may be coincidental). | |
3134 | dw.query_cu_containing_module_address(addr, this); | |
3135 | } | |
3136 | else | |
3137 | { | |
3138 | // Otherwise if we have a function("foo") or statement("foo") | |
3139 | // specifier, we have to scan over all the CUs looking for | |
3140 | // the function(s) in question | |
3141 | assert(has_function_str || has_statement_str); | |
3142 | dw.iterate_over_cus(&query_cu, this); | |
3143 | } | |
3144 | } | |
3145 | ||
5f0a03a6 JK |
3146 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
3147 | dwarf_query * q); | |
3148 | ||
3149 | void | |
3150 | dwarf_query::query_module_symtab() | |
3151 | { | |
3152 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
3153 | if (dbinfo_reqt == dbr_need_dwarf) | |
3154 | return; | |
3155 | ||
3156 | module_info *mi = dw.mod_info; | |
3157 | if (dbinfo_reqt == dbr_need_symtab) | |
3158 | { | |
3159 | if (mi->symtab_status == info_unknown) | |
3160 | mi->get_symtab(this); | |
3161 | if (mi->symtab_status == info_absent) | |
3162 | return; | |
3163 | } | |
3164 | ||
3165 | func_info *fi = NULL; | |
3166 | symbol_table *sym_table = mi->sym_table; | |
3167 | ||
3168 | if (has_function_str) | |
3169 | { | |
3170 | // Per dwarf_query::assess_dbinfo_reqt()... | |
3171 | assert(spec_type == function_alone); | |
3172 | if (dw.name_has_wildcard(function_str_val)) | |
3173 | { | |
3174 | // Until we augment the blacklist sufficently... | |
3175 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
3176 | { | |
3177 | // e.g., kernel.function("*") | |
3178 | cerr << "Error: Pattern '" | |
3179 | << function_str_val | |
3180 | << "' matches every instruction address in the symbol table," | |
3181 | << endl | |
3182 | << "some of which aren't even functions." | |
3183 | << " Please be more precise." | |
3184 | << endl; | |
3185 | return; | |
3186 | } | |
2e67a43b TM |
3187 | symbol_table::iterator_t iter; |
3188 | for (iter = sym_table->list_by_addr.begin(); | |
3189 | iter != sym_table->list_by_addr.end(); | |
3190 | ++iter) | |
5f0a03a6 | 3191 | { |
2e67a43b | 3192 | fi = *iter; |
5f0a03a6 JK |
3193 | if (!null_die(&fi->die)) |
3194 | continue; // already handled in query_module_dwarf() | |
3195 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
3196 | query_func_info(fi->addr, *fi, this); | |
3197 | } | |
3198 | } | |
3199 | else | |
3200 | { | |
3201 | fi = sym_table->lookup_symbol(function_str_val); | |
3202 | if (fi && null_die(&fi->die)) | |
3203 | query_func_info(fi->addr, *fi, this); | |
3204 | } | |
3205 | } | |
3206 | else | |
3207 | { | |
3208 | assert(has_function_num || has_statement_num); | |
3209 | // Find the "function" in which the indicated address resides. | |
3210 | Dwarf_Addr addr = | |
3211 | (has_function_num ? function_num_val : statement_num_val); | |
3212 | fi = sym_table->get_func_containing_address(addr); | |
3213 | if (!fi) | |
3214 | { | |
3215 | cerr << "Warning: address " | |
3216 | << hex << addr << dec | |
3217 | << " out of range for module " | |
3218 | << dw.module_name; | |
3219 | return; | |
3220 | } | |
3221 | if (!null_die(&fi->die)) | |
3222 | { | |
3223 | // addr looks like it's in the compilation unit containing | |
3224 | // the indicated function, but query_module_dwarf() didn't | |
3225 | // match addr to any compilation unit, so addr must be | |
3226 | // above that cu's address range. | |
3227 | cerr << "Warning: address " | |
3228 | << hex << addr << dec | |
3229 | << " maps to no known compilation unit in module " | |
3230 | << dw.module_name; | |
3231 | return; | |
3232 | } | |
3233 | query_func_info(fi->addr, *fi, this); | |
3234 | } | |
3235 | } | |
3236 | ||
3237 | void | |
3238 | dwarf_query::handle_query_module() | |
3239 | { | |
3240 | dw.get_module_dwarf(false, | |
3241 | (dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab)); | |
3242 | if (dw.mod_info->dwarf_status == info_present) | |
3243 | query_module_dwarf(); | |
3244 | // Consult the symbol table if we haven't found all we're looking for. | |
3245 | // asm functions can show up in the symbol table but not in dwarf. | |
3246 | if (sess.consult_symtab && !query_done) | |
3247 | query_module_symtab(); | |
3248 | } | |
3249 | ||
2c384610 | 3250 | |
0daad364 JS |
3251 | void |
3252 | dwarf_query::build_blacklist() | |
3253 | { | |
91af0778 FCE |
3254 | // No blacklist for userspace. |
3255 | if (has_process) | |
3256 | return; | |
3257 | ||
a7301475 FCE |
3258 | // We build up the regexps in these strings |
3259 | ||
3260 | // Add ^ anchors at the front; $ will be added just before regcomp. | |
3261 | ||
a7301475 FCE |
3262 | string blfn = "^("; |
3263 | string blfn_ret = "^("; | |
3264 | string blfile = "^("; | |
3265 | ||
e4c58386 | 3266 | blfile += "kernel/kprobes.c"; // first alternative, no "|" |
a7301475 | 3267 | blfile += "|arch/.*/kernel/kprobes.c"; |
275a898f | 3268 | // Older kernels need ... |
02a929d1 | 3269 | blfile += "|include/asm/io.h"; |
275a898f AM |
3270 | blfile += "|include/asm/bitops.h"; |
3271 | // While newer ones need ... | |
3272 | blfile += "|arch/.*/include/asm/io.h"; | |
3273 | blfile += "|arch/.*/include/asm/bitops.h"; | |
02a929d1 | 3274 | blfile += "|drivers/ide/ide-iops.c"; |
a7301475 FCE |
3275 | |
3276 | // XXX: it would be nice if these blacklisted functions were pulled | |
3277 | // in dynamically, instead of being statically defined here. | |
49f426d9 FCE |
3278 | // Perhaps it could be populated from script files. A "noprobe |
3279 | // kernel.function("...")" construct might do the trick. | |
0daad364 | 3280 | |
b20febf3 | 3281 | // Most of these are marked __kprobes in newer kernels. We list |
a7301475 FCE |
3282 | // them here (anyway) so the translator can block them on older |
3283 | // kernels that don't have the __kprobes function decorator. This | |
3284 | // also allows detection of problems at translate- rather than | |
3285 | // run-time. | |
3286 | ||
e4c58386 | 3287 | blfn += "atomic_notifier_call_chain"; // first blfn; no "|" |
a7301475 FCE |
3288 | blfn += "|default_do_nmi"; |
3289 | blfn += "|__die"; | |
3290 | blfn += "|die_nmi"; | |
3291 | blfn += "|do_debug"; | |
3292 | blfn += "|do_general_protection"; | |
3293 | blfn += "|do_int3"; | |
3294 | blfn += "|do_IRQ"; | |
3295 | blfn += "|do_page_fault"; | |
3296 | blfn += "|do_sparc64_fault"; | |
3297 | blfn += "|do_trap"; | |
3298 | blfn += "|dummy_nmi_callback"; | |
3299 | blfn += "|flush_icache_range"; | |
3300 | blfn += "|ia64_bad_break"; | |
3301 | blfn += "|ia64_do_page_fault"; | |
3302 | blfn += "|ia64_fault"; | |
3303 | blfn += "|io_check_error"; | |
3304 | blfn += "|mem_parity_error"; | |
3305 | blfn += "|nmi_watchdog_tick"; | |
3306 | blfn += "|notifier_call_chain"; | |
3307 | blfn += "|oops_begin"; | |
3308 | blfn += "|oops_end"; | |
3309 | blfn += "|program_check_exception"; | |
3310 | blfn += "|single_step_exception"; | |
3311 | blfn += "|sync_regs"; | |
3312 | blfn += "|unhandled_fault"; | |
3313 | blfn += "|unknown_nmi_error"; | |
3314 | ||
3315 | // Lots of locks | |
3316 | blfn += "|.*raw_.*lock.*"; | |
3317 | blfn += "|.*read_.*lock.*"; | |
3318 | blfn += "|.*write_.*lock.*"; | |
3319 | blfn += "|.*spin_.*lock.*"; | |
3320 | blfn += "|.*rwlock_.*lock.*"; | |
3321 | blfn += "|.*rwsem_.*lock.*"; | |
3322 | blfn += "|.*mutex_.*lock.*"; | |
3323 | blfn += "|raw_.*"; | |
3324 | blfn += "|.*seq_.*lock.*"; | |
3325 | ||
a11f4bae | 3326 | // atomic functions |
2ca12712 JS |
3327 | blfn += "|atomic_.*"; |
3328 | blfn += "|atomic64_.*"; | |
a11f4bae SD |
3329 | |
3330 | // few other problematic cases | |
3331 | blfn += "|get_bh"; | |
3332 | blfn += "|put_bh"; | |
3333 | ||
a7301475 FCE |
3334 | // Experimental |
3335 | blfn += "|.*apic.*|.*APIC.*"; | |
3336 | blfn += "|.*softirq.*"; | |
3337 | blfn += "|.*IRQ.*"; | |
3338 | blfn += "|.*_intr.*"; | |
3339 | blfn += "|__delay"; | |
3340 | blfn += "|.*kernel_text.*"; | |
3341 | blfn += "|get_current"; | |
3342 | blfn += "|current_.*"; | |
3343 | blfn += "|.*exception_tables.*"; | |
3344 | blfn += "|.*setup_rt_frame.*"; | |
c931ec8a | 3345 | |
a8c9be6f | 3346 | // PR 5759, CONFIG_PREEMPT kernels |
a7301475 FCE |
3347 | blfn += "|.*preempt_count.*"; |
3348 | blfn += "|preempt_schedule"; | |
a8c9be6f | 3349 | |
e4c58386 FCE |
3350 | // These functions don't return, so return probes would never be recovered |
3351 | blfn_ret += "do_exit"; // no "|" | |
3352 | blfn_ret += "|sys_exit"; | |
3353 | blfn_ret += "|sys_exit_group"; | |
3354 | ||
a721fbcf MH |
3355 | // __switch_to changes "current" on x86_64 and i686, so return probes |
3356 | // would cause kernel panic, and it is marked as "__kprobes" on x86_64 | |
0daad364 | 3357 | if (sess.architecture == "x86_64") |
a7301475 | 3358 | blfn += "|__switch_to"; |
a721fbcf | 3359 | if (sess.architecture == "i686") |
a7301475 | 3360 | blfn_ret += "|__switch_to"; |
0daad364 | 3361 | |
a7301475 FCE |
3362 | blfn += ")$"; |
3363 | blfn_ret += ")$"; | |
3364 | blfile += ")$"; | |
3365 | ||
41c262f3 | 3366 | if (sess.verbose > 2) |
e4c58386 FCE |
3367 | { |
3368 | clog << "blacklist regexps:" << endl; | |
3369 | clog << "blfn: " << blfn << endl; | |
3370 | clog << "blfn_ret: " << blfn_ret << endl; | |
3371 | clog << "blfile: " << blfile << endl; | |
3372 | } | |
3373 | ||
a7301475 FCE |
3374 | int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED); |
3375 | if (rc) throw semantic_error ("blacklist_func regcomp failed"); | |
3376 | rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED); | |
3377 | if (rc) throw semantic_error ("blacklist_func_ret regcomp failed"); | |
3378 | rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED); | |
3379 | if (rc) throw semantic_error ("blacklist_file regcomp failed"); | |
7a053d3b | 3380 | } |
bd2b1e68 GH |
3381 | |
3382 | ||
bd2b1e68 | 3383 | function_spec_type |
20c6c071 | 3384 | dwarf_query::parse_function_spec(string & spec) |
bd2b1e68 GH |
3385 | { |
3386 | string::const_iterator i = spec.begin(), e = spec.end(); | |
7a053d3b | 3387 | |
bd2b1e68 GH |
3388 | function.clear(); |
3389 | file.clear(); | |
879eb9e9 SC |
3390 | line[0] = 0; |
3391 | line[1] = 0; | |
bd2b1e68 GH |
3392 | |
3393 | while (i != e && *i != '@') | |
3394 | { | |
0c8b7d37 | 3395 | if (*i == ':' || *i == '+') |
bd2b1e68 GH |
3396 | goto bad; |
3397 | function += *i++; | |
3398 | } | |
3399 | ||
3400 | if (i == e) | |
3401 | { | |
b0ee93c4 | 3402 | if (sess.verbose>2) |
7a053d3b RM |
3403 | clog << "parsed '" << spec |
3404 | << "' -> func '" << function | |
db22e55f | 3405 | << "'\n"; |
bd2b1e68 GH |
3406 | return function_alone; |
3407 | } | |
3408 | ||
3409 | if (i++ == e) | |
3410 | goto bad; | |
3411 | ||
0c8b7d37 | 3412 | while (i != e && *i != ':' && *i != '+') |
bd2b1e68 | 3413 | file += *i++; |
41c262f3 | 3414 | if (*i == ':') |
879eb9e9 SC |
3415 | { |
3416 | if (*(i + 1) == '*') | |
3417 | line_type = WILDCARD; | |
3418 | else | |
3419 | line_type = ABSOLUTE; | |
3420 | } | |
0c8b7d37 SC |
3421 | else if (*i == '+') |
3422 | line_type = RELATIVE; | |
7a053d3b | 3423 | |
bd2b1e68 GH |
3424 | if (i == e) |
3425 | { | |
b0ee93c4 | 3426 | if (sess.verbose>2) |
7a053d3b RM |
3427 | clog << "parsed '" << spec |
3428 | << "' -> func '"<< function | |
3429 | << "', file '" << file | |
db22e55f | 3430 | << "'\n"; |
bd2b1e68 GH |
3431 | return function_and_file; |
3432 | } | |
3433 | ||
3434 | if (i++ == e) | |
3435 | goto bad; | |
3436 | ||
3437 | try | |
3438 | { | |
879eb9e9 SC |
3439 | if (line_type != WILDCARD) |
3440 | { | |
3441 | string::const_iterator dash = i; | |
41c262f3 | 3442 | |
879eb9e9 SC |
3443 | while (dash != e && *dash != '-') |
3444 | dash++; | |
3445 | if (dash == e) | |
3446 | line[0] = line[1] = lex_cast<int>(string(i, e)); | |
3447 | else | |
3448 | { | |
3449 | line_type = RANGE; | |
3450 | line[0] = lex_cast<int>(string(i, dash)); | |
3451 | line[1] = lex_cast<int>(string(dash + 1, e)); | |
3452 | } | |
3453 | } | |
41c262f3 | 3454 | |
b0ee93c4 | 3455 | if (sess.verbose>2) |
7a053d3b RM |
3456 | clog << "parsed '" << spec |
3457 | << "' -> func '"<< function | |
3458 | << "', file '" << file | |
db22e55f | 3459 | << "', line " << line << "\n"; |
bd2b1e68 GH |
3460 | return function_file_and_line; |
3461 | } | |
3462 | catch (runtime_error & exn) | |
3463 | { | |
3464 | goto bad; | |
3465 | } | |
3466 | ||
3467 | bad: | |
7a053d3b | 3468 | throw semantic_error("malformed specification '" + spec + "'", |
20c6c071 | 3469 | base_probe->tok); |
bd2b1e68 GH |
3470 | } |
3471 | ||
3472 | ||
91af0778 | 3473 | #if 0 |
b20febf3 | 3474 | // Forward declaration. |
91af0778 | 3475 | static int query_kernel_module (Dwfl_Module *, void **, const char *, |
b20febf3 | 3476 | Dwarf_Addr, void *); |
91af0778 | 3477 | #endif |
7e1279ea | 3478 | |
2930abc7 | 3479 | |
b8da0ad1 | 3480 | // XXX: pull this into dwflpp |
b20febf3 FCE |
3481 | static bool |
3482 | in_kprobes_function(systemtap_session& sess, Dwarf_Addr addr) | |
2930abc7 | 3483 | { |
84048984 | 3484 | if (sess.sym_kprobes_text_start != 0 && sess.sym_kprobes_text_end != 0) |
1d3a40b6 DS |
3485 | { |
3486 | // If the probe point address is anywhere in the __kprobes | |
3487 | // address range, we can't use this probe point. | |
84048984 | 3488 | if (addr >= sess.sym_kprobes_text_start && addr < sess.sym_kprobes_text_end) |
1d3a40b6 DS |
3489 | return true; |
3490 | } | |
3491 | return false; | |
3492 | } | |
3493 | ||
20e4a32c | 3494 | |
36f9dd1d | 3495 | bool |
b20febf3 FCE |
3496 | dwarf_query::blacklisted_p(const string& funcname, |
3497 | const string& filename, | |
78f6bba6 | 3498 | int, |
b20febf3 FCE |
3499 | const string& module, |
3500 | const string& section, | |
36f9dd1d FCE |
3501 | Dwarf_Addr addr) |
3502 | { | |
91af0778 FCE |
3503 | if (has_process) |
3504 | return false; // no blacklist for userspace | |
3505 | ||
b20febf3 | 3506 | if (section.substr(0, 6) == string(".init.") || |
f90f9261 SD |
3507 | section.substr(0, 6) == string(".exit.") || |
3508 | section.substr(0, 9) == string(".devinit.") || | |
3509 | section.substr(0, 9) == string(".devexit.") || | |
3510 | section.substr(0, 9) == string(".cpuinit.") || | |
3511 | section.substr(0, 9) == string(".cpuexit.") || | |
3512 | section.substr(0, 9) == string(".meminit.") || | |
3513 | section.substr(0, 9) == string(".memexit.")) | |
703621ae | 3514 | { |
b8da0ad1 FCE |
3515 | // NB: module .exit. routines could be probed in theory: |
3516 | // if the exit handler in "struct module" is diverted, | |
3517 | // first inserting the kprobes | |
3518 | // then allowing the exit code to run | |
3519 | // then removing these kprobes | |
b20febf3 FCE |
3520 | if (sess.verbose>1) |
3521 | clog << " skipping - init/exit"; | |
3522 | return true; | |
703621ae FCE |
3523 | } |
3524 | ||
1d3a40b6 | 3525 | // Check for function marked '__kprobes'. |
b20febf3 | 3526 | if (module == TOK_KERNEL && in_kprobes_function(sess, addr)) |
1d3a40b6 DS |
3527 | { |
3528 | if (sess.verbose>1) | |
b20febf3 | 3529 | clog << " skipping - __kprobes"; |
1d3a40b6 DS |
3530 | return true; |
3531 | } | |
4baf0e53 | 3532 | |
a7301475 FCE |
3533 | // Check probe point against blacklist. |
3534 | int goodfn = regexec (&blacklist_func, funcname.c_str(), 0, NULL, 0); | |
3535 | if (has_return) | |
3536 | goodfn = goodfn && regexec (&blacklist_func_ret, funcname.c_str(), 0, NULL, 0); | |
3537 | int goodfile = regexec (&blacklist_file, filename.c_str(), 0, NULL, 0); | |
3538 | ||
3539 | if (! (goodfn && goodfile)) | |
36f9dd1d | 3540 | { |
e2ae0696 LR |
3541 | if (sess.guru_mode) |
3542 | { | |
3543 | if (sess.verbose>1) | |
3544 | clog << " guru mode enabled - ignoring blacklist"; | |
3545 | } | |
3546 | else | |
3547 | { | |
3548 | if (sess.verbose>1) | |
3549 | clog << " skipping - blacklisted"; | |
3550 | return true; | |
3551 | } | |
36f9dd1d FCE |
3552 | } |
3553 | ||
3554 | // This probe point is not blacklisted. | |
3555 | return false; | |
3556 | } | |
3557 | ||
d64e82b1 SD |
3558 | string dwarf_query::get_blacklist_section(Dwarf_Addr addr) |
3559 | { | |
d64e82b1 | 3560 | string blacklist_section; |
f9331b29 RM |
3561 | Dwarf_Addr bias; |
3562 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
3563 | // because dwfl_module_getelf can force costly section relocations | |
3564 | // we don't really need, while either will do for this purpose. | |
3565 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias)) | |
3566 | ?: dwfl_module_getelf (dw.module, &bias)); | |
3567 | ||
3568 | Dwarf_Addr offset = addr - bias; | |
d64e82b1 SD |
3569 | if (elf) |
3570 | { | |
3571 | Elf_Scn* scn = 0; | |
3572 | size_t shstrndx; | |
86bf665e | 3573 | dwfl_assert ("getshstrndx", elf_getshstrndx (elf, &shstrndx)); |
d64e82b1 SD |
3574 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
3575 | { | |
3576 | GElf_Shdr shdr_mem; | |
3577 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); | |
3578 | if (! shdr) continue; // XXX error? | |
3579 | ||
f9331b29 RM |
3580 | if (!(shdr->sh_flags & SHF_ALLOC)) |
3581 | continue; | |
3582 | ||
d64e82b1 SD |
3583 | GElf_Addr start = shdr->sh_addr; |
3584 | GElf_Addr end = start + shdr->sh_size; | |
3585 | if (! (offset >= start && offset < end)) | |
3586 | continue; | |
3587 | ||
3588 | blacklist_section = elf_strptr (elf, shstrndx, shdr->sh_name); | |
3589 | break; | |
3590 | } | |
3591 | } | |
3592 | return blacklist_section; | |
3593 | } | |
36f9dd1d | 3594 | |
b20febf3 | 3595 | |
36f9dd1d | 3596 | void |
b20febf3 FCE |
3597 | dwarf_query::add_probe_point(const string& funcname, |
3598 | const char* filename, | |
36f9dd1d | 3599 | int line, |
b20febf3 | 3600 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
3601 | Dwarf_Addr addr) |
3602 | { | |
b20febf3 FCE |
3603 | string reloc_section; // base section for relocation purposes |
3604 | Dwarf_Addr reloc_addr = addr; // relocated | |
3605 | string blacklist_section; // linking section for blacklist purposes | |
3606 | const string& module = dw.module_name; // "kernel" or other | |
36f9dd1d | 3607 | |
37ebca01 FCE |
3608 | assert (! has_absolute); // already handled in dwarf_builder::build() |
3609 | ||
5f0a03a6 JK |
3610 | if (!dw.module) |
3611 | { | |
3612 | assert(module == TOK_KERNEL); | |
3613 | reloc_section = ""; | |
3614 | blacklist_section = ""; | |
3615 | } | |
3616 | else if (dwfl_module_relocations (dw.module) > 0) | |
2930abc7 | 3617 | { |
17c128f2 | 3618 | // This is a relocatable module; libdwfl already knows its |
b20febf3 FCE |
3619 | // sections, so we can relativize addr. |
3620 | int idx = dwfl_module_relocate_address (dw.module, &reloc_addr); | |
3621 | const char* r_s = dwfl_module_relocation_info (dw.module, idx, NULL); | |
3622 | if (r_s) | |
3623 | reloc_section = r_s; | |
3624 | blacklist_section = reloc_section; | |
d64e82b1 SD |
3625 | |
3626 | if(reloc_section == "" && dwfl_module_relocations (dw.module) == 1) | |
17c128f2 FCE |
3627 | { |
3628 | blacklist_section = this->get_blacklist_section(addr); | |
3629 | reloc_section = ".dynamic"; | |
4b0eb118 | 3630 | reloc_addr = addr; |
17c128f2 | 3631 | } |
2930abc7 FCE |
3632 | } |
3633 | else | |
3634 | { | |
d64e82b1 | 3635 | blacklist_section = this->get_blacklist_section(addr); |
17c128f2 | 3636 | reloc_section = ".absolute"; |
2930abc7 FCE |
3637 | } |
3638 | ||
7f9f3386 FCE |
3639 | if (sess.verbose > 1) |
3640 | { | |
b20febf3 FCE |
3641 | clog << "probe " << funcname << "@" << filename << ":" << line; |
3642 | if (string(module) == TOK_KERNEL) | |
3643 | clog << " kernel"; | |
91af0778 | 3644 | else if (has_module) |
b20febf3 | 3645 | clog << " module=" << module; |
91af0778 FCE |
3646 | else if (has_process) |
3647 | clog << " process=" << module; | |
b20febf3 FCE |
3648 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
3649 | if (blacklist_section != "") clog << " section=" << blacklist_section; | |
3650 | clog << " pc=0x" << hex << addr << dec; | |
7f9f3386 | 3651 | } |
4baf0e53 | 3652 | |
b20febf3 FCE |
3653 | bool bad = blacklisted_p (funcname, filename, line, module, blacklist_section, addr); |
3654 | if (sess.verbose > 1) | |
3655 | clog << endl; | |
7f9f3386 | 3656 | |
84048984 FCE |
3657 | if (module == TOK_KERNEL) |
3658 | { | |
3659 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
3660 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 3661 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
3662 | } |
3663 | ||
b20febf3 FCE |
3664 | if (! bad) |
3665 | { | |
1a0dbc5a | 3666 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
3667 | |
3668 | if (has_process) | |
3669 | { | |
3670 | results.push_back (new uprobe_derived_probe(funcname, filename, line, | |
3671 | module, 0, reloc_section, addr, reloc_addr, | |
3672 | *this, scope_die)); | |
3673 | } | |
3674 | else | |
3675 | { | |
3676 | assert (has_kernel || has_module); | |
3677 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 3678 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
3679 | *this, scope_die)); |
3680 | } | |
b20febf3 | 3681 | } |
2930abc7 FCE |
3682 | } |
3683 | ||
5f0a03a6 JK |
3684 | enum dbinfo_reqt |
3685 | dwarf_query::assess_dbinfo_reqt() | |
3686 | { | |
3687 | if (has_absolute) | |
3688 | { | |
3689 | // kernel.statement(NUM).absolute | |
3690 | return dbr_none; | |
3691 | } | |
3692 | if (has_inline) | |
3693 | { | |
3694 | // kernel.function("f").inline or module("m").function("f").inline | |
3695 | return dbr_need_dwarf; | |
3696 | } | |
3697 | if (has_function_str && spec_type == function_alone) | |
3698 | { | |
3699 | // kernel.function("f") or module("m").function("f") | |
3700 | return dbr_need_symtab; | |
3701 | } | |
3702 | if (has_statement_num) | |
3703 | { | |
3704 | // kernel.statement(NUM) or module("m").statement(NUM) | |
3705 | // Technically, all we need is the module offset (or _stext, for | |
3706 | // the kernel). But for that we need either the ELF file or (for | |
3707 | // _stext) the symbol table. In either case, the symbol table | |
3708 | // is available, and that allows us to map the NUM (address) | |
3709 | // to a function, which is goodness. | |
3710 | return dbr_need_symtab; | |
3711 | } | |
3712 | if (has_function_num) | |
3713 | { | |
3714 | // kernel.function(NUM) or module("m").function(NUM) | |
3715 | // Need the symbol table so we can back up from NUM to the | |
3716 | // start of the function. | |
3717 | return dbr_need_symtab; | |
3718 | } | |
3719 | // Symbol table tells us nothing about source files or line numbers. | |
3720 | return dbr_need_dwarf; | |
3721 | } | |
2930abc7 FCE |
3722 | |
3723 | ||
b8da0ad1 FCE |
3724 | // The critical determining factor when interpreting a pattern |
3725 | // string is, perhaps surprisingly: "presence of a lineno". The | |
3726 | // presence of a lineno changes the search strategy completely. | |
3727 | // | |
3728 | // Compare the two cases: | |
3729 | // | |
3730 | // 1. {statement,function}(foo@file.c:lineno) | |
3731 | // - find the files matching file.c | |
3732 | // - in each file, find the functions matching foo | |
3733 | // - query the file for line records matching lineno | |
3734 | // - iterate over the line records, | |
3735 | // - and iterate over the functions, | |
3736 | // - if(haspc(function.DIE, line.addr)) | |
3737 | // - if looking for statements: probe(lineno.addr) | |
3738 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
3739 | // | |
3740 | // 2. {statement,function}(foo@file.c) | |
3741 | // - find the files matching file.c | |
3742 | // - in each file, find the functions matching foo | |
3743 | // - probe(function.{entrypc,return,etc.}) | |
3744 | // | |
3745 | // Thus the first decision we make is based on the presence of a | |
3746 | // lineno, and we enter entirely different sets of callbacks | |
3747 | // depending on that decision. | |
3748 | // | |
3749 | // Note that the first case is a generalization fo the second, in that | |
3750 | // we could theoretically search through line records for matching | |
3751 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
3752 | // are already cached elsewhere, so we can do an "index scan" as an | |
3753 | // optimization. | |
7e1279ea | 3754 | |
bd2b1e68 | 3755 | static void |
4cd232e4 | 3756 | query_statement (string const & func, |
20e4a32c | 3757 | char const * file, |
4cd232e4 | 3758 | int line, |
bcc12710 | 3759 | Dwarf_Die *scope_die, |
20e4a32c | 3760 | Dwarf_Addr stmt_addr, |
4cd232e4 | 3761 | dwarf_query * q) |
bd2b1e68 | 3762 | { |
39bcd429 FCE |
3763 | try |
3764 | { | |
cee35f73 | 3765 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 3766 | line, scope_die, stmt_addr); |
39bcd429 FCE |
3767 | } |
3768 | catch (const semantic_error& e) | |
3769 | { | |
3770 | q->sess.print_error (e); | |
3771 | } | |
bd2b1e68 GH |
3772 | } |
3773 | ||
7e1279ea | 3774 | static void |
3e961ba6 | 3775 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
3776 | dwarf_query * q) |
3777 | { | |
b6581717 | 3778 | try |
7e1279ea | 3779 | { |
b6581717 GH |
3780 | if (q->has_return) |
3781 | { | |
3782 | throw semantic_error ("cannot probe .return of inline function '" + ii.name + "'"); | |
3783 | } | |
3784 | else | |
3785 | { | |
b0ee93c4 | 3786 | if (q->sess.verbose>2) |
20e4a32c | 3787 | clog << "querying entrypc " |
3e961ba6 | 3788 | << hex << ii.entrypc << dec |
db22e55f | 3789 | << " of instance of inline '" << ii.name << "'\n"; |
20e4a32c | 3790 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
3e961ba6 | 3791 | &ii.die, ii.entrypc, q); |
b6581717 | 3792 | } |
7e1279ea | 3793 | } |
b6581717 | 3794 | catch (semantic_error &e) |
7e1279ea | 3795 | { |
b6581717 | 3796 | q->sess.print_error (e); |
7e1279ea FCE |
3797 | } |
3798 | } | |
3799 | ||
3800 | static void | |
3801 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 3802 | func_info & fi, |
7e1279ea FCE |
3803 | dwarf_query * q) |
3804 | { | |
b6581717 | 3805 | try |
7e1279ea | 3806 | { |
b6581717 GH |
3807 | if (q->has_return) |
3808 | { | |
3809 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
3810 | // kretprobe based on the entrypc in this case. | |
20e4a32c | 3811 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
3812 | &fi.die, entrypc, q); |
3813 | } | |
3814 | else | |
3815 | { | |
35dc8b04 | 3816 | if (fi.prologue_end != 0) |
44f75386 | 3817 | { |
44f75386 FCE |
3818 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
3819 | &fi.die, fi.prologue_end, q); | |
3820 | } | |
3821 | else | |
3822 | { | |
3823 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
3824 | &fi.die, entrypc, q); | |
3825 | } | |
b6581717 | 3826 | } |
7e1279ea | 3827 | } |
b6581717 | 3828 | catch (semantic_error &e) |
7e1279ea | 3829 | { |
b6581717 | 3830 | q->sess.print_error (e); |
7e1279ea FCE |
3831 | } |
3832 | } | |
3833 | ||
3834 | ||
3835 | static void | |
86bf665e | 3836 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
3837 | { |
3838 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
3839 | ||
86bf665e | 3840 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 3841 | |
86bf665e | 3842 | int lineno = line.lineno(); |
847bf07f | 3843 | |
86bf665e | 3844 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
3845 | i != q->filtered_functions.end(); ++i) |
3846 | { | |
3e961ba6 | 3847 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 3848 | { |
b0ee93c4 | 3849 | if (q->sess.verbose>3) |
db22e55f | 3850 | clog << "function DIE lands on srcfile\n"; |
4cd232e4 | 3851 | if (q->has_statement_str) |
3e961ba6 | 3852 | query_statement (i->name, i->decl_file, |
847bf07f | 3853 | lineno, // NB: not q->line ! |
3e961ba6 | 3854 | &(i->die), addr, q); |
4cd232e4 | 3855 | else |
3e961ba6 | 3856 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 3857 | } |
20e4a32c RM |
3858 | } |
3859 | ||
86bf665e | 3860 | for (inline_instance_map_t::iterator i |
897820ca GH |
3861 | = q->filtered_inlines.begin(); |
3862 | i != q->filtered_inlines.end(); ++i) | |
3863 | { | |
3e961ba6 | 3864 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 3865 | { |
b0ee93c4 | 3866 | if (q->sess.verbose>3) |
db22e55f | 3867 | clog << "inline instance DIE lands on srcfile\n"; |
897820ca | 3868 | if (q->has_statement_str) |
3e961ba6 JB |
3869 | query_statement (i->name, i->decl_file, |
3870 | q->line[0], &(i->die), addr, q); | |
897820ca | 3871 | else |
3e961ba6 | 3872 | query_inline_instance_info (*i, q); |
897820ca | 3873 | } |
20e4a32c | 3874 | } |
7e1279ea FCE |
3875 | } |
3876 | ||
3877 | ||
4fa7b22b | 3878 | static int |
7e1279ea | 3879 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
3880 | { |
3881 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
7e1279ea | 3882 | assert (!q->has_statement_num); |
bd2b1e68 | 3883 | |
39bcd429 | 3884 | try |
7a053d3b | 3885 | { |
b0ee93c4 | 3886 | if (q->sess.verbose>2) |
db22e55f | 3887 | clog << "examining inline instance of " << q->dw.function_name << "\n"; |
7e1279ea | 3888 | |
4baf0e53 | 3889 | if ((q->has_function_str && ! q->has_call) |
b8da0ad1 | 3890 | || q->has_statement_str) |
7e1279ea | 3891 | { |
b0ee93c4 | 3892 | if (q->sess.verbose>2) |
db22e55f FCE |
3893 | clog << "selected inline instance of " << q->dw.function_name |
3894 | << "\n"; | |
7e1279ea FCE |
3895 | |
3896 | Dwarf_Addr entrypc; | |
3897 | if (q->dw.die_entrypc (die, &entrypc)) | |
3898 | { | |
3899 | inline_instance_info inl; | |
3900 | inl.die = *die; | |
3901 | inl.name = q->dw.function_name; | |
3e961ba6 | 3902 | inl.entrypc = entrypc; |
4cd232e4 GH |
3903 | q->dw.function_file (&inl.decl_file); |
3904 | q->dw.function_line (&inl.decl_line); | |
3e961ba6 | 3905 | q->filtered_inlines.push_back(inl); |
7e1279ea FCE |
3906 | } |
3907 | } | |
3908 | return DWARF_CB_OK; | |
3909 | } | |
3910 | catch (const semantic_error& e) | |
3911 | { | |
3912 | q->sess.print_error (e); | |
3913 | return DWARF_CB_ABORT; | |
3914 | } | |
3915 | } | |
bb788f9f | 3916 | |
7e1279ea | 3917 | static int |
2da9cedb | 3918 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 3919 | { |
2da9cedb | 3920 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
bb788f9f | 3921 | |
7e1279ea FCE |
3922 | try |
3923 | { | |
7e1279ea FCE |
3924 | q->dw.focus_on_function (func); |
3925 | ||
20e4a32c | 3926 | if (q->dw.func_is_inline () |
b8da0ad1 FCE |
3927 | && (! q->has_call) && (! q->has_return) |
3928 | && (((q->has_statement_str || q->has_function_str) | |
3929 | && q->dw.function_name_matches(q->function)))) | |
7e1279ea | 3930 | { |
b0ee93c4 | 3931 | if (q->sess.verbose>3) |
db22e55f FCE |
3932 | clog << "checking instances of inline " << q->dw.function_name |
3933 | << "\n"; | |
2da9cedb | 3934 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
275f40a6 FCE |
3935 | |
3936 | if (q->dw.function_name_final_match (q->function)) | |
3937 | return DWARF_CB_ABORT; | |
7e1279ea | 3938 | } |
396afcee | 3939 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 3940 | { |
7e1279ea FCE |
3941 | bool record_this_function = false; |
3942 | ||
3943 | if ((q->has_statement_str || q->has_function_str) | |
3944 | && q->dw.function_name_matches(q->function)) | |
3945 | { | |
3946 | record_this_function = true; | |
3947 | } | |
e4c58386 | 3948 | else if (q->has_function_num || q->has_statement_num) |
7e1279ea | 3949 | { |
e4c58386 | 3950 | Dwarf_Addr query_addr = |
9b692b91 SC |
3951 | (q->has_function_num ? q->function_num_val : |
3952 | q->has_statement_num ? q->statement_num_val : | |
3953 | (assert(0) , 0)); | |
7e1279ea FCE |
3954 | Dwarf_Die d; |
3955 | q->dw.function_die (&d); | |
20e4a32c | 3956 | |
86bf665e | 3957 | if (q->dw.die_has_pc (d, query_addr)) |
7e1279ea FCE |
3958 | record_this_function = true; |
3959 | } | |
3960 | ||
3961 | if (record_this_function) | |
3962 | { | |
b0ee93c4 | 3963 | if (q->sess.verbose>2) |
db22e55f | 3964 | clog << "selected function " << q->dw.function_name << "\n"; |
7e1279ea | 3965 | |
e4c58386 FCE |
3966 | func_info func; |
3967 | q->dw.function_die (&func.die); | |
3968 | func.name = q->dw.function_name; | |
3969 | q->dw.function_file (&func.decl_file); | |
3970 | q->dw.function_line (&func.decl_line); | |
3971 | ||
3972 | if (q->has_function_num || q->has_function_str || q->has_statement_str) | |
3973 | { | |
3974 | Dwarf_Addr entrypc; | |
3975 | if (q->dw.function_entrypc (&entrypc)) | |
3e961ba6 JB |
3976 | { |
3977 | func.entrypc = entrypc; | |
3978 | q->filtered_functions.push_back (func); | |
3979 | } | |
e4c58386 | 3980 | else |
552fdd9f JB |
3981 | /* this function just be fully inlined, just ignore it */ |
3982 | return DWARF_CB_OK; | |
e4c58386 FCE |
3983 | } |
3984 | else if (q->has_statement_num) | |
3985 | { | |
3e961ba6 JB |
3986 | func.entrypc = q->statement_num_val; |
3987 | q->filtered_functions.push_back (func); | |
275f40a6 FCE |
3988 | if (q->dw.function_name_final_match (q->function)) |
3989 | return DWARF_CB_ABORT; | |
e4c58386 FCE |
3990 | } |
3991 | else | |
3992 | assert(0); | |
3993 | ||
3994 | if (q->dw.function_name_final_match (q->function)) | |
3995 | return DWARF_CB_ABORT; | |
7e1279ea FCE |
3996 | } |
3997 | } | |
39bcd429 | 3998 | return DWARF_CB_OK; |
bd2b1e68 | 3999 | } |
39bcd429 | 4000 | catch (const semantic_error& e) |
bd2b1e68 | 4001 | { |
39bcd429 FCE |
4002 | q->sess.print_error (e); |
4003 | return DWARF_CB_ABORT; | |
bd2b1e68 | 4004 | } |
bd2b1e68 GH |
4005 | } |
4006 | ||
4007 | static int | |
4008 | query_cu (Dwarf_Die * cudie, void * arg) | |
4009 | { | |
20c6c071 | 4010 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
49abf162 | 4011 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 4012 | |
39bcd429 | 4013 | try |
bd2b1e68 | 4014 | { |
7e1279ea | 4015 | q->dw.focus_on_cu (cudie); |
b5d77020 | 4016 | |
b0ee93c4 | 4017 | if (false && q->sess.verbose>2) |
b5d77020 | 4018 | clog << "focused on CU '" << q->dw.cu_name |
db22e55f | 4019 | << "', in module '" << q->dw.module_name << "'\n"; |
d9b516ca | 4020 | |
e4c58386 | 4021 | if (q->has_statement_str || q->has_statement_num |
54efe513 | 4022 | || q->has_function_str || q->has_function_num) |
7e1279ea FCE |
4023 | { |
4024 | q->filtered_srcfiles.clear(); | |
4025 | q->filtered_functions.clear(); | |
4026 | q->filtered_inlines.clear(); | |
4027 | ||
4028 | // In this path, we find "abstract functions", record | |
4029 | // information about them, and then (depending on lineno | |
4030 | // matching) possibly emit one or more of the function's | |
4031 | // associated addresses. Unfortunately the control of this | |
4032 | // cannot easily be turned inside out. | |
4033 | ||
b8da0ad1 | 4034 | if ((q->has_statement_str || q->has_function_str) |
7e1279ea FCE |
4035 | && (q->spec_type != function_alone)) |
4036 | { | |
4037 | // If we have a pattern string with a filename, we need | |
4038 | // to elaborate the srcfile mask in question first. | |
4039 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
4040 | ||
4041 | // If we have a file pattern and *no* srcfile matches, there's | |
4042 | // no need to look further into this CU, so skip. | |
4043 | if (q->filtered_srcfiles.empty()) | |
4044 | return DWARF_CB_OK; | |
4045 | } | |
86bf665e TM |
4046 | // Verify that a raw address matches the beginning of a |
4047 | // statement. This is a somewhat lame check that the address | |
4048 | // is at the start of an assembly instruction. | |
1f0cfd98 SC |
4049 | // Avoid for now since this thwarts a probe on a statement in a macro |
4050 | if (0 && q->has_statement_num) | |
86bf665e TM |
4051 | { |
4052 | Dwarf_Addr queryaddr = q->statement_num_val; | |
4053 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, queryaddr)); | |
4054 | Dwarf_Addr lineaddr = 0; | |
4055 | if (address_line) | |
4056 | lineaddr = address_line.addr(); | |
4057 | if (!address_line || lineaddr != queryaddr) | |
4058 | { | |
4059 | stringstream msg; | |
4060 | msg << "address 0x" << hex << queryaddr | |
1b1b4ceb | 4061 | << " does not match the beginning of a statement"; |
86bf665e TM |
4062 | throw semantic_error(msg.str()); |
4063 | } | |
4064 | } | |
7e1279ea FCE |
4065 | // Pick up [entrypc, name, DIE] tuples for all the functions |
4066 | // matching the query, and fill in the prologue endings of them | |
4067 | // all in a single pass. | |
2da9cedb JS |
4068 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, |
4069 | q->function, | |
4070 | q->has_statement_num); | |
5f0a03a6 JK |
4071 | if (rc != DWARF_CB_OK) |
4072 | q->query_done = true; | |
44f75386 | 4073 | |
35dc8b04 | 4074 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 |
e4c58386 | 4075 | && !q->has_statement_str && !q->has_statement_num) // PR 2608 |
44f75386 FCE |
4076 | if (! q->filtered_functions.empty()) |
4077 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
7e1279ea | 4078 | |
b8da0ad1 | 4079 | if ((q->has_statement_str || q->has_function_str) |
7e1279ea FCE |
4080 | && (q->spec_type == function_file_and_line)) |
4081 | { | |
4082 | // If we have a pattern string with target *line*, we | |
20e4a32c | 4083 | // have to look at lines in all the matched srcfiles. |
7e1279ea FCE |
4084 | for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin(); |
4085 | i != q->filtered_srcfiles.end(); ++i) | |
897820ca | 4086 | q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str, |
0c8b7d37 | 4087 | q->line_type, query_srcfile_line, q); |
7e1279ea | 4088 | } |
0f336e95 SC |
4089 | else if (q->has_label) |
4090 | { | |
4091 | // If we have a pattern string with target *label*, we | |
4092 | // have to look at labels in all the matched srcfiles. | |
4093 | q->dw.iterate_over_cu_labels (q->label_val, q->dw.cu, q, query_statement); | |
4094 | } | |
7e1279ea FCE |
4095 | else |
4096 | { | |
e4c58386 | 4097 | // Otherwise, simply probe all resolved functions. |
86bf665e | 4098 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
e4c58386 | 4099 | i != q->filtered_functions.end(); ++i) |
3e961ba6 | 4100 | query_func_info (i->entrypc, *i, q); |
e4c58386 FCE |
4101 | |
4102 | // And all inline instances (if we're not excluding inlines with ".call") | |
4103 | if (! q->has_call) | |
86bf665e | 4104 | for (inline_instance_map_t::iterator i |
54efe513 | 4105 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) |
3e961ba6 | 4106 | query_inline_instance_info (*i, q); |
7e1279ea FCE |
4107 | } |
4108 | } | |
39bcd429 FCE |
4109 | else |
4110 | { | |
e4c58386 FCE |
4111 | // Before PR 5787, we used to have this: |
4112 | #if 0 | |
7e1279ea FCE |
4113 | // Otherwise we have a statement number, and we can just |
4114 | // query it directly within this module. | |
7e1279ea FCE |
4115 | assert (q->has_statement_num); |
4116 | Dwarf_Addr query_addr = q->statement_num_val; | |
b8da0ad1 | 4117 | query_addr = q->dw.module_address_to_global(query_addr); |
7e1279ea | 4118 | |
bcc12710 | 4119 | query_statement ("", "", -1, NULL, query_addr, q); |
e4c58386 FCE |
4120 | #endif |
4121 | // But now, we traverse CUs/functions even for | |
4122 | // statement_num's, for blacklist sensitivity and $var | |
4123 | // resolution purposes. | |
4124 | ||
4125 | assert (0); // NOTREACHED | |
39bcd429 FCE |
4126 | } |
4127 | return DWARF_CB_OK; | |
bd2b1e68 | 4128 | } |
39bcd429 | 4129 | catch (const semantic_error& e) |
bd2b1e68 | 4130 | { |
39bcd429 FCE |
4131 | q->sess.print_error (e); |
4132 | return DWARF_CB_ABORT; | |
bd2b1e68 | 4133 | } |
bd2b1e68 GH |
4134 | } |
4135 | ||
0ce64fb8 | 4136 | |
91af0778 | 4137 | #if 0 |
1d3a40b6 DS |
4138 | static int |
4139 | query_kernel_module (Dwfl_Module *mod, | |
91af0778 | 4140 | void **, |
1d3a40b6 | 4141 | const char *name, |
b8da0ad1 | 4142 | Dwarf_Addr, |
1d3a40b6 DS |
4143 | void *arg) |
4144 | { | |
4145 | if (TOK_KERNEL == name) | |
4146 | { | |
4147 | Dwfl_Module **m = (Dwfl_Module **)arg; | |
4148 | ||
4149 | *m = mod; | |
4150 | return DWARF_CB_ABORT; | |
4151 | } | |
4152 | return DWARF_CB_OK; | |
4153 | } | |
91af0778 FCE |
4154 | #endif |
4155 | ||
1d3a40b6 | 4156 | |
5f0a03a6 JK |
4157 | static void |
4158 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
4159 | { | |
4160 | // Validate the machine code in this elf file against the | |
4161 | // session machine. This is important, in case the wrong kind | |
4162 | // of debuginfo is being automagically processed by elfutils. | |
4163 | // While we can tell i686 apart from x86-64, unfortunately | |
4164 | // we can't help confusing i586 vs i686 (both EM_386). | |
4165 | ||
4166 | Dwarf_Addr bias; | |
4167 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
4168 | // because dwfl_module_getelf can force costly section relocations | |
4169 | // we don't really need, while either will do for this purpose. | |
4170 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
4171 | ?: dwfl_module_getelf (mod, &bias)); | |
4172 | ||
4173 | GElf_Ehdr ehdr_mem; | |
4174 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 4175 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
5f0a03a6 JK |
4176 | int elf_machine = em->e_machine; |
4177 | const char* debug_filename = ""; | |
4178 | const char* main_filename = ""; | |
4179 | (void) dwfl_module_info (mod, NULL, NULL, | |
4180 | NULL, NULL, NULL, | |
4181 | & main_filename, | |
4182 | & debug_filename); | |
4183 | const string& sess_machine = q->sess.architecture; | |
4184 | string expect_machine; | |
4185 | ||
4186 | switch (elf_machine) | |
4187 | { | |
4188 | case EM_386: expect_machine = "i?86"; break; // accept e.g. i586 | |
4189 | case EM_X86_64: expect_machine = "x86_64"; break; | |
7635926c JK |
4190 | // We don't support 32-bit ppc kernels, but we support 32-bit apps |
4191 | // running on ppc64 kernels. | |
4192 | case EM_PPC: expect_machine = "ppc64"; break; | |
5f0a03a6 JK |
4193 | case EM_PPC64: expect_machine = "ppc64"; break; |
4194 | case EM_S390: expect_machine = "s390x"; break; | |
4195 | case EM_IA_64: expect_machine = "ia64"; break; | |
4196 | case EM_ARM: expect_machine = "armv*"; break; | |
4197 | // XXX: fill in some more of these | |
4198 | default: expect_machine = "?"; break; | |
4199 | } | |
4200 | ||
4201 | if (! debug_filename) debug_filename = main_filename; | |
4202 | if (! debug_filename) debug_filename = name; | |
4203 | ||
4204 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0) | |
4205 | { | |
4206 | stringstream msg; | |
4207 | msg << "ELF machine " << expect_machine << " (code " << elf_machine | |
4208 | << ") mismatch with target " << sess_machine | |
4209 | << " in '" << debug_filename << "'"; | |
4210 | throw semantic_error(msg.str ()); | |
4211 | } | |
4212 | ||
4213 | if (q->sess.verbose>2) | |
4214 | clog << "focused on module '" << q->dw.module_name | |
4215 | << " = [0x" << hex << q->dw.module_start | |
4216 | << "-0x" << q->dw.module_end | |
4217 | << ", bias 0x" << q->dw.module_bias << "]" << dec | |
4218 | << " file " << debug_filename | |
4219 | << " ELF machine " << expect_machine | |
4220 | << " (code " << elf_machine << ")" | |
4221 | << "\n"; | |
4222 | } | |
1d3a40b6 | 4223 | |
91af0778 FCE |
4224 | |
4225 | ||
4226 | static Dwarf_Addr | |
4227 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
4228 | { | |
4229 | int syments = dwfl_module_getsymtab(m); | |
4230 | assert(syments); | |
4231 | for (int i = 1; i < syments; ++i) | |
4232 | { | |
4233 | GElf_Sym sym; | |
4234 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
4235 | if (name != NULL && strcmp(name, wanted) == 0) | |
4236 | return sym.st_value; | |
4237 | } | |
4238 | ||
4239 | return 0; | |
4240 | } | |
4241 | ||
4242 | ||
4243 | ||
bd2b1e68 | 4244 | static int |
b8da0ad1 | 4245 | query_module (Dwfl_Module *mod, |
91af0778 | 4246 | void **, |
b8da0ad1 | 4247 | const char *name, |
6f4c1275 | 4248 | Dwarf_Addr addr, |
b8da0ad1 | 4249 | void *arg) |
bd2b1e68 | 4250 | { |
91af0778 | 4251 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 4252 | |
39bcd429 | 4253 | try |
e38d6504 | 4254 | { |
91af0778 FCE |
4255 | module_info* mi = q->sess.module_cache->cache[name]; |
4256 | if (mi == 0) | |
4257 | { | |
4258 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
4259 | ||
6f4c1275 FCE |
4260 | mi->mod = mod; |
4261 | mi->addr = addr; | |
91af0778 | 4262 | |
6f4c1275 FCE |
4263 | const char* debug_filename = ""; |
4264 | const char* main_filename = ""; | |
4265 | (void) dwfl_module_info (mod, NULL, NULL, | |
4266 | NULL, NULL, NULL, | |
4267 | & main_filename, | |
4268 | & debug_filename); | |
4269 | ||
4270 | if (q->sess.ignore_vmlinux && name == TOK_KERNEL) | |
91af0778 FCE |
4271 | { |
4272 | // report_kernel() in elfutils found vmlinux, but pretend it didn't. | |
4273 | // Given a non-null path, returning 1 means keep reporting modules. | |
4274 | mi->dwarf_status = info_absent; | |
4275 | } | |
6f4c1275 | 4276 | else if (debug_filename || main_filename) |
91af0778 | 4277 | { |
6f4c1275 FCE |
4278 | mi->elf_path = debug_filename ?: main_filename; |
4279 | } | |
4280 | else if (name == TOK_KERNEL) | |
4281 | { | |
4282 | mi->dwarf_status = info_absent; | |
91af0778 | 4283 | } |
91af0778 FCE |
4284 | } |
4285 | // OK, enough of that module_info caching business. | |
4286 | ||
5f0a03a6 | 4287 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 4288 | |
39bcd429 FCE |
4289 | // If we have enough information in the pattern to skip a module and |
4290 | // the module does not match that information, return early. | |
b8da0ad1 | 4291 | if (!q->dw.module_name_matches(q->module_val)) |
39bcd429 | 4292 | return DWARF_CB_OK; |
0cbbf9d1 FCE |
4293 | |
4294 | // Don't allow module("*kernel*") type expressions to match the | |
4295 | // elfutils module "kernel", which we refer to in the probe | |
4296 | // point syntax exclusively as "kernel.*". | |
4297 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
4298 | return DWARF_CB_OK; | |
b5d77020 | 4299 | |
5f0a03a6 JK |
4300 | if (mod) |
4301 | validate_module_elf(mod, name, q); | |
4302 | else | |
91af0778 FCE |
4303 | assert(q->has_kernel); // and no vmlinux to examine |
4304 | ||
4305 | if (q->sess.verbose>2) | |
4306 | cerr << "focused on module '" << q->dw.module_name << "'\n"; | |
4307 | ||
4308 | ||
4309 | // Collect a few kernel addresses. XXX: these belong better | |
4310 | // to the sess.module_info["kernel"] struct. | |
4311 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 4312 | { |
91af0778 FCE |
4313 | if (! q->sess.sym_kprobes_text_start) |
4314 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
4315 | if (! q->sess.sym_kprobes_text_end) | |
4316 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
4317 | if (! q->sess.sym_stext) | |
4318 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
4319 | } |
4320 | ||
91af0778 | 4321 | // Finally, search the module for matches of the probe point. |
2c384610 | 4322 | q->handle_query_module(); |
bb788f9f | 4323 | |
91af0778 | 4324 | |
b8da0ad1 FCE |
4325 | // If we know that there will be no more matches, abort early. |
4326 | if (q->dw.module_name_final_match(q->module_val)) | |
4327 | return DWARF_CB_ABORT; | |
4328 | else | |
4329 | return DWARF_CB_OK; | |
7a053d3b | 4330 | } |
39bcd429 | 4331 | catch (const semantic_error& e) |
bd2b1e68 | 4332 | { |
39bcd429 FCE |
4333 | q->sess.print_error (e); |
4334 | return DWARF_CB_ABORT; | |
bd2b1e68 | 4335 | } |
bd2b1e68 GH |
4336 | } |
4337 | ||
5f0a03a6 | 4338 | void |
c4ce66a1 | 4339 | dwflpp::query_modules(base_query *q) |
5f0a03a6 | 4340 | { |
91af0778 | 4341 | iterate_over_modules(&query_module, q); |
5f0a03a6 | 4342 | } |
2930abc7 | 4343 | |
de688825 | 4344 | struct var_expanding_visitor: public update_visitor |
77de5e9e | 4345 | { |
77de5e9e | 4346 | static unsigned tick; |
e57b735a | 4347 | stack<functioncall**> target_symbol_setter_functioncalls; |
77de5e9e | 4348 | |
de688825 | 4349 | var_expanding_visitor() {} |
35d4ab18 FCE |
4350 | void visit_assignment (assignment* e); |
4351 | }; | |
4352 | ||
4353 | ||
de688825 | 4354 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 4355 | { |
77de5e9e | 4356 | dwarf_query & q; |
bcc12710 | 4357 | Dwarf_Die *scope_die; |
77de5e9e | 4358 | Dwarf_Addr addr; |
8c819921 | 4359 | block *add_block; |
8fc05e57 | 4360 | probe *add_probe; |
cd5b28b2 | 4361 | std::map<std::string, symbol *> return_ts_map; |
b95e2b79 | 4362 | bool visited; |
77de5e9e | 4363 | |
de688825 | 4364 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
b95e2b79 | 4365 | q(q), scope_die(sd), addr(a), add_block(NULL), add_probe(NULL), visited(false) {} |
d7f3e0c5 | 4366 | void visit_target_symbol (target_symbol* e); |
c24447be | 4367 | void visit_cast_op (cast_op* e); |
77de5e9e GH |
4368 | }; |
4369 | ||
4370 | ||
35d4ab18 | 4371 | |
de688825 | 4372 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 4373 | |
77de5e9e | 4374 | void |
de688825 | 4375 | var_expanding_visitor::visit_assignment (assignment* e) |
77de5e9e | 4376 | { |
e57b735a GH |
4377 | // Our job would normally be to require() the left and right sides |
4378 | // into a new assignment. What we're doing is slightly trickier: | |
4379 | // we're pushing a functioncall** onto a stack, and if our left | |
4380 | // child sets the functioncall* for that value, we're going to | |
4381 | // assume our left child was a target symbol -- transformed into a | |
4382 | // set_target_foo(value) call, and it wants to take our right child | |
4383 | // as the argument "value". | |
4384 | // | |
4385 | // This is why some people claim that languages with | |
4386 | // constructor-decomposing case expressions have a leg up on | |
4387 | // visitors. | |
4388 | ||
4389 | functioncall *fcall = NULL; | |
4390 | expression *new_left, *new_right; | |
d9b516ca | 4391 | |
e57b735a | 4392 | target_symbol_setter_functioncalls.push (&fcall); |
4ed05b15 | 4393 | new_left = require (e->left); |
e57b735a | 4394 | target_symbol_setter_functioncalls.pop (); |
4ed05b15 | 4395 | new_right = require (e->right); |
e57b735a GH |
4396 | |
4397 | if (fcall != NULL) | |
77de5e9e | 4398 | { |
e57b735a GH |
4399 | // Our left child is informing us that it was a target variable |
4400 | // and it has been replaced with a set_target_foo() function | |
4401 | // call; we are going to provide that function call -- with the | |
4402 | // right child spliced in as sole argument -- in place of | |
de688825 | 4403 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a GH |
4404 | |
4405 | // FIXME: for the time being, we only support plan $foo = bar, | |
4406 | // not += or any other op= variant. This is fixable, but a bit | |
4407 | // ugly. | |
4408 | if (e->op != "=") | |
4409 | throw semantic_error ("Operator-assign expressions on target " | |
4410 | "variables not implemented", e->tok); | |
4411 | ||
4412 | assert (new_left == fcall); | |
4413 | fcall->args.push_back (new_right); | |
4ed05b15 | 4414 | provide (fcall); |
77de5e9e | 4415 | } |
e57b735a GH |
4416 | else |
4417 | { | |
de688825 JS |
4418 | e->left = new_left; |
4419 | e->right = new_right; | |
4420 | provide (e); | |
e57b735a GH |
4421 | } |
4422 | } | |
d9b516ca | 4423 | |
d7f3e0c5 | 4424 | |
e57b735a | 4425 | void |
de688825 | 4426 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
e57b735a GH |
4427 | { |
4428 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
b95e2b79 | 4429 | visited = true; |
e57b735a | 4430 | |
cf2a1f85 DS |
4431 | bool lvalue = is_active_lvalue(e); |
4432 | if (lvalue && !q.sess.guru_mode) | |
4433 | throw semantic_error("write to target variable not permitted", e->tok); | |
4434 | ||
a43ba433 FCE |
4435 | // See if we need to generate a new probe to save/access function |
4436 | // parameters from a return probe. PR 1382. | |
4437 | if (q.has_return | |
4438 | && e->base_name != "$return" // not the special return-value variable handled below | |
4439 | && e->base_name != "$$return") // nor the other special variable handled below | |
85ecf79a DS |
4440 | { |
4441 | if (lvalue) | |
4442 | throw semantic_error("write to target variable not permitted in .return probes", e->tok); | |
4443 | ||
cd5b28b2 DS |
4444 | // Get the full name of the target symbol. |
4445 | stringstream ts_name_stream; | |
4446 | e->print(ts_name_stream); | |
4447 | string ts_name = ts_name_stream.str(); | |
4448 | ||
4449 | // Check and make sure we haven't already seen this target | |
4450 | // variable in this return probe. If we have, just return our | |
4451 | // last replacement. | |
4452 | map<string, symbol *>::iterator i = return_ts_map.find(ts_name); | |
4453 | if (i != return_ts_map.end()) | |
4454 | { | |
4ed05b15 | 4455 | provide (i->second); |
cd5b28b2 DS |
4456 | return; |
4457 | } | |
4458 | ||
85ecf79a DS |
4459 | // We've got to do several things here to handle target |
4460 | // variables in return probes. | |
4461 | ||
5e600bd6 DS |
4462 | // (1) Synthesize two global arrays. One is the cache of the |
4463 | // target variable and the other contains a thread specific | |
4464 | // nesting level counter. The arrays will look like | |
4465 | // this: | |
85ecf79a DS |
4466 | // |
4467 | // _dwarf_tvar_{name}_{num} | |
85ecf79a DS |
4468 | // _dwarf_tvar_{name}_{num}_ctr |
4469 | ||
4470 | string aname = (string("_dwarf_tvar_") | |
4471 | + e->base_name.substr(1) | |
4472 | + "_" + lex_cast<string>(tick++)); | |
4473 | vardecl* vd = new vardecl; | |
4474 | vd->name = aname; | |
4475 | vd->tok = e->tok; | |
4476 | q.sess.globals.push_back (vd); | |
4477 | ||
4478 | string ctrname = aname + "_ctr"; | |
4479 | vd = new vardecl; | |
4480 | vd->name = ctrname; | |
4481 | vd->tok = e->tok; | |
4482 | q.sess.globals.push_back (vd); | |
4483 | ||
8c819921 DS |
4484 | // (2) Create a new code block we're going to insert at the |
4485 | // beginning of this probe to get the cached value into a | |
4486 | // temporary variable. We'll replace the target variable | |
4487 | // reference with the temporary variable reference. The code | |
4488 | // will look like this: | |
4489 | // | |
4490 | // _dwarf_tvar_tid = tid() | |
4491 | // _dwarf_tvar_{name}_{num}_tmp | |
4492 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4493 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4494 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4495 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
46392da3 DS |
4496 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) |
4497 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
8c819921 DS |
4498 | |
4499 | // (2a) Synthesize the tid temporary expression, which will look | |
85ecf79a DS |
4500 | // like this: |
4501 | // | |
8c819921 DS |
4502 | // _dwarf_tvar_tid = tid() |
4503 | symbol* tidsym = new symbol; | |
4504 | tidsym->name = string("_dwarf_tvar_tid"); | |
4505 | tidsym->tok = e->tok; | |
85ecf79a | 4506 | |
8c819921 DS |
4507 | if (add_block == NULL) |
4508 | { | |
8fc05e57 DS |
4509 | add_block = new block; |
4510 | add_block->tok = e->tok; | |
4511 | ||
4512 | // Synthesize a functioncall to grab the thread id. | |
4513 | functioncall* fc = new functioncall; | |
4514 | fc->tok = e->tok; | |
4515 | fc->function = string("tid"); | |
4516 | ||
4517 | // Assign the tid to '_dwarf_tvar_tid'. | |
4518 | assignment* a = new assignment; | |
4519 | a->tok = e->tok; | |
4520 | a->op = "="; | |
4521 | a->left = tidsym; | |
4522 | a->right = fc; | |
4523 | ||
4524 | expr_statement* es = new expr_statement; | |
4525 | es->tok = e->tok; | |
4526 | es->value = a; | |
4527 | add_block->statements.push_back (es); | |
8c819921 DS |
4528 | } |
4529 | ||
4530 | // (2b) Synthesize an array reference and assign it to a | |
4531 | // temporary variable (that we'll use as replacement for the | |
4532 | // target variable reference). It will look like this: | |
4533 | // | |
4534 | // _dwarf_tvar_{name}_{num}_tmp | |
4535 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4536 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4537 | ||
8fc05e57 DS |
4538 | arrayindex* ai_tvar_base = new arrayindex; |
4539 | ai_tvar_base->tok = e->tok; | |
85ecf79a DS |
4540 | |
4541 | symbol* sym = new symbol; | |
4542 | sym->name = aname; | |
4543 | sym->tok = e->tok; | |
8fc05e57 | 4544 | ai_tvar_base->base = sym; |
8c819921 | 4545 | |
8fc05e57 | 4546 | ai_tvar_base->indexes.push_back(tidsym); |
85ecf79a | 4547 | |
8c819921 DS |
4548 | // We need to create a copy of the array index in its current |
4549 | // state so we can have 2 variants of it (the original and one | |
4550 | // that post-decrements the second index). | |
8fc05e57 DS |
4551 | arrayindex* ai_tvar = new arrayindex; |
4552 | arrayindex* ai_tvar_postdec = new arrayindex; | |
4553 | *ai_tvar = *ai_tvar_base; | |
4554 | *ai_tvar_postdec = *ai_tvar_base; | |
85ecf79a | 4555 | |
8c819921 DS |
4556 | // Synthesize the |
4557 | // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the | |
85ecf79a | 4558 | // second index into the array. |
8c819921 DS |
4559 | arrayindex* ai_ctr = new arrayindex; |
4560 | ai_ctr->tok = e->tok; | |
5e600bd6 | 4561 | |
85ecf79a DS |
4562 | sym = new symbol; |
4563 | sym->name = ctrname; | |
4564 | sym->tok = e->tok; | |
8c819921 DS |
4565 | ai_ctr->base = sym; |
4566 | ai_ctr->indexes.push_back(tidsym); | |
4567 | ai_tvar->indexes.push_back(ai_ctr); | |
4568 | ||
4569 | symbol* tmpsym = new symbol; | |
4570 | tmpsym->name = aname + "_tmp"; | |
4571 | tmpsym->tok = e->tok; | |
4572 | ||
4573 | assignment* a = new assignment; | |
4574 | a->tok = e->tok; | |
4575 | a->op = "="; | |
4576 | a->left = tmpsym; | |
4577 | a->right = ai_tvar; | |
4578 | ||
4579 | expr_statement* es = new expr_statement; | |
4580 | es->tok = e->tok; | |
4581 | es->value = a; | |
4582 | ||
4583 | add_block->statements.push_back (es); | |
4584 | ||
4585 | // (2c) Add a post-decrement to the second array index and | |
4586 | // delete the array value. It will look like this: | |
4587 | // | |
4588 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4589 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
5e600bd6 | 4590 | |
85ecf79a DS |
4591 | post_crement* pc = new post_crement; |
4592 | pc->tok = e->tok; | |
4593 | pc->op = "--"; | |
8c819921 | 4594 | pc->operand = ai_ctr; |
8fc05e57 | 4595 | ai_tvar_postdec->indexes.push_back(pc); |
8c819921 DS |
4596 | |
4597 | delete_statement* ds = new delete_statement; | |
4598 | ds->tok = e->tok; | |
8fc05e57 | 4599 | ds->value = ai_tvar_postdec; |
8c819921 DS |
4600 | |
4601 | add_block->statements.push_back (ds); | |
85ecf79a | 4602 | |
46392da3 DS |
4603 | // (2d) Delete the counter value if it is 0. It will look like |
4604 | // this: | |
4605 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
4606 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
4baf0e53 | 4607 | |
46392da3 DS |
4608 | ds = new delete_statement; |
4609 | ds->tok = e->tok; | |
4610 | ds->value = ai_ctr; | |
4611 | ||
4612 | unary_expression *ue = new unary_expression; | |
4613 | ue->tok = e->tok; | |
4614 | ue->op = "!"; | |
4615 | ue->operand = ai_ctr; | |
4616 | ||
4617 | if_statement *ifs = new if_statement; | |
4618 | ifs->tok = e->tok; | |
4619 | ifs->condition = ue; | |
4620 | ifs->thenblock = ds; | |
4621 | ifs->elseblock = NULL; | |
4baf0e53 | 4622 | |
46392da3 DS |
4623 | add_block->statements.push_back (ifs); |
4624 | ||
85ecf79a | 4625 | // (3) We need an entry probe that saves the value for us in the |
8fc05e57 DS |
4626 | // global array we created. Create the entry probe, which will |
4627 | // look like this: | |
85ecf79a | 4628 | // |
85ecf79a | 4629 | // probe kernel.function("{function}") { |
8fc05e57 DS |
4630 | // _dwarf_tvar_tid = tid() |
4631 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4632 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
85ecf79a DS |
4633 | // = ${param} |
4634 | // } | |
4635 | ||
8fc05e57 | 4636 | if (add_probe == NULL) |
85ecf79a | 4637 | { |
8fc05e57 DS |
4638 | add_probe = new probe; |
4639 | add_probe->tok = e->tok; | |
4640 | ||
4641 | // We need the name of the current probe point, minus the | |
4642 | // ".return" (or anything after it, such as ".maxactive(N)"). | |
4643 | // Create a new probe point, copying all the components, | |
4644 | // stopping when we see the ".return" component. | |
4645 | probe_point* pp = new probe_point; | |
4646 | for (unsigned c = 0; c < q.base_loc->components.size(); c++) | |
4647 | { | |
4648 | if (q.base_loc->components[c]->functor == "return") | |
4649 | break; | |
4650 | else | |
4651 | pp->components.push_back(q.base_loc->components[c]); | |
4652 | } | |
4653 | pp->tok = e->tok; | |
4654 | pp->optional = q.base_loc->optional; | |
4655 | add_probe->locations.push_back(pp); | |
4656 | ||
4657 | add_probe->body = new block; | |
4658 | add_probe->body->tok = e->tok; | |
4659 | ||
4660 | // Synthesize a functioncall to grab the thread id. | |
4661 | functioncall* fc = new functioncall; | |
4662 | fc->tok = e->tok; | |
4663 | fc->function = string("tid"); | |
4664 | ||
4665 | // Assign the tid to '_dwarf_tvar_tid'. | |
4666 | assignment* a = new assignment; | |
4667 | a->tok = e->tok; | |
4668 | a->op = "="; | |
4669 | a->left = tidsym; | |
4670 | a->right = fc; | |
4671 | ||
4672 | expr_statement* es = new expr_statement; | |
4673 | es->tok = e->tok; | |
4674 | es->value = a; | |
ba6f838d | 4675 | add_probe->body = new block(add_probe->body, es); |
8fc05e57 DS |
4676 | |
4677 | vardecl* vd = new vardecl; | |
4678 | vd->tok = e->tok; | |
4679 | vd->name = tidsym->name; | |
4680 | vd->type = pe_long; | |
4681 | vd->set_arity(0); | |
4682 | add_probe->locals.push_back(vd); | |
85ecf79a | 4683 | } |
8fc05e57 DS |
4684 | |
4685 | // Save the value, like this: | |
4686 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4687 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4688 | // = ${param} | |
4689 | arrayindex* ai_tvar_preinc = new arrayindex; | |
4690 | *ai_tvar_preinc = *ai_tvar_base; | |
4baf0e53 | 4691 | |
8fc05e57 DS |
4692 | pre_crement* preinc = new pre_crement; |
4693 | preinc->tok = e->tok; | |
4694 | preinc->op = "++"; | |
4695 | preinc->operand = ai_ctr; | |
4696 | ai_tvar_preinc->indexes.push_back(preinc); | |
4baf0e53 | 4697 | |
8fc05e57 DS |
4698 | a = new assignment; |
4699 | a->tok = e->tok; | |
4700 | a->op = "="; | |
4701 | a->left = ai_tvar_preinc; | |
4702 | a->right = e; | |
4703 | ||
4704 | es = new expr_statement; | |
4705 | es->tok = e->tok; | |
4706 | es->value = a; | |
4707 | ||
ba6f838d | 4708 | add_probe->body = new block(add_probe->body, es); |
85ecf79a | 4709 | |
8c819921 DS |
4710 | // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to |
4711 | // our parent so it can be used as a substitute for the target | |
4712 | // symbol. | |
4ed05b15 | 4713 | provide (tmpsym); |
cd5b28b2 DS |
4714 | |
4715 | // (5) Remember this replacement since we might be able to reuse | |
4716 | // it later if the same return probe references this target | |
4717 | // symbol again. | |
4718 | return_ts_map[ts_name] = tmpsym; | |
85ecf79a DS |
4719 | return; |
4720 | } | |
cf2a1f85 | 4721 | |
2cb3fe26 SC |
4722 | if (e->base_name == "$$vars" |
4723 | || e->base_name == "$$parms" | |
a43ba433 FCE |
4724 | || e->base_name == "$$locals" |
4725 | || (q.has_return && (e->base_name == "$$return"))) | |
2cb3fe26 SC |
4726 | { |
4727 | Dwarf_Die *scopes; | |
4728 | if (dwarf_getscopes_die (scope_die, &scopes) == 0) | |
4729 | return; | |
41c262f3 | 4730 | |
2cb3fe26 SC |
4731 | target_symbol *tsym = new target_symbol; |
4732 | print_format* pf = new print_format; | |
4733 | ||
4734 | // Convert $$parms to sprintf of a list of parms and active local vars | |
4735 | // which we recursively evaluate | |
a43ba433 FCE |
4736 | |
4737 | // NB: we synthesize a new token here rather than reusing | |
4738 | // e->tok, because print_format::print likes to use | |
4739 | // its tok->content. | |
4740 | token* pf_tok = new token; | |
4741 | pf_tok->location = e->tok->location; | |
4742 | pf_tok->type = tok_identifier; | |
4743 | pf_tok->content = "sprint"; | |
4744 | ||
4745 | pf->tok = pf_tok; | |
2cb3fe26 SC |
4746 | pf->print_to_stream = false; |
4747 | pf->print_with_format = true; | |
4748 | pf->print_with_delim = false; | |
4749 | pf->print_with_newline = false; | |
4750 | pf->print_char = false; | |
4751 | ||
a43ba433 FCE |
4752 | if (q.has_return && (e->base_name == "$$return")) |
4753 | { | |
4754 | tsym->tok = e->tok; | |
4755 | tsym->base_name = "$return"; | |
41c262f3 | 4756 | |
a43ba433 FCE |
4757 | // Ignore any variable that isn't accessible. |
4758 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
4759 | expression *texp = tsym; |
4760 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
4761 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
4762 | { | |
2cb3fe26 | 4763 | |
a43ba433 FCE |
4764 | } |
4765 | else | |
4766 | { | |
fd574705 | 4767 | pf->raw_components += "return"; |
a43ba433 | 4768 | pf->raw_components += "=%#x "; |
4ed05b15 | 4769 | pf->args.push_back(texp); |
a43ba433 FCE |
4770 | } |
4771 | } | |
4772 | else | |
4773 | { | |
4774 | // non-.return probe: support $$parms, $$vars, $$locals | |
4775 | Dwarf_Die result; | |
4776 | if (dwarf_child (&scopes[0], &result) == 0) | |
4777 | do | |
00cf3709 | 4778 | { |
a43ba433 FCE |
4779 | switch (dwarf_tag (&result)) |
4780 | { | |
4781 | case DW_TAG_variable: | |
4782 | if (e->base_name == "$$parms") | |
4783 | continue; | |
4784 | break; | |
4785 | case DW_TAG_formal_parameter: | |
4786 | if (e->base_name == "$$locals") | |
4787 | continue; | |
4788 | break; | |
41c262f3 | 4789 | |
a43ba433 FCE |
4790 | default: |
4791 | continue; | |
4792 | } | |
41c262f3 | 4793 | |
a43ba433 | 4794 | const char *diename = dwarf_diename (&result); |
f76427a2 FCE |
4795 | if (! diename) continue; |
4796 | ||
a43ba433 FCE |
4797 | tsym->tok = e->tok; |
4798 | tsym->base_name = "$"; | |
4799 | tsym->base_name += diename; | |
41c262f3 | 4800 | |
a43ba433 FCE |
4801 | // Ignore any variable that isn't accessible. |
4802 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
4803 | expression *texp = tsym; |
4804 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
4805 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
4806 | { | |
12b44fb3 FCE |
4807 | if (q.sess.verbose>2) |
4808 | { | |
4809 | for (semantic_error *c = tsym->saved_conversion_error; | |
4810 | c != 0; | |
4811 | c = c->chain) { | |
4812 | clog << "variable location problem: " << c->what() << endl; | |
4813 | } | |
4814 | } | |
4815 | ||
a43ba433 FCE |
4816 | pf->raw_components += diename; |
4817 | pf->raw_components += "=? "; | |
4818 | } | |
4819 | else | |
4820 | { | |
4821 | pf->raw_components += diename; | |
4822 | pf->raw_components += "=%#x "; | |
4ed05b15 | 4823 | pf->args.push_back(texp); |
a43ba433 | 4824 | } |
00cf3709 | 4825 | } |
a43ba433 FCE |
4826 | while (dwarf_siblingof (&result, &result) == 0); |
4827 | } | |
2cb3fe26 | 4828 | |
2cb3fe26 | 4829 | pf->components = print_format::string_to_components(pf->raw_components); |
4ed05b15 | 4830 | provide (pf); |
2cb3fe26 SC |
4831 | |
4832 | return; | |
4833 | } | |
4834 | ||
e57b735a | 4835 | // Synthesize a function. |
d7f3e0c5 | 4836 | functiondecl *fdecl = new functiondecl; |
7b99c7d3 | 4837 | fdecl->tok = e->tok; |
d7f3e0c5 | 4838 | embeddedcode *ec = new embeddedcode; |
5e309481 | 4839 | ec->tok = e->tok; |
e8fbc5e8 | 4840 | |
1b07c728 | 4841 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
20e4a32c | 4842 | + "_" + e->base_name.substr(1) |
e57b735a GH |
4843 | + "_" + lex_cast<string>(tick++)); |
4844 | ||
66d284f4 | 4845 | try |
e57b735a | 4846 | { |
a43ba433 | 4847 | if (q.has_return && (e->base_name == "$return")) |
e19fda4e DS |
4848 | { |
4849 | ec->code = q.dw.literal_stmt_for_return (scope_die, | |
4850 | addr, | |
4851 | e->components, | |
4852 | lvalue, | |
4853 | fdecl->type); | |
4854 | } | |
4855 | else | |
4856 | { | |
4857 | ec->code = q.dw.literal_stmt_for_local (scope_die, | |
4858 | addr, | |
4859 | e->base_name.substr(1), | |
4860 | e->components, | |
4861 | lvalue, | |
4862 | fdecl->type); | |
4863 | } | |
4864 | ||
1b07c728 FCE |
4865 | if (! lvalue) |
4866 | ec->code += "/* pure */"; | |
66d284f4 FCE |
4867 | } |
4868 | catch (const semantic_error& er) | |
4869 | { | |
3bd0d4df RA |
4870 | if (!q.sess.skip_badvars) |
4871 | { | |
4872 | // We suppress this error message, and pass the unresolved | |
4873 | // target_symbol to the next pass. We hope that this value ends | |
4874 | // up not being referenced after all, so it can be optimized out | |
4875 | // quietly. | |
4876 | provide (e); | |
4877 | semantic_error* saveme = new semantic_error (er); // copy it | |
4878 | saveme->tok1 = e->tok; // XXX: token not passed to q.dw code generation routines | |
4879 | // NB: we can have multiple errors, since a $target variable | |
4880 | // may be expanded in several different contexts: | |
4881 | // function ("*") { $var } | |
4882 | saveme->chain = e->saved_conversion_error; | |
4883 | e->saved_conversion_error = saveme; | |
4884 | } | |
4885 | else | |
4886 | { | |
4887 | // Upon user request for ignoring context, the symbol is replaced | |
4888 | // with a literal 0 and a warning message displayed | |
4889 | literal_number* ln_zero = new literal_number (0); | |
4890 | ln_zero->tok = e->tok; | |
4891 | provide (ln_zero); | |
4892 | q.sess.print_warning ("Bad variable being substituted with literal 0", | |
4893 | e->tok); | |
4894 | } | |
1cde5ba5 JS |
4895 | delete fdecl; |
4896 | delete ec; | |
cbfbbf69 | 4897 | return; |
66d284f4 | 4898 | } |
e57b735a | 4899 | |
d7f3e0c5 GH |
4900 | fdecl->name = fname; |
4901 | fdecl->body = ec; | |
e57b735a GH |
4902 | if (lvalue) |
4903 | { | |
4904 | // Modify the fdecl so it carries a single pe_long formal | |
4905 | // argument called "value". | |
4906 | ||
4907 | // FIXME: For the time being we only support setting target | |
4908 | // variables which have base types; these are 'pe_long' in | |
4909 | // stap's type vocabulary. Strings and pointers might be | |
4910 | // reasonable, some day, but not today. | |
4911 | ||
4912 | vardecl *v = new vardecl; | |
4913 | v->type = pe_long; | |
4914 | v->name = "value"; | |
4915 | v->tok = e->tok; | |
4916 | fdecl->formal_args.push_back(v); | |
4917 | } | |
f76427a2 | 4918 | q.sess.functions[fdecl->name]=fdecl; |
d9b516ca | 4919 | |
e57b735a | 4920 | // Synthesize a functioncall. |
d7f3e0c5 GH |
4921 | functioncall* n = new functioncall; |
4922 | n->tok = e->tok; | |
4923 | n->function = fname; | |
35d4ab18 | 4924 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session |
e57b735a GH |
4925 | |
4926 | if (lvalue) | |
4927 | { | |
4928 | // Provide the functioncall to our parent, so that it can be | |
4929 | // used to substitute for the assignment node immediately above | |
4930 | // us. | |
4931 | assert(!target_symbol_setter_functioncalls.empty()); | |
4932 | *(target_symbol_setter_functioncalls.top()) = n; | |
4933 | } | |
4934 | ||
4ed05b15 | 4935 | provide (n); |
77de5e9e GH |
4936 | } |
4937 | ||
4938 | ||
c24447be JS |
4939 | void |
4940 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
4941 | { | |
4942 | // Fill in our current module context if needed | |
4943 | if (e->module.empty()) | |
4944 | e->module = q.dw.module_name; | |
4945 | ||
4946 | var_expanding_visitor::visit_cast_op(e); | |
4947 | } | |
4948 | ||
4949 | ||
c4ce66a1 JS |
4950 | struct dwarf_cast_query : public base_query |
4951 | { | |
4952 | const cast_op& e; | |
4953 | const bool lvalue; | |
c4ce66a1 | 4954 | |
abb41d92 JS |
4955 | exp_type& pe_type; |
4956 | string& code; | |
c4ce66a1 | 4957 | |
abb41d92 JS |
4958 | dwarf_cast_query(dwflpp& dw, const string& module, const cast_op& e, |
4959 | bool lvalue, exp_type& pe_type, string& code): | |
4960 | base_query(dw, module), e(e), lvalue(lvalue), | |
4961 | pe_type(pe_type), code(code) {} | |
c4ce66a1 JS |
4962 | |
4963 | void handle_query_module(); | |
4964 | int handle_query_cu(Dwarf_Die * cudie); | |
4965 | ||
4966 | static int cast_query_cu (Dwarf_Die * cudie, void * arg); | |
4967 | }; | |
4968 | ||
4969 | ||
c4ce66a1 JS |
4970 | void |
4971 | dwarf_cast_query::handle_query_module() | |
4972 | { | |
abb41d92 | 4973 | if (!code.empty()) |
c4ce66a1 JS |
4974 | return; |
4975 | ||
4976 | // look for the type in each CU | |
4977 | dw.iterate_over_cus(cast_query_cu, this); | |
4978 | } | |
4979 | ||
4980 | ||
4981 | int | |
4982 | dwarf_cast_query::handle_query_cu(Dwarf_Die * cudie) | |
4983 | { | |
abb41d92 | 4984 | if (!code.empty()) |
c4ce66a1 JS |
4985 | return DWARF_CB_ABORT; |
4986 | ||
4987 | dw.focus_on_cu (cudie); | |
4988 | Dwarf_Die* type_die = dw.declaration_resolve(e.type.c_str()); | |
4989 | if (type_die) | |
4990 | { | |
4991 | try | |
4992 | { | |
4993 | code = dw.literal_stmt_for_pointer (type_die, e.components, | |
4994 | lvalue, pe_type); | |
4995 | } | |
4996 | catch (const semantic_error& e) | |
4997 | { | |
4998 | // XXX might be better to save the error | |
4999 | // and try again in another CU | |
5000 | sess.print_error (e); | |
c4ce66a1 | 5001 | } |
c4ce66a1 JS |
5002 | return DWARF_CB_ABORT; |
5003 | } | |
5004 | return DWARF_CB_OK; | |
5005 | } | |
5006 | ||
5007 | ||
5008 | int | |
5009 | dwarf_cast_query::cast_query_cu (Dwarf_Die * cudie, void * arg) | |
5010 | { | |
5011 | dwarf_cast_query * q = static_cast<dwarf_cast_query *>(arg); | |
5012 | if (pending_interrupts) return DWARF_CB_ABORT; | |
5013 | return q->handle_query_cu(cudie); | |
5014 | } | |
5015 | ||
5016 | ||
5017 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor | |
5018 | { | |
5019 | systemtap_session& s; | |
5020 | dwarf_builder& db; | |
5021 | ||
5022 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
5023 | s(s), db(db) {} | |
5024 | void visit_cast_op (cast_op* e); | |
5025 | }; | |
5026 | ||
5027 | ||
5028 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) | |
5029 | { | |
5030 | bool lvalue = is_active_lvalue(e); | |
5031 | if (lvalue && !s.guru_mode) | |
5032 | throw semantic_error("write to typecast value not permitted", e->tok); | |
5033 | ||
5034 | if (e->module.empty()) | |
5035 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
5036 | ||
c4ce66a1 JS |
5037 | string code; |
5038 | exp_type type = pe_long; | |
30369ac1 | 5039 | size_t mod_end = ~0; |
abb41d92 | 5040 | do |
c4ce66a1 | 5041 | { |
abb41d92 JS |
5042 | // split the module string by ':' for alternatives |
5043 | size_t mod_begin = mod_end + 1; | |
5044 | mod_end = e->module.find(':', mod_begin); | |
139dee87 | 5045 | string module = e->module.substr(mod_begin, mod_end - mod_begin); |
abb41d92 | 5046 | |
c4ce66a1 JS |
5047 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
5048 | // which means that userspace modules won't get any PATH searching. | |
5049 | dwflpp* dw; | |
abb41d92 | 5050 | if (module.find('/') == string::npos) |
c4ce66a1 JS |
5051 | { |
5052 | // kernel or kernel module target | |
5053 | if (! db.kern_dw) | |
5054 | { | |
5055 | db.kern_dw = new dwflpp(s); | |
5056 | db.kern_dw->setup_kernel(true); | |
5057 | } | |
5058 | dw = db.kern_dw; | |
5059 | } | |
5060 | else | |
5061 | { | |
abb41d92 | 5062 | module = find_executable (module); // canonicalize it |
c4ce66a1 JS |
5063 | |
5064 | // user-space target; we use one dwflpp instance per module name | |
5065 | // (= program or shared library) | |
abb41d92 | 5066 | if (db.user_dw.find(module) == db.user_dw.end()) |
c4ce66a1 JS |
5067 | { |
5068 | dw = new dwflpp(s); | |
abb41d92 JS |
5069 | dw->setup_user(module); |
5070 | db.user_dw[module] = dw; | |
c4ce66a1 JS |
5071 | } |
5072 | else | |
abb41d92 | 5073 | dw = db.user_dw[module]; |
c4ce66a1 JS |
5074 | } |
5075 | ||
abb41d92 JS |
5076 | dwarf_cast_query q (*dw, module, *e, lvalue, type, code); |
5077 | dw->query_modules(&q); | |
c4ce66a1 | 5078 | } |
abb41d92 JS |
5079 | while (code.empty() && mod_end != string::npos); |
5080 | ||
5081 | if (code.empty()) | |
c4ce66a1 | 5082 | { |
abb41d92 | 5083 | // We generate an error message, and pass the unresolved |
c4ce66a1 JS |
5084 | // cast_op to the next pass. We hope that this value ends |
5085 | // up not being referenced after all, so it can be optimized out | |
5086 | // quietly. | |
d0cd971e MW |
5087 | string msg = "type definition '" + e->type + "' not found"; |
5088 | semantic_error* er = new semantic_error (msg, e->tok); | |
c4ce66a1 JS |
5089 | // NB: we can have multiple errors, since a @cast |
5090 | // may be expanded in several different contexts: | |
5091 | // function ("*") { @cast(...) } | |
abb41d92 JS |
5092 | er->chain = e->saved_conversion_error; |
5093 | e->saved_conversion_error = er; | |
c4ce66a1 JS |
5094 | provide (e); |
5095 | return; | |
5096 | } | |
5097 | ||
5098 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") | |
5099 | + "_" + e->base_name.substr(1) | |
5100 | + "_" + lex_cast<string>(tick++)); | |
5101 | ||
5102 | // Synthesize a function. | |
5103 | functiondecl *fdecl = new functiondecl; | |
5104 | fdecl->tok = e->tok; | |
5105 | fdecl->type = type; | |
5106 | fdecl->name = fname; | |
5107 | ||
5108 | embeddedcode *ec = new embeddedcode; | |
5109 | ec->tok = e->tok; | |
5110 | ec->code = code; | |
5111 | fdecl->body = ec; | |
5112 | ||
5113 | // Give the fdecl an argument for the pointer we're trying to cast | |
5114 | vardecl *v1 = new vardecl; | |
5115 | v1->type = pe_long; | |
5116 | v1->name = "pointer"; | |
5117 | v1->tok = e->tok; | |
5118 | fdecl->formal_args.push_back(v1); | |
5119 | ||
5120 | if (lvalue) | |
5121 | { | |
5122 | // Modify the fdecl so it carries a second pe_long formal | |
5123 | // argument called "value". | |
5124 | ||
5125 | // FIXME: For the time being we only support setting target | |
5126 | // variables which have base types; these are 'pe_long' in | |
5127 | // stap's type vocabulary. Strings and pointers might be | |
5128 | // reasonable, some day, but not today. | |
5129 | ||
5130 | vardecl *v2 = new vardecl; | |
5131 | v2->type = pe_long; | |
5132 | v2->name = "value"; | |
5133 | v2->tok = e->tok; | |
5134 | fdecl->formal_args.push_back(v2); | |
5135 | } | |
5136 | else | |
5137 | ec->code += "/* pure */"; | |
5138 | ||
5139 | s.functions[fdecl->name] = fdecl; | |
5140 | ||
5141 | // Synthesize a functioncall. | |
5142 | functioncall* n = new functioncall; | |
5143 | n->tok = e->tok; | |
5144 | n->function = fname; | |
5145 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
5146 | n->args.push_back(e->operand); | |
5147 | ||
5148 | if (lvalue) | |
5149 | { | |
5150 | // Provide the functioncall to our parent, so that it can be | |
5151 | // used to substitute for the assignment node immediately above | |
5152 | // us. | |
5153 | assert(!target_symbol_setter_functioncalls.empty()); | |
5154 | *(target_symbol_setter_functioncalls.top()) = n; | |
5155 | } | |
5156 | ||
5157 | provide (n); | |
5158 | } | |
5159 | ||
5160 | ||
b8da0ad1 FCE |
5161 | void |
5162 | dwarf_derived_probe::printsig (ostream& o) const | |
5163 | { | |
5164 | // Instead of just printing the plain locations, we add a PC value | |
5165 | // as a comment as a way of telling e.g. apart multiple inlined | |
5166 | // function instances. This is distinct from the verbose/clog | |
5167 | // output, since this part goes into the cache hash calculations. | |
5168 | sole_location()->print (o); | |
6d0f3f0c | 5169 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
5170 | printsig_nested (o); |
5171 | } | |
5172 | ||
5173 | ||
5174 | ||
dc38c0ae | 5175 | void |
b20febf3 FCE |
5176 | dwarf_derived_probe::join_group (systemtap_session& s) |
5177 | { | |
5178 | if (! s.dwarf_derived_probes) | |
5179 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
5180 | s.dwarf_derived_probes->enroll (this); | |
5181 | } | |
5182 | ||
5183 | ||
5184 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, | |
5185 | const string& filename, | |
5186 | int line, | |
91af0778 | 5187 | // module & section specify a relocation |
b20febf3 FCE |
5188 | // base for <addr>, unless section=="" |
5189 | // (equivalently module=="kernel") | |
5190 | const string& module, | |
5191 | const string& section, | |
5192 | // NB: dwfl_addr is the virtualized | |
5193 | // address for this symbol. | |
5194 | Dwarf_Addr dwfl_addr, | |
5195 | // addr is the section-offset for | |
5196 | // actual relocation. | |
5197 | Dwarf_Addr addr, | |
5198 | dwarf_query& q, | |
37ebca01 | 5199 | Dwarf_Die* scope_die /* may be null */) |
1939ea32 | 5200 | : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ), |
b20febf3 | 5201 | module (module), section (section), addr (addr), |
c9bad430 DS |
5202 | has_return (q.has_return), |
5203 | has_maxactive (q.has_maxactive), | |
5204 | maxactive_val (q.maxactive_val) | |
bd2b1e68 | 5205 | { |
b20febf3 | 5206 | // Assert relocation invariants |
4baf0e53 | 5207 | if (section == "" && dwfl_addr != addr) // addr should be absolute |
84048984 FCE |
5208 | throw semantic_error ("missing relocation base against", q.base_loc->tok); |
5209 | if (section != "" && dwfl_addr == addr) // addr should be an offset | |
b20febf3 | 5210 | throw semantic_error ("inconsistent relocation address", q.base_loc->tok); |
df8fadee | 5211 | |
b20febf3 | 5212 | this->tok = q.base_probe->tok; |
b95e2b79 | 5213 | this->access_vars = false; |
2930abc7 | 5214 | |
21beacc9 FCE |
5215 | // XXX: hack for strange g++/gcc's |
5216 | #ifndef USHRT_MAX | |
5217 | #define USHRT_MAX 32767 | |
5218 | #endif | |
5219 | ||
606fd9c8 FCE |
5220 | // Range limit maxactive() value |
5221 | if (q.has_maxactive && (q.maxactive_val < 0 || q.maxactive_val > USHRT_MAX)) | |
5222 | throw semantic_error ("maxactive value out of range [0," | |
5223 | + lex_cast<string>(USHRT_MAX) + "]", | |
5224 | q.base_loc->tok); | |
5225 | ||
de688825 | 5226 | // Expand target variables in the probe body |
5f0a03a6 | 5227 | if (!null_die(scope_die)) |
8fc05e57 | 5228 | { |
de688825 | 5229 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
4ed05b15 | 5230 | this->body = v.require (this->body); |
b95e2b79 | 5231 | this->access_vars = v.visited; |
37ebca01 FCE |
5232 | |
5233 | // If during target-variable-expanding the probe, we added a new block | |
5234 | // of code, add it to the start of the probe. | |
5235 | if (v.add_block) | |
ba6f838d | 5236 | this->body = new block(v.add_block, this->body); |
37ebca01 FCE |
5237 | // If when target-variable-expanding the probe, we added a new |
5238 | // probe, add it in a new file to the list of files to be processed. | |
5239 | if (v.add_probe) | |
5240 | { | |
5241 | stapfile *f = new stapfile; | |
5242 | f->probes.push_back(v.add_probe); | |
5243 | q.sess.files.push_back(f); | |
5244 | } | |
8fc05e57 | 5245 | } |
37ebca01 | 5246 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 5247 | |
5d23847d | 5248 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
5249 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
5250 | // point. This allows a user to see what function / file / line | |
5251 | // number any particular match of the wildcards. | |
2930abc7 | 5252 | |
a229fcd7 | 5253 | vector<probe_point::component*> comps; |
91af0778 FCE |
5254 | if (q.has_kernel) |
5255 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
5256 | else if(q.has_module) | |
5257 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
5258 | else if(q.has_process) | |
5259 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
5260 | else | |
5261 | assert (0); | |
b5d77020 | 5262 | |
db520b00 FCE |
5263 | string fn_or_stmt; |
5264 | if (q.has_function_str || q.has_function_num) | |
5265 | fn_or_stmt = "function"; | |
5266 | else | |
5267 | fn_or_stmt = "statement"; | |
a229fcd7 | 5268 | |
b8da0ad1 | 5269 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 5270 | { |
4cd232e4 | 5271 | string retro_name = funcname; |
b20febf3 | 5272 | if (filename != "") |
cee35f73 | 5273 | { |
fb84c077 | 5274 | retro_name += ("@" + string (filename)); |
cee35f73 | 5275 | if (line > 0) |
fb84c077 | 5276 | retro_name += (":" + lex_cast<string> (line)); |
cee35f73 | 5277 | } |
db520b00 FCE |
5278 | comps.push_back |
5279 | (new probe_point::component | |
5280 | (fn_or_stmt, new literal_string (retro_name))); | |
5281 | } | |
b8da0ad1 | 5282 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
5283 | { |
5284 | Dwarf_Addr retro_addr; | |
5285 | if (q.has_function_num) | |
5286 | retro_addr = q.function_num_val; | |
5287 | else | |
5288 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
5289 | comps.push_back (new probe_point::component |
5290 | (fn_or_stmt, | |
5291 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
37ebca01 FCE |
5292 | |
5293 | if (q.has_absolute) | |
5294 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
5295 | } |
5296 | ||
b8da0ad1 FCE |
5297 | if (q.has_call) |
5298 | comps.push_back (new probe_point::component(TOK_CALL)); | |
5299 | if (q.has_inline) | |
5300 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 5301 | if (has_return) |
b8da0ad1 FCE |
5302 | comps.push_back (new probe_point::component(TOK_RETURN)); |
5303 | if (has_maxactive) | |
5304 | comps.push_back (new probe_point::component | |
5305 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 5306 | |
5d23847d FCE |
5307 | // Overwrite it. |
5308 | this->sole_location()->components = comps; | |
2930abc7 FCE |
5309 | } |
5310 | ||
bd2b1e68 | 5311 | |
7a053d3b | 5312 | void |
20c6c071 | 5313 | dwarf_derived_probe::register_statement_variants(match_node * root, |
bd2b1e68 GH |
5314 | dwarf_builder * dw) |
5315 | { | |
54efe513 | 5316 | root->bind(dw); |
54efe513 GH |
5317 | } |
5318 | ||
7a053d3b | 5319 | void |
fd6602a0 | 5320 | dwarf_derived_probe::register_function_variants(match_node * root, |
c9bad430 | 5321 | dwarf_builder * dw) |
bd2b1e68 | 5322 | { |
fd6602a0 | 5323 | root->bind(dw); |
b8da0ad1 FCE |
5324 | root->bind(TOK_INLINE)->bind(dw); |
5325 | root->bind(TOK_CALL)->bind(dw); | |
fd6602a0 | 5326 | root->bind(TOK_RETURN)->bind(dw); |
c9bad430 | 5327 | root->bind(TOK_RETURN)->bind_num(TOK_MAXACTIVE)->bind(dw); |
bd2b1e68 GH |
5328 | } |
5329 | ||
7a053d3b | 5330 | void |
20c6c071 | 5331 | dwarf_derived_probe::register_function_and_statement_variants(match_node * root, |
bd2b1e68 GH |
5332 | dwarf_builder * dw) |
5333 | { | |
5334 | // Here we match 4 forms: | |
5335 | // | |
5336 | // .function("foo") | |
5337 | // .function(0xdeadbeef) | |
5338 | // .statement("foo") | |
5339 | // .statement(0xdeadbeef) | |
5340 | ||
fd6602a0 FCE |
5341 | register_function_variants(root->bind_str(TOK_FUNCTION), dw); |
5342 | register_function_variants(root->bind_num(TOK_FUNCTION), dw); | |
20c6c071 GH |
5343 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw); |
5344 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw); | |
bd2b1e68 GH |
5345 | } |
5346 | ||
5347 | void | |
c4ce66a1 | 5348 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 5349 | { |
c4ce66a1 | 5350 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
5351 | dwarf_builder *dw = new dwarf_builder(); |
5352 | ||
c4ce66a1 JS |
5353 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
5354 | s.code_filters.push_back(filter); | |
5355 | ||
20c6c071 GH |
5356 | register_function_and_statement_variants(root->bind(TOK_KERNEL), dw); |
5357 | register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw); | |
37ebca01 | 5358 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(dw); |
0f336e95 SC |
5359 | root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); |
5360 | root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); | |
37ebca01 | 5361 | |
7a24d422 | 5362 | register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw); |
f28a8c28 SC |
5363 | root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)->bind(dw); |
5364 | root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK)->bind(dw); | |
bd2b1e68 GH |
5365 | } |
5366 | ||
9020300d FCE |
5367 | void |
5368 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
5369 | { | |
b95e2b79 MH |
5370 | if (access_vars) |
5371 | { | |
5372 | // if accessing $variables, emit bsp cache setup for speeding up | |
5373 | o->newline() << "bspcache(c->unwaddr, c->regs);"; | |
5374 | } | |
9020300d | 5375 | } |
2930abc7 | 5376 | |
b20febf3 | 5377 | // ------------------------------------------------------------------------ |
46b84a80 DS |
5378 | |
5379 | void | |
b20febf3 | 5380 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 5381 | { |
b20febf3 | 5382 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
5383 | |
5384 | // XXX: probes put at the same address should all share a | |
5385 | // single kprobe/kretprobe, and have their handlers executed | |
5386 | // sequentially. | |
b55bc428 FCE |
5387 | } |
5388 | ||
2930abc7 | 5389 | |
7a053d3b | 5390 | void |
775d51e5 | 5391 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 5392 | { |
b20febf3 | 5393 | if (probes_by_module.empty()) return; |
2930abc7 | 5394 | |
775d51e5 DS |
5395 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
5396 | ||
5397 | // Warn of misconfigured kernels | |
f41595cc FCE |
5398 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
5399 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
5400 | s.op->newline() << "#endif"; | |
775d51e5 | 5401 | s.op->newline(); |
f41595cc | 5402 | |
b20febf3 FCE |
5403 | // Forward declare the master entry functions |
5404 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5405 | s.op->line() << " struct pt_regs *regs);"; | |
5406 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5407 | s.op->line() << " struct pt_regs *regs);"; | |
5408 | ||
42cb22bd MH |
5409 | // Emit an array of kprobe/kretprobe pointers |
5410 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5411 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
5412 | s.op->newline() << "#endif"; | |
5413 | ||
b20febf3 | 5414 | // Emit the actual probe list. |
606fd9c8 FCE |
5415 | |
5416 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
5417 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
5418 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
4c2732a1 | 5419 | s.op->newline() << "static struct stap_dwarf_kprobe {"; |
b20febf3 | 5420 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; |
e4cb375f MH |
5421 | s.op->newline() << "#ifdef __ia64__"; |
5422 | s.op->newline() << "struct kprobe dummy;"; | |
5423 | s.op->newline() << "#endif"; | |
606fd9c8 FCE |
5424 | s.op->newline(-1) << "} stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
5425 | // NB: bss! | |
5426 | ||
4c2732a1 | 5427 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
5428 | s.op->newline(1) << "const unsigned return_p:1;"; |
5429 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b20febf3 | 5430 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 5431 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 FCE |
5432 | |
5433 | // Let's find some stats for the three embedded strings. Maybe they | |
5434 | // are small and uniform enough to justify putting char[MAX]'s into | |
5435 | // the array instead of relocated char*'s. | |
5436 | size_t module_name_max = 0, section_name_max = 0, pp_name_max = 0; | |
5437 | size_t module_name_tot = 0, section_name_tot = 0, pp_name_tot = 0; | |
5438 | size_t all_name_cnt = probes_by_module.size(); // for average | |
5439 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
5440 | { | |
5441 | dwarf_derived_probe* p = it->second; | |
5442 | #define DOIT(var,expr) do { \ | |
5443 | size_t var##_size = (expr) + 1; \ | |
5444 | var##_max = max (var##_max, var##_size); \ | |
5445 | var##_tot += var##_size; } while (0) | |
5446 | DOIT(module_name, p->module.size()); | |
5447 | DOIT(section_name, p->section.size()); | |
5448 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
5449 | #undef DOIT | |
5450 | } | |
5451 | ||
5452 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
5453 | // the amount of average waste (max - avg) to the relocation data size | |
5454 | // (3 native long words). | |
5455 | #define CALCIT(var) \ | |
5456 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
5457 | { \ | |
5458 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
5459 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
5460 | << "[" << var##_name_max << "]" << endl; \ | |
5461 | } \ | |
5462 | else \ | |
5463 | { \ | |
b0986e7a | 5464 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
5465 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
5466 | } | |
5467 | ||
5468 | CALCIT(module); | |
5469 | CALCIT(section); | |
5470 | CALCIT(pp); | |
5471 | ||
b0986e7a DS |
5472 | s.op->newline() << "const unsigned long address;"; |
5473 | s.op->newline() << "void (* const ph) (struct context*);"; | |
b20febf3 FCE |
5474 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
5475 | s.op->indent(1); | |
5476 | ||
5477 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 5478 | { |
b20febf3 FCE |
5479 | dwarf_derived_probe* p = it->second; |
5480 | s.op->newline() << "{"; | |
5481 | if (p->has_return) | |
5482 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 5483 | if (p->has_maxactive) |
606fd9c8 FCE |
5484 | { |
5485 | s.op->line() << " .maxactive_p=1,"; | |
5486 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
5487 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
5488 | } | |
dc38c256 | 5489 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
5490 | s.op->line() << " .module=\"" << p->module << "\","; |
5491 | s.op->line() << " .section=\"" << p->section << "\","; | |
b20febf3 FCE |
5492 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
5493 | s.op->line() << " .ph=&" << p->name; | |
5494 | s.op->line() << " },"; | |
2930abc7 | 5495 | } |
2930abc7 | 5496 | |
b20febf3 FCE |
5497 | s.op->newline(-1) << "};"; |
5498 | ||
5499 | // Emit the kprobes callback function | |
5500 | s.op->newline(); | |
5501 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5502 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
5503 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
5504 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
5505 | // Check that the index is plausible | |
5506 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5507 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5508 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5509 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5510 | s.op->line() << "];"; | |
c12d974f | 5511 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
5512 | s.op->newline() << "c->regs = regs;"; |
5513 | s.op->newline() << "(*sdp->ph) (c);"; | |
5514 | common_probe_entryfn_epilogue (s.op); | |
5515 | s.op->newline() << "return 0;"; | |
5516 | s.op->newline(-1) << "}"; | |
5517 | ||
5518 | // Same for kretprobes | |
5519 | s.op->newline(); | |
5520 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5521 | s.op->line() << " struct pt_regs *regs) {"; | |
5522 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
606fd9c8 FCE |
5523 | |
5524 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 5525 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
5526 | // Check that the index is plausible |
5527 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5528 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5529 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5530 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5531 | s.op->line() << "];"; | |
5532 | ||
c12d974f | 5533 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
5534 | s.op->newline() << "c->regs = regs;"; |
5535 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
5536 | s.op->newline() << "(*sdp->ph) (c);"; | |
5537 | common_probe_entryfn_epilogue (s.op); | |
5538 | s.op->newline() << "return 0;"; | |
5539 | s.op->newline(-1) << "}"; | |
20c6c071 | 5540 | } |
ec4373ff | 5541 | |
20c6c071 | 5542 | |
dc38c0ae | 5543 | void |
b20febf3 FCE |
5544 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
5545 | { | |
5546 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5547 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5548 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
f1bad60c | 5549 | s.op->newline() << "unsigned long relocated_addr = _stp_module_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 5550 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
6d0f3f0c | 5551 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
b20febf3 | 5552 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 5553 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 5554 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 5555 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 5556 | s.op->newline(-1) << "} else {"; |
606fd9c8 | 5557 | s.op->newline(1) << "kp->u.krp.maxactive = max(10, 4*NR_CPUS);"; |
c9bad430 | 5558 | s.op->newline(-1) << "}"; |
606fd9c8 | 5559 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
e4cb375f MH |
5560 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
5561 | s.op->newline() << "#ifdef __ia64__"; | |
5562 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5563 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5564 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5565 | s.op->newline() << "if (rc == 0) {"; | |
5566 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5567 | s.op->newline() << "if (rc != 0)"; | |
5568 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5569 | s.op->newline(-2) << "}"; | |
5570 | s.op->newline() << "#else"; | |
606fd9c8 | 5571 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 5572 | s.op->newline() << "#endif"; |
b20febf3 | 5573 | s.op->newline(-1) << "} else {"; |
e4cb375f | 5574 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
5575 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
5576 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
5577 | s.op->newline() << "#ifdef __ia64__"; |
5578 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5579 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5580 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5581 | s.op->newline() << "if (rc == 0) {"; | |
5582 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5583 | s.op->newline() << "if (rc != 0)"; | |
5584 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5585 | s.op->newline(-2) << "}"; | |
5586 | s.op->newline() << "#else"; | |
606fd9c8 | 5587 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 5588 | s.op->newline() << "#endif"; |
b20febf3 | 5589 | s.op->newline(-1) << "}"; |
9063462a FCE |
5590 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
5591 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
5592 | s.op->newline() << "_stp_warn (\"probe %s registration error (rc %d)\", probe_point, rc);"; | |
5593 | s.op->newline() << "rc = 0;"; // continue with other probes | |
5594 | // XXX: shall we increment numskipped? | |
5595 | s.op->newline(-1) << "}"; | |
5596 | ||
5597 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 5598 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 5599 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
5600 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
5601 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
5602 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
5603 | s.op->newline() << "#ifdef __ia64__"; |
5604 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
5605 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
5606 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
5607 | // not run for this early-abort case. | |
5608 | s.op->newline(-1) << "}"; | |
5609 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 5610 | s.op->newline(-1) << "}"; |
9063462a FCE |
5611 | #endif |
5612 | ||
b20febf3 FCE |
5613 | s.op->newline() << "else sdp->registered_p = 1;"; |
5614 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
5615 | } |
5616 | ||
5617 | ||
46b84a80 | 5618 | void |
b20febf3 | 5619 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 5620 | { |
42cb22bd MH |
5621 | //Unregister kprobes by batch interfaces. |
5622 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5623 | s.op->newline() << "j = 0;"; | |
5624 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5625 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5626 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5627 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5628 | s.op->newline() << "if (!sdp->return_p)"; | |
5629 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
5630 | s.op->newline(-2) << "}"; | |
5631 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5632 | s.op->newline() << "j = 0;"; | |
5633 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5634 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5635 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5636 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5637 | s.op->newline() << "if (sdp->return_p)"; | |
5638 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
5639 | s.op->newline(-2) << "}"; | |
5640 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
5641 | s.op->newline() << "#ifdef __ia64__"; |
5642 | s.op->newline() << "j = 0;"; | |
5643 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5644 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5645 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5646 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5647 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
5648 | s.op->newline(-1) << "}"; | |
5649 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5650 | s.op->newline() << "#endif"; | |
42cb22bd MH |
5651 | s.op->newline() << "#endif"; |
5652 | ||
b20febf3 FCE |
5653 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
5654 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5655 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
5656 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
5657 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 5658 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5659 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 5660 | s.op->newline() << "#endif"; |
606fd9c8 | 5661 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; |
73209876 FCE |
5662 | s.op->newline() << "#ifdef STP_TIMING"; |
5663 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
d01eaa30 | 5664 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; |
73209876 | 5665 | s.op->newline(-1) << "#endif"; |
606fd9c8 | 5666 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5667 | s.op->newline() << "#ifdef STP_TIMING"; |
5668 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
d01eaa30 | 5669 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; |
73209876 | 5670 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 5671 | s.op->newline(-1) << "} else {"; |
42cb22bd | 5672 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5673 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 5674 | s.op->newline() << "#endif"; |
606fd9c8 | 5675 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5676 | s.op->newline() << "#ifdef STP_TIMING"; |
5677 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
d01eaa30 | 5678 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);"; |
73209876 | 5679 | s.op->newline(-1) << "#endif"; |
b20febf3 | 5680 | s.op->newline(-1) << "}"; |
e4cb375f MH |
5681 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
5682 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5683 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
5684 | s.op->newline() << "sdp->registered_p = 0;"; |
5685 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
5686 | } |
5687 | ||
5688 | ||
20c6c071 | 5689 | void |
5227f1ea | 5690 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 5691 | probe * base, |
20c6c071 | 5692 | probe_point * location, |
86bf665e | 5693 | literal_map_t const & parameters, |
20c6c071 GH |
5694 | vector<derived_probe *> & finished_results) |
5695 | { | |
b20febf3 FCE |
5696 | // NB: the kernel/user dwlfpp objects are long-lived. |
5697 | // XXX: but they should be per-session, as this builder object | |
5698 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 5699 | |
7a24d422 FCE |
5700 | dwflpp* dw = 0; |
5701 | ||
7a24d422 FCE |
5702 | string module_name; |
5703 | if (has_null_param (parameters, TOK_KERNEL) | |
5704 | || get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 5705 | { |
7a24d422 FCE |
5706 | // kernel or kernel module target |
5707 | if (! kern_dw) | |
5708 | { | |
5709 | kern_dw = new dwflpp(sess); | |
28d29bd3 FCE |
5710 | // XXX: PR 3498, PR 6864 |
5711 | kern_dw->setup_kernel(true); | |
7a24d422 FCE |
5712 | } |
5713 | dw = kern_dw; | |
b8da0ad1 | 5714 | } |
7a24d422 | 5715 | else if (get_param (parameters, TOK_PROCESS, module_name)) |
b8da0ad1 | 5716 | { |
d0a7f5a9 FCE |
5717 | module_name = find_executable (module_name); // canonicalize it |
5718 | ||
7a24d422 FCE |
5719 | // user-space target; we use one dwflpp instance per module name |
5720 | // (= program or shared library) | |
5721 | if (user_dw.find(module_name) == user_dw.end()) | |
84048984 | 5722 | { |
7a24d422 FCE |
5723 | dw = new dwflpp(sess); |
5724 | // XXX: PR 3498 | |
5725 | dw->setup_user(module_name); | |
5726 | user_dw[module_name] = dw; | |
84048984 | 5727 | } |
7a24d422 FCE |
5728 | else |
5729 | dw = user_dw[module_name]; | |
c8959a29 | 5730 | } |
20c6c071 | 5731 | |
f28a8c28 | 5732 | if (((probe_point::component*)(location->components[1]))->functor == TOK_MARK) |
349dc70e SC |
5733 | { |
5734 | enum probe_types | |
f28a8c28 | 5735 | { |
7b534f48 SC |
5736 | probes_and_dwarf = 0, // Use statement address |
5737 | dwarf_no_probes = 1, // Use label name | |
5738 | probes_no_dwarf = 2 | |
349dc70e | 5739 | }; |
f28a8c28 | 5740 | |
7b534f48 SC |
5741 | int probe_type = dwarf_no_probes; |
5742 | string probe_name = (char*) location->components[1]->arg->tok->content.c_str(); | |
5743 | __uint64_t probe_arg = 0; | |
349dc70e | 5744 | Dwarf_Addr bias; |
1f0cfd98 | 5745 | Elf* elf = dwfl_module_getelf (dw->module, &bias); |
349dc70e | 5746 | size_t shstrndx; |
349dc70e | 5747 | Elf_Scn *probe_scn = NULL; |
7b534f48 | 5748 | |
349dc70e | 5749 | dwfl_assert ("getshstrndx", elf_getshstrndx (elf, &shstrndx)); |
7b534f48 SC |
5750 | GElf_Shdr *shdr = NULL; |
5751 | ||
5752 | // Is there a .probes section? | |
349dc70e SC |
5753 | while ((probe_scn = elf_nextscn (elf, probe_scn))) |
5754 | { | |
5755 | GElf_Shdr shdr_mem; | |
7b534f48 | 5756 | shdr = gelf_getshdr (probe_scn, &shdr_mem); |
349dc70e SC |
5757 | assert (shdr != NULL); |
5758 | ||
7b534f48 SC |
5759 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0) |
5760 | { | |
5761 | probe_type = probes_and_dwarf; | |
5762 | break; | |
5763 | } | |
5764 | } | |
5765 | ||
9b753eda | 5766 | if (probe_type == probes_and_dwarf) |
7b534f48 | 5767 | { |
1f0cfd98 | 5768 | Elf_Data *pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
349dc70e SC |
5769 | assert (pdata != NULL); |
5770 | size_t probe_scn_offset = 0; | |
c1008fd0 | 5771 | size_t probe_scn_addr = shdr->sh_addr; |
349dc70e SC |
5772 | while (probe_scn_offset < pdata->d_size) |
5773 | { | |
c1008fd0 | 5774 | const int stap_sentinel = 0x31425250; |
1f0cfd98 | 5775 | probe_type = *((int*)((char*)pdata->d_buf + probe_scn_offset)); |
c1008fd0 SC |
5776 | if (probe_type != stap_sentinel) |
5777 | { | |
5778 | probe_scn_offset += sizeof(int); | |
5779 | continue; | |
5780 | } | |
349dc70e | 5781 | probe_scn_offset += sizeof(int); |
c1008fd0 SC |
5782 | if (probe_scn_offset % (sizeof(__uint64_t))) |
5783 | probe_scn_offset += sizeof(__uint64_t) - (probe_scn_offset % sizeof(__uint64_t)); | |
5784 | ||
c1008fd0 SC |
5785 | probe_name = ((char*)((long)(pdata->d_buf) + (long)(*((int*)((long)pdata->d_buf + probe_scn_offset)) - probe_scn_addr))); |
5786 | probe_scn_offset += sizeof(void*); | |
1f0cfd98 SC |
5787 | if (probe_scn_offset % (sizeof(__uint64_t))) |
5788 | probe_scn_offset += sizeof(__uint64_t) - (probe_scn_offset % sizeof(__uint64_t)); | |
5789 | probe_arg = *((__uint64_t*)((char*)pdata->d_buf + probe_scn_offset)); | |
1f0cfd98 SC |
5790 | if (probe_scn_offset % (sizeof(__uint64_t)*2)) |
5791 | probe_scn_offset = (probe_scn_offset + sizeof(__uint64_t)*2) - (probe_scn_offset % (sizeof(__uint64_t)*2)); | |
64c6aab0 | 5792 | if (strcmp (location->components[1]->arg->tok->content.c_str(), probe_name.c_str()) != 0) |
9e67aff9 SC |
5793 | continue; |
5794 | const token* sv_tok = location->components[1]->arg->tok; | |
5795 | location->components[1]->functor = TOK_STATEMENT; | |
5796 | location->components[1]->arg = new literal_number((int)probe_arg); | |
5797 | location->components[1]->arg->tok = sv_tok; | |
5798 | ((literal_map_t&)parameters)[TOK_STATEMENT] = location->components[1]->arg; | |
5799 | dwarf_query q(sess, base, location, *dw, parameters, finished_results); | |
5800 | dw->query_modules(&q); | |
349dc70e | 5801 | } |
64c6aab0 | 5802 | return; |
349dc70e | 5803 | } |
59b2ec52 | 5804 | |
64c6aab0 | 5805 | if (probe_type == dwarf_no_probes) |
349dc70e | 5806 | { |
7b534f48 SC |
5807 | location->components[1]->functor = TOK_FUNCTION; |
5808 | location->components[1]->arg = new literal_string("*"); | |
5809 | ((literal_map_t&)parameters)[TOK_FUNCTION] = location->components[1]->arg; | |
5810 | location->components.push_back(new probe_point::component(TOK_LABEL)); | |
5811 | location->components[2]->arg = new literal_string("_stapprobe1_" + probe_name); | |
5812 | ((literal_map_t&)parameters).erase(TOK_MARK); | |
5813 | ((literal_map_t&)parameters).insert(pair<string,literal*>(TOK_LABEL, location->components[2]->arg)); | |
349dc70e | 5814 | } |
1f0cfd98 | 5815 | |
349dc70e SC |
5816 | dw->module = 0; |
5817 | } | |
5818 | ||
c8959a29 | 5819 | dwarf_query q(sess, base, location, *dw, parameters, finished_results); |
20c6c071 | 5820 | |
7a24d422 FCE |
5821 | |
5822 | // XXX: kernel.statement.absolute is a special case that requires no | |
5823 | // dwfl processing. This code should be in a separate builder. | |
5824 | ||
5825 | if (q.has_kernel && q.has_absolute) | |
37ebca01 | 5826 | { |
4baf0e53 | 5827 | // assert guru mode for absolute probes |
37ebca01 FCE |
5828 | if (! q.base_probe->privileged) |
5829 | { | |
5830 | throw semantic_error ("absolute statement probe in unprivileged script", q.base_probe->tok); | |
5831 | } | |
5832 | ||
5833 | // For kernel.statement(NUM).absolute probe points, we bypass | |
5834 | // all the debuginfo stuff: We just wire up a | |
5835 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 5836 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
5837 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
5838 | q.statement_num_val, q.statement_num_val, | |
5839 | q, 0); | |
37ebca01 | 5840 | finished_results.push_back (p); |
1a0dbc5a | 5841 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
5842 | return; |
5843 | } | |
5844 | ||
5f0a03a6 JK |
5845 | dw->query_modules(&q); |
5846 | } | |
5847 | ||
5848 | symbol_table::~symbol_table() | |
5849 | { | |
2e67a43b TM |
5850 | for (iterator_t i = list_by_addr.begin(); i != list_by_addr.end(); ++i) |
5851 | delete *i; | |
5f0a03a6 JK |
5852 | } |
5853 | ||
5854 | void | |
ab91b232 JK |
5855 | symbol_table::add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
5856 | Dwarf_Addr *high_addr) | |
5f0a03a6 | 5857 | { |
ab91b232 JK |
5858 | #ifdef __powerpc__ |
5859 | // Map ".sys_foo" to "sys_foo". | |
5860 | if (name[0] == '.') | |
5861 | name++; | |
5862 | #endif | |
5f0a03a6 JK |
5863 | func_info *fi = new func_info(); |
5864 | fi->addr = addr; | |
5865 | fi->name = name; | |
ab91b232 | 5866 | fi->weak = weak; |
5f0a03a6 JK |
5867 | map_by_name[fi->name] = fi; |
5868 | // TODO: Use a multimap in case there are multiple static | |
5869 | // functions with the same name? | |
2e67a43b | 5870 | list_by_addr.push_back(fi); |
5f0a03a6 JK |
5871 | } |
5872 | ||
5873 | enum info_status | |
5874 | symbol_table::read_symbols(FILE *f, const string& path) | |
5875 | { | |
5876 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
5877 | int ret; | |
2e67a43b TM |
5878 | char *name = 0; |
5879 | char *mod = 0; | |
5f0a03a6 JK |
5880 | char type; |
5881 | unsigned long long addr; | |
5882 | Dwarf_Addr high_addr = 0; | |
5883 | int line = 0; | |
5884 | ||
5885 | // %as (non-POSIX) mallocs space for the string and stores its address. | |
5886 | while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0) | |
5887 | { | |
2e67a43b TM |
5888 | auto_free free_name(name); |
5889 | auto_free free_mod(mod); | |
5f0a03a6 JK |
5890 | line++; |
5891 | if (ret < 3) | |
5892 | { | |
41c262f3 | 5893 | cerr << "Symbol table error: Line " |
5f0a03a6 JK |
5894 | << line |
5895 | << " of symbol list from " | |
5896 | << path | |
5897 | << " is not in correct format: address type name [module]"; | |
5898 | // Caller should delete symbol_table object. | |
5899 | return info_absent; | |
5900 | } | |
2e67a43b | 5901 | else if (ret > 3) |
5f0a03a6 JK |
5902 | { |
5903 | // Modules are loaded above the kernel, so if we're getting | |
5904 | // modules, we're done. | |
2e67a43b | 5905 | break; |
5f0a03a6 | 5906 | } |
ab91b232 JK |
5907 | if (type == 'T' || type == 't' || type == 'W') |
5908 | add_symbol(name, (type == 'W'), (Dwarf_Addr) addr, &high_addr); | |
5f0a03a6 JK |
5909 | } |
5910 | ||
5f0a03a6 JK |
5911 | if (list_by_addr.size() < 1) |
5912 | { | |
5913 | cerr << "Symbol table error: " | |
5914 | << path << " contains no function symbols." << endl; | |
5915 | return info_absent; | |
5916 | } | |
2e67a43b | 5917 | sort(); |
5f0a03a6 JK |
5918 | return info_present; |
5919 | } | |
5920 | ||
5921 | // NB: This currently unused. We use get_from_elf() instead because | |
5922 | // that gives us raw addresses -- which we need for modules -- whereas | |
5923 | // nm provides the address relative to the beginning of the section. | |
5924 | enum info_status | |
5925 | symbol_table::read_from_elf_file(const string &path) | |
5926 | { | |
5927 | FILE *f; | |
5928 | string cmd = string("/usr/bin/nm -n --defined-only ") + path; | |
5929 | f = popen(cmd.c_str(), "r"); | |
5930 | if (!f) | |
5931 | { | |
5932 | // nm failures are detected by pclose, not popen. | |
5933 | cerr << "Internal error reading symbol table from " | |
5934 | << path << " -- " << strerror (errno); | |
5935 | return info_absent; | |
5936 | } | |
5937 | enum info_status status = read_symbols(f, path); | |
5938 | if (pclose(f) != 0) | |
5939 | { | |
5940 | if (status == info_present) | |
5941 | cerr << "Warning: nm cannot read symbol table from " << path; | |
5942 | return info_absent; | |
5943 | } | |
5944 | return status; | |
5945 | } | |
5946 | ||
5947 | enum info_status | |
5948 | symbol_table::read_from_text_file(const string& path) | |
5949 | { | |
5950 | FILE *f = fopen(path.c_str(), "r"); | |
5951 | if (!f) | |
5952 | { | |
5953 | cerr << "Warning: cannot read symbol table from " | |
5954 | << path << " -- " << strerror (errno); | |
5955 | return info_absent; | |
5956 | } | |
5957 | enum info_status status = read_symbols(f, path); | |
5958 | (void) fclose(f); | |
5959 | return status; | |
5960 | } | |
5961 | ||
46f7b6be JK |
5962 | void |
5963 | symbol_table::prepare_section_rejection(Dwfl_Module *mod) | |
5964 | { | |
5965 | #ifdef __powerpc__ | |
5966 | /* | |
5967 | * The .opd section contains function descriptors that can look | |
5968 | * just like function entry points. For example, there's a function | |
5969 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
5970 | * Reject all symbols in .opd. | |
5971 | */ | |
5972 | opd_section = SHN_UNDEF; | |
5973 | Dwarf_Addr bias; | |
5974 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
5975 | ?: dwfl_module_getelf (mod, &bias)); | |
5976 | Elf_Scn* scn = 0; | |
5977 | size_t shstrndx; | |
5978 | ||
5979 | if (!elf) | |
5980 | return; | |
5981 | if (elf_getshstrndx(elf, &shstrndx) != 0) | |
5982 | return; | |
5983 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
5984 | { | |
5985 | GElf_Shdr shdr_mem; | |
5986 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
5987 | if (!shdr) | |
5988 | continue; | |
5989 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
5990 | if (!strcmp(name, ".opd")) | |
5991 | { | |
5992 | opd_section = elf_ndxscn(scn); | |
5993 | return; | |
5994 | } | |
5995 | } | |
5996 | #endif | |
5997 | } | |
5998 | ||
5999 | bool | |
6000 | symbol_table::reject_section(GElf_Word section) | |
6001 | { | |
6002 | if (section == SHN_UNDEF) | |
6003 | return true; | |
6004 | #ifdef __powerpc__ | |
6005 | if (section == opd_section) | |
6006 | return true; | |
6007 | #endif | |
6008 | return false; | |
6009 | } | |
6010 | ||
5f0a03a6 JK |
6011 | enum info_status |
6012 | symbol_table::get_from_elf() | |
6013 | { | |
6014 | Dwarf_Addr high_addr = 0; | |
6015 | Dwfl_Module *mod = mod_info->mod; | |
6016 | int syments = dwfl_module_getsymtab(mod); | |
6017 | assert(syments); | |
46f7b6be | 6018 | prepare_section_rejection(mod); |
5f0a03a6 JK |
6019 | for (int i = 1; i < syments; ++i) |
6020 | { | |
6021 | GElf_Sym sym; | |
ab91b232 JK |
6022 | GElf_Word section; |
6023 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
46f7b6be JK |
6024 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC && |
6025 | !reject_section(section)) | |
ab91b232 JK |
6026 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
6027 | sym.st_value, &high_addr); | |
5f0a03a6 | 6028 | } |
2e67a43b | 6029 | sort(); |
5f0a03a6 JK |
6030 | return info_present; |
6031 | } | |
6032 | ||
6033 | void | |
6034 | symbol_table::mark_dwarf_redundancies(dwflpp *dw) | |
6035 | { | |
6036 | // dwflpp.cu_function_cache maps each module_name:cu_name to a | |
6037 | // vector of Dwarf_Dies, one per function. | |
6038 | string module_prefix = string(mod_info->name) + ":"; | |
6039 | ||
73f289b2 | 6040 | for (mod_cu_function_cache_t::iterator cu = dw->cu_function_cache.begin(); |
6561773f | 6041 | cu != dw->cu_function_cache.end(); cu++) |
5f0a03a6 JK |
6042 | { |
6043 | string key = cu->first; | |
6044 | if (key.find(module_prefix) == 0) | |
6045 | { | |
6046 | // Found a compilation unit in the module of interest. | |
6047 | // Mark all its functions in the symbol table. | |
6561773f | 6048 | cu_function_cache_t* v = cu->second; |
5f0a03a6 | 6049 | assert(v); |
6561773f | 6050 | for (cu_function_cache_t::iterator fc = v->begin(); fc != v->end(); fc++) |
5f0a03a6 | 6051 | { |
6561773f FCE |
6052 | Dwarf_Die func = fc->second; |
6053 | string func_name = fc->first; // == dwarf_diename(&func); | |
5f0a03a6 JK |
6054 | // map_by_name[func_name]->die = func; |
6055 | map<string, func_info*>::iterator i = map_by_name.find(func_name); | |
6056 | // Func names can show up in the dwarf but not the symtab (!). | |
6057 | if (i != map_by_name.end()) | |
6058 | { | |
6059 | func_info *fi = i->second; | |
6060 | fi->die = func; | |
6061 | } | |
6062 | } | |
6063 | } | |
6064 | } | |
6065 | } | |
6066 | ||
6067 | func_info * | |
6068 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
6069 | { | |
2e67a43b TM |
6070 | iterator_t iter = upper_bound(list_by_addr.begin(), list_by_addr.end(), addr, |
6071 | func_info::Compare()); | |
6072 | if (iter == list_by_addr.begin()) | |
5f0a03a6 | 6073 | return NULL; |
2e67a43b TM |
6074 | else |
6075 | return *(iter - 1); | |
5f0a03a6 JK |
6076 | } |
6077 | ||
6078 | func_info * | |
6079 | symbol_table::lookup_symbol(const string& name) | |
6080 | { | |
6081 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
6082 | if (i == map_by_name.end()) | |
6083 | return NULL; | |
6084 | return i->second; | |
6085 | } | |
6086 | ||
6087 | Dwarf_Addr | |
6088 | symbol_table::lookup_symbol_address(const string& name) | |
6089 | { | |
6090 | func_info *fi = lookup_symbol(name); | |
6091 | if (fi) | |
6092 | return fi->addr; | |
6093 | return 0; | |
6094 | } | |
6095 | ||
ab91b232 JK |
6096 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
6097 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
6098 | // For system calls not implemented elsewhere, this weak symbol shows up | |
6099 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
6100 | // we refuse to consider such symbols. Here we delete them from our | |
6101 | // symbol table. | |
6102 | // TODO: Consider generalizing this and/or making it part of blacklist | |
6103 | // processing. | |
6104 | void | |
6105 | symbol_table::purge_syscall_stubs() | |
6106 | { | |
6107 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
6108 | if (stub_addr == 0) | |
6109 | return; | |
2e67a43b TM |
6110 | range_t purge_range = equal_range(list_by_addr.begin(), list_by_addr.end(), |
6111 | stub_addr, func_info::Compare()); | |
6112 | for (iterator_t iter = purge_range.first; | |
6113 | iter != purge_range.second; | |
6114 | ++iter) | |
ab91b232 | 6115 | { |
2e67a43b TM |
6116 | func_info *fi = *iter; |
6117 | if (fi->weak && fi->name != "sys_ni_syscall") | |
ab91b232 | 6118 | { |
2e67a43b TM |
6119 | map_by_name.erase(fi->name); |
6120 | delete fi; | |
6121 | *iter = 0; | |
6122 | } | |
ab91b232 | 6123 | } |
2e67a43b TM |
6124 | // Range might have null pointer entries that should be erased. |
6125 | list_by_addr.erase(remove(purge_range.first, purge_range.second, | |
6126 | (func_info*)0), | |
6127 | purge_range.second); | |
6128 | } | |
6129 | ||
6130 | void | |
6131 | symbol_table::sort() | |
6132 | { | |
6133 | stable_sort(list_by_addr.begin(), list_by_addr.end(), func_info::Compare()); | |
ab91b232 JK |
6134 | } |
6135 | ||
5f0a03a6 JK |
6136 | void |
6137 | module_info::get_symtab(dwarf_query *q) | |
6138 | { | |
6139 | systemtap_session &sess = q->sess; | |
6140 | ||
6141 | sym_table = new symbol_table(this); | |
6142 | if (!elf_path.empty()) | |
6143 | { | |
6144 | if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()) | |
6145 | cerr << "Warning: reading symbol table from " | |
6146 | << elf_path | |
6147 | << " -- ignoring " | |
6148 | << sess.kernel_symtab_path | |
6149 | << endl ;; | |
6150 | symtab_status = sym_table->get_from_elf(); | |
6151 | } | |
6152 | else | |
6153 | { | |
6154 | assert(name == TOK_KERNEL); | |
6155 | if (sess.kernel_symtab_path.empty()) | |
6156 | { | |
6157 | symtab_status = info_absent; | |
6158 | cerr << "Error: Cannot find vmlinux." | |
6159 | << " Consider using --kmap instead of --kelf." | |
6160 | << endl;; | |
6161 | } | |
6162 | else | |
6163 | { | |
6164 | symtab_status = | |
6165 | sym_table->read_from_text_file(sess.kernel_symtab_path); | |
6166 | if (symtab_status == info_present) | |
6167 | { | |
6168 | sess.sym_kprobes_text_start = | |
6169 | sym_table->lookup_symbol_address("__kprobes_text_start"); | |
6170 | sess.sym_kprobes_text_end = | |
6171 | sym_table->lookup_symbol_address("__kprobes_text_end"); | |
6172 | sess.sym_stext = sym_table->lookup_symbol_address("_stext"); | |
5f0a03a6 JK |
6173 | } |
6174 | } | |
6175 | } | |
6176 | if (symtab_status == info_absent) | |
6177 | { | |
6178 | delete sym_table; | |
6179 | sym_table = NULL; | |
6180 | return; | |
6181 | } | |
6182 | ||
6183 | // If we have dwarf for the same module, mark the redundant symtab | |
6184 | // entries. | |
6185 | // | |
6186 | // In dwarf_query::handle_query_module(), the call to query_module_dwarf() | |
6187 | // precedes the call to query_module_symtab(). So we should never read | |
6188 | // a module's symbol table without first having tried to get its dwarf. | |
6189 | sym_table->mark_dwarf_redundancies(&q->dw); | |
ab91b232 JK |
6190 | |
6191 | if (name == TOK_KERNEL) | |
6192 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
6193 | } |
6194 | ||
6195 | module_info::~module_info() | |
6196 | { | |
6197 | if (sym_table) | |
6198 | delete sym_table; | |
b55bc428 FCE |
6199 | } |
6200 | ||
6201 | ||
98afd80e | 6202 | |
935447c8 DS |
6203 | // ------------------------------------------------------------------------ |
6204 | // task_finder derived 'probes': These don't really exist. The whole | |
6205 | // purpose of the task_finder_derived_probe_group is to make sure that | |
6206 | // stap_start_task_finder()/stap_stop_task_finder() get called only | |
6207 | // once and in the right place. | |
6208 | // ------------------------------------------------------------------------ | |
6209 | ||
6210 | struct task_finder_derived_probe: public derived_probe | |
6211 | { | |
c295b10e FCE |
6212 | // Dummy constructor for gcc 3.4 compatibility |
6213 | task_finder_derived_probe (): derived_probe (0) { assert(0); } | |
935447c8 DS |
6214 | }; |
6215 | ||
6216 | ||
6217 | struct task_finder_derived_probe_group: public generic_dpg<task_finder_derived_probe> | |
6218 | { | |
6219 | public: | |
6220 | static void create_session_group (systemtap_session& s); | |
6221 | ||
6222 | void emit_module_decls (systemtap_session& ) { } | |
6223 | void emit_module_init (systemtap_session& s); | |
6224 | void emit_module_exit (systemtap_session& s); | |
6225 | }; | |
6226 | ||
6227 | ||
6228 | void | |
6229 | task_finder_derived_probe_group::create_session_group (systemtap_session& s) | |
6230 | { | |
6231 | if (! s.task_finder_derived_probes) | |
6d0f3f0c | 6232 | s.task_finder_derived_probes = new task_finder_derived_probe_group(); |
935447c8 DS |
6233 | } |
6234 | ||
6235 | ||
6236 | void | |
6237 | task_finder_derived_probe_group::emit_module_init (systemtap_session& s) | |
6238 | { | |
6239 | s.op->newline(); | |
6240 | s.op->newline() << "/* ---- task finder ---- */"; | |
6241 | s.op->newline() << "rc = stap_start_task_finder();"; | |
6242 | ||
6243 | s.op->newline() << "if (rc) {"; | |
6d0f3f0c | 6244 | s.op->newline(1) << "stap_stop_task_finder();"; |
935447c8 DS |
6245 | s.op->newline(-1) << "}"; |
6246 | } | |
6247 | ||
6248 | ||
6249 | void | |
6250 | task_finder_derived_probe_group::emit_module_exit (systemtap_session& s) | |
6251 | { | |
6252 | s.op->newline(); | |
6253 | s.op->newline() << "/* ---- task finder ---- */"; | |
6254 | s.op->newline() << "stap_stop_task_finder();"; | |
6255 | } | |
6256 | ||
a96d1db0 DN |
6257 | // ------------------------------------------------------------------------ |
6258 | // itrace user-space probes | |
6259 | // ------------------------------------------------------------------------ | |
6260 | ||
6261 | ||
0afb7073 FCE |
6262 | static string TOK_INSN("insn"); |
6263 | static string TOK_BLOCK("block"); | |
6264 | ||
a96d1db0 DN |
6265 | struct itrace_derived_probe: public derived_probe |
6266 | { | |
6267 | bool has_path; | |
6268 | string path; | |
6269 | int64_t pid; | |
6270 | int single_step; | |
6271 | ||
6272 | itrace_derived_probe (systemtap_session &s, probe* p, probe_point* l, | |
6273 | bool hp, string &pn, int64_t pd, int ss | |
6274 | ); | |
6275 | void join_group (systemtap_session& s); | |
6276 | }; | |
6277 | ||
6278 | ||
6279 | struct itrace_derived_probe_group: public generic_dpg<itrace_derived_probe> | |
6280 | { | |
6281 | private: | |
6282 | map<string, vector<itrace_derived_probe*> > probes_by_path; | |
6283 | typedef map<string, vector<itrace_derived_probe*> >::iterator p_b_path_iterator; | |
6284 | map<int64_t, vector<itrace_derived_probe*> > probes_by_pid; | |
6285 | typedef map<int64_t, vector<itrace_derived_probe*> >::iterator p_b_pid_iterator; | |
6286 | unsigned num_probes; | |
6287 | ||
6288 | void emit_probe_decl (systemtap_session& s, itrace_derived_probe *p); | |
6289 | ||
6290 | public: | |
6291 | itrace_derived_probe_group(): num_probes(0) { } | |
6292 | ||
6293 | void enroll (itrace_derived_probe* probe); | |
6294 | void emit_module_decls (systemtap_session& s); | |
6295 | void emit_module_init (systemtap_session& s); | |
6296 | void emit_module_exit (systemtap_session& s); | |
6297 | }; | |
6298 | ||
6299 | ||
6300 | itrace_derived_probe::itrace_derived_probe (systemtap_session &s, | |
6301 | probe* p, probe_point* l, | |
6302 | bool hp, string &pn, int64_t pd, | |
6303 | int ss | |
6304 | ): | |
6305 | derived_probe(p, l), has_path(hp), path(pn), pid(pd), single_step(ss) | |
6306 | { | |
6307 | } | |
6308 | ||
6309 | ||
6310 | void | |
6311 | itrace_derived_probe::join_group (systemtap_session& s) | |
6312 | { | |
6313 | if (! s.itrace_derived_probes) | |
6314 | s.itrace_derived_probes = new itrace_derived_probe_group (); | |
6315 | ||
6316 | s.itrace_derived_probes->enroll (this); | |
6317 | ||
6318 | task_finder_derived_probe_group::create_session_group (s); | |
6319 | } | |
6320 | ||
6321 | struct itrace_builder: public derived_probe_builder | |
6322 | { | |
6323 | itrace_builder() {} | |
6324 | virtual void build(systemtap_session & sess, | |
6325 | probe * base, | |
6326 | probe_point * location, | |
6327 | std::map<std::string, literal *> const & parameters, | |
6328 | vector<derived_probe *> & finished_results) | |
6329 | { | |
6330 | string path; | |
28d29bd3 | 6331 | int64_t pid = 0; |
a96d1db0 DN |
6332 | int single_step; |
6333 | ||
6334 | bool has_path = get_param (parameters, TOK_PROCESS, path); | |
6335 | bool has_pid = get_param (parameters, TOK_PROCESS, pid); | |
28d29bd3 | 6336 | // XXX: PR 6445 needs !has_path && !has_pid support |
a96d1db0 DN |
6337 | assert (has_path || has_pid); |
6338 | ||
0afb7073 | 6339 | single_step = ! has_null_param (parameters, TOK_BLOCK); |
a96d1db0 DN |
6340 | |
6341 | // If we have a path, we need to validate it. | |
6342 | if (has_path) | |
d0a7f5a9 | 6343 | path = find_executable (path); |
a96d1db0 DN |
6344 | |
6345 | finished_results.push_back(new itrace_derived_probe(sess, base, location, | |
6346 | has_path, path, pid, | |
d0a7f5a9 | 6347 | single_step |
a96d1db0 DN |
6348 | )); |
6349 | } | |
6350 | }; | |
6351 | ||
6352 | ||
6353 | void | |
6354 | itrace_derived_probe_group::enroll (itrace_derived_probe* p) | |
6355 | { | |
6356 | if (p->has_path) | |
6357 | probes_by_path[p->path].push_back(p); | |
6358 | else | |
6359 | probes_by_pid[p->pid].push_back(p); | |
6360 | num_probes++; | |
6361 | ||
6362 | // XXX: multiple exec probes (for instance) for the same path (or | |
6363 | // pid) should all share a itrace report function, and have their | |
6364 | // handlers executed sequentially. | |
6365 | } | |
6366 | ||
6367 | ||
6368 | void | |
6369 | itrace_derived_probe_group::emit_probe_decl (systemtap_session& s, | |
6370 | itrace_derived_probe *p) | |
6371 | { | |
6372 | s.op->newline() << "{"; | |
6373 | s.op->line() << " .tgt={"; | |
6374 | ||
6375 | if (p->has_path) | |
6376 | { | |
6377 | s.op->line() << " .pathname=\"" << p->path << "\","; | |
6378 | s.op->line() << " .pid=0,"; | |
6379 | } | |
6380 | else | |
6381 | { | |
6382 | s.op->line() << " .pathname=NULL,"; | |
6383 | s.op->line() << " .pid=" << p->pid << ","; | |
6384 | } | |
6385 | ||
6386 | s.op->line() << " .callback=&_stp_itrace_probe_cb,"; | |
6387 | s.op->line() << " },"; | |
6388 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
6389 | s.op->line() << " .single_step=" << p->single_step << ","; | |
6390 | s.op->line() << " .ph=&" << p->name << ","; | |
6391 | ||
6392 | s.op->line() << " },"; | |
6393 | } | |
6394 | ||
6395 | ||
6396 | void | |
6397 | itrace_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6398 | { | |
6399 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
6400 | return; | |
6401 | ||
6402 | s.op->newline(); | |
6403 | s.op->newline() << "/* ---- itrace probes ---- */"; | |
1324cc82 | 6404 | s.op->newline() << "#include \"task_finder.c\""; |
a96d1db0 DN |
6405 | s.op->newline() << "struct stap_itrace_probe {"; |
6406 | s.op->indent(1); | |
6407 | s.op->newline() << "struct stap_task_finder_target tgt;"; | |
6408 | s.op->newline() << "const char *pp;"; | |
6409 | s.op->newline() << "void (*ph) (struct context*);"; | |
6410 | s.op->newline() << "int single_step;"; | |
6411 | s.op->newline(-1) << "};"; | |
6412 | s.op->newline() << "static void enter_itrace_probe(struct stap_itrace_probe *p, struct pt_regs *regs, void *data);"; | |
6413 | s.op->newline() << "#include \"itrace.c\""; | |
6414 | ||
6415 | // output routine to call itrace probe | |
6416 | s.op->newline() << "static void enter_itrace_probe(struct stap_itrace_probe *p, struct pt_regs *regs, void *data) {"; | |
6417 | s.op->indent(1); | |
6418 | ||
c12d974f | 6419 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
a96d1db0 DN |
6420 | s.op->newline() << "c->regs = regs;"; |
6421 | s.op->newline() << "c->data = data;"; | |
6422 | ||
6423 | // call probe function | |
6424 | s.op->newline() << "(*p->ph) (c);"; | |
6425 | common_probe_entryfn_epilogue (s.op); | |
6426 | ||
6427 | s.op->newline() << "return;"; | |
6428 | s.op->newline(-1) << "}"; | |
6429 | ||
6430 | // Output task finder callback routine that gets called for all | |
6431 | // itrace probe types. | |
46b3c6cd | 6432 | s.op->newline() << "static int _stp_itrace_probe_cb(struct stap_task_finder_target *tgt, struct task_struct *tsk, int register_p, int process_p) {"; |
a96d1db0 DN |
6433 | s.op->indent(1); |
6434 | s.op->newline() << "int rc = 0;"; | |
6435 | s.op->newline() << "struct stap_itrace_probe *p = container_of(tgt, struct stap_itrace_probe, tgt);"; | |
6436 | ||
6437 | s.op->newline() << "if (register_p) "; | |
6438 | s.op->indent(1); | |
6439 | ||
6440 | s.op->newline() << "rc = usr_itrace_init(p->single_step, tsk->pid, p);"; | |
6441 | s.op->newline(-1) << "else"; | |
6442 | s.op->newline(1) << "remove_usr_itrace_info(find_itrace_info(p->tgt.pid));"; | |
6443 | s.op->newline(-1) << "return rc;"; | |
6444 | s.op->newline(-1) << "}"; | |
6445 | ||
4c2732a1 | 6446 | s.op->newline() << "static struct stap_itrace_probe stap_itrace_probes[] = {"; |
a96d1db0 DN |
6447 | s.op->indent(1); |
6448 | ||
6449 | // Set up 'process(PATH)' probes | |
6450 | if (! probes_by_path.empty()) | |
6451 | { | |
6452 | for (p_b_path_iterator it = probes_by_path.begin(); | |
6453 | it != probes_by_path.end(); it++) | |
6454 | { | |
6455 | for (unsigned i = 0; i < it->second.size(); i++) | |
6456 | { | |
6457 | itrace_derived_probe *p = it->second[i]; | |
6458 | emit_probe_decl(s, p); | |
6459 | } | |
6460 | } | |
6461 | } | |
6462 | ||
6463 | // Set up 'process(PID)' probes | |
6464 | if (! probes_by_pid.empty()) | |
6465 | { | |
6466 | for (p_b_pid_iterator it = probes_by_pid.begin(); | |
6467 | it != probes_by_pid.end(); it++) | |
6468 | { | |
6469 | for (unsigned i = 0; i < it->second.size(); i++) | |
6470 | { | |
6471 | itrace_derived_probe *p = it->second[i]; | |
6472 | emit_probe_decl(s, p); | |
6473 | } | |
6474 | } | |
6475 | } | |
6476 | s.op->newline(-1) << "};"; | |
6477 | } | |
6478 | ||
6479 | ||
6480 | void | |
6481 | itrace_derived_probe_group::emit_module_init (systemtap_session& s) | |
6482 | { | |
6483 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
6484 | return; | |
6485 | ||
6486 | s.op->newline(); | |
6487 | s.op->newline() << "/* ---- itrace probes ---- */"; | |
6488 | ||
6489 | s.op->newline() << "for (i=0; i<" << num_probes << "; i++) {"; | |
6490 | s.op->indent(1); | |
6491 | s.op->newline() << "struct stap_itrace_probe *p = &stap_itrace_probes[i];"; | |
6492 | s.op->newline() << "rc = stap_register_task_finder_target(&p->tgt);"; | |
6493 | s.op->newline(-1) << "}"; | |
6494 | } | |
6495 | ||
6496 | ||
6497 | void | |
6498 | itrace_derived_probe_group::emit_module_exit (systemtap_session& s) | |
6499 | { | |
6500 | if (probes_by_path.empty() && probes_by_pid.empty()) return; | |
6501 | s.op->newline(); | |
6502 | s.op->newline() << "/* ---- itrace probes ---- */"; | |
6503 | s.op->newline() << "cleanup_usr_itrace();"; | |
6504 | } | |
935447c8 DS |
6505 | |
6506 | // ------------------------------------------------------------------------ | |
6507 | // utrace user-space probes | |
6508 | // ------------------------------------------------------------------------ | |
6509 | ||
eff6ac72 | 6510 | static string TOK_THREAD("thread"); |
12b21830 DS |
6511 | static string TOK_SYSCALL("syscall"); |
6512 | ||
eff6ac72 DS |
6513 | // Note that these flags don't match up exactly with UTRACE_EVENT |
6514 | // flags (and that's OK). | |
935447c8 DS |
6515 | enum utrace_derived_probe_flags { |
6516 | UDPF_NONE, | |
eff6ac72 DS |
6517 | UDPF_BEGIN, // process begin |
6518 | UDPF_END, // process end | |
6519 | UDPF_THREAD_BEGIN, // thread begin | |
6520 | UDPF_THREAD_END, // thread end | |
6521 | UDPF_SYSCALL, // syscall entry | |
6522 | UDPF_SYSCALL_RETURN, // syscall exit | |
935447c8 DS |
6523 | UDPF_NFLAGS |
6524 | }; | |
6525 | ||
6526 | struct utrace_derived_probe: public derived_probe | |
6527 | { | |
6528 | bool has_path; | |
6529 | string path; | |
6530 | int64_t pid; | |
6531 | enum utrace_derived_probe_flags flags; | |
6532 | bool target_symbol_seen; | |
6533 | ||
6534 | utrace_derived_probe (systemtap_session &s, probe* p, probe_point* l, | |
6535 | bool hp, string &pn, int64_t pd, | |
6536 | enum utrace_derived_probe_flags f); | |
6537 | void join_group (systemtap_session& s); | |
6538 | }; | |
6539 | ||
6540 | ||
6541 | struct utrace_derived_probe_group: public generic_dpg<utrace_derived_probe> | |
6542 | { | |
6543 | private: | |
6544 | map<string, vector<utrace_derived_probe*> > probes_by_path; | |
6545 | typedef map<string, vector<utrace_derived_probe*> >::iterator p_b_path_iterator; | |
6546 | map<int64_t, vector<utrace_derived_probe*> > probes_by_pid; | |
6547 | typedef map<int64_t, vector<utrace_derived_probe*> >::iterator p_b_pid_iterator; | |
6548 | unsigned num_probes; | |
6549 | bool flags_seen[UDPF_NFLAGS]; | |
6550 | ||
6551 | void emit_probe_decl (systemtap_session& s, utrace_derived_probe *p); | |
a21d81ec DS |
6552 | void emit_vm_callback_probe_decl (systemtap_session& s, bool has_path, |
6553 | string path, int64_t pid, | |
6554 | string vm_callback); | |
935447c8 DS |
6555 | |
6556 | public: | |
6557 | utrace_derived_probe_group(): num_probes(0), flags_seen() { } | |
6558 | ||
6559 | void enroll (utrace_derived_probe* probe); | |
6560 | void emit_module_decls (systemtap_session& s); | |
6561 | void emit_module_init (systemtap_session& s); | |
6562 | void emit_module_exit (systemtap_session& s); | |
6563 | }; | |
6564 | ||
6565 | ||
de688825 | 6566 | struct utrace_var_expanding_visitor: public var_expanding_visitor |
935447c8 | 6567 | { |
de688825 JS |
6568 | utrace_var_expanding_visitor(systemtap_session& s, const string& pn, |
6569 | enum utrace_derived_probe_flags f): | |
935447c8 DS |
6570 | sess (s), probe_name (pn), flags (f), target_symbol_seen (false) {} |
6571 | ||
6572 | systemtap_session& sess; | |
6573 | string probe_name; | |
6574 | enum utrace_derived_probe_flags flags; | |
6575 | bool target_symbol_seen; | |
6576 | ||
6270adc1 | 6577 | void visit_target_symbol_arg (target_symbol* e); |
5d67b47c | 6578 | void visit_target_symbol_context (target_symbol* e); |
935447c8 DS |
6579 | void visit_target_symbol (target_symbol* e); |
6580 | }; | |
6581 | ||
6582 | ||
a7a68293 | 6583 | |
935447c8 DS |
6584 | utrace_derived_probe::utrace_derived_probe (systemtap_session &s, |
6585 | probe* p, probe_point* l, | |
6586 | bool hp, string &pn, int64_t pd, | |
6587 | enum utrace_derived_probe_flags f): | |
d0a7f5a9 FCE |
6588 | derived_probe (p, new probe_point (*l) /* .components soon rewritten */ ), |
6589 | has_path(hp), path(pn), pid(pd), flags(f), | |
935447c8 DS |
6590 | target_symbol_seen(false) |
6591 | { | |
de688825 JS |
6592 | // Expand local variables in the probe body |
6593 | utrace_var_expanding_visitor v (s, name, flags); | |
6594 | this->body = v.require (this->body); | |
935447c8 | 6595 | target_symbol_seen = v.target_symbol_seen; |
d0a7f5a9 FCE |
6596 | |
6597 | // Reset the sole element of the "locations" vector as a | |
6598 | // "reverse-engineered" form of the incoming (q.base_loc) probe | |
6599 | // point. This allows a user to see what program etc. | |
6600 | // number any particular match of the wildcards. | |
6601 | ||
6602 | vector<probe_point::component*> comps; | |
6603 | if (hp) | |
6604 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(path))); | |
e56e51c9 | 6605 | else if (pid != 0) |
06aca46a | 6606 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_number(pid))); |
e56e51c9 FCE |
6607 | else |
6608 | comps.push_back (new probe_point::component(TOK_PROCESS)); | |
6609 | ||
d0a7f5a9 FCE |
6610 | switch (flags) |
6611 | { | |
6612 | case UDPF_THREAD_BEGIN: | |
6613 | comps.push_back (new probe_point::component(TOK_THREAD)); | |
6614 | comps.push_back (new probe_point::component(TOK_BEGIN)); | |
6615 | break; | |
6616 | case UDPF_THREAD_END: | |
6617 | comps.push_back (new probe_point::component(TOK_THREAD)); | |
6618 | comps.push_back (new probe_point::component(TOK_END)); | |
6619 | break; | |
6620 | case UDPF_SYSCALL: | |
6621 | comps.push_back (new probe_point::component(TOK_SYSCALL)); | |
6622 | break; | |
6623 | case UDPF_SYSCALL_RETURN: | |
6624 | comps.push_back (new probe_point::component(TOK_SYSCALL)); | |
6625 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
6626 | break; | |
6627 | case UDPF_BEGIN: | |
6628 | comps.push_back (new probe_point::component(TOK_BEGIN)); | |
6629 | break; | |
6630 | case UDPF_END: | |
6631 | comps.push_back (new probe_point::component(TOK_END)); | |
6632 | break; | |
06aca46a | 6633 | default: |
d0a7f5a9 FCE |
6634 | assert (0); |
6635 | } | |
6636 | ||
6637 | // Overwrite it. | |
6638 | this->sole_location()->components = comps; | |
935447c8 DS |
6639 | } |
6640 | ||
6641 | ||
6642 | void | |
6643 | utrace_derived_probe::join_group (systemtap_session& s) | |
6644 | { | |
6645 | if (! s.utrace_derived_probes) | |
14cdaa0b | 6646 | { |
935447c8 | 6647 | s.utrace_derived_probes = new utrace_derived_probe_group (); |
14cdaa0b | 6648 | } |
935447c8 DS |
6649 | s.utrace_derived_probes->enroll (this); |
6650 | ||
6651 | task_finder_derived_probe_group::create_session_group (s); | |
6652 | } | |
6653 | ||
6654 | ||
6655 | void | |
de688825 | 6656 | utrace_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) |
935447c8 | 6657 | { |
6270adc1 | 6658 | string argnum_s = e->base_name.substr(4,e->base_name.length()-4); |
3a4e19b8 | 6659 | int argnum = lex_cast<int>(argnum_s); |
935447c8 | 6660 | |
6270adc1 MH |
6661 | if (flags != UDPF_SYSCALL) |
6662 | throw semantic_error ("only \"process(PATH_OR_PID).syscall\" support $argN.", e->tok); | |
935447c8 | 6663 | |
6270adc1 MH |
6664 | if (e->components.size() > 0) |
6665 | { | |
6666 | switch (e->components[0].first) | |
6667 | { | |
6668 | case target_symbol::comp_literal_array_index: | |
6669 | throw semantic_error("utrace target variable '$argN' may not be used as array", | |
6670 | e->tok); | |
6671 | break; | |
6672 | case target_symbol::comp_struct_member: | |
6673 | throw semantic_error("utrace target variable '$argN' may not be used as a structure", | |
6674 | e->tok); | |
6675 | break; | |
6676 | default: | |
6677 | throw semantic_error ("invalid use of utrace target variable '$argN'", | |
6678 | e->tok); | |
6679 | break; | |
6680 | } | |
6681 | } | |
6682 | ||
6683 | // FIXME: max argnument number should not be hardcoded. | |
6684 | if (argnum < 1 || argnum > 6) | |
6685 | throw semantic_error ("invalid syscall argument number (1-6)", e->tok); | |
6686 | ||
6687 | bool lvalue = is_active_lvalue(e); | |
6688 | if (lvalue) | |
6689 | throw semantic_error("utrace '$argN' variable is read-only", e->tok); | |
935447c8 | 6690 | |
6270adc1 MH |
6691 | // Remember that we've seen a target variable. |
6692 | target_symbol_seen = true; | |
6693 | ||
6694 | // We're going to substitute a synthesized '_utrace_syscall_arg' | |
6695 | // function call for the '$argN' reference. | |
6696 | functioncall* n = new functioncall; | |
6697 | n->tok = e->tok; | |
6698 | n->function = "_utrace_syscall_arg"; | |
6699 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
6700 | ||
6701 | literal_number *num = new literal_number(argnum - 1); | |
6702 | num->tok = e->tok; | |
6703 | n->args.push_back(num); | |
6704 | ||
4ed05b15 | 6705 | provide (n); |
6270adc1 MH |
6706 | } |
6707 | ||
6708 | void | |
de688825 | 6709 | utrace_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
6270adc1 | 6710 | { |
5d67b47c MH |
6711 | string sname = e->base_name; |
6712 | ||
935447c8 DS |
6713 | if (e->components.size() > 0) |
6714 | { | |
6715 | switch (e->components[0].first) | |
6716 | { | |
6717 | case target_symbol::comp_literal_array_index: | |
5d67b47c | 6718 | throw semantic_error("utrace target variable '" + sname + "' may not be used as array", |
935447c8 DS |
6719 | e->tok); |
6720 | break; | |
6721 | case target_symbol::comp_struct_member: | |
5d67b47c | 6722 | throw semantic_error("utrace target variable '" + sname + "' may not be used as a structure", |
935447c8 DS |
6723 | e->tok); |
6724 | break; | |
6725 | default: | |
5d67b47c | 6726 | throw semantic_error ("invalid use of utrace target variable '" + sname + "'", |
935447c8 DS |
6727 | e->tok); |
6728 | break; | |
6729 | } | |
6730 | } | |
6731 | ||
6732 | bool lvalue = is_active_lvalue(e); | |
6733 | if (lvalue) | |
5d67b47c MH |
6734 | throw semantic_error("utrace '" + sname + "' variable is read-only", e->tok); |
6735 | ||
6736 | string fname; | |
6737 | if (sname == "$return") | |
6738 | { | |
6739 | if (flags != UDPF_SYSCALL_RETURN) | |
6740 | throw semantic_error ("only \"process(PATH_OR_PID).syscall.return\" support $return.", e->tok); | |
6741 | fname = "_utrace_syscall_return"; | |
6742 | } | |
6743 | else | |
6744 | fname = "_utrace_syscall_nr"; | |
935447c8 DS |
6745 | |
6746 | // Remember that we've seen a target variable. | |
6747 | target_symbol_seen = true; | |
6748 | ||
6270adc1 | 6749 | // We're going to substitute a synthesized '_utrace_syscall_nr' |
a7a68293 | 6750 | // function call for the '$syscall' reference. |
935447c8 DS |
6751 | functioncall* n = new functioncall; |
6752 | n->tok = e->tok; | |
5d67b47c | 6753 | n->function = fname; |
935447c8 DS |
6754 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session |
6755 | ||
4ed05b15 | 6756 | provide (n); |
935447c8 DS |
6757 | } |
6758 | ||
6270adc1 | 6759 | void |
de688825 | 6760 | utrace_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
6270adc1 MH |
6761 | { |
6762 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
6763 | ||
6764 | if (flags != UDPF_SYSCALL && flags != UDPF_SYSCALL_RETURN) | |
6765 | throw semantic_error ("only \"process(PATH_OR_PID).syscall\" and \"process(PATH_OR_PID).syscall.return\" probes support target symbols", | |
6766 | e->tok); | |
6767 | ||
6768 | if (e->base_name.substr(0,4) == "$arg") | |
6769 | visit_target_symbol_arg(e); | |
5d67b47c MH |
6770 | else if (e->base_name == "$syscall" || e->base_name == "$return") |
6771 | visit_target_symbol_context(e); | |
6270adc1 | 6772 | else |
5d67b47c | 6773 | throw semantic_error ("invalid target symbol for utrace probe, $syscall, $return or $argN expected", |
6270adc1 MH |
6774 | e->tok); |
6775 | } | |
6776 | ||
935447c8 DS |
6777 | |
6778 | struct utrace_builder: public derived_probe_builder | |
6779 | { | |
6780 | utrace_builder() {} | |
6781 | virtual void build(systemtap_session & sess, | |
6782 | probe * base, | |
6783 | probe_point * location, | |
86bf665e | 6784 | literal_map_t const & parameters, |
935447c8 DS |
6785 | vector<derived_probe *> & finished_results) |
6786 | { | |
6787 | string path; | |
6788 | int64_t pid; | |
6789 | ||
6790 | bool has_path = get_param (parameters, TOK_PROCESS, path); | |
6791 | bool has_pid = get_param (parameters, TOK_PROCESS, pid); | |
6792 | enum utrace_derived_probe_flags flags = UDPF_NONE; | |
d0a7f5a9 | 6793 | |
eff6ac72 DS |
6794 | if (has_null_param (parameters, TOK_THREAD)) |
6795 | { | |
6796 | if (has_null_param (parameters, TOK_BEGIN)) | |
6797 | flags = UDPF_THREAD_BEGIN; | |
6798 | else if (has_null_param (parameters, TOK_END)) | |
6799 | flags = UDPF_THREAD_END; | |
6800 | } | |
12b21830 | 6801 | else if (has_null_param (parameters, TOK_SYSCALL)) |
935447c8 DS |
6802 | { |
6803 | if (has_null_param (parameters, TOK_RETURN)) | |
eff6ac72 | 6804 | flags = UDPF_SYSCALL_RETURN; |
935447c8 | 6805 | else |
eff6ac72 | 6806 | flags = UDPF_SYSCALL; |
935447c8 | 6807 | } |
eff6ac72 DS |
6808 | else if (has_null_param (parameters, TOK_BEGIN)) |
6809 | flags = UDPF_BEGIN; | |
6810 | else if (has_null_param (parameters, TOK_END)) | |
6811 | flags = UDPF_END; | |
935447c8 | 6812 | |
986e98de DS |
6813 | // If we didn't get a path or pid, this means to probe everything. |
6814 | // Convert this to a pid-based probe. | |
6815 | if (! has_path && ! has_pid) | |
6816 | { | |
6817 | has_path = false; | |
6818 | path.clear(); | |
6819 | has_pid = true; | |
6820 | pid = 0; | |
6821 | } | |
c569c2e4 FCE |
6822 | else if (has_path) |
6823 | { | |
6824 | path = find_executable (path); | |
6825 | sess.unwindsym_modules.insert (path); | |
6826 | } | |
986e98de | 6827 | else if (has_pid) |
6cdf2889 | 6828 | { |
c569c2e4 | 6829 | // We can't probe 'init' (pid 1). XXX: where does this limitation come from? |
6cdf2889 DS |
6830 | if (pid < 2) |
6831 | throw semantic_error ("process pid must be greater than 1", | |
6832 | location->tok); | |
28d29bd3 FCE |
6833 | |
6834 | // XXX: could we use /proc/$pid/exe in unwindsym_modules and elsewhere? | |
6cdf2889 | 6835 | } |
0744f531 | 6836 | |
935447c8 DS |
6837 | finished_results.push_back(new utrace_derived_probe(sess, base, location, |
6838 | has_path, path, pid, | |
6839 | flags)); | |
6840 | } | |
6841 | }; | |
6842 | ||
6843 | ||
6844 | void | |
6845 | utrace_derived_probe_group::enroll (utrace_derived_probe* p) | |
6846 | { | |
6847 | if (p->has_path) | |
6848 | probes_by_path[p->path].push_back(p); | |
6849 | else | |
6850 | probes_by_pid[p->pid].push_back(p); | |
6851 | num_probes++; | |
6852 | flags_seen[p->flags] = true; | |
6853 | ||
6854 | // XXX: multiple exec probes (for instance) for the same path (or | |
6855 | // pid) should all share a utrace report function, and have their | |
6856 | // handlers executed sequentially. | |
6857 | } | |
6858 | ||
6859 | ||
6860 | void | |
6861 | utrace_derived_probe_group::emit_probe_decl (systemtap_session& s, | |
6862 | utrace_derived_probe *p) | |
6863 | { | |
6864 | s.op->newline() << "{"; | |
6865 | s.op->line() << " .tgt={"; | |
6866 | ||
6867 | if (p->has_path) | |
6868 | { | |
6869 | s.op->line() << " .pathname=\"" << p->path << "\","; | |
6870 | s.op->line() << " .pid=0,"; | |
6871 | } | |
6872 | else | |
6873 | { | |
6874 | s.op->line() << " .pathname=NULL,"; | |
6875 | s.op->line() << " .pid=" << p->pid << ","; | |
6876 | } | |
6877 | ||
6878 | s.op->line() << " .callback=&_stp_utrace_probe_cb,"; | |
8253e3b8 | 6879 | s.op->line() << " .vm_callback=NULL,"; |
935447c8 DS |
6880 | s.op->line() << " },"; |
6881 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
6882 | s.op->line() << " .ph=&" << p->name << ","; | |
6883 | ||
6884 | // Handle flags | |
6885 | switch (p->flags) | |
6886 | { | |
b20bac3a DS |
6887 | // Notice that we'll just call the probe directly when we get |
6888 | // notified, since the task_finder layer stops the thread for us. | |
eff6ac72 DS |
6889 | case UDPF_BEGIN: // process begin |
6890 | s.op->line() << " .flags=(UDPF_BEGIN),"; | |
935447c8 | 6891 | break; |
eff6ac72 DS |
6892 | case UDPF_THREAD_BEGIN: // thread begin |
6893 | s.op->line() << " .flags=(UDPF_THREAD_BEGIN),"; | |
159cb109 | 6894 | break; |
eff6ac72 DS |
6895 | |
6896 | // Notice we're not setting up a .ops/.report_death handler for | |
6897 | // either UDPF_END or UDPF_THREAD_END. Instead, we'll just call | |
6898 | // the probe directly when we get notified. | |
6899 | case UDPF_END: // process end | |
6900 | s.op->line() << " .flags=(UDPF_END),"; | |
935447c8 | 6901 | break; |
eff6ac72 DS |
6902 | case UDPF_THREAD_END: // thread end |
6903 | s.op->line() << " .flags=(UDPF_THREAD_END),"; | |
6904 | break; | |
6905 | ||
6906 | // For UDPF_SYSCALL/UDPF_SYSCALL_RETURN probes, the .report_death | |
6907 | // handler isn't strictly necessary. However, it helps to keep | |
b20bac3a DS |
6908 | // our attaches/detaches symmetrical. Since the task_finder layer |
6909 | // stops the thread, that works around bug 6841. | |
eff6ac72 DS |
6910 | case UDPF_SYSCALL: |
6911 | s.op->line() << " .flags=(UDPF_SYSCALL),"; | |
b20bac3a DS |
6912 | s.op->line() << " .ops={ .report_syscall_entry=stap_utrace_probe_syscall, .report_death=stap_utrace_task_finder_report_death },"; |
6913 | s.op->line() << " .events=(UTRACE_EVENT(SYSCALL_ENTRY)|UTRACE_EVENT(DEATH)),"; | |
935447c8 | 6914 | break; |
eff6ac72 DS |
6915 | case UDPF_SYSCALL_RETURN: |
6916 | s.op->line() << " .flags=(UDPF_SYSCALL_RETURN),"; | |
b20bac3a DS |
6917 | s.op->line() << " .ops={ .report_syscall_exit=stap_utrace_probe_syscall, .report_death=stap_utrace_task_finder_report_death },"; |
6918 | s.op->line() << " .events=(UTRACE_EVENT(SYSCALL_EXIT)|UTRACE_EVENT(DEATH)),"; | |
935447c8 | 6919 | break; |
930a1798 | 6920 | |
a21d81ec DS |
6921 | case UDPF_NONE: |
6922 | s.op->line() << " .flags=(UDPF_NONE),"; | |
6923 | s.op->line() << " .ops={ },"; | |
6924 | s.op->line() << " .events=0,"; | |
6925 | break; | |
935447c8 DS |
6926 | default: |
6927 | throw semantic_error ("bad utrace probe flag"); | |
6928 | break; | |
6929 | } | |
6930 | s.op->line() << " .engine_attached=0,"; | |
6931 | s.op->line() << " },"; | |
6932 | } | |
6933 | ||
6934 | ||
a21d81ec DS |
6935 | void |
6936 | utrace_derived_probe_group::emit_vm_callback_probe_decl (systemtap_session& s, | |
6937 | bool has_path, | |
6938 | string path, | |
6939 | int64_t pid, | |
6940 | string vm_callback) | |
6941 | { | |
6942 | s.op->newline() << "{"; | |
6943 | s.op->line() << " .tgt={"; | |
6944 | ||
6945 | if (has_path) | |
6946 | { | |
6947 | s.op->line() << " .pathname=\"" << path << "\","; | |
6948 | s.op->line() << " .pid=0,"; | |
6949 | } | |
6950 | else | |
6951 | { | |
6952 | s.op->line() << " .pathname=NULL,"; | |
6953 | s.op->line() << " .pid=" << pid << ","; | |
6954 | } | |
6955 | ||
6956 | s.op->line() << " .callback=NULL,"; | |
6957 | s.op->line() << " .vm_callback=&" << vm_callback << ","; | |
6958 | s.op->line() << " },"; | |
6959 | s.op->line() << " .pp=\"internal\","; | |
6960 | s.op->line() << " .ph=NULL,"; | |
6961 | s.op->line() << " .flags=(UDPF_NONE),"; | |
6962 | s.op->line() << " .ops={ NULL },"; | |
6963 | s.op->line() << " .events=0,"; | |
6964 | s.op->line() << " .engine_attached=0,"; | |
6965 | s.op->line() << " },"; | |
6966 | } | |
6967 | ||
6968 | ||
935447c8 DS |
6969 | void |
6970 | utrace_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6971 | { | |
6972 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
6973 | return; | |
6974 | ||
6975 | s.op->newline(); | |
6976 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
104c6237 FCE |
6977 | s.op->newline() << "#include \"task_finder.c\""; |
6978 | ||
eff6ac72 DS |
6979 | s.op->newline() << "enum utrace_derived_probe_flags {"; |
6980 | s.op->indent(1); | |
6981 | s.op->newline() << "UDPF_NONE,"; | |
6982 | s.op->newline() << "UDPF_BEGIN,"; | |
6983 | s.op->newline() << "UDPF_END,"; | |
6984 | s.op->newline() << "UDPF_THREAD_BEGIN,"; | |
6985 | s.op->newline() << "UDPF_THREAD_END,"; | |
6986 | s.op->newline() << "UDPF_SYSCALL,"; | |
6987 | s.op->newline() << "UDPF_SYSCALL_RETURN,"; | |
6988 | s.op->newline() << "UDPF_NFLAGS"; | |
6989 | s.op->newline(-1) << "};"; | |
6990 | ||
935447c8 DS |
6991 | s.op->newline() << "struct stap_utrace_probe {"; |
6992 | s.op->indent(1); | |
6993 | s.op->newline() << "struct stap_task_finder_target tgt;"; | |
6994 | s.op->newline() << "const char *pp;"; | |
6995 | s.op->newline() << "void (*ph) (struct context*);"; | |
eff6ac72 | 6996 | s.op->newline() << "enum utrace_derived_probe_flags flags;"; |
935447c8 | 6997 | s.op->newline() << "struct utrace_engine_ops ops;"; |
eff6ac72 | 6998 | s.op->newline() << "unsigned long events;"; |
935447c8 DS |
6999 | s.op->newline() << "int engine_attached;"; |
7000 | s.op->newline(-1) << "};"; | |
7001 | ||
eff6ac72 | 7002 | |
b20bac3a DS |
7003 | // Output handler function for UDPF_BEGIN, UDPF_THREAD_BEGIN, |
7004 | // UDPF_END, and UDPF_THREAD_END | |
7005 | if (flags_seen[UDPF_BEGIN] || flags_seen[UDPF_THREAD_BEGIN] | |
7006 | || flags_seen[UDPF_END] || flags_seen[UDPF_THREAD_END]) | |
159cb109 | 7007 | { |
935447c8 DS |
7008 | s.op->newline() << "static void stap_utrace_probe_handler(struct task_struct *tsk, struct stap_utrace_probe *p) {"; |
7009 | s.op->indent(1); | |
7010 | ||
c12d974f | 7011 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
935447c8 DS |
7012 | |
7013 | // call probe function | |
7014 | s.op->newline() << "(*p->ph) (c);"; | |
7015 | common_probe_entryfn_epilogue (s.op); | |
7016 | ||
7017 | s.op->newline() << "return;"; | |
7018 | s.op->newline(-1) << "}"; | |
159cb109 | 7019 | } |
935447c8 DS |
7020 | |
7021 | // Output handler function for SYSCALL_ENTRY and SYSCALL_EXIT events | |
eff6ac72 | 7022 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
935447c8 | 7023 | { |
4550733e | 7024 | s.op->newline() << "#ifdef UTRACE_ORIG_VERSION"; |
935447c8 | 7025 | s.op->newline() << "static u32 stap_utrace_probe_syscall(struct utrace_attached_engine *engine, struct task_struct *tsk, struct pt_regs *regs) {"; |
4550733e DS |
7026 | s.op->newline() << "#else"; |
7027 | 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) {"; | |
7028 | s.op->newline() << "#endif"; | |
7029 | ||
935447c8 DS |
7030 | s.op->indent(1); |
7031 | s.op->newline() << "struct stap_utrace_probe *p = (struct stap_utrace_probe *)engine->data;"; | |
7032 | ||
c12d974f | 7033 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
935447c8 DS |
7034 | s.op->newline() << "c->regs = regs;"; |
7035 | ||
7036 | // call probe function | |
7037 | s.op->newline() << "(*p->ph) (c);"; | |
7038 | common_probe_entryfn_epilogue (s.op); | |
7039 | ||
b20bac3a DS |
7040 | s.op->newline() << "if ((atomic_read (&session_state) != STAP_SESSION_STARTING) && (atomic_read (&session_state) != STAP_SESSION_RUNNING)) {"; |
7041 | s.op->indent(1); | |
7042 | s.op->newline() << "debug_task_finder_detach();"; | |
7043 | s.op->newline() << "return UTRACE_DETACH;"; | |
7044 | s.op->newline(-1) << "}"; | |
4550733e | 7045 | s.op->newline() << "return UTRACE_RESUME;"; |
935447c8 DS |
7046 | s.op->newline(-1) << "}"; |
7047 | } | |
7048 | ||
eff6ac72 | 7049 | // Output task_finder callback routine that gets called for all |
935447c8 | 7050 | // utrace probe types. |
46b3c6cd | 7051 | 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 |
7052 | s.op->indent(1); |
7053 | s.op->newline() << "int rc = 0;"; | |
7054 | s.op->newline() << "struct stap_utrace_probe *p = container_of(tgt, struct stap_utrace_probe, tgt);"; | |
7055 | s.op->newline() << "struct utrace_attached_engine *engine;"; | |
7056 | ||
935447c8 DS |
7057 | s.op->newline() << "if (register_p) {"; |
7058 | s.op->indent(1); | |
7059 | ||
0744f531 | 7060 | s.op->newline() << "switch (p->flags) {"; |
935447c8 | 7061 | s.op->indent(1); |
eff6ac72 DS |
7062 | |
7063 | // When receiving a UTRACE_EVENT(CLONE) event, we can't call the | |
7064 | // begin/thread.begin probe directly. So, we'll just attach an | |
7065 | // engine that waits for the thread to quiesce. When the thread | |
7066 | // quiesces, then call the probe. | |
7067 | if (flags_seen[UDPF_BEGIN]) | |
7068 | { | |
7069 | s.op->newline() << "case UDPF_BEGIN:"; | |
37e24fbe | 7070 | s.op->indent(1); |
eff6ac72 | 7071 | s.op->newline() << "if (process_p) {"; |
37e24fbe | 7072 | s.op->indent(1); |
b20bac3a | 7073 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
37e24fbe DS |
7074 | s.op->newline(-1) << "}"; |
7075 | s.op->newline() << "break;"; | |
7076 | s.op->indent(-1); | |
eff6ac72 DS |
7077 | } |
7078 | if (flags_seen[UDPF_THREAD_BEGIN]) | |
7079 | { | |
7080 | s.op->newline() << "case UDPF_THREAD_BEGIN:"; | |
0744f531 | 7081 | s.op->indent(1); |
eff6ac72 | 7082 | s.op->newline() << "if (! process_p) {"; |
cfac4b1f | 7083 | s.op->indent(1); |
b20bac3a | 7084 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
cfac4b1f | 7085 | s.op->newline(-1) << "}"; |
0744f531 DS |
7086 | s.op->newline() << "break;"; |
7087 | s.op->indent(-1); | |
eff6ac72 DS |
7088 | } |
7089 | ||
7090 | // For end/thread_end probes, do nothing at registration time. | |
7091 | // We'll handle these in the 'register_p == 0' case. | |
7092 | if (flags_seen[UDPF_END] || flags_seen[UDPF_THREAD_END]) | |
0744f531 | 7093 | { |
eff6ac72 DS |
7094 | s.op->newline() << "case UDPF_END:"; |
7095 | s.op->newline() << "case UDPF_THREAD_END:"; | |
0744f531 DS |
7096 | s.op->indent(1); |
7097 | s.op->newline() << "break;"; | |
7098 | s.op->indent(-1); | |
7099 | } | |
eff6ac72 | 7100 | |
37e24fbe | 7101 | // Attach an engine for SYSCALL_ENTRY and SYSCALL_EXIT events. |
eff6ac72 | 7102 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
0744f531 | 7103 | { |
eff6ac72 DS |
7104 | s.op->newline() << "case UDPF_SYSCALL:"; |
7105 | s.op->newline() << "case UDPF_SYSCALL_RETURN:"; | |
0744f531 | 7106 | s.op->indent(1); |
eff6ac72 DS |
7107 | s.op->newline() << "rc = stap_utrace_attach(tsk, &p->ops, p, p->events);"; |
7108 | s.op->newline() << "if (rc == 0) {"; | |
0744f531 | 7109 | s.op->indent(1); |
0744f531 DS |
7110 | s.op->newline() << "p->engine_attached = 1;"; |
7111 | s.op->newline(-1) << "}"; | |
7112 | s.op->newline() << "break;"; | |
7113 | s.op->indent(-1); | |
7114 | } | |
eff6ac72 DS |
7115 | |
7116 | s.op->newline() << "default:"; | |
7117 | s.op->indent(1); | |
7118 | s.op->newline() << "_stp_error(\"unhandled flag value %d at %s:%d\", p->flags, __FUNCTION__, __LINE__);"; | |
7119 | s.op->newline() << "break;"; | |
7120 | s.op->indent(-1); | |
935447c8 DS |
7121 | s.op->newline(-1) << "}"; |
7122 | s.op->newline(-1) << "}"; | |
eff6ac72 | 7123 | |
935447c8 | 7124 | // Since this engine could be attached to multiple threads, don't |
b20bac3a DS |
7125 | // call stap_utrace_detach_ops() here, only call |
7126 | // stap_utrace_detach() as necessary. | |
0744f531 | 7127 | s.op->newline() << "else {"; |
935447c8 | 7128 | s.op->indent(1); |
37e24fbe DS |
7129 | s.op->newline() << "switch (p->flags) {"; |
7130 | s.op->indent(1); | |
0744f531 | 7131 | // For death probes, go ahead and call the probe directly. |
eff6ac72 | 7132 | if (flags_seen[UDPF_END]) |
0744f531 | 7133 | { |
eff6ac72 | 7134 | s.op->newline() << "case UDPF_END:"; |
37e24fbe | 7135 | s.op->indent(1); |
eff6ac72 | 7136 | s.op->newline() << "if (process_p) {"; |
0744f531 | 7137 | s.op->indent(1); |
0744f531 DS |
7138 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
7139 | s.op->newline(-1) << "}"; | |
37e24fbe DS |
7140 | s.op->newline() << "break;"; |
7141 | s.op->indent(-1); | |
0744f531 | 7142 | } |
eff6ac72 | 7143 | if (flags_seen[UDPF_THREAD_END]) |
37e24fbe | 7144 | { |
eff6ac72 | 7145 | s.op->newline() << "case UDPF_THREAD_END:"; |
37e24fbe | 7146 | s.op->indent(1); |
eff6ac72 DS |
7147 | s.op->newline() << "if (! process_p) {"; |
7148 | s.op->indent(1); | |
7149 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; | |
7150 | s.op->newline(-1) << "}"; | |
7151 | s.op->newline() << "break;"; | |
7152 | s.op->indent(-1); | |
7153 | } | |
7154 | ||
b20bac3a DS |
7155 | // For begin/thread_begin probes, we don't need to do anything. |
7156 | if (flags_seen[UDPF_BEGIN] || flags_seen[UDPF_THREAD_BEGIN]) | |
7157 | { | |
eff6ac72 DS |
7158 | s.op->newline() << "case UDPF_BEGIN:"; |
7159 | s.op->newline() << "case UDPF_THREAD_BEGIN:"; | |
b20bac3a DS |
7160 | s.op->indent(1); |
7161 | s.op->newline() << "break;"; | |
7162 | s.op->indent(-1); | |
7163 | } | |
28d29bd3 | 7164 | |
b20bac3a DS |
7165 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
7166 | { | |
eff6ac72 DS |
7167 | s.op->newline() << "case UDPF_SYSCALL:"; |
7168 | s.op->newline() << "case UDPF_SYSCALL_RETURN:"; | |
37e24fbe | 7169 | s.op->indent(1); |
41211ba3 | 7170 | s.op->newline() << "stap_utrace_detach(tsk, &p->ops);"; |
37e24fbe DS |
7171 | s.op->newline() << "break;"; |
7172 | s.op->indent(-1); | |
7173 | } | |
eff6ac72 DS |
7174 | |
7175 | s.op->newline() << "default:"; | |
7176 | s.op->indent(1); | |
7177 | s.op->newline() << "_stp_error(\"unhandled flag value %d at %s:%d\", p->flags, __FUNCTION__, __LINE__);"; | |
7178 | s.op->newline() << "break;"; | |
7179 | s.op->indent(-1); | |
37e24fbe | 7180 | s.op->newline(-1) << "}"; |
935447c8 DS |
7181 | s.op->newline(-1) << "}"; |
7182 | s.op->newline() << "return rc;"; | |
7183 | s.op->newline(-1) << "}"; | |
7184 | ||
4c2732a1 | 7185 | s.op->newline() << "static struct stap_utrace_probe stap_utrace_probes[] = {"; |
935447c8 DS |
7186 | s.op->indent(1); |
7187 | ||
7188 | // Set up 'process(PATH)' probes | |
7189 | if (! probes_by_path.empty()) | |
7190 | { | |
7191 | for (p_b_path_iterator it = probes_by_path.begin(); | |
7192 | it != probes_by_path.end(); it++) | |
7193 | { | |
a21d81ec DS |
7194 | // Emit a "fake" probe decl that is really a hook for to get |
7195 | // our vm_callback called. | |
7196 | string path = it->first; | |
a21d81ec DS |
7197 | emit_vm_callback_probe_decl (s, true, path, (int64_t)0, |
7198 | "__stp_tf_vm_cb"); | |
a21d81ec | 7199 | |
935447c8 DS |
7200 | for (unsigned i = 0; i < it->second.size(); i++) |
7201 | { | |
7202 | utrace_derived_probe *p = it->second[i]; | |
7203 | emit_probe_decl(s, p); | |
7204 | } | |
7205 | } | |
7206 | } | |
7207 | ||
7208 | // Set up 'process(PID)' probes | |
7209 | if (! probes_by_pid.empty()) | |
7210 | { | |
7211 | for (p_b_pid_iterator it = probes_by_pid.begin(); | |
7212 | it != probes_by_pid.end(); it++) | |
7213 | { | |
a21d81ec DS |
7214 | // Emit a "fake" probe decl that is really a hook for to get |
7215 | // our vm_callback called. | |
5e86a4ee | 7216 | emit_vm_callback_probe_decl (s, false, "", it->first, |
a21d81ec | 7217 | "__stp_tf_vm_cb"); |
a21d81ec | 7218 | |
935447c8 DS |
7219 | for (unsigned i = 0; i < it->second.size(); i++) |
7220 | { | |
7221 | utrace_derived_probe *p = it->second[i]; | |
7222 | emit_probe_decl(s, p); | |
7223 | } | |
7224 | } | |
7225 | } | |
7226 | s.op->newline(-1) << "};"; | |
7227 | } | |
7228 | ||
7229 | ||
7230 | void | |
7231 | utrace_derived_probe_group::emit_module_init (systemtap_session& s) | |
7232 | { | |
7233 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
7234 | return; | |
7235 | ||
7236 | s.op->newline(); | |
7237 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
278def10 | 7238 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_utrace_probes); i++) {"; |
935447c8 DS |
7239 | s.op->indent(1); |
7240 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[i];"; | |
7241 | s.op->newline() << "rc = stap_register_task_finder_target(&p->tgt);"; | |
7242 | s.op->newline(-1) << "}"; | |
7243 | ||
7244 | // rollback all utrace probes | |
7245 | s.op->newline() << "if (rc) {"; | |
7246 | s.op->indent(1); | |
7247 | s.op->newline() << "for (j=i-1; j>=0; j--) {"; | |
7248 | s.op->indent(1); | |
7249 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[j];"; | |
7250 | ||
7251 | s.op->newline() << "if (p->engine_attached) {"; | |
7252 | s.op->indent(1); | |
7253 | s.op->newline() << "stap_utrace_detach_ops(&p->ops);"; | |
7254 | s.op->newline(-1) << "}"; | |
7255 | s.op->newline(-1) << "}"; | |
7256 | ||
7257 | s.op->newline(-1) << "}"; | |
7258 | } | |
7259 | ||
7260 | ||
7261 | void | |
7262 | utrace_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7263 | { | |
7264 | if (probes_by_path.empty() && probes_by_pid.empty()) return; | |
7265 | ||
7266 | s.op->newline(); | |
7267 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
278def10 | 7268 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_utrace_probes); i++) {"; |
935447c8 DS |
7269 | s.op->indent(1); |
7270 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[i];"; | |
7271 | ||
7272 | s.op->newline() << "if (p->engine_attached) {"; | |
7273 | s.op->indent(1); | |
7274 | s.op->newline() << "stap_utrace_detach_ops(&p->ops);"; | |
7275 | s.op->newline(-1) << "}"; | |
7276 | s.op->newline(-1) << "}"; | |
7277 | } | |
7278 | ||
7279 | ||
888af770 FCE |
7280 | // ------------------------------------------------------------------------ |
7281 | // user-space probes | |
7282 | // ------------------------------------------------------------------------ | |
7283 | ||
888af770 FCE |
7284 | |
7285 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> | |
7286 | { | |
7287 | public: | |
7288 | void emit_module_decls (systemtap_session& s); | |
7289 | void emit_module_init (systemtap_session& s); | |
7290 | void emit_module_exit (systemtap_session& s); | |
7291 | }; | |
7292 | ||
7293 | ||
6d0f3f0c FCE |
7294 | uprobe_derived_probe::uprobe_derived_probe (const string& function, |
7295 | const string& filename, | |
7296 | int line, | |
7297 | const string& module, | |
7298 | int pid, | |
7299 | const string& section, | |
7300 | Dwarf_Addr dwfl_addr, | |
7301 | Dwarf_Addr addr, | |
7302 | dwarf_query & q, | |
7303 | Dwarf_Die* scope_die /* may be null */): | |
7304 | derived_probe (q.base_probe, new probe_point (*q.base_loc) /* .components soon rewritten */ ), | |
7305 | return_p (q.has_return), module (module), pid (pid), section (section), address (addr) | |
4baf0e53 | 7306 | { |
17c128f2 FCE |
7307 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. |
7308 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
7309 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
7310 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
6d0f3f0c FCE |
7311 | |
7312 | this->tok = q.base_probe->tok; | |
7313 | ||
de688825 | 7314 | // Expand target variables in the probe body |
6d0f3f0c FCE |
7315 | if (!null_die(scope_die)) |
7316 | { | |
de688825 | 7317 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); // XXX: user-space deref's! |
4ed05b15 | 7318 | this->body = v.require (this->body); |
6d0f3f0c FCE |
7319 | |
7320 | // If during target-variable-expanding the probe, we added a new block | |
7321 | // of code, add it to the start of the probe. | |
7322 | if (v.add_block) | |
7323 | this->body = new block(v.add_block, this->body); | |
7324 | ||
7325 | // If when target-variable-expanding the probe, we added a new | |
7326 | // probe, add it in a new file to the list of files to be processed. | |
7327 | if (v.add_probe) | |
7328 | { | |
7329 | stapfile *f = new stapfile; | |
7330 | f->probes.push_back(v.add_probe); | |
7331 | q.sess.files.push_back(f); | |
7332 | } | |
7333 | } | |
7334 | // else - null scope_die - $target variables will produce an error during translate phase | |
7335 | ||
7336 | // Reset the sole element of the "locations" vector as a | |
7337 | // "reverse-engineered" form of the incoming (q.base_loc) probe | |
7338 | // point. This allows a user to see what function / file / line | |
7339 | // number any particular match of the wildcards. | |
7340 | ||
7341 | vector<probe_point::component*> comps; | |
7342 | if(q.has_process) | |
7343 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
7344 | else | |
7345 | assert (0); | |
06aca46a | 7346 | |
6d0f3f0c FCE |
7347 | string fn_or_stmt; |
7348 | if (q.has_function_str || q.has_function_num) | |
7349 | fn_or_stmt = "function"; | |
7350 | else | |
7351 | fn_or_stmt = "statement"; | |
7352 | ||
7353 | if (q.has_function_str || q.has_statement_str) | |
7354 | { | |
7355 | string retro_name = function; | |
7356 | if (filename != "") | |
fb84c077 FCE |
7357 | { |
7358 | retro_name += ("@" + string (filename)); | |
7359 | if (line > 0) | |
7360 | retro_name += (":" + lex_cast<string> (line)); | |
7361 | } | |
6d0f3f0c FCE |
7362 | comps.push_back |
7363 | (new probe_point::component | |
7364 | (fn_or_stmt, new literal_string (retro_name))); | |
7365 | } | |
7366 | else if (q.has_function_num || q.has_statement_num) | |
7367 | { | |
7368 | Dwarf_Addr retro_addr; | |
7369 | if (q.has_function_num) | |
7370 | retro_addr = q.function_num_val; | |
7371 | else | |
7372 | retro_addr = q.statement_num_val; | |
7373 | comps.push_back (new probe_point::component | |
7374 | (fn_or_stmt, | |
7375 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
7376 | ||
7377 | if (q.has_absolute) | |
7378 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
7379 | } | |
7380 | ||
7381 | if (q.has_call) | |
7382 | comps.push_back (new probe_point::component(TOK_CALL)); | |
7383 | if (q.has_inline) | |
7384 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
7385 | if (return_p) | |
7386 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
7387 | /* | |
7388 | if (has_maxactive) | |
7389 | comps.push_back (new probe_point::component | |
7390 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
7391 | */ | |
7392 | ||
7393 | // Overwrite it. | |
7394 | this->sole_location()->components = comps; | |
7395 | } | |
7396 | ||
7397 | ||
0973d815 FCE |
7398 | uprobe_derived_probe::uprobe_derived_probe (probe *base, |
7399 | probe_point *location, | |
7400 | int pid, | |
7401 | Dwarf_Addr addr, | |
7402 | bool has_return): | |
7403 | derived_probe (base, location), // location is not rewritten here | |
7404 | return_p (has_return), pid (pid), address (addr) | |
7405 | { | |
7406 | } | |
7407 | ||
7408 | ||
6d0f3f0c FCE |
7409 | void |
7410 | uprobe_derived_probe::printsig (ostream& o) const | |
7411 | { | |
7412 | // Same as dwarf_derived_probe. | |
7413 | sole_location()->print (o); | |
7414 | o << " /* pc=" << section << "+0x" << hex << address << dec << " */"; | |
7415 | printsig_nested (o); | |
888af770 FCE |
7416 | } |
7417 | ||
7418 | ||
7419 | void | |
7420 | uprobe_derived_probe::join_group (systemtap_session& s) | |
7421 | { | |
7422 | if (! s.uprobe_derived_probes) | |
7423 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
7424 | s.uprobe_derived_probes->enroll (this); | |
6d0f3f0c | 7425 | task_finder_derived_probe_group::create_session_group (s); |
888af770 FCE |
7426 | } |
7427 | ||
7428 | ||
7429 | struct uprobe_builder: public derived_probe_builder | |
7430 | { | |
7431 | uprobe_builder() {} | |
7432 | virtual void build(systemtap_session & sess, | |
7433 | probe * base, | |
7434 | probe_point * location, | |
86bf665e | 7435 | literal_map_t const & parameters, |
888af770 FCE |
7436 | vector<derived_probe *> & finished_results) |
7437 | { | |
7438 | int64_t process, address; | |
7439 | ||
7440 | bool b1 = get_param (parameters, TOK_PROCESS, process); | |
ced347a9 | 7441 | (void) b1; |
888af770 | 7442 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 7443 | (void) b2; |
888af770 FCE |
7444 | bool rr = has_null_param (parameters, TOK_RETURN); |
7445 | assert (b1 && b2); // by pattern_root construction | |
4baf0e53 | 7446 | |
0973d815 | 7447 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
888af770 FCE |
7448 | } |
7449 | }; | |
7450 | ||
7451 | ||
7452 | void | |
775d51e5 | 7453 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) |
888af770 FCE |
7454 | { |
7455 | if (probes.empty()) return; | |
775d51e5 | 7456 | s.op->newline() << "/* ---- user probes ---- */"; |
4baf0e53 | 7457 | |
6d0f3f0c FCE |
7458 | s.need_uprobes = true; // Ask buildrun.cxx to build extra module if needed |
7459 | ||
c480bf3d | 7460 | // If uprobes isn't in the kernel, pull it in from the runtime. |
6274464e | 7461 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; |
888af770 | 7462 | s.op->newline() << "#include <linux/uprobes.h>"; |
c480bf3d | 7463 | s.op->newline() << "#else"; |
6274464e | 7464 | s.op->newline() << "#include \"uprobes/uprobes.h\""; |
c480bf3d | 7465 | s.op->newline() << "#endif"; |
6d0f3f0c FCE |
7466 | s.op->newline() << "#include \"task_finder.c\""; |
7467 | ||
01b05e2e | 7468 | s.op->newline() << "#ifndef MULTIPLE_UPROBES"; |
478c850f FCE |
7469 | s.op->newline() << "#define MULTIPLE_UPROBES 256"; // maximum possible armed uprobes per process() probe point |
7470 | // or apprx. max number of processes mapping a shared library | |
01b05e2e | 7471 | s.op->newline() << "#endif"; |
6d0f3f0c | 7472 | s.op->newline() << "#ifndef MAXUPROBES"; |
01b05e2e | 7473 | s.op->newline() << "#define MAXUPROBES (MULTIPLE_UPROBES * " << probes.size() << ")"; |
6d0f3f0c | 7474 | s.op->newline() << "#endif"; |
888af770 | 7475 | |
5e112f92 FCE |
7476 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
7477 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
4c2732a1 | 7478 | s.op->newline() << "static struct stap_uprobe {"; |
888af770 | 7479 | s.op->newline(1) << "union { struct uprobe up; struct uretprobe urp; };"; |
6d0f3f0c | 7480 | s.op->newline() << "int spec_index;"; // index into stap_uprobe_specs; <0 == free && unregistered |
01b05e2e | 7481 | s.op->newline(-1) << "} stap_uprobes [MAXUPROBES];"; |
5e112f92 | 7482 | s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
6d0f3f0c | 7483 | |
4c2732a1 | 7484 | s.op->newline() << "static struct stap_uprobe_spec {"; |
5e112f92 | 7485 | s.op->newline(1) << "struct stap_task_finder_target finder;"; |
888af770 | 7486 | s.op->newline() << "unsigned long address;"; |
17c128f2 | 7487 | s.op->newline() << "const char *pathname;"; |
888af770 FCE |
7488 | s.op->newline() << "const char *pp;"; |
7489 | s.op->newline() << "void (*ph) (struct context*);"; | |
6d0f3f0c FCE |
7490 | s.op->newline() << "unsigned return_p:1;"; |
7491 | s.op->newline(-1) << "} stap_uprobe_specs [] = {"; | |
888af770 FCE |
7492 | s.op->indent(1); |
7493 | for (unsigned i =0; i<probes.size(); i++) | |
7494 | { | |
7495 | uprobe_derived_probe* p = probes[i]; | |
7496 | s.op->newline() << "{"; | |
0973d815 FCE |
7497 | s.op->line() << " .finder = {"; |
7498 | if (p->pid != 0) | |
17c128f2 FCE |
7499 | s.op->line() << " .pid=" << p->pid; |
7500 | else if (p->section == ".absolute") | |
0973d815 | 7501 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; |
06aca46a | 7502 | // else ".dynamic" gets pathname=0, pid=0, activating task_finder "global tracing" |
0973d815 | 7503 | s.op->line() << "},"; |
17c128f2 FCE |
7504 | if (p->section != ".absolute") |
7505 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
dc38c256 | 7506 | s.op->line() << " .address=(unsigned long)0x" << hex << p->address << dec << "ULL,"; |
888af770 FCE |
7507 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
7508 | s.op->line() << " .ph=&" << p->name << ","; | |
7509 | if (p->return_p) s.op->line() << " .return_p=1,"; | |
7510 | s.op->line() << " },"; | |
7511 | } | |
7512 | s.op->newline(-1) << "};"; | |
7513 | ||
48e685da | 7514 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 7515 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
6d0f3f0c | 7516 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 7517 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
7518 | s.op->newline() << "if (sup->spec_index < 0 ||" |
7519 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 | 7520 | s.op->newline() << "c->regs = regs;"; |
6d0f3f0c | 7521 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
7522 | common_probe_entryfn_epilogue (s.op); |
7523 | s.op->newline(-1) << "}"; | |
7524 | ||
48e685da | 7525 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 7526 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
6d0f3f0c | 7527 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 7528 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
7529 | s.op->newline() << "if (sup->spec_index < 0 ||" |
7530 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 FCE |
7531 | // XXX: kretprobes saves "c->pi = inst;" too |
7532 | s.op->newline() << "c->regs = regs;"; | |
6d0f3f0c | 7533 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
7534 | common_probe_entryfn_epilogue (s.op); |
7535 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7536 | |
17c128f2 FCE |
7537 | |
7538 | ||
6d0f3f0c FCE |
7539 | // NB: Because these utrace callbacks only occur before / after |
7540 | // userspace instructions run, there is no concurrency control issue | |
7541 | // between active uprobe callbacks and these registration / | |
06aca46a | 7542 | // unregistration pieces. |
6d0f3f0c FCE |
7543 | |
7544 | // We protect the stap_uprobe->spec_index (which also serves as a | |
d41d451c FCE |
7545 | // free/busy flag) value with the outer protective stap_probes_lock |
7546 | // spinlock, to protect it against concurrent registration / | |
7547 | // unregistration. | |
6d0f3f0c FCE |
7548 | |
7549 | s.op->newline(); | |
17c128f2 FCE |
7550 | s.op->newline() << "static int stap_uprobe_change (struct task_struct *tsk, int register_p, unsigned long relocation, struct stap_uprobe_spec *sups) {"; |
7551 | s.op->newline(1) << "int spec_index = (sups - stap_uprobe_specs);"; | |
6d0f3f0c | 7552 | s.op->newline() << "int handled_p = 0;"; |
d41d451c | 7553 | s.op->newline() << "int slotted_p = 0;"; |
6d0f3f0c FCE |
7554 | s.op->newline() << "int rc = 0;"; |
7555 | s.op->newline() << "int i;"; | |
3568f1dd | 7556 | |
d41d451c | 7557 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; |
01b05e2e | 7558 | s.op->newline() << "for (i=0; i<MAXUPROBES; i++) {"; // XXX: slow linear search |
5e112f92 | 7559 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; |
6d0f3f0c FCE |
7560 | |
7561 | // register new uprobe | |
7562 | s.op->newline() << "if (register_p && sup->spec_index < 0) {"; | |
80b4ad8b FCE |
7563 | // PR6829: we need to check that the sup we're about to reuse is really completely free. |
7564 | // See PR6829 notes below. | |
7565 | s.op->newline(1) << "if (sup->spec_index == -1 && sup->up.kdata != NULL) continue;"; | |
7566 | s.op->newline() << "else if (sup->spec_index == -2 && sup->urp.u.kdata != NULL) continue;"; | |
7567 | s.op->newline() << "sup->spec_index = spec_index;"; | |
d41d451c FCE |
7568 | s.op->newline() << "slotted_p = 1;"; |
7569 | s.op->newline() << "break;"; | |
7570 | s.op->newline(-1) << "} else if (!register_p && " | |
7571 | << "sup->spec_index == spec_index && " // a u[ret]probe set up for this probe point | |
7572 | << "((sups->return_p && sup->urp.u.pid == tsk->tgid && sup->urp.u.vaddr == relocation + sups->address) ||" // dying uretprobe | |
7573 | << "(!sups->return_p && sup->up.pid == tsk->tgid && sup->up.vaddr == relocation + sups->address))) {"; // dying uprobe | |
7574 | s.op->newline(1) << "slotted_p = 1;"; | |
7575 | s.op->newline() << "break;"; // exit to-free slot search | |
7576 | s.op->newline(-1) << "}"; | |
7577 | ||
7578 | s.op->newline(-1) << "}"; | |
7579 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; | |
7580 | ||
7581 | s.op->newline() << "#ifdef DEBUG_UPROBES"; | |
7582 | s.op->newline() << "printk (KERN_INFO \"%cuprobe spec %d idx %d process %s[%d] reloc %p pp '%s'\\n\", "; | |
7583 | s.op->line() << "(register_p ? '+' : '-'), spec_index, (slotted_p ? i : -1), tsk->comm, tsk->tgid, (void*) relocation, sups->pp);"; | |
7584 | s.op->newline() << "#endif"; | |
7585 | ||
7586 | // Here, slotted_p implies that `i' points to the single | |
7587 | // stap_uprobes[] element that has been slotted in for registration | |
7588 | // or unregistration processing. !slotted_p implies that the table | |
7589 | // was full (registration; MAXUPROBES) or that no matching entry was | |
7590 | // found (unregistration; should not happen). | |
7591 | ||
7592 | s.op->newline() << "if (register_p && slotted_p) {"; | |
7593 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
6d0f3f0c FCE |
7594 | s.op->newline() << "if (sups->return_p) {"; |
7595 | s.op->newline(1) << "sup->urp.u.pid = tsk->tgid;"; | |
17c128f2 | 7596 | s.op->newline() << "sup->urp.u.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
7597 | s.op->newline() << "sup->urp.handler = &enter_uretprobe_probe;"; |
7598 | s.op->newline() << "rc = register_uretprobe (& sup->urp);"; | |
7599 | s.op->newline(-1) << "} else {"; | |
7600 | s.op->newline(1) << "sup->up.pid = tsk->tgid;"; | |
17c128f2 | 7601 | s.op->newline() << "sup->up.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
7602 | s.op->newline() << "sup->up.handler = &enter_uprobe_probe;"; |
7603 | s.op->newline() << "rc = register_uprobe (& sup->up);"; | |
7604 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7605 | s.op->newline() << "if (rc) {"; // failed to register |
d41d451c FCE |
7606 | 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);"; |
7607 | // NB: we need to release this slot, so we need to borrow the mutex temporarily. | |
7608 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; | |
3568f1dd | 7609 | s.op->newline() << "sup->spec_index = -1;"; |
d41d451c | 7610 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
6d0f3f0c FCE |
7611 | s.op->newline(-1) << "} else {"; |
7612 | s.op->newline(1) << "handled_p = 1;"; // success | |
7613 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7614 | |
d41d451c FCE |
7615 | s.op->newline(-1) << "} else if (!register_p && slotted_p) {"; |
7616 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
7617 | // NB: we need to release this slot, so we need to borrow the mutex temporarily. | |
7618 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; | |
7619 | // NB: We must not actually uregister u[ret]probes when a target process execs or exits; | |
80b4ad8b | 7620 | // uprobes does that by itself asynchronously. We can reuse the up/urp struct after |
d41d451c | 7621 | // uprobes clears the sup->{up,urp}->kdata pointer. PR6829. To tell the two |
80b4ad8b | 7622 | // cases apart, we use spec_index -2 vs -1. |
d41d451c | 7623 | s.op->newline() << "sup->spec_index = (sups->return_p ? -2 : -1);"; |
5e112f92 | 7624 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
d41d451c FCE |
7625 | s.op->newline() << "handled_p = 1;"; |
7626 | s.op->newline(-1) << "}"; // if slotted_p | |
7627 | ||
7628 | // NB: handled_p implies slotted_p | |
7629 | ||
6d0f3f0c | 7630 | s.op->newline() << "if (! handled_p) {"; |
73209876 FCE |
7631 | s.op->newline(1) << "#ifdef STP_TIMING"; |
7632 | s.op->newline() << "atomic_inc (register_p ? & skipped_count_uprobe_reg : & skipped_count_uprobe_unreg);"; | |
7633 | s.op->newline() << "#endif"; | |
7634 | // NB: duplicates common_entryfn_epilogue, but then this is not a probe entry fn epilogue. | |
7635 | s.op->newline() << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
6d0f3f0c FCE |
7636 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; |
7637 | s.op->newline() << "_stp_exit ();"; | |
7638 | s.op->newline(-1) << "}"; | |
7639 | s.op->newline(-1) << "}"; | |
06aca46a | 7640 | |
6d0f3f0c FCE |
7641 | s.op->newline() << "return 0;"; // XXX: or rc? |
7642 | s.op->newline(-1) << "}"; | |
7643 | s.op->assert_0_indent(); | |
7644 | ||
17c128f2 FCE |
7645 | |
7646 | // The task_finder_callback we use for ET_EXEC targets. | |
7647 | s.op->newline(); | |
7648 | 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) {"; | |
7649 | ||
7650 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; | |
7651 | s.op->newline() << "if (! process_p) return 0;"; | |
7652 | s.op->newline(0) << "return stap_uprobe_change (tsk, register_p, 0, sups);"; | |
7653 | s.op->newline(-1) << "}"; | |
7654 | ||
7655 | // The task_finder_vm_callback we use for ET_DYN targets. | |
7656 | s.op->newline(); | |
7657 | s.op->newline() << "static int stap_uprobe_vmchange_found (struct stap_task_finder_target *tgt, struct task_struct *tsk, int map_p, char *vm_path, unsigned long vm_start, unsigned long vm_end, unsigned long vm_pgoff) {"; | |
7658 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; | |
7659 | // 1 - shared libraries' executable segments load from offset 0 - ld.so convention | |
06aca46a | 7660 | s.op->newline() << "if (vm_pgoff != 0) return 0;"; |
17c128f2 | 7661 | // 2 - the shared library we're interested in |
06aca46a | 7662 | s.op->newline() << "if (vm_path == NULL || strcmp (vm_path, sups->pathname)) return 0;"; |
17c128f2 | 7663 | // 3 - probe address within the mapping limits; test should not fail |
06aca46a FCE |
7664 | s.op->newline() << "if (vm_end <= vm_start + sups->address) return 0;"; |
7665 | ||
c16d425a FCE |
7666 | s.op->newline() << "#ifdef DEBUG_TASK_FINDER_VMA"; |
7667 | s.op->newline() << "printk (KERN_INFO \"vmchange pid %d map_p %d path %s vms %p vme %p vmp %p\\n\", tsk->tgid, map_p, vm_path, (void*) vm_start, (void*) vm_end, (void*) vm_pgoff);"; | |
7668 | s.op->newline() << "printk (KERN_INFO \"sups %p pp %s path %s address %p\\n\", sups, sups->pp, sups->pathname ?: \"\", (void*) sups->address);"; | |
7669 | s.op->newline() << "#endif"; | |
7670 | ||
17c128f2 FCE |
7671 | s.op->newline(0) << "return stap_uprobe_change (tsk, map_p, vm_start, sups);"; |
7672 | s.op->newline(-1) << "}"; | |
7673 | s.op->assert_0_indent(); | |
7674 | ||
7675 | ||
6d0f3f0c | 7676 | s.op->newline(); |
888af770 FCE |
7677 | } |
7678 | ||
7679 | ||
7680 | void | |
7681 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
7682 | { | |
7683 | if (probes.empty()) return; | |
5e112f92 FCE |
7684 | s.op->newline() << "/* ---- user probes ---- */"; |
7685 | ||
01b05e2e | 7686 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7687 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7688 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
7689 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
7690 | // initialized to zero. This is so that we can use | |
7691 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
7692 | s.op->newline(-1) << "}"; |
7693 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
888af770 FCE |
7694 | |
7695 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
6d0f3f0c FCE |
7696 | s.op->newline(1) << "struct stap_uprobe_spec *sups = & stap_uprobe_specs[i];"; |
7697 | s.op->newline() << "probe_point = sups->pp;"; // for error messages | |
17c128f2 FCE |
7698 | s.op->newline() << "if (sups->finder.pathname) sups->finder.callback = & stap_uprobe_process_found;"; |
7699 | s.op->newline() << "else if (sups->pathname) sups->finder.vm_callback = & stap_uprobe_vmchange_found;"; | |
6d0f3f0c | 7700 | s.op->newline() << "rc = stap_register_task_finder_target (& sups->finder);"; |
5e112f92 FCE |
7701 | |
7702 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any | |
7703 | // finders already registered, since mere registration does not | |
7704 | // cause any utrace or memory allocation actions. That happens only | |
7705 | // later, once the task finder engine starts running. So, for a | |
7706 | // partial initialization requiring unwind, we need do nothing. | |
7707 | s.op->newline() << "if (rc) break;"; | |
7708 | ||
888af770 FCE |
7709 | s.op->newline(-1) << "}"; |
7710 | } | |
7711 | ||
7712 | ||
7713 | void | |
7714 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7715 | { | |
7716 | if (probes.empty()) return; | |
7717 | s.op->newline() << "/* ---- user probes ---- */"; | |
7718 | ||
6d0f3f0c FCE |
7719 | // NB: there is no stap_unregister_task_finder_target call; |
7720 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
7721 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
7722 | // | |
7723 | // This function blocks until all callbacks are completed, so there | |
7724 | // is supposed to be no possibility of any registration-related code starting | |
7725 | // to run in parallel with our shutdown here. So we don't need to protect the | |
7726 | // stap_uprobes[] array with the mutex. | |
5e112f92 | 7727 | |
01b05e2e | 7728 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7729 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7730 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; | |
6d0f3f0c | 7731 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd FCE |
7732 | |
7733 | s.op->newline() << "if (sups->return_p) {"; | |
7734 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 7735 | 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 | 7736 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
7737 | // NB: PR6829 does not change that we still need to unregister at |
7738 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
7739 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
7740 | s.op->newline(-1) << "} else {"; | |
7741 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 7742 | 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 |
7743 | s.op->newline() << "#endif"; |
7744 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
7745 | s.op->newline(-1) << "}"; | |
7746 | ||
6d0f3f0c | 7747 | s.op->newline() << "sup->spec_index = -1;"; |
3568f1dd FCE |
7748 | |
7749 | // XXX: uprobe missed counts? | |
7750 | ||
6d0f3f0c | 7751 | s.op->newline(-1) << "}"; |
5e112f92 FCE |
7752 | |
7753 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; | |
888af770 FCE |
7754 | } |
7755 | ||
7756 | ||
7757 | ||
98afd80e FCE |
7758 | // ------------------------------------------------------------------------ |
7759 | // timer derived probes | |
7760 | // ------------------------------------------------------------------------ | |
7761 | ||
7762 | ||
12b21830 DS |
7763 | static string TOK_TIMER("timer"); |
7764 | ||
98afd80e FCE |
7765 | struct timer_derived_probe: public derived_probe |
7766 | { | |
7767 | int64_t interval, randomize; | |
b20febf3 | 7768 | bool time_is_msecs; // NB: hrtimers get ms-based probes on modern kernels instead |
422d1ceb | 7769 | timer_derived_probe (probe* p, probe_point* l, int64_t i, int64_t r, bool ms=false); |
b20febf3 FCE |
7770 | virtual void join_group (systemtap_session& s); |
7771 | }; | |
98afd80e | 7772 | |
dc38c0ae | 7773 | |
b20febf3 FCE |
7774 | struct timer_derived_probe_group: public generic_dpg<timer_derived_probe> |
7775 | { | |
d006ef4f | 7776 | void emit_interval (translator_output* o); |
b20febf3 FCE |
7777 | public: |
7778 | void emit_module_decls (systemtap_session& s); | |
7779 | void emit_module_init (systemtap_session& s); | |
7780 | void emit_module_exit (systemtap_session& s); | |
98afd80e FCE |
7781 | }; |
7782 | ||
7783 | ||
422d1ceb FCE |
7784 | timer_derived_probe::timer_derived_probe (probe* p, probe_point* l, int64_t i, int64_t r, bool ms): |
7785 | derived_probe (p, l), interval (i), randomize (r), time_is_msecs(ms) | |
98afd80e FCE |
7786 | { |
7787 | if (interval <= 0 || interval > 1000000) // make i and r fit into plain ints | |
7788 | throw semantic_error ("invalid interval for jiffies timer"); | |
7789 | // randomize = 0 means no randomization | |
7790 | if (randomize < 0 || randomize > interval) | |
7791 | throw semantic_error ("invalid randomize for jiffies timer"); | |
7792 | ||
7793 | if (locations.size() != 1) | |
7794 | throw semantic_error ("expect single probe point"); | |
7795 | // so we don't have to loop over them in the other functions | |
7796 | } | |
7797 | ||
7798 | ||
dc38c0ae | 7799 | void |
b20febf3 | 7800 | timer_derived_probe::join_group (systemtap_session& s) |
dc38c0ae | 7801 | { |
b20febf3 FCE |
7802 | if (! s.timer_derived_probes) |
7803 | s.timer_derived_probes = new timer_derived_probe_group (); | |
7804 | s.timer_derived_probes->enroll (this); | |
dc38c0ae DS |
7805 | } |
7806 | ||
7807 | ||
d006ef4f JS |
7808 | void |
7809 | timer_derived_probe_group::emit_interval (translator_output* o) | |
7810 | { | |
7811 | o->line() << "({"; | |
7812 | o->newline(1) << "unsigned i = stp->intrv;"; | |
7813 | o->newline() << "if (stp->rnd != 0)"; | |
7814 | o->newline(1) << "i += _stp_random_pm(stp->rnd);"; | |
7815 | o->newline(-1) << "stp->ms ? msecs_to_jiffies(i) : i;"; | |
7816 | o->newline(-1) << "})"; | |
7817 | } | |
7818 | ||
7819 | ||
98afd80e | 7820 | void |
b20febf3 | 7821 | timer_derived_probe_group::emit_module_decls (systemtap_session& s) |
98afd80e | 7822 | { |
b20febf3 | 7823 | if (probes.empty()) return; |
46b84a80 | 7824 | |
b20febf3 | 7825 | s.op->newline() << "/* ---- timer probes ---- */"; |
46b84a80 | 7826 | |
4c2732a1 | 7827 | s.op->newline() << "static struct stap_timer_probe {"; |
b20febf3 FCE |
7828 | s.op->newline(1) << "struct timer_list timer_list;"; |
7829 | s.op->newline() << "const char *pp;"; | |
7830 | s.op->newline() << "void (*ph) (struct context*);"; | |
7831 | s.op->newline() << "unsigned intrv, ms, rnd;"; | |
7832 | s.op->newline(-1) << "} stap_timer_probes [" << probes.size() << "] = {"; | |
7833 | s.op->indent(1); | |
7834 | for (unsigned i=0; i < probes.size(); i++) | |
7835 | { | |
4baf0e53 RM |
7836 | s.op->newline () << "{"; |
7837 | s.op->line() << " .pp=" | |
b20febf3 FCE |
7838 | << lex_cast_qstring (*probes[i]->sole_location()) << ","; |
7839 | s.op->line() << " .ph=&" << probes[i]->name << ","; | |
7840 | s.op->line() << " .intrv=" << probes[i]->interval << ","; | |
7841 | s.op->line() << " .ms=" << probes[i]->time_is_msecs << ","; | |
7842 | s.op->line() << " .rnd=" << probes[i]->randomize; | |
7843 | s.op->line() << " },"; | |
7844 | } | |
7845 | s.op->newline(-1) << "};"; | |
7846 | s.op->newline(); | |
98afd80e | 7847 | |
b20febf3 FCE |
7848 | s.op->newline() << "static void enter_timer_probe (unsigned long val) {"; |
7849 | s.op->newline(1) << "struct stap_timer_probe* stp = & stap_timer_probes [val];"; | |
e0d86324 JS |
7850 | s.op->newline() << "if ((atomic_read (&session_state) == STAP_SESSION_STARTING) ||"; |
7851 | s.op->newline() << " (atomic_read (&session_state) == STAP_SESSION_RUNNING))"; | |
7852 | s.op->newline(1) << "mod_timer (& stp->timer_list, jiffies + "; | |
d006ef4f JS |
7853 | emit_interval (s.op); |
7854 | s.op->line() << ");"; | |
e0d86324 JS |
7855 | s.op->newline(-1) << "{"; |
7856 | s.op->indent(1); | |
c12d974f | 7857 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "stp->pp"); |
b20febf3 FCE |
7858 | s.op->newline() << "(*stp->ph) (c);"; |
7859 | common_probe_entryfn_epilogue (s.op); | |
7860 | s.op->newline(-1) << "}"; | |
e0d86324 | 7861 | s.op->newline(-1) << "}"; |
98afd80e FCE |
7862 | } |
7863 | ||
7864 | ||
7865 | void | |
b20febf3 | 7866 | timer_derived_probe_group::emit_module_init (systemtap_session& s) |
dc38c0ae | 7867 | { |
b20febf3 | 7868 | if (probes.empty()) return; |
dc38c0ae | 7869 | |
b20febf3 FCE |
7870 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
7871 | s.op->newline(1) << "struct stap_timer_probe* stp = & stap_timer_probes [i];"; | |
6f313a73 | 7872 | s.op->newline() << "probe_point = stp->pp;"; |
b20febf3 FCE |
7873 | s.op->newline() << "init_timer (& stp->timer_list);"; |
7874 | s.op->newline() << "stp->timer_list.function = & enter_timer_probe;"; | |
7875 | s.op->newline() << "stp->timer_list.data = i;"; // NB: important! | |
7876 | // copy timer renew calculations from above :-( | |
d006ef4f JS |
7877 | s.op->newline() << "stp->timer_list.expires = jiffies + "; |
7878 | emit_interval (s.op); | |
7879 | s.op->line() << ";"; | |
7880 | s.op->newline() << "add_timer (& stp->timer_list);"; | |
b20febf3 FCE |
7881 | // note: no partial failure rollback is needed: add_timer cannot fail. |
7882 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
7883 | } |
7884 | ||
7885 | ||
46b84a80 | 7886 | void |
b20febf3 | 7887 | timer_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 7888 | { |
b20febf3 | 7889 | if (probes.empty()) return; |
46b84a80 | 7890 | |
b20febf3 FCE |
7891 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; |
7892 | s.op->newline(1) << "del_timer_sync (& stap_timer_probes[i].timer_list);"; | |
7893 | s.op->indent(-1); | |
46b84a80 DS |
7894 | } |
7895 | ||
7896 | ||
b20febf3 | 7897 | |
39e57ce0 FCE |
7898 | // ------------------------------------------------------------------------ |
7899 | // profile derived probes | |
7900 | // ------------------------------------------------------------------------ | |
7901 | // On kernels < 2.6.10, this uses the register_profile_notifier API to | |
7902 | // generate the timed events for profiling; on kernels >= 2.6.10 this | |
7903 | // uses the register_timer_hook API. The latter doesn't currently allow | |
7904 | // simultaneous users, so insertion will fail if the profiler is busy. | |
7905 | // (Conflicting users may include OProfile, other SystemTap probes, etc.) | |
7906 | ||
7907 | ||
7908 | struct profile_derived_probe: public derived_probe | |
7909 | { | |
6dce2125 | 7910 | profile_derived_probe (systemtap_session &s, probe* p, probe_point* l); |
b20febf3 FCE |
7911 | void join_group (systemtap_session& s); |
7912 | }; | |
39e57ce0 | 7913 | |
dc38c0ae | 7914 | |
b20febf3 FCE |
7915 | struct profile_derived_probe_group: public generic_dpg<profile_derived_probe> |
7916 | { | |
7917 | public: | |
7918 | void emit_module_decls (systemtap_session& s); | |
7919 | void emit_module_init (systemtap_session& s); | |
7920 | void emit_module_exit (systemtap_session& s); | |
39e57ce0 FCE |
7921 | }; |
7922 | ||
7923 | ||
78f6bba6 | 7924 | profile_derived_probe::profile_derived_probe (systemtap_session &, probe* p, probe_point* l): |
6dce2125 | 7925 | derived_probe(p, l) |
4baf0e53 | 7926 | { |
39e57ce0 FCE |
7927 | } |
7928 | ||
7929 | ||
dc38c0ae | 7930 | void |
b20febf3 | 7931 | profile_derived_probe::join_group (systemtap_session& s) |
dc38c0ae | 7932 | { |
b20febf3 FCE |
7933 | if (! s.profile_derived_probes) |
7934 | s.profile_derived_probes = new profile_derived_probe_group (); | |
7935 | s.profile_derived_probes->enroll (this); | |
dc38c0ae DS |
7936 | } |
7937 | ||
7938 | ||
b20febf3 | 7939 | struct profile_builder: public derived_probe_builder |
39e57ce0 | 7940 | { |
b20febf3 FCE |
7941 | profile_builder() {} |
7942 | virtual void build(systemtap_session & sess, | |
7943 | probe * base, | |
7944 | probe_point * location, | |
86bf665e | 7945 | literal_map_t const &, |
b20febf3 FCE |
7946 | vector<derived_probe *> & finished_results) |
7947 | { | |
682f5024 | 7948 | sess.unwindsym_modules.insert ("kernel"); |
b20febf3 FCE |
7949 | finished_results.push_back(new profile_derived_probe(sess, base, location)); |
7950 | } | |
7951 | }; | |
46b84a80 | 7952 | |
39e57ce0 | 7953 | |
b20febf3 FCE |
7954 | // timer.profile probe handlers are hooked up in an entertaining way |
7955 | // to the underlying kernel facility. The fact that 2.6.11+ era | |
7956 | // "register_timer_hook" API allows only one consumer *system-wide* | |
7957 | // will give a hint. We will have a single entry function (and thus | |
7958 | // trivial registration / unregistration), and it will call all probe | |
7959 | // handler functions in sequence. | |
39e57ce0 | 7960 | |
6dce2125 | 7961 | void |
b20febf3 | 7962 | profile_derived_probe_group::emit_module_decls (systemtap_session& s) |
6dce2125 | 7963 | { |
b20febf3 | 7964 | if (probes.empty()) return; |
39e57ce0 | 7965 | |
b20febf3 FCE |
7966 | // kernels < 2.6.10: use register_profile_notifier API |
7967 | // kernels >= 2.6.10: use register_timer_hook API | |
7968 | s.op->newline() << "/* ---- profile probes ---- */"; | |
39e57ce0 | 7969 | |
b20febf3 FCE |
7970 | // This function calls all the profiling probe handlers in sequence. |
7971 | // The only tricky thing is that the context will be reused amongst | |
7972 | // them. While a simple sequence of calls to the individual probe | |
7973 | // handlers is unlikely to go terribly wrong (with c->last_error | |
7974 | // being set causing an early return), but for extra assurance, we | |
7975 | // open-code the same logic here. | |
39e57ce0 | 7976 | |
b20febf3 FCE |
7977 | s.op->newline() << "static void enter_all_profile_probes (struct pt_regs *regs) {"; |
7978 | s.op->indent(1); | |
c12d974f FCE |
7979 | string pp = lex_cast_qstring("timer.profile"); // hard-coded for convenience |
7980 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", pp); | |
29b0069b | 7981 | s.op->newline() << "c->regs = regs;"; |
39e57ce0 | 7982 | |
b20febf3 FCE |
7983 | for (unsigned i=0; i<probes.size(); i++) |
7984 | { | |
7985 | if (i > 0) | |
7986 | { | |
4baf0e53 | 7987 | // Some lightweight inter-probe context resetting |
6f313a73 | 7988 | // XXX: not quite right: MAXERRORS not respected |
29fdb4e4 | 7989 | s.op->newline() << "c->actionremaining = MAXACTION;"; |
b20febf3 FCE |
7990 | } |
7991 | s.op->newline() << "if (c->last_error == NULL) " << probes[i]->name << " (c);"; | |
7992 | } | |
7993 | common_probe_entryfn_epilogue (s.op); | |
7994 | s.op->newline(-1) << "}"; | |
7995 | ||
7996 | s.op->newline() << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)"; // == using_rpn of yore | |
7997 | ||
4c2732a1 | 7998 | s.op->newline() << "static int enter_profile_probes (struct notifier_block *self," |
b20febf3 FCE |
7999 | << " unsigned long val, void *data) {"; |
8000 | s.op->newline(1) << "(void) self; (void) val;"; | |
8001 | s.op->newline() << "enter_all_profile_probes ((struct pt_regs *) data);"; | |
8002 | s.op->newline() << "return 0;"; | |
8003 | s.op->newline(-1) << "}"; | |
8004 | s.op->newline() << "struct notifier_block stap_profile_notifier = {" | |
8005 | << " .notifier_call = & enter_profile_probes };"; | |
4baf0e53 | 8006 | |
b20febf3 | 8007 | s.op->newline() << "#else"; |
6dce2125 | 8008 | |
4c2732a1 | 8009 | s.op->newline() << "static int enter_profile_probes (struct pt_regs *regs) {"; |
b20febf3 FCE |
8010 | s.op->newline(1) << "enter_all_profile_probes (regs);"; |
8011 | s.op->newline() << "return 0;"; | |
8012 | s.op->newline(-1) << "}"; | |
39e57ce0 | 8013 | |
b20febf3 | 8014 | s.op->newline() << "#endif"; |
39e57ce0 FCE |
8015 | } |
8016 | ||
8017 | ||
dc38c0ae | 8018 | void |
b20febf3 | 8019 | profile_derived_probe_group::emit_module_init (systemtap_session& s) |
dc38c0ae | 8020 | { |
b20febf3 FCE |
8021 | if (probes.empty()) return; |
8022 | ||
6f313a73 | 8023 | s.op->newline() << "probe_point = \"timer.profile\";"; // NB: hard-coded for convenience |
b20febf3 FCE |
8024 | s.op->newline() << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)"; // == using_rpn of yore |
8025 | s.op->newline() << "rc = register_profile_notifier (& stap_profile_notifier);"; | |
8026 | s.op->newline() << "#else"; | |
8027 | s.op->newline() << "rc = register_timer_hook (& enter_profile_probes);"; | |
8028 | s.op->newline() << "#endif"; | |
dc38c0ae DS |
8029 | } |
8030 | ||
8031 | ||
46b84a80 | 8032 | void |
b20febf3 | 8033 | profile_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 8034 | { |
b20febf3 | 8035 | if (probes.empty()) return; |
46b84a80 | 8036 | |
b20febf3 FCE |
8037 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; |
8038 | s.op->newline(1) << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)"; // == using_rpn of yore | |
8039 | s.op->newline() << "unregister_profile_notifier (& stap_profile_notifier);"; | |
8040 | s.op->newline() << "#else"; | |
8041 | s.op->newline() << "unregister_timer_hook (& enter_profile_probes);"; | |
8042 | s.op->newline() << "#endif"; | |
8043 | s.op->indent(-1); | |
46b84a80 DS |
8044 | } |
8045 | ||
ce82316f | 8046 | |
342d3f96 | 8047 | |
604eef3b MH |
8048 | // ------------------------------------------------------------------------ |
8049 | // procfs file derived probes | |
8050 | // ------------------------------------------------------------------------ | |
8051 | ||
ce82316f | 8052 | |
12b21830 DS |
8053 | static string TOK_PROCFS("procfs"); |
8054 | static string TOK_READ("read"); | |
8055 | static string TOK_WRITE("write"); | |
8056 | ||
604eef3b MH |
8057 | struct procfs_derived_probe: public derived_probe |
8058 | { | |
8059 | string path; | |
8060 | bool write; | |
87ebf4cb DS |
8061 | bool target_symbol_seen; |
8062 | ||
604eef3b MH |
8063 | procfs_derived_probe (systemtap_session &, probe* p, probe_point* l, string ps, bool w); |
8064 | void join_group (systemtap_session& s); | |
8065 | }; | |
8066 | ||
8067 | ||
87ebf4cb DS |
8068 | struct procfs_probe_set |
8069 | { | |
8070 | procfs_derived_probe* read_probe; | |
8071 | procfs_derived_probe* write_probe; | |
8072 | ||
8073 | procfs_probe_set () : read_probe (NULL), write_probe (NULL) {} | |
8074 | }; | |
8075 | ||
8076 | ||
604eef3b MH |
8077 | struct procfs_derived_probe_group: public generic_dpg<procfs_derived_probe> |
8078 | { | |
87ebf4cb DS |
8079 | private: |
8080 | map<string, procfs_probe_set*> probes_by_path; | |
8081 | typedef map<string, procfs_probe_set*>::iterator p_b_p_iterator; | |
8082 | bool has_read_probes; | |
8083 | bool has_write_probes; | |
8084 | ||
604eef3b | 8085 | public: |
87ebf4cb DS |
8086 | procfs_derived_probe_group () : |
8087 | has_read_probes(false), has_write_probes(false) {} | |
8088 | ||
8089 | void enroll (procfs_derived_probe* probe); | |
604eef3b MH |
8090 | void emit_module_decls (systemtap_session& s); |
8091 | void emit_module_init (systemtap_session& s); | |
8092 | void emit_module_exit (systemtap_session& s); | |
8093 | }; | |
8094 | ||
8095 | ||
de688825 | 8096 | struct procfs_var_expanding_visitor: public var_expanding_visitor |
87ebf4cb | 8097 | { |
de688825 JS |
8098 | procfs_var_expanding_visitor(systemtap_session& s, const string& pn, |
8099 | string path, bool write_probe): | |
87ebf4cb DS |
8100 | sess (s), probe_name (pn), path (path), write_probe (write_probe), |
8101 | target_symbol_seen (false) {} | |
8102 | ||
8103 | systemtap_session& sess; | |
8104 | string probe_name; | |
8105 | string path; | |
8106 | bool write_probe; | |
8107 | bool target_symbol_seen; | |
8108 | ||
8109 | void visit_target_symbol (target_symbol* e); | |
8110 | }; | |
8111 | ||
8112 | ||
8113 | procfs_derived_probe::procfs_derived_probe (systemtap_session &s, probe* p, | |
8114 | probe_point* l, string ps, bool w): | |
8115 | derived_probe(p, l), path(ps), write(w), target_symbol_seen(false) | |
604eef3b | 8116 | { |
de688825 JS |
8117 | // Expand local variables in the probe body |
8118 | procfs_var_expanding_visitor v (s, name, path, write); | |
8119 | this->body = v.require (this->body); | |
87ebf4cb | 8120 | target_symbol_seen = v.target_symbol_seen; |
604eef3b MH |
8121 | } |
8122 | ||
8123 | ||
8124 | void | |
8125 | procfs_derived_probe::join_group (systemtap_session& s) | |
8126 | { | |
8127 | if (! s.procfs_derived_probes) | |
8128 | s.procfs_derived_probes = new procfs_derived_probe_group (); | |
8129 | s.procfs_derived_probes->enroll (this); | |
8130 | } | |
8131 | ||
ce82316f | 8132 | |
604eef3b | 8133 | void |
87ebf4cb | 8134 | procfs_derived_probe_group::enroll (procfs_derived_probe* p) |
604eef3b | 8135 | { |
87ebf4cb | 8136 | procfs_probe_set *pset; |
ce82316f | 8137 | |
87ebf4cb DS |
8138 | if (probes_by_path.count(p->path) == 0) |
8139 | { | |
8140 | pset = new procfs_probe_set; | |
8141 | probes_by_path[p->path] = pset; | |
8142 | } | |
8143 | else | |
8144 | { | |
8145 | pset = probes_by_path[p->path]; | |
8146 | ||
8147 | // You can only specify 1 read and 1 write probe. | |
8148 | if (p->write && pset->write_probe != NULL) | |
8149 | throw semantic_error("only one write procfs probe can exist for procfs path \"" + p->path + "\""); | |
8150 | else if (! p->write && pset->read_probe != NULL) | |
8151 | throw semantic_error("only one read procfs probe can exist for procfs path \"" + p->path + "\""); | |
5d23847d FCE |
8152 | |
8153 | // XXX: multiple writes should be acceptable | |
87ebf4cb DS |
8154 | } |
8155 | ||
8156 | if (p->write) | |
8157 | { | |
8158 | pset->write_probe = p; | |
8159 | has_write_probes = true; | |
8160 | } | |
8161 | else | |
8162 | { | |
8163 | pset->read_probe = p; | |
8164 | has_read_probes = true; | |
8165 | } | |
604eef3b MH |
8166 | } |
8167 | ||
ce82316f | 8168 | |
604eef3b | 8169 | void |
87ebf4cb | 8170 | procfs_derived_probe_group::emit_module_decls (systemtap_session& s) |
604eef3b | 8171 | { |
87ebf4cb | 8172 | if (probes_by_path.empty()) |
ce82316f | 8173 | return; |
604eef3b | 8174 | |
87ebf4cb | 8175 | s.op->newline() << "/* ---- procfs probes ---- */"; |
4bca766b | 8176 | s.op->newline() << "#include \"procfs.c\""; |
87ebf4cb DS |
8177 | |
8178 | // Emit the procfs probe data list | |
4c2732a1 | 8179 | s.op->newline() << "static struct stap_procfs_probe {"; |
87ebf4cb DS |
8180 | s.op->newline(1)<< "const char *path;"; |
8181 | s.op->newline() << "const char *read_pp;"; | |
8182 | s.op->newline() << "void (*read_ph) (struct context*);"; | |
8183 | s.op->newline() << "const char *write_pp;"; | |
8184 | s.op->newline() << "void (*write_ph) (struct context*);"; | |
8185 | s.op->newline(-1) << "} stap_procfs_probes[] = {"; | |
8186 | s.op->indent(1); | |
8187 | ||
8188 | for (p_b_p_iterator it = probes_by_path.begin(); it != probes_by_path.end(); | |
8189 | it++) | |
8190 | { | |
8191 | procfs_probe_set *pset = it->second; | |
8192 | ||
8193 | s.op->newline() << "{"; | |
8194 | s.op->line() << " .path=" << lex_cast_qstring (it->first) << ","; | |
8195 | ||
8196 | if (pset->read_probe != NULL) | |
8197 | { | |
8198 | s.op->line() << " .read_pp=" | |
8199 | << lex_cast_qstring (*pset->read_probe->sole_location()) | |
8200 | << ","; | |
8201 | s.op->line() << " .read_ph=&" << pset->read_probe->name << ","; | |
8202 | } | |
8203 | else | |
ce82316f | 8204 | { |
87ebf4cb DS |
8205 | s.op->line() << " .read_pp=NULL,"; |
8206 | s.op->line() << " .read_ph=NULL,"; | |
604eef3b | 8207 | } |
ce82316f | 8208 | |
87ebf4cb DS |
8209 | if (pset->write_probe != NULL) |
8210 | { | |
8211 | s.op->line() << " .write_pp=" | |
8212 | << lex_cast_qstring (*pset->write_probe->sole_location()) | |
8213 | << ","; | |
8214 | s.op->line() << " .write_ph=&" << pset->write_probe->name; | |
8215 | } | |
ce82316f | 8216 | else |
87ebf4cb DS |
8217 | { |
8218 | s.op->line() << " .write_pp=NULL,"; | |
8219 | s.op->line() << " .write_ph=NULL"; | |
8220 | } | |
8221 | s.op->line() << " },"; | |
8222 | } | |
8223 | s.op->newline(-1) << "};"; | |
8224 | ||
8225 | if (has_read_probes) | |
8226 | { | |
8227 | // Output routine to fill in 'page' with our data. | |
8228 | s.op->newline(); | |
8229 | ||
8230 | s.op->newline() << "static int _stp_procfs_read(char *page, char **start, off_t off, int count, int *eof, void *data) {"; | |
8231 | ||
8232 | s.op->newline(1) << "struct stap_procfs_probe *spp = (struct stap_procfs_probe *)data;"; | |
8233 | s.op->newline() << "int bytes = 0;"; | |
8234 | s.op->newline() << "string_t strdata = {'\\0'};"; | |
8235 | ||
c12d974f | 8236 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "spp->read_pp"); |
87ebf4cb DS |
8237 | |
8238 | s.op->newline() << "if (c->data == NULL)"; | |
8239 | s.op->newline(1) << "c->data = &strdata;"; | |
007f0f1c DS |
8240 | s.op->newline(-1) << "else {"; |
8241 | ||
8242 | s.op->newline(1) << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
8243 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
8244 | s.op->newline() << "_stp_exit ();"; | |
8245 | s.op->newline(-1) << "}"; | |
8246 | s.op->newline() << "atomic_dec (& c->busy);"; | |
8247 | s.op->newline() << "goto probe_epilogue;"; | |
8248 | s.op->newline(-1) << "}"; | |
87ebf4cb DS |
8249 | |
8250 | // call probe function (which copies data into strdata) | |
007f0f1c | 8251 | s.op->newline() << "(*spp->read_ph) (c);"; |
87ebf4cb DS |
8252 | |
8253 | // copy string data into 'page' | |
8254 | s.op->newline() << "c->data = NULL;"; | |
007f0f1c | 8255 | s.op->newline() << "bytes = strnlen(strdata, MAXSTRINGLEN - 1);"; |
87ebf4cb DS |
8256 | s.op->newline() << "if (off >= bytes)"; |
8257 | s.op->newline(1) << "*eof = 1;"; | |
8258 | s.op->newline(-1) << "else {"; | |
8259 | s.op->newline(1) << "bytes -= off;"; | |
8260 | s.op->newline() << "if (bytes > count)"; | |
8261 | s.op->newline(1) << "bytes = count;"; | |
8262 | s.op->newline(-1) << "memcpy(page, strdata + off, bytes);"; | |
8263 | s.op->newline() << "*start = page;"; | |
8264 | s.op->newline(-1) << "}"; | |
8265 | ||
87ebf4cb | 8266 | common_probe_entryfn_epilogue (s.op); |
87ebf4cb DS |
8267 | s.op->newline() << "return bytes;"; |
8268 | ||
8269 | s.op->newline(-1) << "}"; | |
8270 | } | |
8271 | if (has_write_probes) | |
8272 | { | |
8273 | s.op->newline() << "static int _stp_procfs_write(struct file *file, const char *buffer, unsigned long count, void *data) {"; | |
8274 | ||
8275 | s.op->newline(1) << "struct stap_procfs_probe *spp = (struct stap_procfs_probe *)data;"; | |
007f0f1c DS |
8276 | s.op->newline() << "string_t strdata = {'\\0'};"; |
8277 | ||
c12d974f | 8278 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "spp->write_pp"); |
87ebf4cb | 8279 | |
007f0f1c DS |
8280 | s.op->newline() << "if (count > (MAXSTRINGLEN - 1))"; |
8281 | s.op->newline(1) << "count = MAXSTRINGLEN - 1;"; | |
8282 | s.op->newline(-1) << "_stp_copy_from_user(strdata, buffer, count);"; | |
8283 | ||
8284 | s.op->newline() << "if (c->data == NULL)"; | |
8285 | s.op->newline(1) << "c->data = &strdata;"; | |
8286 | s.op->newline(-1) << "else {"; | |
8287 | ||
8288 | s.op->newline(1) << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
8289 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
8290 | s.op->newline() << "_stp_exit ();"; | |
8291 | s.op->newline(-1) << "}"; | |
8292 | s.op->newline() << "atomic_dec (& c->busy);"; | |
8293 | s.op->newline() << "goto probe_epilogue;"; | |
8294 | s.op->newline(-1) << "}"; | |
8295 | ||
8296 | // call probe function (which copies data out of strdata) | |
8297 | s.op->newline() << "(*spp->write_ph) (c);"; | |
8298 | ||
8299 | s.op->newline() << "c->data = NULL;"; | |
8300 | common_probe_entryfn_epilogue (s.op); | |
8301 | ||
8302 | s.op->newline() << "return count;"; | |
87ebf4cb | 8303 | s.op->newline(-1) << "}"; |
604eef3b | 8304 | } |
87ebf4cb DS |
8305 | } |
8306 | ||
8307 | ||
8308 | void | |
8309 | procfs_derived_probe_group::emit_module_init (systemtap_session& s) | |
8310 | { | |
8311 | if (probes_by_path.empty()) | |
8312 | return; | |
8313 | ||
8314 | s.op->newline() << "for (i = 0; i < " << probes_by_path.size() << "; i++) {"; | |
8315 | s.op->newline(1) << "struct stap_procfs_probe *spp = &stap_procfs_probes[i];"; | |
8316 | ||
8317 | s.op->newline() << "if (spp->read_pp)"; | |
8318 | s.op->newline(1) << "probe_point = spp->read_pp;"; | |
8319 | s.op->newline(-1) << "else"; | |
8320 | s.op->newline(1) << "probe_point = spp->write_pp;"; | |
8321 | ||
8322 | s.op->newline(-1) << "rc = _stp_create_procfs(spp->path, i);"; | |
8323 | ||
8324 | s.op->newline() << "if (rc) {"; | |
8325 | s.op->newline(1) << "_stp_close_procfs();"; | |
8326 | s.op->newline() << "break;"; | |
8327 | s.op->newline(-1) << "}"; | |
8328 | ||
4baf0e53 | 8329 | if (has_read_probes) |
bcf31db4 DS |
8330 | { |
8331 | s.op->newline() << "if (spp->read_pp)"; | |
8332 | s.op->newline(1) << "_stp_procfs_files[i]->read_proc = &_stp_procfs_read;"; | |
8333 | s.op->newline(-1) << "else"; | |
8334 | s.op->newline(1) << "_stp_procfs_files[i]->read_proc = NULL;"; | |
8335 | s.op->indent(-1); | |
8336 | } | |
8337 | else | |
8338 | s.op->newline() << "_stp_procfs_files[i]->read_proc = NULL;"; | |
87ebf4cb | 8339 | |
bcf31db4 DS |
8340 | if (has_write_probes) |
8341 | { | |
cf8dc3c7 | 8342 | s.op->newline() << "if (spp->write_pp)"; |
bcf31db4 DS |
8343 | s.op->newline(1) << "_stp_procfs_files[i]->write_proc = &_stp_procfs_write;"; |
8344 | s.op->newline(-1) << "else"; | |
8345 | s.op->newline(1) << "_stp_procfs_files[i]->write_proc = NULL;"; | |
8346 | s.op->indent(-1); | |
8347 | } | |
8348 | else | |
8349 | s.op->newline() << "_stp_procfs_files[i]->write_proc = NULL;"; | |
87ebf4cb | 8350 | |
bcf31db4 | 8351 | s.op->newline() << "_stp_procfs_files[i]->data = spp;"; |
87ebf4cb | 8352 | s.op->newline(-1) << "}"; // for loop |
604eef3b MH |
8353 | } |
8354 | ||
ce82316f | 8355 | |
604eef3b MH |
8356 | void |
8357 | procfs_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8358 | { | |
87ebf4cb DS |
8359 | if (probes_by_path.empty()) |
8360 | return; | |
8361 | ||
4baf0e53 | 8362 | s.op->newline() << "_stp_close_procfs();"; |
604eef3b MH |
8363 | } |
8364 | ||
ce82316f | 8365 | |
87ebf4cb | 8366 | void |
de688825 | 8367 | procfs_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
87ebf4cb | 8368 | { |
87ebf4cb DS |
8369 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); |
8370 | ||
8371 | if (e->base_name != "$value") | |
8372 | throw semantic_error ("invalid target symbol for procfs probe, $value expected", | |
8373 | e->tok); | |
8374 | ||
4ec2f7d0 DS |
8375 | if (e->components.size() > 0) |
8376 | { | |
8377 | switch (e->components[0].first) | |
8378 | { | |
8379 | case target_symbol::comp_literal_array_index: | |
8380 | throw semantic_error("procfs target variable '$value' may not be used as array", | |
8381 | e->tok); | |
8382 | break; | |
8383 | case target_symbol::comp_struct_member: | |
8384 | throw semantic_error("procfs target variable '$value' may not be used as a structure", | |
8385 | e->tok); | |
8386 | break; | |
8387 | default: | |
8388 | throw semantic_error ("invalid use of procfs target variable '$value'", | |
8389 | e->tok); | |
8390 | break; | |
8391 | } | |
8392 | } | |
8393 | ||
87ebf4cb DS |
8394 | bool lvalue = is_active_lvalue(e); |
8395 | if (write_probe && lvalue) | |
8396 | throw semantic_error("procfs $value variable is read-only in a procfs write probe", e->tok); | |
cf8dc3c7 DS |
8397 | else if (! write_probe && ! lvalue) |
8398 | throw semantic_error("procfs $value variable cannot be read in a procfs read probe", e->tok); | |
87ebf4cb DS |
8399 | |
8400 | // Remember that we've seen a target variable. | |
8401 | target_symbol_seen = true; | |
8402 | ||
8403 | // Synthesize a function. | |
8404 | functiondecl *fdecl = new functiondecl; | |
8405 | fdecl->tok = e->tok; | |
8406 | embeddedcode *ec = new embeddedcode; | |
8407 | ec->tok = e->tok; | |
8408 | ||
8409 | string fname = (string(lvalue ? "_procfs_value_set" : "_procfs_value_get") | |
8410 | + "_" + lex_cast<string>(tick++)); | |
8411 | string locvalue = "CONTEXT->data"; | |
8412 | ||
8413 | if (! lvalue) | |
007f0f1c DS |
8414 | ec->code = string("strlcpy (THIS->__retvalue, ") + locvalue |
8415 | + string(", MAXSTRINGLEN); /* pure */"); | |
87ebf4cb DS |
8416 | else |
8417 | ec->code = string("strlcpy (") + locvalue | |
8418 | + string(", THIS->value, MAXSTRINGLEN);"); | |
8419 | ||
8420 | fdecl->name = fname; | |
8421 | fdecl->body = ec; | |
8422 | fdecl->type = pe_string; | |
8423 | ||
8424 | if (lvalue) | |
8425 | { | |
8426 | // Modify the fdecl so it carries a single pe_string formal | |
8427 | // argument called "value". | |
8428 | ||
8429 | vardecl *v = new vardecl; | |
8430 | v->type = pe_string; | |
8431 | v->name = "value"; | |
8432 | v->tok = e->tok; | |
8433 | fdecl->formal_args.push_back(v); | |
8434 | } | |
f76427a2 | 8435 | sess.functions[fdecl->name]=fdecl; |
87ebf4cb DS |
8436 | |
8437 | // Synthesize a functioncall. | |
8438 | functioncall* n = new functioncall; | |
8439 | n->tok = e->tok; | |
8440 | n->function = fname; | |
8441 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
8442 | ||
8443 | if (lvalue) | |
8444 | { | |
8445 | // Provide the functioncall to our parent, so that it can be | |
8446 | // used to substitute for the assignment node immediately above | |
8447 | // us. | |
8448 | assert(!target_symbol_setter_functioncalls.empty()); | |
8449 | *(target_symbol_setter_functioncalls.top()) = n; | |
8450 | } | |
8451 | ||
4ed05b15 | 8452 | provide (n); |
87ebf4cb DS |
8453 | } |
8454 | ||
8455 | ||
604eef3b MH |
8456 | struct procfs_builder: public derived_probe_builder |
8457 | { | |
ce82316f | 8458 | procfs_builder() {} |
604eef3b MH |
8459 | virtual void build(systemtap_session & sess, |
8460 | probe * base, | |
8461 | probe_point * location, | |
86bf665e | 8462 | literal_map_t const & parameters, |
ce82316f | 8463 | vector<derived_probe *> & finished_results); |
604eef3b | 8464 | }; |
39e57ce0 | 8465 | |
ce82316f DS |
8466 | |
8467 | void | |
8468 | procfs_builder::build(systemtap_session & sess, | |
8469 | probe * base, | |
8470 | probe_point * location, | |
86bf665e | 8471 | literal_map_t const & parameters, |
ce82316f DS |
8472 | vector<derived_probe *> & finished_results) |
8473 | { | |
8474 | string path; | |
12b21830 DS |
8475 | bool has_procfs = get_param(parameters, TOK_PROCFS, path); |
8476 | bool has_read = (parameters.find(TOK_READ) != parameters.end()); | |
8477 | bool has_write = (parameters.find(TOK_WRITE) != parameters.end()); | |
ce82316f | 8478 | |
87ebf4cb DS |
8479 | // If no procfs path, default to "command". The runtime will do |
8480 | // this for us, but if we don't do it here, we'll think the | |
8481 | // following 2 probes are attached to different paths: | |
8482 | // | |
8483 | // probe procfs("command").read {}" | |
8484 | // probe procfs.write {} | |
4baf0e53 | 8485 | |
ce82316f DS |
8486 | if (! has_procfs) |
8487 | path = "command"; | |
227cacb9 DS |
8488 | // If we have a path, we need to validate it. |
8489 | else | |
8490 | { | |
8491 | string::size_type start_pos, end_pos; | |
8492 | string component; | |
8493 | start_pos = 0; | |
8494 | while ((end_pos = path.find('/', start_pos)) != string::npos) | |
8495 | { | |
8496 | // Make sure it doesn't start with '/'. | |
8497 | if (end_pos == 0) | |
8498 | throw semantic_error ("procfs path cannot start with a '/'", | |
8499 | location->tok); | |
8500 | ||
8501 | component = path.substr(start_pos, end_pos - start_pos); | |
8502 | // Make sure it isn't empty. | |
8503 | if (component.size() == 0) | |
8504 | throw semantic_error ("procfs path component cannot be empty", | |
8505 | location->tok); | |
8506 | // Make sure it isn't relative. | |
8507 | else if (component == "." || component == "..") | |
8508 | throw semantic_error ("procfs path cannot be relative (and contain '.' or '..')", location->tok); | |
8509 | ||
8510 | start_pos = end_pos + 1; | |
8511 | } | |
8512 | component = path.substr(start_pos); | |
8513 | // Make sure it doesn't end with '/'. | |
8514 | if (component.size() == 0) | |
8515 | throw semantic_error ("procfs path cannot end with a '/'", location->tok); | |
8516 | // Make sure it isn't relative. | |
8517 | else if (component == "." || component == "..") | |
8518 | throw semantic_error ("procfs path cannot be relative (and contain '.' or '..')", location->tok); | |
8519 | } | |
ce82316f DS |
8520 | |
8521 | if (!(has_read ^ has_write)) | |
8522 | throw semantic_error ("need read/write component", location->tok); | |
8523 | ||
8524 | finished_results.push_back(new procfs_derived_probe(sess, base, location, | |
8525 | path, has_write)); | |
8526 | } | |
8527 | ||
8528 | ||
342d3f96 | 8529 | |
30a279be FCE |
8530 | // ------------------------------------------------------------------------ |
8531 | // statically inserted macro-based derived probes | |
8532 | // ------------------------------------------------------------------------ | |
8533 | ||
12b21830 | 8534 | static string TOK_FORMAT("format"); |
30a279be | 8535 | |
2c384610 DS |
8536 | struct mark_arg |
8537 | { | |
8538 | bool str; | |
8539 | string c_type; | |
8540 | exp_type stp_type; | |
8541 | }; | |
8542 | ||
30a279be FCE |
8543 | struct mark_derived_probe: public derived_probe |
8544 | { | |
8545 | mark_derived_probe (systemtap_session &s, | |
eb973c2a | 8546 | const string& probe_name, const string& probe_format, |
5d23847d | 8547 | probe* base_probe, probe_point* location); |
30a279be | 8548 | |
35d4ab18 | 8549 | systemtap_session& sess; |
eb973c2a | 8550 | string probe_name, probe_format; |
2c384610 DS |
8551 | vector <struct mark_arg *> mark_args; |
8552 | bool target_symbol_seen; | |
30a279be | 8553 | |
b20febf3 | 8554 | void join_group (systemtap_session& s); |
35d4ab18 | 8555 | void emit_probe_context_vars (translator_output* o); |
2c384610 | 8556 | void initialize_probe_context_vars (translator_output* o); |
89f3a125 | 8557 | void printargs (std::ostream &o) const; |
2c384610 | 8558 | |
eb973c2a | 8559 | void parse_probe_format (); |
30a279be FCE |
8560 | }; |
8561 | ||
8562 | ||
b20febf3 FCE |
8563 | struct mark_derived_probe_group: public generic_dpg<mark_derived_probe> |
8564 | { | |
8565 | public: | |
2c384610 DS |
8566 | void emit_module_decls (systemtap_session& s); |
8567 | void emit_module_init (systemtap_session& s); | |
8568 | void emit_module_exit (systemtap_session& s); | |
b20febf3 FCE |
8569 | }; |
8570 | ||
8571 | ||
de688825 | 8572 | struct mark_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 8573 | { |
de688825 JS |
8574 | mark_var_expanding_visitor(systemtap_session& s, const string& pn, |
8575 | vector <struct mark_arg *> &mark_args): | |
2c384610 DS |
8576 | sess (s), probe_name (pn), mark_args (mark_args), |
8577 | target_symbol_seen (false) {} | |
35d4ab18 | 8578 | systemtap_session& sess; |
35d4ab18 | 8579 | string probe_name; |
2c384610 DS |
8580 | vector <struct mark_arg *> &mark_args; |
8581 | bool target_symbol_seen; | |
35d4ab18 FCE |
8582 | |
8583 | void visit_target_symbol (target_symbol* e); | |
0a63c36f | 8584 | void visit_target_symbol_arg (target_symbol* e); |
bc54e71c | 8585 | void visit_target_symbol_context (target_symbol* e); |
35d4ab18 FCE |
8586 | }; |
8587 | ||
8588 | ||
1969b5bc DS |
8589 | void |
8590 | hex_dump(unsigned char *data, size_t len) | |
2c384610 | 8591 | { |
1969b5bc DS |
8592 | // Dump data |
8593 | size_t idx = 0; | |
8594 | while (idx < len) | |
8595 | { | |
8596 | string char_rep; | |
8597 | ||
8598 | clog << " 0x" << setfill('0') << setw(8) << hex << internal << idx; | |
8599 | ||
8600 | for (int i = 0; i < 4; i++) | |
8601 | { | |
8602 | clog << " "; | |
8603 | size_t limit = idx + 4; | |
8604 | while (idx < len && idx < limit) | |
8605 | { | |
8606 | clog << setfill('0') << setw(2) | |
8607 | << ((unsigned) *((unsigned char *)data + idx)); | |
8608 | if (isprint(*((char *)data + idx))) | |
8609 | char_rep += *((char *)data + idx); | |
8610 | else | |
8611 | char_rep += '.'; | |
8612 | idx++; | |
8613 | } | |
8614 | while (idx < limit) | |
8615 | { | |
8616 | clog << " "; | |
8617 | idx++; | |
8618 | } | |
8619 | } | |
8620 | clog << " " << char_rep << dec << setfill(' ') << endl; | |
8621 | } | |
8622 | } | |
8623 | ||
2c384610 | 8624 | |
35d4ab18 | 8625 | void |
de688825 | 8626 | mark_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) |
35d4ab18 | 8627 | { |
35d4ab18 FCE |
8628 | string argnum_s = e->base_name.substr(4,e->base_name.length()-4); |
8629 | int argnum = atoi (argnum_s.c_str()); | |
2c384610 DS |
8630 | |
8631 | if (argnum < 1 || argnum > (int)mark_args.size()) | |
35d4ab18 FCE |
8632 | throw semantic_error ("invalid marker argument number", e->tok); |
8633 | ||
2c384610 DS |
8634 | if (is_active_lvalue (e)) |
8635 | throw semantic_error("write to marker parameter not permitted", e->tok); | |
8636 | ||
6f93ef37 DS |
8637 | if (e->components.size() > 0) |
8638 | { | |
8639 | switch (e->components[0].first) | |
8640 | { | |
8641 | case target_symbol::comp_literal_array_index: | |
8642 | throw semantic_error("marker argument may not be used as array", | |
8643 | e->tok); | |
8644 | break; | |
8645 | case target_symbol::comp_struct_member: | |
8646 | throw semantic_error("marker argument may not be used as a structure", | |
8647 | e->tok); | |
8648 | break; | |
8649 | default: | |
8650 | throw semantic_error ("invalid marker argument use", e->tok); | |
8651 | break; | |
8652 | } | |
8653 | } | |
4baf0e53 | 8654 | |
2c384610 DS |
8655 | // Remember that we've seen a target variable. |
8656 | target_symbol_seen = true; | |
35d4ab18 FCE |
8657 | |
8658 | // Synthesize a function. | |
8659 | functiondecl *fdecl = new functiondecl; | |
8660 | fdecl->tok = e->tok; | |
8661 | embeddedcode *ec = new embeddedcode; | |
8662 | ec->tok = e->tok; | |
35d4ab18 | 8663 | |
1b07c728 FCE |
8664 | string fname = string("_mark_tvar_get") |
8665 | + "_" + e->base_name.substr(1) | |
8666 | + "_" + lex_cast<string>(tick++); | |
35d4ab18 | 8667 | |
2c384610 DS |
8668 | if (mark_args[argnum-1]->stp_type == pe_long) |
8669 | ec->code = string("THIS->__retvalue = CONTEXT->locals[0].") | |
8670 | + probe_name + string(".__mark_arg") | |
8671 | + lex_cast<string>(argnum) + string (";"); | |
8672 | else | |
8673 | ec->code = string("strlcpy (THIS->__retvalue, CONTEXT->locals[0].") | |
8674 | + probe_name + string(".__mark_arg") | |
8675 | + lex_cast<string>(argnum) + string (", MAXSTRINGLEN);"); | |
1b07c728 | 8676 | ec->code += "/* pure */"; |
35d4ab18 FCE |
8677 | fdecl->name = fname; |
8678 | fdecl->body = ec; | |
2c384610 | 8679 | fdecl->type = mark_args[argnum-1]->stp_type; |
f76427a2 | 8680 | sess.functions[fdecl->name]=fdecl; |
35d4ab18 FCE |
8681 | |
8682 | // Synthesize a functioncall. | |
8683 | functioncall* n = new functioncall; | |
8684 | n->tok = e->tok; | |
8685 | n->function = fname; | |
8686 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
4ed05b15 | 8687 | provide (n); |
35d4ab18 FCE |
8688 | } |
8689 | ||
8690 | ||
0a63c36f | 8691 | void |
de688825 | 8692 | mark_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
0a63c36f | 8693 | { |
bc54e71c | 8694 | string sname = e->base_name; |
0a63c36f DS |
8695 | |
8696 | if (is_active_lvalue (e)) | |
bc54e71c | 8697 | throw semantic_error("write to marker '" + sname + "' not permitted", e->tok); |
0a63c36f DS |
8698 | |
8699 | if (e->components.size() > 0) | |
8700 | { | |
8701 | switch (e->components[0].first) | |
8702 | { | |
8703 | case target_symbol::comp_literal_array_index: | |
bc54e71c | 8704 | throw semantic_error("marker '" + sname + "' may not be used as array", |
0a63c36f DS |
8705 | e->tok); |
8706 | break; | |
8707 | case target_symbol::comp_struct_member: | |
bc54e71c | 8708 | throw semantic_error("marker '" + sname + "' may not be used as a structure", |
0a63c36f DS |
8709 | e->tok); |
8710 | break; | |
8711 | default: | |
bc54e71c | 8712 | throw semantic_error ("invalid marker '" + sname + "' use", e->tok); |
0a63c36f DS |
8713 | break; |
8714 | } | |
8715 | } | |
4baf0e53 | 8716 | |
bc54e71c MH |
8717 | string fname; |
8718 | if (e->base_name == "$format") { | |
8719 | fname = string("_mark_format_get"); | |
8720 | } else { | |
8721 | fname = string("_mark_name_get"); | |
8722 | } | |
0a63c36f DS |
8723 | |
8724 | // Synthesize a functioncall. | |
8725 | functioncall* n = new functioncall; | |
8726 | n->tok = e->tok; | |
8727 | n->function = fname; | |
8728 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
4ed05b15 | 8729 | provide (n); |
0a63c36f DS |
8730 | } |
8731 | ||
8732 | void | |
de688825 | 8733 | mark_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a63c36f DS |
8734 | { |
8735 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
8736 | ||
8737 | if (e->base_name.substr(0,4) == "$arg") | |
8738 | visit_target_symbol_arg (e); | |
bc54e71c MH |
8739 | else if (e->base_name == "$format" || e->base_name == "$name") |
8740 | visit_target_symbol_context (e); | |
0a63c36f | 8741 | else |
bc54e71c | 8742 | throw semantic_error ("invalid target symbol for marker, $argN, $name or $format expected", |
0a63c36f DS |
8743 | e->tok); |
8744 | } | |
8745 | ||
8746 | ||
35d4ab18 | 8747 | |
30a279be | 8748 | mark_derived_probe::mark_derived_probe (systemtap_session &s, |
35d4ab18 | 8749 | const string& p_n, |
eb973c2a | 8750 | const string& p_f, |
5d23847d | 8751 | probe* base, probe_point* loc): |
a10fa7f5 | 8752 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), |
eb973c2a | 8753 | sess (s), probe_name (p_n), probe_format (p_f), |
f781f849 | 8754 | target_symbol_seen (false) |
30a279be | 8755 | { |
5d23847d FCE |
8756 | // create synthetic probe point name; preserve condition |
8757 | vector<probe_point::component*> comps; | |
12b21830 DS |
8758 | comps.push_back (new probe_point::component (TOK_KERNEL)); |
8759 | comps.push_back (new probe_point::component (TOK_MARK, new literal_string (probe_name))); | |
8760 | comps.push_back (new probe_point::component (TOK_FORMAT, new literal_string (probe_format))); | |
5d23847d | 8761 | this->sole_location()->components = comps; |
cbbe8080 | 8762 | |
eb973c2a DS |
8763 | // expand the marker format |
8764 | parse_probe_format(); | |
35d4ab18 | 8765 | |
de688825 JS |
8766 | // Now expand the local variables in the probe body |
8767 | mark_var_expanding_visitor v (sess, name, mark_args); | |
8768 | this->body = v.require (this->body); | |
2c384610 | 8769 | target_symbol_seen = v.target_symbol_seen; |
35d4ab18 | 8770 | |
1969b5bc | 8771 | if (sess.verbose > 2) |
eb973c2a DS |
8772 | clog << "marker-based " << name << " mark=" << probe_name |
8773 | << " fmt='" << probe_format << "'" << endl; | |
30a279be FCE |
8774 | } |
8775 | ||
8776 | ||
2c384610 DS |
8777 | static int |
8778 | skip_atoi(const char **s) | |
dc38c0ae | 8779 | { |
2c384610 DS |
8780 | int i = 0; |
8781 | while (isdigit(**s)) | |
8782 | i = i * 10 + *((*s)++) - '0'; | |
8783 | return i; | |
dc38c0ae DS |
8784 | } |
8785 | ||
8786 | ||
30a279be | 8787 | void |
eb973c2a | 8788 | mark_derived_probe::parse_probe_format() |
35d4ab18 | 8789 | { |
eb973c2a | 8790 | const char *fmt = probe_format.c_str(); |
2c384610 DS |
8791 | int qualifier; // 'h', 'l', or 'L' for integer fields |
8792 | mark_arg *arg; | |
8793 | ||
8794 | for (; *fmt ; ++fmt) | |
35d4ab18 | 8795 | { |
2c384610 | 8796 | if (*fmt != '%') |
35d4ab18 | 8797 | { |
2c384610 DS |
8798 | /* Skip text */ |
8799 | continue; | |
8800 | } | |
35d4ab18 | 8801 | |
2c384610 DS |
8802 | repeat: |
8803 | ++fmt; | |
35d4ab18 | 8804 | |
2c384610 DS |
8805 | // skip conversion flags (if present) |
8806 | switch (*fmt) | |
8807 | { | |
8808 | case '-': | |
8809 | case '+': | |
8810 | case ' ': | |
8811 | case '#': | |
8812 | case '0': | |
8813 | goto repeat; | |
8814 | } | |
30a279be | 8815 | |
2c384610 DS |
8816 | // skip minimum field witdh (if present) |
8817 | if (isdigit(*fmt)) | |
8818 | skip_atoi(&fmt); | |
30a279be | 8819 | |
2c384610 DS |
8820 | // skip precision (if present) |
8821 | if (*fmt == '.') | |
35d4ab18 | 8822 | { |
2c384610 DS |
8823 | ++fmt; |
8824 | if (isdigit(*fmt)) | |
8825 | skip_atoi(&fmt); | |
8826 | } | |
30a279be | 8827 | |
2c384610 DS |
8828 | // get the conversion qualifier (if present) |
8829 | qualifier = -1; | |
8830 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') | |
8831 | { | |
8832 | qualifier = *fmt; | |
8833 | ++fmt; | |
8834 | if (qualifier == 'l' && *fmt == 'l') | |
8835 | { | |
8836 | qualifier = 'L'; | |
8837 | ++fmt; | |
8838 | } | |
8839 | } | |
30a279be | 8840 | |
2c384610 DS |
8841 | // get the conversion type |
8842 | switch (*fmt) | |
8843 | { | |
8844 | case 'c': | |
8845 | arg = new mark_arg; | |
8846 | arg->str = false; | |
8847 | arg->c_type = "int"; | |
8848 | arg->stp_type = pe_long; | |
8849 | mark_args.push_back(arg); | |
8850 | continue; | |
8851 | ||
8852 | case 's': | |
8853 | arg = new mark_arg; | |
8854 | arg->str = true; | |
8855 | arg->c_type = "char *"; | |
8856 | arg->stp_type = pe_string; | |
8857 | mark_args.push_back(arg); | |
8858 | continue; | |
8859 | ||
8860 | case 'p': | |
8861 | arg = new mark_arg; | |
8862 | arg->str = false; | |
38c963a9 DS |
8863 | // This should really be 'void *'. But, then we'll get a |
8864 | // compile error when we assign the void pointer to an | |
8865 | // integer without a cast. So, we use 'long' instead, since | |
8866 | // it should have the same size as 'void *'. | |
8867 | arg->c_type = "long"; | |
2c384610 DS |
8868 | arg->stp_type = pe_long; |
8869 | mark_args.push_back(arg); | |
8870 | continue; | |
8871 | ||
8872 | case '%': | |
8873 | continue; | |
8874 | ||
8875 | case 'o': | |
8876 | case 'X': | |
8877 | case 'x': | |
8878 | case 'd': | |
8879 | case 'i': | |
8880 | case 'u': | |
8881 | // fall through... | |
8882 | break; | |
30a279be | 8883 | |
2c384610 DS |
8884 | default: |
8885 | if (!*fmt) | |
8886 | --fmt; | |
8887 | continue; | |
8888 | } | |
30a279be | 8889 | |
2c384610 DS |
8890 | arg = new mark_arg; |
8891 | arg->str = false; | |
8892 | arg->stp_type = pe_long; | |
8893 | switch (qualifier) | |
8894 | { | |
8895 | case 'L': | |
8896 | arg->c_type = "long long"; | |
8897 | break; | |
30a279be | 8898 | |
2c384610 DS |
8899 | case 'l': |
8900 | arg->c_type = "long"; | |
8901 | break; | |
e38d6504 | 8902 | |
2c384610 DS |
8903 | case 'h': |
8904 | arg->c_type = "short"; | |
8905 | break; | |
46b84a80 | 8906 | |
2c384610 DS |
8907 | default: |
8908 | arg->c_type = "int"; | |
8909 | break; | |
8910 | } | |
8911 | mark_args.push_back(arg); | |
8912 | } | |
46b84a80 DS |
8913 | } |
8914 | ||
f781f849 | 8915 | |
46b84a80 | 8916 | void |
2c384610 | 8917 | mark_derived_probe::join_group (systemtap_session& s) |
46b84a80 | 8918 | { |
2c384610 | 8919 | if (! s.mark_derived_probes) |
775d51e5 DS |
8920 | { |
8921 | s.mark_derived_probes = new mark_derived_probe_group (); | |
8922 | ||
8923 | // Make sure <linux/marker.h> is included early. | |
8924 | embeddedcode *ec = new embeddedcode; | |
8925 | ec->tok = NULL; | |
8926 | ec->code = string("#if ! defined(CONFIG_MARKERS)\n") | |
8927 | + string("#error \"Need CONFIG_MARKERS!\"\n") | |
8928 | + string("#endif\n") | |
8929 | + string("#include <linux/marker.h>\n"); | |
8930 | ||
8931 | s.embeds.push_back(ec); | |
8932 | } | |
2c384610 | 8933 | s.mark_derived_probes->enroll (this); |
30a279be FCE |
8934 | } |
8935 | ||
35d4ab18 | 8936 | |
30a279be | 8937 | void |
2c384610 | 8938 | mark_derived_probe::emit_probe_context_vars (translator_output* o) |
30a279be | 8939 | { |
2c384610 DS |
8940 | // If we haven't seen a target symbol for this probe, quit. |
8941 | if (! target_symbol_seen) | |
8942 | return; | |
30a279be | 8943 | |
2c384610 DS |
8944 | for (unsigned i = 0; i < mark_args.size(); i++) |
8945 | { | |
8946 | string localname = "__mark_arg" + lex_cast<string>(i+1); | |
8947 | switch (mark_args[i]->stp_type) | |
8948 | { | |
8949 | case pe_long: | |
8950 | o->newline() << "int64_t " << localname << ";"; | |
8951 | break; | |
8952 | case pe_string: | |
8953 | o->newline() << "string_t " << localname << ";"; | |
8954 | break; | |
8955 | default: | |
8956 | throw semantic_error ("cannot expand unknown type"); | |
8957 | break; | |
8958 | } | |
8959 | } | |
30a279be FCE |
8960 | } |
8961 | ||
8962 | ||
dc38c0ae | 8963 | void |
2c384610 | 8964 | mark_derived_probe::initialize_probe_context_vars (translator_output* o) |
dc38c0ae | 8965 | { |
2c384610 DS |
8966 | // If we haven't seen a target symbol for this probe, quit. |
8967 | if (! target_symbol_seen) | |
8968 | return; | |
8969 | ||
d09fee57 | 8970 | bool deref_fault_needed = false; |
2c384610 | 8971 | for (unsigned i = 0; i < mark_args.size(); i++) |
dc38c0ae | 8972 | { |
2c384610 DS |
8973 | string localname = "l->__mark_arg" + lex_cast<string>(i+1); |
8974 | switch (mark_args[i]->stp_type) | |
8975 | { | |
8976 | case pe_long: | |
3b0c565c | 8977 | o->newline() << localname << " = va_arg(*c->mark_va_list, " |
2c384610 DS |
8978 | << mark_args[i]->c_type << ");"; |
8979 | break; | |
8980 | ||
8981 | case pe_string: | |
8982 | // We're assuming that this is a kernel string (this code is | |
8983 | // basically the guts of kernel_string), not a user string. | |
8984 | o->newline() << "{ " << mark_args[i]->c_type | |
3b0c565c | 8985 | << " tmp_str = va_arg(*c->mark_va_list, " |
2c384610 DS |
8986 | << mark_args[i]->c_type << ");"; |
8987 | o->newline() << "deref_string (" << localname | |
d09fee57 DS |
8988 | << ", tmp_str, MAXSTRINGLEN); }"; |
8989 | deref_fault_needed = true; | |
2c384610 DS |
8990 | break; |
8991 | ||
8992 | default: | |
8993 | throw semantic_error ("cannot expand unknown type"); | |
8994 | break; | |
8995 | } | |
dc38c0ae | 8996 | } |
d09fee57 DS |
8997 | if (deref_fault_needed) |
8998 | // Need to report errors? | |
8999 | o->newline() << "deref_fault: ;"; | |
dc38c0ae DS |
9000 | } |
9001 | ||
89f3a125 WH |
9002 | void |
9003 | mark_derived_probe::printargs(std::ostream &o) const | |
9004 | { | |
9005 | for (unsigned i = 0; i < mark_args.size(); i++) | |
9006 | { | |
9007 | string localname = "$arg" + lex_cast<string>(i+1); | |
9008 | switch (mark_args[i]->stp_type) | |
9009 | { | |
9010 | case pe_long: | |
9011 | o << " " << localname << ":long"; | |
9012 | break; | |
9013 | case pe_string: | |
9014 | o << " " << localname << ":string"; | |
9015 | break; | |
9016 | default: | |
9017 | o << " " << localname << ":unknown"; | |
9018 | break; | |
9019 | } | |
9020 | } | |
9021 | } | |
9022 | ||
30a279be | 9023 | |
342d3f96 DS |
9024 | void |
9025 | mark_derived_probe_group::emit_module_decls (systemtap_session& s) | |
9026 | { | |
9027 | if (probes.empty()) | |
9028 | return; | |
9029 | ||
9030 | s.op->newline() << "/* ---- marker probes ---- */"; | |
46b84a80 | 9031 | |
4c2732a1 | 9032 | s.op->newline() << "static struct stap_marker_probe {"; |
0a63c36f DS |
9033 | s.op->newline(1) << "const char * const name;"; |
9034 | s.op->newline() << "const char * const format;"; | |
9035 | s.op->newline() << "const char * const pp;"; | |
9036 | s.op->newline() << "void (* const ph) (struct context *);"; | |
46b84a80 | 9037 | |
2c384610 DS |
9038 | s.op->newline(-1) << "} stap_marker_probes [" << probes.size() << "] = {"; |
9039 | s.op->indent(1); | |
9040 | for (unsigned i=0; i < probes.size(); i++) | |
46b84a80 | 9041 | { |
4baf0e53 | 9042 | s.op->newline () << "{"; |
2c384610 DS |
9043 | s.op->line() << " .name=" << lex_cast_qstring(probes[i]->probe_name) |
9044 | << ","; | |
eb973c2a | 9045 | s.op->line() << " .format=" << lex_cast_qstring(probes[i]->probe_format) |
2c384610 DS |
9046 | << ","; |
9047 | s.op->line() << " .pp=" << lex_cast_qstring (*probes[i]->sole_location()) | |
9048 | << ","; | |
9049 | s.op->line() << " .ph=&" << probes[i]->name; | |
9050 | s.op->line() << " },"; | |
46b84a80 | 9051 | } |
2c384610 DS |
9052 | s.op->newline(-1) << "};"; |
9053 | s.op->newline(); | |
4baf0e53 | 9054 | |
2c384610 DS |
9055 | |
9056 | // Emit the marker callback function | |
9057 | s.op->newline(); | |
3b0c565c DS |
9058 | s.op->newline() << "static void enter_marker_probe (void *probe_data, void *call_data, const char *fmt, va_list *args) {"; |
9059 | s.op->newline(1) << "struct stap_marker_probe *smp = (struct stap_marker_probe *)probe_data;"; | |
c12d974f | 9060 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "smp->pp"); |
bc54e71c MH |
9061 | s.op->newline() << "c->marker_name = smp->name;"; |
9062 | s.op->newline() << "c->marker_format = smp->format;"; | |
3b0c565c | 9063 | s.op->newline() << "c->mark_va_list = args;"; |
2c384610 | 9064 | s.op->newline() << "(*smp->ph) (c);"; |
3b0c565c | 9065 | s.op->newline() << "c->mark_va_list = NULL;"; |
0a63c36f DS |
9066 | s.op->newline() << "c->data = NULL;"; |
9067 | ||
2c384610 DS |
9068 | common_probe_entryfn_epilogue (s.op); |
9069 | s.op->newline(-1) << "}"; | |
46b84a80 | 9070 | |
2c384610 | 9071 | return; |
46b84a80 DS |
9072 | } |
9073 | ||
9074 | ||
2c384610 DS |
9075 | void |
9076 | mark_derived_probe_group::emit_module_init (systemtap_session &s) | |
30a279be | 9077 | { |
2c384610 DS |
9078 | if (probes.size () == 0) |
9079 | return; | |
30a279be | 9080 | |
2c384610 DS |
9081 | s.op->newline() << "/* init marker probes */"; |
9082 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
9083 | s.op->newline(1) << "struct stap_marker_probe *smp = &stap_marker_probes[i];"; | |
9084 | s.op->newline() << "probe_point = smp->pp;"; | |
38c963a9 | 9085 | s.op->newline() << "rc = marker_probe_register(smp->name, smp->format, enter_marker_probe, smp);"; |
3b0c565c | 9086 | s.op->newline() << "if (rc) {"; |
2c384610 DS |
9087 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
9088 | s.op->newline(1) << "struct stap_marker_probe *smp2 = &stap_marker_probes[j];"; | |
3b0c565c | 9089 | s.op->newline() << "marker_probe_unregister(smp2->name, enter_marker_probe, smp2);"; |
2c384610 DS |
9090 | s.op->newline(-1) << "}"; |
9091 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
9092 | s.op->newline(-1) << "}"; | |
9093 | s.op->newline(-1) << "}"; // for loop | |
9094 | } | |
9095 | ||
9096 | ||
9097 | void | |
9098 | mark_derived_probe_group::emit_module_exit (systemtap_session& s) | |
9099 | { | |
9100 | if (probes.empty()) | |
9101 | return; | |
9102 | ||
9103 | s.op->newline() << "/* deregister marker probes */"; | |
9104 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
9105 | s.op->newline(1) << "struct stap_marker_probe *smp = &stap_marker_probes[i];"; | |
3b0c565c | 9106 | s.op->newline() << "marker_probe_unregister(smp->name, enter_marker_probe, smp);"; |
2c384610 DS |
9107 | s.op->newline(-1) << "}"; // for loop |
9108 | } | |
30a279be FCE |
9109 | |
9110 | ||
9111 | struct mark_builder: public derived_probe_builder | |
9112 | { | |
9113 | private: | |
f781f849 | 9114 | bool cache_initialized; |
eb973c2a DS |
9115 | typedef multimap<string, string> mark_cache_t; |
9116 | typedef multimap<string, string>::const_iterator mark_cache_const_iterator_t; | |
9117 | typedef pair<mark_cache_const_iterator_t, mark_cache_const_iterator_t> | |
9118 | mark_cache_const_iterator_pair_t; | |
9119 | mark_cache_t mark_cache; | |
30a279be FCE |
9120 | |
9121 | public: | |
f781f849 | 9122 | mark_builder(): cache_initialized(false) {} |
2c384610 DS |
9123 | |
9124 | void build_no_more (systemtap_session &s) | |
9125 | { | |
f781f849 | 9126 | if (! mark_cache.empty()) |
2c384610 DS |
9127 | { |
9128 | if (s.verbose > 3) | |
f781f849 DS |
9129 | clog << "mark_builder releasing cache" << endl; |
9130 | mark_cache.clear(); | |
2c384610 DS |
9131 | } |
9132 | } | |
9133 | ||
30a279be FCE |
9134 | void build(systemtap_session & sess, |
9135 | probe * base, | |
9136 | probe_point * location, | |
86bf665e | 9137 | literal_map_t const & parameters, |
30a279be FCE |
9138 | vector<derived_probe *> & finished_results); |
9139 | }; | |
9140 | ||
9141 | ||
30a279be FCE |
9142 | void |
9143 | mark_builder::build(systemtap_session & sess, | |
2c384610 | 9144 | probe * base, |
cbbe8080 | 9145 | probe_point *loc, |
86bf665e | 9146 | literal_map_t const & parameters, |
2c384610 | 9147 | vector<derived_probe *> & finished_results) |
30a279be | 9148 | { |
f781f849 | 9149 | string mark_str_val; |
12b21830 | 9150 | bool has_mark_str = get_param (parameters, TOK_MARK, mark_str_val); |
eb973c2a | 9151 | string mark_format_val; |
12b21830 | 9152 | bool has_mark_format = get_param (parameters, TOK_FORMAT, mark_format_val); |
f781f849 | 9153 | assert (has_mark_str); |
ced347a9 | 9154 | (void) has_mark_str; |
30a279be | 9155 | |
f781f849 DS |
9156 | if (! cache_initialized) |
9157 | { | |
9158 | cache_initialized = true; | |
b5e66ada FCE |
9159 | string module_markers_path = sess.kernel_build_tree + "/Module.markers"; |
9160 | ||
f781f849 DS |
9161 | ifstream module_markers; |
9162 | module_markers.open(module_markers_path.c_str(), ifstream::in); | |
9163 | if (! module_markers) | |
9164 | { | |
9165 | if (sess.verbose>3) | |
9166 | clog << module_markers_path << " cannot be opened: " | |
9167 | << strerror(errno) << endl; | |
9168 | return; | |
9169 | } | |
30a279be | 9170 | |
f781f849 DS |
9171 | string name, module, format; |
9172 | do | |
9173 | { | |
9174 | module_markers >> name >> module; | |
9175 | getline(module_markers, format); | |
9176 | ||
9177 | // trim leading whitespace | |
9178 | string::size_type notwhite = format.find_first_not_of(" \t"); | |
9179 | format.erase(0, notwhite); | |
9180 | ||
a53c9645 DS |
9181 | // If the format is empty, make sure we add back a space |
9182 | // character, which is what MARK_NOARGS expands to. | |
9183 | if (format.length() == 0) | |
9184 | format = " "; | |
9185 | ||
f781f849 DS |
9186 | if (sess.verbose>3) |
9187 | clog << "'" << name << "' '" << module << "' '" << format | |
9188 | << "'" << endl; | |
4baf0e53 | 9189 | |
eb973c2a DS |
9190 | if (mark_cache.count(name) > 0) |
9191 | { | |
9192 | // If we have 2 markers with the same we've got 2 cases: | |
9193 | // different format strings or duplicate format strings. | |
9194 | // If an existing marker in the cache doesn't have the | |
9195 | // same format string, add this marker. | |
9196 | mark_cache_const_iterator_pair_t ret; | |
9197 | mark_cache_const_iterator_t it; | |
9198 | bool matching_format_string = false; | |
41c262f3 | 9199 | |
eb973c2a DS |
9200 | ret = mark_cache.equal_range(name); |
9201 | for (it = ret.first; it != ret.second; ++it) | |
9202 | { | |
9203 | if (format == it->second) | |
9204 | { | |
9205 | matching_format_string = true; | |
9206 | break; | |
9207 | } | |
9208 | } | |
9209 | ||
9210 | if (! matching_format_string) | |
9211 | mark_cache.insert(pair<string,string>(name, format)); | |
9212 | } | |
9213 | else | |
9214 | mark_cache.insert(pair<string,string>(name, format)); | |
f781f849 DS |
9215 | } |
9216 | while (! module_markers.eof()); | |
9217 | module_markers.close(); | |
30a279be FCE |
9218 | } |
9219 | ||
f781f849 | 9220 | // Search marker list for matching markers |
eb973c2a | 9221 | for (mark_cache_const_iterator_t it = mark_cache.begin(); |
f781f849 DS |
9222 | it != mark_cache.end(); it++) |
9223 | { | |
9224 | // Below, "rc" has negative polarity: zero iff matching. | |
9225 | int rc = fnmatch(mark_str_val.c_str(), it->first.c_str(), 0); | |
9226 | if (! rc) | |
9227 | { | |
eb973c2a DS |
9228 | bool add_result = true; |
9229 | ||
9230 | // Match format strings (if the user specified one) | |
9231 | if (has_mark_format && fnmatch(mark_format_val.c_str(), | |
9232 | it->second.c_str(), 0)) | |
9233 | add_result = false; | |
9234 | ||
9235 | if (add_result) | |
9236 | { | |
9237 | derived_probe *dp | |
9238 | = new mark_derived_probe (sess, | |
9239 | it->first, it->second, | |
9240 | base, loc); | |
9241 | finished_results.push_back (dp); | |
9242 | } | |
f781f849 DS |
9243 | } |
9244 | } | |
30a279be FCE |
9245 | } |
9246 | ||
9247 | ||
342d3f96 | 9248 | |
0a6f5a3f JS |
9249 | // ------------------------------------------------------------------------ |
9250 | // statically inserted kernel-tracepoint derived probes | |
9251 | // ------------------------------------------------------------------------ | |
9252 | ||
6fb70fb7 JS |
9253 | struct tracepoint_arg |
9254 | { | |
ad370dcc | 9255 | string name, c_type, typecast; |
dcaa1a65 | 9256 | bool usable, used, isptr; |
f8a968bc | 9257 | Dwarf_Die type_die; |
dcaa1a65 | 9258 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 9259 | }; |
0a6f5a3f JS |
9260 | |
9261 | struct tracepoint_derived_probe: public derived_probe | |
9262 | { | |
79189b84 JS |
9263 | tracepoint_derived_probe (systemtap_session& s, |
9264 | dwflpp& dw, Dwarf_Die& func_die, | |
9265 | const string& tracepoint_name, | |
9266 | probe* base_probe, probe_point* location); | |
9267 | ||
9268 | systemtap_session& sess; | |
6fb70fb7 JS |
9269 | string tracepoint_name, header; |
9270 | vector <struct tracepoint_arg> args; | |
79189b84 | 9271 | |
6fb70fb7 | 9272 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
e2086848 | 9273 | void printargs (std::ostream &o) const; |
79189b84 | 9274 | void join_group (systemtap_session& s); |
f8a968bc | 9275 | void emit_probe_context_vars (translator_output* o); |
0a6f5a3f JS |
9276 | }; |
9277 | ||
9278 | ||
9279 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> | |
9280 | { | |
79189b84 JS |
9281 | void emit_module_decls (systemtap_session& s); |
9282 | void emit_module_init (systemtap_session& s); | |
9283 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f JS |
9284 | }; |
9285 | ||
9286 | ||
f8a968bc JS |
9287 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
9288 | { | |
9289 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
9290 | vector <struct tracepoint_arg>& args): | |
9291 | dw (dw), probe_name (probe_name), args (args) {} | |
9292 | dwflpp& dw; | |
9293 | const string& probe_name; | |
9294 | vector <struct tracepoint_arg>& args; | |
9295 | ||
9296 | void visit_target_symbol (target_symbol* e); | |
9297 | void visit_target_symbol_arg (target_symbol* e); | |
9298 | void visit_target_symbol_context (target_symbol* e); | |
9299 | }; | |
9300 | ||
9301 | ||
9302 | void | |
9303 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
9304 | { | |
9305 | string argname = e->base_name.substr(1); | |
9306 | ||
9307 | // search for a tracepoint parameter matching this name | |
9308 | tracepoint_arg *arg = NULL; | |
9309 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 9310 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
9311 | { |
9312 | arg = &args[i]; | |
9313 | arg->used = true; | |
9314 | break; | |
9315 | } | |
9316 | ||
9317 | if (arg == NULL) | |
9318 | { | |
9319 | stringstream alternatives; | |
9320 | for (unsigned i = 0; i < args.size(); ++i) | |
9321 | alternatives << " $" << args[i].name; | |
046e7190 | 9322 | alternatives << " $$name $$parms $$vars"; |
f8a968bc JS |
9323 | |
9324 | // We hope that this value ends up not being referenced after all, so it | |
9325 | // can be optimized out quietly. | |
9326 | semantic_error* saveme = | |
9327 | new semantic_error("unable to find tracepoint variable '" + e->base_name | |
9328 | + "' (alternatives:" + alternatives.str () + ")", e->tok); | |
9329 | // NB: we can have multiple errors, since a target variable | |
9330 | // may be expanded in several different contexts: | |
9331 | // trace ("*") { $foo->bar } | |
9332 | saveme->chain = e->saved_conversion_error; | |
9333 | e->saved_conversion_error = saveme; | |
9334 | provide (e); | |
9335 | return; | |
9336 | } | |
9337 | ||
9338 | // make sure we're not dereferencing base types | |
9339 | if (!e->components.empty() && !arg->isptr) | |
9340 | switch (e->components[0].first) | |
9341 | { | |
9342 | case target_symbol::comp_literal_array_index: | |
9343 | throw semantic_error("tracepoint variable '" + e->base_name | |
9344 | + "' may not be used as array", e->tok); | |
9345 | case target_symbol::comp_struct_member: | |
9346 | throw semantic_error("tracepoint variable '" + e->base_name | |
9347 | + "' may not be used as a structure", e->tok); | |
9348 | default: | |
9349 | throw semantic_error("invalid use of tracepoint variable '" | |
9350 | + e->base_name + "'", e->tok); | |
9351 | } | |
9352 | ||
9353 | // we can only write to dereferenced fields, and only if guru mode is on | |
9354 | bool lvalue = is_active_lvalue(e); | |
9355 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
9356 | throw semantic_error("write to tracepoint variable '" + e->base_name | |
9357 | + "' not permitted", e->tok); | |
ad370dcc JS |
9358 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
9359 | // is also meaningless until you dereference past a pointer member. It's | |
9360 | // harder to detect and prevent that though... | |
f8a968bc JS |
9361 | |
9362 | if (e->components.empty()) | |
9363 | { | |
9364 | // Synthesize a simple function to grab the parameter | |
9365 | functiondecl *fdecl = new functiondecl; | |
9366 | fdecl->tok = e->tok; | |
9367 | embeddedcode *ec = new embeddedcode; | |
9368 | ec->tok = e->tok; | |
9369 | ||
9370 | string fname = (string("_tracepoint_tvar_get") | |
9371 | + "_" + e->base_name.substr(1) | |
9372 | + "_" + lex_cast<string>(tick++)); | |
9373 | ||
9374 | fdecl->name = fname; | |
9375 | fdecl->body = ec; | |
9376 | fdecl->type = pe_long; | |
9377 | ||
9378 | ec->code = (string("THIS->__retvalue = CONTEXT->locals[0].") | |
9379 | + probe_name + string(".__tracepoint_arg_") | |
9380 | + arg->name + string (";/* pure */")); | |
9381 | ||
9382 | dw.sess.functions[fdecl->name] = fdecl; | |
9383 | ||
9384 | // Synthesize a functioncall. | |
9385 | functioncall* n = new functioncall; | |
9386 | n->tok = e->tok; | |
9387 | n->function = fname; | |
9388 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
9389 | ||
9390 | provide (n); | |
9391 | } | |
9392 | else | |
9393 | { | |
9394 | // Synthesize a function to dereference the dwarf fields, | |
9395 | // with a pointer parameter that is the base tracepoint variable | |
9396 | functiondecl *fdecl = new functiondecl; | |
9397 | fdecl->tok = e->tok; | |
9398 | embeddedcode *ec = new embeddedcode; | |
9399 | ec->tok = e->tok; | |
9400 | ||
9401 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") | |
9402 | + "_" + e->base_name.substr(1) | |
9403 | + "_" + lex_cast<string>(tick++)); | |
9404 | ||
9405 | fdecl->name = fname; | |
9406 | fdecl->body = ec; | |
9407 | ||
9408 | try | |
9409 | { | |
9410 | ec->code = dw.literal_stmt_for_pointer (&arg->type_die, e->components, | |
9411 | lvalue, fdecl->type); | |
9412 | } | |
9413 | catch (const semantic_error& er) | |
9414 | { | |
9415 | // We suppress this error message, and pass the unresolved | |
9416 | // variable to the next pass. We hope that this value ends | |
9417 | // up not being referenced after all, so it can be optimized out | |
9418 | // quietly. | |
9419 | semantic_error* saveme = new semantic_error (er); // copy it | |
9420 | saveme->tok1 = e->tok; // XXX: token not passed to dw code generation routines | |
9421 | // NB: we can have multiple errors, since a target variable | |
9422 | // may be expanded in several different contexts: | |
9423 | // trace ("*") { $foo->bar } | |
9424 | saveme->chain = e->saved_conversion_error; | |
9425 | e->saved_conversion_error = saveme; | |
9426 | provide (e); | |
9427 | return; | |
9428 | } | |
9429 | ||
9430 | // Give the fdecl an argument for the raw tracepoint value | |
9431 | vardecl *v1 = new vardecl; | |
9432 | v1->type = pe_long; | |
9433 | v1->name = "pointer"; | |
9434 | v1->tok = e->tok; | |
9435 | fdecl->formal_args.push_back(v1); | |
9436 | ||
9437 | if (lvalue) | |
9438 | { | |
9439 | // Modify the fdecl so it carries a pe_long formal | |
9440 | // argument called "value". | |
9441 | ||
9442 | // FIXME: For the time being we only support setting target | |
9443 | // variables which have base types; these are 'pe_long' in | |
9444 | // stap's type vocabulary. Strings and pointers might be | |
9445 | // reasonable, some day, but not today. | |
9446 | ||
9447 | vardecl *v2 = new vardecl; | |
9448 | v2->type = pe_long; | |
9449 | v2->name = "value"; | |
9450 | v2->tok = e->tok; | |
9451 | fdecl->formal_args.push_back(v2); | |
9452 | } | |
9453 | else | |
9454 | ec->code += "/* pure */"; | |
9455 | ||
9456 | dw.sess.functions[fdecl->name] = fdecl; | |
9457 | ||
9458 | // Synthesize a functioncall. | |
9459 | functioncall* n = new functioncall; | |
9460 | n->tok = e->tok; | |
9461 | n->function = fname; | |
9462 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
9463 | ||
9464 | // make the original a bare target symbol for the tracepoint value, | |
9465 | // which will be passed into the dwarf dereferencing code | |
9466 | e->components.clear(); | |
9467 | n->args.push_back(require(e)); | |
9468 | ||
9469 | if (lvalue) | |
9470 | { | |
9471 | // Provide the functioncall to our parent, so that it can be | |
9472 | // used to substitute for the assignment node immediately above | |
9473 | // us. | |
9474 | assert(!target_symbol_setter_functioncalls.empty()); | |
9475 | *(target_symbol_setter_functioncalls.top()) = n; | |
9476 | } | |
9477 | ||
9478 | provide (n); | |
9479 | } | |
9480 | } | |
9481 | ||
9482 | ||
9483 | void | |
9484 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
9485 | { | |
9486 | if (is_active_lvalue (e)) | |
9487 | throw semantic_error("write to tracepoint '" + e->base_name + "' not permitted", e->tok); | |
9488 | ||
9489 | if (!e->components.empty()) | |
9490 | switch (e->components[0].first) | |
9491 | { | |
9492 | case target_symbol::comp_literal_array_index: | |
9493 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as array", | |
9494 | e->tok); | |
9495 | case target_symbol::comp_struct_member: | |
9496 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as a structure", | |
9497 | e->tok); | |
9498 | default: | |
9499 | throw semantic_error("invalid tracepoint '" + e->base_name + "' use", e->tok); | |
9500 | } | |
9501 | ||
9502 | if (e->base_name == "$$name") | |
9503 | { | |
9504 | // Synthesize a functioncall. | |
9505 | functioncall* n = new functioncall; | |
9506 | n->tok = e->tok; | |
9507 | n->function = "_mark_name_get"; | |
9508 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
9509 | provide (n); | |
9510 | } | |
046e7190 | 9511 | else if (e->base_name == "$$vars" || e->base_name == "$$parms") |
f8a968bc JS |
9512 | { |
9513 | target_symbol *tsym = new target_symbol; | |
9514 | print_format* pf = new print_format; | |
9515 | ||
9516 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
9517 | // NB: we synthesize a new token here rather than reusing | |
9518 | // e->tok, because print_format::print likes to use | |
9519 | // its tok->content. | |
9520 | token* pf_tok = new token(*e->tok); | |
9521 | pf_tok->content = "sprintf"; | |
9522 | ||
9523 | pf->tok = pf_tok; | |
9524 | pf->print_to_stream = false; | |
9525 | pf->print_with_format = true; | |
9526 | pf->print_with_delim = false; | |
9527 | pf->print_with_newline = false; | |
9528 | pf->print_char = false; | |
9529 | ||
9530 | for (unsigned i = 0; i < args.size(); ++i) | |
9531 | { | |
dcaa1a65 JS |
9532 | if (!args[i].usable) |
9533 | continue; | |
f8a968bc JS |
9534 | if (i > 0) |
9535 | pf->raw_components += " "; | |
9536 | pf->raw_components += args[i].name; | |
9537 | tsym->tok = e->tok; | |
9538 | tsym->base_name = "$" + args[i].name; | |
9539 | ||
9540 | // every variable should always be accessible! | |
9541 | tsym->saved_conversion_error = 0; | |
9542 | expression *texp = require (tsym); // NB: throws nothing ... | |
9543 | assert (!tsym->saved_conversion_error); // ... but this is how we know it happened. | |
9544 | ||
9545 | pf->raw_components += "=%#x"; | |
9546 | pf->args.push_back(texp); | |
9547 | } | |
9548 | ||
9549 | pf->components = print_format::string_to_components(pf->raw_components); | |
9550 | provide (pf); | |
9551 | } | |
9552 | else | |
9553 | assert(0); // shouldn't get here | |
9554 | } | |
9555 | ||
9556 | void | |
9557 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
9558 | { | |
9559 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
9560 | ||
046e7190 JS |
9561 | if (e->base_name == "$$name" || |
9562 | e->base_name == "$$parms" || | |
9563 | e->base_name == "$$vars") | |
f8a968bc JS |
9564 | visit_target_symbol_context (e); |
9565 | else | |
9566 | visit_target_symbol_arg (e); | |
9567 | } | |
9568 | ||
9569 | ||
9570 | ||
79189b84 JS |
9571 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
9572 | dwflpp& dw, Dwarf_Die& func_die, | |
9573 | const string& tracepoint_name, | |
9574 | probe* base, probe_point* loc): | |
9575 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), | |
9576 | sess (s), tracepoint_name (tracepoint_name) | |
9577 | { | |
9578 | // create synthetic probe point name; preserve condition | |
9579 | vector<probe_point::component*> comps; | |
9580 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
9581 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
9582 | this->sole_location()->components = comps; | |
9583 | ||
6fb70fb7 JS |
9584 | // fill out the available arguments in this tracepoint |
9585 | build_args(dw, func_die); | |
9586 | ||
9587 | // determine which header defined this tracepoint | |
9588 | string decl_file = dwarf_decl_file(&func_die); | |
9589 | size_t header_pos = decl_file.rfind("trace/"); | |
9590 | if (header_pos == string::npos) | |
9591 | throw semantic_error ("cannot parse header location for tracepoint '" | |
9592 | + tracepoint_name + "' in '" | |
9593 | + decl_file + "'"); | |
9594 | header = decl_file.substr(header_pos); | |
9595 | ||
9596 | // tracepoints from FOO_event_types.h should really be included from FOO.h | |
9597 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
9598 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 9599 | // XXX: see also PR9993. |
6fb70fb7 JS |
9600 | header_pos = header.find("_event_types"); |
9601 | if (header_pos != string::npos) | |
9602 | header.erase(header_pos, 12); | |
9603 | ||
f8a968bc JS |
9604 | // Now expand the local variables in the probe body |
9605 | tracepoint_var_expanding_visitor v (dw, name, args); | |
9606 | this->body = v.require (this->body); | |
9607 | ||
79189b84 JS |
9608 | if (sess.verbose > 2) |
9609 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name | |
9610 | << "'" << endl; | |
9611 | } | |
9612 | ||
9613 | ||
6fb70fb7 JS |
9614 | static bool |
9615 | dwarf_type_name(Dwarf_Die& type_die, string& c_type) | |
9616 | { | |
219e62c7 JS |
9617 | // if we've gotten down to a basic type, then we're done |
9618 | bool done = true; | |
9619 | switch (dwarf_tag(&type_die)) | |
6fb70fb7 | 9620 | { |
219e62c7 JS |
9621 | case DW_TAG_structure_type: |
9622 | c_type.append("struct "); | |
9623 | break; | |
9624 | case DW_TAG_union_type: | |
9625 | c_type.append("union "); | |
9626 | break; | |
9627 | case DW_TAG_typedef: | |
9628 | case DW_TAG_base_type: | |
9629 | break; | |
9630 | default: | |
9631 | done = false; | |
9632 | break; | |
9633 | } | |
9634 | if (done) | |
9635 | { | |
9636 | c_type.append(dwarf_diename_integrate(&type_die)); | |
6fb70fb7 JS |
9637 | return true; |
9638 | } | |
9639 | ||
9640 | // otherwise, this die is a type modifier. | |
9641 | ||
9642 | // recurse into the referent type | |
219e62c7 | 9643 | // if it can't be named, just call it "void" |
6fb70fb7 JS |
9644 | Dwarf_Attribute subtype_attr; |
9645 | Dwarf_Die subtype_die; | |
9646 | if (!dwarf_attr_integrate(&type_die, DW_AT_type, &subtype_attr) | |
9647 | || !dwarf_formref_die(&subtype_attr, &subtype_die) | |
9648 | || !dwarf_type_name(subtype_die, c_type)) | |
219e62c7 | 9649 | c_type = "void"; |
6fb70fb7 JS |
9650 | |
9651 | const char *suffix = NULL; | |
9652 | switch (dwarf_tag(&type_die)) | |
9653 | { | |
9654 | case DW_TAG_pointer_type: | |
9655 | suffix = "*"; | |
9656 | break; | |
9657 | case DW_TAG_array_type: | |
9658 | suffix = "[]"; | |
9659 | break; | |
9660 | case DW_TAG_const_type: | |
9661 | suffix = " const"; | |
9662 | break; | |
9663 | case DW_TAG_volatile_type: | |
9664 | suffix = " volatile"; | |
9665 | break; | |
9666 | default: | |
9667 | return false; | |
9668 | } | |
9669 | c_type.append(suffix); | |
219e62c7 JS |
9670 | |
9671 | // XXX HACK! The va_list isn't usable as found in the debuginfo... | |
9672 | if (c_type == "struct __va_list_tag*") | |
9673 | c_type = "va_list"; | |
9674 | ||
6fb70fb7 JS |
9675 | return true; |
9676 | } | |
9677 | ||
9678 | ||
f8a968bc | 9679 | static bool |
dcaa1a65 | 9680 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
f8a968bc JS |
9681 | { |
9682 | Dwarf_Attribute type_attr; | |
dcaa1a65 | 9683 | switch (dwarf_tag(&arg.type_die)) |
f8a968bc JS |
9684 | { |
9685 | case DW_TAG_typedef: | |
9686 | case DW_TAG_const_type: | |
9687 | case DW_TAG_volatile_type: | |
9688 | // iterate on the referent type | |
dcaa1a65 JS |
9689 | return (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) |
9690 | && dwarf_formref_die(&type_attr, &arg.type_die) | |
9691 | && resolve_tracepoint_arg_type(arg)); | |
f8a968bc JS |
9692 | case DW_TAG_base_type: |
9693 | // base types will simply be treated as script longs | |
dcaa1a65 | 9694 | arg.isptr = false; |
f8a968bc JS |
9695 | return true; |
9696 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
9697 | // pointers can be treated as script longs, |
9698 | // and if we know their type, they can also be dereferenced | |
9699 | if (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) | |
9700 | && dwarf_formref_die(&type_attr, &arg.type_die)) | |
9701 | arg.isptr = true; | |
ad370dcc JS |
9702 | arg.typecast = "(intptr_t)"; |
9703 | return true; | |
9704 | case DW_TAG_structure_type: | |
9705 | case DW_TAG_union_type: | |
9706 | // for structs/unions which are passed by value, we turn it into | |
9707 | // a pointer that can be dereferenced. | |
9708 | arg.isptr = true; | |
9709 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 9710 | return true; |
f8a968bc JS |
9711 | default: |
9712 | // should we consider other types too? | |
9713 | return false; | |
9714 | } | |
9715 | } | |
9716 | ||
9717 | ||
6fb70fb7 JS |
9718 | void |
9719 | tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die) | |
9720 | { | |
9721 | Dwarf_Die arg; | |
9722 | if (dwarf_child(&func_die, &arg) == 0) | |
9723 | do | |
9724 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
9725 | { | |
9726 | // build a tracepoint_arg for this parameter | |
9727 | tracepoint_arg tparg; | |
9728 | tparg.name = dwarf_diename_integrate(&arg); | |
9729 | ||
9730 | // read the type of this parameter | |
9731 | Dwarf_Attribute type_attr; | |
6fb70fb7 | 9732 | if (!dwarf_attr_integrate (&arg, DW_AT_type, &type_attr) |
f8a968bc | 9733 | || !dwarf_formref_die (&type_attr, &tparg.type_die) |
dcaa1a65 | 9734 | || !dwarf_type_name(tparg.type_die, tparg.c_type)) |
6fb70fb7 JS |
9735 | throw semantic_error ("cannot get type of tracepoint '" |
9736 | + tracepoint_name + "' parameter '" | |
9737 | + tparg.name + "'"); | |
9738 | ||
dcaa1a65 | 9739 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
9740 | args.push_back(tparg); |
9741 | if (sess.verbose > 4) | |
9742 | clog << "found parameter for tracepoint '" << tracepoint_name | |
9743 | << "': type:'" << tparg.c_type | |
9744 | << "' name:'" << tparg.name << "'" << endl; | |
9745 | } | |
9746 | while (dwarf_siblingof(&arg, &arg) == 0); | |
9747 | } | |
9748 | ||
e2086848 WH |
9749 | void |
9750 | tracepoint_derived_probe::printargs(std::ostream &o) const | |
9751 | { | |
dcaa1a65 JS |
9752 | for (unsigned i = 0; i < args.size(); ++i) |
9753 | if (args[i].usable) | |
9754 | o << " $" << args[i].name << ":" << args[i].c_type; | |
e2086848 | 9755 | } |
6fb70fb7 | 9756 | |
79189b84 JS |
9757 | void |
9758 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
9759 | { | |
9760 | if (! s.tracepoint_derived_probes) | |
9761 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
9762 | s.tracepoint_derived_probes->enroll (this); | |
9763 | } | |
9764 | ||
9765 | ||
f8a968bc JS |
9766 | void |
9767 | tracepoint_derived_probe::emit_probe_context_vars (translator_output* o) | |
9768 | { | |
9769 | for (unsigned i = 0; i < args.size(); i++) | |
9770 | if (args[i].used) | |
9771 | o->newline() << "int64_t __tracepoint_arg_" << args[i].name << ";"; | |
9772 | } | |
9773 | ||
9774 | ||
3c1b3d06 FCE |
9775 | static vector<string> tracepoint_extra_headers () |
9776 | { | |
9777 | vector<string> they_live; | |
9778 | // PR 9993 | |
9779 | // XXX: may need this to be configurable | |
9780 | they_live.push_back ("linux/skbuff.h"); | |
9781 | return they_live; | |
9782 | } | |
9783 | ||
9784 | ||
79189b84 JS |
9785 | void |
9786 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) | |
9787 | { | |
9788 | if (probes.empty()) | |
9789 | return; | |
9790 | ||
96b030fe JS |
9791 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
9792 | s.op->newline(); | |
79189b84 | 9793 | |
3c1b3d06 FCE |
9794 | // PR9993: Add extra headers to work around undeclared types in individual |
9795 | // include/trace/foo.h files | |
9796 | const vector<string>& extra_headers = tracepoint_extra_headers (); | |
9797 | for (unsigned z=0; z<extra_headers.size(); z++) | |
9798 | s.op->newline() << "#include <" << extra_headers[z] << ">\n"; | |
9799 | ||
6fb70fb7 JS |
9800 | for (unsigned i = 0; i < probes.size(); ++i) |
9801 | { | |
9802 | tracepoint_derived_probe *p = probes[i]; | |
96b030fe JS |
9803 | |
9804 | // emit a separate entry function for each probe, since tracepoints | |
9805 | // don't provide any sort of context pointer. | |
6fb70fb7 JS |
9806 | s.op->newline() << "#include <" << p->header << ">"; |
9807 | s.op->newline() << "static void enter_tracepoint_probe_" << i << "("; | |
8df306c4 JS |
9808 | if (p->args.size() == 0) |
9809 | s.op->line() << "void"; | |
6fb70fb7 JS |
9810 | for (unsigned j = 0; j < p->args.size(); ++j) |
9811 | { | |
9812 | if (j > 0) | |
9813 | s.op->line() << ", "; | |
9814 | s.op->line() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
9815 | } | |
9816 | s.op->line() << ") {"; | |
9817 | s.op->indent(1); | |
c12d974f FCE |
9818 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", |
9819 | lex_cast_qstring (*p->sole_location())); | |
f8a968bc | 9820 | s.op->newline() << "c->marker_name = " |
c12d974f FCE |
9821 | << lex_cast_qstring (p->tracepoint_name) |
9822 | << ";"; | |
f8a968bc JS |
9823 | for (unsigned j = 0; j < p->args.size(); ++j) |
9824 | if (p->args[j].used) | |
9825 | { | |
9826 | s.op->newline() << "c->locals[0]." << p->name << ".__tracepoint_arg_" | |
9827 | << p->args[j].name << " = (int64_t)"; | |
ad370dcc | 9828 | s.op->line() << p->args[j].typecast; |
f8a968bc JS |
9829 | s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";"; |
9830 | } | |
6fb70fb7 JS |
9831 | s.op->newline() << p->name << " (c);"; |
9832 | common_probe_entryfn_epilogue (s.op); | |
9833 | s.op->newline(-1) << "}"; | |
96b030fe JS |
9834 | |
9835 | // emit normalized registration functions | |
9836 | s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {"; | |
9837 | s.op->newline(1) << "return register_trace_" << p->tracepoint_name | |
9838 | << "(enter_tracepoint_probe_" << i << ");"; | |
9839 | s.op->newline(-1) << "}"; | |
86758d5f JS |
9840 | |
9841 | // NB: we're not prepared to deal with unreg failures. However, failures | |
9842 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
9843 | // weren't even registered. The former should be OKed by the initial | |
9844 | // registration call, and the latter is safe to ignore. | |
9845 | s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {"; | |
9846 | s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name | |
96b030fe JS |
9847 | << "(enter_tracepoint_probe_" << i << ");"; |
9848 | s.op->newline(-1) << "}"; | |
6fb70fb7 JS |
9849 | s.op->newline(); |
9850 | } | |
96b030fe JS |
9851 | |
9852 | // emit an array of registration functions for easy init/shutdown | |
9853 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
9854 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 9855 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
9856 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
9857 | s.op->indent(1); | |
9858 | for (unsigned i = 0; i < probes.size(); ++i) | |
9859 | { | |
9860 | s.op->newline () << "{"; | |
9861 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
9862 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
9863 | s.op->line() << " },"; | |
9864 | } | |
9865 | s.op->newline(-1) << "};"; | |
9866 | s.op->newline(); | |
79189b84 JS |
9867 | } |
9868 | ||
9869 | ||
9870 | void | |
9871 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
9872 | { | |
9873 | if (probes.size () == 0) | |
9874 | return; | |
9875 | ||
9876 | s.op->newline() << "/* init tracepoint probes */"; | |
96b030fe JS |
9877 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
9878 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
9879 | s.op->newline() << "if (rc) {"; | |
9880 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
9881 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
9882 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
9883 | s.op->newline(-1) << "}"; | |
9884 | s.op->newline(-1) << "}"; | |
bc9a523d FCE |
9885 | |
9886 | // This would be technically proper (on those autoconf-detectable | |
9887 | // kernels that include this function in tracepoint.h), however we | |
9888 | // already make several calls to synchronze_sched() during our | |
9889 | // shutdown processes. | |
9890 | ||
9891 | // s.op->newline() << "if (rc)"; | |
9892 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
9893 | // s.op->indent(-1); | |
79189b84 JS |
9894 | } |
9895 | ||
9896 | ||
9897 | void | |
9898 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
9899 | { | |
9900 | if (probes.empty()) | |
9901 | return; | |
9902 | ||
96b030fe JS |
9903 | s.op->newline() << "/* deregister tracepoint probes */"; |
9904 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
9905 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
9906 | s.op->indent(-1); | |
bc9a523d FCE |
9907 | |
9908 | // Not necessary: see above. | |
9909 | ||
9910 | // s.op->newline() << "tracepoint_synchronize_unregister();"; | |
79189b84 JS |
9911 | } |
9912 | ||
9913 | ||
75ead1f7 JS |
9914 | struct tracepoint_query : public base_query |
9915 | { | |
9916 | tracepoint_query(dwflpp & dw, const string & tracepoint, | |
9917 | probe * base_probe, probe_point * base_loc, | |
9918 | vector<derived_probe *> & results): | |
9919 | base_query(dw, "*"), tracepoint(tracepoint), | |
9920 | base_probe(base_probe), base_loc(base_loc), | |
9921 | results(results) {} | |
9922 | ||
9923 | const string& tracepoint; | |
9924 | ||
9925 | probe * base_probe; | |
9926 | probe_point * base_loc; | |
9927 | vector<derived_probe *> & results; | |
9928 | ||
9929 | void handle_query_module(); | |
9930 | int handle_query_cu(Dwarf_Die * cudie); | |
9931 | int handle_query_func(Dwarf_Die * func); | |
9932 | ||
9933 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); | |
9934 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
9935 | }; | |
9936 | ||
9937 | ||
9938 | void | |
9939 | tracepoint_query::handle_query_module() | |
9940 | { | |
9941 | // look for the tracepoints in each CU | |
9942 | dw.iterate_over_cus(tracepoint_query_cu, this); | |
9943 | } | |
9944 | ||
9945 | ||
9946 | int | |
9947 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
9948 | { | |
9949 | dw.focus_on_cu (cudie); | |
9950 | ||
9951 | // look at each function to see if it's a tracepoint | |
9952 | string function = "stapprobe_" + tracepoint; | |
9953 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
9954 | } | |
9955 | ||
9956 | ||
9957 | int | |
9958 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
9959 | { | |
9960 | dw.focus_on_function (func); | |
9961 | ||
9962 | assert(dw.function_name.compare(0, 10, "stapprobe_") == 0); | |
9963 | string tracepoint_instance = dw.function_name.substr(10); | |
79189b84 JS |
9964 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
9965 | tracepoint_instance, | |
9966 | base_probe, base_loc); | |
9967 | results.push_back (dp); | |
75ead1f7 JS |
9968 | return DWARF_CB_OK; |
9969 | } | |
9970 | ||
9971 | ||
9972 | int | |
9973 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
9974 | { | |
9975 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); | |
9976 | if (pending_interrupts) return DWARF_CB_ABORT; | |
9977 | return q->handle_query_cu(cudie); | |
9978 | } | |
9979 | ||
9980 | ||
9981 | int | |
9982 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
9983 | { | |
9984 | tracepoint_query * q = static_cast<tracepoint_query *>(query); | |
9985 | if (pending_interrupts) return DWARF_CB_ABORT; | |
9986 | return q->handle_query_func(func); | |
9987 | } | |
9988 | ||
9989 | ||
0a6f5a3f JS |
9990 | struct tracepoint_builder: public derived_probe_builder |
9991 | { | |
9992 | private: | |
9993 | dwflpp *dw; | |
9994 | bool init_dw(systemtap_session& s); | |
9995 | ||
9996 | public: | |
3c1b3d06 | 9997 | |
0a6f5a3f JS |
9998 | tracepoint_builder(): dw(0) {} |
9999 | ~tracepoint_builder() { delete dw; } | |
10000 | ||
10001 | void build_no_more (systemtap_session& s) | |
10002 | { | |
10003 | if (dw && s.verbose > 3) | |
10004 | clog << "tracepoint_builder releasing dwflpp" << endl; | |
10005 | delete dw; | |
10006 | dw = NULL; | |
10007 | } | |
10008 | ||
10009 | void build(systemtap_session& s, | |
10010 | probe *base, probe_point *location, | |
10011 | literal_map_t const& parameters, | |
10012 | vector<derived_probe*>& finished_results); | |
10013 | }; | |
10014 | ||
10015 | ||
10016 | bool | |
10017 | tracepoint_builder::init_dw(systemtap_session& s) | |
10018 | { | |
10019 | if (dw != NULL) | |
10020 | return true; | |
10021 | ||
b278033a JS |
10022 | if (s.use_cache) |
10023 | { | |
10024 | // see if the cached module exists | |
10025 | find_tracequery_hash(s); | |
10026 | if (!s.tracequery_path.empty()) | |
10027 | { | |
10028 | int fd = open(s.tracequery_path.c_str(), O_RDONLY); | |
10029 | if (fd != -1) | |
10030 | { | |
10031 | if (s.verbose > 2) | |
10032 | clog << "Pass 2: using cached " << s.tracequery_path << endl; | |
10033 | ||
10034 | dw = new dwflpp(s); | |
10035 | dw->setup_user(s.tracequery_path); | |
10036 | close(fd); | |
10037 | return true; | |
10038 | } | |
10039 | } | |
10040 | } | |
10041 | ||
10042 | // no cached module, time to make it | |
0a6f5a3f | 10043 | string tracequery_ko; |
3c1b3d06 | 10044 | int rc = make_tracequery(s, tracequery_ko, tracepoint_extra_headers()); |
0a6f5a3f JS |
10045 | if (rc != 0) |
10046 | return false; | |
10047 | ||
b278033a JS |
10048 | if (s.use_cache) |
10049 | { | |
10050 | // try to save tracequery in the cache | |
10051 | if (s.verbose > 2) | |
10052 | clog << "Copying " << tracequery_ko | |
10053 | << " to " << s.tracequery_path << endl; | |
10054 | if (copy_file(tracequery_ko.c_str(), | |
10055 | s.tracequery_path.c_str()) != 0) | |
10056 | cerr << "Copy failed (\"" << tracequery_ko << "\" to \"" | |
10057 | << s.tracequery_path << "\"): " << strerror(errno) << endl; | |
10058 | } | |
0a6f5a3f JS |
10059 | |
10060 | dw = new dwflpp(s); | |
10061 | dw->setup_user(tracequery_ko); | |
10062 | return true; | |
10063 | } | |
10064 | ||
10065 | ||
10066 | void | |
10067 | tracepoint_builder::build(systemtap_session& s, | |
10068 | probe *base, probe_point *location, | |
10069 | literal_map_t const& parameters, | |
10070 | vector<derived_probe*>& finished_results) | |
10071 | { | |
10072 | if (!init_dw(s)) | |
10073 | return; | |
10074 | ||
75ead1f7 JS |
10075 | string tracepoint; |
10076 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
10077 | ||
10078 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); | |
10079 | dw->query_modules(&q); | |
0a6f5a3f JS |
10080 | } |
10081 | ||
10082 | ||
10083 | ||
56894e91 JS |
10084 | // ------------------------------------------------------------------------ |
10085 | // hrtimer derived probes | |
10086 | // ------------------------------------------------------------------------ | |
10087 | // This is a new timer interface that provides more flexibility in specifying | |
10088 | // intervals, and uses the hrtimer APIs when available for greater precision. | |
39014506 JS |
10089 | // While hrtimers were added in 2.6.16, the API's weren't exported until |
10090 | // 2.6.17, so we must check this kernel version before attempting to use | |
10091 | // hrtimers. | |
56894e91 | 10092 | // |
56894e91 | 10093 | // * hrtimer_derived_probe: creates a probe point based on the hrtimer APIs. |
56894e91 JS |
10094 | |
10095 | ||
10096 | struct hrtimer_derived_probe: public derived_probe | |
10097 | { | |
10098 | // set a (generous) maximum of one day in ns | |
10099 | static const int64_t max_ns_interval = 1000000000LL * 60LL * 60LL * 24LL; | |
10100 | ||
10101 | // 100us seems like a reasonable minimum | |
10102 | static const int64_t min_ns_interval = 100000LL; | |
10103 | ||
10104 | int64_t interval, randomize; | |
10105 | ||
10106 | hrtimer_derived_probe (probe* p, probe_point* l, int64_t i, int64_t r): | |
10107 | derived_probe (p, l), interval (i), randomize (r) | |
10108 | { | |
10109 | if ((i < min_ns_interval) || (i > max_ns_interval)) | |
10110 | throw semantic_error("interval value out of range"); | |
10111 | ||
10112 | // randomize = 0 means no randomization | |
10113 | if ((r < 0) || (r > i)) | |
10114 | throw semantic_error("randomization value out of range"); | |
56894e91 JS |
10115 | } |
10116 | ||
b20febf3 FCE |
10117 | void join_group (systemtap_session& s); |
10118 | }; | |
dc38c0ae | 10119 | |
56894e91 | 10120 | |
b20febf3 FCE |
10121 | struct hrtimer_derived_probe_group: public generic_dpg<hrtimer_derived_probe> |
10122 | { | |
10123 | void emit_interval (translator_output* o); | |
10124 | public: | |
10125 | void emit_module_decls (systemtap_session& s); | |
10126 | void emit_module_init (systemtap_session& s); | |
10127 | void emit_module_exit (systemtap_session& s); | |
56894e91 JS |
10128 | }; |
10129 | ||
10130 | ||
dc38c0ae | 10131 | void |
b20febf3 | 10132 | hrtimer_derived_probe::join_group (systemtap_session& s) |
dc38c0ae | 10133 | { |
b20febf3 FCE |
10134 | if (! s.hrtimer_derived_probes) |
10135 | s.hrtimer_derived_probes = new hrtimer_derived_probe_group (); | |
10136 | s.hrtimer_derived_probes->enroll (this); | |
dc38c0ae DS |
10137 | } |
10138 | ||
10139 | ||
56894e91 | 10140 | void |
b20febf3 | 10141 | hrtimer_derived_probe_group::emit_interval (translator_output* o) |
56894e91 JS |
10142 | { |
10143 | o->line() << "({"; | |
ffb0b3ad | 10144 | o->newline(1) << "unsigned long nsecs;"; |
b20febf3 FCE |
10145 | o->newline() << "int64_t i = stp->intrv;"; |
10146 | o->newline() << "if (stp->rnd != 0) {"; | |
10147 | // XXX: why not use stp_random_pm instead of this? | |
10148 | o->newline(1) << "int64_t r;"; | |
10149 | o->newline() << "get_random_bytes(&r, sizeof(r));"; | |
10150 | // ensure that r is positive | |
10151 | o->newline() << "r &= ((uint64_t)1 << (8*sizeof(r) - 1)) - 1;"; | |
10152 | o->newline() << "r = _stp_mod64(NULL, r, (2*stp->rnd+1));"; | |
10153 | o->newline() << "r -= stp->rnd;"; | |
10154 | o->newline() << "i += r;"; | |
10155 | o->newline(-1) << "}"; | |
10156 | o->newline() << "if (unlikely(i < stap_hrtimer_resolution))"; | |
10157 | o->newline(1) << "i = stap_hrtimer_resolution;"; | |
197a4d62 | 10158 | o->indent(-1); |
ffb0b3ad JS |
10159 | o->newline() << "nsecs = do_div(i, NSEC_PER_SEC);"; |
10160 | o->newline() << "ktime_set(i, nsecs);"; | |
56894e91 JS |
10161 | o->newline(-1) << "})"; |
10162 | } | |
10163 | ||
10164 | ||
10165 | void | |
b20febf3 | 10166 | hrtimer_derived_probe_group::emit_module_decls (systemtap_session& s) |
46b84a80 | 10167 | { |
b20febf3 | 10168 | if (probes.empty()) return; |
46b84a80 | 10169 | |
b20febf3 | 10170 | s.op->newline() << "/* ---- hrtimer probes ---- */"; |
46b84a80 | 10171 | |
4c2732a1 JS |
10172 | s.op->newline() << "static unsigned long stap_hrtimer_resolution;"; // init later |
10173 | s.op->newline() << "static struct stap_hrtimer_probe {"; | |
b20febf3 FCE |
10174 | s.op->newline(1) << "struct hrtimer hrtimer;"; |
10175 | s.op->newline() << "const char *pp;"; | |
10176 | s.op->newline() << "void (*ph) (struct context*);"; | |
10177 | s.op->newline() << "int64_t intrv, rnd;"; | |
10178 | s.op->newline(-1) << "} stap_hrtimer_probes [" << probes.size() << "] = {"; | |
10179 | s.op->indent(1); | |
10180 | for (unsigned i=0; i < probes.size(); i++) | |
10181 | { | |
4baf0e53 | 10182 | s.op->newline () << "{"; |
b20febf3 FCE |
10183 | s.op->line() << " .pp=" << lex_cast_qstring (*probes[i]->sole_location()) << ","; |
10184 | s.op->line() << " .ph=&" << probes[i]->name << ","; | |
10185 | s.op->line() << " .intrv=" << probes[i]->interval << "LL,"; | |
10186 | s.op->line() << " .rnd=" << probes[i]->randomize << "LL"; | |
10187 | s.op->line() << " },"; | |
10188 | } | |
10189 | s.op->newline(-1) << "};"; | |
10190 | s.op->newline(); | |
10191 | ||
d9d9f852 JS |
10192 | // autoconf: add get/set expires if missing (pre 2.6.28-rc1) |
10193 | s.op->newline() << "#ifndef STAPCONF_HRTIMER_GETSET_EXPIRES"; | |
10194 | s.op->newline() << "#define hrtimer_get_expires(timer) ((timer)->expires)"; | |
10195 | s.op->newline() << "#define hrtimer_set_expires(timer, time) (void)((timer)->expires = (time))"; | |
10196 | s.op->newline() << "#endif"; | |
10197 | ||
255e4c68 FCE |
10198 | // autoconf: adapt to HRTIMER_REL -> HRTIMER_MODE_REL renaming near 2.6.21 |
10199 | s.op->newline() << "#ifdef STAPCONF_HRTIMER_REL"; | |
10200 | s.op->newline() << "#define HRTIMER_MODE_REL HRTIMER_REL"; | |
10201 | s.op->newline() << "#endif"; | |
4baf0e53 | 10202 | |
5dbd55d7 | 10203 | // The function signature changed in 2.6.21. |
255e4c68 FCE |
10204 | s.op->newline() << "#ifdef STAPCONF_HRTIMER_REL"; |
10205 | s.op->newline() << "static int "; | |
10206 | s.op->newline() << "#else"; | |
10207 | s.op->newline() << "static enum hrtimer_restart "; | |
10208 | s.op->newline() << "#endif"; | |
10209 | s.op->newline() << "enter_hrtimer_probe (struct hrtimer *timer) {"; | |
5dbd55d7 | 10210 | |
4baf0e53 | 10211 | s.op->newline(1) << "int rc = HRTIMER_NORESTART;"; |
e0d86324 JS |
10212 | s.op->newline() << "struct stap_hrtimer_probe *stp = container_of(timer, struct stap_hrtimer_probe, hrtimer);"; |
10213 | s.op->newline() << "if ((atomic_read (&session_state) == STAP_SESSION_STARTING) ||"; | |
10214 | s.op->newline() << " (atomic_read (&session_state) == STAP_SESSION_RUNNING)) {"; | |
b20febf3 | 10215 | // Compute next trigger time |
d9d9f852 | 10216 | s.op->newline(1) << "hrtimer_set_expires(timer, ktime_add (hrtimer_get_expires(timer),"; |
b20febf3 | 10217 | emit_interval (s.op); |
f7674e54 | 10218 | s.op->line() << "));"; |
d9d9f852 | 10219 | s.op->newline() << "rc = HRTIMER_RESTART;"; |
e0d86324 JS |
10220 | s.op->newline(-1) << "}"; |
10221 | s.op->newline() << "{"; | |
10222 | s.op->indent(1); | |
c12d974f | 10223 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "stp->pp"); |
b20febf3 FCE |
10224 | s.op->newline() << "(*stp->ph) (c);"; |
10225 | common_probe_entryfn_epilogue (s.op); | |
e0d86324 JS |
10226 | s.op->newline(-1) << "}"; |
10227 | s.op->newline() << "return rc;"; | |
b20febf3 | 10228 | s.op->newline(-1) << "}"; |
56894e91 JS |
10229 | } |
10230 | ||
10231 | ||
10232 | void | |
b20febf3 | 10233 | hrtimer_derived_probe_group::emit_module_init (systemtap_session& s) |
56894e91 | 10234 | { |
b20febf3 | 10235 | if (probes.empty()) return; |
56894e91 | 10236 | |
b20febf3 FCE |
10237 | s.op->newline() << "{"; |
10238 | s.op->newline(1) << "struct timespec res;"; | |
10239 | s.op->newline() << "hrtimer_get_res (CLOCK_MONOTONIC, &res);"; | |
10240 | s.op->newline() << "stap_hrtimer_resolution = timespec_to_ns (&res);"; | |
10241 | s.op->newline(-1) << "}"; | |
a68f81a2 | 10242 | |
b20febf3 FCE |
10243 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
10244 | s.op->newline(1) << "struct stap_hrtimer_probe* stp = & stap_hrtimer_probes [i];"; | |
6f313a73 | 10245 | s.op->newline() << "probe_point = stp->pp;"; |
255e4c68 | 10246 | s.op->newline() << "hrtimer_init (& stp->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);"; |
b20febf3 FCE |
10247 | s.op->newline() << "stp->hrtimer.function = & enter_hrtimer_probe;"; |
10248 | // There is no hrtimer field to identify *this* (i-th) probe handler | |
10249 | // callback. So instead we'll deduce it at entry time. | |
10250 | s.op->newline() << "(void) hrtimer_start (& stp->hrtimer, "; | |
10251 | emit_interval (s.op); | |
255e4c68 | 10252 | s.op->line() << ", HRTIMER_MODE_REL);"; |
b20febf3 FCE |
10253 | // Note: no partial failure rollback is needed: hrtimer_start only |
10254 | // "fails" if the timer was already active, which cannot be. | |
10255 | s.op->newline(-1) << "}"; // for loop | |
56894e91 JS |
10256 | } |
10257 | ||
10258 | ||
dc38c0ae | 10259 | void |
b20febf3 | 10260 | hrtimer_derived_probe_group::emit_module_exit (systemtap_session& s) |
dc38c0ae | 10261 | { |
b20febf3 | 10262 | if (probes.empty()) return; |
197a4d62 | 10263 | |
b20febf3 FCE |
10264 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; |
10265 | s.op->newline(1) << "hrtimer_cancel (& stap_hrtimer_probes[i].hrtimer);"; | |
10266 | s.op->indent(-1); | |
dc38c0ae DS |
10267 | } |
10268 | ||
10269 | ||
56894e91 | 10270 | |
197a4d62 JS |
10271 | struct timer_builder: public derived_probe_builder |
10272 | { | |
10273 | virtual void build(systemtap_session & sess, | |
10274 | probe * base, probe_point * location, | |
86bf665e | 10275 | literal_map_t const & parameters, |
197a4d62 | 10276 | vector<derived_probe *> & finished_results); |
e38d6504 | 10277 | |
c4ce66a1 | 10278 | static void register_patterns(systemtap_session& s); |
56894e91 JS |
10279 | }; |
10280 | ||
197a4d62 JS |
10281 | void |
10282 | timer_builder::build(systemtap_session & sess, | |
10283 | probe * base, | |
10284 | probe_point * location, | |
86bf665e | 10285 | literal_map_t const & parameters, |
197a4d62 | 10286 | vector<derived_probe *> & finished_results) |
56894e91 | 10287 | { |
197a4d62 | 10288 | int64_t period, rand=0; |
56894e91 | 10289 | |
197a4d62 JS |
10290 | if (!get_param(parameters, "randomize", rand)) |
10291 | rand = 0; | |
56894e91 | 10292 | |
197a4d62 | 10293 | if (get_param(parameters, "jiffies", period)) |
56894e91 | 10294 | { |
197a4d62 JS |
10295 | // always use basic timers for jiffies |
10296 | finished_results.push_back( | |
10297 | new timer_derived_probe(base, location, period, rand, false)); | |
10298 | return; | |
56894e91 | 10299 | } |
197a4d62 | 10300 | else if (get_param(parameters, "hz", period)) |
56894e91 | 10301 | { |
197a4d62 JS |
10302 | if (period <= 0) |
10303 | throw semantic_error ("frequency must be greater than 0"); | |
10304 | period = (1000000000 + period - 1)/period; | |
10305 | } | |
10306 | else if (get_param(parameters, "s", period) | |
10307 | || get_param(parameters, "sec", period)) | |
10308 | { | |
10309 | period *= 1000000000; | |
10310 | rand *= 1000000000; | |
10311 | } | |
10312 | else if (get_param(parameters, "ms", period) | |
10313 | || get_param(parameters, "msec", period)) | |
10314 | { | |
10315 | period *= 1000000; | |
10316 | rand *= 1000000; | |
10317 | } | |
10318 | else if (get_param(parameters, "us", period) | |
10319 | || get_param(parameters, "usec", period)) | |
10320 | { | |
10321 | period *= 1000; | |
10322 | rand *= 1000; | |
10323 | } | |
10324 | else if (get_param(parameters, "ns", period) | |
10325 | || get_param(parameters, "nsec", period)) | |
10326 | { | |
10327 | // ok | |
10328 | } | |
10329 | else | |
10330 | throw semantic_error ("unrecognized timer variant"); | |
56894e91 | 10331 | |
b20febf3 FCE |
10332 | // Redirect wallclock-time based probes to hrtimer code on recent |
10333 | // enough kernels. | |
197a4d62 JS |
10334 | if (strverscmp(sess.kernel_base_release.c_str(), "2.6.17") < 0) |
10335 | { | |
10336 | // hrtimers didn't exist, so use the old-school timers | |
10337 | period = (period + 1000000 - 1)/1000000; | |
10338 | rand = (rand + 1000000 - 1)/1000000; | |
56894e91 | 10339 | |
197a4d62 JS |
10340 | finished_results.push_back( |
10341 | new timer_derived_probe(base, location, period, rand, true)); | |
10342 | } | |
10343 | else | |
10344 | finished_results.push_back( | |
10345 | new hrtimer_derived_probe(base, location, period, rand)); | |
10346 | } | |
56894e91 | 10347 | |
197a4d62 | 10348 | void |
c4ce66a1 | 10349 | timer_builder::register_patterns(systemtap_session& s) |
197a4d62 | 10350 | { |
c4ce66a1 | 10351 | match_node* root = s.pattern_root; |
197a4d62 | 10352 | derived_probe_builder *builder = new timer_builder(); |
56894e91 | 10353 | |
12b21830 | 10354 | root = root->bind(TOK_TIMER); |
56894e91 | 10355 | |
197a4d62 JS |
10356 | root->bind_num("s")->bind(builder); |
10357 | root->bind_num("s")->bind_num("randomize")->bind(builder); | |
10358 | root->bind_num("sec")->bind(builder); | |
10359 | root->bind_num("sec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10360 | |
197a4d62 JS |
10361 | root->bind_num("ms")->bind(builder); |
10362 | root->bind_num("ms")->bind_num("randomize")->bind(builder); | |
10363 | root->bind_num("msec")->bind(builder); | |
10364 | root->bind_num("msec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10365 | |
197a4d62 JS |
10366 | root->bind_num("us")->bind(builder); |
10367 | root->bind_num("us")->bind_num("randomize")->bind(builder); | |
10368 | root->bind_num("usec")->bind(builder); | |
10369 | root->bind_num("usec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10370 | |
197a4d62 JS |
10371 | root->bind_num("ns")->bind(builder); |
10372 | root->bind_num("ns")->bind_num("randomize")->bind(builder); | |
10373 | root->bind_num("nsec")->bind(builder); | |
10374 | root->bind_num("nsec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10375 | |
197a4d62 JS |
10376 | root->bind_num("jiffies")->bind(builder); |
10377 | root->bind_num("jiffies")->bind_num("randomize")->bind(builder); | |
4baf0e53 | 10378 | |
197a4d62 | 10379 | root->bind_num("hz")->bind(builder); |
56894e91 JS |
10380 | } |
10381 | ||
10382 | ||
342d3f96 | 10383 | |
47dd066d WC |
10384 | // ------------------------------------------------------------------------ |
10385 | // perfmon derived probes | |
10386 | // ------------------------------------------------------------------------ | |
10387 | // This is a new interface to the perfmon hw. | |
10388 | // | |
10389 | ||
10390 | ||
de688825 | 10391 | struct perfmon_var_expanding_visitor: public var_expanding_visitor |
47dd066d WC |
10392 | { |
10393 | systemtap_session & sess; | |
10394 | unsigned counter_number; | |
de688825 | 10395 | perfmon_var_expanding_visitor(systemtap_session & s, unsigned c): |
47dd066d WC |
10396 | sess(s), counter_number(c) {} |
10397 | void visit_target_symbol (target_symbol* e); | |
10398 | }; | |
10399 | ||
10400 | ||
10401 | void | |
de688825 | 10402 | perfmon_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
47dd066d WC |
10403 | { |
10404 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
10405 | ||
10406 | // Synthesize a function. | |
10407 | functiondecl *fdecl = new functiondecl; | |
10408 | fdecl->tok = e->tok; | |
10409 | embeddedcode *ec = new embeddedcode; | |
10410 | ec->tok = e->tok; | |
10411 | bool lvalue = is_active_lvalue(e); | |
10412 | ||
10413 | if (lvalue ) | |
10414 | throw semantic_error("writes to $counter not permitted"); | |
10415 | ||
10416 | string fname = string("_perfmon_tvar_get") | |
10417 | + "_" + e->base_name.substr(1) | |
10418 | + "_" + lex_cast<string>(counter_number); | |
10419 | ||
10420 | if (e->base_name != "$counter") | |
10421 | throw semantic_error ("target variables not available to perfmon probes"); | |
10422 | ||
af304783 DS |
10423 | if (e->components.size() > 0) |
10424 | { | |
10425 | switch (e->components[0].first) | |
10426 | { | |
10427 | case target_symbol::comp_literal_array_index: | |
10428 | throw semantic_error("perfmon probe '$counter' variable may not be used as array", | |
10429 | e->tok); | |
10430 | break; | |
10431 | case target_symbol::comp_struct_member: | |
10432 | throw semantic_error("perfmon probe '$counter' variable may not be used as a structure", | |
10433 | e->tok); | |
10434 | break; | |
10435 | default: | |
10436 | throw semantic_error ("invalid use of perfmon probe '$counter' variable", | |
10437 | e->tok); | |
10438 | break; | |
10439 | } | |
10440 | } | |
10441 | ||
4baf0e53 | 10442 | ec->code = "THIS->__retvalue = _pfm_pmd_x[" + |
47dd066d WC |
10443 | lex_cast<string>(counter_number) + "].reg_num;"; |
10444 | ec->code += "/* pure */"; | |
10445 | fdecl->name = fname; | |
10446 | fdecl->body = ec; | |
10447 | fdecl->type = pe_long; | |
f76427a2 | 10448 | sess.functions[fdecl->name]=fdecl; |
47dd066d WC |
10449 | |
10450 | // Synthesize a functioncall. | |
10451 | functioncall* n = new functioncall; | |
10452 | n->tok = e->tok; | |
10453 | n->function = fname; | |
10454 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
10455 | ||
4ed05b15 | 10456 | provide (n); |
47dd066d WC |
10457 | } |
10458 | ||
10459 | ||
10460 | enum perfmon_mode | |
10461 | { | |
10462 | perfmon_count, | |
10463 | perfmon_sample | |
10464 | }; | |
10465 | ||
10466 | ||
10467 | struct perfmon_derived_probe: public derived_probe | |
10468 | { | |
10469 | protected: | |
10470 | static unsigned probes_allocated; | |
10471 | ||
10472 | public: | |
10473 | systemtap_session & sess; | |
10474 | string event; | |
10475 | perfmon_mode mode; | |
10476 | ||
10477 | perfmon_derived_probe (probe* p, probe_point* l, systemtap_session &s, | |
10478 | string e, perfmon_mode m); | |
b20febf3 | 10479 | virtual void join_group (systemtap_session& s); |
47dd066d WC |
10480 | }; |
10481 | ||
10482 | ||
b20febf3 | 10483 | struct perfmon_derived_probe_group: public generic_dpg<perfmon_derived_probe> |
47dd066d | 10484 | { |
47dd066d | 10485 | public: |
78f6bba6 FCE |
10486 | void emit_module_decls (systemtap_session&) {} |
10487 | void emit_module_init (systemtap_session&) {} | |
10488 | void emit_module_exit (systemtap_session&) {} | |
47dd066d WC |
10489 | }; |
10490 | ||
10491 | ||
10492 | struct perfmon_builder: public derived_probe_builder | |
10493 | { | |
10494 | perfmon_builder() {} | |
10495 | virtual void build(systemtap_session & sess, | |
10496 | probe * base, | |
10497 | probe_point * location, | |
86bf665e | 10498 | literal_map_t const & parameters, |
47dd066d WC |
10499 | vector<derived_probe *> & finished_results) |
10500 | { | |
10501 | string event; | |
10502 | if (!get_param (parameters, "counter", event)) | |
10503 | throw semantic_error("perfmon requires an event"); | |
10504 | ||
10505 | sess.perfmon++; | |
10506 | ||
4baf0e53 | 10507 | // XXX: need to revise when doing sampling |
47dd066d WC |
10508 | finished_results.push_back(new perfmon_derived_probe(base, location, |
10509 | sess, event, | |
10510 | perfmon_count)); | |
10511 | } | |
10512 | }; | |
10513 | ||
b20febf3 | 10514 | |
47dd066d WC |
10515 | unsigned perfmon_derived_probe::probes_allocated; |
10516 | ||
10517 | perfmon_derived_probe::perfmon_derived_probe (probe* p, probe_point* l, | |
10518 | systemtap_session &s, | |
10519 | string e, perfmon_mode m) | |
10520 | : derived_probe (p, l), sess(s), event(e), mode(m) | |
10521 | { | |
10522 | ++probes_allocated; | |
10523 | ||
de688825 JS |
10524 | // Now expand the local variables in the probe body |
10525 | perfmon_var_expanding_visitor v (sess, probes_allocated-1); | |
10526 | this->body = v.require (this->body); | |
47dd066d WC |
10527 | |
10528 | if (sess.verbose > 1) | |
10529 | clog << "perfmon-based probe" << endl; | |
10530 | } | |
10531 | ||
10532 | ||
10533 | void | |
b20febf3 | 10534 | perfmon_derived_probe::join_group (systemtap_session& s) |
47dd066d | 10535 | { |
b20febf3 FCE |
10536 | throw semantic_error ("incomplete", this->tok); |
10537 | ||
10538 | if (! s.perfmon_derived_probes) | |
10539 | s.perfmon_derived_probes = new perfmon_derived_probe_group (); | |
10540 | s.perfmon_derived_probes->enroll (this); | |
47dd066d WC |
10541 | } |
10542 | ||
10543 | ||
b20febf3 | 10544 | #if 0 |
47dd066d WC |
10545 | void |
10546 | perfmon_derived_probe::emit_registrations_start (translator_output* o, | |
10547 | unsigned index) | |
10548 | { | |
10549 | for (unsigned i=0; i<locations.size(); i++) | |
10550 | o->newline() << "enter_" << name << "_" << i << " ();"; | |
10551 | } | |
10552 | ||
10553 | ||
10554 | void | |
10555 | perfmon_derived_probe::emit_registrations_end (translator_output * o, | |
10556 | unsigned index) | |
10557 | { | |
10558 | } | |
10559 | ||
10560 | ||
10561 | void | |
10562 | perfmon_derived_probe::emit_deregistrations (translator_output * o) | |
10563 | { | |
10564 | } | |
10565 | ||
10566 | ||
10567 | void | |
10568 | perfmon_derived_probe::emit_probe_entries (translator_output * o) | |
10569 | { | |
10570 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 10571 | // NB: This variable may be multiply (but identically) defined. |
47dd066d WC |
10572 | o->newline() << "static __cacheline_aligned Stat " << "time_" << basest()->name << ";"; |
10573 | o->newline() << "#endif"; | |
10574 | ||
10575 | for (unsigned i=0; i<locations.size(); i++) | |
10576 | { | |
10577 | probe_point *l = locations[i]; | |
10578 | o->newline() << "/* location " << i << ": " << *l << " */"; | |
10579 | o->newline() << "static void enter_" << name << "_" << i << " (void) {"; | |
10580 | ||
10581 | o->indent(1); | |
10582 | o->newline() << "const char* probe_point = " | |
10583 | << lex_cast_qstring(*l) << ";"; | |
10584 | emit_probe_prologue (o, | |
10585 | (mode == perfmon_count ? | |
10586 | "STAP_SESSION_STARTING" : | |
c12d974f FCE |
10587 | "STAP_SESSION_RUNNING"), |
10588 | "probe_point"); | |
47dd066d WC |
10589 | |
10590 | // NB: locals are initialized by probe function itself | |
10591 | o->newline() << name << " (c);"; | |
10592 | ||
10593 | emit_probe_epilogue (o); | |
10594 | ||
10595 | o->newline(-1) << "}\n"; | |
10596 | } | |
10597 | } | |
b20febf3 | 10598 | #endif |
47dd066d WC |
10599 | |
10600 | ||
b20febf3 | 10601 | #if 0 |
47dd066d WC |
10602 | void no_pfm_event_error (string s) |
10603 | { | |
10604 | string msg(string("Cannot find event:" + s)); | |
10605 | throw semantic_error(msg); | |
10606 | } | |
10607 | ||
10608 | ||
10609 | void no_pfm_mask_error (string s) | |
10610 | { | |
10611 | string msg(string("Cannot find mask:" + s)); | |
10612 | throw semantic_error(msg); | |
10613 | } | |
10614 | ||
10615 | ||
10616 | void | |
10617 | split(const string& s, vector<string>& v, const string & separator) | |
10618 | { | |
10619 | string::size_type last_pos = s.find_first_not_of(separator, 0); | |
10620 | string::size_type pos = s.find_first_of(separator, last_pos); | |
10621 | ||
10622 | while (string::npos != pos || string::npos != last_pos) { | |
10623 | v.push_back(s.substr(last_pos, pos - last_pos)); | |
10624 | last_pos = s.find_first_not_of(separator, pos); | |
10625 | pos = s.find_first_of(separator, last_pos); | |
10626 | } | |
10627 | } | |
10628 | ||
10629 | ||
10630 | void | |
10631 | perfmon_derived_probe_group::emit_probes (translator_output* op, unparser* up) | |
10632 | { | |
10633 | for (unsigned i=0; i < probes.size(); i++) | |
10634 | { | |
10635 | op->newline (); | |
10636 | up->emit_probe (probes[i]); | |
10637 | } | |
10638 | } | |
10639 | ||
10640 | ||
10641 | void | |
10642 | perfmon_derived_probe_group::emit_module_init (translator_output* o) | |
10643 | { | |
10644 | int ret; | |
10645 | pfmlib_input_param_t inp; | |
10646 | pfmlib_output_param_t outp; | |
10647 | pfarg_pmd_t pd[PFMLIB_MAX_PMDS]; | |
10648 | pfarg_pmc_t pc[PFMLIB_MAX_PMCS]; | |
10649 | pfarg_ctx_t ctx; | |
10650 | pfarg_load_t load_args; | |
10651 | pfmlib_options_t pfmlib_options; | |
10652 | unsigned int max_counters; | |
10653 | ||
10654 | if ( probes.size() == 0) | |
10655 | return; | |
10656 | ret = pfm_initialize(); | |
10657 | if (ret != PFMLIB_SUCCESS) | |
10658 | throw semantic_error("Unable to generate performance monitoring events (no libpfm)"); | |
10659 | ||
10660 | pfm_get_num_counters(&max_counters); | |
10661 | ||
10662 | memset(&pfmlib_options, 0, sizeof(pfmlib_options)); | |
10663 | pfmlib_options.pfm_debug = 0; /* set to 1 for debug */ | |
10664 | pfmlib_options.pfm_verbose = 0; /* set to 1 for debug */ | |
10665 | pfm_set_options(&pfmlib_options); | |
10666 | ||
10667 | memset(pd, 0, sizeof(pd)); | |
10668 | memset(pc, 0, sizeof(pc)); | |
10669 | memset(&ctx, 0, sizeof(ctx)); | |
10670 | memset(&load_args, 0, sizeof(load_args)); | |
10671 | ||
10672 | /* | |
10673 | * prepare parameters to library. | |
10674 | */ | |
10675 | memset(&inp,0, sizeof(inp)); | |
10676 | memset(&outp,0, sizeof(outp)); | |
10677 | ||
10678 | /* figure out the events */ | |
10679 | for (unsigned i=0; i<probes.size(); ++i) | |
10680 | { | |
10681 | if (probes[i]->event == "cycles") { | |
10682 | if (pfm_get_cycle_event( &inp.pfp_events[i].event) != PFMLIB_SUCCESS) | |
10683 | no_pfm_event_error(probes[i]->event); | |
10684 | } else if (probes[i]->event == "instructions") { | |
10685 | if (pfm_get_inst_retired_event( &inp.pfp_events[i].event) != | |
10686 | PFMLIB_SUCCESS) | |
10687 | no_pfm_event_error(probes[i]->event); | |
10688 | } else { | |
10689 | unsigned int event_id = 0; | |
10690 | unsigned int mask_id = 0; | |
10691 | vector<string> event_spec; | |
10692 | split(probes[i]->event, event_spec, ":"); | |
10693 | int num = event_spec.size(); | |
10694 | int masks = num - 1; | |
10695 | ||
10696 | if (num == 0) | |
10697 | throw semantic_error("No events found"); | |
10698 | ||
10699 | /* setup event */ | |
10700 | if (pfm_find_event(event_spec[0].c_str(), &event_id) != PFMLIB_SUCCESS) | |
10701 | no_pfm_event_error(event_spec[0]); | |
10702 | inp.pfp_events[i].event = event_id; | |
10703 | ||
10704 | /* set up masks */ | |
10705 | if (masks > PFMLIB_MAX_MASKS_PER_EVENT) | |
10706 | throw semantic_error("Too many unit masks specified"); | |
10707 | ||
10708 | for (int j=0; j < masks; j++) { | |
10709 | if (pfm_find_event_mask(event_id, event_spec[j+1].c_str(), | |
10710 | &mask_id) != PFMLIB_SUCCESS) | |
10711 | no_pfm_mask_error(string(event_spec[j+1])); | |
10712 | inp.pfp_events[i].unit_masks[j] = mask_id; | |
10713 | } | |
10714 | inp.pfp_events[i].num_masks = masks; | |
10715 | } | |
10716 | } | |
10717 | ||
10718 | /* number of counters in use */ | |
10719 | inp.pfp_event_count = probes.size(); | |
10720 | ||
10721 | // XXX: no elimination of duplicated counters | |
10722 | if (inp.pfp_event_count>max_counters) | |
10723 | throw semantic_error("Too many performance monitoring events."); | |
10724 | ||
10725 | /* count events both in kernel and user-space */ | |
10726 | inp.pfp_dfl_plm = PFM_PLM0 | PFM_PLM3; | |
10727 | ||
4baf0e53 | 10728 | /* XXX: some cases a perfmon register might be used of watch dog |
47dd066d WC |
10729 | this code doesn't handle that case */ |
10730 | ||
10731 | /* figure out the pmcs for the events */ | |
10732 | if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS) | |
10733 | throw semantic_error("Cannot configure events"); | |
10734 | ||
10735 | for (unsigned i=0; i < outp.pfp_pmc_count; i++) { | |
10736 | pc[i].reg_num = outp.pfp_pmcs[i].reg_num; | |
10737 | pc[i].reg_value = outp.pfp_pmcs[i].reg_value; | |
10738 | } | |
10739 | ||
10740 | /* | |
10741 | * There could be more pmc settings than pmd. | |
10742 | * Figure out the actual pmds to use. | |
10743 | */ | |
10744 | for (unsigned i=0, j=0; i < inp.pfp_event_count; i++) { | |
10745 | pd[i].reg_num = outp.pfp_pmcs[j].reg_pmd_num; | |
10746 | for(; j < outp.pfp_pmc_count; j++) | |
10747 | if (outp.pfp_pmcs[j].reg_evt_idx != i) break; | |
10748 | } | |
10749 | ||
10750 | // Output the be probes create function | |
10751 | o->newline() << "static int register_perfmon_probes (void) {"; | |
10752 | o->newline(1) << "int rc = 0;"; | |
10753 | ||
10754 | o->newline() << "/* data for perfmon */"; | |
10755 | o->newline() << "static int _pfm_num_pmc = " << outp.pfp_pmc_count << ";"; | |
10756 | o->newline() << "static struct pfarg_pmc _pfm_pmc[" << outp.pfp_pmc_count | |
10757 | << "] = {"; | |
10758 | /* output the needed bits for pmc here */ | |
10759 | for (unsigned i=0; i < outp.pfp_pmc_count; i++) { | |
10760 | o->newline() << "{.reg_num=" << pc[i].reg_num << ", " | |
10761 | << ".reg_value=" << lex_cast_hex<string>(pc[i].reg_value) | |
10762 | << "},"; | |
10763 | } | |
10764 | ||
10765 | o->newline() << "};"; | |
10766 | o->newline() << "static int _pfm_num_pmd = " << inp.pfp_event_count << ";"; | |
10767 | o->newline() << "static struct pfarg_pmd _pfm_pmd[" << inp.pfp_event_count | |
10768 | << "] = {"; | |
10769 | /* output the needed bits for pmd here */ | |
10770 | for (unsigned i=0; i < inp.pfp_event_count; i++) { | |
10771 | o->newline() << "{.reg_num=" << pd[i].reg_num << ", " | |
10772 | << ".reg_value=" << pd[i].reg_value << "},"; | |
10773 | } | |
10774 | o->newline() << "};"; | |
10775 | o->newline(); | |
10776 | ||
10777 | o->newline() << "_pfm_pmc_x=_pfm_pmc;"; | |
10778 | o->newline() << "_pfm_num_pmc_x=_pfm_num_pmc;"; | |
10779 | o->newline() << "_pfm_pmd_x=_pfm_pmd;"; | |
10780 | o->newline() << "_pfm_num_pmd_x=_pfm_num_pmd;"; | |
10781 | ||
10782 | // call all the function bodies associated with perfcounters | |
10783 | for (unsigned i=0; i < probes.size (); i++) | |
10784 | probes[i]->emit_registrations_start (o,i); | |
10785 | ||
10786 | /* generate call to turn on instrumentation */ | |
10787 | o->newline() << "_pfm_context.ctx_flags |= PFM_FL_SYSTEM_WIDE;"; | |
10788 | o->newline() << "rc = rc || _stp_perfmon_setup(&_pfm_desc, &_pfm_context,"; | |
10789 | o->newline(1) << "_pfm_pmc, _pfm_num_pmc,"; | |
10790 | o->newline() << "_pfm_pmd, _pfm_num_pmd);"; | |
10791 | o->newline(-1); | |
10792 | ||
10793 | o->newline() << "return rc;"; | |
10794 | o->newline(-1) << "}\n"; | |
10795 | ||
10796 | // Output the be probes destroy function | |
10797 | o->newline() << "static void unregister_perfmon_probes (void) {"; | |
10798 | o->newline(1) << "_stp_perfmon_shutdown(_pfm_desc);"; | |
10799 | o->newline(-1) << "}\n"; | |
10800 | } | |
b20febf3 | 10801 | #endif |
47dd066d | 10802 | |
47dd066d | 10803 | |
b55bc428 | 10804 | // ------------------------------------------------------------------------ |
bd2b1e68 | 10805 | // Standard tapset registry. |
b55bc428 FCE |
10806 | // ------------------------------------------------------------------------ |
10807 | ||
7a053d3b | 10808 | void |
f8220a7b | 10809 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 10810 | { |
12b21830 DS |
10811 | s.pattern_root->bind(TOK_BEGIN)->bind(new be_builder(BEGIN)); |
10812 | s.pattern_root->bind_num(TOK_BEGIN)->bind(new be_builder(BEGIN)); | |
10813 | s.pattern_root->bind(TOK_END)->bind(new be_builder(END)); | |
10814 | s.pattern_root->bind_num(TOK_END)->bind(new be_builder(END)); | |
10815 | s.pattern_root->bind(TOK_ERROR)->bind(new be_builder(ERROR)); | |
10816 | s.pattern_root->bind_num(TOK_ERROR)->bind(new be_builder(ERROR)); | |
16e8f21f | 10817 | |
12b21830 | 10818 | s.pattern_root->bind(TOK_NEVER)->bind(new never_builder()); |
6e3347a9 | 10819 | |
c4ce66a1 | 10820 | timer_builder::register_patterns(s); |
12b21830 DS |
10821 | s.pattern_root->bind(TOK_TIMER)->bind("profile")->bind(new profile_builder()); |
10822 | s.pattern_root->bind("perfmon")->bind_str("counter") | |
10823 | ->bind(new perfmon_builder()); | |
b98a8d73 | 10824 | |
7a24d422 | 10825 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 10826 | dwarf_derived_probe::register_patterns(s); |
30a279be | 10827 | |
888af770 FCE |
10828 | // XXX: user-space starter set |
10829 | s.pattern_root->bind_num(TOK_PROCESS) | |
10830 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
10831 | ->bind(new uprobe_builder ()); | |
10832 | s.pattern_root->bind_num(TOK_PROCESS) | |
10833 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
10834 | ->bind(new uprobe_builder ()); | |
10835 | ||
935447c8 | 10836 | // utrace user-space probes |
eff6ac72 DS |
10837 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_BEGIN) |
10838 | ->bind(new utrace_builder ()); | |
10839 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_BEGIN) | |
10840 | ->bind(new utrace_builder ()); | |
986e98de DS |
10841 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_BEGIN) |
10842 | ->bind(new utrace_builder ()); | |
eff6ac72 | 10843 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_END) |
935447c8 | 10844 | ->bind(new utrace_builder ()); |
eff6ac72 | 10845 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_END) |
935447c8 | 10846 | ->bind(new utrace_builder ()); |
986e98de DS |
10847 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_END) |
10848 | ->bind(new utrace_builder ()); | |
eff6ac72 | 10849 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
159cb109 | 10850 | ->bind(new utrace_builder ()); |
eff6ac72 DS |
10851 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
10852 | ->bind(new utrace_builder ()); | |
986e98de DS |
10853 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
10854 | ->bind(new utrace_builder ()); | |
eff6ac72 DS |
10855 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) |
10856 | ->bind(new utrace_builder ()); | |
10857 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) | |
159cb109 | 10858 | ->bind(new utrace_builder ()); |
986e98de DS |
10859 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) |
10860 | ->bind(new utrace_builder ()); | |
12b21830 | 10861 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_SYSCALL) |
935447c8 | 10862 | ->bind(new utrace_builder ()); |
12b21830 | 10863 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_SYSCALL) |
935447c8 | 10864 | ->bind(new utrace_builder ()); |
480f38d3 DS |
10865 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_SYSCALL) |
10866 | ->bind(new utrace_builder ()); | |
12b21830 | 10867 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
935447c8 | 10868 | ->bind(new utrace_builder ()); |
12b21830 | 10869 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
935447c8 | 10870 | ->bind(new utrace_builder ()); |
480f38d3 DS |
10871 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
10872 | ->bind(new utrace_builder ()); | |
10873 | ||
10874 | // itrace user-space probes | |
0afb7073 FCE |
10875 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_INSN) |
10876 | ->bind(new itrace_builder ()); | |
10877 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_INSN) | |
10878 | ->bind(new itrace_builder ()); | |
10879 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_INSN)->bind(TOK_BLOCK) | |
480f38d3 | 10880 | ->bind(new itrace_builder ()); |
0afb7073 | 10881 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_INSN)->bind(TOK_BLOCK) |
480f38d3 | 10882 | ->bind(new itrace_builder ()); |
935447c8 | 10883 | |
f781f849 | 10884 | // marker-based parts |
12b21830 DS |
10885 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_MARK) |
10886 | ->bind(new mark_builder()); | |
10887 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_MARK)->bind_str(TOK_FORMAT) | |
eb973c2a | 10888 | ->bind(new mark_builder()); |
f781f849 | 10889 | |
0a6f5a3f JS |
10890 | // kernel tracepoint probes |
10891 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
10892 | ->bind(new tracepoint_builder()); | |
10893 | ||
ce82316f | 10894 | // procfs parts |
12b21830 DS |
10895 | s.pattern_root->bind(TOK_PROCFS)->bind(TOK_READ)->bind(new procfs_builder()); |
10896 | s.pattern_root->bind_str(TOK_PROCFS)->bind(TOK_READ) | |
10897 | ->bind(new procfs_builder()); | |
10898 | s.pattern_root->bind(TOK_PROCFS)->bind(TOK_WRITE)->bind(new procfs_builder()); | |
10899 | s.pattern_root->bind_str(TOK_PROCFS)->bind(TOK_WRITE) | |
10900 | ->bind(new procfs_builder()); | |
b55bc428 | 10901 | } |
dc38c0ae DS |
10902 | |
10903 | ||
b20febf3 FCE |
10904 | vector<derived_probe_group*> |
10905 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 10906 | { |
b20febf3 FCE |
10907 | vector<derived_probe_group*> g; |
10908 | #define DOONE(x) if (s. x##_derived_probes) g.push_back (s. x##_derived_probes) | |
ab655cf8 DS |
10909 | |
10910 | // Note that order *is* important here. We want to make sure we | |
10911 | // register (actually run) begin probes before any other probe type | |
10912 | // is run. Similarly, when unregistering probes, we want to | |
10913 | // unregister (actually run) end probes after every other probe type | |
10914 | // has be unregistered. To do the latter, | |
10915 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
10916 | DOONE(be); |
10917 | DOONE(dwarf); | |
888af770 | 10918 | DOONE(uprobe); |
b20febf3 FCE |
10919 | DOONE(timer); |
10920 | DOONE(profile); | |
10921 | DOONE(mark); | |
0a6f5a3f | 10922 | DOONE(tracepoint); |
b20febf3 FCE |
10923 | DOONE(hrtimer); |
10924 | DOONE(perfmon); | |
ce82316f | 10925 | DOONE(procfs); |
935447c8 DS |
10926 | |
10927 | // Another "order is important" item. We want to make sure we | |
10928 | // "register" the dummy task_finder probe group after all probe | |
10929 | // groups that use the task_finder. | |
10930 | DOONE(utrace); | |
a96d1db0 | 10931 | DOONE(itrace); |
935447c8 | 10932 | DOONE(task_finder); |
b20febf3 FCE |
10933 | #undef DOONE |
10934 | return g; | |
46b84a80 | 10935 | } |
73267b89 JS |
10936 | |
10937 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |