]>
Commit | Line | Data |
---|---|---|
56e12059 | 1 | // tapset resolution |
1af100fc | 2 | // Copyright (C) 2005-2013 Red Hat Inc. |
aa30ccd3 | 3 | // Copyright (C) 2005-2007 Intel Corporation. |
0b8f6579 | 4 | // Copyright (C) 2008 James.Bottomley@HansenPartnership.com |
56e12059 FCE |
5 | // |
6 | // This file is part of systemtap, and is free software. You can | |
7 | // redistribute it and/or modify it under the terms of the GNU General | |
8 | // Public License (GPL); either version 2, or (at your option) any | |
9 | // later version. | |
10 | ||
11 | #include "config.h" | |
12 | #include "staptree.h" | |
13 | #include "elaborate.h" | |
b55bc428 | 14 | #include "tapsets.h" |
93646f4d | 15 | #include "task_finder.h" |
f31a77f5 | 16 | #include "tapset-dynprobe.h" |
56e12059 | 17 | #include "translate.h" |
dc38c0ae | 18 | #include "session.h" |
72dbc915 | 19 | #include "util.h" |
0a6f5a3f | 20 | #include "buildrun.h" |
86bf665e | 21 | #include "dwarf_wrappers.h" |
2e67a43b | 22 | #include "auto_free.h" |
b278033a | 23 | #include "hash.h" |
440f755a | 24 | #include "dwflpp.h" |
5f8ca04f | 25 | #include "setupdwfl.h" |
6b51ee12 | 26 | #include <gelf.h> |
22d737e8 | 27 | |
4d0d9e8a | 28 | #include "sdt_types.h" |
bd2b1e68 | 29 | |
3b579393 FCE |
30 | #include <cstdlib> |
31 | #include <algorithm> | |
bd2b1e68 | 32 | #include <deque> |
56e12059 | 33 | #include <iostream> |
d1bcbe71 | 34 | #include <fstream> |
bd2b1e68 | 35 | #include <map> |
ec4373ff | 36 | #include <set> |
56e12059 | 37 | #include <sstream> |
bd2b1e68 | 38 | #include <stdexcept> |
b55bc428 | 39 | #include <vector> |
67959c62 | 40 | #include <stack> |
e36387d7 | 41 | #include <cstdarg> |
29e64872 | 42 | #include <cassert> |
1969b5bc | 43 | #include <iomanip> |
f781f849 | 44 | #include <cerrno> |
bd2b1e68 GH |
45 | |
46 | extern "C" { | |
df8fadee | 47 | #include <fcntl.h> |
bd2b1e68 | 48 | #include <elfutils/libdwfl.h> |
7a053d3b | 49 | #include <elfutils/libdw.h> |
77de5e9e GH |
50 | #include <dwarf.h> |
51 | #include <elf.h> | |
52 | #include <obstack.h> | |
b20febf3 | 53 | #include <glob.h> |
30a279be | 54 | #include <fnmatch.h> |
5f0a03a6 | 55 | #include <stdio.h> |
349dc70e | 56 | #include <sys/types.h> |
37001baa | 57 | #include <sys/stat.h> |
aaf7ffe8 | 58 | #include <math.h> |
aff5d390 | 59 | #include <regex.h> |
37001baa | 60 | #include <unistd.h> |
6d5d594e | 61 | #include <wordexp.h> |
4b1ad75e RM |
62 | |
63 | #define __STDC_FORMAT_MACROS | |
64 | #include <inttypes.h> | |
bd2b1e68 | 65 | } |
77de5e9e | 66 | |
508059f0 MW |
67 | // Old elf.h doesn't know about this machine type. |
68 | #ifndef EM_AARCH64 | |
69 | #define EM_AARCH64 183 | |
70 | #endif | |
71 | ||
56e12059 FCE |
72 | |
73 | using namespace std; | |
2171f774 | 74 | using namespace __gnu_cxx; |
56e12059 | 75 | |
47dd066d | 76 | |
b20febf3 FCE |
77 | |
78 | // ------------------------------------------------------------------------ | |
faea5e16 JS |
79 | |
80 | string | |
81 | common_probe_init (derived_probe* p) | |
82 | { | |
26e63673 JS |
83 | assert(p->session_index != (unsigned)-1); |
84 | return "(&stap_probes[" + lex_cast(p->session_index) + "])"; | |
faea5e16 JS |
85 | } |
86 | ||
87 | ||
b20febf3 | 88 | void |
71db462b DS |
89 | common_probe_entryfn_prologue (systemtap_session& s, |
90 | string statestr, string probe, | |
91 | string probe_type, bool overload_processing) | |
b20febf3 | 92 | { |
944c9a7a | 93 | if (s.runtime_usermode_p()) |
ac34f2aa JS |
94 | { |
95 | // If session_state() is NULL, then we haven't even initialized shm yet, | |
96 | // and there's *nothing* for the probe to do. (even alibi is in shm) | |
97 | // So failure skips this whole block through the end of the epilogue. | |
98 | s.op->newline() << "if (likely(session_state())) {"; | |
e481ab2e | 99 | s.op->indent(1); |
ac34f2aa JS |
100 | } |
101 | ||
71db462b | 102 | s.op->newline() << "#ifdef STP_ALIBI"; |
7c3e97f4 | 103 | s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));"; |
71db462b | 104 | s.op->newline() << "#else"; |
653e6a9a | 105 | |
e481ab2e JS |
106 | if (s.runtime_usermode_p()) |
107 | s.op->newline() << "int _stp_saved_errno = errno;"; | |
108 | ||
fee09ad5 | 109 | s.op->newline() << "struct context* __restrict__ c = NULL;"; |
71db462b DS |
110 | s.op->newline() << "#if !INTERRUPTIBLE"; |
111 | s.op->newline() << "unsigned long flags;"; | |
112 | s.op->newline() << "#endif"; | |
b20febf3 | 113 | |
f887a8c9 | 114 | s.op->newline() << "#ifdef STP_TIMING"; |
7c3e97f4 | 115 | s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);"; |
f887a8c9 DS |
116 | s.op->newline() << "#endif"; |
117 | if (overload_processing && !s.runtime_usermode_p()) | |
71db462b | 118 | s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; |
a58d79d0 | 119 | else |
71db462b | 120 | s.op->newline() << "#ifdef STP_TIMING"; |
b20febf3 | 121 | |
f887a8c9 DS |
122 | if (! s.runtime_usermode_p()) |
123 | s.op->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
124 | else | |
125 | { | |
126 | s.op->newline() << "struct timespec timespec_atstart;"; | |
45639d96 | 127 | s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, ×pec_atstart);"; |
f887a8c9 | 128 | } |
71db462b | 129 | s.op->newline() << "#endif"; |
994aac0e | 130 | |
71db462b DS |
131 | s.op->newline() << "#if INTERRUPTIBLE"; |
132 | s.op->newline() << "preempt_disable ();"; | |
133 | s.op->newline() << "#else"; | |
134 | s.op->newline() << "local_irq_save (flags);"; | |
135 | s.op->newline() << "#endif"; | |
b20febf3 | 136 | |
ac3af990 | 137 | if (! s.runtime_usermode_p()) |
71db462b DS |
138 | { |
139 | // Check for enough free enough stack space | |
140 | s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space | |
141 | s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space | |
142 | // XXX: may need porting to platforms where task_struct is not | |
143 | // at bottom of kernel stack NB: see also | |
144 | // CONFIG_DEBUG_STACKOVERFLOW | |
065d5567 | 145 | s.op->newline() << "atomic_inc (skipped_count());"; |
71db462b | 146 | s.op->newline() << "#ifdef STP_TIMING"; |
065d5567 | 147 | s.op->newline() << "atomic_inc (skipped_count_lowstack());"; |
71db462b DS |
148 | s.op->newline() << "#endif"; |
149 | s.op->newline() << "goto probe_epilogue;"; | |
150 | s.op->newline(-1) << "}"; | |
151 | } | |
c931ec8a | 152 | |
065d5567 | 153 | s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")"; |
71db462b DS |
154 | s.op->newline(1) << "goto probe_epilogue;"; |
155 | s.op->indent(-1); | |
9a604fac | 156 | |
1d0e697d DS |
157 | s.op->newline() << "c = _stp_runtime_entryfn_get_context();"; |
158 | if (s.runtime_usermode_p()) | |
159 | { | |
160 | s.op->newline() << "if (!c) {"; | |
161 | s.op->newline(1) << "#if !INTERRUPTIBLE"; | |
065d5567 | 162 | s.op->newline() << "atomic_inc (skipped_count());"; |
1d0e697d DS |
163 | s.op->newline() << "#endif"; |
164 | s.op->newline() << "#ifdef STP_TIMING"; | |
065d5567 | 165 | s.op->newline() << "atomic_inc (skipped_count_reentrant());"; |
1d0e697d DS |
166 | s.op->newline() << "#ifdef DEBUG_REENTRANCY"; |
167 | s.op->newline() << "_stp_warn (\"Skipped %s\\n\", " << probe << "->pp);"; | |
168 | s.op->newline() << "#endif"; | |
169 | s.op->newline() << "#endif"; | |
170 | s.op->newline() << "goto probe_epilogue;"; | |
171 | s.op->newline(-1) << "}"; | |
172 | } | |
173 | ||
71db462b DS |
174 | s.op->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
175 | s.op->newline(1) << "#if !INTERRUPTIBLE"; | |
065d5567 | 176 | s.op->newline() << "atomic_inc (skipped_count());"; |
71db462b DS |
177 | s.op->newline() << "#endif"; |
178 | s.op->newline() << "#ifdef STP_TIMING"; | |
065d5567 | 179 | s.op->newline() << "atomic_inc (skipped_count_reentrant());"; |
71db462b DS |
180 | s.op->newline() << "#ifdef DEBUG_REENTRANCY"; |
181 | s.op->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
26e63673 | 182 | << probe << "->pp, c->probe_point ?: \"?\", smp_processor_id());"; |
c12d974f FCE |
183 | // NB: There is a conceivable race condition here with reading |
184 | // c->probe_point, knowing that this other probe is sort of running. | |
185 | // However, in reality, it's interrupted. Plus even if it were able | |
186 | // to somehow start again, and stop before we read c->probe_point, | |
187 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
71db462b DS |
188 | s.op->newline() << "#endif"; |
189 | s.op->newline() << "#endif"; | |
190 | s.op->newline() << "atomic_dec (& c->busy);"; | |
191 | s.op->newline() << "goto probe_epilogue;"; | |
192 | s.op->newline(-1) << "}"; | |
193 | s.op->newline(); | |
194 | s.op->newline() << "c->last_stmt = 0;"; | |
195 | s.op->newline() << "c->last_error = 0;"; | |
196 | s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter | |
197 | s.op->newline() << "c->uregs = 0;"; | |
198 | s.op->newline() << "c->kregs = 0;"; | |
199 | s.op->newline() << "#if defined __ia64__"; | |
200 | s.op->newline() << "c->unwaddr = 0;"; | |
201 | s.op->newline() << "#endif"; | |
f4d70a33 JS |
202 | if (s.runtime_usermode_p()) |
203 | s.op->newline() << "c->probe_index = " << probe << "->index;"; | |
71db462b DS |
204 | s.op->newline() << "c->probe_point = " << probe << "->pp;"; |
205 | s.op->newline() << "#ifdef STP_NEED_PROBE_NAME"; | |
206 | s.op->newline() << "c->probe_name = " << probe << "->pn;"; | |
207 | s.op->newline() << "#endif"; | |
208 | s.op->newline() << "c->probe_type = " << probe_type << ";"; | |
6dceb5c9 | 209 | // reset Individual Probe State union |
71db462b | 210 | s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));"; |
e04b5d74 | 211 | s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;"; |
71db462b DS |
212 | s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp |
213 | s.op->newline() << "c->regparm = 0;"; | |
214 | s.op->newline() << "#endif"; | |
e0a17418 | 215 | |
152fa051 LB |
216 | if(!s.suppress_time_limits){ |
217 | s.op->newline() << "#if INTERRUPTIBLE"; | |
218 | s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
219 | s.op->newline() << "#else"; | |
220 | s.op->newline() << "c->actionremaining = MAXACTION;"; | |
221 | s.op->newline() << "#endif"; | |
222 | } | |
9915575b FCE |
223 | // NB: The following would actually be incorrect. |
224 | // That's because cycles_sum/cycles_base values are supposed to survive | |
225 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
226 | // cycles), the values will be reset. | |
227 | /* | |
71db462b DS |
228 | s.op->newline() << "#ifdef STP_OVERLOAD"; |
229 | s.op->newline() << "c->cycles_sum = 0;"; | |
230 | s.op->newline() << "c->cycles_base = 0;"; | |
231 | s.op->newline() << "#endif"; | |
9915575b | 232 | */ |
44cfbe25 SM |
233 | |
234 | s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)"; | |
235 | s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;"; | |
236 | s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;"; | |
237 | s.op->newline() << "#endif"; | |
b20febf3 | 238 | } |
9a604fac | 239 | |
a44a0785 | 240 | |
b20febf3 | 241 | void |
f887a8c9 DS |
242 | common_probe_entryfn_epilogue (systemtap_session& s, |
243 | bool overload_processing) | |
b20febf3 | 244 | { |
f887a8c9 DS |
245 | if (overload_processing && !s.runtime_usermode_p()) |
246 | s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
a58d79d0 | 247 | else |
f887a8c9 DS |
248 | s.op->newline() << "#ifdef STP_TIMING"; |
249 | s.op->newline() << "{"; | |
250 | s.op->indent(1); | |
251 | if (! s.runtime_usermode_p()) | |
252 | { | |
253 | s.op->newline() << "cycles_t cycles_atend = get_cycles ();"; | |
254 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
255 | // fewer, if the hardware counter rolls over really quickly. We | |
256 | // handle 32-bit wraparound here. | |
257 | s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
258 | s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
259 | s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
260 | s.op->indent(-1); | |
261 | } | |
262 | else | |
263 | { | |
264 | s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;"; | |
265 | s.op->newline() << "long cycles_elapsed;"; | |
45639d96 | 266 | s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, ×pec_atend);"; |
f887a8c9 DS |
267 | s.op->newline() << "_stp_timespec_sub(×pec_atend, ×pec_atstart, ×pec_elapsed);"; |
268 | // 'cycles_elapsed' is really elapsed nanoseconds | |
269 | s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;"; | |
270 | } | |
271 | ||
272 | s.op->newline() << "#ifdef STP_TIMING"; | |
273 | s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);"; | |
274 | s.op->newline() << "#endif"; | |
275 | ||
276 | if (overload_processing && !s.runtime_usermode_p()) | |
277 | { | |
278 | s.op->newline() << "#ifdef STP_OVERLOAD"; | |
279 | s.op->newline() << "{"; | |
a58d79d0 DS |
280 | // If the cycle count has wrapped (cycles_atend > cycles_base), |
281 | // let's go ahead and pretend the interval has been reached. | |
282 | // This should reset cycles_base and cycles_sum. | |
f887a8c9 DS |
283 | s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; |
284 | s.op->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
285 | s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
286 | s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
a58d79d0 DS |
287 | |
288 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
289 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
290 | // has overloaded the system and we need to quit. | |
7baf48e9 FCE |
291 | // NB: this is not suppressible via --suppress-runtime-errors, |
292 | // because this is a system safety metric that we cannot trust | |
293 | // unprivileged users to override. | |
f887a8c9 DS |
294 | s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; |
295 | s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
296 | s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
065d5567 JS |
297 | s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);"; |
298 | s.op->newline() << "atomic_inc (error_count());"; | |
f887a8c9 DS |
299 | s.op->newline(-1) << "}"; |
300 | ||
301 | s.op->newline() << "c->cycles_base = cycles_atend;"; | |
302 | s.op->newline() << "c->cycles_sum = 0;"; | |
303 | s.op->newline(-1) << "}"; | |
304 | s.op->newline(-1) << "}"; | |
305 | s.op->newline() << "#endif"; | |
a58d79d0 | 306 | } |
e57b735a | 307 | |
f887a8c9 DS |
308 | s.op->newline(-1) << "}"; |
309 | s.op->newline() << "#endif"; | |
e57b735a | 310 | |
f887a8c9 DS |
311 | s.op->newline() << "c->probe_point = 0;"; // vacated |
312 | s.op->newline() << "#ifdef STP_NEED_PROBE_NAME"; | |
313 | s.op->newline() << "c->probe_name = 0;"; | |
314 | s.op->newline() << "#endif"; | |
315 | s.op->newline() << "c->probe_type = 0;"; | |
7baf48e9 FCE |
316 | |
317 | ||
f887a8c9 DS |
318 | s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; |
319 | s.op->indent(1); | |
320 | if (s.suppress_handler_errors) // PR 13306 | |
b78a0fbb | 321 | { |
065d5567 | 322 | s.op->newline() << "atomic_inc (error_count());"; |
7baf48e9 FCE |
323 | } |
324 | else | |
325 | { | |
f887a8c9 DS |
326 | s.op->newline() << "if (c->last_stmt != NULL)"; |
327 | s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
328 | s.op->newline(-1) << "else"; | |
329 | s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
330 | s.op->indent(-1); | |
065d5567 JS |
331 | s.op->newline() << "atomic_inc (error_count());"; |
332 | s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {"; | |
333 | s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);"; | |
f887a8c9 DS |
334 | s.op->newline() << "_stp_exit ();"; |
335 | s.op->newline(-1) << "}"; | |
7baf48e9 FCE |
336 | } |
337 | ||
f887a8c9 | 338 | s.op->newline(-1) << "}"; |
7baf48e9 FCE |
339 | |
340 | ||
f887a8c9 | 341 | s.op->newline() << "atomic_dec (&c->busy);"; |
e57b735a | 342 | |
f887a8c9 DS |
343 | s.op->newline(-1) << "probe_epilogue:"; // context is free |
344 | s.op->indent(1); | |
e57b735a | 345 | |
8ca891c4 DS |
346 | // In dyninst mode, we're not done with the context yet, since |
347 | // _stp_error() still needs a context structure (since the log | |
348 | // buffers are stored there). | |
349 | if (!s.runtime_usermode_p()) | |
350 | { | |
e481ab2e | 351 | s.op->newline() << "_stp_runtime_entryfn_put_context(c);"; |
8ca891c4 | 352 | } |
f887a8c9 | 353 | if (! s.suppress_handler_errors) // PR 13306 |
7baf48e9 FCE |
354 | { |
355 | // Check for excessive skip counts. | |
065d5567 JS |
356 | s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {"; |
357 | s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))"; | |
f887a8c9 DS |
358 | s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");"; |
359 | s.op->newline(-1) << "}"; | |
7baf48e9 | 360 | } |
e57b735a | 361 | |
f887a8c9 DS |
362 | s.op->newline() << "#if INTERRUPTIBLE"; |
363 | s.op->newline() << "preempt_enable_no_resched ();"; | |
364 | s.op->newline() << "#else"; | |
365 | s.op->newline() << "local_irq_restore (flags);"; | |
366 | s.op->newline() << "#endif"; | |
653e6a9a | 367 | |
944c9a7a | 368 | if (s.runtime_usermode_p()) |
ac34f2aa | 369 | { |
e481ab2e | 370 | s.op->newline() << "_stp_runtime_entryfn_put_context(c);"; |
ac34f2aa | 371 | s.op->newline() << "errno = _stp_saved_errno;"; |
ac34f2aa | 372 | } |
e481ab2e JS |
373 | |
374 | s.op->newline() << "#endif // STP_ALIBI"; | |
375 | ||
376 | if (s.runtime_usermode_p()) | |
377 | s.op->newline(-1) << "}"; | |
440f755a | 378 | } |
e57b735a | 379 | |
e57b735a | 380 | |
440f755a | 381 | // ------------------------------------------------------------------------ |
e57b735a | 382 | |
440f755a JS |
383 | // ------------------------------------------------------------------------ |
384 | // Dwarf derived probes. "We apologize for the inconvience." | |
385 | // ------------------------------------------------------------------------ | |
e57b735a | 386 | |
4627ed58 JS |
387 | static const string TOK_KERNEL("kernel"); |
388 | static const string TOK_MODULE("module"); | |
389 | static const string TOK_FUNCTION("function"); | |
390 | static const string TOK_INLINE("inline"); | |
391 | static const string TOK_CALL("call"); | |
4bda987e | 392 | static const string TOK_EXPORTED("exported"); |
4627ed58 JS |
393 | static const string TOK_RETURN("return"); |
394 | static const string TOK_MAXACTIVE("maxactive"); | |
395 | static const string TOK_STATEMENT("statement"); | |
396 | static const string TOK_ABSOLUTE("absolute"); | |
397 | static const string TOK_PROCESS("process"); | |
a794dbeb | 398 | static const string TOK_PROVIDER("provider"); |
4627ed58 JS |
399 | static const string TOK_MARK("mark"); |
400 | static const string TOK_TRACE("trace"); | |
401 | static const string TOK_LABEL("label"); | |
63b4fd14 | 402 | static const string TOK_LIBRARY("library"); |
576eaefe | 403 | static const string TOK_PLT("plt"); |
e7d4410d LB |
404 | static const string TOK_METHOD("method"); |
405 | static const string TOK_CLASS("class");; | |
c31add50 JL |
406 | static const string TOK_CALLEE("callee");; |
407 | static const string TOK_CALLEES("callees");; | |
e57b735a | 408 | |
5c378838 | 409 | static int query_cu (Dwarf_Die * cudie, dwarf_query *q); |
6b517475 | 410 | static void query_addr(Dwarf_Addr addr, dwarf_query *q); |
f4faaf86 | 411 | static void query_plt_statement(dwarf_query *q); |
e57b735a | 412 | |
440f755a JS |
413 | // Can we handle this query with just symbol-table info? |
414 | enum dbinfo_reqt | |
415 | { | |
416 | dbr_unknown, | |
417 | dbr_none, // kernel.statement(NUM).absolute | |
418 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
419 | dbr_need_dwarf | |
420 | }; | |
e57b735a | 421 | |
20e4a32c | 422 | |
440f755a JS |
423 | struct base_query; // forward decls |
424 | struct dwarf_query; | |
425 | struct dwflpp; | |
426 | struct symbol_table; | |
20e4a32c | 427 | |
a781f401 | 428 | |
440f755a JS |
429 | struct |
430 | symbol_table | |
431 | { | |
432 | module_info *mod_info; // associated module | |
433 | map<string, func_info*> map_by_name; | |
1c6b77e5 JS |
434 | multimap<Dwarf_Addr, func_info*> map_by_addr; |
435 | typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t; | |
440f755a JS |
436 | typedef pair<iterator_t, iterator_t> range_t; |
437 | #ifdef __powerpc__ | |
438 | GElf_Word opd_section; | |
439 | #endif | |
2867a2a1 JS |
440 | void add_symbol(const char *name, bool weak, bool descriptor, |
441 | Dwarf_Addr addr, Dwarf_Addr *high_addr); | |
440f755a | 442 | enum info_status read_symbols(FILE *f, const string& path); |
83ca3872 | 443 | enum info_status read_from_elf_file(const string& path, |
2713ea24 | 444 | systemtap_session &sess); |
83ca3872 | 445 | enum info_status read_from_text_file(const string& path, |
2713ea24 | 446 | systemtap_session &sess); |
440f755a JS |
447 | enum info_status get_from_elf(); |
448 | void prepare_section_rejection(Dwfl_Module *mod); | |
449 | bool reject_section(GElf_Word section); | |
440f755a JS |
450 | void purge_syscall_stubs(); |
451 | func_info *lookup_symbol(const string& name); | |
452 | Dwarf_Addr lookup_symbol_address(const string& name); | |
453 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
3d372d6b | 454 | func_info *get_first_func(); |
7a053d3b | 455 | |
440f755a JS |
456 | symbol_table(module_info *mi) : mod_info(mi) {} |
457 | ~symbol_table(); | |
458 | }; | |
77de5e9e | 459 | |
440f755a JS |
460 | static bool null_die(Dwarf_Die *die) |
461 | { | |
822a6a3d | 462 | static Dwarf_Die null; |
440f755a JS |
463 | return (!die || !memcmp(die, &null, sizeof(null))); |
464 | } | |
c4ce66a1 JS |
465 | |
466 | ||
7a053d3b | 467 | enum |
bd2b1e68 | 468 | function_spec_type |
7a053d3b | 469 | { |
bd2b1e68 GH |
470 | function_alone, |
471 | function_and_file, | |
7a053d3b | 472 | function_file_and_line |
bd2b1e68 GH |
473 | }; |
474 | ||
ec4373ff | 475 | |
bd2b1e68 | 476 | struct dwarf_builder; |
f10534c6 | 477 | struct dwarf_var_expanding_visitor; |
77de5e9e | 478 | |
2930abc7 | 479 | |
b20febf3 FCE |
480 | // XXX: This class is a candidate for subclassing to separate |
481 | // the relocation vs non-relocation variants. Likewise for | |
482 | // kprobe vs kretprobe variants. | |
483 | ||
484 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 485 | { |
b20febf3 FCE |
486 | dwarf_derived_probe (const string& function, |
487 | const string& filename, | |
488 | int line, | |
489 | const string& module, | |
490 | const string& section, | |
491 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 492 | Dwarf_Addr addr, |
b20febf3 FCE |
493 | dwarf_query & q, |
494 | Dwarf_Die* scope_die); | |
20e4a32c | 495 | |
b20febf3 FCE |
496 | string module; |
497 | string section; | |
498 | Dwarf_Addr addr; | |
63b4fd14 | 499 | string path; |
27dc09b1 | 500 | bool has_process; |
2930abc7 | 501 | bool has_return; |
c9bad430 | 502 | bool has_maxactive; |
63b4fd14 | 503 | bool has_library; |
c9bad430 | 504 | long maxactive_val; |
4ad95bbc | 505 | // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition |
b642c901 SC |
506 | string user_path; |
507 | string user_lib; | |
b95e2b79 | 508 | bool access_vars; |
2930abc7 | 509 | |
af234c40 JS |
510 | unsigned saved_longs, saved_strings; |
511 | dwarf_derived_probe* entry_handler; | |
512 | ||
b8da0ad1 | 513 | void printsig (std::ostream &o) const; |
6b66b9f7 | 514 | virtual void join_group (systemtap_session& s); |
3689db05 | 515 | void emit_probe_local_init(systemtap_session& s, translator_output * o); |
d0bfd2ac | 516 | void getargs(std::list<std::string> &arg_set) const; |
0a98fd42 | 517 | |
42e38653 | 518 | void emit_privilege_assertion (translator_output*); |
27dc09b1 DB |
519 | void print_dupe_stamp(ostream& o); |
520 | ||
bd2b1e68 | 521 | // Pattern registration helpers. |
7a053d3b | 522 | static void register_statement_variants(match_node * root, |
27dc09b1 | 523 | dwarf_builder * dw, |
42e38653 | 524 | privilege_t privilege); |
fd6602a0 | 525 | static void register_function_variants(match_node * root, |
27dc09b1 | 526 | dwarf_builder * dw, |
42e38653 | 527 | privilege_t privilege); |
440d9b00 DB |
528 | static void register_function_and_statement_variants(systemtap_session& s, |
529 | match_node * root, | |
27dc09b1 | 530 | dwarf_builder * dw, |
42e38653 | 531 | privilege_t privilege); |
b1615c74 JS |
532 | static void register_sdt_variants(systemtap_session& s, |
533 | match_node * root, | |
534 | dwarf_builder * dw); | |
535 | static void register_plt_variants(systemtap_session& s, | |
536 | match_node * root, | |
537 | dwarf_builder * dw); | |
c4ce66a1 | 538 | static void register_patterns(systemtap_session& s); |
6b66b9f7 JS |
539 | |
540 | protected: | |
541 | dwarf_derived_probe(probe *base, | |
542 | probe_point *location, | |
543 | Dwarf_Addr addr, | |
544 | bool has_return): | |
74fe61bc LB |
545 | derived_probe(base, location), addr(addr), has_process(0), |
546 | has_return(has_return), has_maxactive(0), has_library(0), | |
547 | maxactive_val(0), access_vars(false), saved_longs(0), | |
548 | saved_strings(0), entry_handler(0) | |
6b66b9f7 JS |
549 | {} |
550 | ||
551 | private: | |
d0bfd2ac | 552 | list<string> args; |
8c67c337 | 553 | void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr); |
20c6c071 GH |
554 | }; |
555 | ||
dc38c0ae | 556 | |
6b66b9f7 | 557 | struct uprobe_derived_probe: public dwarf_derived_probe |
6d0f3f0c | 558 | { |
6d0f3f0c | 559 | int pid; // 0 => unrestricted |
0973d815 | 560 | |
6d0f3f0c FCE |
561 | uprobe_derived_probe (const string& function, |
562 | const string& filename, | |
563 | int line, | |
564 | const string& module, | |
6d0f3f0c FCE |
565 | const string& section, |
566 | Dwarf_Addr dwfl_addr, | |
567 | Dwarf_Addr addr, | |
568 | dwarf_query & q, | |
6b66b9f7 JS |
569 | Dwarf_Die* scope_die): |
570 | dwarf_derived_probe(function, filename, line, module, section, | |
571 | dwfl_addr, addr, q, scope_die), pid(0) | |
572 | {} | |
6d0f3f0c | 573 | |
0973d815 FCE |
574 | // alternate constructor for process(PID).statement(ADDR).absolute |
575 | uprobe_derived_probe (probe *base, | |
576 | probe_point *location, | |
577 | int pid, | |
578 | Dwarf_Addr addr, | |
6b66b9f7 JS |
579 | bool has_return): |
580 | dwarf_derived_probe(base, location, addr, has_return), pid(pid) | |
581 | {} | |
9ace370f | 582 | |
6d0f3f0c | 583 | void join_group (systemtap_session& s); |
2865d17a | 584 | |
42e38653 | 585 | void emit_privilege_assertion (translator_output*); |
8f6d8c2b | 586 | void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); } |
c0f84e7b SC |
587 | void getargs(std::list<std::string> &arg_set) const; |
588 | void saveargs(int nargs); | |
589 | private: | |
590 | list<string> args; | |
6d0f3f0c FCE |
591 | }; |
592 | ||
dc38c0ae DS |
593 | struct dwarf_derived_probe_group: public derived_probe_group |
594 | { | |
595 | private: | |
b20febf3 FCE |
596 | multimap<string,dwarf_derived_probe*> probes_by_module; |
597 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
598 | |
599 | public: | |
08b5a50c | 600 | dwarf_derived_probe_group() {} |
b20febf3 FCE |
601 | void enroll (dwarf_derived_probe* probe); |
602 | void emit_module_decls (systemtap_session& s); | |
603 | void emit_module_init (systemtap_session& s); | |
b4be7cbc | 604 | void emit_module_refresh (systemtap_session& s); |
b20febf3 | 605 | void emit_module_exit (systemtap_session& s); |
dc38c0ae DS |
606 | }; |
607 | ||
608 | ||
20c6c071 | 609 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 610 | struct base_query |
20c6c071 | 611 | { |
c4ce66a1 JS |
612 | base_query(dwflpp & dw, literal_map_t const & params); |
613 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 614 | virtual ~base_query() {} |
bd2b1e68 | 615 | |
5227f1ea | 616 | systemtap_session & sess; |
2c384610 | 617 | dwflpp & dw; |
5227f1ea | 618 | |
070764c0 JL |
619 | // Used to keep track of which modules were visited during |
620 | // iterate_over_modules() | |
621 | set<string> visited_modules; | |
622 | ||
bd2b1e68 | 623 | // Parameter extractors. |
86bf665e | 624 | static bool has_null_param(literal_map_t const & params, |
888af770 | 625 | string const & k); |
86bf665e | 626 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 627 | string const & k, string & v); |
86bf665e | 628 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 629 | string const & k, long & v); |
86bf665e | 630 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 631 | string const & k, Dwarf_Addr & v); |
bbbc7241 | 632 | static void query_library_callback (base_query *me, const char *data); |
45cdb40e | 633 | static void query_plt_callback (base_query *me, const char *link, size_t addr); |
f301a9ff | 634 | virtual void query_library (const char *data) = 0; |
576eaefe | 635 | virtual void query_plt (const char *link, size_t addr) = 0; |
84c84ac4 | 636 | |
b55bc428 | 637 | |
2c384610 DS |
638 | // Extracted parameters. |
639 | bool has_kernel; | |
91af0778 FCE |
640 | bool has_module; |
641 | bool has_process; | |
63b4fd14 | 642 | bool has_library; |
576eaefe SC |
643 | bool has_plt; |
644 | bool has_statement; | |
2c384610 | 645 | string module_val; // has_kernel => module_val = "kernel" |
63b4fd14 | 646 | string path; // executable path if module is a .so |
576eaefe | 647 | string plt_val; // has_plt => plt wildcard |
2c384610 DS |
648 | |
649 | virtual void handle_query_module() = 0; | |
650 | }; | |
651 | ||
652 | ||
c4ce66a1 | 653 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
576eaefe | 654 | sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false) |
2c384610 | 655 | { |
91af0778 | 656 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
657 | if (has_kernel) |
658 | module_val = "kernel"; | |
91af0778 FCE |
659 | |
660 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
661 | if (has_module) | |
662 | has_process = false; | |
4baf0e53 | 663 | else |
d0a7f5a9 | 664 | { |
63b4fd14 | 665 | string library_name; |
576eaefe | 666 | long statement_num_val; |
d0a7f5a9 | 667 | has_process = get_string_param(params, TOK_PROCESS, module_val); |
63b4fd14 | 668 | has_library = get_string_param (params, TOK_LIBRARY, library_name); |
576eaefe SC |
669 | if ((has_plt = has_null_param (params, TOK_PLT))) |
670 | plt_val = "*"; | |
671 | else has_plt = get_string_param (params, TOK_PLT, plt_val); | |
672 | has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
673 | ||
84c84ac4 | 674 | if (has_process) |
05fb3e0c | 675 | module_val = find_executable (module_val, sess.sysroot, sess.sysenv); |
84c84ac4 SC |
676 | if (has_library) |
677 | { | |
678 | if (! contains_glob_chars (library_name)) | |
679 | { | |
05fb3e0c WF |
680 | path = path_remove_sysroot(sess, module_val); |
681 | module_val = find_executable (library_name, sess.sysroot, | |
682 | sess.sysenv, "LD_LIBRARY_PATH"); | |
47e226ed SC |
683 | if (module_val.find('/') == string::npos) |
684 | { | |
685 | // We didn't find library_name so use iterate_over_libraries | |
686 | module_val = path; | |
687 | path = library_name; | |
688 | } | |
84c84ac4 SC |
689 | } |
690 | else | |
691 | path = library_name; | |
692 | } | |
d0a7f5a9 | 693 | } |
91af0778 FCE |
694 | |
695 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
696 | } |
697 | ||
c4ce66a1 | 698 | base_query::base_query(dwflpp & dw, const string & module_val) |
576eaefe SC |
699 | : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false), |
700 | module_val(module_val) | |
c4ce66a1 JS |
701 | { |
702 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
703 | // which means that userspace modules won't get any PATH searching. | |
704 | if (module_val.find('/') == string::npos) | |
705 | { | |
706 | has_kernel = (module_val == TOK_KERNEL); | |
707 | has_module = !has_kernel; | |
708 | has_process = false; | |
709 | } | |
710 | else | |
711 | { | |
712 | has_kernel = has_module = false; | |
713 | has_process = true; | |
714 | } | |
715 | } | |
716 | ||
2c384610 | 717 | bool |
86bf665e | 718 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
719 | string const & k) |
720 | { | |
888af770 | 721 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
722 | } |
723 | ||
724 | ||
725 | bool | |
86bf665e | 726 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
727 | string const & k, string & v) |
728 | { | |
729 | return derived_probe_builder::get_param (params, k, v); | |
730 | } | |
731 | ||
732 | ||
733 | bool | |
86bf665e | 734 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
735 | string const & k, long & v) |
736 | { | |
737 | int64_t value; | |
738 | bool present = derived_probe_builder::get_param (params, k, value); | |
739 | v = (long) value; | |
740 | return present; | |
741 | } | |
742 | ||
743 | ||
744 | bool | |
86bf665e | 745 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
746 | string const & k, Dwarf_Addr & v) |
747 | { | |
748 | int64_t value; | |
749 | bool present = derived_probe_builder::get_param (params, k, value); | |
750 | v = (Dwarf_Addr) value; | |
751 | return present; | |
752 | } | |
753 | ||
2c384610 DS |
754 | struct dwarf_query : public base_query |
755 | { | |
e1278bd4 | 756 | dwarf_query(probe * base_probe, |
2c384610 DS |
757 | probe_point * base_loc, |
758 | dwflpp & dw, | |
86bf665e | 759 | literal_map_t const & params, |
b642c901 SC |
760 | vector<derived_probe *> & results, |
761 | const string user_path, | |
762 | const string user_lib); | |
2c384610 | 763 | |
c4ce66a1 | 764 | vector<derived_probe *> & results; |
8f14e444 | 765 | set<string> inlined_non_returnable; // function names |
c4ce66a1 JS |
766 | probe * base_probe; |
767 | probe_point * base_loc; | |
b642c901 SC |
768 | string user_path; |
769 | string user_lib; | |
c4ce66a1 | 770 | |
2c384610 | 771 | virtual void handle_query_module(); |
5f0a03a6 JK |
772 | void query_module_dwarf(); |
773 | void query_module_symtab(); | |
5d5bd369 | 774 | void query_library (const char *data); |
576eaefe | 775 | void query_plt (const char *entry, size_t addr); |
2c384610 | 776 | |
2930abc7 FCE |
777 | void add_probe_point(string const & funcname, |
778 | char const * filename, | |
779 | int line, | |
780 | Dwarf_Die *scope_die, | |
781 | Dwarf_Addr addr); | |
36f9dd1d | 782 | |
857bdfd1 JS |
783 | // Track addresses we've already seen in a given module |
784 | set<Dwarf_Addr> alias_dupes; | |
785 | ||
7fdd3e2c JS |
786 | // Track inlines we've already seen as well |
787 | // NB: this can't be compared just by entrypc, as inlines can overlap | |
788 | set<inline_instance_info> inline_dupes; | |
789 | ||
e2941743 JL |
790 | // Used in .callee[s] probes, when calling iterate_over_callees() (which |
791 | // provides the actual stack). Retains the addrs of the callers unwind addr | |
792 | // where the callee is found. Specifies multiple callers. E.g. when a callee | |
793 | // at depth 2 is found, callers[1] has the addr of the caller, and callers[0] | |
794 | // has the addr of the caller's caller. | |
795 | stack<Dwarf_Addr> *callers; | |
796 | ||
20c6c071 GH |
797 | bool has_function_str; |
798 | bool has_statement_str; | |
799 | bool has_function_num; | |
800 | bool has_statement_num; | |
7a053d3b RM |
801 | string statement_str_val; |
802 | string function_str_val; | |
c239d28c GH |
803 | Dwarf_Addr statement_num_val; |
804 | Dwarf_Addr function_num_val; | |
20c6c071 | 805 | |
b8da0ad1 | 806 | bool has_call; |
4bda987e | 807 | bool has_exported; |
b8da0ad1 | 808 | bool has_inline; |
20c6c071 GH |
809 | bool has_return; |
810 | ||
c9bad430 DS |
811 | bool has_maxactive; |
812 | long maxactive_val; | |
813 | ||
20c6c071 GH |
814 | bool has_label; |
815 | string label_val; | |
816 | ||
c31add50 JL |
817 | bool has_callee; |
818 | string callee_val; | |
819 | ||
820 | bool has_callees_num; | |
821 | long callees_num_val; | |
822 | ||
20c6c071 GH |
823 | bool has_relative; |
824 | long relative_val; | |
825 | ||
37ebca01 FCE |
826 | bool has_absolute; |
827 | ||
467bea43 SC |
828 | bool has_mark; |
829 | ||
5f0a03a6 JK |
830 | enum dbinfo_reqt dbinfo_reqt; |
831 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
832 | ||
7d6d0afc | 833 | void parse_function_spec(const string & spec); |
20c6c071 | 834 | function_spec_type spec_type; |
7d6d0afc | 835 | vector<string> scopes; |
20c6c071 GH |
836 | string function; |
837 | string file; | |
0c8b7d37 | 838 | line_t line_type; |
879eb9e9 | 839 | int line[2]; |
5f0a03a6 | 840 | bool query_done; // Found exact match |
20c6c071 | 841 | |
5a617dc6 JL |
842 | // Holds the prologue end of the current function |
843 | Dwarf_Addr prologue_end; | |
844 | ||
bd25380d | 845 | set<string> filtered_srcfiles; |
7e1279ea FCE |
846 | |
847 | // Map official entrypc -> func_info object | |
86bf665e TM |
848 | inline_instance_map_t filtered_inlines; |
849 | func_info_map_t filtered_functions; | |
4df79aaf JS |
850 | |
851 | void query_module_functions (); | |
e772a6e7 JL |
852 | |
853 | string final_function_name(const string& final_func, | |
854 | const string& final_file, | |
855 | int final_line); | |
b55bc428 FCE |
856 | }; |
857 | ||
98afd80e | 858 | |
435f53a7 FCE |
859 | static void delete_session_module_cache (systemtap_session& s); // forward decl |
860 | ||
861 | ||
98afd80e | 862 | struct dwarf_builder: public derived_probe_builder |
b55bc428 | 863 | { |
665e1256 | 864 | map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */ |
7a24d422 | 865 | map <string,dwflpp*> user_dw; |
b642c901 SC |
866 | string user_path; |
867 | string user_lib; | |
44ffe90c JL |
868 | |
869 | // Holds modules to suggest functions from. NB: aggregates over | |
870 | // recursive calls to build() when deriving globby probes. | |
871 | set <string> modules_seen; | |
872 | ||
ae2552da | 873 | dwarf_builder() {} |
aa30ccd3 | 874 | |
ae2552da | 875 | dwflpp *get_kern_dw(systemtap_session& sess, const string& module) |
707bf35e | 876 | { |
ea14cf67 FCE |
877 | if (kern_dw[module] == 0) |
878 | kern_dw[module] = new dwflpp(sess, module, true); // might throw | |
ae2552da | 879 | return kern_dw[module]; |
707bf35e JS |
880 | } |
881 | ||
882 | dwflpp *get_user_dw(systemtap_session& sess, const string& module) | |
883 | { | |
ea14cf67 FCE |
884 | if (user_dw[module] == 0) |
885 | user_dw[module] = new dwflpp(sess, module, false); // might throw | |
707bf35e JS |
886 | return user_dw[module]; |
887 | } | |
7a24d422 FCE |
888 | |
889 | /* NB: not virtual, so can be called from dtor too: */ | |
822a6a3d | 890 | void dwarf_build_no_more (bool) |
aa30ccd3 | 891 | { |
435f53a7 FCE |
892 | delete_map(kern_dw); |
893 | delete_map(user_dw); | |
7a24d422 FCE |
894 | } |
895 | ||
896 | void build_no_more (systemtap_session &s) | |
897 | { | |
898 | dwarf_build_no_more (s.verbose > 3); | |
435f53a7 | 899 | delete_session_module_cache (s); |
aa30ccd3 FCE |
900 | } |
901 | ||
e38d6504 RM |
902 | ~dwarf_builder() |
903 | { | |
7a24d422 | 904 | dwarf_build_no_more (false); |
c8959a29 | 905 | } |
aa30ccd3 | 906 | |
5227f1ea | 907 | virtual void build(systemtap_session & sess, |
7a053d3b | 908 | probe * base, |
20c6c071 | 909 | probe_point * location, |
86bf665e | 910 | literal_map_t const & parameters, |
20c6c071 | 911 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
912 | }; |
913 | ||
5111fc3e | 914 | |
e1278bd4 | 915 | dwarf_query::dwarf_query(probe * base_probe, |
20c6c071 GH |
916 | probe_point * base_loc, |
917 | dwflpp & dw, | |
86bf665e | 918 | literal_map_t const & params, |
b642c901 SC |
919 | vector<derived_probe *> & results, |
920 | const string user_path, | |
921 | const string user_lib) | |
e2941743 JL |
922 | : base_query(dw, params), results(results), base_probe(base_probe), |
923 | base_loc(base_loc), user_path(user_path), user_lib(user_lib), | |
5a617dc6 | 924 | callers(NULL), has_relative(false), relative_val(0), prologue_end(0) |
bd2b1e68 GH |
925 | { |
926 | // Reduce the query to more reasonable semantic values (booleans, | |
927 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
928 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
929 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
930 | ||
931 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
932 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
933 | ||
0f336e95 | 934 | has_label = get_string_param(params, TOK_LABEL, label_val); |
c31add50 JL |
935 | has_callee = get_string_param(params, TOK_CALLEE, callee_val); |
936 | if (has_null_param(params, TOK_CALLEES)) | |
937 | { // .callees ==> .callees(1) (also equivalent to .callee("*")) | |
938 | has_callees_num = true; | |
939 | callees_num_val = 1; | |
940 | } | |
941 | else | |
942 | { | |
943 | has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val); | |
944 | if (has_callees_num && callees_num_val < 1) | |
945 | throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"), | |
946 | base_probe->tok); | |
947 | } | |
0f336e95 | 948 | |
b8da0ad1 | 949 | has_call = has_null_param(params, TOK_CALL); |
4bda987e | 950 | has_exported = has_null_param(params, TOK_EXPORTED); |
b8da0ad1 | 951 | has_inline = has_null_param(params, TOK_INLINE); |
bd2b1e68 | 952 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 953 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 954 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 955 | has_mark = false; |
37ebca01 | 956 | |
bd2b1e68 | 957 | if (has_function_str) |
7d6d0afc | 958 | parse_function_spec(function_str_val); |
bd2b1e68 | 959 | else if (has_statement_str) |
7d6d0afc | 960 | parse_function_spec(statement_str_val); |
0daad364 | 961 | |
5f0a03a6 JK |
962 | dbinfo_reqt = assess_dbinfo_reqt(); |
963 | query_done = false; | |
0daad364 JS |
964 | } |
965 | ||
966 | ||
440f755a JS |
967 | func_info_map_t * |
968 | get_filtered_functions(dwarf_query *q) | |
969 | { | |
970 | return &q->filtered_functions; | |
971 | } | |
972 | ||
973 | ||
974 | inline_instance_map_t * | |
975 | get_filtered_inlines(dwarf_query *q) | |
976 | { | |
977 | return &q->filtered_inlines; | |
978 | } | |
979 | ||
980 | ||
2c384610 | 981 | void |
5f0a03a6 | 982 | dwarf_query::query_module_dwarf() |
2c384610 DS |
983 | { |
984 | if (has_function_num || has_statement_num) | |
985 | { | |
986 | // If we have module("foo").function(0xbeef) or | |
987 | // module("foo").statement(0xbeef), the address is relative | |
988 | // to the start of the module, so we seek the function | |
989 | // number plus the module's bias. | |
6b517475 JS |
990 | Dwarf_Addr addr = has_function_num ? |
991 | function_num_val : statement_num_val; | |
08d1d520 MW |
992 | |
993 | // These are raw addresses, we need to know what the elf_bias | |
994 | // is to feed it to libdwfl based functions. | |
995 | Dwarf_Addr elf_bias; | |
996 | Elf *elf = dwfl_module_getelf (dw.module, &elf_bias); | |
997 | assert(elf); | |
998 | addr += elf_bias; | |
6b517475 | 999 | query_addr(addr, this); |
2c384610 DS |
1000 | } |
1001 | else | |
1002 | { | |
1003 | // Otherwise if we have a function("foo") or statement("foo") | |
1004 | // specifier, we have to scan over all the CUs looking for | |
1005 | // the function(s) in question | |
1006 | assert(has_function_str || has_statement_str); | |
4df79aaf JS |
1007 | |
1008 | // For simple cases, no wildcard and no source:line, we can do a very | |
1009 | // quick function lookup in a module-wide cache. | |
1ffb8bd1 JS |
1010 | if (spec_type == function_alone && |
1011 | !dw.name_has_wildcard(function) && | |
1012 | !startswith(function, "_Z")) | |
4df79aaf JS |
1013 | query_module_functions(); |
1014 | else | |
337b7c44 | 1015 | dw.iterate_over_cus(&query_cu, this, false); |
2c384610 DS |
1016 | } |
1017 | } | |
1018 | ||
5f0a03a6 JK |
1019 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
1020 | dwarf_query * q); | |
1021 | ||
91bb9081 LB |
1022 | static void |
1023 | query_symtab_func_info (func_info & fi, dwarf_query * q) | |
1024 | { | |
1025 | assert(null_die(&fi.die)); | |
1026 | ||
1027 | Dwarf_Addr addr = fi.addr; | |
1028 | ||
1029 | // Now compensate for the dw bias because the addresses come | |
1030 | // from dwfl_module_symtab, so fi->addr is NOT a normal dw address. | |
1031 | q->dw.get_module_dwarf(false, false); | |
1032 | addr -= q->dw.module_bias; | |
1033 | ||
1034 | // If there are already probes in this module, lets not duplicate. | |
1035 | // This can come from other weak symbols/aliases or existing | |
a41f2ad7 JS |
1036 | // matches from Dwarf DIE functions. Try to add this addr to the |
1037 | // collection, and only continue if it was new. | |
1038 | if (q->alias_dupes.insert(addr).second) | |
1039 | query_func_info(addr, fi, q); | |
91bb9081 LB |
1040 | } |
1041 | ||
5f0a03a6 JK |
1042 | void |
1043 | dwarf_query::query_module_symtab() | |
1044 | { | |
1045 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
1046 | if (dbinfo_reqt == dbr_need_dwarf) | |
1047 | return; | |
1048 | ||
1049 | module_info *mi = dw.mod_info; | |
1050 | if (dbinfo_reqt == dbr_need_symtab) | |
1051 | { | |
1052 | if (mi->symtab_status == info_unknown) | |
1053 | mi->get_symtab(this); | |
1054 | if (mi->symtab_status == info_absent) | |
1055 | return; | |
1056 | } | |
1057 | ||
1058 | func_info *fi = NULL; | |
1059 | symbol_table *sym_table = mi->sym_table; | |
1060 | ||
1061 | if (has_function_str) | |
1062 | { | |
1063 | // Per dwarf_query::assess_dbinfo_reqt()... | |
1064 | assert(spec_type == function_alone); | |
1065 | if (dw.name_has_wildcard(function_str_val)) | |
1066 | { | |
2e67a43b | 1067 | symbol_table::iterator_t iter; |
1c6b77e5 JS |
1068 | for (iter = sym_table->map_by_addr.begin(); |
1069 | iter != sym_table->map_by_addr.end(); | |
2e67a43b | 1070 | ++iter) |
5f0a03a6 | 1071 | { |
1c6b77e5 | 1072 | fi = iter->second; |
5f0a03a6 JK |
1073 | if (!null_die(&fi->die)) |
1074 | continue; // already handled in query_module_dwarf() | |
1075 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
91bb9081 | 1076 | query_symtab_func_info(*fi, this); |
5f0a03a6 JK |
1077 | } |
1078 | } | |
1079 | else | |
1080 | { | |
1081 | fi = sym_table->lookup_symbol(function_str_val); | |
2867a2a1 | 1082 | if (fi && !fi->descriptor && null_die(&fi->die)) |
91bb9081 | 1083 | query_symtab_func_info(*fi, this); |
5f0a03a6 | 1084 | } |
5f0a03a6 JK |
1085 | } |
1086 | } | |
1087 | ||
1088 | void | |
1089 | dwarf_query::handle_query_module() | |
1090 | { | |
f4faaf86 JS |
1091 | if (has_plt && has_statement_num) |
1092 | { | |
1093 | query_plt_statement (this); | |
1094 | return; | |
1095 | } | |
1096 | ||
0035051e | 1097 | bool report = dbinfo_reqt == dbr_need_dwarf; |
1c6b77e5 JS |
1098 | dw.get_module_dwarf(false, report); |
1099 | ||
1100 | // prebuild the symbol table to resolve aliases | |
1101 | dw.mod_info->get_symtab(this); | |
1102 | ||
857bdfd1 JS |
1103 | // reset the dupe-checking for each new module |
1104 | alias_dupes.clear(); | |
7fdd3e2c | 1105 | inline_dupes.clear(); |
857bdfd1 | 1106 | |
5f0a03a6 JK |
1107 | if (dw.mod_info->dwarf_status == info_present) |
1108 | query_module_dwarf(); | |
1c6b77e5 | 1109 | |
91bb9081 LB |
1110 | // Consult the symbol table, asm and weak functions can show up |
1111 | // in the symbol table but not in dwarf and minidebuginfo is | |
1112 | // located in the gnu_debugdata section, alias_dupes checking | |
1113 | // is done before adding any probe points | |
1114 | if (!query_done && !pending_interrupts) | |
5f0a03a6 JK |
1115 | query_module_symtab(); |
1116 | } | |
1117 | ||
2c384610 | 1118 | |
7d6d0afc JS |
1119 | void |
1120 | dwarf_query::parse_function_spec(const string & spec) | |
bd2b1e68 | 1121 | { |
1d12a9b2 JS |
1122 | line_type = ABSOLUTE; |
1123 | line[0] = line[1] = 0; | |
1124 | ||
91699a70 | 1125 | size_t src_pos, line_pos, dash_pos, scope_pos; |
bd2b1e68 | 1126 | |
7d6d0afc | 1127 | // look for named scopes |
91699a70 JS |
1128 | scope_pos = spec.rfind("::"); |
1129 | if (scope_pos != string::npos) | |
bd2b1e68 | 1130 | { |
91699a70 JS |
1131 | tokenize_cxx(spec.substr(0, scope_pos), scopes); |
1132 | scope_pos += 2; | |
bd2b1e68 | 1133 | } |
91699a70 JS |
1134 | else |
1135 | scope_pos = 0; | |
bd2b1e68 | 1136 | |
7d6d0afc JS |
1137 | // look for a source separator |
1138 | src_pos = spec.find('@', scope_pos); | |
1139 | if (src_pos == string::npos) | |
bd2b1e68 | 1140 | { |
7d6d0afc JS |
1141 | function = spec.substr(scope_pos); |
1142 | spec_type = function_alone; | |
bd2b1e68 | 1143 | } |
7d6d0afc | 1144 | else |
879eb9e9 | 1145 | { |
7d6d0afc | 1146 | function = spec.substr(scope_pos, src_pos - scope_pos); |
7a053d3b | 1147 | |
7d6d0afc JS |
1148 | // look for a line-number separator |
1149 | line_pos = spec.find_first_of(":+", src_pos); | |
1150 | if (line_pos == string::npos) | |
1151 | { | |
1152 | file = spec.substr(src_pos + 1); | |
1153 | spec_type = function_and_file; | |
1154 | } | |
1155 | else | |
1156 | { | |
1157 | file = spec.substr(src_pos + 1, line_pos - src_pos - 1); | |
1158 | ||
1159 | // classify the line spec | |
1160 | spec_type = function_file_and_line; | |
1161 | if (spec[line_pos] == '+') | |
1162 | line_type = RELATIVE; | |
1163 | else if (spec[line_pos + 1] == '*' && | |
1164 | spec.length() == line_pos + 2) | |
1165 | line_type = WILDCARD; | |
1166 | else | |
1167 | line_type = ABSOLUTE; | |
1168 | ||
1169 | if (line_type != WILDCARD) | |
1170 | try | |
1171 | { | |
1172 | // try to parse either N or N-M | |
1173 | dash_pos = spec.find('-', line_pos + 1); | |
1174 | if (dash_pos == string::npos) | |
1175 | line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1)); | |
1176 | else | |
1177 | { | |
1178 | line_type = RANGE; | |
1179 | line[0] = lex_cast<int>(spec.substr(line_pos + 1, | |
1180 | dash_pos - line_pos - 1)); | |
1181 | line[1] = lex_cast<int>(spec.substr(dash_pos + 1)); | |
1182 | } | |
1183 | } | |
1184 | catch (runtime_error & exn) | |
1185 | { | |
1186 | goto bad; | |
1187 | } | |
1188 | } | |
bd2b1e68 GH |
1189 | } |
1190 | ||
7d6d0afc JS |
1191 | if (function.empty() || |
1192 | (spec_type != function_alone && file.empty())) | |
bd2b1e68 GH |
1193 | goto bad; |
1194 | ||
7d6d0afc | 1195 | if (sess.verbose > 2) |
bd2b1e68 | 1196 | { |
b530b5b3 LB |
1197 | //clog << "parsed '" << spec << "'"; |
1198 | clog << _F("parse '%s'", spec.c_str()); | |
41c262f3 | 1199 | |
7d6d0afc JS |
1200 | if (!scopes.empty()) |
1201 | clog << ", scope '" << scopes[0] << "'"; | |
1202 | for (unsigned i = 1; i < scopes.size(); ++i) | |
1203 | clog << "::'" << scopes[i] << "'"; | |
41c262f3 | 1204 | |
7d6d0afc JS |
1205 | clog << ", func '" << function << "'"; |
1206 | ||
1207 | if (spec_type != function_alone) | |
1208 | clog << ", file '" << file << "'"; | |
1209 | ||
1210 | if (spec_type == function_file_and_line) | |
1211 | { | |
1212 | clog << ", line "; | |
1213 | switch (line_type) | |
1214 | { | |
1215 | case ABSOLUTE: | |
1216 | clog << line[0]; | |
1217 | break; | |
1218 | ||
1219 | case RELATIVE: | |
1220 | clog << "+" << line[0]; | |
1221 | break; | |
1222 | ||
1223 | case RANGE: | |
1224 | clog << line[0] << " - " << line[1]; | |
1225 | break; | |
1226 | ||
1227 | case WILDCARD: | |
1228 | clog << "*"; | |
1229 | break; | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | clog << endl; | |
bd2b1e68 GH |
1234 | } |
1235 | ||
7d6d0afc JS |
1236 | return; |
1237 | ||
1238 | bad: | |
dc09353a | 1239 | throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()), |
7d6d0afc | 1240 | base_probe->tok); |
bd2b1e68 GH |
1241 | } |
1242 | ||
05fb3e0c WF |
1243 | string path_remove_sysroot(const systemtap_session& sess, const string& path) |
1244 | { | |
1245 | size_t pos; | |
1246 | string retval = path; | |
1247 | if (!sess.sysroot.empty() && | |
1248 | (pos = retval.find(sess.sysroot)) != string::npos) | |
1249 | retval.replace(pos, sess.sysroot.length(), "/"); | |
1250 | return retval; | |
1251 | } | |
bd2b1e68 | 1252 | |
36f9dd1d | 1253 | void |
1ffb8bd1 | 1254 | dwarf_query::add_probe_point(const string& dw_funcname, |
b20febf3 | 1255 | const char* filename, |
36f9dd1d | 1256 | int line, |
b20febf3 | 1257 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
1258 | Dwarf_Addr addr) |
1259 | { | |
b20febf3 | 1260 | string reloc_section; // base section for relocation purposes |
27646582 | 1261 | Dwarf_Addr reloc_addr; // relocated |
b20febf3 | 1262 | const string& module = dw.module_name; // "kernel" or other |
1ffb8bd1 | 1263 | string funcname = dw_funcname; |
36f9dd1d | 1264 | |
37ebca01 FCE |
1265 | assert (! has_absolute); // already handled in dwarf_builder::build() |
1266 | ||
f4faaf86 | 1267 | reloc_addr = dw.relocate_address(addr, reloc_section); |
2930abc7 | 1268 | |
1ffb8bd1 | 1269 | // If we originally used the linkage name, then let's call it that way |
1ffb8bd1 | 1270 | const char* linkage_name; |
91bb9081 | 1271 | if (!null_die(scope_die) && startswith (this->function, "_Z") |
f450a7e3 | 1272 | && (linkage_name = dwarf_linkage_name (scope_die))) |
1ffb8bd1 JS |
1273 | funcname = linkage_name; |
1274 | ||
7f9f3386 FCE |
1275 | if (sess.verbose > 1) |
1276 | { | |
b530b5b3 | 1277 | clog << _("probe ") << funcname << "@" << filename << ":" << line; |
b20febf3 | 1278 | if (string(module) == TOK_KERNEL) |
b530b5b3 | 1279 | clog << _(" kernel"); |
91af0778 | 1280 | else if (has_module) |
b530b5b3 | 1281 | clog << _(" module=") << module; |
91af0778 | 1282 | else if (has_process) |
b530b5b3 | 1283 | clog << _(" process=") << module; |
b20febf3 | 1284 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
b20febf3 | 1285 | clog << " pc=0x" << hex << addr << dec; |
7f9f3386 | 1286 | } |
4baf0e53 | 1287 | |
27646582 | 1288 | bool bad = dw.blacklisted_p (funcname, filename, line, module, |
789448a3 | 1289 | addr, has_return); |
b20febf3 FCE |
1290 | if (sess.verbose > 1) |
1291 | clog << endl; | |
7f9f3386 | 1292 | |
84048984 FCE |
1293 | if (module == TOK_KERNEL) |
1294 | { | |
1295 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
1296 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 1297 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
1298 | } |
1299 | ||
b20febf3 FCE |
1300 | if (! bad) |
1301 | { | |
1a0dbc5a | 1302 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
1303 | |
1304 | if (has_process) | |
1305 | { | |
05fb3e0c | 1306 | string module_tgt = path_remove_sysroot(sess, module); |
6d0f3f0c | 1307 | results.push_back (new uprobe_derived_probe(funcname, filename, line, |
05fb3e0c | 1308 | module_tgt, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1309 | *this, scope_die)); |
1310 | } | |
1311 | else | |
1312 | { | |
1313 | assert (has_kernel || has_module); | |
1314 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 1315 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1316 | *this, scope_die)); |
1317 | } | |
b20febf3 | 1318 | } |
2930abc7 FCE |
1319 | } |
1320 | ||
5f0a03a6 JK |
1321 | enum dbinfo_reqt |
1322 | dwarf_query::assess_dbinfo_reqt() | |
1323 | { | |
1324 | if (has_absolute) | |
1325 | { | |
1326 | // kernel.statement(NUM).absolute | |
1327 | return dbr_none; | |
1328 | } | |
c31add50 | 1329 | if (has_inline || has_label || has_callee || has_callees_num) |
5f0a03a6 JK |
1330 | { |
1331 | // kernel.function("f").inline or module("m").function("f").inline | |
1332 | return dbr_need_dwarf; | |
1333 | } | |
1334 | if (has_function_str && spec_type == function_alone) | |
1335 | { | |
1336 | // kernel.function("f") or module("m").function("f") | |
1337 | return dbr_need_symtab; | |
1338 | } | |
1339 | if (has_statement_num) | |
1340 | { | |
1341 | // kernel.statement(NUM) or module("m").statement(NUM) | |
1342 | // Technically, all we need is the module offset (or _stext, for | |
1343 | // the kernel). But for that we need either the ELF file or (for | |
1344 | // _stext) the symbol table. In either case, the symbol table | |
1345 | // is available, and that allows us to map the NUM (address) | |
1346 | // to a function, which is goodness. | |
1347 | return dbr_need_symtab; | |
1348 | } | |
1349 | if (has_function_num) | |
1350 | { | |
1351 | // kernel.function(NUM) or module("m").function(NUM) | |
1352 | // Need the symbol table so we can back up from NUM to the | |
1353 | // start of the function. | |
1354 | return dbr_need_symtab; | |
1355 | } | |
1356 | // Symbol table tells us nothing about source files or line numbers. | |
1357 | return dbr_need_dwarf; | |
1358 | } | |
2930abc7 | 1359 | |
e772a6e7 JL |
1360 | string |
1361 | dwarf_query::final_function_name(const string& final_func, | |
1362 | const string& final_file, | |
1363 | int final_line) | |
1364 | { | |
1365 | string final_name = final_func; | |
1366 | if (final_file != "") | |
1367 | { | |
1368 | final_name += ("@" + final_file); | |
1369 | if (final_line > 0) | |
1370 | final_name += (":" + lex_cast(final_line)); | |
1371 | } | |
1372 | return final_name; | |
1373 | } | |
2930abc7 | 1374 | |
b8da0ad1 FCE |
1375 | // The critical determining factor when interpreting a pattern |
1376 | // string is, perhaps surprisingly: "presence of a lineno". The | |
1377 | // presence of a lineno changes the search strategy completely. | |
1378 | // | |
1379 | // Compare the two cases: | |
1380 | // | |
1381 | // 1. {statement,function}(foo@file.c:lineno) | |
1382 | // - find the files matching file.c | |
1383 | // - in each file, find the functions matching foo | |
1384 | // - query the file for line records matching lineno | |
1385 | // - iterate over the line records, | |
1386 | // - and iterate over the functions, | |
1387 | // - if(haspc(function.DIE, line.addr)) | |
1388 | // - if looking for statements: probe(lineno.addr) | |
1389 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
1390 | // | |
1391 | // 2. {statement,function}(foo@file.c) | |
1392 | // - find the files matching file.c | |
1393 | // - in each file, find the functions matching foo | |
1394 | // - probe(function.{entrypc,return,etc.}) | |
1395 | // | |
1396 | // Thus the first decision we make is based on the presence of a | |
1397 | // lineno, and we enter entirely different sets of callbacks | |
1398 | // depending on that decision. | |
1399 | // | |
1400 | // Note that the first case is a generalization fo the second, in that | |
1401 | // we could theoretically search through line records for matching | |
1402 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
1403 | // are already cached elsewhere, so we can do an "index scan" as an | |
1404 | // optimization. | |
7e1279ea | 1405 | |
bd2b1e68 | 1406 | static void |
4cd232e4 | 1407 | query_statement (string const & func, |
20e4a32c | 1408 | char const * file, |
4cd232e4 | 1409 | int line, |
bcc12710 | 1410 | Dwarf_Die *scope_die, |
20e4a32c | 1411 | Dwarf_Addr stmt_addr, |
4cd232e4 | 1412 | dwarf_query * q) |
bd2b1e68 | 1413 | { |
39bcd429 FCE |
1414 | try |
1415 | { | |
cee35f73 | 1416 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 1417 | line, scope_die, stmt_addr); |
39bcd429 FCE |
1418 | } |
1419 | catch (const semantic_error& e) | |
1420 | { | |
1421 | q->sess.print_error (e); | |
1422 | } | |
bd2b1e68 GH |
1423 | } |
1424 | ||
6b517475 JS |
1425 | static void |
1426 | query_addr(Dwarf_Addr addr, dwarf_query *q) | |
1427 | { | |
1428 | dwflpp &dw = q->dw; | |
1429 | ||
08d1d520 MW |
1430 | if (q->sess.verbose > 2) |
1431 | clog << "query_addr 0x" << hex << addr << dec << endl; | |
6b517475 JS |
1432 | |
1433 | // First pick which CU contains this address | |
1434 | Dwarf_Die* cudie = dw.query_cu_containing_address(addr); | |
1435 | if (!cudie) // address could be wildly out of range | |
1436 | return; | |
1437 | dw.focus_on_cu(cudie); | |
1438 | ||
1439 | // Now compensate for the dw bias | |
1440 | addr -= dw.module_bias; | |
1441 | ||
1442 | // Per PR5787, we look up the scope die even for | |
1443 | // statement_num's, for blacklist sensitivity and $var | |
1444 | // resolution purposes. | |
1445 | ||
1446 | // Find the scopes containing this address | |
1447 | vector<Dwarf_Die> scopes = dw.getscopes(addr); | |
1448 | if (scopes.empty()) | |
1449 | return; | |
1450 | ||
1451 | // Look for the innermost containing function | |
1452 | Dwarf_Die *fnscope = NULL; | |
1453 | for (size_t i = 0; i < scopes.size(); ++i) | |
1454 | { | |
1455 | int tag = dwarf_tag(&scopes[i]); | |
1456 | if ((tag == DW_TAG_subprogram && !q->has_inline) || | |
1457 | (tag == DW_TAG_inlined_subroutine && | |
4bda987e | 1458 | !q->has_call && !q->has_return && !q->has_exported)) |
6b517475 JS |
1459 | { |
1460 | fnscope = &scopes[i]; | |
1461 | break; | |
1462 | } | |
1463 | } | |
1464 | if (!fnscope) | |
1465 | return; | |
1466 | dw.focus_on_function(fnscope); | |
1467 | ||
1468 | Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0]; | |
1469 | ||
1470 | const char *file = dwarf_decl_file(fnscope); | |
1471 | int line; | |
1472 | dwarf_decl_line(fnscope, &line); | |
1473 | ||
1474 | // Function probes should reset the addr to the function entry | |
1475 | // and possibly perform prologue searching | |
1476 | if (q->has_function_num) | |
1477 | { | |
1bbf3f90 JS |
1478 | if (!dw.die_entrypc(fnscope, &addr)) |
1479 | return; | |
6b517475 | 1480 | if (dwarf_tag(fnscope) == DW_TAG_subprogram && |
b5b9879f JL |
1481 | (q->sess.prologue_searching || |
1482 | (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941 | |
6b517475 JS |
1483 | { |
1484 | func_info func; | |
1485 | func.die = *fnscope; | |
1486 | func.name = dw.function_name; | |
1487 | func.decl_file = file; | |
1488 | func.decl_line = line; | |
1489 | func.entrypc = addr; | |
1490 | ||
1491 | func_info_map_t funcs(1, func); | |
1492 | dw.resolve_prologue_endings (funcs); | |
5a617dc6 JL |
1493 | q->prologue_end = funcs[0].prologue_end; |
1494 | ||
1495 | // PR13200: if it's a .return probe, we need to emit a *retprobe based | |
1496 | // on the entrypc so here we only use prologue_end for non .return | |
1497 | // probes (note however that .return probes still take advantage of | |
1498 | // prologue_end: PR14436) | |
1499 | if (!q->has_return) | |
1500 | addr = funcs[0].prologue_end; | |
6b517475 JS |
1501 | } |
1502 | } | |
1503 | else | |
1504 | { | |
1505 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr)); | |
1506 | if (address_line) | |
1507 | { | |
1508 | file = address_line.linesrc(); | |
1509 | line = address_line.lineno(); | |
1510 | } | |
1511 | ||
1512 | // Verify that a raw address matches the beginning of a | |
1513 | // statement. This is a somewhat lame check that the address | |
1514 | // is at the start of an assembly instruction. Mark probes are in the | |
1515 | // middle of a macro and thus not strictly at a statement beginning. | |
1516 | // Guru mode may override this check. | |
1517 | if (!q->has_mark && (!address_line || address_line.addr() != addr)) | |
1518 | { | |
1519 | stringstream msg; | |
2a97f50b | 1520 | msg << _F("address %#" PRIx64 " does not match the beginning of a statement", |
b530b5b3 | 1521 | addr); |
6b517475 | 1522 | if (address_line) |
2a97f50b | 1523 | msg << _F(" (try %#" PRIx64 ")", address_line.addr()); |
6b517475 | 1524 | else |
b530b5b3 LB |
1525 | msg << _F(" (no line info found for '%s', in module '%s')", |
1526 | dw.cu_name().c_str(), dw.module_name.c_str()); | |
6b517475 | 1527 | if (! q->sess.guru_mode) |
dc09353a | 1528 | throw SEMANTIC_ERROR(msg.str()); |
2713ea24 | 1529 | else |
6b517475 JS |
1530 | q->sess.print_warning(msg.str()); |
1531 | } | |
1532 | } | |
1533 | ||
1534 | // Build a probe at this point | |
1535 | query_statement(dw.function_name, file, line, scope, addr, q); | |
1536 | } | |
1537 | ||
f4faaf86 JS |
1538 | static void |
1539 | query_plt_statement(dwarf_query *q) | |
1540 | { | |
1541 | assert (q->has_plt && q->has_statement_num); | |
1542 | ||
1543 | Dwarf_Addr addr = q->statement_num_val; | |
1544 | if (q->sess.verbose > 2) | |
1545 | clog << "query_plt_statement 0x" << hex << addr << dec << endl; | |
1546 | ||
1547 | // First adjust the raw address to dwfl's elf bias. | |
1548 | Dwarf_Addr elf_bias; | |
1549 | Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias); | |
1550 | assert(elf); | |
1551 | addr += elf_bias; | |
1552 | ||
1553 | // Now compensate for the dw bias | |
1554 | q->dw.get_module_dwarf(false, false); | |
1555 | addr -= q->dw.module_bias; | |
1556 | ||
1557 | // Build a probe at this point | |
1558 | query_statement(q->plt_val, NULL, -1, NULL, addr, q); | |
1559 | } | |
1560 | ||
8096dd7d JS |
1561 | static void |
1562 | query_label (string const & func, | |
1563 | char const * label, | |
1564 | char const * file, | |
1565 | int line, | |
1566 | Dwarf_Die *scope_die, | |
1567 | Dwarf_Addr stmt_addr, | |
1568 | dwarf_query * q) | |
1569 | { | |
6b517475 JS |
1570 | assert (q->has_statement_str || q->has_function_str); |
1571 | ||
8096dd7d JS |
1572 | size_t i = q->results.size(); |
1573 | ||
1574 | // weed out functions whose decl_file isn't one of | |
1575 | // the source files that we actually care about | |
6b517475 | 1576 | if (q->spec_type != function_alone && |
8096dd7d JS |
1577 | q->filtered_srcfiles.count(file) == 0) |
1578 | return; | |
1579 | ||
1580 | query_statement(func, file, line, scope_die, stmt_addr, q); | |
1581 | ||
c72aa911 JS |
1582 | // after the fact, insert the label back into the derivation chain |
1583 | probe_point::component* ppc = | |
1584 | new probe_point::component(TOK_LABEL, new literal_string (label)); | |
1585 | for (; i < q->results.size(); ++i) | |
1586 | { | |
1587 | derived_probe* p = q->results[i]; | |
1588 | probe_point* pp = new probe_point(*p->locations[0]); | |
1589 | pp->components.push_back (ppc); | |
1590 | p->base = p->base->create_alias(p->locations[0], pp); | |
1591 | } | |
8096dd7d JS |
1592 | } |
1593 | ||
67959c62 | 1594 | static void |
e772a6e7 JL |
1595 | query_callee (base_func_info& callee, |
1596 | base_func_info& caller, | |
67959c62 JL |
1597 | stack<Dwarf_Addr> *callers, |
1598 | dwarf_query * q) | |
1599 | { | |
1600 | assert (q->has_function_str); | |
e772a6e7 JL |
1601 | assert (q->has_callee || q->has_callees_num); |
1602 | ||
1603 | // OK, we found a callee for a targeted caller. To help users see the | |
1604 | // derivation, let's add an intermediate probe to the chain. E.g. right now we | |
1605 | // have .function(pattern).callee(pattern) (or .callees) and we want to add as | |
1606 | // the next step .function(caller).callee(callee). This is the step that will | |
1607 | // show up as well when in listing mode. | |
1608 | ||
1609 | // To add this intermediate probe, we first create it and then create a new | |
1610 | // dwarf_query with that probe as base. | |
1611 | ||
1612 | // Build final names for caller and callee | |
1613 | string canon_caller = q->final_function_name(caller.name, caller.decl_file, | |
1614 | caller.decl_line); | |
1615 | string canon_callee = q->final_function_name(callee.name, callee.decl_file, | |
1616 | callee.decl_line); | |
1617 | ||
1618 | // Build the intermediate probe. We replace the function arg with the final | |
1619 | // caller, and the callee arg with the final callee. Also, since the | |
1620 | // process(str) or module(str) currently in the base_loc is still the one the | |
1621 | // user typed in (not necessarily the same as the absolute path we get in the | |
1622 | // sole location), we also replace that (otherwise, function(str).* will | |
1623 | // return a mishmash of absolute and relative paths in listing mode). | |
1624 | ||
1625 | string module = q->dw.module_name; | |
1626 | if (q->has_process) | |
1627 | module = path_remove_sysroot(q->sess, module); | |
1628 | ||
1629 | probe_point *pp = new probe_point(*q->base_loc); | |
1630 | vector<probe_point::component*> pp_comps; | |
1631 | vector<probe_point::component*>::iterator it; | |
1632 | for (it = pp->components.begin(); it != pp->components.end(); ++it) | |
1633 | { | |
1634 | if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE) | |
1635 | pp_comps.push_back(new probe_point::component((*it)->functor, | |
1636 | new literal_string(module))); | |
1637 | else if ((*it)->functor == TOK_FUNCTION) | |
1638 | pp_comps.push_back(new probe_point::component(TOK_FUNCTION, | |
1639 | new literal_string(canon_caller))); | |
1640 | else if ((*it)->functor == TOK_CALLEE || (*it)->functor == TOK_CALLEES) | |
1641 | pp_comps.push_back(new probe_point::component(TOK_CALLEE, | |
1642 | new literal_string(canon_callee))); | |
1643 | else | |
1644 | pp_comps.push_back(*it); | |
1645 | } | |
1646 | ||
1647 | // Finally create the new probe and the new query (use copy-constructor | |
1648 | // instead of creating from scratch so we don't have to rebuild param map) | |
1649 | pp->components = pp_comps; | |
1650 | dwarf_query q_callee(*q); | |
1651 | q_callee.base_loc = pp; | |
1652 | q_callee.base_probe = new probe(q->base_probe, pp); | |
1653 | ||
1654 | // Pass on the callers we'll need to add checks for | |
1655 | q_callee.callers = callers; | |
1656 | ||
1657 | query_statement(callee.name, callee.decl_file, callee.decl_line, | |
1658 | &callee.die, callee.entrypc, &q_callee); | |
67959c62 JL |
1659 | } |
1660 | ||
7e1279ea | 1661 | static void |
3e961ba6 | 1662 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
1663 | dwarf_query * q) |
1664 | { | |
b6581717 | 1665 | try |
7e1279ea | 1666 | { |
8f14e444 FCE |
1667 | assert (! q->has_return); // checked by caller already |
1668 | if (q->sess.verbose>2) | |
b530b5b3 LB |
1669 | clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n", |
1670 | ii.entrypc, ii.name.c_str()); | |
8f14e444 FCE |
1671 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
1672 | &ii.die, ii.entrypc, q); | |
7e1279ea | 1673 | } |
b6581717 | 1674 | catch (semantic_error &e) |
7e1279ea | 1675 | { |
b6581717 | 1676 | q->sess.print_error (e); |
7e1279ea FCE |
1677 | } |
1678 | } | |
1679 | ||
1680 | static void | |
1681 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 1682 | func_info & fi, |
7e1279ea FCE |
1683 | dwarf_query * q) |
1684 | { | |
b6581717 | 1685 | try |
7e1279ea | 1686 | { |
5a617dc6 JL |
1687 | // If it's a .return probe, we need to emit a *retprobe based on the |
1688 | // entrypc (PR13200). Note however that if prologue_end is valid, | |
1689 | // dwarf_derived_probe will still take advantage of it by creating a new | |
1690 | // probe there if necessary to pick up target vars (PR14436). | |
1691 | if (fi.prologue_end == 0 || q->has_return) | |
1692 | { | |
170ccf10 | 1693 | q->prologue_end = fi.prologue_end; |
5a617dc6 JL |
1694 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1695 | &fi.die, entrypc, q); | |
1696 | } | |
b6581717 | 1697 | else |
5a617dc6 | 1698 | { |
5a617dc6 JL |
1699 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1700 | &fi.die, fi.prologue_end, q); | |
1701 | } | |
7e1279ea | 1702 | } |
b6581717 | 1703 | catch (semantic_error &e) |
7e1279ea | 1704 | { |
b6581717 | 1705 | q->sess.print_error (e); |
7e1279ea FCE |
1706 | } |
1707 | } | |
1708 | ||
1709 | ||
bd4b874d | 1710 | static void |
0e2e28a1 | 1711 | query_srcfile_label (const dwarf_line_t& line, dwarf_query * q) |
bd4b874d | 1712 | { |
bd4b874d SC |
1713 | Dwarf_Addr addr = line.addr(); |
1714 | ||
1715 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1716 | i != q->filtered_functions.end(); ++i) | |
1717 | if (q->dw.die_has_pc (i->die, addr)) | |
f09d0d1e JS |
1718 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, |
1719 | q, query_label); | |
1720 | ||
1721 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1722 | i != q->filtered_inlines.end(); ++i) | |
1723 | if (q->dw.die_has_pc (i->die, addr)) | |
1724 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1725 | q, query_label); | |
bd4b874d SC |
1726 | } |
1727 | ||
7e1279ea | 1728 | static void |
0e2e28a1 | 1729 | query_srcfile_line (const dwarf_line_t& line, dwarf_query * q) |
7e1279ea | 1730 | { |
86bf665e | 1731 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 1732 | |
86bf665e | 1733 | int lineno = line.lineno(); |
847bf07f | 1734 | |
86bf665e | 1735 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
1736 | i != q->filtered_functions.end(); ++i) |
1737 | { | |
3e961ba6 | 1738 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1739 | { |
b0ee93c4 | 1740 | if (q->sess.verbose>3) |
b530b5b3 | 1741 | clog << _("function DIE lands on srcfile\n"); |
4cd232e4 | 1742 | if (q->has_statement_str) |
f5958c8f JS |
1743 | { |
1744 | Dwarf_Die scope; | |
1745 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1746 | query_statement (i->name, i->decl_file, | |
1747 | lineno, // NB: not q->line ! | |
1748 | &scope, addr, q); | |
1749 | } | |
4cd232e4 | 1750 | else |
3e961ba6 | 1751 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 1752 | } |
20e4a32c RM |
1753 | } |
1754 | ||
86bf665e | 1755 | for (inline_instance_map_t::iterator i |
897820ca GH |
1756 | = q->filtered_inlines.begin(); |
1757 | i != q->filtered_inlines.end(); ++i) | |
1758 | { | |
3e961ba6 | 1759 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1760 | { |
b0ee93c4 | 1761 | if (q->sess.verbose>3) |
b530b5b3 | 1762 | clog << _("inline instance DIE lands on srcfile\n"); |
897820ca | 1763 | if (q->has_statement_str) |
f5958c8f JS |
1764 | { |
1765 | Dwarf_Die scope; | |
1766 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1767 | query_statement (i->name, i->decl_file, | |
d807e0ab JS |
1768 | lineno, // NB: not q->line ! |
1769 | &scope, addr, q); | |
f5958c8f | 1770 | } |
897820ca | 1771 | else |
3e961ba6 | 1772 | query_inline_instance_info (*i, q); |
897820ca | 1773 | } |
20e4a32c | 1774 | } |
7e1279ea FCE |
1775 | } |
1776 | ||
1777 | ||
7fdd3e2c JS |
1778 | bool |
1779 | inline_instance_info::operator<(const inline_instance_info& other) const | |
1780 | { | |
1781 | if (entrypc != other.entrypc) | |
1782 | return entrypc < other.entrypc; | |
1783 | ||
1784 | if (decl_line != other.decl_line) | |
1785 | return decl_line < other.decl_line; | |
1786 | ||
1787 | int cmp = name.compare(other.name); | |
c60517ca FCE |
1788 | |
1789 | if (!cmp) | |
1790 | { | |
1791 | assert (decl_file); | |
1792 | assert (other.decl_file); | |
1793 | cmp = strcmp(decl_file, other.decl_file); | |
1794 | } | |
1795 | ||
7fdd3e2c JS |
1796 | return cmp < 0; |
1797 | } | |
1798 | ||
1799 | ||
4fa7b22b | 1800 | static int |
3956fccb | 1801 | query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q) |
4fa7b22b | 1802 | { |
6b517475 | 1803 | assert (q->has_statement_str || q->has_function_str); |
4bda987e | 1804 | assert (!q->has_call && !q->has_return && !q->has_exported); |
bd2b1e68 | 1805 | |
39bcd429 | 1806 | try |
7a053d3b | 1807 | { |
b0ee93c4 | 1808 | if (q->sess.verbose>2) |
b530b5b3 | 1809 | clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str()); |
7e1279ea | 1810 | |
6b517475 JS |
1811 | Dwarf_Addr entrypc; |
1812 | if (q->dw.die_entrypc (die, &entrypc)) | |
1813 | { | |
1814 | inline_instance_info inl; | |
1815 | inl.die = *die; | |
1816 | inl.name = q->dw.function_name; | |
1817 | inl.entrypc = entrypc; | |
1818 | q->dw.function_file (&inl.decl_file); | |
1819 | q->dw.function_line (&inl.decl_line); | |
1820 | ||
1821 | // make sure that this inline hasn't already | |
1822 | // been matched from a different CU | |
1823 | if (q->inline_dupes.insert(inl).second) | |
1824 | q->filtered_inlines.push_back(inl); | |
1825 | } | |
7e1279ea FCE |
1826 | return DWARF_CB_OK; |
1827 | } | |
1828 | catch (const semantic_error& e) | |
1829 | { | |
1830 | q->sess.print_error (e); | |
1831 | return DWARF_CB_ABORT; | |
1832 | } | |
1833 | } | |
bb788f9f | 1834 | |
7e1279ea | 1835 | static int |
7d007451 | 1836 | query_dwarf_func (Dwarf_Die * func, dwarf_query * q) |
7e1279ea | 1837 | { |
6b517475 | 1838 | assert (q->has_statement_str || q->has_function_str); |
bb788f9f | 1839 | |
bd25380d JS |
1840 | // weed out functions whose decl_file isn't one of |
1841 | // the source files that we actually care about | |
6b517475 | 1842 | if (q->spec_type != function_alone && |
bd25380d | 1843 | q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0) |
8096dd7d | 1844 | return DWARF_CB_OK; |
bd25380d | 1845 | |
7e1279ea FCE |
1846 | try |
1847 | { | |
7e1279ea FCE |
1848 | q->dw.focus_on_function (func); |
1849 | ||
7d6d0afc JS |
1850 | if (!q->dw.function_scope_matches(q->scopes)) |
1851 | return DWARF_CB_OK; | |
1852 | ||
857bdfd1 JS |
1853 | // make sure that this function address hasn't |
1854 | // already been matched under an aliased name | |
1855 | Dwarf_Addr addr; | |
1856 | if (!q->dw.func_is_inline() && | |
1857 | dwarf_entrypc(func, &addr) == 0 && | |
1858 | !q->alias_dupes.insert(addr).second) | |
1859 | return DWARF_CB_OK; | |
1860 | ||
4bda987e | 1861 | if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported)) |
7e1279ea | 1862 | { |
4bda987e | 1863 | if (q->sess.verbose>3) |
b530b5b3 | 1864 | clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str()); |
4bda987e | 1865 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
7e1279ea | 1866 | } |
8f14e444 FCE |
1867 | else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553 |
1868 | { | |
1869 | q->inlined_non_returnable.insert (q->dw.function_name); | |
1870 | } | |
396afcee | 1871 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 1872 | { |
4bda987e SC |
1873 | if (q->has_exported && !q->dw.func_is_exported ()) |
1874 | return DWARF_CB_OK; | |
6b517475 | 1875 | if (q->sess.verbose>2) |
b530b5b3 | 1876 | clog << _F("selected function %s\n", q->dw.function_name.c_str()); |
6b517475 JS |
1877 | |
1878 | func_info func; | |
1879 | q->dw.function_die (&func.die); | |
1880 | func.name = q->dw.function_name; | |
1881 | q->dw.function_file (&func.decl_file); | |
1882 | q->dw.function_line (&func.decl_line); | |
1883 | ||
1884 | Dwarf_Addr entrypc; | |
1885 | if (q->dw.function_entrypc (&entrypc)) | |
1886 | { | |
1887 | func.entrypc = entrypc; | |
1888 | q->filtered_functions.push_back (func); | |
1889 | } | |
1890 | /* else this function is fully inlined, just ignore it */ | |
7e1279ea | 1891 | } |
39bcd429 | 1892 | return DWARF_CB_OK; |
bd2b1e68 | 1893 | } |
39bcd429 | 1894 | catch (const semantic_error& e) |
bd2b1e68 | 1895 | { |
39bcd429 FCE |
1896 | q->sess.print_error (e); |
1897 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1898 | } |
bd2b1e68 GH |
1899 | } |
1900 | ||
1901 | static int | |
5c378838 | 1902 | query_cu (Dwarf_Die * cudie, dwarf_query * q) |
bd2b1e68 | 1903 | { |
6b517475 JS |
1904 | assert (q->has_statement_str || q->has_function_str); |
1905 | ||
85007c04 | 1906 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 1907 | |
39bcd429 | 1908 | try |
bd2b1e68 | 1909 | { |
7e1279ea | 1910 | q->dw.focus_on_cu (cudie); |
b5d77020 | 1911 | |
b0ee93c4 | 1912 | if (false && q->sess.verbose>2) |
b530b5b3 LB |
1913 | clog << _F("focused on CU '%s', in module '%s'\n", |
1914 | q->dw.cu_name().c_str(), q->dw.module_name.c_str()); | |
d9b516ca | 1915 | |
6b517475 JS |
1916 | q->filtered_srcfiles.clear(); |
1917 | q->filtered_functions.clear(); | |
1918 | q->filtered_inlines.clear(); | |
1919 | ||
1920 | // In this path, we find "abstract functions", record | |
1921 | // information about them, and then (depending on lineno | |
1922 | // matching) possibly emit one or more of the function's | |
1923 | // associated addresses. Unfortunately the control of this | |
1924 | // cannot easily be turned inside out. | |
1925 | ||
1926 | if (q->spec_type != function_alone) | |
39bcd429 | 1927 | { |
6b517475 JS |
1928 | // If we have a pattern string with a filename, we need |
1929 | // to elaborate the srcfile mask in question first. | |
1930 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
1931 | ||
1932 | // If we have a file pattern and *no* srcfile matches, there's | |
1933 | // no need to look further into this CU, so skip. | |
1934 | if (q->filtered_srcfiles.empty()) | |
1935 | return DWARF_CB_OK; | |
1936 | } | |
e4c58386 | 1937 | |
6b517475 JS |
1938 | // Pick up [entrypc, name, DIE] tuples for all the functions |
1939 | // matching the query, and fill in the prologue endings of them | |
1940 | // all in a single pass. | |
5898b6e1 | 1941 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function); |
6b517475 JS |
1942 | if (rc != DWARF_CB_OK) |
1943 | q->query_done = true; | |
1944 | ||
277a2b96 JL |
1945 | if (!q->filtered_functions.empty() && |
1946 | !q->has_statement_str && // PR 2608 | |
1947 | (q->sess.prologue_searching || | |
1948 | (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941 | |
1949 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
6b517475 JS |
1950 | |
1951 | if (q->spec_type == function_file_and_line) | |
1952 | { | |
58b070fb | 1953 | // .statement(...:NN) often gets mixed up with .function(...:NN) |
2713ea24 | 1954 | if (q->has_function_str) |
b530b5b3 | 1955 | q->sess.print_warning (_("For probing a particular line, use a " |
b78a0fbb | 1956 | ".statement() probe, not .function()"), |
af2e341f | 1957 | q->base_probe->tok); |
58b070fb | 1958 | |
6b517475 JS |
1959 | // If we have a pattern string with target *line*, we |
1960 | // have to look at lines in all the matched srcfiles. | |
0e2e28a1 | 1961 | void (* callback) (const dwarf_line_t&, dwarf_query*) = |
6b517475 JS |
1962 | q->has_label ? query_srcfile_label : query_srcfile_line; |
1963 | for (set<string>::const_iterator i = q->filtered_srcfiles.begin(); | |
1964 | i != q->filtered_srcfiles.end(); ++i) | |
1965 | q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str, | |
1966 | q->line_type, callback, q->function, q); | |
1967 | } | |
1968 | else if (q->has_label) | |
1969 | { | |
1970 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1971 | i != q->filtered_functions.end(); ++i) | |
1972 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1973 | q, query_label); | |
1974 | ||
1975 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1976 | i != q->filtered_inlines.end(); ++i) | |
1977 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1978 | q, query_label); | |
39bcd429 | 1979 | } |
67959c62 JL |
1980 | else if (q->has_callee || q->has_callees_num) |
1981 | { | |
1982 | // .callee(str) --> str, .callees[(N)] --> "*" | |
1983 | string callee_val = q->has_callee ? q->callee_val : "*"; | |
1984 | long callees_num_val = q->has_callees_num ? q->callees_num_val : 1; | |
1985 | ||
1986 | // NB: We filter functions that do not match the file here rather than | |
1987 | // in query_callee because we only want the filtering to apply to the | |
1988 | // first level, not to callees that are recursed into if | |
1989 | // callees_num_val > 1. | |
1990 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1991 | i != q->filtered_functions.end(); ++i) | |
1992 | { | |
1993 | if (q->spec_type != function_alone && | |
1994 | q->filtered_srcfiles.count(i->decl_file) == 0) | |
1995 | continue; | |
1996 | q->dw.iterate_over_callees (&i->die, callee_val, | |
1997 | callees_num_val, | |
e772a6e7 | 1998 | q, query_callee, *i); |
67959c62 JL |
1999 | } |
2000 | ||
2001 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
2002 | i != q->filtered_inlines.end(); ++i) | |
2003 | { | |
2004 | if (q->spec_type != function_alone && | |
2005 | q->filtered_srcfiles.count(i->decl_file) == 0) | |
2006 | continue; | |
2007 | q->dw.iterate_over_callees (&i->die, callee_val, | |
2008 | callees_num_val, | |
e772a6e7 | 2009 | q, query_callee, *i); |
67959c62 JL |
2010 | } |
2011 | } | |
6b517475 JS |
2012 | else |
2013 | { | |
2014 | // Otherwise, simply probe all resolved functions. | |
2015 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
2016 | i != q->filtered_functions.end(); ++i) | |
2017 | query_func_info (i->entrypc, *i, q); | |
2018 | ||
2019 | // And all inline instances (if we're not excluding inlines with ".call") | |
2020 | if (! q->has_call) | |
2021 | for (inline_instance_map_t::iterator i | |
2022 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) | |
2023 | query_inline_instance_info (*i, q); | |
e772a6e7 | 2024 | } |
39bcd429 | 2025 | return DWARF_CB_OK; |
bd2b1e68 | 2026 | } |
39bcd429 | 2027 | catch (const semantic_error& e) |
bd2b1e68 | 2028 | { |
39bcd429 FCE |
2029 | q->sess.print_error (e); |
2030 | return DWARF_CB_ABORT; | |
bd2b1e68 | 2031 | } |
bd2b1e68 GH |
2032 | } |
2033 | ||
0ce64fb8 | 2034 | |
4df79aaf JS |
2035 | void |
2036 | dwarf_query::query_module_functions () | |
2037 | { | |
2038 | try | |
2039 | { | |
2040 | filtered_srcfiles.clear(); | |
2041 | filtered_functions.clear(); | |
2042 | filtered_inlines.clear(); | |
2043 | ||
2044 | // Collect all module functions so we know which CUs are interesting | |
2045 | int rc = dw.iterate_single_function(query_dwarf_func, this, function); | |
2046 | if (rc != DWARF_CB_OK) | |
2047 | { | |
2048 | query_done = true; | |
2049 | return; | |
2050 | } | |
2051 | ||
2052 | set<void*> used_cus; // by cu->addr | |
2053 | vector<Dwarf_Die> cus; | |
2054 | Dwarf_Die cu_mem; | |
2055 | ||
2056 | for (func_info_map_t::iterator i = filtered_functions.begin(); | |
2057 | i != filtered_functions.end(); ++i) | |
2058 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
2059 | used_cus.insert(cu_mem.addr).second) | |
2060 | cus.push_back(cu_mem); | |
2061 | ||
2062 | for (inline_instance_map_t::iterator i = filtered_inlines.begin(); | |
2063 | i != filtered_inlines.end(); ++i) | |
2064 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
2065 | used_cus.insert(cu_mem.addr).second) | |
2066 | cus.push_back(cu_mem); | |
2067 | ||
2068 | // Reset the dupes since we didn't actually collect them the first time | |
2069 | alias_dupes.clear(); | |
2070 | inline_dupes.clear(); | |
2071 | ||
2072 | // Run the query again on the individual CUs | |
91bb9081 LB |
2073 | for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){ |
2074 | rc = query_cu(&*i, this); | |
2075 | if (rc != DWARF_CB_OK) | |
2076 | { | |
2077 | query_done = true; | |
2078 | return; | |
2079 | } | |
2080 | } | |
4df79aaf JS |
2081 | } |
2082 | catch (const semantic_error& e) | |
2083 | { | |
2084 | sess.print_error (e); | |
2085 | } | |
2086 | } | |
2087 | ||
2088 | ||
5f0a03a6 JK |
2089 | static void |
2090 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
2091 | { | |
2092 | // Validate the machine code in this elf file against the | |
2093 | // session machine. This is important, in case the wrong kind | |
2094 | // of debuginfo is being automagically processed by elfutils. | |
2095 | // While we can tell i686 apart from x86-64, unfortunately | |
2096 | // we can't help confusing i586 vs i686 (both EM_386). | |
2097 | ||
2098 | Dwarf_Addr bias; | |
2099 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
2100 | // because dwfl_module_getelf can force costly section relocations | |
2101 | // we don't really need, while either will do for this purpose. | |
2102 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
2103 | ?: dwfl_module_getelf (mod, &bias)); | |
2104 | ||
2105 | GElf_Ehdr ehdr_mem; | |
2106 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 2107 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
d8f31d0a | 2108 | assert(em); |
5f0a03a6 JK |
2109 | int elf_machine = em->e_machine; |
2110 | const char* debug_filename = ""; | |
2111 | const char* main_filename = ""; | |
2112 | (void) dwfl_module_info (mod, NULL, NULL, | |
2113 | NULL, NULL, NULL, | |
2114 | & main_filename, | |
2115 | & debug_filename); | |
2116 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
2117 | |
2118 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
2119 | string expect_machine2; | |
5f0a03a6 | 2120 | |
d27e6fd5 | 2121 | // NB: See also the 'uname -m' squashing done in main.cxx. |
5f0a03a6 JK |
2122 | switch (elf_machine) |
2123 | { | |
756c9462 FCE |
2124 | // x86 and ppc are bi-architecture; a 64-bit kernel |
2125 | // can normally run either 32-bit or 64-bit *userspace*. | |
2126 | case EM_386: | |
2127 | expect_machine = "i?86"; | |
2128 | if (! q->has_process) break; // 32-bit kernel/module | |
2129 | /* FALLSTHROUGH */ | |
2130 | case EM_X86_64: | |
2131 | expect_machine2 = "x86_64"; | |
2132 | break; | |
2133 | case EM_PPC: | |
756c9462 | 2134 | case EM_PPC64: |
5a1c472e | 2135 | expect_machine = "powerpc"; |
756c9462 | 2136 | break; |
3fe7d888 | 2137 | case EM_S390: expect_machine = "s390"; break; |
5f0a03a6 | 2138 | case EM_IA_64: expect_machine = "ia64"; break; |
d27e6fd5 | 2139 | case EM_ARM: expect_machine = "arm*"; break; |
86aae990 | 2140 | case EM_AARCH64: expect_machine = "arm64"; break; |
5f0a03a6 JK |
2141 | // XXX: fill in some more of these |
2142 | default: expect_machine = "?"; break; | |
2143 | } | |
2144 | ||
2145 | if (! debug_filename) debug_filename = main_filename; | |
2146 | if (! debug_filename) debug_filename = name; | |
2147 | ||
756c9462 FCE |
2148 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
2149 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
2150 | { |
2151 | stringstream msg; | |
b530b5b3 LB |
2152 | msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'", |
2153 | expect_machine.c_str(), expect_machine2.c_str(), elf_machine, | |
2154 | sess_machine.c_str(), debug_filename); | |
dc09353a | 2155 | throw SEMANTIC_ERROR(msg.str ()); |
5f0a03a6 JK |
2156 | } |
2157 | ||
b57082de | 2158 | if (q->sess.verbose>1) |
b78a0fbb | 2159 | clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64 |
b530b5b3 LB |
2160 | " file %s ELF machine %s|%s (code %d)\n", |
2161 | q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end, | |
2162 | q->dw.module_bias, debug_filename, expect_machine.c_str(), | |
2163 | expect_machine2.c_str(), elf_machine); | |
5f0a03a6 | 2164 | } |
1d3a40b6 | 2165 | |
91af0778 FCE |
2166 | |
2167 | ||
2168 | static Dwarf_Addr | |
2169 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
2170 | { | |
2171 | int syments = dwfl_module_getsymtab(m); | |
2172 | assert(syments); | |
2173 | for (int i = 1; i < syments; ++i) | |
2174 | { | |
2175 | GElf_Sym sym; | |
2176 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
2177 | if (name != NULL && strcmp(name, wanted) == 0) | |
2178 | return sym.st_value; | |
2179 | } | |
2180 | ||
2181 | return 0; | |
2182 | } | |
2183 | ||
2184 | ||
2185 | ||
bd2b1e68 | 2186 | static int |
b8da0ad1 | 2187 | query_module (Dwfl_Module *mod, |
91af0778 | 2188 | void **, |
b8da0ad1 | 2189 | const char *name, |
6f4c1275 | 2190 | Dwarf_Addr addr, |
06de3a04 | 2191 | base_query *q) |
bd2b1e68 | 2192 | { |
39bcd429 | 2193 | try |
e38d6504 | 2194 | { |
91af0778 FCE |
2195 | module_info* mi = q->sess.module_cache->cache[name]; |
2196 | if (mi == 0) | |
2197 | { | |
2198 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
2199 | ||
6f4c1275 FCE |
2200 | mi->mod = mod; |
2201 | mi->addr = addr; | |
91af0778 | 2202 | |
6f4c1275 FCE |
2203 | const char* debug_filename = ""; |
2204 | const char* main_filename = ""; | |
2205 | (void) dwfl_module_info (mod, NULL, NULL, | |
2206 | NULL, NULL, NULL, | |
2207 | & main_filename, | |
2208 | & debug_filename); | |
2209 | ||
ab3ed72d | 2210 | if (debug_filename || main_filename) |
91af0778 | 2211 | { |
6f4c1275 FCE |
2212 | mi->elf_path = debug_filename ?: main_filename; |
2213 | } | |
2214 | else if (name == TOK_KERNEL) | |
2215 | { | |
2216 | mi->dwarf_status = info_absent; | |
91af0778 | 2217 | } |
91af0778 FCE |
2218 | } |
2219 | // OK, enough of that module_info caching business. | |
2220 | ||
5f0a03a6 | 2221 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 2222 | |
39bcd429 FCE |
2223 | // If we have enough information in the pattern to skip a module and |
2224 | // the module does not match that information, return early. | |
b8da0ad1 | 2225 | if (!q->dw.module_name_matches(q->module_val)) |
85007c04 | 2226 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
0cbbf9d1 FCE |
2227 | |
2228 | // Don't allow module("*kernel*") type expressions to match the | |
2229 | // elfutils module "kernel", which we refer to in the probe | |
2230 | // point syntax exclusively as "kernel.*". | |
2231 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
85007c04 | 2232 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
b5d77020 | 2233 | |
5f0a03a6 JK |
2234 | if (mod) |
2235 | validate_module_elf(mod, name, q); | |
2236 | else | |
91af0778 FCE |
2237 | assert(q->has_kernel); // and no vmlinux to examine |
2238 | ||
2239 | if (q->sess.verbose>2) | |
b530b5b3 | 2240 | cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str()); |
91af0778 FCE |
2241 | |
2242 | ||
2243 | // Collect a few kernel addresses. XXX: these belong better | |
2244 | // to the sess.module_info["kernel"] struct. | |
2245 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 2246 | { |
91af0778 FCE |
2247 | if (! q->sess.sym_kprobes_text_start) |
2248 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
2249 | if (! q->sess.sym_kprobes_text_end) | |
2250 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
2251 | if (! q->sess.sym_stext) | |
2252 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
2253 | } |
2254 | ||
47e226ed SC |
2255 | // We either have a wildcard or an unresolved library |
2256 | if (q->has_library && (contains_glob_chars (q->path) | |
2257 | || q->path.find('/') == string::npos)) | |
84c84ac4 SC |
2258 | // handle .library(GLOB) |
2259 | q->dw.iterate_over_libraries (&q->query_library_callback, q); | |
576eaefe SC |
2260 | // .plt is translated to .plt.statement(N). We only want to iterate for the |
2261 | // .plt case | |
2262 | else if (q->has_plt && ! q->has_statement) | |
070764c0 JL |
2263 | { |
2264 | q->dw.iterate_over_plt (q, &q->query_plt_callback); | |
2265 | q->visited_modules.insert(name); | |
2266 | } | |
84c84ac4 | 2267 | else |
070764c0 JL |
2268 | { |
2269 | // search the module for matches of the probe point. | |
2270 | q->handle_query_module(); | |
2271 | q->visited_modules.insert(name); | |
2272 | } | |
bb788f9f | 2273 | |
b8da0ad1 | 2274 | // If we know that there will be no more matches, abort early. |
85007c04 | 2275 | if (q->dw.module_name_final_match(q->module_val) || pending_interrupts) |
b8da0ad1 FCE |
2276 | return DWARF_CB_ABORT; |
2277 | else | |
2278 | return DWARF_CB_OK; | |
7a053d3b | 2279 | } |
39bcd429 | 2280 | catch (const semantic_error& e) |
bd2b1e68 | 2281 | { |
39bcd429 FCE |
2282 | q->sess.print_error (e); |
2283 | return DWARF_CB_ABORT; | |
bd2b1e68 | 2284 | } |
bd2b1e68 GH |
2285 | } |
2286 | ||
35d4ab18 | 2287 | |
84c84ac4 | 2288 | void |
bbbc7241 | 2289 | base_query::query_library_callback (base_query *me, const char *data) |
84c84ac4 | 2290 | { |
5d5bd369 | 2291 | me->query_library (data); |
84c84ac4 SC |
2292 | } |
2293 | ||
2294 | ||
2cbcfa9c | 2295 | string |
51d6bda3 SC |
2296 | query_one_library (const char *library, dwflpp & dw, |
2297 | const string user_lib, probe * base_probe, probe_point *base_loc, | |
2298 | vector<derived_probe *> & results) | |
84c84ac4 | 2299 | { |
47e226ed | 2300 | if (dw.function_name_matches_pattern(library, "*" + user_lib)) |
84c84ac4 | 2301 | { |
05fb3e0c WF |
2302 | string library_path = find_executable (library, "", dw.sess.sysenv, |
2303 | "LD_LIBRARY_PATH"); | |
84c84ac4 SC |
2304 | probe_point* specific_loc = new probe_point(*base_loc); |
2305 | specific_loc->optional = true; | |
ef0943df | 2306 | specific_loc->from_glob = true; |
84c84ac4 SC |
2307 | vector<probe_point::component*> derived_comps; |
2308 | ||
5d25dfb6 JL |
2309 | // Create new probe point for the matching library. This is what will be |
2310 | // shown in listing mode. Also replace the process(str) with the real | |
2311 | // absolute path rather than keeping what the user typed in. | |
84c84ac4 SC |
2312 | vector<probe_point::component*>::iterator it; |
2313 | for (it = specific_loc->components.begin(); | |
2314 | it != specific_loc->components.end(); ++it) | |
5d25dfb6 JL |
2315 | if ((*it)->functor == TOK_PROCESS) |
2316 | derived_comps.push_back(new probe_point::component(TOK_PROCESS, | |
2317 | new literal_string(path_remove_sysroot(dw.sess, dw.module_name)))); | |
2318 | else if ((*it)->functor == TOK_LIBRARY) | |
84c84ac4 SC |
2319 | derived_comps.push_back(new probe_point::component(TOK_LIBRARY, |
2320 | new literal_string(library_path))); | |
2321 | else | |
2322 | derived_comps.push_back(*it); | |
2323 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2324 | derived_loc->components = derived_comps; | |
8159bf55 | 2325 | probe *new_base = new probe (new probe (base_probe, specific_loc), derived_loc); |
51d6bda3 SC |
2326 | derive_probes(dw.sess, new_base, results); |
2327 | if (dw.sess.verbose > 2) | |
2cbcfa9c JL |
2328 | clog << _("module=") << library_path << endl; |
2329 | return library_path; | |
84c84ac4 | 2330 | } |
2cbcfa9c | 2331 | return ""; |
84c84ac4 SC |
2332 | } |
2333 | ||
2334 | ||
51d6bda3 SC |
2335 | void |
2336 | dwarf_query::query_library (const char *library) | |
2337 | { | |
2cbcfa9c JL |
2338 | string library_path = |
2339 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
2340 | if (!library_path.empty()) | |
2341 | visited_modules.insert(library_path); | |
51d6bda3 SC |
2342 | } |
2343 | ||
576eaefe SC |
2344 | struct plt_expanding_visitor: public var_expanding_visitor |
2345 | { | |
2346 | plt_expanding_visitor(const string & entry): | |
2347 | entry (entry) | |
2348 | { | |
2349 | } | |
2350 | const string & entry; | |
2351 | ||
2352 | void visit_target_symbol (target_symbol* e); | |
2353 | }; | |
2354 | ||
2355 | ||
2356 | void | |
45cdb40e | 2357 | base_query::query_plt_callback (base_query *me, const char *entry, size_t address) |
576eaefe | 2358 | { |
576eaefe SC |
2359 | if (me->dw.function_name_matches_pattern (entry, me->plt_val)) |
2360 | me->query_plt (entry, address); | |
7f4964f1 | 2361 | me->dw.mod_info->plt_funcs.insert(entry); |
576eaefe SC |
2362 | } |
2363 | ||
2364 | ||
2365 | void | |
2366 | query_one_plt (const char *entry, long addr, dwflpp & dw, | |
2367 | probe * base_probe, probe_point *base_loc, | |
2368 | vector<derived_probe *> & results) | |
2369 | { | |
2370 | probe_point* specific_loc = new probe_point(*base_loc); | |
576eaefe SC |
2371 | vector<probe_point::component*> derived_comps; |
2372 | ||
2373 | if (dw.sess.verbose > 2) | |
2374 | clog << _F("plt entry=%s\n", entry); | |
2375 | ||
576eaefe SC |
2376 | vector<probe_point::component*>::iterator it; |
2377 | for (it = specific_loc->components.begin(); | |
2378 | it != specific_loc->components.end(); ++it) | |
2379 | if ((*it)->functor == TOK_PLT) | |
3d372d6b | 2380 | { |
8159bf55 FCE |
2381 | *it = new probe_point::component(TOK_PLT, |
2382 | new literal_string(entry)); | |
3d372d6b SC |
2383 | derived_comps.push_back(*it); |
2384 | derived_comps.push_back(new probe_point::component(TOK_STATEMENT, | |
ac7c8669 | 2385 | new literal_number(addr, true))); |
3d372d6b | 2386 | } |
576eaefe SC |
2387 | else |
2388 | derived_comps.push_back(*it); | |
2389 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2390 | derived_loc->components = derived_comps; | |
02c34e38 FCE |
2391 | probe *new_base = new probe (new probe (base_probe, specific_loc), |
2392 | derived_loc); | |
576eaefe SC |
2393 | string e = string(entry); |
2394 | plt_expanding_visitor pltv (e); | |
2395 | pltv.replace (new_base->body); | |
2396 | derive_probes(dw.sess, new_base, results); | |
2397 | } | |
2398 | ||
2399 | ||
2400 | void | |
2401 | dwarf_query::query_plt (const char *entry, size_t address) | |
2402 | { | |
2403 | query_one_plt (entry, address, dw, base_probe, base_loc, results); | |
2404 | } | |
51d6bda3 | 2405 | |
435f53a7 FCE |
2406 | // This would more naturally fit into elaborate.cxx:semantic_pass_symbols, |
2407 | // but the needed declaration for module_cache is not available there. | |
2408 | // Nor for that matter in session.cxx. Only in this CU is that field ever | |
2409 | // set (in query_module() above), so we clean it up here too. | |
2410 | static void | |
2411 | delete_session_module_cache (systemtap_session& s) | |
2412 | { | |
2413 | if (s.module_cache) { | |
2414 | if (s.verbose > 3) | |
b530b5b3 | 2415 | clog << _("deleting module_cache") << endl; |
435f53a7 FCE |
2416 | delete s.module_cache; |
2417 | s.module_cache = 0; | |
2418 | } | |
2419 | } | |
2420 | ||
2421 | ||
de688825 | 2422 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 2423 | { |
77de5e9e | 2424 | dwarf_query & q; |
bcc12710 | 2425 | Dwarf_Die *scope_die; |
77de5e9e | 2426 | Dwarf_Addr addr; |
8c819921 | 2427 | block *add_block; |
2260f4e3 | 2428 | block *add_call_probe; // synthesized from .return probes with saved $vars |
1630966a JS |
2429 | // NB: tids are not always collected in add_block & add_call_probe, because |
2430 | // gen_kretprobe_saved_return doesn't need them. Thus we need these extra | |
2431 | // *_tid bools for gen_mapped_saved_return to tell what's there. | |
8cc799a5 | 2432 | bool add_block_tid, add_call_probe_tid; |
af234c40 JS |
2433 | unsigned saved_longs, saved_strings; // data saved within kretprobes |
2434 | map<std::string, expression *> return_ts_map; | |
729455a7 | 2435 | vector<Dwarf_Die> scopes; |
3689db05 | 2436 | // probe counter name -> pointer of associated probe |
698de6cc | 2437 | std::set<derived_probe*> perf_counter_refs; |
b95e2b79 | 2438 | bool visited; |
77de5e9e | 2439 | |
de688825 | 2440 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
af234c40 | 2441 | q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL), |
8cc799a5 | 2442 | add_block_tid(false), add_call_probe_tid(false), |
af234c40 | 2443 | saved_longs(0), saved_strings(0), visited(false) {} |
277c21bc | 2444 | expression* gen_mapped_saved_return(expression* e, const string& name); |
140be17a | 2445 | expression* gen_kretprobe_saved_return(expression* e); |
a7999c82 JS |
2446 | void visit_target_symbol_saved_return (target_symbol* e); |
2447 | void visit_target_symbol_context (target_symbol* e); | |
d7f3e0c5 | 2448 | void visit_target_symbol (target_symbol* e); |
bd1fcbad | 2449 | void visit_atvar_op (atvar_op* e); |
c24447be | 2450 | void visit_cast_op (cast_op* e); |
8cc799a5 | 2451 | void visit_entry_op (entry_op* e); |
3689db05 | 2452 | void visit_perf_op (perf_op* e); |
729455a7 JS |
2453 | private: |
2454 | vector<Dwarf_Die>& getscopes(target_symbol *e); | |
77de5e9e GH |
2455 | }; |
2456 | ||
2457 | ||
de688825 | 2458 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 2459 | |
a50de939 | 2460 | |
74fe61bc | 2461 | var_expanding_visitor::var_expanding_visitor (): op() |
a50de939 DS |
2462 | { |
2463 | // FIXME: for the time being, by default we only support plain '$foo | |
2464 | // = bar', not '+=' or any other op= variant. This is fixable, but a | |
2465 | // bit ugly. | |
2466 | // | |
2467 | // If derived classes desire to add additional operator support, add | |
2468 | // new operators to this list in the derived class constructor. | |
2469 | valid_ops.insert ("="); | |
2470 | } | |
2471 | ||
2472 | ||
87214add JS |
2473 | bool |
2474 | var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop, | |
2475 | expression*& lvalue, expression*& rvalue) | |
77de5e9e | 2476 | { |
e57b735a GH |
2477 | // Our job would normally be to require() the left and right sides |
2478 | // into a new assignment. What we're doing is slightly trickier: | |
2479 | // we're pushing a functioncall** onto a stack, and if our left | |
2480 | // child sets the functioncall* for that value, we're going to | |
2481 | // assume our left child was a target symbol -- transformed into a | |
2482 | // set_target_foo(value) call, and it wants to take our right child | |
2483 | // as the argument "value". | |
2484 | // | |
2485 | // This is why some people claim that languages with | |
2486 | // constructor-decomposing case expressions have a leg up on | |
2487 | // visitors. | |
2488 | ||
2489 | functioncall *fcall = NULL; | |
d9b516ca | 2490 | |
a50de939 | 2491 | // Let visit_target_symbol know what operator it should handle. |
87214add JS |
2492 | const string* old_op = op; |
2493 | op = &eop; | |
a50de939 | 2494 | |
e57b735a | 2495 | target_symbol_setter_functioncalls.push (&fcall); |
87214add | 2496 | replace (lvalue); |
e57b735a | 2497 | target_symbol_setter_functioncalls.pop (); |
87214add JS |
2498 | replace (rvalue); |
2499 | ||
2500 | op = old_op; | |
e57b735a GH |
2501 | |
2502 | if (fcall != NULL) | |
77de5e9e | 2503 | { |
e57b735a GH |
2504 | // Our left child is informing us that it was a target variable |
2505 | // and it has been replaced with a set_target_foo() function | |
2506 | // call; we are going to provide that function call -- with the | |
2507 | // right child spliced in as sole argument -- in place of | |
de688825 | 2508 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a | 2509 | |
87214add | 2510 | if (valid_ops.find (eop) == valid_ops.end ()) |
a50de939 DS |
2511 | { |
2512 | // Build up a list of supported operators. | |
2513 | string ops; | |
2514 | std::set<string>::iterator i; | |
b530b5b3 | 2515 | int valid_ops_size = 0; |
a50de939 | 2516 | for (i = valid_ops.begin(); i != valid_ops.end(); i++) |
b530b5b3 | 2517 | { |
a50de939 | 2518 | ops += " " + *i + ","; |
b530b5b3 LB |
2519 | valid_ops_size++; |
2520 | } | |
a50de939 DS |
2521 | ops.resize(ops.size() - 1); // chop off the last ',' |
2522 | ||
2523 | // Throw the error. | |
dc09353a | 2524 | throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s", |
1e41115c | 2525 | "Only the following assign operators are implemented on target variables: %s", |
52c2652f | 2526 | valid_ops_size, ops.c_str()), tok); |
b530b5b3 | 2527 | |
a50de939 | 2528 | } |
e57b735a | 2529 | |
87214add JS |
2530 | assert (lvalue == fcall); |
2531 | if (rvalue) | |
2532 | fcall->args.push_back (rvalue); | |
4ed05b15 | 2533 | provide (fcall); |
87214add | 2534 | return true; |
77de5e9e | 2535 | } |
e57b735a | 2536 | else |
87214add JS |
2537 | return false; |
2538 | } | |
2539 | ||
2540 | ||
2541 | void | |
2542 | var_expanding_visitor::visit_assignment (assignment* e) | |
2543 | { | |
2544 | if (!rewrite_lvalue (e->tok, e->op, e->left, e->right)) | |
2545 | provide (e); | |
2546 | } | |
2547 | ||
2548 | ||
2549 | void | |
2550 | var_expanding_visitor::visit_pre_crement (pre_crement* e) | |
2551 | { | |
2552 | expression *dummy = NULL; | |
2553 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2554 | provide (e); | |
2555 | } | |
2556 | ||
2557 | ||
2558 | void | |
2559 | var_expanding_visitor::visit_post_crement (post_crement* e) | |
2560 | { | |
2561 | expression *dummy = NULL; | |
2562 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2563 | provide (e); | |
2564 | } | |
2565 | ||
2566 | ||
2567 | void | |
2568 | var_expanding_visitor::visit_delete_statement (delete_statement* s) | |
2569 | { | |
2570 | string fakeop = "delete"; | |
2571 | expression *dummy = NULL; | |
2572 | if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy)) | |
2573 | provide (s); | |
e57b735a | 2574 | } |
d9b516ca | 2575 | |
d7f3e0c5 | 2576 | |
30263a73 FCE |
2577 | void |
2578 | var_expanding_visitor::visit_defined_op (defined_op* e) | |
2579 | { | |
2580 | bool resolved = true; | |
2581 | ||
2582 | defined_ops.push (e); | |
2583 | try { | |
2584 | // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is | |
2585 | // defined with a target_symbol* operand, a subsidiary call may attempt to | |
2586 | // rewrite it to a general expression* instead, and require<> happily | |
2587 | // casts to/from void*, causing possible memory corruption. We use | |
2588 | // expression* here, being the general case of rewritten $variable. | |
2589 | expression *foo1 = e->operand; | |
2590 | foo1 = require (foo1); | |
2591 | ||
c69a87e0 | 2592 | // NB: Formerly, we had some curious cases to consider here, depending on what |
30263a73 | 2593 | // various visit_target_symbol() implementations do for successful or |
c69a87e0 FCE |
2594 | // erroneous resolutions. Some would signal a visit_target_symbol failure |
2595 | // with an exception, with a set flag within the target_symbol, or nothing | |
2596 | // at all. | |
30263a73 | 2597 | // |
c69a87e0 FCE |
2598 | // Now, failures always have to be signalled with a |
2599 | // saved_conversion_error being chained to the target_symbol. | |
2600 | // Successes have to result in an attempted rewrite of the | |
850bfddd | 2601 | // target_symbol (via provide()). |
780f11ff | 2602 | // |
c69a87e0 FCE |
2603 | // Edna Mode: "no capes". fche: "no exceptions". |
2604 | ||
30263a73 FCE |
2605 | // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error |
2606 | // | |
2607 | // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error | |
2608 | // | |
2609 | // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic | |
2610 | // dwarf probe to take care of it. | |
2611 | // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor | |
2612 | // to pass through @defined() to the synthetic dwarf probe. | |
780f11ff | 2613 | // |
30263a73 FCE |
2614 | // utrace: success: rewrites to function; failure: semantic_error |
2615 | // | |
850bfddd | 2616 | // procfs: success: rewrites to function; failure: semantic_error |
30263a73 FCE |
2617 | |
2618 | target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1); | |
c69a87e0 | 2619 | if (foo2 && foo2->saved_conversion_error) // failing |
30263a73 | 2620 | resolved = false; |
a45664f4 | 2621 | else if (foo2) // unresolved but not marked failing |
b7aedf26 | 2622 | { |
780f11ff JS |
2623 | // There are some visitors that won't touch certain target_symbols, |
2624 | // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should | |
2625 | // leave it for now so some other visitor can have a chance. | |
b7aedf26 JS |
2626 | e->operand = foo2; |
2627 | provide (e); | |
2628 | return; | |
2629 | } | |
30263a73 FCE |
2630 | else // resolved, rewritten to some other expression type |
2631 | resolved = true; | |
780f11ff | 2632 | } catch (const semantic_error& e) { |
c69a87e0 | 2633 | assert (0); // should not happen |
30263a73 FCE |
2634 | } |
2635 | defined_ops.pop (); | |
2636 | ||
2637 | literal_number* ln = new literal_number (resolved ? 1 : 0); | |
2638 | ln->tok = e->tok; | |
2639 | provide (ln); | |
2640 | } | |
2641 | ||
2642 | ||
5f36109e JS |
2643 | struct dwarf_pretty_print |
2644 | { | |
2645 | dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc, | |
2646 | const string& local, bool userspace_p, | |
2647 | const target_symbol& e): | |
d19a9a82 JS |
2648 | dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL), |
2649 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2650 | { |
2651 | init_ts (e); | |
2652 | dw.type_die_for_local (scopes, pc, local, ts, &base_type); | |
2653 | } | |
2654 | ||
2655 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc, | |
2656 | bool userspace_p, const target_symbol& e): | |
d19a9a82 JS |
2657 | dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL), |
2658 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2659 | { |
2660 | init_ts (e); | |
2661 | dw.type_die_for_return (&scopes[0], pc, ts, &base_type); | |
2662 | } | |
2663 | ||
2664 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer, | |
d19a9a82 | 2665 | bool deref_p, bool userspace_p, const target_symbol& e): |
5f36109e | 2666 | dw(dw), pc(0), pointer(pointer), pointer_type(*type_die), |
d19a9a82 | 2667 | userspace_p(userspace_p), deref_p(deref_p) |
5f36109e JS |
2668 | { |
2669 | init_ts (e); | |
2670 | dw.type_die_for_pointer (type_die, ts, &base_type); | |
2671 | } | |
2672 | ||
2673 | functioncall* expand (); | |
ce83ff57 | 2674 | ~dwarf_pretty_print () { delete ts; } |
5f36109e JS |
2675 | |
2676 | private: | |
2677 | dwflpp& dw; | |
2678 | target_symbol* ts; | |
7d11d8c9 | 2679 | bool print_full; |
5f36109e JS |
2680 | Dwarf_Die base_type; |
2681 | ||
2682 | string local; | |
2683 | vector<Dwarf_Die> scopes; | |
2684 | Dwarf_Addr pc; | |
2685 | ||
2686 | expression* pointer; | |
2687 | Dwarf_Die pointer_type; | |
2688 | ||
d19a9a82 | 2689 | const bool userspace_p, deref_p; |
5f36109e JS |
2690 | |
2691 | void recurse (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2692 | print_format* pf, bool top=false); |
600551ca JS |
2693 | void recurse_bitfield (Dwarf_Die* type, target_symbol* e, |
2694 | print_format* pf); | |
5f36109e | 2695 | void recurse_base (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2696 | print_format* pf); |
5f36109e | 2697 | void recurse_array (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2698 | print_format* pf, bool top); |
5f36109e | 2699 | void recurse_pointer (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2700 | print_format* pf, bool top); |
5f36109e | 2701 | void recurse_struct (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2702 | print_format* pf, bool top); |
5f36109e | 2703 | void recurse_struct_members (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2704 | print_format* pf, int& count); |
bbee5bb8 | 2705 | bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf); |
5f36109e JS |
2706 | |
2707 | void init_ts (const target_symbol& e); | |
2708 | expression* deref (target_symbol* e); | |
c55ea10d | 2709 | bool push_deref (print_format* pf, const string& fmt, target_symbol* e); |
5f36109e JS |
2710 | }; |
2711 | ||
2712 | ||
2713 | void | |
2714 | dwarf_pretty_print::init_ts (const target_symbol& e) | |
2715 | { | |
2716 | // Work with a new target_symbol so we can modify arguments | |
2717 | ts = new target_symbol (e); | |
2718 | ||
2719 | if (ts->addressof) | |
dc09353a | 2720 | throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok); |
5f36109e JS |
2721 | |
2722 | if (ts->components.empty() || | |
2723 | ts->components.back().type != target_symbol::comp_pretty_print) | |
dc09353a | 2724 | throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok); |
7d11d8c9 | 2725 | print_full = ts->components.back().member.length() > 1; |
5f36109e JS |
2726 | ts->components.pop_back(); |
2727 | } | |
2728 | ||
2729 | ||
2730 | functioncall* | |
2731 | dwarf_pretty_print::expand () | |
2732 | { | |
2733 | static unsigned tick = 0; | |
2734 | ||
2735 | // function pretty_print_X([pointer], [arg1, arg2, ...]) { | |
7d11d8c9 JS |
2736 | // try { |
2737 | // return sprintf("{.foo=...}", (ts)->foo, ...) | |
2738 | // } catch { | |
2739 | // return "ERROR" | |
2740 | // } | |
5f36109e JS |
2741 | // } |
2742 | ||
2743 | // Create the function decl and call. | |
2744 | ||
2745 | functiondecl *fdecl = new functiondecl; | |
2746 | fdecl->tok = ts->tok; | |
2747 | fdecl->synthetic = true; | |
2748 | fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++); | |
2749 | fdecl->type = pe_string; | |
2750 | ||
2751 | functioncall* fcall = new functioncall; | |
2752 | fcall->tok = ts->tok; | |
2753 | fcall->function = fdecl->name; | |
140be17a | 2754 | fcall->type = pe_string; |
5f36109e JS |
2755 | |
2756 | // If there's a <pointer>, replace it with a new var and make that | |
2757 | // the first function argument. | |
2758 | if (pointer) | |
2759 | { | |
2760 | vardecl *v = new vardecl; | |
2761 | v->type = pe_long; | |
2762 | v->name = "pointer"; | |
2763 | v->tok = ts->tok; | |
2764 | fdecl->formal_args.push_back (v); | |
2765 | fcall->args.push_back (pointer); | |
2766 | ||
2767 | symbol* sym = new symbol; | |
2768 | sym->tok = ts->tok; | |
2769 | sym->name = v->name; | |
5f36109e JS |
2770 | pointer = sym; |
2771 | } | |
2772 | ||
2773 | // For each expression argument, replace it with a function argument. | |
2774 | for (unsigned i = 0; i < ts->components.size(); ++i) | |
2775 | if (ts->components[i].type == target_symbol::comp_expression_array_index) | |
2776 | { | |
2777 | vardecl *v = new vardecl; | |
2778 | v->type = pe_long; | |
2779 | v->name = "index" + lex_cast(i); | |
2780 | v->tok = ts->tok; | |
2781 | fdecl->formal_args.push_back (v); | |
2782 | fcall->args.push_back (ts->components[i].expr_index); | |
2783 | ||
2784 | symbol* sym = new symbol; | |
2785 | sym->tok = ts->tok; | |
2786 | sym->name = v->name; | |
5f36109e JS |
2787 | ts->components[i].expr_index = sym; |
2788 | } | |
2789 | ||
2790 | // Create the return sprintf. | |
1c922ad7 | 2791 | print_format* pf = print_format::create(ts->tok, "sprintf"); |
5f36109e JS |
2792 | return_statement* rs = new return_statement; |
2793 | rs->tok = ts->tok; | |
2794 | rs->value = pf; | |
5f36109e JS |
2795 | |
2796 | // Recurse into the actual values. | |
7d11d8c9 | 2797 | recurse (&base_type, ts, pf, true); |
5f36109e JS |
2798 | pf->components = print_format::string_to_components(pf->raw_components); |
2799 | ||
7d11d8c9 JS |
2800 | // Create the try-catch net |
2801 | try_block* tb = new try_block; | |
2802 | tb->tok = ts->tok; | |
2803 | tb->try_block = rs; | |
2804 | tb->catch_error_var = 0; | |
2805 | return_statement* rs2 = new return_statement; | |
2806 | rs2->tok = ts->tok; | |
2807 | rs2->value = new literal_string ("ERROR"); | |
2808 | rs2->value->tok = ts->tok; | |
2809 | tb->catch_block = rs2; | |
2810 | fdecl->body = tb; | |
2811 | ||
f8809d54 | 2812 | fdecl->join (dw.sess); |
5f36109e JS |
2813 | return fcall; |
2814 | } | |
2815 | ||
2816 | ||
2817 | void | |
2818 | dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e, | |
7d11d8c9 | 2819 | print_format* pf, bool top) |
5f36109e JS |
2820 | { |
2821 | Dwarf_Die type; | |
2822 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2823 | ||
2824 | switch (dwarf_tag(&type)) | |
2825 | { | |
2826 | default: | |
2827 | // XXX need a warning? | |
2828 | // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type)) | |
2829 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2830 | pf->raw_components.append("?"); | |
2831 | break; | |
2832 | ||
2833 | case DW_TAG_enumeration_type: | |
2834 | case DW_TAG_base_type: | |
7d11d8c9 | 2835 | recurse_base (&type, e, pf); |
5f36109e JS |
2836 | break; |
2837 | ||
2838 | case DW_TAG_array_type: | |
7d11d8c9 | 2839 | recurse_array (&type, e, pf, top); |
5f36109e JS |
2840 | break; |
2841 | ||
2842 | case DW_TAG_pointer_type: | |
2843 | case DW_TAG_reference_type: | |
2844 | case DW_TAG_rvalue_reference_type: | |
7d11d8c9 | 2845 | recurse_pointer (&type, e, pf, top); |
5f36109e JS |
2846 | break; |
2847 | ||
2848 | case DW_TAG_subroutine_type: | |
c55ea10d | 2849 | push_deref (pf, "<function>:%p", e); |
5f36109e JS |
2850 | break; |
2851 | ||
2852 | case DW_TAG_union_type: | |
5f36109e JS |
2853 | case DW_TAG_structure_type: |
2854 | case DW_TAG_class_type: | |
7d11d8c9 | 2855 | recurse_struct (&type, e, pf, top); |
5f36109e JS |
2856 | break; |
2857 | } | |
2858 | } | |
2859 | ||
2860 | ||
600551ca JS |
2861 | // Bit fields are handled as a special-case combination of recurse() and |
2862 | // recurse_base(), only called from recurse_struct_members(). The main | |
2863 | // difference is that the value is always printed numerically, even if the | |
2864 | // underlying type is a char. | |
2865 | void | |
2866 | dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e, | |
2867 | print_format* pf) | |
2868 | { | |
2869 | Dwarf_Die type; | |
2870 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2871 | ||
2872 | int tag = dwarf_tag(&type); | |
2873 | if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type) | |
2874 | { | |
2875 | // XXX need a warning? | |
2876 | // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag) | |
2877 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2878 | pf->raw_components.append("?"); | |
2879 | return; | |
2880 | } | |
2881 | ||
2882 | Dwarf_Attribute attr; | |
2883 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2884 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr), | |
2885 | &encoding); | |
2886 | switch (encoding) | |
2887 | { | |
2888 | case DW_ATE_float: | |
2889 | case DW_ATE_complex_float: | |
2890 | // XXX need a warning? | |
2891 | // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding) | |
2892 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2893 | pf->raw_components.append("?"); | |
2894 | break; | |
2895 | ||
2896 | case DW_ATE_unsigned: | |
2897 | case DW_ATE_unsigned_char: | |
2898 | push_deref (pf, "%u", e); | |
2899 | break; | |
2900 | ||
2901 | case DW_ATE_signed: | |
2902 | case DW_ATE_signed_char: | |
2903 | default: | |
2904 | push_deref (pf, "%i", e); | |
2905 | break; | |
2906 | } | |
2907 | } | |
2908 | ||
2909 | ||
5f36109e JS |
2910 | void |
2911 | dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2912 | print_format* pf) |
5f36109e JS |
2913 | { |
2914 | Dwarf_Attribute attr; | |
2915 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2916 | dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr), | |
2917 | &encoding); | |
5f36109e JS |
2918 | switch (encoding) |
2919 | { | |
2920 | case DW_ATE_float: | |
2921 | case DW_ATE_complex_float: | |
2922 | // XXX need a warning? | |
2923 | // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding) | |
2924 | // + ") for " + dwarf_type_name(type), e->tok); | |
2925 | pf->raw_components.append("?"); | |
5f36109e JS |
2926 | break; |
2927 | ||
6561d8d1 | 2928 | case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char |
5f36109e JS |
2929 | case DW_ATE_signed_char: |
2930 | case DW_ATE_unsigned_char: | |
941101c1 JS |
2931 | // Use escapes to make sure that non-printable characters |
2932 | // don't interrupt our stream (especially '\0' values). | |
2933 | push_deref (pf, "'%#c'", e); | |
5f36109e JS |
2934 | break; |
2935 | ||
2936 | case DW_ATE_unsigned: | |
c55ea10d | 2937 | push_deref (pf, "%u", e); |
5f36109e JS |
2938 | break; |
2939 | ||
600551ca | 2940 | case DW_ATE_signed: |
5f36109e | 2941 | default: |
c55ea10d | 2942 | push_deref (pf, "%i", e); |
5f36109e JS |
2943 | break; |
2944 | } | |
5f36109e JS |
2945 | } |
2946 | ||
2947 | ||
2948 | void | |
2949 | dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2950 | print_format* pf, bool top) |
5f36109e | 2951 | { |
7d11d8c9 JS |
2952 | if (!top && !print_full) |
2953 | { | |
2954 | pf->raw_components.append("[...]"); | |
2955 | return; | |
2956 | } | |
2957 | ||
5f36109e JS |
2958 | Dwarf_Die childtype; |
2959 | dwarf_attr_die (type, DW_AT_type, &childtype); | |
bbee5bb8 JS |
2960 | |
2961 | if (print_chars (&childtype, e, pf)) | |
2962 | return; | |
2963 | ||
5f36109e JS |
2964 | pf->raw_components.append("["); |
2965 | ||
2966 | // We print the array up to the first 5 elements. | |
2967 | // XXX how can we determine the array size? | |
2968 | // ... for now, just print the first element | |
64cddf39 | 2969 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
5f36109e | 2970 | unsigned i, size = 1; |
64cddf39 | 2971 | for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i) |
5f36109e JS |
2972 | { |
2973 | if (i > 0) | |
2974 | pf->raw_components.append(", "); | |
2975 | target_symbol* e2 = new target_symbol(*e); | |
2976 | e2->components.push_back (target_symbol::component(e->tok, i)); | |
7d11d8c9 | 2977 | recurse (&childtype, e2, pf); |
5f36109e JS |
2978 | } |
2979 | if (i < size || 1/*XXX until real size is known */) | |
2980 | pf->raw_components.append(", ..."); | |
2981 | pf->raw_components.append("]"); | |
2982 | } | |
2983 | ||
2984 | ||
2985 | void | |
2986 | dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2987 | print_format* pf, bool top) |
5f36109e | 2988 | { |
7d11d8c9 | 2989 | // We chase to top-level pointers, but leave the rest alone |
d19a9a82 | 2990 | bool void_p = true; |
7d11d8c9 | 2991 | Dwarf_Die pointee; |
bbee5bb8 | 2992 | if (dwarf_attr_die (type, DW_AT_type, &pointee)) |
d19a9a82 JS |
2993 | { |
2994 | try | |
2995 | { | |
2996 | dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e); | |
2997 | void_p = false; | |
2998 | } | |
2999 | catch (const semantic_error&) {} | |
3000 | } | |
3001 | ||
3002 | if (!void_p) | |
5f36109e | 3003 | { |
bbee5bb8 JS |
3004 | if (print_chars (&pointee, e, pf)) |
3005 | return; | |
3006 | ||
3007 | if (top) | |
3008 | { | |
3009 | recurse (&pointee, e, pf, top); | |
3010 | return; | |
3011 | } | |
5f36109e | 3012 | } |
bbee5bb8 | 3013 | |
c55ea10d | 3014 | push_deref (pf, "%p", e); |
5f36109e JS |
3015 | } |
3016 | ||
3017 | ||
3018 | void | |
3019 | dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 3020 | print_format* pf, bool top) |
5f36109e | 3021 | { |
bdec0e18 JS |
3022 | if (dwarf_hasattr(type, DW_AT_declaration)) |
3023 | { | |
a44a7cb5 | 3024 | Dwarf_Die *resolved = dw.declaration_resolve(type); |
bdec0e18 JS |
3025 | if (!resolved) |
3026 | { | |
3027 | // could be an error, but for now just stub it | |
3028 | // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok); | |
3029 | pf->raw_components.append("{...}"); | |
3030 | return; | |
3031 | } | |
3032 | type = resolved; | |
3033 | } | |
3034 | ||
5f36109e JS |
3035 | int count = 0; |
3036 | pf->raw_components.append("{"); | |
7d11d8c9 JS |
3037 | if (top || print_full) |
3038 | recurse_struct_members (type, e, pf, count); | |
3039 | else | |
3040 | pf->raw_components.append("..."); | |
5f36109e JS |
3041 | pf->raw_components.append("}"); |
3042 | } | |
3043 | ||
3044 | ||
3045 | void | |
3046 | dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 3047 | print_format* pf, int& count) |
5f36109e | 3048 | { |
a80f28d8 JS |
3049 | /* With inheritance, a subclass may mask member names of parent classes, so |
3050 | * our search among the inheritance tree must be breadth-first rather than | |
3051 | * depth-first (recursive). The type die is still our starting point. When | |
3052 | * we encounter a masked name, just skip it. */ | |
3053 | set<string> dupes; | |
3054 | deque<Dwarf_Die> inheritees(1, *type); | |
3055 | for (; !inheritees.empty(); inheritees.pop_front()) | |
3056 | { | |
dee830d9 | 3057 | Dwarf_Die child, childtype, import; |
a80f28d8 JS |
3058 | if (dwarf_child (&inheritees.front(), &child) == 0) |
3059 | do | |
3060 | { | |
3061 | target_symbol* e2 = e; | |
5f36109e | 3062 | |
a80f28d8 JS |
3063 | // skip static members |
3064 | if (dwarf_hasattr(&child, DW_AT_declaration)) | |
3065 | continue; | |
5f36109e | 3066 | |
a80f28d8 | 3067 | int tag = dwarf_tag (&child); |
5f36109e | 3068 | |
dee830d9 MW |
3069 | /* Pretend imported units contain members by recursing into |
3070 | struct_member printing with the same count. */ | |
3071 | if (tag == DW_TAG_imported_unit | |
3072 | && dwarf_attr_die (&child, DW_AT_import, &import)) | |
3073 | recurse_struct_members (&import, e2, pf, count); | |
3074 | ||
a80f28d8 JS |
3075 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) |
3076 | continue; | |
5f36109e | 3077 | |
a80f28d8 | 3078 | dwarf_attr_die (&child, DW_AT_type, &childtype); |
5f36109e | 3079 | |
a80f28d8 JS |
3080 | if (tag == DW_TAG_inheritance) |
3081 | { | |
3082 | inheritees.push_back(childtype); | |
3083 | continue; | |
3084 | } | |
5f36109e | 3085 | |
a80f28d8 JS |
3086 | int childtag = dwarf_tag (&childtype); |
3087 | const char *member = dwarf_diename (&child); | |
3a147004 | 3088 | |
a80f28d8 JS |
3089 | // "_vptr.foo" members are C++ virtual function tables, |
3090 | // which (generally?) aren't interesting for users. | |
3091 | if (member && startswith(member, "_vptr.")) | |
3092 | continue; | |
3a147004 | 3093 | |
a80f28d8 JS |
3094 | // skip inheritance-masked duplicates |
3095 | if (member && !dupes.insert(member).second) | |
3096 | continue; | |
64cddf39 | 3097 | |
a80f28d8 JS |
3098 | if (++count > 1) |
3099 | pf->raw_components.append(", "); | |
64cddf39 | 3100 | |
a80f28d8 JS |
3101 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
3102 | if (pf->args.size() >= 32) | |
3103 | { | |
3104 | pf->raw_components.append("..."); | |
3105 | break; | |
3106 | } | |
3107 | ||
3108 | if (member) | |
3109 | { | |
3110 | pf->raw_components.append("."); | |
3111 | pf->raw_components.append(member); | |
5f36109e | 3112 | |
a80f28d8 JS |
3113 | e2 = new target_symbol(*e); |
3114 | e2->components.push_back (target_symbol::component(e->tok, member)); | |
3115 | } | |
3116 | else if (childtag == DW_TAG_union_type) | |
3117 | pf->raw_components.append("<union>"); | |
3118 | else if (childtag == DW_TAG_structure_type) | |
3119 | pf->raw_components.append("<class>"); | |
3120 | else if (childtag == DW_TAG_class_type) | |
3121 | pf->raw_components.append("<struct>"); | |
3122 | pf->raw_components.append("="); | |
600551ca JS |
3123 | |
3124 | if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset)) | |
3125 | recurse_bitfield (&childtype, e2, pf); | |
3126 | else | |
3127 | recurse (&childtype, e2, pf); | |
5f36109e | 3128 | } |
a80f28d8 JS |
3129 | while (dwarf_siblingof (&child, &child) == 0); |
3130 | } | |
5f36109e JS |
3131 | } |
3132 | ||
3133 | ||
bbee5bb8 JS |
3134 | bool |
3135 | dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e, | |
3136 | print_format* pf) | |
3137 | { | |
3138 | Dwarf_Die type; | |
3139 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
6561d8d1 JS |
3140 | |
3141 | Dwarf_Attribute attr; | |
3142 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
3143 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr), | |
3144 | &encoding); | |
3145 | switch (encoding) | |
bbee5bb8 | 3146 | { |
6561d8d1 JS |
3147 | case DW_ATE_UTF: |
3148 | case DW_ATE_signed_char: | |
3149 | case DW_ATE_unsigned_char: | |
3150 | break; | |
3151 | default: | |
3152 | return false; | |
3153 | } | |
3154 | ||
3155 | string function = userspace_p ? "user_string2" : "kernel_string2"; | |
3156 | Dwarf_Word size = (Dwarf_Word) -1; | |
3157 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size); | |
3158 | switch (size) | |
3159 | { | |
3160 | case 1: | |
3161 | break; | |
3162 | case 2: | |
3163 | function += "_utf16"; | |
3164 | break; | |
3165 | case 4: | |
3166 | function += "_utf32"; | |
3167 | break; | |
3168 | default: | |
3169 | return false; | |
3170 | } | |
3171 | ||
3172 | if (push_deref (pf, "\"%s\"", e)) | |
3173 | { | |
3174 | // steal the last arg for a string access | |
3175 | assert (!pf->args.empty()); | |
3176 | functioncall* fcall = new functioncall; | |
3177 | fcall->tok = e->tok; | |
3178 | fcall->function = function; | |
3179 | fcall->args.push_back (pf->args.back()); | |
3180 | expression *err_msg = new literal_string ("<unknown>"); | |
3181 | err_msg->tok = e->tok; | |
3182 | fcall->args.push_back (err_msg); | |
3183 | pf->args.back() = fcall; | |
bbee5bb8 | 3184 | } |
6561d8d1 | 3185 | return true; |
bbee5bb8 JS |
3186 | } |
3187 | ||
a5ce5211 MW |
3188 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
3189 | static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n") | |
f1e8e7e0 MW |
3190 | + "#define fetch_register k_fetch_register\n" |
3191 | + "#define store_register k_store_register\n" | |
3192 | + "#define deref kderef\n" | |
3193 | + "#define store_deref store_kderef\n"; | |
a5ce5211 MW |
3194 | |
3195 | static const string EMBEDDED_FETCH_DEREF_USER = string("\n") | |
f1e8e7e0 MW |
3196 | + "#define fetch_register u_fetch_register\n" |
3197 | + "#define store_register u_store_register\n" | |
3198 | + "#define deref uderef\n" | |
3199 | + "#define store_deref store_uderef\n"; | |
a5ce5211 MW |
3200 | |
3201 | #define EMBEDDED_FETCH_DEREF(U) \ | |
f1e8e7e0 | 3202 | (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL) |
a5ce5211 MW |
3203 | |
3204 | static const string EMBEDDED_FETCH_DEREF_DONE = string("\n") | |
f1e8e7e0 MW |
3205 | + "#undef fetch_register\n" |
3206 | + "#undef store_register\n" | |
3207 | + "#undef deref\n" | |
3208 | + "#undef store_deref\n"; | |
bbee5bb8 | 3209 | |
1c0be8c7 JS |
3210 | static functioncall* |
3211 | synthetic_embedded_deref_call(systemtap_session& session, | |
3212 | const string& function_name, | |
3213 | const string& function_code, | |
3214 | exp_type function_type, | |
3215 | bool userspace_p, | |
3216 | bool lvalue_p, | |
3217 | target_symbol* e, | |
3218 | expression* pointer=NULL) | |
3219 | { | |
3220 | // Synthesize a functiondecl for the given embedded code string. | |
5f36109e JS |
3221 | functiondecl *fdecl = new functiondecl; |
3222 | fdecl->synthetic = true; | |
3223 | fdecl->tok = e->tok; | |
1c0be8c7 JS |
3224 | fdecl->name = function_name; |
3225 | fdecl->type = function_type; | |
3226 | ||
5f36109e JS |
3227 | embeddedcode *ec = new embeddedcode; |
3228 | ec->tok = e->tok; | |
1c0be8c7 JS |
3229 | ec->code += "/* unprivileged */"; |
3230 | if (! lvalue_p) | |
3231 | ec->code += "/* pure */"; | |
3232 | ec->code += EMBEDDED_FETCH_DEREF(userspace_p); | |
3233 | ec->code += function_code; | |
3234 | ec->code += EMBEDDED_FETCH_DEREF_DONE; | |
5f36109e JS |
3235 | fdecl->body = ec; |
3236 | ||
3237 | // Synthesize a functioncall. | |
3238 | functioncall* fcall = new functioncall; | |
3239 | fcall->tok = e->tok; | |
3240 | fcall->function = fdecl->name; | |
1c0be8c7 | 3241 | fcall->type = fdecl->type; |
5f36109e | 3242 | |
1c0be8c7 JS |
3243 | // If this code snippet uses a precomputed pointer, |
3244 | // pass that as the first argument. | |
5f36109e JS |
3245 | if (pointer) |
3246 | { | |
5f36109e JS |
3247 | vardecl *v = new vardecl; |
3248 | v->type = pe_long; | |
3249 | v->name = "pointer"; | |
3250 | v->tok = e->tok; | |
3251 | fdecl->formal_args.push_back(v); | |
3252 | fcall->args.push_back(pointer); | |
3253 | } | |
5f36109e | 3254 | |
1c0be8c7 | 3255 | // Any non-literal indexes need to be passed as arguments too. |
5f36109e JS |
3256 | for (unsigned i = 0; i < e->components.size(); ++i) |
3257 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3258 | { | |
3259 | vardecl *v = new vardecl; | |
3260 | v->type = pe_long; | |
3261 | v->name = "index" + lex_cast(i); | |
3262 | v->tok = e->tok; | |
3263 | fdecl->formal_args.push_back(v); | |
3264 | fcall->args.push_back(e->components[i].expr_index); | |
3265 | } | |
3266 | ||
1c0be8c7 JS |
3267 | // If this code snippet is assigning to an lvalue, |
3268 | // add a final argument for the rvalue. | |
3269 | if (lvalue_p) | |
3270 | { | |
3271 | // Modify the fdecl so it carries a single pe_long formal | |
3272 | // argument called "value". | |
5f36109e | 3273 | |
1c0be8c7 JS |
3274 | // FIXME: For the time being we only support setting target |
3275 | // variables which have base types; these are 'pe_long' in | |
3276 | // stap's type vocabulary. Strings and pointers might be | |
3277 | // reasonable, some day, but not today. | |
5f36109e | 3278 | |
1c0be8c7 JS |
3279 | vardecl *v = new vardecl; |
3280 | v->type = pe_long; | |
3281 | v->name = "value"; | |
3282 | v->tok = e->tok; | |
3283 | fdecl->formal_args.push_back(v); | |
3284 | // NB: We don't know the value for fcall argument yet. | |
3285 | // (see target_symbol_setter_functioncalls) | |
3286 | } | |
3287 | ||
3288 | // Add the synthesized decl to the session, and return the call. | |
3289 | fdecl->join (session); | |
5f36109e JS |
3290 | return fcall; |
3291 | } | |
3292 | ||
1c0be8c7 JS |
3293 | expression* |
3294 | dwarf_pretty_print::deref (target_symbol* e) | |
3295 | { | |
3296 | static unsigned tick = 0; | |
3297 | ||
3298 | if (!deref_p) | |
3299 | { | |
3300 | assert (pointer && e->components.empty()); | |
3301 | return pointer; | |
3302 | } | |
3303 | ||
3304 | bool lvalue_p = false; | |
3305 | string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++); | |
3306 | ||
3307 | string code; | |
3308 | exp_type type = pe_long; | |
3309 | if (pointer) | |
3310 | code = dw.literal_stmt_for_pointer (&pointer_type, e, false, type); | |
3311 | else if (!local.empty()) | |
3312 | code = dw.literal_stmt_for_local (scopes, pc, local, e, false, type); | |
3313 | else | |
3314 | code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, type); | |
3315 | ||
3316 | return synthetic_embedded_deref_call(dw.sess, name, code, type, | |
3317 | userspace_p, lvalue_p, e, pointer); | |
3318 | } | |
3319 | ||
5f36109e | 3320 | |
c55ea10d JS |
3321 | bool |
3322 | dwarf_pretty_print::push_deref (print_format* pf, const string& fmt, | |
3323 | target_symbol* e) | |
3324 | { | |
3325 | expression* e2 = NULL; | |
3326 | try | |
3327 | { | |
3328 | e2 = deref (e); | |
3329 | } | |
3330 | catch (const semantic_error&) | |
3331 | { | |
3332 | pf->raw_components.append ("?"); | |
3333 | return false; | |
3334 | } | |
3335 | pf->raw_components.append (fmt); | |
3336 | pf->args.push_back (e2); | |
3337 | return true; | |
3338 | } | |
3339 | ||
3340 | ||
e57b735a | 3341 | void |
a7999c82 | 3342 | dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e) |
e57b735a | 3343 | { |
a7999c82 JS |
3344 | // Get the full name of the target symbol. |
3345 | stringstream ts_name_stream; | |
3346 | e->print(ts_name_stream); | |
3347 | string ts_name = ts_name_stream.str(); | |
3348 | ||
3349 | // Check and make sure we haven't already seen this target | |
3350 | // variable in this return probe. If we have, just return our | |
3351 | // last replacement. | |
af234c40 | 3352 | map<string, expression *>::iterator i = return_ts_map.find(ts_name); |
a7999c82 | 3353 | if (i != return_ts_map.end()) |
85ecf79a | 3354 | { |
a7999c82 JS |
3355 | provide (i->second); |
3356 | return; | |
3357 | } | |
85ecf79a | 3358 | |
70208613 JS |
3359 | // Attempt the expansion directly first, so if there's a problem with the |
3360 | // variable we won't have a bogus entry probe lying around. Like in | |
3361 | // saveargs(), we pretend for a moment that we're not in a .return. | |
3362 | bool saved_has_return = q.has_return; | |
3363 | q.has_return = false; | |
3364 | expression *repl = e; | |
3365 | replace (repl); | |
3366 | q.has_return = saved_has_return; | |
3367 | target_symbol* n = dynamic_cast<target_symbol*>(repl); | |
3368 | if (n && n->saved_conversion_error) | |
3369 | { | |
3370 | provide (repl); | |
3371 | return; | |
3372 | } | |
3373 | ||
af234c40 JS |
3374 | expression *exp; |
3375 | if (!q.has_process && | |
3376 | strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0) | |
140be17a | 3377 | exp = gen_kretprobe_saved_return(repl); |
af234c40 | 3378 | else |
cc9001af | 3379 | exp = gen_mapped_saved_return(repl, e->sym_name()); |
af234c40 JS |
3380 | |
3381 | // Provide the variable to our parent so it can be used as a | |
3382 | // substitute for the target symbol. | |
3383 | provide (exp); | |
3384 | ||
3385 | // Remember this replacement since we might be able to reuse | |
3386 | // it later if the same return probe references this target | |
3387 | // symbol again. | |
3388 | return_ts_map[ts_name] = exp; | |
3389 | } | |
3390 | ||
4a2970a3 | 3391 | static expression* |
23dc94f6 DS |
3392 | gen_mapped_saved_return(systemtap_session &sess, expression* e, |
3393 | const string& name, | |
3394 | block *& add_block, bool& add_block_tid, | |
3395 | block *& add_call_probe, bool& add_call_probe_tid) | |
af234c40 | 3396 | { |
23dc94f6 DS |
3397 | static unsigned tick = 0; |
3398 | ||
a7999c82 JS |
3399 | // We've got to do several things here to handle target |
3400 | // variables in return probes. | |
85ecf79a | 3401 | |
a7999c82 JS |
3402 | // (1) Synthesize two global arrays. One is the cache of the |
3403 | // target variable and the other contains a thread specific | |
3404 | // nesting level counter. The arrays will look like | |
3405 | // this: | |
3406 | // | |
23dc94f6 DS |
3407 | // _entry_tvar_{name}_{num} |
3408 | // _entry_tvar_{name}_{num}_ctr | |
a7999c82 | 3409 | |
23dc94f6 | 3410 | string aname = (string("_entry_tvar_") |
cc9001af | 3411 | + name |
aca66a36 | 3412 | + "_" + lex_cast(tick++)); |
a7999c82 JS |
3413 | vardecl* vd = new vardecl; |
3414 | vd->name = aname; | |
3415 | vd->tok = e->tok; | |
23dc94f6 | 3416 | sess.globals.push_back (vd); |
a7999c82 JS |
3417 | |
3418 | string ctrname = aname + "_ctr"; | |
3419 | vd = new vardecl; | |
3420 | vd->name = ctrname; | |
3421 | vd->tok = e->tok; | |
23dc94f6 | 3422 | sess.globals.push_back (vd); |
a7999c82 JS |
3423 | |
3424 | // (2) Create a new code block we're going to insert at the | |
3425 | // beginning of this probe to get the cached value into a | |
3426 | // temporary variable. We'll replace the target variable | |
3427 | // reference with the temporary variable reference. The code | |
3428 | // will look like this: | |
3429 | // | |
23dc94f6 DS |
3430 | // _entry_tvar_tid = tid() |
3431 | // _entry_tvar_{name}_{num}_tmp | |
3432 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3433 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
3434 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3435 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
3436 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) | |
3437 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3438 | |
3439 | // (2a) Synthesize the tid temporary expression, which will look | |
3440 | // like this: | |
3441 | // | |
23dc94f6 | 3442 | // _entry_tvar_tid = tid() |
a7999c82 | 3443 | symbol* tidsym = new symbol; |
23dc94f6 | 3444 | tidsym->name = string("_entry_tvar_tid"); |
a7999c82 | 3445 | tidsym->tok = e->tok; |
85ecf79a | 3446 | |
a7999c82 JS |
3447 | if (add_block == NULL) |
3448 | { | |
3449 | add_block = new block; | |
3450 | add_block->tok = e->tok; | |
8cc799a5 | 3451 | } |
8c819921 | 3452 | |
8cc799a5 JS |
3453 | if (!add_block_tid) |
3454 | { | |
a7999c82 JS |
3455 | // Synthesize a functioncall to grab the thread id. |
3456 | functioncall* fc = new functioncall; | |
3457 | fc->tok = e->tok; | |
3458 | fc->function = string("tid"); | |
8c819921 | 3459 | |
23dc94f6 | 3460 | // Assign the tid to '_entry_tvar_tid'. |
8c819921 DS |
3461 | assignment* a = new assignment; |
3462 | a->tok = e->tok; | |
3463 | a->op = "="; | |
a7999c82 JS |
3464 | a->left = tidsym; |
3465 | a->right = fc; | |
8c819921 DS |
3466 | |
3467 | expr_statement* es = new expr_statement; | |
3468 | es->tok = e->tok; | |
3469 | es->value = a; | |
8c819921 | 3470 | add_block->statements.push_back (es); |
8cc799a5 | 3471 | add_block_tid = true; |
a7999c82 | 3472 | } |
8c819921 | 3473 | |
a7999c82 JS |
3474 | // (2b) Synthesize an array reference and assign it to a |
3475 | // temporary variable (that we'll use as replacement for the | |
3476 | // target variable reference). It will look like this: | |
3477 | // | |
23dc94f6 DS |
3478 | // _entry_tvar_{name}_{num}_tmp |
3479 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3480 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3481 | |
3482 | arrayindex* ai_tvar_base = new arrayindex; | |
3483 | ai_tvar_base->tok = e->tok; | |
3484 | ||
3485 | symbol* sym = new symbol; | |
3486 | sym->name = aname; | |
3487 | sym->tok = e->tok; | |
3488 | ai_tvar_base->base = sym; | |
3489 | ||
3490 | ai_tvar_base->indexes.push_back(tidsym); | |
3491 | ||
3492 | // We need to create a copy of the array index in its current | |
3493 | // state so we can have 2 variants of it (the original and one | |
3494 | // that post-decrements the second index). | |
3495 | arrayindex* ai_tvar = new arrayindex; | |
3496 | arrayindex* ai_tvar_postdec = new arrayindex; | |
3497 | *ai_tvar = *ai_tvar_base; | |
3498 | *ai_tvar_postdec = *ai_tvar_base; | |
3499 | ||
3500 | // Synthesize the | |
23dc94f6 | 3501 | // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the |
a7999c82 JS |
3502 | // second index into the array. |
3503 | arrayindex* ai_ctr = new arrayindex; | |
3504 | ai_ctr->tok = e->tok; | |
3505 | ||
3506 | sym = new symbol; | |
3507 | sym->name = ctrname; | |
3508 | sym->tok = e->tok; | |
3509 | ai_ctr->base = sym; | |
3510 | ai_ctr->indexes.push_back(tidsym); | |
3511 | ai_tvar->indexes.push_back(ai_ctr); | |
3512 | ||
3513 | symbol* tmpsym = new symbol; | |
3514 | tmpsym->name = aname + "_tmp"; | |
3515 | tmpsym->tok = e->tok; | |
3516 | ||
3517 | assignment* a = new assignment; | |
3518 | a->tok = e->tok; | |
3519 | a->op = "="; | |
3520 | a->left = tmpsym; | |
3521 | a->right = ai_tvar; | |
3522 | ||
3523 | expr_statement* es = new expr_statement; | |
3524 | es->tok = e->tok; | |
3525 | es->value = a; | |
3526 | ||
3527 | add_block->statements.push_back (es); | |
3528 | ||
3529 | // (2c) Add a post-decrement to the second array index and | |
3530 | // delete the array value. It will look like this: | |
3531 | // | |
23dc94f6 DS |
3532 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3533 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
a7999c82 JS |
3534 | |
3535 | post_crement* pc = new post_crement; | |
3536 | pc->tok = e->tok; | |
3537 | pc->op = "--"; | |
3538 | pc->operand = ai_ctr; | |
3539 | ai_tvar_postdec->indexes.push_back(pc); | |
3540 | ||
3541 | delete_statement* ds = new delete_statement; | |
3542 | ds->tok = e->tok; | |
3543 | ds->value = ai_tvar_postdec; | |
3544 | ||
3545 | add_block->statements.push_back (ds); | |
3546 | ||
3547 | // (2d) Delete the counter value if it is 0. It will look like | |
3548 | // this: | |
23dc94f6 DS |
3549 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) |
3550 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3551 | |
3552 | ds = new delete_statement; | |
3553 | ds->tok = e->tok; | |
3554 | ds->value = ai_ctr; | |
3555 | ||
3556 | unary_expression *ue = new unary_expression; | |
3557 | ue->tok = e->tok; | |
3558 | ue->op = "!"; | |
3559 | ue->operand = ai_ctr; | |
3560 | ||
3561 | if_statement *ifs = new if_statement; | |
3562 | ifs->tok = e->tok; | |
3563 | ifs->condition = ue; | |
3564 | ifs->thenblock = ds; | |
3565 | ifs->elseblock = NULL; | |
3566 | ||
3567 | add_block->statements.push_back (ifs); | |
3568 | ||
3569 | // (3) We need an entry probe that saves the value for us in the | |
3570 | // global array we created. Create the entry probe, which will | |
3571 | // look like this: | |
3572 | // | |
2260f4e3 | 3573 | // probe kernel.function("{function}").call { |
23dc94f6 DS |
3574 | // _entry_tvar_tid = tid() |
3575 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3576 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3577 | // = ${param} |
3578 | // } | |
3579 | ||
2260f4e3 | 3580 | if (add_call_probe == NULL) |
a7999c82 | 3581 | { |
2260f4e3 FCE |
3582 | add_call_probe = new block; |
3583 | add_call_probe->tok = e->tok; | |
8cc799a5 | 3584 | } |
4baf0e53 | 3585 | |
8cc799a5 JS |
3586 | if (!add_call_probe_tid) |
3587 | { | |
a7999c82 JS |
3588 | // Synthesize a functioncall to grab the thread id. |
3589 | functioncall* fc = new functioncall; | |
3590 | fc->tok = e->tok; | |
3591 | fc->function = string("tid"); | |
4baf0e53 | 3592 | |
23dc94f6 | 3593 | // Assign the tid to '_entry_tvar_tid'. |
a7999c82 | 3594 | assignment* a = new assignment; |
8fc05e57 DS |
3595 | a->tok = e->tok; |
3596 | a->op = "="; | |
a7999c82 JS |
3597 | a->left = tidsym; |
3598 | a->right = fc; | |
8fc05e57 | 3599 | |
a7999c82 | 3600 | expr_statement* es = new expr_statement; |
8fc05e57 DS |
3601 | es->tok = e->tok; |
3602 | es->value = a; | |
2260f4e3 | 3603 | add_call_probe = new block(add_call_probe, es); |
8cc799a5 | 3604 | add_call_probe_tid = true; |
85ecf79a | 3605 | } |
cf2a1f85 | 3606 | |
a7999c82 | 3607 | // Save the value, like this: |
23dc94f6 DS |
3608 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3609 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3610 | // = ${param} |
3611 | arrayindex* ai_tvar_preinc = new arrayindex; | |
3612 | *ai_tvar_preinc = *ai_tvar_base; | |
3613 | ||
3614 | pre_crement* preinc = new pre_crement; | |
3615 | preinc->tok = e->tok; | |
3616 | preinc->op = "++"; | |
3617 | preinc->operand = ai_ctr; | |
3618 | ai_tvar_preinc->indexes.push_back(preinc); | |
3619 | ||
3620 | a = new assignment; | |
3621 | a->tok = e->tok; | |
3622 | a->op = "="; | |
3623 | a->left = ai_tvar_preinc; | |
3624 | a->right = e; | |
3625 | ||
3626 | es = new expr_statement; | |
3627 | es->tok = e->tok; | |
3628 | es->value = a; | |
3629 | ||
2260f4e3 | 3630 | add_call_probe = new block(add_call_probe, es); |
a7999c82 | 3631 | |
23dc94f6 | 3632 | // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to |
a7999c82 JS |
3633 | // our parent so it can be used as a substitute for the target |
3634 | // symbol. | |
3f803f9e | 3635 | delete ai_tvar_base; |
af234c40 JS |
3636 | return tmpsym; |
3637 | } | |
a7999c82 | 3638 | |
af234c40 | 3639 | |
23dc94f6 DS |
3640 | expression* |
3641 | dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e, | |
3642 | const string& name) | |
3643 | { | |
3644 | return ::gen_mapped_saved_return(q.sess, e, name, add_block, | |
3645 | add_block_tid, add_call_probe, | |
3646 | add_call_probe_tid); | |
3647 | } | |
3648 | ||
3649 | ||
af234c40 | 3650 | expression* |
140be17a | 3651 | dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e) |
af234c40 JS |
3652 | { |
3653 | // The code for this is simple. | |
3654 | // | |
3655 | // .call: | |
3656 | // _set_kretprobe_long(index, $value) | |
3657 | // | |
3658 | // .return: | |
3659 | // _get_kretprobe_long(index) | |
3660 | // | |
3661 | // (or s/long/string/ for things like $$parms) | |
3662 | ||
3663 | unsigned index; | |
3664 | string setfn, getfn; | |
3665 | ||
140be17a JS |
3666 | // We need the caller to predetermine the type of the expression! |
3667 | switch (e->type) | |
af234c40 | 3668 | { |
140be17a | 3669 | case pe_string: |
af234c40 JS |
3670 | index = saved_strings++; |
3671 | setfn = "_set_kretprobe_string"; | |
3672 | getfn = "_get_kretprobe_string"; | |
140be17a JS |
3673 | break; |
3674 | case pe_long: | |
af234c40 JS |
3675 | index = saved_longs++; |
3676 | setfn = "_set_kretprobe_long"; | |
3677 | getfn = "_get_kretprobe_long"; | |
140be17a JS |
3678 | break; |
3679 | default: | |
dc09353a | 3680 | throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok); |
af234c40 JS |
3681 | } |
3682 | ||
3683 | // Create the entry code | |
3684 | // _set_kretprobe_{long|string}(index, $value) | |
3685 | ||
3686 | if (add_call_probe == NULL) | |
3687 | { | |
3688 | add_call_probe = new block; | |
3689 | add_call_probe->tok = e->tok; | |
3690 | } | |
3691 | ||
3692 | functioncall* set_fc = new functioncall; | |
3693 | set_fc->tok = e->tok; | |
3694 | set_fc->function = setfn; | |
3695 | set_fc->args.push_back(new literal_number(index)); | |
3696 | set_fc->args.back()->tok = e->tok; | |
3697 | set_fc->args.push_back(e); | |
3698 | ||
3699 | expr_statement* set_es = new expr_statement; | |
3700 | set_es->tok = e->tok; | |
3701 | set_es->value = set_fc; | |
3702 | ||
3703 | add_call_probe->statements.push_back(set_es); | |
3704 | ||
3705 | // Create the return code | |
3706 | // _get_kretprobe_{long|string}(index) | |
3707 | ||
3708 | functioncall* get_fc = new functioncall; | |
3709 | get_fc->tok = e->tok; | |
3710 | get_fc->function = getfn; | |
3711 | get_fc->args.push_back(new literal_number(index)); | |
3712 | get_fc->args.back()->tok = e->tok; | |
3713 | ||
3714 | return get_fc; | |
a7999c82 | 3715 | } |
a43ba433 | 3716 | |
2cb3fe26 | 3717 | |
a7999c82 JS |
3718 | void |
3719 | dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
3720 | { | |
b78a0fbb LB |
3721 | if (null_die(scope_die)) { |
3722 | print_format* pf = print_format::create(e->tok, "sprintf"); | |
3723 | pf->raw_components = ""; | |
3724 | pf->components = print_format::string_to_components(pf->raw_components); | |
3725 | pf->type = pe_string; | |
3726 | provide (pf); | |
a7999c82 | 3727 | return; |
b78a0fbb | 3728 | } |
2cb3fe26 | 3729 | |
5f36109e JS |
3730 | target_symbol *tsym = new target_symbol(*e); |
3731 | ||
fde50242 JS |
3732 | bool pretty = (!e->components.empty() && |
3733 | e->components[0].type == target_symbol::comp_pretty_print); | |
3734 | string format = pretty ? "=%s" : "=%#x"; | |
a43ba433 | 3735 | |
a7999c82 JS |
3736 | // Convert $$parms to sprintf of a list of parms and active local vars |
3737 | // which we recursively evaluate | |
a43ba433 | 3738 | |
1c922ad7 | 3739 | print_format* pf = print_format::create(e->tok, "sprintf"); |
a7999c82 | 3740 | |
277c21bc | 3741 | if (q.has_return && (e->name == "$$return")) |
a7999c82 | 3742 | { |
277c21bc | 3743 | tsym->name = "$return"; |
a7999c82 JS |
3744 | |
3745 | // Ignore any variable that isn't accessible. | |
3746 | tsym->saved_conversion_error = 0; | |
3747 | expression *texp = tsym; | |
8b095b45 | 3748 | replace (texp); // NB: throws nothing ... |
a7999c82 | 3749 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
a43ba433 | 3750 | { |
2cb3fe26 | 3751 | |
a43ba433 FCE |
3752 | } |
3753 | else | |
3754 | { | |
a7999c82 | 3755 | pf->raw_components += "return"; |
5f36109e | 3756 | pf->raw_components += format; |
a7999c82 JS |
3757 | pf->args.push_back(texp); |
3758 | } | |
3759 | } | |
3760 | else | |
3761 | { | |
3762 | // non-.return probe: support $$parms, $$vars, $$locals | |
345bbb3d | 3763 | bool first = true; |
a7999c82 | 3764 | Dwarf_Die result; |
d48bc7eb JS |
3765 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
3766 | for (unsigned i = 0; i < scopes.size(); ++i) | |
3767 | { | |
3768 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
3769 | break; // we don't want file-level variables | |
3770 | if (dwarf_child (&scopes[i], &result) == 0) | |
3771 | do | |
00cf3709 | 3772 | { |
d48bc7eb JS |
3773 | switch (dwarf_tag (&result)) |
3774 | { | |
3775 | case DW_TAG_variable: | |
3776 | if (e->name == "$$parms") | |
3777 | continue; | |
3778 | break; | |
3779 | case DW_TAG_formal_parameter: | |
3780 | if (e->name == "$$locals") | |
3781 | continue; | |
3782 | break; | |
3783 | ||
3784 | default: | |
3785 | continue; | |
3786 | } | |
41c262f3 | 3787 | |
d48bc7eb JS |
3788 | const char *diename = dwarf_diename (&result); |
3789 | if (! diename) continue; | |
f76427a2 | 3790 | |
d48bc7eb JS |
3791 | if (! first) |
3792 | pf->raw_components += " "; | |
3793 | pf->raw_components += diename; | |
fde50242 JS |
3794 | first = false; |
3795 | ||
3796 | // Write a placeholder for ugly aggregates | |
3797 | Dwarf_Die type; | |
3798 | if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type)) | |
3799 | { | |
3800 | q.dw.resolve_unqualified_inner_typedie(&type, &type, e); | |
3801 | switch (dwarf_tag(&type)) | |
3802 | { | |
3803 | case DW_TAG_union_type: | |
3804 | case DW_TAG_structure_type: | |
3805 | case DW_TAG_class_type: | |
3806 | pf->raw_components += "={...}"; | |
3807 | continue; | |
3808 | ||
3809 | case DW_TAG_array_type: | |
3810 | pf->raw_components += "=[...]"; | |
3811 | continue; | |
3812 | } | |
3813 | } | |
345bbb3d | 3814 | |
d48bc7eb JS |
3815 | tsym->name = "$"; |
3816 | tsym->name += diename; | |
41c262f3 | 3817 | |
d48bc7eb JS |
3818 | // Ignore any variable that isn't accessible. |
3819 | tsym->saved_conversion_error = 0; | |
3820 | expression *texp = tsym; | |
3821 | replace (texp); // NB: throws nothing ... | |
3822 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. | |
a43ba433 | 3823 | { |
d48bc7eb JS |
3824 | if (q.sess.verbose>2) |
3825 | { | |
e26c2f83 | 3826 | for (const semantic_error *c = tsym->saved_conversion_error; |
d48bc7eb | 3827 | c != 0; |
c081af73 | 3828 | c = c->get_chain()) { |
4c5d9906 | 3829 | clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl; |
d48bc7eb JS |
3830 | } |
3831 | } | |
3832 | ||
3833 | pf->raw_components += "=?"; | |
a43ba433 | 3834 | } |
d48bc7eb JS |
3835 | else |
3836 | { | |
3837 | pf->raw_components += format; | |
3838 | pf->args.push_back(texp); | |
3839 | } | |
a7999c82 | 3840 | } |
d48bc7eb JS |
3841 | while (dwarf_siblingof (&result, &result) == 0); |
3842 | } | |
a7999c82 | 3843 | } |
2cb3fe26 | 3844 | |
a7999c82 | 3845 | pf->components = print_format::string_to_components(pf->raw_components); |
140be17a | 3846 | pf->type = pe_string; |
a7999c82 JS |
3847 | provide (pf); |
3848 | } | |
3849 | ||
2cb3fe26 | 3850 | |
bd1fcbad YZ |
3851 | void |
3852 | dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e) | |
3853 | { | |
3854 | // Fill in our current module context if needed | |
3855 | if (e->module.empty()) | |
3856 | e->module = q.dw.module_name; | |
3857 | ||
3858 | if (e->module == q.dw.module_name && e->cu_name.empty()) | |
3859 | { | |
3860 | // process like any other local | |
3861 | // e->sym_name() will do the right thing | |
3862 | visit_target_symbol(e); | |
3863 | return; | |
3864 | } | |
3865 | ||
3866 | var_expanding_visitor::visit_atvar_op(e); | |
3867 | } | |
3868 | ||
3869 | ||
a7999c82 JS |
3870 | void |
3871 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
3872 | { | |
bd1fcbad | 3873 | assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var")); |
a7999c82 | 3874 | visited = true; |
30263a73 FCE |
3875 | bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e)); |
3876 | // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars | |
a7999c82 | 3877 | |
70208613 | 3878 | try |
a7999c82 | 3879 | { |
c69a87e0 FCE |
3880 | bool lvalue = is_active_lvalue(e); |
3881 | if (lvalue && !q.sess.guru_mode) | |
dc09353a | 3882 | throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok); |
2cb3fe26 | 3883 | |
100a540e | 3884 | // XXX: process $context vars should be writable |
70208613 | 3885 | |
c69a87e0 FCE |
3886 | // See if we need to generate a new probe to save/access function |
3887 | // parameters from a return probe. PR 1382. | |
3888 | if (q.has_return | |
3889 | && !defined_being_checked | |
277c21bc JS |
3890 | && e->name != "$return" // not the special return-value variable handled below |
3891 | && e->name != "$$return") // nor the other special variable handled below | |
c69a87e0 FCE |
3892 | { |
3893 | if (lvalue) | |
dc09353a | 3894 | throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok); |
c69a87e0 FCE |
3895 | visit_target_symbol_saved_return(e); |
3896 | return; | |
3897 | } | |
e57b735a | 3898 | |
277c21bc JS |
3899 | if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals" |
3900 | || (q.has_return && (e->name == "$$return"))) | |
c69a87e0 FCE |
3901 | { |
3902 | if (lvalue) | |
dc09353a | 3903 | throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok); |
70208613 | 3904 | |
c69a87e0 | 3905 | if (e->addressof) |
dc09353a | 3906 | throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok); |
70208613 | 3907 | |
5f36109e JS |
3908 | e->assert_no_components("dwarf", true); |
3909 | ||
c69a87e0 FCE |
3910 | visit_target_symbol_context(e); |
3911 | return; | |
3912 | } | |
70208613 | 3913 | |
5f36109e JS |
3914 | if (!e->components.empty() && |
3915 | e->components.back().type == target_symbol::comp_pretty_print) | |
3916 | { | |
3917 | if (lvalue) | |
dc09353a | 3918 | throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 3919 | |
277c21bc | 3920 | if (q.has_return && (e->name == "$return")) |
5f36109e JS |
3921 | { |
3922 | dwarf_pretty_print dpp (q.dw, scope_die, addr, | |
3923 | q.has_process, *e); | |
3924 | dpp.expand()->visit(this); | |
3925 | } | |
3926 | else | |
3927 | { | |
3928 | dwarf_pretty_print dpp (q.dw, getscopes(e), addr, | |
cc9001af | 3929 | e->sym_name(), |
5f36109e JS |
3930 | q.has_process, *e); |
3931 | dpp.expand()->visit(this); | |
3932 | } | |
3933 | return; | |
3934 | } | |
3935 | ||
1c0be8c7 | 3936 | bool userspace_p = q.has_process; |
c69a87e0 | 3937 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
cc9001af | 3938 | + "_" + e->sym_name() |
c69a87e0 | 3939 | + "_" + lex_cast(tick++)); |
70208613 | 3940 | |
70208613 | 3941 | |
1c0be8c7 JS |
3942 | exp_type type = pe_long; |
3943 | string code; | |
277c21bc | 3944 | if (q.has_return && (e->name == "$return")) |
1c0be8c7 | 3945 | code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, type); |
e19fda4e | 3946 | else |
1c0be8c7 JS |
3947 | code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(), |
3948 | e, lvalue, type); | |
70208613 | 3949 | |
1c0be8c7 JS |
3950 | functioncall* n = synthetic_embedded_deref_call(q.sess, fname, code, type, |
3951 | userspace_p, lvalue, e); | |
70208613 | 3952 | |
c69a87e0 FCE |
3953 | if (lvalue) |
3954 | { | |
3955 | // Provide the functioncall to our parent, so that it can be | |
3956 | // used to substitute for the assignment node immediately above | |
3957 | // us. | |
3958 | assert(!target_symbol_setter_functioncalls.empty()); | |
3959 | *(target_symbol_setter_functioncalls.top()) = n; | |
3960 | } | |
70208613 | 3961 | |
1c0be8c7 JS |
3962 | // Revisit the functioncall so arguments can be expanded. |
3963 | n->visit (this); | |
66d284f4 FCE |
3964 | } |
3965 | catch (const semantic_error& er) | |
3966 | { | |
9fab2262 JS |
3967 | // We suppress this error message, and pass the unresolved |
3968 | // target_symbol to the next pass. We hope that this value ends | |
3969 | // up not being referenced after all, so it can be optimized out | |
3970 | // quietly. | |
1af1e62d | 3971 | e->chain (er); |
9fab2262 | 3972 | provide (e); |
66d284f4 | 3973 | } |
77de5e9e GH |
3974 | } |
3975 | ||
3976 | ||
c24447be JS |
3977 | void |
3978 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
3979 | { | |
3980 | // Fill in our current module context if needed | |
3981 | if (e->module.empty()) | |
3982 | e->module = q.dw.module_name; | |
3983 | ||
3984 | var_expanding_visitor::visit_cast_op(e); | |
3985 | } | |
3986 | ||
3987 | ||
8cc799a5 JS |
3988 | void |
3989 | dwarf_var_expanding_visitor::visit_entry_op (entry_op *e) | |
3990 | { | |
3991 | expression *repl = e; | |
3992 | if (q.has_return) | |
3993 | { | |
3994 | // expand the operand as if it weren't a return probe | |
3995 | q.has_return = false; | |
3996 | replace (e->operand); | |
3997 | q.has_return = true; | |
3998 | ||
3999 | // XXX it would be nice to use gen_kretprobe_saved_return when available, | |
4000 | // but it requires knowing the types already, which is problematic for | |
4001 | // arbitrary expressons. | |
cc9001af | 4002 | repl = gen_mapped_saved_return (e->operand, "entry"); |
8cc799a5 JS |
4003 | } |
4004 | provide (repl); | |
4005 | } | |
4006 | ||
3689db05 SC |
4007 | void |
4008 | dwarf_var_expanding_visitor::visit_perf_op (perf_op *e) | |
4009 | { | |
ace7c23f | 4010 | string e_lit_val = e->operand->value; |
b78a0fbb | 4011 | |
3689db05 | 4012 | add_block = new block; |
9eaaceaa | 4013 | add_block->tok = e->tok; |
3689db05 SC |
4014 | |
4015 | systemtap_session &s = this->q.sess; | |
4016 | map<string, pair<string,derived_probe*> >::iterator it; | |
4017 | // Find the associated perf.counter probe | |
4018 | for (it=s.perf_counters.begin(); | |
4019 | it != s.perf_counters.end(); | |
4020 | it++) | |
4021 | if ((*it).first == e_lit_val) | |
4022 | { | |
4023 | // if perf .function("name") omitted, then set it to this process name | |
4024 | if ((*it).second.first.length() == 0) | |
4025 | ((*it).second).first = this->q.user_path; | |
4026 | if (((*it).second).first == this->q.user_path) | |
4027 | break; | |
4028 | } | |
4029 | ||
4030 | if (it != s.perf_counters.end()) | |
4031 | { | |
698de6cc | 4032 | perf_counter_refs.insert((*it).second.second); |
3689db05 SC |
4033 | // __perf_read_N is assigned in the probe prologue |
4034 | symbol* sym = new symbol; | |
4653caf1 | 4035 | sym->tok = e->tok; |
3689db05 SC |
4036 | sym->name = "__perf_read_" + (*it).first; |
4037 | provide (sym); | |
4038 | } | |
4039 | else | |
dc09353a | 4040 | throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str())); |
3689db05 SC |
4041 | } |
4042 | ||
8cc799a5 | 4043 | |
729455a7 JS |
4044 | vector<Dwarf_Die>& |
4045 | dwarf_var_expanding_visitor::getscopes(target_symbol *e) | |
4046 | { | |
4047 | if (scopes.empty()) | |
4048 | { | |
f25a9197 CM |
4049 | if(scope_die != NULL) |
4050 | scopes = q.dw.getscopes(scope_die); | |
729455a7 | 4051 | if (scopes.empty()) |
b530b5b3 LB |
4052 | //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok); |
4053 | // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" )) | |
dc09353a | 4054 | throw SEMANTIC_ERROR ("unable to find any scopes containing " |
729455a7 JS |
4055 | + lex_cast_hex(addr) |
4056 | + ((scope_die == NULL) ? "" | |
4057 | : (string (" in ") | |
4058 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
4059 | + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") | |
4060 | + ")")) | |
4061 | + " while searching for local '" | |
cc9001af | 4062 | + e->sym_name() + "'", |
729455a7 JS |
4063 | e->tok); |
4064 | } | |
4065 | return scopes; | |
4066 | } | |
4067 | ||
4068 | ||
5f36109e JS |
4069 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor |
4070 | { | |
4071 | systemtap_session& s; | |
4072 | dwarf_builder& db; | |
4073 | ||
4074 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
4075 | s(s), db(db) {} | |
4076 | void visit_cast_op (cast_op* e); | |
4077 | void filter_special_modules(string& module); | |
4078 | }; | |
4079 | ||
4080 | ||
c4ce66a1 JS |
4081 | struct dwarf_cast_query : public base_query |
4082 | { | |
946e1a48 | 4083 | cast_op& e; |
c4ce66a1 | 4084 | const bool lvalue; |
5f36109e JS |
4085 | const bool userspace_p; |
4086 | functioncall*& result; | |
c4ce66a1 | 4087 | |
5f36109e JS |
4088 | dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue, |
4089 | const bool userspace_p, functioncall*& result): | |
abb41d92 | 4090 | base_query(dw, module), e(e), lvalue(lvalue), |
5f36109e | 4091 | userspace_p(userspace_p), result(result) {} |
c4ce66a1 JS |
4092 | |
4093 | void handle_query_module(); | |
822a6a3d | 4094 | void query_library (const char *) {} |
576eaefe | 4095 | void query_plt (const char *entry, size_t addr) {} |
c4ce66a1 JS |
4096 | }; |
4097 | ||
4098 | ||
c4ce66a1 JS |
4099 | void |
4100 | dwarf_cast_query::handle_query_module() | |
4101 | { | |
5f36109e JS |
4102 | static unsigned tick = 0; |
4103 | ||
4104 | if (result) | |
c4ce66a1 JS |
4105 | return; |
4106 | ||
ea1e477a | 4107 | // look for the type in any CU |
a44a7cb5 JS |
4108 | Dwarf_Die* type_die = NULL; |
4109 | if (startswith(e.type_name, "class ")) | |
4110 | { | |
4111 | // normalize to match dwflpp::global_alias_caching_callback | |
4112 | string struct_name = "struct " + e.type_name.substr(6); | |
4113 | type_die = dw.declaration_resolve_other_cus(struct_name); | |
4114 | } | |
4115 | else | |
4116 | type_die = dw.declaration_resolve_other_cus(e.type_name); | |
4117 | ||
4118 | // NB: We now index the types as "struct name"/"union name"/etc. instead of | |
4119 | // just "name". But since we didn't require users to be explicit before, and | |
4120 | // actually sort of discouraged it, we must be flexible now. So if a lookup | |
4121 | // fails with a bare name, try augmenting it. | |
4122 | if (!type_die && | |
4123 | !startswith(e.type_name, "class ") && | |
4124 | !startswith(e.type_name, "struct ") && | |
4125 | !startswith(e.type_name, "union ") && | |
4126 | !startswith(e.type_name, "enum ")) | |
4127 | { | |
4128 | type_die = dw.declaration_resolve_other_cus("struct " + e.type_name); | |
4129 | if (!type_die) | |
4130 | type_die = dw.declaration_resolve_other_cus("union " + e.type_name); | |
4131 | if (!type_die) | |
4132 | type_die = dw.declaration_resolve_other_cus("enum " + e.type_name); | |
4133 | } | |
4134 | ||
ea1e477a JS |
4135 | if (!type_die) |
4136 | return; | |
c4ce66a1 | 4137 | |
5f36109e JS |
4138 | string code; |
4139 | exp_type type = pe_long; | |
4140 | ||
ea1e477a | 4141 | try |
c4ce66a1 | 4142 | { |
ea1e477a JS |
4143 | Dwarf_Die cu_mem; |
4144 | dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL)); | |
5f36109e JS |
4145 | |
4146 | if (!e.components.empty() && | |
4147 | e.components.back().type == target_symbol::comp_pretty_print) | |
4148 | { | |
4149 | if (lvalue) | |
dc09353a | 4150 | throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e.tok); |
5f36109e | 4151 | |
d19a9a82 | 4152 | dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e); |
5f36109e JS |
4153 | result = dpp.expand(); |
4154 | return; | |
4155 | } | |
4156 | ||
4157 | code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type); | |
ea1e477a JS |
4158 | } |
4159 | catch (const semantic_error& er) | |
4160 | { | |
4161 | // NB: we can have multiple errors, since a @cast | |
1af1e62d JS |
4162 | // may be attempted using several different modules: |
4163 | // @cast(ptr, "type", "module1:module2:...") | |
4164 | e.chain (er); | |
c4ce66a1 | 4165 | } |
c4ce66a1 | 4166 | |
5f36109e JS |
4167 | if (code.empty()) |
4168 | return; | |
c4ce66a1 | 4169 | |
5f36109e | 4170 | string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get") |
cc9001af | 4171 | + "_" + e.sym_name() |
5f36109e | 4172 | + "_" + lex_cast(tick++)); |
c4ce66a1 | 4173 | |
1c0be8c7 JS |
4174 | result = synthetic_embedded_deref_call(dw.sess, fname, code, type, |
4175 | userspace_p, lvalue, &e, e.operand); | |
5f36109e | 4176 | } |
c4ce66a1 JS |
4177 | |
4178 | ||
fb0274bc JS |
4179 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
4180 | { | |
d90053e7 | 4181 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 4182 | // for those cases, build a module including that header |
d90053e7 | 4183 | if (module[module.size() - 1] == '>' && |
60d98537 | 4184 | (module[0] == '<' || startswith(module, "kernel<"))) |
fb0274bc JS |
4185 | { |
4186 | string cached_module; | |
4187 | if (s.use_cache) | |
4188 | { | |
4189 | // see if the cached module exists | |
a2639cb7 | 4190 | cached_module = find_typequery_hash(s, module); |
d105f664 | 4191 | if (!cached_module.empty() && !s.poison_cache) |
fb0274bc JS |
4192 | { |
4193 | int fd = open(cached_module.c_str(), O_RDONLY); | |
4194 | if (fd != -1) | |
4195 | { | |
4196 | if (s.verbose > 2) | |
b530b5b3 LB |
4197 | //TRANSLATORS: Here we're using a cached module. |
4198 | clog << _("Pass 2: using cached ") << cached_module << endl; | |
fb0274bc JS |
4199 | module = cached_module; |
4200 | close(fd); | |
4201 | return; | |
4202 | } | |
4203 | } | |
4204 | } | |
4205 | ||
4206 | // no cached module, time to make it | |
d90053e7 | 4207 | if (make_typequery(s, module) == 0) |
fb0274bc | 4208 | { |
e16dc041 | 4209 | // try to save typequery in the cache |
fb0274bc | 4210 | if (s.use_cache) |
e16dc041 | 4211 | copy_file(module, cached_module, s.verbose > 2); |
fb0274bc JS |
4212 | } |
4213 | } | |
4214 | } | |
4215 | ||
4216 | ||
c4ce66a1 JS |
4217 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
4218 | { | |
4219 | bool lvalue = is_active_lvalue(e); | |
4220 | if (lvalue && !s.guru_mode) | |
dc09353a | 4221 | throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok); |
c4ce66a1 JS |
4222 | |
4223 | if (e->module.empty()) | |
4224 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
4225 | ||
5f36109e | 4226 | functioncall* result = NULL; |
8b31197b JS |
4227 | |
4228 | // split the module string by ':' for alternatives | |
4229 | vector<string> modules; | |
4230 | tokenize(e->module, modules, ":"); | |
b5a0dd41 | 4231 | bool userspace_p=false; // PR10601 |
5f36109e | 4232 | for (unsigned i = 0; !result && i < modules.size(); ++i) |
c4ce66a1 | 4233 | { |
8b31197b | 4234 | string& module = modules[i]; |
fb0274bc | 4235 | filter_special_modules(module); |
abb41d92 | 4236 | |
c4ce66a1 JS |
4237 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
4238 | // which means that userspace modules won't get any PATH searching. | |
4239 | dwflpp* dw; | |
707bf35e JS |
4240 | try |
4241 | { | |
b5a0dd41 FCE |
4242 | userspace_p=is_user_module (module); |
4243 | if (! userspace_p) | |
707bf35e JS |
4244 | { |
4245 | // kernel or kernel module target | |
ae2552da | 4246 | dw = db.get_kern_dw(s, module); |
707bf35e JS |
4247 | } |
4248 | else | |
4249 | { | |
05fb3e0c | 4250 | module = find_executable (module, "", s.sysenv); // canonicalize it |
707bf35e JS |
4251 | dw = db.get_user_dw(s, module); |
4252 | } | |
4253 | } | |
4254 | catch (const semantic_error& er) | |
4255 | { | |
4256 | /* ignore and go to the next module */ | |
4257 | continue; | |
4258 | } | |
c4ce66a1 | 4259 | |
5f36109e | 4260 | dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result); |
06de3a04 | 4261 | dw->iterate_over_modules<base_query>(&query_module, &q); |
c4ce66a1 | 4262 | } |
abb41d92 | 4263 | |
5f36109e | 4264 | if (!result) |
c4ce66a1 | 4265 | { |
946e1a48 JS |
4266 | // We pass the unresolved cast_op to the next pass, and hope |
4267 | // that this value ends up not being referenced after all, so | |
4268 | // it can be optimized out quietly. | |
c4ce66a1 JS |
4269 | provide (e); |
4270 | return; | |
4271 | } | |
4272 | ||
c4ce66a1 JS |
4273 | if (lvalue) |
4274 | { | |
4275 | // Provide the functioncall to our parent, so that it can be | |
4276 | // used to substitute for the assignment node immediately above | |
4277 | // us. | |
4278 | assert(!target_symbol_setter_functioncalls.empty()); | |
5f36109e | 4279 | *(target_symbol_setter_functioncalls.top()) = result; |
c4ce66a1 JS |
4280 | } |
4281 | ||
5f36109e | 4282 | result->visit (this); |
77de5e9e GH |
4283 | } |
4284 | ||
4285 | ||
bd1fcbad YZ |
4286 | struct dwarf_atvar_expanding_visitor: public var_expanding_visitor |
4287 | { | |
4288 | systemtap_session& s; | |
4289 | dwarf_builder& db; | |
4290 | ||
4291 | dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
4292 | s(s), db(db) {} | |
4293 | void visit_atvar_op (atvar_op* e); | |
4294 | }; | |
4295 | ||
4296 | ||
4297 | struct dwarf_atvar_query: public base_query | |
4298 | { | |
4299 | atvar_op& e; | |
4300 | const bool userspace_p, lvalue; | |
4301 | functioncall*& result; | |
4302 | unsigned& tick; | |
4303 | const string cu_name_pattern; | |
4304 | ||
4305 | dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e, | |
4306 | const bool userspace_p, const bool lvalue, | |
4307 | functioncall*& result, | |
4308 | unsigned& tick): | |
4309 | base_query(dw, module), e(e), | |
4310 | userspace_p(userspace_p), lvalue(lvalue), result(result), | |
4311 | tick(tick), cu_name_pattern(string("*/") + e.cu_name) {} | |
4312 | ||
4313 | void handle_query_module (); | |
4314 | void query_library (const char *) {} | |
4315 | void query_plt (const char *entry, size_t addr) {} | |
5c378838 | 4316 | static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q); |
bd1fcbad YZ |
4317 | }; |
4318 | ||
4319 | ||
4320 | int | |
5c378838 | 4321 | dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q) |
bd1fcbad | 4322 | { |
bd1fcbad YZ |
4323 | if (! q->e.cu_name.empty()) |
4324 | { | |
c60517ca | 4325 | const char *die_name = dwarf_diename(cudie) ?: ""; |
bd1fcbad YZ |
4326 | if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match |
4327 | && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0) | |
4328 | { | |
4329 | return DWARF_CB_OK; | |
4330 | } | |
4331 | } | |
4332 | ||
4333 | try | |
4334 | { | |
4335 | vector<Dwarf_Die> scopes(1, *cudie); | |
4336 | ||
4337 | q->dw.focus_on_cu (cudie); | |
4338 | ||
4339 | if (! q->e.components.empty() && | |
4340 | q->e.components.back().type == target_symbol::comp_pretty_print) | |
4341 | { | |
4342 | dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(), | |
4343 | q->userspace_p, q->e); | |
4344 | q->result = dpp.expand(); | |
4345 | return DWARF_CB_ABORT; | |
4346 | } | |
4347 | ||
4348 | exp_type type = pe_long; | |
4349 | string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(), | |
4350 | &q->e, q->lvalue, type); | |
4351 | ||
4352 | if (code.empty()) | |
4353 | return DWARF_CB_OK; | |
4354 | ||
4355 | string fname = (string(q->lvalue ? "_dwarf_tvar_set" | |
4356 | : "_dwarf_tvar_get") | |
4357 | + "_" + q->e.sym_name() | |
4358 | + "_" + lex_cast(q->tick++)); | |
4359 | ||
4360 | q->result = synthetic_embedded_deref_call (q->sess, fname, code, type, | |
4361 | q->userspace_p, q->lvalue, | |
4362 | &q->e); | |
4363 | } | |
4364 | catch (const semantic_error& er) | |
4365 | { | |
4366 | // Here we suppress the error because we often just have too many | |
4367 | // when scanning all the CUs. | |
4368 | return DWARF_CB_OK; | |
4369 | } | |
4370 | ||
4371 | if (q->result) { | |
4372 | return DWARF_CB_ABORT; | |
4373 | } | |
4374 | ||
4375 | return DWARF_CB_OK; | |
4376 | } | |
4377 | ||
4378 | ||
4379 | void | |
4380 | dwarf_atvar_query::handle_query_module () | |
4381 | { | |
4382 | ||
4383 | dw.iterate_over_cus(atvar_query_cu, this, false); | |
4384 | } | |
4385 | ||
4386 | ||
4387 | void | |
4388 | dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e) | |
4389 | { | |
4390 | const bool lvalue = is_active_lvalue(e); | |
4391 | if (lvalue && !s.guru_mode) | |
dc09353a | 4392 | throw SEMANTIC_ERROR(_("write to @var variable not permitted; " |
bd1fcbad YZ |
4393 | "need stap -g"), e->tok); |
4394 | ||
4395 | if (e->module.empty()) | |
4396 | e->module = "kernel"; | |
4397 | ||
4398 | functioncall* result = NULL; | |
4399 | ||
4400 | // split the module string by ':' for alternatives | |
4401 | vector<string> modules; | |
4402 | tokenize(e->module, modules, ":"); | |
4403 | bool userspace_p = false; | |
4404 | for (unsigned i = 0; !result && i < modules.size(); ++i) | |
4405 | { | |
4406 | string& module = modules[i]; | |
4407 | ||
4408 | dwflpp* dw; | |
4409 | try | |
4410 | { | |
4411 | userspace_p = is_user_module(module); | |
4412 | if (!userspace_p) | |
4413 | { | |
4414 | // kernel or kernel module target | |
4415 | dw = db.get_kern_dw(s, module); | |
4416 | } | |
4417 | else | |
4418 | { | |
4419 | module = find_executable(module, "", s.sysenv); | |
4420 | dw = db.get_user_dw(s, module); | |
4421 | } | |
4422 | } | |
4423 | catch (const semantic_error& er) | |
4424 | { | |
4425 | /* ignore and go to the next module */ | |
4426 | continue; | |
4427 | } | |
4428 | ||
4429 | dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick); | |
06de3a04 | 4430 | dw->iterate_over_modules<base_query>(&query_module, &q); |
bd1fcbad YZ |
4431 | |
4432 | if (result) | |
4433 | { | |
4434 | s.unwindsym_modules.insert(module); | |
bd1fcbad YZ |
4435 | |
4436 | if (lvalue) | |
4437 | { | |
4438 | // Provide the functioncall to our parent, so that it can be | |
4439 | // used to substitute for the assignment node immediately above | |
4440 | // us. | |
4441 | assert(!target_symbol_setter_functioncalls.empty()); | |
4442 | *(target_symbol_setter_functioncalls.top()) = result; | |
4443 | } | |
4444 | ||
4445 | result->visit(this); | |
4446 | return; | |
4447 | } | |
4448 | ||
4449 | /* Unable to find the variable in the current module, so we chain | |
4450 | * an error in atvar_op */ | |
dc09353a | 4451 | semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s", |
bd1fcbad YZ |
4452 | e->sym_name().c_str(), module.c_str(), |
4453 | e->cu_name.empty() ? "" : _(", in "), | |
4454 | e->cu_name.c_str())); | |
4455 | e->chain (er); | |
4456 | } | |
4457 | ||
4458 | provide(e); | |
4459 | } | |
4460 | ||
4461 | ||
b8da0ad1 FCE |
4462 | void |
4463 | dwarf_derived_probe::printsig (ostream& o) const | |
4464 | { | |
4465 | // Instead of just printing the plain locations, we add a PC value | |
4466 | // as a comment as a way of telling e.g. apart multiple inlined | |
4467 | // function instances. This is distinct from the verbose/clog | |
4468 | // output, since this part goes into the cache hash calculations. | |
4469 | sole_location()->print (o); | |
6d0f3f0c | 4470 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
4471 | printsig_nested (o); |
4472 | } | |
4473 | ||
4474 | ||
4475 | ||
dc38c0ae | 4476 | void |
b20febf3 FCE |
4477 | dwarf_derived_probe::join_group (systemtap_session& s) |
4478 | { | |
af234c40 JS |
4479 | // skip probes which are paired entry-handlers |
4480 | if (!has_return && (saved_longs || saved_strings)) | |
4481 | return; | |
4482 | ||
b20febf3 FCE |
4483 | if (! s.dwarf_derived_probes) |
4484 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
4485 | s.dwarf_derived_probes->enroll (this); | |
4486 | } | |
4487 | ||
4488 | ||
2b69faaf JS |
4489 | static bool |
4490 | kernel_supports_inode_uprobes(systemtap_session& s) | |
4491 | { | |
4492 | // The arch-supports is new to the builtin inode-uprobes, so it makes a | |
4493 | // reasonable indicator of the new API. Else we'll need an autoconf... | |
64e807c2 | 4494 | // see also buildrun.cxx:kernel_built_uprobs() |
2b69faaf JS |
4495 | return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y" |
4496 | && s.kernel_config["CONFIG_UPROBES"] == "y"); | |
4497 | } | |
4498 | ||
4499 | ||
3667d615 JS |
4500 | static bool |
4501 | kernel_supports_inode_uretprobes(systemtap_session& s) | |
4502 | { | |
766013af JS |
4503 | // We need inode-uprobes first, then look for a sign of uretprobes. The only |
4504 | // non-static function at present is arch_uretprobe_hijack_return_addr. | |
3667d615 | 4505 | return kernel_supports_inode_uprobes(s) && |
af9e147f | 4506 | (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0); |
3667d615 JS |
4507 | } |
4508 | ||
4509 | ||
5261f7ab DS |
4510 | void |
4511 | check_process_probe_kernel_support(systemtap_session& s) | |
4512 | { | |
4513 | // If we've got utrace, we're good to go. | |
4514 | if (s.kernel_config["CONFIG_UTRACE"] == "y") | |
4515 | return; | |
4516 | ||
8c021542 DS |
4517 | // We don't have utrace. For process probes that aren't |
4518 | // uprobes-based, we just need the task_finder. The task_finder | |
b266d318 DS |
4519 | // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a |
4520 | // specific autoconf test for its needs. | |
8c021542 DS |
4521 | // |
4522 | // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty | |
4523 | // approximation. | |
4524 | if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y") | |
4525 | return; | |
4526 | ||
d3e959b0 DS |
4527 | // For uprobes-based process probes, we need the task_finder plus |
4528 | // the builtin inode-uprobes. | |
8c021542 DS |
4529 | if (s.need_uprobes |
4530 | && s.kernel_config["CONFIG_TRACEPOINTS"] == "y" | |
d3e959b0 | 4531 | && kernel_supports_inode_uprobes(s)) |
8c021542 DS |
4532 | return; |
4533 | ||
dc09353a | 4534 | throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES")); |
5261f7ab DS |
4535 | } |
4536 | ||
4537 | ||
b20febf3 FCE |
4538 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, |
4539 | const string& filename, | |
4540 | int line, | |
91af0778 | 4541 | // module & section specify a relocation |
b20febf3 FCE |
4542 | // base for <addr>, unless section=="" |
4543 | // (equivalently module=="kernel") | |
4544 | const string& module, | |
4545 | const string& section, | |
4546 | // NB: dwfl_addr is the virtualized | |
4547 | // address for this symbol. | |
4548 | Dwarf_Addr dwfl_addr, | |
4549 | // addr is the section-offset for | |
4550 | // actual relocation. | |
4551 | Dwarf_Addr addr, | |
4552 | dwarf_query& q, | |
37ebca01 | 4553 | Dwarf_Die* scope_die /* may be null */) |
4c5d1300 | 4554 | : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ), |
b20febf3 | 4555 | module (module), section (section), addr (addr), |
63b4fd14 | 4556 | path (q.path), |
27dc09b1 | 4557 | has_process (q.has_process), |
c9bad430 DS |
4558 | has_return (q.has_return), |
4559 | has_maxactive (q.has_maxactive), | |
c57ea854 | 4560 | has_library (q.has_library), |
6b66b9f7 | 4561 | maxactive_val (q.maxactive_val), |
b642c901 SC |
4562 | user_path (q.user_path), |
4563 | user_lib (q.user_lib), | |
af234c40 | 4564 | access_vars(false), |
c57ea854 | 4565 | saved_longs(0), saved_strings(0), |
af234c40 | 4566 | entry_handler(0) |
bd2b1e68 | 4567 | { |
b642c901 SC |
4568 | if (user_lib.size() != 0) |
4569 | has_library = true; | |
4570 | ||
6b66b9f7 JS |
4571 | if (q.has_process) |
4572 | { | |
4573 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. | |
4574 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
4575 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
4576 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
4577 | // XXX Assert invariants here too? | |
2b69faaf JS |
4578 | |
4579 | // inode-uprobes needs an offset rather than an absolute VM address. | |
4441e344 | 4580 | // ditto for userspace runtimes (dyninst) |
ac3af990 | 4581 | if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) && |
2b69faaf JS |
4582 | section == ".absolute" && addr == dwfl_addr && |
4583 | addr >= q.dw.module_start && addr < q.dw.module_end) | |
4584 | this->addr = addr - q.dw.module_start; | |
6b66b9f7 JS |
4585 | } |
4586 | else | |
4587 | { | |
4588 | // Assert kernel relocation invariants | |
4589 | if (section == "" && dwfl_addr != addr) // addr should be absolute | |
dc09353a | 4590 | throw SEMANTIC_ERROR (_("missing relocation basis"), tok); |
6b66b9f7 | 4591 | if (section != "" && dwfl_addr == addr) // addr should be an offset |
dc09353a | 4592 | throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok); |
6b66b9f7 | 4593 | } |
2930abc7 | 4594 | |
21beacc9 FCE |
4595 | // XXX: hack for strange g++/gcc's |
4596 | #ifndef USHRT_MAX | |
4597 | #define USHRT_MAX 32767 | |
4598 | #endif | |
4599 | ||
606fd9c8 | 4600 | // Range limit maxactive() value |
6b66b9f7 | 4601 | if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX)) |
dc09353a | 4602 | throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]", |
b530b5b3 | 4603 | lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok); |
606fd9c8 | 4604 | |
de688825 | 4605 | // Expand target variables in the probe body |
b78a0fbb | 4606 | if (!null_die(scope_die) || (!q.has_kernel && q.dw.has_gnu_debugdata())) |
8fc05e57 | 4607 | { |
6b66b9f7 | 4608 | // XXX: user-space deref's for q.has_process! |
5a617dc6 JL |
4609 | |
4610 | // PR14436: if we're expanding target variables in the probe body of a | |
4611 | // .return probe, we need to make the expansion at the postprologue addr | |
4612 | // instead (if any), which is then also the spot where the entry handler | |
4613 | // probe is placed. (Note that at this point, a nonzero prologue_end | |
4614 | // implies that it should be used, i.e. code is unoptimized). | |
4615 | Dwarf_Addr handler_dwfl_addr = dwfl_addr; | |
4616 | if (q.prologue_end != 0 && q.has_return) | |
4617 | { | |
4618 | handler_dwfl_addr = q.prologue_end; | |
4619 | if (q.sess.verbose > 2) | |
4620 | clog << _F("expanding .return vars at prologue_end (0x%s) " | |
4621 | "rather than entrypc (0x%s)\n", | |
4622 | lex_cast_hex(handler_dwfl_addr).c_str(), | |
4623 | lex_cast_hex(dwfl_addr).c_str()); | |
4624 | } | |
4625 | dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr); | |
8b095b45 | 4626 | v.replace (this->body); |
3689db05 SC |
4627 | |
4628 | // Propagate perf.counters so we can emit later | |
4629 | this->perf_counter_refs = v.perf_counter_refs; | |
4630 | // Emit local var used to save the perf counter read value | |
698de6cc | 4631 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
4632 | for (pcii = v.perf_counter_refs.begin(); |
4633 | pcii != v.perf_counter_refs.end(); pcii++) | |
4634 | { | |
4635 | map<string, pair<string,derived_probe*> >::iterator it; | |
4636 | // Find the associated perf counter probe | |
4637 | for (it=q.sess.perf_counters.begin() ; | |
4638 | it != q.sess.perf_counters.end(); | |
4639 | it++) | |
4640 | if ((*it).second.second == (*pcii)) | |
4641 | break; | |
4642 | vardecl* vd = new vardecl; | |
4643 | vd->name = "__perf_read_" + (*it).first; | |
4644 | vd->tok = this->tok; | |
4645 | vd->set_arity(0, this->tok); | |
4646 | vd->type = pe_long; | |
4647 | vd->synthetic = true; | |
4648 | this->locals.push_back (vd); | |
4649 | } | |
4650 | ||
4651 | ||
6b66b9f7 JS |
4652 | if (!q.has_process) |
4653 | access_vars = v.visited; | |
37ebca01 FCE |
4654 | |
4655 | // If during target-variable-expanding the probe, we added a new block | |
4656 | // of code, add it to the start of the probe. | |
4657 | if (v.add_block) | |
ba6f838d | 4658 | this->body = new block(v.add_block, this->body); |
2260f4e3 FCE |
4659 | |
4660 | // If when target-variable-expanding the probe, we need to synthesize a | |
4661 | // sibling function-entry probe. We don't go through the whole probe derivation | |
4662 | // business (PR10642) that could lead to wildcard/alias resolution, or for that | |
4663 | // dwarf-induced duplication. | |
4664 | if (v.add_call_probe) | |
37ebca01 | 4665 | { |
2260f4e3 FCE |
4666 | assert (q.has_return && !q.has_call); |
4667 | ||
4668 | // We temporarily replace q.base_probe. | |
4669 | statement* old_body = q.base_probe->body; | |
4670 | q.base_probe->body = v.add_call_probe; | |
4671 | q.has_return = false; | |
4672 | q.has_call = true; | |
af234c40 | 4673 | |
da23eceb | 4674 | if (q.has_process) |
5a617dc6 JL |
4675 | { |
4676 | // Place handler probe at the same addr as where the vars were | |
4677 | // expanded (which may not be the same addr as the one for the | |
4678 | // main retprobe, PR14436). | |
4679 | Dwarf_Addr handler_addr = addr; | |
4680 | if (handler_dwfl_addr != dwfl_addr) | |
4681 | // adjust section offset by prologue_end-entrypc | |
4682 | handler_addr += handler_dwfl_addr - dwfl_addr; | |
4683 | entry_handler = new uprobe_derived_probe (funcname, filename, | |
4684 | line, module, section, | |
4685 | handler_dwfl_addr, | |
4686 | handler_addr, q, | |
4687 | scope_die); | |
4688 | } | |
da23eceb | 4689 | else |
af234c40 JS |
4690 | entry_handler = new dwarf_derived_probe (funcname, filename, line, |
4691 | module, section, dwfl_addr, | |
4692 | addr, q, scope_die); | |
4693 | ||
4694 | saved_longs = entry_handler->saved_longs = v.saved_longs; | |
4695 | saved_strings = entry_handler->saved_strings = v.saved_strings; | |
4696 | ||
4697 | q.results.push_back (entry_handler); | |
2260f4e3 FCE |
4698 | |
4699 | q.has_return = true; | |
4700 | q.has_call = false; | |
4701 | q.base_probe->body = old_body; | |
37ebca01 | 4702 | } |
f10534c6 WH |
4703 | // Save the local variables for listing mode |
4704 | if (q.sess.listing_mode_vars) | |
8c67c337 | 4705 | saveargs(q, scope_die, dwfl_addr); |
8fc05e57 | 4706 | } |
37ebca01 | 4707 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 4708 | |
f10534c6 | 4709 | // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs |
0a98fd42 | 4710 | |
5d23847d | 4711 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
4712 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
4713 | // point. This allows a user to see what function / file / line | |
4714 | // number any particular match of the wildcards. | |
919debfc JL |
4715 | |
4716 | vector<probe_point::component*> comps; | |
4717 | if (q.has_kernel) | |
4718 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
4719 | else if(q.has_module) | |
4720 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
4721 | else if(q.has_process) | |
4722 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
4723 | else | |
4724 | assert (0); | |
4725 | ||
4726 | string fn_or_stmt; | |
4727 | if (q.has_function_str || q.has_function_num) | |
e772a6e7 | 4728 | fn_or_stmt = TOK_FUNCTION; |
919debfc | 4729 | else |
e772a6e7 | 4730 | fn_or_stmt = TOK_STATEMENT; |
919debfc JL |
4731 | |
4732 | if (q.has_function_str || q.has_statement_str) | |
4733 | { | |
e772a6e7 | 4734 | string retro_name = q.final_function_name(funcname, filename, line); |
919debfc JL |
4735 | comps.push_back |
4736 | (new probe_point::component | |
4737 | (fn_or_stmt, new literal_string (retro_name))); | |
4738 | } | |
4739 | else if (q.has_function_num || q.has_statement_num) | |
4740 | { | |
4741 | Dwarf_Addr retro_addr; | |
4742 | if (q.has_function_num) | |
4743 | retro_addr = q.function_num_val; | |
4744 | else | |
4745 | retro_addr = q.statement_num_val; | |
4746 | comps.push_back (new probe_point::component | |
4747 | (fn_or_stmt, | |
4748 | new literal_number(retro_addr, true))); | |
4749 | ||
4750 | if (q.has_absolute) | |
4751 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
4752 | } | |
4753 | ||
4754 | if (q.has_call) | |
4755 | comps.push_back (new probe_point::component(TOK_CALL)); | |
4756 | if (q.has_exported) | |
4757 | comps.push_back (new probe_point::component(TOK_EXPORTED)); | |
4758 | if (q.has_inline) | |
4759 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
4760 | if (has_return) | |
4761 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
4762 | if (has_maxactive) | |
4763 | comps.push_back (new probe_point::component | |
4764 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
4765 | ||
4766 | // Overwrite it. | |
4767 | this->sole_location()->components = comps; | |
e2941743 JL |
4768 | |
4769 | // if it's a .callee[s[(N)]] call, add checks to the probe body so that the | |
4770 | // user body is only 'triggered' when called from q.callers[N-1], which | |
4771 | // itself is called from q.callers[N-2], etc... I.E. | |
4772 | // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1) | |
4773 | if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty()) | |
4774 | { | |
4775 | if (q.sess.verbose > 2) | |
7761da8b | 4776 | clog << _F("adding caller checks for callee %s\n", funcname.c_str()); |
e2941743 JL |
4777 | |
4778 | // Copy the stack and empty it out | |
4779 | stack<Dwarf_Addr> callers(*q.callers); | |
4780 | for (unsigned level = 1; !callers.empty(); level++, | |
4781 | callers.pop()) | |
4782 | { | |
4783 | Dwarf_Addr caller = callers.top(); | |
4784 | ||
4785 | // We first need to make the caller addr relocatable | |
4786 | string caller_section; | |
4787 | Dwarf_Addr caller_reloc; | |
4788 | if (module == TOK_KERNEL) | |
4789 | { // allow for relocatable kernel (see also add_probe_point()) | |
4790 | caller_reloc = caller - q.sess.sym_stext; | |
4791 | caller_section = "_stext"; | |
4792 | } | |
4793 | else | |
4794 | caller_reloc = q.dw.relocate_address(caller, | |
4795 | caller_section); | |
4796 | ||
4797 | if (q.sess.verbose > 2) | |
7761da8b JL |
4798 | clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n", |
4799 | level, lex_cast_hex(caller_reloc).c_str()); | |
e2941743 JL |
4800 | |
4801 | // We want to add a statement like this: | |
4802 | // if (!caller_match(user, mod, sec, addr)) next; | |
4803 | // Something similar is done in semantic_pass_conditions() | |
4804 | ||
4805 | functioncall* check = new functioncall(); | |
4806 | check->tok = this->tok; | |
4807 | check->function = "caller_match"; | |
4808 | check->args.push_back(new literal_number(q.has_process)); | |
4809 | check->args[0]->tok = this->tok; | |
4810 | check->args.push_back(new literal_number(level)); | |
4811 | check->args[1]->tok = this->tok; | |
4812 | check->args.push_back(new literal_string(module)); | |
4813 | check->args[2]->tok = this->tok; | |
4814 | check->args.push_back(new literal_string(caller_section)); | |
4815 | check->args[3]->tok = this->tok; | |
4816 | check->args.push_back(new literal_number(caller_reloc, true /* hex */)); | |
4817 | check->args[4]->tok = this->tok; | |
4818 | ||
4819 | unary_expression* notexp = new unary_expression(); | |
4820 | notexp->tok = this->tok; | |
4821 | notexp->op = "!"; | |
4822 | notexp->operand = check; | |
4823 | ||
4824 | if_statement* ifs = new if_statement(); | |
4825 | ifs->tok = this->tok; | |
4826 | ifs->thenblock = new next_statement(); | |
4827 | ifs->thenblock->tok = this->tok; | |
4828 | ifs->elseblock = NULL; | |
4829 | ifs->condition = notexp; | |
4830 | ||
4831 | this->body = new block(ifs, this->body); | |
4832 | } | |
4833 | } | |
2930abc7 FCE |
4834 | } |
4835 | ||
bd2b1e68 | 4836 | |
0a98fd42 | 4837 | void |
8c67c337 JS |
4838 | dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die, |
4839 | Dwarf_Addr dwfl_addr) | |
0a98fd42 | 4840 | { |
9aa8ffce | 4841 | if (null_die(scope_die)) |
0a98fd42 | 4842 | return; |
0a98fd42 | 4843 | |
8c67c337 | 4844 | bool verbose = q.sess.verbose > 2; |
0a98fd42 | 4845 | |
8c67c337 | 4846 | if (verbose) |
b530b5b3 | 4847 | clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die)); |
0a98fd42 | 4848 | |
8c67c337 JS |
4849 | if (has_return) |
4850 | { | |
4851 | /* Only save the return value if it has a type. */ | |
4852 | string type_name; | |
4853 | Dwarf_Die type_die; | |
4854 | if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) && | |
4855 | dwarf_type_name(&type_die, type_name)) | |
4856 | args.push_back("$return:"+type_name); | |
4857 | ||
4858 | else if (verbose) | |
b530b5b3 LB |
4859 | clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n", |
4860 | lex_cast_hex(dwarf_dieoffset(scope_die)).c_str()); | |
8c67c337 | 4861 | } |
d87623a1 | 4862 | |
0a98fd42 | 4863 | Dwarf_Die arg; |
4ef35696 JS |
4864 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
4865 | for (unsigned i = 0; i < scopes.size(); ++i) | |
4866 | { | |
4867 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
4868 | break; // we don't want file-level variables | |
4869 | if (dwarf_child (&scopes[i], &arg) == 0) | |
4870 | do | |
0a98fd42 | 4871 | { |
4ef35696 JS |
4872 | switch (dwarf_tag (&arg)) |
4873 | { | |
4874 | case DW_TAG_variable: | |
4875 | case DW_TAG_formal_parameter: | |
4876 | break; | |
0a98fd42 | 4877 | |
4ef35696 JS |
4878 | default: |
4879 | continue; | |
4880 | } | |
0a98fd42 | 4881 | |
4ef35696 JS |
4882 | /* Ignore this local if it has no name. */ |
4883 | const char *arg_name = dwarf_diename (&arg); | |
4884 | if (!arg_name) | |
8c67c337 JS |
4885 | { |
4886 | if (verbose) | |
b530b5b3 LB |
4887 | clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n", |
4888 | lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4889 | continue; |
4890 | } | |
4ef35696 JS |
4891 | |
4892 | if (verbose) | |
b530b5b3 LB |
4893 | clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n", |
4894 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4895 | |
4896 | /* Ignore this local if it has no location (or not at this PC). */ | |
4897 | /* NB: It still may not be directly accessible, e.g. if it is an | |
4898 | * aggregate type, implicit_pointer, etc., but the user can later | |
4899 | * figure out how to access the interesting parts. */ | |
45b02a36 FCE |
4900 | |
4901 | /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing | |
4902 | * several synthetic | |
4903 | * probe foo { $var } | |
4904 | * probes, testing them for overall resolvability. | |
4905 | */ | |
4906 | ||
4ef35696 JS |
4907 | Dwarf_Attribute attr_mem; |
4908 | if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem)) | |
4909 | { | |
4910 | Dwarf_Op *expr; | |
4911 | size_t len; | |
4912 | if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem)) | |
4913 | { | |
4914 | if (verbose) | |
b530b5b3 LB |
4915 | clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n", |
4916 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4917 | continue; |
4918 | } | |
4919 | else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr, | |
4920 | &len, 1) == 1 && len > 0)) | |
4921 | { | |
45b02a36 FCE |
4922 | Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg); |
4923 | if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr, | |
4924 | &len, 1) == 1 && len > 0))) { | |
4925 | if (verbose) | |
4926 | clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n", | |
4927 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str()); | |
4928 | continue; | |
4929 | } | |
4ef35696 JS |
4930 | } |
4931 | } | |
4932 | ||
4933 | /* Ignore this local if it has no type. */ | |
4934 | string type_name; | |
4935 | Dwarf_Die type_die; | |
4936 | if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) || | |
4937 | !dwarf_type_name(&type_die, type_name)) | |
8c67c337 JS |
4938 | { |
4939 | if (verbose) | |
b530b5b3 LB |
4940 | clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n", |
4941 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4942 | continue; |
4943 | } | |
8c67c337 | 4944 | |
4ef35696 JS |
4945 | /* This local looks good -- save it! */ |
4946 | args.push_back("$"+string(arg_name)+":"+type_name); | |
8c67c337 | 4947 | } |
4ef35696 JS |
4948 | while (dwarf_siblingof (&arg, &arg) == 0); |
4949 | } | |
0a98fd42 JS |
4950 | } |
4951 | ||
4952 | ||
4953 | void | |
d0bfd2ac | 4954 | dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const |
0a98fd42 | 4955 | { |
d0bfd2ac | 4956 | arg_set.insert(arg_set.end(), args.begin(), args.end()); |
0a98fd42 JS |
4957 | } |
4958 | ||
4959 | ||
27dc09b1 | 4960 | void |
42e38653 | 4961 | dwarf_derived_probe::emit_privilege_assertion (translator_output* o) |
27dc09b1 DB |
4962 | { |
4963 | if (has_process) | |
4964 | { | |
4965 | // These probes are allowed for unprivileged users, but only in the | |
4966 | // context of processes which they own. | |
4967 | emit_process_owner_assertion (o); | |
4968 | return; | |
4969 | } | |
4970 | ||
4971 | // Other probes must contain the default assertion which aborts | |
4972 | // if executed by an unprivileged user. | |
42e38653 | 4973 | derived_probe::emit_privilege_assertion (o); |
27dc09b1 DB |
4974 | } |
4975 | ||
4976 | ||
4977 | void | |
4978 | dwarf_derived_probe::print_dupe_stamp(ostream& o) | |
4979 | { | |
4980 | if (has_process) | |
4981 | { | |
4982 | // These probes are allowed for unprivileged users, but only in the | |
4983 | // context of processes which they own. | |
4984 | print_dupe_stamp_unprivileged_process_owner (o); | |
4985 | return; | |
4986 | } | |
4987 | ||
4988 | // Other probes must contain the default dupe stamp | |
4989 | derived_probe::print_dupe_stamp (o); | |
4990 | } | |
4991 | ||
64211010 | 4992 | |
7a053d3b | 4993 | void |
20c6c071 | 4994 | dwarf_derived_probe::register_statement_variants(match_node * root, |
27dc09b1 | 4995 | dwarf_builder * dw, |
42e38653 | 4996 | privilege_t privilege) |
bd2b1e68 | 4997 | { |
27dc09b1 | 4998 | root |
42e38653 | 4999 | ->bind_privilege(privilege) |
27dc09b1 | 5000 | ->bind(dw); |
54efe513 GH |
5001 | } |
5002 | ||
7a053d3b | 5003 | void |
fd6602a0 | 5004 | dwarf_derived_probe::register_function_variants(match_node * root, |
27dc09b1 | 5005 | dwarf_builder * dw, |
42e38653 | 5006 | privilege_t privilege) |
2865d17a | 5007 | { |
27dc09b1 | 5008 | root |
42e38653 | 5009 | ->bind_privilege(privilege) |
27dc09b1 | 5010 | ->bind(dw); |
27dc09b1 | 5011 | root->bind(TOK_CALL) |
42e38653 | 5012 | ->bind_privilege(privilege) |
27dc09b1 | 5013 | ->bind(dw); |
4bda987e SC |
5014 | root->bind(TOK_EXPORTED) |
5015 | ->bind_privilege(privilege) | |
5016 | ->bind(dw); | |
27dc09b1 | 5017 | root->bind(TOK_RETURN) |
42e38653 | 5018 | ->bind_privilege(privilege) |
27dc09b1 | 5019 | ->bind(dw); |
1e035395 | 5020 | |
f6be7c06 DB |
5021 | // For process probes / uprobes, .maxactive() is unused. |
5022 | if (! pr_contains (privilege, pr_stapusr)) | |
1e035395 FCE |
5023 | { |
5024 | root->bind(TOK_RETURN) | |
1e035395 FCE |
5025 | ->bind_num(TOK_MAXACTIVE)->bind(dw); |
5026 | } | |
bd2b1e68 GH |
5027 | } |
5028 | ||
7a053d3b | 5029 | void |
27dc09b1 | 5030 | dwarf_derived_probe::register_function_and_statement_variants( |
440d9b00 | 5031 | systemtap_session& s, |
27dc09b1 DB |
5032 | match_node * root, |
5033 | dwarf_builder * dw, | |
42e38653 | 5034 | privilege_t privilege |
27dc09b1 | 5035 | ) |
bd2b1e68 GH |
5036 | { |
5037 | // Here we match 4 forms: | |
5038 | // | |
5039 | // .function("foo") | |
5040 | // .function(0xdeadbeef) | |
5041 | // .statement("foo") | |
5042 | // .statement(0xdeadbeef) | |
5043 | ||
440d9b00 | 5044 | match_node *fv_root = root->bind_str(TOK_FUNCTION); |
42e38653 | 5045 | register_function_variants(fv_root, dw, privilege); |
7f02ca94 | 5046 | // ROOT.function("STRING") always gets the .inline and .label variants. |
440d9b00 | 5047 | fv_root->bind(TOK_INLINE) |
42e38653 | 5048 | ->bind_privilege(privilege) |
440d9b00 | 5049 | ->bind(dw); |
7f02ca94 JS |
5050 | fv_root->bind_str(TOK_LABEL) |
5051 | ->bind_privilege(privilege) | |
440d9b00 | 5052 | ->bind(dw); |
c31add50 JL |
5053 | fv_root->bind_str(TOK_CALLEE) |
5054 | ->bind_privilege(privilege) | |
5055 | ->bind(dw); | |
5056 | fv_root->bind(TOK_CALLEES) | |
5057 | ->bind_privilege(privilege) | |
5058 | ->bind(dw); | |
5059 | fv_root->bind_num(TOK_CALLEES) | |
5060 | ->bind_privilege(privilege) | |
5061 | ->bind(dw); | |
440d9b00 DB |
5062 | |
5063 | fv_root = root->bind_num(TOK_FUNCTION); | |
42e38653 | 5064 | register_function_variants(fv_root, dw, privilege); |
440d9b00 DB |
5065 | // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter. |
5066 | if (strverscmp(s.compatible.c_str(), "1.7") <= 0) | |
5067 | { | |
5068 | fv_root->bind(TOK_INLINE) | |
42e38653 | 5069 | ->bind_privilege(privilege) |
440d9b00 DB |
5070 | ->bind(dw); |
5071 | } | |
5072 | ||
42e38653 DB |
5073 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege); |
5074 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege); | |
bd2b1e68 GH |
5075 | } |
5076 | ||
b1615c74 JS |
5077 | void |
5078 | dwarf_derived_probe::register_sdt_variants(systemtap_session& s, | |
5079 | match_node * root, | |
5080 | dwarf_builder * dw) | |
5081 | { | |
5082 | root->bind_str(TOK_MARK) | |
f66bb29a | 5083 | ->bind_privilege(pr_all) |
b1615c74 JS |
5084 | ->bind(dw); |
5085 | root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) | |
f66bb29a | 5086 | ->bind_privilege(pr_all) |
b1615c74 JS |
5087 | ->bind(dw); |
5088 | } | |
5089 | ||
5090 | void | |
5091 | dwarf_derived_probe::register_plt_variants(systemtap_session& s, | |
5092 | match_node * root, | |
5093 | dwarf_builder * dw) | |
5094 | { | |
5095 | root->bind(TOK_PLT) | |
f66bb29a | 5096 | ->bind_privilege(pr_all) |
b1615c74 JS |
5097 | ->bind(dw); |
5098 | root->bind_str(TOK_PLT) | |
f66bb29a | 5099 | ->bind_privilege(pr_all) |
b1615c74 JS |
5100 | ->bind(dw); |
5101 | root->bind(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 5102 | ->bind_privilege(pr_all) |
b1615c74 JS |
5103 | ->bind(dw); |
5104 | root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 5105 | ->bind_privilege(pr_all) |
b1615c74 | 5106 | ->bind(dw); |
bd2b1e68 GH |
5107 | } |
5108 | ||
5109 | void | |
c4ce66a1 | 5110 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 5111 | { |
c4ce66a1 | 5112 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
5113 | dwarf_builder *dw = new dwarf_builder(); |
5114 | ||
c4ce66a1 JS |
5115 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
5116 | s.code_filters.push_back(filter); | |
5117 | ||
bd1fcbad YZ |
5118 | filter = new dwarf_atvar_expanding_visitor(s, *dw); |
5119 | s.code_filters.push_back(filter); | |
5120 | ||
73f52eb4 DB |
5121 | register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged); |
5122 | register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged); | |
27dc09b1 DB |
5123 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) |
5124 | ->bind(dw); | |
2cab6244 | 5125 | |
7f02ca94 JS |
5126 | match_node* uprobes[] = { |
5127 | root->bind(TOK_PROCESS), | |
5128 | root->bind_str(TOK_PROCESS), | |
5129 | root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
5130 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
5131 | }; | |
5132 | for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i) | |
5133 | { | |
f66bb29a | 5134 | register_function_and_statement_variants(s, uprobes[i], dw, pr_all); |
7f02ca94 JS |
5135 | register_sdt_variants(s, uprobes[i], dw); |
5136 | register_plt_variants(s, uprobes[i], dw); | |
5137 | } | |
bd2b1e68 GH |
5138 | } |
5139 | ||
9020300d | 5140 | void |
3689db05 SC |
5141 | dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o) |
5142 | { | |
698de6cc | 5143 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
5144 | for (pcii = perf_counter_refs.begin(); |
5145 | pcii != perf_counter_refs.end(); | |
5146 | pcii++) | |
5147 | { | |
5148 | map<string, pair<string,derived_probe*> >::iterator it; | |
5149 | // Find the associated perf.counter probe | |
5150 | unsigned i = 0; | |
5151 | for (it=s.perf_counters.begin() ; | |
5152 | it != s.perf_counters.end(); | |
5153 | it++, i++) | |
5154 | if ((*it).second.second == (*pcii)) | |
5155 | break; | |
5156 | // place the perf counter read so it precedes stp_lock_probe | |
5157 | o->newline() << "l->l___perf_read_" + (*it).first | |
5158 | + " = (((int64_t) (_stp_perf_read(smp_processor_id()," | |
5159 | + lex_cast(i) + "))));"; | |
5160 | } | |
b78a0fbb | 5161 | |
b95e2b79 MH |
5162 | if (access_vars) |
5163 | { | |
5164 | // if accessing $variables, emit bsp cache setup for speeding up | |
d4670309 | 5165 | o->newline() << "#if defined __ia64__"; |
d9aed31e | 5166 | o->newline() << "bspcache(c->unwaddr, c->kregs);"; |
d4670309 | 5167 | o->newline() << "#endif"; |
b95e2b79 | 5168 | } |
9020300d | 5169 | } |
2930abc7 | 5170 | |
b20febf3 | 5171 | // ------------------------------------------------------------------------ |
46b84a80 DS |
5172 | |
5173 | void | |
b20febf3 | 5174 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 5175 | { |
b20febf3 | 5176 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
5177 | |
5178 | // XXX: probes put at the same address should all share a | |
5179 | // single kprobe/kretprobe, and have their handlers executed | |
5180 | // sequentially. | |
b55bc428 FCE |
5181 | } |
5182 | ||
7a053d3b | 5183 | void |
775d51e5 | 5184 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 5185 | { |
b20febf3 | 5186 | if (probes_by_module.empty()) return; |
2930abc7 | 5187 | |
775d51e5 DS |
5188 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
5189 | ||
5190 | // Warn of misconfigured kernels | |
f41595cc FCE |
5191 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
5192 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
5193 | s.op->newline() << "#endif"; | |
775d51e5 | 5194 | s.op->newline(); |
f41595cc | 5195 | |
f07c3b68 | 5196 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 5197 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
5198 | s.op->newline() << "#endif"; |
5199 | ||
14cf7e42 | 5200 | // Forward decls |
2ba1736a | 5201 | s.op->newline() << "#include \"linux/kprobes-common.h\""; |
14cf7e42 | 5202 | |
b20febf3 FCE |
5203 | // Forward declare the master entry functions |
5204 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5205 | s.op->line() << " struct pt_regs *regs);"; | |
5206 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5207 | s.op->line() << " struct pt_regs *regs);"; | |
5208 | ||
42cb22bd MH |
5209 | // Emit an array of kprobe/kretprobe pointers |
5210 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5211 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
5212 | s.op->newline() << "#endif"; | |
5213 | ||
b20febf3 | 5214 | // Emit the actual probe list. |
606fd9c8 FCE |
5215 | |
5216 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
5217 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
5218 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
14cf7e42 | 5219 | s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
606fd9c8 FCE |
5220 | // NB: bss! |
5221 | ||
4c2732a1 | 5222 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
5223 | s.op->newline(1) << "const unsigned return_p:1;"; |
5224 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 5225 | s.op->newline() << "const unsigned optional_p:1;"; |
b20febf3 | 5226 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 5227 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 | 5228 | |
af234c40 JS |
5229 | // data saved in the kretprobe_instance packet |
5230 | s.op->newline() << "const unsigned short saved_longs;"; | |
5231 | s.op->newline() << "const unsigned short saved_strings;"; | |
5232 | ||
faea5e16 | 5233 | // Let's find some stats for the embedded strings. Maybe they |
606fd9c8 FCE |
5234 | // are small and uniform enough to justify putting char[MAX]'s into |
5235 | // the array instead of relocated char*'s. | |
faea5e16 JS |
5236 | size_t module_name_max = 0, section_name_max = 0; |
5237 | size_t module_name_tot = 0, section_name_tot = 0; | |
606fd9c8 FCE |
5238 | size_t all_name_cnt = probes_by_module.size(); // for average |
5239 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
5240 | { | |
5241 | dwarf_derived_probe* p = it->second; | |
5242 | #define DOIT(var,expr) do { \ | |
5243 | size_t var##_size = (expr) + 1; \ | |
5244 | var##_max = max (var##_max, var##_size); \ | |
5245 | var##_tot += var##_size; } while (0) | |
5246 | DOIT(module_name, p->module.size()); | |
5247 | DOIT(section_name, p->section.size()); | |
606fd9c8 FCE |
5248 | #undef DOIT |
5249 | } | |
5250 | ||
5251 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
5252 | // the amount of average waste (max - avg) to the relocation data size | |
5253 | // (3 native long words). | |
5254 | #define CALCIT(var) \ | |
5255 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
5256 | { \ | |
5257 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
5258 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
5259 | << "[" << var##_name_max << "]" << endl; \ | |
5260 | } \ | |
5261 | else \ | |
5262 | { \ | |
b0986e7a | 5263 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
5264 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
5265 | } | |
5266 | ||
5267 | CALCIT(module); | |
5268 | CALCIT(section); | |
e6fe60e7 | 5269 | #undef CALCIT |
606fd9c8 | 5270 | |
b0986e7a | 5271 | s.op->newline() << "const unsigned long address;"; |
7c3e97f4 JS |
5272 | s.op->newline() << "const struct stap_probe * const probe;"; |
5273 | s.op->newline() << "const struct stap_probe * const entry_probe;"; | |
b20febf3 FCE |
5274 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
5275 | s.op->indent(1); | |
5276 | ||
5277 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 5278 | { |
b20febf3 FCE |
5279 | dwarf_derived_probe* p = it->second; |
5280 | s.op->newline() << "{"; | |
5281 | if (p->has_return) | |
5282 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 5283 | if (p->has_maxactive) |
606fd9c8 FCE |
5284 | { |
5285 | s.op->line() << " .maxactive_p=1,"; | |
5286 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
5287 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
5288 | } | |
af234c40 JS |
5289 | if (p->saved_longs || p->saved_strings) |
5290 | { | |
5291 | if (p->saved_longs) | |
5292 | s.op->line() << " .saved_longs=" << p->saved_longs << ","; | |
5293 | if (p->saved_strings) | |
5294 | s.op->line() << " .saved_strings=" << p->saved_strings << ","; | |
5295 | if (p->entry_handler) | |
c87ae2c1 | 5296 | s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ","; |
af234c40 | 5297 | } |
b350f56b JS |
5298 | if (p->locations[0]->optional) |
5299 | s.op->line() << " .optional_p=1,"; | |
dc38c256 | 5300 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
5301 | s.op->line() << " .module=\"" << p->module << "\","; |
5302 | s.op->line() << " .section=\"" << p->section << "\","; | |
faea5e16 | 5303 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
b20febf3 | 5304 | s.op->line() << " },"; |
2930abc7 | 5305 | } |
2930abc7 | 5306 | |
b20febf3 FCE |
5307 | s.op->newline(-1) << "};"; |
5308 | ||
5309 | // Emit the kprobes callback function | |
5310 | s.op->newline(); | |
5311 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5312 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
5313 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
5314 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
5315 | // Check that the index is plausible | |
5316 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5317 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5318 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5319 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5320 | s.op->line() << "];"; | |
71db462b | 5321 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 5322 | "stp_probe_type_kprobe"); |
d9aed31e | 5323 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
5324 | |
5325 | // Make it look like the IP is set as it wouldn't have been replaced | |
5326 | // by a breakpoint instruction when calling real probe handler. Reset | |
5327 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
5328 | s.op->newline() << "{"; | |
5329 | s.op->indent(1); | |
d9aed31e | 5330 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 5331 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 5332 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 5333 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
5334 | s.op->newline(-1) << "}"; |
5335 | ||
f887a8c9 | 5336 | common_probe_entryfn_epilogue (s, true); |
b20febf3 FCE |
5337 | s.op->newline() << "return 0;"; |
5338 | s.op->newline(-1) << "}"; | |
5339 | ||
5340 | // Same for kretprobes | |
5341 | s.op->newline(); | |
af234c40 JS |
5342 | s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,"; |
5343 | s.op->line() << " struct pt_regs *regs, int entry) {"; | |
b20febf3 | 5344 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; |
606fd9c8 FCE |
5345 | |
5346 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 5347 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
5348 | // Check that the index is plausible |
5349 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5350 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5351 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5352 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5353 | s.op->line() << "];"; | |
5354 | ||
7c3e97f4 | 5355 | s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;"; |
c87ae2c1 JS |
5356 | s.op->newline() << "if (sp) {"; |
5357 | s.op->indent(1); | |
71db462b | 5358 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp", |
cda141c2 | 5359 | "stp_probe_type_kretprobe"); |
d9aed31e | 5360 | s.op->newline() << "c->kregs = regs;"; |
af234c40 JS |
5361 | |
5362 | // for assisting runtime's backtrace logic and accessing kretprobe data packets | |
6dceb5c9 MW |
5363 | s.op->newline() << "c->ips.krp.pi = inst;"; |
5364 | s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;"; | |
6415ddde MW |
5365 | |
5366 | // Make it look like the IP is set as it wouldn't have been replaced | |
5367 | // by a breakpoint instruction when calling real probe handler. Reset | |
5368 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
5369 | s.op->newline() << "{"; | |
d9aed31e | 5370 | s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
c87ae2c1 JS |
5371 | s.op->newline() << "if (entry)"; |
5372 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; | |
5373 | s.op->newline(-1) << "else"; | |
5374 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);"; | |
5375 | s.op->newline(-1) << "(sp->ph) (c);"; | |
259d54c0 | 5376 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
5377 | s.op->newline(-1) << "}"; |
5378 | ||
f887a8c9 | 5379 | common_probe_entryfn_epilogue (s, true); |
c87ae2c1 | 5380 | s.op->newline(-1) << "}"; |
b20febf3 FCE |
5381 | s.op->newline() << "return 0;"; |
5382 | s.op->newline(-1) << "}"; | |
af234c40 JS |
5383 | |
5384 | s.op->newline(); | |
5385 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5386 | s.op->line() << " struct pt_regs *regs) {"; | |
5387 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);"; | |
5388 | s.op->newline(-1) << "}"; | |
5389 | ||
5390 | s.op->newline(); | |
5391 | s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,"; | |
5392 | s.op->line() << " struct pt_regs *regs) {"; | |
5393 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);"; | |
5394 | s.op->newline(-1) << "}"; | |
b642c901 | 5395 | |
14cf7e42 | 5396 | s.op->newline(); |
20c6c071 | 5397 | } |
ec4373ff | 5398 | |
20c6c071 | 5399 | |
dc38c0ae | 5400 | void |
b20febf3 FCE |
5401 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
5402 | { | |
5403 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5404 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5405 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
a049e342 | 5406 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 5407 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
26e63673 | 5408 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b20febf3 | 5409 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 5410 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 5411 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 5412 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 5413 | s.op->newline(-1) << "} else {"; |
f07c3b68 | 5414 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
c9bad430 | 5415 | s.op->newline(-1) << "}"; |
606fd9c8 | 5416 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
af234c40 | 5417 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; |
c87ae2c1 | 5418 | s.op->newline() << "if (sdp->entry_probe) {"; |
af234c40 JS |
5419 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; |
5420 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
5421 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
5422 | s.op->newline(-1) << "}"; | |
5423 | s.op->newline() << "#endif"; | |
e4cb375f MH |
5424 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
5425 | s.op->newline() << "#ifdef __ia64__"; | |
5426 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5427 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5428 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5429 | s.op->newline() << "if (rc == 0) {"; | |
5430 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5431 | s.op->newline() << "if (rc != 0)"; | |
5432 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5433 | s.op->newline(-2) << "}"; | |
5434 | s.op->newline() << "#else"; | |
606fd9c8 | 5435 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 5436 | s.op->newline() << "#endif"; |
b20febf3 | 5437 | s.op->newline(-1) << "} else {"; |
e4cb375f | 5438 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
5439 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
5440 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
5441 | s.op->newline() << "#ifdef __ia64__"; |
5442 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5443 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5444 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5445 | s.op->newline() << "if (rc == 0) {"; | |
5446 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5447 | s.op->newline() << "if (rc != 0)"; | |
5448 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5449 | s.op->newline(-2) << "}"; | |
5450 | s.op->newline() << "#else"; | |
606fd9c8 | 5451 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 5452 | s.op->newline() << "#endif"; |
b20febf3 | 5453 | s.op->newline(-1) << "}"; |
9063462a FCE |
5454 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
5455 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 5456 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 5457 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);"; |
b350f56b | 5458 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
9063462a FCE |
5459 | // XXX: shall we increment numskipped? |
5460 | s.op->newline(-1) << "}"; | |
5461 | ||
5462 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 5463 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 5464 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
5465 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
5466 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
5467 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
5468 | s.op->newline() << "#ifdef __ia64__"; |
5469 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
5470 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
5471 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
5472 | // not run for this early-abort case. | |
5473 | s.op->newline(-1) << "}"; | |
5474 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 5475 | s.op->newline(-1) << "}"; |
9063462a FCE |
5476 | #endif |
5477 | ||
b20febf3 FCE |
5478 | s.op->newline() << "else sdp->registered_p = 1;"; |
5479 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
5480 | } |
5481 | ||
5482 | ||
b4be7cbc FCE |
5483 | void |
5484 | dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s) | |
5485 | { | |
5486 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5487 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5488 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5489 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; | |
5490 | s.op->newline() << "int rc;"; | |
5491 | ||
5492 | // new module arrived? | |
5493 | s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {"; | |
5494 | s.op->newline(1) << "if (sdp->return_p) {"; | |
5495 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; | |
5496 | s.op->newline() << "if (sdp->maxactive_p) {"; | |
5497 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
5498 | s.op->newline(-1) << "} else {"; | |
5499 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; | |
5500 | s.op->newline(-1) << "}"; | |
5501 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; | |
5502 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; | |
5503 | s.op->newline() << "if (sdp->entry_probe) {"; | |
5504 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; | |
5505 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
5506 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
5507 | s.op->newline(-1) << "}"; | |
5508 | s.op->newline() << "#endif"; | |
5509 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
5510 | s.op->newline() << "#ifdef __ia64__"; | |
5511 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5512 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5513 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5514 | s.op->newline() << "if (rc == 0) {"; | |
5515 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5516 | s.op->newline() << "if (rc != 0)"; | |
5517 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5518 | s.op->newline(-2) << "}"; | |
5519 | s.op->newline() << "#else"; | |
5520 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
5521 | s.op->newline() << "#endif"; | |
5522 | s.op->newline(-1) << "} else {"; | |
5523 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
5524 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; | |
5525 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
5526 | s.op->newline() << "#ifdef __ia64__"; | |
5527 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5528 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5529 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5530 | s.op->newline() << "if (rc == 0) {"; | |
5531 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5532 | s.op->newline() << "if (rc != 0)"; | |
5533 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5534 | s.op->newline(-2) << "}"; | |
5535 | s.op->newline() << "#else"; | |
5536 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
5537 | s.op->newline() << "#endif"; | |
5538 | s.op->newline(-1) << "}"; | |
5539 | s.op->newline() << "if (rc == 0) sdp->registered_p = 1;"; | |
5540 | ||
5541 | // old module disappeared? | |
5542 | s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {"; | |
5543 | s.op->newline(1) << "if (sdp->return_p) {"; | |
5544 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
065d5567 | 5545 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5546 | s.op->newline() << "#ifdef STP_TIMING"; |
5547 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
5548 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; | |
5549 | s.op->newline(-1) << "#endif"; | |
065d5567 | 5550 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5551 | s.op->newline() << "#ifdef STP_TIMING"; |
5552 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
5553 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; | |
5554 | s.op->newline(-1) << "#endif"; | |
5555 | s.op->newline(-1) << "} else {"; | |
5556 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
065d5567 | 5557 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5558 | s.op->newline() << "#ifdef STP_TIMING"; |
5559 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
5560 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; | |
5561 | s.op->newline(-1) << "#endif"; | |
5562 | s.op->newline(-1) << "}"; | |
5563 | s.op->newline() << "#if defined(__ia64__)"; | |
5564 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5565 | s.op->newline() << "#endif"; | |
5566 | s.op->newline() << "sdp->registered_p = 0;"; | |
5567 | s.op->newline(-1) << "}"; | |
5568 | ||
5569 | s.op->newline(-1) << "}"; // for loop | |
5570 | } | |
5571 | ||
5572 | ||
5573 | ||
5574 | ||
46b84a80 | 5575 | void |
b20febf3 | 5576 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 5577 | { |
42cb22bd MH |
5578 | //Unregister kprobes by batch interfaces. |
5579 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5580 | s.op->newline() << "j = 0;"; | |
5581 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5582 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5583 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5584 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5585 | s.op->newline() << "if (!sdp->return_p)"; | |
5586 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
5587 | s.op->newline(-2) << "}"; | |
5588 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5589 | s.op->newline() << "j = 0;"; | |
5590 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5591 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5592 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5593 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5594 | s.op->newline() << "if (sdp->return_p)"; | |
5595 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
5596 | s.op->newline(-2) << "}"; | |
5597 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
5598 | s.op->newline() << "#ifdef __ia64__"; |
5599 | s.op->newline() << "j = 0;"; | |
5600 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5601 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5602 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5603 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5604 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
5605 | s.op->newline(-1) << "}"; | |
5606 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5607 | s.op->newline() << "#endif"; | |
42cb22bd MH |
5608 | s.op->newline() << "#endif"; |
5609 | ||
b20febf3 FCE |
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];"; | |
a36378d7 | 5612 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
5613 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
5614 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 5615 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5616 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 5617 | s.op->newline() << "#endif"; |
065d5567 | 5618 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
73209876 FCE |
5619 | s.op->newline() << "#ifdef STP_TIMING"; |
5620 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 5621 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
73209876 | 5622 | s.op->newline(-1) << "#endif"; |
065d5567 | 5623 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
73209876 FCE |
5624 | s.op->newline() << "#ifdef STP_TIMING"; |
5625 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 5626 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; |
73209876 | 5627 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 5628 | s.op->newline(-1) << "} else {"; |
42cb22bd | 5629 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5630 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 5631 | s.op->newline() << "#endif"; |
065d5567 | 5632 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
73209876 FCE |
5633 | s.op->newline() << "#ifdef STP_TIMING"; |
5634 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 5635 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
73209876 | 5636 | s.op->newline(-1) << "#endif"; |
b20febf3 | 5637 | s.op->newline(-1) << "}"; |
e4cb375f MH |
5638 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
5639 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5640 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
5641 | s.op->newline() << "sdp->registered_p = 0;"; |
5642 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
5643 | } |
5644 | ||
272c9036 WF |
5645 | static void sdt_v3_tokenize(const string& str, vector<string>& tokens) |
5646 | { | |
5647 | string::size_type pos; | |
5648 | string::size_type lastPos = str.find_first_not_of(" ", 0); | |
5649 | string::size_type nextAt = str.find("@", lastPos); | |
e9a90eee JS |
5650 | |
5651 | if (nextAt == string::npos) | |
5652 | { | |
5653 | // PR13934: Assembly probes are not forced to use the N@OP form. | |
5654 | // In this case, N is inferred to be the native word size. Since we | |
5655 | // don't have a nice delimiter, just split it on spaces. SDT-asm authors | |
5656 | // then must not put any spaces in arguments, to avoid ambiguity. | |
5657 | tokenize(str, tokens, " "); | |
5658 | return; | |
5659 | } | |
5660 | ||
272c9036 WF |
5661 | while (lastPos != string::npos) |
5662 | { | |
5663 | pos = nextAt + 1; | |
5664 | nextAt = str.find("@", pos); | |
5665 | if (nextAt == string::npos) | |
5666 | pos = string::npos; | |
5667 | else | |
5668 | pos = str.rfind(" ", nextAt); | |
5669 | ||
5670 | tokens.push_back(str.substr(lastPos, pos - lastPos)); | |
5671 | lastPos = str.find_first_not_of(" ", pos); | |
5672 | } | |
5673 | } | |
8aabf152 | 5674 | |
8aabf152 | 5675 | |
aff5d390 | 5676 | struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor |
7a05f484 | 5677 | { |
ae1418f0 | 5678 | enum regwidths {QI, QIh, HI, SI, DI}; |
f83336a5 FCE |
5679 | sdt_uprobe_var_expanding_visitor(systemtap_session& s, |
5680 | int elf_machine, | |
5681 | const string & process_name, | |
a794dbeb | 5682 | const string & provider_name, |
aff5d390 | 5683 | const string & probe_name, |
71e5e13d | 5684 | stap_sdt_probe_type probe_type, |
aff5d390 | 5685 | const string & arg_string, |
8aabf152 | 5686 | int ac): |
332ba7e7 | 5687 | session (s), elf_machine (elf_machine), process_name (process_name), |
71e5e13d SC |
5688 | provider_name (provider_name), probe_name (probe_name), |
5689 | probe_type (probe_type), arg_count ((unsigned) ac) | |
a8ec7719 | 5690 | { |
f83336a5 FCE |
5691 | /* Register name mapping table depends on the elf machine of this particular |
5692 | probe target process/file, not upon the host. So we can't just | |
5693 | #ifdef _i686_ etc. */ | |
ae1418f0 FCE |
5694 | |
5695 | #define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width) | |
f83336a5 | 5696 | if (elf_machine == EM_X86_64) { |
b78a0fbb LB |
5697 | DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); |
5698 | DRI ("%al", 0, QI); DRI ("%ah", 0, QIh); | |
46a94997 SC |
5699 | DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI); |
5700 | DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh); | |
5701 | DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI); | |
5702 | DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh); | |
5703 | DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); | |
b78a0fbb LB |
5704 | DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh); |
5705 | DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI); | |
46a94997 SC |
5706 | DRI ("%sil", 4, QI); |
5707 | DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI); | |
5708 | DRI ("%dil", 5, QI); | |
5709 | DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI); | |
2c7281e7 | 5710 | DRI ("%bpl", 6, QI); |
46a94997 | 5711 | DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI); |
2c7281e7 | 5712 | DRI ("%spl", 7, QI); |
46a94997 SC |
5713 | DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI); |
5714 | DRI ("%r8b", 8, QI); | |
5715 | DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI); | |
5716 | DRI ("%r9b", 9, QI); | |
5717 | DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI); | |
5718 | DRI ("%r10b", 10, QI); | |
5719 | DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI); | |
5720 | DRI ("%r11b", 11, QI); | |
5721 | DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI); | |
5722 | DRI ("%r12b", 12, QI); | |
5723 | DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI); | |
5724 | DRI ("%r13b", 13, QI); | |
5725 | DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI); | |
5726 | DRI ("%r14b", 14, QI); | |
5727 | DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI); | |
5728 | DRI ("%r15b", 15, QI); | |
f83336a5 | 5729 | } else if (elf_machine == EM_386) { |
46a94997 SC |
5730 | DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI); |
5731 | DRI ("%ah", 0, QIh); | |
5732 | DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI); | |
5733 | DRI ("%ch", 1, QIh); | |
5734 | DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI); | |
5735 | DRI ("%dh", 2, QIh); | |
5736 | DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI); | |
5737 | DRI ("%bh", 3, QIh); | |
b78a0fbb | 5738 | DRI ("%esp", 4, SI); DRI ("%sp", 4, HI); |
46a94997 SC |
5739 | DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI); |
5740 | DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI); | |
5741 | DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI); | |
0491c523 | 5742 | } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) { |
46a94997 SC |
5743 | DRI ("%r0", 0, DI); |
5744 | DRI ("%r1", 1, DI); | |
5745 | DRI ("%r2", 2, DI); | |
5746 | DRI ("%r3", 3, DI); | |
5747 | DRI ("%r4", 4, DI); | |
5748 | DRI ("%r5", 5, DI); | |
5749 | DRI ("%r6", 6, DI); | |
5750 | DRI ("%r7", 7, DI); | |
5751 | DRI ("%r8", 8, DI); | |
5752 | DRI ("%r9", 9, DI); | |
5753 | DRI ("%r10", 10, DI); | |
5754 | DRI ("%r11", 11, DI); | |
5755 | DRI ("%r12", 12, DI); | |
5756 | DRI ("%r13", 13, DI); | |
5757 | DRI ("%r14", 14, DI); | |
5758 | DRI ("%r15", 15, DI); | |
5759 | DRI ("%r16", 16, DI); | |
5760 | DRI ("%r17", 17, DI); | |
5761 | DRI ("%r18", 18, DI); | |
5762 | DRI ("%r19", 19, DI); | |
5763 | DRI ("%r20", 20, DI); | |
5764 | DRI ("%r21", 21, DI); | |
5765 | DRI ("%r22", 22, DI); | |
5766 | DRI ("%r23", 23, DI); | |
5767 | DRI ("%r24", 24, DI); | |
5768 | DRI ("%r25", 25, DI); | |
5769 | DRI ("%r26", 26, DI); | |
5770 | DRI ("%r27", 27, DI); | |
5771 | DRI ("%r28", 28, DI); | |
5772 | DRI ("%r29", 29, DI); | |
5773 | DRI ("%r30", 30, DI); | |
5774 | DRI ("%r31", 31, DI); | |
8aabf152 | 5775 | // PR11821: unadorned register "names" without -mregnames |
46a94997 SC |
5776 | DRI ("0", 0, DI); |
5777 | DRI ("1", 1, DI); | |
5778 | DRI ("2", 2, DI); | |
5779 | DRI ("3", 3, DI); | |
5780 | DRI ("4", 4, DI); | |
5781 | DRI ("5", 5, DI); | |
5782 | DRI ("6", 6, DI); | |
5783 | DRI ("7", 7, DI); | |
5784 | DRI ("8", 8, DI); | |
5785 | DRI ("9", 9, DI); | |
5786 | DRI ("10", 10, DI); | |
5787 | DRI ("11", 11, DI); | |
5788 | DRI ("12", 12, DI); | |
5789 | DRI ("13", 13, DI); | |
5790 | DRI ("14", 14, DI); | |
5791 | DRI ("15", 15, DI); | |
5792 | DRI ("16", 16, DI); | |
5793 | DRI ("17", 17, DI); | |
5794 | DRI ("18", 18, DI); | |
5795 | DRI ("19", 19, DI); | |
5796 | DRI ("20", 20, DI); | |
5797 | DRI ("21", 21, DI); | |
5798 | DRI ("22", 22, DI); | |
5799 | DRI ("23", 23, DI); | |
5800 | DRI ("24", 24, DI); | |
5801 | DRI ("25", 25, DI); | |
5802 | DRI ("26", 26, DI); | |
5803 | DRI ("27", 27, DI); | |
5804 | DRI ("28", 28, DI); | |
5805 | DRI ("29", 29, DI); | |
5806 | DRI ("30", 30, DI); | |
5807 | DRI ("31", 31, DI); | |
14900130 | 5808 | } else if (elf_machine == EM_S390) { |
46a94997 SC |
5809 | DRI ("%r0", 0, DI); |
5810 | DRI ("%r1", 1, DI); | |
5811 | DRI ("%r2", 2, DI); | |
5812 | DRI ("%r3", 3, DI); | |
5813 | DRI ("%r4", 4, DI); | |
5814 | DRI ("%r5", 5, DI); | |
5815 | DRI ("%r6", 6, DI); | |
5816 | DRI ("%r7", 7, DI); | |
5817 | DRI ("%r8", 8, DI); | |
5818 | DRI ("%r9", 9, DI); | |
5819 | DRI ("%r10", 10, DI); | |
5820 | DRI ("%r11", 11, DI); | |
5821 | DRI ("%r12", 12, DI); | |
5822 | DRI ("%r13", 13, DI); | |
5823 | DRI ("%r14", 14, DI); | |
5824 | DRI ("%r15", 15, DI); | |
272c9036 WF |
5825 | } else if (elf_machine == EM_ARM) { |
5826 | DRI ("r0", 0, SI); | |
5827 | DRI ("r1", 1, SI); | |
5828 | DRI ("r2", 2, SI); | |
5829 | DRI ("r3", 3, SI); | |
5830 | DRI ("r4", 4, SI); | |
5831 | DRI ("r5", 5, SI); | |
5832 | DRI ("r6", 6, SI); | |
5833 | DRI ("r7", 7, SI); | |
5834 | DRI ("r8", 8, SI); | |
5835 | DRI ("r9", 9, SI); | |
669a2feb | 5836 | DRI ("r10", 10, SI); DRI ("sl", 10, SI); |
272c9036 WF |
5837 | DRI ("fp", 11, SI); |
5838 | DRI ("ip", 12, SI); | |
5839 | DRI ("sp", 13, SI); | |
5840 | DRI ("lr", 14, SI); | |
5841 | DRI ("pc", 15, SI); | |
86aae990 WC |
5842 | } else if (elf_machine == EM_AARCH64) { |
5843 | DRI ("x0", 0, DI); DRI ("w0", 0, SI); | |
5844 | DRI ("x1", 1, DI); DRI ("w1", 1, SI); | |
5845 | DRI ("x2", 2, DI); DRI ("w2", 2, SI); | |
5846 | DRI ("x3", 3, DI); DRI ("w3", 3, SI); | |
5847 | DRI ("x4", 4, DI); DRI ("w4", 4, SI); | |
5848 | DRI ("x5", 5, DI); DRI ("w5", 5, SI); | |
5849 | DRI ("x6", 6, DI); DRI ("w6", 6, SI); | |
5850 | DRI ("x7", 7, DI); DRI ("w7", 7, SI); | |
5851 | DRI ("x8", 8, DI); DRI ("w8", 8, SI); | |
5852 | DRI ("x9", 9, DI); DRI ("w9", 9, SI); | |
5853 | DRI ("x10", 10, DI); DRI ("w10", 10, SI); | |
5854 | DRI ("x11", 11, DI); DRI ("w11", 11, SI); | |
5855 | DRI ("x12", 12, DI); DRI ("w12", 12, SI); | |
5856 | DRI ("x13", 13, DI); DRI ("w13", 13, SI); | |
5857 | DRI ("x14", 14, DI); DRI ("w14", 14, SI); | |
5858 | DRI ("x15", 15, DI); DRI ("w15", 15, SI); | |
5859 | DRI ("x16", 16, DI); DRI ("w16", 16, SI); | |
5860 | DRI ("x17", 17, DI); DRI ("w17", 17, SI); | |
5861 | DRI ("x18", 18, DI); DRI ("w18", 18, SI); | |
5862 | DRI ("x19", 19, DI); DRI ("w19", 19, SI); | |
5863 | DRI ("x20", 20, DI); DRI ("w20", 20, SI); | |
5864 | DRI ("x21", 21, DI); DRI ("w21", 21, SI); | |
5865 | DRI ("x22", 22, DI); DRI ("w22", 22, SI); | |
5866 | DRI ("x23", 23, DI); DRI ("w23", 23, SI); | |
5867 | DRI ("x24", 24, DI); DRI ("w24", 24, SI); | |
5868 | DRI ("x25", 25, DI); DRI ("w25", 25, SI); | |
5869 | DRI ("x26", 26, DI); DRI ("w26", 26, SI); | |
5870 | DRI ("x27", 27, DI); DRI ("w27", 27, SI); | |
5871 | DRI ("x28", 28, DI); DRI ("w28", 28, SI); | |
5872 | DRI ("x29", 29, DI); DRI ("w29", 29, SI); | |
5873 | DRI ("x30", 30, DI); DRI ("w30", 30, SI); | |
5874 | DRI ("sp", 31, DI); | |
14900130 | 5875 | } else if (arg_count) { |
8aabf152 | 5876 | /* permit this case; just fall back to dwarf */ |
f83336a5 | 5877 | } |
ae1418f0 | 5878 | #undef DRI |
f83336a5 | 5879 | |
ebbd2b45 | 5880 | need_debug_info = false; |
88e39987 | 5881 | if (probe_type == uprobe3_type) |
272c9036 WF |
5882 | { |
5883 | sdt_v3_tokenize(arg_string, arg_tokens); | |
5884 | assert(arg_count <= 12); | |
5885 | } | |
88e39987 | 5886 | else |
272c9036 WF |
5887 | { |
5888 | tokenize(arg_string, arg_tokens, " "); | |
5889 | assert(arg_count <= 10); | |
5890 | } | |
a8ec7719 | 5891 | } |
8aabf152 | 5892 | |
f83336a5 | 5893 | systemtap_session& session; |
332ba7e7 | 5894 | int elf_machine; |
aff5d390 | 5895 | const string & process_name; |
a794dbeb | 5896 | const string & provider_name; |
aff5d390 | 5897 | const string & probe_name; |
71e5e13d | 5898 | stap_sdt_probe_type probe_type; |
8aabf152 | 5899 | unsigned arg_count; |
aff5d390 | 5900 | vector<string> arg_tokens; |
46a94997 | 5901 | map<string, pair<unsigned,int> > dwarf_regs; |
ebbd2b45 | 5902 | bool need_debug_info; |
aff5d390 SC |
5903 | |
5904 | void visit_target_symbol (target_symbol* e); | |
6ef331c8 SC |
5905 | void visit_target_symbol_arg (target_symbol* e); |
5906 | void visit_target_symbol_context (target_symbol* e); | |
bd1fcbad | 5907 | void visit_atvar_op (atvar_op* e); |
40a0c64e | 5908 | void visit_cast_op (cast_op* e); |
aff5d390 SC |
5909 | }; |
5910 | ||
7a05f484 SC |
5911 | |
5912 | void | |
6ef331c8 | 5913 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
7a05f484 | 5914 | { |
6ef331c8 | 5915 | if (e->addressof) |
dc09353a | 5916 | throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok); |
6ef331c8 SC |
5917 | |
5918 | if (e->name == "$$name") | |
7a05f484 | 5919 | { |
6ef331c8 SC |
5920 | literal_string *myname = new literal_string (probe_name); |
5921 | myname->tok = e->tok; | |
5922 | provide(myname); | |
5923 | return; | |
5924 | } | |
7a05f484 | 5925 | |
6ef331c8 SC |
5926 | else if (e->name == "$$provider") |
5927 | { | |
5928 | literal_string *myname = new literal_string (provider_name); | |
5929 | myname->tok = e->tok; | |
5930 | provide(myname); | |
5931 | return; | |
5932 | } | |
a794dbeb | 5933 | |
6ef331c8 SC |
5934 | else if (e->name == "$$vars" || e->name == "$$parms") |
5935 | { | |
5936 | e->assert_no_components("sdt", true); | |
6ef331c8 SC |
5937 | |
5938 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
63ea4244 | 5939 | |
1c922ad7 | 5940 | print_format* pf = print_format::create(e->tok, "sprintf"); |
6ef331c8 SC |
5941 | |
5942 | for (unsigned i = 1; i <= arg_count; ++i) | |
5943 | { | |
5944 | if (i > 1) | |
5945 | pf->raw_components += " "; | |
5946 | target_symbol *tsym = new target_symbol; | |
5947 | tsym->tok = e->tok; | |
5948 | tsym->name = "$arg" + lex_cast(i); | |
5949 | pf->raw_components += tsym->name; | |
5950 | tsym->components = e->components; | |
5951 | ||
8c2f50c0 | 5952 | expression *texp = require<expression> (tsym); |
6ef331c8 SC |
5953 | if (!e->components.empty() && |
5954 | e->components[0].type == target_symbol::comp_pretty_print) | |
5955 | pf->raw_components += "=%s"; | |
5956 | else | |
5957 | pf->raw_components += "=%#x"; | |
5958 | pf->args.push_back(texp); | |
5959 | } | |
5960 | ||
5961 | pf->components = print_format::string_to_components(pf->raw_components); | |
5962 | provide (pf); | |
5963 | } | |
5964 | else | |
5965 | assert(0); // shouldn't get here | |
5966 | } | |
5967 | ||
5968 | ||
5969 | void | |
5970 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e) | |
5971 | { | |
5972 | try | |
5973 | { | |
8aabf152 | 5974 | unsigned argno = 0; // the N in $argN |
c69a87e0 | 5975 | try |
aff5d390 | 5976 | { |
5ecaa5a7 | 5977 | if (startswith(e->name, "$arg")) |
8aabf152 | 5978 | argno = lex_cast<unsigned>(e->name.substr(4)); |
aff5d390 | 5979 | } |
c69a87e0 | 5980 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF |
aff5d390 | 5981 | { |
8aabf152 | 5982 | argno = 0; |
aff5d390 | 5983 | } |
5ecaa5a7 | 5984 | |
8aabf152 FCE |
5985 | if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count |
5986 | argno < 1 || argno > arg_count) // a $argN with out-of-range N | |
aff5d390 | 5987 | { |
8aabf152 FCE |
5988 | // NB: Either |
5989 | // 1) uprobe1_type $argN or $FOO (we don't know the arg_count) | |
5990 | // 2) uprobe2_type $FOO (no probe args) | |
5991 | // both of which get resolved later. | |
73d53dd2 | 5992 | // Throw it now, and it might be resolved by DWARF later. |
8aabf152 | 5993 | need_debug_info = true; |
dc09353a | 5994 | throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok); |
aff5d390 | 5995 | } |
277c21bc | 5996 | |
8aabf152 FCE |
5997 | assert (arg_tokens.size() >= argno); |
5998 | string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0] | |
c57ea854 | 5999 | |
8aabf152 FCE |
6000 | // Now we try to parse this thing, which is an assembler operand |
6001 | // expression. If we can't, we warn, back down to need_debug_info | |
b874bd52 | 6002 | // and hope for the best. Here is the syntax for a few architectures. |
9859b766 | 6003 | // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i. |
8095a157 FCE |
6004 | // |
6005 | // literal reg reg reg + base+index*size+offset | |
b874bd52 | 6006 | // indirect offset |
8095a157 | 6007 | // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) |
b874bd52 | 6008 | // power iN R (R) N(R) |
b78a0fbb | 6009 | // ia64 N rR [r16] |
b874bd52 SC |
6010 | // s390 N %rR 0(rR) N(r15) |
6011 | // arm #N rR [rR] [rR, #N] | |
6012 | ||
8aabf152 FCE |
6013 | expression* argexpr = 0; // filled in in case of successful parse |
6014 | ||
6015 | string percent_regnames; | |
6016 | string regnames; | |
6017 | vector<string> matches; | |
71e5e13d | 6018 | long precision; |
8aabf152 FCE |
6019 | int rc; |
6020 | ||
40fe32e0 SC |
6021 | // Parse the leading length |
6022 | ||
6023 | if (asmarg.find('@') != string::npos) | |
6024 | { | |
6025 | precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@'))); | |
6026 | asmarg = asmarg.substr(asmarg.find('@')+1); | |
6027 | } | |
71e5e13d SC |
6028 | else |
6029 | { | |
6030 | // V1/V2 do not have precision field so default to signed long | |
6031 | // V3 asm does not have precision field so default to unsigned long | |
6032 | if (probe_type == uprobe3_type) | |
6033 | precision = sizeof(long); // this is an asm probe | |
6034 | else | |
6035 | precision = -sizeof(long); | |
6036 | } | |
40fe32e0 | 6037 | |
8aabf152 FCE |
6038 | // test for a numeric literal. |
6039 | // Only accept (signed) decimals throughout. XXX | |
6040 | ||
6041 | // PR11821. NB: on powerpc, literals are not prefixed with $, | |
6042 | // so this regex does not match. But that's OK, since without | |
6043 | // -mregnames, we can't tell them apart from register numbers | |
6044 | // anyway. With -mregnames, we could, if gcc somehow | |
6045 | // communicated to us the presence of that option, but alas it | |
6046 | // doesn't. http://gcc.gnu.org/PR44995. | |
272c9036 | 6047 | rc = regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches); |
8aabf152 FCE |
6048 | if (! rc) |
6049 | { | |
75a371ce JS |
6050 | string sn = matches[0].substr(1); |
6051 | int64_t n; | |
6052 | try | |
6053 | { | |
6054 | // We have to pay attention to the size & sign, as gcc sometimes | |
6055 | // propagates constants that don't quite match, like a negative | |
6056 | // value to fill an unsigned type. | |
6057 | switch (precision) | |
6058 | { | |
6059 | case -1: n = lex_cast< int8_t>(sn); break; | |
6060 | case 1: n = lex_cast< uint8_t>(sn); break; | |
6061 | case -2: n = lex_cast< int16_t>(sn); break; | |
6062 | case 2: n = lex_cast<uint16_t>(sn); break; | |
6063 | case -4: n = lex_cast< int32_t>(sn); break; | |
6064 | case 4: n = lex_cast<uint32_t>(sn); break; | |
6065 | default: | |
6066 | case -8: n = lex_cast< int64_t>(sn); break; | |
6067 | case 8: n = lex_cast<uint64_t>(sn); break; | |
6068 | } | |
6069 | } | |
6070 | catch (std::runtime_error&) | |
6071 | { | |
6072 | goto not_matched; | |
6073 | } | |
6074 | literal_number* ln = new literal_number(n); | |
8aabf152 FCE |
6075 | ln->tok = e->tok; |
6076 | argexpr = ln; | |
6077 | goto matched; | |
6078 | } | |
6079 | ||
14900130 SC |
6080 | if (dwarf_regs.empty()) |
6081 | goto not_matched; | |
b78a0fbb | 6082 | |
8aabf152 FCE |
6083 | // Build regex pieces out of the known dwarf_regs. We keep two separate |
6084 | // lists: ones with the % prefix (and thus unambigiuous even despite PR11821), | |
6085 | // and ones with no prefix (and thus only usable in unambiguous contexts). | |
46a94997 | 6086 | for (map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++) |
8aabf152 FCE |
6087 | { |
6088 | string regname = ri->first; | |
6089 | assert (regname != ""); | |
6090 | regnames += string("|")+regname; | |
6091 | if (regname[0]=='%') | |
6092 | percent_regnames += string("|")+regname; | |
6093 | } | |
6094 | // clip off leading | | |
6095 | regnames = regnames.substr(1); | |
272c9036 WF |
6096 | if (percent_regnames != "") |
6097 | percent_regnames = percent_regnames.substr(1); | |
8aabf152 FCE |
6098 | |
6099 | // test for REGISTER | |
6100 | // NB: Because PR11821, we must use percent_regnames here. | |
272c9036 | 6101 | if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM) |
9109f487 SC |
6102 | rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches); |
6103 | else | |
332ba7e7 | 6104 | rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches); |
8aabf152 FCE |
6105 | if (! rc) |
6106 | { | |
6107 | string regname = matches[1]; | |
46a94997 SC |
6108 | map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname); |
6109 | if (ri != dwarf_regs.end()) // known register | |
8aabf152 FCE |
6110 | { |
6111 | embedded_expr *get_arg1 = new embedded_expr; | |
19c22e1f | 6112 | string width_adjust; |
46a94997 | 6113 | switch (ri->second.second) |
19c22e1f | 6114 | { |
892ec39a SC |
6115 | case QI: width_adjust = ") & 0xff)"; break; |
6116 | case QIh: width_adjust = ">>8) & 0xff)"; break; | |
46a94997 | 6117 | case HI: |
71e5e13d | 6118 | // preserve 16 bit register signness |
892ec39a SC |
6119 | width_adjust = ") & 0xffff)"; |
6120 | if (precision < 0) | |
55b377f4 | 6121 | width_adjust += " << 48 >> 48"; |
ac8a78aa SC |
6122 | break; |
6123 | case SI: | |
6124 | // preserve 32 bit register signness | |
892ec39a SC |
6125 | width_adjust = ") & 0xffffffff)"; |
6126 | if (precision < 0) | |
55b377f4 | 6127 | width_adjust += " << 32 >> 32"; |
19c22e1f | 6128 | break; |
892ec39a | 6129 | default: width_adjust = "))"; |
19c22e1f | 6130 | } |
55b377f4 SC |
6131 | string type = ""; |
6132 | if (probe_type == uprobe3_type) | |
6133 | type = (precision < 0 | |
6134 | ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)"; | |
6135 | type = type + "(("; | |
8aabf152 FCE |
6136 | get_arg1->tok = e->tok; |
6137 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
892ec39a | 6138 | + string(" ((int64_t)") + type |
8aabf152 FCE |
6139 | + (is_user_module (process_name) |
6140 | ? string("u_fetch_register(") | |
6141 | : string("k_fetch_register(")) | |
46a94997 | 6142 | + lex_cast(dwarf_regs[regname].first) + string("))") |
19c22e1f | 6143 | + width_adjust; |
8aabf152 FCE |
6144 | argexpr = get_arg1; |
6145 | goto matched; | |
6146 | } | |
6147 | // invalid register name, fall through | |
6148 | } | |
40fe32e0 | 6149 | |
272c9036 | 6150 | int reg, offset1; |
e5b7b83f | 6151 | // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N |
40fe32e0 | 6152 | // NB: Despite PR11821, we can use regnames here, since the parentheses |
e5b7b83f | 6153 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) |
272c9036 WF |
6154 | // On ARM test for [REGISTER, OFFSET] |
6155 | if (elf_machine == EM_ARM) | |
6156 | { | |
669a2feb | 6157 | rc = regexp_match (asmarg, string("^\\[(")+regnames+string(")(, #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$"), matches); |
272c9036 | 6158 | reg = 1; |
669a2feb | 6159 | offset1 = 3; |
272c9036 WF |
6160 | } |
6161 | else | |
6162 | { | |
6163 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string(")[)]$"), matches); | |
6164 | reg = 4; | |
6165 | offset1 = 1; | |
6166 | } | |
8aabf152 FCE |
6167 | if (! rc) |
6168 | { | |
e5b7b83f | 6169 | string regname; |
8aabf152 | 6170 | int64_t disp = 0; |
272c9036 WF |
6171 | if (matches[reg].length()) |
6172 | regname = matches[reg]; | |
8095a157 FCE |
6173 | if (dwarf_regs.find (regname) == dwarf_regs.end()) |
6174 | goto not_matched; | |
6175 | ||
272c9036 | 6176 | for (int i=offset1; i <= (offset1 + 2); i++) |
e5b7b83f SC |
6177 | if (matches[i].length()) |
6178 | try | |
6179 | { | |
6180 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
6181 | } | |
8aabf152 FCE |
6182 | catch (const runtime_error& f) // unparseable offset |
6183 | { | |
6184 | goto not_matched; // can't just 'break' out of | |
6185 | // this case or use a sentinel | |
6186 | // value, unfortunately | |
6187 | } | |
6188 | ||
8aabf152 | 6189 | // synthesize user_long(%{fetch_register(R)%} + D) |
8aabf152 FCE |
6190 | embedded_expr *get_arg1 = new embedded_expr; |
6191 | get_arg1->tok = e->tok; | |
6192 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
6193 | + (is_user_module (process_name) | |
6194 | ? string("u_fetch_register(") | |
6195 | : string("k_fetch_register(")) | |
46a94997 | 6196 | + lex_cast(dwarf_regs[regname].first) + string(")"); |
8aabf152 | 6197 | // XXX: may we ever need to cast that to a narrower type? |
40fe32e0 | 6198 | |
8aabf152 FCE |
6199 | literal_number* inc = new literal_number(disp); |
6200 | inc->tok = e->tok; | |
40fe32e0 | 6201 | |
8aabf152 FCE |
6202 | binary_expression *be = new binary_expression; |
6203 | be->tok = e->tok; | |
6204 | be->left = get_arg1; | |
6205 | be->op = "+"; | |
6206 | be->right = inc; | |
40fe32e0 | 6207 | |
8aabf152 | 6208 | functioncall *fc = new functioncall; |
40fe32e0 SC |
6209 | switch (precision) |
6210 | { | |
7f6ce9ab SC |
6211 | case 1: case -1: |
6212 | fc->function = "user_int8"; break; | |
6213 | case 2: | |
6214 | fc->function = "user_uint16"; break; | |
6215 | case -2: | |
6216 | fc->function = "user_int16"; break; | |
6217 | case 4: | |
6218 | fc->function = "user_uint32"; break; | |
6219 | case -4: | |
6220 | fc->function = "user_int32"; break; | |
6221 | case 8: case -8: | |
6222 | fc->function = "user_int64"; break; | |
40fe32e0 SC |
6223 | default: fc->function = "user_long"; |
6224 | } | |
8aabf152 FCE |
6225 | fc->tok = e->tok; |
6226 | fc->args.push_back(be); | |
366af4e7 | 6227 | |
8aabf152 FCE |
6228 | argexpr = fc; |
6229 | goto matched; | |
6230 | } | |
8095a157 FCE |
6231 | |
6232 | // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N | |
6233 | // NB: Despite PR11821, we can use regnames here, since the parentheses | |
6234 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) | |
0f7b51d6 | 6235 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string("),(")+regnames+string(")(,[1248])?[)]$"), matches); |
8095a157 FCE |
6236 | if (! rc) |
6237 | { | |
6238 | string baseregname; | |
6239 | string indexregname; | |
6240 | int64_t disp = 0; | |
6241 | short scale = 1; | |
6242 | ||
6243 | if (matches[6].length()) | |
6244 | try | |
6245 | { | |
6246 | scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma! | |
6247 | // We could verify that scale is one of 1,2,4,8, | |
6248 | // but it doesn't really matter. An erroneous | |
6249 | // address merely results in run-time errors. | |
8aabf152 | 6250 | } |
8095a157 FCE |
6251 | catch (const runtime_error &f) // unparseable scale |
6252 | { | |
6253 | goto not_matched; | |
6254 | } | |
6255 | ||
6256 | if (matches[4].length()) | |
6257 | baseregname = matches[4]; | |
6258 | if (dwarf_regs.find (baseregname) == dwarf_regs.end()) | |
6259 | goto not_matched; | |
6260 | ||
6261 | if (matches[5].length()) | |
6262 | indexregname = matches[5]; | |
6263 | if (dwarf_regs.find (indexregname) == dwarf_regs.end()) | |
6264 | goto not_matched; | |
6265 | ||
f7719b3d | 6266 | for (int i = 1; i <= 3; i++) // up to three OFFSET terms |
8095a157 FCE |
6267 | if (matches[i].length()) |
6268 | try | |
6269 | { | |
6270 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
6271 | } | |
6272 | catch (const runtime_error& f) // unparseable offset | |
6273 | { | |
6274 | goto not_matched; // can't just 'break' out of | |
6275 | // this case or use a sentinel | |
6276 | // value, unfortunately | |
6277 | } | |
6278 | ||
6279 | // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D) | |
6280 | ||
6281 | embedded_expr *get_arg1 = new embedded_expr; | |
6282 | string regfn = is_user_module (process_name) | |
6283 | ? string("u_fetch_register") | |
6284 | : string("k_fetch_register"); // NB: in practice sdt.h probes are for userspace only | |
6285 | ||
6286 | get_arg1->tok = e->tok; | |
6287 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
6288 | + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")") | |
6289 | + string("+(") | |
6290 | + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")") | |
6291 | + string("*") | |
6292 | + lex_cast(scale) | |
6293 | + string(")"); | |
6294 | ||
6295 | // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too | |
6296 | literal_number* inc = new literal_number(disp); | |
6297 | inc->tok = e->tok; | |
6298 | ||
6299 | binary_expression *be = new binary_expression; | |
6300 | be->tok = e->tok; | |
6301 | be->left = get_arg1; | |
6302 | be->op = "+"; | |
6303 | be->right = inc; | |
6304 | ||
6305 | functioncall *fc = new functioncall; | |
6306 | switch (precision) | |
6307 | { | |
6308 | case 1: case -1: | |
6309 | fc->function = "user_int8"; break; | |
6310 | case 2: | |
6311 | fc->function = "user_uint16"; break; | |
6312 | case -2: | |
6313 | fc->function = "user_int16"; break; | |
6314 | case 4: | |
6315 | fc->function = "user_uint32"; break; | |
6316 | case -4: | |
6317 | fc->function = "user_int32"; break; | |
6318 | case 8: case -8: | |
6319 | fc->function = "user_int64"; break; | |
6320 | default: fc->function = "user_long"; | |
6321 | } | |
6322 | fc->tok = e->tok; | |
6323 | fc->args.push_back(be); | |
6324 | ||
6325 | argexpr = fc; | |
6326 | goto matched; | |
8aabf152 FCE |
6327 | } |
6328 | ||
8aabf152 FCE |
6329 | |
6330 | not_matched: | |
6331 | // The asmarg operand was not recognized. Back down to dwarf. | |
6332 | if (! session.suppress_warnings) | |
84fef8ee FCE |
6333 | { |
6334 | if (probe_type == UPROBE3_TYPE) | |
6c9e1946 | 6335 | session.print_warning (_F("Can't parse SDT_V3 operand '%s' [man error::sdt]", asmarg.c_str()), e->tok); |
84fef8ee | 6336 | else // must be *PROBE2; others don't get asm operands |
b78a0fbb | 6337 | session.print_warning (_F("Downgrading SDT_V2 probe argument to dwarf, can't parse '%s' [man error::sdt]", |
84fef8ee FCE |
6338 | asmarg.c_str()), e->tok); |
6339 | } | |
8aabf152 FCE |
6340 | assert (argexpr == 0); |
6341 | need_debug_info = true; | |
dc09353a | 6342 | throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo [man error::sdt]"), e->tok); |
366af4e7 | 6343 | |
8aabf152 FCE |
6344 | matched: |
6345 | assert (argexpr != 0); | |
366af4e7 RM |
6346 | |
6347 | if (session.verbose > 2) | |
1e41115c | 6348 | //TRANSLATORS: We're mapping the operand to a new expression*. |
b530b5b3 | 6349 | clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl; |
366af4e7 | 6350 | |
aff5d390 | 6351 | if (e->components.empty()) // We have a scalar |
8aabf152 FCE |
6352 | { |
6353 | if (e->addressof) | |
dc09353a | 6354 | throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok); |
8aabf152 FCE |
6355 | provide (argexpr); |
6356 | return; | |
6357 | } | |
6358 | else // $var->foo | |
6359 | { | |
6360 | cast_op *cast = new cast_op; | |
6361 | cast->name = "@cast"; | |
6362 | cast->tok = e->tok; | |
6363 | cast->operand = argexpr; | |
6364 | cast->components = e->components; | |
6365 | cast->type_name = probe_name + "_arg" + lex_cast(argno); | |
6366 | cast->module = process_name; | |
6367 | cast->visit(this); | |
6368 | return; | |
6369 | } | |
366af4e7 | 6370 | |
8aabf152 | 6371 | /* NOTREACHED */ |
aff5d390 SC |
6372 | } |
6373 | catch (const semantic_error &er) | |
6374 | { | |
6375 | e->chain (er); | |
6376 | provide (e); | |
6377 | } | |
6378 | } | |
6379 | ||
6380 | ||
6ef331c8 SC |
6381 | void |
6382 | sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
6383 | { | |
6384 | try | |
6385 | { | |
49131a6d | 6386 | assert(e->name.size() > 0 |
bd1fcbad | 6387 | && (e->name[0] == '$' || e->name == "@var")); |
6ef331c8 SC |
6388 | |
6389 | if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars") | |
6390 | visit_target_symbol_context (e); | |
6391 | else | |
6392 | visit_target_symbol_arg (e); | |
6393 | } | |
6394 | catch (const semantic_error &er) | |
6395 | { | |
6396 | e->chain (er); | |
6397 | provide (e); | |
6398 | } | |
6399 | } | |
6400 | ||
6401 | ||
bd1fcbad YZ |
6402 | void |
6403 | sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e) | |
6404 | { | |
6405 | need_debug_info = true; | |
6406 | ||
6407 | // Fill in our current module context if needed | |
6408 | if (e->module.empty()) | |
6409 | e->module = process_name; | |
6410 | ||
6411 | var_expanding_visitor::visit_atvar_op(e); | |
6412 | } | |
6413 | ||
6414 | ||
40a0c64e JS |
6415 | void |
6416 | sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e) | |
6417 | { | |
6418 | // Fill in our current module context if needed | |
6419 | if (e->module.empty()) | |
6420 | e->module = process_name; | |
6421 | ||
6422 | var_expanding_visitor::visit_cast_op(e); | |
6423 | } | |
6424 | ||
6425 | ||
576eaefe SC |
6426 | void |
6427 | plt_expanding_visitor::visit_target_symbol (target_symbol *e) | |
6428 | { | |
6429 | try | |
6430 | { | |
6431 | if (e->name == "$$name") | |
6432 | { | |
6433 | literal_string *myname = new literal_string (entry); | |
6434 | myname->tok = e->tok; | |
6435 | provide(myname); | |
6436 | return; | |
6437 | } | |
3d69c03f JS |
6438 | |
6439 | // variable not found -> throw a semantic error | |
6440 | // (only to be caught right away, but this may be more complex later...) | |
6441 | string alternatives = "$$name"; | |
dc09353a | 6442 | throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)", |
3d69c03f | 6443 | e->name.c_str(), alternatives.c_str()), e->tok); |
576eaefe SC |
6444 | } |
6445 | catch (const semantic_error &er) | |
6446 | { | |
6447 | e->chain (er); | |
6448 | provide (e); | |
6449 | } | |
6450 | } | |
6451 | ||
6452 | ||
edce5b67 JS |
6453 | struct sdt_query : public base_query |
6454 | { | |
6455 | sdt_query(probe * base_probe, probe_point * base_loc, | |
6456 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 6457 | vector<derived_probe *> & results, const string user_lib); |
edce5b67 | 6458 | |
51d6bda3 | 6459 | void query_library (const char *data); |
576eaefe | 6460 | void query_plt (const char *entry, size_t addr) {} |
edce5b67 JS |
6461 | void handle_query_module(); |
6462 | ||
6463 | private: | |
15284963 | 6464 | stap_sdt_probe_type probe_type; |
d61ea602 | 6465 | enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc; |
edce5b67 JS |
6466 | probe * base_probe; |
6467 | probe_point * base_loc; | |
6846cfc8 | 6468 | literal_map_t const & params; |
edce5b67 | 6469 | vector<derived_probe *> & results; |
a794dbeb FCE |
6470 | string pp_mark; |
6471 | string pp_provider; | |
51d6bda3 | 6472 | string user_lib; |
edce5b67 JS |
6473 | |
6474 | set<string> probes_handled; | |
6475 | ||
6476 | Elf_Data *pdata; | |
6477 | size_t probe_scn_offset; | |
6478 | size_t probe_scn_addr; | |
aff5d390 | 6479 | uint64_t arg_count; |
40fe32e0 | 6480 | GElf_Addr base; |
c57ea854 | 6481 | GElf_Addr pc; |
aff5d390 | 6482 | string arg_string; |
edce5b67 | 6483 | string probe_name; |
a794dbeb | 6484 | string provider_name; |
909ab234 | 6485 | GElf_Addr semaphore_load_offset; |
79a0ca08 | 6486 | Dwarf_Addr semaphore; |
edce5b67 JS |
6487 | |
6488 | bool init_probe_scn(); | |
6b51ee12 | 6489 | bool get_next_probe(); |
c57ea854 SC |
6490 | void iterate_over_probe_entries(); |
6491 | void handle_probe_entry(); | |
edce5b67 | 6492 | |
488d3c1b JL |
6493 | static void setup_note_probe_entry_callback (sdt_query *me, |
6494 | int type, | |
6495 | const char *data, | |
6496 | size_t len); | |
40fe32e0 SC |
6497 | void setup_note_probe_entry (int type, const char *data, size_t len); |
6498 | ||
edce5b67 | 6499 | void convert_probe(probe *base); |
4ddb6dd0 | 6500 | void record_semaphore(vector<derived_probe *> & results, unsigned start); |
c72aa911 | 6501 | probe* convert_location(); |
40fe32e0 | 6502 | bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;} |
c57ea854 SC |
6503 | bool have_debuginfo_uprobe(bool need_debug_info) |
6504 | {return probe_type == uprobe1_type | |
40fe32e0 | 6505 | || ((probe_type == uprobe2_type || probe_type == uprobe3_type) |
c57ea854 | 6506 | && need_debug_info);} |
40fe32e0 | 6507 | bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;} |
edce5b67 JS |
6508 | }; |
6509 | ||
6510 | ||
6511 | sdt_query::sdt_query(probe * base_probe, probe_point * base_loc, | |
6512 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 6513 | vector<derived_probe *> & results, const string user_lib): |
d61ea602 JS |
6514 | base_query(dw, params), probe_type(unknown_probe_type), |
6515 | probe_loc(unknown_section), base_probe(base_probe), | |
74fe61bc LB |
6516 | base_loc(base_loc), params(params), results(results), user_lib(user_lib), |
6517 | probe_scn_offset(0), probe_scn_addr(0), arg_count(0), base(0), pc(0), | |
909ab234 | 6518 | semaphore_load_offset(0), semaphore(0) |
edce5b67 | 6519 | { |
a794dbeb FCE |
6520 | assert(get_string_param(params, TOK_MARK, pp_mark)); |
6521 | get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified | |
6522 | ||
ef428667 FCE |
6523 | // PR10245: permit usage of dtrace-y "-" separator in marker name; |
6524 | // map it to double-underscores. | |
6525 | size_t pos = 0; | |
6526 | while (1) // there may be more than one | |
6527 | { | |
a794dbeb | 6528 | size_t i = pp_mark.find("-", pos); |
ef428667 | 6529 | if (i == string::npos) break; |
a794dbeb | 6530 | pp_mark.replace (i, 1, "__"); |
ef428667 FCE |
6531 | pos = i+1; // resume searching after the inserted __ |
6532 | } | |
a794dbeb FCE |
6533 | |
6534 | // XXX: same for pp_provider? | |
edce5b67 JS |
6535 | } |
6536 | ||
6537 | ||
6538 | void | |
c57ea854 | 6539 | sdt_query::handle_probe_entry() |
edce5b67 | 6540 | { |
c57ea854 SC |
6541 | if (! have_uprobe() |
6542 | && !probes_handled.insert(probe_name).second) | |
edce5b67 JS |
6543 | return; |
6544 | ||
6545 | if (sess.verbose > 3) | |
c57ea854 | 6546 | { |
b78a0fbb | 6547 | //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe |
b530b5b3 LB |
6548 | //TRANSLATORS: is matched to. |
6549 | clog << _F("matched probe_name %s probe type ", probe_name.c_str()); | |
c57ea854 SC |
6550 | switch (probe_type) |
6551 | { | |
6552 | case uprobe1_type: | |
6553 | clog << "uprobe1 at 0x" << hex << pc << dec << endl; | |
6554 | break; | |
6555 | case uprobe2_type: | |
6556 | clog << "uprobe2 at 0x" << hex << pc << dec << endl; | |
6557 | break; | |
40fe32e0 SC |
6558 | case uprobe3_type: |
6559 | clog << "uprobe3 at 0x" << hex << pc << dec << endl; | |
6560 | break; | |
d61ea602 JS |
6561 | default: |
6562 | clog << "unknown!" << endl; | |
6563 | break; | |
c57ea854 SC |
6564 | } |
6565 | } | |
edce5b67 | 6566 | |
c57ea854 SC |
6567 | // Extend the derivation chain |
6568 | probe *new_base = convert_location(); | |
6569 | probe_point *new_location = new_base->locations[0]; | |
6570 | ||
c57ea854 SC |
6571 | bool need_debug_info = false; |
6572 | ||
7d395255 JS |
6573 | // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...)) |
6574 | // or dwfl_module_getelf(...). We only need it for the machine type, which | |
6575 | // should be the same. The bias is used for relocating debuginfoless probes, | |
6576 | // though, so that must come from the possibly-prelinked ELF file, not DWARF. | |
c57ea854 | 6577 | Dwarf_Addr bias; |
7d395255 | 6578 | Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias); |
c57ea854 | 6579 | |
1cc41cd6 DS |
6580 | /* Figure out the architecture of this particular ELF file. The |
6581 | dwarfless register-name mappings depend on it. */ | |
6582 | GElf_Ehdr ehdr_mem; | |
6583 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
6584 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } | |
d8f31d0a | 6585 | assert(em); |
1cc41cd6 DS |
6586 | int elf_machine = em->e_machine; |
6587 | sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, module_val, | |
6588 | provider_name, probe_name, | |
6589 | probe_type, arg_string, arg_count); | |
6590 | svv.replace (new_base->body); | |
6591 | need_debug_info = svv.need_debug_info; | |
c57ea854 | 6592 | |
1cc41cd6 DS |
6593 | // XXX: why not derive_probes() in the uprobes case too? |
6594 | literal_map_t params; | |
6595 | for (unsigned i = 0; i < new_location->components.size(); ++i) | |
6596 | { | |
6597 | probe_point::component *c = new_location->components[i]; | |
6598 | params[c->functor] = c->arg; | |
6599 | } | |
c57ea854 | 6600 | |
73d53dd2 | 6601 | unsigned prior_results_size = results.size(); |
1cc41cd6 DS |
6602 | dwarf_query q(new_base, new_location, dw, params, results, "", ""); |
6603 | q.has_mark = true; // enables mid-statement probing | |
30263a73 | 6604 | |
73d53dd2 JS |
6605 | // V1 probes always need dwarf info |
6606 | // V2+ probes need dwarf info in case of a variable reference | |
1cc41cd6 | 6607 | if (have_debuginfo_uprobe(need_debug_info)) |
06de3a04 | 6608 | dw.iterate_over_modules<base_query>(&query_module, &q); |
73d53dd2 JS |
6609 | |
6610 | // For V2+ probes, if variable references weren't used or failed (PR14369), | |
6611 | // then try with the more direct approach. Unresolved $vars might still | |
6612 | // cause their own error, but this gives them a chance to be optimized out. | |
6613 | if (have_debuginfoless_uprobe() && results.size() == prior_results_size) | |
1cc41cd6 DS |
6614 | { |
6615 | string section; | |
6616 | Dwarf_Addr reloc_addr = q.statement_num_val + bias; | |
6617 | if (dwfl_module_relocations (q.dw.mod_info->mod) > 0) | |
6618 | { | |
6619 | dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr); | |
6620 | section = ".dynamic"; | |
6621 | } | |
6622 | else | |
6623 | section = ".absolute"; | |
edce5b67 | 6624 | |
1cc41cd6 DS |
6625 | uprobe_derived_probe* p = |
6626 | new uprobe_derived_probe ("", "", 0, | |
6627 | path_remove_sysroot(sess,q.module_val), | |
6628 | section, | |
6629 | q.statement_num_val, reloc_addr, q, 0); | |
6630 | p->saveargs (arg_count); | |
6631 | results.push_back (p); | |
c57ea854 | 6632 | } |
487bf4e2 | 6633 | sess.unwindsym_modules.insert (dw.module_name); |
73d53dd2 | 6634 | record_semaphore(results, prior_results_size); |
c57ea854 | 6635 | } |
edce5b67 | 6636 | |
4ddb6dd0 | 6637 | |
c57ea854 SC |
6638 | void |
6639 | sdt_query::handle_query_module() | |
6640 | { | |
6641 | if (!init_probe_scn()) | |
6642 | return; | |
edce5b67 | 6643 | |
c57ea854 SC |
6644 | if (sess.verbose > 3) |
6645 | clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl; | |
edce5b67 | 6646 | |
40fe32e0 SC |
6647 | if (probe_loc == note_section) |
6648 | { | |
6649 | GElf_Shdr shdr_mem; | |
6650 | GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem); | |
6651 | ||
909ab234 JS |
6652 | // The 'base' lets us adjust the hardcoded addresses in notes for prelink |
6653 | // effects. The 'semaphore_load_offset' accounts for the difference in | |
6654 | // load addresses between text and data, so the semaphore can be | |
6655 | // converted to a file offset if needed. | |
40fe32e0 | 6656 | if (shdr) |
909ab234 JS |
6657 | { |
6658 | base = shdr->sh_addr; | |
6659 | GElf_Addr base_offset = shdr->sh_offset; | |
6660 | shdr = dw.get_section (".probes", &shdr_mem); | |
6661 | if (shdr) | |
6662 | semaphore_load_offset = | |
6663 | (shdr->sh_addr - shdr->sh_offset) - (base - base_offset); | |
6664 | } | |
40fe32e0 | 6665 | else |
909ab234 JS |
6666 | base = semaphore_load_offset = 0; |
6667 | ||
488d3c1b | 6668 | dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback); |
40fe32e0 | 6669 | } |
d61ea602 | 6670 | else if (probe_loc == probe_section) |
40fe32e0 | 6671 | iterate_over_probe_entries (); |
edce5b67 JS |
6672 | } |
6673 | ||
6674 | ||
6675 | bool | |
6676 | sdt_query::init_probe_scn() | |
6677 | { | |
448a86b7 | 6678 | Elf* elf; |
edce5b67 | 6679 | GElf_Shdr shdr_mem; |
40fe32e0 SC |
6680 | |
6681 | GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem); | |
6682 | if (shdr) | |
6683 | { | |
6684 | probe_loc = note_section; | |
6685 | return true; | |
6686 | } | |
edce5b67 | 6687 | |
448a86b7 | 6688 | shdr = dw.get_section (".probes", &shdr_mem, &elf); |
fea74777 | 6689 | if (shdr) |
edce5b67 | 6690 | { |
fea74777 SC |
6691 | pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
6692 | probe_scn_offset = 0; | |
6693 | probe_scn_addr = shdr->sh_addr; | |
6694 | assert (pdata != NULL); | |
6695 | if (sess.verbose > 4) | |
ce0f6648 LB |
6696 | clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: " |
6697 | << pdata->d_size << endl; | |
40fe32e0 | 6698 | probe_loc = probe_section; |
fea74777 | 6699 | return true; |
edce5b67 | 6700 | } |
fea74777 | 6701 | else |
edce5b67 | 6702 | return false; |
edce5b67 JS |
6703 | } |
6704 | ||
40fe32e0 | 6705 | void |
488d3c1b | 6706 | sdt_query::setup_note_probe_entry_callback (sdt_query *me, int type, const char *data, size_t len) |
40fe32e0 | 6707 | { |
40fe32e0 SC |
6708 | me->setup_note_probe_entry (type, data, len); |
6709 | } | |
6710 | ||
6711 | ||
6712 | void | |
6713 | sdt_query::setup_note_probe_entry (int type, const char *data, size_t len) | |
6714 | { | |
6715 | // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME | |
6716 | // && !memcmp (data->d_buf + name_off, | |
6717 | // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME)) | |
6718 | ||
6719 | // probes are in the .note.stapsdt section | |
6720 | #define _SDT_NOTE_TYPE 3 | |
6721 | if (type != _SDT_NOTE_TYPE) | |
6722 | return; | |
6723 | ||
6724 | union | |
6725 | { | |
6726 | Elf64_Addr a64[3]; | |
6727 | Elf32_Addr a32[3]; | |
6728 | } buf; | |
6729 | Dwarf_Addr bias; | |
6730 | Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias)); | |
6731 | Elf_Data dst = | |
6732 | { | |
6733 | &buf, ELF_T_ADDR, EV_CURRENT, | |
6734 | gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0 | |
6735 | }; | |
6736 | assert (dst.d_size <= sizeof buf); | |
6737 | ||
6738 | if (len < dst.d_size + 3) | |
6739 | return; | |
6740 | ||
6741 | Elf_Data src = | |
6742 | { | |
6743 | (void *) data, ELF_T_ADDR, EV_CURRENT, | |
6744 | dst.d_size, 0, 0 | |
6745 | }; | |
6746 | ||
6747 | if (gelf_xlatetom (elf, &dst, &src, | |
6748 | elf_getident (elf, NULL)[EI_DATA]) == NULL) | |
6749 | printf ("gelf_xlatetom: %s", elf_errmsg (-1)); | |
6750 | ||
6751 | probe_type = uprobe3_type; | |
6752 | const char * provider = data + dst.d_size; | |
3f803f9e | 6753 | |
40fe32e0 | 6754 | const char *name = (const char*)memchr (provider, '\0', data + len - provider); |
3f803f9e CM |
6755 | if(name++ == NULL) |
6756 | return; | |
6757 | ||
6758 | const char *args = (const char*)memchr (name, '\0', data + len - name); | |
6759 | if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1) | |
6760 | return; | |
6761 | ||
6762 | provider_name = provider; | |
6763 | probe_name = name; | |
6764 | arg_string = args; | |
40fe32e0 | 6765 | |
e90006d7 JL |
6766 | dw.mod_info->marks.insert(make_pair(provider, name)); |
6767 | ||
40fe32e0 SC |
6768 | // Did we find a matching probe? |
6769 | if (! (dw.function_name_matches_pattern (probe_name, pp_mark) | |
6770 | && ((pp_provider == "") | |
6771 | || dw.function_name_matches_pattern (provider_name, pp_provider)))) | |
6772 | return; | |
6773 | ||
e9a90eee JS |
6774 | // PR13934: Assembly probes are not forced to use the N@OP form. |
6775 | // If we have '@' then great, else count based on space-delimiters. | |
6776 | arg_count = count(arg_string.begin(), arg_string.end(), '@'); | |
6777 | if (!arg_count && !arg_string.empty()) | |
6778 | arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' '); | |
6779 | ||
40fe32e0 SC |
6780 | GElf_Addr base_ref; |
6781 | if (gelf_getclass (elf) == ELFCLASS32) | |
6782 | { | |
6783 | pc = buf.a32[0]; | |
6784 | base_ref = buf.a32[1]; | |
6785 | semaphore = buf.a32[2]; | |
6786 | } | |
6787 | else | |
6788 | { | |
6789 | pc = buf.a64[0]; | |
6790 | base_ref = buf.a64[1]; | |
6791 | semaphore = buf.a64[2]; | |
6792 | } | |
6793 | ||
6794 | semaphore += base - base_ref; | |
6795 | pc += base - base_ref; | |
6796 | ||
7d395255 JS |
6797 | // The semaphore also needs the ELF bias added now, so |
6798 | // record_semaphore can properly relocate it later. | |
6799 | semaphore += bias; | |
6800 | ||
40fe32e0 | 6801 | if (sess.verbose > 4) |
b530b5b3 | 6802 | clog << _F(" saw .note.stapsdt %s%s ", probe_name.c_str(), (provider_name != "" ? _(" (provider ")+provider_name+") " : "").c_str()) << "@0x" << hex << pc << dec << endl; |
40fe32e0 SC |
6803 | |
6804 | handle_probe_entry(); | |
6805 | } | |
6806 | ||
6807 | ||
c57ea854 SC |
6808 | void |
6809 | sdt_query::iterate_over_probe_entries() | |
edce5b67 | 6810 | { |
c57ea854 | 6811 | // probes are in the .probe section |
edce5b67 JS |
6812 | while (probe_scn_offset < pdata->d_size) |
6813 | { | |
aff5d390 SC |
6814 | stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset); |
6815 | stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset); | |
15284963 | 6816 | probe_type = (stap_sdt_probe_type)(pbe_v1->type_a); |
1cc41cd6 | 6817 | if (! have_uprobe()) |
edce5b67 JS |
6818 | { |
6819 | // Unless this is a mangled .probes section, this happens | |
6820 | // because the name of the probe comes first, followed by | |
6821 | // the sentinel. | |
6822 | if (sess.verbose > 5) | |
b530b5b3 | 6823 | clog << _F("got unknown probe_type : 0x%x", probe_type) << endl; |
edce5b67 JS |
6824 | probe_scn_offset += sizeof(__uint32_t); |
6825 | continue; | |
6826 | } | |
aff5d390 SC |
6827 | if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b |
6828 | { | |
6829 | pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t)); | |
1cc41cd6 | 6830 | if (pbe_v1->type_b != uprobe1_type) |
aff5d390 SC |
6831 | continue; |
6832 | } | |
6833 | ||
1cc41cd6 | 6834 | if (probe_type == uprobe1_type) |
aff5d390 | 6835 | { |
79a0ca08 | 6836 | if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6837 | return; |
79a0ca08 | 6838 | semaphore = 0; |
aff5d390 | 6839 | probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr); |
a794dbeb | 6840 | provider_name = ""; // unknown |
1cc41cd6 DS |
6841 | pc = pbe_v1->arg; |
6842 | arg_count = 0; | |
aff5d390 SC |
6843 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v1); |
6844 | } | |
08b5a50c | 6845 | else if (probe_type == uprobe2_type) |
aff5d390 | 6846 | { |
79a0ca08 | 6847 | if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6848 | return; |
79a0ca08 | 6849 | semaphore = pbe_v2->semaphore; |
aff5d390 | 6850 | probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr); |
a794dbeb | 6851 | provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr); |
aff5d390 SC |
6852 | arg_count = pbe_v2->arg_count; |
6853 | pc = pbe_v2->pc; | |
6854 | if (pbe_v2->arg_string) | |
6855 | arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr); | |
79a0ca08 SC |
6856 | // skip over pbe_v2, probe_name text and provider text |
6857 | probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length(); | |
6858 | probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t); | |
aff5d390 | 6859 | } |
edce5b67 | 6860 | if (sess.verbose > 4) |
b530b5b3 | 6861 | clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "") |
aff5d390 | 6862 | << "@0x" << hex << pc << dec << endl; |
edce5b67 | 6863 | |
e90006d7 JL |
6864 | dw.mod_info->marks.insert(make_pair(provider_name, probe_name)); |
6865 | ||
a794dbeb FCE |
6866 | if (dw.function_name_matches_pattern (probe_name, pp_mark) |
6867 | && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider))) | |
c57ea854 | 6868 | handle_probe_entry (); |
edce5b67 | 6869 | } |
edce5b67 JS |
6870 | } |
6871 | ||
6872 | ||
6846cfc8 | 6873 | void |
4ddb6dd0 | 6874 | sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start) |
6846cfc8 | 6875 | { |
a794dbeb FCE |
6876 | for (unsigned i=0; i<2; i++) { |
6877 | // prefer with-provider symbol; look without provider prefix for backward compatibility only | |
6878 | string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore"; | |
6879 | // XXX: multiple addresses? | |
6880 | if (sess.verbose > 2) | |
b530b5b3 | 6881 | clog << _F("looking for semaphore symbol %s ", semaphore.c_str()); |
a794dbeb | 6882 | |
79a0ca08 SC |
6883 | Dwarf_Addr addr; |
6884 | if (this->semaphore) | |
6885 | addr = this->semaphore; | |
6886 | else | |
6887 | addr = lookup_symbol_address(dw.module, semaphore.c_str()); | |
a794dbeb FCE |
6888 | if (addr) |
6889 | { | |
7d395255 | 6890 | if (dwfl_module_relocations (dw.module) > 0) |
a794dbeb FCE |
6891 | dwfl_module_relocate_address (dw.module, &addr); |
6892 | // XXX: relocation basis? | |
909ab234 JS |
6893 | |
6894 | // Dyninst needs the *file*-based offset for semaphores, | |
6895 | // so subtract the difference in load addresses between .text and .probes | |
6896 | if (dw.sess.runtime_usermode_p()) | |
6897 | addr -= semaphore_load_offset; | |
6898 | ||
a794dbeb FCE |
6899 | for (unsigned i = start; i < results.size(); ++i) |
6900 | results[i]->sdt_semaphore_addr = addr; | |
6901 | if (sess.verbose > 2) | |
b530b5b3 | 6902 | clog << _(", found at 0x") << hex << addr << dec << endl; |
a794dbeb FCE |
6903 | return; |
6904 | } | |
6905 | else | |
6906 | if (sess.verbose > 2) | |
b530b5b3 | 6907 | clog << _(", not found") << endl; |
a794dbeb | 6908 | } |
6846cfc8 SC |
6909 | } |
6910 | ||
6911 | ||
edce5b67 JS |
6912 | void |
6913 | sdt_query::convert_probe (probe *base) | |
6914 | { | |
6915 | block *b = new block; | |
6916 | b->tok = base->body->tok; | |
6917 | ||
edce5b67 JS |
6918 | // Generate: if (arg1 != mark("label")) next; |
6919 | functioncall *fc = new functioncall; | |
bbafcb1e | 6920 | fc->function = "ulong_arg"; |
edce5b67 | 6921 | fc->tok = b->tok; |
bbafcb1e | 6922 | literal_number* num = new literal_number(1); |
edce5b67 JS |
6923 | num->tok = b->tok; |
6924 | fc->args.push_back(num); | |
6925 | ||
6926 | functioncall *fcus = new functioncall; | |
6927 | fcus->function = "user_string"; | |
6928 | fcus->type = pe_string; | |
6929 | fcus->tok = b->tok; | |
6930 | fcus->args.push_back(fc); | |
6931 | ||
6932 | if_statement *is = new if_statement; | |
6933 | is->thenblock = new next_statement; | |
6934 | is->elseblock = NULL; | |
6935 | is->tok = b->tok; | |
63ea4244 | 6936 | is->thenblock->tok = b->tok; |
edce5b67 JS |
6937 | comparison *be = new comparison; |
6938 | be->op = "!="; | |
6939 | be->tok = b->tok; | |
6940 | be->left = fcus; | |
6941 | be->right = new literal_string(probe_name); | |
63ea4244 | 6942 | be->right->tok = b->tok; |
edce5b67 JS |
6943 | is->condition = be; |
6944 | b->statements.push_back(is); | |
6945 | ||
6946 | // Now replace the body | |
6947 | b->statements.push_back(base->body); | |
6948 | base->body = b; | |
6949 | } | |
6950 | ||
6951 | ||
c72aa911 JS |
6952 | probe* |
6953 | sdt_query::convert_location () | |
edce5b67 | 6954 | { |
c72aa911 | 6955 | probe_point* specific_loc = new probe_point(*base_loc); |
662539d9 | 6956 | vector<probe_point::component*> derived_comps; |
edce5b67 | 6957 | |
662539d9 JS |
6958 | vector<probe_point::component*>::iterator it; |
6959 | for (it = specific_loc->components.begin(); | |
6960 | it != specific_loc->components.end(); ++it) | |
6961 | if ((*it)->functor == TOK_PROCESS) | |
6962 | { | |
1cc41cd6 DS |
6963 | // copy the process name |
6964 | derived_comps.push_back(*it); | |
662539d9 JS |
6965 | } |
6966 | else if ((*it)->functor == TOK_LIBRARY) | |
6967 | { | |
1cc41cd6 DS |
6968 | // copy the library name for process probes |
6969 | derived_comps.push_back(*it); | |
662539d9 JS |
6970 | } |
6971 | else if ((*it)->functor == TOK_PROVIDER) | |
6972 | { | |
6973 | // replace the possibly wildcarded arg with the specific provider name | |
6974 | *it = new probe_point::component(TOK_PROVIDER, | |
6975 | new literal_string(provider_name)); | |
6976 | } | |
6977 | else if ((*it)->functor == TOK_MARK) | |
c72aa911 JS |
6978 | { |
6979 | // replace the possibly wildcarded arg with the specific marker name | |
662539d9 JS |
6980 | *it = new probe_point::component(TOK_MARK, |
6981 | new literal_string(probe_name)); | |
a794dbeb | 6982 | |
aff5d390 SC |
6983 | if (sess.verbose > 3) |
6984 | switch (probe_type) | |
6985 | { | |
6986 | case uprobe1_type: | |
b530b5b3 | 6987 | clog << _("probe_type == uprobe1, use statement addr: 0x") |
aff5d390 SC |
6988 | << hex << pc << dec << endl; |
6989 | break; | |
6990 | case uprobe2_type: | |
b530b5b3 | 6991 | clog << _("probe_type == uprobe2, use statement addr: 0x") |
aff5d390 SC |
6992 | << hex << pc << dec << endl; |
6993 | break; | |
40fe32e0 | 6994 | case uprobe3_type: |
b530b5b3 | 6995 | clog << _("probe_type == uprobe3, use statement addr: 0x") |
40fe32e0 SC |
6996 | << hex << pc << dec << endl; |
6997 | break; | |
aff5d390 | 6998 | default: |
b530b5b3 LB |
6999 | clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s", |
7000 | pp_mark.c_str()) << endl; | |
aff5d390 SC |
7001 | } |
7002 | ||
c72aa911 JS |
7003 | switch (probe_type) |
7004 | { | |
aff5d390 SC |
7005 | case uprobe1_type: |
7006 | case uprobe2_type: | |
40fe32e0 | 7007 | case uprobe3_type: |
c72aa911 | 7008 | // process("executable").statement(probe_arg) |
662539d9 JS |
7009 | derived_comps.push_back |
7010 | (new probe_point::component(TOK_STATEMENT, | |
7011 | new literal_number(pc, true))); | |
c72aa911 JS |
7012 | break; |
7013 | ||
a794dbeb | 7014 | default: // deprecated |
c72aa911 | 7015 | // process("executable").function("*").label("_stapprobe1_MARK_NAME") |
662539d9 JS |
7016 | derived_comps.push_back |
7017 | (new probe_point::component(TOK_FUNCTION, | |
7018 | new literal_string("*"))); | |
7019 | derived_comps.push_back | |
c72aa911 | 7020 | (new probe_point::component(TOK_LABEL, |
a794dbeb | 7021 | new literal_string("_stapprobe1_" + pp_mark))); |
c72aa911 JS |
7022 | break; |
7023 | } | |
7024 | } | |
edce5b67 | 7025 | |
662539d9 JS |
7026 | probe_point* derived_loc = new probe_point(*specific_loc); |
7027 | derived_loc->components = derived_comps; | |
8159bf55 | 7028 | return new probe (new probe (base_probe, specific_loc), derived_loc); |
edce5b67 JS |
7029 | } |
7030 | ||
7031 | ||
51d6bda3 SC |
7032 | void |
7033 | sdt_query::query_library (const char *library) | |
7034 | { | |
7035 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
7036 | } | |
7037 | ||
e90006d7 JL |
7038 | string |
7039 | suggest_marks(systemtap_session& sess, | |
7040 | const set<string>& modules, | |
7041 | const string& mark, | |
7042 | const string& provider) | |
7043 | { | |
7044 | if (mark.empty() || modules.empty() || sess.module_cache == NULL) | |
7045 | return ""; | |
7046 | ||
7047 | set<string> marks; | |
7048 | const map<string, module_info*> &cache = sess.module_cache->cache; | |
7049 | bool dash_suggestions = (mark.find("-") != string::npos); | |
7050 | ||
7051 | for (set<string>::iterator itmod = modules.begin(); | |
7052 | itmod != modules.end(); ++itmod) | |
7053 | { | |
7054 | map<string, module_info*>::const_iterator itcache; | |
7055 | if ((itcache = cache.find(*itmod)) != cache.end()) | |
7056 | { | |
7057 | set<pair<string,string> >::const_iterator itmarks; | |
7058 | for (itmarks = itcache->second->marks.begin(); | |
7059 | itmarks != itcache->second->marks.end(); ++itmarks) | |
7060 | { | |
7061 | if (provider.empty() | |
7062 | // simulating dw.function_name_matches_pattern() | |
7063 | || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0)) | |
7064 | { | |
7065 | string marksug = itmarks->second; | |
7066 | if (dash_suggestions) | |
7067 | { | |
7068 | size_t pos = 0; | |
7069 | while (1) // there may be more than one | |
7070 | { | |
7071 | size_t i = marksug.find("__", pos); | |
7072 | if (i == string::npos) break; | |
7073 | marksug.replace (i, 2, "-"); | |
7074 | pos = i+1; // resume searching after the inserted - | |
7075 | } | |
7076 | } | |
7077 | marks.insert(marksug); | |
7078 | } | |
7079 | } | |
7080 | } | |
7081 | } | |
7082 | ||
7083 | if (sess.verbose > 2) | |
7084 | { | |
7085 | clog << "suggesting " << marks.size() << " marks " | |
7086 | << "from modules:" << endl; | |
7087 | for (set<string>::iterator itmod = modules.begin(); | |
7088 | itmod != modules.end(); ++itmod) | |
7089 | clog << *itmod << endl; | |
7090 | } | |
7091 | ||
7092 | if (marks.empty()) | |
7093 | return ""; | |
7094 | ||
7095 | return levenshtein_suggest(mark, marks, 5); // print top 5 marks only | |
7096 | } | |
7097 | ||
40a70f52 JL |
7098 | string |
7099 | suggest_plt_functions(systemtap_session& sess, | |
7100 | const set<string>& modules, | |
7101 | const string& func) | |
7102 | { | |
7103 | if (func.empty() || modules.empty() || sess.module_cache == NULL) | |
7104 | return ""; | |
7105 | ||
7106 | set<string> funcs; | |
7107 | const map<string, module_info*> &cache = sess.module_cache->cache; | |
7108 | ||
7109 | for (set<string>::iterator itmod = modules.begin(); | |
7110 | itmod != modules.end(); ++itmod) | |
7111 | { | |
7112 | map<string, module_info*>::const_iterator itcache; | |
7113 | if ((itcache = cache.find(*itmod)) != cache.end()) | |
7114 | funcs.insert(itcache->second->plt_funcs.begin(), | |
7115 | itcache->second->plt_funcs.end()); | |
7116 | } | |
7117 | ||
7118 | if (sess.verbose > 2) | |
d82dd71f JL |
7119 | { |
7120 | clog << "suggesting " << funcs.size() << " plt functions " | |
7121 | << "from modules:" << endl; | |
7122 | for (set<string>::iterator itmod = modules.begin(); | |
7123 | itmod != modules.end(); ++itmod) | |
7124 | clog << *itmod << endl; | |
7125 | } | |
40a70f52 JL |
7126 | |
7127 | if (funcs.empty()) | |
7128 | return ""; | |
7129 | ||
7130 | return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only | |
7131 | } | |
51d6bda3 | 7132 | |
0508c45f JL |
7133 | string |
7134 | suggest_dwarf_functions(systemtap_session& sess, | |
7135 | const set<string>& modules, | |
7136 | string func) | |
44ffe90c JL |
7137 | { |
7138 | // Trim any @ component | |
7139 | size_t pos = func.find('@'); | |
7140 | if (pos != string::npos) | |
7141 | func.erase(pos); | |
7142 | ||
0508c45f | 7143 | if (func.empty() || modules.empty() || sess.module_cache == NULL) |
44ffe90c JL |
7144 | return ""; |
7145 | ||
7146 | // We must first aggregate all the functions from the cache | |
7147 | set<string> funcs; | |
7148 | const map<string, module_info*> &cache = sess.module_cache->cache; | |
7149 | ||
0508c45f JL |
7150 | for (set<string>::iterator itmod = modules.begin(); |
7151 | itmod != modules.end(); ++itmod) | |
44ffe90c | 7152 | { |
6e6d9a3b JL |
7153 | module_info *module; |
7154 | ||
7155 | // retrieve module_info from cache | |
44ffe90c JL |
7156 | map<string, module_info*>::const_iterator itcache; |
7157 | if ((itcache = cache.find(*itmod)) != cache.end()) | |
6e6d9a3b JL |
7158 | module = itcache->second; |
7159 | else // module not found | |
7160 | continue; | |
7161 | ||
7162 | // add inlines | |
7163 | funcs.insert(module->inlined_funcs.begin(), | |
7164 | module->inlined_funcs.end()); | |
7165 | ||
7166 | // add all function symbols in cache | |
7167 | if (module->symtab_status != info_present || module->sym_table == NULL) | |
7168 | continue; | |
7169 | map<string, func_info*>& modfuncs = module->sym_table->map_by_name; | |
7170 | for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin(); | |
7171 | itfuncs != modfuncs.end(); ++itfuncs) | |
7172 | funcs.insert(itfuncs->first); | |
44ffe90c JL |
7173 | } |
7174 | ||
7175 | if (sess.verbose > 2) | |
d82dd71f JL |
7176 | { |
7177 | clog << "suggesting " << funcs.size() << " dwarf functions " | |
7178 | << "from modules:" << endl; | |
7179 | for (set<string>::iterator itmod = modules.begin(); | |
7180 | itmod != modules.end(); ++itmod) | |
7181 | clog << *itmod << endl; | |
7182 | } | |
44ffe90c JL |
7183 | |
7184 | if (funcs.empty()) | |
7185 | return ""; | |
7186 | ||
593f09eb | 7187 | return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only |
44ffe90c JL |
7188 | } |
7189 | ||
20c6c071 | 7190 | void |
5227f1ea | 7191 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 7192 | probe * base, |
20c6c071 | 7193 | probe_point * location, |
86bf665e | 7194 | literal_map_t const & parameters, |
20c6c071 GH |
7195 | vector<derived_probe *> & finished_results) |
7196 | { | |
b20febf3 FCE |
7197 | // NB: the kernel/user dwlfpp objects are long-lived. |
7198 | // XXX: but they should be per-session, as this builder object | |
7199 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 7200 | |
7a24d422 | 7201 | dwflpp* dw = 0; |
6d5d594e | 7202 | literal_map_t filled_parameters = parameters; |
7a24d422 | 7203 | |
7a24d422 | 7204 | string module_name; |
ae2552da FCE |
7205 | if (has_null_param (parameters, TOK_KERNEL)) |
7206 | { | |
7207 | dw = get_kern_dw(sess, "kernel"); | |
7208 | } | |
7209 | else if (get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 7210 | { |
c523a015 LB |
7211 | size_t dash_pos = 0; |
7212 | while((dash_pos=module_name.find('-'))!=string::npos) | |
7213 | module_name.replace(int(dash_pos),1,"_"); | |
7214 | filled_parameters[TOK_MODULE] = new literal_string(module_name); | |
37001baa FCE |
7215 | // NB: glob patterns get expanded later, during the offline |
7216 | // elfutils module listing. | |
ae2552da | 7217 | dw = get_kern_dw(sess, module_name); |
b8da0ad1 | 7218 | } |
6d5d594e | 7219 | else if (get_param (parameters, TOK_PROCESS, module_name) || has_null_param(parameters, TOK_PROCESS)) |
84c84ac4 | 7220 | { |
05fb3e0c | 7221 | module_name = sess.sysroot + module_name; |
6d5d594e LB |
7222 | if(has_null_param(filled_parameters, TOK_PROCESS)) |
7223 | { | |
7224 | wordexp_t words; | |
7225 | int rc = wordexp(sess.cmd.c_str(), &words, WRDE_NOCMD|WRDE_UNDEF); | |
7226 | if(rc || words.we_wordc <= 0) | |
dc09353a | 7227 | throw SEMANTIC_ERROR(_("unspecified process probe is invalid without a -c COMMAND")); |
05fb3e0c | 7228 | module_name = sess.sysroot + words.we_wordv[0]; |
6d5d594e LB |
7229 | filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map |
7230 | // in the case of TOK_MARK we need to modify locations as well | |
7231 | if(location->components[0]->functor==TOK_PROCESS && | |
7232 | location->components[0]->arg == 0) | |
7233 | location->components[0]->arg = new literal_string(module_name); | |
7234 | wordfree (& words); | |
b78a0fbb | 7235 | } |
5750ecc6 | 7236 | |
37001baa FCE |
7237 | // PR6456 process("/bin/*") glob handling |
7238 | if (contains_glob_chars (module_name)) | |
7239 | { | |
7240 | // Expand glob via rewriting the probe-point process("....") | |
7241 | // parameter, asserted to be the first one. | |
7242 | ||
7243 | assert (location->components.size() > 0); | |
7244 | assert (location->components[0]->functor == TOK_PROCESS); | |
7245 | assert (location->components[0]->arg); | |
7246 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
7247 | assert (lit); | |
7248 | ||
7249 | // Evaluate glob here, and call derive_probes recursively with each match. | |
7250 | glob_t the_blob; | |
88f8e2c0 | 7251 | set<string> dupes; |
37001baa | 7252 | int rc = glob (module_name.c_str(), 0, NULL, & the_blob); |
b530b5b3 | 7253 | if (rc) |
dc09353a | 7254 | throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() )); |
44ffe90c | 7255 | unsigned results_pre = finished_results.size(); |
37001baa FCE |
7256 | for (unsigned i = 0; i < the_blob.gl_pathc; ++i) |
7257 | { | |
e19ebcf7 | 7258 | assert_no_interrupts(); |
37001baa FCE |
7259 | |
7260 | const char* globbed = the_blob.gl_pathv[i]; | |
7261 | struct stat st; | |
7262 | ||
7263 | if (access (globbed, X_OK) == 0 | |
7264 | && stat (globbed, &st) == 0 | |
7265 | && S_ISREG (st.st_mode)) // see find_executable() | |
7266 | { | |
7977a734 FCE |
7267 | // Need to call canonicalize here, in order to path-expand |
7268 | // patterns like process("stap*"). Otherwise it may go through | |
7269 | // to the next round of expansion as ("stap"), leading to a $PATH | |
7270 | // search that's not consistent with the glob search already done. | |
5bca76a8 JS |
7271 | string canononicalized = resolve_path (globbed); |
7272 | globbed = canononicalized.c_str(); | |
7977a734 | 7273 | |
88f8e2c0 JS |
7274 | // The canonical names can result in duplication, for example |
7275 | // having followed symlinks that are common with shared | |
7276 | // libraries. Filter those out. | |
7277 | if (!dupes.insert(canononicalized).second) | |
7278 | continue; | |
7279 | ||
37001baa FCE |
7280 | // synthesize a new probe_point, with the glob-expanded string |
7281 | probe_point *pp = new probe_point (*location); | |
ef0943df JL |
7282 | pp->from_glob = true; |
7283 | ||
5750ecc6 FCE |
7284 | // PR13338: quote results to prevent recursion |
7285 | string eglobbed = escape_glob_chars (globbed); | |
7286 | ||
7287 | if (sess.verbose > 1) | |
7288 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", | |
7289 | module_name.c_str(), eglobbed.c_str()) << endl; | |
05fb3e0c | 7290 | string eglobbed_tgt = path_remove_sysroot(sess, eglobbed); |
5750ecc6 | 7291 | |
37001baa | 7292 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 7293 | new literal_string (eglobbed_tgt)); |
37001baa FCE |
7294 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
7295 | pp->components[0] = ppc; | |
7296 | ||
d885563b | 7297 | probe* new_probe = new probe (base, pp); |
7977a734 FCE |
7298 | |
7299 | // We override "optional = true" here, as if the | |
7300 | // wildcarded probe point was given a "?" suffix. | |
7301 | ||
7302 | // This is because wildcard probes will be expected | |
7303 | // by users to apply only to some subset of the | |
7304 | // matching binaries, in the sense of "any", rather | |
7305 | // than "all", sort of similarly how | |
7306 | // module("*").function("...") patterns work. | |
7307 | ||
7308 | derive_probes (sess, new_probe, finished_results, | |
7309 | true /* NB: not location->optional */ ); | |
37001baa FCE |
7310 | } |
7311 | } | |
7312 | ||
7313 | globfree (& the_blob); | |
44ffe90c JL |
7314 | |
7315 | unsigned results_post = finished_results.size(); | |
7316 | ||
e90006d7 | 7317 | // Did we fail to find a function/plt/mark by name? Let's suggest |
44ffe90c JL |
7318 | // something! |
7319 | string func; | |
7320 | if (results_pre == results_post | |
7321 | && get_param(filled_parameters, TOK_FUNCTION, func) | |
7322 | && !func.empty()) | |
7323 | { | |
0508c45f | 7324 | string sugs = suggest_dwarf_functions(sess, modules_seen, func); |
44ffe90c JL |
7325 | modules_seen.clear(); |
7326 | if (!sugs.empty()) | |
dc09353a | 7327 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", |
44ffe90c JL |
7328 | "no match (similar functions: %s)", |
7329 | sugs.find(',') == string::npos, | |
7330 | sugs.c_str())); | |
7331 | } | |
40a70f52 JL |
7332 | else if (results_pre == results_post |
7333 | && get_param(filled_parameters, TOK_PLT, func) | |
7334 | && !func.empty()) | |
7335 | { | |
7336 | string sugs = suggest_plt_functions(sess, modules_seen, func); | |
7337 | modules_seen.clear(); | |
7338 | if (!sugs.empty()) | |
7339 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", | |
7340 | "no match (similar functions: %s)", | |
7341 | sugs.find(',') == string::npos, | |
7342 | sugs.c_str())); | |
7343 | } | |
e90006d7 JL |
7344 | else if (results_pre == results_post |
7345 | && get_param(filled_parameters, TOK_MARK, func) | |
7346 | && !func.empty()) | |
7347 | { | |
7348 | string provider; | |
7349 | get_param(filled_parameters, TOK_PROVIDER, provider); | |
7350 | ||
7351 | string sugs = suggest_marks(sess, modules_seen, func, provider); | |
7352 | modules_seen.clear(); | |
7353 | if (!sugs.empty()) | |
7354 | throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)", | |
7355 | "no match (similar marks: %s)", | |
7356 | sugs.find(',') == string::npos, | |
7357 | sugs.c_str())); | |
7358 | } | |
44ffe90c | 7359 | |
37001baa FCE |
7360 | return; // avoid falling through |
7361 | } | |
7362 | ||
5750ecc6 FCE |
7363 | // PR13338: unquote glob results |
7364 | module_name = unescape_glob_chars (module_name); | |
05fb3e0c | 7365 | user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it |
d1bcbe71 RH |
7366 | |
7367 | // if the executable starts with "#!", we look for the interpreter of the script | |
7368 | { | |
7369 | ifstream script_file (user_path.c_str () ); | |
7370 | ||
7371 | if (script_file.good ()) | |
7372 | { | |
7373 | string line; | |
7374 | ||
7375 | getline (script_file, line); | |
7376 | ||
7377 | if (line.compare (0, 2, "#!") == 0) | |
7378 | { | |
7379 | string path_head = line.substr(2); | |
7380 | ||
7381 | // remove white spaces at the beginning of the string | |
7382 | size_t p2 = path_head.find_first_not_of(" \t"); | |
7383 | ||
7384 | if (p2 != string::npos) | |
7385 | { | |
7386 | string path = path_head.substr(p2); | |
7387 | ||
7388 | // remove white spaces at the end of the string | |
7389 | p2 = path.find_last_not_of(" \t\n"); | |
7390 | if (string::npos != p2) | |
7391 | path.erase(p2+1); | |
7392 | ||
8e13c1a1 RH |
7393 | // handle "#!/usr/bin/env" redirect |
7394 | size_t offset = 0; | |
7395 | if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0) | |
7396 | { | |
7397 | offset = sizeof("/bin/env")-1; | |
7398 | } | |
7399 | else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0) | |
7400 | { | |
7401 | offset = sizeof("/usr/bin/env")-1; | |
7402 | } | |
7403 | ||
7404 | if (offset != 0) | |
7405 | { | |
7406 | size_t p3 = path.find_first_not_of(" \t", offset); | |
7407 | ||
7408 | if (p3 != string::npos) | |
7409 | { | |
7410 | string env_path = path.substr(p3); | |
05fb3e0c WF |
7411 | user_path = find_executable (env_path, sess.sysroot, |
7412 | sess.sysenv); | |
8e13c1a1 RH |
7413 | } |
7414 | } | |
7415 | else | |
7416 | { | |
05fb3e0c | 7417 | user_path = find_executable (path, sess.sysroot, sess.sysenv); |
8e13c1a1 | 7418 | } |
d1bcbe71 RH |
7419 | |
7420 | struct stat st; | |
7421 | ||
7422 | if (access (user_path.c_str(), X_OK) == 0 | |
7423 | && stat (user_path.c_str(), &st) == 0 | |
7424 | && S_ISREG (st.st_mode)) // see find_executable() | |
7425 | { | |
7426 | if (sess.verbose > 1) | |
b530b5b3 LB |
7427 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", |
7428 | module_name.c_str(), user_path.c_str()) << endl; | |
d1bcbe71 RH |
7429 | |
7430 | assert (location->components.size() > 0); | |
7431 | assert (location->components[0]->functor == TOK_PROCESS); | |
7432 | assert (location->components[0]->arg); | |
7433 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
7434 | assert (lit); | |
7435 | ||
7436 | // synthesize a new probe_point, with the expanded string | |
7437 | probe_point *pp = new probe_point (*location); | |
05fb3e0c | 7438 | string user_path_tgt = path_remove_sysroot(sess, user_path); |
d1bcbe71 | 7439 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 7440 | new literal_string (user_path_tgt.c_str())); |
d1bcbe71 RH |
7441 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
7442 | pp->components[0] = ppc; | |
7443 | ||
d885563b | 7444 | probe* new_probe = new probe (base, pp); |
d1bcbe71 RH |
7445 | |
7446 | derive_probes (sess, new_probe, finished_results); | |
7447 | ||
7448 | script_file.close(); | |
7449 | return; | |
7450 | } | |
7451 | } | |
7452 | } | |
7453 | } | |
7454 | script_file.close(); | |
7455 | } | |
7456 | ||
47e226ed | 7457 | if (get_param (parameters, TOK_LIBRARY, user_lib) |
378d78b5 | 7458 | && user_lib.length() && ! contains_glob_chars (user_lib)) |
47e226ed SC |
7459 | { |
7460 | module_name = find_executable (user_lib, sess.sysroot, sess.sysenv, | |
7461 | "LD_LIBRARY_PATH"); | |
7462 | if (module_name.find('/') == string::npos) | |
7463 | // We didn't find user_lib so use iterate_over_libraries | |
7464 | module_name = user_path; | |
7465 | } | |
63b4fd14 | 7466 | else |
b642c901 | 7467 | module_name = user_path; // canonicalize it |
d0a7f5a9 | 7468 | |
3667d615 | 7469 | // uretprobes aren't available everywhere |
536f1261 | 7470 | if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p()) |
2b69faaf | 7471 | { |
3667d615 JS |
7472 | if (kernel_supports_inode_uprobes(sess) && |
7473 | !kernel_supports_inode_uretprobes(sess)) | |
dc09353a | 7474 | throw SEMANTIC_ERROR |
3ffeaf3c | 7475 | (_("process return probes not available [man error::inode-uprobes]")); |
2b69faaf | 7476 | } |
3667d615 | 7477 | |
f4000852 MW |
7478 | // There is a similar check in pass 4 (buildrun), but it is |
7479 | // needed here too to make sure alternatives for optional | |
7480 | // (? or !) process probes are disposed and/or alternatives | |
7481 | // are selected. | |
3667d615 JS |
7482 | if (!sess.runtime_usermode_p()) |
7483 | check_process_probe_kernel_support(sess); | |
e34d5d13 | 7484 | |
7a24d422 FCE |
7485 | // user-space target; we use one dwflpp instance per module name |
7486 | // (= program or shared library) | |
707bf35e | 7487 | dw = get_user_dw(sess, module_name); |
c8959a29 | 7488 | } |
20c6c071 | 7489 | |
1acfc030 JS |
7490 | assert(dw); |
7491 | ||
e90006d7 JL |
7492 | unsigned results_pre = finished_results.size(); |
7493 | ||
5896cd05 | 7494 | if (sess.verbose > 3) |
b530b5b3 | 7495 | clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl; |
5896cd05 | 7496 | |
a794dbeb FCE |
7497 | string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __ |
7498 | if (get_param(parameters, TOK_MARK, dummy_mark_name)) | |
f28a8c28 | 7499 | { |
51d6bda3 | 7500 | sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib); |
06de3a04 | 7501 | dw->iterate_over_modules<base_query>(&query_module, &sdtq); |
e90006d7 JL |
7502 | |
7503 | // We need to update modules_seen with the modules we've visited | |
7504 | modules_seen.insert(sdtq.visited_modules.begin(), | |
7505 | sdtq.visited_modules.end()); | |
7506 | ||
7507 | // Did we fail to find a mark? | |
7508 | if (results_pre == finished_results.size() && !location->from_glob) | |
7509 | { | |
7510 | string provider; | |
7511 | get_param(filled_parameters, TOK_PROVIDER, provider); | |
7512 | ||
7513 | string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider); | |
7514 | modules_seen.clear(); | |
7515 | if (!sugs.empty()) | |
7516 | throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)", | |
7517 | "no match (similar marks: %s)", | |
7518 | sugs.find(',') == string::npos, | |
7519 | sugs.c_str())); | |
7520 | } | |
7521 | ||
edce5b67 | 7522 | return; |
7a05f484 | 7523 | } |
20c6c071 | 7524 | |
6d5d594e | 7525 | dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib); |
7a24d422 FCE |
7526 | |
7527 | // XXX: kernel.statement.absolute is a special case that requires no | |
7528 | // dwfl processing. This code should be in a separate builder. | |
7a24d422 | 7529 | if (q.has_kernel && q.has_absolute) |
37ebca01 | 7530 | { |
4baf0e53 | 7531 | // assert guru mode for absolute probes |
37ebca01 FCE |
7532 | if (! q.base_probe->privileged) |
7533 | { | |
dc09353a | 7534 | throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), |
edce5b67 | 7535 | q.base_probe->tok); |
37ebca01 FCE |
7536 | } |
7537 | ||
7538 | // For kernel.statement(NUM).absolute probe points, we bypass | |
7539 | // all the debuginfo stuff: We just wire up a | |
7540 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 7541 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
7542 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
7543 | q.statement_num_val, q.statement_num_val, | |
7544 | q, 0); | |
37ebca01 | 7545 | finished_results.push_back (p); |
1a0dbc5a | 7546 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
7547 | return; |
7548 | } | |
7549 | ||
06de3a04 | 7550 | dw->iterate_over_modules<base_query>(&query_module, &q); |
8f14e444 | 7551 | |
44ffe90c JL |
7552 | // We need to update modules_seen with the modules we've visited |
7553 | modules_seen.insert(q.visited_modules.begin(), | |
7554 | q.visited_modules.end()); | |
8f14e444 FCE |
7555 | |
7556 | // PR11553 special processing: .return probes requested, but | |
7557 | // some inlined function instances matched. | |
7558 | unsigned i_n_r = q.inlined_non_returnable.size(); | |
7559 | unsigned results_post = finished_results.size(); | |
7560 | if (i_n_r > 0) | |
7561 | { | |
7562 | if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning | |
7563 | { | |
7564 | string quicklist; | |
7565 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
7566 | it != q.inlined_non_returnable.end(); | |
7567 | it++) | |
7568 | { | |
7569 | quicklist += " " + (*it); | |
7570 | if (quicklist.size() > 80) // heuristic, don't make an overlong report line | |
7571 | { | |
7572 | quicklist += " ..."; | |
7573 | break; | |
7574 | } | |
7575 | } | |
c57ea854 | 7576 | |
52c2652f | 7577 | sess.print_warning (_NF("cannot probe .return of %u inlined function %s", |
b530b5b3 | 7578 | "cannot probe .return of %u inlined functions %s", |
52c2652f | 7579 | quicklist.size(), i_n_r, quicklist.c_str())); |
8f14e444 FCE |
7580 | // There will be also a "no matches" semantic error generated. |
7581 | } | |
7582 | if (sess.verbose > 1) | |
52c2652f NMA |
7583 | clog << _NF("skipped .return probe of %u inlined function", |
7584 | "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl; | |
8f14e444 FCE |
7585 | if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity |
7586 | { | |
7587 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
7588 | it != q.inlined_non_returnable.end(); | |
7589 | it++) | |
7590 | clog << (*it) << " "; | |
7591 | clog << endl; | |
7592 | } | |
7593 | } // i_n_r > 0 | |
44ffe90c | 7594 | |
e90006d7 | 7595 | // If we just failed to resolve a function/plt by name, we can suggest |
ef0943df JL |
7596 | // something. We only suggest things for probe points that were not |
7597 | // synthesized from a glob, i.e. only for 'real' probes. This is also | |
7598 | // required because modules_seen needs to accumulate across recursive | |
7599 | // calls for process(glob)[.library(glob)] probes. | |
44ffe90c | 7600 | string func; |
ef0943df | 7601 | if (results_pre == results_post && !location->from_glob |
44ffe90c JL |
7602 | && get_param(filled_parameters, TOK_FUNCTION, func) |
7603 | && !func.empty()) | |
7604 | { | |
0508c45f | 7605 | string sugs = suggest_dwarf_functions(sess, modules_seen, func); |
44ffe90c JL |
7606 | modules_seen.clear(); |
7607 | if (!sugs.empty()) | |
dc09353a | 7608 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", |
44ffe90c JL |
7609 | "no match (similar functions: %s)", |
7610 | sugs.find(',') == string::npos, | |
7611 | sugs.c_str())); | |
7612 | } | |
ef0943df | 7613 | else if (results_pre == results_post && !location->from_glob |
40a70f52 JL |
7614 | && get_param(filled_parameters, TOK_PLT, func) |
7615 | && !func.empty()) | |
7616 | { | |
7617 | string sugs = suggest_plt_functions(sess, modules_seen, func); | |
7618 | modules_seen.clear(); | |
7619 | if (!sugs.empty()) | |
7620 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", | |
7621 | "no match (similar functions: %s)", | |
7622 | sugs.find(',') == string::npos, | |
7623 | sugs.c_str())); | |
7624 | } | |
44ffe90c JL |
7625 | else if (results_pre != results_post) |
7626 | // Something was derived so we won't need to suggest something | |
7627 | modules_seen.clear(); | |
5f0a03a6 JK |
7628 | } |
7629 | ||
7630 | symbol_table::~symbol_table() | |
7631 | { | |
c9efa5c9 | 7632 | delete_map(map_by_addr); |
5f0a03a6 JK |
7633 | } |
7634 | ||
7635 | void | |
2867a2a1 | 7636 | symbol_table::add_symbol(const char *name, bool weak, bool descriptor, |
44ffe90c | 7637 | Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/) |
5f0a03a6 | 7638 | { |
ab91b232 JK |
7639 | #ifdef __powerpc__ |
7640 | // Map ".sys_foo" to "sys_foo". | |
7641 | if (name[0] == '.') | |
7642 | name++; | |
7643 | #endif | |
5f0a03a6 JK |
7644 | func_info *fi = new func_info(); |
7645 | fi->addr = addr; | |
7646 | fi->name = name; | |
ab91b232 | 7647 | fi->weak = weak; |
2867a2a1 | 7648 | fi->descriptor = descriptor; |
5f0a03a6 JK |
7649 | map_by_name[fi->name] = fi; |
7650 | // TODO: Use a multimap in case there are multiple static | |
7651 | // functions with the same name? | |
1c6b77e5 | 7652 | map_by_addr.insert(make_pair(addr, fi)); |
5f0a03a6 JK |
7653 | } |
7654 | ||
7655 | enum info_status | |
7656 | symbol_table::read_symbols(FILE *f, const string& path) | |
7657 | { | |
7658 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
7659 | int ret; | |
2e67a43b TM |
7660 | char *name = 0; |
7661 | char *mod = 0; | |
5f0a03a6 JK |
7662 | char type; |
7663 | unsigned long long addr; | |
7664 | Dwarf_Addr high_addr = 0; | |
7665 | int line = 0; | |
7666 | ||
62a4021d FCE |
7667 | #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7) |
7668 | #define MS_FMT "%ms" | |
7669 | #else | |
7670 | #define MS_FMT "%as" | |
7671 | #endif | |
7672 | // %ms (newer than %as) mallocs space for the string and stores its address. | |
7673 | while ((ret = fscanf(f, "%llx %c " MS_FMT " [" MS_FMT, &addr, &type, &name, &mod)) > 0) | |
5f0a03a6 | 7674 | { |
2e67a43b TM |
7675 | auto_free free_name(name); |
7676 | auto_free free_mod(mod); | |
5f0a03a6 JK |
7677 | line++; |
7678 | if (ret < 3) | |
7679 | { | |
3d372d6b | 7680 | cerr << _F("Symbol table error: Line %d of symbol list from %s is not in correct format: address type name [module]\n", |
b530b5b3 | 7681 | line, path.c_str()); |
5f0a03a6 JK |
7682 | // Caller should delete symbol_table object. |
7683 | return info_absent; | |
7684 | } | |
2e67a43b | 7685 | else if (ret > 3) |
5f0a03a6 JK |
7686 | { |
7687 | // Modules are loaded above the kernel, so if we're getting | |
7688 | // modules, we're done. | |
2e67a43b | 7689 | break; |
5f0a03a6 | 7690 | } |
ab91b232 | 7691 | if (type == 'T' || type == 't' || type == 'W') |
2867a2a1 | 7692 | add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr); |
5f0a03a6 JK |
7693 | } |
7694 | ||
1c6b77e5 | 7695 | if (map_by_addr.size() < 1) |
5f0a03a6 | 7696 | { |
3d372d6b | 7697 | cerr << _F("Symbol table error: %s contains no function symbols.\n", |
b530b5b3 | 7698 | path.c_str()) << endl; |
5f0a03a6 JK |
7699 | return info_absent; |
7700 | } | |
7701 | return info_present; | |
7702 | } | |
7703 | ||
7704 | // NB: This currently unused. We use get_from_elf() instead because | |
7705 | // that gives us raw addresses -- which we need for modules -- whereas | |
7706 | // nm provides the address relative to the beginning of the section. | |
7707 | enum info_status | |
83ca3872 | 7708 | symbol_table::read_from_elf_file(const string &path, |
2713ea24 | 7709 | systemtap_session &sess) |
5f0a03a6 | 7710 | { |
58502ae4 JS |
7711 | vector<string> cmd; |
7712 | cmd.push_back("/usr/bin/nm"); | |
7713 | cmd.push_back("-n"); | |
7714 | cmd.push_back("--defined-only"); | |
7715 | cmd.push_back("path"); | |
7716 | ||
5f0a03a6 | 7717 | FILE *f; |
58502ae4 JS |
7718 | int child_fd; |
7719 | pid_t child = stap_spawn_piped(sess.verbose, cmd, NULL, &child_fd); | |
7720 | if (child <= 0 || !(f = fdopen(child_fd, "r"))) | |
5f0a03a6 | 7721 | { |
58502ae4 | 7722 | // nm failures are detected by stap_waitpid |
3d372d6b | 7723 | cerr << _F("Internal error reading symbol table from %s -- %s\n", |
b530b5b3 | 7724 | path.c_str(), strerror(errno)); |
5f0a03a6 JK |
7725 | return info_absent; |
7726 | } | |
7727 | enum info_status status = read_symbols(f, path); | |
58502ae4 | 7728 | if (fclose(f) || stap_waitpid(sess.verbose, child)) |
5f0a03a6 | 7729 | { |
2713ea24 CM |
7730 | if (status == info_present) |
7731 | sess.print_warning("nm cannot read symbol table from " + path); | |
5f0a03a6 JK |
7732 | return info_absent; |
7733 | } | |
7734 | return status; | |
7735 | } | |
7736 | ||
7737 | enum info_status | |
83ca3872 | 7738 | symbol_table::read_from_text_file(const string& path, |
2713ea24 | 7739 | systemtap_session &sess) |
5f0a03a6 JK |
7740 | { |
7741 | FILE *f = fopen(path.c_str(), "r"); | |
7742 | if (!f) | |
7743 | { | |
2713ea24 | 7744 | sess.print_warning("cannot read symbol table from " + path + " -- " + strerror(errno)); |
5f0a03a6 JK |
7745 | return info_absent; |
7746 | } | |
7747 | enum info_status status = read_symbols(f, path); | |
7748 | (void) fclose(f); | |
7749 | return status; | |
7750 | } | |
7751 | ||
46f7b6be | 7752 | void |
f98c6346 | 7753 | symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused))) |
46f7b6be JK |
7754 | { |
7755 | #ifdef __powerpc__ | |
7756 | /* | |
7757 | * The .opd section contains function descriptors that can look | |
7758 | * just like function entry points. For example, there's a function | |
7759 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
7760 | * Reject all symbols in .opd. | |
7761 | */ | |
7762 | opd_section = SHN_UNDEF; | |
7763 | Dwarf_Addr bias; | |
7764 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
7765 | ?: dwfl_module_getelf (mod, &bias)); | |
7766 | Elf_Scn* scn = 0; | |
7767 | size_t shstrndx; | |
7768 | ||
7769 | if (!elf) | |
7770 | return; | |
fcc30d6d | 7771 | if (elf_getshdrstrndx (elf, &shstrndx) != 0) |
46f7b6be JK |
7772 | return; |
7773 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
7774 | { | |
7775 | GElf_Shdr shdr_mem; | |
7776 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
7777 | if (!shdr) | |
7778 | continue; | |
7779 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
7780 | if (!strcmp(name, ".opd")) | |
7781 | { | |
7782 | opd_section = elf_ndxscn(scn); | |
7783 | return; | |
7784 | } | |
7785 | } | |
7786 | #endif | |
7787 | } | |
7788 | ||
7789 | bool | |
7790 | symbol_table::reject_section(GElf_Word section) | |
7791 | { | |
7792 | if (section == SHN_UNDEF) | |
7793 | return true; | |
7794 | #ifdef __powerpc__ | |
7795 | if (section == opd_section) | |
7796 | return true; | |
7797 | #endif | |
7798 | return false; | |
7799 | } | |
7800 | ||
5f0a03a6 JK |
7801 | enum info_status |
7802 | symbol_table::get_from_elf() | |
7803 | { | |
7804 | Dwarf_Addr high_addr = 0; | |
7805 | Dwfl_Module *mod = mod_info->mod; | |
7806 | int syments = dwfl_module_getsymtab(mod); | |
7807 | assert(syments); | |
46f7b6be | 7808 | prepare_section_rejection(mod); |
5f0a03a6 JK |
7809 | for (int i = 1; i < syments; ++i) |
7810 | { | |
7811 | GElf_Sym sym; | |
ab91b232 JK |
7812 | GElf_Word section; |
7813 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
2867a2a1 | 7814 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC) |
ab91b232 | 7815 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
2867a2a1 | 7816 | reject_section(section), sym.st_value, &high_addr); |
5f0a03a6 JK |
7817 | } |
7818 | return info_present; | |
7819 | } | |
7820 | ||
5f0a03a6 JK |
7821 | func_info * |
7822 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
7823 | { | |
1c6b77e5 JS |
7824 | iterator_t iter = map_by_addr.upper_bound(addr); |
7825 | if (iter == map_by_addr.begin()) | |
5f0a03a6 | 7826 | return NULL; |
2e67a43b | 7827 | else |
1c6b77e5 | 7828 | return (--iter)->second; |
5f0a03a6 JK |
7829 | } |
7830 | ||
3d372d6b SC |
7831 | func_info * |
7832 | symbol_table::get_first_func() | |
7833 | { | |
7834 | iterator_t iter = map_by_addr.begin(); | |
7835 | return (iter)->second; | |
7836 | } | |
7837 | ||
5f0a03a6 JK |
7838 | func_info * |
7839 | symbol_table::lookup_symbol(const string& name) | |
7840 | { | |
7841 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
7842 | if (i == map_by_name.end()) | |
7843 | return NULL; | |
7844 | return i->second; | |
7845 | } | |
7846 | ||
7847 | Dwarf_Addr | |
7848 | symbol_table::lookup_symbol_address(const string& name) | |
7849 | { | |
7850 | func_info *fi = lookup_symbol(name); | |
7851 | if (fi) | |
7852 | return fi->addr; | |
7853 | return 0; | |
7854 | } | |
7855 | ||
ab91b232 JK |
7856 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
7857 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
7858 | // For system calls not implemented elsewhere, this weak symbol shows up | |
7859 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
7860 | // we refuse to consider such symbols. Here we delete them from our | |
7861 | // symbol table. | |
7862 | // TODO: Consider generalizing this and/or making it part of blacklist | |
7863 | // processing. | |
7864 | void | |
7865 | symbol_table::purge_syscall_stubs() | |
7866 | { | |
7867 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
7868 | if (stub_addr == 0) | |
7869 | return; | |
1c6b77e5 | 7870 | range_t purge_range = map_by_addr.equal_range(stub_addr); |
2e67a43b TM |
7871 | for (iterator_t iter = purge_range.first; |
7872 | iter != purge_range.second; | |
1c6b77e5 | 7873 | ) |
ab91b232 | 7874 | { |
1c6b77e5 | 7875 | func_info *fi = iter->second; |
2e67a43b | 7876 | if (fi->weak && fi->name != "sys_ni_syscall") |
ab91b232 | 7877 | { |
2e67a43b | 7878 | map_by_name.erase(fi->name); |
1c6b77e5 | 7879 | map_by_addr.erase(iter++); |
2e67a43b | 7880 | delete fi; |
2e67a43b | 7881 | } |
1c6b77e5 JS |
7882 | else |
7883 | iter++; | |
ab91b232 JK |
7884 | } |
7885 | } | |
7886 | ||
5f0a03a6 | 7887 | void |
d906ab9d | 7888 | module_info::get_symtab(base_query *q) |
5f0a03a6 | 7889 | { |
1c6b77e5 JS |
7890 | if (symtab_status != info_unknown) |
7891 | return; | |
7892 | ||
5f0a03a6 JK |
7893 | sym_table = new symbol_table(this); |
7894 | if (!elf_path.empty()) | |
7895 | { | |
5f0a03a6 JK |
7896 | symtab_status = sym_table->get_from_elf(); |
7897 | } | |
7898 | else | |
7899 | { | |
7900 | assert(name == TOK_KERNEL); | |
ab3ed72d DS |
7901 | symtab_status = info_absent; |
7902 | cerr << _("Error: Cannot find vmlinux.") << endl;; | |
5f0a03a6 JK |
7903 | } |
7904 | if (symtab_status == info_absent) | |
7905 | { | |
7906 | delete sym_table; | |
7907 | sym_table = NULL; | |
7908 | return; | |
7909 | } | |
7910 | ||
ab91b232 JK |
7911 | if (name == TOK_KERNEL) |
7912 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
7913 | } |
7914 | ||
1c6b77e5 JS |
7915 | // update_symtab reconciles data between the elf symbol table and the dwarf |
7916 | // function enumeration. It updates the symbol table entries with the dwarf | |
7917 | // die that describes the function, which also signals to query_module_symtab | |
7918 | // that a statement probe isn't needed. In return, it also adds aliases to the | |
7919 | // function table for names that share the same addr/die. | |
7920 | void | |
7921 | module_info::update_symtab(cu_function_cache_t *funcs) | |
7922 | { | |
7923 | if (!sym_table) | |
7924 | return; | |
7925 | ||
7926 | cu_function_cache_t new_funcs; | |
7927 | ||
7928 | for (cu_function_cache_t::iterator func = funcs->begin(); | |
7929 | func != funcs->end(); func++) | |
7930 | { | |
7931 | // optimization: inlines will never be in the symbol table | |
7932 | if (dwarf_func_inline(&func->second) != 0) | |
6e6d9a3b JL |
7933 | { |
7934 | inlined_funcs.insert(func->first); | |
7935 | continue; | |
7936 | } | |
1c6b77e5 | 7937 | |
1ffb8bd1 JS |
7938 | // XXX We may want to make additional efforts to match mangled elf names |
7939 | // to dwarf too. MIPS_linkage_name can help, but that's sometimes | |
7940 | // missing, so we may also need to try matching by address. See also the | |
7941 | // notes about _Z in dwflpp::iterate_over_functions(). | |
7942 | ||
1c6b77e5 JS |
7943 | func_info *fi = sym_table->lookup_symbol(func->first); |
7944 | if (!fi) | |
7945 | continue; | |
7946 | ||
7947 | // iterate over all functions at the same address | |
7948 | symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr); | |
7949 | for (symbol_table::iterator_t it = er.first; it != er.second; ++it) | |
7950 | { | |
7951 | // update this function with the dwarf die | |
7952 | it->second->die = func->second; | |
7953 | ||
7954 | // if this function is a new alias, then | |
7955 | // save it to merge into the function cache | |
7956 | if (it->second != fi) | |
b7478964 | 7957 | new_funcs.insert(make_pair(it->second->name, it->second->die)); |
1c6b77e5 JS |
7958 | } |
7959 | } | |
7960 | ||
7961 | // add all discovered aliases back into the function cache | |
7962 | // NB: this won't replace any names that dwarf may have already found | |
7963 | funcs->insert(new_funcs.begin(), new_funcs.end()); | |
7964 | } | |
7965 | ||
5f0a03a6 JK |
7966 | module_info::~module_info() |
7967 | { | |
7968 | if (sym_table) | |
7969 | delete sym_table; | |
b55bc428 FCE |
7970 | } |
7971 | ||
935447c8 | 7972 | // ------------------------------------------------------------------------ |
888af770 | 7973 | // user-space probes |
935447c8 DS |
7974 | // ------------------------------------------------------------------------ |
7975 | ||
935447c8 | 7976 | |
888af770 | 7977 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> |
935447c8 | 7978 | { |
89ba3085 FCE |
7979 | private: |
7980 | string make_pbm_key (uprobe_derived_probe* p) { | |
cfcab6c7 | 7981 | return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid); |
89ba3085 FCE |
7982 | } |
7983 | ||
cfcab6c7 JS |
7984 | void emit_module_maxuprobes (systemtap_session& s); |
7985 | ||
2b69faaf JS |
7986 | // Using our own utrace-based uprobes |
7987 | void emit_module_utrace_decls (systemtap_session& s); | |
7988 | void emit_module_utrace_init (systemtap_session& s); | |
7989 | void emit_module_utrace_exit (systemtap_session& s); | |
7990 | ||
7991 | // Using the upstream inode-based uprobes | |
7992 | void emit_module_inode_decls (systemtap_session& s); | |
7993 | void emit_module_inode_init (systemtap_session& s); | |
7994 | void emit_module_inode_exit (systemtap_session& s); | |
7995 | ||
3a894f7e JS |
7996 | // Using the dyninst backend (via stapdyn) |
7997 | void emit_module_dyninst_decls (systemtap_session& s); | |
7998 | void emit_module_dyninst_init (systemtap_session& s); | |
7999 | void emit_module_dyninst_exit (systemtap_session& s); | |
8000 | ||
935447c8 | 8001 | public: |
888af770 | 8002 | void emit_module_decls (systemtap_session& s); |
935447c8 DS |
8003 | void emit_module_init (systemtap_session& s); |
8004 | void emit_module_exit (systemtap_session& s); | |
8005 | }; | |
8006 | ||
8007 | ||
888af770 FCE |
8008 | void |
8009 | uprobe_derived_probe::join_group (systemtap_session& s) | |
8010 | { | |
8011 | if (! s.uprobe_derived_probes) | |
8012 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
8013 | s.uprobe_derived_probes->enroll (this); | |
f31a77f5 DS |
8014 | if (s.runtime_usermode_p()) |
8015 | enable_dynprobes(s); | |
8016 | else | |
4441e344 | 8017 | enable_task_finder(s); |
a96d1db0 | 8018 | |
8a03658e | 8019 | // Ask buildrun.cxx to build extra module if needed, and |
d3e959b0 DS |
8020 | // signal staprun to load that module. If we're using the builtin |
8021 | // inode-uprobes, we still need to know that it is required. | |
8a03658e | 8022 | s.need_uprobes = true; |
a96d1db0 DN |
8023 | } |
8024 | ||
888af770 | 8025 | |
c0f84e7b SC |
8026 | void |
8027 | uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const | |
8028 | { | |
8029 | dwarf_derived_probe::getargs(arg_set); | |
8030 | arg_set.insert(arg_set.end(), args.begin(), args.end()); | |
8031 | } | |
8032 | ||
8033 | ||
8034 | void | |
8035 | uprobe_derived_probe::saveargs(int nargs) | |
8036 | { | |
8037 | for (int i = 1; i <= nargs; i++) | |
8038 | args.push_back("$arg" + lex_cast (i) + ":long"); | |
8039 | } | |
8040 | ||
8041 | ||
2865d17a | 8042 | void |
42e38653 | 8043 | uprobe_derived_probe::emit_privilege_assertion (translator_output* o) |
2865d17a DB |
8044 | { |
8045 | // These probes are allowed for unprivileged users, but only in the | |
8046 | // context of processes which they own. | |
8047 | emit_process_owner_assertion (o); | |
8048 | } | |
8049 | ||
8050 | ||
888af770 | 8051 | struct uprobe_builder: public derived_probe_builder |
a96d1db0 | 8052 | { |
888af770 | 8053 | uprobe_builder() {} |
2b69faaf | 8054 | virtual void build(systemtap_session & sess, |
a96d1db0 DN |
8055 | probe * base, |
8056 | probe_point * location, | |
86bf665e | 8057 | literal_map_t const & parameters, |
a96d1db0 DN |
8058 | vector<derived_probe *> & finished_results) |
8059 | { | |
888af770 | 8060 | int64_t process, address; |
a96d1db0 | 8061 | |
2b69faaf | 8062 | if (kernel_supports_inode_uprobes(sess)) |
dc09353a | 8063 | throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]")); |
2b69faaf | 8064 | |
888af770 | 8065 | bool b1 = get_param (parameters, TOK_PROCESS, process); |
ced347a9 | 8066 | (void) b1; |
888af770 | 8067 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 8068 | (void) b2; |
888af770 FCE |
8069 | bool rr = has_null_param (parameters, TOK_RETURN); |
8070 | assert (b1 && b2); // by pattern_root construction | |
a96d1db0 | 8071 | |
0973d815 | 8072 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
a96d1db0 DN |
8073 | } |
8074 | }; | |
8075 | ||
8076 | ||
8077 | void | |
cfcab6c7 | 8078 | uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s) |
a96d1db0 | 8079 | { |
43241c44 FCE |
8080 | // We'll probably need at least this many: |
8081 | unsigned minuprobes = probes.size(); | |
8082 | // .. but we don't want so many that .bss is inflated (PR10507): | |
8083 | unsigned uprobesize = 64; | |
8084 | unsigned maxuprobesmem = 10*1024*1024; // 10 MB | |
8085 | unsigned maxuprobes = maxuprobesmem / uprobesize; | |
8086 | ||
aaf7ffe8 FCE |
8087 | // Let's choose a value on the geometric middle. This should end up |
8088 | // between minuprobes and maxuprobes. It's OK if this number turns | |
8089 | // out to be < minuprobes or > maxuprobes. At worst, we get a | |
8090 | // run-time error of one kind (too few: missed uprobe registrations) | |
8091 | // or another (too many: vmalloc errors at module load time). | |
8092 | unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes); | |
43241c44 | 8093 | |
6d0f3f0c | 8094 | s.op->newline() << "#ifndef MAXUPROBES"; |
43241c44 | 8095 | s.op->newline() << "#define MAXUPROBES " << default_maxuprobes; |
6d0f3f0c | 8096 | s.op->newline() << "#endif"; |
cfcab6c7 JS |
8097 | } |
8098 | ||
8099 | ||
8100 | void | |
8101 | uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s) | |
8102 | { | |
8103 | if (probes.empty()) return; | |
8104 | s.op->newline() << "/* ---- utrace uprobes ---- */"; | |
8105 | // If uprobes isn't in the kernel, pull it in from the runtime. | |
8106 | ||
8107 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; | |
8108 | s.op->newline() << "#include <linux/uprobes.h>"; | |
8109 | s.op->newline() << "#else"; | |
2ba1736a | 8110 | s.op->newline() << "#include \"linux/uprobes/uprobes.h\""; |
cfcab6c7 JS |
8111 | s.op->newline() << "#endif"; |
8112 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; | |
8113 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
8114 | s.op->newline() << "#endif"; | |
8115 | ||
8116 | emit_module_maxuprobes (s); | |
a96d1db0 | 8117 | |
cc52276b | 8118 | // Forward decls |
2ba1736a | 8119 | s.op->newline() << "#include \"linux/uprobes-common.h\""; |
cc52276b | 8120 | |
5e112f92 FCE |
8121 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
8122 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
cc52276b WC |
8123 | // XXX: consider a slab cache or somesuch for stap_uprobes |
8124 | s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];"; | |
6b378b7c | 8125 | s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
a96d1db0 | 8126 | |
89ba3085 FCE |
8127 | s.op->assert_0_indent(); |
8128 | ||
89ba3085 FCE |
8129 | // Assign task-finder numbers as we build up the stap_uprobe_tf table. |
8130 | // This means we process probes[] in two passes. | |
8131 | map <string,unsigned> module_index; | |
8132 | unsigned module_index_ctr = 0; | |
8133 | ||
cc52276b WC |
8134 | // not const since embedded task_finder_target struct changes |
8135 | s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {"; | |
89ba3085 FCE |
8136 | s.op->indent(1); |
8137 | for (unsigned i=0; i<probes.size(); i++) | |
8138 | { | |
8139 | uprobe_derived_probe *p = probes[i]; | |
8140 | string pbmkey = make_pbm_key (p); | |
8141 | if (module_index.find (pbmkey) == module_index.end()) | |
8142 | { | |
8143 | module_index[pbmkey] = module_index_ctr++; | |
8144 | ||
8145 | s.op->newline() << "{"; | |
8146 | // NB: it's essential that make_pbm_key() use all of and | |
8147 | // only the same fields as we're about to emit. | |
8148 | s.op->line() << " .finder={"; | |
1af100fc | 8149 | s.op->line() << " .purpose=\"uprobes\","; |
89ba3085 | 8150 | if (p->pid != 0) |
68910c97 JK |
8151 | s.op->line() << " .pid=" << p->pid << ","; |
8152 | ||
8153 | if (p->section == "") // .statement(addr).absolute | |
8154 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
89ba3085 FCE |
8155 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program |
8156 | { | |
8157 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
8158 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
8159 | } | |
68910c97 | 8160 | else if (p->section != ".absolute") // ET_DYN |
89ba3085 | 8161 | { |
4ad95bbc SC |
8162 | if (p->has_library) |
8163 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
89ba3085 FCE |
8164 | s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, "; |
8165 | s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, "; | |
19d91f6c | 8166 | s.op->line() << " .callback=&stap_uprobe_process_munmap,"; |
89ba3085 | 8167 | } |
89ba3085 | 8168 | s.op->line() << " },"; |
68910c97 JK |
8169 | if (p->module != "") |
8170 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
89ba3085 FCE |
8171 | s.op->line() << " },"; |
8172 | } | |
c57ea854 | 8173 | else |
822a6a3d | 8174 | { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it. |
89ba3085 FCE |
8175 | } |
8176 | s.op->newline(-1) << "};"; | |
8177 | ||
8178 | s.op->assert_0_indent(); | |
8179 | ||
3689db05 SC |
8180 | unsigned pci; |
8181 | for (pci=0; pci<probes.size(); pci++) | |
8182 | { | |
8183 | // List of perf counters used by each probe | |
8184 | // This list is an index into struct stap_perf_probe, | |
8185 | uprobe_derived_probe *p = probes[pci]; | |
698de6cc | 8186 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
8187 | s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {"; |
8188 | for (pcii = p->perf_counter_refs.begin(); | |
8189 | pcii != p->perf_counter_refs.end(); pcii++) | |
8190 | { | |
8191 | map<string, pair<string,derived_probe*> >::iterator it; | |
8192 | unsigned i = 0; | |
8193 | // Find the associated perf.counter probe | |
8194 | for (it=s.perf_counters.begin() ; | |
8195 | it != s.perf_counters.end(); it++, i++) | |
8196 | if ((*it).second.second == (*pcii)) | |
8197 | break; | |
8198 | s.op->line() << lex_cast(i) << ", "; | |
8199 | } | |
8200 | s.op->newline() << "};"; | |
8201 | } | |
8202 | ||
cc52276b WC |
8203 | // NB: read-only structure |
8204 | s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {"; | |
a96d1db0 | 8205 | s.op->indent(1); |
888af770 FCE |
8206 | for (unsigned i =0; i<probes.size(); i++) |
8207 | { | |
8208 | uprobe_derived_probe* p = probes[i]; | |
8209 | s.op->newline() << "{"; | |
89ba3085 FCE |
8210 | string key = make_pbm_key (p); |
8211 | unsigned value = module_index[key]; | |
759e1d76 FCE |
8212 | if (value != 0) |
8213 | s.op->line() << " .tfi=" << value << ","; | |
6b66b9f7 | 8214 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
faea5e16 | 8215 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
4ddb6dd0 | 8216 | |
038c38c6 | 8217 | if (p->sdt_semaphore_addr != 0) |
63b4fd14 | 8218 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" |
038c38c6 | 8219 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; |
4ddb6dd0 | 8220 | |
0d049a1d | 8221 | // XXX: don't bother emit if array is empty |
3689db05 SC |
8222 | s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),"; |
8223 | // List of perf counters used by a probe from above | |
0d049a1d | 8224 | s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ","; |
3689db05 | 8225 | |
4ddb6dd0 JS |
8226 | if (p->has_return) |
8227 | s.op->line() << " .return_p=1,"; | |
888af770 FCE |
8228 | s.op->line() << " },"; |
8229 | } | |
8230 | s.op->newline(-1) << "};"; | |
a96d1db0 | 8231 | |
89ba3085 FCE |
8232 | s.op->assert_0_indent(); |
8233 | ||
48e685da | 8234 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 8235 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
89ba3085 | 8236 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
71db462b | 8237 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe", |
cda141c2 | 8238 | "stp_probe_type_uprobe"); |
0e090c74 | 8239 | s.op->newline() << "if (sup->spec_index < 0 || " |
6e895029 DS |
8240 | << "sup->spec_index >= " << probes.size() << ") {"; |
8241 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 8242 | << "): %s\", sup->spec_index, c->probe_point);"; |
6e895029 DS |
8243 | s.op->newline() << "atomic_dec (&c->busy);"; |
8244 | s.op->newline() << "goto probe_epilogue;"; | |
8245 | s.op->newline(-1) << "}"; | |
d9aed31e | 8246 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 8247 | s.op->newline() << "c->user_mode_p = 1;"; |
6415ddde MW |
8248 | |
8249 | // Make it look like the IP is set as it would in the actual user | |
8250 | // task when calling real probe handler. Reset IP regs on return, so | |
8251 | // we don't confuse uprobes. PR10458 | |
8252 | s.op->newline() << "{"; | |
8253 | s.op->indent(1); | |
d9aed31e | 8254 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
259d54c0 | 8255 | s.op->newline() << "SET_REG_IP(regs, inst->vaddr);"; |
26e63673 | 8256 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 8257 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
8258 | s.op->newline(-1) << "}"; |
8259 | ||
f887a8c9 | 8260 | common_probe_entryfn_epilogue (s, true); |
888af770 | 8261 | s.op->newline(-1) << "}"; |
a96d1db0 | 8262 | |
48e685da | 8263 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 8264 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
89ba3085 | 8265 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
71db462b | 8266 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe", |
cda141c2 | 8267 | "stp_probe_type_uretprobe"); |
6dceb5c9 | 8268 | s.op->newline() << "c->ips.ri = inst;"; |
0e090c74 | 8269 | s.op->newline() << "if (sup->spec_index < 0 || " |
0d5561a5 DS |
8270 | << "sup->spec_index >= " << probes.size() << ") {"; |
8271 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 8272 | << "): %s\", sup->spec_index, c->probe_point);"; |
0d5561a5 DS |
8273 | s.op->newline() << "atomic_dec (&c->busy);"; |
8274 | s.op->newline() << "goto probe_epilogue;"; | |
8275 | s.op->newline(-1) << "}"; | |
8276 | ||
d9aed31e | 8277 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 8278 | s.op->newline() << "c->user_mode_p = 1;"; |
6415ddde MW |
8279 | |
8280 | // Make it look like the IP is set as it would in the actual user | |
8281 | // task when calling real probe handler. Reset IP regs on return, so | |
8282 | // we don't confuse uprobes. PR10458 | |
8283 | s.op->newline() << "{"; | |
8284 | s.op->indent(1); | |
d9aed31e | 8285 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
5e562a69 | 8286 | s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);"; |
26e63673 | 8287 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 8288 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
8289 | s.op->newline(-1) << "}"; |
8290 | ||
f887a8c9 | 8291 | common_probe_entryfn_epilogue (s, true); |
a96d1db0 DN |
8292 | s.op->newline(-1) << "}"; |
8293 | ||
89ba3085 | 8294 | s.op->newline(); |
2ba1736a | 8295 | s.op->newline() << "#include \"linux/uprobes-common.c\""; |
6d0f3f0c | 8296 | s.op->newline(); |
888af770 | 8297 | } |
935447c8 DS |
8298 | |
8299 | ||
888af770 | 8300 | void |
2b69faaf | 8301 | uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s) |
935447c8 | 8302 | { |
888af770 | 8303 | if (probes.empty()) return; |
935447c8 | 8304 | |
2b69faaf | 8305 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
935447c8 | 8306 | |
01b05e2e | 8307 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
8308 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
8309 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
8310 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
8311 | // initialized to zero. This is so that we can use | |
8312 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
8313 | s.op->newline(-1) << "}"; |
8314 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
935447c8 | 8315 | |
89ba3085 FCE |
8316 | // Set up the task_finders |
8317 | s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {"; | |
8318 | s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];"; | |
c57ea854 | 8319 | s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better |
89ba3085 | 8320 | s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);"; |
935447c8 | 8321 | |
5e112f92 FCE |
8322 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any |
8323 | // finders already registered, since mere registration does not | |
8324 | // cause any utrace or memory allocation actions. That happens only | |
8325 | // later, once the task finder engine starts running. So, for a | |
8326 | // partial initialization requiring unwind, we need do nothing. | |
8327 | s.op->newline() << "if (rc) break;"; | |
a7a68293 | 8328 | |
888af770 FCE |
8329 | s.op->newline(-1) << "}"; |
8330 | } | |
d0ea46ce | 8331 | |
d0a7f5a9 | 8332 | |
888af770 | 8333 | void |
2b69faaf | 8334 | uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s) |
888af770 FCE |
8335 | { |
8336 | if (probes.empty()) return; | |
2b69faaf | 8337 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
e56e51c9 | 8338 | |
6d0f3f0c FCE |
8339 | // NB: there is no stap_unregister_task_finder_target call; |
8340 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
8341 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
8342 | // | |
8343 | // This function blocks until all callbacks are completed, so there | |
8344 | // is supposed to be no possibility of any registration-related code starting | |
8345 | // to run in parallel with our shutdown here. So we don't need to protect the | |
8346 | // stap_uprobes[] array with the mutex. | |
d0a7f5a9 | 8347 | |
01b05e2e | 8348 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 | 8349 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
89ba3085 | 8350 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6d0f3f0c | 8351 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd | 8352 | |
8faa1fc5 | 8353 | // PR10655: decrement that ENABLED semaphore |
c116c31b | 8354 | s.op->newline() << "if (sup->sdt_sem_address) {"; |
8faa1fc5 FCE |
8355 | s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size |
8356 | s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);"; | |
8357 | s.op->newline() << "struct task_struct *tsk;"; | |
8358 | s.op->newline() << "rcu_read_lock();"; | |
6846cfc8 | 8359 | |
86229a55 DS |
8360 | // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes |
8361 | // that the pid is always in the global namespace, not in any | |
8362 | // private namespace. | |
8faa1fc5 | 8363 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)"; |
86229a55 DS |
8364 | // We'd like to call find_task_by_pid_ns() here, but it isn't |
8365 | // exported. So, we call what it calls... | |
8366 | s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);"; | |
8faa1fc5 FCE |
8367 | s.op->newline() << "#else"; |
8368 | s.op->newline() << " tsk = find_task_by_pid (pid);"; | |
8369 | s.op->newline() << "#endif /* 2.6.24 */"; | |
8faa1fc5 FCE |
8370 | |
8371 | s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching | |
3c5b8e2b | 8372 | s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {"; |
63b4fd14 | 8373 | s.op->newline(1) << "sdt_semaphore --;"; |
903b9fcd | 8374 | s.op->newline() << "#ifdef DEBUG_UPROBES"; |
c116c31b | 8375 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);"; |
903b9fcd | 8376 | s.op->newline() << "#endif"; |
3c5b8e2b | 8377 | s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);"; |
93c84191 | 8378 | s.op->newline(-1) << "}"; |
8faa1fc5 FCE |
8379 | // XXX: need to analyze possibility of race condition |
8380 | s.op->newline(-1) << "}"; | |
8381 | s.op->newline() << "rcu_read_unlock();"; | |
8382 | s.op->newline(-1) << "}"; | |
6846cfc8 | 8383 | |
3568f1dd FCE |
8384 | s.op->newline() << "if (sups->return_p) {"; |
8385 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
89ba3085 | 8386 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);"; |
3568f1dd | 8387 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
8388 | // NB: PR6829 does not change that we still need to unregister at |
8389 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
8390 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
8391 | s.op->newline(-1) << "} else {"; | |
8392 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
8faa1fc5 | 8393 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);"; |
3568f1dd FCE |
8394 | s.op->newline() << "#endif"; |
8395 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
8396 | s.op->newline(-1) << "}"; | |
935447c8 | 8397 | |
6d0f3f0c | 8398 | s.op->newline() << "sup->spec_index = -1;"; |
935447c8 | 8399 | |
3568f1dd FCE |
8400 | // XXX: uprobe missed counts? |
8401 | ||
6d0f3f0c | 8402 | s.op->newline(-1) << "}"; |
935447c8 | 8403 | |
5e112f92 | 8404 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; |
935447c8 DS |
8405 | } |
8406 | ||
2b69faaf JS |
8407 | |
8408 | void | |
8409 | uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s) | |
8410 | { | |
8411 | if (probes.empty()) return; | |
8412 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 8413 | emit_module_maxuprobes (s); |
2ba1736a | 8414 | s.op->newline() << "#include \"linux/uprobes-inode.c\""; |
2b69faaf JS |
8415 | |
8416 | // Write the probe handler. | |
79af55c3 JS |
8417 | s.op->newline() << "static int stapiu_probe_handler " |
8418 | << "(struct stapiu_consumer *sup, struct pt_regs *regs) {"; | |
8419 | s.op->newline(1); | |
2dbbd473 JS |
8420 | |
8421 | // Since we're sharing the entry function, we have to dynamically choose the probe_type | |
8422 | string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)"; | |
71db462b | 8423 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe", |
2dbbd473 JS |
8424 | probe_type); |
8425 | ||
3bff6634 | 8426 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 8427 | s.op->newline() << "c->user_mode_p = 1;"; |
497cfcb2 | 8428 | // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c |
2b69faaf | 8429 | s.op->newline() << "(*sup->probe->ph) (c);"; |
39e63bae | 8430 | |
f887a8c9 | 8431 | common_probe_entryfn_epilogue (s, true); |
2b69faaf JS |
8432 | s.op->newline() << "return 0;"; |
8433 | s.op->newline(-1) << "}"; | |
8434 | s.op->assert_0_indent(); | |
8435 | ||
8436 | // Index of all the modules for which we need inodes. | |
8437 | map<string, unsigned> module_index; | |
8438 | unsigned module_index_ctr = 0; | |
8439 | ||
8440 | // Discover and declare targets for each unique path. | |
cfcab6c7 | 8441 | s.op->newline() << "static struct stapiu_target " |
2b69faaf JS |
8442 | << "stap_inode_uprobe_targets[] = {"; |
8443 | s.op->indent(1); | |
8444 | for (unsigned i=0; i<probes.size(); i++) | |
8445 | { | |
8446 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
8447 | const string key = make_pbm_key(p); |
8448 | if (module_index.find (key) == module_index.end()) | |
2b69faaf | 8449 | { |
cfcab6c7 JS |
8450 | module_index[key] = module_index_ctr++; |
8451 | s.op->newline() << "{"; | |
8452 | s.op->line() << " .finder={"; | |
b78a0fbb | 8453 | s.op->line() << " .purpose=\"inode-uprobes\","; |
cfcab6c7 JS |
8454 | if (p->pid != 0) |
8455 | s.op->line() << " .pid=" << p->pid << ","; | |
8456 | ||
8457 | if (p->section == "") // .statement(addr).absolute XXX? | |
8458 | s.op->line() << " .callback=&stapiu_process_found,"; | |
8459 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program | |
8460 | { | |
8461 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
8462 | s.op->line() << " .callback=&stapiu_process_found,"; | |
8463 | } | |
8464 | else if (p->section != ".absolute") // ET_DYN | |
8465 | { | |
8466 | if (p->has_library) | |
8467 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
8468 | s.op->line() << " .mmap_callback=&stapiu_mmap_found, "; | |
8469 | s.op->line() << " .munmap_callback=&stapiu_munmap_found, "; | |
8470 | s.op->line() << " .callback=&stapiu_process_munmap,"; | |
8471 | } | |
8472 | s.op->line() << " },"; | |
8473 | s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ","; | |
8474 | s.op->line() << " },"; | |
2b69faaf JS |
8475 | } |
8476 | } | |
8477 | s.op->newline(-1) << "};"; | |
8478 | s.op->assert_0_indent(); | |
8479 | ||
8480 | // Declare the actual probes. | |
3689db05 SC |
8481 | unsigned pci; |
8482 | for (pci=0; pci<probes.size(); pci++) | |
8483 | { | |
8484 | // List of perf counters used by each probe | |
8485 | // This list is an index into struct stap_perf_probe, | |
8486 | uprobe_derived_probe *p = probes[pci]; | |
698de6cc | 8487 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
8488 | s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {"; |
8489 | for (pcii = p->perf_counter_refs.begin(); | |
8490 | pcii != p->perf_counter_refs.end(); pcii++) | |
8491 | { | |
8492 | map<string, pair<string,derived_probe*> >::iterator it; | |
8493 | unsigned i = 0; | |
8494 | // Find the associated perf.counter probe | |
4fa83377 SC |
8495 | for (it=s.perf_counters.begin() ; |
8496 | it != s.perf_counters.end(); it++, i++) | |
3689db05 SC |
8497 | if ((*it).second.second == (*pcii)) |
8498 | break; | |
8499 | s.op->line() << lex_cast(i) << ", "; | |
8500 | } | |
8501 | s.op->newline() << "};"; | |
8502 | } | |
8503 | ||
cfcab6c7 | 8504 | s.op->newline() << "static struct stapiu_consumer " |
2b69faaf JS |
8505 | << "stap_inode_uprobe_consumers[] = {"; |
8506 | s.op->indent(1); | |
8507 | for (unsigned i=0; i<probes.size(); i++) | |
8508 | { | |
8509 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
8510 | unsigned index = module_index[make_pbm_key(p)]; |
8511 | s.op->newline() << "{"; | |
79af55c3 JS |
8512 | if (p->has_return) |
8513 | s.op->line() << " .return_p=1,"; | |
cfcab6c7 JS |
8514 | s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],"; |
8515 | s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,"; | |
8516 | if (p->sdt_semaphore_addr) | |
8517 | s.op->line() << " .sdt_sem_offset=(loff_t)0x" | |
8518 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; | |
0d049a1d | 8519 | // XXX: don't bother emit if array is empty |
3689db05 SC |
8520 | s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),"; |
8521 | // List of perf counters used by a probe from above | |
0d049a1d | 8522 | s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ","; |
cfcab6c7 JS |
8523 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
8524 | s.op->line() << " },"; | |
2b69faaf JS |
8525 | } |
8526 | s.op->newline(-1) << "};"; | |
8527 | s.op->assert_0_indent(); | |
8528 | } | |
8529 | ||
8530 | ||
8531 | void | |
8532 | uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s) | |
8533 | { | |
8534 | if (probes.empty()) return; | |
8535 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
b0f614a0 DS |
8536 | // Let stapiu_init() handle reporting errors by setting probe_point |
8537 | // to NULL. | |
8538 | s.op->newline() << "probe_point = NULL;"; | |
cfcab6c7 | 8539 | s.op->newline() << "rc = stapiu_init (" |
2b69faaf JS |
8540 | << "stap_inode_uprobe_targets, " |
8541 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
8542 | << "stap_inode_uprobe_consumers, " | |
8543 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
8544 | } | |
8545 | ||
8546 | ||
8547 | void | |
8548 | uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s) | |
8549 | { | |
8550 | if (probes.empty()) return; | |
8551 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 8552 | s.op->newline() << "stapiu_exit (" |
2b69faaf JS |
8553 | << "stap_inode_uprobe_targets, " |
8554 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
8555 | << "stap_inode_uprobe_consumers, " | |
8556 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
8557 | } | |
8558 | ||
8559 | ||
3a894f7e JS |
8560 | void |
8561 | uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s) | |
8562 | { | |
8563 | if (probes.empty()) return; | |
8564 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
8565 | emit_module_maxuprobes (s); | |
e00f3fb7 | 8566 | s.op->newline() << "#include \"dyninst/uprobes.h\""; |
3a894f7e | 8567 | |
f31a77f5 DS |
8568 | // Let the dynprobe_derived_probe_group handle outputting targets |
8569 | // and probes. This allows us to merge different types of probes. | |
8570 | s.op->newline() << "static struct stapdu_probe stapdu_probes[];"; | |
8571 | for (unsigned i = 0; i < probes.size(); i++) | |
3a894f7e JS |
8572 | { |
8573 | uprobe_derived_probe *p = probes[i]; | |
e00f3fb7 | 8574 | |
f31a77f5 DS |
8575 | dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr, |
8576 | (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"), | |
8577 | common_probe_init(p)); | |
3a894f7e | 8578 | } |
874d38bf JS |
8579 | // loc2c-generated code assumes pt_regs are available, so use this to make |
8580 | // sure we always have *something* for it to dereference... | |
f31a77f5 | 8581 | s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;"; |
e00f3fb7 | 8582 | |
3a894f7e JS |
8583 | // Write the probe handler. |
8584 | // NB: not static, so dyninst can find it | |
8585 | s.op->newline() << "int enter_dyninst_uprobe " | |
8586 | << "(uint64_t index, struct pt_regs *regs) {"; | |
e00f3fb7 | 8587 | s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];"; |
92b97104 JS |
8588 | |
8589 | // Since we're sharing the entry function, we have to dynamically choose the probe_type | |
8590 | string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?" | |
8591 | " stp_probe_type_uretprobe : stp_probe_type_uprobe)"; | |
71db462b | 8592 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe", |
92b97104 JS |
8593 | probe_type); |
8594 | ||
874d38bf | 8595 | s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;"; |
e04b5d74 | 8596 | s.op->newline() << "c->user_mode_p = 1;"; |
3a894f7e JS |
8597 | // XXX: once we have regs, check how dyninst sets the IP |
8598 | // XXX: the way that dyninst rewrites stuff is probably going to be | |
8599 | // ... very confusing to our backtracer (at least if we stay in process) | |
8600 | s.op->newline() << "(*sup->probe->ph) (c);"; | |
f887a8c9 | 8601 | common_probe_entryfn_epilogue (s, true); |
3a894f7e JS |
8602 | s.op->newline() << "return 0;"; |
8603 | s.op->newline(-1) << "}"; | |
3debb935 | 8604 | s.op->newline() << "#include \"dyninst/uprobes-regs.c\""; |
3a894f7e JS |
8605 | s.op->assert_0_indent(); |
8606 | } | |
8607 | ||
8608 | ||
8609 | void | |
8610 | uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s) | |
8611 | { | |
8612 | if (probes.empty()) return; | |
8613 | ||
8614 | /* stapdyn handles the dirty work via dyninst */ | |
8615 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
8616 | s.op->newline() << "/* this section left intentionally blank */"; | |
8617 | } | |
8618 | ||
8619 | ||
8620 | void | |
8621 | uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s) | |
8622 | { | |
8623 | if (probes.empty()) return; | |
8624 | ||
8625 | /* stapdyn handles the dirty work via dyninst */ | |
8626 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
8627 | s.op->newline() << "/* this section left intentionally blank */"; | |
8628 | } | |
8629 | ||
8630 | ||
2b69faaf JS |
8631 | void |
8632 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8633 | { | |
ac3af990 | 8634 | if (s.runtime_usermode_p()) |
4441e344 JS |
8635 | emit_module_dyninst_decls (s); |
8636 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
8637 | emit_module_inode_decls (s); |
8638 | else | |
8639 | emit_module_utrace_decls (s); | |
8640 | } | |
8641 | ||
8642 | ||
8643 | void | |
8644 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
8645 | { | |
ac3af990 | 8646 | if (s.runtime_usermode_p()) |
4441e344 JS |
8647 | emit_module_dyninst_init (s); |
8648 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
8649 | emit_module_inode_init (s); |
8650 | else | |
8651 | emit_module_utrace_init (s); | |
8652 | } | |
8653 | ||
8654 | ||
8655 | void | |
8656 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8657 | { | |
ac3af990 | 8658 | if (s.runtime_usermode_p()) |
4441e344 JS |
8659 | emit_module_dyninst_exit (s); |
8660 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
8661 | emit_module_inode_exit (s); |
8662 | else | |
8663 | emit_module_utrace_exit (s); | |
8664 | } | |
8665 | ||
8666 | ||
e6fe60e7 AM |
8667 | // ------------------------------------------------------------------------ |
8668 | // Kprobe derived probes | |
8669 | // ------------------------------------------------------------------------ | |
8670 | ||
4627ed58 | 8671 | static const string TOK_KPROBE("kprobe"); |
935447c8 | 8672 | |
bae55db9 | 8673 | struct kprobe_derived_probe: public derived_probe |
d0ea46ce | 8674 | { |
23dc94f6 DS |
8675 | kprobe_derived_probe (systemtap_session& sess, |
8676 | vector<derived_probe *> & results, | |
8677 | probe *base, | |
bae55db9 JS |
8678 | probe_point *location, |
8679 | const string& name, | |
8680 | int64_t stmt_addr, | |
8681 | bool has_return, | |
8682 | bool has_statement, | |
8683 | bool has_maxactive, | |
b642c901 SC |
8684 | bool has_path, |
8685 | bool has_library, | |
8686 | long maxactive_val, | |
8687 | const string& path, | |
8688 | const string& library | |
bae55db9 JS |
8689 | ); |
8690 | string symbol_name; | |
8691 | Dwarf_Addr addr; | |
8692 | bool has_return; | |
8693 | bool has_statement; | |
8694 | bool has_maxactive; | |
b642c901 SC |
8695 | bool has_path; |
8696 | bool has_library; | |
bae55db9 | 8697 | long maxactive_val; |
b642c901 SC |
8698 | string path; |
8699 | string library; | |
bae55db9 JS |
8700 | bool access_var; |
8701 | void printsig (std::ostream &o) const; | |
8702 | void join_group (systemtap_session& s); | |
8703 | }; | |
d0ea46ce | 8704 | |
bae55db9 JS |
8705 | struct kprobe_derived_probe_group: public derived_probe_group |
8706 | { | |
8707 | private: | |
8708 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
8709 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
d0ea46ce | 8710 | |
bae55db9 JS |
8711 | public: |
8712 | void enroll (kprobe_derived_probe* probe); | |
8713 | void emit_module_decls (systemtap_session& s); | |
8714 | void emit_module_init (systemtap_session& s); | |
8715 | void emit_module_exit (systemtap_session& s); | |
8716 | }; | |
d0ea46ce | 8717 | |
23dc94f6 DS |
8718 | struct kprobe_var_expanding_visitor: public var_expanding_visitor |
8719 | { | |
8720 | systemtap_session& sess; | |
8721 | block *add_block; | |
8722 | block *add_call_probe; // synthesized from .return probes with saved $vars | |
8723 | bool add_block_tid, add_call_probe_tid; | |
bd5b25e1 | 8724 | bool has_return; |
23dc94f6 | 8725 | |
bd5b25e1 | 8726 | kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return): |
23dc94f6 | 8727 | sess(sess), add_block(NULL), add_call_probe(NULL), |
bd5b25e1 JS |
8728 | add_block_tid(false), add_call_probe_tid(false), |
8729 | has_return(has_return) {} | |
23dc94f6 DS |
8730 | |
8731 | void visit_entry_op (entry_op* e); | |
8732 | }; | |
8733 | ||
8734 | ||
8735 | kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess, | |
8736 | vector<derived_probe *> & results, | |
8737 | probe *base, | |
e6fe60e7 | 8738 | probe_point *location, |
b6371390 | 8739 | const string& name, |
e6fe60e7 | 8740 | int64_t stmt_addr, |
b6371390 JS |
8741 | bool has_return, |
8742 | bool has_statement, | |
8743 | bool has_maxactive, | |
b642c901 SC |
8744 | bool has_path, |
8745 | bool has_library, | |
8746 | long maxactive_val, | |
8747 | const string& path, | |
8748 | const string& library | |
b6371390 | 8749 | ): |
4c5d1300 | 8750 | derived_probe (base, location, true /* .components soon rewritten */ ), |
e6fe60e7 | 8751 | symbol_name (name), addr (stmt_addr), |
b6371390 | 8752 | has_return (has_return), has_statement (has_statement), |
b642c901 SC |
8753 | has_maxactive (has_maxactive), has_path (has_path), |
8754 | has_library (has_library), | |
8755 | maxactive_val (maxactive_val), | |
8756 | path (path), library (library) | |
e6fe60e7 AM |
8757 | { |
8758 | this->tok = base->tok; | |
8759 | this->access_var = false; | |
d0ea46ce | 8760 | |
e6fe60e7 AM |
8761 | #ifndef USHRT_MAX |
8762 | #define USHRT_MAX 32767 | |
8763 | #endif | |
d0ea46ce | 8764 | |
46856d8d JS |
8765 | // Expansion of $target variables in the probe body produces an error during |
8766 | // translate phase, since we're not using debuginfo | |
d0ea46ce | 8767 | |
e6fe60e7 | 8768 | vector<probe_point::component*> comps; |
46856d8d | 8769 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 8770 | |
46856d8d JS |
8771 | if (has_statement) |
8772 | { | |
9ea68eb9 JS |
8773 | comps.push_back (new probe_point::component(TOK_STATEMENT, |
8774 | new literal_number(addr, true))); | |
46856d8d JS |
8775 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); |
8776 | } | |
8777 | else | |
8778 | { | |
8779 | size_t pos = name.find(':'); | |
8780 | if (pos != string::npos) | |
d0ea46ce | 8781 | { |
46856d8d JS |
8782 | string module = name.substr(0, pos); |
8783 | string function = name.substr(pos + 1); | |
8784 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
8785 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
8786 | } | |
8787 | else | |
8788 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 8789 | } |
d0ea46ce | 8790 | |
b6371390 JS |
8791 | if (has_return) |
8792 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
8793 | if (has_maxactive) | |
8794 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d0ea46ce | 8795 | |
bd5b25e1 | 8796 | kprobe_var_expanding_visitor v (sess, has_return); |
23dc94f6 DS |
8797 | v.replace (this->body); |
8798 | ||
8799 | // If during target-variable-expanding the probe, we added a new block | |
8800 | // of code, add it to the start of the probe. | |
8801 | if (v.add_block) | |
8802 | this->body = new block(v.add_block, this->body); | |
8803 | ||
8804 | // If when target-variable-expanding the probe, we need to | |
8805 | // synthesize a sibling function-entry probe. We don't go through | |
8806 | // the whole probe derivation business (PR10642) that could lead to | |
8807 | // wildcard/alias resolution, or for that dwarf-induced duplication. | |
8808 | if (v.add_call_probe) | |
8809 | { | |
8810 | assert (has_return); | |
8811 | ||
8812 | // We temporarily replace base. | |
8813 | statement* old_body = base->body; | |
8814 | base->body = v.add_call_probe; | |
8815 | ||
8816 | derived_probe *entry_handler | |
8817 | = new kprobe_derived_probe (sess, results, base, location, name, 0, | |
8818 | false, has_statement, has_maxactive, | |
8819 | has_path, has_library, maxactive_val, | |
8820 | path, library); | |
8821 | results.push_back (entry_handler); | |
8822 | ||
8823 | base->body = old_body; | |
8824 | } | |
8825 | ||
e6fe60e7 AM |
8826 | this->sole_location()->components = comps; |
8827 | } | |
d0ea46ce | 8828 | |
e6fe60e7 AM |
8829 | void kprobe_derived_probe::printsig (ostream& o) const |
8830 | { | |
8831 | sole_location()->print (o); | |
8832 | o << " /* " << " name = " << symbol_name << "*/"; | |
8833 | printsig_nested (o); | |
8834 | } | |
d0ea46ce | 8835 | |
e6fe60e7 AM |
8836 | void kprobe_derived_probe::join_group (systemtap_session& s) |
8837 | { | |
d0ea46ce | 8838 | |
e6fe60e7 AM |
8839 | if (! s.kprobe_derived_probes) |
8840 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
8841 | s.kprobe_derived_probes->enroll (this); | |
d0ea46ce | 8842 | |
e6fe60e7 | 8843 | } |
d0ea46ce | 8844 | |
e6fe60e7 AM |
8845 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) |
8846 | { | |
8847 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
8848 | // probes of same symbol should share single kprobe/kretprobe | |
8849 | } | |
d0ea46ce | 8850 | |
e6fe60e7 AM |
8851 | void |
8852 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8853 | { | |
8854 | if (probes_by_module.empty()) return; | |
d0ea46ce | 8855 | |
e6fe60e7 | 8856 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; |
d0ea46ce | 8857 | |
e6fe60e7 AM |
8858 | // Warn of misconfigured kernels |
8859 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
8860 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
8861 | s.op->newline() << "#endif"; | |
8862 | s.op->newline(); | |
d0ea46ce | 8863 | |
f07c3b68 | 8864 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 8865 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
8866 | s.op->newline() << "#endif"; |
8867 | ||
e6fe60e7 | 8868 | // Forward declare the master entry functions |
88747011 | 8869 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 8870 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 8871 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 | 8872 | s.op->line() << " struct pt_regs *regs);"; |
d0ea46ce | 8873 | |
e6fe60e7 AM |
8874 | // Emit an array of kprobe/kretprobe pointers |
8875 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
c9116e99 | 8876 | s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];"; |
e6fe60e7 | 8877 | s.op->newline() << "#endif"; |
d0ea46ce | 8878 | |
e6fe60e7 | 8879 | // Emit the actual probe list. |
d0ea46ce | 8880 | |
e6fe60e7 AM |
8881 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; |
8882 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
8883 | s.op->newline() << "#ifdef __ia64__"; | |
8884 | s.op->newline() << "struct kprobe dummy;"; | |
8885 | s.op->newline() << "#endif"; | |
8886 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
8887 | // NB: bss! | |
d0ea46ce | 8888 | |
e6fe60e7 AM |
8889 | s.op->newline() << "static struct stap_dwarfless_probe {"; |
8890 | s.op->newline(1) << "const unsigned return_p:1;"; | |
8891 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 8892 | s.op->newline() << "const unsigned optional_p:1;"; |
e6fe60e7 AM |
8893 | s.op->newline() << "unsigned registered_p:1;"; |
8894 | s.op->newline() << "const unsigned short maxactive_val;"; | |
935447c8 | 8895 | |
e6fe60e7 AM |
8896 | // Function Names are mostly small and uniform enough to justify putting |
8897 | // char[MAX]'s into the array instead of relocated char*'s. | |
935447c8 | 8898 | |
faea5e16 JS |
8899 | size_t symbol_string_name_max = 0; |
8900 | size_t symbol_string_name_tot = 0; | |
e6fe60e7 | 8901 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
6270adc1 | 8902 | { |
e6fe60e7 AM |
8903 | kprobe_derived_probe* p = it->second; |
8904 | #define DOIT(var,expr) do { \ | |
8905 | size_t var##_size = (expr) + 1; \ | |
8906 | var##_max = max (var##_max, var##_size); \ | |
8907 | var##_tot += var##_size; } while (0) | |
e6fe60e7 AM |
8908 | DOIT(symbol_string_name, p->symbol_name.size()); |
8909 | #undef DOIT | |
6270adc1 MH |
8910 | } |
8911 | ||
e6fe60e7 AM |
8912 | #define CALCIT(var) \ |
8913 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
935447c8 | 8914 | |
e6fe60e7 AM |
8915 | CALCIT(symbol_string); |
8916 | #undef CALCIT | |
6270adc1 | 8917 | |
bd659351 | 8918 | s.op->newline() << "unsigned long address;"; |
7c3e97f4 | 8919 | s.op->newline() << "const struct stap_probe * const probe;"; |
e6fe60e7 AM |
8920 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; |
8921 | s.op->indent(1); | |
6270adc1 | 8922 | |
e6fe60e7 AM |
8923 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
8924 | { | |
8925 | kprobe_derived_probe* p = it->second; | |
8926 | s.op->newline() << "{"; | |
8927 | if (p->has_return) | |
8928 | s.op->line() << " .return_p=1,"; | |
6270adc1 | 8929 | |
e6fe60e7 AM |
8930 | if (p->has_maxactive) |
8931 | { | |
8932 | s.op->line() << " .maxactive_p=1,"; | |
8933 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
8934 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
8935 | } | |
6270adc1 | 8936 | |
b350f56b JS |
8937 | if (p->locations[0]->optional) |
8938 | s.op->line() << " .optional_p=1,"; | |
8939 | ||
e6fe60e7 | 8940 | if (p->has_statement) |
c8d9d15e | 8941 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 8942 | else |
c8d9d15e | 8943 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
5d67b47c | 8944 | |
faea5e16 | 8945 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
e6fe60e7 | 8946 | s.op->line() << " },"; |
935447c8 DS |
8947 | } |
8948 | ||
e6fe60e7 | 8949 | s.op->newline(-1) << "};"; |
5d67b47c | 8950 | |
e6fe60e7 AM |
8951 | // Emit the kprobes callback function |
8952 | s.op->newline(); | |
88747011 | 8953 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
8954 | s.op->line() << " struct pt_regs *regs) {"; |
8955 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
8956 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8957 | // Check that the index is plausible | |
8958 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8959 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8960 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8961 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8962 | s.op->line() << "];"; | |
71db462b | 8963 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 8964 | "stp_probe_type_kprobe"); |
d9aed31e | 8965 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
8966 | |
8967 | // Make it look like the IP is set as it wouldn't have been replaced | |
8968 | // by a breakpoint instruction when calling real probe handler. Reset | |
8969 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
8970 | s.op->newline() << "{"; | |
8971 | s.op->indent(1); | |
d9aed31e | 8972 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 8973 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 8974 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 8975 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
8976 | s.op->newline(-1) << "}"; |
8977 | ||
f887a8c9 | 8978 | common_probe_entryfn_epilogue (s, true); |
e6fe60e7 AM |
8979 | s.op->newline() << "return 0;"; |
8980 | s.op->newline(-1) << "}"; | |
935447c8 | 8981 | |
e6fe60e7 AM |
8982 | // Same for kretprobes |
8983 | s.op->newline(); | |
88747011 | 8984 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
8985 | s.op->line() << " struct pt_regs *regs) {"; |
8986 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
935447c8 | 8987 | |
e6fe60e7 AM |
8988 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
8989 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8990 | // Check that the index is plausible | |
8991 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8992 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8993 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8994 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8995 | s.op->line() << "];"; | |
935447c8 | 8996 | |
71db462b | 8997 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 8998 | "stp_probe_type_kretprobe"); |
d9aed31e | 8999 | s.op->newline() << "c->kregs = regs;"; |
6dceb5c9 | 9000 | s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic |
6415ddde MW |
9001 | |
9002 | // Make it look like the IP is set as it wouldn't have been replaced | |
9003 | // by a breakpoint instruction when calling real probe handler. Reset | |
9004 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
9005 | s.op->newline() << "{"; | |
9006 | s.op->indent(1); | |
d9aed31e | 9007 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 9008 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
26e63673 | 9009 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 9010 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
9011 | s.op->newline(-1) << "}"; |
9012 | ||
f887a8c9 | 9013 | common_probe_entryfn_epilogue (s, true); |
e6fe60e7 AM |
9014 | s.op->newline() << "return 0;"; |
9015 | s.op->newline(-1) << "}"; | |
bd659351 | 9016 | |
03a4ec63 | 9017 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
bd659351 MW |
9018 | s.op->newline() << "static int kprobe_resolve(void *data, const char *name,"; |
9019 | s.op->newline() << " struct module *owner,"; | |
9020 | s.op->newline() << " unsigned long val) {"; | |
9021 | s.op->newline(1) << "int i;"; | |
fc1d2aa2 MW |
9022 | s.op->newline() << "int *p = (int *) data;"; |
9023 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() | |
9024 | << " && *p > 0; i++) {"; | |
bd659351 | 9025 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
8c272819 YW |
9026 | s.op->newline() << "if (! sdp->address) {"; |
9027 | s.op->indent(1); | |
9028 | s.op->newline() << "const char *colon;"; | |
9029 | s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {"; | |
9030 | s.op->indent(1); | |
9031 | s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))"; | |
9032 | s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)"; | |
9033 | s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {"; | |
9034 | s.op->newline(1) << "sdp->address = val;"; | |
9035 | s.op->newline() << "(*p)--;"; | |
9036 | s.op->newline(-1) << "}"; | |
9037 | s.op->newline(-1) << "}"; | |
9038 | s.op->newline() << "else {"; | |
fc1d2aa2 | 9039 | s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {"; |
bd659351 | 9040 | s.op->newline(1) << "sdp->address = val;"; |
fc1d2aa2 MW |
9041 | s.op->newline() << "(*p)--;"; |
9042 | s.op->newline(-1) << "}"; | |
8c272819 YW |
9043 | s.op->newline(-1) << "}"; |
9044 | s.op->newline(-1) << "}"; | |
9045 | s.op->newline(-1) << "}"; | |
fc1d2aa2 | 9046 | s.op->newline() << "return (p > 0) ? 0 : -1;"; |
bd659351 | 9047 | s.op->newline(-1) << "}"; |
03a4ec63 | 9048 | s.op->newline() << "#endif"; |
935447c8 DS |
9049 | } |
9050 | ||
e6fe60e7 | 9051 | |
6270adc1 | 9052 | void |
e6fe60e7 | 9053 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) |
6270adc1 | 9054 | { |
03a4ec63 | 9055 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
fc1d2aa2 MW |
9056 | s.op->newline() << "{"; |
9057 | s.op->newline(1) << "int p = 0;"; | |
9058 | s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {"; | |
9059 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
9060 | s.op->newline() << "if (! sdp->address)"; | |
9061 | s.op->newline(1) << "p++;"; | |
9062 | s.op->newline(-2) << "}"; | |
9063 | s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);"; | |
9064 | s.op->newline(-1) << "}"; | |
03a4ec63 | 9065 | s.op->newline() << "#endif"; |
bd659351 | 9066 | |
e6fe60e7 | 9067 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 9068 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 9069 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e | 9070 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
03a4ec63 MW |
9071 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; |
9072 | ||
9073 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; | |
bd659351 MW |
9074 | s.op->newline() << "if (! addr) {"; |
9075 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
9319b767 MW |
9076 | s.op->newline() << "if (!sdp->optional_p)"; |
9077 | s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);"; | |
9078 | s.op->newline(-1) << "continue;"; | |
bd659351 | 9079 | s.op->newline(-1) << "}"; |
03a4ec63 MW |
9080 | s.op->newline() << "#endif"; |
9081 | ||
26e63673 | 9082 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
e6fe60e7 | 9083 | s.op->newline() << "if (sdp->return_p) {"; |
c8d9d15e | 9084 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
766cee5f | 9085 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 9086 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 9087 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
9088 | s.op->newline() << "if (sdp->maxactive_p) {"; |
9089 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
9090 | s.op->newline(-1) << "} else {"; | |
f07c3b68 | 9091 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
e6fe60e7 | 9092 | s.op->newline(-1) << "}"; |
88747011 | 9093 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
9094 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
9095 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 9096 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
766cee5f | 9097 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 9098 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
766cee5f | 9099 | s.op->newline() << "#endif"; |
c8d9d15e | 9100 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
e6fe60e7 AM |
9101 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
9102 | s.op->newline() << "if (rc == 0) {"; | |
9103 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
9104 | s.op->newline() << "if (rc != 0)"; | |
9105 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
9106 | s.op->newline(-2) << "}"; | |
9107 | s.op->newline() << "#else"; | |
9108 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
9109 | s.op->newline() << "#endif"; | |
9110 | s.op->newline(-1) << "} else {"; | |
9111 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 9112 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
766cee5f | 9113 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 9114 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 9115 | s.op->newline() << "#endif"; |
88747011 | 9116 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 9117 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 9118 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e | 9119 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
766cee5f | 9120 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 9121 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; |
766cee5f | 9122 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
9123 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
9124 | s.op->newline() << "if (rc == 0) {"; | |
9125 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
9126 | s.op->newline() << "if (rc != 0)"; | |
9127 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
9128 | s.op->newline(-2) << "}"; | |
9129 | s.op->newline() << "#else"; | |
9130 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
9131 | s.op->newline() << "#endif"; | |
9132 | s.op->newline(-1) << "}"; | |
9133 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
9134 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 9135 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 9136 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);"; |
b350f56b | 9137 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
e6fe60e7 AM |
9138 | // XXX: shall we increment numskipped? |
9139 | s.op->newline(-1) << "}"; | |
6270adc1 | 9140 | |
e6fe60e7 AM |
9141 | s.op->newline() << "else sdp->registered_p = 1;"; |
9142 | s.op->newline(-1) << "}"; // for loop | |
6270adc1 MH |
9143 | } |
9144 | ||
b4be7cbc | 9145 | |
e6fe60e7 AM |
9146 | void |
9147 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
935447c8 | 9148 | { |
e6fe60e7 AM |
9149 | //Unregister kprobes by batch interfaces. |
9150 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
9151 | s.op->newline() << "j = 0;"; | |
9152 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
9153 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
9154 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
9155 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
9156 | s.op->newline() << "if (!sdp->return_p)"; | |
c9116e99 | 9157 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;"; |
e6fe60e7 | 9158 | s.op->newline(-2) << "}"; |
c9116e99 | 9159 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
9160 | s.op->newline() << "j = 0;"; |
9161 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
9162 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
9163 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
9164 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
9165 | s.op->newline() << "if (sdp->return_p)"; | |
c9116e99 | 9166 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;"; |
e6fe60e7 | 9167 | s.op->newline(-2) << "}"; |
c9116e99 | 9168 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
9169 | s.op->newline() << "#ifdef __ia64__"; |
9170 | s.op->newline() << "j = 0;"; | |
9171 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
9172 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
9173 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
9174 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
c9116e99 | 9175 | s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;"; |
e6fe60e7 | 9176 | s.op->newline(-1) << "}"; |
c9116e99 | 9177 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
9178 | s.op->newline() << "#endif"; |
9179 | s.op->newline() << "#endif"; | |
3e3bd7b6 | 9180 | |
e6fe60e7 AM |
9181 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
9182 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
9183 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
9184 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
9185 | s.op->newline() << "if (sdp->return_p) {"; | |
9186 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
9187 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
9188 | s.op->newline() << "#endif"; | |
065d5567 | 9189 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
e6fe60e7 AM |
9190 | s.op->newline() << "#ifdef STP_TIMING"; |
9191 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 9192 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
e6fe60e7 | 9193 | s.op->newline(-1) << "#endif"; |
065d5567 | 9194 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
e6fe60e7 AM |
9195 | s.op->newline() << "#ifdef STP_TIMING"; |
9196 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 9197 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; |
e6fe60e7 AM |
9198 | s.op->newline(-1) << "#endif"; |
9199 | s.op->newline(-1) << "} else {"; | |
9200 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
9201 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
9202 | s.op->newline() << "#endif"; | |
065d5567 | 9203 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
e6fe60e7 AM |
9204 | s.op->newline() << "#ifdef STP_TIMING"; |
9205 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 9206 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
e6fe60e7 AM |
9207 | s.op->newline(-1) << "#endif"; |
9208 | s.op->newline(-1) << "}"; | |
9209 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
9210 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
9211 | s.op->newline() << "#endif"; | |
9212 | s.op->newline() << "sdp->registered_p = 0;"; | |
9213 | s.op->newline(-1) << "}"; | |
f8a968bc JS |
9214 | } |
9215 | ||
e6fe60e7 | 9216 | struct kprobe_builder: public derived_probe_builder |
3c1b3d06 | 9217 | { |
9fdf787d | 9218 | public: |
2a639817 | 9219 | kprobe_builder() {} |
9fdf787d | 9220 | |
2a639817 | 9221 | void build_no_more (systemtap_session &s) {} |
9fdf787d | 9222 | |
e6fe60e7 AM |
9223 | virtual void build(systemtap_session & sess, |
9224 | probe * base, | |
9225 | probe_point * location, | |
9226 | literal_map_t const & parameters, | |
9227 | vector<derived_probe *> & finished_results); | |
9228 | }; | |
3c1b3d06 FCE |
9229 | |
9230 | ||
79189b84 | 9231 | void |
05fb3e0c | 9232 | kprobe_builder::build(systemtap_session & sess, |
e6fe60e7 AM |
9233 | probe * base, |
9234 | probe_point * location, | |
9235 | literal_map_t const & parameters, | |
9236 | vector<derived_probe *> & finished_results) | |
79189b84 | 9237 | { |
e6fe60e7 | 9238 | string function_string_val, module_string_val; |
05fb3e0c | 9239 | string path, library, path_tgt, library_tgt; |
b6371390 JS |
9240 | int64_t statement_num_val = 0, maxactive_val = 0; |
9241 | bool has_function_str, has_module_str, has_statement_num; | |
9242 | bool has_absolute, has_return, has_maxactive; | |
b642c901 | 9243 | bool has_path, has_library; |
79189b84 | 9244 | |
b6371390 JS |
9245 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
9246 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
9247 | has_return = has_null_param (parameters, TOK_RETURN); | |
9248 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
9249 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
9250 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
b642c901 SC |
9251 | has_path = get_param (parameters, TOK_PROCESS, path); |
9252 | has_library = get_param (parameters, TOK_LIBRARY, library); | |
9253 | ||
9254 | if (has_path) | |
05fb3e0c WF |
9255 | { |
9256 | path = find_executable (path, sess.sysroot, sess.sysenv); | |
9257 | path_tgt = path_remove_sysroot(sess, path); | |
9258 | } | |
b642c901 | 9259 | if (has_library) |
05fb3e0c WF |
9260 | { |
9261 | library = find_executable (library, sess.sysroot, sess.sysenv, | |
9262 | "LD_LIBRARY_PATH"); | |
9263 | library_tgt = path_remove_sysroot(sess, library); | |
9264 | } | |
c57ea854 | 9265 | |
b6371390 | 9266 | if (has_function_str) |
6fb70fb7 | 9267 | { |
2a639817 | 9268 | if (has_module_str) |
9fdf787d DS |
9269 | { |
9270 | function_string_val = module_string_val + ":" + function_string_val; | |
9271 | derived_probe *dp | |
23dc94f6 DS |
9272 | = new kprobe_derived_probe (sess, finished_results, base, |
9273 | location, function_string_val, | |
9fdf787d DS |
9274 | 0, has_return, has_statement_num, |
9275 | has_maxactive, has_path, has_library, | |
9276 | maxactive_val, path_tgt, library_tgt); | |
9277 | finished_results.push_back (dp); | |
9278 | } | |
9279 | else | |
9280 | { | |
2a639817 JS |
9281 | vector<string> matches; |
9282 | ||
9283 | // Simple names can be found directly | |
9284 | if (function_string_val.find_first_of("*?[") == string::npos) | |
9285 | { | |
9286 | if (sess.kernel_functions.count(function_string_val)) | |
9287 | matches.push_back(function_string_val); | |
9288 | } | |
9289 | else // Search function name list for matching names | |
9290 | { | |
9291 | for (set<string>::const_iterator it = sess.kernel_functions.begin(); | |
9292 | it != sess.kernel_functions.end(); it++) | |
9293 | // fnmatch returns zero for matching. | |
9294 | if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0) | |
9295 | matches.push_back(*it); | |
9296 | } | |
86758d5f | 9297 | |
2a639817 JS |
9298 | for (vector<string>::const_iterator it = matches.begin(); |
9299 | it != matches.end(); it++) | |
9fdf787d | 9300 | { |
2a639817 JS |
9301 | derived_probe *dp |
9302 | = new kprobe_derived_probe (sess, finished_results, base, | |
9303 | location, *it, 0, has_return, | |
9304 | has_statement_num, | |
9305 | has_maxactive, has_path, | |
9306 | has_library, maxactive_val, | |
9307 | path_tgt, library_tgt); | |
9308 | finished_results.push_back (dp); | |
9fdf787d DS |
9309 | } |
9310 | } | |
6fb70fb7 | 9311 | } |
e6fe60e7 | 9312 | else |
b6371390 JS |
9313 | { |
9314 | // assert guru mode for absolute probes | |
9315 | if ( has_statement_num && has_absolute && !base->privileged ) | |
dc09353a | 9316 | throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok); |
b6371390 | 9317 | |
23dc94f6 DS |
9318 | finished_results.push_back (new kprobe_derived_probe (sess, |
9319 | finished_results, | |
9320 | base, | |
b6371390 JS |
9321 | location, "", |
9322 | statement_num_val, | |
9323 | has_return, | |
9324 | has_statement_num, | |
9325 | has_maxactive, | |
b642c901 SC |
9326 | has_path, |
9327 | has_library, | |
9328 | maxactive_val, | |
05fb3e0c WF |
9329 | path_tgt, |
9330 | library_tgt)); | |
96b030fe | 9331 | } |
79189b84 JS |
9332 | } |
9333 | ||
23dc94f6 DS |
9334 | |
9335 | void | |
9336 | kprobe_var_expanding_visitor::visit_entry_op (entry_op *e) | |
9337 | { | |
9338 | expression *repl = e; | |
9339 | ||
bd5b25e1 JS |
9340 | if (has_return) |
9341 | { | |
9342 | // expand the operand as if it weren't a return probe | |
9343 | has_return = false; | |
9344 | replace (e->operand); | |
9345 | has_return = true; | |
23dc94f6 | 9346 | |
bd5b25e1 JS |
9347 | // XXX it would be nice to use gen_kretprobe_saved_return when |
9348 | // available, but it requires knowing the types already, which is | |
9349 | // problematic for arbitrary expressons. | |
9350 | repl = gen_mapped_saved_return (sess, e->operand, "entry", | |
9351 | add_block, add_block_tid, | |
9352 | add_call_probe, add_call_probe_tid); | |
9353 | } | |
23dc94f6 DS |
9354 | provide (repl); |
9355 | } | |
9356 | ||
9357 | ||
dd225250 PS |
9358 | // ------------------------------------------------------------------------ |
9359 | // Hardware breakpoint based probes. | |
9360 | // ------------------------------------------------------------------------ | |
9361 | ||
9362 | static const string TOK_HWBKPT("data"); | |
9363 | static const string TOK_HWBKPT_WRITE("write"); | |
9364 | static const string TOK_HWBKPT_RW("rw"); | |
9365 | static const string TOK_LENGTH("length"); | |
9366 | ||
9367 | #define HWBKPT_READ 0 | |
9368 | #define HWBKPT_WRITE 1 | |
9369 | #define HWBKPT_RW 2 | |
9370 | struct hwbkpt_derived_probe: public derived_probe | |
9371 | { | |
9372 | hwbkpt_derived_probe (probe *base, | |
9373 | probe_point *location, | |
9374 | uint64_t addr, | |
9375 | string symname, | |
9376 | unsigned int len, | |
9377 | bool has_only_read_access, | |
9378 | bool has_only_write_access, | |
9379 | bool has_rw_access | |
9380 | ); | |
9381 | Dwarf_Addr hwbkpt_addr; | |
9382 | string symbol_name; | |
9383 | unsigned int hwbkpt_access,hwbkpt_len; | |
9384 | ||
9385 | void printsig (std::ostream &o) const; | |
9386 | void join_group (systemtap_session& s); | |
9387 | }; | |
9388 | ||
9389 | struct hwbkpt_derived_probe_group: public derived_probe_group | |
9390 | { | |
dd225250 | 9391 | private: |
dac77b80 | 9392 | vector<hwbkpt_derived_probe*> hwbkpt_probes; |
dd225250 PS |
9393 | |
9394 | public: | |
9395 | void enroll (hwbkpt_derived_probe* probe, systemtap_session& s); | |
9396 | void emit_module_decls (systemtap_session& s); | |
9397 | void emit_module_init (systemtap_session& s); | |
9398 | void emit_module_exit (systemtap_session& s); | |
9399 | }; | |
9400 | ||
9401 | hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base, | |
9ea68eb9 JS |
9402 | probe_point *location, |
9403 | uint64_t addr, | |
9404 | string symname, | |
9405 | unsigned int len, | |
9406 | bool has_only_read_access, | |
9407 | bool has_only_write_access, | |
822a6a3d | 9408 | bool): |
4c5d1300 | 9409 | derived_probe (base, location, true /* .components soon rewritten */ ), |
dd225250 PS |
9410 | hwbkpt_addr (addr), |
9411 | symbol_name (symname), | |
9412 | hwbkpt_len (len) | |
9413 | { | |
9414 | this->tok = base->tok; | |
9415 | ||
9416 | vector<probe_point::component*> comps; | |
9417 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
9418 | ||
9419 | if (hwbkpt_addr) | |
9ea68eb9 JS |
9420 | comps.push_back (new probe_point::component (TOK_HWBKPT, |
9421 | new literal_number(hwbkpt_addr, true))); | |
9422 | else if (symbol_name.size()) | |
9423 | comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name))); | |
dd225250 PS |
9424 | |
9425 | comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len))); | |
9426 | ||
9427 | if (has_only_read_access) | |
9ea68eb9 | 9428 | this->hwbkpt_access = HWBKPT_READ ; |
dd225250 PS |
9429 | //TODO add code for comps.push_back for read, since this flag is not for x86 |
9430 | ||
9431 | else | |
9ea68eb9 JS |
9432 | { |
9433 | if (has_only_write_access) | |
9434 | { | |
9435 | this->hwbkpt_access = HWBKPT_WRITE ; | |
9436 | comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE)); | |
9437 | } | |
9438 | else | |
9439 | { | |
9440 | this->hwbkpt_access = HWBKPT_RW ; | |
9441 | comps.push_back (new probe_point::component(TOK_HWBKPT_RW)); | |
9442 | } | |
9443 | } | |
dd225250 PS |
9444 | |
9445 | this->sole_location()->components = comps; | |
9446 | } | |
9447 | ||
9448 | void hwbkpt_derived_probe::printsig (ostream& o) const | |
9449 | { | |
9450 | sole_location()->print (o); | |
9451 | printsig_nested (o); | |
9452 | } | |
9453 | ||
9454 | void hwbkpt_derived_probe::join_group (systemtap_session& s) | |
9455 | { | |
dac77b80 FCE |
9456 | if (! s.hwbkpt_derived_probes) |
9457 | s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group (); | |
dd225250 PS |
9458 | s.hwbkpt_derived_probes->enroll (this, s); |
9459 | } | |
9460 | ||
9461 | void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s) | |
9462 | { | |
dac77b80 FCE |
9463 | hwbkpt_probes.push_back (p); |
9464 | ||
9465 | unsigned max_hwbkpt_probes_by_arch = 0; | |
9466 | if (s.architecture == "i386" || s.architecture == "x86_64") | |
9467 | max_hwbkpt_probes_by_arch = 4; | |
9468 | else if (s.architecture == "s390") | |
9469 | max_hwbkpt_probes_by_arch = 1; | |
9470 | ||
c57ea854 | 9471 | if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch) |
2713ea24 | 9472 | s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)", |
b530b5b3 | 9473 | s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch)); |
dd225250 PS |
9474 | } |
9475 | ||
9476 | void | |
9477 | hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s) | |
9478 | { | |
dac77b80 | 9479 | if (hwbkpt_probes.empty()) return; |
dd225250 PS |
9480 | |
9481 | s.op->newline() << "/* ---- hwbkpt-based probes ---- */"; | |
9482 | ||
9483 | s.op->newline() << "#include <linux/perf_event.h>"; | |
9484 | s.op->newline() << "#include <linux/hw_breakpoint.h>"; | |
9485 | s.op->newline(); | |
9486 | ||
9487 | // Forward declare the master entry functions | |
23063de1 | 9488 | s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI"; |
dd225250 PS |
9489 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; |
9490 | s.op->line() << " int nmi,"; | |
9491 | s.op->line() << " struct perf_sample_data *data,"; | |
9492 | s.op->line() << " struct pt_regs *regs);"; | |
23063de1 FCE |
9493 | s.op->newline() << "#else"; |
9494 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
9495 | s.op->line() << " struct perf_sample_data *data,"; | |
9496 | s.op->line() << " struct pt_regs *regs);"; | |
9497 | s.op->newline() << "#endif"; | |
79189b84 | 9498 | |
dd225250 PS |
9499 | // Emit the actual probe list. |
9500 | ||
9501 | s.op->newline() << "static struct perf_event_attr "; | |
dac77b80 | 9502 | s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
9503 | |
9504 | s.op->newline() << "static struct perf_event **"; | |
dac77b80 | 9505 | s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
9506 | s.op->newline() << "static struct stap_hwbkpt_probe {"; |
9507 | s.op->newline() << "int registered_p:1;"; | |
43650b10 | 9508 | // registered_p = 0 signifies a probe that is unregistered (or failed) |
dd225250 PS |
9509 | // registered_p = 1 signifies a probe that got registered successfully |
9510 | ||
faea5e16 | 9511 | // Symbol Names are mostly small and uniform enough |
dd225250 | 9512 | // to justify putting const char*. |
dac77b80 | 9513 | s.op->newline() << "const char * const symbol;"; |
dd225250 PS |
9514 | |
9515 | s.op->newline() << "const unsigned long address;"; | |
9516 | s.op->newline() << "uint8_t atype;"; | |
bb0a4e12 | 9517 | s.op->newline() << "unsigned int len;"; |
7c3e97f4 | 9518 | s.op->newline() << "const struct stap_probe * const probe;"; |
dd225250 PS |
9519 | s.op->newline() << "} stap_hwbkpt_probes[] = {"; |
9520 | s.op->indent(1); | |
9521 | ||
dac77b80 | 9522 | for (unsigned int it = 0; it < hwbkpt_probes.size(); it++) |
dd225250 | 9523 | { |
dac77b80 | 9524 | hwbkpt_derived_probe* p = hwbkpt_probes.at(it); |
dd225250 | 9525 | s.op->newline() << "{"; |
dd225250 PS |
9526 | if (p->symbol_name.size()) |
9527 | s.op->line() << " .address=(unsigned long)0x0" << "ULL,"; | |
9528 | else | |
9529 | s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,"; | |
9530 | switch(p->hwbkpt_access){ | |
9531 | case HWBKPT_READ: | |
9532 | s.op->line() << " .atype=HW_BREAKPOINT_R ,"; | |
bb0a4e12 | 9533 | break; |
dd225250 PS |
9534 | case HWBKPT_WRITE: |
9535 | s.op->line() << " .atype=HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 9536 | break; |
dd225250 PS |
9537 | case HWBKPT_RW: |
9538 | s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 9539 | break; |
dd225250 PS |
9540 | }; |
9541 | s.op->line() << " .len=" << p->hwbkpt_len << ","; | |
faea5e16 | 9542 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
dd225250 | 9543 | s.op->line() << " .symbol=\"" << p->symbol_name << "\","; |
dd225250 PS |
9544 | s.op->line() << " },"; |
9545 | } | |
dac77b80 | 9546 | s.op->newline(-1) << "};"; |
dd225250 PS |
9547 | |
9548 | // Emit the hwbkpt callback function | |
9549 | s.op->newline() ; | |
23063de1 | 9550 | s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI"; |
dd225250 PS |
9551 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; |
9552 | s.op->line() << " int nmi,"; | |
9553 | s.op->line() << " struct perf_sample_data *data,"; | |
9554 | s.op->line() << " struct pt_regs *regs) {"; | |
23063de1 FCE |
9555 | s.op->newline() << "#else"; |
9556 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
9557 | s.op->line() << " struct perf_sample_data *data,"; | |
9558 | s.op->line() << " struct pt_regs *regs) {"; | |
9559 | s.op->newline() << "#endif"; | |
dac77b80 FCE |
9560 | s.op->newline(1) << "unsigned int i;"; |
9561 | s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;"; | |
9562 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; | |
9563 | s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
9564 | // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead? | |
9565 | s.op->newline() << "if (bp->attr.bp_addr==hp->bp_addr && bp->attr.bp_type==hp->bp_type && bp->attr.bp_len==hp->bp_len) {"; | |
9566 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];"; | |
71db462b | 9567 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 9568 | "stp_probe_type_hwbkpt"); |
d9aed31e | 9569 | s.op->newline() << "if (user_mode(regs)) {"; |
e04b5d74 | 9570 | s.op->newline(1)<< "c->user_mode_p = 1;"; |
d9aed31e MW |
9571 | s.op->newline() << "c->uregs = regs;"; |
9572 | s.op->newline(-1) << "} else {"; | |
9573 | s.op->newline(1) << "c->kregs = regs;"; | |
9574 | s.op->newline(-1) << "}"; | |
26e63673 | 9575 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
f887a8c9 | 9576 | common_probe_entryfn_epilogue (s, true); |
dac77b80 | 9577 | s.op->newline(-1) << "}"; |
dd225250 PS |
9578 | s.op->newline(-1) << "}"; |
9579 | s.op->newline() << "return 0;"; | |
dac77b80 | 9580 | s.op->newline(-1) << "}"; |
dd225250 PS |
9581 | } |
9582 | ||
9583 | void | |
9584 | hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s) | |
9585 | { | |
dac77b80 | 9586 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 PS |
9587 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
9588 | s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
9589 | s.op->newline() << "void *addr = (void *) sdp->address;"; | |
9590 | s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;"; | |
dac77b80 FCE |
9591 | s.op->newline() << "hw_breakpoint_init(hp);"; |
9592 | s.op->newline() << "if (addr)"; | |
9593 | s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;"; | |
9594 | s.op->newline(-1) << "else { "; | |
9595 | s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);"; | |
9596 | s.op->newline() << "if (!hp->bp_addr) { "; | |
26e63673 | 9597 | s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);"; |
dac77b80 FCE |
9598 | s.op->newline() << "continue;"; |
9599 | s.op->newline(-1) << "}"; | |
9600 | s.op->newline(-1) << "}"; | |
9601 | s.op->newline() << "hp->bp_type = sdp->atype;"; | |
9602 | ||
9603 | // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!). | |
c57ea854 | 9604 | if (s.architecture == "i386" || s.architecture == "x86_64" ) |
dac77b80 FCE |
9605 | { |
9606 | s.op->newline() << "switch(sdp->len) {"; | |
9607 | s.op->newline() << "case 1:"; | |
9608 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;"; | |
9609 | s.op->newline() << "break;"; | |
9610 | s.op->newline(-1) << "case 2:"; | |
9611 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;"; | |
9612 | s.op->newline() << "break;"; | |
9613 | s.op->newline(-1) << "case 3:"; | |
9614 | s.op->newline() << "case 4:"; | |
9615 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;"; | |
9616 | s.op->newline() << "break;"; | |
9617 | s.op->newline(-1) << "case 5:"; | |
9618 | s.op->newline() << "case 6:"; | |
9619 | s.op->newline() << "case 7:"; | |
9620 | s.op->newline() << "case 8:"; | |
9621 | s.op->newline() << "default:"; // XXX: could instead reject | |
9622 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;"; | |
9623 | s.op->newline() << "break;"; | |
9624 | s.op->newline(-1) << "}"; | |
9625 | } | |
9626 | else // other architectures presumed straightforward | |
9627 | s.op->newline() << "hp->bp_len = sdp->len;"; | |
9628 | ||
26e63673 | 9629 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b273669e MW |
9630 | s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT"; |
9631 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);"; | |
9632 | s.op->newline() << "#else"; | |
dac77b80 | 9633 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);"; |
b273669e | 9634 | s.op->newline() << "#endif"; |
43650b10 | 9635 | s.op->newline() << "rc = 0;"; |
dac77b80 | 9636 | s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {"; |
43650b10 WC |
9637 | s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);"; |
9638 | s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;"; | |
9639 | s.op->newline(-1) << "}"; | |
217ef1f4 WC |
9640 | s.op->newline() << "if (rc) {"; |
9641 | s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);"; | |
43650b10 | 9642 | s.op->newline() << "sdp->registered_p = 0;"; |
dac77b80 | 9643 | s.op->newline(-1) << "}"; |
dd225250 | 9644 | s.op->newline() << " else sdp->registered_p = 1;"; |
dd225250 PS |
9645 | s.op->newline(-1) << "}"; // for loop |
9646 | } | |
9647 | ||
9648 | void | |
9649 | hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s) | |
9650 | { | |
9651 | //Unregister hwbkpt probes. | |
dac77b80 | 9652 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 | 9653 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
dac77b80 FCE |
9654 | s.op->newline() << "if (sdp->registered_p == 0) continue;"; |
9655 | s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);"; | |
dd225250 PS |
9656 | s.op->newline() << "sdp->registered_p = 0;"; |
9657 | s.op->newline(-1) << "}"; | |
9658 | } | |
9659 | ||
9660 | struct hwbkpt_builder: public derived_probe_builder | |
9661 | { | |
9662 | hwbkpt_builder() {} | |
9663 | virtual void build(systemtap_session & sess, | |
9664 | probe * base, | |
9665 | probe_point * location, | |
9666 | literal_map_t const & parameters, | |
9667 | vector<derived_probe *> & finished_results); | |
9668 | }; | |
9669 | ||
9670 | void | |
9671 | hwbkpt_builder::build(systemtap_session & sess, | |
9672 | probe * base, | |
9673 | probe_point * location, | |
9674 | literal_map_t const & parameters, | |
9675 | vector<derived_probe *> & finished_results) | |
9676 | { | |
9677 | string symbol_str_val; | |
9678 | int64_t hwbkpt_address, len; | |
9679 | bool has_addr, has_symbol_str, has_write, has_rw, has_len; | |
9680 | ||
b47f3a55 | 9681 | if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y"))) |
dc09353a | 9682 | throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"), |
b47f3a55 FCE |
9683 | location->components[0]->tok); |
9684 | if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y"))) | |
dc09353a | 9685 | throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"), |
b47f3a55 FCE |
9686 | location->components[0]->tok); |
9687 | ||
dd225250 PS |
9688 | has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address); |
9689 | has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val); | |
9690 | has_len = get_param (parameters, TOK_LENGTH, len); | |
9691 | has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end()); | |
9692 | has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end()); | |
9693 | ||
9694 | if (!has_len) | |
9695 | len = 1; | |
9696 | ||
9697 | if (has_addr) | |
9698 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
9699 | location, | |
9700 | hwbkpt_address, | |
9701 | "",len,0, | |
9702 | has_write, | |
9703 | has_rw)); | |
5d8a0aea | 9704 | else if (has_symbol_str) |
dd225250 PS |
9705 | finished_results.push_back (new hwbkpt_derived_probe (base, |
9706 | location, | |
9707 | 0, | |
9708 | symbol_str_val,len,0, | |
9709 | has_write, | |
9710 | has_rw)); | |
5d8a0aea FCE |
9711 | else |
9712 | assert (0); | |
dd225250 | 9713 | } |
342d3f96 | 9714 | |
0a6f5a3f JS |
9715 | // ------------------------------------------------------------------------ |
9716 | // statically inserted kernel-tracepoint derived probes | |
9717 | // ------------------------------------------------------------------------ | |
9718 | ||
6fb70fb7 | 9719 | struct tracepoint_arg |
79189b84 | 9720 | { |
ad370dcc | 9721 | string name, c_type, typecast; |
dcaa1a65 | 9722 | bool usable, used, isptr; |
f8a968bc | 9723 | Dwarf_Die type_die; |
dcaa1a65 | 9724 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 9725 | }; |
79189b84 | 9726 | |
0a6f5a3f JS |
9727 | struct tracepoint_derived_probe: public derived_probe |
9728 | { | |
79189b84 JS |
9729 | tracepoint_derived_probe (systemtap_session& s, |
9730 | dwflpp& dw, Dwarf_Die& func_die, | |
9731 | const string& tracepoint_name, | |
9732 | probe* base_probe, probe_point* location); | |
bc9a523d | 9733 | |
79189b84 | 9734 | systemtap_session& sess; |
6fb70fb7 JS |
9735 | string tracepoint_name, header; |
9736 | vector <struct tracepoint_arg> args; | |
bc9a523d | 9737 | |
6fb70fb7 | 9738 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
d0bfd2ac | 9739 | void getargs (std::list<std::string> &arg_set) const; |
79189b84 | 9740 | void join_group (systemtap_session& s); |
3e3bd7b6 | 9741 | void print_dupe_stamp(ostream& o); |
0a6f5a3f | 9742 | }; |
79189b84 JS |
9743 | |
9744 | ||
0a6f5a3f | 9745 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> |
79189b84 | 9746 | { |
79189b84 JS |
9747 | void emit_module_decls (systemtap_session& s); |
9748 | void emit_module_init (systemtap_session& s); | |
9749 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f | 9750 | }; |
79189b84 | 9751 | |
bc9a523d | 9752 | |
f8a968bc JS |
9753 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
9754 | { | |
9755 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
9756 | vector <struct tracepoint_arg>& args): | |
9757 | dw (dw), probe_name (probe_name), args (args) {} | |
9758 | dwflpp& dw; | |
9759 | const string& probe_name; | |
9760 | vector <struct tracepoint_arg>& args; | |
bc9a523d | 9761 | |
f8a968bc JS |
9762 | void visit_target_symbol (target_symbol* e); |
9763 | void visit_target_symbol_arg (target_symbol* e); | |
9764 | void visit_target_symbol_context (target_symbol* e); | |
9765 | }; | |
79189b84 JS |
9766 | |
9767 | ||
f8a968bc JS |
9768 | void |
9769 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
75ead1f7 | 9770 | { |
cc9001af | 9771 | string argname = e->sym_name(); |
75ead1f7 | 9772 | |
f8a968bc JS |
9773 | // search for a tracepoint parameter matching this name |
9774 | tracepoint_arg *arg = NULL; | |
9775 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 9776 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
9777 | { |
9778 | arg = &args[i]; | |
9779 | arg->used = true; | |
9780 | break; | |
9781 | } | |
75ead1f7 | 9782 | |
f8a968bc JS |
9783 | if (arg == NULL) |
9784 | { | |
1d0499c2 | 9785 | set<string> vars; |
f8a968bc | 9786 | for (unsigned i = 0; i < args.size(); ++i) |
1d0499c2 JL |
9787 | vars.insert("$" + args[i].name); |
9788 | vars.insert("$$name"); | |
9789 | vars.insert("$$parms"); | |
9790 | vars.insert("$$vars"); | |
9791 | string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many | |
75ead1f7 | 9792 | |
f8a968bc JS |
9793 | // We hope that this value ends up not being referenced after all, so it |
9794 | // can be optimized out quietly. | |
1d0499c2 JL |
9795 | throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s", |
9796 | e->name.c_str(), sugs.empty() ? "" : | |
9797 | (_(" (alternatives: ") + sugs + ")").c_str()), e->tok); | |
9798 | // NB: we use 'alternatives' because we list all | |
f8a968bc JS |
9799 | // NB: we can have multiple errors, since a target variable |
9800 | // may be expanded in several different contexts: | |
9801 | // trace ("*") { $foo->bar } | |
f8a968bc | 9802 | } |
75ead1f7 | 9803 | |
f8a968bc | 9804 | // make sure we're not dereferencing base types |
dc5a09fc | 9805 | if (!arg->isptr) |
d19a9a82 | 9806 | e->assert_no_components("tracepoint", true); |
75ead1f7 | 9807 | |
f8a968bc JS |
9808 | // we can only write to dereferenced fields, and only if guru mode is on |
9809 | bool lvalue = is_active_lvalue(e); | |
9810 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
dc09353a | 9811 | throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok); |
c69a87e0 | 9812 | |
ad370dcc JS |
9813 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
9814 | // is also meaningless until you dereference past a pointer member. It's | |
9815 | // harder to detect and prevent that though... | |
75ead1f7 | 9816 | |
f8a968bc JS |
9817 | if (e->components.empty()) |
9818 | { | |
03c75a4a | 9819 | if (e->addressof) |
dc09353a | 9820 | throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok); |
a45664f4 | 9821 | |
3e3bd7b6 | 9822 | // Just grab the value from the probe locals |
a45664f4 JS |
9823 | symbol* sym = new symbol; |
9824 | sym->tok = e->tok; | |
9825 | sym->name = "__tracepoint_arg_" + arg->name; | |
9826 | provide (sym); | |
f8a968bc JS |
9827 | } |
9828 | else | |
9829 | { | |
5f36109e JS |
9830 | // make a copy of the original as a bare target symbol for the tracepoint |
9831 | // value, which will be passed into the dwarf dereferencing code | |
9832 | target_symbol* e2 = deep_copy_visitor::deep_copy(e); | |
9833 | e2->components.clear(); | |
9834 | ||
9835 | if (e->components.back().type == target_symbol::comp_pretty_print) | |
9836 | { | |
9837 | if (lvalue) | |
dc09353a | 9838 | throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 9839 | |
d19a9a82 | 9840 | dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e); |
5f36109e JS |
9841 | dpp.expand()->visit (this); |
9842 | return; | |
9843 | } | |
9844 | ||
1c0be8c7 | 9845 | bool userspace_p = false; |
f8a968bc | 9846 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") |
cc9001af | 9847 | + "_" + e->sym_name() |
aca66a36 | 9848 | + "_" + lex_cast(tick++)); |
75ead1f7 | 9849 | |
1c0be8c7 JS |
9850 | exp_type type = pe_long; |
9851 | string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, type); | |
6fda2dff | 9852 | |
1c0be8c7 JS |
9853 | functioncall* n = synthetic_embedded_deref_call(dw.sess, fname, code, type, |
9854 | userspace_p, lvalue, e, e2); | |
75ead1f7 | 9855 | |
f8a968bc JS |
9856 | if (lvalue) |
9857 | { | |
9858 | // Provide the functioncall to our parent, so that it can be | |
9859 | // used to substitute for the assignment node immediately above | |
9860 | // us. | |
9861 | assert(!target_symbol_setter_functioncalls.empty()); | |
9862 | *(target_symbol_setter_functioncalls.top()) = n; | |
9863 | } | |
75ead1f7 | 9864 | |
1c0be8c7 JS |
9865 | // Revisit the functioncall so arguments can be expanded. |
9866 | n->visit (this); | |
f8a968bc | 9867 | } |
75ead1f7 JS |
9868 | } |
9869 | ||
9870 | ||
f8a968bc JS |
9871 | void |
9872 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
0a6f5a3f | 9873 | { |
03c75a4a | 9874 | if (e->addressof) |
dc09353a | 9875 | throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok); |
03c75a4a | 9876 | |
f8a968bc | 9877 | if (is_active_lvalue (e)) |
dc09353a | 9878 | throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok); |
0a6f5a3f | 9879 | |
277c21bc | 9880 | if (e->name == "$$name") |
f8a968bc | 9881 | { |
5f36109e JS |
9882 | e->assert_no_components("tracepoint"); |
9883 | ||
bfdaad1e DS |
9884 | // Synthesize an embedded expression. |
9885 | embedded_expr *expr = new embedded_expr; | |
9886 | expr->tok = e->tok; | |
9887 | expr->code = string("/* string */ /* pure */ ") | |
6dceb5c9 | 9888 | + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\""); |
bfdaad1e | 9889 | provide (expr); |
f8a968bc | 9890 | } |
277c21bc | 9891 | else if (e->name == "$$vars" || e->name == "$$parms") |
f8a968bc | 9892 | { |
5f36109e JS |
9893 | e->assert_no_components("tracepoint", true); |
9894 | ||
1c922ad7 | 9895 | print_format* pf = print_format::create(e->tok, "sprintf"); |
0a6f5a3f | 9896 | |
f8a968bc | 9897 | for (unsigned i = 0; i < args.size(); ++i) |
b278033a | 9898 | { |
dcaa1a65 JS |
9899 | if (!args[i].usable) |
9900 | continue; | |
f8a968bc JS |
9901 | if (i > 0) |
9902 | pf->raw_components += " "; | |
9903 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 9904 | target_symbol *tsym = new target_symbol; |
f8a968bc | 9905 | tsym->tok = e->tok; |
277c21bc | 9906 | tsym->name = "$" + args[i].name; |
5f36109e | 9907 | tsym->components = e->components; |
b278033a | 9908 | |
f8a968bc JS |
9909 | // every variable should always be accessible! |
9910 | tsym->saved_conversion_error = 0; | |
8c2f50c0 | 9911 | expression *texp = require<expression> (tsym); // NB: throws nothing ... |
14a97852 JS |
9912 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
9913 | { | |
9914 | if (dw.sess.verbose>2) | |
e26c2f83 | 9915 | for (const semantic_error *c = tsym->saved_conversion_error; |
c081af73 | 9916 | c != 0; c = c->get_chain()) |
4c5d9906 | 9917 | clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl; |
14a97852 JS |
9918 | pf->raw_components += "=?"; |
9919 | continue; | |
9920 | } | |
b278033a | 9921 | |
5f36109e JS |
9922 | if (!e->components.empty() && |
9923 | e->components[0].type == target_symbol::comp_pretty_print) | |
9924 | pf->raw_components += "=%s"; | |
9925 | else | |
9926 | pf->raw_components += args[i].isptr ? "=%p" : "=%#x"; | |
f8a968bc JS |
9927 | pf->args.push_back(texp); |
9928 | } | |
0a6f5a3f | 9929 | |
f8a968bc JS |
9930 | pf->components = print_format::string_to_components(pf->raw_components); |
9931 | provide (pf); | |
b278033a | 9932 | } |
f8a968bc JS |
9933 | else |
9934 | assert(0); // shouldn't get here | |
0a6f5a3f JS |
9935 | } |
9936 | ||
0a6f5a3f | 9937 | void |
f8a968bc | 9938 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a6f5a3f | 9939 | { |
aff5d390 | 9940 | try |
c69a87e0 | 9941 | { |
bd1fcbad | 9942 | assert(e->name.size() > 0 && e->name[0] == '$'); |
aff5d390 | 9943 | |
277c21bc | 9944 | if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars") |
c69a87e0 | 9945 | visit_target_symbol_context (e); |
bd1fcbad | 9946 | |
c69a87e0 FCE |
9947 | else |
9948 | visit_target_symbol_arg (e); | |
9949 | } | |
9950 | catch (const semantic_error &er) | |
9951 | { | |
1af1e62d | 9952 | e->chain (er); |
c69a87e0 FCE |
9953 | provide (e); |
9954 | } | |
0a6f5a3f JS |
9955 | } |
9956 | ||
9957 | ||
79189b84 JS |
9958 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
9959 | dwflpp& dw, Dwarf_Die& func_die, | |
9960 | const string& tracepoint_name, | |
9961 | probe* base, probe_point* loc): | |
4c5d1300 | 9962 | derived_probe (base, loc, true /* .components soon rewritten */), |
79189b84 | 9963 | sess (s), tracepoint_name (tracepoint_name) |
56894e91 | 9964 | { |
79189b84 JS |
9965 | // create synthetic probe point name; preserve condition |
9966 | vector<probe_point::component*> comps; | |
9967 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
9968 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
9969 | this->sole_location()->components = comps; | |
9970 | ||
6fb70fb7 JS |
9971 | // fill out the available arguments in this tracepoint |
9972 | build_args(dw, func_die); | |
56894e91 | 9973 | |
6fb70fb7 JS |
9974 | // determine which header defined this tracepoint |
9975 | string decl_file = dwarf_decl_file(&func_die); | |
b78a0fbb | 9976 | header = decl_file; |
d4393459 FCE |
9977 | |
9978 | #if 0 /* This convention is not enforced. */ | |
6fb70fb7 JS |
9979 | size_t header_pos = decl_file.rfind("trace/"); |
9980 | if (header_pos == string::npos) | |
dc09353a | 9981 | throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '" |
6fb70fb7 JS |
9982 | + tracepoint_name + "' in '" |
9983 | + decl_file + "'"); | |
9984 | header = decl_file.substr(header_pos); | |
d4393459 | 9985 | #endif |
56894e91 | 9986 | |
6fb70fb7 JS |
9987 | // tracepoints from FOO_event_types.h should really be included from FOO.h |
9988 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
9989 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 9990 | // XXX: see also PR9993. |
d4393459 | 9991 | size_t header_pos = header.find("_event_types"); |
6fb70fb7 JS |
9992 | if (header_pos != string::npos) |
9993 | header.erase(header_pos, 12); | |
56894e91 | 9994 | |
f8a968bc JS |
9995 | // Now expand the local variables in the probe body |
9996 | tracepoint_var_expanding_visitor v (dw, name, args); | |
8b095b45 | 9997 | v.replace (this->body); |
a45664f4 JS |
9998 | for (unsigned i = 0; i < args.size(); i++) |
9999 | if (args[i].used) | |
10000 | { | |
10001 | vardecl* v = new vardecl; | |
10002 | v->name = "__tracepoint_arg_" + args[i].name; | |
10003 | v->tok = this->tok; | |
58701b78 | 10004 | v->set_arity(0, this->tok); |
a45664f4 | 10005 | v->type = pe_long; |
69aa668e | 10006 | v->synthetic = true; |
a45664f4 JS |
10007 | this->locals.push_back (v); |
10008 | } | |
56894e91 | 10009 | |
79189b84 | 10010 | if (sess.verbose > 2) |
ce0f6648 | 10011 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl; |
79189b84 | 10012 | } |
dc38c0ae | 10013 | |
56894e91 | 10014 | |
f8a968bc | 10015 | static bool |
dcaa1a65 | 10016 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
46b84a80 | 10017 | { |
d19a9a82 | 10018 | Dwarf_Die type; |
dcaa1a65 | 10019 | switch (dwarf_tag(&arg.type_die)) |
b20febf3 | 10020 | { |
f8a968bc JS |
10021 | case DW_TAG_typedef: |
10022 | case DW_TAG_const_type: | |
10023 | case DW_TAG_volatile_type: | |
10024 | // iterate on the referent type | |
3d1ad340 | 10025 | return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die) |
dcaa1a65 | 10026 | && resolve_tracepoint_arg_type(arg)); |
f8a968bc | 10027 | case DW_TAG_base_type: |
a52d2ac0 | 10028 | case DW_TAG_enumeration_type: |
f8a968bc | 10029 | // base types will simply be treated as script longs |
dcaa1a65 | 10030 | arg.isptr = false; |
f8a968bc JS |
10031 | return true; |
10032 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
10033 | // pointers can be treated as script longs, |
10034 | // and if we know their type, they can also be dereferenced | |
d19a9a82 JS |
10035 | type = arg.type_die; |
10036 | while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)) | |
10037 | { | |
10038 | // It still might be a non-type, e.g. const void, | |
10039 | // so we need to strip away all qualifiers. | |
10040 | int tag = dwarf_tag(&arg.type_die); | |
10041 | if (tag != DW_TAG_typedef && | |
10042 | tag != DW_TAG_const_type && | |
10043 | tag != DW_TAG_volatile_type) | |
10044 | { | |
10045 | arg.isptr = true; | |
10046 | break; | |
10047 | } | |
10048 | } | |
10049 | if (!arg.isptr) | |
10050 | arg.type_die = type; | |
ad370dcc JS |
10051 | arg.typecast = "(intptr_t)"; |
10052 | return true; | |
10053 | case DW_TAG_structure_type: | |
10054 | case DW_TAG_union_type: | |
10055 | // for structs/unions which are passed by value, we turn it into | |
10056 | // a pointer that can be dereferenced. | |
10057 | arg.isptr = true; | |
10058 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 10059 | return true; |
f8a968bc JS |
10060 | default: |
10061 | // should we consider other types too? | |
10062 | return false; | |
b20febf3 | 10063 | } |
56894e91 JS |
10064 | } |
10065 | ||
10066 | ||
10067 | void | |
822a6a3d | 10068 | tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die) |
56894e91 | 10069 | { |
6fb70fb7 JS |
10070 | Dwarf_Die arg; |
10071 | if (dwarf_child(&func_die, &arg) == 0) | |
10072 | do | |
10073 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
10074 | { | |
10075 | // build a tracepoint_arg for this parameter | |
10076 | tracepoint_arg tparg; | |
c60517ca | 10077 | tparg.name = dwarf_diename(&arg) ?: ""; |
56894e91 | 10078 | |
6fb70fb7 | 10079 | // read the type of this parameter |
3d1ad340 | 10080 | if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die) |
f1c8f8a5 | 10081 | || !dwarf_type_name(&tparg.type_die, tparg.c_type)) |
dc09353a | 10082 | throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'", |
ce0f6648 | 10083 | tparg.name.c_str(), tracepoint_name.c_str())); |
a68f81a2 | 10084 | |
dcaa1a65 | 10085 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
10086 | args.push_back(tparg); |
10087 | if (sess.verbose > 4) | |
a52d2ac0 JS |
10088 | clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s", |
10089 | tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(), | |
10090 | tparg.usable ? "ok" : "unavailable") << endl; | |
6fb70fb7 JS |
10091 | } |
10092 | while (dwarf_siblingof(&arg, &arg) == 0); | |
56894e91 JS |
10093 | } |
10094 | ||
dc38c0ae | 10095 | void |
d0bfd2ac | 10096 | tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const |
dc38c0ae | 10097 | { |
dcaa1a65 JS |
10098 | for (unsigned i = 0; i < args.size(); ++i) |
10099 | if (args[i].usable) | |
d0bfd2ac | 10100 | arg_set.push_back("$"+args[i].name+":"+args[i].c_type); |
dc38c0ae DS |
10101 | } |
10102 | ||
79189b84 JS |
10103 | void |
10104 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
197a4d62 | 10105 | { |
79189b84 JS |
10106 | if (! s.tracepoint_derived_probes) |
10107 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
10108 | s.tracepoint_derived_probes->enroll (this); | |
10109 | } | |
e38d6504 | 10110 | |
56894e91 | 10111 | |
197a4d62 | 10112 | void |
3e3bd7b6 | 10113 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) |
56894e91 | 10114 | { |
3e3bd7b6 JS |
10115 | for (unsigned i = 0; i < args.size(); i++) |
10116 | if (args[i].used) | |
10117 | o << "__tracepoint_arg_" << args[i].name << endl; | |
197a4d62 | 10118 | } |
56894e91 | 10119 | |
3e3bd7b6 | 10120 | |
c9ccb642 | 10121 | static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header) |
47dd066d | 10122 | { |
3c1b3d06 FCE |
10123 | vector<string> they_live; |
10124 | // PR 9993 | |
10125 | // XXX: may need this to be configurable | |
d4393459 | 10126 | they_live.push_back ("#include <linux/skbuff.h>"); |
9e0cd21a FCE |
10127 | |
10128 | // PR11649: conditional extra header | |
10129 | // for kvm tracepoints in 2.6.33ish | |
10130 | if (s.kernel_config["CONFIG_KVM"] != string("")) { | |
d4393459 FCE |
10131 | they_live.push_back ("#include <linux/kvm_host.h>"); |
10132 | } | |
10133 | ||
50b72692 | 10134 | if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) { |
d4393459 | 10135 | they_live.push_back ("#define XFS_BIG_BLKNOS 1"); |
88637c31 FCE |
10136 | if (s.kernel_source_tree != "") |
10137 | they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree | |
d4393459 FCE |
10138 | they_live.push_back ("struct xfs_mount;"); |
10139 | they_live.push_back ("struct xfs_inode;"); | |
10140 | they_live.push_back ("struct xfs_buf;"); | |
10141 | they_live.push_back ("struct xfs_bmbt_irec;"); | |
c2cf1b87 | 10142 | they_live.push_back ("struct xfs_trans;"); |
9e0cd21a | 10143 | } |
d4393459 | 10144 | |
50b72692 | 10145 | if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) { |
d4393459 FCE |
10146 | they_live.push_back ("struct rpc_task;"); |
10147 | } | |
b64d65e2 FCE |
10148 | // RHEL6.3 |
10149 | if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) { | |
10150 | they_live.push_back ("struct rpc_clnt;"); | |
10151 | they_live.push_back ("struct rpc_wait_queue;"); | |
10152 | } | |
d4393459 FCE |
10153 | |
10154 | they_live.push_back ("#include <asm/cputime.h>"); | |
10155 | ||
c2cf1b87 FCE |
10156 | // linux 3.0 |
10157 | they_live.push_back ("struct cpu_workqueue_struct;"); | |
10158 | ||
50b72692 | 10159 | if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string("")) |
c2cf1b87 FCE |
10160 | if (s.kernel_source_tree != "") |
10161 | they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree | |
10162 | ||
d97d428e | 10163 | if (header.find("ext3") != string::npos) |
50b72692 HP |
10164 | they_live.push_back ("struct ext3_reserve_window_node;"); |
10165 | ||
d97d428e FCE |
10166 | if (header.find("workqueue") != string::npos) |
10167 | { | |
10168 | they_live.push_back ("struct pool_workqueue;"); | |
10169 | they_live.push_back ("struct work_struct;"); | |
10170 | } | |
10171 | ||
10172 | if (header.find("asoc") != string::npos) | |
10173 | they_live.push_back ("struct snd_soc_dapm_path;"); | |
10174 | ||
10175 | if (header.find("9p") != string::npos) | |
10176 | { | |
10177 | they_live.push_back ("struct p9_client;"); | |
10178 | they_live.push_back ("struct p9_fcall;"); | |
10179 | } | |
10180 | ||
10181 | if (header.find("bcache") != string::npos) | |
10182 | { | |
10183 | they_live.push_back ("struct bkey;"); | |
10184 | they_live.push_back ("struct btree;"); | |
10185 | they_live.push_back ("struct cache_set;"); | |
10186 | they_live.push_back ("struct cache;"); | |
10187 | } | |
10188 | ||
10189 | if (header.find("f2fs") != string::npos) | |
10190 | { | |
10191 | // cannot get fs/f2fs/f2fs.h #included | |
10192 | they_live.push_back ("typedef u32 block_t;"); | |
10193 | they_live.push_back ("typedef u32 nid_t;"); | |
10194 | } | |
10195 | ||
10196 | if (header.find("radeon") != string::npos) | |
10197 | they_live.push_back ("struct radeon_bo;"); | |
10198 | ||
10199 | // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I | |
10200 | // also brcms_trace_events.h -> ... -> "types.h" | |
10201 | // XXX: need a way to add a temporary -I flag | |
10202 | ||
10203 | if (header.find("/ath/") != string::npos) | |
10204 | they_live.push_back ("struct ath5k_hw;"); | |
10205 | ||
10206 | ||
3c1b3d06 FCE |
10207 | return they_live; |
10208 | } | |
47dd066d WC |
10209 | |
10210 | ||
10211 | void | |
79189b84 | 10212 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) |
47dd066d | 10213 | { |
79189b84 JS |
10214 | if (probes.empty()) |
10215 | return; | |
47dd066d | 10216 | |
96b030fe JS |
10217 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
10218 | s.op->newline(); | |
79189b84 | 10219 | |
47dd066d | 10220 | |
a4b9c3b3 FCE |
10221 | // We create a MODULE_aux_N.c file for each tracepoint header, to allow them |
10222 | // to be separately compiled. That's because kernel tracepoint headers sometimes | |
10223 | // conflict. PR13155. | |
10224 | ||
10225 | map<string,translator_output*> per_header_aux; | |
10226 | // GC NB: the translator_output* structs are owned/retained by the systemtap_session. | |
47dd066d | 10227 | |
6fb70fb7 JS |
10228 | for (unsigned i = 0; i < probes.size(); ++i) |
10229 | { | |
10230 | tracepoint_derived_probe *p = probes[i]; | |
75ae2ec9 | 10231 | string header = p->header; |
5f73a260 | 10232 | |
a4b9c3b3 FCE |
10233 | // We cache the auxiliary output files on a per-header basis. We don't |
10234 | // need one aux file per tracepoint, only one per tracepoint-header. | |
10235 | translator_output *tpop = per_header_aux[header]; | |
10236 | if (tpop == 0) | |
10237 | { | |
10238 | tpop = s.op_create_auxiliary(); | |
10239 | per_header_aux[header] = tpop; | |
10240 | ||
10241 | // PR9993: Add extra headers to work around undeclared types in individual | |
10242 | // include/trace/foo.h files | |
10243 | const vector<string>& extra_decls = tracepoint_extra_decls (s, header); | |
10244 | for (unsigned z=0; z<extra_decls.size(); z++) | |
10245 | tpop->newline() << extra_decls[z] << "\n"; | |
720c435f | 10246 | |
a4b9c3b3 FCE |
10247 | // strip include/ substring, the same way as done in get_tracequery_module() |
10248 | size_t root_pos = header.rfind("include/"); | |
10249 | header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header); | |
bfffa443 FCE |
10250 | |
10251 | tpop->newline() << "#include <linux/tracepoint.h>" << endl; | |
a4b9c3b3 | 10252 | tpop->newline() << "#include <" << header << ">"; |
720c435f | 10253 | |
a4b9c3b3 FCE |
10254 | // Starting in 2.6.35, at the same time NOARGS was added, the callback |
10255 | // always has a void* as the first parameter. PR11599 | |
10256 | tpop->newline() << "#ifdef DECLARE_TRACE_NOARGS"; | |
10257 | tpop->newline() << "#define STAP_TP_DATA , NULL"; | |
10258 | tpop->newline() << "#define STAP_TP_PROTO void *cb_data" | |
10259 | << " __attribute__ ((unused))"; | |
10260 | if (!p->args.empty()) | |
10261 | tpop->line() << ","; | |
10262 | tpop->newline() << "#else"; | |
10263 | tpop->newline() << "#define STAP_TP_DATA"; | |
10264 | tpop->newline() << "#define STAP_TP_PROTO"; | |
10265 | if (p->args.empty()) | |
10266 | tpop->line() << " void"; | |
10267 | tpop->newline() << "#endif"; | |
720c435f JS |
10268 | |
10269 | tpop->newline() << "#define intptr_t long"; | |
a4b9c3b3 FCE |
10270 | } |
10271 | ||
720c435f JS |
10272 | // collect the args that are actually in use |
10273 | vector<const tracepoint_arg*> used_args; | |
6fb70fb7 | 10274 | for (unsigned j = 0; j < p->args.size(); ++j) |
720c435f JS |
10275 | if (p->args[j].used) |
10276 | used_args.push_back(&p->args[j]); | |
10277 | ||
a4b9c3b3 FCE |
10278 | // forward-declare the generated-side tracepoint callback |
10279 | tpop->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
10280 | tpop->indent(2); |
10281 | if (used_args.empty()) | |
10282 | tpop->line() << "void"; | |
10283 | for (unsigned j = 0; j < used_args.size(); ++j) | |
6fb70fb7 JS |
10284 | { |
10285 | if (j > 0) | |
a4b9c3b3 FCE |
10286 | tpop->line() << ", "; |
10287 | tpop->line() << "int64_t"; | |
6fb70fb7 | 10288 | } |
720c435f JS |
10289 | tpop->line() << ");"; |
10290 | tpop->indent(-2); | |
5f73a260 | 10291 | |
a4b9c3b3 FCE |
10292 | // define the generated-side tracepoint callback - in the main translator-output |
10293 | s.op->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
10294 | s.op->indent(2); |
10295 | if (used_args.empty()) | |
10296 | s.op->newline() << "void"; | |
10297 | for (unsigned j = 0; j < used_args.size(); ++j) | |
a4b9c3b3 | 10298 | { |
a4b9c3b3 FCE |
10299 | if (j > 0) |
10300 | s.op->line() << ", "; | |
720c435f | 10301 | s.op->newline() << "int64_t __tracepoint_arg_" << used_args[j]->name; |
6fb70fb7 | 10302 | } |
5f73a260 JS |
10303 | s.op->newline() << ")"; |
10304 | s.op->newline(-2) << "{"; | |
7c3e97f4 | 10305 | s.op->newline(1) << "const struct stap_probe * const probe = " |
faea5e16 | 10306 | << common_probe_init (p) << ";"; |
71db462b | 10307 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe", |
cda141c2 | 10308 | "stp_probe_type_tracepoint"); |
6dceb5c9 | 10309 | s.op->newline() << "c->ips.tracepoint_name = " |
c12d974f FCE |
10310 | << lex_cast_qstring (p->tracepoint_name) |
10311 | << ";"; | |
720c435f JS |
10312 | for (unsigned j = 0; j < used_args.size(); ++j) |
10313 | { | |
10314 | s.op->newline() << "c->probe_locals." << p->name | |
0386bbcf | 10315 | << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name) |
720c435f JS |
10316 | << " = __tracepoint_arg_" << used_args[j]->name << ";"; |
10317 | } | |
26e63673 | 10318 | s.op->newline() << "(*probe->ph) (c);"; |
f887a8c9 | 10319 | common_probe_entryfn_epilogue (s, true); |
6fb70fb7 | 10320 | s.op->newline(-1) << "}"; |
47dd066d | 10321 | |
a4b9c3b3 | 10322 | // define the real tracepoint callback function |
720c435f | 10323 | tpop->newline() << "static void enter_tracepoint_probe_" << i << "("; |
a4b9c3b3 FCE |
10324 | tpop->newline(2) << "STAP_TP_PROTO"; |
10325 | for (unsigned j = 0; j < p->args.size(); ++j) | |
10326 | { | |
10327 | if (j > 0) | |
10328 | tpop->line() << ", "; | |
10329 | tpop->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
10330 | } | |
10331 | tpop->newline() << ")"; | |
10332 | tpop->newline(-2) << "{"; | |
720c435f JS |
10333 | tpop->newline(1) << "enter_real_tracepoint_probe_" << i << "("; |
10334 | tpop->indent(2); | |
10335 | for (unsigned j = 0; j < used_args.size(); ++j) | |
10336 | { | |
10337 | if (j > 0) | |
10338 | tpop->line() << ", "; | |
10339 | tpop->newline() << "(int64_t)" << used_args[j]->typecast | |
10340 | << "__tracepoint_arg_" << used_args[j]->name; | |
10341 | } | |
10342 | tpop->newline() << ");"; | |
10343 | tpop->newline(-3) << "}"; | |
a4b9c3b3 FCE |
10344 | |
10345 | ||
96b030fe | 10346 | // emit normalized registration functions |
720c435f | 10347 | tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 10348 | tpop->newline(1) << "return register_trace_" << p->tracepoint_name |
5f73a260 | 10349 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 | 10350 | tpop->newline(-1) << "}"; |
47dd066d | 10351 | |
86758d5f JS |
10352 | // NB: we're not prepared to deal with unreg failures. However, failures |
10353 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
10354 | // weren't even registered. The former should be OKed by the initial | |
10355 | // registration call, and the latter is safe to ignore. | |
720c435f | 10356 | tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 10357 | tpop->newline(1) << "(void) unregister_trace_" << p->tracepoint_name |
5f73a260 | 10358 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 FCE |
10359 | tpop->newline(-1) << "}"; |
10360 | tpop->newline(); | |
5f73a260 | 10361 | |
720c435f JS |
10362 | // declare normalized registration functions |
10363 | s.op->newline() << "int register_tracepoint_probe_" << i << "(void);"; | |
10364 | s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);"; | |
5f73a260 | 10365 | |
a4b9c3b3 | 10366 | tpop->assert_0_indent(); |
af304783 DS |
10367 | } |
10368 | ||
96b030fe JS |
10369 | // emit an array of registration functions for easy init/shutdown |
10370 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
10371 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 10372 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
10373 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
10374 | s.op->indent(1); | |
10375 | for (unsigned i = 0; i < probes.size(); ++i) | |
10376 | { | |
10377 | s.op->newline () << "{"; | |
10378 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
10379 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
10380 | s.op->line() << " },"; | |
10381 | } | |
10382 | s.op->newline(-1) << "};"; | |
10383 | s.op->newline(); | |
47dd066d WC |
10384 | } |
10385 | ||
10386 | ||
79189b84 JS |
10387 | void |
10388 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
47dd066d | 10389 | { |
79189b84 JS |
10390 | if (probes.size () == 0) |
10391 | return; | |
47dd066d | 10392 | |
79189b84 | 10393 | s.op->newline() << "/* init tracepoint probes */"; |
96b030fe JS |
10394 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
10395 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
10396 | s.op->newline() << "if (rc) {"; | |
10397 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
10398 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
10399 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
10400 | s.op->newline(-1) << "}"; | |
10401 | s.op->newline(-1) << "}"; | |
47dd066d | 10402 | |
bc9a523d FCE |
10403 | // This would be technically proper (on those autoconf-detectable |
10404 | // kernels that include this function in tracepoint.h), however we | |
10405 | // already make several calls to synchronze_sched() during our | |
10406 | // shutdown processes. | |
47dd066d | 10407 | |
bc9a523d FCE |
10408 | // s.op->newline() << "if (rc)"; |
10409 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
10410 | // s.op->indent(-1); | |
79189b84 | 10411 | } |
47dd066d WC |
10412 | |
10413 | ||
79189b84 JS |
10414 | void |
10415 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
47dd066d | 10416 | { |
79189b84 JS |
10417 | if (probes.empty()) |
10418 | return; | |
47dd066d | 10419 | |
96b030fe JS |
10420 | s.op->newline() << "/* deregister tracepoint probes */"; |
10421 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
10422 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
10423 | s.op->indent(-1); | |
47dd066d | 10424 | |
bc9a523d | 10425 | // Not necessary: see above. |
47dd066d | 10426 | |
bc9a523d | 10427 | // s.op->newline() << "tracepoint_synchronize_unregister();"; |
79189b84 | 10428 | } |
b20febf3 | 10429 | |
47dd066d | 10430 | |
75ead1f7 | 10431 | struct tracepoint_query : public base_query |
47dd066d | 10432 | { |
75ead1f7 JS |
10433 | tracepoint_query(dwflpp & dw, const string & tracepoint, |
10434 | probe * base_probe, probe_point * base_loc, | |
10435 | vector<derived_probe *> & results): | |
10436 | base_query(dw, "*"), tracepoint(tracepoint), | |
10437 | base_probe(base_probe), base_loc(base_loc), | |
10438 | results(results) {} | |
47dd066d | 10439 | |
75ead1f7 | 10440 | const string& tracepoint; |
47dd066d | 10441 | |
75ead1f7 JS |
10442 | probe * base_probe; |
10443 | probe_point * base_loc; | |
10444 | vector<derived_probe *> & results; | |
f982c59b | 10445 | set<string> probed_names; |
47dd066d | 10446 | |
75ead1f7 JS |
10447 | void handle_query_module(); |
10448 | int handle_query_cu(Dwarf_Die * cudie); | |
10449 | int handle_query_func(Dwarf_Die * func); | |
822a6a3d | 10450 | void query_library (const char *) {} |
576eaefe | 10451 | void query_plt (const char *entry, size_t addr) {} |
b20febf3 | 10452 | |
5c378838 | 10453 | static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q); |
7d007451 | 10454 | static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q); |
75ead1f7 | 10455 | }; |
47dd066d WC |
10456 | |
10457 | ||
10458 | void | |
75ead1f7 | 10459 | tracepoint_query::handle_query_module() |
47dd066d | 10460 | { |
75ead1f7 | 10461 | // look for the tracepoints in each CU |
337b7c44 | 10462 | dw.iterate_over_cus(tracepoint_query_cu, this, false); |
47dd066d WC |
10463 | } |
10464 | ||
10465 | ||
75ead1f7 JS |
10466 | int |
10467 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
47dd066d | 10468 | { |
75ead1f7 | 10469 | dw.focus_on_cu (cudie); |
d906ab9d | 10470 | dw.mod_info->get_symtab(this); |
47dd066d | 10471 | |
75ead1f7 JS |
10472 | // look at each function to see if it's a tracepoint |
10473 | string function = "stapprobe_" + tracepoint; | |
10474 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
47dd066d WC |
10475 | } |
10476 | ||
10477 | ||
75ead1f7 JS |
10478 | int |
10479 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
47dd066d | 10480 | { |
75ead1f7 | 10481 | dw.focus_on_function (func); |
47dd066d | 10482 | |
60d98537 | 10483 | assert(startswith(dw.function_name, "stapprobe_")); |
75ead1f7 | 10484 | string tracepoint_instance = dw.function_name.substr(10); |
f982c59b JS |
10485 | |
10486 | // check for duplicates -- sometimes tracepoint headers may be indirectly | |
10487 | // included in more than one of our tracequery modules. | |
10488 | if (!probed_names.insert(tracepoint_instance).second) | |
10489 | return DWARF_CB_OK; | |
10490 | ||
79189b84 JS |
10491 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
10492 | tracepoint_instance, | |
10493 | base_probe, base_loc); | |
10494 | results.push_back (dp); | |
75ead1f7 | 10495 | return DWARF_CB_OK; |
47dd066d WC |
10496 | } |
10497 | ||
10498 | ||
75ead1f7 | 10499 | int |
5c378838 | 10500 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q) |
47dd066d | 10501 | { |
85007c04 | 10502 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 10503 | return q->handle_query_cu(cudie); |
47dd066d WC |
10504 | } |
10505 | ||
10506 | ||
75ead1f7 | 10507 | int |
7d007451 | 10508 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q) |
47dd066d | 10509 | { |
85007c04 | 10510 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 10511 | return q->handle_query_func(func); |
47dd066d WC |
10512 | } |
10513 | ||
10514 | ||
0a6f5a3f | 10515 | struct tracepoint_builder: public derived_probe_builder |
47dd066d | 10516 | { |
0a6f5a3f JS |
10517 | private: |
10518 | dwflpp *dw; | |
10519 | bool init_dw(systemtap_session& s); | |
c9ccb642 FCE |
10520 | void get_tracequery_modules(systemtap_session& s, |
10521 | const vector<string>& headers, | |
10522 | vector<string>& modules); | |
47dd066d | 10523 | |
0a6f5a3f | 10524 | public: |
47dd066d | 10525 | |
0a6f5a3f JS |
10526 | tracepoint_builder(): dw(0) {} |
10527 | ~tracepoint_builder() { delete dw; } | |
47dd066d | 10528 | |
0a6f5a3f JS |
10529 | void build_no_more (systemtap_session& s) |
10530 | { | |
10531 | if (dw && s.verbose > 3) | |
b530b5b3 | 10532 | clog << _("tracepoint_builder releasing dwflpp") << endl; |
0a6f5a3f JS |
10533 | delete dw; |
10534 | dw = NULL; | |
435f53a7 FCE |
10535 | |
10536 | delete_session_module_cache (s); | |
0a6f5a3f | 10537 | } |
47dd066d | 10538 | |
0a6f5a3f JS |
10539 | void build(systemtap_session& s, |
10540 | probe *base, probe_point *location, | |
10541 | literal_map_t const& parameters, | |
10542 | vector<derived_probe*>& finished_results); | |
10543 | }; | |
47dd066d | 10544 | |
47dd066d | 10545 | |
c9ccb642 | 10546 | |
2a0e62a8 | 10547 | // Create (or cache) one or more tracequery .o modules, based upon the |
c9ccb642 FCE |
10548 | // tracepoint-related header files given. Return the generated or cached |
10549 | // modules[]. | |
10550 | ||
10551 | void | |
10552 | tracepoint_builder::get_tracequery_modules(systemtap_session& s, | |
10553 | const vector<string>& headers, | |
10554 | vector<string>& modules) | |
0a6f5a3f | 10555 | { |
c95eddf7 | 10556 | if (s.verbose > 2) |
55e50c24 | 10557 | { |
ce0f6648 | 10558 | clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl; |
55e50c24 JS |
10559 | for (size_t i = 0; i < headers.size(); ++i) |
10560 | clog << " " << headers[i] << endl; | |
10561 | } | |
c95eddf7 | 10562 | |
2a0e62a8 JS |
10563 | map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name |
10564 | // Map the headers to cache .o names. Note that this has side-effects of | |
c9ccb642 FCE |
10565 | // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file, |
10566 | // so we prefer not to repeat this. | |
10567 | vector<string> uncached_headers; | |
10568 | for (size_t i=0; i<headers.size(); i++) | |
2a0e62a8 | 10569 | headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]); |
c9ccb642 FCE |
10570 | |
10571 | // They may be in the cache already. | |
10572 | if (s.use_cache && !s.poison_cache) | |
10573 | for (size_t i=0; i<headers.size(); i++) | |
10574 | { | |
10575 | // see if the cached module exists | |
2a0e62a8 | 10576 | const string& tracequery_path = headers_cache_obj[headers[i]]; |
c9ccb642 FCE |
10577 | if (!tracequery_path.empty() && file_exists(tracequery_path)) |
10578 | { | |
10579 | if (s.verbose > 2) | |
10580 | clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl; | |
47dd066d | 10581 | |
c252fca2 JS |
10582 | // an empty file is a cached failure |
10583 | if (get_file_size(tracequery_path) > 0) | |
10584 | modules.push_back (tracequery_path); | |
c9ccb642 FCE |
10585 | } |
10586 | else | |
10587 | uncached_headers.push_back(headers[i]); | |
10588 | } | |
10589 | else | |
10590 | uncached_headers = headers; | |
f982c59b | 10591 | |
c9ccb642 FCE |
10592 | // If we have nothing left to search for, quit |
10593 | if (uncached_headers.empty()) return; | |
55e50c24 | 10594 | |
c9ccb642 | 10595 | map<string,string> headers_tracequery_src; // header -> C-source code mapping |
55e50c24 | 10596 | |
c9ccb642 FCE |
10597 | // We could query several subsets of headers[] to make this go |
10598 | // faster, but let's KISS and do one at a time. | |
10599 | for (size_t i=0; i<uncached_headers.size(); i++) | |
55e50c24 | 10600 | { |
c9ccb642 FCE |
10601 | const string& header = uncached_headers[i]; |
10602 | ||
10603 | // create a tracequery source file | |
10604 | ostringstream osrc; | |
10605 | ||
10606 | // PR9993: Add extra headers to work around undeclared types in individual | |
10607 | // include/trace/foo.h files | |
10608 | vector<string> short_decls = tracepoint_extra_decls(s, header); | |
10609 | ||
10610 | // add each requested tracepoint header | |
75ae2ec9 | 10611 | size_t root_pos = header.rfind("include/"); |
832f100d | 10612 | short_decls.push_back(string("#include <") + |
75ae2ec9 | 10613 | ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) + |
d4393459 | 10614 | string(">")); |
f982c59b | 10615 | |
c9ccb642 FCE |
10616 | osrc << "#ifdef CONFIG_TRACEPOINTS" << endl; |
10617 | osrc << "#include <linux/tracepoint.h>" << endl; | |
832f100d JS |
10618 | |
10619 | // the kernel has changed this naming a few times, previously TPPROTO, | |
10620 | // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest. | |
10621 | osrc << "#ifndef PARAMS" << endl; | |
10622 | osrc << "#define PARAMS(args...) args" << endl; | |
10623 | osrc << "#endif" << endl; | |
10624 | ||
c9ccb642 FCE |
10625 | // override DECLARE_TRACE to synthesize probe functions for us |
10626 | osrc << "#undef DECLARE_TRACE" << endl; | |
10627 | osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl; | |
10628 | osrc << " void stapprobe_##name(proto) {}" << endl; | |
832f100d | 10629 | |
c9ccb642 FCE |
10630 | // 2.6.35 added the NOARGS variant, but it's the same for us |
10631 | osrc << "#undef DECLARE_TRACE_NOARGS" << endl; | |
10632 | osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl; | |
10633 | osrc << " DECLARE_TRACE(name, void, )" << endl; | |
832f100d JS |
10634 | |
10635 | // 2.6.38 added the CONDITION variant, which can also just redirect | |
10636 | osrc << "#undef DECLARE_TRACE_CONDITION" << endl; | |
10637 | osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl; | |
10638 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; | |
10639 | ||
c9ccb642 FCE |
10640 | // older tracepoints used DEFINE_TRACE, so redirect that too |
10641 | osrc << "#undef DEFINE_TRACE" << endl; | |
10642 | osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl; | |
832f100d JS |
10643 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; |
10644 | ||
c9ccb642 FCE |
10645 | // add the specified decls/#includes |
10646 | for (unsigned z=0; z<short_decls.size(); z++) | |
10647 | osrc << "#undef TRACE_INCLUDE_FILE\n" | |
10648 | << "#undef TRACE_INCLUDE_PATH\n" | |
10649 | << short_decls[z] << "\n"; | |
10650 | ||
10651 | // finish up the module source | |
10652 | osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl; | |
47dd066d | 10653 | |
c9ccb642 FCE |
10654 | // save the source file away |
10655 | headers_tracequery_src[header] = osrc.str(); | |
55e50c24 | 10656 | } |
f982c59b | 10657 | |
c9ccb642 | 10658 | // now build them all together |
2a0e62a8 | 10659 | map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src); |
47dd066d | 10660 | |
c9ccb642 | 10661 | // now plop them into the cache |
b278033a | 10662 | if (s.use_cache) |
c9ccb642 FCE |
10663 | for (size_t i=0; i<uncached_headers.size(); i++) |
10664 | { | |
10665 | const string& header = uncached_headers[i]; | |
2a0e62a8 JS |
10666 | const string& tracequery_obj = tracequery_objs[header]; |
10667 | const string& tracequery_path = headers_cache_obj[header]; | |
10668 | if (tracequery_obj !="" && file_exists(tracequery_obj)) | |
c9ccb642 | 10669 | { |
2a0e62a8 | 10670 | copy_file(tracequery_obj, tracequery_path, s.verbose > 2); |
c9ccb642 FCE |
10671 | modules.push_back (tracequery_path); |
10672 | } | |
c252fca2 JS |
10673 | else |
10674 | // cache an empty file for failures | |
10675 | copy_file("/dev/null", tracequery_path, s.verbose > 2); | |
c9ccb642 | 10676 | } |
f982c59b JS |
10677 | } |
10678 | ||
10679 | ||
d4393459 | 10680 | |
f982c59b JS |
10681 | bool |
10682 | tracepoint_builder::init_dw(systemtap_session& s) | |
10683 | { | |
10684 | if (dw != NULL) | |
10685 | return true; | |
10686 | ||
10687 | vector<string> tracequery_modules; | |
55e50c24 | 10688 | vector<string> system_headers; |
f982c59b JS |
10689 | |
10690 | glob_t trace_glob; | |
d4393459 FCE |
10691 | |
10692 | // find kernel_source_tree | |
10693 | if (s.kernel_source_tree == "") | |
f982c59b | 10694 | { |
d4393459 FCE |
10695 | unsigned found; |
10696 | DwflPtr dwfl_ptr = setup_dwfl_kernel ("kernel", &found, s); | |
10697 | Dwfl *dwfl = dwfl_ptr.get()->dwfl; | |
10698 | if (found) | |
10699 | { | |
10700 | Dwarf_Die *cudie = 0; | |
10701 | Dwarf_Addr bias; | |
10702 | while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL) | |
10703 | { | |
e19ebcf7 | 10704 | assert_no_interrupts(); |
d4393459 FCE |
10705 | Dwarf_Attribute attr; |
10706 | const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr)); | |
36d65b45 | 10707 | if (name) |
d4393459 | 10708 | { |
36d65b45 JS |
10709 | // check that the path actually exists locally before we try to use it |
10710 | if (file_exists(name)) | |
10711 | { | |
10712 | if (s.verbose > 2) | |
10713 | clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl; | |
10714 | s.kernel_source_tree = name; | |
10715 | } | |
10716 | else | |
10717 | { | |
10718 | if (s.verbose > 2) | |
10719 | clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl; | |
10720 | } | |
61f1a63b | 10721 | |
d4393459 FCE |
10722 | break; // skip others; modern Kbuild uses same comp_dir for them all |
10723 | } | |
10724 | } | |
10725 | } | |
10726 | } | |
10727 | ||
10728 | // prefixes | |
10729 | vector<string> glob_prefixes; | |
10730 | glob_prefixes.push_back (s.kernel_build_tree); | |
10731 | if (s.kernel_source_tree != "") | |
10732 | glob_prefixes.push_back (s.kernel_source_tree); | |
10733 | ||
10734 | // suffixes | |
10735 | vector<string> glob_suffixes; | |
10736 | glob_suffixes.push_back("include/trace/events/*.h"); | |
10737 | glob_suffixes.push_back("include/trace/*.h"); | |
d97d428e | 10738 | glob_suffixes.push_back("include/ras/*_event.h"); |
d4393459 | 10739 | glob_suffixes.push_back("arch/x86/kvm/*trace.h"); |
d97d428e FCE |
10740 | glob_suffixes.push_back("arch/x86/kernel/*trace.h"); |
10741 | glob_suffixes.push_back("arch/*/include/asm/trace*.h"); | |
10742 | glob_suffixes.push_back("arch/*/include/asm/trace/*.h"); | |
f89ff3e2 | 10743 | glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h"); |
d97d428e FCE |
10744 | glob_suffixes.push_back("fs/*/*trace*.h"); |
10745 | glob_suffixes.push_back("net/*/*trace*.h"); | |
10746 | glob_suffixes.push_back("sound/pci/hda/*_trace.h"); | |
10747 | glob_suffixes.push_back("drivers/gpu/drm/*_trace.h"); | |
10748 | glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h"); | |
10749 | glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h"); | |
10750 | glob_suffixes.push_back("drivers/usb/host/*trace*.h"); | |
10751 | ||
10752 | // see also tracepoint_extra_decls above | |
d4393459 FCE |
10753 | |
10754 | // compute cartesian product | |
10755 | vector<string> globs; | |
10756 | for (unsigned i=0; i<glob_prefixes.size(); i++) | |
10757 | for (unsigned j=0; j<glob_suffixes.size(); j++) | |
10758 | globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]); | |
10759 | ||
8aa43b8d | 10760 | set<string> duped_headers; |
d4393459 FCE |
10761 | for (unsigned z = 0; z < globs.size(); z++) |
10762 | { | |
10763 | string glob_str = globs[z]; | |
10764 | if (s.verbose > 3) | |
b530b5b3 | 10765 | clog << _("Checking tracepoint glob ") << glob_str << endl; |
d4393459 | 10766 | |
067cc66f CM |
10767 | int r = glob(glob_str.c_str(), 0, NULL, &trace_glob); |
10768 | if (r == GLOB_NOSPACE || r == GLOB_ABORTED) | |
10769 | throw runtime_error("Error globbing tracepoint"); | |
10770 | ||
f982c59b JS |
10771 | for (unsigned i = 0; i < trace_glob.gl_pathc; ++i) |
10772 | { | |
10773 | string header(trace_glob.gl_pathv[i]); | |
10774 | ||
10775 | // filter out a few known "internal-only" headers | |
60d98537 JS |
10776 | if (endswith(header, "/define_trace.h") || |
10777 | endswith(header, "/ftrace.h") || | |
10778 | endswith(header, "/trace_events.h") || | |
10779 | endswith(header, "_event_types.h")) | |
f982c59b JS |
10780 | continue; |
10781 | ||
b1966849 FCE |
10782 | // With headers now plopped under arch/FOO/include/asm/*, |
10783 | // the following logic miss some tracepoints. | |
10784 | #if 0 | |
8aa43b8d JS |
10785 | // skip identical headers from the build and source trees. |
10786 | size_t root_pos = header.rfind("include/"); | |
10787 | if (root_pos != string::npos && | |
10788 | !duped_headers.insert(header.substr(root_pos + 8)).second) | |
10789 | continue; | |
b1966849 | 10790 | #endif |
8aa43b8d | 10791 | |
55e50c24 | 10792 | system_headers.push_back(header); |
f982c59b JS |
10793 | } |
10794 | globfree(&trace_glob); | |
10795 | } | |
10796 | ||
c9ccb642 FCE |
10797 | // Build tracequery modules |
10798 | get_tracequery_modules(s, system_headers, tracequery_modules); | |
55e50c24 | 10799 | |
f982c59b JS |
10800 | // TODO: consider other sources of tracepoint headers too, like from |
10801 | // a command-line parameter or some environment or .systemtaprc | |
47dd066d | 10802 | |
59c11f91 | 10803 | dw = new dwflpp(s, tracequery_modules, true); |
0a6f5a3f JS |
10804 | return true; |
10805 | } | |
47dd066d | 10806 | |
0a6f5a3f JS |
10807 | void |
10808 | tracepoint_builder::build(systemtap_session& s, | |
10809 | probe *base, probe_point *location, | |
10810 | literal_map_t const& parameters, | |
10811 | vector<derived_probe*>& finished_results) | |
10812 | { | |
10813 | if (!init_dw(s)) | |
10814 | return; | |
47dd066d | 10815 | |
75ead1f7 JS |
10816 | string tracepoint; |
10817 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
47dd066d | 10818 | |
75ead1f7 | 10819 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); |
d906ab9d | 10820 | unsigned results_pre = finished_results.size(); |
06de3a04 | 10821 | dw->iterate_over_modules<base_query>(&query_module, &q); |
d906ab9d JL |
10822 | unsigned results_post = finished_results.size(); |
10823 | ||
10824 | // Did we fail to find a match? Let's suggest something! | |
10825 | if (results_pre == results_post) | |
10826 | { | |
10827 | size_t pos; | |
10828 | string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint); | |
10829 | while ((pos = sugs.find("stapprobe_")) != string::npos) | |
10830 | sugs.erase(pos, string("stapprobe_").size()); | |
10831 | if (!sugs.empty()) | |
ece93f53 JL |
10832 | throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)", |
10833 | "no match (similar tracepoints: %s)", | |
d906ab9d JL |
10834 | sugs.find(',') == string::npos, |
10835 | sugs.c_str())); | |
10836 | } | |
47dd066d | 10837 | } |
47dd066d | 10838 | |
e6fe60e7 | 10839 | |
b55bc428 | 10840 | // ------------------------------------------------------------------------ |
bd2b1e68 | 10841 | // Standard tapset registry. |
b55bc428 FCE |
10842 | // ------------------------------------------------------------------------ |
10843 | ||
7a053d3b | 10844 | void |
f8220a7b | 10845 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 10846 | { |
47e0478e | 10847 | register_tapset_been(s); |
93646f4d | 10848 | register_tapset_itrace(s); |
dd0e4fa7 | 10849 | register_tapset_mark(s); |
7a212aa8 | 10850 | register_tapset_procfs(s); |
912e8c59 | 10851 | register_tapset_timers(s); |
8d9609f5 | 10852 | register_tapset_netfilter(s); |
b84779a5 | 10853 | register_tapset_utrace(s); |
b98a8d73 | 10854 | |
7a24d422 | 10855 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 10856 | dwarf_derived_probe::register_patterns(s); |
30a279be | 10857 | |
888af770 FCE |
10858 | // XXX: user-space starter set |
10859 | s.pattern_root->bind_num(TOK_PROCESS) | |
10860 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
f66bb29a | 10861 | ->bind_privilege(pr_all) |
888af770 FCE |
10862 | ->bind(new uprobe_builder ()); |
10863 | s.pattern_root->bind_num(TOK_PROCESS) | |
10864 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
f66bb29a | 10865 | ->bind_privilege(pr_all) |
888af770 FCE |
10866 | ->bind(new uprobe_builder ()); |
10867 | ||
0a6f5a3f JS |
10868 | // kernel tracepoint probes |
10869 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
10870 | ->bind(new tracepoint_builder()); | |
10871 | ||
e6fe60e7 AM |
10872 | // Kprobe based probe |
10873 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
10874 | ->bind(new kprobe_builder()); | |
3c57fe1f JS |
10875 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL) |
10876 | ->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10877 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10878 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
3c57fe1f JS |
10879 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10880 | ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10881 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
10882 | ->bind(new kprobe_builder()); | |
b6371390 JS |
10883 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
10884 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10885 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10886 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
10887 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10888 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
10889 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10890 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
10891 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
dd225250 PS |
10892 | |
10893 | //Hwbkpt based probe | |
b47f3a55 FCE |
10894 | // NB: we formerly registered the probe point types only if the kernel configuration |
10895 | // allowed it. However, we get better error messages if we allow probes to resolve. | |
10896 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10897 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10898 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10899 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10900 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10901 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10902 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10903 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10904 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10905 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10906 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10907 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10908 | // length supported with address only, not symbol names | |
83ea76b1 WC |
10909 | |
10910 | //perf event based probe | |
4763f713 | 10911 | register_tapset_perf(s); |
a29858ef | 10912 | register_tapset_java(s); |
b55bc428 | 10913 | } |
dc38c0ae DS |
10914 | |
10915 | ||
b20febf3 FCE |
10916 | vector<derived_probe_group*> |
10917 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 10918 | { |
b20febf3 | 10919 | vector<derived_probe_group*> g; |
912e8c59 JS |
10920 | |
10921 | #define DOONE(x) \ | |
10922 | if (s. x##_derived_probes) \ | |
10923 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
10924 | |
10925 | // Note that order *is* important here. We want to make sure we | |
10926 | // register (actually run) begin probes before any other probe type | |
10927 | // is run. Similarly, when unregistering probes, we want to | |
10928 | // unregister (actually run) end probes after every other probe type | |
10929 | // has be unregistered. To do the latter, | |
10930 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
10931 | DOONE(be); |
10932 | DOONE(dwarf); | |
888af770 | 10933 | DOONE(uprobe); |
b20febf3 FCE |
10934 | DOONE(timer); |
10935 | DOONE(profile); | |
10936 | DOONE(mark); | |
0a6f5a3f | 10937 | DOONE(tracepoint); |
e6fe60e7 | 10938 | DOONE(kprobe); |
dd225250 | 10939 | DOONE(hwbkpt); |
83ea76b1 | 10940 | DOONE(perf); |
b20febf3 | 10941 | DOONE(hrtimer); |
ce82316f | 10942 | DOONE(procfs); |
8d9609f5 | 10943 | DOONE(netfilter); |
935447c8 DS |
10944 | |
10945 | // Another "order is important" item. We want to make sure we | |
10946 | // "register" the dummy task_finder probe group after all probe | |
10947 | // groups that use the task_finder. | |
10948 | DOONE(utrace); | |
a96d1db0 | 10949 | DOONE(itrace); |
f31a77f5 | 10950 | DOONE(dynprobe); |
e7d4410d | 10951 | DOONE(java); |
935447c8 | 10952 | DOONE(task_finder); |
b20febf3 FCE |
10953 | #undef DOONE |
10954 | return g; | |
46b84a80 | 10955 | } |
73267b89 JS |
10956 | |
10957 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |