]>
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 FCE |
605 | struct stringhash { |
606 | size_t operator() (const string& s) const { hash<const char*> h; return h(s.c_str()); } | |
607 | }; | |
608 | ||
609 | typedef hash_map<string,Dwarf_Die,stringhash> cu_function_cache_t; | |
610 | typedef hash_map<string,cu_function_cache_t*,stringhash> mod_cu_function_cache_t; // module:cu -> function -> die | |
3bf6ac45 | 611 | #endif |
5f0a03a6 JK |
612 | |
613 | struct | |
614 | symbol_table | |
615 | { | |
616 | module_info *mod_info; // associated module | |
617 | map<string, func_info*> map_by_name; | |
618 | vector<func_info*> list_by_addr; | |
2e67a43b TM |
619 | typedef vector<func_info*>::iterator iterator_t; |
620 | typedef pair<iterator_t, iterator_t> range_t; | |
46f7b6be JK |
621 | #ifdef __powerpc__ |
622 | GElf_Word opd_section; | |
623 | #endif | |
2e67a43b TM |
624 | // add_symbol doesn't leave symbol table in order; call |
625 | // symbol_table::sort() when done adding symbols. | |
ab91b232 JK |
626 | void add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
627 | Dwarf_Addr *high_addr); | |
2e67a43b | 628 | void sort(); |
5f0a03a6 JK |
629 | enum info_status read_symbols(FILE *f, const string& path); |
630 | enum info_status read_from_elf_file(const string& path); | |
631 | enum info_status read_from_text_file(const string& path); | |
632 | enum info_status get_from_elf(); | |
46f7b6be JK |
633 | void prepare_section_rejection(Dwfl_Module *mod); |
634 | bool reject_section(GElf_Word section); | |
5f0a03a6 | 635 | void mark_dwarf_redundancies(dwflpp *dw); |
ab91b232 | 636 | void purge_syscall_stubs(); |
5f0a03a6 JK |
637 | func_info *lookup_symbol(const string& name); |
638 | Dwarf_Addr lookup_symbol_address(const string& name); | |
639 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
5f0a03a6 JK |
640 | |
641 | symbol_table(module_info *mi) : mod_info(mi) {} | |
642 | ~symbol_table(); | |
643 | }; | |
644 | ||
645 | static bool null_die(Dwarf_Die *die) | |
646 | { | |
647 | static Dwarf_Die null = { 0 }; | |
648 | return (!die || !memcmp(die, &null, sizeof(null))); | |
649 | } | |
650 | ||
7e1279ea FCE |
651 | static int |
652 | query_cu (Dwarf_Die * cudie, void * arg); | |
59ff2773 FCE |
653 | |
654 | ||
bd2b1e68 GH |
655 | // Helper for dealing with selected portions of libdwfl in a more readable |
656 | // fashion, and with specific cleanup / checking / logging options. | |
657 | ||
91eefb1c GH |
658 | static const char * |
659 | dwarf_diename_integrate (Dwarf_Die *die) | |
660 | { | |
661 | Dwarf_Attribute attr_mem; | |
662 | return dwarf_formstring (dwarf_attr_integrate (die, DW_AT_name, &attr_mem)); | |
663 | } | |
664 | ||
879eb9e9 SC |
665 | enum line_t { ABSOLUTE, RELATIVE, RANGE, WILDCARD }; |
666 | ||
3e961ba6 JB |
667 | typedef vector<inline_instance_info> inline_instance_map_t; |
668 | typedef vector<func_info> func_info_map_t; | |
669 | ||
b20febf3 | 670 | struct dwflpp |
bd2b1e68 | 671 | { |
5227f1ea | 672 | systemtap_session & sess; |
bd2b1e68 GH |
673 | Dwfl * dwfl; |
674 | ||
675 | // These are "current" values we focus on. | |
676 | Dwfl_Module * module; | |
677 | Dwarf * module_dwarf; | |
678 | Dwarf_Addr module_bias; | |
5f0a03a6 | 679 | module_info * mod_info; |
50e0d793 GH |
680 | |
681 | // These describe the current module's PC address range | |
682 | Dwarf_Addr module_start; | |
683 | Dwarf_Addr module_end; | |
684 | ||
bd2b1e68 | 685 | Dwarf_Die * cu; |
20e4a32c | 686 | Dwarf_Die * function; |
bd2b1e68 GH |
687 | |
688 | string module_name; | |
689 | string cu_name; | |
690 | string function_name; | |
691 | ||
7a053d3b | 692 | string const default_name(char const * in, |
78f6bba6 | 693 | char const *) |
bd2b1e68 | 694 | { |
7a053d3b | 695 | if (in) |
bd2b1e68 | 696 | return in; |
a229fcd7 | 697 | return string(""); |
bd2b1e68 GH |
698 | } |
699 | ||
50e0d793 | 700 | |
5f0a03a6 | 701 | void get_module_dwarf(bool required = false, bool report = true) |
5227f1ea | 702 | { |
91af0778 FCE |
703 | module_dwarf = dwfl_module_getdwarf(module, &module_bias); |
704 | mod_info->dwarf_status = (module_dwarf ? info_present : info_absent); | |
5f0a03a6 JK |
705 | if (!module_dwarf && report) |
706 | { | |
707 | string msg = "cannot find "; | |
708 | if (module_name == "") | |
709 | msg += "kernel"; | |
710 | else | |
711 | msg += string("module ") + module_name; | |
712 | msg += " debuginfo"; | |
713 | ||
714 | int i = dwfl_errno(); | |
715 | if (i) | |
716 | msg += string(": ") + dwfl_errmsg (i); | |
717 | ||
718 | if (required) | |
719 | throw semantic_error (msg); | |
720 | else | |
721 | cerr << "WARNING: " << msg << "\n"; | |
0ce64fb8 | 722 | } |
5227f1ea GH |
723 | } |
724 | ||
5f0a03a6 | 725 | void focus_on_module(Dwfl_Module * m, module_info * mi) |
bd2b1e68 | 726 | { |
bd2b1e68 | 727 | module = m; |
5f0a03a6 JK |
728 | mod_info = mi; |
729 | if (m) | |
730 | { | |
731 | module_name = default_name(dwfl_module_info(module, NULL, | |
50e0d793 | 732 | &module_start, &module_end, |
bd2b1e68 GH |
733 | NULL, NULL, |
734 | NULL, NULL), | |
735 | "module"); | |
5f0a03a6 JK |
736 | } |
737 | else | |
738 | { | |
739 | assert(mi && mi->name && mi->name == TOK_KERNEL); | |
740 | module_name = mi->name; | |
741 | module_start = 0; | |
742 | module_end = 0; | |
743 | module_bias = mi->bias; | |
744 | } | |
50e0d793 GH |
745 | |
746 | // Reset existing pointers and names | |
747 | ||
748 | module_dwarf = NULL; | |
749 | ||
a229fcd7 | 750 | cu_name.clear(); |
50e0d793 GH |
751 | cu = NULL; |
752 | ||
a229fcd7 | 753 | function_name.clear(); |
50e0d793 | 754 | function = NULL; |
bd2b1e68 GH |
755 | } |
756 | ||
50e0d793 | 757 | |
bd2b1e68 GH |
758 | void focus_on_cu(Dwarf_Die * c) |
759 | { | |
760 | assert(c); | |
50e0d793 GH |
761 | assert(module); |
762 | ||
bd2b1e68 | 763 | cu = c; |
50e0d793 GH |
764 | cu_name = default_name(dwarf_diename(c), "CU"); |
765 | ||
766 | // Reset existing pointers and names | |
a229fcd7 | 767 | function_name.clear(); |
50e0d793 | 768 | function = NULL; |
bd2b1e68 GH |
769 | } |
770 | ||
50e0d793 | 771 | |
20e4a32c | 772 | void focus_on_function(Dwarf_Die * f) |
bd2b1e68 GH |
773 | { |
774 | assert(f); | |
50e0d793 GH |
775 | assert(module); |
776 | assert(cu); | |
777 | ||
bd2b1e68 | 778 | function = f; |
20e4a32c | 779 | function_name = default_name(dwarf_diename(function), |
bd2b1e68 | 780 | "function"); |
bd2b1e68 GH |
781 | } |
782 | ||
50e0d793 | 783 | |
bd2b1e68 GH |
784 | void focus_on_module_containing_global_address(Dwarf_Addr a) |
785 | { | |
786 | assert(dwfl); | |
50e0d793 | 787 | cu = NULL; |
0ce64fb8 FCE |
788 | Dwfl_Module* mod = dwfl_addrmodule(dwfl, a); |
789 | if (mod) // address could be wildly out of range | |
5f0a03a6 | 790 | focus_on_module(mod, NULL); |
bd2b1e68 GH |
791 | } |
792 | ||
50e0d793 | 793 | |
7e1279ea | 794 | void query_cu_containing_global_address(Dwarf_Addr a, void *arg) |
bd2b1e68 | 795 | { |
bd2b1e68 | 796 | Dwarf_Addr bias; |
50e0d793 | 797 | assert(dwfl); |
5227f1ea | 798 | get_module_dwarf(); |
ab55a5ae FCE |
799 | Dwarf_Die* cudie = dwfl_module_addrdie(module, a, &bias); |
800 | if (cudie) // address could be wildly out of range | |
801 | query_cu (cudie, arg); | |
bd2b1e68 GH |
802 | assert(bias == module_bias); |
803 | } | |
804 | ||
50e0d793 | 805 | |
7e1279ea | 806 | void query_cu_containing_module_address(Dwarf_Addr a, void *arg) |
bd2b1e68 | 807 | { |
7e1279ea | 808 | query_cu_containing_global_address(module_address_to_global(a), arg); |
bd2b1e68 GH |
809 | } |
810 | ||
50e0d793 | 811 | |
bd2b1e68 GH |
812 | Dwarf_Addr module_address_to_global(Dwarf_Addr a) |
813 | { | |
50e0d793 | 814 | assert(dwfl); |
bd2b1e68 | 815 | assert(module); |
5227f1ea | 816 | get_module_dwarf(); |
76e954ca | 817 | if (module_name == TOK_KERNEL || dwfl_module_relocations (module) <= 0) |
c239d28c | 818 | return a; |
50e0d793 | 819 | return a + module_start; |
bd2b1e68 GH |
820 | } |
821 | ||
50e0d793 | 822 | |
bd2b1e68 GH |
823 | Dwarf_Addr global_address_to_module(Dwarf_Addr a) |
824 | { | |
825 | assert(module); | |
5227f1ea | 826 | get_module_dwarf(); |
bd2b1e68 GH |
827 | return a - module_bias; |
828 | } | |
829 | ||
830 | ||
831 | bool module_name_matches(string pattern) | |
832 | { | |
bd2b1e68 | 833 | bool t = (fnmatch(pattern.c_str(), module_name.c_str(), 0) == 0); |
b8da0ad1 | 834 | if (t && sess.verbose>3) |
bd2b1e68 | 835 | clog << "pattern '" << pattern << "' " |
24cb178f | 836 | << "matches " |
db22e55f | 837 | << "module '" << module_name << "'" << "\n"; |
bd2b1e68 GH |
838 | return t; |
839 | } | |
5f0a03a6 JK |
840 | bool name_has_wildcard(string pattern) |
841 | { | |
842 | return (pattern.find('*') != string::npos || | |
843 | pattern.find('?') != string::npos || | |
844 | pattern.find('[') != string::npos); | |
845 | } | |
b8da0ad1 FCE |
846 | bool module_name_final_match(string pattern) |
847 | { | |
848 | // Assume module_name_matches(). Can there be any more matches? | |
849 | // Not unless the pattern is a wildcard, since module names are | |
850 | // presumed unique. | |
5f0a03a6 | 851 | return !name_has_wildcard(pattern); |
b8da0ad1 | 852 | } |
bd2b1e68 | 853 | |
50e0d793 | 854 | |
5f0a03a6 | 855 | bool function_name_matches_pattern(string name, string pattern) |
bd2b1e68 | 856 | { |
5f0a03a6 | 857 | bool t = (fnmatch(pattern.c_str(), name.c_str(), 0) == 0); |
b8da0ad1 | 858 | if (t && sess.verbose>3) |
bd2b1e68 | 859 | clog << "pattern '" << pattern << "' " |
24cb178f | 860 | << "matches " |
5f0a03a6 | 861 | << "function '" << name << "'" << "\n"; |
bd2b1e68 GH |
862 | return t; |
863 | } | |
5f0a03a6 JK |
864 | bool function_name_matches(string pattern) |
865 | { | |
866 | assert(function); | |
867 | return function_name_matches_pattern(function_name, pattern); | |
868 | } | |
275f40a6 FCE |
869 | bool function_name_final_match(string pattern) |
870 | { | |
871 | return module_name_final_match (pattern); | |
872 | } | |
bd2b1e68 | 873 | |
50e0d793 | 874 | |
bd2b1e68 GH |
875 | bool cu_name_matches(string pattern) |
876 | { | |
877 | assert(cu); | |
3213d089 FCE |
878 | |
879 | // PR 5049: implicit * in front of given path pattern. | |
880 | // NB: fnmatch() is used without FNM_PATHNAME. | |
79640c29 | 881 | string prefixed_pattern = string("*/") + pattern; |
3213d089 | 882 | |
79640c29 FCE |
883 | bool t = (fnmatch(pattern.c_str(), cu_name.c_str(), 0) == 0 || |
884 | fnmatch(prefixed_pattern.c_str(), cu_name.c_str(), 0) == 0); | |
b8da0ad1 | 885 | if (t && sess.verbose>3) |
3213d089 | 886 | clog << "pattern '" << prefixed_pattern << "' " |
24cb178f | 887 | << "matches " |
db22e55f | 888 | << "CU '" << cu_name << "'" << "\n"; |
bd2b1e68 GH |
889 | return t; |
890 | } | |
891 | ||
5f0a03a6 | 892 | dwflpp(systemtap_session & session) |
bd2b1e68 | 893 | : |
5f0a03a6 | 894 | sess(session), |
bd2b1e68 GH |
895 | dwfl(NULL), |
896 | module(NULL), | |
897 | module_dwarf(NULL), | |
898 | module_bias(0), | |
5f0a03a6 | 899 | mod_info(NULL), |
50e0d793 GH |
900 | module_start(0), |
901 | module_end(0), | |
bd2b1e68 GH |
902 | cu(NULL), |
903 | function(NULL) | |
5f0a03a6 | 904 | { |
5f0a03a6 | 905 | } |
7a053d3b | 906 | |
50e0d793 | 907 | |
b5e66ada FCE |
908 | // XXX: See also translate.cxx:emit_symbol_data |
909 | ||
7a24d422 | 910 | void setup_kernel(bool debuginfo_needed = true) |
bd2b1e68 | 911 | { |
405b71b8 FCE |
912 | if (! sess.module_cache) |
913 | sess.module_cache = new module_cache (); | |
914 | ||
0dc34322 | 915 | static const char *debuginfo_path_arr = "+:.debug:/usr/lib/debug:build"; |
b5e66ada FCE |
916 | static const char *debuginfo_env_arr = getenv("SYSTEMTAP_DEBUGINFO_PATH"); |
917 | static const char *debuginfo_path = (debuginfo_env_arr ?: debuginfo_path_arr ); | |
b5d77020 | 918 | |
bd2b1e68 GH |
919 | static const Dwfl_Callbacks kernel_callbacks = |
920 | { | |
921 | dwfl_linux_kernel_find_elf, | |
922 | dwfl_standard_find_debuginfo, | |
b20febf3 | 923 | dwfl_offline_section_address, |
b5e66ada | 924 | (char **) & debuginfo_path |
bd2b1e68 GH |
925 | }; |
926 | ||
7a24d422 FCE |
927 | dwfl = dwfl_begin (&kernel_callbacks); |
928 | if (!dwfl) | |
929 | throw semantic_error ("cannot open dwfl"); | |
930 | dwfl_report_begin (dwfl); | |
06aca46a | 931 | |
b5e66ada FCE |
932 | // We have a problem with -r REVISION vs -r BUILDDIR here. If |
933 | // we're running against a fedora/rhel style kernel-debuginfo | |
934 | // tree, s.kernel_build_tree is not the place where the unstripped | |
935 | // vmlinux will be installed. Rather, it's over yonder at | |
936 | // /usr/lib/debug/lib/modules/$REVISION/. It seems that there is | |
937 | // no way to set the dwfl_callback.debuginfo_path and always | |
938 | // passs the plain kernel_release here. So instead we have to | |
939 | // hard-code this magic here. | |
940 | string elfutils_kernel_path; | |
941 | if (sess.kernel_build_tree == string("/lib/modules/" + sess.kernel_release + "/build")) | |
942 | elfutils_kernel_path = sess.kernel_release; | |
943 | else | |
944 | elfutils_kernel_path = sess.kernel_build_tree; | |
945 | ||
7a24d422 | 946 | int rc = dwfl_linux_kernel_report_offline (dwfl, |
b5e66ada | 947 | elfutils_kernel_path.c_str(), |
91af0778 | 948 | NULL); |
06aca46a | 949 | |
7a24d422 | 950 | if (debuginfo_needed) |
b5e66ada FCE |
951 | dwfl_assert (string("missing ") + sess.architecture + |
952 | string(" kernel/module debuginfo under '") + | |
953 | sess.kernel_build_tree + string("'"), | |
7a24d422 | 954 | rc); |
06aca46a | 955 | |
7a24d422 FCE |
956 | // XXX: it would be nice if we could do a single |
957 | // ..._report_offline call for an entire systemtap script, so | |
958 | // that a selection predicate would filter out modules outside | |
959 | // the union of all the requested wildcards. But we build | |
960 | // derived_probes one-by-one and we don't have lookahead. | |
961 | // PR 3498. | |
06aca46a | 962 | |
7a24d422 FCE |
963 | // XXX: a special case: if we have only kernel.* probe points, |
964 | // we shouldn't waste time looking for module debug-info (and | |
965 | // vice versa). | |
06aca46a | 966 | |
7a24d422 FCE |
967 | // NB: the result of an _offline call is the assignment of |
968 | // virtualized addresses to relocatable objects such as | |
969 | // modules. These have to be converted to real addresses at | |
970 | // run time. See the dwarf_derived_probe ctor and its caller. | |
971 | ||
972 | dwfl_assert ("dwfl_report_end", dwfl_report_end(dwfl, NULL, NULL)); | |
973 | } | |
974 | ||
975 | void setup_user(string module_name, bool debuginfo_needed = true) | |
976 | { | |
dd22832a JS |
977 | if (! sess.module_cache) |
978 | sess.module_cache = new module_cache (); | |
979 | ||
0dc34322 | 980 | static const char *debuginfo_path_arr = "+:.debug:/usr/lib/debug:build"; |
b5e66ada FCE |
981 | static const char *debuginfo_env_arr = getenv("SYSTEMTAP_DEBUGINFO_PATH"); |
982 | // NB: kernel_build_tree doesn't enter into this, as it's for | |
983 | // kernel-side modules only. | |
984 | static const char *debuginfo_path = (debuginfo_env_arr ?: debuginfo_path_arr); | |
7a24d422 FCE |
985 | |
986 | static const Dwfl_Callbacks user_callbacks = | |
bd2b1e68 | 987 | { |
7a24d422 FCE |
988 | NULL, /* dwfl_linux_kernel_find_elf, */ |
989 | dwfl_standard_find_debuginfo, | |
990 | dwfl_offline_section_address, | |
b5e66ada | 991 | (char **) & debuginfo_path |
7a24d422 | 992 | }; |
b20febf3 | 993 | |
7a24d422 FCE |
994 | dwfl = dwfl_begin (&user_callbacks); |
995 | if (!dwfl) | |
996 | throw semantic_error ("cannot open dwfl"); | |
997 | dwfl_report_begin (dwfl); | |
998 | ||
7a24d422 | 999 | // XXX: should support buildid-based naming |
405b71b8 | 1000 | |
7a24d422 FCE |
1001 | Dwfl_Module *mod = dwfl_report_offline (dwfl, |
1002 | module_name.c_str(), | |
1003 | module_name.c_str(), | |
1004 | -1); | |
1005 | // XXX: save mod! | |
5f0a03a6 | 1006 | |
7a24d422 FCE |
1007 | if (debuginfo_needed) |
1008 | dwfl_assert (string("missing process ") + | |
1009 | module_name + | |
1010 | string(" ") + | |
1011 | sess.architecture + | |
1012 | string(" debuginfo"), | |
1013 | mod); | |
b20febf3 | 1014 | |
f28a8c28 SC |
1015 | if (!module) |
1016 | module = mod; | |
1017 | ||
7a24d422 FCE |
1018 | // NB: the result of an _offline call is the assignment of |
1019 | // virtualized addresses to relocatable objects such as | |
1020 | // modules. These have to be converted to real addresses at | |
1021 | // run time. See the dwarf_derived_probe ctor and its caller. | |
bd2b1e68 | 1022 | |
7e1279ea | 1023 | dwfl_assert ("dwfl_report_end", dwfl_report_end(dwfl, NULL, NULL)); |
bd2b1e68 GH |
1024 | } |
1025 | ||
b8da0ad1 FCE |
1026 | |
1027 | ||
1028 | // ----------------------------------------------------------------- | |
1029 | ||
91af0778 | 1030 | void iterate_over_modules(int (* callback)(Dwfl_Module *, void **, |
5f0a03a6 JK |
1031 | const char *, Dwarf_Addr, |
1032 | void *), | |
c4ce66a1 | 1033 | base_query *data) |
5f0a03a6 | 1034 | { |
91af0778 FCE |
1035 | ptrdiff_t off = 0; |
1036 | do | |
b8da0ad1 | 1037 | { |
49abf162 | 1038 | if (pending_interrupts) return; |
91af0778 | 1039 | off = dwfl_getmodules (dwfl, callback, data, off); |
bd2b1e68 | 1040 | } |
91af0778 FCE |
1041 | while (off > 0); |
1042 | dwfl_assert("dwfl_getmodules", off == 0); | |
6f4c1275 FCE |
1043 | |
1044 | // PR6864 XXX: For dwarfless case (if .../vmlinux is missing), then the | |
1045 | // "kernel" module is not reported in the loop above. However, we | |
1046 | // may be able to make do with symbol table data. | |
bd2b1e68 GH |
1047 | } |
1048 | ||
91af0778 | 1049 | |
5f0a03a6 | 1050 | // Defined after dwarf_query |
c4ce66a1 | 1051 | void query_modules(base_query *q); |
7e1279ea | 1052 | |
b8da0ad1 FCE |
1053 | |
1054 | // ----------------------------------------------------------------- | |
1055 | ||
1056 | typedef map<Dwarf*, vector<Dwarf_Die>*> module_cu_cache_t; | |
1057 | module_cu_cache_t module_cu_cache; | |
1058 | ||
7a053d3b | 1059 | void iterate_over_cus (int (*callback)(Dwarf_Die * die, void * arg), |
bd2b1e68 GH |
1060 | void * data) |
1061 | { | |
0ce64fb8 | 1062 | get_module_dwarf(false); |
b8da0ad1 FCE |
1063 | Dwarf *dw = module_dwarf; |
1064 | if (!dw) return; | |
5227f1ea | 1065 | |
b8da0ad1 FCE |
1066 | vector<Dwarf_Die>* v = module_cu_cache[dw]; |
1067 | if (v == 0) | |
1068 | { | |
1069 | v = new vector<Dwarf_Die>; | |
1070 | module_cu_cache[dw] = v; | |
bd2b1e68 | 1071 | |
b8da0ad1 FCE |
1072 | Dwarf_Off off = 0; |
1073 | size_t cuhl; | |
1074 | Dwarf_Off noff; | |
1075 | while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) | |
1076 | { | |
49abf162 | 1077 | if (pending_interrupts) return; |
b8da0ad1 FCE |
1078 | Dwarf_Die die_mem; |
1079 | Dwarf_Die *die; | |
1080 | die = dwarf_offdie (dw, off + cuhl, &die_mem); | |
1081 | v->push_back (*die); /* copy */ | |
1082 | off = noff; | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | for (unsigned i = 0; i < v->size(); i++) | |
7a053d3b | 1087 | { |
49abf162 | 1088 | if (pending_interrupts) return; |
b8da0ad1 FCE |
1089 | Dwarf_Die die = v->at(i); |
1090 | int rc = (*callback)(& die, data); | |
1091 | if (rc != DWARF_CB_OK) break; | |
bd2b1e68 GH |
1092 | } |
1093 | } | |
1094 | ||
bd2b1e68 | 1095 | |
b8da0ad1 FCE |
1096 | // ----------------------------------------------------------------- |
1097 | ||
7e1279ea | 1098 | bool func_is_inline() |
bd2b1e68 | 1099 | { |
7e1279ea FCE |
1100 | assert (function); |
1101 | return dwarf_func_inline (function) != 0; | |
bd2b1e68 GH |
1102 | } |
1103 | ||
b8da0ad1 FCE |
1104 | |
1105 | typedef map<string, vector<Dwarf_Die>*> cu_inl_function_cache_t; | |
1106 | cu_inl_function_cache_t cu_inl_function_cache; | |
1107 | ||
1108 | static int cu_inl_function_caching_callback (Dwarf_Die* func, void *arg) | |
1109 | { | |
1110 | vector<Dwarf_Die>* v = static_cast<vector<Dwarf_Die>*>(arg); | |
1111 | v->push_back (* func); | |
1112 | return DWARF_CB_OK; | |
1113 | } | |
1114 | ||
7e1279ea FCE |
1115 | void iterate_over_inline_instances (int (* callback)(Dwarf_Die * die, void * arg), |
1116 | void * data) | |
bd2b1e68 | 1117 | { |
7e1279ea FCE |
1118 | assert (function); |
1119 | assert (func_is_inline ()); | |
b8da0ad1 FCE |
1120 | |
1121 | string key = module_name + ":" + cu_name + ":" + function_name; | |
1122 | vector<Dwarf_Die>* v = cu_inl_function_cache[key]; | |
1123 | if (v == 0) | |
1124 | { | |
1125 | v = new vector<Dwarf_Die>; | |
1126 | cu_inl_function_cache[key] = v; | |
1127 | dwarf_func_inline_instances (function, cu_inl_function_caching_callback, v); | |
1128 | } | |
1129 | ||
1130 | for (unsigned i=0; i<v->size(); i++) | |
1131 | { | |
f76427a2 | 1132 | if (pending_interrupts) return; |
b8da0ad1 FCE |
1133 | Dwarf_Die die = v->at(i); |
1134 | int rc = (*callback)(& die, data); | |
1135 | if (rc != DWARF_CB_OK) break; | |
1136 | } | |
4fa7b22b | 1137 | } |
bd2b1e68 | 1138 | |
50e0d793 | 1139 | |
b8da0ad1 FCE |
1140 | // ----------------------------------------------------------------- |
1141 | ||
7bb73781 | 1142 | /* The global alias cache is used to resolve any DIE found in a |
0b8f6579 JB |
1143 | * module that is stubbed out with DW_AT_declaration with a defining |
1144 | * DIE found in a different module. The current assumption is that | |
1145 | * this only applies to structures and unions, which have a global | |
1146 | * namespace (it deliberately only traverses program scope), so this | |
1147 | * cache is indexed by name. If other declaration lookups were | |
1148 | * added to it, it would have to be indexed by name and tag | |
1149 | */ | |
7bb73781 | 1150 | mod_cu_function_cache_t global_alias_cache; |
0b8f6579 JB |
1151 | static int global_alias_caching_callback(Dwarf_Die *die, void *arg) |
1152 | { | |
7bb73781 | 1153 | cu_function_cache_t *cache = static_cast<cu_function_cache_t*>(arg); |
0b8f6579 JB |
1154 | const char *name = dwarf_diename(die); |
1155 | ||
1156 | if (!name) | |
1157 | return DWARF_CB_OK; | |
1158 | ||
1159 | string structure_name = name; | |
1160 | ||
1161 | if (!dwarf_hasattr(die, DW_AT_declaration) && | |
7bb73781 FCE |
1162 | cache->find(structure_name) == cache->end()) |
1163 | (*cache)[structure_name] = *die; | |
0b8f6579 JB |
1164 | |
1165 | return DWARF_CB_OK; | |
1166 | } | |
1167 | ||
c4ce66a1 | 1168 | Dwarf_Die *declaration_resolve(const char *name) |
0b8f6579 | 1169 | { |
0b8f6579 JB |
1170 | if (!name) |
1171 | return NULL; | |
1172 | ||
7bb73781 FCE |
1173 | string key = module_name + ":" + cu_name; |
1174 | cu_function_cache_t *v = global_alias_cache[key]; | |
1175 | if (v == 0) // need to build the cache, just once per encountered module/cu | |
1176 | { | |
1177 | v = new cu_function_cache_t; | |
1178 | global_alias_cache[key] = v; | |
1179 | iterate_over_globals(global_alias_caching_callback, v); | |
1180 | if (sess.verbose > 4) | |
1181 | clog << "global alias cache " << key << " size " << v->size() << endl; | |
1182 | } | |
1183 | ||
1184 | // XXX: it may be desirable to search other modules' declarations | |
1185 | // too, in case a module/shared-library processes a | |
1186 | // forward-declared pointer type only, where the actual definition | |
1187 | // may only be in vmlinux or the application. | |
1188 | ||
1189 | // XXX: it is probably desirable to search other CU's declarations | |
1190 | // in the same module. | |
41c262f3 | 1191 | |
7bb73781 | 1192 | if (v->find(name) == v->end()) |
0b8f6579 JB |
1193 | return NULL; |
1194 | ||
7bb73781 | 1195 | return & ((*v)[name]); |
0b8f6579 JB |
1196 | } |
1197 | ||
6561773f | 1198 | mod_cu_function_cache_t cu_function_cache; |
b8da0ad1 FCE |
1199 | |
1200 | static int cu_function_caching_callback (Dwarf_Die* func, void *arg) | |
1201 | { | |
6561773f | 1202 | cu_function_cache_t* v = static_cast<cu_function_cache_t*>(arg); |
0e68eaaa DS |
1203 | const char *name = dwarf_diename(func); |
1204 | if (!name) | |
1205 | return DWARF_CB_OK; | |
1206 | ||
1207 | string function_name = name; | |
6561773f | 1208 | (*v)[function_name] = * func; |
b8da0ad1 FCE |
1209 | return DWARF_CB_OK; |
1210 | } | |
1211 | ||
2da9cedb JS |
1212 | int iterate_over_functions (int (* callback)(Dwarf_Die * func, base_query * q), |
1213 | base_query * q, const string& function, | |
1214 | bool has_statement_num=false); | |
0b8f6579 JB |
1215 | int iterate_over_globals (int (* callback)(Dwarf_Die *, void *), |
1216 | void * data); | |
1217 | ||
fedd4090 | 1218 | bool has_single_line_record (dwarf_query * q, char const * srcfile, int lineno); |
897820ca | 1219 | |
7e1279ea | 1220 | void iterate_over_srcfile_lines (char const * srcfile, |
879eb9e9 | 1221 | int lines[2], |
897820ca | 1222 | bool need_single_match, |
879eb9e9 | 1223 | enum line_t line_type, |
86bf665e TM |
1224 | void (* callback) (const dwarf_line_t& line, |
1225 | void * arg), | |
7e1279ea FCE |
1226 | void *data) |
1227 | { | |
6315bd76 GH |
1228 | Dwarf_Line **srcsp = NULL; |
1229 | size_t nsrcs = 0; | |
fedd4090 | 1230 | dwarf_query * q = static_cast<dwarf_query *>(data); |
879eb9e9 | 1231 | int lineno = lines[0]; |
558982c5 | 1232 | auto_free_ref<Dwarf_Line**> free_srcsp(srcsp); |
41c262f3 | 1233 | |
7e1279ea | 1234 | get_module_dwarf(); |
bb788f9f | 1235 | |
41c262f3 | 1236 | if (line_type == RELATIVE) |
0c8b7d37 SC |
1237 | { |
1238 | Dwarf_Addr addr; | |
1239 | Dwarf_Line *line; | |
1240 | int line_number; | |
41c262f3 | 1241 | |
0c8b7d37 SC |
1242 | dwarf_assert ("dwarf_entrypc", dwarf_entrypc (this->function, &addr)); |
1243 | line = dwarf_getsrc_die (this->cu, addr); | |
1244 | dwarf_assert ("dwarf_getsrc_die", line == NULL); | |
1245 | dwarf_assert ("dwarf_lineno", dwarf_lineno (line, &line_number)); | |
1246 | lineno += line_number; | |
1247 | } | |
879eb9e9 SC |
1248 | else if (line_type == WILDCARD) |
1249 | function_line (&lineno); | |
41c262f3 | 1250 | |
879eb9e9 SC |
1251 | for (int l = lineno; ; l = l + 1) |
1252 | { | |
2ffc7958 SC |
1253 | set<int> lines_probed; |
1254 | pair<set<int>::iterator,bool> line_probed; | |
879eb9e9 SC |
1255 | dwarf_assert ("dwarf_getsrc_file", |
1256 | dwarf_getsrc_file (module_dwarf, | |
1257 | srcfile, l, 0, | |
1258 | &srcsp, &nsrcs)); | |
879eb9e9 SC |
1259 | if (line_type == WILDCARD || line_type == RANGE) |
1260 | { | |
1261 | Dwarf_Addr line_addr; | |
1262 | dwarf_lineno (srcsp [0], &lineno); | |
2ffc7958 | 1263 | line_probed = lines_probed.insert(lineno); |
69087111 | 1264 | if (lineno != l || line_probed.second == false || nsrcs > 1) |
879eb9e9 SC |
1265 | continue; |
1266 | dwarf_lineaddr (srcsp [0], &line_addr); | |
1267 | if (dwarf_haspc (function, line_addr) != 1) | |
1268 | break; | |
1269 | } | |
fedd4090 | 1270 | |
879eb9e9 | 1271 | // NB: Formerly, we used to filter, because: |
20e4a32c | 1272 | |
879eb9e9 SC |
1273 | // dwarf_getsrc_file gets one *near hits* for line numbers, not |
1274 | // exact matches. For example, an existing file but a nonexistent | |
1275 | // line number will be rounded up to the next definition in that | |
1276 | // file. This may be similar to the GDB breakpoint algorithm, but | |
1277 | // we don't want to be so fuzzy in systemtap land. So we filter. | |
847bf07f | 1278 | |
879eb9e9 | 1279 | // But we now see the error of our ways, and skip this filtering. |
fedd4090 | 1280 | |
879eb9e9 SC |
1281 | // XXX: the code also fails to match e.g. inline function |
1282 | // definitions when the srcfile is a header file rather than the | |
1283 | // CU name. | |
847bf07f | 1284 | |
879eb9e9 | 1285 | size_t remaining_nsrcs = nsrcs; |
847bf07f | 1286 | |
879eb9e9 | 1287 | if (need_single_match && remaining_nsrcs > 1) |
897820ca | 1288 | { |
879eb9e9 SC |
1289 | // We wanted a single line record (a unique address for the |
1290 | // line) and we got a bunch of line records. We're going to | |
1291 | // skip this probe (throw an exception) but before we throw | |
1292 | // we're going to look around a bit to see if there's a low or | |
1293 | // high line number nearby which *doesn't* have this problem, | |
1294 | // so we can give the user some advice. | |
1295 | ||
1296 | int lo_try = -1; | |
1297 | int hi_try = -1; | |
1298 | for (size_t i = 1; i < 6; ++i) | |
1299 | { | |
1300 | if (lo_try == -1 && has_single_line_record(q, srcfile, lineno - i)) | |
1301 | lo_try = lineno - i; | |
897820ca | 1302 | |
879eb9e9 SC |
1303 | if (hi_try == -1 && has_single_line_record(q, srcfile, lineno + i)) |
1304 | hi_try = lineno + i; | |
1305 | } | |
897820ca | 1306 | |
879eb9e9 SC |
1307 | stringstream advice; |
1308 | advice << "multiple addresses for " << srcfile << ":" << lineno; | |
1309 | if (lo_try > 0 || hi_try > 0) | |
1310 | { | |
1311 | advice << " (try "; | |
1312 | if (lo_try > 0) | |
1313 | advice << srcfile << ":" << lo_try; | |
1314 | if (lo_try > 0 && hi_try > 0) | |
1315 | advice << " or "; | |
1316 | if (hi_try > 0) | |
1317 | advice << srcfile << ":" << hi_try; | |
1318 | advice << ")"; | |
1319 | } | |
1320 | throw semantic_error (advice.str()); | |
1321 | } | |
897820ca | 1322 | |
2e67a43b TM |
1323 | for (size_t i = 0; i < nsrcs; ++i) |
1324 | { | |
1325 | if (pending_interrupts) return; | |
1326 | if (srcsp [i]) // skip over mismatched lines | |
1327 | callback (dwarf_line_t(srcsp[i]), data); | |
1328 | } | |
69087111 SC |
1329 | |
1330 | if (line_type == ABSOLUTE || line_type == RELATIVE) | |
1331 | break; | |
1332 | else if (line_type == RANGE && l == lines[1]) | |
1333 | break; | |
20e4a32c | 1334 | } |
50e0d793 GH |
1335 | } |
1336 | ||
0f336e95 SC |
1337 | void |
1338 | iterate_over_cu_labels (string label_val, Dwarf_Die *cu, void *data, | |
1339 | void (* callback)(const string &, | |
1340 | const char *, | |
1341 | int, | |
1342 | Dwarf_Die *, | |
1343 | Dwarf_Addr, | |
1344 | dwarf_query *)) | |
1345 | { | |
1346 | dwarf_query * q __attribute__ ((unused)) = static_cast<dwarf_query *>(data) ; | |
1347 | ||
1348 | get_module_dwarf(); | |
1349 | ||
1350 | const char * sym = label_val.c_str(); | |
1351 | Dwarf_Die die; | |
3c1f71d5 MW |
1352 | int res = dwarf_child (cu, &die); |
1353 | if (res != 0) | |
1354 | return; // die without children, bail out. | |
1355 | ||
0f336e95 SC |
1356 | static string function_name; |
1357 | do | |
1358 | { | |
1359 | Dwarf_Attribute attr_mem; | |
1360 | Dwarf_Attribute *attr = dwarf_attr (&die, DW_AT_name, &attr_mem); | |
1361 | int tag = dwarf_tag(&die); | |
1362 | const char *name = dwarf_formstring (attr); | |
9e67aff9 | 1363 | if (tag == DW_TAG_subprogram && name != 0) |
0f336e95 SC |
1364 | { |
1365 | function_name = name; | |
1366 | } | |
9e67aff9 | 1367 | else if (tag == DW_TAG_label && name != 0 |
7b534f48 | 1368 | && ((strncmp(name, sym, strlen(sym)) == 0) |
0f336e95 SC |
1369 | || (name_has_wildcard (sym) |
1370 | && function_name_matches_pattern (name, sym)))) | |
1371 | { | |
1372 | const char *file = dwarf_decl_file (&die); | |
7b534f48 SC |
1373 | // Get the line number for this label |
1374 | Dwarf_Attribute attr; | |
1375 | dwarf_attr (&die,DW_AT_decl_line, &attr); | |
1376 | Dwarf_Sword dline; | |
1377 | dwarf_formsdata (&attr, &dline); | |
0f336e95 SC |
1378 | Dwarf_Addr stmt_addr; |
1379 | if (dwarf_lowpc (&die, &stmt_addr) != 0) | |
7b534f48 SC |
1380 | { |
1381 | // There is no lowpc so figure out the address | |
1382 | // Get the real die for this cu | |
1383 | Dwarf_Die cudie; | |
1384 | dwarf_diecu (cu, &cudie, NULL, NULL); | |
1385 | size_t nlines = 0; | |
1386 | // Get the line for this label | |
1387 | Dwarf_Line **aline; | |
1388 | dwarf_getsrc_file (module_dwarf, file, (int)dline, 0, &aline, &nlines); | |
1389 | // Get the address | |
1390 | for (size_t i = 0; i < nlines; i++) | |
1391 | { | |
1392 | dwarf_lineaddr (*aline, &stmt_addr); | |
1393 | if ((dwarf_haspc (&die, stmt_addr))) | |
1394 | break; | |
1395 | } | |
1396 | } | |
1397 | ||
0f336e95 SC |
1398 | Dwarf_Die *scopes; |
1399 | int nscopes = 0; | |
1400 | nscopes = dwarf_getscopes_die (&die, &scopes); | |
1401 | if (nscopes > 1) | |
1402 | callback(function_name.c_str(), file, | |
7b534f48 | 1403 | (int)dline, &scopes[1], stmt_addr, q); |
0f336e95 SC |
1404 | } |
1405 | if (dwarf_haschildren (&die) && tag != DW_TAG_structure_type | |
1406 | && tag != DW_TAG_union_type) | |
1407 | { | |
1408 | iterate_over_cu_labels (label_val, &die, q, callback); | |
1409 | } | |
1410 | } | |
1411 | while (dwarf_siblingof (&die, &die) == 0); | |
1412 | } | |
1413 | ||
50e0d793 | 1414 | |
7e1279ea FCE |
1415 | void collect_srcfiles_matching (string const & pattern, |
1416 | set<char const *> & filtered_srcfiles) | |
50e0d793 | 1417 | { |
7e1279ea FCE |
1418 | assert (module); |
1419 | assert (cu); | |
bb788f9f | 1420 | |
7e1279ea FCE |
1421 | size_t nfiles; |
1422 | Dwarf_Files *srcfiles; | |
bb788f9f | 1423 | |
3213d089 FCE |
1424 | // PR 5049: implicit * in front of given path pattern. |
1425 | // NB: fnmatch() is used without FNM_PATHNAME. | |
79640c29 | 1426 | string prefixed_pattern = string("*/") + pattern; |
3213d089 | 1427 | |
20e4a32c | 1428 | dwarf_assert ("dwarf_getsrcfiles", |
7e1279ea FCE |
1429 | dwarf_getsrcfiles (cu, &srcfiles, &nfiles)); |
1430 | { | |
1431 | for (size_t i = 0; i < nfiles; ++i) | |
50e0d793 | 1432 | { |
7e1279ea | 1433 | char const * fname = dwarf_filesrc (srcfiles, i, NULL, NULL); |
79640c29 FCE |
1434 | if (fnmatch (pattern.c_str(), fname, 0) == 0 || |
1435 | fnmatch (prefixed_pattern.c_str(), fname, 0) == 0) | |
50e0d793 | 1436 | { |
7e1279ea | 1437 | filtered_srcfiles.insert (fname); |
b0ee93c4 | 1438 | if (sess.verbose>2) |
db22e55f | 1439 | clog << "selected source file '" << fname << "'\n"; |
50e0d793 GH |
1440 | } |
1441 | } | |
7e1279ea | 1442 | } |
20e4a32c | 1443 | } |
50e0d793 | 1444 | |
3e961ba6 | 1445 | void resolve_prologue_endings (func_info_map_t & funcs) |
7d71e1d5 FCE |
1446 | { |
1447 | // This heuristic attempts to pick the first address that has a | |
34ca7d84 FCE |
1448 | // source line distinct from the function declaration's. In a |
1449 | // perfect world, this would be the first statement *past* the | |
1450 | // prologue. | |
1451 | ||
7d71e1d5 FCE |
1452 | assert(module); |
1453 | assert(cu); | |
1454 | ||
456aa31c FCE |
1455 | size_t nlines = 0; |
1456 | Dwarf_Lines *lines = NULL; | |
7d71e1d5 | 1457 | |
dc223023 FCE |
1458 | /* trouble cases: |
1459 | malloc do_symlink in init/initramfs.c tail-recursive/tiny then no-prologue | |
1460 | sys_get?id in kernel/timer.c no-prologue | |
1461 | sys_exit_group tail-recursive | |
1462 | {do_,}sys_open extra-long-prologue (gcc 3.4) | |
1463 | cpu_to_logical_apicid NULL-decl_file | |
1464 | */ | |
1465 | ||
1466 | // Fetch all srcline records, sorted by address. | |
20e4a32c RM |
1467 | dwarf_assert ("dwarf_getsrclines", |
1468 | dwarf_getsrclines(cu, &lines, &nlines)); | |
dc223023 | 1469 | // XXX: free lines[] later, but how? |
7d71e1d5 | 1470 | |
3e961ba6 | 1471 | for(func_info_map_t::iterator it = funcs.begin(); it != funcs.end(); it++) |
7d71e1d5 | 1472 | { |
dc223023 FCE |
1473 | #if 0 /* someday */ |
1474 | Dwarf_Addr* bkpts = 0; | |
3e961ba6 | 1475 | int n = dwarf_entry_breakpoints (& it->die, & bkpts); |
dc223023 FCE |
1476 | // ... |
1477 | free (bkpts); | |
1478 | #endif | |
20e4a32c | 1479 | |
3e961ba6 | 1480 | Dwarf_Addr entrypc = it->entrypc; |
dc223023 | 1481 | Dwarf_Addr highpc; // NB: highpc is exclusive: [entrypc,highpc) |
3e961ba6 | 1482 | dwfl_assert ("dwarf_highpc", dwarf_highpc (& it->die, |
dc223023 FCE |
1483 | & highpc)); |
1484 | ||
3e961ba6 | 1485 | if (it->decl_file == 0) it->decl_file = ""; |
e38d6504 | 1486 | |
dc223023 | 1487 | unsigned entrypc_srcline_idx = 0; |
86bf665e | 1488 | dwarf_line_t entrypc_srcline; |
dc223023 FCE |
1489 | // open-code binary search for exact match |
1490 | { | |
1491 | unsigned l = 0, h = nlines; | |
1492 | while (l < h) | |
1493 | { | |
1494 | entrypc_srcline_idx = (l + h) / 2; | |
86bf665e TM |
1495 | const dwarf_line_t lr(dwarf_onesrcline(lines, |
1496 | entrypc_srcline_idx)); | |
1497 | Dwarf_Addr addr = lr.addr(); | |
dc223023 FCE |
1498 | if (addr == entrypc) { entrypc_srcline = lr; break; } |
1499 | else if (l + 1 == h) { break; } // ran off bottom of tree | |
1500 | else if (addr < entrypc) { l = entrypc_srcline_idx; } | |
1501 | else { h = entrypc_srcline_idx; } | |
e38d6504 | 1502 | } |
dc223023 | 1503 | } |
86bf665e | 1504 | if (!entrypc_srcline) |
dfa11ddb FCE |
1505 | { |
1506 | if (sess.verbose > 2) | |
1507 | clog << "missing entrypc dwarf line record for function '" | |
1508 | << it->name << "'\n"; | |
1509 | // This is probably an inlined function. We'll end up using | |
1510 | // its lowpc as a probe address. | |
1511 | continue; | |
1512 | } | |
dc223023 FCE |
1513 | |
1514 | if (sess.verbose>2) | |
3e961ba6 | 1515 | clog << "prologue searching function '" << it->name << "'" |
5fe3e97f | 1516 | << " 0x" << hex << entrypc << "-0x" << highpc << dec |
3e961ba6 | 1517 | << "@" << it->decl_file << ":" << it->decl_line |
dc223023 FCE |
1518 | << "\n"; |
1519 | ||
1520 | // Now we go searching for the first line record that has a | |
1521 | // file/line different from the one in the declaration. | |
1522 | // Normally, this will be the next one. BUT: | |
1523 | // | |
1524 | // We may have to skip a few because some old compilers plop | |
1525 | // in dummy line records for longer prologues. If we go too | |
1526 | // far (addr >= highpc), we take the previous one. Or, it may | |
1527 | // be the first one, if the function had no prologue, and thus | |
1528 | // the entrypc maps to a statement in the body rather than the | |
1529 | // declaration. | |
1530 | ||
1531 | unsigned postprologue_srcline_idx = entrypc_srcline_idx; | |
1532 | bool ranoff_end = false; | |
35f5f091 | 1533 | while (postprologue_srcline_idx < nlines) |
7d71e1d5 | 1534 | { |
86bf665e TM |
1535 | dwarf_line_t lr(dwarf_onesrcline(lines, postprologue_srcline_idx)); |
1536 | Dwarf_Addr postprologue_addr = lr.addr(); | |
1537 | const char* postprologue_file = lr.linesrc(); | |
1538 | int postprologue_lineno = lr.lineno(); | |
456aa31c | 1539 | |
b0ee93c4 | 1540 | if (sess.verbose>2) |
dc223023 FCE |
1541 | clog << "checking line record 0x" << hex << postprologue_addr << dec |
1542 | << "@" << postprologue_file << ":" << postprologue_lineno << "\n"; | |
1543 | ||
1544 | if (postprologue_addr >= highpc) | |
e38d6504 RM |
1545 | { |
1546 | ranoff_end = true; | |
1547 | postprologue_srcline_idx --; | |
dc223023 FCE |
1548 | continue; |
1549 | } | |
1550 | if (ranoff_end || | |
3e961ba6 JB |
1551 | (strcmp (postprologue_file, it->decl_file) || // We have a winner! |
1552 | (postprologue_lineno != it->decl_line))) | |
dc223023 | 1553 | { |
3e961ba6 | 1554 | it->prologue_end = postprologue_addr; |
dc223023 FCE |
1555 | |
1556 | if (sess.verbose>2) | |
1557 | { | |
3e961ba6 | 1558 | clog << "prologue found function '" << it->name << "'"; |
dc223023 FCE |
1559 | // Add a little classification datum |
1560 | if (postprologue_srcline_idx == entrypc_srcline_idx) clog << " (naked)"; | |
1561 | if (ranoff_end) clog << " (tail-call?)"; | |
1562 | clog << " = 0x" << hex << postprologue_addr << dec << "\n"; | |
1563 | } | |
1564 | ||
1565 | break; | |
1566 | } | |
e38d6504 | 1567 | |
dc223023 FCE |
1568 | // Let's try the next srcline. |
1569 | postprologue_srcline_idx ++; | |
1570 | } // loop over srclines | |
7d71e1d5 | 1571 | |
3e961ba6 | 1572 | // if (strlen(it->decl_file) == 0) it->decl_file = NULL; |
dc223023 FCE |
1573 | |
1574 | } // loop over functions | |
b20febf3 FCE |
1575 | |
1576 | // XXX: how to free lines? | |
bd2b1e68 GH |
1577 | } |
1578 | ||
7e1279ea FCE |
1579 | |
1580 | bool function_entrypc (Dwarf_Addr * addr) | |
1581 | { | |
1582 | assert (function); | |
20e4a32c | 1583 | return (dwarf_entrypc (function, addr) == 0); |
b8da0ad1 | 1584 | // XXX: see also _lowpc ? |
7e1279ea FCE |
1585 | } |
1586 | ||
1587 | ||
1588 | bool die_entrypc (Dwarf_Die * die, Dwarf_Addr * addr) | |
1589 | { | |
5bc4ac10 FCE |
1590 | int rc = 0; |
1591 | string lookup_method; | |
7e1279ea | 1592 | |
5bc4ac10 FCE |
1593 | * addr = 0; |
1594 | ||
1595 | lookup_method = "dwarf_entrypc"; | |
1596 | rc = dwarf_entrypc (die, addr); | |
1597 | ||
4baf0e53 | 1598 | if (rc) |
5bc4ac10 FCE |
1599 | { |
1600 | lookup_method = "dwarf_lowpc"; | |
1601 | rc = dwarf_lowpc (die, addr); | |
1602 | } | |
1603 | ||
1604 | if (rc) | |
1605 | { | |
1606 | lookup_method = "dwarf_ranges"; | |
1607 | ||
1608 | Dwarf_Addr base; | |
1609 | Dwarf_Addr begin; | |
1610 | Dwarf_Addr end; | |
1611 | ptrdiff_t offset = dwarf_ranges (die, 0, &base, &begin, &end); | |
1612 | if (offset < 0) rc = -1; | |
1613 | else if (offset > 0) | |
1614 | { | |
1615 | * addr = begin; | |
1616 | rc = 0; | |
1617 | ||
1618 | // Now we need to check that there are no more ranges | |
1619 | // associated with this function, which could conceivably | |
1620 | // happen if a function is inlined, then pieces of it are | |
1621 | // split amongst different conditional branches. It's not | |
1622 | // obvious which of them to favour. As a heuristic, we | |
1623 | // pick the beginning of the first range, and ignore the | |
1624 | // others (but with a warning). | |
1625 | ||
1626 | unsigned extra = 0; | |
1627 | while ((offset = dwarf_ranges (die, offset, &base, &begin, &end)) > 0) | |
1628 | extra ++; | |
1629 | if (extra) | |
1630 | lookup_method += ", ignored " + lex_cast<string>(extra) + " more"; | |
1631 | } | |
1632 | } | |
4baf0e53 | 1633 | |
5bc4ac10 FCE |
1634 | if (sess.verbose > 2) |
1635 | clog << "entry-pc lookup (" << lookup_method << ") = 0x" << hex << *addr << dec | |
4baf0e53 | 1636 | << " (rc " << rc << ")" |
5bc4ac10 FCE |
1637 | << endl; |
1638 | return (rc == 0); | |
7e1279ea FCE |
1639 | } |
1640 | ||
4cd232e4 GH |
1641 | void function_die (Dwarf_Die *d) |
1642 | { | |
1643 | assert (function); | |
20e4a32c | 1644 | *d = *function; |
4cd232e4 | 1645 | } |
7e1279ea | 1646 | |
4cd232e4 | 1647 | void function_file (char const ** c) |
7e1279ea FCE |
1648 | { |
1649 | assert (function); | |
4cd232e4 | 1650 | assert (c); |
20e4a32c | 1651 | *c = dwarf_decl_file (function); |
7e1279ea FCE |
1652 | } |
1653 | ||
4cd232e4 | 1654 | void function_line (int *linep) |
7e1279ea FCE |
1655 | { |
1656 | assert (function); | |
20e4a32c | 1657 | dwarf_decl_line (function, linep); |
7e1279ea FCE |
1658 | } |
1659 | ||
86bf665e | 1660 | bool die_has_pc (Dwarf_Die & die, Dwarf_Addr pc) |
7e1279ea | 1661 | { |
86bf665e | 1662 | int res = dwarf_haspc (&die, pc); |
a688cff2 SC |
1663 | // dwarf_ranges will return -1 if a function die has no DW_AT_ranges |
1664 | // if (res == -1) | |
1665 | // dwarf_assert ("dwarf_haspc", res); | |
7e1279ea FCE |
1666 | return res == 1; |
1667 | } | |
1668 | ||
1669 | ||
78f6bba6 | 1670 | static void loc2c_error (void *, const char *fmt, ...) |
e36387d7 | 1671 | { |
78f6bba6 FCE |
1672 | const char *msg = "?"; |
1673 | char *tmp = NULL; | |
5ce20b7a | 1674 | int rc; |
e36387d7 RM |
1675 | va_list ap; |
1676 | va_start (ap, fmt); | |
78f6bba6 FCE |
1677 | rc = vasprintf (& tmp, fmt, ap); |
1678 | if (rc < 0) | |
1679 | msg = "?"; | |
1680 | else | |
1681 | msg = tmp; | |
e36387d7 RM |
1682 | va_end (ap); |
1683 | throw semantic_error (msg); | |
1684 | } | |
bd2b1e68 | 1685 | |
e664cf5b FCE |
1686 | // This function generates code used for addressing computations of |
1687 | // target variables. | |
e38d6504 RM |
1688 | void emit_address (struct obstack *pool, Dwarf_Addr address) |
1689 | { | |
e664cf5b FCE |
1690 | #if 0 |
1691 | // The easy but incorrect way is to just print a hard-wired | |
1692 | // constant. | |
e38d6504 | 1693 | obstack_printf (pool, "%#" PRIx64 "UL", address); |
e664cf5b | 1694 | #endif |
e38d6504 RM |
1695 | |
1696 | // Turn this address into a section-relative offset if it should be one. | |
1697 | // We emit a comment approximating the variable+offset expression that | |
1698 | // relocatable module probing code will need to have. | |
1699 | Dwfl_Module *mod = dwfl_addrmodule (dwfl, address); | |
86bf665e | 1700 | dwfl_assert ("dwfl_addrmodule", mod); |
e38d6504 | 1701 | int n = dwfl_module_relocations (mod); |
ba53ea9f | 1702 | dwfl_assert ("dwfl_module_relocations", n >= 0); |
d64e82b1 | 1703 | int i = dwfl_module_relocate_address (mod, &address); |
ba53ea9f | 1704 | dwfl_assert ("dwfl_module_relocate_address", i >= 0); |
d64e82b1 SD |
1705 | const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, |
1706 | NULL, NULL, NULL, NULL); | |
86bf665e | 1707 | dwfl_assert ("dwfl_module_info", modname); |
d64e82b1 SD |
1708 | const char *secname = dwfl_module_relocation_info (mod, i, NULL); |
1709 | ||
1710 | if (n > 0 && !(n == 1 && secname == NULL)) | |
1711 | { | |
ba53ea9f | 1712 | dwfl_assert ("dwfl_module_relocation_info", secname); |
e38d6504 | 1713 | if (n > 1 || secname[0] != '\0') |
7e41d3dc FCE |
1714 | { |
1715 | // This gives us the module name, and section name within the | |
1716 | // module, for a kernel module (or other ET_REL module object). | |
1717 | obstack_printf (pool, "({ static unsigned long addr = 0; "); | |
1718 | obstack_printf (pool, "if (addr==0) addr = _stp_module_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", | |
1719 | modname, secname, address); | |
1720 | obstack_printf (pool, "addr; })"); | |
1721 | } | |
4baf0e53 | 1722 | else if (n == 1 && module_name == TOK_KERNEL && secname[0] == '\0') |
21beacc9 FCE |
1723 | { |
1724 | // elfutils' way of telling us that this is a relocatable kernel address, which we | |
1725 | // need to treat the same way here as dwarf_query::add_probe_point does: _stext. | |
1726 | address -= sess.sym_stext; | |
1727 | secname = "_stext"; | |
1728 | obstack_printf (pool, "({ static unsigned long addr = 0; "); | |
1729 | obstack_printf (pool, "if (addr==0) addr = _stp_module_relocate (\"%s\",\"%s\",%#" PRIx64 "); ", | |
1730 | modname, secname, address); | |
1731 | obstack_printf (pool, "addr; })"); | |
1732 | } | |
e38d6504 | 1733 | else |
e664cf5b FCE |
1734 | { |
1735 | throw semantic_error ("cannot relocate user-space dso (?) address"); | |
1736 | #if 0 | |
1737 | // This would happen for a Dwfl_Module that's a user-level DSO. | |
1738 | obstack_printf (pool, " /* %s+%#" PRIx64 " */", | |
1739 | modname, address); | |
1740 | #endif | |
1741 | } | |
e38d6504 | 1742 | } |
e664cf5b FCE |
1743 | else |
1744 | obstack_printf (pool, "%#" PRIx64 "UL", address); // assume as constant | |
e38d6504 | 1745 | } |
7e1279ea | 1746 | |
4b1ad75e RM |
1747 | static void loc2c_emit_address (void *arg, struct obstack *pool, |
1748 | Dwarf_Addr address) | |
1749 | { | |
1750 | dwflpp *dwfl = (dwflpp *) arg; | |
e38d6504 | 1751 | dwfl->emit_address (pool, address); |
4b1ad75e RM |
1752 | } |
1753 | ||
82e72903 DS |
1754 | void print_locals(Dwarf_Die *die, ostream &o) |
1755 | { | |
1756 | // Try to get the first child of die. | |
82e72903 DS |
1757 | Dwarf_Die child; |
1758 | if (dwarf_child (die, &child) == 0) | |
1759 | { | |
1760 | do | |
1761 | { | |
0e68eaaa | 1762 | const char *name; |
82e72903 DS |
1763 | // Output each sibling's name (that is a variable or |
1764 | // parameter) to 'o'. | |
1765 | switch (dwarf_tag (&child)) | |
1766 | { | |
1767 | case DW_TAG_variable: | |
1768 | case DW_TAG_formal_parameter: | |
0e68eaaa DS |
1769 | name = dwarf_diename (&child); |
1770 | if (name) | |
1771 | o << " " << name; | |
82e72903 DS |
1772 | break; |
1773 | default: | |
1774 | break; | |
1775 | } | |
1776 | } | |
1777 | while (dwarf_siblingof (&child, &child) == 0); | |
1778 | } | |
82e72903 DS |
1779 | } |
1780 | ||
e57b735a GH |
1781 | Dwarf_Attribute * |
1782 | find_variable_and_frame_base (Dwarf_Die *scope_die, | |
20e4a32c | 1783 | Dwarf_Addr pc, |
91eefb1c | 1784 | string const & local, |
e57b735a GH |
1785 | Dwarf_Die *vardie, |
1786 | Dwarf_Attribute *fb_attr_mem) | |
77de5e9e | 1787 | { |
77de5e9e | 1788 | Dwarf_Die *scopes; |
bcc12710 | 1789 | int nscopes = 0; |
e57b735a GH |
1790 | Dwarf_Attribute *fb_attr = NULL; |
1791 | ||
1792 | assert (cu); | |
bcc12710 | 1793 | |
f46d1b8a SC |
1794 | nscopes = dwarf_getscopes (cu, pc, &scopes); |
1795 | int sidx; | |
1796 | // if pc and scope_die are disjoint then we need dwarf_getscopes_die | |
1797 | for (sidx = 0; sidx < nscopes; sidx++) | |
1798 | if (scopes[sidx].addr == scope_die->addr) | |
1799 | break; | |
1800 | if (sidx == nscopes) | |
1801 | nscopes = dwarf_getscopes_die (scope_die, &scopes); | |
77de5e9e | 1802 | |
955925b7 | 1803 | if (nscopes <= 0) |
77de5e9e | 1804 | { |
7a053d3b | 1805 | throw semantic_error ("unable to find any scopes containing " |
59ff2773 | 1806 | + lex_cast_hex<string>(pc) |
1c7643a9 | 1807 | + ((scope_die == NULL) ? "" |
0e68eaaa DS |
1808 | : (string (" in ") |
1809 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
1810 | + "(" + (dwarf_diename(cu) ?: "<unknown>") | |
1811 | + ")")) | |
77de5e9e GH |
1812 | + " while searching for local '" + local + "'"); |
1813 | } | |
7a053d3b | 1814 | |
77de5e9e | 1815 | int declaring_scope = dwarf_getscopevar (scopes, nscopes, |
7a053d3b RM |
1816 | local.c_str(), |
1817 | 0, NULL, 0, 0, | |
e57b735a | 1818 | vardie); |
77de5e9e GH |
1819 | if (declaring_scope < 0) |
1820 | { | |
82e72903 DS |
1821 | stringstream alternatives; |
1822 | print_locals (scopes, alternatives); | |
77de5e9e | 1823 | throw semantic_error ("unable to find local '" + local + "'" |
82e72903 | 1824 | + " near pc " + lex_cast_hex<string>(pc) |
1c7643a9 | 1825 | + ((scope_die == NULL) ? "" |
0e68eaaa DS |
1826 | : (string (" in ") |
1827 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
1828 | + "(" + (dwarf_diename(cu) ?: "<unknown>") | |
1829 | + ")")) | |
8215ea9a | 1830 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); |
77de5e9e | 1831 | } |
7a053d3b | 1832 | |
77de5e9e GH |
1833 | for (int inner = 0; inner < nscopes; ++inner) |
1834 | { | |
1835 | switch (dwarf_tag (&scopes[inner])) | |
1836 | { | |
1837 | default: | |
1838 | continue; | |
1839 | case DW_TAG_subprogram: | |
1840 | case DW_TAG_entry_point: | |
1841 | case DW_TAG_inlined_subroutine: /* XXX */ | |
1842 | if (inner >= declaring_scope) | |
1843 | fb_attr = dwarf_attr_integrate (&scopes[inner], | |
1844 | DW_AT_frame_base, | |
e57b735a | 1845 | fb_attr_mem); |
77de5e9e GH |
1846 | break; |
1847 | } | |
1848 | } | |
e57b735a GH |
1849 | return fb_attr; |
1850 | } | |
77de5e9e | 1851 | |
77de5e9e | 1852 | |
d1531387 RM |
1853 | struct location * |
1854 | translate_location(struct obstack *pool, | |
1855 | Dwarf_Attribute *attr, Dwarf_Addr pc, | |
1856 | Dwarf_Attribute *fb_attr, | |
1857 | struct location **tail) | |
1858 | { | |
1859 | Dwarf_Op *expr; | |
1860 | size_t len; | |
1861 | ||
67982217 FCE |
1862 | /* PR9768: formerly, we added pc+module_bias here. However, that bias value |
1863 | is not present in the pc value by the time we get it, so adding it would | |
1864 | result in false negatives of variable reachibility. In other instances | |
1865 | further below, the c_translate_FOO functions, the module_bias value used | |
1866 | to be passed in, but instead should now be zero for the same reason. */ | |
1867 | ||
1868 | switch (dwarf_getlocation_addr (attr, pc /*+ module_bias*/, &expr, &len, 1)) | |
d1531387 RM |
1869 | { |
1870 | case 1: /* Should always happen. */ | |
1871 | if (len > 0) | |
1872 | break; | |
1873 | /* Fall through. */ | |
1874 | ||
1875 | case 0: /* Shouldn't happen. */ | |
1876 | throw semantic_error ("not accessible at this address"); | |
1877 | ||
1878 | default: /* Shouldn't happen. */ | |
1879 | case -1: | |
1880 | throw semantic_error (string ("dwarf_getlocation_addr failed") + | |
1881 | string (dwarf_errmsg (-1))); | |
1882 | } | |
1883 | ||
1884 | return c_translate_location (pool, &loc2c_error, this, | |
1885 | &loc2c_emit_address, | |
67982217 | 1886 | 1, 0 /* PR9768 */, |
d1531387 RM |
1887 | pc, expr, len, tail, fb_attr); |
1888 | } | |
1889 | ||
82e72903 DS |
1890 | void |
1891 | print_members(Dwarf_Die *vardie, ostream &o) | |
1892 | { | |
1893 | const int typetag = dwarf_tag (vardie); | |
1894 | ||
1895 | if (typetag != DW_TAG_structure_type && typetag != DW_TAG_union_type) | |
1896 | { | |
1897 | o << " Error: " | |
1898 | << (dwarf_diename_integrate (vardie) ?: "<anonymous>") | |
1899 | << " isn't a struct/union"; | |
1900 | return; | |
1901 | } | |
1902 | ||
1903 | // Try to get the first child of vardie. | |
1904 | Dwarf_Die die_mem; | |
1905 | Dwarf_Die *die = &die_mem; | |
1906 | switch (dwarf_child (vardie, die)) | |
1907 | { | |
1908 | case 1: // No children. | |
1909 | o << ((typetag == DW_TAG_union_type) ? " union " : " struct ") | |
1910 | << (dwarf_diename_integrate (die) ?: "<anonymous>") | |
1911 | << " is empty"; | |
1912 | break; | |
1913 | ||
1914 | case -1: // Error. | |
1915 | default: // Shouldn't happen. | |
1916 | o << ((typetag == DW_TAG_union_type) ? " union " : " struct ") | |
1917 | << (dwarf_diename_integrate (die) ?: "<anonymous>") | |
1918 | << ": " << dwarf_errmsg (-1); | |
1919 | break; | |
1920 | ||
1921 | case 0: // Success. | |
1922 | break; | |
1923 | } | |
1924 | ||
1925 | // Output each sibling's name to 'o'. | |
1926 | while (dwarf_tag (die) == DW_TAG_member) | |
1927 | { | |
4b3b2cc7 PS |
1928 | const char *member = dwarf_diename_integrate (die) ; |
1929 | ||
1930 | if ( member != NULL ) | |
4baf0e53 | 1931 | |
4b3b2cc7 PS |
1932 | o << " " << member; |
1933 | ||
1934 | else | |
1935 | { | |
1936 | Dwarf_Die temp_die = *die; | |
1937 | Dwarf_Attribute temp_attr ; | |
1938 | ||
1939 | if (!dwarf_attr_integrate (&temp_die, DW_AT_type, &temp_attr)) | |
1940 | { | |
1941 | clog<<"\n Error in obtaining type attribute for " | |
1942 | <<(dwarf_diename(&temp_die)?:"<anonymous>"); | |
1943 | return ; | |
1944 | } | |
1945 | ||
1946 | if ( ! dwarf_formref_die (&temp_attr,&temp_die)) | |
1947 | { | |
1948 | clog<<"\n Error in decoding type attribute for " | |
1949 | <<(dwarf_diename(&temp_die)?:"<anonymous>"); | |
1950 | return ; | |
1951 | } | |
1952 | print_members(&temp_die,o); | |
1953 | ||
1954 | } | |
82e72903 DS |
1955 | |
1956 | if (dwarf_siblingof (die, &die_mem) != 0) | |
1957 | break; | |
1958 | } | |
1959 | } | |
1960 | ||
e57b735a GH |
1961 | Dwarf_Die * |
1962 | translate_components(struct obstack *pool, | |
20e4a32c RM |
1963 | struct location **tail, |
1964 | Dwarf_Addr pc, | |
e57b735a GH |
1965 | vector<pair<target_symbol::component_type, |
1966 | std::string> > const & components, | |
1967 | Dwarf_Die *vardie, | |
1968 | Dwarf_Die *die_mem, | |
1969 | Dwarf_Attribute *attr_mem) | |
1970 | { | |
c4ce66a1 | 1971 | Dwarf_Die *die = die_mem; |
82e72903 | 1972 | Dwarf_Die struct_die; |
4b3b2cc7 PS |
1973 | Dwarf_Attribute temp_attr; |
1974 | ||
d9b516ca | 1975 | unsigned i = 0; |
4b3b2cc7 | 1976 | |
c4ce66a1 JS |
1977 | if (vardie) |
1978 | *die_mem = *vardie; | |
1979 | ||
4b3b2cc7 PS |
1980 | static unsigned int func_call_level ; |
1981 | static unsigned int dwarf_error_flag ; // indicates current error is dwarf error | |
1982 | static unsigned int dwarf_error_count ; // keeps track of no of dwarf errors | |
1983 | static semantic_error saved_dwarf_error(""); | |
1984 | ||
d9b516ca RM |
1985 | while (i < components.size()) |
1986 | { | |
0301cfe7 FCE |
1987 | /* XXX: This would be desirable, but we don't get the target_symbol token, |
1988 | and printing that gives us the file:line number too early anyway. */ | |
1989 | #if 0 | |
1990 | // Emit a marker to note which field is being access-attempted, to give | |
1991 | // better error messages if deref() fails. | |
1992 | string piece = string(...target_symbol token...) + string ("#") + stringify(components[i].second); | |
1993 | obstack_printf (pool, "c->last_stmt = %s;", lex_cast_qstring(piece).c_str()); | |
1994 | #endif | |
1995 | ||
d9b516ca RM |
1996 | const int typetag = dwarf_tag (die); |
1997 | switch (typetag) | |
1998 | { | |
1999 | case DW_TAG_typedef: | |
fdfbe4f7 GH |
2000 | case DW_TAG_const_type: |
2001 | case DW_TAG_volatile_type: | |
d9b516ca RM |
2002 | /* Just iterate on the referent type. */ |
2003 | break; | |
91eefb1c | 2004 | |
d9b516ca RM |
2005 | case DW_TAG_pointer_type: |
2006 | if (components[i].first == target_symbol::comp_literal_array_index) | |
2302c47e FCE |
2007 | throw semantic_error ("cannot index pointer"); |
2008 | // XXX: of course, we should support this the same way C does, | |
b0be9bdb | 2009 | // by explicit pointer arithmetic etc. PR4166. |
91eefb1c | 2010 | |
67982217 | 2011 | c_translate_pointer (pool, 1, 0 /* PR9768*/, die, tail); |
d9b516ca | 2012 | break; |
91eefb1c | 2013 | |
d9b516ca RM |
2014 | case DW_TAG_array_type: |
2015 | if (components[i].first == target_symbol::comp_literal_array_index) | |
2016 | { | |
67982217 | 2017 | c_translate_array (pool, 1, 0 /* PR9768 */, die, tail, |
d9b516ca RM |
2018 | NULL, lex_cast<Dwarf_Word>(components[i].second)); |
2019 | ++i; | |
2020 | } | |
2021 | else | |
2022 | throw semantic_error("bad field '" | |
2023 | + components[i].second | |
2024 | + "' for array type"); | |
2025 | break; | |
91eefb1c | 2026 | |
d9b516ca RM |
2027 | case DW_TAG_structure_type: |
2028 | case DW_TAG_union_type: | |
82e72903 | 2029 | struct_die = *die; |
0b8f6579 JB |
2030 | if (dwarf_hasattr(die, DW_AT_declaration)) |
2031 | { | |
c4ce66a1 | 2032 | Dwarf_Die *tmpdie = dwflpp::declaration_resolve(dwarf_diename(die)); |
0b8f6579 JB |
2033 | if (tmpdie == NULL) |
2034 | throw semantic_error ("unresolved struct " | |
2035 | + string (dwarf_diename_integrate (die) ?: "<anonymous>")); | |
2036 | *die_mem = *tmpdie; | |
2037 | } | |
e57b735a | 2038 | switch (dwarf_child (die, die_mem)) |
d9b516ca RM |
2039 | { |
2040 | case 1: /* No children. */ | |
4b3b2cc7 | 2041 | return NULL; |
d9b516ca RM |
2042 | case -1: /* Error. */ |
2043 | default: /* Shouldn't happen */ | |
2044 | throw semantic_error (string (typetag == DW_TAG_union_type ? "union" : "struct") | |
2045 | + string (dwarf_diename_integrate (die) ?: "<anonymous>") | |
2046 | + string (dwarf_errmsg (-1))); | |
2047 | break; | |
2048 | ||
2049 | case 0: | |
2050 | break; | |
2051 | } | |
2052 | ||
2053 | while (dwarf_tag (die) != DW_TAG_member | |
2054 | || ({ const char *member = dwarf_diename_integrate (die); | |
2055 | member == NULL || string(member) != components[i].second; })) | |
4b3b2cc7 PS |
2056 | { |
2057 | if ( dwarf_diename (die) == NULL ) // handling Anonymous structs/unions | |
2058 | { | |
2059 | Dwarf_Die temp_die = *die; | |
2060 | Dwarf_Die temp_die_2; | |
2061 | ||
2062 | try | |
2063 | { | |
2064 | if (!dwarf_attr_integrate (&temp_die, DW_AT_type, &temp_attr)) | |
2065 | { | |
2066 | dwarf_error_flag ++ ; | |
2067 | dwarf_error_count ++; | |
2068 | throw semantic_error(" Error in obtaining type attribute for "+ string(dwarf_diename(&temp_die)?:"<anonymous>")); | |
2069 | } | |
2070 | ||
2071 | if ( !dwarf_formref_die (&temp_attr, &temp_die)) | |
2072 | { | |
2073 | dwarf_error_flag ++ ; | |
2074 | dwarf_error_count ++; | |
2075 | throw semantic_error(" Error in decoding DW_AT_type attribute for " + string(dwarf_diename(&temp_die)?:"<anonymous>")); | |
2076 | } | |
2077 | ||
2078 | func_call_level ++ ; | |
2079 | ||
2080 | Dwarf_Die *result_die = translate_components(pool, tail, pc, components, &temp_die, &temp_die_2, &temp_attr ); | |
2081 | ||
2082 | func_call_level -- ; | |
2083 | ||
2084 | if (result_die != NULL) | |
2085 | { | |
2086 | memcpy(die_mem, &temp_die_2, sizeof(Dwarf_Die)); | |
2087 | memcpy(attr_mem, &temp_attr, sizeof(Dwarf_Attribute)); | |
2088 | return die_mem; | |
2089 | } | |
2090 | } | |
2091 | catch (const semantic_error& e) | |
2092 | { | |
2093 | if ( !dwarf_error_flag ) //not a dwarf error | |
2094 | throw; | |
2095 | else | |
2096 | { | |
2097 | dwarf_error_flag = 0 ; | |
2098 | saved_dwarf_error = e ; | |
2099 | } | |
2100 | } | |
2101 | } | |
e57b735a | 2102 | if (dwarf_siblingof (die, die_mem) != 0) |
4b3b2cc7 PS |
2103 | { |
2104 | if ( func_call_level == 0 && dwarf_error_count ) // this is parent call & a dwarf error has been reported in a branch somewhere | |
2105 | throw semantic_error( saved_dwarf_error ); | |
2106 | else | |
2107 | return NULL; | |
2108 | } | |
2109 | } | |
d9b516ca RM |
2110 | |
2111 | if (dwarf_attr_integrate (die, DW_AT_data_member_location, | |
e57b735a | 2112 | attr_mem) == NULL) |
d9b516ca RM |
2113 | { |
2114 | /* Union members don't usually have a location, | |
2115 | but just use the containing union's location. */ | |
2116 | if (typetag != DW_TAG_union_type) | |
9f36b77f | 2117 | throw semantic_error ("no location for field '" |
d9b516ca | 2118 | + components[i].second |
9f36b77f | 2119 | + "' :" + string(dwarf_errmsg (-1))); |
d9b516ca RM |
2120 | } |
2121 | else | |
d1531387 | 2122 | translate_location (pool, attr_mem, pc, NULL, tail); |
d9b516ca RM |
2123 | ++i; |
2124 | break; | |
2125 | ||
2126 | case DW_TAG_base_type: | |
9f36b77f | 2127 | throw semantic_error ("field '" |
d9b516ca | 2128 | + components[i].second |
9f36b77f | 2129 | + "' vs. base type " |
d9b516ca RM |
2130 | + string(dwarf_diename_integrate (die) ?: "<anonymous type>")); |
2131 | break; | |
2132 | case -1: | |
2133 | throw semantic_error ("cannot find type: " + string(dwarf_errmsg (-1))); | |
2134 | break; | |
2135 | ||
2136 | default: | |
2137 | throw semantic_error (string(dwarf_diename_integrate (die) ?: "<anonymous type>") | |
2138 | + ": unexpected type tag " | |
2139 | + lex_cast<string>(dwarf_tag (die))); | |
2140 | break; | |
2141 | } | |
2142 | ||
2143 | /* Now iterate on the type in DIE's attribute. */ | |
e57b735a | 2144 | if (dwarf_attr_integrate (die, DW_AT_type, attr_mem) == NULL) |
d9b516ca | 2145 | throw semantic_error ("cannot get type of field: " + string(dwarf_errmsg (-1))); |
c4ce66a1 | 2146 | die = dwarf_formref_die (attr_mem, die_mem); |
d9b516ca | 2147 | } |
e57b735a GH |
2148 | return die; |
2149 | } | |
91eefb1c | 2150 | |
d9b516ca | 2151 | |
e57b735a GH |
2152 | Dwarf_Die * |
2153 | resolve_unqualified_inner_typedie (Dwarf_Die *typedie_mem, | |
2154 | Dwarf_Attribute *attr_mem) | |
2155 | { | |
2156 | ; | |
d9b516ca | 2157 | Dwarf_Die *typedie; |
e57b735a | 2158 | int typetag = 0; |
d9b516ca | 2159 | while (1) |
20e4a32c | 2160 | { |
e57b735a | 2161 | typedie = dwarf_formref_die (attr_mem, typedie_mem); |
d9b516ca | 2162 | if (typedie == NULL) |
e57b735a | 2163 | throw semantic_error ("cannot get type: " + string(dwarf_errmsg (-1))); |
d9b516ca | 2164 | typetag = dwarf_tag (typedie); |
20e4a32c | 2165 | if (typetag != DW_TAG_typedef && |
fdfbe4f7 GH |
2166 | typetag != DW_TAG_const_type && |
2167 | typetag != DW_TAG_volatile_type) | |
91eefb1c | 2168 | break; |
e57b735a GH |
2169 | if (dwarf_attr_integrate (typedie, DW_AT_type, attr_mem) == NULL) |
2170 | throw semantic_error ("cannot get type of pointee: " + string(dwarf_errmsg (-1))); | |
d9b516ca | 2171 | } |
e57b735a GH |
2172 | return typedie; |
2173 | } | |
91eefb1c | 2174 | |
91eefb1c | 2175 | |
20e4a32c | 2176 | void |
e57b735a GH |
2177 | translate_final_fetch_or_store (struct obstack *pool, |
2178 | struct location **tail, | |
2179 | Dwarf_Addr module_bias, | |
2180 | Dwarf_Die *die, | |
2181 | Dwarf_Attribute *attr_mem, | |
2182 | bool lvalue, | |
b8da0ad1 FCE |
2183 | string &, |
2184 | string &, | |
e57b735a GH |
2185 | exp_type & ty) |
2186 | { | |
2187 | /* First boil away any qualifiers associated with the type DIE of | |
2188 | the final location to be accessed. */ | |
fdfbe4f7 | 2189 | |
e57b735a GH |
2190 | Dwarf_Die typedie_mem; |
2191 | Dwarf_Die *typedie; | |
2192 | int typetag; | |
022b623f DS |
2193 | char const *dname; |
2194 | string diestr; | |
e57b735a GH |
2195 | |
2196 | typedie = resolve_unqualified_inner_typedie (&typedie_mem, attr_mem); | |
2197 | typetag = dwarf_tag (typedie); | |
2198 | ||
2199 | /* Then switch behavior depending on the type of fetch/store we | |
2200 | want, and the type and pointer-ness of the final location. */ | |
20e4a32c | 2201 | |
fdfbe4f7 GH |
2202 | switch (typetag) |
2203 | { | |
fdfbe4f7 | 2204 | default: |
022b623f DS |
2205 | dname = dwarf_diename(die); |
2206 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
66d284f4 | 2207 | throw semantic_error ("unsupported type tag " |
022b623f DS |
2208 | + lex_cast<string>(typetag) |
2209 | + " for " + diestr); | |
2210 | break; | |
2211 | ||
2212 | case DW_TAG_structure_type: | |
2213 | case DW_TAG_union_type: | |
2214 | dname = dwarf_diename(die); | |
2215 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
2216 | throw semantic_error ("struct/union '" + diestr | |
2217 | + "' is being accessed instead of a member of the struct/union"); | |
fdfbe4f7 | 2218 | break; |
66d284f4 | 2219 | |
e7a012f0 | 2220 | case DW_TAG_enumeration_type: |
fdfbe4f7 | 2221 | case DW_TAG_base_type: |
00cf3709 FCE |
2222 | |
2223 | // Reject types we can't handle in systemtap | |
2224 | { | |
2225 | dname = dwarf_diename(die); | |
2226 | diestr = (dname != NULL) ? dname : "<unknown>"; | |
2227 | ||
2228 | Dwarf_Attribute encoding_attr; | |
6503a1cc | 2229 | Dwarf_Word encoding = (Dwarf_Word) -1; |
00cf3709 FCE |
2230 | dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding, &encoding_attr), |
2231 | & encoding); | |
2232 | if (encoding < 0) | |
2233 | { | |
2234 | // clog << "bad type1 " << encoding << " diestr" << endl; | |
2235 | throw semantic_error ("unsupported type (mystery encoding " + lex_cast<string>(encoding) + ")" + | |
2236 | " for " + diestr); | |
2237 | } | |
2238 | ||
2239 | if (encoding == DW_ATE_float | |
2240 | || encoding == DW_ATE_complex_float | |
2241 | /* XXX || many others? */) | |
2242 | { | |
2243 | // clog << "bad type " << encoding << " diestr" << endl; | |
2244 | throw semantic_error ("unsupported type (encoding " + lex_cast<string>(encoding) + ")" + | |
2245 | " for " + diestr); | |
2246 | } | |
2247 | } | |
2248 | ||
fdfbe4f7 | 2249 | ty = pe_long; |
e57b735a | 2250 | if (lvalue) |
67982217 | 2251 | c_translate_store (pool, 1, 0 /* PR9768 */, die, typedie, tail, |
e57b735a | 2252 | "THIS->value"); |
20e4a32c | 2253 | else |
67982217 | 2254 | c_translate_fetch (pool, 1, 0 /* PR9768 */, die, typedie, tail, |
e57b735a | 2255 | "THIS->__retvalue"); |
fdfbe4f7 GH |
2256 | break; |
2257 | ||
2258 | case DW_TAG_array_type: | |
2259 | case DW_TAG_pointer_type: | |
e57b735a | 2260 | |
b0be9bdb | 2261 | { |
fdfbe4f7 GH |
2262 | Dwarf_Die pointee_typedie_mem; |
2263 | Dwarf_Die *pointee_typedie; | |
2264 | Dwarf_Word pointee_encoding; | |
246b383e | 2265 | Dwarf_Word pointee_byte_size = 0; |
fdfbe4f7 | 2266 | |
e57b735a GH |
2267 | pointee_typedie = resolve_unqualified_inner_typedie (&pointee_typedie_mem, attr_mem); |
2268 | ||
2269 | if (dwarf_attr_integrate (pointee_typedie, DW_AT_byte_size, attr_mem)) | |
2270 | dwarf_formudata (attr_mem, &pointee_byte_size); | |
20e4a32c RM |
2271 | |
2272 | dwarf_formudata (dwarf_attr_integrate (pointee_typedie, DW_AT_encoding, attr_mem), | |
fdfbe4f7 GH |
2273 | &pointee_encoding); |
2274 | ||
b0be9bdb FCE |
2275 | if (lvalue) |
2276 | { | |
2277 | ty = pe_long; | |
2278 | if (typetag == DW_TAG_array_type) | |
2279 | throw semantic_error ("cannot write to array address"); | |
2280 | assert (typetag == DW_TAG_pointer_type); | |
67982217 | 2281 | c_translate_pointer_store (pool, 1, 0 /* PR9768 */, typedie, tail, |
b0be9bdb FCE |
2282 | "THIS->value"); |
2283 | } | |
2284 | else | |
2285 | { | |
2286 | // We have the pointer: cast it to an integral type via &(*(...)) | |
41c262f3 | 2287 | |
b0be9bdb FCE |
2288 | // NB: per bug #1187, at one point char*-like types were |
2289 | // automagically converted here to systemtap string values. | |
2290 | // For several reasons, this was taken back out, leaving | |
2291 | // pointer-to-string "conversion" (copying) to tapset functions. | |
41c262f3 | 2292 | |
b0be9bdb FCE |
2293 | ty = pe_long; |
2294 | if (typetag == DW_TAG_array_type) | |
67982217 | 2295 | c_translate_array (pool, 1, 0 /* PR9768 */, typedie, tail, NULL, 0); |
b0be9bdb | 2296 | else |
67982217 FCE |
2297 | c_translate_pointer (pool, 1, 0 /* PR9768 */, typedie, tail); |
2298 | c_translate_addressof (pool, 1, 0 /* PR9768 */, NULL, pointee_typedie, tail, | |
b0be9bdb FCE |
2299 | "THIS->__retvalue"); |
2300 | } | |
2301 | } | |
20e4a32c | 2302 | break; |
fdfbe4f7 | 2303 | } |
20e4a32c | 2304 | } |
e57b735a | 2305 | |
e19fda4e DS |
2306 | string |
2307 | express_as_string (string prelude, | |
2308 | string postlude, | |
2309 | struct location *head) | |
2310 | { | |
2311 | size_t bufsz = 1024; | |
2312 | char *buf = static_cast<char*>(malloc(bufsz)); | |
2313 | assert(buf); | |
2314 | ||
2315 | FILE *memstream = open_memstream (&buf, &bufsz); | |
2316 | assert(memstream); | |
2317 | ||
2318 | fprintf(memstream, "{\n"); | |
bec508de | 2319 | fprintf(memstream, "%s", prelude.c_str()); |
e19fda4e | 2320 | bool deref = c_emit_location (memstream, head, 1); |
bec508de | 2321 | fprintf(memstream, "%s", postlude.c_str()); |
e19fda4e DS |
2322 | fprintf(memstream, " goto out;\n"); |
2323 | ||
2324 | // dummy use of deref_fault label, to disable warning if deref() not used | |
2325 | fprintf(memstream, "if (0) goto deref_fault;\n"); | |
2326 | ||
2327 | // XXX: deref flag not reliable; emit fault label unconditionally | |
78f6bba6 | 2328 | (void) deref; |
e19fda4e DS |
2329 | fprintf(memstream, |
2330 | "deref_fault:\n" | |
e19fda4e DS |
2331 | " goto out;\n"); |
2332 | fprintf(memstream, "}\n"); | |
2333 | ||
2334 | fclose (memstream); | |
2335 | string result(buf); | |
2336 | free (buf); | |
2337 | return result; | |
2338 | } | |
e57b735a | 2339 | |
20e4a32c | 2340 | string |
e57b735a | 2341 | literal_stmt_for_local (Dwarf_Die *scope_die, |
20e4a32c | 2342 | Dwarf_Addr pc, |
e57b735a GH |
2343 | string const & local, |
2344 | vector<pair<target_symbol::component_type, | |
2345 | std::string> > const & components, | |
2346 | bool lvalue, | |
2347 | exp_type & ty) | |
2348 | { | |
2349 | Dwarf_Die vardie; | |
2350 | Dwarf_Attribute fb_attr_mem, *fb_attr = NULL; | |
2351 | ||
20e4a32c | 2352 | fb_attr = find_variable_and_frame_base (scope_die, pc, local, |
e57b735a GH |
2353 | &vardie, &fb_attr_mem); |
2354 | ||
b0ee93c4 | 2355 | if (sess.verbose>2) |
e57b735a | 2356 | clog << "finding location for local '" << local |
12b44fb3 FCE |
2357 | << "' near address 0x" << hex << pc |
2358 | << ", module bias 0x" << module_bias << dec | |
db22e55f | 2359 | << "\n"; |
e57b735a GH |
2360 | |
2361 | Dwarf_Attribute attr_mem; | |
2362 | if (dwarf_attr_integrate (&vardie, DW_AT_location, &attr_mem) == NULL) | |
2363 | { | |
2364 | throw semantic_error("failed to retrieve location " | |
20e4a32c RM |
2365 | "attribute for local '" + local |
2366 | + "' (dieoffset: " | |
2367 | + lex_cast_hex<string>(dwarf_dieoffset (&vardie)) | |
e57b735a GH |
2368 | + ")"); |
2369 | } | |
2370 | ||
2371 | #define obstack_chunk_alloc malloc | |
2372 | #define obstack_chunk_free free | |
2373 | ||
2374 | struct obstack pool; | |
2375 | obstack_init (&pool); | |
2376 | struct location *tail = NULL; | |
2377 | ||
2378 | /* Given $foo->bar->baz[NN], translate the location of foo. */ | |
2379 | ||
d1531387 RM |
2380 | struct location *head = translate_location (&pool, |
2381 | &attr_mem, pc, fb_attr, &tail); | |
e57b735a GH |
2382 | |
2383 | if (dwarf_attr_integrate (&vardie, DW_AT_type, &attr_mem) == NULL) | |
2384 | throw semantic_error("failed to retrieve type " | |
2385 | "attribute for local '" + local + "'"); | |
2386 | ||
e57b735a GH |
2387 | /* Translate the ->bar->baz[NN] parts. */ |
2388 | ||
2389 | Dwarf_Die die_mem, *die = NULL; | |
c4ce66a1 | 2390 | die = dwarf_formref_die (&attr_mem, &die_mem); |
20e4a32c | 2391 | die = translate_components (&pool, &tail, pc, components, |
c4ce66a1 | 2392 | die, &die_mem, &attr_mem); |
4b3b2cc7 PS |
2393 | if(!die) |
2394 | { | |
2395 | die = dwarf_formref_die (&attr_mem, &vardie); | |
2396 | stringstream alternatives; | |
a3de5d6e MW |
2397 | if (die != NULL) |
2398 | print_members(die,alternatives); | |
b487a14d FCE |
2399 | throw semantic_error("unable to find local '" + local + "'" |
2400 | + " near pc " + lex_cast_hex<string>(pc) | |
2401 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); | |
4b3b2cc7 | 2402 | } |
e57b735a | 2403 | |
20e4a32c RM |
2404 | /* Translate the assignment part, either |
2405 | x = $foo->bar->baz[NN] | |
2406 | or | |
e57b735a GH |
2407 | $foo->bar->baz[NN] = x |
2408 | */ | |
2409 | ||
2410 | string prelude, postlude; | |
20e4a32c | 2411 | translate_final_fetch_or_store (&pool, &tail, module_bias, |
e57b735a GH |
2412 | die, &attr_mem, lvalue, |
2413 | prelude, postlude, ty); | |
2414 | ||
2415 | /* Write the translation to a string. */ | |
e19fda4e DS |
2416 | return express_as_string(prelude, postlude, head); |
2417 | } | |
20e4a32c | 2418 | |
20e4a32c | 2419 | |
e19fda4e DS |
2420 | string |
2421 | literal_stmt_for_return (Dwarf_Die *scope_die, | |
2422 | Dwarf_Addr pc, | |
2423 | vector<pair<target_symbol::component_type, | |
2424 | std::string> > const & components, | |
2425 | bool lvalue, | |
2426 | exp_type & ty) | |
2427 | { | |
2428 | if (sess.verbose>2) | |
2429 | clog << "literal_stmt_for_return: finding return value for " | |
0e68eaaa | 2430 | << (dwarf_diename(scope_die) ?: "<unknown>") |
e19fda4e | 2431 | << "(" |
0e68eaaa | 2432 | << (dwarf_diename(cu) ?: "<unknown>") |
e19fda4e | 2433 | << ")\n"; |
7a053d3b | 2434 | |
e19fda4e DS |
2435 | struct obstack pool; |
2436 | obstack_init (&pool); | |
2437 | struct location *tail = NULL; | |
7a053d3b | 2438 | |
e19fda4e DS |
2439 | /* Given $return->bar->baz[NN], translate the location of return. */ |
2440 | const Dwarf_Op *locops; | |
2441 | int nlocops = dwfl_module_return_value_location (module, scope_die, | |
2442 | &locops); | |
2443 | if (nlocops < 0) | |
2444 | { | |
194c6687 | 2445 | throw semantic_error("failed to retrieve return value location" |
0e68eaaa DS |
2446 | " for " |
2447 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2448 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
2449 | + ")"); | |
e19fda4e DS |
2450 | } |
2451 | // the function has no return value (e.g. "void" in C) | |
2452 | else if (nlocops == 0) | |
2453 | { | |
0e68eaaa DS |
2454 | throw semantic_error("function " |
2455 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2456 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
194c6687 | 2457 | + ") has no return value"); |
e19fda4e | 2458 | } |
a781f401 | 2459 | |
e19fda4e DS |
2460 | struct location *head = c_translate_location (&pool, &loc2c_error, this, |
2461 | &loc2c_emit_address, | |
67982217 | 2462 | 1, 0 /* PR9768 */, |
e19fda4e DS |
2463 | pc, locops, nlocops, |
2464 | &tail, NULL); | |
7a053d3b | 2465 | |
e19fda4e | 2466 | /* Translate the ->bar->baz[NN] parts. */ |
7a053d3b | 2467 | |
e19fda4e DS |
2468 | Dwarf_Attribute attr_mem; |
2469 | Dwarf_Attribute *attr = dwarf_attr (scope_die, DW_AT_type, &attr_mem); | |
2470 | ||
2471 | Dwarf_Die vardie_mem; | |
2472 | Dwarf_Die *vardie = dwarf_formref_die (attr, &vardie_mem); | |
2473 | ||
2474 | Dwarf_Die die_mem, *die = NULL; | |
2475 | die = translate_components (&pool, &tail, pc, components, | |
2476 | vardie, &die_mem, &attr_mem); | |
4b3b2cc7 PS |
2477 | if(!die) |
2478 | { | |
2479 | die = dwarf_formref_die (&attr_mem, vardie); | |
2480 | stringstream alternatives; | |
a3de5d6e MW |
2481 | if (die != NULL) |
2482 | print_members(die,alternatives); | |
b487a14d FCE |
2483 | throw semantic_error("unable to find return value" |
2484 | " near pc " + lex_cast_hex<string>(pc) | |
0e68eaaa DS |
2485 | + " for " |
2486 | + string(dwarf_diename(scope_die) ?: "<unknown>") | |
2487 | + "(" + string(dwarf_diename(cu) ?: "<unknown>") | |
2488 | + ")" | |
b487a14d | 2489 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); |
4b3b2cc7 PS |
2490 | } |
2491 | ||
e19fda4e DS |
2492 | |
2493 | /* Translate the assignment part, either | |
2494 | x = $return->bar->baz[NN] | |
2495 | or | |
2496 | $return->bar->baz[NN] = x | |
2497 | */ | |
2498 | ||
2499 | string prelude, postlude; | |
2500 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
2501 | die, &attr_mem, lvalue, | |
2502 | prelude, postlude, ty); | |
2503 | ||
2504 | /* Write the translation to a string. */ | |
2505 | return express_as_string(prelude, postlude, head); | |
2506 | } | |
7a053d3b | 2507 | |
77de5e9e | 2508 | |
c4ce66a1 JS |
2509 | string |
2510 | literal_stmt_for_pointer (Dwarf_Die *type_die, | |
2511 | vector<pair<target_symbol::component_type, | |
2512 | std::string> > const & components, | |
2513 | bool lvalue, | |
2514 | exp_type & ty) | |
2515 | { | |
2516 | if (sess.verbose>2) | |
2517 | clog << "literal_stmt_for_pointer: finding value for " | |
2518 | << (dwarf_diename(type_die) ?: "<unknown>") | |
2519 | << "(" | |
2520 | << (dwarf_diename(cu) ?: "<unknown>") | |
2521 | << ")\n"; | |
2522 | ||
2523 | struct obstack pool; | |
2524 | obstack_init (&pool); | |
2525 | struct location *head = c_translate_argument (&pool, &loc2c_error, this, | |
2526 | &loc2c_emit_address, | |
2527 | 1, "THIS->pointer"); | |
2528 | struct location *tail = head; | |
2529 | ||
2530 | /* Translate the ->bar->baz[NN] parts. */ | |
2531 | ||
2532 | Dwarf_Attribute attr_mem; | |
2533 | Dwarf_Die die_mem, *die = NULL; | |
2534 | die = translate_components (&pool, &tail, 0, components, | |
2535 | type_die, &die_mem, &attr_mem); | |
2536 | if(!die) | |
2537 | { | |
2538 | die = dwarf_formref_die (&attr_mem, &die_mem); | |
2539 | stringstream alternatives; | |
2540 | print_members(die ?: type_die, alternatives); | |
2541 | throw semantic_error("unable to find member for struct " | |
2542 | + string(dwarf_diename(die ?: type_die) ?: "<unknown>") | |
2543 | + (alternatives.str() == "" ? "" : (" (alternatives:" + alternatives.str () + ")"))); | |
2544 | } | |
2545 | ||
2546 | ||
2547 | /* Translate the assignment part, either | |
2548 | x = (THIS->pointer)->bar->baz[NN] | |
2549 | or | |
2550 | (THIS->pointer)->bar->baz[NN] = x | |
2551 | */ | |
2552 | ||
2553 | string prelude, postlude; | |
2554 | translate_final_fetch_or_store (&pool, &tail, module_bias, | |
2555 | die, &attr_mem, lvalue, | |
2556 | prelude, postlude, ty); | |
2557 | ||
2558 | /* Write the translation to a string. */ | |
2559 | return express_as_string(prelude, postlude, head); | |
2560 | } | |
2561 | ||
2562 | ||
bd2b1e68 GH |
2563 | ~dwflpp() |
2564 | { | |
2565 | if (dwfl) | |
2566 | dwfl_end(dwfl); | |
2567 | } | |
2568 | }; | |
2569 | ||
405b71b8 | 2570 | |
20c6c071 | 2571 | |
7a053d3b | 2572 | enum |
bd2b1e68 | 2573 | function_spec_type |
7a053d3b | 2574 | { |
bd2b1e68 GH |
2575 | function_alone, |
2576 | function_and_file, | |
7a053d3b | 2577 | function_file_and_line |
bd2b1e68 GH |
2578 | }; |
2579 | ||
ec4373ff | 2580 | |
bd2b1e68 | 2581 | struct dwarf_builder; |
77de5e9e | 2582 | |
2930abc7 | 2583 | |
b20febf3 FCE |
2584 | // XXX: This class is a candidate for subclassing to separate |
2585 | // the relocation vs non-relocation variants. Likewise for | |
2586 | // kprobe vs kretprobe variants. | |
2587 | ||
2588 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 2589 | { |
b20febf3 FCE |
2590 | dwarf_derived_probe (const string& function, |
2591 | const string& filename, | |
2592 | int line, | |
2593 | const string& module, | |
2594 | const string& section, | |
2595 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 2596 | Dwarf_Addr addr, |
b20febf3 FCE |
2597 | dwarf_query & q, |
2598 | Dwarf_Die* scope_die); | |
20e4a32c | 2599 | |
b20febf3 FCE |
2600 | string module; |
2601 | string section; | |
2602 | Dwarf_Addr addr; | |
2930abc7 | 2603 | bool has_return; |
c9bad430 DS |
2604 | bool has_maxactive; |
2605 | long maxactive_val; | |
b95e2b79 | 2606 | bool access_vars; |
2930abc7 | 2607 | |
b8da0ad1 | 2608 | void printsig (std::ostream &o) const; |
b20febf3 | 2609 | void join_group (systemtap_session& s); |
9020300d FCE |
2610 | void emit_probe_local_init(translator_output * o); |
2611 | ||
bd2b1e68 | 2612 | // Pattern registration helpers. |
7a053d3b | 2613 | static void register_statement_variants(match_node * root, |
bd2b1e68 | 2614 | dwarf_builder * dw); |
fd6602a0 FCE |
2615 | static void register_function_variants(match_node * root, |
2616 | dwarf_builder * dw); | |
7a053d3b | 2617 | static void register_function_and_statement_variants(match_node * root, |
bd2b1e68 | 2618 | dwarf_builder * dw); |
c4ce66a1 | 2619 | static void register_patterns(systemtap_session& s); |
20c6c071 GH |
2620 | }; |
2621 | ||
dc38c0ae | 2622 | |
6d0f3f0c FCE |
2623 | struct uprobe_derived_probe: public derived_probe |
2624 | { | |
2625 | bool return_p; | |
2626 | string module; // * => unrestricted | |
2627 | int pid; // 0 => unrestricted | |
2628 | string section; // empty => absolute address | |
2629 | Dwarf_Addr address; | |
2630 | // bool has_maxactive; | |
2631 | // long maxactive_val; | |
0973d815 | 2632 | |
6d0f3f0c FCE |
2633 | uprobe_derived_probe (const string& function, |
2634 | const string& filename, | |
2635 | int line, | |
2636 | const string& module, | |
2637 | int pid, | |
2638 | const string& section, | |
2639 | Dwarf_Addr dwfl_addr, | |
2640 | Dwarf_Addr addr, | |
2641 | dwarf_query & q, | |
2642 | Dwarf_Die* scope_die); | |
2643 | ||
0973d815 FCE |
2644 | // alternate constructor for process(PID).statement(ADDR).absolute |
2645 | uprobe_derived_probe (probe *base, | |
2646 | probe_point *location, | |
2647 | int pid, | |
2648 | Dwarf_Addr addr, | |
2649 | bool return_p); | |
2650 | ||
6d0f3f0c FCE |
2651 | void printsig (std::ostream &o) const; |
2652 | void join_group (systemtap_session& s); | |
2653 | }; | |
2654 | ||
2655 | ||
2656 | ||
dc38c0ae DS |
2657 | struct dwarf_derived_probe_group: public derived_probe_group |
2658 | { | |
2659 | private: | |
b20febf3 FCE |
2660 | multimap<string,dwarf_derived_probe*> probes_by_module; |
2661 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
2662 | |
2663 | public: | |
b20febf3 FCE |
2664 | void enroll (dwarf_derived_probe* probe); |
2665 | void emit_module_decls (systemtap_session& s); | |
2666 | void emit_module_init (systemtap_session& s); | |
2667 | void emit_module_exit (systemtap_session& s); | |
dc38c0ae DS |
2668 | }; |
2669 | ||
2670 | ||
20c6c071 | 2671 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 2672 | struct base_query |
20c6c071 | 2673 | { |
c4ce66a1 JS |
2674 | base_query(dwflpp & dw, literal_map_t const & params); |
2675 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 2676 | virtual ~base_query() {} |
bd2b1e68 | 2677 | |
5227f1ea | 2678 | systemtap_session & sess; |
2c384610 | 2679 | dwflpp & dw; |
5227f1ea | 2680 | |
bd2b1e68 | 2681 | // Parameter extractors. |
86bf665e | 2682 | static bool has_null_param(literal_map_t const & params, |
888af770 | 2683 | string const & k); |
86bf665e | 2684 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 2685 | string const & k, string & v); |
86bf665e | 2686 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 2687 | string const & k, long & v); |
86bf665e | 2688 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 2689 | string const & k, Dwarf_Addr & v); |
b55bc428 | 2690 | |
2c384610 DS |
2691 | // Extracted parameters. |
2692 | bool has_kernel; | |
91af0778 FCE |
2693 | bool has_module; |
2694 | bool has_process; | |
2c384610 DS |
2695 | string module_val; // has_kernel => module_val = "kernel" |
2696 | ||
2697 | virtual void handle_query_module() = 0; | |
2698 | }; | |
2699 | ||
2700 | ||
c4ce66a1 JS |
2701 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
2702 | sess(dw.sess), dw(dw) | |
2c384610 | 2703 | { |
91af0778 | 2704 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
2705 | if (has_kernel) |
2706 | module_val = "kernel"; | |
91af0778 FCE |
2707 | |
2708 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
2709 | if (has_module) | |
2710 | has_process = false; | |
4baf0e53 | 2711 | else |
d0a7f5a9 FCE |
2712 | { |
2713 | has_process = get_string_param(params, TOK_PROCESS, module_val); | |
06aca46a | 2714 | if (has_process) |
d0a7f5a9 FCE |
2715 | module_val = find_executable (module_val); |
2716 | } | |
91af0778 FCE |
2717 | |
2718 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
2719 | } |
2720 | ||
c4ce66a1 JS |
2721 | base_query::base_query(dwflpp & dw, const string & module_val) |
2722 | : sess(dw.sess), dw(dw), module_val(module_val) | |
2723 | { | |
2724 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
2725 | // which means that userspace modules won't get any PATH searching. | |
2726 | if (module_val.find('/') == string::npos) | |
2727 | { | |
2728 | has_kernel = (module_val == TOK_KERNEL); | |
2729 | has_module = !has_kernel; | |
2730 | has_process = false; | |
2731 | } | |
2732 | else | |
2733 | { | |
2734 | has_kernel = has_module = false; | |
2735 | has_process = true; | |
2736 | } | |
2737 | } | |
2738 | ||
2c384610 | 2739 | bool |
86bf665e | 2740 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
2741 | string const & k) |
2742 | { | |
888af770 | 2743 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
2744 | } |
2745 | ||
2746 | ||
2747 | bool | |
86bf665e | 2748 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
2749 | string const & k, string & v) |
2750 | { | |
2751 | return derived_probe_builder::get_param (params, k, v); | |
2752 | } | |
2753 | ||
2754 | ||
2755 | bool | |
86bf665e | 2756 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
2757 | string const & k, long & v) |
2758 | { | |
2759 | int64_t value; | |
2760 | bool present = derived_probe_builder::get_param (params, k, value); | |
2761 | v = (long) value; | |
2762 | return present; | |
2763 | } | |
2764 | ||
2765 | ||
2766 | bool | |
86bf665e | 2767 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
2768 | string const & k, Dwarf_Addr & v) |
2769 | { | |
2770 | int64_t value; | |
2771 | bool present = derived_probe_builder::get_param (params, k, value); | |
2772 | v = (Dwarf_Addr) value; | |
2773 | return present; | |
2774 | } | |
2775 | ||
2c384610 DS |
2776 | struct dwarf_query : public base_query |
2777 | { | |
2778 | dwarf_query(systemtap_session & sess, | |
2779 | probe * base_probe, | |
2780 | probe_point * base_loc, | |
2781 | dwflpp & dw, | |
86bf665e | 2782 | literal_map_t const & params, |
2c384610 DS |
2783 | vector<derived_probe *> & results); |
2784 | ||
c4ce66a1 JS |
2785 | vector<derived_probe *> & results; |
2786 | probe * base_probe; | |
2787 | probe_point * base_loc; | |
2788 | ||
2c384610 | 2789 | virtual void handle_query_module(); |
5f0a03a6 JK |
2790 | void query_module_dwarf(); |
2791 | void query_module_symtab(); | |
2c384610 | 2792 | |
2930abc7 FCE |
2793 | void add_probe_point(string const & funcname, |
2794 | char const * filename, | |
2795 | int line, | |
2796 | Dwarf_Die *scope_die, | |
2797 | Dwarf_Addr addr); | |
d64e82b1 | 2798 | string get_blacklist_section(Dwarf_Addr addr); |
20c6c071 | 2799 | |
a7301475 FCE |
2800 | regex_t blacklist_func; // function/statement probes |
2801 | regex_t blacklist_func_ret; // only for .return probes | |
2802 | regex_t blacklist_file; // file name | |
0daad364 JS |
2803 | void build_blacklist(); |
2804 | ||
b20febf3 FCE |
2805 | bool blacklisted_p(const string& funcname, |
2806 | const string& filename, | |
36f9dd1d | 2807 | int line, |
b20febf3 FCE |
2808 | const string& module, |
2809 | const string& section, | |
36f9dd1d FCE |
2810 | Dwarf_Addr addr); |
2811 | ||
2930abc7 | 2812 | // Extracted parameters. |
7a053d3b | 2813 | string function_val; |
20c6c071 GH |
2814 | |
2815 | bool has_function_str; | |
2816 | bool has_statement_str; | |
2817 | bool has_function_num; | |
2818 | bool has_statement_num; | |
7a053d3b RM |
2819 | string statement_str_val; |
2820 | string function_str_val; | |
c239d28c GH |
2821 | Dwarf_Addr statement_num_val; |
2822 | Dwarf_Addr function_num_val; | |
20c6c071 | 2823 | |
b8da0ad1 FCE |
2824 | bool has_call; |
2825 | bool has_inline; | |
20c6c071 GH |
2826 | bool has_return; |
2827 | ||
c9bad430 DS |
2828 | bool has_maxactive; |
2829 | long maxactive_val; | |
2830 | ||
20c6c071 GH |
2831 | bool has_label; |
2832 | string label_val; | |
2833 | ||
2834 | bool has_relative; | |
2835 | long relative_val; | |
2836 | ||
37ebca01 FCE |
2837 | bool has_absolute; |
2838 | ||
5f0a03a6 JK |
2839 | enum dbinfo_reqt dbinfo_reqt; |
2840 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
2841 | ||
20c6c071 GH |
2842 | function_spec_type parse_function_spec(string & spec); |
2843 | function_spec_type spec_type; | |
2844 | string function; | |
2845 | string file; | |
0c8b7d37 | 2846 | line_t line_type; |
879eb9e9 | 2847 | int line[2]; |
5f0a03a6 | 2848 | bool query_done; // Found exact match |
20c6c071 | 2849 | |
7e1279ea FCE |
2850 | set<char const *> filtered_srcfiles; |
2851 | ||
2852 | // Map official entrypc -> func_info object | |
86bf665e TM |
2853 | inline_instance_map_t filtered_inlines; |
2854 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
2855 | bool choose_next_line; |
2856 | Dwarf_Addr entrypc_for_next_line; | |
b55bc428 FCE |
2857 | }; |
2858 | ||
98afd80e | 2859 | |
fedd4090 FCE |
2860 | // This little test routine represents an unfortunate breakdown in |
2861 | // abstraction between dwflpp (putatively, a layer right on top of | |
2862 | // elfutils), and dwarf_query (interpreting a systemtap probe point). | |
2863 | // It arises because we sometimes try to fix up slightly-off | |
2864 | // .statement() probes (something we find out in fairly low-level). | |
2865 | // | |
2cb3fe26 | 2866 | // An alternative would be to put some more intelligence into query_cu(), |
4baf0e53 | 2867 | // and have it print additional suggestions after finding that |
fedd4090 FCE |
2868 | // q->dw.iterate_over_srcfile_lines resulted in no new finished_results. |
2869 | ||
2870 | bool | |
2871 | dwflpp::has_single_line_record (dwarf_query * q, char const * srcfile, int lineno) | |
2872 | { | |
2873 | if (lineno < 0) | |
2874 | return false; | |
2875 | ||
2876 | Dwarf_Line **srcsp = NULL; | |
2877 | size_t nsrcs = 0; | |
2878 | ||
2879 | dwarf_assert ("dwarf_getsrc_file", | |
2880 | dwarf_getsrc_file (module_dwarf, | |
2881 | srcfile, lineno, 0, | |
2882 | &srcsp, &nsrcs)); | |
2883 | ||
4baf0e53 | 2884 | if (nsrcs != 1) |
fedd4090 FCE |
2885 | { |
2886 | if (sess.verbose>4) | |
2887 | clog << "alternative line " << lineno << " rejected: nsrcs=" << nsrcs << endl; | |
2888 | return false; | |
2889 | } | |
2890 | ||
2891 | // We also try to filter out lines that leave the selected | |
2892 | // functions (if any). | |
2893 | ||
86bf665e TM |
2894 | dwarf_line_t line(srcsp[0]); |
2895 | Dwarf_Addr addr = line.addr(); | |
fedd4090 | 2896 | |
86bf665e | 2897 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
fedd4090 FCE |
2898 | i != q->filtered_functions.end(); ++i) |
2899 | { | |
3e961ba6 | 2900 | if (q->dw.die_has_pc (i->die, addr)) |
fedd4090 FCE |
2901 | { |
2902 | if (q->sess.verbose>4) | |
3e961ba6 | 2903 | clog << "alternative line " << lineno << " accepted: fn=" << i->name << endl; |
fedd4090 FCE |
2904 | return true; |
2905 | } | |
2906 | } | |
2907 | ||
86bf665e | 2908 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); |
fedd4090 FCE |
2909 | i != q->filtered_inlines.end(); ++i) |
2910 | { | |
3e961ba6 | 2911 | if (q->dw.die_has_pc (i->die, addr)) |
fedd4090 FCE |
2912 | { |
2913 | if (sess.verbose>4) | |
3e961ba6 | 2914 | clog << "alternative line " << lineno << " accepted: ifn=" << i->name << endl; |
fedd4090 FCE |
2915 | return true; |
2916 | } | |
2917 | } | |
2918 | ||
2919 | if (sess.verbose>4) | |
2920 | clog << "alternative line " << lineno << " rejected: leaves selected fns" << endl; | |
2921 | return false; | |
2922 | } | |
2923 | ||
0b8f6579 JB |
2924 | /* This basically only goes one level down from the compile unit so it |
2925 | * only picks up top level stuff (i.e. nothing in a lower scope) */ | |
2926 | int | |
2927 | dwflpp::iterate_over_globals (int (* callback)(Dwarf_Die *, void *), | |
2928 | void * data) | |
2929 | { | |
2930 | int rc = DWARF_CB_OK; | |
2931 | Dwarf_Die die; | |
2932 | ||
2933 | assert (module); | |
2934 | assert (cu); | |
2935 | assert (dwarf_tag(cu) == DW_TAG_compile_unit); | |
2936 | ||
2937 | if (dwarf_child(cu, &die) != 0) | |
2938 | return rc; | |
2939 | ||
2940 | do { | |
2941 | /* We're only currently looking for structures and unions, | |
2942 | * although other types of declarations exist */ | |
2943 | if (dwarf_tag(&die) != DW_TAG_structure_type && | |
2944 | dwarf_tag(&die) != DW_TAG_union_type) | |
2945 | continue; | |
2946 | ||
2947 | rc = (*callback)(&die, data); | |
2948 | if (rc != DWARF_CB_OK) | |
2949 | break; | |
2950 | ||
2951 | } while (dwarf_siblingof(&die, &die) == 0); | |
2952 | ||
2953 | return rc; | |
2954 | } | |
fedd4090 | 2955 | |
6561773f | 2956 | int |
2da9cedb JS |
2957 | dwflpp::iterate_over_functions (int (* callback)(Dwarf_Die * func, base_query * q), |
2958 | base_query * q, const string& function, | |
2959 | bool has_statement_num) | |
6561773f FCE |
2960 | { |
2961 | int rc = DWARF_CB_OK; | |
2962 | assert (module); | |
2963 | assert (cu); | |
41c262f3 | 2964 | |
6561773f FCE |
2965 | string key = module_name + ":" + cu_name; |
2966 | cu_function_cache_t *v = cu_function_cache[key]; | |
2967 | if (v == 0) | |
2968 | { | |
2969 | v = new cu_function_cache_t; | |
2970 | cu_function_cache[key] = v; | |
2971 | dwarf_getfuncs (cu, cu_function_caching_callback, v, 0); | |
2972 | if (q->sess.verbose > 4) | |
2973 | clog << "function cache " << key << " size " << v->size() << endl; | |
2974 | } | |
41c262f3 | 2975 | |
2da9cedb | 2976 | string subkey = function; |
6561773f FCE |
2977 | if (v->find(subkey) != v->end()) |
2978 | { | |
2171f774 | 2979 | Dwarf_Die die = v->find(subkey)->second; |
6561773f FCE |
2980 | if (q->sess.verbose > 4) |
2981 | clog << "function cache " << key << " hit " << subkey << endl; | |
2da9cedb | 2982 | return (*callback)(& die, q); |
6561773f FCE |
2983 | } |
2984 | else if (name_has_wildcard (subkey)) | |
2985 | { | |
2986 | for (cu_function_cache_t::iterator it = v->begin(); it != v->end(); it++) | |
2987 | { | |
f76427a2 | 2988 | if (pending_interrupts) return DWARF_CB_ABORT; |
6561773f FCE |
2989 | string func_name = it->first; |
2990 | Dwarf_Die die = it->second; | |
2991 | if (function_name_matches_pattern (func_name, subkey)) | |
2992 | { | |
2993 | if (q->sess.verbose > 4) | |
2994 | clog << "function cache " << key << " match " << func_name << " vs " << subkey << endl; | |
41c262f3 | 2995 | |
2da9cedb | 2996 | rc = (*callback)(& die, q); |
6561773f FCE |
2997 | if (rc != DWARF_CB_OK) break; |
2998 | } | |
2999 | } | |
3000 | } | |
2da9cedb | 3001 | else if (has_statement_num) // searching all for kernel.statement |
cf314c0f WH |
3002 | { |
3003 | for (cu_function_cache_t::iterator it = v->begin(); it != v->end(); it++) | |
3004 | { | |
3005 | Dwarf_Die die = it->second; | |
2da9cedb | 3006 | rc = (*callback)(& die, q); |
cf314c0f WH |
3007 | if (rc != DWARF_CB_OK) break; |
3008 | } | |
3009 | } | |
6561773f FCE |
3010 | else // not a wildcard and no match in this CU |
3011 | { | |
3012 | // do nothing | |
3013 | } | |
3014 | return rc; | |
3015 | } | |
3016 | ||
3017 | ||
fedd4090 | 3018 | |
98afd80e | 3019 | struct dwarf_builder: public derived_probe_builder |
b55bc428 | 3020 | { |
e38d6504 | 3021 | dwflpp *kern_dw; |
7a24d422 | 3022 | map <string,dwflpp*> user_dw; |
b8da0ad1 | 3023 | dwarf_builder(): kern_dw(0) {} |
aa30ccd3 | 3024 | |
7a24d422 FCE |
3025 | |
3026 | /* NB: not virtual, so can be called from dtor too: */ | |
06aca46a | 3027 | void dwarf_build_no_more (bool verbose) |
aa30ccd3 FCE |
3028 | { |
3029 | if (kern_dw) | |
3030 | { | |
7a24d422 FCE |
3031 | if (verbose) |
3032 | clog << "dwarf_builder releasing kernel dwflpp" << endl; | |
aa30ccd3 FCE |
3033 | delete kern_dw; |
3034 | kern_dw = 0; | |
3035 | } | |
7a24d422 FCE |
3036 | |
3037 | for (map<string,dwflpp*>::iterator udi = user_dw.begin(); | |
3038 | udi != user_dw.end(); | |
3039 | udi ++) | |
3040 | { | |
3041 | if (verbose) | |
3042 | clog << "dwarf_builder releasing user dwflpp " << udi->first << endl; | |
3043 | delete udi->second; | |
3044 | } | |
3045 | user_dw.erase (user_dw.begin(), user_dw.end()); | |
3046 | } | |
3047 | ||
3048 | void build_no_more (systemtap_session &s) | |
3049 | { | |
3050 | dwarf_build_no_more (s.verbose > 3); | |
aa30ccd3 FCE |
3051 | } |
3052 | ||
e38d6504 RM |
3053 | ~dwarf_builder() |
3054 | { | |
7a24d422 | 3055 | dwarf_build_no_more (false); |
c8959a29 | 3056 | } |
aa30ccd3 | 3057 | |
5227f1ea | 3058 | virtual void build(systemtap_session & sess, |
7a053d3b | 3059 | probe * base, |
20c6c071 | 3060 | probe_point * location, |
86bf665e | 3061 | literal_map_t const & parameters, |
20c6c071 | 3062 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
3063 | }; |
3064 | ||
888af770 | 3065 | |
5227f1ea GH |
3066 | dwarf_query::dwarf_query(systemtap_session & sess, |
3067 | probe * base_probe, | |
20c6c071 GH |
3068 | probe_point * base_loc, |
3069 | dwflpp & dw, | |
86bf665e | 3070 | literal_map_t const & params, |
20c6c071 | 3071 | vector<derived_probe *> & results) |
c4ce66a1 JS |
3072 | : base_query(dw, params), results(results), |
3073 | base_probe(base_probe), base_loc(base_loc) | |
bd2b1e68 GH |
3074 | { |
3075 | // Reduce the query to more reasonable semantic values (booleans, | |
3076 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
3077 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
3078 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
3079 | ||
3080 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
3081 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
3082 | ||
0f336e95 SC |
3083 | has_label = get_string_param(params, TOK_LABEL, label_val); |
3084 | ||
b8da0ad1 FCE |
3085 | has_call = has_null_param(params, TOK_CALL); |
3086 | has_inline = has_null_param(params, TOK_INLINE); | |
bd2b1e68 | 3087 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 3088 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 FCE |
3089 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
3090 | ||
bd2b1e68 GH |
3091 | if (has_function_str) |
3092 | spec_type = parse_function_spec(function_str_val); | |
3093 | else if (has_statement_str) | |
3094 | spec_type = parse_function_spec(statement_str_val); | |
0daad364 | 3095 | |
b8da0ad1 | 3096 | build_blacklist(); // XXX: why not reuse amongst dwarf_query instances? |
5f0a03a6 JK |
3097 | dbinfo_reqt = assess_dbinfo_reqt(); |
3098 | query_done = false; | |
0daad364 JS |
3099 | } |
3100 | ||
3101 | ||
2c384610 | 3102 | void |
5f0a03a6 | 3103 | dwarf_query::query_module_dwarf() |
2c384610 DS |
3104 | { |
3105 | if (has_function_num || has_statement_num) | |
3106 | { | |
3107 | // If we have module("foo").function(0xbeef) or | |
3108 | // module("foo").statement(0xbeef), the address is relative | |
3109 | // to the start of the module, so we seek the function | |
3110 | // number plus the module's bias. | |
3111 | ||
3112 | Dwarf_Addr addr; | |
3113 | if (has_function_num) | |
3114 | addr = function_num_val; | |
3115 | else | |
3116 | addr = statement_num_val; | |
4baf0e53 | 3117 | |
2c384610 DS |
3118 | // NB: we don't need to add the module base address or bias |
3119 | // value here (for reasons that may be coincidental). | |
3120 | dw.query_cu_containing_module_address(addr, this); | |
3121 | } | |
3122 | else | |
3123 | { | |
3124 | // Otherwise if we have a function("foo") or statement("foo") | |
3125 | // specifier, we have to scan over all the CUs looking for | |
3126 | // the function(s) in question | |
3127 | assert(has_function_str || has_statement_str); | |
3128 | dw.iterate_over_cus(&query_cu, this); | |
3129 | } | |
3130 | } | |
3131 | ||
5f0a03a6 JK |
3132 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
3133 | dwarf_query * q); | |
3134 | ||
3135 | void | |
3136 | dwarf_query::query_module_symtab() | |
3137 | { | |
3138 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
3139 | if (dbinfo_reqt == dbr_need_dwarf) | |
3140 | return; | |
3141 | ||
3142 | module_info *mi = dw.mod_info; | |
3143 | if (dbinfo_reqt == dbr_need_symtab) | |
3144 | { | |
3145 | if (mi->symtab_status == info_unknown) | |
3146 | mi->get_symtab(this); | |
3147 | if (mi->symtab_status == info_absent) | |
3148 | return; | |
3149 | } | |
3150 | ||
3151 | func_info *fi = NULL; | |
3152 | symbol_table *sym_table = mi->sym_table; | |
3153 | ||
3154 | if (has_function_str) | |
3155 | { | |
3156 | // Per dwarf_query::assess_dbinfo_reqt()... | |
3157 | assert(spec_type == function_alone); | |
3158 | if (dw.name_has_wildcard(function_str_val)) | |
3159 | { | |
3160 | // Until we augment the blacklist sufficently... | |
3161 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
3162 | { | |
3163 | // e.g., kernel.function("*") | |
3164 | cerr << "Error: Pattern '" | |
3165 | << function_str_val | |
3166 | << "' matches every instruction address in the symbol table," | |
3167 | << endl | |
3168 | << "some of which aren't even functions." | |
3169 | << " Please be more precise." | |
3170 | << endl; | |
3171 | return; | |
3172 | } | |
2e67a43b TM |
3173 | symbol_table::iterator_t iter; |
3174 | for (iter = sym_table->list_by_addr.begin(); | |
3175 | iter != sym_table->list_by_addr.end(); | |
3176 | ++iter) | |
5f0a03a6 | 3177 | { |
2e67a43b | 3178 | fi = *iter; |
5f0a03a6 JK |
3179 | if (!null_die(&fi->die)) |
3180 | continue; // already handled in query_module_dwarf() | |
3181 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
3182 | query_func_info(fi->addr, *fi, this); | |
3183 | } | |
3184 | } | |
3185 | else | |
3186 | { | |
3187 | fi = sym_table->lookup_symbol(function_str_val); | |
3188 | if (fi && null_die(&fi->die)) | |
3189 | query_func_info(fi->addr, *fi, this); | |
3190 | } | |
3191 | } | |
3192 | else | |
3193 | { | |
3194 | assert(has_function_num || has_statement_num); | |
3195 | // Find the "function" in which the indicated address resides. | |
3196 | Dwarf_Addr addr = | |
3197 | (has_function_num ? function_num_val : statement_num_val); | |
3198 | fi = sym_table->get_func_containing_address(addr); | |
3199 | if (!fi) | |
3200 | { | |
3201 | cerr << "Warning: address " | |
3202 | << hex << addr << dec | |
3203 | << " out of range for module " | |
3204 | << dw.module_name; | |
3205 | return; | |
3206 | } | |
3207 | if (!null_die(&fi->die)) | |
3208 | { | |
3209 | // addr looks like it's in the compilation unit containing | |
3210 | // the indicated function, but query_module_dwarf() didn't | |
3211 | // match addr to any compilation unit, so addr must be | |
3212 | // above that cu's address range. | |
3213 | cerr << "Warning: address " | |
3214 | << hex << addr << dec | |
3215 | << " maps to no known compilation unit in module " | |
3216 | << dw.module_name; | |
3217 | return; | |
3218 | } | |
3219 | query_func_info(fi->addr, *fi, this); | |
3220 | } | |
3221 | } | |
3222 | ||
3223 | void | |
3224 | dwarf_query::handle_query_module() | |
3225 | { | |
3226 | dw.get_module_dwarf(false, | |
3227 | (dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab)); | |
3228 | if (dw.mod_info->dwarf_status == info_present) | |
3229 | query_module_dwarf(); | |
3230 | // Consult the symbol table if we haven't found all we're looking for. | |
3231 | // asm functions can show up in the symbol table but not in dwarf. | |
3232 | if (sess.consult_symtab && !query_done) | |
3233 | query_module_symtab(); | |
3234 | } | |
3235 | ||
2c384610 | 3236 | |
0daad364 JS |
3237 | void |
3238 | dwarf_query::build_blacklist() | |
3239 | { | |
91af0778 FCE |
3240 | // No blacklist for userspace. |
3241 | if (has_process) | |
3242 | return; | |
3243 | ||
a7301475 FCE |
3244 | // We build up the regexps in these strings |
3245 | ||
3246 | // Add ^ anchors at the front; $ will be added just before regcomp. | |
3247 | ||
a7301475 FCE |
3248 | string blfn = "^("; |
3249 | string blfn_ret = "^("; | |
3250 | string blfile = "^("; | |
3251 | ||
e4c58386 | 3252 | blfile += "kernel/kprobes.c"; // first alternative, no "|" |
a7301475 | 3253 | blfile += "|arch/.*/kernel/kprobes.c"; |
275a898f | 3254 | // Older kernels need ... |
02a929d1 | 3255 | blfile += "|include/asm/io.h"; |
275a898f AM |
3256 | blfile += "|include/asm/bitops.h"; |
3257 | // While newer ones need ... | |
3258 | blfile += "|arch/.*/include/asm/io.h"; | |
3259 | blfile += "|arch/.*/include/asm/bitops.h"; | |
02a929d1 | 3260 | blfile += "|drivers/ide/ide-iops.c"; |
a7301475 FCE |
3261 | |
3262 | // XXX: it would be nice if these blacklisted functions were pulled | |
3263 | // in dynamically, instead of being statically defined here. | |
49f426d9 FCE |
3264 | // Perhaps it could be populated from script files. A "noprobe |
3265 | // kernel.function("...")" construct might do the trick. | |
0daad364 | 3266 | |
b20febf3 | 3267 | // Most of these are marked __kprobes in newer kernels. We list |
a7301475 FCE |
3268 | // them here (anyway) so the translator can block them on older |
3269 | // kernels that don't have the __kprobes function decorator. This | |
3270 | // also allows detection of problems at translate- rather than | |
3271 | // run-time. | |
3272 | ||
e4c58386 | 3273 | blfn += "atomic_notifier_call_chain"; // first blfn; no "|" |
a7301475 FCE |
3274 | blfn += "|default_do_nmi"; |
3275 | blfn += "|__die"; | |
3276 | blfn += "|die_nmi"; | |
3277 | blfn += "|do_debug"; | |
3278 | blfn += "|do_general_protection"; | |
3279 | blfn += "|do_int3"; | |
3280 | blfn += "|do_IRQ"; | |
3281 | blfn += "|do_page_fault"; | |
3282 | blfn += "|do_sparc64_fault"; | |
3283 | blfn += "|do_trap"; | |
3284 | blfn += "|dummy_nmi_callback"; | |
3285 | blfn += "|flush_icache_range"; | |
3286 | blfn += "|ia64_bad_break"; | |
3287 | blfn += "|ia64_do_page_fault"; | |
3288 | blfn += "|ia64_fault"; | |
3289 | blfn += "|io_check_error"; | |
3290 | blfn += "|mem_parity_error"; | |
3291 | blfn += "|nmi_watchdog_tick"; | |
3292 | blfn += "|notifier_call_chain"; | |
3293 | blfn += "|oops_begin"; | |
3294 | blfn += "|oops_end"; | |
3295 | blfn += "|program_check_exception"; | |
3296 | blfn += "|single_step_exception"; | |
3297 | blfn += "|sync_regs"; | |
3298 | blfn += "|unhandled_fault"; | |
3299 | blfn += "|unknown_nmi_error"; | |
3300 | ||
3301 | // Lots of locks | |
3302 | blfn += "|.*raw_.*lock.*"; | |
3303 | blfn += "|.*read_.*lock.*"; | |
3304 | blfn += "|.*write_.*lock.*"; | |
3305 | blfn += "|.*spin_.*lock.*"; | |
3306 | blfn += "|.*rwlock_.*lock.*"; | |
3307 | blfn += "|.*rwsem_.*lock.*"; | |
3308 | blfn += "|.*mutex_.*lock.*"; | |
3309 | blfn += "|raw_.*"; | |
3310 | blfn += "|.*seq_.*lock.*"; | |
3311 | ||
a11f4bae | 3312 | // atomic functions |
2ca12712 JS |
3313 | blfn += "|atomic_.*"; |
3314 | blfn += "|atomic64_.*"; | |
a11f4bae SD |
3315 | |
3316 | // few other problematic cases | |
3317 | blfn += "|get_bh"; | |
3318 | blfn += "|put_bh"; | |
3319 | ||
a7301475 FCE |
3320 | // Experimental |
3321 | blfn += "|.*apic.*|.*APIC.*"; | |
3322 | blfn += "|.*softirq.*"; | |
3323 | blfn += "|.*IRQ.*"; | |
3324 | blfn += "|.*_intr.*"; | |
3325 | blfn += "|__delay"; | |
3326 | blfn += "|.*kernel_text.*"; | |
3327 | blfn += "|get_current"; | |
3328 | blfn += "|current_.*"; | |
3329 | blfn += "|.*exception_tables.*"; | |
3330 | blfn += "|.*setup_rt_frame.*"; | |
c931ec8a | 3331 | |
a8c9be6f | 3332 | // PR 5759, CONFIG_PREEMPT kernels |
a7301475 FCE |
3333 | blfn += "|.*preempt_count.*"; |
3334 | blfn += "|preempt_schedule"; | |
a8c9be6f | 3335 | |
e4c58386 FCE |
3336 | // These functions don't return, so return probes would never be recovered |
3337 | blfn_ret += "do_exit"; // no "|" | |
3338 | blfn_ret += "|sys_exit"; | |
3339 | blfn_ret += "|sys_exit_group"; | |
3340 | ||
a721fbcf MH |
3341 | // __switch_to changes "current" on x86_64 and i686, so return probes |
3342 | // would cause kernel panic, and it is marked as "__kprobes" on x86_64 | |
0daad364 | 3343 | if (sess.architecture == "x86_64") |
a7301475 | 3344 | blfn += "|__switch_to"; |
a721fbcf | 3345 | if (sess.architecture == "i686") |
a7301475 | 3346 | blfn_ret += "|__switch_to"; |
0daad364 | 3347 | |
a7301475 FCE |
3348 | blfn += ")$"; |
3349 | blfn_ret += ")$"; | |
3350 | blfile += ")$"; | |
3351 | ||
41c262f3 | 3352 | if (sess.verbose > 2) |
e4c58386 FCE |
3353 | { |
3354 | clog << "blacklist regexps:" << endl; | |
3355 | clog << "blfn: " << blfn << endl; | |
3356 | clog << "blfn_ret: " << blfn_ret << endl; | |
3357 | clog << "blfile: " << blfile << endl; | |
3358 | } | |
3359 | ||
a7301475 FCE |
3360 | int rc = regcomp (& blacklist_func, blfn.c_str(), REG_NOSUB|REG_EXTENDED); |
3361 | if (rc) throw semantic_error ("blacklist_func regcomp failed"); | |
3362 | rc = regcomp (& blacklist_func_ret, blfn_ret.c_str(), REG_NOSUB|REG_EXTENDED); | |
3363 | if (rc) throw semantic_error ("blacklist_func_ret regcomp failed"); | |
3364 | rc = regcomp (& blacklist_file, blfile.c_str(), REG_NOSUB|REG_EXTENDED); | |
3365 | if (rc) throw semantic_error ("blacklist_file regcomp failed"); | |
7a053d3b | 3366 | } |
bd2b1e68 GH |
3367 | |
3368 | ||
bd2b1e68 | 3369 | function_spec_type |
20c6c071 | 3370 | dwarf_query::parse_function_spec(string & spec) |
bd2b1e68 GH |
3371 | { |
3372 | string::const_iterator i = spec.begin(), e = spec.end(); | |
7a053d3b | 3373 | |
bd2b1e68 GH |
3374 | function.clear(); |
3375 | file.clear(); | |
879eb9e9 SC |
3376 | line[0] = 0; |
3377 | line[1] = 0; | |
bd2b1e68 GH |
3378 | |
3379 | while (i != e && *i != '@') | |
3380 | { | |
0c8b7d37 | 3381 | if (*i == ':' || *i == '+') |
bd2b1e68 GH |
3382 | goto bad; |
3383 | function += *i++; | |
3384 | } | |
3385 | ||
3386 | if (i == e) | |
3387 | { | |
b0ee93c4 | 3388 | if (sess.verbose>2) |
7a053d3b RM |
3389 | clog << "parsed '" << spec |
3390 | << "' -> func '" << function | |
db22e55f | 3391 | << "'\n"; |
bd2b1e68 GH |
3392 | return function_alone; |
3393 | } | |
3394 | ||
3395 | if (i++ == e) | |
3396 | goto bad; | |
3397 | ||
0c8b7d37 | 3398 | while (i != e && *i != ':' && *i != '+') |
bd2b1e68 | 3399 | file += *i++; |
41c262f3 | 3400 | if (*i == ':') |
879eb9e9 SC |
3401 | { |
3402 | if (*(i + 1) == '*') | |
3403 | line_type = WILDCARD; | |
3404 | else | |
3405 | line_type = ABSOLUTE; | |
3406 | } | |
0c8b7d37 SC |
3407 | else if (*i == '+') |
3408 | line_type = RELATIVE; | |
7a053d3b | 3409 | |
bd2b1e68 GH |
3410 | if (i == e) |
3411 | { | |
b0ee93c4 | 3412 | if (sess.verbose>2) |
7a053d3b RM |
3413 | clog << "parsed '" << spec |
3414 | << "' -> func '"<< function | |
3415 | << "', file '" << file | |
db22e55f | 3416 | << "'\n"; |
bd2b1e68 GH |
3417 | return function_and_file; |
3418 | } | |
3419 | ||
3420 | if (i++ == e) | |
3421 | goto bad; | |
3422 | ||
3423 | try | |
3424 | { | |
879eb9e9 SC |
3425 | if (line_type != WILDCARD) |
3426 | { | |
3427 | string::const_iterator dash = i; | |
41c262f3 | 3428 | |
879eb9e9 SC |
3429 | while (dash != e && *dash != '-') |
3430 | dash++; | |
3431 | if (dash == e) | |
3432 | line[0] = line[1] = lex_cast<int>(string(i, e)); | |
3433 | else | |
3434 | { | |
3435 | line_type = RANGE; | |
3436 | line[0] = lex_cast<int>(string(i, dash)); | |
3437 | line[1] = lex_cast<int>(string(dash + 1, e)); | |
3438 | } | |
3439 | } | |
41c262f3 | 3440 | |
b0ee93c4 | 3441 | if (sess.verbose>2) |
7a053d3b RM |
3442 | clog << "parsed '" << spec |
3443 | << "' -> func '"<< function | |
3444 | << "', file '" << file | |
db22e55f | 3445 | << "', line " << line << "\n"; |
bd2b1e68 GH |
3446 | return function_file_and_line; |
3447 | } | |
3448 | catch (runtime_error & exn) | |
3449 | { | |
3450 | goto bad; | |
3451 | } | |
3452 | ||
3453 | bad: | |
7a053d3b | 3454 | throw semantic_error("malformed specification '" + spec + "'", |
20c6c071 | 3455 | base_probe->tok); |
bd2b1e68 GH |
3456 | } |
3457 | ||
3458 | ||
91af0778 | 3459 | #if 0 |
b20febf3 | 3460 | // Forward declaration. |
91af0778 | 3461 | static int query_kernel_module (Dwfl_Module *, void **, const char *, |
b20febf3 | 3462 | Dwarf_Addr, void *); |
91af0778 | 3463 | #endif |
7e1279ea | 3464 | |
2930abc7 | 3465 | |
b8da0ad1 | 3466 | // XXX: pull this into dwflpp |
b20febf3 FCE |
3467 | static bool |
3468 | in_kprobes_function(systemtap_session& sess, Dwarf_Addr addr) | |
2930abc7 | 3469 | { |
84048984 | 3470 | if (sess.sym_kprobes_text_start != 0 && sess.sym_kprobes_text_end != 0) |
1d3a40b6 DS |
3471 | { |
3472 | // If the probe point address is anywhere in the __kprobes | |
3473 | // address range, we can't use this probe point. | |
84048984 | 3474 | if (addr >= sess.sym_kprobes_text_start && addr < sess.sym_kprobes_text_end) |
1d3a40b6 DS |
3475 | return true; |
3476 | } | |
3477 | return false; | |
3478 | } | |
3479 | ||
20e4a32c | 3480 | |
36f9dd1d | 3481 | bool |
b20febf3 FCE |
3482 | dwarf_query::blacklisted_p(const string& funcname, |
3483 | const string& filename, | |
78f6bba6 | 3484 | int, |
b20febf3 FCE |
3485 | const string& module, |
3486 | const string& section, | |
36f9dd1d FCE |
3487 | Dwarf_Addr addr) |
3488 | { | |
91af0778 FCE |
3489 | if (has_process) |
3490 | return false; // no blacklist for userspace | |
3491 | ||
b20febf3 | 3492 | if (section.substr(0, 6) == string(".init.") || |
f90f9261 SD |
3493 | section.substr(0, 6) == string(".exit.") || |
3494 | section.substr(0, 9) == string(".devinit.") || | |
3495 | section.substr(0, 9) == string(".devexit.") || | |
3496 | section.substr(0, 9) == string(".cpuinit.") || | |
3497 | section.substr(0, 9) == string(".cpuexit.") || | |
3498 | section.substr(0, 9) == string(".meminit.") || | |
3499 | section.substr(0, 9) == string(".memexit.")) | |
703621ae | 3500 | { |
b8da0ad1 FCE |
3501 | // NB: module .exit. routines could be probed in theory: |
3502 | // if the exit handler in "struct module" is diverted, | |
3503 | // first inserting the kprobes | |
3504 | // then allowing the exit code to run | |
3505 | // then removing these kprobes | |
b20febf3 FCE |
3506 | if (sess.verbose>1) |
3507 | clog << " skipping - init/exit"; | |
3508 | return true; | |
703621ae FCE |
3509 | } |
3510 | ||
1d3a40b6 | 3511 | // Check for function marked '__kprobes'. |
b20febf3 | 3512 | if (module == TOK_KERNEL && in_kprobes_function(sess, addr)) |
1d3a40b6 DS |
3513 | { |
3514 | if (sess.verbose>1) | |
b20febf3 | 3515 | clog << " skipping - __kprobes"; |
1d3a40b6 DS |
3516 | return true; |
3517 | } | |
4baf0e53 | 3518 | |
a7301475 FCE |
3519 | // Check probe point against blacklist. |
3520 | int goodfn = regexec (&blacklist_func, funcname.c_str(), 0, NULL, 0); | |
3521 | if (has_return) | |
3522 | goodfn = goodfn && regexec (&blacklist_func_ret, funcname.c_str(), 0, NULL, 0); | |
3523 | int goodfile = regexec (&blacklist_file, filename.c_str(), 0, NULL, 0); | |
3524 | ||
3525 | if (! (goodfn && goodfile)) | |
36f9dd1d | 3526 | { |
e2ae0696 LR |
3527 | if (sess.guru_mode) |
3528 | { | |
3529 | if (sess.verbose>1) | |
3530 | clog << " guru mode enabled - ignoring blacklist"; | |
3531 | } | |
3532 | else | |
3533 | { | |
3534 | if (sess.verbose>1) | |
3535 | clog << " skipping - blacklisted"; | |
3536 | return true; | |
3537 | } | |
36f9dd1d FCE |
3538 | } |
3539 | ||
3540 | // This probe point is not blacklisted. | |
3541 | return false; | |
3542 | } | |
3543 | ||
d64e82b1 SD |
3544 | string dwarf_query::get_blacklist_section(Dwarf_Addr addr) |
3545 | { | |
d64e82b1 | 3546 | string blacklist_section; |
f9331b29 RM |
3547 | Dwarf_Addr bias; |
3548 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
3549 | // because dwfl_module_getelf can force costly section relocations | |
3550 | // we don't really need, while either will do for this purpose. | |
3551 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias)) | |
3552 | ?: dwfl_module_getelf (dw.module, &bias)); | |
3553 | ||
3554 | Dwarf_Addr offset = addr - bias; | |
d64e82b1 SD |
3555 | if (elf) |
3556 | { | |
3557 | Elf_Scn* scn = 0; | |
3558 | size_t shstrndx; | |
86bf665e | 3559 | dwfl_assert ("getshstrndx", elf_getshstrndx (elf, &shstrndx)); |
d64e82b1 SD |
3560 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
3561 | { | |
3562 | GElf_Shdr shdr_mem; | |
3563 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); | |
3564 | if (! shdr) continue; // XXX error? | |
3565 | ||
f9331b29 RM |
3566 | if (!(shdr->sh_flags & SHF_ALLOC)) |
3567 | continue; | |
3568 | ||
d64e82b1 SD |
3569 | GElf_Addr start = shdr->sh_addr; |
3570 | GElf_Addr end = start + shdr->sh_size; | |
3571 | if (! (offset >= start && offset < end)) | |
3572 | continue; | |
3573 | ||
3574 | blacklist_section = elf_strptr (elf, shstrndx, shdr->sh_name); | |
3575 | break; | |
3576 | } | |
3577 | } | |
3578 | return blacklist_section; | |
3579 | } | |
36f9dd1d | 3580 | |
b20febf3 | 3581 | |
36f9dd1d | 3582 | void |
b20febf3 FCE |
3583 | dwarf_query::add_probe_point(const string& funcname, |
3584 | const char* filename, | |
36f9dd1d | 3585 | int line, |
b20febf3 | 3586 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
3587 | Dwarf_Addr addr) |
3588 | { | |
b20febf3 FCE |
3589 | string reloc_section; // base section for relocation purposes |
3590 | Dwarf_Addr reloc_addr = addr; // relocated | |
3591 | string blacklist_section; // linking section for blacklist purposes | |
3592 | const string& module = dw.module_name; // "kernel" or other | |
36f9dd1d | 3593 | |
37ebca01 FCE |
3594 | assert (! has_absolute); // already handled in dwarf_builder::build() |
3595 | ||
5f0a03a6 JK |
3596 | if (!dw.module) |
3597 | { | |
3598 | assert(module == TOK_KERNEL); | |
3599 | reloc_section = ""; | |
3600 | blacklist_section = ""; | |
3601 | } | |
3602 | else if (dwfl_module_relocations (dw.module) > 0) | |
2930abc7 | 3603 | { |
17c128f2 | 3604 | // This is a relocatable module; libdwfl already knows its |
b20febf3 FCE |
3605 | // sections, so we can relativize addr. |
3606 | int idx = dwfl_module_relocate_address (dw.module, &reloc_addr); | |
3607 | const char* r_s = dwfl_module_relocation_info (dw.module, idx, NULL); | |
3608 | if (r_s) | |
3609 | reloc_section = r_s; | |
3610 | blacklist_section = reloc_section; | |
d64e82b1 SD |
3611 | |
3612 | if(reloc_section == "" && dwfl_module_relocations (dw.module) == 1) | |
17c128f2 FCE |
3613 | { |
3614 | blacklist_section = this->get_blacklist_section(addr); | |
3615 | reloc_section = ".dynamic"; | |
4b0eb118 | 3616 | reloc_addr = addr; |
17c128f2 | 3617 | } |
2930abc7 FCE |
3618 | } |
3619 | else | |
3620 | { | |
d64e82b1 | 3621 | blacklist_section = this->get_blacklist_section(addr); |
17c128f2 | 3622 | reloc_section = ".absolute"; |
2930abc7 FCE |
3623 | } |
3624 | ||
7f9f3386 FCE |
3625 | if (sess.verbose > 1) |
3626 | { | |
b20febf3 FCE |
3627 | clog << "probe " << funcname << "@" << filename << ":" << line; |
3628 | if (string(module) == TOK_KERNEL) | |
3629 | clog << " kernel"; | |
91af0778 | 3630 | else if (has_module) |
b20febf3 | 3631 | clog << " module=" << module; |
91af0778 FCE |
3632 | else if (has_process) |
3633 | clog << " process=" << module; | |
b20febf3 FCE |
3634 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
3635 | if (blacklist_section != "") clog << " section=" << blacklist_section; | |
3636 | clog << " pc=0x" << hex << addr << dec; | |
7f9f3386 | 3637 | } |
4baf0e53 | 3638 | |
b20febf3 FCE |
3639 | bool bad = blacklisted_p (funcname, filename, line, module, blacklist_section, addr); |
3640 | if (sess.verbose > 1) | |
3641 | clog << endl; | |
7f9f3386 | 3642 | |
84048984 FCE |
3643 | if (module == TOK_KERNEL) |
3644 | { | |
3645 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
3646 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 3647 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
3648 | } |
3649 | ||
b20febf3 FCE |
3650 | if (! bad) |
3651 | { | |
1a0dbc5a | 3652 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
3653 | |
3654 | if (has_process) | |
3655 | { | |
3656 | results.push_back (new uprobe_derived_probe(funcname, filename, line, | |
3657 | module, 0, reloc_section, addr, reloc_addr, | |
3658 | *this, scope_die)); | |
3659 | } | |
3660 | else | |
3661 | { | |
3662 | assert (has_kernel || has_module); | |
3663 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 3664 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
3665 | *this, scope_die)); |
3666 | } | |
b20febf3 | 3667 | } |
2930abc7 FCE |
3668 | } |
3669 | ||
5f0a03a6 JK |
3670 | enum dbinfo_reqt |
3671 | dwarf_query::assess_dbinfo_reqt() | |
3672 | { | |
3673 | if (has_absolute) | |
3674 | { | |
3675 | // kernel.statement(NUM).absolute | |
3676 | return dbr_none; | |
3677 | } | |
3678 | if (has_inline) | |
3679 | { | |
3680 | // kernel.function("f").inline or module("m").function("f").inline | |
3681 | return dbr_need_dwarf; | |
3682 | } | |
3683 | if (has_function_str && spec_type == function_alone) | |
3684 | { | |
3685 | // kernel.function("f") or module("m").function("f") | |
3686 | return dbr_need_symtab; | |
3687 | } | |
3688 | if (has_statement_num) | |
3689 | { | |
3690 | // kernel.statement(NUM) or module("m").statement(NUM) | |
3691 | // Technically, all we need is the module offset (or _stext, for | |
3692 | // the kernel). But for that we need either the ELF file or (for | |
3693 | // _stext) the symbol table. In either case, the symbol table | |
3694 | // is available, and that allows us to map the NUM (address) | |
3695 | // to a function, which is goodness. | |
3696 | return dbr_need_symtab; | |
3697 | } | |
3698 | if (has_function_num) | |
3699 | { | |
3700 | // kernel.function(NUM) or module("m").function(NUM) | |
3701 | // Need the symbol table so we can back up from NUM to the | |
3702 | // start of the function. | |
3703 | return dbr_need_symtab; | |
3704 | } | |
3705 | // Symbol table tells us nothing about source files or line numbers. | |
3706 | return dbr_need_dwarf; | |
3707 | } | |
2930abc7 FCE |
3708 | |
3709 | ||
b8da0ad1 FCE |
3710 | // The critical determining factor when interpreting a pattern |
3711 | // string is, perhaps surprisingly: "presence of a lineno". The | |
3712 | // presence of a lineno changes the search strategy completely. | |
3713 | // | |
3714 | // Compare the two cases: | |
3715 | // | |
3716 | // 1. {statement,function}(foo@file.c:lineno) | |
3717 | // - find the files matching file.c | |
3718 | // - in each file, find the functions matching foo | |
3719 | // - query the file for line records matching lineno | |
3720 | // - iterate over the line records, | |
3721 | // - and iterate over the functions, | |
3722 | // - if(haspc(function.DIE, line.addr)) | |
3723 | // - if looking for statements: probe(lineno.addr) | |
3724 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
3725 | // | |
3726 | // 2. {statement,function}(foo@file.c) | |
3727 | // - find the files matching file.c | |
3728 | // - in each file, find the functions matching foo | |
3729 | // - probe(function.{entrypc,return,etc.}) | |
3730 | // | |
3731 | // Thus the first decision we make is based on the presence of a | |
3732 | // lineno, and we enter entirely different sets of callbacks | |
3733 | // depending on that decision. | |
3734 | // | |
3735 | // Note that the first case is a generalization fo the second, in that | |
3736 | // we could theoretically search through line records for matching | |
3737 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
3738 | // are already cached elsewhere, so we can do an "index scan" as an | |
3739 | // optimization. | |
7e1279ea | 3740 | |
bd2b1e68 | 3741 | static void |
4cd232e4 | 3742 | query_statement (string const & func, |
20e4a32c | 3743 | char const * file, |
4cd232e4 | 3744 | int line, |
bcc12710 | 3745 | Dwarf_Die *scope_die, |
20e4a32c | 3746 | Dwarf_Addr stmt_addr, |
4cd232e4 | 3747 | dwarf_query * q) |
bd2b1e68 | 3748 | { |
39bcd429 FCE |
3749 | try |
3750 | { | |
cee35f73 | 3751 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 3752 | line, scope_die, stmt_addr); |
39bcd429 FCE |
3753 | } |
3754 | catch (const semantic_error& e) | |
3755 | { | |
3756 | q->sess.print_error (e); | |
3757 | } | |
bd2b1e68 GH |
3758 | } |
3759 | ||
7e1279ea | 3760 | static void |
3e961ba6 | 3761 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
3762 | dwarf_query * q) |
3763 | { | |
b6581717 | 3764 | try |
7e1279ea | 3765 | { |
b6581717 GH |
3766 | if (q->has_return) |
3767 | { | |
3768 | throw semantic_error ("cannot probe .return of inline function '" + ii.name + "'"); | |
3769 | } | |
3770 | else | |
3771 | { | |
b0ee93c4 | 3772 | if (q->sess.verbose>2) |
20e4a32c | 3773 | clog << "querying entrypc " |
3e961ba6 | 3774 | << hex << ii.entrypc << dec |
db22e55f | 3775 | << " of instance of inline '" << ii.name << "'\n"; |
20e4a32c | 3776 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
3e961ba6 | 3777 | &ii.die, ii.entrypc, q); |
b6581717 | 3778 | } |
7e1279ea | 3779 | } |
b6581717 | 3780 | catch (semantic_error &e) |
7e1279ea | 3781 | { |
b6581717 | 3782 | q->sess.print_error (e); |
7e1279ea FCE |
3783 | } |
3784 | } | |
3785 | ||
3786 | static void | |
3787 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 3788 | func_info & fi, |
7e1279ea FCE |
3789 | dwarf_query * q) |
3790 | { | |
b6581717 | 3791 | try |
7e1279ea | 3792 | { |
b6581717 GH |
3793 | if (q->has_return) |
3794 | { | |
3795 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
3796 | // kretprobe based on the entrypc in this case. | |
20e4a32c | 3797 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
3798 | &fi.die, entrypc, q); |
3799 | } | |
3800 | else | |
3801 | { | |
35dc8b04 | 3802 | if (fi.prologue_end != 0) |
44f75386 | 3803 | { |
44f75386 FCE |
3804 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
3805 | &fi.die, fi.prologue_end, q); | |
3806 | } | |
3807 | else | |
3808 | { | |
3809 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
3810 | &fi.die, entrypc, q); | |
3811 | } | |
b6581717 | 3812 | } |
7e1279ea | 3813 | } |
b6581717 | 3814 | catch (semantic_error &e) |
7e1279ea | 3815 | { |
b6581717 | 3816 | q->sess.print_error (e); |
7e1279ea FCE |
3817 | } |
3818 | } | |
3819 | ||
3820 | ||
3821 | static void | |
86bf665e | 3822 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
3823 | { |
3824 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
3825 | ||
86bf665e | 3826 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 3827 | |
86bf665e | 3828 | int lineno = line.lineno(); |
847bf07f | 3829 | |
86bf665e | 3830 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
3831 | i != q->filtered_functions.end(); ++i) |
3832 | { | |
3e961ba6 | 3833 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 3834 | { |
b0ee93c4 | 3835 | if (q->sess.verbose>3) |
db22e55f | 3836 | clog << "function DIE lands on srcfile\n"; |
4cd232e4 | 3837 | if (q->has_statement_str) |
3e961ba6 | 3838 | query_statement (i->name, i->decl_file, |
847bf07f | 3839 | lineno, // NB: not q->line ! |
3e961ba6 | 3840 | &(i->die), addr, q); |
4cd232e4 | 3841 | else |
3e961ba6 | 3842 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 3843 | } |
20e4a32c RM |
3844 | } |
3845 | ||
86bf665e | 3846 | for (inline_instance_map_t::iterator i |
897820ca GH |
3847 | = q->filtered_inlines.begin(); |
3848 | i != q->filtered_inlines.end(); ++i) | |
3849 | { | |
3e961ba6 | 3850 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 3851 | { |
b0ee93c4 | 3852 | if (q->sess.verbose>3) |
db22e55f | 3853 | clog << "inline instance DIE lands on srcfile\n"; |
897820ca | 3854 | if (q->has_statement_str) |
3e961ba6 JB |
3855 | query_statement (i->name, i->decl_file, |
3856 | q->line[0], &(i->die), addr, q); | |
897820ca | 3857 | else |
3e961ba6 | 3858 | query_inline_instance_info (*i, q); |
897820ca | 3859 | } |
20e4a32c | 3860 | } |
7e1279ea FCE |
3861 | } |
3862 | ||
3863 | ||
4fa7b22b | 3864 | static int |
7e1279ea | 3865 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
3866 | { |
3867 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
7e1279ea | 3868 | assert (!q->has_statement_num); |
bd2b1e68 | 3869 | |
39bcd429 | 3870 | try |
7a053d3b | 3871 | { |
b0ee93c4 | 3872 | if (q->sess.verbose>2) |
db22e55f | 3873 | clog << "examining inline instance of " << q->dw.function_name << "\n"; |
7e1279ea | 3874 | |
4baf0e53 | 3875 | if ((q->has_function_str && ! q->has_call) |
b8da0ad1 | 3876 | || q->has_statement_str) |
7e1279ea | 3877 | { |
b0ee93c4 | 3878 | if (q->sess.verbose>2) |
db22e55f FCE |
3879 | clog << "selected inline instance of " << q->dw.function_name |
3880 | << "\n"; | |
7e1279ea FCE |
3881 | |
3882 | Dwarf_Addr entrypc; | |
3883 | if (q->dw.die_entrypc (die, &entrypc)) | |
3884 | { | |
3885 | inline_instance_info inl; | |
3886 | inl.die = *die; | |
3887 | inl.name = q->dw.function_name; | |
3e961ba6 | 3888 | inl.entrypc = entrypc; |
4cd232e4 GH |
3889 | q->dw.function_file (&inl.decl_file); |
3890 | q->dw.function_line (&inl.decl_line); | |
3e961ba6 | 3891 | q->filtered_inlines.push_back(inl); |
7e1279ea FCE |
3892 | } |
3893 | } | |
3894 | return DWARF_CB_OK; | |
3895 | } | |
3896 | catch (const semantic_error& e) | |
3897 | { | |
3898 | q->sess.print_error (e); | |
3899 | return DWARF_CB_ABORT; | |
3900 | } | |
3901 | } | |
bb788f9f | 3902 | |
7e1279ea | 3903 | static int |
2da9cedb | 3904 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 3905 | { |
2da9cedb | 3906 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
bb788f9f | 3907 | |
7e1279ea FCE |
3908 | try |
3909 | { | |
7e1279ea FCE |
3910 | q->dw.focus_on_function (func); |
3911 | ||
20e4a32c | 3912 | if (q->dw.func_is_inline () |
b8da0ad1 FCE |
3913 | && (! q->has_call) && (! q->has_return) |
3914 | && (((q->has_statement_str || q->has_function_str) | |
3915 | && q->dw.function_name_matches(q->function)))) | |
7e1279ea | 3916 | { |
b0ee93c4 | 3917 | if (q->sess.verbose>3) |
db22e55f FCE |
3918 | clog << "checking instances of inline " << q->dw.function_name |
3919 | << "\n"; | |
2da9cedb | 3920 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
275f40a6 FCE |
3921 | |
3922 | if (q->dw.function_name_final_match (q->function)) | |
3923 | return DWARF_CB_ABORT; | |
7e1279ea | 3924 | } |
396afcee | 3925 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 3926 | { |
7e1279ea FCE |
3927 | bool record_this_function = false; |
3928 | ||
3929 | if ((q->has_statement_str || q->has_function_str) | |
3930 | && q->dw.function_name_matches(q->function)) | |
3931 | { | |
3932 | record_this_function = true; | |
3933 | } | |
e4c58386 | 3934 | else if (q->has_function_num || q->has_statement_num) |
7e1279ea | 3935 | { |
e4c58386 | 3936 | Dwarf_Addr query_addr = |
9b692b91 SC |
3937 | (q->has_function_num ? q->function_num_val : |
3938 | q->has_statement_num ? q->statement_num_val : | |
3939 | (assert(0) , 0)); | |
7e1279ea FCE |
3940 | Dwarf_Die d; |
3941 | q->dw.function_die (&d); | |
20e4a32c | 3942 | |
86bf665e | 3943 | if (q->dw.die_has_pc (d, query_addr)) |
7e1279ea FCE |
3944 | record_this_function = true; |
3945 | } | |
3946 | ||
3947 | if (record_this_function) | |
3948 | { | |
b0ee93c4 | 3949 | if (q->sess.verbose>2) |
db22e55f | 3950 | clog << "selected function " << q->dw.function_name << "\n"; |
7e1279ea | 3951 | |
e4c58386 FCE |
3952 | func_info func; |
3953 | q->dw.function_die (&func.die); | |
3954 | func.name = q->dw.function_name; | |
3955 | q->dw.function_file (&func.decl_file); | |
3956 | q->dw.function_line (&func.decl_line); | |
3957 | ||
3958 | if (q->has_function_num || q->has_function_str || q->has_statement_str) | |
3959 | { | |
3960 | Dwarf_Addr entrypc; | |
3961 | if (q->dw.function_entrypc (&entrypc)) | |
3e961ba6 JB |
3962 | { |
3963 | func.entrypc = entrypc; | |
3964 | q->filtered_functions.push_back (func); | |
3965 | } | |
e4c58386 | 3966 | else |
552fdd9f JB |
3967 | /* this function just be fully inlined, just ignore it */ |
3968 | return DWARF_CB_OK; | |
e4c58386 FCE |
3969 | } |
3970 | else if (q->has_statement_num) | |
3971 | { | |
3e961ba6 JB |
3972 | func.entrypc = q->statement_num_val; |
3973 | q->filtered_functions.push_back (func); | |
275f40a6 FCE |
3974 | if (q->dw.function_name_final_match (q->function)) |
3975 | return DWARF_CB_ABORT; | |
e4c58386 FCE |
3976 | } |
3977 | else | |
3978 | assert(0); | |
3979 | ||
3980 | if (q->dw.function_name_final_match (q->function)) | |
3981 | return DWARF_CB_ABORT; | |
7e1279ea FCE |
3982 | } |
3983 | } | |
39bcd429 | 3984 | return DWARF_CB_OK; |
bd2b1e68 | 3985 | } |
39bcd429 | 3986 | catch (const semantic_error& e) |
bd2b1e68 | 3987 | { |
39bcd429 FCE |
3988 | q->sess.print_error (e); |
3989 | return DWARF_CB_ABORT; | |
bd2b1e68 | 3990 | } |
bd2b1e68 GH |
3991 | } |
3992 | ||
3993 | static int | |
3994 | query_cu (Dwarf_Die * cudie, void * arg) | |
3995 | { | |
20c6c071 | 3996 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
49abf162 | 3997 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 3998 | |
39bcd429 | 3999 | try |
bd2b1e68 | 4000 | { |
7e1279ea | 4001 | q->dw.focus_on_cu (cudie); |
b5d77020 | 4002 | |
b0ee93c4 | 4003 | if (false && q->sess.verbose>2) |
b5d77020 | 4004 | clog << "focused on CU '" << q->dw.cu_name |
db22e55f | 4005 | << "', in module '" << q->dw.module_name << "'\n"; |
d9b516ca | 4006 | |
e4c58386 | 4007 | if (q->has_statement_str || q->has_statement_num |
54efe513 | 4008 | || q->has_function_str || q->has_function_num) |
7e1279ea FCE |
4009 | { |
4010 | q->filtered_srcfiles.clear(); | |
4011 | q->filtered_functions.clear(); | |
4012 | q->filtered_inlines.clear(); | |
4013 | ||
4014 | // In this path, we find "abstract functions", record | |
4015 | // information about them, and then (depending on lineno | |
4016 | // matching) possibly emit one or more of the function's | |
4017 | // associated addresses. Unfortunately the control of this | |
4018 | // cannot easily be turned inside out. | |
4019 | ||
b8da0ad1 | 4020 | if ((q->has_statement_str || q->has_function_str) |
7e1279ea FCE |
4021 | && (q->spec_type != function_alone)) |
4022 | { | |
4023 | // If we have a pattern string with a filename, we need | |
4024 | // to elaborate the srcfile mask in question first. | |
4025 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
4026 | ||
4027 | // If we have a file pattern and *no* srcfile matches, there's | |
4028 | // no need to look further into this CU, so skip. | |
4029 | if (q->filtered_srcfiles.empty()) | |
4030 | return DWARF_CB_OK; | |
4031 | } | |
86bf665e TM |
4032 | // Verify that a raw address matches the beginning of a |
4033 | // statement. This is a somewhat lame check that the address | |
4034 | // is at the start of an assembly instruction. | |
1f0cfd98 SC |
4035 | // Avoid for now since this thwarts a probe on a statement in a macro |
4036 | if (0 && q->has_statement_num) | |
86bf665e TM |
4037 | { |
4038 | Dwarf_Addr queryaddr = q->statement_num_val; | |
4039 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, queryaddr)); | |
4040 | Dwarf_Addr lineaddr = 0; | |
4041 | if (address_line) | |
4042 | lineaddr = address_line.addr(); | |
4043 | if (!address_line || lineaddr != queryaddr) | |
4044 | { | |
4045 | stringstream msg; | |
4046 | msg << "address 0x" << hex << queryaddr | |
1b1b4ceb | 4047 | << " does not match the beginning of a statement"; |
86bf665e TM |
4048 | throw semantic_error(msg.str()); |
4049 | } | |
4050 | } | |
7e1279ea FCE |
4051 | // Pick up [entrypc, name, DIE] tuples for all the functions |
4052 | // matching the query, and fill in the prologue endings of them | |
4053 | // all in a single pass. | |
2da9cedb JS |
4054 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, |
4055 | q->function, | |
4056 | q->has_statement_num); | |
5f0a03a6 JK |
4057 | if (rc != DWARF_CB_OK) |
4058 | q->query_done = true; | |
44f75386 | 4059 | |
35dc8b04 | 4060 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 |
e4c58386 | 4061 | && !q->has_statement_str && !q->has_statement_num) // PR 2608 |
44f75386 FCE |
4062 | if (! q->filtered_functions.empty()) |
4063 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
7e1279ea | 4064 | |
b8da0ad1 | 4065 | if ((q->has_statement_str || q->has_function_str) |
7e1279ea FCE |
4066 | && (q->spec_type == function_file_and_line)) |
4067 | { | |
4068 | // If we have a pattern string with target *line*, we | |
20e4a32c | 4069 | // have to look at lines in all the matched srcfiles. |
7e1279ea FCE |
4070 | for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin(); |
4071 | i != q->filtered_srcfiles.end(); ++i) | |
897820ca | 4072 | q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str, |
0c8b7d37 | 4073 | q->line_type, query_srcfile_line, q); |
7e1279ea | 4074 | } |
0f336e95 SC |
4075 | else if (q->has_label) |
4076 | { | |
4077 | // If we have a pattern string with target *label*, we | |
4078 | // have to look at labels in all the matched srcfiles. | |
4079 | q->dw.iterate_over_cu_labels (q->label_val, q->dw.cu, q, query_statement); | |
4080 | } | |
7e1279ea FCE |
4081 | else |
4082 | { | |
e4c58386 | 4083 | // Otherwise, simply probe all resolved functions. |
86bf665e | 4084 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
e4c58386 | 4085 | i != q->filtered_functions.end(); ++i) |
3e961ba6 | 4086 | query_func_info (i->entrypc, *i, q); |
e4c58386 FCE |
4087 | |
4088 | // And all inline instances (if we're not excluding inlines with ".call") | |
4089 | if (! q->has_call) | |
86bf665e | 4090 | for (inline_instance_map_t::iterator i |
54efe513 | 4091 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) |
3e961ba6 | 4092 | query_inline_instance_info (*i, q); |
7e1279ea FCE |
4093 | } |
4094 | } | |
39bcd429 FCE |
4095 | else |
4096 | { | |
e4c58386 FCE |
4097 | // Before PR 5787, we used to have this: |
4098 | #if 0 | |
7e1279ea FCE |
4099 | // Otherwise we have a statement number, and we can just |
4100 | // query it directly within this module. | |
7e1279ea FCE |
4101 | assert (q->has_statement_num); |
4102 | Dwarf_Addr query_addr = q->statement_num_val; | |
b8da0ad1 | 4103 | query_addr = q->dw.module_address_to_global(query_addr); |
7e1279ea | 4104 | |
bcc12710 | 4105 | query_statement ("", "", -1, NULL, query_addr, q); |
e4c58386 FCE |
4106 | #endif |
4107 | // But now, we traverse CUs/functions even for | |
4108 | // statement_num's, for blacklist sensitivity and $var | |
4109 | // resolution purposes. | |
4110 | ||
4111 | assert (0); // NOTREACHED | |
39bcd429 FCE |
4112 | } |
4113 | return DWARF_CB_OK; | |
bd2b1e68 | 4114 | } |
39bcd429 | 4115 | catch (const semantic_error& e) |
bd2b1e68 | 4116 | { |
39bcd429 FCE |
4117 | q->sess.print_error (e); |
4118 | return DWARF_CB_ABORT; | |
bd2b1e68 | 4119 | } |
bd2b1e68 GH |
4120 | } |
4121 | ||
0ce64fb8 | 4122 | |
91af0778 | 4123 | #if 0 |
1d3a40b6 DS |
4124 | static int |
4125 | query_kernel_module (Dwfl_Module *mod, | |
91af0778 | 4126 | void **, |
1d3a40b6 | 4127 | const char *name, |
b8da0ad1 | 4128 | Dwarf_Addr, |
1d3a40b6 DS |
4129 | void *arg) |
4130 | { | |
4131 | if (TOK_KERNEL == name) | |
4132 | { | |
4133 | Dwfl_Module **m = (Dwfl_Module **)arg; | |
4134 | ||
4135 | *m = mod; | |
4136 | return DWARF_CB_ABORT; | |
4137 | } | |
4138 | return DWARF_CB_OK; | |
4139 | } | |
91af0778 FCE |
4140 | #endif |
4141 | ||
1d3a40b6 | 4142 | |
5f0a03a6 JK |
4143 | static void |
4144 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
4145 | { | |
4146 | // Validate the machine code in this elf file against the | |
4147 | // session machine. This is important, in case the wrong kind | |
4148 | // of debuginfo is being automagically processed by elfutils. | |
4149 | // While we can tell i686 apart from x86-64, unfortunately | |
4150 | // we can't help confusing i586 vs i686 (both EM_386). | |
4151 | ||
4152 | Dwarf_Addr bias; | |
4153 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
4154 | // because dwfl_module_getelf can force costly section relocations | |
4155 | // we don't really need, while either will do for this purpose. | |
4156 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
4157 | ?: dwfl_module_getelf (mod, &bias)); | |
4158 | ||
4159 | GElf_Ehdr ehdr_mem; | |
4160 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 4161 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
5f0a03a6 JK |
4162 | int elf_machine = em->e_machine; |
4163 | const char* debug_filename = ""; | |
4164 | const char* main_filename = ""; | |
4165 | (void) dwfl_module_info (mod, NULL, NULL, | |
4166 | NULL, NULL, NULL, | |
4167 | & main_filename, | |
4168 | & debug_filename); | |
4169 | const string& sess_machine = q->sess.architecture; | |
4170 | string expect_machine; | |
4171 | ||
4172 | switch (elf_machine) | |
4173 | { | |
4174 | case EM_386: expect_machine = "i?86"; break; // accept e.g. i586 | |
4175 | case EM_X86_64: expect_machine = "x86_64"; break; | |
7635926c JK |
4176 | // We don't support 32-bit ppc kernels, but we support 32-bit apps |
4177 | // running on ppc64 kernels. | |
4178 | case EM_PPC: expect_machine = "ppc64"; break; | |
5f0a03a6 JK |
4179 | case EM_PPC64: expect_machine = "ppc64"; break; |
4180 | case EM_S390: expect_machine = "s390x"; break; | |
4181 | case EM_IA_64: expect_machine = "ia64"; break; | |
4182 | case EM_ARM: expect_machine = "armv*"; break; | |
4183 | // XXX: fill in some more of these | |
4184 | default: expect_machine = "?"; break; | |
4185 | } | |
4186 | ||
4187 | if (! debug_filename) debug_filename = main_filename; | |
4188 | if (! debug_filename) debug_filename = name; | |
4189 | ||
4190 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0) | |
4191 | { | |
4192 | stringstream msg; | |
4193 | msg << "ELF machine " << expect_machine << " (code " << elf_machine | |
4194 | << ") mismatch with target " << sess_machine | |
4195 | << " in '" << debug_filename << "'"; | |
4196 | throw semantic_error(msg.str ()); | |
4197 | } | |
4198 | ||
4199 | if (q->sess.verbose>2) | |
4200 | clog << "focused on module '" << q->dw.module_name | |
4201 | << " = [0x" << hex << q->dw.module_start | |
4202 | << "-0x" << q->dw.module_end | |
4203 | << ", bias 0x" << q->dw.module_bias << "]" << dec | |
4204 | << " file " << debug_filename | |
4205 | << " ELF machine " << expect_machine | |
4206 | << " (code " << elf_machine << ")" | |
4207 | << "\n"; | |
4208 | } | |
1d3a40b6 | 4209 | |
91af0778 FCE |
4210 | |
4211 | ||
4212 | static Dwarf_Addr | |
4213 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
4214 | { | |
4215 | int syments = dwfl_module_getsymtab(m); | |
4216 | assert(syments); | |
4217 | for (int i = 1; i < syments; ++i) | |
4218 | { | |
4219 | GElf_Sym sym; | |
4220 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
4221 | if (name != NULL && strcmp(name, wanted) == 0) | |
4222 | return sym.st_value; | |
4223 | } | |
4224 | ||
4225 | return 0; | |
4226 | } | |
4227 | ||
4228 | ||
4229 | ||
bd2b1e68 | 4230 | static int |
b8da0ad1 | 4231 | query_module (Dwfl_Module *mod, |
91af0778 | 4232 | void **, |
b8da0ad1 | 4233 | const char *name, |
6f4c1275 | 4234 | Dwarf_Addr addr, |
b8da0ad1 | 4235 | void *arg) |
bd2b1e68 | 4236 | { |
91af0778 | 4237 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 4238 | |
39bcd429 | 4239 | try |
e38d6504 | 4240 | { |
91af0778 FCE |
4241 | module_info* mi = q->sess.module_cache->cache[name]; |
4242 | if (mi == 0) | |
4243 | { | |
4244 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
4245 | ||
6f4c1275 FCE |
4246 | mi->mod = mod; |
4247 | mi->addr = addr; | |
91af0778 | 4248 | |
6f4c1275 FCE |
4249 | const char* debug_filename = ""; |
4250 | const char* main_filename = ""; | |
4251 | (void) dwfl_module_info (mod, NULL, NULL, | |
4252 | NULL, NULL, NULL, | |
4253 | & main_filename, | |
4254 | & debug_filename); | |
4255 | ||
4256 | if (q->sess.ignore_vmlinux && name == TOK_KERNEL) | |
91af0778 FCE |
4257 | { |
4258 | // report_kernel() in elfutils found vmlinux, but pretend it didn't. | |
4259 | // Given a non-null path, returning 1 means keep reporting modules. | |
4260 | mi->dwarf_status = info_absent; | |
4261 | } | |
6f4c1275 | 4262 | else if (debug_filename || main_filename) |
91af0778 | 4263 | { |
6f4c1275 FCE |
4264 | mi->elf_path = debug_filename ?: main_filename; |
4265 | } | |
4266 | else if (name == TOK_KERNEL) | |
4267 | { | |
4268 | mi->dwarf_status = info_absent; | |
91af0778 | 4269 | } |
91af0778 FCE |
4270 | } |
4271 | // OK, enough of that module_info caching business. | |
4272 | ||
5f0a03a6 | 4273 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 4274 | |
39bcd429 FCE |
4275 | // If we have enough information in the pattern to skip a module and |
4276 | // the module does not match that information, return early. | |
b8da0ad1 | 4277 | if (!q->dw.module_name_matches(q->module_val)) |
39bcd429 | 4278 | return DWARF_CB_OK; |
0cbbf9d1 FCE |
4279 | |
4280 | // Don't allow module("*kernel*") type expressions to match the | |
4281 | // elfutils module "kernel", which we refer to in the probe | |
4282 | // point syntax exclusively as "kernel.*". | |
4283 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
4284 | return DWARF_CB_OK; | |
b5d77020 | 4285 | |
5f0a03a6 JK |
4286 | if (mod) |
4287 | validate_module_elf(mod, name, q); | |
4288 | else | |
91af0778 FCE |
4289 | assert(q->has_kernel); // and no vmlinux to examine |
4290 | ||
4291 | if (q->sess.verbose>2) | |
4292 | cerr << "focused on module '" << q->dw.module_name << "'\n"; | |
4293 | ||
4294 | ||
4295 | // Collect a few kernel addresses. XXX: these belong better | |
4296 | // to the sess.module_info["kernel"] struct. | |
4297 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 4298 | { |
91af0778 FCE |
4299 | if (! q->sess.sym_kprobes_text_start) |
4300 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
4301 | if (! q->sess.sym_kprobes_text_end) | |
4302 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
4303 | if (! q->sess.sym_stext) | |
4304 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
4305 | } |
4306 | ||
91af0778 | 4307 | // Finally, search the module for matches of the probe point. |
2c384610 | 4308 | q->handle_query_module(); |
bb788f9f | 4309 | |
91af0778 | 4310 | |
b8da0ad1 FCE |
4311 | // If we know that there will be no more matches, abort early. |
4312 | if (q->dw.module_name_final_match(q->module_val)) | |
4313 | return DWARF_CB_ABORT; | |
4314 | else | |
4315 | return DWARF_CB_OK; | |
7a053d3b | 4316 | } |
39bcd429 | 4317 | catch (const semantic_error& e) |
bd2b1e68 | 4318 | { |
39bcd429 FCE |
4319 | q->sess.print_error (e); |
4320 | return DWARF_CB_ABORT; | |
bd2b1e68 | 4321 | } |
bd2b1e68 GH |
4322 | } |
4323 | ||
5f0a03a6 | 4324 | void |
c4ce66a1 | 4325 | dwflpp::query_modules(base_query *q) |
5f0a03a6 | 4326 | { |
91af0778 | 4327 | iterate_over_modules(&query_module, q); |
5f0a03a6 | 4328 | } |
2930abc7 | 4329 | |
de688825 | 4330 | struct var_expanding_visitor: public update_visitor |
77de5e9e | 4331 | { |
77de5e9e | 4332 | static unsigned tick; |
e57b735a | 4333 | stack<functioncall**> target_symbol_setter_functioncalls; |
77de5e9e | 4334 | |
de688825 | 4335 | var_expanding_visitor() {} |
35d4ab18 FCE |
4336 | void visit_assignment (assignment* e); |
4337 | }; | |
4338 | ||
4339 | ||
de688825 | 4340 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 4341 | { |
77de5e9e | 4342 | dwarf_query & q; |
bcc12710 | 4343 | Dwarf_Die *scope_die; |
77de5e9e | 4344 | Dwarf_Addr addr; |
8c819921 | 4345 | block *add_block; |
8fc05e57 | 4346 | probe *add_probe; |
cd5b28b2 | 4347 | std::map<std::string, symbol *> return_ts_map; |
b95e2b79 | 4348 | bool visited; |
77de5e9e | 4349 | |
de688825 | 4350 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
b95e2b79 | 4351 | q(q), scope_die(sd), addr(a), add_block(NULL), add_probe(NULL), visited(false) {} |
d7f3e0c5 | 4352 | void visit_target_symbol (target_symbol* e); |
c24447be | 4353 | void visit_cast_op (cast_op* e); |
77de5e9e GH |
4354 | }; |
4355 | ||
4356 | ||
35d4ab18 | 4357 | |
de688825 | 4358 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 4359 | |
77de5e9e | 4360 | void |
de688825 | 4361 | var_expanding_visitor::visit_assignment (assignment* e) |
77de5e9e | 4362 | { |
e57b735a GH |
4363 | // Our job would normally be to require() the left and right sides |
4364 | // into a new assignment. What we're doing is slightly trickier: | |
4365 | // we're pushing a functioncall** onto a stack, and if our left | |
4366 | // child sets the functioncall* for that value, we're going to | |
4367 | // assume our left child was a target symbol -- transformed into a | |
4368 | // set_target_foo(value) call, and it wants to take our right child | |
4369 | // as the argument "value". | |
4370 | // | |
4371 | // This is why some people claim that languages with | |
4372 | // constructor-decomposing case expressions have a leg up on | |
4373 | // visitors. | |
4374 | ||
4375 | functioncall *fcall = NULL; | |
4376 | expression *new_left, *new_right; | |
d9b516ca | 4377 | |
e57b735a | 4378 | target_symbol_setter_functioncalls.push (&fcall); |
4ed05b15 | 4379 | new_left = require (e->left); |
e57b735a | 4380 | target_symbol_setter_functioncalls.pop (); |
4ed05b15 | 4381 | new_right = require (e->right); |
e57b735a GH |
4382 | |
4383 | if (fcall != NULL) | |
77de5e9e | 4384 | { |
e57b735a GH |
4385 | // Our left child is informing us that it was a target variable |
4386 | // and it has been replaced with a set_target_foo() function | |
4387 | // call; we are going to provide that function call -- with the | |
4388 | // right child spliced in as sole argument -- in place of | |
de688825 | 4389 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a GH |
4390 | |
4391 | // FIXME: for the time being, we only support plan $foo = bar, | |
4392 | // not += or any other op= variant. This is fixable, but a bit | |
4393 | // ugly. | |
4394 | if (e->op != "=") | |
4395 | throw semantic_error ("Operator-assign expressions on target " | |
4396 | "variables not implemented", e->tok); | |
4397 | ||
4398 | assert (new_left == fcall); | |
4399 | fcall->args.push_back (new_right); | |
4ed05b15 | 4400 | provide (fcall); |
77de5e9e | 4401 | } |
e57b735a GH |
4402 | else |
4403 | { | |
de688825 JS |
4404 | e->left = new_left; |
4405 | e->right = new_right; | |
4406 | provide (e); | |
e57b735a GH |
4407 | } |
4408 | } | |
d9b516ca | 4409 | |
d7f3e0c5 | 4410 | |
e57b735a | 4411 | void |
de688825 | 4412 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
e57b735a GH |
4413 | { |
4414 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
b95e2b79 | 4415 | visited = true; |
e57b735a | 4416 | |
cf2a1f85 DS |
4417 | bool lvalue = is_active_lvalue(e); |
4418 | if (lvalue && !q.sess.guru_mode) | |
4419 | throw semantic_error("write to target variable not permitted", e->tok); | |
4420 | ||
a43ba433 FCE |
4421 | // See if we need to generate a new probe to save/access function |
4422 | // parameters from a return probe. PR 1382. | |
4423 | if (q.has_return | |
4424 | && e->base_name != "$return" // not the special return-value variable handled below | |
4425 | && e->base_name != "$$return") // nor the other special variable handled below | |
85ecf79a DS |
4426 | { |
4427 | if (lvalue) | |
4428 | throw semantic_error("write to target variable not permitted in .return probes", e->tok); | |
4429 | ||
cd5b28b2 DS |
4430 | // Get the full name of the target symbol. |
4431 | stringstream ts_name_stream; | |
4432 | e->print(ts_name_stream); | |
4433 | string ts_name = ts_name_stream.str(); | |
4434 | ||
4435 | // Check and make sure we haven't already seen this target | |
4436 | // variable in this return probe. If we have, just return our | |
4437 | // last replacement. | |
4438 | map<string, symbol *>::iterator i = return_ts_map.find(ts_name); | |
4439 | if (i != return_ts_map.end()) | |
4440 | { | |
4ed05b15 | 4441 | provide (i->second); |
cd5b28b2 DS |
4442 | return; |
4443 | } | |
4444 | ||
85ecf79a DS |
4445 | // We've got to do several things here to handle target |
4446 | // variables in return probes. | |
4447 | ||
5e600bd6 DS |
4448 | // (1) Synthesize two global arrays. One is the cache of the |
4449 | // target variable and the other contains a thread specific | |
4450 | // nesting level counter. The arrays will look like | |
4451 | // this: | |
85ecf79a DS |
4452 | // |
4453 | // _dwarf_tvar_{name}_{num} | |
85ecf79a DS |
4454 | // _dwarf_tvar_{name}_{num}_ctr |
4455 | ||
4456 | string aname = (string("_dwarf_tvar_") | |
4457 | + e->base_name.substr(1) | |
4458 | + "_" + lex_cast<string>(tick++)); | |
4459 | vardecl* vd = new vardecl; | |
4460 | vd->name = aname; | |
4461 | vd->tok = e->tok; | |
4462 | q.sess.globals.push_back (vd); | |
4463 | ||
4464 | string ctrname = aname + "_ctr"; | |
4465 | vd = new vardecl; | |
4466 | vd->name = ctrname; | |
4467 | vd->tok = e->tok; | |
4468 | q.sess.globals.push_back (vd); | |
4469 | ||
8c819921 DS |
4470 | // (2) Create a new code block we're going to insert at the |
4471 | // beginning of this probe to get the cached value into a | |
4472 | // temporary variable. We'll replace the target variable | |
4473 | // reference with the temporary variable reference. The code | |
4474 | // will look like this: | |
4475 | // | |
4476 | // _dwarf_tvar_tid = tid() | |
4477 | // _dwarf_tvar_{name}_{num}_tmp | |
4478 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4479 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4480 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4481 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
46392da3 DS |
4482 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) |
4483 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
8c819921 DS |
4484 | |
4485 | // (2a) Synthesize the tid temporary expression, which will look | |
85ecf79a DS |
4486 | // like this: |
4487 | // | |
8c819921 DS |
4488 | // _dwarf_tvar_tid = tid() |
4489 | symbol* tidsym = new symbol; | |
4490 | tidsym->name = string("_dwarf_tvar_tid"); | |
4491 | tidsym->tok = e->tok; | |
85ecf79a | 4492 | |
8c819921 DS |
4493 | if (add_block == NULL) |
4494 | { | |
8fc05e57 DS |
4495 | add_block = new block; |
4496 | add_block->tok = e->tok; | |
4497 | ||
4498 | // Synthesize a functioncall to grab the thread id. | |
4499 | functioncall* fc = new functioncall; | |
4500 | fc->tok = e->tok; | |
4501 | fc->function = string("tid"); | |
4502 | ||
4503 | // Assign the tid to '_dwarf_tvar_tid'. | |
4504 | assignment* a = new assignment; | |
4505 | a->tok = e->tok; | |
4506 | a->op = "="; | |
4507 | a->left = tidsym; | |
4508 | a->right = fc; | |
4509 | ||
4510 | expr_statement* es = new expr_statement; | |
4511 | es->tok = e->tok; | |
4512 | es->value = a; | |
4513 | add_block->statements.push_back (es); | |
8c819921 DS |
4514 | } |
4515 | ||
4516 | // (2b) Synthesize an array reference and assign it to a | |
4517 | // temporary variable (that we'll use as replacement for the | |
4518 | // target variable reference). It will look like this: | |
4519 | // | |
4520 | // _dwarf_tvar_{name}_{num}_tmp | |
4521 | // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4522 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4523 | ||
8fc05e57 DS |
4524 | arrayindex* ai_tvar_base = new arrayindex; |
4525 | ai_tvar_base->tok = e->tok; | |
85ecf79a DS |
4526 | |
4527 | symbol* sym = new symbol; | |
4528 | sym->name = aname; | |
4529 | sym->tok = e->tok; | |
8fc05e57 | 4530 | ai_tvar_base->base = sym; |
8c819921 | 4531 | |
8fc05e57 | 4532 | ai_tvar_base->indexes.push_back(tidsym); |
85ecf79a | 4533 | |
8c819921 DS |
4534 | // We need to create a copy of the array index in its current |
4535 | // state so we can have 2 variants of it (the original and one | |
4536 | // that post-decrements the second index). | |
8fc05e57 DS |
4537 | arrayindex* ai_tvar = new arrayindex; |
4538 | arrayindex* ai_tvar_postdec = new arrayindex; | |
4539 | *ai_tvar = *ai_tvar_base; | |
4540 | *ai_tvar_postdec = *ai_tvar_base; | |
85ecf79a | 4541 | |
8c819921 DS |
4542 | // Synthesize the |
4543 | // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the | |
85ecf79a | 4544 | // second index into the array. |
8c819921 DS |
4545 | arrayindex* ai_ctr = new arrayindex; |
4546 | ai_ctr->tok = e->tok; | |
5e600bd6 | 4547 | |
85ecf79a DS |
4548 | sym = new symbol; |
4549 | sym->name = ctrname; | |
4550 | sym->tok = e->tok; | |
8c819921 DS |
4551 | ai_ctr->base = sym; |
4552 | ai_ctr->indexes.push_back(tidsym); | |
4553 | ai_tvar->indexes.push_back(ai_ctr); | |
4554 | ||
4555 | symbol* tmpsym = new symbol; | |
4556 | tmpsym->name = aname + "_tmp"; | |
4557 | tmpsym->tok = e->tok; | |
4558 | ||
4559 | assignment* a = new assignment; | |
4560 | a->tok = e->tok; | |
4561 | a->op = "="; | |
4562 | a->left = tmpsym; | |
4563 | a->right = ai_tvar; | |
4564 | ||
4565 | expr_statement* es = new expr_statement; | |
4566 | es->tok = e->tok; | |
4567 | es->value = a; | |
4568 | ||
4569 | add_block->statements.push_back (es); | |
4570 | ||
4571 | // (2c) Add a post-decrement to the second array index and | |
4572 | // delete the array value. It will look like this: | |
4573 | // | |
4574 | // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4575 | // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--] | |
5e600bd6 | 4576 | |
85ecf79a DS |
4577 | post_crement* pc = new post_crement; |
4578 | pc->tok = e->tok; | |
4579 | pc->op = "--"; | |
8c819921 | 4580 | pc->operand = ai_ctr; |
8fc05e57 | 4581 | ai_tvar_postdec->indexes.push_back(pc); |
8c819921 DS |
4582 | |
4583 | delete_statement* ds = new delete_statement; | |
4584 | ds->tok = e->tok; | |
8fc05e57 | 4585 | ds->value = ai_tvar_postdec; |
8c819921 DS |
4586 | |
4587 | add_block->statements.push_back (ds); | |
85ecf79a | 4588 | |
46392da3 DS |
4589 | // (2d) Delete the counter value if it is 0. It will look like |
4590 | // this: | |
4591 | // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]) | |
4592 | // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid] | |
4baf0e53 | 4593 | |
46392da3 DS |
4594 | ds = new delete_statement; |
4595 | ds->tok = e->tok; | |
4596 | ds->value = ai_ctr; | |
4597 | ||
4598 | unary_expression *ue = new unary_expression; | |
4599 | ue->tok = e->tok; | |
4600 | ue->op = "!"; | |
4601 | ue->operand = ai_ctr; | |
4602 | ||
4603 | if_statement *ifs = new if_statement; | |
4604 | ifs->tok = e->tok; | |
4605 | ifs->condition = ue; | |
4606 | ifs->thenblock = ds; | |
4607 | ifs->elseblock = NULL; | |
4baf0e53 | 4608 | |
46392da3 DS |
4609 | add_block->statements.push_back (ifs); |
4610 | ||
85ecf79a | 4611 | // (3) We need an entry probe that saves the value for us in the |
8fc05e57 DS |
4612 | // global array we created. Create the entry probe, which will |
4613 | // look like this: | |
85ecf79a | 4614 | // |
85ecf79a | 4615 | // probe kernel.function("{function}") { |
8fc05e57 DS |
4616 | // _dwarf_tvar_tid = tid() |
4617 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4618 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
85ecf79a DS |
4619 | // = ${param} |
4620 | // } | |
4621 | ||
8fc05e57 | 4622 | if (add_probe == NULL) |
85ecf79a | 4623 | { |
8fc05e57 DS |
4624 | add_probe = new probe; |
4625 | add_probe->tok = e->tok; | |
4626 | ||
4627 | // We need the name of the current probe point, minus the | |
4628 | // ".return" (or anything after it, such as ".maxactive(N)"). | |
4629 | // Create a new probe point, copying all the components, | |
4630 | // stopping when we see the ".return" component. | |
4631 | probe_point* pp = new probe_point; | |
4632 | for (unsigned c = 0; c < q.base_loc->components.size(); c++) | |
4633 | { | |
4634 | if (q.base_loc->components[c]->functor == "return") | |
4635 | break; | |
4636 | else | |
4637 | pp->components.push_back(q.base_loc->components[c]); | |
4638 | } | |
4639 | pp->tok = e->tok; | |
4640 | pp->optional = q.base_loc->optional; | |
4641 | add_probe->locations.push_back(pp); | |
4642 | ||
4643 | add_probe->body = new block; | |
4644 | add_probe->body->tok = e->tok; | |
4645 | ||
4646 | // Synthesize a functioncall to grab the thread id. | |
4647 | functioncall* fc = new functioncall; | |
4648 | fc->tok = e->tok; | |
4649 | fc->function = string("tid"); | |
4650 | ||
4651 | // Assign the tid to '_dwarf_tvar_tid'. | |
4652 | assignment* a = new assignment; | |
4653 | a->tok = e->tok; | |
4654 | a->op = "="; | |
4655 | a->left = tidsym; | |
4656 | a->right = fc; | |
4657 | ||
4658 | expr_statement* es = new expr_statement; | |
4659 | es->tok = e->tok; | |
4660 | es->value = a; | |
ba6f838d | 4661 | add_probe->body = new block(add_probe->body, es); |
8fc05e57 DS |
4662 | |
4663 | vardecl* vd = new vardecl; | |
4664 | vd->tok = e->tok; | |
4665 | vd->name = tidsym->name; | |
4666 | vd->type = pe_long; | |
4667 | vd->set_arity(0); | |
4668 | add_probe->locals.push_back(vd); | |
85ecf79a | 4669 | } |
8fc05e57 DS |
4670 | |
4671 | // Save the value, like this: | |
4672 | // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid, | |
4673 | // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]] | |
4674 | // = ${param} | |
4675 | arrayindex* ai_tvar_preinc = new arrayindex; | |
4676 | *ai_tvar_preinc = *ai_tvar_base; | |
4baf0e53 | 4677 | |
8fc05e57 DS |
4678 | pre_crement* preinc = new pre_crement; |
4679 | preinc->tok = e->tok; | |
4680 | preinc->op = "++"; | |
4681 | preinc->operand = ai_ctr; | |
4682 | ai_tvar_preinc->indexes.push_back(preinc); | |
4baf0e53 | 4683 | |
8fc05e57 DS |
4684 | a = new assignment; |
4685 | a->tok = e->tok; | |
4686 | a->op = "="; | |
4687 | a->left = ai_tvar_preinc; | |
4688 | a->right = e; | |
4689 | ||
4690 | es = new expr_statement; | |
4691 | es->tok = e->tok; | |
4692 | es->value = a; | |
4693 | ||
ba6f838d | 4694 | add_probe->body = new block(add_probe->body, es); |
85ecf79a | 4695 | |
8c819921 DS |
4696 | // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to |
4697 | // our parent so it can be used as a substitute for the target | |
4698 | // symbol. | |
4ed05b15 | 4699 | provide (tmpsym); |
cd5b28b2 DS |
4700 | |
4701 | // (5) Remember this replacement since we might be able to reuse | |
4702 | // it later if the same return probe references this target | |
4703 | // symbol again. | |
4704 | return_ts_map[ts_name] = tmpsym; | |
85ecf79a DS |
4705 | return; |
4706 | } | |
cf2a1f85 | 4707 | |
2cb3fe26 SC |
4708 | if (e->base_name == "$$vars" |
4709 | || e->base_name == "$$parms" | |
a43ba433 FCE |
4710 | || e->base_name == "$$locals" |
4711 | || (q.has_return && (e->base_name == "$$return"))) | |
2cb3fe26 SC |
4712 | { |
4713 | Dwarf_Die *scopes; | |
4714 | if (dwarf_getscopes_die (scope_die, &scopes) == 0) | |
4715 | return; | |
41c262f3 | 4716 | |
2cb3fe26 SC |
4717 | target_symbol *tsym = new target_symbol; |
4718 | print_format* pf = new print_format; | |
4719 | ||
4720 | // Convert $$parms to sprintf of a list of parms and active local vars | |
4721 | // which we recursively evaluate | |
a43ba433 FCE |
4722 | |
4723 | // NB: we synthesize a new token here rather than reusing | |
4724 | // e->tok, because print_format::print likes to use | |
4725 | // its tok->content. | |
4726 | token* pf_tok = new token; | |
4727 | pf_tok->location = e->tok->location; | |
4728 | pf_tok->type = tok_identifier; | |
4729 | pf_tok->content = "sprint"; | |
4730 | ||
4731 | pf->tok = pf_tok; | |
2cb3fe26 SC |
4732 | pf->print_to_stream = false; |
4733 | pf->print_with_format = true; | |
4734 | pf->print_with_delim = false; | |
4735 | pf->print_with_newline = false; | |
4736 | pf->print_char = false; | |
4737 | ||
a43ba433 FCE |
4738 | if (q.has_return && (e->base_name == "$$return")) |
4739 | { | |
4740 | tsym->tok = e->tok; | |
4741 | tsym->base_name = "$return"; | |
41c262f3 | 4742 | |
a43ba433 FCE |
4743 | // Ignore any variable that isn't accessible. |
4744 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
4745 | expression *texp = tsym; |
4746 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
4747 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
4748 | { | |
2cb3fe26 | 4749 | |
a43ba433 FCE |
4750 | } |
4751 | else | |
4752 | { | |
fd574705 | 4753 | pf->raw_components += "return"; |
a43ba433 | 4754 | pf->raw_components += "=%#x "; |
4ed05b15 | 4755 | pf->args.push_back(texp); |
a43ba433 FCE |
4756 | } |
4757 | } | |
4758 | else | |
4759 | { | |
4760 | // non-.return probe: support $$parms, $$vars, $$locals | |
4761 | Dwarf_Die result; | |
4762 | if (dwarf_child (&scopes[0], &result) == 0) | |
4763 | do | |
00cf3709 | 4764 | { |
a43ba433 FCE |
4765 | switch (dwarf_tag (&result)) |
4766 | { | |
4767 | case DW_TAG_variable: | |
4768 | if (e->base_name == "$$parms") | |
4769 | continue; | |
4770 | break; | |
4771 | case DW_TAG_formal_parameter: | |
4772 | if (e->base_name == "$$locals") | |
4773 | continue; | |
4774 | break; | |
41c262f3 | 4775 | |
a43ba433 FCE |
4776 | default: |
4777 | continue; | |
4778 | } | |
41c262f3 | 4779 | |
a43ba433 | 4780 | const char *diename = dwarf_diename (&result); |
f76427a2 FCE |
4781 | if (! diename) continue; |
4782 | ||
a43ba433 FCE |
4783 | tsym->tok = e->tok; |
4784 | tsym->base_name = "$"; | |
4785 | tsym->base_name += diename; | |
41c262f3 | 4786 | |
a43ba433 FCE |
4787 | // Ignore any variable that isn't accessible. |
4788 | tsym->saved_conversion_error = 0; | |
4ed05b15 JS |
4789 | expression *texp = tsym; |
4790 | texp = require (texp); // NB: throws nothing ... | |
a43ba433 FCE |
4791 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
4792 | { | |
12b44fb3 FCE |
4793 | if (q.sess.verbose>2) |
4794 | { | |
4795 | for (semantic_error *c = tsym->saved_conversion_error; | |
4796 | c != 0; | |
4797 | c = c->chain) { | |
4798 | clog << "variable location problem: " << c->what() << endl; | |
4799 | } | |
4800 | } | |
4801 | ||
a43ba433 FCE |
4802 | pf->raw_components += diename; |
4803 | pf->raw_components += "=? "; | |
4804 | } | |
4805 | else | |
4806 | { | |
4807 | pf->raw_components += diename; | |
4808 | pf->raw_components += "=%#x "; | |
4ed05b15 | 4809 | pf->args.push_back(texp); |
a43ba433 | 4810 | } |
00cf3709 | 4811 | } |
a43ba433 FCE |
4812 | while (dwarf_siblingof (&result, &result) == 0); |
4813 | } | |
2cb3fe26 | 4814 | |
2cb3fe26 | 4815 | pf->components = print_format::string_to_components(pf->raw_components); |
4ed05b15 | 4816 | provide (pf); |
2cb3fe26 SC |
4817 | |
4818 | return; | |
4819 | } | |
4820 | ||
e57b735a | 4821 | // Synthesize a function. |
d7f3e0c5 | 4822 | functiondecl *fdecl = new functiondecl; |
7b99c7d3 | 4823 | fdecl->tok = e->tok; |
d7f3e0c5 | 4824 | embeddedcode *ec = new embeddedcode; |
5e309481 | 4825 | ec->tok = e->tok; |
e8fbc5e8 | 4826 | |
1b07c728 | 4827 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
20e4a32c | 4828 | + "_" + e->base_name.substr(1) |
e57b735a GH |
4829 | + "_" + lex_cast<string>(tick++)); |
4830 | ||
66d284f4 | 4831 | try |
e57b735a | 4832 | { |
a43ba433 | 4833 | if (q.has_return && (e->base_name == "$return")) |
e19fda4e DS |
4834 | { |
4835 | ec->code = q.dw.literal_stmt_for_return (scope_die, | |
4836 | addr, | |
4837 | e->components, | |
4838 | lvalue, | |
4839 | fdecl->type); | |
4840 | } | |
4841 | else | |
4842 | { | |
4843 | ec->code = q.dw.literal_stmt_for_local (scope_die, | |
4844 | addr, | |
4845 | e->base_name.substr(1), | |
4846 | e->components, | |
4847 | lvalue, | |
4848 | fdecl->type); | |
4849 | } | |
4850 | ||
1b07c728 FCE |
4851 | if (! lvalue) |
4852 | ec->code += "/* pure */"; | |
66d284f4 FCE |
4853 | } |
4854 | catch (const semantic_error& er) | |
4855 | { | |
3bd0d4df RA |
4856 | if (!q.sess.skip_badvars) |
4857 | { | |
4858 | // We suppress this error message, and pass the unresolved | |
4859 | // target_symbol to the next pass. We hope that this value ends | |
4860 | // up not being referenced after all, so it can be optimized out | |
4861 | // quietly. | |
4862 | provide (e); | |
4863 | semantic_error* saveme = new semantic_error (er); // copy it | |
4864 | saveme->tok1 = e->tok; // XXX: token not passed to q.dw code generation routines | |
4865 | // NB: we can have multiple errors, since a $target variable | |
4866 | // may be expanded in several different contexts: | |
4867 | // function ("*") { $var } | |
4868 | saveme->chain = e->saved_conversion_error; | |
4869 | e->saved_conversion_error = saveme; | |
4870 | } | |
4871 | else | |
4872 | { | |
4873 | // Upon user request for ignoring context, the symbol is replaced | |
4874 | // with a literal 0 and a warning message displayed | |
4875 | literal_number* ln_zero = new literal_number (0); | |
4876 | ln_zero->tok = e->tok; | |
4877 | provide (ln_zero); | |
4878 | q.sess.print_warning ("Bad variable being substituted with literal 0", | |
4879 | e->tok); | |
4880 | } | |
1cde5ba5 JS |
4881 | delete fdecl; |
4882 | delete ec; | |
cbfbbf69 | 4883 | return; |
66d284f4 | 4884 | } |
e57b735a | 4885 | |
d7f3e0c5 GH |
4886 | fdecl->name = fname; |
4887 | fdecl->body = ec; | |
e57b735a GH |
4888 | if (lvalue) |
4889 | { | |
4890 | // Modify the fdecl so it carries a single pe_long formal | |
4891 | // argument called "value". | |
4892 | ||
4893 | // FIXME: For the time being we only support setting target | |
4894 | // variables which have base types; these are 'pe_long' in | |
4895 | // stap's type vocabulary. Strings and pointers might be | |
4896 | // reasonable, some day, but not today. | |
4897 | ||
4898 | vardecl *v = new vardecl; | |
4899 | v->type = pe_long; | |
4900 | v->name = "value"; | |
4901 | v->tok = e->tok; | |
4902 | fdecl->formal_args.push_back(v); | |
4903 | } | |
f76427a2 | 4904 | q.sess.functions[fdecl->name]=fdecl; |
d9b516ca | 4905 | |
e57b735a | 4906 | // Synthesize a functioncall. |
d7f3e0c5 GH |
4907 | functioncall* n = new functioncall; |
4908 | n->tok = e->tok; | |
4909 | n->function = fname; | |
35d4ab18 | 4910 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session |
e57b735a GH |
4911 | |
4912 | if (lvalue) | |
4913 | { | |
4914 | // Provide the functioncall to our parent, so that it can be | |
4915 | // used to substitute for the assignment node immediately above | |
4916 | // us. | |
4917 | assert(!target_symbol_setter_functioncalls.empty()); | |
4918 | *(target_symbol_setter_functioncalls.top()) = n; | |
4919 | } | |
4920 | ||
4ed05b15 | 4921 | provide (n); |
77de5e9e GH |
4922 | } |
4923 | ||
4924 | ||
c24447be JS |
4925 | void |
4926 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
4927 | { | |
4928 | // Fill in our current module context if needed | |
4929 | if (e->module.empty()) | |
4930 | e->module = q.dw.module_name; | |
4931 | ||
4932 | var_expanding_visitor::visit_cast_op(e); | |
4933 | } | |
4934 | ||
4935 | ||
c4ce66a1 JS |
4936 | struct dwarf_cast_query : public base_query |
4937 | { | |
4938 | const cast_op& e; | |
4939 | const bool lvalue; | |
c4ce66a1 | 4940 | |
abb41d92 JS |
4941 | exp_type& pe_type; |
4942 | string& code; | |
c4ce66a1 | 4943 | |
abb41d92 JS |
4944 | dwarf_cast_query(dwflpp& dw, const string& module, const cast_op& e, |
4945 | bool lvalue, exp_type& pe_type, string& code): | |
4946 | base_query(dw, module), e(e), lvalue(lvalue), | |
4947 | pe_type(pe_type), code(code) {} | |
c4ce66a1 JS |
4948 | |
4949 | void handle_query_module(); | |
4950 | int handle_query_cu(Dwarf_Die * cudie); | |
4951 | ||
4952 | static int cast_query_cu (Dwarf_Die * cudie, void * arg); | |
4953 | }; | |
4954 | ||
4955 | ||
c4ce66a1 JS |
4956 | void |
4957 | dwarf_cast_query::handle_query_module() | |
4958 | { | |
abb41d92 | 4959 | if (!code.empty()) |
c4ce66a1 JS |
4960 | return; |
4961 | ||
4962 | // look for the type in each CU | |
4963 | dw.iterate_over_cus(cast_query_cu, this); | |
4964 | } | |
4965 | ||
4966 | ||
4967 | int | |
4968 | dwarf_cast_query::handle_query_cu(Dwarf_Die * cudie) | |
4969 | { | |
abb41d92 | 4970 | if (!code.empty()) |
c4ce66a1 JS |
4971 | return DWARF_CB_ABORT; |
4972 | ||
4973 | dw.focus_on_cu (cudie); | |
4974 | Dwarf_Die* type_die = dw.declaration_resolve(e.type.c_str()); | |
4975 | if (type_die) | |
4976 | { | |
4977 | try | |
4978 | { | |
4979 | code = dw.literal_stmt_for_pointer (type_die, e.components, | |
4980 | lvalue, pe_type); | |
4981 | } | |
4982 | catch (const semantic_error& e) | |
4983 | { | |
4984 | // XXX might be better to save the error | |
4985 | // and try again in another CU | |
4986 | sess.print_error (e); | |
c4ce66a1 | 4987 | } |
c4ce66a1 JS |
4988 | return DWARF_CB_ABORT; |
4989 | } | |
4990 | return DWARF_CB_OK; | |
4991 | } | |
4992 | ||
4993 | ||
4994 | int | |
4995 | dwarf_cast_query::cast_query_cu (Dwarf_Die * cudie, void * arg) | |
4996 | { | |
4997 | dwarf_cast_query * q = static_cast<dwarf_cast_query *>(arg); | |
4998 | if (pending_interrupts) return DWARF_CB_ABORT; | |
4999 | return q->handle_query_cu(cudie); | |
5000 | } | |
5001 | ||
5002 | ||
5003 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor | |
5004 | { | |
5005 | systemtap_session& s; | |
5006 | dwarf_builder& db; | |
5007 | ||
5008 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
5009 | s(s), db(db) {} | |
5010 | void visit_cast_op (cast_op* e); | |
5011 | }; | |
5012 | ||
5013 | ||
5014 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) | |
5015 | { | |
5016 | bool lvalue = is_active_lvalue(e); | |
5017 | if (lvalue && !s.guru_mode) | |
5018 | throw semantic_error("write to typecast value not permitted", e->tok); | |
5019 | ||
5020 | if (e->module.empty()) | |
5021 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
5022 | ||
c4ce66a1 JS |
5023 | string code; |
5024 | exp_type type = pe_long; | |
abb41d92 JS |
5025 | size_t mod_end = -1; |
5026 | do | |
c4ce66a1 | 5027 | { |
abb41d92 JS |
5028 | // split the module string by ':' for alternatives |
5029 | size_t mod_begin = mod_end + 1; | |
5030 | mod_end = e->module.find(':', mod_begin); | |
139dee87 | 5031 | string module = e->module.substr(mod_begin, mod_end - mod_begin); |
abb41d92 | 5032 | |
c4ce66a1 JS |
5033 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
5034 | // which means that userspace modules won't get any PATH searching. | |
5035 | dwflpp* dw; | |
abb41d92 | 5036 | if (module.find('/') == string::npos) |
c4ce66a1 JS |
5037 | { |
5038 | // kernel or kernel module target | |
5039 | if (! db.kern_dw) | |
5040 | { | |
5041 | db.kern_dw = new dwflpp(s); | |
5042 | db.kern_dw->setup_kernel(true); | |
5043 | } | |
5044 | dw = db.kern_dw; | |
5045 | } | |
5046 | else | |
5047 | { | |
abb41d92 | 5048 | module = find_executable (module); // canonicalize it |
c4ce66a1 JS |
5049 | |
5050 | // user-space target; we use one dwflpp instance per module name | |
5051 | // (= program or shared library) | |
abb41d92 | 5052 | if (db.user_dw.find(module) == db.user_dw.end()) |
c4ce66a1 JS |
5053 | { |
5054 | dw = new dwflpp(s); | |
abb41d92 JS |
5055 | dw->setup_user(module); |
5056 | db.user_dw[module] = dw; | |
c4ce66a1 JS |
5057 | } |
5058 | else | |
abb41d92 | 5059 | dw = db.user_dw[module]; |
c4ce66a1 JS |
5060 | } |
5061 | ||
abb41d92 JS |
5062 | dwarf_cast_query q (*dw, module, *e, lvalue, type, code); |
5063 | dw->query_modules(&q); | |
c4ce66a1 | 5064 | } |
abb41d92 JS |
5065 | while (code.empty() && mod_end != string::npos); |
5066 | ||
5067 | if (code.empty()) | |
c4ce66a1 | 5068 | { |
abb41d92 | 5069 | // We generate an error message, and pass the unresolved |
c4ce66a1 JS |
5070 | // cast_op to the next pass. We hope that this value ends |
5071 | // up not being referenced after all, so it can be optimized out | |
5072 | // quietly. | |
d0cd971e MW |
5073 | string msg = "type definition '" + e->type + "' not found"; |
5074 | semantic_error* er = new semantic_error (msg, e->tok); | |
c4ce66a1 JS |
5075 | // NB: we can have multiple errors, since a @cast |
5076 | // may be expanded in several different contexts: | |
5077 | // function ("*") { @cast(...) } | |
abb41d92 JS |
5078 | er->chain = e->saved_conversion_error; |
5079 | e->saved_conversion_error = er; | |
c4ce66a1 JS |
5080 | provide (e); |
5081 | return; | |
5082 | } | |
5083 | ||
5084 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") | |
5085 | + "_" + e->base_name.substr(1) | |
5086 | + "_" + lex_cast<string>(tick++)); | |
5087 | ||
5088 | // Synthesize a function. | |
5089 | functiondecl *fdecl = new functiondecl; | |
5090 | fdecl->tok = e->tok; | |
5091 | fdecl->type = type; | |
5092 | fdecl->name = fname; | |
5093 | ||
5094 | embeddedcode *ec = new embeddedcode; | |
5095 | ec->tok = e->tok; | |
5096 | ec->code = code; | |
5097 | fdecl->body = ec; | |
5098 | ||
5099 | // Give the fdecl an argument for the pointer we're trying to cast | |
5100 | vardecl *v1 = new vardecl; | |
5101 | v1->type = pe_long; | |
5102 | v1->name = "pointer"; | |
5103 | v1->tok = e->tok; | |
5104 | fdecl->formal_args.push_back(v1); | |
5105 | ||
5106 | if (lvalue) | |
5107 | { | |
5108 | // Modify the fdecl so it carries a second pe_long formal | |
5109 | // argument called "value". | |
5110 | ||
5111 | // FIXME: For the time being we only support setting target | |
5112 | // variables which have base types; these are 'pe_long' in | |
5113 | // stap's type vocabulary. Strings and pointers might be | |
5114 | // reasonable, some day, but not today. | |
5115 | ||
5116 | vardecl *v2 = new vardecl; | |
5117 | v2->type = pe_long; | |
5118 | v2->name = "value"; | |
5119 | v2->tok = e->tok; | |
5120 | fdecl->formal_args.push_back(v2); | |
5121 | } | |
5122 | else | |
5123 | ec->code += "/* pure */"; | |
5124 | ||
5125 | s.functions[fdecl->name] = fdecl; | |
5126 | ||
5127 | // Synthesize a functioncall. | |
5128 | functioncall* n = new functioncall; | |
5129 | n->tok = e->tok; | |
5130 | n->function = fname; | |
5131 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
5132 | n->args.push_back(e->operand); | |
5133 | ||
5134 | if (lvalue) | |
5135 | { | |
5136 | // Provide the functioncall to our parent, so that it can be | |
5137 | // used to substitute for the assignment node immediately above | |
5138 | // us. | |
5139 | assert(!target_symbol_setter_functioncalls.empty()); | |
5140 | *(target_symbol_setter_functioncalls.top()) = n; | |
5141 | } | |
5142 | ||
5143 | provide (n); | |
5144 | } | |
5145 | ||
5146 | ||
b8da0ad1 FCE |
5147 | void |
5148 | dwarf_derived_probe::printsig (ostream& o) const | |
5149 | { | |
5150 | // Instead of just printing the plain locations, we add a PC value | |
5151 | // as a comment as a way of telling e.g. apart multiple inlined | |
5152 | // function instances. This is distinct from the verbose/clog | |
5153 | // output, since this part goes into the cache hash calculations. | |
5154 | sole_location()->print (o); | |
6d0f3f0c | 5155 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
5156 | printsig_nested (o); |
5157 | } | |
5158 | ||
5159 | ||
5160 | ||
dc38c0ae | 5161 | void |
b20febf3 FCE |
5162 | dwarf_derived_probe::join_group (systemtap_session& s) |
5163 | { | |
5164 | if (! s.dwarf_derived_probes) | |
5165 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
5166 | s.dwarf_derived_probes->enroll (this); | |
5167 | } | |
5168 | ||
5169 | ||
5170 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, | |
5171 | const string& filename, | |
5172 | int line, | |
91af0778 | 5173 | // module & section specify a relocation |
b20febf3 FCE |
5174 | // base for <addr>, unless section=="" |
5175 | // (equivalently module=="kernel") | |
5176 | const string& module, | |
5177 | const string& section, | |
5178 | // NB: dwfl_addr is the virtualized | |
5179 | // address for this symbol. | |
5180 | Dwarf_Addr dwfl_addr, | |
5181 | // addr is the section-offset for | |
5182 | // actual relocation. | |
5183 | Dwarf_Addr addr, | |
5184 | dwarf_query& q, | |
37ebca01 | 5185 | Dwarf_Die* scope_die /* may be null */) |
1939ea32 | 5186 | : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ), |
b20febf3 | 5187 | module (module), section (section), addr (addr), |
c9bad430 DS |
5188 | has_return (q.has_return), |
5189 | has_maxactive (q.has_maxactive), | |
5190 | maxactive_val (q.maxactive_val) | |
bd2b1e68 | 5191 | { |
b20febf3 | 5192 | // Assert relocation invariants |
4baf0e53 | 5193 | if (section == "" && dwfl_addr != addr) // addr should be absolute |
84048984 FCE |
5194 | throw semantic_error ("missing relocation base against", q.base_loc->tok); |
5195 | if (section != "" && dwfl_addr == addr) // addr should be an offset | |
b20febf3 | 5196 | throw semantic_error ("inconsistent relocation address", q.base_loc->tok); |
df8fadee | 5197 | |
b20febf3 | 5198 | this->tok = q.base_probe->tok; |
b95e2b79 | 5199 | this->access_vars = false; |
2930abc7 | 5200 | |
21beacc9 FCE |
5201 | // XXX: hack for strange g++/gcc's |
5202 | #ifndef USHRT_MAX | |
5203 | #define USHRT_MAX 32767 | |
5204 | #endif | |
5205 | ||
606fd9c8 FCE |
5206 | // Range limit maxactive() value |
5207 | if (q.has_maxactive && (q.maxactive_val < 0 || q.maxactive_val > USHRT_MAX)) | |
5208 | throw semantic_error ("maxactive value out of range [0," | |
5209 | + lex_cast<string>(USHRT_MAX) + "]", | |
5210 | q.base_loc->tok); | |
5211 | ||
de688825 | 5212 | // Expand target variables in the probe body |
5f0a03a6 | 5213 | if (!null_die(scope_die)) |
8fc05e57 | 5214 | { |
de688825 | 5215 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
4ed05b15 | 5216 | this->body = v.require (this->body); |
b95e2b79 | 5217 | this->access_vars = v.visited; |
37ebca01 FCE |
5218 | |
5219 | // If during target-variable-expanding the probe, we added a new block | |
5220 | // of code, add it to the start of the probe. | |
5221 | if (v.add_block) | |
ba6f838d | 5222 | this->body = new block(v.add_block, this->body); |
37ebca01 FCE |
5223 | // If when target-variable-expanding the probe, we added a new |
5224 | // probe, add it in a new file to the list of files to be processed. | |
5225 | if (v.add_probe) | |
5226 | { | |
5227 | stapfile *f = new stapfile; | |
5228 | f->probes.push_back(v.add_probe); | |
5229 | q.sess.files.push_back(f); | |
5230 | } | |
8fc05e57 | 5231 | } |
37ebca01 | 5232 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 5233 | |
5d23847d | 5234 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
5235 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
5236 | // point. This allows a user to see what function / file / line | |
5237 | // number any particular match of the wildcards. | |
2930abc7 | 5238 | |
a229fcd7 | 5239 | vector<probe_point::component*> comps; |
91af0778 FCE |
5240 | if (q.has_kernel) |
5241 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
5242 | else if(q.has_module) | |
5243 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
5244 | else if(q.has_process) | |
5245 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
5246 | else | |
5247 | assert (0); | |
b5d77020 | 5248 | |
db520b00 FCE |
5249 | string fn_or_stmt; |
5250 | if (q.has_function_str || q.has_function_num) | |
5251 | fn_or_stmt = "function"; | |
5252 | else | |
5253 | fn_or_stmt = "statement"; | |
a229fcd7 | 5254 | |
b8da0ad1 | 5255 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 5256 | { |
4cd232e4 | 5257 | string retro_name = funcname; |
b20febf3 | 5258 | if (filename != "") |
cee35f73 | 5259 | { |
fb84c077 | 5260 | retro_name += ("@" + string (filename)); |
cee35f73 | 5261 | if (line > 0) |
fb84c077 | 5262 | retro_name += (":" + lex_cast<string> (line)); |
cee35f73 | 5263 | } |
db520b00 FCE |
5264 | comps.push_back |
5265 | (new probe_point::component | |
5266 | (fn_or_stmt, new literal_string (retro_name))); | |
5267 | } | |
b8da0ad1 | 5268 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
5269 | { |
5270 | Dwarf_Addr retro_addr; | |
5271 | if (q.has_function_num) | |
5272 | retro_addr = q.function_num_val; | |
5273 | else | |
5274 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
5275 | comps.push_back (new probe_point::component |
5276 | (fn_or_stmt, | |
5277 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
37ebca01 FCE |
5278 | |
5279 | if (q.has_absolute) | |
5280 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
5281 | } |
5282 | ||
b8da0ad1 FCE |
5283 | if (q.has_call) |
5284 | comps.push_back (new probe_point::component(TOK_CALL)); | |
5285 | if (q.has_inline) | |
5286 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 5287 | if (has_return) |
b8da0ad1 FCE |
5288 | comps.push_back (new probe_point::component(TOK_RETURN)); |
5289 | if (has_maxactive) | |
5290 | comps.push_back (new probe_point::component | |
5291 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 5292 | |
5d23847d FCE |
5293 | // Overwrite it. |
5294 | this->sole_location()->components = comps; | |
2930abc7 FCE |
5295 | } |
5296 | ||
bd2b1e68 | 5297 | |
7a053d3b | 5298 | void |
20c6c071 | 5299 | dwarf_derived_probe::register_statement_variants(match_node * root, |
bd2b1e68 GH |
5300 | dwarf_builder * dw) |
5301 | { | |
54efe513 | 5302 | root->bind(dw); |
54efe513 GH |
5303 | } |
5304 | ||
7a053d3b | 5305 | void |
fd6602a0 | 5306 | dwarf_derived_probe::register_function_variants(match_node * root, |
c9bad430 | 5307 | dwarf_builder * dw) |
bd2b1e68 | 5308 | { |
fd6602a0 | 5309 | root->bind(dw); |
b8da0ad1 FCE |
5310 | root->bind(TOK_INLINE)->bind(dw); |
5311 | root->bind(TOK_CALL)->bind(dw); | |
fd6602a0 | 5312 | root->bind(TOK_RETURN)->bind(dw); |
c9bad430 | 5313 | root->bind(TOK_RETURN)->bind_num(TOK_MAXACTIVE)->bind(dw); |
bd2b1e68 GH |
5314 | } |
5315 | ||
7a053d3b | 5316 | void |
20c6c071 | 5317 | dwarf_derived_probe::register_function_and_statement_variants(match_node * root, |
bd2b1e68 GH |
5318 | dwarf_builder * dw) |
5319 | { | |
5320 | // Here we match 4 forms: | |
5321 | // | |
5322 | // .function("foo") | |
5323 | // .function(0xdeadbeef) | |
5324 | // .statement("foo") | |
5325 | // .statement(0xdeadbeef) | |
5326 | ||
fd6602a0 FCE |
5327 | register_function_variants(root->bind_str(TOK_FUNCTION), dw); |
5328 | register_function_variants(root->bind_num(TOK_FUNCTION), dw); | |
20c6c071 GH |
5329 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw); |
5330 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw); | |
bd2b1e68 GH |
5331 | } |
5332 | ||
5333 | void | |
c4ce66a1 | 5334 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 5335 | { |
c4ce66a1 | 5336 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
5337 | dwarf_builder *dw = new dwarf_builder(); |
5338 | ||
c4ce66a1 JS |
5339 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
5340 | s.code_filters.push_back(filter); | |
5341 | ||
20c6c071 GH |
5342 | register_function_and_statement_variants(root->bind(TOK_KERNEL), dw); |
5343 | register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw); | |
37ebca01 | 5344 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(dw); |
0f336e95 SC |
5345 | root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); |
5346 | root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw); | |
37ebca01 | 5347 | |
7a24d422 | 5348 | register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw); |
f28a8c28 SC |
5349 | root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)->bind(dw); |
5350 | root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK)->bind(dw); | |
bd2b1e68 GH |
5351 | } |
5352 | ||
9020300d FCE |
5353 | void |
5354 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
5355 | { | |
b95e2b79 MH |
5356 | if (access_vars) |
5357 | { | |
5358 | // if accessing $variables, emit bsp cache setup for speeding up | |
5359 | o->newline() << "bspcache(c->unwaddr, c->regs);"; | |
5360 | } | |
9020300d | 5361 | } |
2930abc7 | 5362 | |
b20febf3 | 5363 | // ------------------------------------------------------------------------ |
46b84a80 DS |
5364 | |
5365 | void | |
b20febf3 | 5366 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 5367 | { |
b20febf3 | 5368 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
5369 | |
5370 | // XXX: probes put at the same address should all share a | |
5371 | // single kprobe/kretprobe, and have their handlers executed | |
5372 | // sequentially. | |
b55bc428 FCE |
5373 | } |
5374 | ||
2930abc7 | 5375 | |
7a053d3b | 5376 | void |
775d51e5 | 5377 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 5378 | { |
b20febf3 | 5379 | if (probes_by_module.empty()) return; |
2930abc7 | 5380 | |
775d51e5 DS |
5381 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
5382 | ||
5383 | // Warn of misconfigured kernels | |
f41595cc FCE |
5384 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
5385 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
5386 | s.op->newline() << "#endif"; | |
775d51e5 | 5387 | s.op->newline(); |
f41595cc | 5388 | |
b20febf3 FCE |
5389 | // Forward declare the master entry functions |
5390 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5391 | s.op->line() << " struct pt_regs *regs);"; | |
5392 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5393 | s.op->line() << " struct pt_regs *regs);"; | |
5394 | ||
42cb22bd MH |
5395 | // Emit an array of kprobe/kretprobe pointers |
5396 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5397 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
5398 | s.op->newline() << "#endif"; | |
5399 | ||
b20febf3 | 5400 | // Emit the actual probe list. |
606fd9c8 FCE |
5401 | |
5402 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
5403 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
5404 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
4c2732a1 | 5405 | s.op->newline() << "static struct stap_dwarf_kprobe {"; |
b20febf3 | 5406 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; |
e4cb375f MH |
5407 | s.op->newline() << "#ifdef __ia64__"; |
5408 | s.op->newline() << "struct kprobe dummy;"; | |
5409 | s.op->newline() << "#endif"; | |
606fd9c8 FCE |
5410 | s.op->newline(-1) << "} stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
5411 | // NB: bss! | |
5412 | ||
4c2732a1 | 5413 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
5414 | s.op->newline(1) << "const unsigned return_p:1;"; |
5415 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b20febf3 | 5416 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 5417 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 FCE |
5418 | |
5419 | // Let's find some stats for the three embedded strings. Maybe they | |
5420 | // are small and uniform enough to justify putting char[MAX]'s into | |
5421 | // the array instead of relocated char*'s. | |
5422 | size_t module_name_max = 0, section_name_max = 0, pp_name_max = 0; | |
5423 | size_t module_name_tot = 0, section_name_tot = 0, pp_name_tot = 0; | |
5424 | size_t all_name_cnt = probes_by_module.size(); // for average | |
5425 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
5426 | { | |
5427 | dwarf_derived_probe* p = it->second; | |
5428 | #define DOIT(var,expr) do { \ | |
5429 | size_t var##_size = (expr) + 1; \ | |
5430 | var##_max = max (var##_max, var##_size); \ | |
5431 | var##_tot += var##_size; } while (0) | |
5432 | DOIT(module_name, p->module.size()); | |
5433 | DOIT(section_name, p->section.size()); | |
5434 | DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size()); | |
5435 | #undef DOIT | |
5436 | } | |
5437 | ||
5438 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
5439 | // the amount of average waste (max - avg) to the relocation data size | |
5440 | // (3 native long words). | |
5441 | #define CALCIT(var) \ | |
5442 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
5443 | { \ | |
5444 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
5445 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
5446 | << "[" << var##_name_max << "]" << endl; \ | |
5447 | } \ | |
5448 | else \ | |
5449 | { \ | |
b0986e7a | 5450 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
5451 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
5452 | } | |
5453 | ||
5454 | CALCIT(module); | |
5455 | CALCIT(section); | |
5456 | CALCIT(pp); | |
5457 | ||
b0986e7a DS |
5458 | s.op->newline() << "const unsigned long address;"; |
5459 | s.op->newline() << "void (* const ph) (struct context*);"; | |
b20febf3 FCE |
5460 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
5461 | s.op->indent(1); | |
5462 | ||
5463 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 5464 | { |
b20febf3 FCE |
5465 | dwarf_derived_probe* p = it->second; |
5466 | s.op->newline() << "{"; | |
5467 | if (p->has_return) | |
5468 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 5469 | if (p->has_maxactive) |
606fd9c8 FCE |
5470 | { |
5471 | s.op->line() << " .maxactive_p=1,"; | |
5472 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
5473 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
5474 | } | |
dc38c256 | 5475 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
5476 | s.op->line() << " .module=\"" << p->module << "\","; |
5477 | s.op->line() << " .section=\"" << p->section << "\","; | |
b20febf3 FCE |
5478 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
5479 | s.op->line() << " .ph=&" << p->name; | |
5480 | s.op->line() << " },"; | |
2930abc7 | 5481 | } |
2930abc7 | 5482 | |
b20febf3 FCE |
5483 | s.op->newline(-1) << "};"; |
5484 | ||
5485 | // Emit the kprobes callback function | |
5486 | s.op->newline(); | |
5487 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5488 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
5489 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
5490 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
5491 | // Check that the index is plausible | |
5492 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5493 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5494 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5495 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5496 | s.op->line() << "];"; | |
c12d974f | 5497 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
5498 | s.op->newline() << "c->regs = regs;"; |
5499 | s.op->newline() << "(*sdp->ph) (c);"; | |
5500 | common_probe_entryfn_epilogue (s.op); | |
5501 | s.op->newline() << "return 0;"; | |
5502 | s.op->newline(-1) << "}"; | |
5503 | ||
5504 | // Same for kretprobes | |
5505 | s.op->newline(); | |
5506 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5507 | s.op->line() << " struct pt_regs *regs) {"; | |
5508 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
606fd9c8 FCE |
5509 | |
5510 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 5511 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
5512 | // Check that the index is plausible |
5513 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5514 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5515 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5516 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5517 | s.op->line() << "];"; | |
5518 | ||
c12d974f | 5519 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp"); |
b20febf3 FCE |
5520 | s.op->newline() << "c->regs = regs;"; |
5521 | s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic | |
5522 | s.op->newline() << "(*sdp->ph) (c);"; | |
5523 | common_probe_entryfn_epilogue (s.op); | |
5524 | s.op->newline() << "return 0;"; | |
5525 | s.op->newline(-1) << "}"; | |
20c6c071 | 5526 | } |
ec4373ff | 5527 | |
20c6c071 | 5528 | |
dc38c0ae | 5529 | void |
b20febf3 FCE |
5530 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
5531 | { | |
5532 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5533 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5534 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
f1bad60c | 5535 | s.op->newline() << "unsigned long relocated_addr = _stp_module_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 5536 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
6d0f3f0c | 5537 | s.op->newline() << "probe_point = sdp->pp;"; // for error messages |
b20febf3 | 5538 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 5539 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 5540 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 5541 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 5542 | s.op->newline(-1) << "} else {"; |
606fd9c8 | 5543 | s.op->newline(1) << "kp->u.krp.maxactive = max(10, 4*NR_CPUS);"; |
c9bad430 | 5544 | s.op->newline(-1) << "}"; |
606fd9c8 | 5545 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
e4cb375f MH |
5546 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
5547 | s.op->newline() << "#ifdef __ia64__"; | |
5548 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5549 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5550 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5551 | s.op->newline() << "if (rc == 0) {"; | |
5552 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5553 | s.op->newline() << "if (rc != 0)"; | |
5554 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5555 | s.op->newline(-2) << "}"; | |
5556 | s.op->newline() << "#else"; | |
606fd9c8 | 5557 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 5558 | s.op->newline() << "#endif"; |
b20febf3 | 5559 | s.op->newline(-1) << "} else {"; |
e4cb375f | 5560 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
5561 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
5562 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
5563 | s.op->newline() << "#ifdef __ia64__"; |
5564 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5565 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5566 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5567 | s.op->newline() << "if (rc == 0) {"; | |
5568 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5569 | s.op->newline() << "if (rc != 0)"; | |
5570 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5571 | s.op->newline(-2) << "}"; | |
5572 | s.op->newline() << "#else"; | |
606fd9c8 | 5573 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 5574 | s.op->newline() << "#endif"; |
b20febf3 | 5575 | s.op->newline(-1) << "}"; |
9063462a FCE |
5576 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
5577 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
5578 | s.op->newline() << "_stp_warn (\"probe %s registration error (rc %d)\", probe_point, rc);"; | |
5579 | s.op->newline() << "rc = 0;"; // continue with other probes | |
5580 | // XXX: shall we increment numskipped? | |
5581 | s.op->newline(-1) << "}"; | |
5582 | ||
5583 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 5584 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 5585 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
5586 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
5587 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
5588 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
5589 | s.op->newline() << "#ifdef __ia64__"; |
5590 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
5591 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
5592 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
5593 | // not run for this early-abort case. | |
5594 | s.op->newline(-1) << "}"; | |
5595 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 5596 | s.op->newline(-1) << "}"; |
9063462a FCE |
5597 | #endif |
5598 | ||
b20febf3 FCE |
5599 | s.op->newline() << "else sdp->registered_p = 1;"; |
5600 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
5601 | } |
5602 | ||
5603 | ||
46b84a80 | 5604 | void |
b20febf3 | 5605 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 5606 | { |
42cb22bd MH |
5607 | //Unregister kprobes by batch interfaces. |
5608 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5609 | s.op->newline() << "j = 0;"; | |
5610 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5611 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5612 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5613 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5614 | s.op->newline() << "if (!sdp->return_p)"; | |
5615 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
5616 | s.op->newline(-2) << "}"; | |
5617 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5618 | s.op->newline() << "j = 0;"; | |
5619 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5620 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5621 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5622 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5623 | s.op->newline() << "if (sdp->return_p)"; | |
5624 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
5625 | s.op->newline(-2) << "}"; | |
5626 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
5627 | s.op->newline() << "#ifdef __ia64__"; |
5628 | s.op->newline() << "j = 0;"; | |
5629 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5630 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5631 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5632 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5633 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
5634 | s.op->newline(-1) << "}"; | |
5635 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5636 | s.op->newline() << "#endif"; | |
42cb22bd MH |
5637 | s.op->newline() << "#endif"; |
5638 | ||
b20febf3 FCE |
5639 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
5640 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5641 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
5642 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
5643 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 5644 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5645 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 5646 | s.op->newline() << "#endif"; |
606fd9c8 | 5647 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);"; |
73209876 FCE |
5648 | s.op->newline() << "#ifdef STP_TIMING"; |
5649 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
d01eaa30 | 5650 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);"; |
73209876 | 5651 | s.op->newline(-1) << "#endif"; |
606fd9c8 | 5652 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5653 | s.op->newline() << "#ifdef STP_TIMING"; |
5654 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
d01eaa30 | 5655 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);"; |
73209876 | 5656 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 5657 | s.op->newline(-1) << "} else {"; |
42cb22bd | 5658 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5659 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 5660 | s.op->newline() << "#endif"; |
606fd9c8 | 5661 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);"; |
73209876 FCE |
5662 | s.op->newline() << "#ifdef STP_TIMING"; |
5663 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
d01eaa30 | 5664 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);"; |
73209876 | 5665 | s.op->newline(-1) << "#endif"; |
b20febf3 | 5666 | s.op->newline(-1) << "}"; |
e4cb375f MH |
5667 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
5668 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5669 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
5670 | s.op->newline() << "sdp->registered_p = 0;"; |
5671 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
5672 | } |
5673 | ||
5674 | ||
20c6c071 | 5675 | void |
5227f1ea | 5676 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 5677 | probe * base, |
20c6c071 | 5678 | probe_point * location, |
86bf665e | 5679 | literal_map_t const & parameters, |
20c6c071 GH |
5680 | vector<derived_probe *> & finished_results) |
5681 | { | |
b20febf3 FCE |
5682 | // NB: the kernel/user dwlfpp objects are long-lived. |
5683 | // XXX: but they should be per-session, as this builder object | |
5684 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 5685 | |
7a24d422 FCE |
5686 | dwflpp* dw = 0; |
5687 | ||
7a24d422 FCE |
5688 | string module_name; |
5689 | if (has_null_param (parameters, TOK_KERNEL) | |
5690 | || get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 5691 | { |
7a24d422 FCE |
5692 | // kernel or kernel module target |
5693 | if (! kern_dw) | |
5694 | { | |
5695 | kern_dw = new dwflpp(sess); | |
28d29bd3 FCE |
5696 | // XXX: PR 3498, PR 6864 |
5697 | kern_dw->setup_kernel(true); | |
7a24d422 FCE |
5698 | } |
5699 | dw = kern_dw; | |
b8da0ad1 | 5700 | } |
7a24d422 | 5701 | else if (get_param (parameters, TOK_PROCESS, module_name)) |
b8da0ad1 | 5702 | { |
d0a7f5a9 FCE |
5703 | module_name = find_executable (module_name); // canonicalize it |
5704 | ||
7a24d422 FCE |
5705 | // user-space target; we use one dwflpp instance per module name |
5706 | // (= program or shared library) | |
5707 | if (user_dw.find(module_name) == user_dw.end()) | |
84048984 | 5708 | { |
7a24d422 FCE |
5709 | dw = new dwflpp(sess); |
5710 | // XXX: PR 3498 | |
5711 | dw->setup_user(module_name); | |
5712 | user_dw[module_name] = dw; | |
84048984 | 5713 | } |
7a24d422 FCE |
5714 | else |
5715 | dw = user_dw[module_name]; | |
c8959a29 | 5716 | } |
20c6c071 | 5717 | |
f28a8c28 | 5718 | if (((probe_point::component*)(location->components[1]))->functor == TOK_MARK) |
349dc70e SC |
5719 | { |
5720 | enum probe_types | |
f28a8c28 | 5721 | { |
7b534f48 SC |
5722 | probes_and_dwarf = 0, // Use statement address |
5723 | dwarf_no_probes = 1, // Use label name | |
5724 | probes_no_dwarf = 2 | |
349dc70e | 5725 | }; |
f28a8c28 | 5726 | |
7b534f48 SC |
5727 | int probe_type = dwarf_no_probes; |
5728 | string probe_name = (char*) location->components[1]->arg->tok->content.c_str(); | |
5729 | __uint64_t probe_arg = 0; | |
349dc70e | 5730 | Dwarf_Addr bias; |
1f0cfd98 | 5731 | Elf* elf = dwfl_module_getelf (dw->module, &bias); |
349dc70e | 5732 | size_t shstrndx; |
349dc70e | 5733 | Elf_Scn *probe_scn = NULL; |
7b534f48 | 5734 | |
349dc70e | 5735 | dwfl_assert ("getshstrndx", elf_getshstrndx (elf, &shstrndx)); |
7b534f48 SC |
5736 | GElf_Shdr *shdr = NULL; |
5737 | ||
5738 | // Is there a .probes section? | |
349dc70e SC |
5739 | while ((probe_scn = elf_nextscn (elf, probe_scn))) |
5740 | { | |
5741 | GElf_Shdr shdr_mem; | |
7b534f48 | 5742 | shdr = gelf_getshdr (probe_scn, &shdr_mem); |
349dc70e SC |
5743 | assert (shdr != NULL); |
5744 | ||
7b534f48 SC |
5745 | if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0) |
5746 | { | |
5747 | probe_type = probes_and_dwarf; | |
5748 | break; | |
5749 | } | |
5750 | } | |
5751 | ||
9b753eda | 5752 | if (probe_type == probes_and_dwarf) |
7b534f48 | 5753 | { |
1f0cfd98 | 5754 | Elf_Data *pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
349dc70e SC |
5755 | assert (pdata != NULL); |
5756 | size_t probe_scn_offset = 0; | |
c1008fd0 | 5757 | size_t probe_scn_addr = shdr->sh_addr; |
349dc70e SC |
5758 | while (probe_scn_offset < pdata->d_size) |
5759 | { | |
c1008fd0 | 5760 | const int stap_sentinel = 0x31425250; |
1f0cfd98 | 5761 | probe_type = *((int*)((char*)pdata->d_buf + probe_scn_offset)); |
c1008fd0 SC |
5762 | if (probe_type != stap_sentinel) |
5763 | { | |
5764 | probe_scn_offset += sizeof(int); | |
5765 | continue; | |
5766 | } | |
349dc70e | 5767 | probe_scn_offset += sizeof(int); |
c1008fd0 SC |
5768 | if (probe_scn_offset % (sizeof(__uint64_t))) |
5769 | probe_scn_offset += sizeof(__uint64_t) - (probe_scn_offset % sizeof(__uint64_t)); | |
5770 | ||
c1008fd0 SC |
5771 | probe_name = ((char*)((long)(pdata->d_buf) + (long)(*((int*)((long)pdata->d_buf + probe_scn_offset)) - probe_scn_addr))); |
5772 | probe_scn_offset += sizeof(void*); | |
1f0cfd98 SC |
5773 | if (probe_scn_offset % (sizeof(__uint64_t))) |
5774 | probe_scn_offset += sizeof(__uint64_t) - (probe_scn_offset % sizeof(__uint64_t)); | |
5775 | probe_arg = *((__uint64_t*)((char*)pdata->d_buf + probe_scn_offset)); | |
1f0cfd98 SC |
5776 | if (probe_scn_offset % (sizeof(__uint64_t)*2)) |
5777 | probe_scn_offset = (probe_scn_offset + sizeof(__uint64_t)*2) - (probe_scn_offset % (sizeof(__uint64_t)*2)); | |
64c6aab0 | 5778 | if (strcmp (location->components[1]->arg->tok->content.c_str(), probe_name.c_str()) != 0) |
9e67aff9 SC |
5779 | continue; |
5780 | const token* sv_tok = location->components[1]->arg->tok; | |
5781 | location->components[1]->functor = TOK_STATEMENT; | |
5782 | location->components[1]->arg = new literal_number((int)probe_arg); | |
5783 | location->components[1]->arg->tok = sv_tok; | |
5784 | ((literal_map_t&)parameters)[TOK_STATEMENT] = location->components[1]->arg; | |
5785 | dwarf_query q(sess, base, location, *dw, parameters, finished_results); | |
5786 | dw->query_modules(&q); | |
349dc70e | 5787 | } |
64c6aab0 | 5788 | return; |
349dc70e | 5789 | } |
59b2ec52 | 5790 | |
64c6aab0 | 5791 | if (probe_type == dwarf_no_probes) |
349dc70e | 5792 | { |
7b534f48 SC |
5793 | location->components[1]->functor = TOK_FUNCTION; |
5794 | location->components[1]->arg = new literal_string("*"); | |
5795 | ((literal_map_t&)parameters)[TOK_FUNCTION] = location->components[1]->arg; | |
5796 | location->components.push_back(new probe_point::component(TOK_LABEL)); | |
5797 | location->components[2]->arg = new literal_string("_stapprobe1_" + probe_name); | |
5798 | ((literal_map_t&)parameters).erase(TOK_MARK); | |
5799 | ((literal_map_t&)parameters).insert(pair<string,literal*>(TOK_LABEL, location->components[2]->arg)); | |
349dc70e | 5800 | } |
1f0cfd98 | 5801 | |
349dc70e SC |
5802 | dw->module = 0; |
5803 | } | |
5804 | ||
c8959a29 | 5805 | dwarf_query q(sess, base, location, *dw, parameters, finished_results); |
20c6c071 | 5806 | |
7a24d422 FCE |
5807 | |
5808 | // XXX: kernel.statement.absolute is a special case that requires no | |
5809 | // dwfl processing. This code should be in a separate builder. | |
5810 | ||
5811 | if (q.has_kernel && q.has_absolute) | |
37ebca01 | 5812 | { |
4baf0e53 | 5813 | // assert guru mode for absolute probes |
37ebca01 FCE |
5814 | if (! q.base_probe->privileged) |
5815 | { | |
5816 | throw semantic_error ("absolute statement probe in unprivileged script", q.base_probe->tok); | |
5817 | } | |
5818 | ||
5819 | // For kernel.statement(NUM).absolute probe points, we bypass | |
5820 | // all the debuginfo stuff: We just wire up a | |
5821 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 5822 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
5823 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
5824 | q.statement_num_val, q.statement_num_val, | |
5825 | q, 0); | |
37ebca01 | 5826 | finished_results.push_back (p); |
1a0dbc5a | 5827 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
5828 | return; |
5829 | } | |
5830 | ||
5f0a03a6 JK |
5831 | dw->query_modules(&q); |
5832 | } | |
5833 | ||
5834 | symbol_table::~symbol_table() | |
5835 | { | |
2e67a43b TM |
5836 | for (iterator_t i = list_by_addr.begin(); i != list_by_addr.end(); ++i) |
5837 | delete *i; | |
5f0a03a6 JK |
5838 | } |
5839 | ||
5840 | void | |
ab91b232 JK |
5841 | symbol_table::add_symbol(const char *name, bool weak, Dwarf_Addr addr, |
5842 | Dwarf_Addr *high_addr) | |
5f0a03a6 | 5843 | { |
ab91b232 JK |
5844 | #ifdef __powerpc__ |
5845 | // Map ".sys_foo" to "sys_foo". | |
5846 | if (name[0] == '.') | |
5847 | name++; | |
5848 | #endif | |
5f0a03a6 JK |
5849 | func_info *fi = new func_info(); |
5850 | fi->addr = addr; | |
5851 | fi->name = name; | |
ab91b232 | 5852 | fi->weak = weak; |
5f0a03a6 JK |
5853 | map_by_name[fi->name] = fi; |
5854 | // TODO: Use a multimap in case there are multiple static | |
5855 | // functions with the same name? | |
2e67a43b | 5856 | list_by_addr.push_back(fi); |
5f0a03a6 JK |
5857 | } |
5858 | ||
5859 | enum info_status | |
5860 | symbol_table::read_symbols(FILE *f, const string& path) | |
5861 | { | |
5862 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
5863 | int ret; | |
2e67a43b TM |
5864 | char *name = 0; |
5865 | char *mod = 0; | |
5f0a03a6 JK |
5866 | char type; |
5867 | unsigned long long addr; | |
5868 | Dwarf_Addr high_addr = 0; | |
5869 | int line = 0; | |
5870 | ||
5871 | // %as (non-POSIX) mallocs space for the string and stores its address. | |
5872 | while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0) | |
5873 | { | |
2e67a43b TM |
5874 | auto_free free_name(name); |
5875 | auto_free free_mod(mod); | |
5f0a03a6 JK |
5876 | line++; |
5877 | if (ret < 3) | |
5878 | { | |
41c262f3 | 5879 | cerr << "Symbol table error: Line " |
5f0a03a6 JK |
5880 | << line |
5881 | << " of symbol list from " | |
5882 | << path | |
5883 | << " is not in correct format: address type name [module]"; | |
5884 | // Caller should delete symbol_table object. | |
5885 | return info_absent; | |
5886 | } | |
2e67a43b | 5887 | else if (ret > 3) |
5f0a03a6 JK |
5888 | { |
5889 | // Modules are loaded above the kernel, so if we're getting | |
5890 | // modules, we're done. | |
2e67a43b | 5891 | break; |
5f0a03a6 | 5892 | } |
ab91b232 JK |
5893 | if (type == 'T' || type == 't' || type == 'W') |
5894 | add_symbol(name, (type == 'W'), (Dwarf_Addr) addr, &high_addr); | |
5f0a03a6 JK |
5895 | } |
5896 | ||
5f0a03a6 JK |
5897 | if (list_by_addr.size() < 1) |
5898 | { | |
5899 | cerr << "Symbol table error: " | |
5900 | << path << " contains no function symbols." << endl; | |
5901 | return info_absent; | |
5902 | } | |
2e67a43b | 5903 | sort(); |
5f0a03a6 JK |
5904 | return info_present; |
5905 | } | |
5906 | ||
5907 | // NB: This currently unused. We use get_from_elf() instead because | |
5908 | // that gives us raw addresses -- which we need for modules -- whereas | |
5909 | // nm provides the address relative to the beginning of the section. | |
5910 | enum info_status | |
5911 | symbol_table::read_from_elf_file(const string &path) | |
5912 | { | |
5913 | FILE *f; | |
5914 | string cmd = string("/usr/bin/nm -n --defined-only ") + path; | |
5915 | f = popen(cmd.c_str(), "r"); | |
5916 | if (!f) | |
5917 | { | |
5918 | // nm failures are detected by pclose, not popen. | |
5919 | cerr << "Internal error reading symbol table from " | |
5920 | << path << " -- " << strerror (errno); | |
5921 | return info_absent; | |
5922 | } | |
5923 | enum info_status status = read_symbols(f, path); | |
5924 | if (pclose(f) != 0) | |
5925 | { | |
5926 | if (status == info_present) | |
5927 | cerr << "Warning: nm cannot read symbol table from " << path; | |
5928 | return info_absent; | |
5929 | } | |
5930 | return status; | |
5931 | } | |
5932 | ||
5933 | enum info_status | |
5934 | symbol_table::read_from_text_file(const string& path) | |
5935 | { | |
5936 | FILE *f = fopen(path.c_str(), "r"); | |
5937 | if (!f) | |
5938 | { | |
5939 | cerr << "Warning: cannot read symbol table from " | |
5940 | << path << " -- " << strerror (errno); | |
5941 | return info_absent; | |
5942 | } | |
5943 | enum info_status status = read_symbols(f, path); | |
5944 | (void) fclose(f); | |
5945 | return status; | |
5946 | } | |
5947 | ||
46f7b6be JK |
5948 | void |
5949 | symbol_table::prepare_section_rejection(Dwfl_Module *mod) | |
5950 | { | |
5951 | #ifdef __powerpc__ | |
5952 | /* | |
5953 | * The .opd section contains function descriptors that can look | |
5954 | * just like function entry points. For example, there's a function | |
5955 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
5956 | * Reject all symbols in .opd. | |
5957 | */ | |
5958 | opd_section = SHN_UNDEF; | |
5959 | Dwarf_Addr bias; | |
5960 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
5961 | ?: dwfl_module_getelf (mod, &bias)); | |
5962 | Elf_Scn* scn = 0; | |
5963 | size_t shstrndx; | |
5964 | ||
5965 | if (!elf) | |
5966 | return; | |
5967 | if (elf_getshstrndx(elf, &shstrndx) != 0) | |
5968 | return; | |
5969 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
5970 | { | |
5971 | GElf_Shdr shdr_mem; | |
5972 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
5973 | if (!shdr) | |
5974 | continue; | |
5975 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
5976 | if (!strcmp(name, ".opd")) | |
5977 | { | |
5978 | opd_section = elf_ndxscn(scn); | |
5979 | return; | |
5980 | } | |
5981 | } | |
5982 | #endif | |
5983 | } | |
5984 | ||
5985 | bool | |
5986 | symbol_table::reject_section(GElf_Word section) | |
5987 | { | |
5988 | if (section == SHN_UNDEF) | |
5989 | return true; | |
5990 | #ifdef __powerpc__ | |
5991 | if (section == opd_section) | |
5992 | return true; | |
5993 | #endif | |
5994 | return false; | |
5995 | } | |
5996 | ||
5f0a03a6 JK |
5997 | enum info_status |
5998 | symbol_table::get_from_elf() | |
5999 | { | |
6000 | Dwarf_Addr high_addr = 0; | |
6001 | Dwfl_Module *mod = mod_info->mod; | |
6002 | int syments = dwfl_module_getsymtab(mod); | |
6003 | assert(syments); | |
46f7b6be | 6004 | prepare_section_rejection(mod); |
5f0a03a6 JK |
6005 | for (int i = 1; i < syments; ++i) |
6006 | { | |
6007 | GElf_Sym sym; | |
ab91b232 JK |
6008 | GElf_Word section; |
6009 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
46f7b6be JK |
6010 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC && |
6011 | !reject_section(section)) | |
ab91b232 JK |
6012 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
6013 | sym.st_value, &high_addr); | |
5f0a03a6 | 6014 | } |
2e67a43b | 6015 | sort(); |
5f0a03a6 JK |
6016 | return info_present; |
6017 | } | |
6018 | ||
6019 | void | |
6020 | symbol_table::mark_dwarf_redundancies(dwflpp *dw) | |
6021 | { | |
6022 | // dwflpp.cu_function_cache maps each module_name:cu_name to a | |
6023 | // vector of Dwarf_Dies, one per function. | |
6024 | string module_prefix = string(mod_info->name) + ":"; | |
6025 | ||
73f289b2 | 6026 | for (mod_cu_function_cache_t::iterator cu = dw->cu_function_cache.begin(); |
6561773f | 6027 | cu != dw->cu_function_cache.end(); cu++) |
5f0a03a6 JK |
6028 | { |
6029 | string key = cu->first; | |
6030 | if (key.find(module_prefix) == 0) | |
6031 | { | |
6032 | // Found a compilation unit in the module of interest. | |
6033 | // Mark all its functions in the symbol table. | |
6561773f | 6034 | cu_function_cache_t* v = cu->second; |
5f0a03a6 | 6035 | assert(v); |
6561773f | 6036 | for (cu_function_cache_t::iterator fc = v->begin(); fc != v->end(); fc++) |
5f0a03a6 | 6037 | { |
6561773f FCE |
6038 | Dwarf_Die func = fc->second; |
6039 | string func_name = fc->first; // == dwarf_diename(&func); | |
5f0a03a6 JK |
6040 | // map_by_name[func_name]->die = func; |
6041 | map<string, func_info*>::iterator i = map_by_name.find(func_name); | |
6042 | // Func names can show up in the dwarf but not the symtab (!). | |
6043 | if (i != map_by_name.end()) | |
6044 | { | |
6045 | func_info *fi = i->second; | |
6046 | fi->die = func; | |
6047 | } | |
6048 | } | |
6049 | } | |
6050 | } | |
6051 | } | |
6052 | ||
6053 | func_info * | |
6054 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
6055 | { | |
2e67a43b TM |
6056 | iterator_t iter = upper_bound(list_by_addr.begin(), list_by_addr.end(), addr, |
6057 | func_info::Compare()); | |
6058 | if (iter == list_by_addr.begin()) | |
5f0a03a6 | 6059 | return NULL; |
2e67a43b TM |
6060 | else |
6061 | return *(iter - 1); | |
5f0a03a6 JK |
6062 | } |
6063 | ||
6064 | func_info * | |
6065 | symbol_table::lookup_symbol(const string& name) | |
6066 | { | |
6067 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
6068 | if (i == map_by_name.end()) | |
6069 | return NULL; | |
6070 | return i->second; | |
6071 | } | |
6072 | ||
6073 | Dwarf_Addr | |
6074 | symbol_table::lookup_symbol_address(const string& name) | |
6075 | { | |
6076 | func_info *fi = lookup_symbol(name); | |
6077 | if (fi) | |
6078 | return fi->addr; | |
6079 | return 0; | |
6080 | } | |
6081 | ||
ab91b232 JK |
6082 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
6083 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
6084 | // For system calls not implemented elsewhere, this weak symbol shows up | |
6085 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
6086 | // we refuse to consider such symbols. Here we delete them from our | |
6087 | // symbol table. | |
6088 | // TODO: Consider generalizing this and/or making it part of blacklist | |
6089 | // processing. | |
6090 | void | |
6091 | symbol_table::purge_syscall_stubs() | |
6092 | { | |
6093 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
6094 | if (stub_addr == 0) | |
6095 | return; | |
2e67a43b TM |
6096 | range_t purge_range = equal_range(list_by_addr.begin(), list_by_addr.end(), |
6097 | stub_addr, func_info::Compare()); | |
6098 | for (iterator_t iter = purge_range.first; | |
6099 | iter != purge_range.second; | |
6100 | ++iter) | |
ab91b232 | 6101 | { |
2e67a43b TM |
6102 | func_info *fi = *iter; |
6103 | if (fi->weak && fi->name != "sys_ni_syscall") | |
ab91b232 | 6104 | { |
2e67a43b TM |
6105 | map_by_name.erase(fi->name); |
6106 | delete fi; | |
6107 | *iter = 0; | |
6108 | } | |
ab91b232 | 6109 | } |
2e67a43b TM |
6110 | // Range might have null pointer entries that should be erased. |
6111 | list_by_addr.erase(remove(purge_range.first, purge_range.second, | |
6112 | (func_info*)0), | |
6113 | purge_range.second); | |
6114 | } | |
6115 | ||
6116 | void | |
6117 | symbol_table::sort() | |
6118 | { | |
6119 | stable_sort(list_by_addr.begin(), list_by_addr.end(), func_info::Compare()); | |
ab91b232 JK |
6120 | } |
6121 | ||
5f0a03a6 JK |
6122 | void |
6123 | module_info::get_symtab(dwarf_query *q) | |
6124 | { | |
6125 | systemtap_session &sess = q->sess; | |
6126 | ||
6127 | sym_table = new symbol_table(this); | |
6128 | if (!elf_path.empty()) | |
6129 | { | |
6130 | if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()) | |
6131 | cerr << "Warning: reading symbol table from " | |
6132 | << elf_path | |
6133 | << " -- ignoring " | |
6134 | << sess.kernel_symtab_path | |
6135 | << endl ;; | |
6136 | symtab_status = sym_table->get_from_elf(); | |
6137 | } | |
6138 | else | |
6139 | { | |
6140 | assert(name == TOK_KERNEL); | |
6141 | if (sess.kernel_symtab_path.empty()) | |
6142 | { | |
6143 | symtab_status = info_absent; | |
6144 | cerr << "Error: Cannot find vmlinux." | |
6145 | << " Consider using --kmap instead of --kelf." | |
6146 | << endl;; | |
6147 | } | |
6148 | else | |
6149 | { | |
6150 | symtab_status = | |
6151 | sym_table->read_from_text_file(sess.kernel_symtab_path); | |
6152 | if (symtab_status == info_present) | |
6153 | { | |
6154 | sess.sym_kprobes_text_start = | |
6155 | sym_table->lookup_symbol_address("__kprobes_text_start"); | |
6156 | sess.sym_kprobes_text_end = | |
6157 | sym_table->lookup_symbol_address("__kprobes_text_end"); | |
6158 | sess.sym_stext = sym_table->lookup_symbol_address("_stext"); | |
5f0a03a6 JK |
6159 | } |
6160 | } | |
6161 | } | |
6162 | if (symtab_status == info_absent) | |
6163 | { | |
6164 | delete sym_table; | |
6165 | sym_table = NULL; | |
6166 | return; | |
6167 | } | |
6168 | ||
6169 | // If we have dwarf for the same module, mark the redundant symtab | |
6170 | // entries. | |
6171 | // | |
6172 | // In dwarf_query::handle_query_module(), the call to query_module_dwarf() | |
6173 | // precedes the call to query_module_symtab(). So we should never read | |
6174 | // a module's symbol table without first having tried to get its dwarf. | |
6175 | sym_table->mark_dwarf_redundancies(&q->dw); | |
ab91b232 JK |
6176 | |
6177 | if (name == TOK_KERNEL) | |
6178 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
6179 | } |
6180 | ||
6181 | module_info::~module_info() | |
6182 | { | |
6183 | if (sym_table) | |
6184 | delete sym_table; | |
b55bc428 FCE |
6185 | } |
6186 | ||
6187 | ||
98afd80e | 6188 | |
935447c8 DS |
6189 | // ------------------------------------------------------------------------ |
6190 | // task_finder derived 'probes': These don't really exist. The whole | |
6191 | // purpose of the task_finder_derived_probe_group is to make sure that | |
6192 | // stap_start_task_finder()/stap_stop_task_finder() get called only | |
6193 | // once and in the right place. | |
6194 | // ------------------------------------------------------------------------ | |
6195 | ||
6196 | struct task_finder_derived_probe: public derived_probe | |
6197 | { | |
c295b10e FCE |
6198 | // Dummy constructor for gcc 3.4 compatibility |
6199 | task_finder_derived_probe (): derived_probe (0) { assert(0); } | |
935447c8 DS |
6200 | }; |
6201 | ||
6202 | ||
6203 | struct task_finder_derived_probe_group: public generic_dpg<task_finder_derived_probe> | |
6204 | { | |
6205 | public: | |
6206 | static void create_session_group (systemtap_session& s); | |
6207 | ||
6208 | void emit_module_decls (systemtap_session& ) { } | |
6209 | void emit_module_init (systemtap_session& s); | |
6210 | void emit_module_exit (systemtap_session& s); | |
6211 | }; | |
6212 | ||
6213 | ||
6214 | void | |
6215 | task_finder_derived_probe_group::create_session_group (systemtap_session& s) | |
6216 | { | |
6217 | if (! s.task_finder_derived_probes) | |
6d0f3f0c | 6218 | s.task_finder_derived_probes = new task_finder_derived_probe_group(); |
935447c8 DS |
6219 | } |
6220 | ||
6221 | ||
6222 | void | |
6223 | task_finder_derived_probe_group::emit_module_init (systemtap_session& s) | |
6224 | { | |
6225 | s.op->newline(); | |
6226 | s.op->newline() << "/* ---- task finder ---- */"; | |
6227 | s.op->newline() << "rc = stap_start_task_finder();"; | |
6228 | ||
6229 | s.op->newline() << "if (rc) {"; | |
6d0f3f0c | 6230 | s.op->newline(1) << "stap_stop_task_finder();"; |
935447c8 DS |
6231 | s.op->newline(-1) << "}"; |
6232 | } | |
6233 | ||
6234 | ||
6235 | void | |
6236 | task_finder_derived_probe_group::emit_module_exit (systemtap_session& s) | |
6237 | { | |
6238 | s.op->newline(); | |
6239 | s.op->newline() << "/* ---- task finder ---- */"; | |
6240 | s.op->newline() << "stap_stop_task_finder();"; | |
6241 | } | |
6242 | ||
a96d1db0 DN |
6243 | // ------------------------------------------------------------------------ |
6244 | // itrace user-space probes | |
6245 | // ------------------------------------------------------------------------ | |
6246 | ||
6247 | ||
0afb7073 FCE |
6248 | static string TOK_INSN("insn"); |
6249 | static string TOK_BLOCK("block"); | |
6250 | ||
a96d1db0 DN |
6251 | struct itrace_derived_probe: public derived_probe |
6252 | { | |
6253 | bool has_path; | |
6254 | string path; | |
6255 | int64_t pid; | |
6256 | int single_step; | |
6257 | ||
6258 | itrace_derived_probe (systemtap_session &s, probe* p, probe_point* l, | |
6259 | bool hp, string &pn, int64_t pd, int ss | |
6260 | ); | |
6261 | void join_group (systemtap_session& s); | |
6262 | }; | |
6263 | ||
6264 | ||
6265 | struct itrace_derived_probe_group: public generic_dpg<itrace_derived_probe> | |
6266 | { | |
6267 | private: | |
6268 | map<string, vector<itrace_derived_probe*> > probes_by_path; | |
6269 | typedef map<string, vector<itrace_derived_probe*> >::iterator p_b_path_iterator; | |
6270 | map<int64_t, vector<itrace_derived_probe*> > probes_by_pid; | |
6271 | typedef map<int64_t, vector<itrace_derived_probe*> >::iterator p_b_pid_iterator; | |
6272 | unsigned num_probes; | |
6273 | ||
6274 | void emit_probe_decl (systemtap_session& s, itrace_derived_probe *p); | |
6275 | ||
6276 | public: | |
6277 | itrace_derived_probe_group(): num_probes(0) { } | |
6278 | ||
6279 | void enroll (itrace_derived_probe* probe); | |
6280 | void emit_module_decls (systemtap_session& s); | |
6281 | void emit_module_init (systemtap_session& s); | |
6282 | void emit_module_exit (systemtap_session& s); | |
6283 | }; | |
6284 | ||
6285 | ||
6286 | itrace_derived_probe::itrace_derived_probe (systemtap_session &s, | |
6287 | probe* p, probe_point* l, | |
6288 | bool hp, string &pn, int64_t pd, | |
6289 | int ss | |
6290 | ): | |
6291 | derived_probe(p, l), has_path(hp), path(pn), pid(pd), single_step(ss) | |
6292 | { | |
6293 | } | |
6294 | ||
6295 | ||
6296 | void | |
6297 | itrace_derived_probe::join_group (systemtap_session& s) | |
6298 | { | |
6299 | if (! s.itrace_derived_probes) | |
6300 | s.itrace_derived_probes = new itrace_derived_probe_group (); | |
6301 | ||
6302 | s.itrace_derived_probes->enroll (this); | |
6303 | ||
6304 | task_finder_derived_probe_group::create_session_group (s); | |
6305 | } | |
6306 | ||
6307 | struct itrace_builder: public derived_probe_builder | |
6308 | { | |
6309 | itrace_builder() {} | |
6310 | virtual void build(systemtap_session & sess, | |
6311 | probe * base, | |
6312 | probe_point * location, | |
6313 | std::map<std::string, literal *> const & parameters, | |
6314 | vector<derived_probe *> & finished_results) | |
6315 | { | |
6316 | string path; | |
28d29bd3 | 6317 | int64_t pid = 0; |
a96d1db0 DN |
6318 | int single_step; |
6319 | ||
6320 | bool has_path = get_param (parameters, TOK_PROCESS, path); | |
6321 | bool has_pid = get_param (parameters, TOK_PROCESS, pid); | |
28d29bd3 | 6322 | // XXX: PR 6445 needs !has_path && !has_pid support |
a96d1db0 DN |
6323 | assert (has_path || has_pid); |
6324 | ||
0afb7073 | 6325 | single_step = ! has_null_param (parameters, TOK_BLOCK); |
a96d1db0 DN |
6326 | |
6327 | // If we have a path, we need to validate it. | |
6328 | if (has_path) | |
d0a7f5a9 | 6329 | path = find_executable (path); |
a96d1db0 DN |
6330 | |
6331 | finished_results.push_back(new itrace_derived_probe(sess, base, location, | |
6332 | has_path, path, pid, | |
d0a7f5a9 | 6333 | single_step |
a96d1db0 DN |
6334 | )); |
6335 | } | |
6336 | }; | |
6337 | ||
6338 | ||
6339 | void | |
6340 | itrace_derived_probe_group::enroll (itrace_derived_probe* p) | |
6341 | { | |
6342 | if (p->has_path) | |
6343 | probes_by_path[p->path].push_back(p); | |
6344 | else | |
6345 | probes_by_pid[p->pid].push_back(p); | |
6346 | num_probes++; | |
6347 | ||
6348 | // XXX: multiple exec probes (for instance) for the same path (or | |
6349 | // pid) should all share a itrace report function, and have their | |
6350 | // handlers executed sequentially. | |
6351 | } | |
6352 | ||
6353 | ||
6354 | void | |
6355 | itrace_derived_probe_group::emit_probe_decl (systemtap_session& s, | |
6356 | itrace_derived_probe *p) | |
6357 | { | |
6358 | s.op->newline() << "{"; | |
6359 | s.op->line() << " .tgt={"; | |
6360 | ||
6361 | if (p->has_path) | |
6362 | { | |
6363 | s.op->line() << " .pathname=\"" << p->path << "\","; | |
6364 | s.op->line() << " .pid=0,"; | |
6365 | } | |
6366 | else | |
6367 | { | |
6368 | s.op->line() << " .pathname=NULL,"; | |
6369 | s.op->line() << " .pid=" << p->pid << ","; | |
6370 | } | |
6371 | ||
6372 | s.op->line() << " .callback=&_stp_itrace_probe_cb,"; | |
6373 | s.op->line() << " },"; | |
6374 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
6375 | s.op->line() << " .single_step=" << p->single_step << ","; | |
6376 | s.op->line() << " .ph=&" << p->name << ","; | |
6377 | ||
6378 | s.op->line() << " },"; | |
6379 | } | |
6380 | ||
6381 | ||
6382 | void | |
6383 | itrace_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6384 | { | |
6385 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
6386 | return; | |
6387 | ||
6388 | s.op->newline(); | |
6389 | s.op->newline() << "/* ---- itrace probes ---- */"; | |
1324cc82 | 6390 | s.op->newline() << "#include \"task_finder.c\""; |
a96d1db0 DN |
6391 | s.op->newline() << "struct stap_itrace_probe {"; |
6392 | s.op->indent(1); | |
6393 | s.op->newline() << "struct stap_task_finder_target tgt;"; | |
6394 | s.op->newline() << "const char *pp;"; | |
6395 | s.op->newline() << "void (*ph) (struct context*);"; | |
6396 | s.op->newline() << "int single_step;"; | |
6397 | s.op->newline(-1) << "};"; | |
6398 | s.op->newline() << "static void enter_itrace_probe(struct stap_itrace_probe *p, struct pt_regs *regs, void *data);"; | |
6399 | s.op->newline() << "#include \"itrace.c\""; | |
6400 | ||
6401 | // output routine to call itrace probe | |
6402 | s.op->newline() << "static void enter_itrace_probe(struct stap_itrace_probe *p, struct pt_regs *regs, void *data) {"; | |
6403 | s.op->indent(1); | |
6404 | ||
c12d974f | 6405 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
a96d1db0 DN |
6406 | s.op->newline() << "c->regs = regs;"; |
6407 | s.op->newline() << "c->data = data;"; | |
6408 | ||
6409 | // call probe function | |
6410 | s.op->newline() << "(*p->ph) (c);"; | |
6411 | common_probe_entryfn_epilogue (s.op); | |
6412 | ||
6413 | s.op->newline() << "return;"; | |
6414 | s.op->newline(-1) << "}"; | |
6415 | ||
6416 | // Output task finder callback routine that gets called for all | |
6417 | // itrace probe types. | |
46b3c6cd | 6418 | 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 |
6419 | s.op->indent(1); |
6420 | s.op->newline() << "int rc = 0;"; | |
6421 | s.op->newline() << "struct stap_itrace_probe *p = container_of(tgt, struct stap_itrace_probe, tgt);"; | |
6422 | ||
6423 | s.op->newline() << "if (register_p) "; | |
6424 | s.op->indent(1); | |
6425 | ||
6426 | s.op->newline() << "rc = usr_itrace_init(p->single_step, tsk->pid, p);"; | |
6427 | s.op->newline(-1) << "else"; | |
6428 | s.op->newline(1) << "remove_usr_itrace_info(find_itrace_info(p->tgt.pid));"; | |
6429 | s.op->newline(-1) << "return rc;"; | |
6430 | s.op->newline(-1) << "}"; | |
6431 | ||
4c2732a1 | 6432 | s.op->newline() << "static struct stap_itrace_probe stap_itrace_probes[] = {"; |
a96d1db0 DN |
6433 | s.op->indent(1); |
6434 | ||
6435 | // Set up 'process(PATH)' probes | |
6436 | if (! probes_by_path.empty()) | |
6437 | { | |
6438 | for (p_b_path_iterator it = probes_by_path.begin(); | |
6439 | it != probes_by_path.end(); it++) | |
6440 | { | |
6441 | for (unsigned i = 0; i < it->second.size(); i++) | |
6442 | { | |
6443 | itrace_derived_probe *p = it->second[i]; | |
6444 | emit_probe_decl(s, p); | |
6445 | } | |
6446 | } | |
6447 | } | |
6448 | ||
6449 | // Set up 'process(PID)' probes | |
6450 | if (! probes_by_pid.empty()) | |
6451 | { | |
6452 | for (p_b_pid_iterator it = probes_by_pid.begin(); | |
6453 | it != probes_by_pid.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 | s.op->newline(-1) << "};"; | |
6463 | } | |
6464 | ||
6465 | ||
6466 | void | |
6467 | itrace_derived_probe_group::emit_module_init (systemtap_session& s) | |
6468 | { | |
6469 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
6470 | return; | |
6471 | ||
6472 | s.op->newline(); | |
6473 | s.op->newline() << "/* ---- itrace probes ---- */"; | |
6474 | ||
6475 | s.op->newline() << "for (i=0; i<" << num_probes << "; i++) {"; | |
6476 | s.op->indent(1); | |
6477 | s.op->newline() << "struct stap_itrace_probe *p = &stap_itrace_probes[i];"; | |
6478 | s.op->newline() << "rc = stap_register_task_finder_target(&p->tgt);"; | |
6479 | s.op->newline(-1) << "}"; | |
6480 | } | |
6481 | ||
6482 | ||
6483 | void | |
6484 | itrace_derived_probe_group::emit_module_exit (systemtap_session& s) | |
6485 | { | |
6486 | if (probes_by_path.empty() && probes_by_pid.empty()) return; | |
6487 | s.op->newline(); | |
6488 | s.op->newline() << "/* ---- itrace probes ---- */"; | |
6489 | s.op->newline() << "cleanup_usr_itrace();"; | |
6490 | } | |
935447c8 DS |
6491 | |
6492 | // ------------------------------------------------------------------------ | |
6493 | // utrace user-space probes | |
6494 | // ------------------------------------------------------------------------ | |
6495 | ||
eff6ac72 | 6496 | static string TOK_THREAD("thread"); |
12b21830 DS |
6497 | static string TOK_SYSCALL("syscall"); |
6498 | ||
eff6ac72 DS |
6499 | // Note that these flags don't match up exactly with UTRACE_EVENT |
6500 | // flags (and that's OK). | |
935447c8 DS |
6501 | enum utrace_derived_probe_flags { |
6502 | UDPF_NONE, | |
eff6ac72 DS |
6503 | UDPF_BEGIN, // process begin |
6504 | UDPF_END, // process end | |
6505 | UDPF_THREAD_BEGIN, // thread begin | |
6506 | UDPF_THREAD_END, // thread end | |
6507 | UDPF_SYSCALL, // syscall entry | |
6508 | UDPF_SYSCALL_RETURN, // syscall exit | |
935447c8 DS |
6509 | UDPF_NFLAGS |
6510 | }; | |
6511 | ||
6512 | struct utrace_derived_probe: public derived_probe | |
6513 | { | |
6514 | bool has_path; | |
6515 | string path; | |
6516 | int64_t pid; | |
6517 | enum utrace_derived_probe_flags flags; | |
6518 | bool target_symbol_seen; | |
6519 | ||
6520 | utrace_derived_probe (systemtap_session &s, probe* p, probe_point* l, | |
6521 | bool hp, string &pn, int64_t pd, | |
6522 | enum utrace_derived_probe_flags f); | |
6523 | void join_group (systemtap_session& s); | |
6524 | }; | |
6525 | ||
6526 | ||
6527 | struct utrace_derived_probe_group: public generic_dpg<utrace_derived_probe> | |
6528 | { | |
6529 | private: | |
6530 | map<string, vector<utrace_derived_probe*> > probes_by_path; | |
6531 | typedef map<string, vector<utrace_derived_probe*> >::iterator p_b_path_iterator; | |
6532 | map<int64_t, vector<utrace_derived_probe*> > probes_by_pid; | |
6533 | typedef map<int64_t, vector<utrace_derived_probe*> >::iterator p_b_pid_iterator; | |
6534 | unsigned num_probes; | |
6535 | bool flags_seen[UDPF_NFLAGS]; | |
6536 | ||
6537 | void emit_probe_decl (systemtap_session& s, utrace_derived_probe *p); | |
a21d81ec DS |
6538 | void emit_vm_callback_probe_decl (systemtap_session& s, bool has_path, |
6539 | string path, int64_t pid, | |
6540 | string vm_callback); | |
935447c8 DS |
6541 | |
6542 | public: | |
6543 | utrace_derived_probe_group(): num_probes(0), flags_seen() { } | |
6544 | ||
6545 | void enroll (utrace_derived_probe* probe); | |
6546 | void emit_module_decls (systemtap_session& s); | |
6547 | void emit_module_init (systemtap_session& s); | |
6548 | void emit_module_exit (systemtap_session& s); | |
6549 | }; | |
6550 | ||
6551 | ||
de688825 | 6552 | struct utrace_var_expanding_visitor: public var_expanding_visitor |
935447c8 | 6553 | { |
de688825 JS |
6554 | utrace_var_expanding_visitor(systemtap_session& s, const string& pn, |
6555 | enum utrace_derived_probe_flags f): | |
935447c8 DS |
6556 | sess (s), probe_name (pn), flags (f), target_symbol_seen (false) {} |
6557 | ||
6558 | systemtap_session& sess; | |
6559 | string probe_name; | |
6560 | enum utrace_derived_probe_flags flags; | |
6561 | bool target_symbol_seen; | |
6562 | ||
6270adc1 | 6563 | void visit_target_symbol_arg (target_symbol* e); |
5d67b47c | 6564 | void visit_target_symbol_context (target_symbol* e); |
935447c8 DS |
6565 | void visit_target_symbol (target_symbol* e); |
6566 | }; | |
6567 | ||
6568 | ||
a7a68293 | 6569 | |
935447c8 DS |
6570 | utrace_derived_probe::utrace_derived_probe (systemtap_session &s, |
6571 | probe* p, probe_point* l, | |
6572 | bool hp, string &pn, int64_t pd, | |
6573 | enum utrace_derived_probe_flags f): | |
d0a7f5a9 FCE |
6574 | derived_probe (p, new probe_point (*l) /* .components soon rewritten */ ), |
6575 | has_path(hp), path(pn), pid(pd), flags(f), | |
935447c8 DS |
6576 | target_symbol_seen(false) |
6577 | { | |
de688825 JS |
6578 | // Expand local variables in the probe body |
6579 | utrace_var_expanding_visitor v (s, name, flags); | |
6580 | this->body = v.require (this->body); | |
935447c8 | 6581 | target_symbol_seen = v.target_symbol_seen; |
d0a7f5a9 FCE |
6582 | |
6583 | // Reset the sole element of the "locations" vector as a | |
6584 | // "reverse-engineered" form of the incoming (q.base_loc) probe | |
6585 | // point. This allows a user to see what program etc. | |
6586 | // number any particular match of the wildcards. | |
6587 | ||
6588 | vector<probe_point::component*> comps; | |
6589 | if (hp) | |
6590 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(path))); | |
e56e51c9 | 6591 | else if (pid != 0) |
06aca46a | 6592 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_number(pid))); |
e56e51c9 FCE |
6593 | else |
6594 | comps.push_back (new probe_point::component(TOK_PROCESS)); | |
6595 | ||
d0a7f5a9 FCE |
6596 | switch (flags) |
6597 | { | |
6598 | case UDPF_THREAD_BEGIN: | |
6599 | comps.push_back (new probe_point::component(TOK_THREAD)); | |
6600 | comps.push_back (new probe_point::component(TOK_BEGIN)); | |
6601 | break; | |
6602 | case UDPF_THREAD_END: | |
6603 | comps.push_back (new probe_point::component(TOK_THREAD)); | |
6604 | comps.push_back (new probe_point::component(TOK_END)); | |
6605 | break; | |
6606 | case UDPF_SYSCALL: | |
6607 | comps.push_back (new probe_point::component(TOK_SYSCALL)); | |
6608 | break; | |
6609 | case UDPF_SYSCALL_RETURN: | |
6610 | comps.push_back (new probe_point::component(TOK_SYSCALL)); | |
6611 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
6612 | break; | |
6613 | case UDPF_BEGIN: | |
6614 | comps.push_back (new probe_point::component(TOK_BEGIN)); | |
6615 | break; | |
6616 | case UDPF_END: | |
6617 | comps.push_back (new probe_point::component(TOK_END)); | |
6618 | break; | |
06aca46a | 6619 | default: |
d0a7f5a9 FCE |
6620 | assert (0); |
6621 | } | |
6622 | ||
6623 | // Overwrite it. | |
6624 | this->sole_location()->components = comps; | |
935447c8 DS |
6625 | } |
6626 | ||
6627 | ||
6628 | void | |
6629 | utrace_derived_probe::join_group (systemtap_session& s) | |
6630 | { | |
6631 | if (! s.utrace_derived_probes) | |
14cdaa0b | 6632 | { |
935447c8 | 6633 | s.utrace_derived_probes = new utrace_derived_probe_group (); |
14cdaa0b | 6634 | } |
935447c8 DS |
6635 | s.utrace_derived_probes->enroll (this); |
6636 | ||
6637 | task_finder_derived_probe_group::create_session_group (s); | |
6638 | } | |
6639 | ||
6640 | ||
6641 | void | |
de688825 | 6642 | utrace_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) |
935447c8 | 6643 | { |
6270adc1 | 6644 | string argnum_s = e->base_name.substr(4,e->base_name.length()-4); |
3a4e19b8 | 6645 | int argnum = lex_cast<int>(argnum_s); |
935447c8 | 6646 | |
6270adc1 MH |
6647 | if (flags != UDPF_SYSCALL) |
6648 | throw semantic_error ("only \"process(PATH_OR_PID).syscall\" support $argN.", e->tok); | |
935447c8 | 6649 | |
6270adc1 MH |
6650 | if (e->components.size() > 0) |
6651 | { | |
6652 | switch (e->components[0].first) | |
6653 | { | |
6654 | case target_symbol::comp_literal_array_index: | |
6655 | throw semantic_error("utrace target variable '$argN' may not be used as array", | |
6656 | e->tok); | |
6657 | break; | |
6658 | case target_symbol::comp_struct_member: | |
6659 | throw semantic_error("utrace target variable '$argN' may not be used as a structure", | |
6660 | e->tok); | |
6661 | break; | |
6662 | default: | |
6663 | throw semantic_error ("invalid use of utrace target variable '$argN'", | |
6664 | e->tok); | |
6665 | break; | |
6666 | } | |
6667 | } | |
6668 | ||
6669 | // FIXME: max argnument number should not be hardcoded. | |
6670 | if (argnum < 1 || argnum > 6) | |
6671 | throw semantic_error ("invalid syscall argument number (1-6)", e->tok); | |
6672 | ||
6673 | bool lvalue = is_active_lvalue(e); | |
6674 | if (lvalue) | |
6675 | throw semantic_error("utrace '$argN' variable is read-only", e->tok); | |
935447c8 | 6676 | |
6270adc1 MH |
6677 | // Remember that we've seen a target variable. |
6678 | target_symbol_seen = true; | |
6679 | ||
6680 | // We're going to substitute a synthesized '_utrace_syscall_arg' | |
6681 | // function call for the '$argN' reference. | |
6682 | functioncall* n = new functioncall; | |
6683 | n->tok = e->tok; | |
6684 | n->function = "_utrace_syscall_arg"; | |
6685 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
6686 | ||
6687 | literal_number *num = new literal_number(argnum - 1); | |
6688 | num->tok = e->tok; | |
6689 | n->args.push_back(num); | |
6690 | ||
4ed05b15 | 6691 | provide (n); |
6270adc1 MH |
6692 | } |
6693 | ||
6694 | void | |
de688825 | 6695 | utrace_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
6270adc1 | 6696 | { |
5d67b47c MH |
6697 | string sname = e->base_name; |
6698 | ||
935447c8 DS |
6699 | if (e->components.size() > 0) |
6700 | { | |
6701 | switch (e->components[0].first) | |
6702 | { | |
6703 | case target_symbol::comp_literal_array_index: | |
5d67b47c | 6704 | throw semantic_error("utrace target variable '" + sname + "' may not be used as array", |
935447c8 DS |
6705 | e->tok); |
6706 | break; | |
6707 | case target_symbol::comp_struct_member: | |
5d67b47c | 6708 | throw semantic_error("utrace target variable '" + sname + "' may not be used as a structure", |
935447c8 DS |
6709 | e->tok); |
6710 | break; | |
6711 | default: | |
5d67b47c | 6712 | throw semantic_error ("invalid use of utrace target variable '" + sname + "'", |
935447c8 DS |
6713 | e->tok); |
6714 | break; | |
6715 | } | |
6716 | } | |
6717 | ||
6718 | bool lvalue = is_active_lvalue(e); | |
6719 | if (lvalue) | |
5d67b47c MH |
6720 | throw semantic_error("utrace '" + sname + "' variable is read-only", e->tok); |
6721 | ||
6722 | string fname; | |
6723 | if (sname == "$return") | |
6724 | { | |
6725 | if (flags != UDPF_SYSCALL_RETURN) | |
6726 | throw semantic_error ("only \"process(PATH_OR_PID).syscall.return\" support $return.", e->tok); | |
6727 | fname = "_utrace_syscall_return"; | |
6728 | } | |
6729 | else | |
6730 | fname = "_utrace_syscall_nr"; | |
935447c8 DS |
6731 | |
6732 | // Remember that we've seen a target variable. | |
6733 | target_symbol_seen = true; | |
6734 | ||
6270adc1 | 6735 | // We're going to substitute a synthesized '_utrace_syscall_nr' |
a7a68293 | 6736 | // function call for the '$syscall' reference. |
935447c8 DS |
6737 | functioncall* n = new functioncall; |
6738 | n->tok = e->tok; | |
5d67b47c | 6739 | n->function = fname; |
935447c8 DS |
6740 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session |
6741 | ||
4ed05b15 | 6742 | provide (n); |
935447c8 DS |
6743 | } |
6744 | ||
6270adc1 | 6745 | void |
de688825 | 6746 | utrace_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
6270adc1 MH |
6747 | { |
6748 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
6749 | ||
6750 | if (flags != UDPF_SYSCALL && flags != UDPF_SYSCALL_RETURN) | |
6751 | throw semantic_error ("only \"process(PATH_OR_PID).syscall\" and \"process(PATH_OR_PID).syscall.return\" probes support target symbols", | |
6752 | e->tok); | |
6753 | ||
6754 | if (e->base_name.substr(0,4) == "$arg") | |
6755 | visit_target_symbol_arg(e); | |
5d67b47c MH |
6756 | else if (e->base_name == "$syscall" || e->base_name == "$return") |
6757 | visit_target_symbol_context(e); | |
6270adc1 | 6758 | else |
5d67b47c | 6759 | throw semantic_error ("invalid target symbol for utrace probe, $syscall, $return or $argN expected", |
6270adc1 MH |
6760 | e->tok); |
6761 | } | |
6762 | ||
935447c8 DS |
6763 | |
6764 | struct utrace_builder: public derived_probe_builder | |
6765 | { | |
6766 | utrace_builder() {} | |
6767 | virtual void build(systemtap_session & sess, | |
6768 | probe * base, | |
6769 | probe_point * location, | |
86bf665e | 6770 | literal_map_t const & parameters, |
935447c8 DS |
6771 | vector<derived_probe *> & finished_results) |
6772 | { | |
6773 | string path; | |
6774 | int64_t pid; | |
6775 | ||
6776 | bool has_path = get_param (parameters, TOK_PROCESS, path); | |
6777 | bool has_pid = get_param (parameters, TOK_PROCESS, pid); | |
6778 | enum utrace_derived_probe_flags flags = UDPF_NONE; | |
d0a7f5a9 | 6779 | |
eff6ac72 DS |
6780 | if (has_null_param (parameters, TOK_THREAD)) |
6781 | { | |
6782 | if (has_null_param (parameters, TOK_BEGIN)) | |
6783 | flags = UDPF_THREAD_BEGIN; | |
6784 | else if (has_null_param (parameters, TOK_END)) | |
6785 | flags = UDPF_THREAD_END; | |
6786 | } | |
12b21830 | 6787 | else if (has_null_param (parameters, TOK_SYSCALL)) |
935447c8 DS |
6788 | { |
6789 | if (has_null_param (parameters, TOK_RETURN)) | |
eff6ac72 | 6790 | flags = UDPF_SYSCALL_RETURN; |
935447c8 | 6791 | else |
eff6ac72 | 6792 | flags = UDPF_SYSCALL; |
935447c8 | 6793 | } |
eff6ac72 DS |
6794 | else if (has_null_param (parameters, TOK_BEGIN)) |
6795 | flags = UDPF_BEGIN; | |
6796 | else if (has_null_param (parameters, TOK_END)) | |
6797 | flags = UDPF_END; | |
935447c8 | 6798 | |
986e98de DS |
6799 | // If we didn't get a path or pid, this means to probe everything. |
6800 | // Convert this to a pid-based probe. | |
6801 | if (! has_path && ! has_pid) | |
6802 | { | |
6803 | has_path = false; | |
6804 | path.clear(); | |
6805 | has_pid = true; | |
6806 | pid = 0; | |
6807 | } | |
c569c2e4 FCE |
6808 | else if (has_path) |
6809 | { | |
6810 | path = find_executable (path); | |
6811 | sess.unwindsym_modules.insert (path); | |
6812 | } | |
986e98de | 6813 | else if (has_pid) |
6cdf2889 | 6814 | { |
c569c2e4 | 6815 | // We can't probe 'init' (pid 1). XXX: where does this limitation come from? |
6cdf2889 DS |
6816 | if (pid < 2) |
6817 | throw semantic_error ("process pid must be greater than 1", | |
6818 | location->tok); | |
28d29bd3 FCE |
6819 | |
6820 | // XXX: could we use /proc/$pid/exe in unwindsym_modules and elsewhere? | |
6cdf2889 | 6821 | } |
0744f531 | 6822 | |
935447c8 DS |
6823 | finished_results.push_back(new utrace_derived_probe(sess, base, location, |
6824 | has_path, path, pid, | |
6825 | flags)); | |
6826 | } | |
6827 | }; | |
6828 | ||
6829 | ||
6830 | void | |
6831 | utrace_derived_probe_group::enroll (utrace_derived_probe* p) | |
6832 | { | |
6833 | if (p->has_path) | |
6834 | probes_by_path[p->path].push_back(p); | |
6835 | else | |
6836 | probes_by_pid[p->pid].push_back(p); | |
6837 | num_probes++; | |
6838 | flags_seen[p->flags] = true; | |
6839 | ||
6840 | // XXX: multiple exec probes (for instance) for the same path (or | |
6841 | // pid) should all share a utrace report function, and have their | |
6842 | // handlers executed sequentially. | |
6843 | } | |
6844 | ||
6845 | ||
6846 | void | |
6847 | utrace_derived_probe_group::emit_probe_decl (systemtap_session& s, | |
6848 | utrace_derived_probe *p) | |
6849 | { | |
6850 | s.op->newline() << "{"; | |
6851 | s.op->line() << " .tgt={"; | |
6852 | ||
6853 | if (p->has_path) | |
6854 | { | |
6855 | s.op->line() << " .pathname=\"" << p->path << "\","; | |
6856 | s.op->line() << " .pid=0,"; | |
6857 | } | |
6858 | else | |
6859 | { | |
6860 | s.op->line() << " .pathname=NULL,"; | |
6861 | s.op->line() << " .pid=" << p->pid << ","; | |
6862 | } | |
6863 | ||
6864 | s.op->line() << " .callback=&_stp_utrace_probe_cb,"; | |
8253e3b8 | 6865 | s.op->line() << " .vm_callback=NULL,"; |
935447c8 DS |
6866 | s.op->line() << " },"; |
6867 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; | |
6868 | s.op->line() << " .ph=&" << p->name << ","; | |
6869 | ||
6870 | // Handle flags | |
6871 | switch (p->flags) | |
6872 | { | |
b20bac3a DS |
6873 | // Notice that we'll just call the probe directly when we get |
6874 | // notified, since the task_finder layer stops the thread for us. | |
eff6ac72 DS |
6875 | case UDPF_BEGIN: // process begin |
6876 | s.op->line() << " .flags=(UDPF_BEGIN),"; | |
935447c8 | 6877 | break; |
eff6ac72 DS |
6878 | case UDPF_THREAD_BEGIN: // thread begin |
6879 | s.op->line() << " .flags=(UDPF_THREAD_BEGIN),"; | |
159cb109 | 6880 | break; |
eff6ac72 DS |
6881 | |
6882 | // Notice we're not setting up a .ops/.report_death handler for | |
6883 | // either UDPF_END or UDPF_THREAD_END. Instead, we'll just call | |
6884 | // the probe directly when we get notified. | |
6885 | case UDPF_END: // process end | |
6886 | s.op->line() << " .flags=(UDPF_END),"; | |
935447c8 | 6887 | break; |
eff6ac72 DS |
6888 | case UDPF_THREAD_END: // thread end |
6889 | s.op->line() << " .flags=(UDPF_THREAD_END),"; | |
6890 | break; | |
6891 | ||
6892 | // For UDPF_SYSCALL/UDPF_SYSCALL_RETURN probes, the .report_death | |
6893 | // handler isn't strictly necessary. However, it helps to keep | |
b20bac3a DS |
6894 | // our attaches/detaches symmetrical. Since the task_finder layer |
6895 | // stops the thread, that works around bug 6841. | |
eff6ac72 DS |
6896 | case UDPF_SYSCALL: |
6897 | s.op->line() << " .flags=(UDPF_SYSCALL),"; | |
b20bac3a DS |
6898 | s.op->line() << " .ops={ .report_syscall_entry=stap_utrace_probe_syscall, .report_death=stap_utrace_task_finder_report_death },"; |
6899 | s.op->line() << " .events=(UTRACE_EVENT(SYSCALL_ENTRY)|UTRACE_EVENT(DEATH)),"; | |
935447c8 | 6900 | break; |
eff6ac72 DS |
6901 | case UDPF_SYSCALL_RETURN: |
6902 | s.op->line() << " .flags=(UDPF_SYSCALL_RETURN),"; | |
b20bac3a DS |
6903 | s.op->line() << " .ops={ .report_syscall_exit=stap_utrace_probe_syscall, .report_death=stap_utrace_task_finder_report_death },"; |
6904 | s.op->line() << " .events=(UTRACE_EVENT(SYSCALL_EXIT)|UTRACE_EVENT(DEATH)),"; | |
935447c8 | 6905 | break; |
930a1798 | 6906 | |
a21d81ec DS |
6907 | case UDPF_NONE: |
6908 | s.op->line() << " .flags=(UDPF_NONE),"; | |
6909 | s.op->line() << " .ops={ },"; | |
6910 | s.op->line() << " .events=0,"; | |
6911 | break; | |
935447c8 DS |
6912 | default: |
6913 | throw semantic_error ("bad utrace probe flag"); | |
6914 | break; | |
6915 | } | |
6916 | s.op->line() << " .engine_attached=0,"; | |
6917 | s.op->line() << " },"; | |
6918 | } | |
6919 | ||
6920 | ||
a21d81ec DS |
6921 | void |
6922 | utrace_derived_probe_group::emit_vm_callback_probe_decl (systemtap_session& s, | |
6923 | bool has_path, | |
6924 | string path, | |
6925 | int64_t pid, | |
6926 | string vm_callback) | |
6927 | { | |
6928 | s.op->newline() << "{"; | |
6929 | s.op->line() << " .tgt={"; | |
6930 | ||
6931 | if (has_path) | |
6932 | { | |
6933 | s.op->line() << " .pathname=\"" << path << "\","; | |
6934 | s.op->line() << " .pid=0,"; | |
6935 | } | |
6936 | else | |
6937 | { | |
6938 | s.op->line() << " .pathname=NULL,"; | |
6939 | s.op->line() << " .pid=" << pid << ","; | |
6940 | } | |
6941 | ||
6942 | s.op->line() << " .callback=NULL,"; | |
6943 | s.op->line() << " .vm_callback=&" << vm_callback << ","; | |
6944 | s.op->line() << " },"; | |
6945 | s.op->line() << " .pp=\"internal\","; | |
6946 | s.op->line() << " .ph=NULL,"; | |
6947 | s.op->line() << " .flags=(UDPF_NONE),"; | |
6948 | s.op->line() << " .ops={ NULL },"; | |
6949 | s.op->line() << " .events=0,"; | |
6950 | s.op->line() << " .engine_attached=0,"; | |
6951 | s.op->line() << " },"; | |
6952 | } | |
6953 | ||
6954 | ||
935447c8 DS |
6955 | void |
6956 | utrace_derived_probe_group::emit_module_decls (systemtap_session& s) | |
6957 | { | |
6958 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
6959 | return; | |
6960 | ||
6961 | s.op->newline(); | |
6962 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
104c6237 FCE |
6963 | s.op->newline() << "#include \"task_finder.c\""; |
6964 | ||
eff6ac72 DS |
6965 | s.op->newline() << "enum utrace_derived_probe_flags {"; |
6966 | s.op->indent(1); | |
6967 | s.op->newline() << "UDPF_NONE,"; | |
6968 | s.op->newline() << "UDPF_BEGIN,"; | |
6969 | s.op->newline() << "UDPF_END,"; | |
6970 | s.op->newline() << "UDPF_THREAD_BEGIN,"; | |
6971 | s.op->newline() << "UDPF_THREAD_END,"; | |
6972 | s.op->newline() << "UDPF_SYSCALL,"; | |
6973 | s.op->newline() << "UDPF_SYSCALL_RETURN,"; | |
6974 | s.op->newline() << "UDPF_NFLAGS"; | |
6975 | s.op->newline(-1) << "};"; | |
6976 | ||
935447c8 DS |
6977 | s.op->newline() << "struct stap_utrace_probe {"; |
6978 | s.op->indent(1); | |
6979 | s.op->newline() << "struct stap_task_finder_target tgt;"; | |
6980 | s.op->newline() << "const char *pp;"; | |
6981 | s.op->newline() << "void (*ph) (struct context*);"; | |
eff6ac72 | 6982 | s.op->newline() << "enum utrace_derived_probe_flags flags;"; |
935447c8 | 6983 | s.op->newline() << "struct utrace_engine_ops ops;"; |
eff6ac72 | 6984 | s.op->newline() << "unsigned long events;"; |
935447c8 DS |
6985 | s.op->newline() << "int engine_attached;"; |
6986 | s.op->newline(-1) << "};"; | |
6987 | ||
eff6ac72 | 6988 | |
b20bac3a DS |
6989 | // Output handler function for UDPF_BEGIN, UDPF_THREAD_BEGIN, |
6990 | // UDPF_END, and UDPF_THREAD_END | |
6991 | if (flags_seen[UDPF_BEGIN] || flags_seen[UDPF_THREAD_BEGIN] | |
6992 | || flags_seen[UDPF_END] || flags_seen[UDPF_THREAD_END]) | |
159cb109 | 6993 | { |
935447c8 DS |
6994 | s.op->newline() << "static void stap_utrace_probe_handler(struct task_struct *tsk, struct stap_utrace_probe *p) {"; |
6995 | s.op->indent(1); | |
6996 | ||
c12d974f | 6997 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
935447c8 DS |
6998 | |
6999 | // call probe function | |
7000 | s.op->newline() << "(*p->ph) (c);"; | |
7001 | common_probe_entryfn_epilogue (s.op); | |
7002 | ||
7003 | s.op->newline() << "return;"; | |
7004 | s.op->newline(-1) << "}"; | |
159cb109 | 7005 | } |
935447c8 DS |
7006 | |
7007 | // Output handler function for SYSCALL_ENTRY and SYSCALL_EXIT events | |
eff6ac72 | 7008 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
935447c8 | 7009 | { |
4550733e | 7010 | s.op->newline() << "#ifdef UTRACE_ORIG_VERSION"; |
935447c8 | 7011 | s.op->newline() << "static u32 stap_utrace_probe_syscall(struct utrace_attached_engine *engine, struct task_struct *tsk, struct pt_regs *regs) {"; |
4550733e DS |
7012 | s.op->newline() << "#else"; |
7013 | 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) {"; | |
7014 | s.op->newline() << "#endif"; | |
7015 | ||
935447c8 DS |
7016 | s.op->indent(1); |
7017 | s.op->newline() << "struct stap_utrace_probe *p = (struct stap_utrace_probe *)engine->data;"; | |
7018 | ||
c12d974f | 7019 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "p->pp"); |
935447c8 DS |
7020 | s.op->newline() << "c->regs = regs;"; |
7021 | ||
7022 | // call probe function | |
7023 | s.op->newline() << "(*p->ph) (c);"; | |
7024 | common_probe_entryfn_epilogue (s.op); | |
7025 | ||
b20bac3a DS |
7026 | s.op->newline() << "if ((atomic_read (&session_state) != STAP_SESSION_STARTING) && (atomic_read (&session_state) != STAP_SESSION_RUNNING)) {"; |
7027 | s.op->indent(1); | |
7028 | s.op->newline() << "debug_task_finder_detach();"; | |
7029 | s.op->newline() << "return UTRACE_DETACH;"; | |
7030 | s.op->newline(-1) << "}"; | |
4550733e | 7031 | s.op->newline() << "return UTRACE_RESUME;"; |
935447c8 DS |
7032 | s.op->newline(-1) << "}"; |
7033 | } | |
7034 | ||
eff6ac72 | 7035 | // Output task_finder callback routine that gets called for all |
935447c8 | 7036 | // utrace probe types. |
46b3c6cd | 7037 | 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 |
7038 | s.op->indent(1); |
7039 | s.op->newline() << "int rc = 0;"; | |
7040 | s.op->newline() << "struct stap_utrace_probe *p = container_of(tgt, struct stap_utrace_probe, tgt);"; | |
7041 | s.op->newline() << "struct utrace_attached_engine *engine;"; | |
7042 | ||
935447c8 DS |
7043 | s.op->newline() << "if (register_p) {"; |
7044 | s.op->indent(1); | |
7045 | ||
0744f531 | 7046 | s.op->newline() << "switch (p->flags) {"; |
935447c8 | 7047 | s.op->indent(1); |
eff6ac72 DS |
7048 | |
7049 | // When receiving a UTRACE_EVENT(CLONE) event, we can't call the | |
7050 | // begin/thread.begin probe directly. So, we'll just attach an | |
7051 | // engine that waits for the thread to quiesce. When the thread | |
7052 | // quiesces, then call the probe. | |
7053 | if (flags_seen[UDPF_BEGIN]) | |
7054 | { | |
7055 | s.op->newline() << "case UDPF_BEGIN:"; | |
37e24fbe | 7056 | s.op->indent(1); |
eff6ac72 | 7057 | s.op->newline() << "if (process_p) {"; |
37e24fbe | 7058 | s.op->indent(1); |
b20bac3a | 7059 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
37e24fbe DS |
7060 | s.op->newline(-1) << "}"; |
7061 | s.op->newline() << "break;"; | |
7062 | s.op->indent(-1); | |
eff6ac72 DS |
7063 | } |
7064 | if (flags_seen[UDPF_THREAD_BEGIN]) | |
7065 | { | |
7066 | s.op->newline() << "case UDPF_THREAD_BEGIN:"; | |
0744f531 | 7067 | s.op->indent(1); |
eff6ac72 | 7068 | s.op->newline() << "if (! process_p) {"; |
cfac4b1f | 7069 | s.op->indent(1); |
b20bac3a | 7070 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
cfac4b1f | 7071 | s.op->newline(-1) << "}"; |
0744f531 DS |
7072 | s.op->newline() << "break;"; |
7073 | s.op->indent(-1); | |
eff6ac72 DS |
7074 | } |
7075 | ||
7076 | // For end/thread_end probes, do nothing at registration time. | |
7077 | // We'll handle these in the 'register_p == 0' case. | |
7078 | if (flags_seen[UDPF_END] || flags_seen[UDPF_THREAD_END]) | |
0744f531 | 7079 | { |
eff6ac72 DS |
7080 | s.op->newline() << "case UDPF_END:"; |
7081 | s.op->newline() << "case UDPF_THREAD_END:"; | |
0744f531 DS |
7082 | s.op->indent(1); |
7083 | s.op->newline() << "break;"; | |
7084 | s.op->indent(-1); | |
7085 | } | |
eff6ac72 | 7086 | |
37e24fbe | 7087 | // Attach an engine for SYSCALL_ENTRY and SYSCALL_EXIT events. |
eff6ac72 | 7088 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
0744f531 | 7089 | { |
eff6ac72 DS |
7090 | s.op->newline() << "case UDPF_SYSCALL:"; |
7091 | s.op->newline() << "case UDPF_SYSCALL_RETURN:"; | |
0744f531 | 7092 | s.op->indent(1); |
eff6ac72 DS |
7093 | s.op->newline() << "rc = stap_utrace_attach(tsk, &p->ops, p, p->events);"; |
7094 | s.op->newline() << "if (rc == 0) {"; | |
0744f531 | 7095 | s.op->indent(1); |
0744f531 DS |
7096 | s.op->newline() << "p->engine_attached = 1;"; |
7097 | s.op->newline(-1) << "}"; | |
7098 | s.op->newline() << "break;"; | |
7099 | s.op->indent(-1); | |
7100 | } | |
eff6ac72 DS |
7101 | |
7102 | s.op->newline() << "default:"; | |
7103 | s.op->indent(1); | |
7104 | s.op->newline() << "_stp_error(\"unhandled flag value %d at %s:%d\", p->flags, __FUNCTION__, __LINE__);"; | |
7105 | s.op->newline() << "break;"; | |
7106 | s.op->indent(-1); | |
935447c8 DS |
7107 | s.op->newline(-1) << "}"; |
7108 | s.op->newline(-1) << "}"; | |
eff6ac72 | 7109 | |
935447c8 | 7110 | // Since this engine could be attached to multiple threads, don't |
b20bac3a DS |
7111 | // call stap_utrace_detach_ops() here, only call |
7112 | // stap_utrace_detach() as necessary. | |
0744f531 | 7113 | s.op->newline() << "else {"; |
935447c8 | 7114 | s.op->indent(1); |
37e24fbe DS |
7115 | s.op->newline() << "switch (p->flags) {"; |
7116 | s.op->indent(1); | |
0744f531 | 7117 | // For death probes, go ahead and call the probe directly. |
eff6ac72 | 7118 | if (flags_seen[UDPF_END]) |
0744f531 | 7119 | { |
eff6ac72 | 7120 | s.op->newline() << "case UDPF_END:"; |
37e24fbe | 7121 | s.op->indent(1); |
eff6ac72 | 7122 | s.op->newline() << "if (process_p) {"; |
0744f531 | 7123 | s.op->indent(1); |
0744f531 DS |
7124 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; |
7125 | s.op->newline(-1) << "}"; | |
37e24fbe DS |
7126 | s.op->newline() << "break;"; |
7127 | s.op->indent(-1); | |
0744f531 | 7128 | } |
eff6ac72 | 7129 | if (flags_seen[UDPF_THREAD_END]) |
37e24fbe | 7130 | { |
eff6ac72 | 7131 | s.op->newline() << "case UDPF_THREAD_END:"; |
37e24fbe | 7132 | s.op->indent(1); |
eff6ac72 DS |
7133 | s.op->newline() << "if (! process_p) {"; |
7134 | s.op->indent(1); | |
7135 | s.op->newline() << "stap_utrace_probe_handler(tsk, p);"; | |
7136 | s.op->newline(-1) << "}"; | |
7137 | s.op->newline() << "break;"; | |
7138 | s.op->indent(-1); | |
7139 | } | |
7140 | ||
b20bac3a DS |
7141 | // For begin/thread_begin probes, we don't need to do anything. |
7142 | if (flags_seen[UDPF_BEGIN] || flags_seen[UDPF_THREAD_BEGIN]) | |
7143 | { | |
eff6ac72 DS |
7144 | s.op->newline() << "case UDPF_BEGIN:"; |
7145 | s.op->newline() << "case UDPF_THREAD_BEGIN:"; | |
b20bac3a DS |
7146 | s.op->indent(1); |
7147 | s.op->newline() << "break;"; | |
7148 | s.op->indent(-1); | |
7149 | } | |
28d29bd3 | 7150 | |
b20bac3a DS |
7151 | if (flags_seen[UDPF_SYSCALL] || flags_seen[UDPF_SYSCALL_RETURN]) |
7152 | { | |
eff6ac72 DS |
7153 | s.op->newline() << "case UDPF_SYSCALL:"; |
7154 | s.op->newline() << "case UDPF_SYSCALL_RETURN:"; | |
37e24fbe | 7155 | s.op->indent(1); |
41211ba3 | 7156 | s.op->newline() << "stap_utrace_detach(tsk, &p->ops);"; |
37e24fbe DS |
7157 | s.op->newline() << "break;"; |
7158 | s.op->indent(-1); | |
7159 | } | |
eff6ac72 DS |
7160 | |
7161 | s.op->newline() << "default:"; | |
7162 | s.op->indent(1); | |
7163 | s.op->newline() << "_stp_error(\"unhandled flag value %d at %s:%d\", p->flags, __FUNCTION__, __LINE__);"; | |
7164 | s.op->newline() << "break;"; | |
7165 | s.op->indent(-1); | |
37e24fbe | 7166 | s.op->newline(-1) << "}"; |
935447c8 DS |
7167 | s.op->newline(-1) << "}"; |
7168 | s.op->newline() << "return rc;"; | |
7169 | s.op->newline(-1) << "}"; | |
7170 | ||
4c2732a1 | 7171 | s.op->newline() << "static struct stap_utrace_probe stap_utrace_probes[] = {"; |
935447c8 DS |
7172 | s.op->indent(1); |
7173 | ||
7174 | // Set up 'process(PATH)' probes | |
7175 | if (! probes_by_path.empty()) | |
7176 | { | |
7177 | for (p_b_path_iterator it = probes_by_path.begin(); | |
7178 | it != probes_by_path.end(); it++) | |
7179 | { | |
a21d81ec DS |
7180 | // Emit a "fake" probe decl that is really a hook for to get |
7181 | // our vm_callback called. | |
7182 | string path = it->first; | |
a21d81ec DS |
7183 | emit_vm_callback_probe_decl (s, true, path, (int64_t)0, |
7184 | "__stp_tf_vm_cb"); | |
a21d81ec | 7185 | |
935447c8 DS |
7186 | for (unsigned i = 0; i < it->second.size(); i++) |
7187 | { | |
7188 | utrace_derived_probe *p = it->second[i]; | |
7189 | emit_probe_decl(s, p); | |
7190 | } | |
7191 | } | |
7192 | } | |
7193 | ||
7194 | // Set up 'process(PID)' probes | |
7195 | if (! probes_by_pid.empty()) | |
7196 | { | |
7197 | for (p_b_pid_iterator it = probes_by_pid.begin(); | |
7198 | it != probes_by_pid.end(); it++) | |
7199 | { | |
a21d81ec DS |
7200 | // Emit a "fake" probe decl that is really a hook for to get |
7201 | // our vm_callback called. | |
5e86a4ee | 7202 | emit_vm_callback_probe_decl (s, false, "", it->first, |
a21d81ec | 7203 | "__stp_tf_vm_cb"); |
a21d81ec | 7204 | |
935447c8 DS |
7205 | for (unsigned i = 0; i < it->second.size(); i++) |
7206 | { | |
7207 | utrace_derived_probe *p = it->second[i]; | |
7208 | emit_probe_decl(s, p); | |
7209 | } | |
7210 | } | |
7211 | } | |
7212 | s.op->newline(-1) << "};"; | |
7213 | } | |
7214 | ||
7215 | ||
7216 | void | |
7217 | utrace_derived_probe_group::emit_module_init (systemtap_session& s) | |
7218 | { | |
7219 | if (probes_by_path.empty() && probes_by_pid.empty()) | |
7220 | return; | |
7221 | ||
7222 | s.op->newline(); | |
7223 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
278def10 | 7224 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_utrace_probes); i++) {"; |
935447c8 DS |
7225 | s.op->indent(1); |
7226 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[i];"; | |
7227 | s.op->newline() << "rc = stap_register_task_finder_target(&p->tgt);"; | |
7228 | s.op->newline(-1) << "}"; | |
7229 | ||
7230 | // rollback all utrace probes | |
7231 | s.op->newline() << "if (rc) {"; | |
7232 | s.op->indent(1); | |
7233 | s.op->newline() << "for (j=i-1; j>=0; j--) {"; | |
7234 | s.op->indent(1); | |
7235 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[j];"; | |
7236 | ||
7237 | s.op->newline() << "if (p->engine_attached) {"; | |
7238 | s.op->indent(1); | |
7239 | s.op->newline() << "stap_utrace_detach_ops(&p->ops);"; | |
7240 | s.op->newline(-1) << "}"; | |
7241 | s.op->newline(-1) << "}"; | |
7242 | ||
7243 | s.op->newline(-1) << "}"; | |
7244 | } | |
7245 | ||
7246 | ||
7247 | void | |
7248 | utrace_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7249 | { | |
7250 | if (probes_by_path.empty() && probes_by_pid.empty()) return; | |
7251 | ||
7252 | s.op->newline(); | |
7253 | s.op->newline() << "/* ---- utrace probes ---- */"; | |
278def10 | 7254 | s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_utrace_probes); i++) {"; |
935447c8 DS |
7255 | s.op->indent(1); |
7256 | s.op->newline() << "struct stap_utrace_probe *p = &stap_utrace_probes[i];"; | |
7257 | ||
7258 | s.op->newline() << "if (p->engine_attached) {"; | |
7259 | s.op->indent(1); | |
7260 | s.op->newline() << "stap_utrace_detach_ops(&p->ops);"; | |
7261 | s.op->newline(-1) << "}"; | |
7262 | s.op->newline(-1) << "}"; | |
7263 | } | |
7264 | ||
7265 | ||
888af770 FCE |
7266 | // ------------------------------------------------------------------------ |
7267 | // user-space probes | |
7268 | // ------------------------------------------------------------------------ | |
7269 | ||
888af770 FCE |
7270 | |
7271 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> | |
7272 | { | |
7273 | public: | |
7274 | void emit_module_decls (systemtap_session& s); | |
7275 | void emit_module_init (systemtap_session& s); | |
7276 | void emit_module_exit (systemtap_session& s); | |
7277 | }; | |
7278 | ||
7279 | ||
6d0f3f0c FCE |
7280 | uprobe_derived_probe::uprobe_derived_probe (const string& function, |
7281 | const string& filename, | |
7282 | int line, | |
7283 | const string& module, | |
7284 | int pid, | |
7285 | const string& section, | |
7286 | Dwarf_Addr dwfl_addr, | |
7287 | Dwarf_Addr addr, | |
7288 | dwarf_query & q, | |
7289 | Dwarf_Die* scope_die /* may be null */): | |
7290 | derived_probe (q.base_probe, new probe_point (*q.base_loc) /* .components soon rewritten */ ), | |
7291 | return_p (q.has_return), module (module), pid (pid), section (section), address (addr) | |
4baf0e53 | 7292 | { |
17c128f2 FCE |
7293 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. |
7294 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
7295 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
7296 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
6d0f3f0c FCE |
7297 | |
7298 | this->tok = q.base_probe->tok; | |
7299 | ||
de688825 | 7300 | // Expand target variables in the probe body |
6d0f3f0c FCE |
7301 | if (!null_die(scope_die)) |
7302 | { | |
de688825 | 7303 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); // XXX: user-space deref's! |
4ed05b15 | 7304 | this->body = v.require (this->body); |
6d0f3f0c FCE |
7305 | |
7306 | // If during target-variable-expanding the probe, we added a new block | |
7307 | // of code, add it to the start of the probe. | |
7308 | if (v.add_block) | |
7309 | this->body = new block(v.add_block, this->body); | |
7310 | ||
7311 | // If when target-variable-expanding the probe, we added a new | |
7312 | // probe, add it in a new file to the list of files to be processed. | |
7313 | if (v.add_probe) | |
7314 | { | |
7315 | stapfile *f = new stapfile; | |
7316 | f->probes.push_back(v.add_probe); | |
7317 | q.sess.files.push_back(f); | |
7318 | } | |
7319 | } | |
7320 | // else - null scope_die - $target variables will produce an error during translate phase | |
7321 | ||
7322 | // Reset the sole element of the "locations" vector as a | |
7323 | // "reverse-engineered" form of the incoming (q.base_loc) probe | |
7324 | // point. This allows a user to see what function / file / line | |
7325 | // number any particular match of the wildcards. | |
7326 | ||
7327 | vector<probe_point::component*> comps; | |
7328 | if(q.has_process) | |
7329 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
7330 | else | |
7331 | assert (0); | |
06aca46a | 7332 | |
6d0f3f0c FCE |
7333 | string fn_or_stmt; |
7334 | if (q.has_function_str || q.has_function_num) | |
7335 | fn_or_stmt = "function"; | |
7336 | else | |
7337 | fn_or_stmt = "statement"; | |
7338 | ||
7339 | if (q.has_function_str || q.has_statement_str) | |
7340 | { | |
7341 | string retro_name = function; | |
7342 | if (filename != "") | |
fb84c077 FCE |
7343 | { |
7344 | retro_name += ("@" + string (filename)); | |
7345 | if (line > 0) | |
7346 | retro_name += (":" + lex_cast<string> (line)); | |
7347 | } | |
6d0f3f0c FCE |
7348 | comps.push_back |
7349 | (new probe_point::component | |
7350 | (fn_or_stmt, new literal_string (retro_name))); | |
7351 | } | |
7352 | else if (q.has_function_num || q.has_statement_num) | |
7353 | { | |
7354 | Dwarf_Addr retro_addr; | |
7355 | if (q.has_function_num) | |
7356 | retro_addr = q.function_num_val; | |
7357 | else | |
7358 | retro_addr = q.statement_num_val; | |
7359 | comps.push_back (new probe_point::component | |
7360 | (fn_or_stmt, | |
7361 | new literal_number(retro_addr))); // XXX: should be hex if possible | |
7362 | ||
7363 | if (q.has_absolute) | |
7364 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
7365 | } | |
7366 | ||
7367 | if (q.has_call) | |
7368 | comps.push_back (new probe_point::component(TOK_CALL)); | |
7369 | if (q.has_inline) | |
7370 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
7371 | if (return_p) | |
7372 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
7373 | /* | |
7374 | if (has_maxactive) | |
7375 | comps.push_back (new probe_point::component | |
7376 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
7377 | */ | |
7378 | ||
7379 | // Overwrite it. | |
7380 | this->sole_location()->components = comps; | |
7381 | } | |
7382 | ||
7383 | ||
0973d815 FCE |
7384 | uprobe_derived_probe::uprobe_derived_probe (probe *base, |
7385 | probe_point *location, | |
7386 | int pid, | |
7387 | Dwarf_Addr addr, | |
7388 | bool has_return): | |
7389 | derived_probe (base, location), // location is not rewritten here | |
7390 | return_p (has_return), pid (pid), address (addr) | |
7391 | { | |
7392 | } | |
7393 | ||
7394 | ||
6d0f3f0c FCE |
7395 | void |
7396 | uprobe_derived_probe::printsig (ostream& o) const | |
7397 | { | |
7398 | // Same as dwarf_derived_probe. | |
7399 | sole_location()->print (o); | |
7400 | o << " /* pc=" << section << "+0x" << hex << address << dec << " */"; | |
7401 | printsig_nested (o); | |
888af770 FCE |
7402 | } |
7403 | ||
7404 | ||
7405 | void | |
7406 | uprobe_derived_probe::join_group (systemtap_session& s) | |
7407 | { | |
7408 | if (! s.uprobe_derived_probes) | |
7409 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
7410 | s.uprobe_derived_probes->enroll (this); | |
6d0f3f0c | 7411 | task_finder_derived_probe_group::create_session_group (s); |
888af770 FCE |
7412 | } |
7413 | ||
7414 | ||
7415 | struct uprobe_builder: public derived_probe_builder | |
7416 | { | |
7417 | uprobe_builder() {} | |
7418 | virtual void build(systemtap_session & sess, | |
7419 | probe * base, | |
7420 | probe_point * location, | |
86bf665e | 7421 | literal_map_t const & parameters, |
888af770 FCE |
7422 | vector<derived_probe *> & finished_results) |
7423 | { | |
7424 | int64_t process, address; | |
7425 | ||
7426 | bool b1 = get_param (parameters, TOK_PROCESS, process); | |
ced347a9 | 7427 | (void) b1; |
888af770 | 7428 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 7429 | (void) b2; |
888af770 FCE |
7430 | bool rr = has_null_param (parameters, TOK_RETURN); |
7431 | assert (b1 && b2); // by pattern_root construction | |
4baf0e53 | 7432 | |
0973d815 | 7433 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
888af770 FCE |
7434 | } |
7435 | }; | |
7436 | ||
7437 | ||
7438 | void | |
775d51e5 | 7439 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) |
888af770 FCE |
7440 | { |
7441 | if (probes.empty()) return; | |
775d51e5 | 7442 | s.op->newline() << "/* ---- user probes ---- */"; |
4baf0e53 | 7443 | |
6d0f3f0c FCE |
7444 | s.need_uprobes = true; // Ask buildrun.cxx to build extra module if needed |
7445 | ||
c480bf3d | 7446 | // If uprobes isn't in the kernel, pull it in from the runtime. |
6274464e | 7447 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; |
888af770 | 7448 | s.op->newline() << "#include <linux/uprobes.h>"; |
c480bf3d | 7449 | s.op->newline() << "#else"; |
6274464e | 7450 | s.op->newline() << "#include \"uprobes/uprobes.h\""; |
c480bf3d | 7451 | s.op->newline() << "#endif"; |
6d0f3f0c FCE |
7452 | s.op->newline() << "#include \"task_finder.c\""; |
7453 | ||
01b05e2e | 7454 | s.op->newline() << "#ifndef MULTIPLE_UPROBES"; |
478c850f FCE |
7455 | s.op->newline() << "#define MULTIPLE_UPROBES 256"; // maximum possible armed uprobes per process() probe point |
7456 | // or apprx. max number of processes mapping a shared library | |
01b05e2e | 7457 | s.op->newline() << "#endif"; |
6d0f3f0c | 7458 | s.op->newline() << "#ifndef MAXUPROBES"; |
01b05e2e | 7459 | s.op->newline() << "#define MAXUPROBES (MULTIPLE_UPROBES * " << probes.size() << ")"; |
6d0f3f0c | 7460 | s.op->newline() << "#endif"; |
888af770 | 7461 | |
5e112f92 FCE |
7462 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
7463 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
4c2732a1 | 7464 | s.op->newline() << "static struct stap_uprobe {"; |
888af770 | 7465 | s.op->newline(1) << "union { struct uprobe up; struct uretprobe urp; };"; |
6d0f3f0c | 7466 | s.op->newline() << "int spec_index;"; // index into stap_uprobe_specs; <0 == free && unregistered |
01b05e2e | 7467 | s.op->newline(-1) << "} stap_uprobes [MAXUPROBES];"; |
5e112f92 | 7468 | s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
6d0f3f0c | 7469 | |
4c2732a1 | 7470 | s.op->newline() << "static struct stap_uprobe_spec {"; |
5e112f92 | 7471 | s.op->newline(1) << "struct stap_task_finder_target finder;"; |
888af770 | 7472 | s.op->newline() << "unsigned long address;"; |
17c128f2 | 7473 | s.op->newline() << "const char *pathname;"; |
888af770 FCE |
7474 | s.op->newline() << "const char *pp;"; |
7475 | s.op->newline() << "void (*ph) (struct context*);"; | |
6d0f3f0c FCE |
7476 | s.op->newline() << "unsigned return_p:1;"; |
7477 | s.op->newline(-1) << "} stap_uprobe_specs [] = {"; | |
888af770 FCE |
7478 | s.op->indent(1); |
7479 | for (unsigned i =0; i<probes.size(); i++) | |
7480 | { | |
7481 | uprobe_derived_probe* p = probes[i]; | |
7482 | s.op->newline() << "{"; | |
0973d815 FCE |
7483 | s.op->line() << " .finder = {"; |
7484 | if (p->pid != 0) | |
17c128f2 FCE |
7485 | s.op->line() << " .pid=" << p->pid; |
7486 | else if (p->section == ".absolute") | |
0973d815 | 7487 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; |
06aca46a | 7488 | // else ".dynamic" gets pathname=0, pid=0, activating task_finder "global tracing" |
0973d815 | 7489 | s.op->line() << "},"; |
17c128f2 FCE |
7490 | if (p->section != ".absolute") |
7491 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
dc38c256 | 7492 | s.op->line() << " .address=(unsigned long)0x" << hex << p->address << dec << "ULL,"; |
888af770 FCE |
7493 | s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ","; |
7494 | s.op->line() << " .ph=&" << p->name << ","; | |
7495 | if (p->return_p) s.op->line() << " .return_p=1,"; | |
7496 | s.op->line() << " },"; | |
7497 | } | |
7498 | s.op->newline(-1) << "};"; | |
7499 | ||
48e685da | 7500 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 7501 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
6d0f3f0c | 7502 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 7503 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
7504 | s.op->newline() << "if (sup->spec_index < 0 ||" |
7505 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 | 7506 | s.op->newline() << "c->regs = regs;"; |
6d0f3f0c | 7507 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
7508 | common_probe_entryfn_epilogue (s.op); |
7509 | s.op->newline(-1) << "}"; | |
7510 | ||
48e685da | 7511 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 7512 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
6d0f3f0c | 7513 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
c12d974f | 7514 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp"); |
6d0f3f0c FCE |
7515 | s.op->newline() << "if (sup->spec_index < 0 ||" |
7516 | << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen | |
888af770 FCE |
7517 | // XXX: kretprobes saves "c->pi = inst;" too |
7518 | s.op->newline() << "c->regs = regs;"; | |
6d0f3f0c | 7519 | s.op->newline() << "(*sups->ph) (c);"; |
888af770 FCE |
7520 | common_probe_entryfn_epilogue (s.op); |
7521 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7522 | |
17c128f2 FCE |
7523 | |
7524 | ||
6d0f3f0c FCE |
7525 | // NB: Because these utrace callbacks only occur before / after |
7526 | // userspace instructions run, there is no concurrency control issue | |
7527 | // between active uprobe callbacks and these registration / | |
06aca46a | 7528 | // unregistration pieces. |
6d0f3f0c FCE |
7529 | |
7530 | // We protect the stap_uprobe->spec_index (which also serves as a | |
d41d451c FCE |
7531 | // free/busy flag) value with the outer protective stap_probes_lock |
7532 | // spinlock, to protect it against concurrent registration / | |
7533 | // unregistration. | |
6d0f3f0c FCE |
7534 | |
7535 | s.op->newline(); | |
17c128f2 FCE |
7536 | s.op->newline() << "static int stap_uprobe_change (struct task_struct *tsk, int register_p, unsigned long relocation, struct stap_uprobe_spec *sups) {"; |
7537 | s.op->newline(1) << "int spec_index = (sups - stap_uprobe_specs);"; | |
6d0f3f0c | 7538 | s.op->newline() << "int handled_p = 0;"; |
d41d451c | 7539 | s.op->newline() << "int slotted_p = 0;"; |
6d0f3f0c FCE |
7540 | s.op->newline() << "int rc = 0;"; |
7541 | s.op->newline() << "int i;"; | |
3568f1dd | 7542 | |
d41d451c | 7543 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; |
01b05e2e | 7544 | s.op->newline() << "for (i=0; i<MAXUPROBES; i++) {"; // XXX: slow linear search |
5e112f92 | 7545 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; |
6d0f3f0c FCE |
7546 | |
7547 | // register new uprobe | |
7548 | s.op->newline() << "if (register_p && sup->spec_index < 0) {"; | |
80b4ad8b FCE |
7549 | // PR6829: we need to check that the sup we're about to reuse is really completely free. |
7550 | // See PR6829 notes below. | |
7551 | s.op->newline(1) << "if (sup->spec_index == -1 && sup->up.kdata != NULL) continue;"; | |
7552 | s.op->newline() << "else if (sup->spec_index == -2 && sup->urp.u.kdata != NULL) continue;"; | |
7553 | s.op->newline() << "sup->spec_index = spec_index;"; | |
d41d451c FCE |
7554 | s.op->newline() << "slotted_p = 1;"; |
7555 | s.op->newline() << "break;"; | |
7556 | s.op->newline(-1) << "} else if (!register_p && " | |
7557 | << "sup->spec_index == spec_index && " // a u[ret]probe set up for this probe point | |
7558 | << "((sups->return_p && sup->urp.u.pid == tsk->tgid && sup->urp.u.vaddr == relocation + sups->address) ||" // dying uretprobe | |
7559 | << "(!sups->return_p && sup->up.pid == tsk->tgid && sup->up.vaddr == relocation + sups->address))) {"; // dying uprobe | |
7560 | s.op->newline(1) << "slotted_p = 1;"; | |
7561 | s.op->newline() << "break;"; // exit to-free slot search | |
7562 | s.op->newline(-1) << "}"; | |
7563 | ||
7564 | s.op->newline(-1) << "}"; | |
7565 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; | |
7566 | ||
7567 | s.op->newline() << "#ifdef DEBUG_UPROBES"; | |
7568 | s.op->newline() << "printk (KERN_INFO \"%cuprobe spec %d idx %d process %s[%d] reloc %p pp '%s'\\n\", "; | |
7569 | s.op->line() << "(register_p ? '+' : '-'), spec_index, (slotted_p ? i : -1), tsk->comm, tsk->tgid, (void*) relocation, sups->pp);"; | |
7570 | s.op->newline() << "#endif"; | |
7571 | ||
7572 | // Here, slotted_p implies that `i' points to the single | |
7573 | // stap_uprobes[] element that has been slotted in for registration | |
7574 | // or unregistration processing. !slotted_p implies that the table | |
7575 | // was full (registration; MAXUPROBES) or that no matching entry was | |
7576 | // found (unregistration; should not happen). | |
7577 | ||
7578 | s.op->newline() << "if (register_p && slotted_p) {"; | |
7579 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
6d0f3f0c FCE |
7580 | s.op->newline() << "if (sups->return_p) {"; |
7581 | s.op->newline(1) << "sup->urp.u.pid = tsk->tgid;"; | |
17c128f2 | 7582 | s.op->newline() << "sup->urp.u.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
7583 | s.op->newline() << "sup->urp.handler = &enter_uretprobe_probe;"; |
7584 | s.op->newline() << "rc = register_uretprobe (& sup->urp);"; | |
7585 | s.op->newline(-1) << "} else {"; | |
7586 | s.op->newline(1) << "sup->up.pid = tsk->tgid;"; | |
17c128f2 | 7587 | s.op->newline() << "sup->up.vaddr = relocation + sups->address;"; |
6d0f3f0c FCE |
7588 | s.op->newline() << "sup->up.handler = &enter_uprobe_probe;"; |
7589 | s.op->newline() << "rc = register_uprobe (& sup->up);"; | |
7590 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7591 | s.op->newline() << "if (rc) {"; // failed to register |
d41d451c FCE |
7592 | 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);"; |
7593 | // NB: we need to release this slot, so we need to borrow the mutex temporarily. | |
7594 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; | |
3568f1dd | 7595 | s.op->newline() << "sup->spec_index = -1;"; |
d41d451c | 7596 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
6d0f3f0c FCE |
7597 | s.op->newline(-1) << "} else {"; |
7598 | s.op->newline(1) << "handled_p = 1;"; // success | |
7599 | s.op->newline(-1) << "}"; | |
6d0f3f0c | 7600 | |
d41d451c FCE |
7601 | s.op->newline(-1) << "} else if (!register_p && slotted_p) {"; |
7602 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];"; | |
7603 | // NB: we need to release this slot, so we need to borrow the mutex temporarily. | |
7604 | s.op->newline() << "mutex_lock (& stap_uprobes_lock);"; | |
7605 | // NB: We must not actually uregister u[ret]probes when a target process execs or exits; | |
80b4ad8b | 7606 | // uprobes does that by itself asynchronously. We can reuse the up/urp struct after |
d41d451c | 7607 | // uprobes clears the sup->{up,urp}->kdata pointer. PR6829. To tell the two |
80b4ad8b | 7608 | // cases apart, we use spec_index -2 vs -1. |
d41d451c | 7609 | s.op->newline() << "sup->spec_index = (sups->return_p ? -2 : -1);"; |
5e112f92 | 7610 | s.op->newline() << "mutex_unlock (& stap_uprobes_lock);"; |
d41d451c FCE |
7611 | s.op->newline() << "handled_p = 1;"; |
7612 | s.op->newline(-1) << "}"; // if slotted_p | |
7613 | ||
7614 | // NB: handled_p implies slotted_p | |
7615 | ||
6d0f3f0c | 7616 | s.op->newline() << "if (! handled_p) {"; |
73209876 FCE |
7617 | s.op->newline(1) << "#ifdef STP_TIMING"; |
7618 | s.op->newline() << "atomic_inc (register_p ? & skipped_count_uprobe_reg : & skipped_count_uprobe_unreg);"; | |
7619 | s.op->newline() << "#endif"; | |
7620 | // NB: duplicates common_entryfn_epilogue, but then this is not a probe entry fn epilogue. | |
7621 | s.op->newline() << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
6d0f3f0c FCE |
7622 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; |
7623 | s.op->newline() << "_stp_exit ();"; | |
7624 | s.op->newline(-1) << "}"; | |
7625 | s.op->newline(-1) << "}"; | |
06aca46a | 7626 | |
6d0f3f0c FCE |
7627 | s.op->newline() << "return 0;"; // XXX: or rc? |
7628 | s.op->newline(-1) << "}"; | |
7629 | s.op->assert_0_indent(); | |
7630 | ||
17c128f2 FCE |
7631 | |
7632 | // The task_finder_callback we use for ET_EXEC targets. | |
7633 | s.op->newline(); | |
7634 | 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) {"; | |
7635 | ||
7636 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; | |
7637 | s.op->newline() << "if (! process_p) return 0;"; | |
7638 | s.op->newline(0) << "return stap_uprobe_change (tsk, register_p, 0, sups);"; | |
7639 | s.op->newline(-1) << "}"; | |
7640 | ||
7641 | // The task_finder_vm_callback we use for ET_DYN targets. | |
7642 | s.op->newline(); | |
7643 | 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) {"; | |
7644 | s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);"; | |
7645 | // 1 - shared libraries' executable segments load from offset 0 - ld.so convention | |
06aca46a | 7646 | s.op->newline() << "if (vm_pgoff != 0) return 0;"; |
17c128f2 | 7647 | // 2 - the shared library we're interested in |
06aca46a | 7648 | s.op->newline() << "if (vm_path == NULL || strcmp (vm_path, sups->pathname)) return 0;"; |
17c128f2 | 7649 | // 3 - probe address within the mapping limits; test should not fail |
06aca46a FCE |
7650 | s.op->newline() << "if (vm_end <= vm_start + sups->address) return 0;"; |
7651 | ||
c16d425a FCE |
7652 | s.op->newline() << "#ifdef DEBUG_TASK_FINDER_VMA"; |
7653 | 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);"; | |
7654 | s.op->newline() << "printk (KERN_INFO \"sups %p pp %s path %s address %p\\n\", sups, sups->pp, sups->pathname ?: \"\", (void*) sups->address);"; | |
7655 | s.op->newline() << "#endif"; | |
7656 | ||
17c128f2 FCE |
7657 | s.op->newline(0) << "return stap_uprobe_change (tsk, map_p, vm_start, sups);"; |
7658 | s.op->newline(-1) << "}"; | |
7659 | s.op->assert_0_indent(); | |
7660 | ||
7661 | ||
6d0f3f0c | 7662 | s.op->newline(); |
888af770 FCE |
7663 | } |
7664 | ||
7665 | ||
7666 | void | |
7667 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
7668 | { | |
7669 | if (probes.empty()) return; | |
5e112f92 FCE |
7670 | s.op->newline() << "/* ---- user probes ---- */"; |
7671 | ||
01b05e2e | 7672 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7673 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7674 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
7675 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
7676 | // initialized to zero. This is so that we can use | |
7677 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
7678 | s.op->newline(-1) << "}"; |
7679 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
888af770 FCE |
7680 | |
7681 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
6d0f3f0c FCE |
7682 | s.op->newline(1) << "struct stap_uprobe_spec *sups = & stap_uprobe_specs[i];"; |
7683 | s.op->newline() << "probe_point = sups->pp;"; // for error messages | |
17c128f2 FCE |
7684 | s.op->newline() << "if (sups->finder.pathname) sups->finder.callback = & stap_uprobe_process_found;"; |
7685 | s.op->newline() << "else if (sups->pathname) sups->finder.vm_callback = & stap_uprobe_vmchange_found;"; | |
6d0f3f0c | 7686 | s.op->newline() << "rc = stap_register_task_finder_target (& sups->finder);"; |
5e112f92 FCE |
7687 | |
7688 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any | |
7689 | // finders already registered, since mere registration does not | |
7690 | // cause any utrace or memory allocation actions. That happens only | |
7691 | // later, once the task finder engine starts running. So, for a | |
7692 | // partial initialization requiring unwind, we need do nothing. | |
7693 | s.op->newline() << "if (rc) break;"; | |
7694 | ||
888af770 FCE |
7695 | s.op->newline(-1) << "}"; |
7696 | } | |
7697 | ||
7698 | ||
7699 | void | |
7700 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7701 | { | |
7702 | if (probes.empty()) return; | |
7703 | s.op->newline() << "/* ---- user probes ---- */"; | |
7704 | ||
6d0f3f0c FCE |
7705 | // NB: there is no stap_unregister_task_finder_target call; |
7706 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
7707 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
7708 | // | |
7709 | // This function blocks until all callbacks are completed, so there | |
7710 | // is supposed to be no possibility of any registration-related code starting | |
7711 | // to run in parallel with our shutdown here. So we don't need to protect the | |
7712 | // stap_uprobes[] array with the mutex. | |
5e112f92 | 7713 | |
01b05e2e | 7714 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7715 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7716 | s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; | |
6d0f3f0c | 7717 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd FCE |
7718 | |
7719 | s.op->newline() << "if (sups->return_p) {"; | |
7720 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 7721 | 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 | 7722 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
7723 | // NB: PR6829 does not change that we still need to unregister at |
7724 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
7725 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
7726 | s.op->newline(-1) << "} else {"; | |
7727 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
d41d451c | 7728 | 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 |
7729 | s.op->newline() << "#endif"; |
7730 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
7731 | s.op->newline(-1) << "}"; | |
7732 | ||
6d0f3f0c | 7733 | s.op->newline() << "sup->spec_index = -1;"; |
3568f1dd FCE |
7734 | |
7735 | // XXX: uprobe missed counts? | |
7736 | ||
6d0f3f0c | 7737 | s.op->newline(-1) << "}"; |
5e112f92 FCE |
7738 | |
7739 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; | |
888af770 FCE |
7740 | } |
7741 | ||
7742 | ||
7743 | ||
98afd80e FCE |
7744 | // ------------------------------------------------------------------------ |
7745 | // timer derived probes | |
7746 | // ------------------------------------------------------------------------ | |
7747 | ||
7748 | ||
12b21830 DS |
7749 | static string TOK_TIMER("timer"); |
7750 | ||
98afd80e FCE |
7751 | struct timer_derived_probe: public derived_probe |
7752 | { | |
7753 | int64_t interval, randomize; | |
b20febf3 | 7754 | bool time_is_msecs; // NB: hrtimers get ms-based probes on modern kernels instead |
422d1ceb | 7755 | timer_derived_probe (probe* p, probe_point* l, int64_t i, int64_t r, bool ms=false); |
b20febf3 FCE |
7756 | virtual void join_group (systemtap_session& s); |
7757 | }; | |
98afd80e | 7758 | |
dc38c0ae | 7759 | |
b20febf3 FCE |
7760 | struct timer_derived_probe_group: public generic_dpg<timer_derived_probe> |
7761 | { | |
d006ef4f | 7762 | void emit_interval (translator_output* o); |
b20febf3 FCE |
7763 | public: |
7764 | void emit_module_decls (systemtap_session& s); | |
7765 | void emit_module_init (systemtap_session& s); | |
7766 | void emit_module_exit (systemtap_session& s); | |
98afd80e FCE |
7767 | }; |
7768 | ||
7769 | ||
422d1ceb FCE |
7770 | timer_derived_probe::timer_derived_probe (probe* p, probe_point* l, int64_t i, int64_t r, bool ms): |
7771 | derived_probe (p, l), interval (i), randomize (r), time_is_msecs(ms) | |
98afd80e FCE |
7772 | { |
7773 | if (interval <= 0 || interval > 1000000) // make i and r fit into plain ints | |
7774 | throw semantic_error ("invalid interval for jiffies timer"); | |
7775 | // randomize = 0 means no randomization | |
7776 | if (randomize < 0 || randomize > interval) | |
7777 | throw semantic_error ("invalid randomize for jiffies timer"); | |
7778 | ||
7779 | if (locations.size() != 1) | |
7780 | throw semantic_error ("expect single probe point"); | |
7781 | // so we don't have to loop over them in the other functions | |
7782 | } | |
7783 | ||
7784 | ||
dc38c0ae | 7785 | void |
b20febf3 | 7786 | timer_derived_probe::join_group (systemtap_session& s) |
dc38c0ae | 7787 | { |
b20febf3 FCE |
7788 | if (! s.timer_derived_probes) |
7789 | s.timer_derived_probes = new timer_derived_probe_group (); | |
7790 | s.timer_derived_probes->enroll (this); | |
dc38c0ae DS |
7791 | } |
7792 | ||
7793 | ||
d006ef4f JS |
7794 | void |
7795 | timer_derived_probe_group::emit_interval (translator_output* o) | |
7796 | { | |
7797 | o->line() << "({"; | |
7798 | o->newline(1) << "unsigned i = stp->intrv;"; | |
7799 | o->newline() << "if (stp->rnd != 0)"; | |
7800 | o->newline(1) << "i += _stp_random_pm(stp->rnd);"; | |
7801 | o->newline(-1) << "stp->ms ? msecs_to_jiffies(i) : i;"; | |
7802 | o->newline(-1) << "})"; | |
7803 | } | |
7804 | ||
7805 | ||
98afd80e | 7806 | void |
b20febf3 | 7807 | timer_derived_probe_group::emit_module_decls (systemtap_session& s) |
98afd80e | 7808 | { |
b20febf3 | 7809 | if (probes.empty()) return; |
46b84a80 | 7810 | |
b20febf3 | 7811 | s.op->newline() << "/* ---- timer probes ---- */"; |
46b84a80 | 7812 | |
4c2732a1 | 7813 | s.op->newline() << "static struct stap_timer_probe {"; |
b20febf3 FCE |
7814 | s.op->newline(1) << "struct timer_list timer_list;"; |
7815 | s.op->newline() << "const char *pp;"; | |
7816 | s.op->newline() << "void (*ph) (struct context*);"; | |
7817 | s.op->newline() << "unsigned intrv, ms, rnd;"; | |
7818 | s.op->newline(-1) << "} stap_timer_probes [" << probes.size() << "] = {"; | |
7819 | s.op->indent(1); | |
7820 | for (unsigned i=0; i < probes.size(); i++) | |
7821 | { | |
4baf0e53 RM |
7822 | s.op->newline () << "{"; |
7823 | s.op->line() << " .pp=" | |
b20febf3 FCE |
7824 | << lex_cast_qstring (*probes[i]->sole_location()) << ","; |
7825 | s.op->line() << " .ph=&" << probes[i]->name << ","; | |
7826 | s.op->line() << " .intrv=" << probes[i]->interval << ","; | |
7827 | s.op->line() << " .ms=" << probes[i]->time_is_msecs << ","; | |
7828 | s.op->line() << " .rnd=" << probes[i]->randomize; | |
7829 | s.op->line() << " },"; | |
7830 | } | |
7831 | s.op->newline(-1) << "};"; | |
7832 | s.op->newline(); | |
98afd80e | 7833 | |
b20febf3 FCE |
7834 | s.op->newline() << "static void enter_timer_probe (unsigned long val) {"; |
7835 | s.op->newline(1) << "struct stap_timer_probe* stp = & stap_timer_probes [val];"; | |
e0d86324 JS |
7836 | s.op->newline() << "if ((atomic_read (&session_state) == STAP_SESSION_STARTING) ||"; |
7837 | s.op->newline() << " (atomic_read (&session_state) == STAP_SESSION_RUNNING))"; | |
7838 | s.op->newline(1) << "mod_timer (& stp->timer_list, jiffies + "; | |
d006ef4f JS |
7839 | emit_interval (s.op); |
7840 | s.op->line() << ");"; | |
e0d86324 JS |
7841 | s.op->newline(-1) << "{"; |
7842 | s.op->indent(1); | |
c12d974f | 7843 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "stp->pp"); |
b20febf3 FCE |
7844 | s.op->newline() << "(*stp->ph) (c);"; |
7845 | common_probe_entryfn_epilogue (s.op); | |
7846 | s.op->newline(-1) << "}"; | |
e0d86324 | 7847 | s.op->newline(-1) << "}"; |
98afd80e FCE |
7848 | } |
7849 | ||
7850 | ||
7851 | void | |
b20febf3 | 7852 | timer_derived_probe_group::emit_module_init (systemtap_session& s) |
dc38c0ae | 7853 | { |
b20febf3 | 7854 | if (probes.empty()) return; |
dc38c0ae | 7855 | |
b20febf3 FCE |
7856 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
7857 | s.op->newline(1) << "struct stap_timer_probe* stp = & stap_timer_probes [i];"; | |
6f313a73 | 7858 | s.op->newline() << "probe_point = stp->pp;"; |
b20febf3 FCE |
7859 | s.op->newline() << "init_timer (& stp->timer_list);"; |
7860 | s.op->newline() << "stp->timer_list.function = & enter_timer_probe;"; | |
7861 | s.op->newline() << "stp->timer_list.data = i;"; // NB: important! | |
7862 | // copy timer renew calculations from above :-( | |
d006ef4f JS |
7863 | s.op->newline() << "stp->timer_list.expires = jiffies + "; |
7864 | emit_interval (s.op); | |
7865 | s.op->line() << ";"; | |
7866 | s.op->newline() << "add_timer (& stp->timer_list);"; | |
b20febf3 FCE |
7867 | // note: no partial failure rollback is needed: add_timer cannot fail. |
7868 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
7869 | } |
7870 | ||
7871 | ||
46b84a80 | 7872 | void |
b20febf3 | 7873 | timer_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 7874 | { |
b20febf3 | 7875 | if (probes.empty()) return; |
46b84a80 | 7876 | |
b20febf3 FCE |
7877 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; |
7878 | s.op->newline(1) << "del_timer_sync (& stap_timer_probes[i].timer_list);"; | |
7879 | s.op->indent(-1); | |
46b84a80 DS |
7880 | } |
7881 | ||
7882 | ||
b20febf3 | 7883 | |
39e57ce0 FCE |
7884 | // ------------------------------------------------------------------------ |
7885 | // profile derived probes | |
7886 | // ------------------------------------------------------------------------ | |
7887 | // On kernels < 2.6.10, this uses the register_profile_notifier API to | |
7888 | // generate the timed events for profiling; on kernels >= 2.6.10 this | |
7889 | // uses the register_timer_hook API. The latter doesn't currently allow | |
7890 | // simultaneous users, so insertion will fail if the profiler is busy. | |
7891 | // (Conflicting users may include OProfile, other SystemTap probes, etc.) | |
7892 | ||
7893 | ||
7894 | struct profile_derived_probe: public derived_probe | |
7895 | { | |
6dce2125 | 7896 | profile_derived_probe (systemtap_session &s, probe* p, probe_point* l); |
b20febf3 FCE |
7897 | void join_group (systemtap_session& s); |
7898 | }; | |
39e57ce0 | 7899 | |
dc38c0ae | 7900 | |
b20febf3 FCE |
7901 | struct profile_derived_probe_group: public generic_dpg<profile_derived_probe> |
7902 | { | |
7903 | public: | |
7904 | void emit_module_decls (systemtap_session& s); | |
7905 | void emit_module_init (systemtap_session& s); | |
7906 | void emit_module_exit (systemtap_session& s); | |
39e57ce0 FCE |
7907 | }; |
7908 | ||
7909 | ||
78f6bba6 | 7910 | profile_derived_probe::profile_derived_probe (systemtap_session &, probe* p, probe_point* l): |
6dce2125 | 7911 | derived_probe(p, l) |
4baf0e53 | 7912 | { |
39e57ce0 FCE |
7913 | } |
7914 | ||
7915 | ||
dc38c0ae | 7916 | void |
b20febf3 | 7917 | profile_derived_probe::join_group (systemtap_session& s) |
dc38c0ae | 7918 | { |
b20febf3 FCE |
7919 | if (! s.profile_derived_probes) |
7920 | s.profile_derived_probes = new profile_derived_probe_group (); | |
7921 | s.profile_derived_probes->enroll (this); | |
dc38c0ae DS |
7922 | } |
7923 | ||
7924 | ||
b20febf3 | 7925 | struct profile_builder: public derived_probe_builder |
39e57ce0 | 7926 | { |
b20febf3 FCE |
7927 | profile_builder() {} |
7928 | virtual void build(systemtap_session & sess, | |
7929 | probe * base, | |
7930 | probe_point * location, | |
86bf665e | 7931 | literal_map_t const &, |
b20febf3 FCE |
7932 | vector<derived_probe *> & finished_results) |
7933 | { | |
682f5024 | 7934 | sess.unwindsym_modules.insert ("kernel"); |
b20febf3 FCE |
7935 | finished_results.push_back(new profile_derived_probe(sess, base, location)); |
7936 | } | |
7937 | }; | |
46b84a80 | 7938 | |
39e57ce0 | 7939 | |
b20febf3 FCE |
7940 | // timer.profile probe handlers are hooked up in an entertaining way |
7941 | // to the underlying kernel facility. The fact that 2.6.11+ era | |
7942 | // "register_timer_hook" API allows only one consumer *system-wide* | |
7943 | // will give a hint. We will have a single entry function (and thus | |
7944 | // trivial registration / unregistration), and it will call all probe | |
7945 | // handler functions in sequence. | |
39e57ce0 | 7946 | |
6dce2125 | 7947 | void |
b20febf3 | 7948 | profile_derived_probe_group::emit_module_decls (systemtap_session& s) |
6dce2125 | 7949 | { |
b20febf3 | 7950 | if (probes.empty()) return; |
39e57ce0 | 7951 | |
b20febf3 FCE |
7952 | // kernels < 2.6.10: use register_profile_notifier API |
7953 | // kernels >= 2.6.10: use register_timer_hook API | |
7954 | s.op->newline() << "/* ---- profile probes ---- */"; | |
39e57ce0 | 7955 | |
b20febf3 FCE |
7956 | // This function calls all the profiling probe handlers in sequence. |
7957 | // The only tricky thing is that the context will be reused amongst | |
7958 | // them. While a simple sequence of calls to the individual probe | |
7959 | // handlers is unlikely to go terribly wrong (with c->last_error | |
7960 | // being set causing an early return), but for extra assurance, we | |
7961 | // open-code the same logic here. | |
39e57ce0 | 7962 | |
b20febf3 FCE |
7963 | s.op->newline() << "static void enter_all_profile_probes (struct pt_regs *regs) {"; |
7964 | s.op->indent(1); | |
c12d974f FCE |
7965 | string pp = lex_cast_qstring("timer.profile"); // hard-coded for convenience |
7966 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", pp); | |
29b0069b | 7967 | s.op->newline() << "c->regs = regs;"; |
39e57ce0 | 7968 | |
b20febf3 FCE |
7969 | for (unsigned i=0; i<probes.size(); i++) |
7970 | { | |
7971 | if (i > 0) | |
7972 | { | |
4baf0e53 | 7973 | // Some lightweight inter-probe context resetting |
6f313a73 | 7974 | // XXX: not quite right: MAXERRORS not respected |
29fdb4e4 | 7975 | s.op->newline() << "c->actionremaining = MAXACTION;"; |
b20febf3 FCE |
7976 | } |
7977 | s.op->newline() << "if (c->last_error == NULL) " << probes[i]->name << " (c);"; | |
7978 | } | |
7979 | common_probe_entryfn_epilogue (s.op); | |
7980 | s.op->newline(-1) << "}"; | |
7981 | ||
7982 | s.op->newline() << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)"; // == using_rpn of yore | |
7983 | ||
4c2732a1 | 7984 | s.op->newline() << "static int enter_profile_probes (struct notifier_block *self," |
b20febf3 FCE |
7985 | << " unsigned long val, void *data) {"; |
7986 | s.op->newline(1) << "(void) self; (void) val;"; | |
7987 | s.op->newline() << "enter_all_profile_probes ((struct pt_regs *) data);"; | |
7988 | s.op->newline() << "return 0;"; | |
7989 | s.op->newline(-1) << "}"; | |
7990 | s.op->newline() << "struct notifier_block stap_profile_notifier = {" | |
7991 | << " .notifier_call = & enter_profile_probes };"; | |
4baf0e53 | 7992 | |
b20febf3 | 7993 | s.op->newline() << "#else"; |
6dce2125 | 7994 | |
4c2732a1 | 7995 | s.op->newline() << "static int enter_profile_probes (struct pt_regs *regs) {"; |
b20febf3 FCE |
7996 | s.op->newline(1) << "enter_all_profile_probes (regs);"; |
7997 | s.op->newline() << "return 0;"; | |
7998 | s.op->newline(-1) << "}"; | |
39e57ce0 | 7999 | |
b20febf3 | 8000 | s.op->newline() << "#endif"; |
39e57ce0 FCE |
8001 | } |
8002 | ||
8003 | ||
dc38c0ae | 8004 | void |
b20febf3 | 8005 | profile_derived_probe_group::emit_module_init (systemtap_session& s) |
dc38c0ae | 8006 | { |
b20febf3 FCE |
8007 | if (probes.empty()) return; |
8008 | ||
6f313a73 | 8009 | s.op->newline() << "probe_point = \"timer.profile\";"; // NB: hard-coded for convenience |
b20febf3 FCE |
8010 | s.op->newline() << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)"; // == using_rpn of yore |
8011 | s.op->newline() << "rc = register_profile_notifier (& stap_profile_notifier);"; | |
8012 | s.op->newline() << "#else"; | |
8013 | s.op->newline() << "rc = register_timer_hook (& enter_profile_probes);"; | |
8014 | s.op->newline() << "#endif"; | |
dc38c0ae DS |
8015 | } |
8016 | ||
8017 | ||
46b84a80 | 8018 | void |
b20febf3 | 8019 | profile_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 8020 | { |
b20febf3 | 8021 | if (probes.empty()) return; |
46b84a80 | 8022 | |
b20febf3 FCE |
8023 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; |
8024 | s.op->newline(1) << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)"; // == using_rpn of yore | |
8025 | s.op->newline() << "unregister_profile_notifier (& stap_profile_notifier);"; | |
8026 | s.op->newline() << "#else"; | |
8027 | s.op->newline() << "unregister_timer_hook (& enter_profile_probes);"; | |
8028 | s.op->newline() << "#endif"; | |
8029 | s.op->indent(-1); | |
46b84a80 DS |
8030 | } |
8031 | ||
ce82316f | 8032 | |
342d3f96 | 8033 | |
604eef3b MH |
8034 | // ------------------------------------------------------------------------ |
8035 | // procfs file derived probes | |
8036 | // ------------------------------------------------------------------------ | |
8037 | ||
ce82316f | 8038 | |
12b21830 DS |
8039 | static string TOK_PROCFS("procfs"); |
8040 | static string TOK_READ("read"); | |
8041 | static string TOK_WRITE("write"); | |
8042 | ||
604eef3b MH |
8043 | struct procfs_derived_probe: public derived_probe |
8044 | { | |
8045 | string path; | |
8046 | bool write; | |
87ebf4cb DS |
8047 | bool target_symbol_seen; |
8048 | ||
604eef3b MH |
8049 | procfs_derived_probe (systemtap_session &, probe* p, probe_point* l, string ps, bool w); |
8050 | void join_group (systemtap_session& s); | |
8051 | }; | |
8052 | ||
8053 | ||
87ebf4cb DS |
8054 | struct procfs_probe_set |
8055 | { | |
8056 | procfs_derived_probe* read_probe; | |
8057 | procfs_derived_probe* write_probe; | |
8058 | ||
8059 | procfs_probe_set () : read_probe (NULL), write_probe (NULL) {} | |
8060 | }; | |
8061 | ||
8062 | ||
604eef3b MH |
8063 | struct procfs_derived_probe_group: public generic_dpg<procfs_derived_probe> |
8064 | { | |
87ebf4cb DS |
8065 | private: |
8066 | map<string, procfs_probe_set*> probes_by_path; | |
8067 | typedef map<string, procfs_probe_set*>::iterator p_b_p_iterator; | |
8068 | bool has_read_probes; | |
8069 | bool has_write_probes; | |
8070 | ||
604eef3b | 8071 | public: |
87ebf4cb DS |
8072 | procfs_derived_probe_group () : |
8073 | has_read_probes(false), has_write_probes(false) {} | |
8074 | ||
8075 | void enroll (procfs_derived_probe* probe); | |
604eef3b MH |
8076 | void emit_module_decls (systemtap_session& s); |
8077 | void emit_module_init (systemtap_session& s); | |
8078 | void emit_module_exit (systemtap_session& s); | |
8079 | }; | |
8080 | ||
8081 | ||
de688825 | 8082 | struct procfs_var_expanding_visitor: public var_expanding_visitor |
87ebf4cb | 8083 | { |
de688825 JS |
8084 | procfs_var_expanding_visitor(systemtap_session& s, const string& pn, |
8085 | string path, bool write_probe): | |
87ebf4cb DS |
8086 | sess (s), probe_name (pn), path (path), write_probe (write_probe), |
8087 | target_symbol_seen (false) {} | |
8088 | ||
8089 | systemtap_session& sess; | |
8090 | string probe_name; | |
8091 | string path; | |
8092 | bool write_probe; | |
8093 | bool target_symbol_seen; | |
8094 | ||
8095 | void visit_target_symbol (target_symbol* e); | |
8096 | }; | |
8097 | ||
8098 | ||
8099 | procfs_derived_probe::procfs_derived_probe (systemtap_session &s, probe* p, | |
8100 | probe_point* l, string ps, bool w): | |
8101 | derived_probe(p, l), path(ps), write(w), target_symbol_seen(false) | |
604eef3b | 8102 | { |
de688825 JS |
8103 | // Expand local variables in the probe body |
8104 | procfs_var_expanding_visitor v (s, name, path, write); | |
8105 | this->body = v.require (this->body); | |
87ebf4cb | 8106 | target_symbol_seen = v.target_symbol_seen; |
604eef3b MH |
8107 | } |
8108 | ||
8109 | ||
8110 | void | |
8111 | procfs_derived_probe::join_group (systemtap_session& s) | |
8112 | { | |
8113 | if (! s.procfs_derived_probes) | |
8114 | s.procfs_derived_probes = new procfs_derived_probe_group (); | |
8115 | s.procfs_derived_probes->enroll (this); | |
8116 | } | |
8117 | ||
ce82316f | 8118 | |
604eef3b | 8119 | void |
87ebf4cb | 8120 | procfs_derived_probe_group::enroll (procfs_derived_probe* p) |
604eef3b | 8121 | { |
87ebf4cb | 8122 | procfs_probe_set *pset; |
ce82316f | 8123 | |
87ebf4cb DS |
8124 | if (probes_by_path.count(p->path) == 0) |
8125 | { | |
8126 | pset = new procfs_probe_set; | |
8127 | probes_by_path[p->path] = pset; | |
8128 | } | |
8129 | else | |
8130 | { | |
8131 | pset = probes_by_path[p->path]; | |
8132 | ||
8133 | // You can only specify 1 read and 1 write probe. | |
8134 | if (p->write && pset->write_probe != NULL) | |
8135 | throw semantic_error("only one write procfs probe can exist for procfs path \"" + p->path + "\""); | |
8136 | else if (! p->write && pset->read_probe != NULL) | |
8137 | throw semantic_error("only one read procfs probe can exist for procfs path \"" + p->path + "\""); | |
5d23847d FCE |
8138 | |
8139 | // XXX: multiple writes should be acceptable | |
87ebf4cb DS |
8140 | } |
8141 | ||
8142 | if (p->write) | |
8143 | { | |
8144 | pset->write_probe = p; | |
8145 | has_write_probes = true; | |
8146 | } | |
8147 | else | |
8148 | { | |
8149 | pset->read_probe = p; | |
8150 | has_read_probes = true; | |
8151 | } | |
604eef3b MH |
8152 | } |
8153 | ||
ce82316f | 8154 | |
604eef3b | 8155 | void |
87ebf4cb | 8156 | procfs_derived_probe_group::emit_module_decls (systemtap_session& s) |
604eef3b | 8157 | { |
87ebf4cb | 8158 | if (probes_by_path.empty()) |
ce82316f | 8159 | return; |
604eef3b | 8160 | |
87ebf4cb | 8161 | s.op->newline() << "/* ---- procfs probes ---- */"; |
4bca766b | 8162 | s.op->newline() << "#include \"procfs.c\""; |
87ebf4cb DS |
8163 | |
8164 | // Emit the procfs probe data list | |
4c2732a1 | 8165 | s.op->newline() << "static struct stap_procfs_probe {"; |
87ebf4cb DS |
8166 | s.op->newline(1)<< "const char *path;"; |
8167 | s.op->newline() << "const char *read_pp;"; | |
8168 | s.op->newline() << "void (*read_ph) (struct context*);"; | |
8169 | s.op->newline() << "const char *write_pp;"; | |
8170 | s.op->newline() << "void (*write_ph) (struct context*);"; | |
8171 | s.op->newline(-1) << "} stap_procfs_probes[] = {"; | |
8172 | s.op->indent(1); | |
8173 | ||
8174 | for (p_b_p_iterator it = probes_by_path.begin(); it != probes_by_path.end(); | |
8175 | it++) | |
8176 | { | |
8177 | procfs_probe_set *pset = it->second; | |
8178 | ||
8179 | s.op->newline() << "{"; | |
8180 | s.op->line() << " .path=" << lex_cast_qstring (it->first) << ","; | |
8181 | ||
8182 | if (pset->read_probe != NULL) | |
8183 | { | |
8184 | s.op->line() << " .read_pp=" | |
8185 | << lex_cast_qstring (*pset->read_probe->sole_location()) | |
8186 | << ","; | |
8187 | s.op->line() << " .read_ph=&" << pset->read_probe->name << ","; | |
8188 | } | |
8189 | else | |
ce82316f | 8190 | { |
87ebf4cb DS |
8191 | s.op->line() << " .read_pp=NULL,"; |
8192 | s.op->line() << " .read_ph=NULL,"; | |
604eef3b | 8193 | } |
ce82316f | 8194 | |
87ebf4cb DS |
8195 | if (pset->write_probe != NULL) |
8196 | { | |
8197 | s.op->line() << " .write_pp=" | |
8198 | << lex_cast_qstring (*pset->write_probe->sole_location()) | |
8199 | << ","; | |
8200 | s.op->line() << " .write_ph=&" << pset->write_probe->name; | |
8201 | } | |
ce82316f | 8202 | else |
87ebf4cb DS |
8203 | { |
8204 | s.op->line() << " .write_pp=NULL,"; | |
8205 | s.op->line() << " .write_ph=NULL"; | |
8206 | } | |
8207 | s.op->line() << " },"; | |
8208 | } | |
8209 | s.op->newline(-1) << "};"; | |
8210 | ||
8211 | if (has_read_probes) | |
8212 | { | |
8213 | // Output routine to fill in 'page' with our data. | |
8214 | s.op->newline(); | |
8215 | ||
8216 | s.op->newline() << "static int _stp_procfs_read(char *page, char **start, off_t off, int count, int *eof, void *data) {"; | |
8217 | ||
8218 | s.op->newline(1) << "struct stap_procfs_probe *spp = (struct stap_procfs_probe *)data;"; | |
8219 | s.op->newline() << "int bytes = 0;"; | |
8220 | s.op->newline() << "string_t strdata = {'\\0'};"; | |
8221 | ||
c12d974f | 8222 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "spp->read_pp"); |
87ebf4cb DS |
8223 | |
8224 | s.op->newline() << "if (c->data == NULL)"; | |
8225 | s.op->newline(1) << "c->data = &strdata;"; | |
007f0f1c DS |
8226 | s.op->newline(-1) << "else {"; |
8227 | ||
8228 | s.op->newline(1) << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
8229 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
8230 | s.op->newline() << "_stp_exit ();"; | |
8231 | s.op->newline(-1) << "}"; | |
8232 | s.op->newline() << "atomic_dec (& c->busy);"; | |
8233 | s.op->newline() << "goto probe_epilogue;"; | |
8234 | s.op->newline(-1) << "}"; | |
87ebf4cb DS |
8235 | |
8236 | // call probe function (which copies data into strdata) | |
007f0f1c | 8237 | s.op->newline() << "(*spp->read_ph) (c);"; |
87ebf4cb DS |
8238 | |
8239 | // copy string data into 'page' | |
8240 | s.op->newline() << "c->data = NULL;"; | |
007f0f1c | 8241 | s.op->newline() << "bytes = strnlen(strdata, MAXSTRINGLEN - 1);"; |
87ebf4cb DS |
8242 | s.op->newline() << "if (off >= bytes)"; |
8243 | s.op->newline(1) << "*eof = 1;"; | |
8244 | s.op->newline(-1) << "else {"; | |
8245 | s.op->newline(1) << "bytes -= off;"; | |
8246 | s.op->newline() << "if (bytes > count)"; | |
8247 | s.op->newline(1) << "bytes = count;"; | |
8248 | s.op->newline(-1) << "memcpy(page, strdata + off, bytes);"; | |
8249 | s.op->newline() << "*start = page;"; | |
8250 | s.op->newline(-1) << "}"; | |
8251 | ||
87ebf4cb | 8252 | common_probe_entryfn_epilogue (s.op); |
87ebf4cb DS |
8253 | s.op->newline() << "return bytes;"; |
8254 | ||
8255 | s.op->newline(-1) << "}"; | |
8256 | } | |
8257 | if (has_write_probes) | |
8258 | { | |
8259 | s.op->newline() << "static int _stp_procfs_write(struct file *file, const char *buffer, unsigned long count, void *data) {"; | |
8260 | ||
8261 | s.op->newline(1) << "struct stap_procfs_probe *spp = (struct stap_procfs_probe *)data;"; | |
007f0f1c DS |
8262 | s.op->newline() << "string_t strdata = {'\\0'};"; |
8263 | ||
c12d974f | 8264 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "spp->write_pp"); |
87ebf4cb | 8265 | |
007f0f1c DS |
8266 | s.op->newline() << "if (count > (MAXSTRINGLEN - 1))"; |
8267 | s.op->newline(1) << "count = MAXSTRINGLEN - 1;"; | |
8268 | s.op->newline(-1) << "_stp_copy_from_user(strdata, buffer, count);"; | |
8269 | ||
8270 | s.op->newline() << "if (c->data == NULL)"; | |
8271 | s.op->newline(1) << "c->data = &strdata;"; | |
8272 | s.op->newline(-1) << "else {"; | |
8273 | ||
8274 | s.op->newline(1) << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {"; | |
8275 | s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);"; | |
8276 | s.op->newline() << "_stp_exit ();"; | |
8277 | s.op->newline(-1) << "}"; | |
8278 | s.op->newline() << "atomic_dec (& c->busy);"; | |
8279 | s.op->newline() << "goto probe_epilogue;"; | |
8280 | s.op->newline(-1) << "}"; | |
8281 | ||
8282 | // call probe function (which copies data out of strdata) | |
8283 | s.op->newline() << "(*spp->write_ph) (c);"; | |
8284 | ||
8285 | s.op->newline() << "c->data = NULL;"; | |
8286 | common_probe_entryfn_epilogue (s.op); | |
8287 | ||
8288 | s.op->newline() << "return count;"; | |
87ebf4cb | 8289 | s.op->newline(-1) << "}"; |
604eef3b | 8290 | } |
87ebf4cb DS |
8291 | } |
8292 | ||
8293 | ||
8294 | void | |
8295 | procfs_derived_probe_group::emit_module_init (systemtap_session& s) | |
8296 | { | |
8297 | if (probes_by_path.empty()) | |
8298 | return; | |
8299 | ||
8300 | s.op->newline() << "for (i = 0; i < " << probes_by_path.size() << "; i++) {"; | |
8301 | s.op->newline(1) << "struct stap_procfs_probe *spp = &stap_procfs_probes[i];"; | |
8302 | ||
8303 | s.op->newline() << "if (spp->read_pp)"; | |
8304 | s.op->newline(1) << "probe_point = spp->read_pp;"; | |
8305 | s.op->newline(-1) << "else"; | |
8306 | s.op->newline(1) << "probe_point = spp->write_pp;"; | |
8307 | ||
8308 | s.op->newline(-1) << "rc = _stp_create_procfs(spp->path, i);"; | |
8309 | ||
8310 | s.op->newline() << "if (rc) {"; | |
8311 | s.op->newline(1) << "_stp_close_procfs();"; | |
8312 | s.op->newline() << "break;"; | |
8313 | s.op->newline(-1) << "}"; | |
8314 | ||
4baf0e53 | 8315 | if (has_read_probes) |
bcf31db4 DS |
8316 | { |
8317 | s.op->newline() << "if (spp->read_pp)"; | |
8318 | s.op->newline(1) << "_stp_procfs_files[i]->read_proc = &_stp_procfs_read;"; | |
8319 | s.op->newline(-1) << "else"; | |
8320 | s.op->newline(1) << "_stp_procfs_files[i]->read_proc = NULL;"; | |
8321 | s.op->indent(-1); | |
8322 | } | |
8323 | else | |
8324 | s.op->newline() << "_stp_procfs_files[i]->read_proc = NULL;"; | |
87ebf4cb | 8325 | |
bcf31db4 DS |
8326 | if (has_write_probes) |
8327 | { | |
cf8dc3c7 | 8328 | s.op->newline() << "if (spp->write_pp)"; |
bcf31db4 DS |
8329 | s.op->newline(1) << "_stp_procfs_files[i]->write_proc = &_stp_procfs_write;"; |
8330 | s.op->newline(-1) << "else"; | |
8331 | s.op->newline(1) << "_stp_procfs_files[i]->write_proc = NULL;"; | |
8332 | s.op->indent(-1); | |
8333 | } | |
8334 | else | |
8335 | s.op->newline() << "_stp_procfs_files[i]->write_proc = NULL;"; | |
87ebf4cb | 8336 | |
bcf31db4 | 8337 | s.op->newline() << "_stp_procfs_files[i]->data = spp;"; |
87ebf4cb | 8338 | s.op->newline(-1) << "}"; // for loop |
604eef3b MH |
8339 | } |
8340 | ||
ce82316f | 8341 | |
604eef3b MH |
8342 | void |
8343 | procfs_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8344 | { | |
87ebf4cb DS |
8345 | if (probes_by_path.empty()) |
8346 | return; | |
8347 | ||
4baf0e53 | 8348 | s.op->newline() << "_stp_close_procfs();"; |
604eef3b MH |
8349 | } |
8350 | ||
ce82316f | 8351 | |
87ebf4cb | 8352 | void |
de688825 | 8353 | procfs_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
87ebf4cb | 8354 | { |
87ebf4cb DS |
8355 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); |
8356 | ||
8357 | if (e->base_name != "$value") | |
8358 | throw semantic_error ("invalid target symbol for procfs probe, $value expected", | |
8359 | e->tok); | |
8360 | ||
4ec2f7d0 DS |
8361 | if (e->components.size() > 0) |
8362 | { | |
8363 | switch (e->components[0].first) | |
8364 | { | |
8365 | case target_symbol::comp_literal_array_index: | |
8366 | throw semantic_error("procfs target variable '$value' may not be used as array", | |
8367 | e->tok); | |
8368 | break; | |
8369 | case target_symbol::comp_struct_member: | |
8370 | throw semantic_error("procfs target variable '$value' may not be used as a structure", | |
8371 | e->tok); | |
8372 | break; | |
8373 | default: | |
8374 | throw semantic_error ("invalid use of procfs target variable '$value'", | |
8375 | e->tok); | |
8376 | break; | |
8377 | } | |
8378 | } | |
8379 | ||
87ebf4cb DS |
8380 | bool lvalue = is_active_lvalue(e); |
8381 | if (write_probe && lvalue) | |
8382 | throw semantic_error("procfs $value variable is read-only in a procfs write probe", e->tok); | |
cf8dc3c7 DS |
8383 | else if (! write_probe && ! lvalue) |
8384 | throw semantic_error("procfs $value variable cannot be read in a procfs read probe", e->tok); | |
87ebf4cb DS |
8385 | |
8386 | // Remember that we've seen a target variable. | |
8387 | target_symbol_seen = true; | |
8388 | ||
8389 | // Synthesize a function. | |
8390 | functiondecl *fdecl = new functiondecl; | |
8391 | fdecl->tok = e->tok; | |
8392 | embeddedcode *ec = new embeddedcode; | |
8393 | ec->tok = e->tok; | |
8394 | ||
8395 | string fname = (string(lvalue ? "_procfs_value_set" : "_procfs_value_get") | |
8396 | + "_" + lex_cast<string>(tick++)); | |
8397 | string locvalue = "CONTEXT->data"; | |
8398 | ||
8399 | if (! lvalue) | |
007f0f1c DS |
8400 | ec->code = string("strlcpy (THIS->__retvalue, ") + locvalue |
8401 | + string(", MAXSTRINGLEN); /* pure */"); | |
87ebf4cb DS |
8402 | else |
8403 | ec->code = string("strlcpy (") + locvalue | |
8404 | + string(", THIS->value, MAXSTRINGLEN);"); | |
8405 | ||
8406 | fdecl->name = fname; | |
8407 | fdecl->body = ec; | |
8408 | fdecl->type = pe_string; | |
8409 | ||
8410 | if (lvalue) | |
8411 | { | |
8412 | // Modify the fdecl so it carries a single pe_string formal | |
8413 | // argument called "value". | |
8414 | ||
8415 | vardecl *v = new vardecl; | |
8416 | v->type = pe_string; | |
8417 | v->name = "value"; | |
8418 | v->tok = e->tok; | |
8419 | fdecl->formal_args.push_back(v); | |
8420 | } | |
f76427a2 | 8421 | sess.functions[fdecl->name]=fdecl; |
87ebf4cb DS |
8422 | |
8423 | // Synthesize a functioncall. | |
8424 | functioncall* n = new functioncall; | |
8425 | n->tok = e->tok; | |
8426 | n->function = fname; | |
8427 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
8428 | ||
8429 | if (lvalue) | |
8430 | { | |
8431 | // Provide the functioncall to our parent, so that it can be | |
8432 | // used to substitute for the assignment node immediately above | |
8433 | // us. | |
8434 | assert(!target_symbol_setter_functioncalls.empty()); | |
8435 | *(target_symbol_setter_functioncalls.top()) = n; | |
8436 | } | |
8437 | ||
4ed05b15 | 8438 | provide (n); |
87ebf4cb DS |
8439 | } |
8440 | ||
8441 | ||
604eef3b MH |
8442 | struct procfs_builder: public derived_probe_builder |
8443 | { | |
ce82316f | 8444 | procfs_builder() {} |
604eef3b MH |
8445 | virtual void build(systemtap_session & sess, |
8446 | probe * base, | |
8447 | probe_point * location, | |
86bf665e | 8448 | literal_map_t const & parameters, |
ce82316f | 8449 | vector<derived_probe *> & finished_results); |
604eef3b | 8450 | }; |
39e57ce0 | 8451 | |
ce82316f DS |
8452 | |
8453 | void | |
8454 | procfs_builder::build(systemtap_session & sess, | |
8455 | probe * base, | |
8456 | probe_point * location, | |
86bf665e | 8457 | literal_map_t const & parameters, |
ce82316f DS |
8458 | vector<derived_probe *> & finished_results) |
8459 | { | |
8460 | string path; | |
12b21830 DS |
8461 | bool has_procfs = get_param(parameters, TOK_PROCFS, path); |
8462 | bool has_read = (parameters.find(TOK_READ) != parameters.end()); | |
8463 | bool has_write = (parameters.find(TOK_WRITE) != parameters.end()); | |
ce82316f | 8464 | |
87ebf4cb DS |
8465 | // If no procfs path, default to "command". The runtime will do |
8466 | // this for us, but if we don't do it here, we'll think the | |
8467 | // following 2 probes are attached to different paths: | |
8468 | // | |
8469 | // probe procfs("command").read {}" | |
8470 | // probe procfs.write {} | |
4baf0e53 | 8471 | |
ce82316f DS |
8472 | if (! has_procfs) |
8473 | path = "command"; | |
227cacb9 DS |
8474 | // If we have a path, we need to validate it. |
8475 | else | |
8476 | { | |
8477 | string::size_type start_pos, end_pos; | |
8478 | string component; | |
8479 | start_pos = 0; | |
8480 | while ((end_pos = path.find('/', start_pos)) != string::npos) | |
8481 | { | |
8482 | // Make sure it doesn't start with '/'. | |
8483 | if (end_pos == 0) | |
8484 | throw semantic_error ("procfs path cannot start with a '/'", | |
8485 | location->tok); | |
8486 | ||
8487 | component = path.substr(start_pos, end_pos - start_pos); | |
8488 | // Make sure it isn't empty. | |
8489 | if (component.size() == 0) | |
8490 | throw semantic_error ("procfs path component cannot be empty", | |
8491 | location->tok); | |
8492 | // Make sure it isn't relative. | |
8493 | else if (component == "." || component == "..") | |
8494 | throw semantic_error ("procfs path cannot be relative (and contain '.' or '..')", location->tok); | |
8495 | ||
8496 | start_pos = end_pos + 1; | |
8497 | } | |
8498 | component = path.substr(start_pos); | |
8499 | // Make sure it doesn't end with '/'. | |
8500 | if (component.size() == 0) | |
8501 | throw semantic_error ("procfs path cannot end with a '/'", location->tok); | |
8502 | // Make sure it isn't relative. | |
8503 | else if (component == "." || component == "..") | |
8504 | throw semantic_error ("procfs path cannot be relative (and contain '.' or '..')", location->tok); | |
8505 | } | |
ce82316f DS |
8506 | |
8507 | if (!(has_read ^ has_write)) | |
8508 | throw semantic_error ("need read/write component", location->tok); | |
8509 | ||
8510 | finished_results.push_back(new procfs_derived_probe(sess, base, location, | |
8511 | path, has_write)); | |
8512 | } | |
8513 | ||
8514 | ||
342d3f96 | 8515 | |
30a279be FCE |
8516 | // ------------------------------------------------------------------------ |
8517 | // statically inserted macro-based derived probes | |
8518 | // ------------------------------------------------------------------------ | |
8519 | ||
12b21830 | 8520 | static string TOK_FORMAT("format"); |
30a279be | 8521 | |
2c384610 DS |
8522 | struct mark_arg |
8523 | { | |
8524 | bool str; | |
8525 | string c_type; | |
8526 | exp_type stp_type; | |
8527 | }; | |
8528 | ||
30a279be FCE |
8529 | struct mark_derived_probe: public derived_probe |
8530 | { | |
8531 | mark_derived_probe (systemtap_session &s, | |
eb973c2a | 8532 | const string& probe_name, const string& probe_format, |
5d23847d | 8533 | probe* base_probe, probe_point* location); |
30a279be | 8534 | |
35d4ab18 | 8535 | systemtap_session& sess; |
eb973c2a | 8536 | string probe_name, probe_format; |
2c384610 DS |
8537 | vector <struct mark_arg *> mark_args; |
8538 | bool target_symbol_seen; | |
30a279be | 8539 | |
b20febf3 | 8540 | void join_group (systemtap_session& s); |
35d4ab18 | 8541 | void emit_probe_context_vars (translator_output* o); |
2c384610 | 8542 | void initialize_probe_context_vars (translator_output* o); |
89f3a125 | 8543 | void printargs (std::ostream &o) const; |
2c384610 | 8544 | |
eb973c2a | 8545 | void parse_probe_format (); |
30a279be FCE |
8546 | }; |
8547 | ||
8548 | ||
b20febf3 FCE |
8549 | struct mark_derived_probe_group: public generic_dpg<mark_derived_probe> |
8550 | { | |
8551 | public: | |
2c384610 DS |
8552 | void emit_module_decls (systemtap_session& s); |
8553 | void emit_module_init (systemtap_session& s); | |
8554 | void emit_module_exit (systemtap_session& s); | |
b20febf3 FCE |
8555 | }; |
8556 | ||
8557 | ||
de688825 | 8558 | struct mark_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 8559 | { |
de688825 JS |
8560 | mark_var_expanding_visitor(systemtap_session& s, const string& pn, |
8561 | vector <struct mark_arg *> &mark_args): | |
2c384610 DS |
8562 | sess (s), probe_name (pn), mark_args (mark_args), |
8563 | target_symbol_seen (false) {} | |
35d4ab18 | 8564 | systemtap_session& sess; |
35d4ab18 | 8565 | string probe_name; |
2c384610 DS |
8566 | vector <struct mark_arg *> &mark_args; |
8567 | bool target_symbol_seen; | |
35d4ab18 FCE |
8568 | |
8569 | void visit_target_symbol (target_symbol* e); | |
0a63c36f | 8570 | void visit_target_symbol_arg (target_symbol* e); |
bc54e71c | 8571 | void visit_target_symbol_context (target_symbol* e); |
35d4ab18 FCE |
8572 | }; |
8573 | ||
8574 | ||
1969b5bc DS |
8575 | void |
8576 | hex_dump(unsigned char *data, size_t len) | |
2c384610 | 8577 | { |
1969b5bc DS |
8578 | // Dump data |
8579 | size_t idx = 0; | |
8580 | while (idx < len) | |
8581 | { | |
8582 | string char_rep; | |
8583 | ||
8584 | clog << " 0x" << setfill('0') << setw(8) << hex << internal << idx; | |
8585 | ||
8586 | for (int i = 0; i < 4; i++) | |
8587 | { | |
8588 | clog << " "; | |
8589 | size_t limit = idx + 4; | |
8590 | while (idx < len && idx < limit) | |
8591 | { | |
8592 | clog << setfill('0') << setw(2) | |
8593 | << ((unsigned) *((unsigned char *)data + idx)); | |
8594 | if (isprint(*((char *)data + idx))) | |
8595 | char_rep += *((char *)data + idx); | |
8596 | else | |
8597 | char_rep += '.'; | |
8598 | idx++; | |
8599 | } | |
8600 | while (idx < limit) | |
8601 | { | |
8602 | clog << " "; | |
8603 | idx++; | |
8604 | } | |
8605 | } | |
8606 | clog << " " << char_rep << dec << setfill(' ') << endl; | |
8607 | } | |
8608 | } | |
8609 | ||
2c384610 | 8610 | |
35d4ab18 | 8611 | void |
de688825 | 8612 | mark_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) |
35d4ab18 | 8613 | { |
35d4ab18 FCE |
8614 | string argnum_s = e->base_name.substr(4,e->base_name.length()-4); |
8615 | int argnum = atoi (argnum_s.c_str()); | |
2c384610 DS |
8616 | |
8617 | if (argnum < 1 || argnum > (int)mark_args.size()) | |
35d4ab18 FCE |
8618 | throw semantic_error ("invalid marker argument number", e->tok); |
8619 | ||
2c384610 DS |
8620 | if (is_active_lvalue (e)) |
8621 | throw semantic_error("write to marker parameter not permitted", e->tok); | |
8622 | ||
6f93ef37 DS |
8623 | if (e->components.size() > 0) |
8624 | { | |
8625 | switch (e->components[0].first) | |
8626 | { | |
8627 | case target_symbol::comp_literal_array_index: | |
8628 | throw semantic_error("marker argument may not be used as array", | |
8629 | e->tok); | |
8630 | break; | |
8631 | case target_symbol::comp_struct_member: | |
8632 | throw semantic_error("marker argument may not be used as a structure", | |
8633 | e->tok); | |
8634 | break; | |
8635 | default: | |
8636 | throw semantic_error ("invalid marker argument use", e->tok); | |
8637 | break; | |
8638 | } | |
8639 | } | |
4baf0e53 | 8640 | |
2c384610 DS |
8641 | // Remember that we've seen a target variable. |
8642 | target_symbol_seen = true; | |
35d4ab18 FCE |
8643 | |
8644 | // Synthesize a function. | |
8645 | functiondecl *fdecl = new functiondecl; | |
8646 | fdecl->tok = e->tok; | |
8647 | embeddedcode *ec = new embeddedcode; | |
8648 | ec->tok = e->tok; | |
35d4ab18 | 8649 | |
1b07c728 FCE |
8650 | string fname = string("_mark_tvar_get") |
8651 | + "_" + e->base_name.substr(1) | |
8652 | + "_" + lex_cast<string>(tick++); | |
35d4ab18 | 8653 | |
2c384610 DS |
8654 | if (mark_args[argnum-1]->stp_type == pe_long) |
8655 | ec->code = string("THIS->__retvalue = CONTEXT->locals[0].") | |
8656 | + probe_name + string(".__mark_arg") | |
8657 | + lex_cast<string>(argnum) + string (";"); | |
8658 | else | |
8659 | ec->code = string("strlcpy (THIS->__retvalue, CONTEXT->locals[0].") | |
8660 | + probe_name + string(".__mark_arg") | |
8661 | + lex_cast<string>(argnum) + string (", MAXSTRINGLEN);"); | |
1b07c728 | 8662 | ec->code += "/* pure */"; |
35d4ab18 FCE |
8663 | fdecl->name = fname; |
8664 | fdecl->body = ec; | |
2c384610 | 8665 | fdecl->type = mark_args[argnum-1]->stp_type; |
f76427a2 | 8666 | sess.functions[fdecl->name]=fdecl; |
35d4ab18 FCE |
8667 | |
8668 | // Synthesize a functioncall. | |
8669 | functioncall* n = new functioncall; | |
8670 | n->tok = e->tok; | |
8671 | n->function = fname; | |
8672 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
4ed05b15 | 8673 | provide (n); |
35d4ab18 FCE |
8674 | } |
8675 | ||
8676 | ||
0a63c36f | 8677 | void |
de688825 | 8678 | mark_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
0a63c36f | 8679 | { |
bc54e71c | 8680 | string sname = e->base_name; |
0a63c36f DS |
8681 | |
8682 | if (is_active_lvalue (e)) | |
bc54e71c | 8683 | throw semantic_error("write to marker '" + sname + "' not permitted", e->tok); |
0a63c36f DS |
8684 | |
8685 | if (e->components.size() > 0) | |
8686 | { | |
8687 | switch (e->components[0].first) | |
8688 | { | |
8689 | case target_symbol::comp_literal_array_index: | |
bc54e71c | 8690 | throw semantic_error("marker '" + sname + "' may not be used as array", |
0a63c36f DS |
8691 | e->tok); |
8692 | break; | |
8693 | case target_symbol::comp_struct_member: | |
bc54e71c | 8694 | throw semantic_error("marker '" + sname + "' may not be used as a structure", |
0a63c36f DS |
8695 | e->tok); |
8696 | break; | |
8697 | default: | |
bc54e71c | 8698 | throw semantic_error ("invalid marker '" + sname + "' use", e->tok); |
0a63c36f DS |
8699 | break; |
8700 | } | |
8701 | } | |
4baf0e53 | 8702 | |
bc54e71c MH |
8703 | string fname; |
8704 | if (e->base_name == "$format") { | |
8705 | fname = string("_mark_format_get"); | |
8706 | } else { | |
8707 | fname = string("_mark_name_get"); | |
8708 | } | |
0a63c36f DS |
8709 | |
8710 | // Synthesize a functioncall. | |
8711 | functioncall* n = new functioncall; | |
8712 | n->tok = e->tok; | |
8713 | n->function = fname; | |
8714 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
4ed05b15 | 8715 | provide (n); |
0a63c36f DS |
8716 | } |
8717 | ||
8718 | void | |
de688825 | 8719 | mark_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a63c36f DS |
8720 | { |
8721 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
8722 | ||
8723 | if (e->base_name.substr(0,4) == "$arg") | |
8724 | visit_target_symbol_arg (e); | |
bc54e71c MH |
8725 | else if (e->base_name == "$format" || e->base_name == "$name") |
8726 | visit_target_symbol_context (e); | |
0a63c36f | 8727 | else |
bc54e71c | 8728 | throw semantic_error ("invalid target symbol for marker, $argN, $name or $format expected", |
0a63c36f DS |
8729 | e->tok); |
8730 | } | |
8731 | ||
8732 | ||
35d4ab18 | 8733 | |
30a279be | 8734 | mark_derived_probe::mark_derived_probe (systemtap_session &s, |
35d4ab18 | 8735 | const string& p_n, |
eb973c2a | 8736 | const string& p_f, |
5d23847d | 8737 | probe* base, probe_point* loc): |
a10fa7f5 | 8738 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), |
eb973c2a | 8739 | sess (s), probe_name (p_n), probe_format (p_f), |
f781f849 | 8740 | target_symbol_seen (false) |
30a279be | 8741 | { |
5d23847d FCE |
8742 | // create synthetic probe point name; preserve condition |
8743 | vector<probe_point::component*> comps; | |
12b21830 DS |
8744 | comps.push_back (new probe_point::component (TOK_KERNEL)); |
8745 | comps.push_back (new probe_point::component (TOK_MARK, new literal_string (probe_name))); | |
8746 | comps.push_back (new probe_point::component (TOK_FORMAT, new literal_string (probe_format))); | |
5d23847d | 8747 | this->sole_location()->components = comps; |
cbbe8080 | 8748 | |
eb973c2a DS |
8749 | // expand the marker format |
8750 | parse_probe_format(); | |
35d4ab18 | 8751 | |
de688825 JS |
8752 | // Now expand the local variables in the probe body |
8753 | mark_var_expanding_visitor v (sess, name, mark_args); | |
8754 | this->body = v.require (this->body); | |
2c384610 | 8755 | target_symbol_seen = v.target_symbol_seen; |
35d4ab18 | 8756 | |
1969b5bc | 8757 | if (sess.verbose > 2) |
eb973c2a DS |
8758 | clog << "marker-based " << name << " mark=" << probe_name |
8759 | << " fmt='" << probe_format << "'" << endl; | |
30a279be FCE |
8760 | } |
8761 | ||
8762 | ||
2c384610 DS |
8763 | static int |
8764 | skip_atoi(const char **s) | |
dc38c0ae | 8765 | { |
2c384610 DS |
8766 | int i = 0; |
8767 | while (isdigit(**s)) | |
8768 | i = i * 10 + *((*s)++) - '0'; | |
8769 | return i; | |
dc38c0ae DS |
8770 | } |
8771 | ||
8772 | ||
30a279be | 8773 | void |
eb973c2a | 8774 | mark_derived_probe::parse_probe_format() |
35d4ab18 | 8775 | { |
eb973c2a | 8776 | const char *fmt = probe_format.c_str(); |
2c384610 DS |
8777 | int qualifier; // 'h', 'l', or 'L' for integer fields |
8778 | mark_arg *arg; | |
8779 | ||
8780 | for (; *fmt ; ++fmt) | |
35d4ab18 | 8781 | { |
2c384610 | 8782 | if (*fmt != '%') |
35d4ab18 | 8783 | { |
2c384610 DS |
8784 | /* Skip text */ |
8785 | continue; | |
8786 | } | |
35d4ab18 | 8787 | |
2c384610 DS |
8788 | repeat: |
8789 | ++fmt; | |
35d4ab18 | 8790 | |
2c384610 DS |
8791 | // skip conversion flags (if present) |
8792 | switch (*fmt) | |
8793 | { | |
8794 | case '-': | |
8795 | case '+': | |
8796 | case ' ': | |
8797 | case '#': | |
8798 | case '0': | |
8799 | goto repeat; | |
8800 | } | |
30a279be | 8801 | |
2c384610 DS |
8802 | // skip minimum field witdh (if present) |
8803 | if (isdigit(*fmt)) | |
8804 | skip_atoi(&fmt); | |
30a279be | 8805 | |
2c384610 DS |
8806 | // skip precision (if present) |
8807 | if (*fmt == '.') | |
35d4ab18 | 8808 | { |
2c384610 DS |
8809 | ++fmt; |
8810 | if (isdigit(*fmt)) | |
8811 | skip_atoi(&fmt); | |
8812 | } | |
30a279be | 8813 | |
2c384610 DS |
8814 | // get the conversion qualifier (if present) |
8815 | qualifier = -1; | |
8816 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') | |
8817 | { | |
8818 | qualifier = *fmt; | |
8819 | ++fmt; | |
8820 | if (qualifier == 'l' && *fmt == 'l') | |
8821 | { | |
8822 | qualifier = 'L'; | |
8823 | ++fmt; | |
8824 | } | |
8825 | } | |
30a279be | 8826 | |
2c384610 DS |
8827 | // get the conversion type |
8828 | switch (*fmt) | |
8829 | { | |
8830 | case 'c': | |
8831 | arg = new mark_arg; | |
8832 | arg->str = false; | |
8833 | arg->c_type = "int"; | |
8834 | arg->stp_type = pe_long; | |
8835 | mark_args.push_back(arg); | |
8836 | continue; | |
8837 | ||
8838 | case 's': | |
8839 | arg = new mark_arg; | |
8840 | arg->str = true; | |
8841 | arg->c_type = "char *"; | |
8842 | arg->stp_type = pe_string; | |
8843 | mark_args.push_back(arg); | |
8844 | continue; | |
8845 | ||
8846 | case 'p': | |
8847 | arg = new mark_arg; | |
8848 | arg->str = false; | |
38c963a9 DS |
8849 | // This should really be 'void *'. But, then we'll get a |
8850 | // compile error when we assign the void pointer to an | |
8851 | // integer without a cast. So, we use 'long' instead, since | |
8852 | // it should have the same size as 'void *'. | |
8853 | arg->c_type = "long"; | |
2c384610 DS |
8854 | arg->stp_type = pe_long; |
8855 | mark_args.push_back(arg); | |
8856 | continue; | |
8857 | ||
8858 | case '%': | |
8859 | continue; | |
8860 | ||
8861 | case 'o': | |
8862 | case 'X': | |
8863 | case 'x': | |
8864 | case 'd': | |
8865 | case 'i': | |
8866 | case 'u': | |
8867 | // fall through... | |
8868 | break; | |
30a279be | 8869 | |
2c384610 DS |
8870 | default: |
8871 | if (!*fmt) | |
8872 | --fmt; | |
8873 | continue; | |
8874 | } | |
30a279be | 8875 | |
2c384610 DS |
8876 | arg = new mark_arg; |
8877 | arg->str = false; | |
8878 | arg->stp_type = pe_long; | |
8879 | switch (qualifier) | |
8880 | { | |
8881 | case 'L': | |
8882 | arg->c_type = "long long"; | |
8883 | break; | |
30a279be | 8884 | |
2c384610 DS |
8885 | case 'l': |
8886 | arg->c_type = "long"; | |
8887 | break; | |
e38d6504 | 8888 | |
2c384610 DS |
8889 | case 'h': |
8890 | arg->c_type = "short"; | |
8891 | break; | |
46b84a80 | 8892 | |
2c384610 DS |
8893 | default: |
8894 | arg->c_type = "int"; | |
8895 | break; | |
8896 | } | |
8897 | mark_args.push_back(arg); | |
8898 | } | |
46b84a80 DS |
8899 | } |
8900 | ||
f781f849 | 8901 | |
46b84a80 | 8902 | void |
2c384610 | 8903 | mark_derived_probe::join_group (systemtap_session& s) |
46b84a80 | 8904 | { |
2c384610 | 8905 | if (! s.mark_derived_probes) |
775d51e5 DS |
8906 | { |
8907 | s.mark_derived_probes = new mark_derived_probe_group (); | |
8908 | ||
8909 | // Make sure <linux/marker.h> is included early. | |
8910 | embeddedcode *ec = new embeddedcode; | |
8911 | ec->tok = NULL; | |
8912 | ec->code = string("#if ! defined(CONFIG_MARKERS)\n") | |
8913 | + string("#error \"Need CONFIG_MARKERS!\"\n") | |
8914 | + string("#endif\n") | |
8915 | + string("#include <linux/marker.h>\n"); | |
8916 | ||
8917 | s.embeds.push_back(ec); | |
8918 | } | |
2c384610 | 8919 | s.mark_derived_probes->enroll (this); |
30a279be FCE |
8920 | } |
8921 | ||
35d4ab18 | 8922 | |
30a279be | 8923 | void |
2c384610 | 8924 | mark_derived_probe::emit_probe_context_vars (translator_output* o) |
30a279be | 8925 | { |
2c384610 DS |
8926 | // If we haven't seen a target symbol for this probe, quit. |
8927 | if (! target_symbol_seen) | |
8928 | return; | |
30a279be | 8929 | |
2c384610 DS |
8930 | for (unsigned i = 0; i < mark_args.size(); i++) |
8931 | { | |
8932 | string localname = "__mark_arg" + lex_cast<string>(i+1); | |
8933 | switch (mark_args[i]->stp_type) | |
8934 | { | |
8935 | case pe_long: | |
8936 | o->newline() << "int64_t " << localname << ";"; | |
8937 | break; | |
8938 | case pe_string: | |
8939 | o->newline() << "string_t " << localname << ";"; | |
8940 | break; | |
8941 | default: | |
8942 | throw semantic_error ("cannot expand unknown type"); | |
8943 | break; | |
8944 | } | |
8945 | } | |
30a279be FCE |
8946 | } |
8947 | ||
8948 | ||
dc38c0ae | 8949 | void |
2c384610 | 8950 | mark_derived_probe::initialize_probe_context_vars (translator_output* o) |
dc38c0ae | 8951 | { |
2c384610 DS |
8952 | // If we haven't seen a target symbol for this probe, quit. |
8953 | if (! target_symbol_seen) | |
8954 | return; | |
8955 | ||
d09fee57 | 8956 | bool deref_fault_needed = false; |
2c384610 | 8957 | for (unsigned i = 0; i < mark_args.size(); i++) |
dc38c0ae | 8958 | { |
2c384610 DS |
8959 | string localname = "l->__mark_arg" + lex_cast<string>(i+1); |
8960 | switch (mark_args[i]->stp_type) | |
8961 | { | |
8962 | case pe_long: | |
3b0c565c | 8963 | o->newline() << localname << " = va_arg(*c->mark_va_list, " |
2c384610 DS |
8964 | << mark_args[i]->c_type << ");"; |
8965 | break; | |
8966 | ||
8967 | case pe_string: | |
8968 | // We're assuming that this is a kernel string (this code is | |
8969 | // basically the guts of kernel_string), not a user string. | |
8970 | o->newline() << "{ " << mark_args[i]->c_type | |
3b0c565c | 8971 | << " tmp_str = va_arg(*c->mark_va_list, " |
2c384610 DS |
8972 | << mark_args[i]->c_type << ");"; |
8973 | o->newline() << "deref_string (" << localname | |
d09fee57 DS |
8974 | << ", tmp_str, MAXSTRINGLEN); }"; |
8975 | deref_fault_needed = true; | |
2c384610 DS |
8976 | break; |
8977 | ||
8978 | default: | |
8979 | throw semantic_error ("cannot expand unknown type"); | |
8980 | break; | |
8981 | } | |
dc38c0ae | 8982 | } |
d09fee57 DS |
8983 | if (deref_fault_needed) |
8984 | // Need to report errors? | |
8985 | o->newline() << "deref_fault: ;"; | |
dc38c0ae DS |
8986 | } |
8987 | ||
89f3a125 WH |
8988 | void |
8989 | mark_derived_probe::printargs(std::ostream &o) const | |
8990 | { | |
8991 | for (unsigned i = 0; i < mark_args.size(); i++) | |
8992 | { | |
8993 | string localname = "$arg" + lex_cast<string>(i+1); | |
8994 | switch (mark_args[i]->stp_type) | |
8995 | { | |
8996 | case pe_long: | |
8997 | o << " " << localname << ":long"; | |
8998 | break; | |
8999 | case pe_string: | |
9000 | o << " " << localname << ":string"; | |
9001 | break; | |
9002 | default: | |
9003 | o << " " << localname << ":unknown"; | |
9004 | break; | |
9005 | } | |
9006 | } | |
9007 | } | |
9008 | ||
30a279be | 9009 | |
342d3f96 DS |
9010 | void |
9011 | mark_derived_probe_group::emit_module_decls (systemtap_session& s) | |
9012 | { | |
9013 | if (probes.empty()) | |
9014 | return; | |
9015 | ||
9016 | s.op->newline() << "/* ---- marker probes ---- */"; | |
46b84a80 | 9017 | |
4c2732a1 | 9018 | s.op->newline() << "static struct stap_marker_probe {"; |
0a63c36f DS |
9019 | s.op->newline(1) << "const char * const name;"; |
9020 | s.op->newline() << "const char * const format;"; | |
9021 | s.op->newline() << "const char * const pp;"; | |
9022 | s.op->newline() << "void (* const ph) (struct context *);"; | |
46b84a80 | 9023 | |
2c384610 DS |
9024 | s.op->newline(-1) << "} stap_marker_probes [" << probes.size() << "] = {"; |
9025 | s.op->indent(1); | |
9026 | for (unsigned i=0; i < probes.size(); i++) | |
46b84a80 | 9027 | { |
4baf0e53 | 9028 | s.op->newline () << "{"; |
2c384610 DS |
9029 | s.op->line() << " .name=" << lex_cast_qstring(probes[i]->probe_name) |
9030 | << ","; | |
eb973c2a | 9031 | s.op->line() << " .format=" << lex_cast_qstring(probes[i]->probe_format) |
2c384610 DS |
9032 | << ","; |
9033 | s.op->line() << " .pp=" << lex_cast_qstring (*probes[i]->sole_location()) | |
9034 | << ","; | |
9035 | s.op->line() << " .ph=&" << probes[i]->name; | |
9036 | s.op->line() << " },"; | |
46b84a80 | 9037 | } |
2c384610 DS |
9038 | s.op->newline(-1) << "};"; |
9039 | s.op->newline(); | |
4baf0e53 | 9040 | |
2c384610 DS |
9041 | |
9042 | // Emit the marker callback function | |
9043 | s.op->newline(); | |
3b0c565c DS |
9044 | s.op->newline() << "static void enter_marker_probe (void *probe_data, void *call_data, const char *fmt, va_list *args) {"; |
9045 | s.op->newline(1) << "struct stap_marker_probe *smp = (struct stap_marker_probe *)probe_data;"; | |
c12d974f | 9046 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "smp->pp"); |
bc54e71c MH |
9047 | s.op->newline() << "c->marker_name = smp->name;"; |
9048 | s.op->newline() << "c->marker_format = smp->format;"; | |
3b0c565c | 9049 | s.op->newline() << "c->mark_va_list = args;"; |
2c384610 | 9050 | s.op->newline() << "(*smp->ph) (c);"; |
3b0c565c | 9051 | s.op->newline() << "c->mark_va_list = NULL;"; |
0a63c36f DS |
9052 | s.op->newline() << "c->data = NULL;"; |
9053 | ||
2c384610 DS |
9054 | common_probe_entryfn_epilogue (s.op); |
9055 | s.op->newline(-1) << "}"; | |
46b84a80 | 9056 | |
2c384610 | 9057 | return; |
46b84a80 DS |
9058 | } |
9059 | ||
9060 | ||
2c384610 DS |
9061 | void |
9062 | mark_derived_probe_group::emit_module_init (systemtap_session &s) | |
30a279be | 9063 | { |
2c384610 DS |
9064 | if (probes.size () == 0) |
9065 | return; | |
30a279be | 9066 | |
2c384610 DS |
9067 | s.op->newline() << "/* init marker probes */"; |
9068 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
9069 | s.op->newline(1) << "struct stap_marker_probe *smp = &stap_marker_probes[i];"; | |
9070 | s.op->newline() << "probe_point = smp->pp;"; | |
38c963a9 | 9071 | s.op->newline() << "rc = marker_probe_register(smp->name, smp->format, enter_marker_probe, smp);"; |
3b0c565c | 9072 | s.op->newline() << "if (rc) {"; |
2c384610 DS |
9073 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
9074 | s.op->newline(1) << "struct stap_marker_probe *smp2 = &stap_marker_probes[j];"; | |
3b0c565c | 9075 | s.op->newline() << "marker_probe_unregister(smp2->name, enter_marker_probe, smp2);"; |
2c384610 DS |
9076 | s.op->newline(-1) << "}"; |
9077 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
9078 | s.op->newline(-1) << "}"; | |
9079 | s.op->newline(-1) << "}"; // for loop | |
9080 | } | |
9081 | ||
9082 | ||
9083 | void | |
9084 | mark_derived_probe_group::emit_module_exit (systemtap_session& s) | |
9085 | { | |
9086 | if (probes.empty()) | |
9087 | return; | |
9088 | ||
9089 | s.op->newline() << "/* deregister marker probes */"; | |
9090 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; | |
9091 | s.op->newline(1) << "struct stap_marker_probe *smp = &stap_marker_probes[i];"; | |
3b0c565c | 9092 | s.op->newline() << "marker_probe_unregister(smp->name, enter_marker_probe, smp);"; |
2c384610 DS |
9093 | s.op->newline(-1) << "}"; // for loop |
9094 | } | |
30a279be FCE |
9095 | |
9096 | ||
9097 | struct mark_builder: public derived_probe_builder | |
9098 | { | |
9099 | private: | |
f781f849 | 9100 | bool cache_initialized; |
eb973c2a DS |
9101 | typedef multimap<string, string> mark_cache_t; |
9102 | typedef multimap<string, string>::const_iterator mark_cache_const_iterator_t; | |
9103 | typedef pair<mark_cache_const_iterator_t, mark_cache_const_iterator_t> | |
9104 | mark_cache_const_iterator_pair_t; | |
9105 | mark_cache_t mark_cache; | |
30a279be FCE |
9106 | |
9107 | public: | |
f781f849 | 9108 | mark_builder(): cache_initialized(false) {} |
2c384610 DS |
9109 | |
9110 | void build_no_more (systemtap_session &s) | |
9111 | { | |
f781f849 | 9112 | if (! mark_cache.empty()) |
2c384610 DS |
9113 | { |
9114 | if (s.verbose > 3) | |
f781f849 DS |
9115 | clog << "mark_builder releasing cache" << endl; |
9116 | mark_cache.clear(); | |
2c384610 DS |
9117 | } |
9118 | } | |
9119 | ||
30a279be FCE |
9120 | void build(systemtap_session & sess, |
9121 | probe * base, | |
9122 | probe_point * location, | |
86bf665e | 9123 | literal_map_t const & parameters, |
30a279be FCE |
9124 | vector<derived_probe *> & finished_results); |
9125 | }; | |
9126 | ||
9127 | ||
30a279be FCE |
9128 | void |
9129 | mark_builder::build(systemtap_session & sess, | |
2c384610 | 9130 | probe * base, |
cbbe8080 | 9131 | probe_point *loc, |
86bf665e | 9132 | literal_map_t const & parameters, |
2c384610 | 9133 | vector<derived_probe *> & finished_results) |
30a279be | 9134 | { |
f781f849 | 9135 | string mark_str_val; |
12b21830 | 9136 | bool has_mark_str = get_param (parameters, TOK_MARK, mark_str_val); |
eb973c2a | 9137 | string mark_format_val; |
12b21830 | 9138 | bool has_mark_format = get_param (parameters, TOK_FORMAT, mark_format_val); |
f781f849 | 9139 | assert (has_mark_str); |
ced347a9 | 9140 | (void) has_mark_str; |
30a279be | 9141 | |
f781f849 DS |
9142 | if (! cache_initialized) |
9143 | { | |
9144 | cache_initialized = true; | |
b5e66ada FCE |
9145 | string module_markers_path = sess.kernel_build_tree + "/Module.markers"; |
9146 | ||
f781f849 DS |
9147 | ifstream module_markers; |
9148 | module_markers.open(module_markers_path.c_str(), ifstream::in); | |
9149 | if (! module_markers) | |
9150 | { | |
9151 | if (sess.verbose>3) | |
9152 | clog << module_markers_path << " cannot be opened: " | |
9153 | << strerror(errno) << endl; | |
9154 | return; | |
9155 | } | |
30a279be | 9156 | |
f781f849 DS |
9157 | string name, module, format; |
9158 | do | |
9159 | { | |
9160 | module_markers >> name >> module; | |
9161 | getline(module_markers, format); | |
9162 | ||
9163 | // trim leading whitespace | |
9164 | string::size_type notwhite = format.find_first_not_of(" \t"); | |
9165 | format.erase(0, notwhite); | |
9166 | ||
a53c9645 DS |
9167 | // If the format is empty, make sure we add back a space |
9168 | // character, which is what MARK_NOARGS expands to. | |
9169 | if (format.length() == 0) | |
9170 | format = " "; | |
9171 | ||
f781f849 DS |
9172 | if (sess.verbose>3) |
9173 | clog << "'" << name << "' '" << module << "' '" << format | |
9174 | << "'" << endl; | |
4baf0e53 | 9175 | |
eb973c2a DS |
9176 | if (mark_cache.count(name) > 0) |
9177 | { | |
9178 | // If we have 2 markers with the same we've got 2 cases: | |
9179 | // different format strings or duplicate format strings. | |
9180 | // If an existing marker in the cache doesn't have the | |
9181 | // same format string, add this marker. | |
9182 | mark_cache_const_iterator_pair_t ret; | |
9183 | mark_cache_const_iterator_t it; | |
9184 | bool matching_format_string = false; | |
41c262f3 | 9185 | |
eb973c2a DS |
9186 | ret = mark_cache.equal_range(name); |
9187 | for (it = ret.first; it != ret.second; ++it) | |
9188 | { | |
9189 | if (format == it->second) | |
9190 | { | |
9191 | matching_format_string = true; | |
9192 | break; | |
9193 | } | |
9194 | } | |
9195 | ||
9196 | if (! matching_format_string) | |
9197 | mark_cache.insert(pair<string,string>(name, format)); | |
9198 | } | |
9199 | else | |
9200 | mark_cache.insert(pair<string,string>(name, format)); | |
f781f849 DS |
9201 | } |
9202 | while (! module_markers.eof()); | |
9203 | module_markers.close(); | |
30a279be FCE |
9204 | } |
9205 | ||
f781f849 | 9206 | // Search marker list for matching markers |
eb973c2a | 9207 | for (mark_cache_const_iterator_t it = mark_cache.begin(); |
f781f849 DS |
9208 | it != mark_cache.end(); it++) |
9209 | { | |
9210 | // Below, "rc" has negative polarity: zero iff matching. | |
9211 | int rc = fnmatch(mark_str_val.c_str(), it->first.c_str(), 0); | |
9212 | if (! rc) | |
9213 | { | |
eb973c2a DS |
9214 | bool add_result = true; |
9215 | ||
9216 | // Match format strings (if the user specified one) | |
9217 | if (has_mark_format && fnmatch(mark_format_val.c_str(), | |
9218 | it->second.c_str(), 0)) | |
9219 | add_result = false; | |
9220 | ||
9221 | if (add_result) | |
9222 | { | |
9223 | derived_probe *dp | |
9224 | = new mark_derived_probe (sess, | |
9225 | it->first, it->second, | |
9226 | base, loc); | |
9227 | finished_results.push_back (dp); | |
9228 | } | |
f781f849 DS |
9229 | } |
9230 | } | |
30a279be FCE |
9231 | } |
9232 | ||
9233 | ||
342d3f96 | 9234 | |
0a6f5a3f JS |
9235 | // ------------------------------------------------------------------------ |
9236 | // statically inserted kernel-tracepoint derived probes | |
9237 | // ------------------------------------------------------------------------ | |
9238 | ||
6fb70fb7 JS |
9239 | struct tracepoint_arg |
9240 | { | |
ad370dcc | 9241 | string name, c_type, typecast; |
dcaa1a65 | 9242 | bool usable, used, isptr; |
f8a968bc | 9243 | Dwarf_Die type_die; |
dcaa1a65 | 9244 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 9245 | }; |
0a6f5a3f JS |
9246 | |
9247 | struct tracepoint_derived_probe: public derived_probe | |
9248 | { | |
79189b84 JS |
9249 | tracepoint_derived_probe (systemtap_session& s, |
9250 | dwflpp& dw, Dwarf_Die& func_die, | |
9251 | const string& tracepoint_name, | |
9252 | probe* base_probe, probe_point* location); | |
9253 | ||
9254 | systemtap_session& sess; | |
6fb70fb7 JS |
9255 | string tracepoint_name, header; |
9256 | vector <struct tracepoint_arg> args; | |
79189b84 | 9257 | |
6fb70fb7 | 9258 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
e2086848 | 9259 | void printargs (std::ostream &o) const; |
79189b84 | 9260 | void join_group (systemtap_session& s); |
f8a968bc | 9261 | void emit_probe_context_vars (translator_output* o); |
0a6f5a3f JS |
9262 | }; |
9263 | ||
9264 | ||
9265 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> | |
9266 | { | |
79189b84 JS |
9267 | void emit_module_decls (systemtap_session& s); |
9268 | void emit_module_init (systemtap_session& s); | |
9269 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f JS |
9270 | }; |
9271 | ||
9272 | ||
f8a968bc JS |
9273 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
9274 | { | |
9275 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
9276 | vector <struct tracepoint_arg>& args): | |
9277 | dw (dw), probe_name (probe_name), args (args) {} | |
9278 | dwflpp& dw; | |
9279 | const string& probe_name; | |
9280 | vector <struct tracepoint_arg>& args; | |
9281 | ||
9282 | void visit_target_symbol (target_symbol* e); | |
9283 | void visit_target_symbol_arg (target_symbol* e); | |
9284 | void visit_target_symbol_context (target_symbol* e); | |
9285 | }; | |
9286 | ||
9287 | ||
9288 | void | |
9289 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
9290 | { | |
9291 | string argname = e->base_name.substr(1); | |
9292 | ||
9293 | // search for a tracepoint parameter matching this name | |
9294 | tracepoint_arg *arg = NULL; | |
9295 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 9296 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
9297 | { |
9298 | arg = &args[i]; | |
9299 | arg->used = true; | |
9300 | break; | |
9301 | } | |
9302 | ||
9303 | if (arg == NULL) | |
9304 | { | |
9305 | stringstream alternatives; | |
9306 | for (unsigned i = 0; i < args.size(); ++i) | |
9307 | alternatives << " $" << args[i].name; | |
046e7190 | 9308 | alternatives << " $$name $$parms $$vars"; |
f8a968bc JS |
9309 | |
9310 | // We hope that this value ends up not being referenced after all, so it | |
9311 | // can be optimized out quietly. | |
9312 | semantic_error* saveme = | |
9313 | new semantic_error("unable to find tracepoint variable '" + e->base_name | |
9314 | + "' (alternatives:" + alternatives.str () + ")", e->tok); | |
9315 | // NB: we can have multiple errors, since a target variable | |
9316 | // may be expanded in several different contexts: | |
9317 | // trace ("*") { $foo->bar } | |
9318 | saveme->chain = e->saved_conversion_error; | |
9319 | e->saved_conversion_error = saveme; | |
9320 | provide (e); | |
9321 | return; | |
9322 | } | |
9323 | ||
9324 | // make sure we're not dereferencing base types | |
9325 | if (!e->components.empty() && !arg->isptr) | |
9326 | switch (e->components[0].first) | |
9327 | { | |
9328 | case target_symbol::comp_literal_array_index: | |
9329 | throw semantic_error("tracepoint variable '" + e->base_name | |
9330 | + "' may not be used as array", e->tok); | |
9331 | case target_symbol::comp_struct_member: | |
9332 | throw semantic_error("tracepoint variable '" + e->base_name | |
9333 | + "' may not be used as a structure", e->tok); | |
9334 | default: | |
9335 | throw semantic_error("invalid use of tracepoint variable '" | |
9336 | + e->base_name + "'", e->tok); | |
9337 | } | |
9338 | ||
9339 | // we can only write to dereferenced fields, and only if guru mode is on | |
9340 | bool lvalue = is_active_lvalue(e); | |
9341 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
9342 | throw semantic_error("write to tracepoint variable '" + e->base_name | |
9343 | + "' not permitted", e->tok); | |
ad370dcc JS |
9344 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
9345 | // is also meaningless until you dereference past a pointer member. It's | |
9346 | // harder to detect and prevent that though... | |
f8a968bc JS |
9347 | |
9348 | if (e->components.empty()) | |
9349 | { | |
9350 | // Synthesize a simple function to grab the parameter | |
9351 | functiondecl *fdecl = new functiondecl; | |
9352 | fdecl->tok = e->tok; | |
9353 | embeddedcode *ec = new embeddedcode; | |
9354 | ec->tok = e->tok; | |
9355 | ||
9356 | string fname = (string("_tracepoint_tvar_get") | |
9357 | + "_" + e->base_name.substr(1) | |
9358 | + "_" + lex_cast<string>(tick++)); | |
9359 | ||
9360 | fdecl->name = fname; | |
9361 | fdecl->body = ec; | |
9362 | fdecl->type = pe_long; | |
9363 | ||
9364 | ec->code = (string("THIS->__retvalue = CONTEXT->locals[0].") | |
9365 | + probe_name + string(".__tracepoint_arg_") | |
9366 | + arg->name + string (";/* pure */")); | |
9367 | ||
9368 | dw.sess.functions[fdecl->name] = fdecl; | |
9369 | ||
9370 | // Synthesize a functioncall. | |
9371 | functioncall* n = new functioncall; | |
9372 | n->tok = e->tok; | |
9373 | n->function = fname; | |
9374 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
9375 | ||
9376 | provide (n); | |
9377 | } | |
9378 | else | |
9379 | { | |
9380 | // Synthesize a function to dereference the dwarf fields, | |
9381 | // with a pointer parameter that is the base tracepoint variable | |
9382 | functiondecl *fdecl = new functiondecl; | |
9383 | fdecl->tok = e->tok; | |
9384 | embeddedcode *ec = new embeddedcode; | |
9385 | ec->tok = e->tok; | |
9386 | ||
9387 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") | |
9388 | + "_" + e->base_name.substr(1) | |
9389 | + "_" + lex_cast<string>(tick++)); | |
9390 | ||
9391 | fdecl->name = fname; | |
9392 | fdecl->body = ec; | |
9393 | ||
9394 | try | |
9395 | { | |
9396 | ec->code = dw.literal_stmt_for_pointer (&arg->type_die, e->components, | |
9397 | lvalue, fdecl->type); | |
9398 | } | |
9399 | catch (const semantic_error& er) | |
9400 | { | |
9401 | // We suppress this error message, and pass the unresolved | |
9402 | // variable to the next pass. We hope that this value ends | |
9403 | // up not being referenced after all, so it can be optimized out | |
9404 | // quietly. | |
9405 | semantic_error* saveme = new semantic_error (er); // copy it | |
9406 | saveme->tok1 = e->tok; // XXX: token not passed to dw code generation routines | |
9407 | // NB: we can have multiple errors, since a target variable | |
9408 | // may be expanded in several different contexts: | |
9409 | // trace ("*") { $foo->bar } | |
9410 | saveme->chain = e->saved_conversion_error; | |
9411 | e->saved_conversion_error = saveme; | |
9412 | provide (e); | |
9413 | return; | |
9414 | } | |
9415 | ||
9416 | // Give the fdecl an argument for the raw tracepoint value | |
9417 | vardecl *v1 = new vardecl; | |
9418 | v1->type = pe_long; | |
9419 | v1->name = "pointer"; | |
9420 | v1->tok = e->tok; | |
9421 | fdecl->formal_args.push_back(v1); | |
9422 | ||
9423 | if (lvalue) | |
9424 | { | |
9425 | // Modify the fdecl so it carries a pe_long formal | |
9426 | // argument called "value". | |
9427 | ||
9428 | // FIXME: For the time being we only support setting target | |
9429 | // variables which have base types; these are 'pe_long' in | |
9430 | // stap's type vocabulary. Strings and pointers might be | |
9431 | // reasonable, some day, but not today. | |
9432 | ||
9433 | vardecl *v2 = new vardecl; | |
9434 | v2->type = pe_long; | |
9435 | v2->name = "value"; | |
9436 | v2->tok = e->tok; | |
9437 | fdecl->formal_args.push_back(v2); | |
9438 | } | |
9439 | else | |
9440 | ec->code += "/* pure */"; | |
9441 | ||
9442 | dw.sess.functions[fdecl->name] = fdecl; | |
9443 | ||
9444 | // Synthesize a functioncall. | |
9445 | functioncall* n = new functioncall; | |
9446 | n->tok = e->tok; | |
9447 | n->function = fname; | |
9448 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
9449 | ||
9450 | // make the original a bare target symbol for the tracepoint value, | |
9451 | // which will be passed into the dwarf dereferencing code | |
9452 | e->components.clear(); | |
9453 | n->args.push_back(require(e)); | |
9454 | ||
9455 | if (lvalue) | |
9456 | { | |
9457 | // Provide the functioncall to our parent, so that it can be | |
9458 | // used to substitute for the assignment node immediately above | |
9459 | // us. | |
9460 | assert(!target_symbol_setter_functioncalls.empty()); | |
9461 | *(target_symbol_setter_functioncalls.top()) = n; | |
9462 | } | |
9463 | ||
9464 | provide (n); | |
9465 | } | |
9466 | } | |
9467 | ||
9468 | ||
9469 | void | |
9470 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
9471 | { | |
9472 | if (is_active_lvalue (e)) | |
9473 | throw semantic_error("write to tracepoint '" + e->base_name + "' not permitted", e->tok); | |
9474 | ||
9475 | if (!e->components.empty()) | |
9476 | switch (e->components[0].first) | |
9477 | { | |
9478 | case target_symbol::comp_literal_array_index: | |
9479 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as array", | |
9480 | e->tok); | |
9481 | case target_symbol::comp_struct_member: | |
9482 | throw semantic_error("tracepoint '" + e->base_name + "' may not be used as a structure", | |
9483 | e->tok); | |
9484 | default: | |
9485 | throw semantic_error("invalid tracepoint '" + e->base_name + "' use", e->tok); | |
9486 | } | |
9487 | ||
9488 | if (e->base_name == "$$name") | |
9489 | { | |
9490 | // Synthesize a functioncall. | |
9491 | functioncall* n = new functioncall; | |
9492 | n->tok = e->tok; | |
9493 | n->function = "_mark_name_get"; | |
9494 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
9495 | provide (n); | |
9496 | } | |
046e7190 | 9497 | else if (e->base_name == "$$vars" || e->base_name == "$$parms") |
f8a968bc JS |
9498 | { |
9499 | target_symbol *tsym = new target_symbol; | |
9500 | print_format* pf = new print_format; | |
9501 | ||
9502 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
9503 | // NB: we synthesize a new token here rather than reusing | |
9504 | // e->tok, because print_format::print likes to use | |
9505 | // its tok->content. | |
9506 | token* pf_tok = new token(*e->tok); | |
9507 | pf_tok->content = "sprintf"; | |
9508 | ||
9509 | pf->tok = pf_tok; | |
9510 | pf->print_to_stream = false; | |
9511 | pf->print_with_format = true; | |
9512 | pf->print_with_delim = false; | |
9513 | pf->print_with_newline = false; | |
9514 | pf->print_char = false; | |
9515 | ||
9516 | for (unsigned i = 0; i < args.size(); ++i) | |
9517 | { | |
dcaa1a65 JS |
9518 | if (!args[i].usable) |
9519 | continue; | |
f8a968bc JS |
9520 | if (i > 0) |
9521 | pf->raw_components += " "; | |
9522 | pf->raw_components += args[i].name; | |
9523 | tsym->tok = e->tok; | |
9524 | tsym->base_name = "$" + args[i].name; | |
9525 | ||
9526 | // every variable should always be accessible! | |
9527 | tsym->saved_conversion_error = 0; | |
9528 | expression *texp = require (tsym); // NB: throws nothing ... | |
9529 | assert (!tsym->saved_conversion_error); // ... but this is how we know it happened. | |
9530 | ||
9531 | pf->raw_components += "=%#x"; | |
9532 | pf->args.push_back(texp); | |
9533 | } | |
9534 | ||
9535 | pf->components = print_format::string_to_components(pf->raw_components); | |
9536 | provide (pf); | |
9537 | } | |
9538 | else | |
9539 | assert(0); // shouldn't get here | |
9540 | } | |
9541 | ||
9542 | void | |
9543 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
9544 | { | |
9545 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
9546 | ||
046e7190 JS |
9547 | if (e->base_name == "$$name" || |
9548 | e->base_name == "$$parms" || | |
9549 | e->base_name == "$$vars") | |
f8a968bc JS |
9550 | visit_target_symbol_context (e); |
9551 | else | |
9552 | visit_target_symbol_arg (e); | |
9553 | } | |
9554 | ||
9555 | ||
9556 | ||
79189b84 JS |
9557 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
9558 | dwflpp& dw, Dwarf_Die& func_die, | |
9559 | const string& tracepoint_name, | |
9560 | probe* base, probe_point* loc): | |
9561 | derived_probe (base, new probe_point(*loc) /* .components soon rewritten */), | |
9562 | sess (s), tracepoint_name (tracepoint_name) | |
9563 | { | |
9564 | // create synthetic probe point name; preserve condition | |
9565 | vector<probe_point::component*> comps; | |
9566 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
9567 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
9568 | this->sole_location()->components = comps; | |
9569 | ||
6fb70fb7 JS |
9570 | // fill out the available arguments in this tracepoint |
9571 | build_args(dw, func_die); | |
9572 | ||
9573 | // determine which header defined this tracepoint | |
9574 | string decl_file = dwarf_decl_file(&func_die); | |
9575 | size_t header_pos = decl_file.rfind("trace/"); | |
9576 | if (header_pos == string::npos) | |
9577 | throw semantic_error ("cannot parse header location for tracepoint '" | |
9578 | + tracepoint_name + "' in '" | |
9579 | + decl_file + "'"); | |
9580 | header = decl_file.substr(header_pos); | |
9581 | ||
9582 | // tracepoints from FOO_event_types.h should really be included from FOO.h | |
9583 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
9584 | // ... walk up to see which one was directly included by tracequery.c | |
9585 | header_pos = header.find("_event_types"); | |
9586 | if (header_pos != string::npos) | |
9587 | header.erase(header_pos, 12); | |
9588 | ||
f8a968bc JS |
9589 | // Now expand the local variables in the probe body |
9590 | tracepoint_var_expanding_visitor v (dw, name, args); | |
9591 | this->body = v.require (this->body); | |
9592 | ||
79189b84 JS |
9593 | if (sess.verbose > 2) |
9594 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name | |
9595 | << "'" << endl; | |
9596 | } | |
9597 | ||
9598 | ||
6fb70fb7 JS |
9599 | static bool |
9600 | dwarf_type_name(Dwarf_Die& type_die, string& c_type) | |
9601 | { | |
219e62c7 JS |
9602 | // if we've gotten down to a basic type, then we're done |
9603 | bool done = true; | |
9604 | switch (dwarf_tag(&type_die)) | |
6fb70fb7 | 9605 | { |
219e62c7 JS |
9606 | case DW_TAG_structure_type: |
9607 | c_type.append("struct "); | |
9608 | break; | |
9609 | case DW_TAG_union_type: | |
9610 | c_type.append("union "); | |
9611 | break; | |
9612 | case DW_TAG_typedef: | |
9613 | case DW_TAG_base_type: | |
9614 | break; | |
9615 | default: | |
9616 | done = false; | |
9617 | break; | |
9618 | } | |
9619 | if (done) | |
9620 | { | |
9621 | c_type.append(dwarf_diename_integrate(&type_die)); | |
6fb70fb7 JS |
9622 | return true; |
9623 | } | |
9624 | ||
9625 | // otherwise, this die is a type modifier. | |
9626 | ||
9627 | // recurse into the referent type | |
219e62c7 | 9628 | // if it can't be named, just call it "void" |
6fb70fb7 JS |
9629 | Dwarf_Attribute subtype_attr; |
9630 | Dwarf_Die subtype_die; | |
9631 | if (!dwarf_attr_integrate(&type_die, DW_AT_type, &subtype_attr) | |
9632 | || !dwarf_formref_die(&subtype_attr, &subtype_die) | |
9633 | || !dwarf_type_name(subtype_die, c_type)) | |
219e62c7 | 9634 | c_type = "void"; |
6fb70fb7 JS |
9635 | |
9636 | const char *suffix = NULL; | |
9637 | switch (dwarf_tag(&type_die)) | |
9638 | { | |
9639 | case DW_TAG_pointer_type: | |
9640 | suffix = "*"; | |
9641 | break; | |
9642 | case DW_TAG_array_type: | |
9643 | suffix = "[]"; | |
9644 | break; | |
9645 | case DW_TAG_const_type: | |
9646 | suffix = " const"; | |
9647 | break; | |
9648 | case DW_TAG_volatile_type: | |
9649 | suffix = " volatile"; | |
9650 | break; | |
9651 | default: | |
9652 | return false; | |
9653 | } | |
9654 | c_type.append(suffix); | |
219e62c7 JS |
9655 | |
9656 | // XXX HACK! The va_list isn't usable as found in the debuginfo... | |
9657 | if (c_type == "struct __va_list_tag*") | |
9658 | c_type = "va_list"; | |
9659 | ||
6fb70fb7 JS |
9660 | return true; |
9661 | } | |
9662 | ||
9663 | ||
f8a968bc | 9664 | static bool |
dcaa1a65 | 9665 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
f8a968bc JS |
9666 | { |
9667 | Dwarf_Attribute type_attr; | |
dcaa1a65 | 9668 | switch (dwarf_tag(&arg.type_die)) |
f8a968bc JS |
9669 | { |
9670 | case DW_TAG_typedef: | |
9671 | case DW_TAG_const_type: | |
9672 | case DW_TAG_volatile_type: | |
9673 | // iterate on the referent type | |
dcaa1a65 JS |
9674 | return (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) |
9675 | && dwarf_formref_die(&type_attr, &arg.type_die) | |
9676 | && resolve_tracepoint_arg_type(arg)); | |
f8a968bc JS |
9677 | case DW_TAG_base_type: |
9678 | // base types will simply be treated as script longs | |
dcaa1a65 | 9679 | arg.isptr = false; |
f8a968bc JS |
9680 | return true; |
9681 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
9682 | // pointers can be treated as script longs, |
9683 | // and if we know their type, they can also be dereferenced | |
9684 | if (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr) | |
9685 | && dwarf_formref_die(&type_attr, &arg.type_die)) | |
9686 | arg.isptr = true; | |
ad370dcc JS |
9687 | arg.typecast = "(intptr_t)"; |
9688 | return true; | |
9689 | case DW_TAG_structure_type: | |
9690 | case DW_TAG_union_type: | |
9691 | // for structs/unions which are passed by value, we turn it into | |
9692 | // a pointer that can be dereferenced. | |
9693 | arg.isptr = true; | |
9694 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 9695 | return true; |
f8a968bc JS |
9696 | default: |
9697 | // should we consider other types too? | |
9698 | return false; | |
9699 | } | |
9700 | } | |
9701 | ||
9702 | ||
6fb70fb7 JS |
9703 | void |
9704 | tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die) | |
9705 | { | |
9706 | Dwarf_Die arg; | |
9707 | if (dwarf_child(&func_die, &arg) == 0) | |
9708 | do | |
9709 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
9710 | { | |
9711 | // build a tracepoint_arg for this parameter | |
9712 | tracepoint_arg tparg; | |
9713 | tparg.name = dwarf_diename_integrate(&arg); | |
9714 | ||
9715 | // read the type of this parameter | |
9716 | Dwarf_Attribute type_attr; | |
6fb70fb7 | 9717 | if (!dwarf_attr_integrate (&arg, DW_AT_type, &type_attr) |
f8a968bc | 9718 | || !dwarf_formref_die (&type_attr, &tparg.type_die) |
dcaa1a65 | 9719 | || !dwarf_type_name(tparg.type_die, tparg.c_type)) |
6fb70fb7 JS |
9720 | throw semantic_error ("cannot get type of tracepoint '" |
9721 | + tracepoint_name + "' parameter '" | |
9722 | + tparg.name + "'"); | |
9723 | ||
dcaa1a65 | 9724 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
9725 | args.push_back(tparg); |
9726 | if (sess.verbose > 4) | |
9727 | clog << "found parameter for tracepoint '" << tracepoint_name | |
9728 | << "': type:'" << tparg.c_type | |
9729 | << "' name:'" << tparg.name << "'" << endl; | |
9730 | } | |
9731 | while (dwarf_siblingof(&arg, &arg) == 0); | |
9732 | } | |
9733 | ||
e2086848 WH |
9734 | void |
9735 | tracepoint_derived_probe::printargs(std::ostream &o) const | |
9736 | { | |
dcaa1a65 JS |
9737 | for (unsigned i = 0; i < args.size(); ++i) |
9738 | if (args[i].usable) | |
9739 | o << " $" << args[i].name << ":" << args[i].c_type; | |
e2086848 | 9740 | } |
6fb70fb7 | 9741 | |
79189b84 JS |
9742 | void |
9743 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
9744 | { | |
9745 | if (! s.tracepoint_derived_probes) | |
9746 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
9747 | s.tracepoint_derived_probes->enroll (this); | |
9748 | } | |
9749 | ||
9750 | ||
f8a968bc JS |
9751 | void |
9752 | tracepoint_derived_probe::emit_probe_context_vars (translator_output* o) | |
9753 | { | |
9754 | for (unsigned i = 0; i < args.size(); i++) | |
9755 | if (args[i].used) | |
9756 | o->newline() << "int64_t __tracepoint_arg_" << args[i].name << ";"; | |
9757 | } | |
9758 | ||
9759 | ||
79189b84 JS |
9760 | void |
9761 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) | |
9762 | { | |
9763 | if (probes.empty()) | |
9764 | return; | |
9765 | ||
96b030fe JS |
9766 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
9767 | s.op->newline(); | |
79189b84 | 9768 | |
6fb70fb7 JS |
9769 | for (unsigned i = 0; i < probes.size(); ++i) |
9770 | { | |
9771 | tracepoint_derived_probe *p = probes[i]; | |
96b030fe JS |
9772 | |
9773 | // emit a separate entry function for each probe, since tracepoints | |
9774 | // don't provide any sort of context pointer. | |
6fb70fb7 JS |
9775 | s.op->newline() << "#include <" << p->header << ">"; |
9776 | s.op->newline() << "static void enter_tracepoint_probe_" << i << "("; | |
8df306c4 JS |
9777 | if (p->args.size() == 0) |
9778 | s.op->line() << "void"; | |
6fb70fb7 JS |
9779 | for (unsigned j = 0; j < p->args.size(); ++j) |
9780 | { | |
9781 | if (j > 0) | |
9782 | s.op->line() << ", "; | |
9783 | s.op->line() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
9784 | } | |
9785 | s.op->line() << ") {"; | |
9786 | s.op->indent(1); | |
c12d974f FCE |
9787 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", |
9788 | lex_cast_qstring (*p->sole_location())); | |
f8a968bc | 9789 | s.op->newline() << "c->marker_name = " |
c12d974f FCE |
9790 | << lex_cast_qstring (p->tracepoint_name) |
9791 | << ";"; | |
f8a968bc JS |
9792 | for (unsigned j = 0; j < p->args.size(); ++j) |
9793 | if (p->args[j].used) | |
9794 | { | |
9795 | s.op->newline() << "c->locals[0]." << p->name << ".__tracepoint_arg_" | |
9796 | << p->args[j].name << " = (int64_t)"; | |
ad370dcc | 9797 | s.op->line() << p->args[j].typecast; |
f8a968bc JS |
9798 | s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";"; |
9799 | } | |
6fb70fb7 JS |
9800 | s.op->newline() << p->name << " (c);"; |
9801 | common_probe_entryfn_epilogue (s.op); | |
9802 | s.op->newline(-1) << "}"; | |
96b030fe JS |
9803 | |
9804 | // emit normalized registration functions | |
9805 | s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {"; | |
9806 | s.op->newline(1) << "return register_trace_" << p->tracepoint_name | |
9807 | << "(enter_tracepoint_probe_" << i << ");"; | |
9808 | s.op->newline(-1) << "}"; | |
86758d5f JS |
9809 | |
9810 | // NB: we're not prepared to deal with unreg failures. However, failures | |
9811 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
9812 | // weren't even registered. The former should be OKed by the initial | |
9813 | // registration call, and the latter is safe to ignore. | |
9814 | s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {"; | |
9815 | s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name | |
96b030fe JS |
9816 | << "(enter_tracepoint_probe_" << i << ");"; |
9817 | s.op->newline(-1) << "}"; | |
6fb70fb7 JS |
9818 | s.op->newline(); |
9819 | } | |
96b030fe JS |
9820 | |
9821 | // emit an array of registration functions for easy init/shutdown | |
9822 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
9823 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 9824 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
9825 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
9826 | s.op->indent(1); | |
9827 | for (unsigned i = 0; i < probes.size(); ++i) | |
9828 | { | |
9829 | s.op->newline () << "{"; | |
9830 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
9831 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
9832 | s.op->line() << " },"; | |
9833 | } | |
9834 | s.op->newline(-1) << "};"; | |
9835 | s.op->newline(); | |
79189b84 JS |
9836 | } |
9837 | ||
9838 | ||
9839 | void | |
9840 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
9841 | { | |
9842 | if (probes.size () == 0) | |
9843 | return; | |
9844 | ||
9845 | s.op->newline() << "/* init tracepoint probes */"; | |
96b030fe JS |
9846 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
9847 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
9848 | s.op->newline() << "if (rc) {"; | |
9849 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
9850 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
9851 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
9852 | s.op->newline(-1) << "}"; | |
9853 | s.op->newline(-1) << "}"; | |
bc9a523d FCE |
9854 | |
9855 | // This would be technically proper (on those autoconf-detectable | |
9856 | // kernels that include this function in tracepoint.h), however we | |
9857 | // already make several calls to synchronze_sched() during our | |
9858 | // shutdown processes. | |
9859 | ||
9860 | // s.op->newline() << "if (rc)"; | |
9861 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
9862 | // s.op->indent(-1); | |
79189b84 JS |
9863 | } |
9864 | ||
9865 | ||
9866 | void | |
9867 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
9868 | { | |
9869 | if (probes.empty()) | |
9870 | return; | |
9871 | ||
96b030fe JS |
9872 | s.op->newline() << "/* deregister tracepoint probes */"; |
9873 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
9874 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
9875 | s.op->indent(-1); | |
bc9a523d FCE |
9876 | |
9877 | // Not necessary: see above. | |
9878 | ||
9879 | // s.op->newline() << "tracepoint_synchronize_unregister();"; | |
79189b84 JS |
9880 | } |
9881 | ||
9882 | ||
75ead1f7 JS |
9883 | struct tracepoint_query : public base_query |
9884 | { | |
9885 | tracepoint_query(dwflpp & dw, const string & tracepoint, | |
9886 | probe * base_probe, probe_point * base_loc, | |
9887 | vector<derived_probe *> & results): | |
9888 | base_query(dw, "*"), tracepoint(tracepoint), | |
9889 | base_probe(base_probe), base_loc(base_loc), | |
9890 | results(results) {} | |
9891 | ||
9892 | const string& tracepoint; | |
9893 | ||
9894 | probe * base_probe; | |
9895 | probe_point * base_loc; | |
9896 | vector<derived_probe *> & results; | |
9897 | ||
9898 | void handle_query_module(); | |
9899 | int handle_query_cu(Dwarf_Die * cudie); | |
9900 | int handle_query_func(Dwarf_Die * func); | |
9901 | ||
9902 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); | |
9903 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
9904 | }; | |
9905 | ||
9906 | ||
9907 | void | |
9908 | tracepoint_query::handle_query_module() | |
9909 | { | |
9910 | // look for the tracepoints in each CU | |
9911 | dw.iterate_over_cus(tracepoint_query_cu, this); | |
9912 | } | |
9913 | ||
9914 | ||
9915 | int | |
9916 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
9917 | { | |
9918 | dw.focus_on_cu (cudie); | |
9919 | ||
9920 | // look at each function to see if it's a tracepoint | |
9921 | string function = "stapprobe_" + tracepoint; | |
9922 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
9923 | } | |
9924 | ||
9925 | ||
9926 | int | |
9927 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
9928 | { | |
9929 | dw.focus_on_function (func); | |
9930 | ||
9931 | assert(dw.function_name.compare(0, 10, "stapprobe_") == 0); | |
9932 | string tracepoint_instance = dw.function_name.substr(10); | |
79189b84 JS |
9933 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
9934 | tracepoint_instance, | |
9935 | base_probe, base_loc); | |
9936 | results.push_back (dp); | |
75ead1f7 JS |
9937 | return DWARF_CB_OK; |
9938 | } | |
9939 | ||
9940 | ||
9941 | int | |
9942 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
9943 | { | |
9944 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); | |
9945 | if (pending_interrupts) return DWARF_CB_ABORT; | |
9946 | return q->handle_query_cu(cudie); | |
9947 | } | |
9948 | ||
9949 | ||
9950 | int | |
9951 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
9952 | { | |
9953 | tracepoint_query * q = static_cast<tracepoint_query *>(query); | |
9954 | if (pending_interrupts) return DWARF_CB_ABORT; | |
9955 | return q->handle_query_func(func); | |
9956 | } | |
9957 | ||
9958 | ||
0a6f5a3f JS |
9959 | struct tracepoint_builder: public derived_probe_builder |
9960 | { | |
9961 | private: | |
9962 | dwflpp *dw; | |
9963 | bool init_dw(systemtap_session& s); | |
9964 | ||
9965 | public: | |
9966 | tracepoint_builder(): dw(0) {} | |
9967 | ~tracepoint_builder() { delete dw; } | |
9968 | ||
9969 | void build_no_more (systemtap_session& s) | |
9970 | { | |
9971 | if (dw && s.verbose > 3) | |
9972 | clog << "tracepoint_builder releasing dwflpp" << endl; | |
9973 | delete dw; | |
9974 | dw = NULL; | |
9975 | } | |
9976 | ||
9977 | void build(systemtap_session& s, | |
9978 | probe *base, probe_point *location, | |
9979 | literal_map_t const& parameters, | |
9980 | vector<derived_probe*>& finished_results); | |
9981 | }; | |
9982 | ||
9983 | ||
9984 | bool | |
9985 | tracepoint_builder::init_dw(systemtap_session& s) | |
9986 | { | |
9987 | if (dw != NULL) | |
9988 | return true; | |
9989 | ||
b278033a JS |
9990 | if (s.use_cache) |
9991 | { | |
9992 | // see if the cached module exists | |
9993 | find_tracequery_hash(s); | |
9994 | if (!s.tracequery_path.empty()) | |
9995 | { | |
9996 | int fd = open(s.tracequery_path.c_str(), O_RDONLY); | |
9997 | if (fd != -1) | |
9998 | { | |
9999 | if (s.verbose > 2) | |
10000 | clog << "Pass 2: using cached " << s.tracequery_path << endl; | |
10001 | ||
10002 | dw = new dwflpp(s); | |
10003 | dw->setup_user(s.tracequery_path); | |
10004 | close(fd); | |
10005 | return true; | |
10006 | } | |
10007 | } | |
10008 | } | |
10009 | ||
10010 | // no cached module, time to make it | |
0a6f5a3f JS |
10011 | string tracequery_ko; |
10012 | int rc = make_tracequery(s, tracequery_ko); | |
10013 | if (rc != 0) | |
10014 | return false; | |
10015 | ||
b278033a JS |
10016 | if (s.use_cache) |
10017 | { | |
10018 | // try to save tracequery in the cache | |
10019 | if (s.verbose > 2) | |
10020 | clog << "Copying " << tracequery_ko | |
10021 | << " to " << s.tracequery_path << endl; | |
10022 | if (copy_file(tracequery_ko.c_str(), | |
10023 | s.tracequery_path.c_str()) != 0) | |
10024 | cerr << "Copy failed (\"" << tracequery_ko << "\" to \"" | |
10025 | << s.tracequery_path << "\"): " << strerror(errno) << endl; | |
10026 | } | |
0a6f5a3f JS |
10027 | |
10028 | dw = new dwflpp(s); | |
10029 | dw->setup_user(tracequery_ko); | |
10030 | return true; | |
10031 | } | |
10032 | ||
10033 | ||
10034 | void | |
10035 | tracepoint_builder::build(systemtap_session& s, | |
10036 | probe *base, probe_point *location, | |
10037 | literal_map_t const& parameters, | |
10038 | vector<derived_probe*>& finished_results) | |
10039 | { | |
10040 | if (!init_dw(s)) | |
10041 | return; | |
10042 | ||
75ead1f7 JS |
10043 | string tracepoint; |
10044 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
10045 | ||
10046 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); | |
10047 | dw->query_modules(&q); | |
0a6f5a3f JS |
10048 | } |
10049 | ||
10050 | ||
10051 | ||
56894e91 JS |
10052 | // ------------------------------------------------------------------------ |
10053 | // hrtimer derived probes | |
10054 | // ------------------------------------------------------------------------ | |
10055 | // This is a new timer interface that provides more flexibility in specifying | |
10056 | // intervals, and uses the hrtimer APIs when available for greater precision. | |
39014506 JS |
10057 | // While hrtimers were added in 2.6.16, the API's weren't exported until |
10058 | // 2.6.17, so we must check this kernel version before attempting to use | |
10059 | // hrtimers. | |
56894e91 | 10060 | // |
56894e91 | 10061 | // * hrtimer_derived_probe: creates a probe point based on the hrtimer APIs. |
56894e91 JS |
10062 | |
10063 | ||
10064 | struct hrtimer_derived_probe: public derived_probe | |
10065 | { | |
10066 | // set a (generous) maximum of one day in ns | |
10067 | static const int64_t max_ns_interval = 1000000000LL * 60LL * 60LL * 24LL; | |
10068 | ||
10069 | // 100us seems like a reasonable minimum | |
10070 | static const int64_t min_ns_interval = 100000LL; | |
10071 | ||
10072 | int64_t interval, randomize; | |
10073 | ||
10074 | hrtimer_derived_probe (probe* p, probe_point* l, int64_t i, int64_t r): | |
10075 | derived_probe (p, l), interval (i), randomize (r) | |
10076 | { | |
10077 | if ((i < min_ns_interval) || (i > max_ns_interval)) | |
10078 | throw semantic_error("interval value out of range"); | |
10079 | ||
10080 | // randomize = 0 means no randomization | |
10081 | if ((r < 0) || (r > i)) | |
10082 | throw semantic_error("randomization value out of range"); | |
56894e91 JS |
10083 | } |
10084 | ||
b20febf3 FCE |
10085 | void join_group (systemtap_session& s); |
10086 | }; | |
dc38c0ae | 10087 | |
56894e91 | 10088 | |
b20febf3 FCE |
10089 | struct hrtimer_derived_probe_group: public generic_dpg<hrtimer_derived_probe> |
10090 | { | |
10091 | void emit_interval (translator_output* o); | |
10092 | public: | |
10093 | void emit_module_decls (systemtap_session& s); | |
10094 | void emit_module_init (systemtap_session& s); | |
10095 | void emit_module_exit (systemtap_session& s); | |
56894e91 JS |
10096 | }; |
10097 | ||
10098 | ||
dc38c0ae | 10099 | void |
b20febf3 | 10100 | hrtimer_derived_probe::join_group (systemtap_session& s) |
dc38c0ae | 10101 | { |
b20febf3 FCE |
10102 | if (! s.hrtimer_derived_probes) |
10103 | s.hrtimer_derived_probes = new hrtimer_derived_probe_group (); | |
10104 | s.hrtimer_derived_probes->enroll (this); | |
dc38c0ae DS |
10105 | } |
10106 | ||
10107 | ||
56894e91 | 10108 | void |
b20febf3 | 10109 | hrtimer_derived_probe_group::emit_interval (translator_output* o) |
56894e91 JS |
10110 | { |
10111 | o->line() << "({"; | |
ffb0b3ad | 10112 | o->newline(1) << "unsigned long nsecs;"; |
b20febf3 FCE |
10113 | o->newline() << "int64_t i = stp->intrv;"; |
10114 | o->newline() << "if (stp->rnd != 0) {"; | |
10115 | // XXX: why not use stp_random_pm instead of this? | |
10116 | o->newline(1) << "int64_t r;"; | |
10117 | o->newline() << "get_random_bytes(&r, sizeof(r));"; | |
10118 | // ensure that r is positive | |
10119 | o->newline() << "r &= ((uint64_t)1 << (8*sizeof(r) - 1)) - 1;"; | |
10120 | o->newline() << "r = _stp_mod64(NULL, r, (2*stp->rnd+1));"; | |
10121 | o->newline() << "r -= stp->rnd;"; | |
10122 | o->newline() << "i += r;"; | |
10123 | o->newline(-1) << "}"; | |
10124 | o->newline() << "if (unlikely(i < stap_hrtimer_resolution))"; | |
10125 | o->newline(1) << "i = stap_hrtimer_resolution;"; | |
197a4d62 | 10126 | o->indent(-1); |
ffb0b3ad JS |
10127 | o->newline() << "nsecs = do_div(i, NSEC_PER_SEC);"; |
10128 | o->newline() << "ktime_set(i, nsecs);"; | |
56894e91 JS |
10129 | o->newline(-1) << "})"; |
10130 | } | |
10131 | ||
10132 | ||
10133 | void | |
b20febf3 | 10134 | hrtimer_derived_probe_group::emit_module_decls (systemtap_session& s) |
46b84a80 | 10135 | { |
b20febf3 | 10136 | if (probes.empty()) return; |
46b84a80 | 10137 | |
b20febf3 | 10138 | s.op->newline() << "/* ---- hrtimer probes ---- */"; |
46b84a80 | 10139 | |
4c2732a1 JS |
10140 | s.op->newline() << "static unsigned long stap_hrtimer_resolution;"; // init later |
10141 | s.op->newline() << "static struct stap_hrtimer_probe {"; | |
b20febf3 FCE |
10142 | s.op->newline(1) << "struct hrtimer hrtimer;"; |
10143 | s.op->newline() << "const char *pp;"; | |
10144 | s.op->newline() << "void (*ph) (struct context*);"; | |
10145 | s.op->newline() << "int64_t intrv, rnd;"; | |
10146 | s.op->newline(-1) << "} stap_hrtimer_probes [" << probes.size() << "] = {"; | |
10147 | s.op->indent(1); | |
10148 | for (unsigned i=0; i < probes.size(); i++) | |
10149 | { | |
4baf0e53 | 10150 | s.op->newline () << "{"; |
b20febf3 FCE |
10151 | s.op->line() << " .pp=" << lex_cast_qstring (*probes[i]->sole_location()) << ","; |
10152 | s.op->line() << " .ph=&" << probes[i]->name << ","; | |
10153 | s.op->line() << " .intrv=" << probes[i]->interval << "LL,"; | |
10154 | s.op->line() << " .rnd=" << probes[i]->randomize << "LL"; | |
10155 | s.op->line() << " },"; | |
10156 | } | |
10157 | s.op->newline(-1) << "};"; | |
10158 | s.op->newline(); | |
10159 | ||
d9d9f852 JS |
10160 | // autoconf: add get/set expires if missing (pre 2.6.28-rc1) |
10161 | s.op->newline() << "#ifndef STAPCONF_HRTIMER_GETSET_EXPIRES"; | |
10162 | s.op->newline() << "#define hrtimer_get_expires(timer) ((timer)->expires)"; | |
10163 | s.op->newline() << "#define hrtimer_set_expires(timer, time) (void)((timer)->expires = (time))"; | |
10164 | s.op->newline() << "#endif"; | |
10165 | ||
255e4c68 FCE |
10166 | // autoconf: adapt to HRTIMER_REL -> HRTIMER_MODE_REL renaming near 2.6.21 |
10167 | s.op->newline() << "#ifdef STAPCONF_HRTIMER_REL"; | |
10168 | s.op->newline() << "#define HRTIMER_MODE_REL HRTIMER_REL"; | |
10169 | s.op->newline() << "#endif"; | |
4baf0e53 | 10170 | |
5dbd55d7 | 10171 | // The function signature changed in 2.6.21. |
255e4c68 FCE |
10172 | s.op->newline() << "#ifdef STAPCONF_HRTIMER_REL"; |
10173 | s.op->newline() << "static int "; | |
10174 | s.op->newline() << "#else"; | |
10175 | s.op->newline() << "static enum hrtimer_restart "; | |
10176 | s.op->newline() << "#endif"; | |
10177 | s.op->newline() << "enter_hrtimer_probe (struct hrtimer *timer) {"; | |
5dbd55d7 | 10178 | |
4baf0e53 | 10179 | s.op->newline(1) << "int rc = HRTIMER_NORESTART;"; |
e0d86324 JS |
10180 | s.op->newline() << "struct stap_hrtimer_probe *stp = container_of(timer, struct stap_hrtimer_probe, hrtimer);"; |
10181 | s.op->newline() << "if ((atomic_read (&session_state) == STAP_SESSION_STARTING) ||"; | |
10182 | s.op->newline() << " (atomic_read (&session_state) == STAP_SESSION_RUNNING)) {"; | |
b20febf3 | 10183 | // Compute next trigger time |
d9d9f852 | 10184 | s.op->newline(1) << "hrtimer_set_expires(timer, ktime_add (hrtimer_get_expires(timer),"; |
b20febf3 | 10185 | emit_interval (s.op); |
f7674e54 | 10186 | s.op->line() << "));"; |
d9d9f852 | 10187 | s.op->newline() << "rc = HRTIMER_RESTART;"; |
e0d86324 JS |
10188 | s.op->newline(-1) << "}"; |
10189 | s.op->newline() << "{"; | |
10190 | s.op->indent(1); | |
c12d974f | 10191 | common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "stp->pp"); |
b20febf3 FCE |
10192 | s.op->newline() << "(*stp->ph) (c);"; |
10193 | common_probe_entryfn_epilogue (s.op); | |
e0d86324 JS |
10194 | s.op->newline(-1) << "}"; |
10195 | s.op->newline() << "return rc;"; | |
b20febf3 | 10196 | s.op->newline(-1) << "}"; |
56894e91 JS |
10197 | } |
10198 | ||
10199 | ||
10200 | void | |
b20febf3 | 10201 | hrtimer_derived_probe_group::emit_module_init (systemtap_session& s) |
56894e91 | 10202 | { |
b20febf3 | 10203 | if (probes.empty()) return; |
56894e91 | 10204 | |
b20febf3 FCE |
10205 | s.op->newline() << "{"; |
10206 | s.op->newline(1) << "struct timespec res;"; | |
10207 | s.op->newline() << "hrtimer_get_res (CLOCK_MONOTONIC, &res);"; | |
10208 | s.op->newline() << "stap_hrtimer_resolution = timespec_to_ns (&res);"; | |
10209 | s.op->newline(-1) << "}"; | |
a68f81a2 | 10210 | |
b20febf3 FCE |
10211 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
10212 | s.op->newline(1) << "struct stap_hrtimer_probe* stp = & stap_hrtimer_probes [i];"; | |
6f313a73 | 10213 | s.op->newline() << "probe_point = stp->pp;"; |
255e4c68 | 10214 | s.op->newline() << "hrtimer_init (& stp->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);"; |
b20febf3 FCE |
10215 | s.op->newline() << "stp->hrtimer.function = & enter_hrtimer_probe;"; |
10216 | // There is no hrtimer field to identify *this* (i-th) probe handler | |
10217 | // callback. So instead we'll deduce it at entry time. | |
10218 | s.op->newline() << "(void) hrtimer_start (& stp->hrtimer, "; | |
10219 | emit_interval (s.op); | |
255e4c68 | 10220 | s.op->line() << ", HRTIMER_MODE_REL);"; |
b20febf3 FCE |
10221 | // Note: no partial failure rollback is needed: hrtimer_start only |
10222 | // "fails" if the timer was already active, which cannot be. | |
10223 | s.op->newline(-1) << "}"; // for loop | |
56894e91 JS |
10224 | } |
10225 | ||
10226 | ||
dc38c0ae | 10227 | void |
b20febf3 | 10228 | hrtimer_derived_probe_group::emit_module_exit (systemtap_session& s) |
dc38c0ae | 10229 | { |
b20febf3 | 10230 | if (probes.empty()) return; |
197a4d62 | 10231 | |
b20febf3 FCE |
10232 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; |
10233 | s.op->newline(1) << "hrtimer_cancel (& stap_hrtimer_probes[i].hrtimer);"; | |
10234 | s.op->indent(-1); | |
dc38c0ae DS |
10235 | } |
10236 | ||
10237 | ||
56894e91 | 10238 | |
197a4d62 JS |
10239 | struct timer_builder: public derived_probe_builder |
10240 | { | |
10241 | virtual void build(systemtap_session & sess, | |
10242 | probe * base, probe_point * location, | |
86bf665e | 10243 | literal_map_t const & parameters, |
197a4d62 | 10244 | vector<derived_probe *> & finished_results); |
e38d6504 | 10245 | |
c4ce66a1 | 10246 | static void register_patterns(systemtap_session& s); |
56894e91 JS |
10247 | }; |
10248 | ||
197a4d62 JS |
10249 | void |
10250 | timer_builder::build(systemtap_session & sess, | |
10251 | probe * base, | |
10252 | probe_point * location, | |
86bf665e | 10253 | literal_map_t const & parameters, |
197a4d62 | 10254 | vector<derived_probe *> & finished_results) |
56894e91 | 10255 | { |
197a4d62 | 10256 | int64_t period, rand=0; |
56894e91 | 10257 | |
197a4d62 JS |
10258 | if (!get_param(parameters, "randomize", rand)) |
10259 | rand = 0; | |
56894e91 | 10260 | |
197a4d62 | 10261 | if (get_param(parameters, "jiffies", period)) |
56894e91 | 10262 | { |
197a4d62 JS |
10263 | // always use basic timers for jiffies |
10264 | finished_results.push_back( | |
10265 | new timer_derived_probe(base, location, period, rand, false)); | |
10266 | return; | |
56894e91 | 10267 | } |
197a4d62 | 10268 | else if (get_param(parameters, "hz", period)) |
56894e91 | 10269 | { |
197a4d62 JS |
10270 | if (period <= 0) |
10271 | throw semantic_error ("frequency must be greater than 0"); | |
10272 | period = (1000000000 + period - 1)/period; | |
10273 | } | |
10274 | else if (get_param(parameters, "s", period) | |
10275 | || get_param(parameters, "sec", period)) | |
10276 | { | |
10277 | period *= 1000000000; | |
10278 | rand *= 1000000000; | |
10279 | } | |
10280 | else if (get_param(parameters, "ms", period) | |
10281 | || get_param(parameters, "msec", period)) | |
10282 | { | |
10283 | period *= 1000000; | |
10284 | rand *= 1000000; | |
10285 | } | |
10286 | else if (get_param(parameters, "us", period) | |
10287 | || get_param(parameters, "usec", period)) | |
10288 | { | |
10289 | period *= 1000; | |
10290 | rand *= 1000; | |
10291 | } | |
10292 | else if (get_param(parameters, "ns", period) | |
10293 | || get_param(parameters, "nsec", period)) | |
10294 | { | |
10295 | // ok | |
10296 | } | |
10297 | else | |
10298 | throw semantic_error ("unrecognized timer variant"); | |
56894e91 | 10299 | |
b20febf3 FCE |
10300 | // Redirect wallclock-time based probes to hrtimer code on recent |
10301 | // enough kernels. | |
197a4d62 JS |
10302 | if (strverscmp(sess.kernel_base_release.c_str(), "2.6.17") < 0) |
10303 | { | |
10304 | // hrtimers didn't exist, so use the old-school timers | |
10305 | period = (period + 1000000 - 1)/1000000; | |
10306 | rand = (rand + 1000000 - 1)/1000000; | |
56894e91 | 10307 | |
197a4d62 JS |
10308 | finished_results.push_back( |
10309 | new timer_derived_probe(base, location, period, rand, true)); | |
10310 | } | |
10311 | else | |
10312 | finished_results.push_back( | |
10313 | new hrtimer_derived_probe(base, location, period, rand)); | |
10314 | } | |
56894e91 | 10315 | |
197a4d62 | 10316 | void |
c4ce66a1 | 10317 | timer_builder::register_patterns(systemtap_session& s) |
197a4d62 | 10318 | { |
c4ce66a1 | 10319 | match_node* root = s.pattern_root; |
197a4d62 | 10320 | derived_probe_builder *builder = new timer_builder(); |
56894e91 | 10321 | |
12b21830 | 10322 | root = root->bind(TOK_TIMER); |
56894e91 | 10323 | |
197a4d62 JS |
10324 | root->bind_num("s")->bind(builder); |
10325 | root->bind_num("s")->bind_num("randomize")->bind(builder); | |
10326 | root->bind_num("sec")->bind(builder); | |
10327 | root->bind_num("sec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10328 | |
197a4d62 JS |
10329 | root->bind_num("ms")->bind(builder); |
10330 | root->bind_num("ms")->bind_num("randomize")->bind(builder); | |
10331 | root->bind_num("msec")->bind(builder); | |
10332 | root->bind_num("msec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10333 | |
197a4d62 JS |
10334 | root->bind_num("us")->bind(builder); |
10335 | root->bind_num("us")->bind_num("randomize")->bind(builder); | |
10336 | root->bind_num("usec")->bind(builder); | |
10337 | root->bind_num("usec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10338 | |
197a4d62 JS |
10339 | root->bind_num("ns")->bind(builder); |
10340 | root->bind_num("ns")->bind_num("randomize")->bind(builder); | |
10341 | root->bind_num("nsec")->bind(builder); | |
10342 | root->bind_num("nsec")->bind_num("randomize")->bind(builder); | |
56894e91 | 10343 | |
197a4d62 JS |
10344 | root->bind_num("jiffies")->bind(builder); |
10345 | root->bind_num("jiffies")->bind_num("randomize")->bind(builder); | |
4baf0e53 | 10346 | |
197a4d62 | 10347 | root->bind_num("hz")->bind(builder); |
56894e91 JS |
10348 | } |
10349 | ||
10350 | ||
342d3f96 | 10351 | |
47dd066d WC |
10352 | // ------------------------------------------------------------------------ |
10353 | // perfmon derived probes | |
10354 | // ------------------------------------------------------------------------ | |
10355 | // This is a new interface to the perfmon hw. | |
10356 | // | |
10357 | ||
10358 | ||
de688825 | 10359 | struct perfmon_var_expanding_visitor: public var_expanding_visitor |
47dd066d WC |
10360 | { |
10361 | systemtap_session & sess; | |
10362 | unsigned counter_number; | |
de688825 | 10363 | perfmon_var_expanding_visitor(systemtap_session & s, unsigned c): |
47dd066d WC |
10364 | sess(s), counter_number(c) {} |
10365 | void visit_target_symbol (target_symbol* e); | |
10366 | }; | |
10367 | ||
10368 | ||
10369 | void | |
de688825 | 10370 | perfmon_var_expanding_visitor::visit_target_symbol (target_symbol *e) |
47dd066d WC |
10371 | { |
10372 | assert(e->base_name.size() > 0 && e->base_name[0] == '$'); | |
10373 | ||
10374 | // Synthesize a function. | |
10375 | functiondecl *fdecl = new functiondecl; | |
10376 | fdecl->tok = e->tok; | |
10377 | embeddedcode *ec = new embeddedcode; | |
10378 | ec->tok = e->tok; | |
10379 | bool lvalue = is_active_lvalue(e); | |
10380 | ||
10381 | if (lvalue ) | |
10382 | throw semantic_error("writes to $counter not permitted"); | |
10383 | ||
10384 | string fname = string("_perfmon_tvar_get") | |
10385 | + "_" + e->base_name.substr(1) | |
10386 | + "_" + lex_cast<string>(counter_number); | |
10387 | ||
10388 | if (e->base_name != "$counter") | |
10389 | throw semantic_error ("target variables not available to perfmon probes"); | |
10390 | ||
af304783 DS |
10391 | if (e->components.size() > 0) |
10392 | { | |
10393 | switch (e->components[0].first) | |
10394 | { | |
10395 | case target_symbol::comp_literal_array_index: | |
10396 | throw semantic_error("perfmon probe '$counter' variable may not be used as array", | |
10397 | e->tok); | |
10398 | break; | |
10399 | case target_symbol::comp_struct_member: | |
10400 | throw semantic_error("perfmon probe '$counter' variable may not be used as a structure", | |
10401 | e->tok); | |
10402 | break; | |
10403 | default: | |
10404 | throw semantic_error ("invalid use of perfmon probe '$counter' variable", | |
10405 | e->tok); | |
10406 | break; | |
10407 | } | |
10408 | } | |
10409 | ||
4baf0e53 | 10410 | ec->code = "THIS->__retvalue = _pfm_pmd_x[" + |
47dd066d WC |
10411 | lex_cast<string>(counter_number) + "].reg_num;"; |
10412 | ec->code += "/* pure */"; | |
10413 | fdecl->name = fname; | |
10414 | fdecl->body = ec; | |
10415 | fdecl->type = pe_long; | |
f76427a2 | 10416 | sess.functions[fdecl->name]=fdecl; |
47dd066d WC |
10417 | |
10418 | // Synthesize a functioncall. | |
10419 | functioncall* n = new functioncall; | |
10420 | n->tok = e->tok; | |
10421 | n->function = fname; | |
10422 | n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session | |
10423 | ||
4ed05b15 | 10424 | provide (n); |
47dd066d WC |
10425 | } |
10426 | ||
10427 | ||
10428 | enum perfmon_mode | |
10429 | { | |
10430 | perfmon_count, | |
10431 | perfmon_sample | |
10432 | }; | |
10433 | ||
10434 | ||
10435 | struct perfmon_derived_probe: public derived_probe | |
10436 | { | |
10437 | protected: | |
10438 | static unsigned probes_allocated; | |
10439 | ||
10440 | public: | |
10441 | systemtap_session & sess; | |
10442 | string event; | |
10443 | perfmon_mode mode; | |
10444 | ||
10445 | perfmon_derived_probe (probe* p, probe_point* l, systemtap_session &s, | |
10446 | string e, perfmon_mode m); | |
b20febf3 | 10447 | virtual void join_group (systemtap_session& s); |
47dd066d WC |
10448 | }; |
10449 | ||
10450 | ||
b20febf3 | 10451 | struct perfmon_derived_probe_group: public generic_dpg<perfmon_derived_probe> |
47dd066d | 10452 | { |
47dd066d | 10453 | public: |
78f6bba6 FCE |
10454 | void emit_module_decls (systemtap_session&) {} |
10455 | void emit_module_init (systemtap_session&) {} | |
10456 | void emit_module_exit (systemtap_session&) {} | |
47dd066d WC |
10457 | }; |
10458 | ||
10459 | ||
10460 | struct perfmon_builder: public derived_probe_builder | |
10461 | { | |
10462 | perfmon_builder() {} | |
10463 | virtual void build(systemtap_session & sess, | |
10464 | probe * base, | |
10465 | probe_point * location, | |
86bf665e | 10466 | literal_map_t const & parameters, |
47dd066d WC |
10467 | vector<derived_probe *> & finished_results) |
10468 | { | |
10469 | string event; | |
10470 | if (!get_param (parameters, "counter", event)) | |
10471 | throw semantic_error("perfmon requires an event"); | |
10472 | ||
10473 | sess.perfmon++; | |
10474 | ||
4baf0e53 | 10475 | // XXX: need to revise when doing sampling |
47dd066d WC |
10476 | finished_results.push_back(new perfmon_derived_probe(base, location, |
10477 | sess, event, | |
10478 | perfmon_count)); | |
10479 | } | |
10480 | }; | |
10481 | ||
b20febf3 | 10482 | |
47dd066d WC |
10483 | unsigned perfmon_derived_probe::probes_allocated; |
10484 | ||
10485 | perfmon_derived_probe::perfmon_derived_probe (probe* p, probe_point* l, | |
10486 | systemtap_session &s, | |
10487 | string e, perfmon_mode m) | |
10488 | : derived_probe (p, l), sess(s), event(e), mode(m) | |
10489 | { | |
10490 | ++probes_allocated; | |
10491 | ||
de688825 JS |
10492 | // Now expand the local variables in the probe body |
10493 | perfmon_var_expanding_visitor v (sess, probes_allocated-1); | |
10494 | this->body = v.require (this->body); | |
47dd066d WC |
10495 | |
10496 | if (sess.verbose > 1) | |
10497 | clog << "perfmon-based probe" << endl; | |
10498 | } | |
10499 | ||
10500 | ||
10501 | void | |
b20febf3 | 10502 | perfmon_derived_probe::join_group (systemtap_session& s) |
47dd066d | 10503 | { |
b20febf3 FCE |
10504 | throw semantic_error ("incomplete", this->tok); |
10505 | ||
10506 | if (! s.perfmon_derived_probes) | |
10507 | s.perfmon_derived_probes = new perfmon_derived_probe_group (); | |
10508 | s.perfmon_derived_probes->enroll (this); | |
47dd066d WC |
10509 | } |
10510 | ||
10511 | ||
b20febf3 | 10512 | #if 0 |
47dd066d WC |
10513 | void |
10514 | perfmon_derived_probe::emit_registrations_start (translator_output* o, | |
10515 | unsigned index) | |
10516 | { | |
10517 | for (unsigned i=0; i<locations.size(); i++) | |
10518 | o->newline() << "enter_" << name << "_" << i << " ();"; | |
10519 | } | |
10520 | ||
10521 | ||
10522 | void | |
10523 | perfmon_derived_probe::emit_registrations_end (translator_output * o, | |
10524 | unsigned index) | |
10525 | { | |
10526 | } | |
10527 | ||
10528 | ||
10529 | void | |
10530 | perfmon_derived_probe::emit_deregistrations (translator_output * o) | |
10531 | { | |
10532 | } | |
10533 | ||
10534 | ||
10535 | void | |
10536 | perfmon_derived_probe::emit_probe_entries (translator_output * o) | |
10537 | { | |
10538 | o->newline() << "#ifdef STP_TIMING"; | |
dbb68664 | 10539 | // NB: This variable may be multiply (but identically) defined. |
47dd066d WC |
10540 | o->newline() << "static __cacheline_aligned Stat " << "time_" << basest()->name << ";"; |
10541 | o->newline() << "#endif"; | |
10542 | ||
10543 | for (unsigned i=0; i<locations.size(); i++) | |
10544 | { | |
10545 | probe_point *l = locations[i]; | |
10546 | o->newline() << "/* location " << i << ": " << *l << " */"; | |
10547 | o->newline() << "static void enter_" << name << "_" << i << " (void) {"; | |
10548 | ||
10549 | o->indent(1); | |
10550 | o->newline() << "const char* probe_point = " | |
10551 | << lex_cast_qstring(*l) << ";"; | |
10552 | emit_probe_prologue (o, | |
10553 | (mode == perfmon_count ? | |
10554 | "STAP_SESSION_STARTING" : | |
c12d974f FCE |
10555 | "STAP_SESSION_RUNNING"), |
10556 | "probe_point"); | |
47dd066d WC |
10557 | |
10558 | // NB: locals are initialized by probe function itself | |
10559 | o->newline() << name << " (c);"; | |
10560 | ||
10561 | emit_probe_epilogue (o); | |
10562 | ||
10563 | o->newline(-1) << "}\n"; | |
10564 | } | |
10565 | } | |
b20febf3 | 10566 | #endif |
47dd066d WC |
10567 | |
10568 | ||
b20febf3 | 10569 | #if 0 |
47dd066d WC |
10570 | void no_pfm_event_error (string s) |
10571 | { | |
10572 | string msg(string("Cannot find event:" + s)); | |
10573 | throw semantic_error(msg); | |
10574 | } | |
10575 | ||
10576 | ||
10577 | void no_pfm_mask_error (string s) | |
10578 | { | |
10579 | string msg(string("Cannot find mask:" + s)); | |
10580 | throw semantic_error(msg); | |
10581 | } | |
10582 | ||
10583 | ||
10584 | void | |
10585 | split(const string& s, vector<string>& v, const string & separator) | |
10586 | { | |
10587 | string::size_type last_pos = s.find_first_not_of(separator, 0); | |
10588 | string::size_type pos = s.find_first_of(separator, last_pos); | |
10589 | ||
10590 | while (string::npos != pos || string::npos != last_pos) { | |
10591 | v.push_back(s.substr(last_pos, pos - last_pos)); | |
10592 | last_pos = s.find_first_not_of(separator, pos); | |
10593 | pos = s.find_first_of(separator, last_pos); | |
10594 | } | |
10595 | } | |
10596 | ||
10597 | ||
10598 | void | |
10599 | perfmon_derived_probe_group::emit_probes (translator_output* op, unparser* up) | |
10600 | { | |
10601 | for (unsigned i=0; i < probes.size(); i++) | |
10602 | { | |
10603 | op->newline (); | |
10604 | up->emit_probe (probes[i]); | |
10605 | } | |
10606 | } | |
10607 | ||
10608 | ||
10609 | void | |
10610 | perfmon_derived_probe_group::emit_module_init (translator_output* o) | |
10611 | { | |
10612 | int ret; | |
10613 | pfmlib_input_param_t inp; | |
10614 | pfmlib_output_param_t outp; | |
10615 | pfarg_pmd_t pd[PFMLIB_MAX_PMDS]; | |
10616 | pfarg_pmc_t pc[PFMLIB_MAX_PMCS]; | |
10617 | pfarg_ctx_t ctx; | |
10618 | pfarg_load_t load_args; | |
10619 | pfmlib_options_t pfmlib_options; | |
10620 | unsigned int max_counters; | |
10621 | ||
10622 | if ( probes.size() == 0) | |
10623 | return; | |
10624 | ret = pfm_initialize(); | |
10625 | if (ret != PFMLIB_SUCCESS) | |
10626 | throw semantic_error("Unable to generate performance monitoring events (no libpfm)"); | |
10627 | ||
10628 | pfm_get_num_counters(&max_counters); | |
10629 | ||
10630 | memset(&pfmlib_options, 0, sizeof(pfmlib_options)); | |
10631 | pfmlib_options.pfm_debug = 0; /* set to 1 for debug */ | |
10632 | pfmlib_options.pfm_verbose = 0; /* set to 1 for debug */ | |
10633 | pfm_set_options(&pfmlib_options); | |
10634 | ||
10635 | memset(pd, 0, sizeof(pd)); | |
10636 | memset(pc, 0, sizeof(pc)); | |
10637 | memset(&ctx, 0, sizeof(ctx)); | |
10638 | memset(&load_args, 0, sizeof(load_args)); | |
10639 | ||
10640 | /* | |
10641 | * prepare parameters to library. | |
10642 | */ | |
10643 | memset(&inp,0, sizeof(inp)); | |
10644 | memset(&outp,0, sizeof(outp)); | |
10645 | ||
10646 | /* figure out the events */ | |
10647 | for (unsigned i=0; i<probes.size(); ++i) | |
10648 | { | |
10649 | if (probes[i]->event == "cycles") { | |
10650 | if (pfm_get_cycle_event( &inp.pfp_events[i].event) != PFMLIB_SUCCESS) | |
10651 | no_pfm_event_error(probes[i]->event); | |
10652 | } else if (probes[i]->event == "instructions") { | |
10653 | if (pfm_get_inst_retired_event( &inp.pfp_events[i].event) != | |
10654 | PFMLIB_SUCCESS) | |
10655 | no_pfm_event_error(probes[i]->event); | |
10656 | } else { | |
10657 | unsigned int event_id = 0; | |
10658 | unsigned int mask_id = 0; | |
10659 | vector<string> event_spec; | |
10660 | split(probes[i]->event, event_spec, ":"); | |
10661 | int num = event_spec.size(); | |
10662 | int masks = num - 1; | |
10663 | ||
10664 | if (num == 0) | |
10665 | throw semantic_error("No events found"); | |
10666 | ||
10667 | /* setup event */ | |
10668 | if (pfm_find_event(event_spec[0].c_str(), &event_id) != PFMLIB_SUCCESS) | |
10669 | no_pfm_event_error(event_spec[0]); | |
10670 | inp.pfp_events[i].event = event_id; | |
10671 | ||
10672 | /* set up masks */ | |
10673 | if (masks > PFMLIB_MAX_MASKS_PER_EVENT) | |
10674 | throw semantic_error("Too many unit masks specified"); | |
10675 | ||
10676 | for (int j=0; j < masks; j++) { | |
10677 | if (pfm_find_event_mask(event_id, event_spec[j+1].c_str(), | |
10678 | &mask_id) != PFMLIB_SUCCESS) | |
10679 | no_pfm_mask_error(string(event_spec[j+1])); | |
10680 | inp.pfp_events[i].unit_masks[j] = mask_id; | |
10681 | } | |
10682 | inp.pfp_events[i].num_masks = masks; | |
10683 | } | |
10684 | } | |
10685 | ||
10686 | /* number of counters in use */ | |
10687 | inp.pfp_event_count = probes.size(); | |
10688 | ||
10689 | // XXX: no elimination of duplicated counters | |
10690 | if (inp.pfp_event_count>max_counters) | |
10691 | throw semantic_error("Too many performance monitoring events."); | |
10692 | ||
10693 | /* count events both in kernel and user-space */ | |
10694 | inp.pfp_dfl_plm = PFM_PLM0 | PFM_PLM3; | |
10695 | ||
4baf0e53 | 10696 | /* XXX: some cases a perfmon register might be used of watch dog |
47dd066d WC |
10697 | this code doesn't handle that case */ |
10698 | ||
10699 | /* figure out the pmcs for the events */ | |
10700 | if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS) | |
10701 | throw semantic_error("Cannot configure events"); | |
10702 | ||
10703 | for (unsigned i=0; i < outp.pfp_pmc_count; i++) { | |
10704 | pc[i].reg_num = outp.pfp_pmcs[i].reg_num; | |
10705 | pc[i].reg_value = outp.pfp_pmcs[i].reg_value; | |
10706 | } | |
10707 | ||
10708 | /* | |
10709 | * There could be more pmc settings than pmd. | |
10710 | * Figure out the actual pmds to use. | |
10711 | */ | |
10712 | for (unsigned i=0, j=0; i < inp.pfp_event_count; i++) { | |
10713 | pd[i].reg_num = outp.pfp_pmcs[j].reg_pmd_num; | |
10714 | for(; j < outp.pfp_pmc_count; j++) | |
10715 | if (outp.pfp_pmcs[j].reg_evt_idx != i) break; | |
10716 | } | |
10717 | ||
10718 | // Output the be probes create function | |
10719 | o->newline() << "static int register_perfmon_probes (void) {"; | |
10720 | o->newline(1) << "int rc = 0;"; | |
10721 | ||
10722 | o->newline() << "/* data for perfmon */"; | |
10723 | o->newline() << "static int _pfm_num_pmc = " << outp.pfp_pmc_count << ";"; | |
10724 | o->newline() << "static struct pfarg_pmc _pfm_pmc[" << outp.pfp_pmc_count | |
10725 | << "] = {"; | |
10726 | /* output the needed bits for pmc here */ | |
10727 | for (unsigned i=0; i < outp.pfp_pmc_count; i++) { | |
10728 | o->newline() << "{.reg_num=" << pc[i].reg_num << ", " | |
10729 | << ".reg_value=" << lex_cast_hex<string>(pc[i].reg_value) | |
10730 | << "},"; | |
10731 | } | |
10732 | ||
10733 | o->newline() << "};"; | |
10734 | o->newline() << "static int _pfm_num_pmd = " << inp.pfp_event_count << ";"; | |
10735 | o->newline() << "static struct pfarg_pmd _pfm_pmd[" << inp.pfp_event_count | |
10736 | << "] = {"; | |
10737 | /* output the needed bits for pmd here */ | |
10738 | for (unsigned i=0; i < inp.pfp_event_count; i++) { | |
10739 | o->newline() << "{.reg_num=" << pd[i].reg_num << ", " | |
10740 | << ".reg_value=" << pd[i].reg_value << "},"; | |
10741 | } | |
10742 | o->newline() << "};"; | |
10743 | o->newline(); | |
10744 | ||
10745 | o->newline() << "_pfm_pmc_x=_pfm_pmc;"; | |
10746 | o->newline() << "_pfm_num_pmc_x=_pfm_num_pmc;"; | |
10747 | o->newline() << "_pfm_pmd_x=_pfm_pmd;"; | |
10748 | o->newline() << "_pfm_num_pmd_x=_pfm_num_pmd;"; | |
10749 | ||
10750 | // call all the function bodies associated with perfcounters | |
10751 | for (unsigned i=0; i < probes.size (); i++) | |
10752 | probes[i]->emit_registrations_start (o,i); | |
10753 | ||
10754 | /* generate call to turn on instrumentation */ | |
10755 | o->newline() << "_pfm_context.ctx_flags |= PFM_FL_SYSTEM_WIDE;"; | |
10756 | o->newline() << "rc = rc || _stp_perfmon_setup(&_pfm_desc, &_pfm_context,"; | |
10757 | o->newline(1) << "_pfm_pmc, _pfm_num_pmc,"; | |
10758 | o->newline() << "_pfm_pmd, _pfm_num_pmd);"; | |
10759 | o->newline(-1); | |
10760 | ||
10761 | o->newline() << "return rc;"; | |
10762 | o->newline(-1) << "}\n"; | |
10763 | ||
10764 | // Output the be probes destroy function | |
10765 | o->newline() << "static void unregister_perfmon_probes (void) {"; | |
10766 | o->newline(1) << "_stp_perfmon_shutdown(_pfm_desc);"; | |
10767 | o->newline(-1) << "}\n"; | |
10768 | } | |
b20febf3 | 10769 | #endif |
47dd066d | 10770 | |
47dd066d | 10771 | |
b55bc428 | 10772 | // ------------------------------------------------------------------------ |
bd2b1e68 | 10773 | // Standard tapset registry. |
b55bc428 FCE |
10774 | // ------------------------------------------------------------------------ |
10775 | ||
7a053d3b | 10776 | void |
f8220a7b | 10777 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 10778 | { |
12b21830 DS |
10779 | s.pattern_root->bind(TOK_BEGIN)->bind(new be_builder(BEGIN)); |
10780 | s.pattern_root->bind_num(TOK_BEGIN)->bind(new be_builder(BEGIN)); | |
10781 | s.pattern_root->bind(TOK_END)->bind(new be_builder(END)); | |
10782 | s.pattern_root->bind_num(TOK_END)->bind(new be_builder(END)); | |
10783 | s.pattern_root->bind(TOK_ERROR)->bind(new be_builder(ERROR)); | |
10784 | s.pattern_root->bind_num(TOK_ERROR)->bind(new be_builder(ERROR)); | |
16e8f21f | 10785 | |
12b21830 | 10786 | s.pattern_root->bind(TOK_NEVER)->bind(new never_builder()); |
6e3347a9 | 10787 | |
c4ce66a1 | 10788 | timer_builder::register_patterns(s); |
12b21830 DS |
10789 | s.pattern_root->bind(TOK_TIMER)->bind("profile")->bind(new profile_builder()); |
10790 | s.pattern_root->bind("perfmon")->bind_str("counter") | |
10791 | ->bind(new perfmon_builder()); | |
b98a8d73 | 10792 | |
7a24d422 | 10793 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 10794 | dwarf_derived_probe::register_patterns(s); |
30a279be | 10795 | |
888af770 FCE |
10796 | // XXX: user-space starter set |
10797 | s.pattern_root->bind_num(TOK_PROCESS) | |
10798 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
10799 | ->bind(new uprobe_builder ()); | |
10800 | s.pattern_root->bind_num(TOK_PROCESS) | |
10801 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
10802 | ->bind(new uprobe_builder ()); | |
10803 | ||
935447c8 | 10804 | // utrace user-space probes |
eff6ac72 DS |
10805 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_BEGIN) |
10806 | ->bind(new utrace_builder ()); | |
10807 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_BEGIN) | |
10808 | ->bind(new utrace_builder ()); | |
986e98de DS |
10809 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_BEGIN) |
10810 | ->bind(new utrace_builder ()); | |
eff6ac72 | 10811 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_END) |
935447c8 | 10812 | ->bind(new utrace_builder ()); |
eff6ac72 | 10813 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_END) |
935447c8 | 10814 | ->bind(new utrace_builder ()); |
986e98de DS |
10815 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_END) |
10816 | ->bind(new utrace_builder ()); | |
eff6ac72 | 10817 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
159cb109 | 10818 | ->bind(new utrace_builder ()); |
eff6ac72 DS |
10819 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
10820 | ->bind(new utrace_builder ()); | |
986e98de DS |
10821 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_BEGIN) |
10822 | ->bind(new utrace_builder ()); | |
eff6ac72 DS |
10823 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) |
10824 | ->bind(new utrace_builder ()); | |
10825 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) | |
159cb109 | 10826 | ->bind(new utrace_builder ()); |
986e98de DS |
10827 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_THREAD)->bind(TOK_END) |
10828 | ->bind(new utrace_builder ()); | |
12b21830 | 10829 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_SYSCALL) |
935447c8 | 10830 | ->bind(new utrace_builder ()); |
12b21830 | 10831 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_SYSCALL) |
935447c8 | 10832 | ->bind(new utrace_builder ()); |
480f38d3 DS |
10833 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_SYSCALL) |
10834 | ->bind(new utrace_builder ()); | |
12b21830 | 10835 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
935447c8 | 10836 | ->bind(new utrace_builder ()); |
12b21830 | 10837 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
935447c8 | 10838 | ->bind(new utrace_builder ()); |
480f38d3 DS |
10839 | s.pattern_root->bind(TOK_PROCESS)->bind(TOK_SYSCALL)->bind(TOK_RETURN) |
10840 | ->bind(new utrace_builder ()); | |
10841 | ||
10842 | // itrace user-space probes | |
0afb7073 FCE |
10843 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_INSN) |
10844 | ->bind(new itrace_builder ()); | |
10845 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_INSN) | |
10846 | ->bind(new itrace_builder ()); | |
10847 | s.pattern_root->bind_str(TOK_PROCESS)->bind(TOK_INSN)->bind(TOK_BLOCK) | |
480f38d3 | 10848 | ->bind(new itrace_builder ()); |
0afb7073 | 10849 | s.pattern_root->bind_num(TOK_PROCESS)->bind(TOK_INSN)->bind(TOK_BLOCK) |
480f38d3 | 10850 | ->bind(new itrace_builder ()); |
935447c8 | 10851 | |
f781f849 | 10852 | // marker-based parts |
12b21830 DS |
10853 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_MARK) |
10854 | ->bind(new mark_builder()); | |
10855 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_MARK)->bind_str(TOK_FORMAT) | |
eb973c2a | 10856 | ->bind(new mark_builder()); |
f781f849 | 10857 | |
0a6f5a3f JS |
10858 | // kernel tracepoint probes |
10859 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
10860 | ->bind(new tracepoint_builder()); | |
10861 | ||
ce82316f | 10862 | // procfs parts |
12b21830 DS |
10863 | s.pattern_root->bind(TOK_PROCFS)->bind(TOK_READ)->bind(new procfs_builder()); |
10864 | s.pattern_root->bind_str(TOK_PROCFS)->bind(TOK_READ) | |
10865 | ->bind(new procfs_builder()); | |
10866 | s.pattern_root->bind(TOK_PROCFS)->bind(TOK_WRITE)->bind(new procfs_builder()); | |
10867 | s.pattern_root->bind_str(TOK_PROCFS)->bind(TOK_WRITE) | |
10868 | ->bind(new procfs_builder()); | |
b55bc428 | 10869 | } |
dc38c0ae DS |
10870 | |
10871 | ||
b20febf3 FCE |
10872 | vector<derived_probe_group*> |
10873 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 10874 | { |
b20febf3 FCE |
10875 | vector<derived_probe_group*> g; |
10876 | #define DOONE(x) if (s. x##_derived_probes) g.push_back (s. x##_derived_probes) | |
ab655cf8 DS |
10877 | |
10878 | // Note that order *is* important here. We want to make sure we | |
10879 | // register (actually run) begin probes before any other probe type | |
10880 | // is run. Similarly, when unregistering probes, we want to | |
10881 | // unregister (actually run) end probes after every other probe type | |
10882 | // has be unregistered. To do the latter, | |
10883 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
10884 | DOONE(be); |
10885 | DOONE(dwarf); | |
888af770 | 10886 | DOONE(uprobe); |
b20febf3 FCE |
10887 | DOONE(timer); |
10888 | DOONE(profile); | |
10889 | DOONE(mark); | |
0a6f5a3f | 10890 | DOONE(tracepoint); |
b20febf3 FCE |
10891 | DOONE(hrtimer); |
10892 | DOONE(perfmon); | |
ce82316f | 10893 | DOONE(procfs); |
935447c8 DS |
10894 | |
10895 | // Another "order is important" item. We want to make sure we | |
10896 | // "register" the dummy task_finder probe group after all probe | |
10897 | // groups that use the task_finder. | |
10898 | DOONE(utrace); | |
a96d1db0 | 10899 | DOONE(itrace); |
935447c8 | 10900 | DOONE(task_finder); |
b20febf3 FCE |
10901 | #undef DOONE |
10902 | return g; | |
46b84a80 | 10903 | } |
73267b89 JS |
10904 | |
10905 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |