]>
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> |
e36387d7 | 40 | #include <cstdarg> |
29e64872 | 41 | #include <cassert> |
1969b5bc | 42 | #include <iomanip> |
f781f849 | 43 | #include <cerrno> |
bd2b1e68 GH |
44 | |
45 | extern "C" { | |
df8fadee | 46 | #include <fcntl.h> |
bd2b1e68 | 47 | #include <elfutils/libdwfl.h> |
7a053d3b | 48 | #include <elfutils/libdw.h> |
77de5e9e GH |
49 | #include <dwarf.h> |
50 | #include <elf.h> | |
51 | #include <obstack.h> | |
b20febf3 | 52 | #include <glob.h> |
30a279be | 53 | #include <fnmatch.h> |
5f0a03a6 | 54 | #include <stdio.h> |
349dc70e | 55 | #include <sys/types.h> |
37001baa | 56 | #include <sys/stat.h> |
aaf7ffe8 | 57 | #include <math.h> |
aff5d390 | 58 | #include <regex.h> |
37001baa | 59 | #include <unistd.h> |
6d5d594e | 60 | #include <wordexp.h> |
4b1ad75e RM |
61 | |
62 | #define __STDC_FORMAT_MACROS | |
63 | #include <inttypes.h> | |
bd2b1e68 | 64 | } |
77de5e9e | 65 | |
508059f0 MW |
66 | // Old elf.h doesn't know about this machine type. |
67 | #ifndef EM_AARCH64 | |
68 | #define EM_AARCH64 183 | |
69 | #endif | |
70 | ||
56e12059 FCE |
71 | |
72 | using namespace std; | |
2171f774 | 73 | using namespace __gnu_cxx; |
56e12059 | 74 | |
47dd066d | 75 | |
b20febf3 FCE |
76 | |
77 | // ------------------------------------------------------------------------ | |
faea5e16 JS |
78 | |
79 | string | |
80 | common_probe_init (derived_probe* p) | |
81 | { | |
26e63673 JS |
82 | assert(p->session_index != (unsigned)-1); |
83 | return "(&stap_probes[" + lex_cast(p->session_index) + "])"; | |
faea5e16 JS |
84 | } |
85 | ||
86 | ||
b20febf3 | 87 | void |
71db462b DS |
88 | common_probe_entryfn_prologue (systemtap_session& s, |
89 | string statestr, string probe, | |
90 | string probe_type, bool overload_processing) | |
b20febf3 | 91 | { |
944c9a7a | 92 | if (s.runtime_usermode_p()) |
ac34f2aa JS |
93 | { |
94 | // If session_state() is NULL, then we haven't even initialized shm yet, | |
95 | // and there's *nothing* for the probe to do. (even alibi is in shm) | |
96 | // So failure skips this whole block through the end of the epilogue. | |
97 | s.op->newline() << "if (likely(session_state())) {"; | |
e481ab2e | 98 | s.op->indent(1); |
ac34f2aa JS |
99 | } |
100 | ||
71db462b | 101 | s.op->newline() << "#ifdef STP_ALIBI"; |
7c3e97f4 | 102 | s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));"; |
71db462b | 103 | s.op->newline() << "#else"; |
653e6a9a | 104 | |
e481ab2e JS |
105 | if (s.runtime_usermode_p()) |
106 | s.op->newline() << "int _stp_saved_errno = errno;"; | |
107 | ||
fee09ad5 | 108 | s.op->newline() << "struct context* __restrict__ c = NULL;"; |
71db462b DS |
109 | s.op->newline() << "#if !INTERRUPTIBLE"; |
110 | s.op->newline() << "unsigned long flags;"; | |
111 | s.op->newline() << "#endif"; | |
b20febf3 | 112 | |
f887a8c9 | 113 | s.op->newline() << "#ifdef STP_TIMING"; |
7c3e97f4 | 114 | s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);"; |
f887a8c9 DS |
115 | s.op->newline() << "#endif"; |
116 | if (overload_processing && !s.runtime_usermode_p()) | |
71db462b | 117 | s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; |
a58d79d0 | 118 | else |
71db462b | 119 | s.op->newline() << "#ifdef STP_TIMING"; |
b20febf3 | 120 | |
f887a8c9 DS |
121 | if (! s.runtime_usermode_p()) |
122 | s.op->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
123 | else | |
124 | { | |
125 | s.op->newline() << "struct timespec timespec_atstart;"; | |
126 | s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC_RAW, ×pec_atstart);"; | |
127 | } | |
71db462b | 128 | s.op->newline() << "#endif"; |
994aac0e | 129 | |
71db462b DS |
130 | s.op->newline() << "#if INTERRUPTIBLE"; |
131 | s.op->newline() << "preempt_disable ();"; | |
132 | s.op->newline() << "#else"; | |
133 | s.op->newline() << "local_irq_save (flags);"; | |
134 | s.op->newline() << "#endif"; | |
b20febf3 | 135 | |
ac3af990 | 136 | if (! s.runtime_usermode_p()) |
71db462b DS |
137 | { |
138 | // Check for enough free enough stack space | |
139 | s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space | |
140 | s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space | |
141 | // XXX: may need porting to platforms where task_struct is not | |
142 | // at bottom of kernel stack NB: see also | |
143 | // CONFIG_DEBUG_STACKOVERFLOW | |
065d5567 | 144 | s.op->newline() << "atomic_inc (skipped_count());"; |
71db462b | 145 | s.op->newline() << "#ifdef STP_TIMING"; |
065d5567 | 146 | s.op->newline() << "atomic_inc (skipped_count_lowstack());"; |
71db462b DS |
147 | s.op->newline() << "#endif"; |
148 | s.op->newline() << "goto probe_epilogue;"; | |
149 | s.op->newline(-1) << "}"; | |
150 | } | |
c931ec8a | 151 | |
065d5567 | 152 | s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")"; |
71db462b DS |
153 | s.op->newline(1) << "goto probe_epilogue;"; |
154 | s.op->indent(-1); | |
9a604fac | 155 | |
1d0e697d DS |
156 | s.op->newline() << "c = _stp_runtime_entryfn_get_context();"; |
157 | if (s.runtime_usermode_p()) | |
158 | { | |
159 | s.op->newline() << "if (!c) {"; | |
160 | s.op->newline(1) << "#if !INTERRUPTIBLE"; | |
065d5567 | 161 | s.op->newline() << "atomic_inc (skipped_count());"; |
1d0e697d DS |
162 | s.op->newline() << "#endif"; |
163 | s.op->newline() << "#ifdef STP_TIMING"; | |
065d5567 | 164 | s.op->newline() << "atomic_inc (skipped_count_reentrant());"; |
1d0e697d DS |
165 | s.op->newline() << "#ifdef DEBUG_REENTRANCY"; |
166 | s.op->newline() << "_stp_warn (\"Skipped %s\\n\", " << probe << "->pp);"; | |
167 | s.op->newline() << "#endif"; | |
168 | s.op->newline() << "#endif"; | |
169 | s.op->newline() << "goto probe_epilogue;"; | |
170 | s.op->newline(-1) << "}"; | |
171 | } | |
172 | ||
71db462b DS |
173 | s.op->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
174 | s.op->newline(1) << "#if !INTERRUPTIBLE"; | |
065d5567 | 175 | s.op->newline() << "atomic_inc (skipped_count());"; |
71db462b DS |
176 | s.op->newline() << "#endif"; |
177 | s.op->newline() << "#ifdef STP_TIMING"; | |
065d5567 | 178 | s.op->newline() << "atomic_inc (skipped_count_reentrant());"; |
71db462b DS |
179 | s.op->newline() << "#ifdef DEBUG_REENTRANCY"; |
180 | s.op->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
26e63673 | 181 | << probe << "->pp, c->probe_point ?: \"?\", smp_processor_id());"; |
c12d974f FCE |
182 | // NB: There is a conceivable race condition here with reading |
183 | // c->probe_point, knowing that this other probe is sort of running. | |
184 | // However, in reality, it's interrupted. Plus even if it were able | |
185 | // to somehow start again, and stop before we read c->probe_point, | |
186 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
71db462b DS |
187 | s.op->newline() << "#endif"; |
188 | s.op->newline() << "#endif"; | |
189 | s.op->newline() << "atomic_dec (& c->busy);"; | |
190 | s.op->newline() << "goto probe_epilogue;"; | |
191 | s.op->newline(-1) << "}"; | |
192 | s.op->newline(); | |
193 | s.op->newline() << "c->last_stmt = 0;"; | |
194 | s.op->newline() << "c->last_error = 0;"; | |
195 | s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter | |
196 | s.op->newline() << "c->uregs = 0;"; | |
197 | s.op->newline() << "c->kregs = 0;"; | |
198 | s.op->newline() << "#if defined __ia64__"; | |
199 | s.op->newline() << "c->unwaddr = 0;"; | |
200 | s.op->newline() << "#endif"; | |
f4d70a33 JS |
201 | if (s.runtime_usermode_p()) |
202 | s.op->newline() << "c->probe_index = " << probe << "->index;"; | |
71db462b DS |
203 | s.op->newline() << "c->probe_point = " << probe << "->pp;"; |
204 | s.op->newline() << "#ifdef STP_NEED_PROBE_NAME"; | |
205 | s.op->newline() << "c->probe_name = " << probe << "->pn;"; | |
206 | s.op->newline() << "#endif"; | |
207 | s.op->newline() << "c->probe_type = " << probe_type << ";"; | |
6dceb5c9 | 208 | // reset Individual Probe State union |
71db462b | 209 | s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));"; |
e04b5d74 | 210 | s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;"; |
71db462b DS |
211 | s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp |
212 | s.op->newline() << "c->regparm = 0;"; | |
213 | s.op->newline() << "#endif"; | |
e0a17418 | 214 | |
152fa051 LB |
215 | if(!s.suppress_time_limits){ |
216 | s.op->newline() << "#if INTERRUPTIBLE"; | |
217 | s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
218 | s.op->newline() << "#else"; | |
219 | s.op->newline() << "c->actionremaining = MAXACTION;"; | |
220 | s.op->newline() << "#endif"; | |
221 | } | |
9915575b FCE |
222 | // NB: The following would actually be incorrect. |
223 | // That's because cycles_sum/cycles_base values are supposed to survive | |
224 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
225 | // cycles), the values will be reset. | |
226 | /* | |
71db462b DS |
227 | s.op->newline() << "#ifdef STP_OVERLOAD"; |
228 | s.op->newline() << "c->cycles_sum = 0;"; | |
229 | s.op->newline() << "c->cycles_base = 0;"; | |
230 | s.op->newline() << "#endif"; | |
9915575b | 231 | */ |
44cfbe25 SM |
232 | |
233 | s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)"; | |
234 | s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;"; | |
235 | s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;"; | |
236 | s.op->newline() << "#endif"; | |
b20febf3 | 237 | } |
9a604fac | 238 | |
a44a0785 | 239 | |
b20febf3 | 240 | void |
f887a8c9 DS |
241 | common_probe_entryfn_epilogue (systemtap_session& s, |
242 | bool overload_processing) | |
b20febf3 | 243 | { |
f887a8c9 DS |
244 | if (overload_processing && !s.runtime_usermode_p()) |
245 | s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
a58d79d0 | 246 | else |
f887a8c9 DS |
247 | s.op->newline() << "#ifdef STP_TIMING"; |
248 | s.op->newline() << "{"; | |
249 | s.op->indent(1); | |
250 | if (! s.runtime_usermode_p()) | |
251 | { | |
252 | s.op->newline() << "cycles_t cycles_atend = get_cycles ();"; | |
253 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
254 | // fewer, if the hardware counter rolls over really quickly. We | |
255 | // handle 32-bit wraparound here. | |
256 | s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
257 | s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
258 | s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
259 | s.op->indent(-1); | |
260 | } | |
261 | else | |
262 | { | |
263 | s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;"; | |
264 | s.op->newline() << "long cycles_elapsed;"; | |
265 | s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC_RAW, ×pec_atend);"; | |
266 | s.op->newline() << "_stp_timespec_sub(×pec_atend, ×pec_atstart, ×pec_elapsed);"; | |
267 | // 'cycles_elapsed' is really elapsed nanoseconds | |
268 | s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;"; | |
269 | } | |
270 | ||
271 | s.op->newline() << "#ifdef STP_TIMING"; | |
272 | s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);"; | |
273 | s.op->newline() << "#endif"; | |
274 | ||
275 | if (overload_processing && !s.runtime_usermode_p()) | |
276 | { | |
277 | s.op->newline() << "#ifdef STP_OVERLOAD"; | |
278 | s.op->newline() << "{"; | |
a58d79d0 DS |
279 | // If the cycle count has wrapped (cycles_atend > cycles_base), |
280 | // let's go ahead and pretend the interval has been reached. | |
281 | // This should reset cycles_base and cycles_sum. | |
f887a8c9 DS |
282 | s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; |
283 | s.op->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
284 | s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
285 | s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
a58d79d0 DS |
286 | |
287 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
288 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
289 | // has overloaded the system and we need to quit. | |
7baf48e9 FCE |
290 | // NB: this is not suppressible via --suppress-runtime-errors, |
291 | // because this is a system safety metric that we cannot trust | |
292 | // unprivileged users to override. | |
f887a8c9 DS |
293 | s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; |
294 | s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
295 | s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
065d5567 JS |
296 | s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);"; |
297 | s.op->newline() << "atomic_inc (error_count());"; | |
f887a8c9 DS |
298 | s.op->newline(-1) << "}"; |
299 | ||
300 | s.op->newline() << "c->cycles_base = cycles_atend;"; | |
301 | s.op->newline() << "c->cycles_sum = 0;"; | |
302 | s.op->newline(-1) << "}"; | |
303 | s.op->newline(-1) << "}"; | |
304 | s.op->newline() << "#endif"; | |
a58d79d0 | 305 | } |
e57b735a | 306 | |
f887a8c9 DS |
307 | s.op->newline(-1) << "}"; |
308 | s.op->newline() << "#endif"; | |
e57b735a | 309 | |
f887a8c9 DS |
310 | s.op->newline() << "c->probe_point = 0;"; // vacated |
311 | s.op->newline() << "#ifdef STP_NEED_PROBE_NAME"; | |
312 | s.op->newline() << "c->probe_name = 0;"; | |
313 | s.op->newline() << "#endif"; | |
314 | s.op->newline() << "c->probe_type = 0;"; | |
7baf48e9 FCE |
315 | |
316 | ||
f887a8c9 DS |
317 | s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; |
318 | s.op->indent(1); | |
319 | if (s.suppress_handler_errors) // PR 13306 | |
7baf48e9 | 320 | { |
065d5567 | 321 | s.op->newline() << "atomic_inc (error_count());"; |
7baf48e9 FCE |
322 | } |
323 | else | |
324 | { | |
f887a8c9 DS |
325 | s.op->newline() << "if (c->last_stmt != NULL)"; |
326 | s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
327 | s.op->newline(-1) << "else"; | |
328 | s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
329 | s.op->indent(-1); | |
065d5567 JS |
330 | s.op->newline() << "atomic_inc (error_count());"; |
331 | s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {"; | |
332 | s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);"; | |
f887a8c9 DS |
333 | s.op->newline() << "_stp_exit ();"; |
334 | s.op->newline(-1) << "}"; | |
7baf48e9 FCE |
335 | } |
336 | ||
f887a8c9 | 337 | s.op->newline(-1) << "}"; |
7baf48e9 FCE |
338 | |
339 | ||
f887a8c9 | 340 | s.op->newline() << "atomic_dec (&c->busy);"; |
e57b735a | 341 | |
f887a8c9 DS |
342 | s.op->newline(-1) << "probe_epilogue:"; // context is free |
343 | s.op->indent(1); | |
e57b735a | 344 | |
8ca891c4 DS |
345 | // In dyninst mode, we're not done with the context yet, since |
346 | // _stp_error() still needs a context structure (since the log | |
347 | // buffers are stored there). | |
348 | if (!s.runtime_usermode_p()) | |
349 | { | |
e481ab2e | 350 | s.op->newline() << "_stp_runtime_entryfn_put_context(c);"; |
8ca891c4 | 351 | } |
f887a8c9 | 352 | if (! s.suppress_handler_errors) // PR 13306 |
7baf48e9 FCE |
353 | { |
354 | // Check for excessive skip counts. | |
065d5567 JS |
355 | s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {"; |
356 | s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))"; | |
f887a8c9 DS |
357 | s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");"; |
358 | s.op->newline(-1) << "}"; | |
7baf48e9 | 359 | } |
e57b735a | 360 | |
f887a8c9 DS |
361 | s.op->newline() << "#if INTERRUPTIBLE"; |
362 | s.op->newline() << "preempt_enable_no_resched ();"; | |
363 | s.op->newline() << "#else"; | |
364 | s.op->newline() << "local_irq_restore (flags);"; | |
365 | s.op->newline() << "#endif"; | |
653e6a9a | 366 | |
944c9a7a | 367 | if (s.runtime_usermode_p()) |
ac34f2aa | 368 | { |
e481ab2e | 369 | s.op->newline() << "_stp_runtime_entryfn_put_context(c);"; |
ac34f2aa | 370 | s.op->newline() << "errno = _stp_saved_errno;"; |
ac34f2aa | 371 | } |
e481ab2e JS |
372 | |
373 | s.op->newline() << "#endif // STP_ALIBI"; | |
374 | ||
375 | if (s.runtime_usermode_p()) | |
376 | s.op->newline(-1) << "}"; | |
440f755a | 377 | } |
e57b735a | 378 | |
e57b735a | 379 | |
440f755a | 380 | // ------------------------------------------------------------------------ |
e57b735a | 381 | |
440f755a JS |
382 | // ------------------------------------------------------------------------ |
383 | // Dwarf derived probes. "We apologize for the inconvience." | |
384 | // ------------------------------------------------------------------------ | |
e57b735a | 385 | |
4627ed58 JS |
386 | static const string TOK_KERNEL("kernel"); |
387 | static const string TOK_MODULE("module"); | |
388 | static const string TOK_FUNCTION("function"); | |
389 | static const string TOK_INLINE("inline"); | |
390 | static const string TOK_CALL("call"); | |
4bda987e | 391 | static const string TOK_EXPORTED("exported"); |
4627ed58 JS |
392 | static const string TOK_RETURN("return"); |
393 | static const string TOK_MAXACTIVE("maxactive"); | |
394 | static const string TOK_STATEMENT("statement"); | |
395 | static const string TOK_ABSOLUTE("absolute"); | |
396 | static const string TOK_PROCESS("process"); | |
a794dbeb | 397 | static const string TOK_PROVIDER("provider"); |
4627ed58 JS |
398 | static const string TOK_MARK("mark"); |
399 | static const string TOK_TRACE("trace"); | |
400 | static const string TOK_LABEL("label"); | |
63b4fd14 | 401 | static const string TOK_LIBRARY("library"); |
576eaefe | 402 | static const string TOK_PLT("plt"); |
e7d4410d LB |
403 | static const string TOK_METHOD("method"); |
404 | static const string TOK_CLASS("class");; | |
e57b735a | 405 | |
1adf8ef1 | 406 | static int query_cu (Dwarf_Die * cudie, void * arg); |
6b517475 | 407 | static void query_addr(Dwarf_Addr addr, dwarf_query *q); |
e57b735a | 408 | |
440f755a JS |
409 | // Can we handle this query with just symbol-table info? |
410 | enum dbinfo_reqt | |
411 | { | |
412 | dbr_unknown, | |
413 | dbr_none, // kernel.statement(NUM).absolute | |
414 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
415 | dbr_need_dwarf | |
416 | }; | |
e57b735a | 417 | |
20e4a32c | 418 | |
440f755a JS |
419 | struct base_query; // forward decls |
420 | struct dwarf_query; | |
421 | struct dwflpp; | |
422 | struct symbol_table; | |
20e4a32c | 423 | |
a781f401 | 424 | |
440f755a JS |
425 | struct |
426 | symbol_table | |
427 | { | |
428 | module_info *mod_info; // associated module | |
429 | map<string, func_info*> map_by_name; | |
1c6b77e5 JS |
430 | multimap<Dwarf_Addr, func_info*> map_by_addr; |
431 | typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t; | |
440f755a JS |
432 | typedef pair<iterator_t, iterator_t> range_t; |
433 | #ifdef __powerpc__ | |
434 | GElf_Word opd_section; | |
435 | #endif | |
2867a2a1 JS |
436 | void add_symbol(const char *name, bool weak, bool descriptor, |
437 | Dwarf_Addr addr, Dwarf_Addr *high_addr); | |
440f755a | 438 | enum info_status read_symbols(FILE *f, const string& path); |
83ca3872 | 439 | enum info_status read_from_elf_file(const string& path, |
2713ea24 | 440 | systemtap_session &sess); |
83ca3872 | 441 | enum info_status read_from_text_file(const string& path, |
2713ea24 | 442 | systemtap_session &sess); |
440f755a JS |
443 | enum info_status get_from_elf(); |
444 | void prepare_section_rejection(Dwfl_Module *mod); | |
445 | bool reject_section(GElf_Word section); | |
440f755a JS |
446 | void purge_syscall_stubs(); |
447 | func_info *lookup_symbol(const string& name); | |
448 | Dwarf_Addr lookup_symbol_address(const string& name); | |
449 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
3d372d6b | 450 | func_info *get_first_func(); |
7a053d3b | 451 | |
440f755a JS |
452 | symbol_table(module_info *mi) : mod_info(mi) {} |
453 | ~symbol_table(); | |
454 | }; | |
77de5e9e | 455 | |
440f755a JS |
456 | static bool null_die(Dwarf_Die *die) |
457 | { | |
822a6a3d | 458 | static Dwarf_Die null; |
440f755a JS |
459 | return (!die || !memcmp(die, &null, sizeof(null))); |
460 | } | |
c4ce66a1 JS |
461 | |
462 | ||
7a053d3b | 463 | enum |
bd2b1e68 | 464 | function_spec_type |
7a053d3b | 465 | { |
bd2b1e68 GH |
466 | function_alone, |
467 | function_and_file, | |
7a053d3b | 468 | function_file_and_line |
bd2b1e68 GH |
469 | }; |
470 | ||
ec4373ff | 471 | |
bd2b1e68 | 472 | struct dwarf_builder; |
f10534c6 | 473 | struct dwarf_var_expanding_visitor; |
77de5e9e | 474 | |
2930abc7 | 475 | |
b20febf3 FCE |
476 | // XXX: This class is a candidate for subclassing to separate |
477 | // the relocation vs non-relocation variants. Likewise for | |
478 | // kprobe vs kretprobe variants. | |
479 | ||
480 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 481 | { |
b20febf3 FCE |
482 | dwarf_derived_probe (const string& function, |
483 | const string& filename, | |
484 | int line, | |
485 | const string& module, | |
486 | const string& section, | |
487 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 488 | Dwarf_Addr addr, |
b20febf3 FCE |
489 | dwarf_query & q, |
490 | Dwarf_Die* scope_die); | |
20e4a32c | 491 | |
b20febf3 FCE |
492 | string module; |
493 | string section; | |
494 | Dwarf_Addr addr; | |
63b4fd14 | 495 | string path; |
27dc09b1 | 496 | bool has_process; |
2930abc7 | 497 | bool has_return; |
c9bad430 | 498 | bool has_maxactive; |
63b4fd14 | 499 | bool has_library; |
c9bad430 | 500 | long maxactive_val; |
4ad95bbc | 501 | // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition |
b642c901 SC |
502 | string user_path; |
503 | string user_lib; | |
b95e2b79 | 504 | bool access_vars; |
2930abc7 | 505 | |
af234c40 JS |
506 | unsigned saved_longs, saved_strings; |
507 | dwarf_derived_probe* entry_handler; | |
508 | ||
b8da0ad1 | 509 | void printsig (std::ostream &o) const; |
6b66b9f7 | 510 | virtual void join_group (systemtap_session& s); |
3689db05 | 511 | void emit_probe_local_init(systemtap_session& s, translator_output * o); |
d0bfd2ac | 512 | void getargs(std::list<std::string> &arg_set) const; |
0a98fd42 | 513 | |
42e38653 | 514 | void emit_privilege_assertion (translator_output*); |
27dc09b1 DB |
515 | void print_dupe_stamp(ostream& o); |
516 | ||
bd2b1e68 | 517 | // Pattern registration helpers. |
7a053d3b | 518 | static void register_statement_variants(match_node * root, |
27dc09b1 | 519 | dwarf_builder * dw, |
42e38653 | 520 | privilege_t privilege); |
fd6602a0 | 521 | static void register_function_variants(match_node * root, |
27dc09b1 | 522 | dwarf_builder * dw, |
42e38653 | 523 | privilege_t privilege); |
440d9b00 DB |
524 | static void register_function_and_statement_variants(systemtap_session& s, |
525 | match_node * root, | |
27dc09b1 | 526 | dwarf_builder * dw, |
42e38653 | 527 | privilege_t privilege); |
b1615c74 JS |
528 | static void register_sdt_variants(systemtap_session& s, |
529 | match_node * root, | |
530 | dwarf_builder * dw); | |
531 | static void register_plt_variants(systemtap_session& s, | |
532 | match_node * root, | |
533 | dwarf_builder * dw); | |
c4ce66a1 | 534 | static void register_patterns(systemtap_session& s); |
6b66b9f7 JS |
535 | |
536 | protected: | |
537 | dwarf_derived_probe(probe *base, | |
538 | probe_point *location, | |
539 | Dwarf_Addr addr, | |
540 | bool has_return): | |
74fe61bc LB |
541 | derived_probe(base, location), addr(addr), has_process(0), |
542 | has_return(has_return), has_maxactive(0), has_library(0), | |
543 | maxactive_val(0), access_vars(false), saved_longs(0), | |
544 | saved_strings(0), entry_handler(0) | |
6b66b9f7 JS |
545 | {} |
546 | ||
547 | private: | |
d0bfd2ac | 548 | list<string> args; |
8c67c337 | 549 | void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr); |
20c6c071 GH |
550 | }; |
551 | ||
dc38c0ae | 552 | |
6b66b9f7 | 553 | struct uprobe_derived_probe: public dwarf_derived_probe |
6d0f3f0c | 554 | { |
6d0f3f0c | 555 | int pid; // 0 => unrestricted |
0973d815 | 556 | |
6d0f3f0c FCE |
557 | uprobe_derived_probe (const string& function, |
558 | const string& filename, | |
559 | int line, | |
560 | const string& module, | |
6d0f3f0c FCE |
561 | const string& section, |
562 | Dwarf_Addr dwfl_addr, | |
563 | Dwarf_Addr addr, | |
564 | dwarf_query & q, | |
6b66b9f7 JS |
565 | Dwarf_Die* scope_die): |
566 | dwarf_derived_probe(function, filename, line, module, section, | |
567 | dwfl_addr, addr, q, scope_die), pid(0) | |
568 | {} | |
6d0f3f0c | 569 | |
0973d815 FCE |
570 | // alternate constructor for process(PID).statement(ADDR).absolute |
571 | uprobe_derived_probe (probe *base, | |
572 | probe_point *location, | |
573 | int pid, | |
574 | Dwarf_Addr addr, | |
6b66b9f7 JS |
575 | bool has_return): |
576 | dwarf_derived_probe(base, location, addr, has_return), pid(pid) | |
577 | {} | |
9ace370f | 578 | |
6d0f3f0c | 579 | void join_group (systemtap_session& s); |
2865d17a | 580 | |
42e38653 | 581 | void emit_privilege_assertion (translator_output*); |
8f6d8c2b | 582 | void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); } |
c0f84e7b SC |
583 | void getargs(std::list<std::string> &arg_set) const; |
584 | void saveargs(int nargs); | |
585 | private: | |
586 | list<string> args; | |
6d0f3f0c FCE |
587 | }; |
588 | ||
dc38c0ae DS |
589 | struct dwarf_derived_probe_group: public derived_probe_group |
590 | { | |
591 | private: | |
b20febf3 FCE |
592 | multimap<string,dwarf_derived_probe*> probes_by_module; |
593 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
594 | |
595 | public: | |
08b5a50c | 596 | dwarf_derived_probe_group() {} |
b20febf3 FCE |
597 | void enroll (dwarf_derived_probe* probe); |
598 | void emit_module_decls (systemtap_session& s); | |
599 | void emit_module_init (systemtap_session& s); | |
b4be7cbc | 600 | void emit_module_refresh (systemtap_session& s); |
b20febf3 | 601 | void emit_module_exit (systemtap_session& s); |
dc38c0ae DS |
602 | }; |
603 | ||
604 | ||
20c6c071 | 605 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 606 | struct base_query |
20c6c071 | 607 | { |
c4ce66a1 JS |
608 | base_query(dwflpp & dw, literal_map_t const & params); |
609 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 610 | virtual ~base_query() {} |
bd2b1e68 | 611 | |
5227f1ea | 612 | systemtap_session & sess; |
2c384610 | 613 | dwflpp & dw; |
5227f1ea | 614 | |
070764c0 JL |
615 | // Used to keep track of which modules were visited during |
616 | // iterate_over_modules() | |
617 | set<string> visited_modules; | |
618 | ||
bd2b1e68 | 619 | // Parameter extractors. |
86bf665e | 620 | static bool has_null_param(literal_map_t const & params, |
888af770 | 621 | string const & k); |
86bf665e | 622 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 623 | string const & k, string & v); |
86bf665e | 624 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 625 | string const & k, long & v); |
86bf665e | 626 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 627 | string const & k, Dwarf_Addr & v); |
f301a9ff | 628 | static void query_library_callback (void *object, const char *data); |
576eaefe | 629 | static void query_plt_callback (void *object, const char *link, size_t addr); |
f301a9ff | 630 | virtual void query_library (const char *data) = 0; |
576eaefe | 631 | virtual void query_plt (const char *link, size_t addr) = 0; |
84c84ac4 | 632 | |
b55bc428 | 633 | |
2c384610 DS |
634 | // Extracted parameters. |
635 | bool has_kernel; | |
91af0778 FCE |
636 | bool has_module; |
637 | bool has_process; | |
63b4fd14 | 638 | bool has_library; |
576eaefe SC |
639 | bool has_plt; |
640 | bool has_statement; | |
2c384610 | 641 | string module_val; // has_kernel => module_val = "kernel" |
63b4fd14 | 642 | string path; // executable path if module is a .so |
576eaefe | 643 | string plt_val; // has_plt => plt wildcard |
2c384610 DS |
644 | |
645 | virtual void handle_query_module() = 0; | |
646 | }; | |
647 | ||
648 | ||
c4ce66a1 | 649 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
576eaefe | 650 | sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false) |
2c384610 | 651 | { |
91af0778 | 652 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
653 | if (has_kernel) |
654 | module_val = "kernel"; | |
91af0778 FCE |
655 | |
656 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
657 | if (has_module) | |
658 | has_process = false; | |
4baf0e53 | 659 | else |
d0a7f5a9 | 660 | { |
63b4fd14 | 661 | string library_name; |
576eaefe | 662 | long statement_num_val; |
d0a7f5a9 | 663 | has_process = get_string_param(params, TOK_PROCESS, module_val); |
63b4fd14 | 664 | has_library = get_string_param (params, TOK_LIBRARY, library_name); |
576eaefe SC |
665 | if ((has_plt = has_null_param (params, TOK_PLT))) |
666 | plt_val = "*"; | |
667 | else has_plt = get_string_param (params, TOK_PLT, plt_val); | |
18418d34 SC |
668 | if (has_plt) |
669 | sess.consult_symtab = true; | |
576eaefe SC |
670 | has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val); |
671 | ||
84c84ac4 | 672 | if (has_process) |
05fb3e0c | 673 | module_val = find_executable (module_val, sess.sysroot, sess.sysenv); |
84c84ac4 SC |
674 | if (has_library) |
675 | { | |
676 | if (! contains_glob_chars (library_name)) | |
677 | { | |
05fb3e0c WF |
678 | path = path_remove_sysroot(sess, module_val); |
679 | module_val = find_executable (library_name, sess.sysroot, | |
680 | sess.sysenv, "LD_LIBRARY_PATH"); | |
47e226ed SC |
681 | if (module_val.find('/') == string::npos) |
682 | { | |
683 | // We didn't find library_name so use iterate_over_libraries | |
684 | module_val = path; | |
685 | path = library_name; | |
686 | } | |
84c84ac4 SC |
687 | } |
688 | else | |
689 | path = library_name; | |
690 | } | |
d0a7f5a9 | 691 | } |
91af0778 FCE |
692 | |
693 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
694 | } |
695 | ||
c4ce66a1 | 696 | base_query::base_query(dwflpp & dw, const string & module_val) |
576eaefe SC |
697 | : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false), |
698 | module_val(module_val) | |
c4ce66a1 JS |
699 | { |
700 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
701 | // which means that userspace modules won't get any PATH searching. | |
702 | if (module_val.find('/') == string::npos) | |
703 | { | |
704 | has_kernel = (module_val == TOK_KERNEL); | |
705 | has_module = !has_kernel; | |
706 | has_process = false; | |
707 | } | |
708 | else | |
709 | { | |
710 | has_kernel = has_module = false; | |
711 | has_process = true; | |
712 | } | |
713 | } | |
714 | ||
2c384610 | 715 | bool |
86bf665e | 716 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
717 | string const & k) |
718 | { | |
888af770 | 719 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
720 | } |
721 | ||
722 | ||
723 | bool | |
86bf665e | 724 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
725 | string const & k, string & v) |
726 | { | |
727 | return derived_probe_builder::get_param (params, k, v); | |
728 | } | |
729 | ||
730 | ||
731 | bool | |
86bf665e | 732 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
733 | string const & k, long & v) |
734 | { | |
735 | int64_t value; | |
736 | bool present = derived_probe_builder::get_param (params, k, value); | |
737 | v = (long) value; | |
738 | return present; | |
739 | } | |
740 | ||
741 | ||
742 | bool | |
86bf665e | 743 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
744 | string const & k, Dwarf_Addr & v) |
745 | { | |
746 | int64_t value; | |
747 | bool present = derived_probe_builder::get_param (params, k, value); | |
748 | v = (Dwarf_Addr) value; | |
749 | return present; | |
750 | } | |
751 | ||
2c384610 DS |
752 | struct dwarf_query : public base_query |
753 | { | |
e1278bd4 | 754 | dwarf_query(probe * base_probe, |
2c384610 DS |
755 | probe_point * base_loc, |
756 | dwflpp & dw, | |
86bf665e | 757 | literal_map_t const & params, |
b642c901 SC |
758 | vector<derived_probe *> & results, |
759 | const string user_path, | |
760 | const string user_lib); | |
2c384610 | 761 | |
c4ce66a1 | 762 | vector<derived_probe *> & results; |
8f14e444 | 763 | set<string> inlined_non_returnable; // function names |
c4ce66a1 JS |
764 | probe * base_probe; |
765 | probe_point * base_loc; | |
b642c901 SC |
766 | string user_path; |
767 | string user_lib; | |
c4ce66a1 | 768 | |
2c384610 | 769 | virtual void handle_query_module(); |
5f0a03a6 JK |
770 | void query_module_dwarf(); |
771 | void query_module_symtab(); | |
5d5bd369 | 772 | void query_library (const char *data); |
576eaefe | 773 | void query_plt (const char *entry, size_t addr); |
2c384610 | 774 | |
2930abc7 FCE |
775 | void add_probe_point(string const & funcname, |
776 | char const * filename, | |
777 | int line, | |
778 | Dwarf_Die *scope_die, | |
779 | Dwarf_Addr addr); | |
36f9dd1d | 780 | |
857bdfd1 JS |
781 | // Track addresses we've already seen in a given module |
782 | set<Dwarf_Addr> alias_dupes; | |
783 | ||
7fdd3e2c JS |
784 | // Track inlines we've already seen as well |
785 | // NB: this can't be compared just by entrypc, as inlines can overlap | |
786 | set<inline_instance_info> inline_dupes; | |
787 | ||
2930abc7 | 788 | // Extracted parameters. |
7a053d3b | 789 | string function_val; |
20c6c071 GH |
790 | |
791 | bool has_function_str; | |
792 | bool has_statement_str; | |
793 | bool has_function_num; | |
794 | bool has_statement_num; | |
7a053d3b RM |
795 | string statement_str_val; |
796 | string function_str_val; | |
c239d28c GH |
797 | Dwarf_Addr statement_num_val; |
798 | Dwarf_Addr function_num_val; | |
20c6c071 | 799 | |
b8da0ad1 | 800 | bool has_call; |
4bda987e | 801 | bool has_exported; |
b8da0ad1 | 802 | bool has_inline; |
20c6c071 GH |
803 | bool has_return; |
804 | ||
c9bad430 DS |
805 | bool has_maxactive; |
806 | long maxactive_val; | |
807 | ||
20c6c071 GH |
808 | bool has_label; |
809 | string label_val; | |
810 | ||
811 | bool has_relative; | |
812 | long relative_val; | |
813 | ||
37ebca01 FCE |
814 | bool has_absolute; |
815 | ||
467bea43 SC |
816 | bool has_mark; |
817 | ||
5f0a03a6 JK |
818 | enum dbinfo_reqt dbinfo_reqt; |
819 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
820 | ||
7d6d0afc | 821 | void parse_function_spec(const string & spec); |
20c6c071 | 822 | function_spec_type spec_type; |
7d6d0afc | 823 | vector<string> scopes; |
20c6c071 GH |
824 | string function; |
825 | string file; | |
0c8b7d37 | 826 | line_t line_type; |
879eb9e9 | 827 | int line[2]; |
5f0a03a6 | 828 | bool query_done; // Found exact match |
20c6c071 | 829 | |
bd25380d | 830 | set<string> filtered_srcfiles; |
7e1279ea FCE |
831 | |
832 | // Map official entrypc -> func_info object | |
86bf665e TM |
833 | inline_instance_map_t filtered_inlines; |
834 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
835 | bool choose_next_line; |
836 | Dwarf_Addr entrypc_for_next_line; | |
4df79aaf JS |
837 | |
838 | void query_module_functions (); | |
b55bc428 FCE |
839 | }; |
840 | ||
98afd80e | 841 | |
435f53a7 FCE |
842 | static void delete_session_module_cache (systemtap_session& s); // forward decl |
843 | ||
844 | ||
98afd80e | 845 | struct dwarf_builder: public derived_probe_builder |
b55bc428 | 846 | { |
665e1256 | 847 | map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */ |
7a24d422 | 848 | map <string,dwflpp*> user_dw; |
b642c901 SC |
849 | string user_path; |
850 | string user_lib; | |
44ffe90c JL |
851 | |
852 | // Holds modules to suggest functions from. NB: aggregates over | |
853 | // recursive calls to build() when deriving globby probes. | |
854 | set <string> modules_seen; | |
855 | ||
ae2552da | 856 | dwarf_builder() {} |
aa30ccd3 | 857 | |
ae2552da | 858 | dwflpp *get_kern_dw(systemtap_session& sess, const string& module) |
707bf35e | 859 | { |
ea14cf67 FCE |
860 | if (kern_dw[module] == 0) |
861 | kern_dw[module] = new dwflpp(sess, module, true); // might throw | |
ae2552da | 862 | return kern_dw[module]; |
707bf35e JS |
863 | } |
864 | ||
865 | dwflpp *get_user_dw(systemtap_session& sess, const string& module) | |
866 | { | |
ea14cf67 FCE |
867 | if (user_dw[module] == 0) |
868 | user_dw[module] = new dwflpp(sess, module, false); // might throw | |
707bf35e JS |
869 | return user_dw[module]; |
870 | } | |
7a24d422 FCE |
871 | |
872 | /* NB: not virtual, so can be called from dtor too: */ | |
822a6a3d | 873 | void dwarf_build_no_more (bool) |
aa30ccd3 | 874 | { |
435f53a7 FCE |
875 | delete_map(kern_dw); |
876 | delete_map(user_dw); | |
7a24d422 FCE |
877 | } |
878 | ||
879 | void build_no_more (systemtap_session &s) | |
880 | { | |
881 | dwarf_build_no_more (s.verbose > 3); | |
435f53a7 | 882 | delete_session_module_cache (s); |
aa30ccd3 FCE |
883 | } |
884 | ||
e38d6504 RM |
885 | ~dwarf_builder() |
886 | { | |
7a24d422 | 887 | dwarf_build_no_more (false); |
c8959a29 | 888 | } |
aa30ccd3 | 889 | |
5227f1ea | 890 | virtual void build(systemtap_session & sess, |
7a053d3b | 891 | probe * base, |
20c6c071 | 892 | probe_point * location, |
86bf665e | 893 | literal_map_t const & parameters, |
20c6c071 | 894 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
895 | }; |
896 | ||
5111fc3e | 897 | |
e1278bd4 | 898 | dwarf_query::dwarf_query(probe * base_probe, |
20c6c071 GH |
899 | probe_point * base_loc, |
900 | dwflpp & dw, | |
86bf665e | 901 | literal_map_t const & params, |
b642c901 SC |
902 | vector<derived_probe *> & results, |
903 | const string user_path, | |
904 | const string user_lib) | |
c4ce66a1 | 905 | : base_query(dw, params), results(results), |
b642c901 | 906 | base_probe(base_probe), base_loc(base_loc), |
74fe61bc LB |
907 | user_path(user_path), user_lib(user_lib), has_relative(false), |
908 | relative_val(0), choose_next_line(false), entrypc_for_next_line(0) | |
bd2b1e68 GH |
909 | { |
910 | // Reduce the query to more reasonable semantic values (booleans, | |
911 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
912 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
913 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
914 | ||
915 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
916 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
917 | ||
0f336e95 SC |
918 | has_label = get_string_param(params, TOK_LABEL, label_val); |
919 | ||
b8da0ad1 | 920 | has_call = has_null_param(params, TOK_CALL); |
4bda987e | 921 | has_exported = has_null_param(params, TOK_EXPORTED); |
b8da0ad1 | 922 | has_inline = has_null_param(params, TOK_INLINE); |
bd2b1e68 | 923 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 924 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 925 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 926 | has_mark = false; |
37ebca01 | 927 | |
bd2b1e68 | 928 | if (has_function_str) |
7d6d0afc | 929 | parse_function_spec(function_str_val); |
bd2b1e68 | 930 | else if (has_statement_str) |
7d6d0afc | 931 | parse_function_spec(statement_str_val); |
0daad364 | 932 | |
5f0a03a6 JK |
933 | dbinfo_reqt = assess_dbinfo_reqt(); |
934 | query_done = false; | |
0daad364 JS |
935 | } |
936 | ||
937 | ||
440f755a JS |
938 | func_info_map_t * |
939 | get_filtered_functions(dwarf_query *q) | |
940 | { | |
941 | return &q->filtered_functions; | |
942 | } | |
943 | ||
944 | ||
945 | inline_instance_map_t * | |
946 | get_filtered_inlines(dwarf_query *q) | |
947 | { | |
948 | return &q->filtered_inlines; | |
949 | } | |
950 | ||
951 | ||
2c384610 | 952 | void |
5f0a03a6 | 953 | dwarf_query::query_module_dwarf() |
2c384610 DS |
954 | { |
955 | if (has_function_num || has_statement_num) | |
956 | { | |
957 | // If we have module("foo").function(0xbeef) or | |
958 | // module("foo").statement(0xbeef), the address is relative | |
959 | // to the start of the module, so we seek the function | |
960 | // number plus the module's bias. | |
6b517475 JS |
961 | Dwarf_Addr addr = has_function_num ? |
962 | function_num_val : statement_num_val; | |
08d1d520 MW |
963 | |
964 | // These are raw addresses, we need to know what the elf_bias | |
965 | // is to feed it to libdwfl based functions. | |
966 | Dwarf_Addr elf_bias; | |
967 | Elf *elf = dwfl_module_getelf (dw.module, &elf_bias); | |
968 | assert(elf); | |
969 | addr += elf_bias; | |
6b517475 | 970 | query_addr(addr, this); |
2c384610 DS |
971 | } |
972 | else | |
973 | { | |
974 | // Otherwise if we have a function("foo") or statement("foo") | |
975 | // specifier, we have to scan over all the CUs looking for | |
976 | // the function(s) in question | |
977 | assert(has_function_str || has_statement_str); | |
4df79aaf JS |
978 | |
979 | // For simple cases, no wildcard and no source:line, we can do a very | |
980 | // quick function lookup in a module-wide cache. | |
1ffb8bd1 JS |
981 | if (spec_type == function_alone && |
982 | !dw.name_has_wildcard(function) && | |
983 | !startswith(function, "_Z")) | |
4df79aaf JS |
984 | query_module_functions(); |
985 | else | |
337b7c44 | 986 | dw.iterate_over_cus(&query_cu, this, false); |
2c384610 DS |
987 | } |
988 | } | |
989 | ||
5f0a03a6 JK |
990 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
991 | dwarf_query * q); | |
992 | ||
993 | void | |
994 | dwarf_query::query_module_symtab() | |
995 | { | |
996 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
997 | if (dbinfo_reqt == dbr_need_dwarf) | |
998 | return; | |
999 | ||
1000 | module_info *mi = dw.mod_info; | |
1001 | if (dbinfo_reqt == dbr_need_symtab) | |
1002 | { | |
1003 | if (mi->symtab_status == info_unknown) | |
1004 | mi->get_symtab(this); | |
1005 | if (mi->symtab_status == info_absent) | |
1006 | return; | |
1007 | } | |
1008 | ||
1009 | func_info *fi = NULL; | |
1010 | symbol_table *sym_table = mi->sym_table; | |
1011 | ||
1012 | if (has_function_str) | |
1013 | { | |
1014 | // Per dwarf_query::assess_dbinfo_reqt()... | |
1015 | assert(spec_type == function_alone); | |
1016 | if (dw.name_has_wildcard(function_str_val)) | |
1017 | { | |
1018 | // Until we augment the blacklist sufficently... | |
c5810d31 | 1019 | if ((function_str_val.find_first_not_of("*?") == string::npos) && !dw.has_gnu_debugdata()) |
5f0a03a6 JK |
1020 | { |
1021 | // e.g., kernel.function("*") | |
b530b5b3 LB |
1022 | cerr << _F("Error: Pattern '%s' matches every single " |
1023 | "instruction address in the symbol table,\n" | |
1024 | "some of which aren't even functions.\n", function_str_val.c_str()) << endl; | |
5f0a03a6 JK |
1025 | return; |
1026 | } | |
2e67a43b | 1027 | symbol_table::iterator_t iter; |
1c6b77e5 JS |
1028 | for (iter = sym_table->map_by_addr.begin(); |
1029 | iter != sym_table->map_by_addr.end(); | |
2e67a43b | 1030 | ++iter) |
5f0a03a6 | 1031 | { |
1c6b77e5 | 1032 | fi = iter->second; |
5f0a03a6 JK |
1033 | if (!null_die(&fi->die)) |
1034 | continue; // already handled in query_module_dwarf() | |
1035 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
1036 | query_func_info(fi->addr, *fi, this); | |
1037 | } | |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | fi = sym_table->lookup_symbol(function_str_val); | |
2867a2a1 | 1042 | if (fi && !fi->descriptor && null_die(&fi->die)) |
5f0a03a6 JK |
1043 | query_func_info(fi->addr, *fi, this); |
1044 | } | |
1045 | } | |
1046 | else | |
1047 | { | |
1048 | assert(has_function_num || has_statement_num); | |
1049 | // Find the "function" in which the indicated address resides. | |
1050 | Dwarf_Addr addr = | |
1051 | (has_function_num ? function_num_val : statement_num_val); | |
576eaefe | 1052 | if (has_plt) |
3d372d6b SC |
1053 | { |
1054 | // Use the raw address from the .plt | |
1055 | fi = sym_table->get_first_func(); | |
1056 | fi->addr = addr; | |
1057 | } | |
1058 | else | |
1059 | fi = sym_table->get_func_containing_address(addr); | |
1060 | ||
5f0a03a6 JK |
1061 | if (!fi) |
1062 | { | |
2713ea24 CM |
1063 | sess.print_warning(_F("address %#" PRIx64 " out of range for module %s", |
1064 | addr, dw.module_name.c_str())); | |
1065 | return; | |
5f0a03a6 JK |
1066 | } |
1067 | if (!null_die(&fi->die)) | |
1068 | { | |
1069 | // addr looks like it's in the compilation unit containing | |
1070 | // the indicated function, but query_module_dwarf() didn't | |
1071 | // match addr to any compilation unit, so addr must be | |
1072 | // above that cu's address range. | |
2713ea24 CM |
1073 | sess.print_warning(_F("address %#" PRIx64 " maps to no known compilation unit in module %s", |
1074 | addr, dw.module_name.c_str())); | |
5f0a03a6 JK |
1075 | return; |
1076 | } | |
1077 | query_func_info(fi->addr, *fi, this); | |
1078 | } | |
1079 | } | |
1080 | ||
1081 | void | |
1082 | dwarf_query::handle_query_module() | |
1083 | { | |
1c6b77e5 JS |
1084 | bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab; |
1085 | dw.get_module_dwarf(false, report); | |
1086 | ||
1087 | // prebuild the symbol table to resolve aliases | |
1088 | dw.mod_info->get_symtab(this); | |
1089 | ||
857bdfd1 JS |
1090 | // reset the dupe-checking for each new module |
1091 | alias_dupes.clear(); | |
7fdd3e2c | 1092 | inline_dupes.clear(); |
857bdfd1 | 1093 | |
5f0a03a6 JK |
1094 | if (dw.mod_info->dwarf_status == info_present) |
1095 | query_module_dwarf(); | |
1c6b77e5 | 1096 | |
5f0a03a6 | 1097 | // Consult the symbol table if we haven't found all we're looking for. |
c5810d31 LB |
1098 | // asm functions can show up in the symbol table but not in dwarf, |
1099 | // or if we want to check the .gnu_debugdata section | |
1100 | if ((sess.consult_symtab || dw.has_gnu_debugdata()) && !query_done) | |
5f0a03a6 JK |
1101 | query_module_symtab(); |
1102 | } | |
1103 | ||
2c384610 | 1104 | |
7d6d0afc JS |
1105 | void |
1106 | dwarf_query::parse_function_spec(const string & spec) | |
bd2b1e68 | 1107 | { |
1d12a9b2 JS |
1108 | line_type = ABSOLUTE; |
1109 | line[0] = line[1] = 0; | |
1110 | ||
91699a70 | 1111 | size_t src_pos, line_pos, dash_pos, scope_pos; |
bd2b1e68 | 1112 | |
7d6d0afc | 1113 | // look for named scopes |
91699a70 JS |
1114 | scope_pos = spec.rfind("::"); |
1115 | if (scope_pos != string::npos) | |
bd2b1e68 | 1116 | { |
91699a70 JS |
1117 | tokenize_cxx(spec.substr(0, scope_pos), scopes); |
1118 | scope_pos += 2; | |
bd2b1e68 | 1119 | } |
91699a70 JS |
1120 | else |
1121 | scope_pos = 0; | |
bd2b1e68 | 1122 | |
7d6d0afc JS |
1123 | // look for a source separator |
1124 | src_pos = spec.find('@', scope_pos); | |
1125 | if (src_pos == string::npos) | |
bd2b1e68 | 1126 | { |
7d6d0afc JS |
1127 | function = spec.substr(scope_pos); |
1128 | spec_type = function_alone; | |
bd2b1e68 | 1129 | } |
7d6d0afc | 1130 | else |
879eb9e9 | 1131 | { |
7d6d0afc | 1132 | function = spec.substr(scope_pos, src_pos - scope_pos); |
7a053d3b | 1133 | |
7d6d0afc JS |
1134 | // look for a line-number separator |
1135 | line_pos = spec.find_first_of(":+", src_pos); | |
1136 | if (line_pos == string::npos) | |
1137 | { | |
1138 | file = spec.substr(src_pos + 1); | |
1139 | spec_type = function_and_file; | |
1140 | } | |
1141 | else | |
1142 | { | |
1143 | file = spec.substr(src_pos + 1, line_pos - src_pos - 1); | |
1144 | ||
1145 | // classify the line spec | |
1146 | spec_type = function_file_and_line; | |
1147 | if (spec[line_pos] == '+') | |
1148 | line_type = RELATIVE; | |
1149 | else if (spec[line_pos + 1] == '*' && | |
1150 | spec.length() == line_pos + 2) | |
1151 | line_type = WILDCARD; | |
1152 | else | |
1153 | line_type = ABSOLUTE; | |
1154 | ||
1155 | if (line_type != WILDCARD) | |
1156 | try | |
1157 | { | |
1158 | // try to parse either N or N-M | |
1159 | dash_pos = spec.find('-', line_pos + 1); | |
1160 | if (dash_pos == string::npos) | |
1161 | line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1)); | |
1162 | else | |
1163 | { | |
1164 | line_type = RANGE; | |
1165 | line[0] = lex_cast<int>(spec.substr(line_pos + 1, | |
1166 | dash_pos - line_pos - 1)); | |
1167 | line[1] = lex_cast<int>(spec.substr(dash_pos + 1)); | |
1168 | } | |
1169 | } | |
1170 | catch (runtime_error & exn) | |
1171 | { | |
1172 | goto bad; | |
1173 | } | |
1174 | } | |
bd2b1e68 GH |
1175 | } |
1176 | ||
7d6d0afc JS |
1177 | if (function.empty() || |
1178 | (spec_type != function_alone && file.empty())) | |
bd2b1e68 GH |
1179 | goto bad; |
1180 | ||
7d6d0afc | 1181 | if (sess.verbose > 2) |
bd2b1e68 | 1182 | { |
b530b5b3 LB |
1183 | //clog << "parsed '" << spec << "'"; |
1184 | clog << _F("parse '%s'", spec.c_str()); | |
41c262f3 | 1185 | |
7d6d0afc JS |
1186 | if (!scopes.empty()) |
1187 | clog << ", scope '" << scopes[0] << "'"; | |
1188 | for (unsigned i = 1; i < scopes.size(); ++i) | |
1189 | clog << "::'" << scopes[i] << "'"; | |
41c262f3 | 1190 | |
7d6d0afc JS |
1191 | clog << ", func '" << function << "'"; |
1192 | ||
1193 | if (spec_type != function_alone) | |
1194 | clog << ", file '" << file << "'"; | |
1195 | ||
1196 | if (spec_type == function_file_and_line) | |
1197 | { | |
1198 | clog << ", line "; | |
1199 | switch (line_type) | |
1200 | { | |
1201 | case ABSOLUTE: | |
1202 | clog << line[0]; | |
1203 | break; | |
1204 | ||
1205 | case RELATIVE: | |
1206 | clog << "+" << line[0]; | |
1207 | break; | |
1208 | ||
1209 | case RANGE: | |
1210 | clog << line[0] << " - " << line[1]; | |
1211 | break; | |
1212 | ||
1213 | case WILDCARD: | |
1214 | clog << "*"; | |
1215 | break; | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | clog << endl; | |
bd2b1e68 GH |
1220 | } |
1221 | ||
7d6d0afc JS |
1222 | return; |
1223 | ||
1224 | bad: | |
dc09353a | 1225 | throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()), |
7d6d0afc | 1226 | base_probe->tok); |
bd2b1e68 GH |
1227 | } |
1228 | ||
05fb3e0c WF |
1229 | string path_remove_sysroot(const systemtap_session& sess, const string& path) |
1230 | { | |
1231 | size_t pos; | |
1232 | string retval = path; | |
1233 | if (!sess.sysroot.empty() && | |
1234 | (pos = retval.find(sess.sysroot)) != string::npos) | |
1235 | retval.replace(pos, sess.sysroot.length(), "/"); | |
1236 | return retval; | |
1237 | } | |
bd2b1e68 | 1238 | |
36f9dd1d | 1239 | void |
1ffb8bd1 | 1240 | dwarf_query::add_probe_point(const string& dw_funcname, |
b20febf3 | 1241 | const char* filename, |
36f9dd1d | 1242 | int line, |
b20febf3 | 1243 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
1244 | Dwarf_Addr addr) |
1245 | { | |
b20febf3 | 1246 | string reloc_section; // base section for relocation purposes |
27646582 | 1247 | Dwarf_Addr reloc_addr; // relocated |
b20febf3 | 1248 | const string& module = dw.module_name; // "kernel" or other |
1ffb8bd1 | 1249 | string funcname = dw_funcname; |
36f9dd1d | 1250 | |
37ebca01 FCE |
1251 | assert (! has_absolute); // already handled in dwarf_builder::build() |
1252 | ||
576eaefe SC |
1253 | if (!has_plt) |
1254 | reloc_addr = dw.relocate_address(addr, reloc_section); | |
1255 | else | |
1256 | { | |
3d372d6b | 1257 | // Set the reloc_section but use the plt entry for reloc_addr |
576eaefe SC |
1258 | dw.relocate_address(addr, reloc_section); |
1259 | reloc_addr = addr; | |
1260 | } | |
2930abc7 | 1261 | |
1ffb8bd1 | 1262 | // If we originally used the linkage name, then let's call it that way |
1ffb8bd1 JS |
1263 | const char* linkage_name; |
1264 | if (scope_die && startswith (this->function, "_Z") | |
f450a7e3 | 1265 | && (linkage_name = dwarf_linkage_name (scope_die))) |
1ffb8bd1 JS |
1266 | funcname = linkage_name; |
1267 | ||
7f9f3386 FCE |
1268 | if (sess.verbose > 1) |
1269 | { | |
b530b5b3 | 1270 | clog << _("probe ") << funcname << "@" << filename << ":" << line; |
b20febf3 | 1271 | if (string(module) == TOK_KERNEL) |
b530b5b3 | 1272 | clog << _(" kernel"); |
91af0778 | 1273 | else if (has_module) |
b530b5b3 | 1274 | clog << _(" module=") << module; |
91af0778 | 1275 | else if (has_process) |
b530b5b3 | 1276 | clog << _(" process=") << module; |
b20febf3 | 1277 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
b20febf3 | 1278 | clog << " pc=0x" << hex << addr << dec; |
7f9f3386 | 1279 | } |
4baf0e53 | 1280 | |
27646582 | 1281 | bool bad = dw.blacklisted_p (funcname, filename, line, module, |
789448a3 | 1282 | addr, has_return); |
b20febf3 FCE |
1283 | if (sess.verbose > 1) |
1284 | clog << endl; | |
7f9f3386 | 1285 | |
84048984 FCE |
1286 | if (module == TOK_KERNEL) |
1287 | { | |
1288 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
1289 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 1290 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
1291 | } |
1292 | ||
b20febf3 FCE |
1293 | if (! bad) |
1294 | { | |
1a0dbc5a | 1295 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
1296 | |
1297 | if (has_process) | |
1298 | { | |
05fb3e0c | 1299 | string module_tgt = path_remove_sysroot(sess, module); |
6d0f3f0c | 1300 | results.push_back (new uprobe_derived_probe(funcname, filename, line, |
05fb3e0c | 1301 | module_tgt, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1302 | *this, scope_die)); |
1303 | } | |
1304 | else | |
1305 | { | |
1306 | assert (has_kernel || has_module); | |
1307 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 1308 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1309 | *this, scope_die)); |
1310 | } | |
b20febf3 | 1311 | } |
2930abc7 FCE |
1312 | } |
1313 | ||
5f0a03a6 JK |
1314 | enum dbinfo_reqt |
1315 | dwarf_query::assess_dbinfo_reqt() | |
1316 | { | |
1317 | if (has_absolute) | |
1318 | { | |
1319 | // kernel.statement(NUM).absolute | |
1320 | return dbr_none; | |
1321 | } | |
1322 | if (has_inline) | |
1323 | { | |
1324 | // kernel.function("f").inline or module("m").function("f").inline | |
1325 | return dbr_need_dwarf; | |
1326 | } | |
1327 | if (has_function_str && spec_type == function_alone) | |
1328 | { | |
1329 | // kernel.function("f") or module("m").function("f") | |
1330 | return dbr_need_symtab; | |
1331 | } | |
1332 | if (has_statement_num) | |
1333 | { | |
1334 | // kernel.statement(NUM) or module("m").statement(NUM) | |
1335 | // Technically, all we need is the module offset (or _stext, for | |
1336 | // the kernel). But for that we need either the ELF file or (for | |
1337 | // _stext) the symbol table. In either case, the symbol table | |
1338 | // is available, and that allows us to map the NUM (address) | |
1339 | // to a function, which is goodness. | |
1340 | return dbr_need_symtab; | |
1341 | } | |
1342 | if (has_function_num) | |
1343 | { | |
1344 | // kernel.function(NUM) or module("m").function(NUM) | |
1345 | // Need the symbol table so we can back up from NUM to the | |
1346 | // start of the function. | |
1347 | return dbr_need_symtab; | |
1348 | } | |
1349 | // Symbol table tells us nothing about source files or line numbers. | |
1350 | return dbr_need_dwarf; | |
1351 | } | |
2930abc7 FCE |
1352 | |
1353 | ||
b8da0ad1 FCE |
1354 | // The critical determining factor when interpreting a pattern |
1355 | // string is, perhaps surprisingly: "presence of a lineno". The | |
1356 | // presence of a lineno changes the search strategy completely. | |
1357 | // | |
1358 | // Compare the two cases: | |
1359 | // | |
1360 | // 1. {statement,function}(foo@file.c:lineno) | |
1361 | // - find the files matching file.c | |
1362 | // - in each file, find the functions matching foo | |
1363 | // - query the file for line records matching lineno | |
1364 | // - iterate over the line records, | |
1365 | // - and iterate over the functions, | |
1366 | // - if(haspc(function.DIE, line.addr)) | |
1367 | // - if looking for statements: probe(lineno.addr) | |
1368 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
1369 | // | |
1370 | // 2. {statement,function}(foo@file.c) | |
1371 | // - find the files matching file.c | |
1372 | // - in each file, find the functions matching foo | |
1373 | // - probe(function.{entrypc,return,etc.}) | |
1374 | // | |
1375 | // Thus the first decision we make is based on the presence of a | |
1376 | // lineno, and we enter entirely different sets of callbacks | |
1377 | // depending on that decision. | |
1378 | // | |
1379 | // Note that the first case is a generalization fo the second, in that | |
1380 | // we could theoretically search through line records for matching | |
1381 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
1382 | // are already cached elsewhere, so we can do an "index scan" as an | |
1383 | // optimization. | |
7e1279ea | 1384 | |
bd2b1e68 | 1385 | static void |
4cd232e4 | 1386 | query_statement (string const & func, |
20e4a32c | 1387 | char const * file, |
4cd232e4 | 1388 | int line, |
bcc12710 | 1389 | Dwarf_Die *scope_die, |
20e4a32c | 1390 | Dwarf_Addr stmt_addr, |
4cd232e4 | 1391 | dwarf_query * q) |
bd2b1e68 | 1392 | { |
39bcd429 FCE |
1393 | try |
1394 | { | |
cee35f73 | 1395 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 1396 | line, scope_die, stmt_addr); |
39bcd429 FCE |
1397 | } |
1398 | catch (const semantic_error& e) | |
1399 | { | |
1400 | q->sess.print_error (e); | |
1401 | } | |
bd2b1e68 GH |
1402 | } |
1403 | ||
6b517475 JS |
1404 | static void |
1405 | query_addr(Dwarf_Addr addr, dwarf_query *q) | |
1406 | { | |
1407 | dwflpp &dw = q->dw; | |
1408 | ||
08d1d520 MW |
1409 | if (q->sess.verbose > 2) |
1410 | clog << "query_addr 0x" << hex << addr << dec << endl; | |
6b517475 JS |
1411 | |
1412 | // First pick which CU contains this address | |
1413 | Dwarf_Die* cudie = dw.query_cu_containing_address(addr); | |
1414 | if (!cudie) // address could be wildly out of range | |
1415 | return; | |
1416 | dw.focus_on_cu(cudie); | |
1417 | ||
1418 | // Now compensate for the dw bias | |
1419 | addr -= dw.module_bias; | |
1420 | ||
1421 | // Per PR5787, we look up the scope die even for | |
1422 | // statement_num's, for blacklist sensitivity and $var | |
1423 | // resolution purposes. | |
1424 | ||
1425 | // Find the scopes containing this address | |
1426 | vector<Dwarf_Die> scopes = dw.getscopes(addr); | |
1427 | if (scopes.empty()) | |
1428 | return; | |
1429 | ||
1430 | // Look for the innermost containing function | |
1431 | Dwarf_Die *fnscope = NULL; | |
1432 | for (size_t i = 0; i < scopes.size(); ++i) | |
1433 | { | |
1434 | int tag = dwarf_tag(&scopes[i]); | |
1435 | if ((tag == DW_TAG_subprogram && !q->has_inline) || | |
1436 | (tag == DW_TAG_inlined_subroutine && | |
4bda987e | 1437 | !q->has_call && !q->has_return && !q->has_exported)) |
6b517475 JS |
1438 | { |
1439 | fnscope = &scopes[i]; | |
1440 | break; | |
1441 | } | |
1442 | } | |
1443 | if (!fnscope) | |
1444 | return; | |
1445 | dw.focus_on_function(fnscope); | |
1446 | ||
1447 | Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0]; | |
1448 | ||
1449 | const char *file = dwarf_decl_file(fnscope); | |
1450 | int line; | |
1451 | dwarf_decl_line(fnscope, &line); | |
1452 | ||
1453 | // Function probes should reset the addr to the function entry | |
1454 | // and possibly perform prologue searching | |
1455 | if (q->has_function_num) | |
1456 | { | |
1457 | dw.die_entrypc(fnscope, &addr); | |
1458 | if (dwarf_tag(fnscope) == DW_TAG_subprogram && | |
1459 | (q->sess.prologue_searching || q->has_process)) // PR 6871 | |
1460 | { | |
1461 | func_info func; | |
1462 | func.die = *fnscope; | |
1463 | func.name = dw.function_name; | |
1464 | func.decl_file = file; | |
1465 | func.decl_line = line; | |
1466 | func.entrypc = addr; | |
1467 | ||
1468 | func_info_map_t funcs(1, func); | |
1469 | dw.resolve_prologue_endings (funcs); | |
464379bb FCE |
1470 | if (q->has_return) // PR13200 |
1471 | { | |
1472 | if (q->sess.verbose > 2) | |
1473 | clog << "ignoring prologue for .return probes" << endl; | |
1474 | } | |
1475 | else | |
1476 | { | |
1477 | if (funcs[0].prologue_end) | |
1478 | addr = funcs[0].prologue_end; | |
1479 | } | |
6b517475 JS |
1480 | } |
1481 | } | |
1482 | else | |
1483 | { | |
1484 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr)); | |
1485 | if (address_line) | |
1486 | { | |
1487 | file = address_line.linesrc(); | |
1488 | line = address_line.lineno(); | |
1489 | } | |
1490 | ||
1491 | // Verify that a raw address matches the beginning of a | |
1492 | // statement. This is a somewhat lame check that the address | |
1493 | // is at the start of an assembly instruction. Mark probes are in the | |
1494 | // middle of a macro and thus not strictly at a statement beginning. | |
1495 | // Guru mode may override this check. | |
1496 | if (!q->has_mark && (!address_line || address_line.addr() != addr)) | |
1497 | { | |
1498 | stringstream msg; | |
2a97f50b | 1499 | msg << _F("address %#" PRIx64 " does not match the beginning of a statement", |
b530b5b3 | 1500 | addr); |
6b517475 | 1501 | if (address_line) |
2a97f50b | 1502 | msg << _F(" (try %#" PRIx64 ")", address_line.addr()); |
6b517475 | 1503 | else |
b530b5b3 LB |
1504 | msg << _F(" (no line info found for '%s', in module '%s')", |
1505 | dw.cu_name().c_str(), dw.module_name.c_str()); | |
6b517475 | 1506 | if (! q->sess.guru_mode) |
dc09353a | 1507 | throw SEMANTIC_ERROR(msg.str()); |
2713ea24 | 1508 | else |
6b517475 JS |
1509 | q->sess.print_warning(msg.str()); |
1510 | } | |
1511 | } | |
1512 | ||
1513 | // Build a probe at this point | |
1514 | query_statement(dw.function_name, file, line, scope, addr, q); | |
1515 | } | |
1516 | ||
8096dd7d JS |
1517 | static void |
1518 | query_label (string const & func, | |
1519 | char const * label, | |
1520 | char const * file, | |
1521 | int line, | |
1522 | Dwarf_Die *scope_die, | |
1523 | Dwarf_Addr stmt_addr, | |
1524 | dwarf_query * q) | |
1525 | { | |
6b517475 JS |
1526 | assert (q->has_statement_str || q->has_function_str); |
1527 | ||
8096dd7d JS |
1528 | size_t i = q->results.size(); |
1529 | ||
1530 | // weed out functions whose decl_file isn't one of | |
1531 | // the source files that we actually care about | |
6b517475 | 1532 | if (q->spec_type != function_alone && |
8096dd7d JS |
1533 | q->filtered_srcfiles.count(file) == 0) |
1534 | return; | |
1535 | ||
1536 | query_statement(func, file, line, scope_die, stmt_addr, q); | |
1537 | ||
c72aa911 JS |
1538 | // after the fact, insert the label back into the derivation chain |
1539 | probe_point::component* ppc = | |
1540 | new probe_point::component(TOK_LABEL, new literal_string (label)); | |
1541 | for (; i < q->results.size(); ++i) | |
1542 | { | |
1543 | derived_probe* p = q->results[i]; | |
1544 | probe_point* pp = new probe_point(*p->locations[0]); | |
1545 | pp->components.push_back (ppc); | |
1546 | p->base = p->base->create_alias(p->locations[0], pp); | |
1547 | } | |
8096dd7d JS |
1548 | } |
1549 | ||
7e1279ea | 1550 | static void |
3e961ba6 | 1551 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
1552 | dwarf_query * q) |
1553 | { | |
b6581717 | 1554 | try |
7e1279ea | 1555 | { |
8f14e444 FCE |
1556 | assert (! q->has_return); // checked by caller already |
1557 | if (q->sess.verbose>2) | |
b530b5b3 LB |
1558 | clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n", |
1559 | ii.entrypc, ii.name.c_str()); | |
8f14e444 FCE |
1560 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
1561 | &ii.die, ii.entrypc, q); | |
7e1279ea | 1562 | } |
b6581717 | 1563 | catch (semantic_error &e) |
7e1279ea | 1564 | { |
b6581717 | 1565 | q->sess.print_error (e); |
7e1279ea FCE |
1566 | } |
1567 | } | |
1568 | ||
1569 | static void | |
1570 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 1571 | func_info & fi, |
7e1279ea FCE |
1572 | dwarf_query * q) |
1573 | { | |
b6581717 | 1574 | try |
7e1279ea | 1575 | { |
b6581717 GH |
1576 | if (q->has_return) |
1577 | { | |
1578 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
1579 | // kretprobe based on the entrypc in this case. | |
464379bb FCE |
1580 | if (fi.prologue_end != 0 && q->has_return) // PR13200 |
1581 | { | |
1582 | if (q->sess.verbose > 2) | |
1583 | clog << "ignoring prologue for .return probes" << endl; | |
1584 | } | |
20e4a32c | 1585 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
1586 | &fi.die, entrypc, q); |
1587 | } | |
1588 | else | |
1589 | { | |
35dc8b04 | 1590 | if (fi.prologue_end != 0) |
44f75386 | 1591 | { |
44f75386 FCE |
1592 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1593 | &fi.die, fi.prologue_end, q); | |
1594 | } | |
1595 | else | |
1596 | { | |
1597 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
1598 | &fi.die, entrypc, q); | |
1599 | } | |
b6581717 | 1600 | } |
7e1279ea | 1601 | } |
b6581717 | 1602 | catch (semantic_error &e) |
7e1279ea | 1603 | { |
b6581717 | 1604 | q->sess.print_error (e); |
7e1279ea FCE |
1605 | } |
1606 | } | |
1607 | ||
1608 | ||
bd4b874d SC |
1609 | static void |
1610 | query_srcfile_label (const dwarf_line_t& line, void * arg) | |
1611 | { | |
1612 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1613 | ||
1614 | Dwarf_Addr addr = line.addr(); | |
1615 | ||
1616 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1617 | i != q->filtered_functions.end(); ++i) | |
1618 | if (q->dw.die_has_pc (i->die, addr)) | |
f09d0d1e JS |
1619 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, |
1620 | q, query_label); | |
1621 | ||
1622 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1623 | i != q->filtered_inlines.end(); ++i) | |
1624 | if (q->dw.die_has_pc (i->die, addr)) | |
1625 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1626 | q, query_label); | |
bd4b874d SC |
1627 | } |
1628 | ||
7e1279ea | 1629 | static void |
86bf665e | 1630 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
1631 | { |
1632 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1633 | ||
86bf665e | 1634 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 1635 | |
86bf665e | 1636 | int lineno = line.lineno(); |
847bf07f | 1637 | |
86bf665e | 1638 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
1639 | i != q->filtered_functions.end(); ++i) |
1640 | { | |
3e961ba6 | 1641 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1642 | { |
b0ee93c4 | 1643 | if (q->sess.verbose>3) |
b530b5b3 | 1644 | clog << _("function DIE lands on srcfile\n"); |
4cd232e4 | 1645 | if (q->has_statement_str) |
f5958c8f JS |
1646 | { |
1647 | Dwarf_Die scope; | |
1648 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1649 | query_statement (i->name, i->decl_file, | |
1650 | lineno, // NB: not q->line ! | |
1651 | &scope, addr, q); | |
1652 | } | |
4cd232e4 | 1653 | else |
3e961ba6 | 1654 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 1655 | } |
20e4a32c RM |
1656 | } |
1657 | ||
86bf665e | 1658 | for (inline_instance_map_t::iterator i |
897820ca GH |
1659 | = q->filtered_inlines.begin(); |
1660 | i != q->filtered_inlines.end(); ++i) | |
1661 | { | |
3e961ba6 | 1662 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1663 | { |
b0ee93c4 | 1664 | if (q->sess.verbose>3) |
b530b5b3 | 1665 | clog << _("inline instance DIE lands on srcfile\n"); |
897820ca | 1666 | if (q->has_statement_str) |
f5958c8f JS |
1667 | { |
1668 | Dwarf_Die scope; | |
1669 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1670 | query_statement (i->name, i->decl_file, | |
d807e0ab JS |
1671 | lineno, // NB: not q->line ! |
1672 | &scope, addr, q); | |
f5958c8f | 1673 | } |
897820ca | 1674 | else |
3e961ba6 | 1675 | query_inline_instance_info (*i, q); |
897820ca | 1676 | } |
20e4a32c | 1677 | } |
7e1279ea FCE |
1678 | } |
1679 | ||
1680 | ||
7fdd3e2c JS |
1681 | bool |
1682 | inline_instance_info::operator<(const inline_instance_info& other) const | |
1683 | { | |
1684 | if (entrypc != other.entrypc) | |
1685 | return entrypc < other.entrypc; | |
1686 | ||
1687 | if (decl_line != other.decl_line) | |
1688 | return decl_line < other.decl_line; | |
1689 | ||
1690 | int cmp = name.compare(other.name); | |
1691 | if (!cmp) | |
1692 | cmp = strcmp(decl_file, other.decl_file); | |
1693 | return cmp < 0; | |
1694 | } | |
1695 | ||
1696 | ||
4fa7b22b | 1697 | static int |
7e1279ea | 1698 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
1699 | { |
1700 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
6b517475 | 1701 | assert (q->has_statement_str || q->has_function_str); |
4bda987e | 1702 | assert (!q->has_call && !q->has_return && !q->has_exported); |
bd2b1e68 | 1703 | |
39bcd429 | 1704 | try |
7a053d3b | 1705 | { |
b0ee93c4 | 1706 | if (q->sess.verbose>2) |
b530b5b3 | 1707 | clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str()); |
7e1279ea | 1708 | |
6b517475 JS |
1709 | Dwarf_Addr entrypc; |
1710 | if (q->dw.die_entrypc (die, &entrypc)) | |
1711 | { | |
1712 | inline_instance_info inl; | |
1713 | inl.die = *die; | |
1714 | inl.name = q->dw.function_name; | |
1715 | inl.entrypc = entrypc; | |
1716 | q->dw.function_file (&inl.decl_file); | |
1717 | q->dw.function_line (&inl.decl_line); | |
1718 | ||
1719 | // make sure that this inline hasn't already | |
1720 | // been matched from a different CU | |
1721 | if (q->inline_dupes.insert(inl).second) | |
1722 | q->filtered_inlines.push_back(inl); | |
1723 | } | |
7e1279ea FCE |
1724 | return DWARF_CB_OK; |
1725 | } | |
1726 | catch (const semantic_error& e) | |
1727 | { | |
1728 | q->sess.print_error (e); | |
1729 | return DWARF_CB_ABORT; | |
1730 | } | |
1731 | } | |
bb788f9f | 1732 | |
7e1279ea | 1733 | static int |
2da9cedb | 1734 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 1735 | { |
2da9cedb | 1736 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
6b517475 | 1737 | assert (q->has_statement_str || q->has_function_str); |
bb788f9f | 1738 | |
bd25380d JS |
1739 | // weed out functions whose decl_file isn't one of |
1740 | // the source files that we actually care about | |
6b517475 | 1741 | if (q->spec_type != function_alone && |
bd25380d | 1742 | q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0) |
8096dd7d | 1743 | return DWARF_CB_OK; |
bd25380d | 1744 | |
7e1279ea FCE |
1745 | try |
1746 | { | |
7e1279ea FCE |
1747 | q->dw.focus_on_function (func); |
1748 | ||
7d6d0afc JS |
1749 | if (!q->dw.function_scope_matches(q->scopes)) |
1750 | return DWARF_CB_OK; | |
1751 | ||
857bdfd1 JS |
1752 | // make sure that this function address hasn't |
1753 | // already been matched under an aliased name | |
1754 | Dwarf_Addr addr; | |
1755 | if (!q->dw.func_is_inline() && | |
1756 | dwarf_entrypc(func, &addr) == 0 && | |
1757 | !q->alias_dupes.insert(addr).second) | |
1758 | return DWARF_CB_OK; | |
1759 | ||
4bda987e | 1760 | if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported)) |
7e1279ea | 1761 | { |
4bda987e | 1762 | if (q->sess.verbose>3) |
b530b5b3 | 1763 | clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str()); |
4bda987e | 1764 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
7e1279ea | 1765 | } |
8f14e444 FCE |
1766 | else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553 |
1767 | { | |
1768 | q->inlined_non_returnable.insert (q->dw.function_name); | |
1769 | } | |
396afcee | 1770 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 1771 | { |
4bda987e SC |
1772 | if (q->has_exported && !q->dw.func_is_exported ()) |
1773 | return DWARF_CB_OK; | |
6b517475 | 1774 | if (q->sess.verbose>2) |
b530b5b3 | 1775 | clog << _F("selected function %s\n", q->dw.function_name.c_str()); |
6b517475 JS |
1776 | |
1777 | func_info func; | |
1778 | q->dw.function_die (&func.die); | |
1779 | func.name = q->dw.function_name; | |
1780 | q->dw.function_file (&func.decl_file); | |
1781 | q->dw.function_line (&func.decl_line); | |
1782 | ||
1783 | Dwarf_Addr entrypc; | |
1784 | if (q->dw.function_entrypc (&entrypc)) | |
1785 | { | |
1786 | func.entrypc = entrypc; | |
1787 | q->filtered_functions.push_back (func); | |
1788 | } | |
1789 | /* else this function is fully inlined, just ignore it */ | |
7e1279ea | 1790 | } |
39bcd429 | 1791 | return DWARF_CB_OK; |
bd2b1e68 | 1792 | } |
39bcd429 | 1793 | catch (const semantic_error& e) |
bd2b1e68 | 1794 | { |
39bcd429 FCE |
1795 | q->sess.print_error (e); |
1796 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1797 | } |
bd2b1e68 GH |
1798 | } |
1799 | ||
1800 | static int | |
1801 | query_cu (Dwarf_Die * cudie, void * arg) | |
1802 | { | |
20c6c071 | 1803 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
6b517475 JS |
1804 | assert (q->has_statement_str || q->has_function_str); |
1805 | ||
85007c04 | 1806 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 1807 | |
39bcd429 | 1808 | try |
bd2b1e68 | 1809 | { |
7e1279ea | 1810 | q->dw.focus_on_cu (cudie); |
b5d77020 | 1811 | |
b0ee93c4 | 1812 | if (false && q->sess.verbose>2) |
b530b5b3 LB |
1813 | clog << _F("focused on CU '%s', in module '%s'\n", |
1814 | q->dw.cu_name().c_str(), q->dw.module_name.c_str()); | |
d9b516ca | 1815 | |
6b517475 JS |
1816 | q->filtered_srcfiles.clear(); |
1817 | q->filtered_functions.clear(); | |
1818 | q->filtered_inlines.clear(); | |
1819 | ||
1820 | // In this path, we find "abstract functions", record | |
1821 | // information about them, and then (depending on lineno | |
1822 | // matching) possibly emit one or more of the function's | |
1823 | // associated addresses. Unfortunately the control of this | |
1824 | // cannot easily be turned inside out. | |
1825 | ||
1826 | if (q->spec_type != function_alone) | |
39bcd429 | 1827 | { |
6b517475 JS |
1828 | // If we have a pattern string with a filename, we need |
1829 | // to elaborate the srcfile mask in question first. | |
1830 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
1831 | ||
1832 | // If we have a file pattern and *no* srcfile matches, there's | |
1833 | // no need to look further into this CU, so skip. | |
1834 | if (q->filtered_srcfiles.empty()) | |
1835 | return DWARF_CB_OK; | |
1836 | } | |
e4c58386 | 1837 | |
6b517475 JS |
1838 | // Pick up [entrypc, name, DIE] tuples for all the functions |
1839 | // matching the query, and fill in the prologue endings of them | |
1840 | // all in a single pass. | |
5898b6e1 | 1841 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function); |
6b517475 JS |
1842 | if (rc != DWARF_CB_OK) |
1843 | q->query_done = true; | |
1844 | ||
1845 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 | |
1846 | && !q->has_statement_str) // PR 2608 | |
1847 | if (! q->filtered_functions.empty()) | |
1848 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
464379bb FCE |
1849 | // NB: we could skip the resolve_prologue_endings() call here for has_return case (PR13200), |
1850 | // but don't have to. We can resolve the prologue, just not actually use it in query_addr(). | |
6b517475 JS |
1851 | |
1852 | if (q->spec_type == function_file_and_line) | |
1853 | { | |
58b070fb | 1854 | // .statement(...:NN) often gets mixed up with .function(...:NN) |
2713ea24 | 1855 | if (q->has_function_str) |
b530b5b3 LB |
1856 | q->sess.print_warning (_("For probing a particular line, use a " |
1857 | ".statement() probe, not .function()"), | |
af2e341f | 1858 | q->base_probe->tok); |
58b070fb | 1859 | |
6b517475 JS |
1860 | // If we have a pattern string with target *line*, we |
1861 | // have to look at lines in all the matched srcfiles. | |
1862 | void (* callback) (const dwarf_line_t&, void*) = | |
1863 | q->has_label ? query_srcfile_label : query_srcfile_line; | |
1864 | for (set<string>::const_iterator i = q->filtered_srcfiles.begin(); | |
1865 | i != q->filtered_srcfiles.end(); ++i) | |
1866 | q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str, | |
1867 | q->line_type, callback, q->function, q); | |
1868 | } | |
1869 | else if (q->has_label) | |
1870 | { | |
1871 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1872 | i != q->filtered_functions.end(); ++i) | |
1873 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1874 | q, query_label); | |
1875 | ||
1876 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1877 | i != q->filtered_inlines.end(); ++i) | |
1878 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1879 | q, query_label); | |
39bcd429 | 1880 | } |
6b517475 JS |
1881 | else |
1882 | { | |
1883 | // Otherwise, simply probe all resolved functions. | |
1884 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1885 | i != q->filtered_functions.end(); ++i) | |
1886 | query_func_info (i->entrypc, *i, q); | |
1887 | ||
1888 | // And all inline instances (if we're not excluding inlines with ".call") | |
1889 | if (! q->has_call) | |
1890 | for (inline_instance_map_t::iterator i | |
1891 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) | |
1892 | query_inline_instance_info (*i, q); | |
1893 | } | |
39bcd429 | 1894 | return DWARF_CB_OK; |
bd2b1e68 | 1895 | } |
39bcd429 | 1896 | catch (const semantic_error& e) |
bd2b1e68 | 1897 | { |
39bcd429 FCE |
1898 | q->sess.print_error (e); |
1899 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1900 | } |
bd2b1e68 GH |
1901 | } |
1902 | ||
0ce64fb8 | 1903 | |
4df79aaf JS |
1904 | void |
1905 | dwarf_query::query_module_functions () | |
1906 | { | |
1907 | try | |
1908 | { | |
1909 | filtered_srcfiles.clear(); | |
1910 | filtered_functions.clear(); | |
1911 | filtered_inlines.clear(); | |
1912 | ||
1913 | // Collect all module functions so we know which CUs are interesting | |
1914 | int rc = dw.iterate_single_function(query_dwarf_func, this, function); | |
1915 | if (rc != DWARF_CB_OK) | |
1916 | { | |
1917 | query_done = true; | |
1918 | return; | |
1919 | } | |
1920 | ||
1921 | set<void*> used_cus; // by cu->addr | |
1922 | vector<Dwarf_Die> cus; | |
1923 | Dwarf_Die cu_mem; | |
1924 | ||
1925 | for (func_info_map_t::iterator i = filtered_functions.begin(); | |
1926 | i != filtered_functions.end(); ++i) | |
1927 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1928 | used_cus.insert(cu_mem.addr).second) | |
1929 | cus.push_back(cu_mem); | |
1930 | ||
1931 | for (inline_instance_map_t::iterator i = filtered_inlines.begin(); | |
1932 | i != filtered_inlines.end(); ++i) | |
1933 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1934 | used_cus.insert(cu_mem.addr).second) | |
1935 | cus.push_back(cu_mem); | |
1936 | ||
1937 | // Reset the dupes since we didn't actually collect them the first time | |
1938 | alias_dupes.clear(); | |
1939 | inline_dupes.clear(); | |
1940 | ||
1941 | // Run the query again on the individual CUs | |
1942 | for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i) | |
1943 | query_cu(&*i, this); | |
1944 | } | |
1945 | catch (const semantic_error& e) | |
1946 | { | |
1947 | sess.print_error (e); | |
1948 | } | |
1949 | } | |
1950 | ||
1951 | ||
5f0a03a6 JK |
1952 | static void |
1953 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
1954 | { | |
1955 | // Validate the machine code in this elf file against the | |
1956 | // session machine. This is important, in case the wrong kind | |
1957 | // of debuginfo is being automagically processed by elfutils. | |
1958 | // While we can tell i686 apart from x86-64, unfortunately | |
1959 | // we can't help confusing i586 vs i686 (both EM_386). | |
1960 | ||
1961 | Dwarf_Addr bias; | |
1962 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
1963 | // because dwfl_module_getelf can force costly section relocations | |
1964 | // we don't really need, while either will do for this purpose. | |
1965 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
1966 | ?: dwfl_module_getelf (mod, &bias)); | |
1967 | ||
1968 | GElf_Ehdr ehdr_mem; | |
1969 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 1970 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
d8f31d0a | 1971 | assert(em); |
5f0a03a6 JK |
1972 | int elf_machine = em->e_machine; |
1973 | const char* debug_filename = ""; | |
1974 | const char* main_filename = ""; | |
1975 | (void) dwfl_module_info (mod, NULL, NULL, | |
1976 | NULL, NULL, NULL, | |
1977 | & main_filename, | |
1978 | & debug_filename); | |
1979 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
1980 | |
1981 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
1982 | string expect_machine2; | |
5f0a03a6 | 1983 | |
d27e6fd5 | 1984 | // NB: See also the 'uname -m' squashing done in main.cxx. |
5f0a03a6 JK |
1985 | switch (elf_machine) |
1986 | { | |
756c9462 FCE |
1987 | // x86 and ppc are bi-architecture; a 64-bit kernel |
1988 | // can normally run either 32-bit or 64-bit *userspace*. | |
1989 | case EM_386: | |
1990 | expect_machine = "i?86"; | |
1991 | if (! q->has_process) break; // 32-bit kernel/module | |
1992 | /* FALLSTHROUGH */ | |
1993 | case EM_X86_64: | |
1994 | expect_machine2 = "x86_64"; | |
1995 | break; | |
1996 | case EM_PPC: | |
756c9462 | 1997 | case EM_PPC64: |
5a1c472e | 1998 | expect_machine = "powerpc"; |
756c9462 | 1999 | break; |
3fe7d888 | 2000 | case EM_S390: expect_machine = "s390"; break; |
5f0a03a6 | 2001 | case EM_IA_64: expect_machine = "ia64"; break; |
d27e6fd5 | 2002 | case EM_ARM: expect_machine = "arm*"; break; |
86aae990 | 2003 | case EM_AARCH64: expect_machine = "arm64"; break; |
5f0a03a6 JK |
2004 | // XXX: fill in some more of these |
2005 | default: expect_machine = "?"; break; | |
2006 | } | |
2007 | ||
2008 | if (! debug_filename) debug_filename = main_filename; | |
2009 | if (! debug_filename) debug_filename = name; | |
2010 | ||
756c9462 FCE |
2011 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
2012 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
2013 | { |
2014 | stringstream msg; | |
b530b5b3 LB |
2015 | msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'", |
2016 | expect_machine.c_str(), expect_machine2.c_str(), elf_machine, | |
2017 | sess_machine.c_str(), debug_filename); | |
dc09353a | 2018 | throw SEMANTIC_ERROR(msg.str ()); |
5f0a03a6 JK |
2019 | } |
2020 | ||
b57082de | 2021 | if (q->sess.verbose>1) |
2a97f50b | 2022 | clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64 |
b530b5b3 LB |
2023 | " file %s ELF machine %s|%s (code %d)\n", |
2024 | q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end, | |
2025 | q->dw.module_bias, debug_filename, expect_machine.c_str(), | |
2026 | expect_machine2.c_str(), elf_machine); | |
5f0a03a6 | 2027 | } |
1d3a40b6 | 2028 | |
91af0778 FCE |
2029 | |
2030 | ||
2031 | static Dwarf_Addr | |
2032 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
2033 | { | |
2034 | int syments = dwfl_module_getsymtab(m); | |
2035 | assert(syments); | |
2036 | for (int i = 1; i < syments; ++i) | |
2037 | { | |
2038 | GElf_Sym sym; | |
2039 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
2040 | if (name != NULL && strcmp(name, wanted) == 0) | |
2041 | return sym.st_value; | |
2042 | } | |
2043 | ||
2044 | return 0; | |
2045 | } | |
2046 | ||
2047 | ||
2048 | ||
bd2b1e68 | 2049 | static int |
b8da0ad1 | 2050 | query_module (Dwfl_Module *mod, |
91af0778 | 2051 | void **, |
b8da0ad1 | 2052 | const char *name, |
6f4c1275 | 2053 | Dwarf_Addr addr, |
b8da0ad1 | 2054 | void *arg) |
bd2b1e68 | 2055 | { |
91af0778 | 2056 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 2057 | |
39bcd429 | 2058 | try |
e38d6504 | 2059 | { |
91af0778 FCE |
2060 | module_info* mi = q->sess.module_cache->cache[name]; |
2061 | if (mi == 0) | |
2062 | { | |
2063 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
2064 | ||
6f4c1275 FCE |
2065 | mi->mod = mod; |
2066 | mi->addr = addr; | |
91af0778 | 2067 | |
6f4c1275 FCE |
2068 | const char* debug_filename = ""; |
2069 | const char* main_filename = ""; | |
2070 | (void) dwfl_module_info (mod, NULL, NULL, | |
2071 | NULL, NULL, NULL, | |
2072 | & main_filename, | |
2073 | & debug_filename); | |
2074 | ||
ab3ed72d | 2075 | if (debug_filename || main_filename) |
91af0778 | 2076 | { |
6f4c1275 FCE |
2077 | mi->elf_path = debug_filename ?: main_filename; |
2078 | } | |
2079 | else if (name == TOK_KERNEL) | |
2080 | { | |
2081 | mi->dwarf_status = info_absent; | |
91af0778 | 2082 | } |
91af0778 FCE |
2083 | } |
2084 | // OK, enough of that module_info caching business. | |
2085 | ||
5f0a03a6 | 2086 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 2087 | |
39bcd429 FCE |
2088 | // If we have enough information in the pattern to skip a module and |
2089 | // the module does not match that information, return early. | |
b8da0ad1 | 2090 | if (!q->dw.module_name_matches(q->module_val)) |
85007c04 | 2091 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
0cbbf9d1 FCE |
2092 | |
2093 | // Don't allow module("*kernel*") type expressions to match the | |
2094 | // elfutils module "kernel", which we refer to in the probe | |
2095 | // point syntax exclusively as "kernel.*". | |
2096 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
85007c04 | 2097 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
b5d77020 | 2098 | |
5f0a03a6 JK |
2099 | if (mod) |
2100 | validate_module_elf(mod, name, q); | |
2101 | else | |
91af0778 FCE |
2102 | assert(q->has_kernel); // and no vmlinux to examine |
2103 | ||
2104 | if (q->sess.verbose>2) | |
b530b5b3 | 2105 | cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str()); |
91af0778 FCE |
2106 | |
2107 | ||
2108 | // Collect a few kernel addresses. XXX: these belong better | |
2109 | // to the sess.module_info["kernel"] struct. | |
2110 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 2111 | { |
91af0778 FCE |
2112 | if (! q->sess.sym_kprobes_text_start) |
2113 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
2114 | if (! q->sess.sym_kprobes_text_end) | |
2115 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
2116 | if (! q->sess.sym_stext) | |
2117 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
2118 | } |
2119 | ||
47e226ed SC |
2120 | // We either have a wildcard or an unresolved library |
2121 | if (q->has_library && (contains_glob_chars (q->path) | |
2122 | || q->path.find('/') == string::npos)) | |
84c84ac4 SC |
2123 | // handle .library(GLOB) |
2124 | q->dw.iterate_over_libraries (&q->query_library_callback, q); | |
576eaefe SC |
2125 | // .plt is translated to .plt.statement(N). We only want to iterate for the |
2126 | // .plt case | |
2127 | else if (q->has_plt && ! q->has_statement) | |
070764c0 JL |
2128 | { |
2129 | q->dw.iterate_over_plt (q, &q->query_plt_callback); | |
2130 | q->visited_modules.insert(name); | |
2131 | } | |
84c84ac4 | 2132 | else |
070764c0 JL |
2133 | { |
2134 | // search the module for matches of the probe point. | |
2135 | q->handle_query_module(); | |
2136 | q->visited_modules.insert(name); | |
2137 | } | |
bb788f9f | 2138 | |
b8da0ad1 | 2139 | // If we know that there will be no more matches, abort early. |
85007c04 | 2140 | if (q->dw.module_name_final_match(q->module_val) || pending_interrupts) |
b8da0ad1 FCE |
2141 | return DWARF_CB_ABORT; |
2142 | else | |
2143 | return DWARF_CB_OK; | |
7a053d3b | 2144 | } |
39bcd429 | 2145 | catch (const semantic_error& e) |
bd2b1e68 | 2146 | { |
39bcd429 FCE |
2147 | q->sess.print_error (e); |
2148 | return DWARF_CB_ABORT; | |
bd2b1e68 | 2149 | } |
bd2b1e68 GH |
2150 | } |
2151 | ||
35d4ab18 | 2152 | |
84c84ac4 | 2153 | void |
5d5bd369 | 2154 | base_query::query_library_callback (void *q, const char *data) |
84c84ac4 SC |
2155 | { |
2156 | base_query *me = (base_query*)q; | |
5d5bd369 | 2157 | me->query_library (data); |
84c84ac4 SC |
2158 | } |
2159 | ||
2160 | ||
2cbcfa9c | 2161 | string |
51d6bda3 SC |
2162 | query_one_library (const char *library, dwflpp & dw, |
2163 | const string user_lib, probe * base_probe, probe_point *base_loc, | |
2164 | vector<derived_probe *> & results) | |
84c84ac4 | 2165 | { |
47e226ed | 2166 | if (dw.function_name_matches_pattern(library, "*" + user_lib)) |
84c84ac4 | 2167 | { |
05fb3e0c WF |
2168 | string library_path = find_executable (library, "", dw.sess.sysenv, |
2169 | "LD_LIBRARY_PATH"); | |
84c84ac4 SC |
2170 | probe_point* specific_loc = new probe_point(*base_loc); |
2171 | specific_loc->optional = true; | |
2172 | vector<probe_point::component*> derived_comps; | |
2173 | ||
2174 | vector<probe_point::component*>::iterator it; | |
2175 | for (it = specific_loc->components.begin(); | |
2176 | it != specific_loc->components.end(); ++it) | |
2177 | if ((*it)->functor == TOK_LIBRARY) | |
2178 | derived_comps.push_back(new probe_point::component(TOK_LIBRARY, | |
2179 | new literal_string(library_path))); | |
2180 | else | |
2181 | derived_comps.push_back(*it); | |
2182 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2183 | derived_loc->components = derived_comps; | |
8159bf55 | 2184 | probe *new_base = new probe (new probe (base_probe, specific_loc), derived_loc); |
51d6bda3 SC |
2185 | derive_probes(dw.sess, new_base, results); |
2186 | if (dw.sess.verbose > 2) | |
2cbcfa9c JL |
2187 | clog << _("module=") << library_path << endl; |
2188 | return library_path; | |
84c84ac4 | 2189 | } |
2cbcfa9c | 2190 | return ""; |
84c84ac4 SC |
2191 | } |
2192 | ||
2193 | ||
51d6bda3 SC |
2194 | void |
2195 | dwarf_query::query_library (const char *library) | |
2196 | { | |
2cbcfa9c JL |
2197 | string library_path = |
2198 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
2199 | if (!library_path.empty()) | |
2200 | visited_modules.insert(library_path); | |
51d6bda3 SC |
2201 | } |
2202 | ||
576eaefe SC |
2203 | struct plt_expanding_visitor: public var_expanding_visitor |
2204 | { | |
2205 | plt_expanding_visitor(const string & entry): | |
2206 | entry (entry) | |
2207 | { | |
2208 | } | |
2209 | const string & entry; | |
2210 | ||
2211 | void visit_target_symbol (target_symbol* e); | |
2212 | }; | |
2213 | ||
2214 | ||
2215 | void | |
2216 | base_query::query_plt_callback (void *q, const char *entry, size_t address) | |
2217 | { | |
2218 | base_query *me = (base_query*)q; | |
2219 | if (me->dw.function_name_matches_pattern (entry, me->plt_val)) | |
2220 | me->query_plt (entry, address); | |
7f4964f1 | 2221 | me->dw.mod_info->plt_funcs.insert(entry); |
576eaefe SC |
2222 | } |
2223 | ||
2224 | ||
2225 | void | |
2226 | query_one_plt (const char *entry, long addr, dwflpp & dw, | |
2227 | probe * base_probe, probe_point *base_loc, | |
2228 | vector<derived_probe *> & results) | |
2229 | { | |
2230 | probe_point* specific_loc = new probe_point(*base_loc); | |
576eaefe SC |
2231 | vector<probe_point::component*> derived_comps; |
2232 | ||
2233 | if (dw.sess.verbose > 2) | |
2234 | clog << _F("plt entry=%s\n", entry); | |
2235 | ||
2236 | // query_module_symtab requires .plt to recognize that it can set the probe at | |
2237 | // a plt entry so we convert process.plt to process.plt.statement | |
2238 | vector<probe_point::component*>::iterator it; | |
2239 | for (it = specific_loc->components.begin(); | |
2240 | it != specific_loc->components.end(); ++it) | |
2241 | if ((*it)->functor == TOK_PLT) | |
3d372d6b | 2242 | { |
8159bf55 FCE |
2243 | *it = new probe_point::component(TOK_PLT, |
2244 | new literal_string(entry)); | |
3d372d6b SC |
2245 | derived_comps.push_back(*it); |
2246 | derived_comps.push_back(new probe_point::component(TOK_STATEMENT, | |
ac7c8669 | 2247 | new literal_number(addr, true))); |
3d372d6b | 2248 | } |
576eaefe SC |
2249 | else |
2250 | derived_comps.push_back(*it); | |
2251 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2252 | derived_loc->components = derived_comps; | |
02c34e38 FCE |
2253 | probe *new_base = new probe (new probe (base_probe, specific_loc), |
2254 | derived_loc); | |
576eaefe SC |
2255 | string e = string(entry); |
2256 | plt_expanding_visitor pltv (e); | |
2257 | pltv.replace (new_base->body); | |
2258 | derive_probes(dw.sess, new_base, results); | |
2259 | } | |
2260 | ||
2261 | ||
2262 | void | |
2263 | dwarf_query::query_plt (const char *entry, size_t address) | |
2264 | { | |
2265 | query_one_plt (entry, address, dw, base_probe, base_loc, results); | |
2266 | } | |
51d6bda3 | 2267 | |
435f53a7 FCE |
2268 | // This would more naturally fit into elaborate.cxx:semantic_pass_symbols, |
2269 | // but the needed declaration for module_cache is not available there. | |
2270 | // Nor for that matter in session.cxx. Only in this CU is that field ever | |
2271 | // set (in query_module() above), so we clean it up here too. | |
2272 | static void | |
2273 | delete_session_module_cache (systemtap_session& s) | |
2274 | { | |
2275 | if (s.module_cache) { | |
2276 | if (s.verbose > 3) | |
b530b5b3 | 2277 | clog << _("deleting module_cache") << endl; |
435f53a7 FCE |
2278 | delete s.module_cache; |
2279 | s.module_cache = 0; | |
2280 | } | |
2281 | } | |
2282 | ||
2283 | ||
de688825 | 2284 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 2285 | { |
77de5e9e | 2286 | dwarf_query & q; |
bcc12710 | 2287 | Dwarf_Die *scope_die; |
77de5e9e | 2288 | Dwarf_Addr addr; |
8c819921 | 2289 | block *add_block; |
2260f4e3 | 2290 | block *add_call_probe; // synthesized from .return probes with saved $vars |
1630966a JS |
2291 | // NB: tids are not always collected in add_block & add_call_probe, because |
2292 | // gen_kretprobe_saved_return doesn't need them. Thus we need these extra | |
2293 | // *_tid bools for gen_mapped_saved_return to tell what's there. | |
8cc799a5 | 2294 | bool add_block_tid, add_call_probe_tid; |
af234c40 JS |
2295 | unsigned saved_longs, saved_strings; // data saved within kretprobes |
2296 | map<std::string, expression *> return_ts_map; | |
729455a7 | 2297 | vector<Dwarf_Die> scopes; |
3689db05 | 2298 | // probe counter name -> pointer of associated probe |
698de6cc | 2299 | std::set<derived_probe*> perf_counter_refs; |
b95e2b79 | 2300 | bool visited; |
77de5e9e | 2301 | |
de688825 | 2302 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
af234c40 | 2303 | q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL), |
8cc799a5 | 2304 | add_block_tid(false), add_call_probe_tid(false), |
af234c40 | 2305 | saved_longs(0), saved_strings(0), visited(false) {} |
277c21bc | 2306 | expression* gen_mapped_saved_return(expression* e, const string& name); |
140be17a | 2307 | expression* gen_kretprobe_saved_return(expression* e); |
a7999c82 JS |
2308 | void visit_target_symbol_saved_return (target_symbol* e); |
2309 | void visit_target_symbol_context (target_symbol* e); | |
d7f3e0c5 | 2310 | void visit_target_symbol (target_symbol* e); |
bd1fcbad | 2311 | void visit_atvar_op (atvar_op* e); |
c24447be | 2312 | void visit_cast_op (cast_op* e); |
8cc799a5 | 2313 | void visit_entry_op (entry_op* e); |
3689db05 | 2314 | void visit_perf_op (perf_op* e); |
729455a7 JS |
2315 | private: |
2316 | vector<Dwarf_Die>& getscopes(target_symbol *e); | |
77de5e9e GH |
2317 | }; |
2318 | ||
2319 | ||
de688825 | 2320 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 2321 | |
a50de939 | 2322 | |
74fe61bc | 2323 | var_expanding_visitor::var_expanding_visitor (): op() |
a50de939 DS |
2324 | { |
2325 | // FIXME: for the time being, by default we only support plain '$foo | |
2326 | // = bar', not '+=' or any other op= variant. This is fixable, but a | |
2327 | // bit ugly. | |
2328 | // | |
2329 | // If derived classes desire to add additional operator support, add | |
2330 | // new operators to this list in the derived class constructor. | |
2331 | valid_ops.insert ("="); | |
2332 | } | |
2333 | ||
2334 | ||
87214add JS |
2335 | bool |
2336 | var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop, | |
2337 | expression*& lvalue, expression*& rvalue) | |
77de5e9e | 2338 | { |
e57b735a GH |
2339 | // Our job would normally be to require() the left and right sides |
2340 | // into a new assignment. What we're doing is slightly trickier: | |
2341 | // we're pushing a functioncall** onto a stack, and if our left | |
2342 | // child sets the functioncall* for that value, we're going to | |
2343 | // assume our left child was a target symbol -- transformed into a | |
2344 | // set_target_foo(value) call, and it wants to take our right child | |
2345 | // as the argument "value". | |
2346 | // | |
2347 | // This is why some people claim that languages with | |
2348 | // constructor-decomposing case expressions have a leg up on | |
2349 | // visitors. | |
2350 | ||
2351 | functioncall *fcall = NULL; | |
d9b516ca | 2352 | |
a50de939 | 2353 | // Let visit_target_symbol know what operator it should handle. |
87214add JS |
2354 | const string* old_op = op; |
2355 | op = &eop; | |
a50de939 | 2356 | |
e57b735a | 2357 | target_symbol_setter_functioncalls.push (&fcall); |
87214add | 2358 | replace (lvalue); |
e57b735a | 2359 | target_symbol_setter_functioncalls.pop (); |
87214add JS |
2360 | replace (rvalue); |
2361 | ||
2362 | op = old_op; | |
e57b735a GH |
2363 | |
2364 | if (fcall != NULL) | |
77de5e9e | 2365 | { |
e57b735a GH |
2366 | // Our left child is informing us that it was a target variable |
2367 | // and it has been replaced with a set_target_foo() function | |
2368 | // call; we are going to provide that function call -- with the | |
2369 | // right child spliced in as sole argument -- in place of | |
de688825 | 2370 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a | 2371 | |
87214add | 2372 | if (valid_ops.find (eop) == valid_ops.end ()) |
a50de939 DS |
2373 | { |
2374 | // Build up a list of supported operators. | |
2375 | string ops; | |
2376 | std::set<string>::iterator i; | |
b530b5b3 | 2377 | int valid_ops_size = 0; |
a50de939 | 2378 | for (i = valid_ops.begin(); i != valid_ops.end(); i++) |
b530b5b3 | 2379 | { |
a50de939 | 2380 | ops += " " + *i + ","; |
b530b5b3 LB |
2381 | valid_ops_size++; |
2382 | } | |
a50de939 DS |
2383 | ops.resize(ops.size() - 1); // chop off the last ',' |
2384 | ||
2385 | // Throw the error. | |
dc09353a | 2386 | throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s", |
1e41115c | 2387 | "Only the following assign operators are implemented on target variables: %s", |
52c2652f | 2388 | valid_ops_size, ops.c_str()), tok); |
b530b5b3 | 2389 | |
a50de939 | 2390 | } |
e57b735a | 2391 | |
87214add JS |
2392 | assert (lvalue == fcall); |
2393 | if (rvalue) | |
2394 | fcall->args.push_back (rvalue); | |
4ed05b15 | 2395 | provide (fcall); |
87214add | 2396 | return true; |
77de5e9e | 2397 | } |
e57b735a | 2398 | else |
87214add JS |
2399 | return false; |
2400 | } | |
2401 | ||
2402 | ||
2403 | void | |
2404 | var_expanding_visitor::visit_assignment (assignment* e) | |
2405 | { | |
2406 | if (!rewrite_lvalue (e->tok, e->op, e->left, e->right)) | |
2407 | provide (e); | |
2408 | } | |
2409 | ||
2410 | ||
2411 | void | |
2412 | var_expanding_visitor::visit_pre_crement (pre_crement* e) | |
2413 | { | |
2414 | expression *dummy = NULL; | |
2415 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2416 | provide (e); | |
2417 | } | |
2418 | ||
2419 | ||
2420 | void | |
2421 | var_expanding_visitor::visit_post_crement (post_crement* e) | |
2422 | { | |
2423 | expression *dummy = NULL; | |
2424 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2425 | provide (e); | |
2426 | } | |
2427 | ||
2428 | ||
2429 | void | |
2430 | var_expanding_visitor::visit_delete_statement (delete_statement* s) | |
2431 | { | |
2432 | string fakeop = "delete"; | |
2433 | expression *dummy = NULL; | |
2434 | if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy)) | |
2435 | provide (s); | |
e57b735a | 2436 | } |
d9b516ca | 2437 | |
d7f3e0c5 | 2438 | |
30263a73 FCE |
2439 | void |
2440 | var_expanding_visitor::visit_defined_op (defined_op* e) | |
2441 | { | |
2442 | bool resolved = true; | |
2443 | ||
2444 | defined_ops.push (e); | |
2445 | try { | |
2446 | // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is | |
2447 | // defined with a target_symbol* operand, a subsidiary call may attempt to | |
2448 | // rewrite it to a general expression* instead, and require<> happily | |
2449 | // casts to/from void*, causing possible memory corruption. We use | |
2450 | // expression* here, being the general case of rewritten $variable. | |
2451 | expression *foo1 = e->operand; | |
2452 | foo1 = require (foo1); | |
2453 | ||
c69a87e0 | 2454 | // NB: Formerly, we had some curious cases to consider here, depending on what |
30263a73 | 2455 | // various visit_target_symbol() implementations do for successful or |
c69a87e0 FCE |
2456 | // erroneous resolutions. Some would signal a visit_target_symbol failure |
2457 | // with an exception, with a set flag within the target_symbol, or nothing | |
2458 | // at all. | |
30263a73 | 2459 | // |
c69a87e0 FCE |
2460 | // Now, failures always have to be signalled with a |
2461 | // saved_conversion_error being chained to the target_symbol. | |
2462 | // Successes have to result in an attempted rewrite of the | |
850bfddd | 2463 | // target_symbol (via provide()). |
780f11ff | 2464 | // |
c69a87e0 FCE |
2465 | // Edna Mode: "no capes". fche: "no exceptions". |
2466 | ||
30263a73 FCE |
2467 | // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error |
2468 | // | |
2469 | // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error | |
2470 | // | |
2471 | // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic | |
2472 | // dwarf probe to take care of it. | |
2473 | // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor | |
2474 | // to pass through @defined() to the synthetic dwarf probe. | |
780f11ff | 2475 | // |
30263a73 FCE |
2476 | // utrace: success: rewrites to function; failure: semantic_error |
2477 | // | |
850bfddd | 2478 | // procfs: success: rewrites to function; failure: semantic_error |
30263a73 FCE |
2479 | |
2480 | target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1); | |
c69a87e0 | 2481 | if (foo2 && foo2->saved_conversion_error) // failing |
30263a73 | 2482 | resolved = false; |
a45664f4 | 2483 | else if (foo2) // unresolved but not marked failing |
b7aedf26 | 2484 | { |
780f11ff JS |
2485 | // There are some visitors that won't touch certain target_symbols, |
2486 | // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should | |
2487 | // leave it for now so some other visitor can have a chance. | |
b7aedf26 JS |
2488 | e->operand = foo2; |
2489 | provide (e); | |
2490 | return; | |
2491 | } | |
30263a73 FCE |
2492 | else // resolved, rewritten to some other expression type |
2493 | resolved = true; | |
780f11ff | 2494 | } catch (const semantic_error& e) { |
c69a87e0 | 2495 | assert (0); // should not happen |
30263a73 FCE |
2496 | } |
2497 | defined_ops.pop (); | |
2498 | ||
2499 | literal_number* ln = new literal_number (resolved ? 1 : 0); | |
2500 | ln->tok = e->tok; | |
2501 | provide (ln); | |
2502 | } | |
2503 | ||
2504 | ||
5f36109e JS |
2505 | struct dwarf_pretty_print |
2506 | { | |
2507 | dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc, | |
2508 | const string& local, bool userspace_p, | |
2509 | const target_symbol& e): | |
d19a9a82 JS |
2510 | dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL), |
2511 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2512 | { |
2513 | init_ts (e); | |
2514 | dw.type_die_for_local (scopes, pc, local, ts, &base_type); | |
2515 | } | |
2516 | ||
2517 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc, | |
2518 | bool userspace_p, const target_symbol& e): | |
d19a9a82 JS |
2519 | dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL), |
2520 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2521 | { |
2522 | init_ts (e); | |
2523 | dw.type_die_for_return (&scopes[0], pc, ts, &base_type); | |
2524 | } | |
2525 | ||
2526 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer, | |
d19a9a82 | 2527 | bool deref_p, bool userspace_p, const target_symbol& e): |
5f36109e | 2528 | dw(dw), pc(0), pointer(pointer), pointer_type(*type_die), |
d19a9a82 | 2529 | userspace_p(userspace_p), deref_p(deref_p) |
5f36109e JS |
2530 | { |
2531 | init_ts (e); | |
2532 | dw.type_die_for_pointer (type_die, ts, &base_type); | |
2533 | } | |
2534 | ||
2535 | functioncall* expand (); | |
ce83ff57 | 2536 | ~dwarf_pretty_print () { delete ts; } |
5f36109e JS |
2537 | |
2538 | private: | |
2539 | dwflpp& dw; | |
2540 | target_symbol* ts; | |
7d11d8c9 | 2541 | bool print_full; |
5f36109e JS |
2542 | Dwarf_Die base_type; |
2543 | ||
2544 | string local; | |
2545 | vector<Dwarf_Die> scopes; | |
2546 | Dwarf_Addr pc; | |
2547 | ||
2548 | expression* pointer; | |
2549 | Dwarf_Die pointer_type; | |
2550 | ||
d19a9a82 | 2551 | const bool userspace_p, deref_p; |
5f36109e JS |
2552 | |
2553 | void recurse (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2554 | print_format* pf, bool top=false); |
600551ca JS |
2555 | void recurse_bitfield (Dwarf_Die* type, target_symbol* e, |
2556 | print_format* pf); | |
5f36109e | 2557 | void recurse_base (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2558 | print_format* pf); |
5f36109e | 2559 | void recurse_array (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2560 | print_format* pf, bool top); |
5f36109e | 2561 | void recurse_pointer (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2562 | print_format* pf, bool top); |
5f36109e | 2563 | void recurse_struct (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2564 | print_format* pf, bool top); |
5f36109e | 2565 | void recurse_struct_members (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2566 | print_format* pf, int& count); |
bbee5bb8 | 2567 | bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf); |
5f36109e JS |
2568 | |
2569 | void init_ts (const target_symbol& e); | |
2570 | expression* deref (target_symbol* e); | |
c55ea10d | 2571 | bool push_deref (print_format* pf, const string& fmt, target_symbol* e); |
5f36109e JS |
2572 | }; |
2573 | ||
2574 | ||
2575 | void | |
2576 | dwarf_pretty_print::init_ts (const target_symbol& e) | |
2577 | { | |
2578 | // Work with a new target_symbol so we can modify arguments | |
2579 | ts = new target_symbol (e); | |
2580 | ||
2581 | if (ts->addressof) | |
dc09353a | 2582 | throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok); |
5f36109e JS |
2583 | |
2584 | if (ts->components.empty() || | |
2585 | ts->components.back().type != target_symbol::comp_pretty_print) | |
dc09353a | 2586 | throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok); |
7d11d8c9 | 2587 | print_full = ts->components.back().member.length() > 1; |
5f36109e JS |
2588 | ts->components.pop_back(); |
2589 | } | |
2590 | ||
2591 | ||
2592 | functioncall* | |
2593 | dwarf_pretty_print::expand () | |
2594 | { | |
2595 | static unsigned tick = 0; | |
2596 | ||
2597 | // function pretty_print_X([pointer], [arg1, arg2, ...]) { | |
7d11d8c9 JS |
2598 | // try { |
2599 | // return sprintf("{.foo=...}", (ts)->foo, ...) | |
2600 | // } catch { | |
2601 | // return "ERROR" | |
2602 | // } | |
5f36109e JS |
2603 | // } |
2604 | ||
2605 | // Create the function decl and call. | |
2606 | ||
2607 | functiondecl *fdecl = new functiondecl; | |
2608 | fdecl->tok = ts->tok; | |
2609 | fdecl->synthetic = true; | |
2610 | fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++); | |
2611 | fdecl->type = pe_string; | |
2612 | ||
2613 | functioncall* fcall = new functioncall; | |
2614 | fcall->tok = ts->tok; | |
2615 | fcall->function = fdecl->name; | |
140be17a | 2616 | fcall->type = pe_string; |
5f36109e JS |
2617 | |
2618 | // If there's a <pointer>, replace it with a new var and make that | |
2619 | // the first function argument. | |
2620 | if (pointer) | |
2621 | { | |
2622 | vardecl *v = new vardecl; | |
2623 | v->type = pe_long; | |
2624 | v->name = "pointer"; | |
2625 | v->tok = ts->tok; | |
2626 | fdecl->formal_args.push_back (v); | |
2627 | fcall->args.push_back (pointer); | |
2628 | ||
2629 | symbol* sym = new symbol; | |
2630 | sym->tok = ts->tok; | |
2631 | sym->name = v->name; | |
5f36109e JS |
2632 | pointer = sym; |
2633 | } | |
2634 | ||
2635 | // For each expression argument, replace it with a function argument. | |
2636 | for (unsigned i = 0; i < ts->components.size(); ++i) | |
2637 | if (ts->components[i].type == target_symbol::comp_expression_array_index) | |
2638 | { | |
2639 | vardecl *v = new vardecl; | |
2640 | v->type = pe_long; | |
2641 | v->name = "index" + lex_cast(i); | |
2642 | v->tok = ts->tok; | |
2643 | fdecl->formal_args.push_back (v); | |
2644 | fcall->args.push_back (ts->components[i].expr_index); | |
2645 | ||
2646 | symbol* sym = new symbol; | |
2647 | sym->tok = ts->tok; | |
2648 | sym->name = v->name; | |
5f36109e JS |
2649 | ts->components[i].expr_index = sym; |
2650 | } | |
2651 | ||
2652 | // Create the return sprintf. | |
2653 | token* pf_tok = new token(*ts->tok); | |
2654 | pf_tok->content = "sprintf"; | |
2655 | print_format* pf = print_format::create(pf_tok); | |
2656 | return_statement* rs = new return_statement; | |
2657 | rs->tok = ts->tok; | |
2658 | rs->value = pf; | |
5f36109e JS |
2659 | |
2660 | // Recurse into the actual values. | |
7d11d8c9 | 2661 | recurse (&base_type, ts, pf, true); |
5f36109e JS |
2662 | pf->components = print_format::string_to_components(pf->raw_components); |
2663 | ||
7d11d8c9 JS |
2664 | // Create the try-catch net |
2665 | try_block* tb = new try_block; | |
2666 | tb->tok = ts->tok; | |
2667 | tb->try_block = rs; | |
2668 | tb->catch_error_var = 0; | |
2669 | return_statement* rs2 = new return_statement; | |
2670 | rs2->tok = ts->tok; | |
2671 | rs2->value = new literal_string ("ERROR"); | |
2672 | rs2->value->tok = ts->tok; | |
2673 | tb->catch_block = rs2; | |
2674 | fdecl->body = tb; | |
2675 | ||
f8809d54 | 2676 | fdecl->join (dw.sess); |
5f36109e JS |
2677 | return fcall; |
2678 | } | |
2679 | ||
2680 | ||
2681 | void | |
2682 | dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e, | |
7d11d8c9 | 2683 | print_format* pf, bool top) |
5f36109e JS |
2684 | { |
2685 | Dwarf_Die type; | |
2686 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2687 | ||
2688 | switch (dwarf_tag(&type)) | |
2689 | { | |
2690 | default: | |
2691 | // XXX need a warning? | |
2692 | // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type)) | |
2693 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2694 | pf->raw_components.append("?"); | |
2695 | break; | |
2696 | ||
2697 | case DW_TAG_enumeration_type: | |
2698 | case DW_TAG_base_type: | |
7d11d8c9 | 2699 | recurse_base (&type, e, pf); |
5f36109e JS |
2700 | break; |
2701 | ||
2702 | case DW_TAG_array_type: | |
7d11d8c9 | 2703 | recurse_array (&type, e, pf, top); |
5f36109e JS |
2704 | break; |
2705 | ||
2706 | case DW_TAG_pointer_type: | |
2707 | case DW_TAG_reference_type: | |
2708 | case DW_TAG_rvalue_reference_type: | |
7d11d8c9 | 2709 | recurse_pointer (&type, e, pf, top); |
5f36109e JS |
2710 | break; |
2711 | ||
2712 | case DW_TAG_subroutine_type: | |
c55ea10d | 2713 | push_deref (pf, "<function>:%p", e); |
5f36109e JS |
2714 | break; |
2715 | ||
2716 | case DW_TAG_union_type: | |
5f36109e JS |
2717 | case DW_TAG_structure_type: |
2718 | case DW_TAG_class_type: | |
7d11d8c9 | 2719 | recurse_struct (&type, e, pf, top); |
5f36109e JS |
2720 | break; |
2721 | } | |
2722 | } | |
2723 | ||
2724 | ||
600551ca JS |
2725 | // Bit fields are handled as a special-case combination of recurse() and |
2726 | // recurse_base(), only called from recurse_struct_members(). The main | |
2727 | // difference is that the value is always printed numerically, even if the | |
2728 | // underlying type is a char. | |
2729 | void | |
2730 | dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e, | |
2731 | print_format* pf) | |
2732 | { | |
2733 | Dwarf_Die type; | |
2734 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2735 | ||
2736 | int tag = dwarf_tag(&type); | |
2737 | if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type) | |
2738 | { | |
2739 | // XXX need a warning? | |
2740 | // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag) | |
2741 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2742 | pf->raw_components.append("?"); | |
2743 | return; | |
2744 | } | |
2745 | ||
2746 | Dwarf_Attribute attr; | |
2747 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2748 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr), | |
2749 | &encoding); | |
2750 | switch (encoding) | |
2751 | { | |
2752 | case DW_ATE_float: | |
2753 | case DW_ATE_complex_float: | |
2754 | // XXX need a warning? | |
2755 | // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding) | |
2756 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2757 | pf->raw_components.append("?"); | |
2758 | break; | |
2759 | ||
2760 | case DW_ATE_unsigned: | |
2761 | case DW_ATE_unsigned_char: | |
2762 | push_deref (pf, "%u", e); | |
2763 | break; | |
2764 | ||
2765 | case DW_ATE_signed: | |
2766 | case DW_ATE_signed_char: | |
2767 | default: | |
2768 | push_deref (pf, "%i", e); | |
2769 | break; | |
2770 | } | |
2771 | } | |
2772 | ||
2773 | ||
5f36109e JS |
2774 | void |
2775 | dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2776 | print_format* pf) |
5f36109e JS |
2777 | { |
2778 | Dwarf_Attribute attr; | |
2779 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2780 | dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr), | |
2781 | &encoding); | |
5f36109e JS |
2782 | switch (encoding) |
2783 | { | |
2784 | case DW_ATE_float: | |
2785 | case DW_ATE_complex_float: | |
2786 | // XXX need a warning? | |
2787 | // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding) | |
2788 | // + ") for " + dwarf_type_name(type), e->tok); | |
2789 | pf->raw_components.append("?"); | |
5f36109e JS |
2790 | break; |
2791 | ||
6561d8d1 | 2792 | case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char |
5f36109e JS |
2793 | case DW_ATE_signed_char: |
2794 | case DW_ATE_unsigned_char: | |
941101c1 JS |
2795 | // Use escapes to make sure that non-printable characters |
2796 | // don't interrupt our stream (especially '\0' values). | |
2797 | push_deref (pf, "'%#c'", e); | |
5f36109e JS |
2798 | break; |
2799 | ||
2800 | case DW_ATE_unsigned: | |
c55ea10d | 2801 | push_deref (pf, "%u", e); |
5f36109e JS |
2802 | break; |
2803 | ||
600551ca | 2804 | case DW_ATE_signed: |
5f36109e | 2805 | default: |
c55ea10d | 2806 | push_deref (pf, "%i", e); |
5f36109e JS |
2807 | break; |
2808 | } | |
5f36109e JS |
2809 | } |
2810 | ||
2811 | ||
2812 | void | |
2813 | dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2814 | print_format* pf, bool top) |
5f36109e | 2815 | { |
7d11d8c9 JS |
2816 | if (!top && !print_full) |
2817 | { | |
2818 | pf->raw_components.append("[...]"); | |
2819 | return; | |
2820 | } | |
2821 | ||
5f36109e JS |
2822 | Dwarf_Die childtype; |
2823 | dwarf_attr_die (type, DW_AT_type, &childtype); | |
bbee5bb8 JS |
2824 | |
2825 | if (print_chars (&childtype, e, pf)) | |
2826 | return; | |
2827 | ||
5f36109e JS |
2828 | pf->raw_components.append("["); |
2829 | ||
2830 | // We print the array up to the first 5 elements. | |
2831 | // XXX how can we determine the array size? | |
2832 | // ... for now, just print the first element | |
64cddf39 | 2833 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
5f36109e | 2834 | unsigned i, size = 1; |
64cddf39 | 2835 | for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i) |
5f36109e JS |
2836 | { |
2837 | if (i > 0) | |
2838 | pf->raw_components.append(", "); | |
2839 | target_symbol* e2 = new target_symbol(*e); | |
2840 | e2->components.push_back (target_symbol::component(e->tok, i)); | |
7d11d8c9 | 2841 | recurse (&childtype, e2, pf); |
5f36109e JS |
2842 | } |
2843 | if (i < size || 1/*XXX until real size is known */) | |
2844 | pf->raw_components.append(", ..."); | |
2845 | pf->raw_components.append("]"); | |
2846 | } | |
2847 | ||
2848 | ||
2849 | void | |
2850 | dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2851 | print_format* pf, bool top) |
5f36109e | 2852 | { |
7d11d8c9 | 2853 | // We chase to top-level pointers, but leave the rest alone |
d19a9a82 | 2854 | bool void_p = true; |
7d11d8c9 | 2855 | Dwarf_Die pointee; |
bbee5bb8 | 2856 | if (dwarf_attr_die (type, DW_AT_type, &pointee)) |
d19a9a82 JS |
2857 | { |
2858 | try | |
2859 | { | |
2860 | dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e); | |
2861 | void_p = false; | |
2862 | } | |
2863 | catch (const semantic_error&) {} | |
2864 | } | |
2865 | ||
2866 | if (!void_p) | |
5f36109e | 2867 | { |
bbee5bb8 JS |
2868 | if (print_chars (&pointee, e, pf)) |
2869 | return; | |
2870 | ||
2871 | if (top) | |
2872 | { | |
2873 | recurse (&pointee, e, pf, top); | |
2874 | return; | |
2875 | } | |
5f36109e | 2876 | } |
bbee5bb8 | 2877 | |
c55ea10d | 2878 | push_deref (pf, "%p", e); |
5f36109e JS |
2879 | } |
2880 | ||
2881 | ||
2882 | void | |
2883 | dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2884 | print_format* pf, bool top) |
5f36109e | 2885 | { |
bdec0e18 JS |
2886 | if (dwarf_hasattr(type, DW_AT_declaration)) |
2887 | { | |
a44a7cb5 | 2888 | Dwarf_Die *resolved = dw.declaration_resolve(type); |
bdec0e18 JS |
2889 | if (!resolved) |
2890 | { | |
2891 | // could be an error, but for now just stub it | |
2892 | // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok); | |
2893 | pf->raw_components.append("{...}"); | |
2894 | return; | |
2895 | } | |
2896 | type = resolved; | |
2897 | } | |
2898 | ||
5f36109e JS |
2899 | int count = 0; |
2900 | pf->raw_components.append("{"); | |
7d11d8c9 JS |
2901 | if (top || print_full) |
2902 | recurse_struct_members (type, e, pf, count); | |
2903 | else | |
2904 | pf->raw_components.append("..."); | |
5f36109e JS |
2905 | pf->raw_components.append("}"); |
2906 | } | |
2907 | ||
2908 | ||
2909 | void | |
2910 | dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2911 | print_format* pf, int& count) |
5f36109e | 2912 | { |
a80f28d8 JS |
2913 | /* With inheritance, a subclass may mask member names of parent classes, so |
2914 | * our search among the inheritance tree must be breadth-first rather than | |
2915 | * depth-first (recursive). The type die is still our starting point. When | |
2916 | * we encounter a masked name, just skip it. */ | |
2917 | set<string> dupes; | |
2918 | deque<Dwarf_Die> inheritees(1, *type); | |
2919 | for (; !inheritees.empty(); inheritees.pop_front()) | |
2920 | { | |
dee830d9 | 2921 | Dwarf_Die child, childtype, import; |
a80f28d8 JS |
2922 | if (dwarf_child (&inheritees.front(), &child) == 0) |
2923 | do | |
2924 | { | |
2925 | target_symbol* e2 = e; | |
5f36109e | 2926 | |
a80f28d8 JS |
2927 | // skip static members |
2928 | if (dwarf_hasattr(&child, DW_AT_declaration)) | |
2929 | continue; | |
5f36109e | 2930 | |
a80f28d8 | 2931 | int tag = dwarf_tag (&child); |
5f36109e | 2932 | |
dee830d9 MW |
2933 | /* Pretend imported units contain members by recursing into |
2934 | struct_member printing with the same count. */ | |
2935 | if (tag == DW_TAG_imported_unit | |
2936 | && dwarf_attr_die (&child, DW_AT_import, &import)) | |
2937 | recurse_struct_members (&import, e2, pf, count); | |
2938 | ||
a80f28d8 JS |
2939 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) |
2940 | continue; | |
5f36109e | 2941 | |
a80f28d8 | 2942 | dwarf_attr_die (&child, DW_AT_type, &childtype); |
5f36109e | 2943 | |
a80f28d8 JS |
2944 | if (tag == DW_TAG_inheritance) |
2945 | { | |
2946 | inheritees.push_back(childtype); | |
2947 | continue; | |
2948 | } | |
5f36109e | 2949 | |
a80f28d8 JS |
2950 | int childtag = dwarf_tag (&childtype); |
2951 | const char *member = dwarf_diename (&child); | |
3a147004 | 2952 | |
a80f28d8 JS |
2953 | // "_vptr.foo" members are C++ virtual function tables, |
2954 | // which (generally?) aren't interesting for users. | |
2955 | if (member && startswith(member, "_vptr.")) | |
2956 | continue; | |
3a147004 | 2957 | |
a80f28d8 JS |
2958 | // skip inheritance-masked duplicates |
2959 | if (member && !dupes.insert(member).second) | |
2960 | continue; | |
64cddf39 | 2961 | |
a80f28d8 JS |
2962 | if (++count > 1) |
2963 | pf->raw_components.append(", "); | |
64cddf39 | 2964 | |
a80f28d8 JS |
2965 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
2966 | if (pf->args.size() >= 32) | |
2967 | { | |
2968 | pf->raw_components.append("..."); | |
2969 | break; | |
2970 | } | |
2971 | ||
2972 | if (member) | |
2973 | { | |
2974 | pf->raw_components.append("."); | |
2975 | pf->raw_components.append(member); | |
5f36109e | 2976 | |
a80f28d8 JS |
2977 | e2 = new target_symbol(*e); |
2978 | e2->components.push_back (target_symbol::component(e->tok, member)); | |
2979 | } | |
2980 | else if (childtag == DW_TAG_union_type) | |
2981 | pf->raw_components.append("<union>"); | |
2982 | else if (childtag == DW_TAG_structure_type) | |
2983 | pf->raw_components.append("<class>"); | |
2984 | else if (childtag == DW_TAG_class_type) | |
2985 | pf->raw_components.append("<struct>"); | |
2986 | pf->raw_components.append("="); | |
600551ca JS |
2987 | |
2988 | if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset)) | |
2989 | recurse_bitfield (&childtype, e2, pf); | |
2990 | else | |
2991 | recurse (&childtype, e2, pf); | |
5f36109e | 2992 | } |
a80f28d8 JS |
2993 | while (dwarf_siblingof (&child, &child) == 0); |
2994 | } | |
5f36109e JS |
2995 | } |
2996 | ||
2997 | ||
bbee5bb8 JS |
2998 | bool |
2999 | dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e, | |
3000 | print_format* pf) | |
3001 | { | |
3002 | Dwarf_Die type; | |
3003 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
6561d8d1 JS |
3004 | |
3005 | Dwarf_Attribute attr; | |
3006 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
3007 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr), | |
3008 | &encoding); | |
3009 | switch (encoding) | |
bbee5bb8 | 3010 | { |
6561d8d1 JS |
3011 | case DW_ATE_UTF: |
3012 | case DW_ATE_signed_char: | |
3013 | case DW_ATE_unsigned_char: | |
3014 | break; | |
3015 | default: | |
3016 | return false; | |
3017 | } | |
3018 | ||
3019 | string function = userspace_p ? "user_string2" : "kernel_string2"; | |
3020 | Dwarf_Word size = (Dwarf_Word) -1; | |
3021 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size); | |
3022 | switch (size) | |
3023 | { | |
3024 | case 1: | |
3025 | break; | |
3026 | case 2: | |
3027 | function += "_utf16"; | |
3028 | break; | |
3029 | case 4: | |
3030 | function += "_utf32"; | |
3031 | break; | |
3032 | default: | |
3033 | return false; | |
3034 | } | |
3035 | ||
3036 | if (push_deref (pf, "\"%s\"", e)) | |
3037 | { | |
3038 | // steal the last arg for a string access | |
3039 | assert (!pf->args.empty()); | |
3040 | functioncall* fcall = new functioncall; | |
3041 | fcall->tok = e->tok; | |
3042 | fcall->function = function; | |
3043 | fcall->args.push_back (pf->args.back()); | |
3044 | expression *err_msg = new literal_string ("<unknown>"); | |
3045 | err_msg->tok = e->tok; | |
3046 | fcall->args.push_back (err_msg); | |
3047 | pf->args.back() = fcall; | |
bbee5bb8 | 3048 | } |
6561d8d1 | 3049 | return true; |
bbee5bb8 JS |
3050 | } |
3051 | ||
a5ce5211 MW |
3052 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
3053 | static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n") | |
f1e8e7e0 MW |
3054 | + "#define fetch_register k_fetch_register\n" |
3055 | + "#define store_register k_store_register\n" | |
3056 | + "#define deref kderef\n" | |
3057 | + "#define store_deref store_kderef\n"; | |
a5ce5211 MW |
3058 | |
3059 | static const string EMBEDDED_FETCH_DEREF_USER = string("\n") | |
f1e8e7e0 MW |
3060 | + "#define fetch_register u_fetch_register\n" |
3061 | + "#define store_register u_store_register\n" | |
3062 | + "#define deref uderef\n" | |
3063 | + "#define store_deref store_uderef\n"; | |
a5ce5211 MW |
3064 | |
3065 | #define EMBEDDED_FETCH_DEREF(U) \ | |
f1e8e7e0 | 3066 | (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL) |
a5ce5211 MW |
3067 | |
3068 | static const string EMBEDDED_FETCH_DEREF_DONE = string("\n") | |
f1e8e7e0 MW |
3069 | + "#undef fetch_register\n" |
3070 | + "#undef store_register\n" | |
3071 | + "#undef deref\n" | |
3072 | + "#undef store_deref\n"; | |
bbee5bb8 | 3073 | |
1c0be8c7 JS |
3074 | static functioncall* |
3075 | synthetic_embedded_deref_call(systemtap_session& session, | |
3076 | const string& function_name, | |
3077 | const string& function_code, | |
3078 | exp_type function_type, | |
3079 | bool userspace_p, | |
3080 | bool lvalue_p, | |
3081 | target_symbol* e, | |
3082 | expression* pointer=NULL) | |
3083 | { | |
3084 | // Synthesize a functiondecl for the given embedded code string. | |
5f36109e JS |
3085 | functiondecl *fdecl = new functiondecl; |
3086 | fdecl->synthetic = true; | |
3087 | fdecl->tok = e->tok; | |
1c0be8c7 JS |
3088 | fdecl->name = function_name; |
3089 | fdecl->type = function_type; | |
3090 | ||
5f36109e JS |
3091 | embeddedcode *ec = new embeddedcode; |
3092 | ec->tok = e->tok; | |
1c0be8c7 JS |
3093 | ec->code += "/* unprivileged */"; |
3094 | if (! lvalue_p) | |
3095 | ec->code += "/* pure */"; | |
3096 | ec->code += EMBEDDED_FETCH_DEREF(userspace_p); | |
3097 | ec->code += function_code; | |
3098 | ec->code += EMBEDDED_FETCH_DEREF_DONE; | |
5f36109e JS |
3099 | fdecl->body = ec; |
3100 | ||
3101 | // Synthesize a functioncall. | |
3102 | functioncall* fcall = new functioncall; | |
3103 | fcall->tok = e->tok; | |
3104 | fcall->function = fdecl->name; | |
1c0be8c7 | 3105 | fcall->type = fdecl->type; |
5f36109e | 3106 | |
1c0be8c7 JS |
3107 | // If this code snippet uses a precomputed pointer, |
3108 | // pass that as the first argument. | |
5f36109e JS |
3109 | if (pointer) |
3110 | { | |
5f36109e JS |
3111 | vardecl *v = new vardecl; |
3112 | v->type = pe_long; | |
3113 | v->name = "pointer"; | |
3114 | v->tok = e->tok; | |
3115 | fdecl->formal_args.push_back(v); | |
3116 | fcall->args.push_back(pointer); | |
3117 | } | |
5f36109e | 3118 | |
1c0be8c7 | 3119 | // Any non-literal indexes need to be passed as arguments too. |
5f36109e JS |
3120 | for (unsigned i = 0; i < e->components.size(); ++i) |
3121 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3122 | { | |
3123 | vardecl *v = new vardecl; | |
3124 | v->type = pe_long; | |
3125 | v->name = "index" + lex_cast(i); | |
3126 | v->tok = e->tok; | |
3127 | fdecl->formal_args.push_back(v); | |
3128 | fcall->args.push_back(e->components[i].expr_index); | |
3129 | } | |
3130 | ||
1c0be8c7 JS |
3131 | // If this code snippet is assigning to an lvalue, |
3132 | // add a final argument for the rvalue. | |
3133 | if (lvalue_p) | |
3134 | { | |
3135 | // Modify the fdecl so it carries a single pe_long formal | |
3136 | // argument called "value". | |
5f36109e | 3137 | |
1c0be8c7 JS |
3138 | // FIXME: For the time being we only support setting target |
3139 | // variables which have base types; these are 'pe_long' in | |
3140 | // stap's type vocabulary. Strings and pointers might be | |
3141 | // reasonable, some day, but not today. | |
5f36109e | 3142 | |
1c0be8c7 JS |
3143 | vardecl *v = new vardecl; |
3144 | v->type = pe_long; | |
3145 | v->name = "value"; | |
3146 | v->tok = e->tok; | |
3147 | fdecl->formal_args.push_back(v); | |
3148 | // NB: We don't know the value for fcall argument yet. | |
3149 | // (see target_symbol_setter_functioncalls) | |
3150 | } | |
3151 | ||
3152 | // Add the synthesized decl to the session, and return the call. | |
3153 | fdecl->join (session); | |
5f36109e JS |
3154 | return fcall; |
3155 | } | |
3156 | ||
1c0be8c7 JS |
3157 | expression* |
3158 | dwarf_pretty_print::deref (target_symbol* e) | |
3159 | { | |
3160 | static unsigned tick = 0; | |
3161 | ||
3162 | if (!deref_p) | |
3163 | { | |
3164 | assert (pointer && e->components.empty()); | |
3165 | return pointer; | |
3166 | } | |
3167 | ||
3168 | bool lvalue_p = false; | |
3169 | string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++); | |
3170 | ||
3171 | string code; | |
3172 | exp_type type = pe_long; | |
3173 | if (pointer) | |
3174 | code = dw.literal_stmt_for_pointer (&pointer_type, e, false, type); | |
3175 | else if (!local.empty()) | |
3176 | code = dw.literal_stmt_for_local (scopes, pc, local, e, false, type); | |
3177 | else | |
3178 | code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, type); | |
3179 | ||
3180 | return synthetic_embedded_deref_call(dw.sess, name, code, type, | |
3181 | userspace_p, lvalue_p, e, pointer); | |
3182 | } | |
3183 | ||
5f36109e | 3184 | |
c55ea10d JS |
3185 | bool |
3186 | dwarf_pretty_print::push_deref (print_format* pf, const string& fmt, | |
3187 | target_symbol* e) | |
3188 | { | |
3189 | expression* e2 = NULL; | |
3190 | try | |
3191 | { | |
3192 | e2 = deref (e); | |
3193 | } | |
3194 | catch (const semantic_error&) | |
3195 | { | |
3196 | pf->raw_components.append ("?"); | |
3197 | return false; | |
3198 | } | |
3199 | pf->raw_components.append (fmt); | |
3200 | pf->args.push_back (e2); | |
3201 | return true; | |
3202 | } | |
3203 | ||
3204 | ||
e57b735a | 3205 | void |
a7999c82 | 3206 | dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e) |
e57b735a | 3207 | { |
a7999c82 JS |
3208 | // Get the full name of the target symbol. |
3209 | stringstream ts_name_stream; | |
3210 | e->print(ts_name_stream); | |
3211 | string ts_name = ts_name_stream.str(); | |
3212 | ||
3213 | // Check and make sure we haven't already seen this target | |
3214 | // variable in this return probe. If we have, just return our | |
3215 | // last replacement. | |
af234c40 | 3216 | map<string, expression *>::iterator i = return_ts_map.find(ts_name); |
a7999c82 | 3217 | if (i != return_ts_map.end()) |
85ecf79a | 3218 | { |
a7999c82 JS |
3219 | provide (i->second); |
3220 | return; | |
3221 | } | |
85ecf79a | 3222 | |
70208613 JS |
3223 | // Attempt the expansion directly first, so if there's a problem with the |
3224 | // variable we won't have a bogus entry probe lying around. Like in | |
3225 | // saveargs(), we pretend for a moment that we're not in a .return. | |
3226 | bool saved_has_return = q.has_return; | |
3227 | q.has_return = false; | |
3228 | expression *repl = e; | |
3229 | replace (repl); | |
3230 | q.has_return = saved_has_return; | |
3231 | target_symbol* n = dynamic_cast<target_symbol*>(repl); | |
3232 | if (n && n->saved_conversion_error) | |
3233 | { | |
3234 | provide (repl); | |
3235 | return; | |
3236 | } | |
3237 | ||
af234c40 JS |
3238 | expression *exp; |
3239 | if (!q.has_process && | |
3240 | strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0) | |
140be17a | 3241 | exp = gen_kretprobe_saved_return(repl); |
af234c40 | 3242 | else |
cc9001af | 3243 | exp = gen_mapped_saved_return(repl, e->sym_name()); |
af234c40 JS |
3244 | |
3245 | // Provide the variable to our parent so it can be used as a | |
3246 | // substitute for the target symbol. | |
3247 | provide (exp); | |
3248 | ||
3249 | // Remember this replacement since we might be able to reuse | |
3250 | // it later if the same return probe references this target | |
3251 | // symbol again. | |
3252 | return_ts_map[ts_name] = exp; | |
3253 | } | |
3254 | ||
4a2970a3 | 3255 | static expression* |
23dc94f6 DS |
3256 | gen_mapped_saved_return(systemtap_session &sess, expression* e, |
3257 | const string& name, | |
3258 | block *& add_block, bool& add_block_tid, | |
3259 | block *& add_call_probe, bool& add_call_probe_tid) | |
af234c40 | 3260 | { |
23dc94f6 DS |
3261 | static unsigned tick = 0; |
3262 | ||
a7999c82 JS |
3263 | // We've got to do several things here to handle target |
3264 | // variables in return probes. | |
85ecf79a | 3265 | |
a7999c82 JS |
3266 | // (1) Synthesize two global arrays. One is the cache of the |
3267 | // target variable and the other contains a thread specific | |
3268 | // nesting level counter. The arrays will look like | |
3269 | // this: | |
3270 | // | |
23dc94f6 DS |
3271 | // _entry_tvar_{name}_{num} |
3272 | // _entry_tvar_{name}_{num}_ctr | |
a7999c82 | 3273 | |
23dc94f6 | 3274 | string aname = (string("_entry_tvar_") |
cc9001af | 3275 | + name |
aca66a36 | 3276 | + "_" + lex_cast(tick++)); |
a7999c82 JS |
3277 | vardecl* vd = new vardecl; |
3278 | vd->name = aname; | |
3279 | vd->tok = e->tok; | |
23dc94f6 | 3280 | sess.globals.push_back (vd); |
a7999c82 JS |
3281 | |
3282 | string ctrname = aname + "_ctr"; | |
3283 | vd = new vardecl; | |
3284 | vd->name = ctrname; | |
3285 | vd->tok = e->tok; | |
23dc94f6 | 3286 | sess.globals.push_back (vd); |
a7999c82 JS |
3287 | |
3288 | // (2) Create a new code block we're going to insert at the | |
3289 | // beginning of this probe to get the cached value into a | |
3290 | // temporary variable. We'll replace the target variable | |
3291 | // reference with the temporary variable reference. The code | |
3292 | // will look like this: | |
3293 | // | |
23dc94f6 DS |
3294 | // _entry_tvar_tid = tid() |
3295 | // _entry_tvar_{name}_{num}_tmp | |
3296 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3297 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
3298 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3299 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
3300 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) | |
3301 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3302 | |
3303 | // (2a) Synthesize the tid temporary expression, which will look | |
3304 | // like this: | |
3305 | // | |
23dc94f6 | 3306 | // _entry_tvar_tid = tid() |
a7999c82 | 3307 | symbol* tidsym = new symbol; |
23dc94f6 | 3308 | tidsym->name = string("_entry_tvar_tid"); |
a7999c82 | 3309 | tidsym->tok = e->tok; |
85ecf79a | 3310 | |
a7999c82 JS |
3311 | if (add_block == NULL) |
3312 | { | |
3313 | add_block = new block; | |
3314 | add_block->tok = e->tok; | |
8cc799a5 | 3315 | } |
8c819921 | 3316 | |
8cc799a5 JS |
3317 | if (!add_block_tid) |
3318 | { | |
a7999c82 JS |
3319 | // Synthesize a functioncall to grab the thread id. |
3320 | functioncall* fc = new functioncall; | |
3321 | fc->tok = e->tok; | |
3322 | fc->function = string("tid"); | |
8c819921 | 3323 | |
23dc94f6 | 3324 | // Assign the tid to '_entry_tvar_tid'. |
8c819921 DS |
3325 | assignment* a = new assignment; |
3326 | a->tok = e->tok; | |
3327 | a->op = "="; | |
a7999c82 JS |
3328 | a->left = tidsym; |
3329 | a->right = fc; | |
8c819921 DS |
3330 | |
3331 | expr_statement* es = new expr_statement; | |
3332 | es->tok = e->tok; | |
3333 | es->value = a; | |
8c819921 | 3334 | add_block->statements.push_back (es); |
8cc799a5 | 3335 | add_block_tid = true; |
a7999c82 | 3336 | } |
8c819921 | 3337 | |
a7999c82 JS |
3338 | // (2b) Synthesize an array reference and assign it to a |
3339 | // temporary variable (that we'll use as replacement for the | |
3340 | // target variable reference). It will look like this: | |
3341 | // | |
23dc94f6 DS |
3342 | // _entry_tvar_{name}_{num}_tmp |
3343 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3344 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3345 | |
3346 | arrayindex* ai_tvar_base = new arrayindex; | |
3347 | ai_tvar_base->tok = e->tok; | |
3348 | ||
3349 | symbol* sym = new symbol; | |
3350 | sym->name = aname; | |
3351 | sym->tok = e->tok; | |
3352 | ai_tvar_base->base = sym; | |
3353 | ||
3354 | ai_tvar_base->indexes.push_back(tidsym); | |
3355 | ||
3356 | // We need to create a copy of the array index in its current | |
3357 | // state so we can have 2 variants of it (the original and one | |
3358 | // that post-decrements the second index). | |
3359 | arrayindex* ai_tvar = new arrayindex; | |
3360 | arrayindex* ai_tvar_postdec = new arrayindex; | |
3361 | *ai_tvar = *ai_tvar_base; | |
3362 | *ai_tvar_postdec = *ai_tvar_base; | |
3363 | ||
3364 | // Synthesize the | |
23dc94f6 | 3365 | // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the |
a7999c82 JS |
3366 | // second index into the array. |
3367 | arrayindex* ai_ctr = new arrayindex; | |
3368 | ai_ctr->tok = e->tok; | |
3369 | ||
3370 | sym = new symbol; | |
3371 | sym->name = ctrname; | |
3372 | sym->tok = e->tok; | |
3373 | ai_ctr->base = sym; | |
3374 | ai_ctr->indexes.push_back(tidsym); | |
3375 | ai_tvar->indexes.push_back(ai_ctr); | |
3376 | ||
3377 | symbol* tmpsym = new symbol; | |
3378 | tmpsym->name = aname + "_tmp"; | |
3379 | tmpsym->tok = e->tok; | |
3380 | ||
3381 | assignment* a = new assignment; | |
3382 | a->tok = e->tok; | |
3383 | a->op = "="; | |
3384 | a->left = tmpsym; | |
3385 | a->right = ai_tvar; | |
3386 | ||
3387 | expr_statement* es = new expr_statement; | |
3388 | es->tok = e->tok; | |
3389 | es->value = a; | |
3390 | ||
3391 | add_block->statements.push_back (es); | |
3392 | ||
3393 | // (2c) Add a post-decrement to the second array index and | |
3394 | // delete the array value. It will look like this: | |
3395 | // | |
23dc94f6 DS |
3396 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3397 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
a7999c82 JS |
3398 | |
3399 | post_crement* pc = new post_crement; | |
3400 | pc->tok = e->tok; | |
3401 | pc->op = "--"; | |
3402 | pc->operand = ai_ctr; | |
3403 | ai_tvar_postdec->indexes.push_back(pc); | |
3404 | ||
3405 | delete_statement* ds = new delete_statement; | |
3406 | ds->tok = e->tok; | |
3407 | ds->value = ai_tvar_postdec; | |
3408 | ||
3409 | add_block->statements.push_back (ds); | |
3410 | ||
3411 | // (2d) Delete the counter value if it is 0. It will look like | |
3412 | // this: | |
23dc94f6 DS |
3413 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) |
3414 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3415 | |
3416 | ds = new delete_statement; | |
3417 | ds->tok = e->tok; | |
3418 | ds->value = ai_ctr; | |
3419 | ||
3420 | unary_expression *ue = new unary_expression; | |
3421 | ue->tok = e->tok; | |
3422 | ue->op = "!"; | |
3423 | ue->operand = ai_ctr; | |
3424 | ||
3425 | if_statement *ifs = new if_statement; | |
3426 | ifs->tok = e->tok; | |
3427 | ifs->condition = ue; | |
3428 | ifs->thenblock = ds; | |
3429 | ifs->elseblock = NULL; | |
3430 | ||
3431 | add_block->statements.push_back (ifs); | |
3432 | ||
3433 | // (3) We need an entry probe that saves the value for us in the | |
3434 | // global array we created. Create the entry probe, which will | |
3435 | // look like this: | |
3436 | // | |
2260f4e3 | 3437 | // probe kernel.function("{function}").call { |
23dc94f6 DS |
3438 | // _entry_tvar_tid = tid() |
3439 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3440 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3441 | // = ${param} |
3442 | // } | |
3443 | ||
2260f4e3 | 3444 | if (add_call_probe == NULL) |
a7999c82 | 3445 | { |
2260f4e3 FCE |
3446 | add_call_probe = new block; |
3447 | add_call_probe->tok = e->tok; | |
8cc799a5 | 3448 | } |
4baf0e53 | 3449 | |
8cc799a5 JS |
3450 | if (!add_call_probe_tid) |
3451 | { | |
a7999c82 JS |
3452 | // Synthesize a functioncall to grab the thread id. |
3453 | functioncall* fc = new functioncall; | |
3454 | fc->tok = e->tok; | |
3455 | fc->function = string("tid"); | |
4baf0e53 | 3456 | |
23dc94f6 | 3457 | // Assign the tid to '_entry_tvar_tid'. |
a7999c82 | 3458 | assignment* a = new assignment; |
8fc05e57 DS |
3459 | a->tok = e->tok; |
3460 | a->op = "="; | |
a7999c82 JS |
3461 | a->left = tidsym; |
3462 | a->right = fc; | |
8fc05e57 | 3463 | |
a7999c82 | 3464 | expr_statement* es = new expr_statement; |
8fc05e57 DS |
3465 | es->tok = e->tok; |
3466 | es->value = a; | |
2260f4e3 | 3467 | add_call_probe = new block(add_call_probe, es); |
8cc799a5 | 3468 | add_call_probe_tid = true; |
85ecf79a | 3469 | } |
cf2a1f85 | 3470 | |
a7999c82 | 3471 | // Save the value, like this: |
23dc94f6 DS |
3472 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3473 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3474 | // = ${param} |
3475 | arrayindex* ai_tvar_preinc = new arrayindex; | |
3476 | *ai_tvar_preinc = *ai_tvar_base; | |
3477 | ||
3478 | pre_crement* preinc = new pre_crement; | |
3479 | preinc->tok = e->tok; | |
3480 | preinc->op = "++"; | |
3481 | preinc->operand = ai_ctr; | |
3482 | ai_tvar_preinc->indexes.push_back(preinc); | |
3483 | ||
3484 | a = new assignment; | |
3485 | a->tok = e->tok; | |
3486 | a->op = "="; | |
3487 | a->left = ai_tvar_preinc; | |
3488 | a->right = e; | |
3489 | ||
3490 | es = new expr_statement; | |
3491 | es->tok = e->tok; | |
3492 | es->value = a; | |
3493 | ||
2260f4e3 | 3494 | add_call_probe = new block(add_call_probe, es); |
a7999c82 | 3495 | |
23dc94f6 | 3496 | // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to |
a7999c82 JS |
3497 | // our parent so it can be used as a substitute for the target |
3498 | // symbol. | |
3f803f9e | 3499 | delete ai_tvar_base; |
af234c40 JS |
3500 | return tmpsym; |
3501 | } | |
a7999c82 | 3502 | |
af234c40 | 3503 | |
23dc94f6 DS |
3504 | expression* |
3505 | dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e, | |
3506 | const string& name) | |
3507 | { | |
3508 | return ::gen_mapped_saved_return(q.sess, e, name, add_block, | |
3509 | add_block_tid, add_call_probe, | |
3510 | add_call_probe_tid); | |
3511 | } | |
3512 | ||
3513 | ||
af234c40 | 3514 | expression* |
140be17a | 3515 | dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e) |
af234c40 JS |
3516 | { |
3517 | // The code for this is simple. | |
3518 | // | |
3519 | // .call: | |
3520 | // _set_kretprobe_long(index, $value) | |
3521 | // | |
3522 | // .return: | |
3523 | // _get_kretprobe_long(index) | |
3524 | // | |
3525 | // (or s/long/string/ for things like $$parms) | |
3526 | ||
3527 | unsigned index; | |
3528 | string setfn, getfn; | |
3529 | ||
140be17a JS |
3530 | // We need the caller to predetermine the type of the expression! |
3531 | switch (e->type) | |
af234c40 | 3532 | { |
140be17a | 3533 | case pe_string: |
af234c40 JS |
3534 | index = saved_strings++; |
3535 | setfn = "_set_kretprobe_string"; | |
3536 | getfn = "_get_kretprobe_string"; | |
140be17a JS |
3537 | break; |
3538 | case pe_long: | |
af234c40 JS |
3539 | index = saved_longs++; |
3540 | setfn = "_set_kretprobe_long"; | |
3541 | getfn = "_get_kretprobe_long"; | |
140be17a JS |
3542 | break; |
3543 | default: | |
dc09353a | 3544 | throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok); |
af234c40 JS |
3545 | } |
3546 | ||
3547 | // Create the entry code | |
3548 | // _set_kretprobe_{long|string}(index, $value) | |
3549 | ||
3550 | if (add_call_probe == NULL) | |
3551 | { | |
3552 | add_call_probe = new block; | |
3553 | add_call_probe->tok = e->tok; | |
3554 | } | |
3555 | ||
3556 | functioncall* set_fc = new functioncall; | |
3557 | set_fc->tok = e->tok; | |
3558 | set_fc->function = setfn; | |
3559 | set_fc->args.push_back(new literal_number(index)); | |
3560 | set_fc->args.back()->tok = e->tok; | |
3561 | set_fc->args.push_back(e); | |
3562 | ||
3563 | expr_statement* set_es = new expr_statement; | |
3564 | set_es->tok = e->tok; | |
3565 | set_es->value = set_fc; | |
3566 | ||
3567 | add_call_probe->statements.push_back(set_es); | |
3568 | ||
3569 | // Create the return code | |
3570 | // _get_kretprobe_{long|string}(index) | |
3571 | ||
3572 | functioncall* get_fc = new functioncall; | |
3573 | get_fc->tok = e->tok; | |
3574 | get_fc->function = getfn; | |
3575 | get_fc->args.push_back(new literal_number(index)); | |
3576 | get_fc->args.back()->tok = e->tok; | |
3577 | ||
3578 | return get_fc; | |
a7999c82 | 3579 | } |
a43ba433 | 3580 | |
2cb3fe26 | 3581 | |
a7999c82 JS |
3582 | void |
3583 | dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
3584 | { | |
9aa8ffce | 3585 | if (null_die(scope_die)) |
a7999c82 | 3586 | return; |
2cb3fe26 | 3587 | |
5f36109e JS |
3588 | target_symbol *tsym = new target_symbol(*e); |
3589 | ||
fde50242 JS |
3590 | bool pretty = (!e->components.empty() && |
3591 | e->components[0].type == target_symbol::comp_pretty_print); | |
3592 | string format = pretty ? "=%s" : "=%#x"; | |
a43ba433 | 3593 | |
a7999c82 JS |
3594 | // Convert $$parms to sprintf of a list of parms and active local vars |
3595 | // which we recursively evaluate | |
a43ba433 | 3596 | |
a7999c82 JS |
3597 | // NB: we synthesize a new token here rather than reusing |
3598 | // e->tok, because print_format::print likes to use | |
3599 | // its tok->content. | |
5f36109e | 3600 | token* pf_tok = new token(*e->tok); |
a7999c82 | 3601 | pf_tok->type = tok_identifier; |
b393f6f2 | 3602 | pf_tok->content = "sprintf"; |
2cb3fe26 | 3603 | |
d5e178c1 | 3604 | print_format* pf = print_format::create(pf_tok); |
a7999c82 | 3605 | |
277c21bc | 3606 | if (q.has_return && (e->name == "$$return")) |
a7999c82 | 3607 | { |
277c21bc | 3608 | tsym->name = "$return"; |
a7999c82 JS |
3609 | |
3610 | // Ignore any variable that isn't accessible. | |
3611 | tsym->saved_conversion_error = 0; | |
3612 | expression *texp = tsym; | |
8b095b45 | 3613 | replace (texp); // NB: throws nothing ... |
a7999c82 | 3614 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
a43ba433 | 3615 | { |
2cb3fe26 | 3616 | |
a43ba433 FCE |
3617 | } |
3618 | else | |
3619 | { | |
a7999c82 | 3620 | pf->raw_components += "return"; |
5f36109e | 3621 | pf->raw_components += format; |
a7999c82 JS |
3622 | pf->args.push_back(texp); |
3623 | } | |
3624 | } | |
3625 | else | |
3626 | { | |
3627 | // non-.return probe: support $$parms, $$vars, $$locals | |
345bbb3d | 3628 | bool first = true; |
a7999c82 | 3629 | Dwarf_Die result; |
d48bc7eb JS |
3630 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
3631 | for (unsigned i = 0; i < scopes.size(); ++i) | |
3632 | { | |
3633 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
3634 | break; // we don't want file-level variables | |
3635 | if (dwarf_child (&scopes[i], &result) == 0) | |
3636 | do | |
00cf3709 | 3637 | { |
d48bc7eb JS |
3638 | switch (dwarf_tag (&result)) |
3639 | { | |
3640 | case DW_TAG_variable: | |
3641 | if (e->name == "$$parms") | |
3642 | continue; | |
3643 | break; | |
3644 | case DW_TAG_formal_parameter: | |
3645 | if (e->name == "$$locals") | |
3646 | continue; | |
3647 | break; | |
3648 | ||
3649 | default: | |
3650 | continue; | |
3651 | } | |
41c262f3 | 3652 | |
d48bc7eb JS |
3653 | const char *diename = dwarf_diename (&result); |
3654 | if (! diename) continue; | |
f76427a2 | 3655 | |
d48bc7eb JS |
3656 | if (! first) |
3657 | pf->raw_components += " "; | |
3658 | pf->raw_components += diename; | |
fde50242 JS |
3659 | first = false; |
3660 | ||
3661 | // Write a placeholder for ugly aggregates | |
3662 | Dwarf_Die type; | |
3663 | if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type)) | |
3664 | { | |
3665 | q.dw.resolve_unqualified_inner_typedie(&type, &type, e); | |
3666 | switch (dwarf_tag(&type)) | |
3667 | { | |
3668 | case DW_TAG_union_type: | |
3669 | case DW_TAG_structure_type: | |
3670 | case DW_TAG_class_type: | |
3671 | pf->raw_components += "={...}"; | |
3672 | continue; | |
3673 | ||
3674 | case DW_TAG_array_type: | |
3675 | pf->raw_components += "=[...]"; | |
3676 | continue; | |
3677 | } | |
3678 | } | |
345bbb3d | 3679 | |
d48bc7eb JS |
3680 | tsym->name = "$"; |
3681 | tsym->name += diename; | |
41c262f3 | 3682 | |
d48bc7eb JS |
3683 | // Ignore any variable that isn't accessible. |
3684 | tsym->saved_conversion_error = 0; | |
3685 | expression *texp = tsym; | |
3686 | replace (texp); // NB: throws nothing ... | |
3687 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. | |
a43ba433 | 3688 | { |
d48bc7eb JS |
3689 | if (q.sess.verbose>2) |
3690 | { | |
e26c2f83 | 3691 | for (const semantic_error *c = tsym->saved_conversion_error; |
d48bc7eb JS |
3692 | c != 0; |
3693 | c = c->chain) { | |
4c5d9906 | 3694 | clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl; |
d48bc7eb JS |
3695 | } |
3696 | } | |
3697 | ||
3698 | pf->raw_components += "=?"; | |
a43ba433 | 3699 | } |
d48bc7eb JS |
3700 | else |
3701 | { | |
3702 | pf->raw_components += format; | |
3703 | pf->args.push_back(texp); | |
3704 | } | |
a7999c82 | 3705 | } |
d48bc7eb JS |
3706 | while (dwarf_siblingof (&result, &result) == 0); |
3707 | } | |
a7999c82 | 3708 | } |
2cb3fe26 | 3709 | |
a7999c82 | 3710 | pf->components = print_format::string_to_components(pf->raw_components); |
140be17a | 3711 | pf->type = pe_string; |
a7999c82 JS |
3712 | provide (pf); |
3713 | } | |
3714 | ||
2cb3fe26 | 3715 | |
bd1fcbad YZ |
3716 | void |
3717 | dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e) | |
3718 | { | |
3719 | // Fill in our current module context if needed | |
3720 | if (e->module.empty()) | |
3721 | e->module = q.dw.module_name; | |
3722 | ||
3723 | if (e->module == q.dw.module_name && e->cu_name.empty()) | |
3724 | { | |
3725 | // process like any other local | |
3726 | // e->sym_name() will do the right thing | |
3727 | visit_target_symbol(e); | |
3728 | return; | |
3729 | } | |
3730 | ||
3731 | var_expanding_visitor::visit_atvar_op(e); | |
3732 | } | |
3733 | ||
3734 | ||
a7999c82 JS |
3735 | void |
3736 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
3737 | { | |
bd1fcbad | 3738 | assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var")); |
a7999c82 | 3739 | visited = true; |
30263a73 FCE |
3740 | bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e)); |
3741 | // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars | |
a7999c82 | 3742 | |
70208613 | 3743 | try |
a7999c82 | 3744 | { |
c69a87e0 FCE |
3745 | bool lvalue = is_active_lvalue(e); |
3746 | if (lvalue && !q.sess.guru_mode) | |
dc09353a | 3747 | throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok); |
2cb3fe26 | 3748 | |
100a540e | 3749 | // XXX: process $context vars should be writable |
70208613 | 3750 | |
c69a87e0 FCE |
3751 | // See if we need to generate a new probe to save/access function |
3752 | // parameters from a return probe. PR 1382. | |
3753 | if (q.has_return | |
3754 | && !defined_being_checked | |
277c21bc JS |
3755 | && e->name != "$return" // not the special return-value variable handled below |
3756 | && e->name != "$$return") // nor the other special variable handled below | |
c69a87e0 FCE |
3757 | { |
3758 | if (lvalue) | |
dc09353a | 3759 | throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok); |
c69a87e0 FCE |
3760 | visit_target_symbol_saved_return(e); |
3761 | return; | |
3762 | } | |
e57b735a | 3763 | |
277c21bc JS |
3764 | if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals" |
3765 | || (q.has_return && (e->name == "$$return"))) | |
c69a87e0 FCE |
3766 | { |
3767 | if (lvalue) | |
dc09353a | 3768 | throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok); |
70208613 | 3769 | |
c69a87e0 | 3770 | if (e->addressof) |
dc09353a | 3771 | throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok); |
70208613 | 3772 | |
5f36109e JS |
3773 | e->assert_no_components("dwarf", true); |
3774 | ||
c69a87e0 FCE |
3775 | visit_target_symbol_context(e); |
3776 | return; | |
3777 | } | |
70208613 | 3778 | |
5f36109e JS |
3779 | if (!e->components.empty() && |
3780 | e->components.back().type == target_symbol::comp_pretty_print) | |
3781 | { | |
3782 | if (lvalue) | |
dc09353a | 3783 | throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 3784 | |
277c21bc | 3785 | if (q.has_return && (e->name == "$return")) |
5f36109e JS |
3786 | { |
3787 | dwarf_pretty_print dpp (q.dw, scope_die, addr, | |
3788 | q.has_process, *e); | |
3789 | dpp.expand()->visit(this); | |
3790 | } | |
3791 | else | |
3792 | { | |
3793 | dwarf_pretty_print dpp (q.dw, getscopes(e), addr, | |
cc9001af | 3794 | e->sym_name(), |
5f36109e JS |
3795 | q.has_process, *e); |
3796 | dpp.expand()->visit(this); | |
3797 | } | |
3798 | return; | |
3799 | } | |
3800 | ||
1c0be8c7 | 3801 | bool userspace_p = q.has_process; |
c69a87e0 | 3802 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
cc9001af | 3803 | + "_" + e->sym_name() |
c69a87e0 | 3804 | + "_" + lex_cast(tick++)); |
70208613 | 3805 | |
70208613 | 3806 | |
1c0be8c7 JS |
3807 | exp_type type = pe_long; |
3808 | string code; | |
277c21bc | 3809 | if (q.has_return && (e->name == "$return")) |
1c0be8c7 | 3810 | code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, type); |
e19fda4e | 3811 | else |
1c0be8c7 JS |
3812 | code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(), |
3813 | e, lvalue, type); | |
70208613 | 3814 | |
1c0be8c7 JS |
3815 | functioncall* n = synthetic_embedded_deref_call(q.sess, fname, code, type, |
3816 | userspace_p, lvalue, e); | |
70208613 | 3817 | |
c69a87e0 FCE |
3818 | if (lvalue) |
3819 | { | |
3820 | // Provide the functioncall to our parent, so that it can be | |
3821 | // used to substitute for the assignment node immediately above | |
3822 | // us. | |
3823 | assert(!target_symbol_setter_functioncalls.empty()); | |
3824 | *(target_symbol_setter_functioncalls.top()) = n; | |
3825 | } | |
70208613 | 3826 | |
1c0be8c7 JS |
3827 | // Revisit the functioncall so arguments can be expanded. |
3828 | n->visit (this); | |
66d284f4 FCE |
3829 | } |
3830 | catch (const semantic_error& er) | |
3831 | { | |
9fab2262 JS |
3832 | // We suppress this error message, and pass the unresolved |
3833 | // target_symbol to the next pass. We hope that this value ends | |
3834 | // up not being referenced after all, so it can be optimized out | |
3835 | // quietly. | |
1af1e62d | 3836 | e->chain (er); |
9fab2262 | 3837 | provide (e); |
66d284f4 | 3838 | } |
77de5e9e GH |
3839 | } |
3840 | ||
3841 | ||
c24447be JS |
3842 | void |
3843 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
3844 | { | |
3845 | // Fill in our current module context if needed | |
3846 | if (e->module.empty()) | |
3847 | e->module = q.dw.module_name; | |
3848 | ||
3849 | var_expanding_visitor::visit_cast_op(e); | |
3850 | } | |
3851 | ||
3852 | ||
8cc799a5 JS |
3853 | void |
3854 | dwarf_var_expanding_visitor::visit_entry_op (entry_op *e) | |
3855 | { | |
3856 | expression *repl = e; | |
3857 | if (q.has_return) | |
3858 | { | |
3859 | // expand the operand as if it weren't a return probe | |
3860 | q.has_return = false; | |
3861 | replace (e->operand); | |
3862 | q.has_return = true; | |
3863 | ||
3864 | // XXX it would be nice to use gen_kretprobe_saved_return when available, | |
3865 | // but it requires knowing the types already, which is problematic for | |
3866 | // arbitrary expressons. | |
cc9001af | 3867 | repl = gen_mapped_saved_return (e->operand, "entry"); |
8cc799a5 JS |
3868 | } |
3869 | provide (repl); | |
3870 | } | |
3871 | ||
3689db05 SC |
3872 | void |
3873 | dwarf_var_expanding_visitor::visit_perf_op (perf_op *e) | |
3874 | { | |
3875 | token* t = new token; | |
ace7c23f | 3876 | string e_lit_val = e->operand->value; |
3689db05 SC |
3877 | |
3878 | t->location = e->tok->location; | |
3879 | t->type = tok_identifier; | |
3880 | t->content = e_lit_val; | |
3881 | ||
3882 | add_block = new block; | |
9eaaceaa | 3883 | add_block->tok = e->tok; |
3689db05 SC |
3884 | |
3885 | systemtap_session &s = this->q.sess; | |
3886 | map<string, pair<string,derived_probe*> >::iterator it; | |
3887 | // Find the associated perf.counter probe | |
3888 | for (it=s.perf_counters.begin(); | |
3889 | it != s.perf_counters.end(); | |
3890 | it++) | |
3891 | if ((*it).first == e_lit_val) | |
3892 | { | |
3893 | // if perf .function("name") omitted, then set it to this process name | |
3894 | if ((*it).second.first.length() == 0) | |
3895 | ((*it).second).first = this->q.user_path; | |
3896 | if (((*it).second).first == this->q.user_path) | |
3897 | break; | |
3898 | } | |
3899 | ||
3900 | if (it != s.perf_counters.end()) | |
3901 | { | |
698de6cc | 3902 | perf_counter_refs.insert((*it).second.second); |
3689db05 SC |
3903 | // __perf_read_N is assigned in the probe prologue |
3904 | symbol* sym = new symbol; | |
3905 | sym->tok = t; | |
3906 | sym->name = "__perf_read_" + (*it).first; | |
3907 | provide (sym); | |
3908 | } | |
3909 | else | |
dc09353a | 3910 | throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str())); |
3689db05 SC |
3911 | } |
3912 | ||
8cc799a5 | 3913 | |
729455a7 JS |
3914 | vector<Dwarf_Die>& |
3915 | dwarf_var_expanding_visitor::getscopes(target_symbol *e) | |
3916 | { | |
3917 | if (scopes.empty()) | |
3918 | { | |
f25a9197 CM |
3919 | if(scope_die != NULL) |
3920 | scopes = q.dw.getscopes(scope_die); | |
729455a7 | 3921 | if (scopes.empty()) |
b530b5b3 LB |
3922 | //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok); |
3923 | // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" )) | |
dc09353a | 3924 | throw SEMANTIC_ERROR ("unable to find any scopes containing " |
729455a7 JS |
3925 | + lex_cast_hex(addr) |
3926 | + ((scope_die == NULL) ? "" | |
3927 | : (string (" in ") | |
3928 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
3929 | + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") | |
3930 | + ")")) | |
3931 | + " while searching for local '" | |
cc9001af | 3932 | + e->sym_name() + "'", |
729455a7 JS |
3933 | e->tok); |
3934 | } | |
3935 | return scopes; | |
3936 | } | |
3937 | ||
3938 | ||
5f36109e JS |
3939 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor |
3940 | { | |
3941 | systemtap_session& s; | |
3942 | dwarf_builder& db; | |
3943 | ||
3944 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
3945 | s(s), db(db) {} | |
3946 | void visit_cast_op (cast_op* e); | |
3947 | void filter_special_modules(string& module); | |
3948 | }; | |
3949 | ||
3950 | ||
c4ce66a1 JS |
3951 | struct dwarf_cast_query : public base_query |
3952 | { | |
946e1a48 | 3953 | cast_op& e; |
c4ce66a1 | 3954 | const bool lvalue; |
5f36109e JS |
3955 | const bool userspace_p; |
3956 | functioncall*& result; | |
c4ce66a1 | 3957 | |
5f36109e JS |
3958 | dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue, |
3959 | const bool userspace_p, functioncall*& result): | |
abb41d92 | 3960 | base_query(dw, module), e(e), lvalue(lvalue), |
5f36109e | 3961 | userspace_p(userspace_p), result(result) {} |
c4ce66a1 JS |
3962 | |
3963 | void handle_query_module(); | |
822a6a3d | 3964 | void query_library (const char *) {} |
576eaefe | 3965 | void query_plt (const char *entry, size_t addr) {} |
c4ce66a1 JS |
3966 | }; |
3967 | ||
3968 | ||
c4ce66a1 JS |
3969 | void |
3970 | dwarf_cast_query::handle_query_module() | |
3971 | { | |
5f36109e JS |
3972 | static unsigned tick = 0; |
3973 | ||
3974 | if (result) | |
c4ce66a1 JS |
3975 | return; |
3976 | ||
ea1e477a | 3977 | // look for the type in any CU |
a44a7cb5 JS |
3978 | Dwarf_Die* type_die = NULL; |
3979 | if (startswith(e.type_name, "class ")) | |
3980 | { | |
3981 | // normalize to match dwflpp::global_alias_caching_callback | |
3982 | string struct_name = "struct " + e.type_name.substr(6); | |
3983 | type_die = dw.declaration_resolve_other_cus(struct_name); | |
3984 | } | |
3985 | else | |
3986 | type_die = dw.declaration_resolve_other_cus(e.type_name); | |
3987 | ||
3988 | // NB: We now index the types as "struct name"/"union name"/etc. instead of | |
3989 | // just "name". But since we didn't require users to be explicit before, and | |
3990 | // actually sort of discouraged it, we must be flexible now. So if a lookup | |
3991 | // fails with a bare name, try augmenting it. | |
3992 | if (!type_die && | |
3993 | !startswith(e.type_name, "class ") && | |
3994 | !startswith(e.type_name, "struct ") && | |
3995 | !startswith(e.type_name, "union ") && | |
3996 | !startswith(e.type_name, "enum ")) | |
3997 | { | |
3998 | type_die = dw.declaration_resolve_other_cus("struct " + e.type_name); | |
3999 | if (!type_die) | |
4000 | type_die = dw.declaration_resolve_other_cus("union " + e.type_name); | |
4001 | if (!type_die) | |
4002 | type_die = dw.declaration_resolve_other_cus("enum " + e.type_name); | |
4003 | } | |
4004 | ||
ea1e477a JS |
4005 | if (!type_die) |
4006 | return; | |
c4ce66a1 | 4007 | |
5f36109e JS |
4008 | string code; |
4009 | exp_type type = pe_long; | |
4010 | ||
ea1e477a | 4011 | try |
c4ce66a1 | 4012 | { |
ea1e477a JS |
4013 | Dwarf_Die cu_mem; |
4014 | dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL)); | |
5f36109e JS |
4015 | |
4016 | if (!e.components.empty() && | |
4017 | e.components.back().type == target_symbol::comp_pretty_print) | |
4018 | { | |
4019 | if (lvalue) | |
dc09353a | 4020 | throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e.tok); |
5f36109e | 4021 | |
d19a9a82 | 4022 | dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e); |
5f36109e JS |
4023 | result = dpp.expand(); |
4024 | return; | |
4025 | } | |
4026 | ||
4027 | code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type); | |
ea1e477a JS |
4028 | } |
4029 | catch (const semantic_error& er) | |
4030 | { | |
4031 | // NB: we can have multiple errors, since a @cast | |
1af1e62d JS |
4032 | // may be attempted using several different modules: |
4033 | // @cast(ptr, "type", "module1:module2:...") | |
4034 | e.chain (er); | |
c4ce66a1 | 4035 | } |
c4ce66a1 | 4036 | |
5f36109e JS |
4037 | if (code.empty()) |
4038 | return; | |
c4ce66a1 | 4039 | |
5f36109e | 4040 | string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get") |
cc9001af | 4041 | + "_" + e.sym_name() |
5f36109e | 4042 | + "_" + lex_cast(tick++)); |
c4ce66a1 | 4043 | |
1c0be8c7 JS |
4044 | result = synthetic_embedded_deref_call(dw.sess, fname, code, type, |
4045 | userspace_p, lvalue, &e, e.operand); | |
5f36109e | 4046 | } |
c4ce66a1 JS |
4047 | |
4048 | ||
fb0274bc JS |
4049 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
4050 | { | |
d90053e7 | 4051 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 4052 | // for those cases, build a module including that header |
d90053e7 | 4053 | if (module[module.size() - 1] == '>' && |
60d98537 | 4054 | (module[0] == '<' || startswith(module, "kernel<"))) |
fb0274bc JS |
4055 | { |
4056 | string cached_module; | |
4057 | if (s.use_cache) | |
4058 | { | |
4059 | // see if the cached module exists | |
a2639cb7 | 4060 | cached_module = find_typequery_hash(s, module); |
d105f664 | 4061 | if (!cached_module.empty() && !s.poison_cache) |
fb0274bc JS |
4062 | { |
4063 | int fd = open(cached_module.c_str(), O_RDONLY); | |
4064 | if (fd != -1) | |
4065 | { | |
4066 | if (s.verbose > 2) | |
b530b5b3 LB |
4067 | //TRANSLATORS: Here we're using a cached module. |
4068 | clog << _("Pass 2: using cached ") << cached_module << endl; | |
fb0274bc JS |
4069 | module = cached_module; |
4070 | close(fd); | |
4071 | return; | |
4072 | } | |
4073 | } | |
4074 | } | |
4075 | ||
4076 | // no cached module, time to make it | |
d90053e7 | 4077 | if (make_typequery(s, module) == 0) |
fb0274bc | 4078 | { |
e16dc041 | 4079 | // try to save typequery in the cache |
fb0274bc | 4080 | if (s.use_cache) |
e16dc041 | 4081 | copy_file(module, cached_module, s.verbose > 2); |
fb0274bc JS |
4082 | } |
4083 | } | |
4084 | } | |
4085 | ||
4086 | ||
c4ce66a1 JS |
4087 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
4088 | { | |
4089 | bool lvalue = is_active_lvalue(e); | |
4090 | if (lvalue && !s.guru_mode) | |
dc09353a | 4091 | throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok); |
c4ce66a1 JS |
4092 | |
4093 | if (e->module.empty()) | |
4094 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
4095 | ||
5f36109e | 4096 | functioncall* result = NULL; |
8b31197b JS |
4097 | |
4098 | // split the module string by ':' for alternatives | |
4099 | vector<string> modules; | |
4100 | tokenize(e->module, modules, ":"); | |
b5a0dd41 | 4101 | bool userspace_p=false; // PR10601 |
5f36109e | 4102 | for (unsigned i = 0; !result && i < modules.size(); ++i) |
c4ce66a1 | 4103 | { |
8b31197b | 4104 | string& module = modules[i]; |
fb0274bc | 4105 | filter_special_modules(module); |
abb41d92 | 4106 | |
c4ce66a1 JS |
4107 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
4108 | // which means that userspace modules won't get any PATH searching. | |
4109 | dwflpp* dw; | |
707bf35e JS |
4110 | try |
4111 | { | |
b5a0dd41 FCE |
4112 | userspace_p=is_user_module (module); |
4113 | if (! userspace_p) | |
707bf35e JS |
4114 | { |
4115 | // kernel or kernel module target | |
ae2552da | 4116 | dw = db.get_kern_dw(s, module); |
707bf35e JS |
4117 | } |
4118 | else | |
4119 | { | |
05fb3e0c | 4120 | module = find_executable (module, "", s.sysenv); // canonicalize it |
707bf35e JS |
4121 | dw = db.get_user_dw(s, module); |
4122 | } | |
4123 | } | |
4124 | catch (const semantic_error& er) | |
4125 | { | |
4126 | /* ignore and go to the next module */ | |
4127 | continue; | |
4128 | } | |
c4ce66a1 | 4129 | |
5f36109e | 4130 | dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result); |
51178501 | 4131 | dw->iterate_over_modules(&query_module, &q); |
c4ce66a1 | 4132 | } |
abb41d92 | 4133 | |
5f36109e | 4134 | if (!result) |
c4ce66a1 | 4135 | { |
946e1a48 JS |
4136 | // We pass the unresolved cast_op to the next pass, and hope |
4137 | // that this value ends up not being referenced after all, so | |
4138 | // it can be optimized out quietly. | |
c4ce66a1 JS |
4139 | provide (e); |
4140 | return; | |
4141 | } | |
4142 | ||
c4ce66a1 JS |
4143 | if (lvalue) |
4144 | { | |
4145 | // Provide the functioncall to our parent, so that it can be | |
4146 | // used to substitute for the assignment node immediately above | |
4147 | // us. | |
4148 | assert(!target_symbol_setter_functioncalls.empty()); | |
5f36109e | 4149 | *(target_symbol_setter_functioncalls.top()) = result; |
c4ce66a1 JS |
4150 | } |
4151 | ||
5f36109e | 4152 | result->visit (this); |
77de5e9e GH |
4153 | } |
4154 | ||
4155 | ||
bd1fcbad YZ |
4156 | struct dwarf_atvar_expanding_visitor: public var_expanding_visitor |
4157 | { | |
4158 | systemtap_session& s; | |
4159 | dwarf_builder& db; | |
4160 | ||
4161 | dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
4162 | s(s), db(db) {} | |
4163 | void visit_atvar_op (atvar_op* e); | |
4164 | }; | |
4165 | ||
4166 | ||
4167 | struct dwarf_atvar_query: public base_query | |
4168 | { | |
4169 | atvar_op& e; | |
4170 | const bool userspace_p, lvalue; | |
4171 | functioncall*& result; | |
4172 | unsigned& tick; | |
4173 | const string cu_name_pattern; | |
4174 | ||
4175 | dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e, | |
4176 | const bool userspace_p, const bool lvalue, | |
4177 | functioncall*& result, | |
4178 | unsigned& tick): | |
4179 | base_query(dw, module), e(e), | |
4180 | userspace_p(userspace_p), lvalue(lvalue), result(result), | |
4181 | tick(tick), cu_name_pattern(string("*/") + e.cu_name) {} | |
4182 | ||
4183 | void handle_query_module (); | |
4184 | void query_library (const char *) {} | |
4185 | void query_plt (const char *entry, size_t addr) {} | |
4186 | static int atvar_query_cu (Dwarf_Die *cudie, void *data); | |
4187 | }; | |
4188 | ||
4189 | ||
4190 | int | |
4191 | dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, void * data) | |
4192 | { | |
4193 | dwarf_atvar_query * q = static_cast<dwarf_atvar_query *>(data); | |
4194 | ||
4195 | if (! q->e.cu_name.empty()) | |
4196 | { | |
4197 | const char *die_name = dwarf_diename(cudie); | |
4198 | ||
4199 | if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match | |
4200 | && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0) | |
4201 | { | |
4202 | return DWARF_CB_OK; | |
4203 | } | |
4204 | } | |
4205 | ||
4206 | try | |
4207 | { | |
4208 | vector<Dwarf_Die> scopes(1, *cudie); | |
4209 | ||
4210 | q->dw.focus_on_cu (cudie); | |
4211 | ||
4212 | if (! q->e.components.empty() && | |
4213 | q->e.components.back().type == target_symbol::comp_pretty_print) | |
4214 | { | |
4215 | dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(), | |
4216 | q->userspace_p, q->e); | |
4217 | q->result = dpp.expand(); | |
4218 | return DWARF_CB_ABORT; | |
4219 | } | |
4220 | ||
4221 | exp_type type = pe_long; | |
4222 | string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(), | |
4223 | &q->e, q->lvalue, type); | |
4224 | ||
4225 | if (code.empty()) | |
4226 | return DWARF_CB_OK; | |
4227 | ||
4228 | string fname = (string(q->lvalue ? "_dwarf_tvar_set" | |
4229 | : "_dwarf_tvar_get") | |
4230 | + "_" + q->e.sym_name() | |
4231 | + "_" + lex_cast(q->tick++)); | |
4232 | ||
4233 | q->result = synthetic_embedded_deref_call (q->sess, fname, code, type, | |
4234 | q->userspace_p, q->lvalue, | |
4235 | &q->e); | |
4236 | } | |
4237 | catch (const semantic_error& er) | |
4238 | { | |
4239 | // Here we suppress the error because we often just have too many | |
4240 | // when scanning all the CUs. | |
4241 | return DWARF_CB_OK; | |
4242 | } | |
4243 | ||
4244 | if (q->result) { | |
4245 | return DWARF_CB_ABORT; | |
4246 | } | |
4247 | ||
4248 | return DWARF_CB_OK; | |
4249 | } | |
4250 | ||
4251 | ||
4252 | void | |
4253 | dwarf_atvar_query::handle_query_module () | |
4254 | { | |
4255 | ||
4256 | dw.iterate_over_cus(atvar_query_cu, this, false); | |
4257 | } | |
4258 | ||
4259 | ||
4260 | void | |
4261 | dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e) | |
4262 | { | |
4263 | const bool lvalue = is_active_lvalue(e); | |
4264 | if (lvalue && !s.guru_mode) | |
dc09353a | 4265 | throw SEMANTIC_ERROR(_("write to @var variable not permitted; " |
bd1fcbad YZ |
4266 | "need stap -g"), e->tok); |
4267 | ||
4268 | if (e->module.empty()) | |
4269 | e->module = "kernel"; | |
4270 | ||
4271 | functioncall* result = NULL; | |
4272 | ||
4273 | // split the module string by ':' for alternatives | |
4274 | vector<string> modules; | |
4275 | tokenize(e->module, modules, ":"); | |
4276 | bool userspace_p = false; | |
4277 | for (unsigned i = 0; !result && i < modules.size(); ++i) | |
4278 | { | |
4279 | string& module = modules[i]; | |
4280 | ||
4281 | dwflpp* dw; | |
4282 | try | |
4283 | { | |
4284 | userspace_p = is_user_module(module); | |
4285 | if (!userspace_p) | |
4286 | { | |
4287 | // kernel or kernel module target | |
4288 | dw = db.get_kern_dw(s, module); | |
4289 | } | |
4290 | else | |
4291 | { | |
4292 | module = find_executable(module, "", s.sysenv); | |
4293 | dw = db.get_user_dw(s, module); | |
4294 | } | |
4295 | } | |
4296 | catch (const semantic_error& er) | |
4297 | { | |
4298 | /* ignore and go to the next module */ | |
4299 | continue; | |
4300 | } | |
4301 | ||
4302 | dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick); | |
4303 | dw->iterate_over_modules(&query_module, &q); | |
4304 | ||
4305 | if (result) | |
4306 | { | |
4307 | s.unwindsym_modules.insert(module); | |
bd1fcbad YZ |
4308 | |
4309 | if (lvalue) | |
4310 | { | |
4311 | // Provide the functioncall to our parent, so that it can be | |
4312 | // used to substitute for the assignment node immediately above | |
4313 | // us. | |
4314 | assert(!target_symbol_setter_functioncalls.empty()); | |
4315 | *(target_symbol_setter_functioncalls.top()) = result; | |
4316 | } | |
4317 | ||
4318 | result->visit(this); | |
4319 | return; | |
4320 | } | |
4321 | ||
4322 | /* Unable to find the variable in the current module, so we chain | |
4323 | * an error in atvar_op */ | |
dc09353a | 4324 | semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s", |
bd1fcbad YZ |
4325 | e->sym_name().c_str(), module.c_str(), |
4326 | e->cu_name.empty() ? "" : _(", in "), | |
4327 | e->cu_name.c_str())); | |
4328 | e->chain (er); | |
4329 | } | |
4330 | ||
4331 | provide(e); | |
4332 | } | |
4333 | ||
4334 | ||
b8da0ad1 FCE |
4335 | void |
4336 | dwarf_derived_probe::printsig (ostream& o) const | |
4337 | { | |
4338 | // Instead of just printing the plain locations, we add a PC value | |
4339 | // as a comment as a way of telling e.g. apart multiple inlined | |
4340 | // function instances. This is distinct from the verbose/clog | |
4341 | // output, since this part goes into the cache hash calculations. | |
4342 | sole_location()->print (o); | |
6d0f3f0c | 4343 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
4344 | printsig_nested (o); |
4345 | } | |
4346 | ||
4347 | ||
4348 | ||
dc38c0ae | 4349 | void |
b20febf3 FCE |
4350 | dwarf_derived_probe::join_group (systemtap_session& s) |
4351 | { | |
af234c40 JS |
4352 | // skip probes which are paired entry-handlers |
4353 | if (!has_return && (saved_longs || saved_strings)) | |
4354 | return; | |
4355 | ||
b20febf3 FCE |
4356 | if (! s.dwarf_derived_probes) |
4357 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
4358 | s.dwarf_derived_probes->enroll (this); | |
4359 | } | |
4360 | ||
4361 | ||
2b69faaf JS |
4362 | static bool |
4363 | kernel_supports_inode_uprobes(systemtap_session& s) | |
4364 | { | |
4365 | // The arch-supports is new to the builtin inode-uprobes, so it makes a | |
4366 | // reasonable indicator of the new API. Else we'll need an autoconf... | |
64e807c2 | 4367 | // see also buildrun.cxx:kernel_built_uprobs() |
2b69faaf JS |
4368 | return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y" |
4369 | && s.kernel_config["CONFIG_UPROBES"] == "y"); | |
4370 | } | |
4371 | ||
4372 | ||
3667d615 JS |
4373 | static bool |
4374 | kernel_supports_inode_uretprobes(systemtap_session& s) | |
4375 | { | |
766013af JS |
4376 | // We need inode-uprobes first, then look for a sign of uretprobes. The only |
4377 | // non-static function at present is arch_uretprobe_hijack_return_addr. | |
3667d615 | 4378 | return kernel_supports_inode_uprobes(s) && |
af9e147f | 4379 | (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0); |
3667d615 JS |
4380 | } |
4381 | ||
4382 | ||
5261f7ab DS |
4383 | void |
4384 | check_process_probe_kernel_support(systemtap_session& s) | |
4385 | { | |
4386 | // If we've got utrace, we're good to go. | |
4387 | if (s.kernel_config["CONFIG_UTRACE"] == "y") | |
4388 | return; | |
4389 | ||
8c021542 DS |
4390 | // We don't have utrace. For process probes that aren't |
4391 | // uprobes-based, we just need the task_finder. The task_finder | |
b266d318 DS |
4392 | // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a |
4393 | // specific autoconf test for its needs. | |
8c021542 DS |
4394 | // |
4395 | // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty | |
4396 | // approximation. | |
4397 | if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y") | |
4398 | return; | |
4399 | ||
d3e959b0 DS |
4400 | // For uprobes-based process probes, we need the task_finder plus |
4401 | // the builtin inode-uprobes. | |
8c021542 DS |
4402 | if (s.need_uprobes |
4403 | && s.kernel_config["CONFIG_TRACEPOINTS"] == "y" | |
d3e959b0 | 4404 | && kernel_supports_inode_uprobes(s)) |
8c021542 DS |
4405 | return; |
4406 | ||
dc09353a | 4407 | throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES")); |
5261f7ab DS |
4408 | } |
4409 | ||
4410 | ||
b20febf3 FCE |
4411 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, |
4412 | const string& filename, | |
4413 | int line, | |
91af0778 | 4414 | // module & section specify a relocation |
b20febf3 FCE |
4415 | // base for <addr>, unless section=="" |
4416 | // (equivalently module=="kernel") | |
4417 | const string& module, | |
4418 | const string& section, | |
4419 | // NB: dwfl_addr is the virtualized | |
4420 | // address for this symbol. | |
4421 | Dwarf_Addr dwfl_addr, | |
4422 | // addr is the section-offset for | |
4423 | // actual relocation. | |
4424 | Dwarf_Addr addr, | |
4425 | dwarf_query& q, | |
37ebca01 | 4426 | Dwarf_Die* scope_die /* may be null */) |
4c5d1300 | 4427 | : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ), |
b20febf3 | 4428 | module (module), section (section), addr (addr), |
63b4fd14 | 4429 | path (q.path), |
27dc09b1 | 4430 | has_process (q.has_process), |
c9bad430 DS |
4431 | has_return (q.has_return), |
4432 | has_maxactive (q.has_maxactive), | |
c57ea854 | 4433 | has_library (q.has_library), |
6b66b9f7 | 4434 | maxactive_val (q.maxactive_val), |
b642c901 SC |
4435 | user_path (q.user_path), |
4436 | user_lib (q.user_lib), | |
af234c40 | 4437 | access_vars(false), |
c57ea854 | 4438 | saved_longs(0), saved_strings(0), |
af234c40 | 4439 | entry_handler(0) |
bd2b1e68 | 4440 | { |
b642c901 SC |
4441 | if (user_lib.size() != 0) |
4442 | has_library = true; | |
4443 | ||
6b66b9f7 JS |
4444 | if (q.has_process) |
4445 | { | |
4446 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. | |
4447 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
4448 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
4449 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
4450 | // XXX Assert invariants here too? | |
2b69faaf JS |
4451 | |
4452 | // inode-uprobes needs an offset rather than an absolute VM address. | |
4441e344 | 4453 | // ditto for userspace runtimes (dyninst) |
ac3af990 | 4454 | if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) && |
2b69faaf JS |
4455 | section == ".absolute" && addr == dwfl_addr && |
4456 | addr >= q.dw.module_start && addr < q.dw.module_end) | |
4457 | this->addr = addr - q.dw.module_start; | |
6b66b9f7 JS |
4458 | } |
4459 | else | |
4460 | { | |
4461 | // Assert kernel relocation invariants | |
4462 | if (section == "" && dwfl_addr != addr) // addr should be absolute | |
dc09353a | 4463 | throw SEMANTIC_ERROR (_("missing relocation basis"), tok); |
6b66b9f7 | 4464 | if (section != "" && dwfl_addr == addr) // addr should be an offset |
dc09353a | 4465 | throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok); |
6b66b9f7 | 4466 | } |
2930abc7 | 4467 | |
21beacc9 FCE |
4468 | // XXX: hack for strange g++/gcc's |
4469 | #ifndef USHRT_MAX | |
4470 | #define USHRT_MAX 32767 | |
4471 | #endif | |
4472 | ||
606fd9c8 | 4473 | // Range limit maxactive() value |
6b66b9f7 | 4474 | if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX)) |
dc09353a | 4475 | throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]", |
b530b5b3 | 4476 | lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok); |
606fd9c8 | 4477 | |
de688825 | 4478 | // Expand target variables in the probe body |
5f0a03a6 | 4479 | if (!null_die(scope_die)) |
8fc05e57 | 4480 | { |
6b66b9f7 | 4481 | // XXX: user-space deref's for q.has_process! |
de688825 | 4482 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
8b095b45 | 4483 | v.replace (this->body); |
3689db05 SC |
4484 | |
4485 | // Propagate perf.counters so we can emit later | |
4486 | this->perf_counter_refs = v.perf_counter_refs; | |
4487 | // Emit local var used to save the perf counter read value | |
698de6cc | 4488 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
4489 | for (pcii = v.perf_counter_refs.begin(); |
4490 | pcii != v.perf_counter_refs.end(); pcii++) | |
4491 | { | |
4492 | map<string, pair<string,derived_probe*> >::iterator it; | |
4493 | // Find the associated perf counter probe | |
4494 | for (it=q.sess.perf_counters.begin() ; | |
4495 | it != q.sess.perf_counters.end(); | |
4496 | it++) | |
4497 | if ((*it).second.second == (*pcii)) | |
4498 | break; | |
4499 | vardecl* vd = new vardecl; | |
4500 | vd->name = "__perf_read_" + (*it).first; | |
4501 | vd->tok = this->tok; | |
4502 | vd->set_arity(0, this->tok); | |
4503 | vd->type = pe_long; | |
4504 | vd->synthetic = true; | |
4505 | this->locals.push_back (vd); | |
4506 | } | |
4507 | ||
4508 | ||
6b66b9f7 JS |
4509 | if (!q.has_process) |
4510 | access_vars = v.visited; | |
37ebca01 FCE |
4511 | |
4512 | // If during target-variable-expanding the probe, we added a new block | |
4513 | // of code, add it to the start of the probe. | |
4514 | if (v.add_block) | |
ba6f838d | 4515 | this->body = new block(v.add_block, this->body); |
2260f4e3 FCE |
4516 | |
4517 | // If when target-variable-expanding the probe, we need to synthesize a | |
4518 | // sibling function-entry probe. We don't go through the whole probe derivation | |
4519 | // business (PR10642) that could lead to wildcard/alias resolution, or for that | |
4520 | // dwarf-induced duplication. | |
4521 | if (v.add_call_probe) | |
37ebca01 | 4522 | { |
2260f4e3 FCE |
4523 | assert (q.has_return && !q.has_call); |
4524 | ||
4525 | // We temporarily replace q.base_probe. | |
4526 | statement* old_body = q.base_probe->body; | |
4527 | q.base_probe->body = v.add_call_probe; | |
4528 | q.has_return = false; | |
4529 | q.has_call = true; | |
af234c40 | 4530 | |
da23eceb | 4531 | if (q.has_process) |
af234c40 JS |
4532 | entry_handler = new uprobe_derived_probe (funcname, filename, line, |
4533 | module, section, dwfl_addr, | |
4534 | addr, q, scope_die); | |
da23eceb | 4535 | else |
af234c40 JS |
4536 | entry_handler = new dwarf_derived_probe (funcname, filename, line, |
4537 | module, section, dwfl_addr, | |
4538 | addr, q, scope_die); | |
4539 | ||
4540 | saved_longs = entry_handler->saved_longs = v.saved_longs; | |
4541 | saved_strings = entry_handler->saved_strings = v.saved_strings; | |
4542 | ||
4543 | q.results.push_back (entry_handler); | |
2260f4e3 FCE |
4544 | |
4545 | q.has_return = true; | |
4546 | q.has_call = false; | |
4547 | q.base_probe->body = old_body; | |
37ebca01 | 4548 | } |
f10534c6 WH |
4549 | // Save the local variables for listing mode |
4550 | if (q.sess.listing_mode_vars) | |
8c67c337 | 4551 | saveargs(q, scope_die, dwfl_addr); |
8fc05e57 | 4552 | } |
37ebca01 | 4553 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 4554 | |
f10534c6 | 4555 | // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs |
0a98fd42 | 4556 | |
5d23847d | 4557 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
4558 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
4559 | // point. This allows a user to see what function / file / line | |
4560 | // number any particular match of the wildcards. | |
2930abc7 | 4561 | |
a229fcd7 | 4562 | vector<probe_point::component*> comps; |
91af0778 FCE |
4563 | if (q.has_kernel) |
4564 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
4565 | else if(q.has_module) | |
4566 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
4567 | else if(q.has_process) | |
4568 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
4569 | else | |
4570 | assert (0); | |
b5d77020 | 4571 | |
db520b00 FCE |
4572 | string fn_or_stmt; |
4573 | if (q.has_function_str || q.has_function_num) | |
4574 | fn_or_stmt = "function"; | |
4575 | else | |
4576 | fn_or_stmt = "statement"; | |
a229fcd7 | 4577 | |
b8da0ad1 | 4578 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 4579 | { |
4cd232e4 | 4580 | string retro_name = funcname; |
b20febf3 | 4581 | if (filename != "") |
cee35f73 | 4582 | { |
fb84c077 | 4583 | retro_name += ("@" + string (filename)); |
cee35f73 | 4584 | if (line > 0) |
aca66a36 | 4585 | retro_name += (":" + lex_cast (line)); |
cee35f73 | 4586 | } |
db520b00 FCE |
4587 | comps.push_back |
4588 | (new probe_point::component | |
4589 | (fn_or_stmt, new literal_string (retro_name))); | |
4590 | } | |
b8da0ad1 | 4591 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
4592 | { |
4593 | Dwarf_Addr retro_addr; | |
4594 | if (q.has_function_num) | |
4595 | retro_addr = q.function_num_val; | |
4596 | else | |
4597 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
4598 | comps.push_back (new probe_point::component |
4599 | (fn_or_stmt, | |
9ea68eb9 | 4600 | new literal_number(retro_addr, true))); |
37ebca01 FCE |
4601 | |
4602 | if (q.has_absolute) | |
4603 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
4604 | } |
4605 | ||
b8da0ad1 FCE |
4606 | if (q.has_call) |
4607 | comps.push_back (new probe_point::component(TOK_CALL)); | |
4bda987e SC |
4608 | if (q.has_exported) |
4609 | comps.push_back (new probe_point::component(TOK_EXPORTED)); | |
b8da0ad1 FCE |
4610 | if (q.has_inline) |
4611 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 4612 | if (has_return) |
b8da0ad1 FCE |
4613 | comps.push_back (new probe_point::component(TOK_RETURN)); |
4614 | if (has_maxactive) | |
4615 | comps.push_back (new probe_point::component | |
4616 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 4617 | |
5d23847d FCE |
4618 | // Overwrite it. |
4619 | this->sole_location()->components = comps; | |
2930abc7 FCE |
4620 | } |
4621 | ||
bd2b1e68 | 4622 | |
0a98fd42 | 4623 | void |
8c67c337 JS |
4624 | dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die, |
4625 | Dwarf_Addr dwfl_addr) | |
0a98fd42 | 4626 | { |
9aa8ffce | 4627 | if (null_die(scope_die)) |
0a98fd42 | 4628 | return; |
0a98fd42 | 4629 | |
8c67c337 | 4630 | bool verbose = q.sess.verbose > 2; |
0a98fd42 | 4631 | |
8c67c337 | 4632 | if (verbose) |
b530b5b3 | 4633 | clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die)); |
0a98fd42 | 4634 | |
8c67c337 JS |
4635 | if (has_return) |
4636 | { | |
4637 | /* Only save the return value if it has a type. */ | |
4638 | string type_name; | |
4639 | Dwarf_Die type_die; | |
4640 | if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) && | |
4641 | dwarf_type_name(&type_die, type_name)) | |
4642 | args.push_back("$return:"+type_name); | |
4643 | ||
4644 | else if (verbose) | |
b530b5b3 LB |
4645 | clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n", |
4646 | lex_cast_hex(dwarf_dieoffset(scope_die)).c_str()); | |
8c67c337 | 4647 | } |
d87623a1 | 4648 | |
0a98fd42 | 4649 | Dwarf_Die arg; |
4ef35696 JS |
4650 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
4651 | for (unsigned i = 0; i < scopes.size(); ++i) | |
4652 | { | |
4653 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
4654 | break; // we don't want file-level variables | |
4655 | if (dwarf_child (&scopes[i], &arg) == 0) | |
4656 | do | |
0a98fd42 | 4657 | { |
4ef35696 JS |
4658 | switch (dwarf_tag (&arg)) |
4659 | { | |
4660 | case DW_TAG_variable: | |
4661 | case DW_TAG_formal_parameter: | |
4662 | break; | |
0a98fd42 | 4663 | |
4ef35696 JS |
4664 | default: |
4665 | continue; | |
4666 | } | |
0a98fd42 | 4667 | |
4ef35696 JS |
4668 | /* Ignore this local if it has no name. */ |
4669 | const char *arg_name = dwarf_diename (&arg); | |
4670 | if (!arg_name) | |
8c67c337 JS |
4671 | { |
4672 | if (verbose) | |
b530b5b3 LB |
4673 | clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n", |
4674 | lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4675 | continue; |
4676 | } | |
4ef35696 JS |
4677 | |
4678 | if (verbose) | |
b530b5b3 LB |
4679 | clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n", |
4680 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4681 | |
4682 | /* Ignore this local if it has no location (or not at this PC). */ | |
4683 | /* NB: It still may not be directly accessible, e.g. if it is an | |
4684 | * aggregate type, implicit_pointer, etc., but the user can later | |
4685 | * figure out how to access the interesting parts. */ | |
45b02a36 FCE |
4686 | |
4687 | /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing | |
4688 | * several synthetic | |
4689 | * probe foo { $var } | |
4690 | * probes, testing them for overall resolvability. | |
4691 | */ | |
4692 | ||
4ef35696 JS |
4693 | Dwarf_Attribute attr_mem; |
4694 | if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem)) | |
4695 | { | |
4696 | Dwarf_Op *expr; | |
4697 | size_t len; | |
4698 | if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem)) | |
4699 | { | |
4700 | if (verbose) | |
b530b5b3 LB |
4701 | clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n", |
4702 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4703 | continue; |
4704 | } | |
4705 | else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr, | |
4706 | &len, 1) == 1 && len > 0)) | |
4707 | { | |
45b02a36 FCE |
4708 | Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg); |
4709 | if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr, | |
4710 | &len, 1) == 1 && len > 0))) { | |
4711 | if (verbose) | |
4712 | clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n", | |
4713 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str()); | |
4714 | continue; | |
4715 | } | |
4ef35696 JS |
4716 | } |
4717 | } | |
4718 | ||
4719 | /* Ignore this local if it has no type. */ | |
4720 | string type_name; | |
4721 | Dwarf_Die type_die; | |
4722 | if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) || | |
4723 | !dwarf_type_name(&type_die, type_name)) | |
8c67c337 JS |
4724 | { |
4725 | if (verbose) | |
b530b5b3 LB |
4726 | clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n", |
4727 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4728 | continue; |
4729 | } | |
8c67c337 | 4730 | |
4ef35696 JS |
4731 | /* This local looks good -- save it! */ |
4732 | args.push_back("$"+string(arg_name)+":"+type_name); | |
8c67c337 | 4733 | } |
4ef35696 JS |
4734 | while (dwarf_siblingof (&arg, &arg) == 0); |
4735 | } | |
0a98fd42 JS |
4736 | } |
4737 | ||
4738 | ||
4739 | void | |
d0bfd2ac | 4740 | dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const |
0a98fd42 | 4741 | { |
d0bfd2ac | 4742 | arg_set.insert(arg_set.end(), args.begin(), args.end()); |
0a98fd42 JS |
4743 | } |
4744 | ||
4745 | ||
27dc09b1 | 4746 | void |
42e38653 | 4747 | dwarf_derived_probe::emit_privilege_assertion (translator_output* o) |
27dc09b1 DB |
4748 | { |
4749 | if (has_process) | |
4750 | { | |
4751 | // These probes are allowed for unprivileged users, but only in the | |
4752 | // context of processes which they own. | |
4753 | emit_process_owner_assertion (o); | |
4754 | return; | |
4755 | } | |
4756 | ||
4757 | // Other probes must contain the default assertion which aborts | |
4758 | // if executed by an unprivileged user. | |
42e38653 | 4759 | derived_probe::emit_privilege_assertion (o); |
27dc09b1 DB |
4760 | } |
4761 | ||
4762 | ||
4763 | void | |
4764 | dwarf_derived_probe::print_dupe_stamp(ostream& o) | |
4765 | { | |
4766 | if (has_process) | |
4767 | { | |
4768 | // These probes are allowed for unprivileged users, but only in the | |
4769 | // context of processes which they own. | |
4770 | print_dupe_stamp_unprivileged_process_owner (o); | |
4771 | return; | |
4772 | } | |
4773 | ||
4774 | // Other probes must contain the default dupe stamp | |
4775 | derived_probe::print_dupe_stamp (o); | |
4776 | } | |
4777 | ||
64211010 | 4778 | |
7a053d3b | 4779 | void |
20c6c071 | 4780 | dwarf_derived_probe::register_statement_variants(match_node * root, |
27dc09b1 | 4781 | dwarf_builder * dw, |
42e38653 | 4782 | privilege_t privilege) |
bd2b1e68 | 4783 | { |
27dc09b1 | 4784 | root |
42e38653 | 4785 | ->bind_privilege(privilege) |
27dc09b1 | 4786 | ->bind(dw); |
54efe513 GH |
4787 | } |
4788 | ||
7a053d3b | 4789 | void |
fd6602a0 | 4790 | dwarf_derived_probe::register_function_variants(match_node * root, |
27dc09b1 | 4791 | dwarf_builder * dw, |
42e38653 | 4792 | privilege_t privilege) |
2865d17a | 4793 | { |
27dc09b1 | 4794 | root |
42e38653 | 4795 | ->bind_privilege(privilege) |
27dc09b1 | 4796 | ->bind(dw); |
27dc09b1 | 4797 | root->bind(TOK_CALL) |
42e38653 | 4798 | ->bind_privilege(privilege) |
27dc09b1 | 4799 | ->bind(dw); |
4bda987e SC |
4800 | root->bind(TOK_EXPORTED) |
4801 | ->bind_privilege(privilege) | |
4802 | ->bind(dw); | |
27dc09b1 | 4803 | root->bind(TOK_RETURN) |
42e38653 | 4804 | ->bind_privilege(privilege) |
27dc09b1 | 4805 | ->bind(dw); |
1e035395 | 4806 | |
f6be7c06 DB |
4807 | // For process probes / uprobes, .maxactive() is unused. |
4808 | if (! pr_contains (privilege, pr_stapusr)) | |
1e035395 FCE |
4809 | { |
4810 | root->bind(TOK_RETURN) | |
1e035395 FCE |
4811 | ->bind_num(TOK_MAXACTIVE)->bind(dw); |
4812 | } | |
bd2b1e68 GH |
4813 | } |
4814 | ||
7a053d3b | 4815 | void |
27dc09b1 | 4816 | dwarf_derived_probe::register_function_and_statement_variants( |
440d9b00 | 4817 | systemtap_session& s, |
27dc09b1 DB |
4818 | match_node * root, |
4819 | dwarf_builder * dw, | |
42e38653 | 4820 | privilege_t privilege |
27dc09b1 | 4821 | ) |
bd2b1e68 GH |
4822 | { |
4823 | // Here we match 4 forms: | |
4824 | // | |
4825 | // .function("foo") | |
4826 | // .function(0xdeadbeef) | |
4827 | // .statement("foo") | |
4828 | // .statement(0xdeadbeef) | |
4829 | ||
440d9b00 | 4830 | match_node *fv_root = root->bind_str(TOK_FUNCTION); |
42e38653 | 4831 | register_function_variants(fv_root, dw, privilege); |
7f02ca94 | 4832 | // ROOT.function("STRING") always gets the .inline and .label variants. |
440d9b00 | 4833 | fv_root->bind(TOK_INLINE) |
42e38653 | 4834 | ->bind_privilege(privilege) |
440d9b00 | 4835 | ->bind(dw); |
7f02ca94 JS |
4836 | fv_root->bind_str(TOK_LABEL) |
4837 | ->bind_privilege(privilege) | |
440d9b00 DB |
4838 | ->bind(dw); |
4839 | ||
4840 | fv_root = root->bind_num(TOK_FUNCTION); | |
42e38653 | 4841 | register_function_variants(fv_root, dw, privilege); |
440d9b00 DB |
4842 | // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter. |
4843 | if (strverscmp(s.compatible.c_str(), "1.7") <= 0) | |
4844 | { | |
4845 | fv_root->bind(TOK_INLINE) | |
42e38653 | 4846 | ->bind_privilege(privilege) |
440d9b00 DB |
4847 | ->bind(dw); |
4848 | } | |
4849 | ||
42e38653 DB |
4850 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege); |
4851 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege); | |
bd2b1e68 GH |
4852 | } |
4853 | ||
b1615c74 JS |
4854 | void |
4855 | dwarf_derived_probe::register_sdt_variants(systemtap_session& s, | |
4856 | match_node * root, | |
4857 | dwarf_builder * dw) | |
4858 | { | |
4859 | root->bind_str(TOK_MARK) | |
f66bb29a | 4860 | ->bind_privilege(pr_all) |
b1615c74 JS |
4861 | ->bind(dw); |
4862 | root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) | |
f66bb29a | 4863 | ->bind_privilege(pr_all) |
b1615c74 JS |
4864 | ->bind(dw); |
4865 | } | |
4866 | ||
4867 | void | |
4868 | dwarf_derived_probe::register_plt_variants(systemtap_session& s, | |
4869 | match_node * root, | |
4870 | dwarf_builder * dw) | |
4871 | { | |
4872 | root->bind(TOK_PLT) | |
f66bb29a | 4873 | ->bind_privilege(pr_all) |
b1615c74 JS |
4874 | ->bind(dw); |
4875 | root->bind_str(TOK_PLT) | |
f66bb29a | 4876 | ->bind_privilege(pr_all) |
b1615c74 JS |
4877 | ->bind(dw); |
4878 | root->bind(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 4879 | ->bind_privilege(pr_all) |
b1615c74 JS |
4880 | ->bind(dw); |
4881 | root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 4882 | ->bind_privilege(pr_all) |
b1615c74 | 4883 | ->bind(dw); |
bd2b1e68 GH |
4884 | } |
4885 | ||
4886 | void | |
c4ce66a1 | 4887 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 4888 | { |
c4ce66a1 | 4889 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
4890 | dwarf_builder *dw = new dwarf_builder(); |
4891 | ||
c4ce66a1 JS |
4892 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
4893 | s.code_filters.push_back(filter); | |
4894 | ||
bd1fcbad YZ |
4895 | filter = new dwarf_atvar_expanding_visitor(s, *dw); |
4896 | s.code_filters.push_back(filter); | |
4897 | ||
73f52eb4 DB |
4898 | register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged); |
4899 | register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged); | |
27dc09b1 DB |
4900 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) |
4901 | ->bind(dw); | |
2cab6244 | 4902 | |
7f02ca94 JS |
4903 | match_node* uprobes[] = { |
4904 | root->bind(TOK_PROCESS), | |
4905 | root->bind_str(TOK_PROCESS), | |
4906 | root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
4907 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
4908 | }; | |
4909 | for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i) | |
4910 | { | |
f66bb29a | 4911 | register_function_and_statement_variants(s, uprobes[i], dw, pr_all); |
7f02ca94 JS |
4912 | register_sdt_variants(s, uprobes[i], dw); |
4913 | register_plt_variants(s, uprobes[i], dw); | |
4914 | } | |
bd2b1e68 GH |
4915 | } |
4916 | ||
9020300d | 4917 | void |
3689db05 SC |
4918 | dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o) |
4919 | { | |
698de6cc | 4920 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
4921 | for (pcii = perf_counter_refs.begin(); |
4922 | pcii != perf_counter_refs.end(); | |
4923 | pcii++) | |
4924 | { | |
4925 | map<string, pair<string,derived_probe*> >::iterator it; | |
4926 | // Find the associated perf.counter probe | |
4927 | unsigned i = 0; | |
4928 | for (it=s.perf_counters.begin() ; | |
4929 | it != s.perf_counters.end(); | |
4930 | it++, i++) | |
4931 | if ((*it).second.second == (*pcii)) | |
4932 | break; | |
4933 | // place the perf counter read so it precedes stp_lock_probe | |
4934 | o->newline() << "l->l___perf_read_" + (*it).first | |
4935 | + " = (((int64_t) (_stp_perf_read(smp_processor_id()," | |
4936 | + lex_cast(i) + "))));"; | |
4937 | } | |
4938 | ||
b95e2b79 MH |
4939 | if (access_vars) |
4940 | { | |
4941 | // if accessing $variables, emit bsp cache setup for speeding up | |
d4670309 | 4942 | o->newline() << "#if defined __ia64__"; |
d9aed31e | 4943 | o->newline() << "bspcache(c->unwaddr, c->kregs);"; |
d4670309 | 4944 | o->newline() << "#endif"; |
b95e2b79 | 4945 | } |
9020300d | 4946 | } |
2930abc7 | 4947 | |
b20febf3 | 4948 | // ------------------------------------------------------------------------ |
46b84a80 DS |
4949 | |
4950 | void | |
b20febf3 | 4951 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 4952 | { |
b20febf3 | 4953 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
4954 | |
4955 | // XXX: probes put at the same address should all share a | |
4956 | // single kprobe/kretprobe, and have their handlers executed | |
4957 | // sequentially. | |
b55bc428 FCE |
4958 | } |
4959 | ||
7a053d3b | 4960 | void |
775d51e5 | 4961 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 4962 | { |
b20febf3 | 4963 | if (probes_by_module.empty()) return; |
2930abc7 | 4964 | |
775d51e5 DS |
4965 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
4966 | ||
4967 | // Warn of misconfigured kernels | |
f41595cc FCE |
4968 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
4969 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
4970 | s.op->newline() << "#endif"; | |
775d51e5 | 4971 | s.op->newline(); |
f41595cc | 4972 | |
f07c3b68 | 4973 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 4974 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
4975 | s.op->newline() << "#endif"; |
4976 | ||
14cf7e42 | 4977 | // Forward decls |
2ba1736a | 4978 | s.op->newline() << "#include \"linux/kprobes-common.h\""; |
14cf7e42 | 4979 | |
b20febf3 FCE |
4980 | // Forward declare the master entry functions |
4981 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4982 | s.op->line() << " struct pt_regs *regs);"; | |
4983 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4984 | s.op->line() << " struct pt_regs *regs);"; | |
4985 | ||
42cb22bd MH |
4986 | // Emit an array of kprobe/kretprobe pointers |
4987 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4988 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
4989 | s.op->newline() << "#endif"; | |
4990 | ||
b20febf3 | 4991 | // Emit the actual probe list. |
606fd9c8 FCE |
4992 | |
4993 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
4994 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
4995 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
14cf7e42 | 4996 | s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
606fd9c8 FCE |
4997 | // NB: bss! |
4998 | ||
4c2732a1 | 4999 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
5000 | s.op->newline(1) << "const unsigned return_p:1;"; |
5001 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 5002 | s.op->newline() << "const unsigned optional_p:1;"; |
b20febf3 | 5003 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 5004 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 | 5005 | |
af234c40 JS |
5006 | // data saved in the kretprobe_instance packet |
5007 | s.op->newline() << "const unsigned short saved_longs;"; | |
5008 | s.op->newline() << "const unsigned short saved_strings;"; | |
5009 | ||
faea5e16 | 5010 | // Let's find some stats for the embedded strings. Maybe they |
606fd9c8 FCE |
5011 | // are small and uniform enough to justify putting char[MAX]'s into |
5012 | // the array instead of relocated char*'s. | |
faea5e16 JS |
5013 | size_t module_name_max = 0, section_name_max = 0; |
5014 | size_t module_name_tot = 0, section_name_tot = 0; | |
606fd9c8 FCE |
5015 | size_t all_name_cnt = probes_by_module.size(); // for average |
5016 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
5017 | { | |
5018 | dwarf_derived_probe* p = it->second; | |
5019 | #define DOIT(var,expr) do { \ | |
5020 | size_t var##_size = (expr) + 1; \ | |
5021 | var##_max = max (var##_max, var##_size); \ | |
5022 | var##_tot += var##_size; } while (0) | |
5023 | DOIT(module_name, p->module.size()); | |
5024 | DOIT(section_name, p->section.size()); | |
606fd9c8 FCE |
5025 | #undef DOIT |
5026 | } | |
5027 | ||
5028 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
5029 | // the amount of average waste (max - avg) to the relocation data size | |
5030 | // (3 native long words). | |
5031 | #define CALCIT(var) \ | |
5032 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
5033 | { \ | |
5034 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
5035 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
5036 | << "[" << var##_name_max << "]" << endl; \ | |
5037 | } \ | |
5038 | else \ | |
5039 | { \ | |
b0986e7a | 5040 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
5041 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
5042 | } | |
5043 | ||
5044 | CALCIT(module); | |
5045 | CALCIT(section); | |
e6fe60e7 | 5046 | #undef CALCIT |
606fd9c8 | 5047 | |
b0986e7a | 5048 | s.op->newline() << "const unsigned long address;"; |
7c3e97f4 JS |
5049 | s.op->newline() << "const struct stap_probe * const probe;"; |
5050 | s.op->newline() << "const struct stap_probe * const entry_probe;"; | |
b20febf3 FCE |
5051 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
5052 | s.op->indent(1); | |
5053 | ||
5054 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 5055 | { |
b20febf3 FCE |
5056 | dwarf_derived_probe* p = it->second; |
5057 | s.op->newline() << "{"; | |
5058 | if (p->has_return) | |
5059 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 5060 | if (p->has_maxactive) |
606fd9c8 FCE |
5061 | { |
5062 | s.op->line() << " .maxactive_p=1,"; | |
5063 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
5064 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
5065 | } | |
af234c40 JS |
5066 | if (p->saved_longs || p->saved_strings) |
5067 | { | |
5068 | if (p->saved_longs) | |
5069 | s.op->line() << " .saved_longs=" << p->saved_longs << ","; | |
5070 | if (p->saved_strings) | |
5071 | s.op->line() << " .saved_strings=" << p->saved_strings << ","; | |
5072 | if (p->entry_handler) | |
c87ae2c1 | 5073 | s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ","; |
af234c40 | 5074 | } |
b350f56b JS |
5075 | if (p->locations[0]->optional) |
5076 | s.op->line() << " .optional_p=1,"; | |
dc38c256 | 5077 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
5078 | s.op->line() << " .module=\"" << p->module << "\","; |
5079 | s.op->line() << " .section=\"" << p->section << "\","; | |
faea5e16 | 5080 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
b20febf3 | 5081 | s.op->line() << " },"; |
2930abc7 | 5082 | } |
2930abc7 | 5083 | |
b20febf3 FCE |
5084 | s.op->newline(-1) << "};"; |
5085 | ||
5086 | // Emit the kprobes callback function | |
5087 | s.op->newline(); | |
5088 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
5089 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
5090 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
5091 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
5092 | // Check that the index is plausible | |
5093 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5094 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5095 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5096 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5097 | s.op->line() << "];"; | |
71db462b | 5098 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 5099 | "stp_probe_type_kprobe"); |
d9aed31e | 5100 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
5101 | |
5102 | // Make it look like the IP is set as it wouldn't have been replaced | |
5103 | // by a breakpoint instruction when calling real probe handler. Reset | |
5104 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
5105 | s.op->newline() << "{"; | |
5106 | s.op->indent(1); | |
d9aed31e | 5107 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 5108 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 5109 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 5110 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
5111 | s.op->newline(-1) << "}"; |
5112 | ||
f887a8c9 | 5113 | common_probe_entryfn_epilogue (s, true); |
b20febf3 FCE |
5114 | s.op->newline() << "return 0;"; |
5115 | s.op->newline(-1) << "}"; | |
5116 | ||
5117 | // Same for kretprobes | |
5118 | s.op->newline(); | |
af234c40 JS |
5119 | s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,"; |
5120 | s.op->line() << " struct pt_regs *regs, int entry) {"; | |
b20febf3 | 5121 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; |
606fd9c8 FCE |
5122 | |
5123 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 5124 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
5125 | // Check that the index is plausible |
5126 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
5127 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
5128 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
5129 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
5130 | s.op->line() << "];"; | |
5131 | ||
7c3e97f4 | 5132 | s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;"; |
c87ae2c1 JS |
5133 | s.op->newline() << "if (sp) {"; |
5134 | s.op->indent(1); | |
71db462b | 5135 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp", |
cda141c2 | 5136 | "stp_probe_type_kretprobe"); |
d9aed31e | 5137 | s.op->newline() << "c->kregs = regs;"; |
af234c40 JS |
5138 | |
5139 | // for assisting runtime's backtrace logic and accessing kretprobe data packets | |
6dceb5c9 MW |
5140 | s.op->newline() << "c->ips.krp.pi = inst;"; |
5141 | s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;"; | |
6415ddde MW |
5142 | |
5143 | // Make it look like the IP is set as it wouldn't have been replaced | |
5144 | // by a breakpoint instruction when calling real probe handler. Reset | |
5145 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
5146 | s.op->newline() << "{"; | |
d9aed31e | 5147 | s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
c87ae2c1 JS |
5148 | s.op->newline() << "if (entry)"; |
5149 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; | |
5150 | s.op->newline(-1) << "else"; | |
5151 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);"; | |
5152 | s.op->newline(-1) << "(sp->ph) (c);"; | |
259d54c0 | 5153 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
5154 | s.op->newline(-1) << "}"; |
5155 | ||
f887a8c9 | 5156 | common_probe_entryfn_epilogue (s, true); |
c87ae2c1 | 5157 | s.op->newline(-1) << "}"; |
b20febf3 FCE |
5158 | s.op->newline() << "return 0;"; |
5159 | s.op->newline(-1) << "}"; | |
af234c40 JS |
5160 | |
5161 | s.op->newline(); | |
5162 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
5163 | s.op->line() << " struct pt_regs *regs) {"; | |
5164 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);"; | |
5165 | s.op->newline(-1) << "}"; | |
5166 | ||
5167 | s.op->newline(); | |
5168 | s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,"; | |
5169 | s.op->line() << " struct pt_regs *regs) {"; | |
5170 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);"; | |
5171 | s.op->newline(-1) << "}"; | |
b642c901 | 5172 | |
14cf7e42 | 5173 | s.op->newline(); |
20c6c071 | 5174 | } |
ec4373ff | 5175 | |
20c6c071 | 5176 | |
dc38c0ae | 5177 | void |
b20febf3 FCE |
5178 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
5179 | { | |
5180 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5181 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5182 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
a049e342 | 5183 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 5184 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
26e63673 | 5185 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b20febf3 | 5186 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 5187 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 5188 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 5189 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 5190 | s.op->newline(-1) << "} else {"; |
f07c3b68 | 5191 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
c9bad430 | 5192 | s.op->newline(-1) << "}"; |
606fd9c8 | 5193 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
af234c40 | 5194 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; |
c87ae2c1 | 5195 | s.op->newline() << "if (sdp->entry_probe) {"; |
af234c40 JS |
5196 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; |
5197 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
5198 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
5199 | s.op->newline(-1) << "}"; | |
5200 | s.op->newline() << "#endif"; | |
e4cb375f MH |
5201 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
5202 | s.op->newline() << "#ifdef __ia64__"; | |
5203 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5204 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5205 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5206 | s.op->newline() << "if (rc == 0) {"; | |
5207 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5208 | s.op->newline() << "if (rc != 0)"; | |
5209 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5210 | s.op->newline(-2) << "}"; | |
5211 | s.op->newline() << "#else"; | |
606fd9c8 | 5212 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 5213 | s.op->newline() << "#endif"; |
b20febf3 | 5214 | s.op->newline(-1) << "} else {"; |
e4cb375f | 5215 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
5216 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
5217 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
5218 | s.op->newline() << "#ifdef __ia64__"; |
5219 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5220 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5221 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5222 | s.op->newline() << "if (rc == 0) {"; | |
5223 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5224 | s.op->newline() << "if (rc != 0)"; | |
5225 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5226 | s.op->newline(-2) << "}"; | |
5227 | s.op->newline() << "#else"; | |
606fd9c8 | 5228 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 5229 | s.op->newline() << "#endif"; |
b20febf3 | 5230 | s.op->newline(-1) << "}"; |
9063462a FCE |
5231 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
5232 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 5233 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 5234 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);"; |
b350f56b | 5235 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
9063462a FCE |
5236 | // XXX: shall we increment numskipped? |
5237 | s.op->newline(-1) << "}"; | |
5238 | ||
5239 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 5240 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 5241 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
5242 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
5243 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
5244 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
5245 | s.op->newline() << "#ifdef __ia64__"; |
5246 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
5247 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
5248 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
5249 | // not run for this early-abort case. | |
5250 | s.op->newline(-1) << "}"; | |
5251 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 5252 | s.op->newline(-1) << "}"; |
9063462a FCE |
5253 | #endif |
5254 | ||
b20febf3 FCE |
5255 | s.op->newline() << "else sdp->registered_p = 1;"; |
5256 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
5257 | } |
5258 | ||
5259 | ||
b4be7cbc FCE |
5260 | void |
5261 | dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s) | |
5262 | { | |
5263 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5264 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5265 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5266 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; | |
5267 | s.op->newline() << "int rc;"; | |
5268 | ||
5269 | // new module arrived? | |
5270 | s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {"; | |
5271 | s.op->newline(1) << "if (sdp->return_p) {"; | |
5272 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; | |
5273 | s.op->newline() << "if (sdp->maxactive_p) {"; | |
5274 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
5275 | s.op->newline(-1) << "} else {"; | |
5276 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; | |
5277 | s.op->newline(-1) << "}"; | |
5278 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; | |
5279 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; | |
5280 | s.op->newline() << "if (sdp->entry_probe) {"; | |
5281 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; | |
5282 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
5283 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
5284 | s.op->newline(-1) << "}"; | |
5285 | s.op->newline() << "#endif"; | |
5286 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
5287 | s.op->newline() << "#ifdef __ia64__"; | |
5288 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5289 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5290 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5291 | s.op->newline() << "if (rc == 0) {"; | |
5292 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5293 | s.op->newline() << "if (rc != 0)"; | |
5294 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5295 | s.op->newline(-2) << "}"; | |
5296 | s.op->newline() << "#else"; | |
5297 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
5298 | s.op->newline() << "#endif"; | |
5299 | s.op->newline(-1) << "} else {"; | |
5300 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
5301 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; | |
5302 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
5303 | s.op->newline() << "#ifdef __ia64__"; | |
5304 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5305 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5306 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5307 | s.op->newline() << "if (rc == 0) {"; | |
5308 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5309 | s.op->newline() << "if (rc != 0)"; | |
5310 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5311 | s.op->newline(-2) << "}"; | |
5312 | s.op->newline() << "#else"; | |
5313 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
5314 | s.op->newline() << "#endif"; | |
5315 | s.op->newline(-1) << "}"; | |
5316 | s.op->newline() << "if (rc == 0) sdp->registered_p = 1;"; | |
5317 | ||
5318 | // old module disappeared? | |
5319 | s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {"; | |
5320 | s.op->newline(1) << "if (sdp->return_p) {"; | |
5321 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
065d5567 | 5322 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5323 | s.op->newline() << "#ifdef STP_TIMING"; |
5324 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
5325 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; | |
5326 | s.op->newline(-1) << "#endif"; | |
065d5567 | 5327 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5328 | s.op->newline() << "#ifdef STP_TIMING"; |
5329 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
5330 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; | |
5331 | s.op->newline(-1) << "#endif"; | |
5332 | s.op->newline(-1) << "} else {"; | |
5333 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
065d5567 | 5334 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5335 | s.op->newline() << "#ifdef STP_TIMING"; |
5336 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
5337 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; | |
5338 | s.op->newline(-1) << "#endif"; | |
5339 | s.op->newline(-1) << "}"; | |
5340 | s.op->newline() << "#if defined(__ia64__)"; | |
5341 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5342 | s.op->newline() << "#endif"; | |
5343 | s.op->newline() << "sdp->registered_p = 0;"; | |
5344 | s.op->newline(-1) << "}"; | |
5345 | ||
5346 | s.op->newline(-1) << "}"; // for loop | |
5347 | } | |
5348 | ||
5349 | ||
5350 | ||
5351 | ||
46b84a80 | 5352 | void |
b20febf3 | 5353 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 5354 | { |
42cb22bd MH |
5355 | //Unregister kprobes by batch interfaces. |
5356 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5357 | s.op->newline() << "j = 0;"; | |
5358 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5359 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5360 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5361 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5362 | s.op->newline() << "if (!sdp->return_p)"; | |
5363 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
5364 | s.op->newline(-2) << "}"; | |
5365 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5366 | s.op->newline() << "j = 0;"; | |
5367 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5368 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5369 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5370 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5371 | s.op->newline() << "if (sdp->return_p)"; | |
5372 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
5373 | s.op->newline(-2) << "}"; | |
5374 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
5375 | s.op->newline() << "#ifdef __ia64__"; |
5376 | s.op->newline() << "j = 0;"; | |
5377 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5378 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5379 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5380 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5381 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
5382 | s.op->newline(-1) << "}"; | |
5383 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5384 | s.op->newline() << "#endif"; | |
42cb22bd MH |
5385 | s.op->newline() << "#endif"; |
5386 | ||
b20febf3 FCE |
5387 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
5388 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5389 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
5390 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
5391 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 5392 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5393 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 5394 | s.op->newline() << "#endif"; |
065d5567 | 5395 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
73209876 FCE |
5396 | s.op->newline() << "#ifdef STP_TIMING"; |
5397 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 5398 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
73209876 | 5399 | s.op->newline(-1) << "#endif"; |
065d5567 | 5400 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
73209876 FCE |
5401 | s.op->newline() << "#ifdef STP_TIMING"; |
5402 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 5403 | 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 | 5404 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 5405 | s.op->newline(-1) << "} else {"; |
42cb22bd | 5406 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5407 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 5408 | s.op->newline() << "#endif"; |
065d5567 | 5409 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
73209876 FCE |
5410 | s.op->newline() << "#ifdef STP_TIMING"; |
5411 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 5412 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
73209876 | 5413 | s.op->newline(-1) << "#endif"; |
b20febf3 | 5414 | s.op->newline(-1) << "}"; |
e4cb375f MH |
5415 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
5416 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5417 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
5418 | s.op->newline() << "sdp->registered_p = 0;"; |
5419 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
5420 | } |
5421 | ||
272c9036 WF |
5422 | static void sdt_v3_tokenize(const string& str, vector<string>& tokens) |
5423 | { | |
5424 | string::size_type pos; | |
5425 | string::size_type lastPos = str.find_first_not_of(" ", 0); | |
5426 | string::size_type nextAt = str.find("@", lastPos); | |
e9a90eee JS |
5427 | |
5428 | if (nextAt == string::npos) | |
5429 | { | |
5430 | // PR13934: Assembly probes are not forced to use the N@OP form. | |
5431 | // In this case, N is inferred to be the native word size. Since we | |
5432 | // don't have a nice delimiter, just split it on spaces. SDT-asm authors | |
5433 | // then must not put any spaces in arguments, to avoid ambiguity. | |
5434 | tokenize(str, tokens, " "); | |
5435 | return; | |
5436 | } | |
5437 | ||
272c9036 WF |
5438 | while (lastPos != string::npos) |
5439 | { | |
5440 | pos = nextAt + 1; | |
5441 | nextAt = str.find("@", pos); | |
5442 | if (nextAt == string::npos) | |
5443 | pos = string::npos; | |
5444 | else | |
5445 | pos = str.rfind(" ", nextAt); | |
5446 | ||
5447 | tokens.push_back(str.substr(lastPos, pos - lastPos)); | |
5448 | lastPos = str.find_first_not_of(" ", pos); | |
5449 | } | |
5450 | } | |
8aabf152 | 5451 | |
8aabf152 | 5452 | |
aff5d390 | 5453 | struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor |
7a05f484 | 5454 | { |
ae1418f0 | 5455 | enum regwidths {QI, QIh, HI, SI, DI}; |
f83336a5 FCE |
5456 | sdt_uprobe_var_expanding_visitor(systemtap_session& s, |
5457 | int elf_machine, | |
5458 | const string & process_name, | |
a794dbeb | 5459 | const string & provider_name, |
aff5d390 | 5460 | const string & probe_name, |
71e5e13d | 5461 | stap_sdt_probe_type probe_type, |
aff5d390 | 5462 | const string & arg_string, |
8aabf152 | 5463 | int ac): |
332ba7e7 | 5464 | session (s), elf_machine (elf_machine), process_name (process_name), |
71e5e13d SC |
5465 | provider_name (provider_name), probe_name (probe_name), |
5466 | probe_type (probe_type), arg_count ((unsigned) ac) | |
a8ec7719 | 5467 | { |
f83336a5 FCE |
5468 | /* Register name mapping table depends on the elf machine of this particular |
5469 | probe target process/file, not upon the host. So we can't just | |
5470 | #ifdef _i686_ etc. */ | |
ae1418f0 FCE |
5471 | |
5472 | #define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width) | |
f83336a5 | 5473 | if (elf_machine == EM_X86_64) { |
46a94997 SC |
5474 | DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); |
5475 | DRI ("%al", 0, QI); DRI ("%ah", 0, QIh); | |
5476 | DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI); | |
5477 | DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh); | |
5478 | DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI); | |
5479 | DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh); | |
5480 | DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); | |
5481 | DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh); | |
5482 | DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI); | |
5483 | DRI ("%sil", 4, QI); | |
5484 | DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI); | |
5485 | DRI ("%dil", 5, QI); | |
5486 | DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI); | |
2c7281e7 | 5487 | DRI ("%bpl", 6, QI); |
46a94997 | 5488 | DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI); |
2c7281e7 | 5489 | DRI ("%spl", 7, QI); |
46a94997 SC |
5490 | DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI); |
5491 | DRI ("%r8b", 8, QI); | |
5492 | DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI); | |
5493 | DRI ("%r9b", 9, QI); | |
5494 | DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI); | |
5495 | DRI ("%r10b", 10, QI); | |
5496 | DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI); | |
5497 | DRI ("%r11b", 11, QI); | |
5498 | DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI); | |
5499 | DRI ("%r12b", 12, QI); | |
5500 | DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI); | |
5501 | DRI ("%r13b", 13, QI); | |
5502 | DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI); | |
5503 | DRI ("%r14b", 14, QI); | |
5504 | DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI); | |
5505 | DRI ("%r15b", 15, QI); | |
f83336a5 | 5506 | } else if (elf_machine == EM_386) { |
46a94997 SC |
5507 | DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI); |
5508 | DRI ("%ah", 0, QIh); | |
5509 | DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI); | |
5510 | DRI ("%ch", 1, QIh); | |
5511 | DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI); | |
5512 | DRI ("%dh", 2, QIh); | |
5513 | DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI); | |
5514 | DRI ("%bh", 3, QIh); | |
5515 | DRI ("%esp", 4, SI); DRI ("%sp", 4, HI); | |
5516 | DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI); | |
5517 | DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI); | |
5518 | DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI); | |
0491c523 | 5519 | } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) { |
46a94997 SC |
5520 | DRI ("%r0", 0, DI); |
5521 | DRI ("%r1", 1, DI); | |
5522 | DRI ("%r2", 2, DI); | |
5523 | DRI ("%r3", 3, DI); | |
5524 | DRI ("%r4", 4, DI); | |
5525 | DRI ("%r5", 5, DI); | |
5526 | DRI ("%r6", 6, DI); | |
5527 | DRI ("%r7", 7, DI); | |
5528 | DRI ("%r8", 8, DI); | |
5529 | DRI ("%r9", 9, DI); | |
5530 | DRI ("%r10", 10, DI); | |
5531 | DRI ("%r11", 11, DI); | |
5532 | DRI ("%r12", 12, DI); | |
5533 | DRI ("%r13", 13, DI); | |
5534 | DRI ("%r14", 14, DI); | |
5535 | DRI ("%r15", 15, DI); | |
5536 | DRI ("%r16", 16, DI); | |
5537 | DRI ("%r17", 17, DI); | |
5538 | DRI ("%r18", 18, DI); | |
5539 | DRI ("%r19", 19, DI); | |
5540 | DRI ("%r20", 20, DI); | |
5541 | DRI ("%r21", 21, DI); | |
5542 | DRI ("%r22", 22, DI); | |
5543 | DRI ("%r23", 23, DI); | |
5544 | DRI ("%r24", 24, DI); | |
5545 | DRI ("%r25", 25, DI); | |
5546 | DRI ("%r26", 26, DI); | |
5547 | DRI ("%r27", 27, DI); | |
5548 | DRI ("%r28", 28, DI); | |
5549 | DRI ("%r29", 29, DI); | |
5550 | DRI ("%r30", 30, DI); | |
5551 | DRI ("%r31", 31, DI); | |
8aabf152 | 5552 | // PR11821: unadorned register "names" without -mregnames |
46a94997 SC |
5553 | DRI ("0", 0, DI); |
5554 | DRI ("1", 1, DI); | |
5555 | DRI ("2", 2, DI); | |
5556 | DRI ("3", 3, DI); | |
5557 | DRI ("4", 4, DI); | |
5558 | DRI ("5", 5, DI); | |
5559 | DRI ("6", 6, DI); | |
5560 | DRI ("7", 7, DI); | |
5561 | DRI ("8", 8, DI); | |
5562 | DRI ("9", 9, DI); | |
5563 | DRI ("10", 10, DI); | |
5564 | DRI ("11", 11, DI); | |
5565 | DRI ("12", 12, DI); | |
5566 | DRI ("13", 13, DI); | |
5567 | DRI ("14", 14, DI); | |
5568 | DRI ("15", 15, DI); | |
5569 | DRI ("16", 16, DI); | |
5570 | DRI ("17", 17, DI); | |
5571 | DRI ("18", 18, DI); | |
5572 | DRI ("19", 19, DI); | |
5573 | DRI ("20", 20, DI); | |
5574 | DRI ("21", 21, DI); | |
5575 | DRI ("22", 22, DI); | |
5576 | DRI ("23", 23, DI); | |
5577 | DRI ("24", 24, DI); | |
5578 | DRI ("25", 25, DI); | |
5579 | DRI ("26", 26, DI); | |
5580 | DRI ("27", 27, DI); | |
5581 | DRI ("28", 28, DI); | |
5582 | DRI ("29", 29, DI); | |
5583 | DRI ("30", 30, DI); | |
5584 | DRI ("31", 31, DI); | |
14900130 | 5585 | } else if (elf_machine == EM_S390) { |
46a94997 SC |
5586 | DRI ("%r0", 0, DI); |
5587 | DRI ("%r1", 1, DI); | |
5588 | DRI ("%r2", 2, DI); | |
5589 | DRI ("%r3", 3, DI); | |
5590 | DRI ("%r4", 4, DI); | |
5591 | DRI ("%r5", 5, DI); | |
5592 | DRI ("%r6", 6, DI); | |
5593 | DRI ("%r7", 7, DI); | |
5594 | DRI ("%r8", 8, DI); | |
5595 | DRI ("%r9", 9, DI); | |
5596 | DRI ("%r10", 10, DI); | |
5597 | DRI ("%r11", 11, DI); | |
5598 | DRI ("%r12", 12, DI); | |
5599 | DRI ("%r13", 13, DI); | |
5600 | DRI ("%r14", 14, DI); | |
5601 | DRI ("%r15", 15, DI); | |
272c9036 WF |
5602 | } else if (elf_machine == EM_ARM) { |
5603 | DRI ("r0", 0, SI); | |
5604 | DRI ("r1", 1, SI); | |
5605 | DRI ("r2", 2, SI); | |
5606 | DRI ("r3", 3, SI); | |
5607 | DRI ("r4", 4, SI); | |
5608 | DRI ("r5", 5, SI); | |
5609 | DRI ("r6", 6, SI); | |
5610 | DRI ("r7", 7, SI); | |
5611 | DRI ("r8", 8, SI); | |
5612 | DRI ("r9", 9, SI); | |
669a2feb | 5613 | DRI ("r10", 10, SI); DRI ("sl", 10, SI); |
272c9036 WF |
5614 | DRI ("fp", 11, SI); |
5615 | DRI ("ip", 12, SI); | |
5616 | DRI ("sp", 13, SI); | |
5617 | DRI ("lr", 14, SI); | |
5618 | DRI ("pc", 15, SI); | |
86aae990 WC |
5619 | } else if (elf_machine == EM_AARCH64) { |
5620 | DRI ("x0", 0, DI); DRI ("w0", 0, SI); | |
5621 | DRI ("x1", 1, DI); DRI ("w1", 1, SI); | |
5622 | DRI ("x2", 2, DI); DRI ("w2", 2, SI); | |
5623 | DRI ("x3", 3, DI); DRI ("w3", 3, SI); | |
5624 | DRI ("x4", 4, DI); DRI ("w4", 4, SI); | |
5625 | DRI ("x5", 5, DI); DRI ("w5", 5, SI); | |
5626 | DRI ("x6", 6, DI); DRI ("w6", 6, SI); | |
5627 | DRI ("x7", 7, DI); DRI ("w7", 7, SI); | |
5628 | DRI ("x8", 8, DI); DRI ("w8", 8, SI); | |
5629 | DRI ("x9", 9, DI); DRI ("w9", 9, SI); | |
5630 | DRI ("x10", 10, DI); DRI ("w10", 10, SI); | |
5631 | DRI ("x11", 11, DI); DRI ("w11", 11, SI); | |
5632 | DRI ("x12", 12, DI); DRI ("w12", 12, SI); | |
5633 | DRI ("x13", 13, DI); DRI ("w13", 13, SI); | |
5634 | DRI ("x14", 14, DI); DRI ("w14", 14, SI); | |
5635 | DRI ("x15", 15, DI); DRI ("w15", 15, SI); | |
5636 | DRI ("x16", 16, DI); DRI ("w16", 16, SI); | |
5637 | DRI ("x17", 17, DI); DRI ("w17", 17, SI); | |
5638 | DRI ("x18", 18, DI); DRI ("w18", 18, SI); | |
5639 | DRI ("x19", 19, DI); DRI ("w19", 19, SI); | |
5640 | DRI ("x20", 20, DI); DRI ("w20", 20, SI); | |
5641 | DRI ("x21", 21, DI); DRI ("w21", 21, SI); | |
5642 | DRI ("x22", 22, DI); DRI ("w22", 22, SI); | |
5643 | DRI ("x23", 23, DI); DRI ("w23", 23, SI); | |
5644 | DRI ("x24", 24, DI); DRI ("w24", 24, SI); | |
5645 | DRI ("x25", 25, DI); DRI ("w25", 25, SI); | |
5646 | DRI ("x26", 26, DI); DRI ("w26", 26, SI); | |
5647 | DRI ("x27", 27, DI); DRI ("w27", 27, SI); | |
5648 | DRI ("x28", 28, DI); DRI ("w28", 28, SI); | |
5649 | DRI ("x29", 29, DI); DRI ("w29", 29, SI); | |
5650 | DRI ("x30", 30, DI); DRI ("w30", 30, SI); | |
5651 | DRI ("sp", 31, DI); | |
14900130 | 5652 | } else if (arg_count) { |
8aabf152 | 5653 | /* permit this case; just fall back to dwarf */ |
f83336a5 | 5654 | } |
ae1418f0 | 5655 | #undef DRI |
f83336a5 | 5656 | |
ebbd2b45 | 5657 | need_debug_info = false; |
88e39987 | 5658 | if (probe_type == uprobe3_type) |
272c9036 WF |
5659 | { |
5660 | sdt_v3_tokenize(arg_string, arg_tokens); | |
5661 | assert(arg_count <= 12); | |
5662 | } | |
88e39987 | 5663 | else |
272c9036 WF |
5664 | { |
5665 | tokenize(arg_string, arg_tokens, " "); | |
5666 | assert(arg_count <= 10); | |
5667 | } | |
a8ec7719 | 5668 | } |
8aabf152 | 5669 | |
f83336a5 | 5670 | systemtap_session& session; |
332ba7e7 | 5671 | int elf_machine; |
aff5d390 | 5672 | const string & process_name; |
a794dbeb | 5673 | const string & provider_name; |
aff5d390 | 5674 | const string & probe_name; |
71e5e13d | 5675 | stap_sdt_probe_type probe_type; |
8aabf152 | 5676 | unsigned arg_count; |
aff5d390 | 5677 | vector<string> arg_tokens; |
46a94997 | 5678 | map<string, pair<unsigned,int> > dwarf_regs; |
ebbd2b45 | 5679 | bool need_debug_info; |
aff5d390 SC |
5680 | |
5681 | void visit_target_symbol (target_symbol* e); | |
6ef331c8 SC |
5682 | void visit_target_symbol_arg (target_symbol* e); |
5683 | void visit_target_symbol_context (target_symbol* e); | |
bd1fcbad | 5684 | void visit_atvar_op (atvar_op* e); |
40a0c64e | 5685 | void visit_cast_op (cast_op* e); |
aff5d390 SC |
5686 | }; |
5687 | ||
7a05f484 SC |
5688 | |
5689 | void | |
6ef331c8 | 5690 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
7a05f484 | 5691 | { |
6ef331c8 | 5692 | if (e->addressof) |
dc09353a | 5693 | throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok); |
6ef331c8 SC |
5694 | |
5695 | if (e->name == "$$name") | |
7a05f484 | 5696 | { |
6ef331c8 SC |
5697 | literal_string *myname = new literal_string (probe_name); |
5698 | myname->tok = e->tok; | |
5699 | provide(myname); | |
5700 | return; | |
5701 | } | |
7a05f484 | 5702 | |
6ef331c8 SC |
5703 | else if (e->name == "$$provider") |
5704 | { | |
5705 | literal_string *myname = new literal_string (provider_name); | |
5706 | myname->tok = e->tok; | |
5707 | provide(myname); | |
5708 | return; | |
5709 | } | |
a794dbeb | 5710 | |
6ef331c8 SC |
5711 | else if (e->name == "$$vars" || e->name == "$$parms") |
5712 | { | |
5713 | e->assert_no_components("sdt", true); | |
6ef331c8 SC |
5714 | |
5715 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
5716 | // NB: we synthesize a new token here rather than reusing | |
5717 | // e->tok, because print_format::print likes to use | |
5718 | // its tok->content. | |
5719 | token* pf_tok = new token(*e->tok); | |
5720 | pf_tok->content = "sprintf"; | |
63ea4244 | 5721 | |
6ef331c8 SC |
5722 | print_format* pf = print_format::create(pf_tok); |
5723 | ||
5724 | for (unsigned i = 1; i <= arg_count; ++i) | |
5725 | { | |
5726 | if (i > 1) | |
5727 | pf->raw_components += " "; | |
5728 | target_symbol *tsym = new target_symbol; | |
5729 | tsym->tok = e->tok; | |
5730 | tsym->name = "$arg" + lex_cast(i); | |
5731 | pf->raw_components += tsym->name; | |
5732 | tsym->components = e->components; | |
5733 | ||
8c2f50c0 | 5734 | expression *texp = require<expression> (tsym); |
6ef331c8 SC |
5735 | if (!e->components.empty() && |
5736 | e->components[0].type == target_symbol::comp_pretty_print) | |
5737 | pf->raw_components += "=%s"; | |
5738 | else | |
5739 | pf->raw_components += "=%#x"; | |
5740 | pf->args.push_back(texp); | |
5741 | } | |
5742 | ||
5743 | pf->components = print_format::string_to_components(pf->raw_components); | |
5744 | provide (pf); | |
5745 | } | |
5746 | else | |
5747 | assert(0); // shouldn't get here | |
5748 | } | |
5749 | ||
5750 | ||
5751 | void | |
5752 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e) | |
5753 | { | |
5754 | try | |
5755 | { | |
8aabf152 | 5756 | unsigned argno = 0; // the N in $argN |
c69a87e0 | 5757 | try |
aff5d390 | 5758 | { |
5ecaa5a7 | 5759 | if (startswith(e->name, "$arg")) |
8aabf152 | 5760 | argno = lex_cast<unsigned>(e->name.substr(4)); |
aff5d390 | 5761 | } |
c69a87e0 | 5762 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF |
aff5d390 | 5763 | { |
8aabf152 | 5764 | argno = 0; |
aff5d390 | 5765 | } |
5ecaa5a7 | 5766 | |
8aabf152 FCE |
5767 | if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count |
5768 | argno < 1 || argno > arg_count) // a $argN with out-of-range N | |
aff5d390 | 5769 | { |
8aabf152 FCE |
5770 | // NB: Either |
5771 | // 1) uprobe1_type $argN or $FOO (we don't know the arg_count) | |
5772 | // 2) uprobe2_type $FOO (no probe args) | |
5773 | // both of which get resolved later. | |
73d53dd2 | 5774 | // Throw it now, and it might be resolved by DWARF later. |
8aabf152 | 5775 | need_debug_info = true; |
dc09353a | 5776 | throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok); |
aff5d390 | 5777 | } |
277c21bc | 5778 | |
8aabf152 FCE |
5779 | assert (arg_tokens.size() >= argno); |
5780 | string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0] | |
c57ea854 | 5781 | |
8aabf152 FCE |
5782 | // Now we try to parse this thing, which is an assembler operand |
5783 | // expression. If we can't, we warn, back down to need_debug_info | |
b874bd52 | 5784 | // and hope for the best. Here is the syntax for a few architectures. |
9859b766 | 5785 | // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i. |
8095a157 FCE |
5786 | // |
5787 | // literal reg reg reg + base+index*size+offset | |
b874bd52 | 5788 | // indirect offset |
8095a157 | 5789 | // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) |
b874bd52 SC |
5790 | // power iN R (R) N(R) |
5791 | // ia64 N rR [r16] | |
5792 | // s390 N %rR 0(rR) N(r15) | |
5793 | // arm #N rR [rR] [rR, #N] | |
5794 | ||
8aabf152 FCE |
5795 | expression* argexpr = 0; // filled in in case of successful parse |
5796 | ||
5797 | string percent_regnames; | |
5798 | string regnames; | |
5799 | vector<string> matches; | |
71e5e13d | 5800 | long precision; |
8aabf152 FCE |
5801 | int rc; |
5802 | ||
40fe32e0 SC |
5803 | // Parse the leading length |
5804 | ||
5805 | if (asmarg.find('@') != string::npos) | |
5806 | { | |
5807 | precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@'))); | |
5808 | asmarg = asmarg.substr(asmarg.find('@')+1); | |
5809 | } | |
71e5e13d SC |
5810 | else |
5811 | { | |
5812 | // V1/V2 do not have precision field so default to signed long | |
5813 | // V3 asm does not have precision field so default to unsigned long | |
5814 | if (probe_type == uprobe3_type) | |
5815 | precision = sizeof(long); // this is an asm probe | |
5816 | else | |
5817 | precision = -sizeof(long); | |
5818 | } | |
40fe32e0 | 5819 | |
8aabf152 FCE |
5820 | // test for a numeric literal. |
5821 | // Only accept (signed) decimals throughout. XXX | |
5822 | ||
5823 | // PR11821. NB: on powerpc, literals are not prefixed with $, | |
5824 | // so this regex does not match. But that's OK, since without | |
5825 | // -mregnames, we can't tell them apart from register numbers | |
5826 | // anyway. With -mregnames, we could, if gcc somehow | |
5827 | // communicated to us the presence of that option, but alas it | |
5828 | // doesn't. http://gcc.gnu.org/PR44995. | |
272c9036 | 5829 | rc = regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches); |
8aabf152 FCE |
5830 | if (! rc) |
5831 | { | |
75a371ce JS |
5832 | string sn = matches[0].substr(1); |
5833 | int64_t n; | |
5834 | try | |
5835 | { | |
5836 | // We have to pay attention to the size & sign, as gcc sometimes | |
5837 | // propagates constants that don't quite match, like a negative | |
5838 | // value to fill an unsigned type. | |
5839 | switch (precision) | |
5840 | { | |
5841 | case -1: n = lex_cast< int8_t>(sn); break; | |
5842 | case 1: n = lex_cast< uint8_t>(sn); break; | |
5843 | case -2: n = lex_cast< int16_t>(sn); break; | |
5844 | case 2: n = lex_cast<uint16_t>(sn); break; | |
5845 | case -4: n = lex_cast< int32_t>(sn); break; | |
5846 | case 4: n = lex_cast<uint32_t>(sn); break; | |
5847 | default: | |
5848 | case -8: n = lex_cast< int64_t>(sn); break; | |
5849 | case 8: n = lex_cast<uint64_t>(sn); break; | |
5850 | } | |
5851 | } | |
5852 | catch (std::runtime_error&) | |
5853 | { | |
5854 | goto not_matched; | |
5855 | } | |
5856 | literal_number* ln = new literal_number(n); | |
8aabf152 FCE |
5857 | ln->tok = e->tok; |
5858 | argexpr = ln; | |
5859 | goto matched; | |
5860 | } | |
5861 | ||
14900130 SC |
5862 | if (dwarf_regs.empty()) |
5863 | goto not_matched; | |
d5b83cee | 5864 | |
8aabf152 FCE |
5865 | // Build regex pieces out of the known dwarf_regs. We keep two separate |
5866 | // lists: ones with the % prefix (and thus unambigiuous even despite PR11821), | |
5867 | // and ones with no prefix (and thus only usable in unambiguous contexts). | |
46a94997 | 5868 | for (map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++) |
8aabf152 FCE |
5869 | { |
5870 | string regname = ri->first; | |
5871 | assert (regname != ""); | |
5872 | regnames += string("|")+regname; | |
5873 | if (regname[0]=='%') | |
5874 | percent_regnames += string("|")+regname; | |
5875 | } | |
5876 | // clip off leading | | |
5877 | regnames = regnames.substr(1); | |
272c9036 WF |
5878 | if (percent_regnames != "") |
5879 | percent_regnames = percent_regnames.substr(1); | |
8aabf152 FCE |
5880 | |
5881 | // test for REGISTER | |
5882 | // NB: Because PR11821, we must use percent_regnames here. | |
272c9036 | 5883 | if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM) |
9109f487 SC |
5884 | rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches); |
5885 | else | |
332ba7e7 | 5886 | rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches); |
8aabf152 FCE |
5887 | if (! rc) |
5888 | { | |
5889 | string regname = matches[1]; | |
46a94997 SC |
5890 | map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname); |
5891 | if (ri != dwarf_regs.end()) // known register | |
8aabf152 FCE |
5892 | { |
5893 | embedded_expr *get_arg1 = new embedded_expr; | |
19c22e1f | 5894 | string width_adjust; |
46a94997 | 5895 | switch (ri->second.second) |
19c22e1f | 5896 | { |
892ec39a SC |
5897 | case QI: width_adjust = ") & 0xff)"; break; |
5898 | case QIh: width_adjust = ">>8) & 0xff)"; break; | |
46a94997 | 5899 | case HI: |
71e5e13d | 5900 | // preserve 16 bit register signness |
892ec39a SC |
5901 | width_adjust = ") & 0xffff)"; |
5902 | if (precision < 0) | |
55b377f4 | 5903 | width_adjust += " << 48 >> 48"; |
ac8a78aa SC |
5904 | break; |
5905 | case SI: | |
5906 | // preserve 32 bit register signness | |
892ec39a SC |
5907 | width_adjust = ") & 0xffffffff)"; |
5908 | if (precision < 0) | |
55b377f4 | 5909 | width_adjust += " << 32 >> 32"; |
19c22e1f | 5910 | break; |
892ec39a | 5911 | default: width_adjust = "))"; |
19c22e1f | 5912 | } |
55b377f4 SC |
5913 | string type = ""; |
5914 | if (probe_type == uprobe3_type) | |
5915 | type = (precision < 0 | |
5916 | ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)"; | |
5917 | type = type + "(("; | |
8aabf152 FCE |
5918 | get_arg1->tok = e->tok; |
5919 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
892ec39a | 5920 | + string(" ((int64_t)") + type |
8aabf152 FCE |
5921 | + (is_user_module (process_name) |
5922 | ? string("u_fetch_register(") | |
5923 | : string("k_fetch_register(")) | |
46a94997 | 5924 | + lex_cast(dwarf_regs[regname].first) + string("))") |
19c22e1f | 5925 | + width_adjust; |
8aabf152 FCE |
5926 | argexpr = get_arg1; |
5927 | goto matched; | |
5928 | } | |
5929 | // invalid register name, fall through | |
5930 | } | |
40fe32e0 | 5931 | |
272c9036 | 5932 | int reg, offset1; |
e5b7b83f | 5933 | // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N |
40fe32e0 | 5934 | // NB: Despite PR11821, we can use regnames here, since the parentheses |
e5b7b83f | 5935 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) |
272c9036 WF |
5936 | // On ARM test for [REGISTER, OFFSET] |
5937 | if (elf_machine == EM_ARM) | |
5938 | { | |
669a2feb | 5939 | rc = regexp_match (asmarg, string("^\\[(")+regnames+string(")(, #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$"), matches); |
272c9036 | 5940 | reg = 1; |
669a2feb | 5941 | offset1 = 3; |
272c9036 WF |
5942 | } |
5943 | else | |
5944 | { | |
5945 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string(")[)]$"), matches); | |
5946 | reg = 4; | |
5947 | offset1 = 1; | |
5948 | } | |
8aabf152 FCE |
5949 | if (! rc) |
5950 | { | |
e5b7b83f | 5951 | string regname; |
8aabf152 | 5952 | int64_t disp = 0; |
272c9036 WF |
5953 | if (matches[reg].length()) |
5954 | regname = matches[reg]; | |
8095a157 FCE |
5955 | if (dwarf_regs.find (regname) == dwarf_regs.end()) |
5956 | goto not_matched; | |
5957 | ||
272c9036 | 5958 | for (int i=offset1; i <= (offset1 + 2); i++) |
e5b7b83f SC |
5959 | if (matches[i].length()) |
5960 | try | |
5961 | { | |
5962 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
5963 | } | |
8aabf152 FCE |
5964 | catch (const runtime_error& f) // unparseable offset |
5965 | { | |
5966 | goto not_matched; // can't just 'break' out of | |
5967 | // this case or use a sentinel | |
5968 | // value, unfortunately | |
5969 | } | |
5970 | ||
8aabf152 | 5971 | // synthesize user_long(%{fetch_register(R)%} + D) |
8aabf152 FCE |
5972 | embedded_expr *get_arg1 = new embedded_expr; |
5973 | get_arg1->tok = e->tok; | |
5974 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
5975 | + (is_user_module (process_name) | |
5976 | ? string("u_fetch_register(") | |
5977 | : string("k_fetch_register(")) | |
46a94997 | 5978 | + lex_cast(dwarf_regs[regname].first) + string(")"); |
8aabf152 | 5979 | // XXX: may we ever need to cast that to a narrower type? |
40fe32e0 | 5980 | |
8aabf152 FCE |
5981 | literal_number* inc = new literal_number(disp); |
5982 | inc->tok = e->tok; | |
40fe32e0 | 5983 | |
8aabf152 FCE |
5984 | binary_expression *be = new binary_expression; |
5985 | be->tok = e->tok; | |
5986 | be->left = get_arg1; | |
5987 | be->op = "+"; | |
5988 | be->right = inc; | |
40fe32e0 | 5989 | |
8aabf152 | 5990 | functioncall *fc = new functioncall; |
40fe32e0 SC |
5991 | switch (precision) |
5992 | { | |
7f6ce9ab SC |
5993 | case 1: case -1: |
5994 | fc->function = "user_int8"; break; | |
5995 | case 2: | |
5996 | fc->function = "user_uint16"; break; | |
5997 | case -2: | |
5998 | fc->function = "user_int16"; break; | |
5999 | case 4: | |
6000 | fc->function = "user_uint32"; break; | |
6001 | case -4: | |
6002 | fc->function = "user_int32"; break; | |
6003 | case 8: case -8: | |
6004 | fc->function = "user_int64"; break; | |
40fe32e0 SC |
6005 | default: fc->function = "user_long"; |
6006 | } | |
8aabf152 FCE |
6007 | fc->tok = e->tok; |
6008 | fc->args.push_back(be); | |
366af4e7 | 6009 | |
8aabf152 FCE |
6010 | argexpr = fc; |
6011 | goto matched; | |
6012 | } | |
8095a157 FCE |
6013 | |
6014 | // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N | |
6015 | // NB: Despite PR11821, we can use regnames here, since the parentheses | |
6016 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) | |
0f7b51d6 | 6017 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string("),(")+regnames+string(")(,[1248])?[)]$"), matches); |
8095a157 FCE |
6018 | if (! rc) |
6019 | { | |
6020 | string baseregname; | |
6021 | string indexregname; | |
6022 | int64_t disp = 0; | |
6023 | short scale = 1; | |
6024 | ||
6025 | if (matches[6].length()) | |
6026 | try | |
6027 | { | |
6028 | scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma! | |
6029 | // We could verify that scale is one of 1,2,4,8, | |
6030 | // but it doesn't really matter. An erroneous | |
6031 | // address merely results in run-time errors. | |
8aabf152 | 6032 | } |
8095a157 FCE |
6033 | catch (const runtime_error &f) // unparseable scale |
6034 | { | |
6035 | goto not_matched; | |
6036 | } | |
6037 | ||
6038 | if (matches[4].length()) | |
6039 | baseregname = matches[4]; | |
6040 | if (dwarf_regs.find (baseregname) == dwarf_regs.end()) | |
6041 | goto not_matched; | |
6042 | ||
6043 | if (matches[5].length()) | |
6044 | indexregname = matches[5]; | |
6045 | if (dwarf_regs.find (indexregname) == dwarf_regs.end()) | |
6046 | goto not_matched; | |
6047 | ||
f7719b3d | 6048 | for (int i = 1; i <= 3; i++) // up to three OFFSET terms |
8095a157 FCE |
6049 | if (matches[i].length()) |
6050 | try | |
6051 | { | |
6052 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
6053 | } | |
6054 | catch (const runtime_error& f) // unparseable offset | |
6055 | { | |
6056 | goto not_matched; // can't just 'break' out of | |
6057 | // this case or use a sentinel | |
6058 | // value, unfortunately | |
6059 | } | |
6060 | ||
6061 | // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D) | |
6062 | ||
6063 | embedded_expr *get_arg1 = new embedded_expr; | |
6064 | string regfn = is_user_module (process_name) | |
6065 | ? string("u_fetch_register") | |
6066 | : string("k_fetch_register"); // NB: in practice sdt.h probes are for userspace only | |
6067 | ||
6068 | get_arg1->tok = e->tok; | |
6069 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
6070 | + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")") | |
6071 | + string("+(") | |
6072 | + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")") | |
6073 | + string("*") | |
6074 | + lex_cast(scale) | |
6075 | + string(")"); | |
6076 | ||
6077 | // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too | |
6078 | literal_number* inc = new literal_number(disp); | |
6079 | inc->tok = e->tok; | |
6080 | ||
6081 | binary_expression *be = new binary_expression; | |
6082 | be->tok = e->tok; | |
6083 | be->left = get_arg1; | |
6084 | be->op = "+"; | |
6085 | be->right = inc; | |
6086 | ||
6087 | functioncall *fc = new functioncall; | |
6088 | switch (precision) | |
6089 | { | |
6090 | case 1: case -1: | |
6091 | fc->function = "user_int8"; break; | |
6092 | case 2: | |
6093 | fc->function = "user_uint16"; break; | |
6094 | case -2: | |
6095 | fc->function = "user_int16"; break; | |
6096 | case 4: | |
6097 | fc->function = "user_uint32"; break; | |
6098 | case -4: | |
6099 | fc->function = "user_int32"; break; | |
6100 | case 8: case -8: | |
6101 | fc->function = "user_int64"; break; | |
6102 | default: fc->function = "user_long"; | |
6103 | } | |
6104 | fc->tok = e->tok; | |
6105 | fc->args.push_back(be); | |
6106 | ||
6107 | argexpr = fc; | |
6108 | goto matched; | |
8aabf152 FCE |
6109 | } |
6110 | ||
8aabf152 FCE |
6111 | |
6112 | not_matched: | |
6113 | // The asmarg operand was not recognized. Back down to dwarf. | |
6114 | if (! session.suppress_warnings) | |
84fef8ee FCE |
6115 | { |
6116 | if (probe_type == UPROBE3_TYPE) | |
6c9e1946 | 6117 | session.print_warning (_F("Can't parse SDT_V3 operand '%s' [man error::sdt]", asmarg.c_str()), e->tok); |
84fef8ee | 6118 | else // must be *PROBE2; others don't get asm operands |
6c9e1946 | 6119 | session.print_warning (_F("Downgrading SDT_V2 probe argument to dwarf, can't parse '%s' [man error::sdt]", |
84fef8ee FCE |
6120 | asmarg.c_str()), e->tok); |
6121 | } | |
8aabf152 FCE |
6122 | assert (argexpr == 0); |
6123 | need_debug_info = true; | |
dc09353a | 6124 | throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo [man error::sdt]"), e->tok); |
366af4e7 | 6125 | |
8aabf152 FCE |
6126 | matched: |
6127 | assert (argexpr != 0); | |
366af4e7 RM |
6128 | |
6129 | if (session.verbose > 2) | |
1e41115c | 6130 | //TRANSLATORS: We're mapping the operand to a new expression*. |
b530b5b3 | 6131 | clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl; |
366af4e7 | 6132 | |
aff5d390 | 6133 | if (e->components.empty()) // We have a scalar |
8aabf152 FCE |
6134 | { |
6135 | if (e->addressof) | |
dc09353a | 6136 | throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok); |
8aabf152 FCE |
6137 | provide (argexpr); |
6138 | return; | |
6139 | } | |
6140 | else // $var->foo | |
6141 | { | |
6142 | cast_op *cast = new cast_op; | |
6143 | cast->name = "@cast"; | |
6144 | cast->tok = e->tok; | |
6145 | cast->operand = argexpr; | |
6146 | cast->components = e->components; | |
6147 | cast->type_name = probe_name + "_arg" + lex_cast(argno); | |
6148 | cast->module = process_name; | |
6149 | cast->visit(this); | |
6150 | return; | |
6151 | } | |
366af4e7 | 6152 | |
8aabf152 | 6153 | /* NOTREACHED */ |
aff5d390 SC |
6154 | } |
6155 | catch (const semantic_error &er) | |
6156 | { | |
6157 | e->chain (er); | |
6158 | provide (e); | |
6159 | } | |
6160 | } | |
6161 | ||
6162 | ||
6ef331c8 SC |
6163 | void |
6164 | sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
6165 | { | |
6166 | try | |
6167 | { | |
49131a6d | 6168 | assert(e->name.size() > 0 |
bd1fcbad | 6169 | && (e->name[0] == '$' || e->name == "@var")); |
6ef331c8 SC |
6170 | |
6171 | if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars") | |
6172 | visit_target_symbol_context (e); | |
6173 | else | |
6174 | visit_target_symbol_arg (e); | |
6175 | } | |
6176 | catch (const semantic_error &er) | |
6177 | { | |
6178 | e->chain (er); | |
6179 | provide (e); | |
6180 | } | |
6181 | } | |
6182 | ||
6183 | ||
bd1fcbad YZ |
6184 | void |
6185 | sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e) | |
6186 | { | |
6187 | need_debug_info = true; | |
6188 | ||
6189 | // Fill in our current module context if needed | |
6190 | if (e->module.empty()) | |
6191 | e->module = process_name; | |
6192 | ||
6193 | var_expanding_visitor::visit_atvar_op(e); | |
6194 | } | |
6195 | ||
6196 | ||
40a0c64e JS |
6197 | void |
6198 | sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e) | |
6199 | { | |
6200 | // Fill in our current module context if needed | |
6201 | if (e->module.empty()) | |
6202 | e->module = process_name; | |
6203 | ||
6204 | var_expanding_visitor::visit_cast_op(e); | |
6205 | } | |
6206 | ||
6207 | ||
576eaefe SC |
6208 | void |
6209 | plt_expanding_visitor::visit_target_symbol (target_symbol *e) | |
6210 | { | |
6211 | try | |
6212 | { | |
6213 | if (e->name == "$$name") | |
6214 | { | |
6215 | literal_string *myname = new literal_string (entry); | |
6216 | myname->tok = e->tok; | |
6217 | provide(myname); | |
6218 | return; | |
6219 | } | |
3d69c03f JS |
6220 | |
6221 | // variable not found -> throw a semantic error | |
6222 | // (only to be caught right away, but this may be more complex later...) | |
6223 | string alternatives = "$$name"; | |
dc09353a | 6224 | throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)", |
3d69c03f | 6225 | e->name.c_str(), alternatives.c_str()), e->tok); |
576eaefe SC |
6226 | } |
6227 | catch (const semantic_error &er) | |
6228 | { | |
6229 | e->chain (er); | |
6230 | provide (e); | |
6231 | } | |
6232 | } | |
6233 | ||
6234 | ||
edce5b67 JS |
6235 | struct sdt_query : public base_query |
6236 | { | |
6237 | sdt_query(probe * base_probe, probe_point * base_loc, | |
6238 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 6239 | vector<derived_probe *> & results, const string user_lib); |
edce5b67 | 6240 | |
51d6bda3 | 6241 | void query_library (const char *data); |
576eaefe | 6242 | void query_plt (const char *entry, size_t addr) {} |
edce5b67 JS |
6243 | void handle_query_module(); |
6244 | ||
6245 | private: | |
15284963 | 6246 | stap_sdt_probe_type probe_type; |
d61ea602 | 6247 | enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc; |
edce5b67 JS |
6248 | probe * base_probe; |
6249 | probe_point * base_loc; | |
6846cfc8 | 6250 | literal_map_t const & params; |
edce5b67 | 6251 | vector<derived_probe *> & results; |
a794dbeb FCE |
6252 | string pp_mark; |
6253 | string pp_provider; | |
51d6bda3 | 6254 | string user_lib; |
edce5b67 JS |
6255 | |
6256 | set<string> probes_handled; | |
6257 | ||
6258 | Elf_Data *pdata; | |
6259 | size_t probe_scn_offset; | |
6260 | size_t probe_scn_addr; | |
aff5d390 | 6261 | uint64_t arg_count; |
40fe32e0 | 6262 | GElf_Addr base; |
c57ea854 | 6263 | GElf_Addr pc; |
aff5d390 | 6264 | string arg_string; |
edce5b67 | 6265 | string probe_name; |
a794dbeb | 6266 | string provider_name; |
909ab234 | 6267 | GElf_Addr semaphore_load_offset; |
79a0ca08 | 6268 | Dwarf_Addr semaphore; |
edce5b67 JS |
6269 | |
6270 | bool init_probe_scn(); | |
6b51ee12 | 6271 | bool get_next_probe(); |
c57ea854 SC |
6272 | void iterate_over_probe_entries(); |
6273 | void handle_probe_entry(); | |
edce5b67 | 6274 | |
40fe32e0 SC |
6275 | static void setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len); |
6276 | void setup_note_probe_entry (int type, const char *data, size_t len); | |
6277 | ||
edce5b67 | 6278 | void convert_probe(probe *base); |
4ddb6dd0 | 6279 | void record_semaphore(vector<derived_probe *> & results, unsigned start); |
c72aa911 | 6280 | probe* convert_location(); |
40fe32e0 | 6281 | bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;} |
c57ea854 SC |
6282 | bool have_debuginfo_uprobe(bool need_debug_info) |
6283 | {return probe_type == uprobe1_type | |
40fe32e0 | 6284 | || ((probe_type == uprobe2_type || probe_type == uprobe3_type) |
c57ea854 | 6285 | && need_debug_info);} |
40fe32e0 | 6286 | bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;} |
edce5b67 JS |
6287 | }; |
6288 | ||
6289 | ||
6290 | sdt_query::sdt_query(probe * base_probe, probe_point * base_loc, | |
6291 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 6292 | vector<derived_probe *> & results, const string user_lib): |
d61ea602 JS |
6293 | base_query(dw, params), probe_type(unknown_probe_type), |
6294 | probe_loc(unknown_section), base_probe(base_probe), | |
74fe61bc LB |
6295 | base_loc(base_loc), params(params), results(results), user_lib(user_lib), |
6296 | probe_scn_offset(0), probe_scn_addr(0), arg_count(0), base(0), pc(0), | |
909ab234 | 6297 | semaphore_load_offset(0), semaphore(0) |
edce5b67 | 6298 | { |
a794dbeb FCE |
6299 | assert(get_string_param(params, TOK_MARK, pp_mark)); |
6300 | get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified | |
6301 | ||
ef428667 FCE |
6302 | // PR10245: permit usage of dtrace-y "-" separator in marker name; |
6303 | // map it to double-underscores. | |
6304 | size_t pos = 0; | |
6305 | while (1) // there may be more than one | |
6306 | { | |
a794dbeb | 6307 | size_t i = pp_mark.find("-", pos); |
ef428667 | 6308 | if (i == string::npos) break; |
a794dbeb | 6309 | pp_mark.replace (i, 1, "__"); |
ef428667 FCE |
6310 | pos = i+1; // resume searching after the inserted __ |
6311 | } | |
a794dbeb FCE |
6312 | |
6313 | // XXX: same for pp_provider? | |
edce5b67 JS |
6314 | } |
6315 | ||
6316 | ||
6317 | void | |
c57ea854 | 6318 | sdt_query::handle_probe_entry() |
edce5b67 | 6319 | { |
c57ea854 SC |
6320 | if (! have_uprobe() |
6321 | && !probes_handled.insert(probe_name).second) | |
edce5b67 JS |
6322 | return; |
6323 | ||
6324 | if (sess.verbose > 3) | |
c57ea854 | 6325 | { |
b530b5b3 LB |
6326 | //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe |
6327 | //TRANSLATORS: is matched to. | |
6328 | clog << _F("matched probe_name %s probe type ", probe_name.c_str()); | |
c57ea854 SC |
6329 | switch (probe_type) |
6330 | { | |
6331 | case uprobe1_type: | |
6332 | clog << "uprobe1 at 0x" << hex << pc << dec << endl; | |
6333 | break; | |
6334 | case uprobe2_type: | |
6335 | clog << "uprobe2 at 0x" << hex << pc << dec << endl; | |
6336 | break; | |
40fe32e0 SC |
6337 | case uprobe3_type: |
6338 | clog << "uprobe3 at 0x" << hex << pc << dec << endl; | |
6339 | break; | |
d61ea602 JS |
6340 | default: |
6341 | clog << "unknown!" << endl; | |
6342 | break; | |
c57ea854 SC |
6343 | } |
6344 | } | |
edce5b67 | 6345 | |
c57ea854 SC |
6346 | // Extend the derivation chain |
6347 | probe *new_base = convert_location(); | |
6348 | probe_point *new_location = new_base->locations[0]; | |
6349 | ||
c57ea854 SC |
6350 | bool need_debug_info = false; |
6351 | ||
7d395255 JS |
6352 | // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...)) |
6353 | // or dwfl_module_getelf(...). We only need it for the machine type, which | |
6354 | // should be the same. The bias is used for relocating debuginfoless probes, | |
6355 | // though, so that must come from the possibly-prelinked ELF file, not DWARF. | |
c57ea854 | 6356 | Dwarf_Addr bias; |
7d395255 | 6357 | Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias); |
c57ea854 | 6358 | |
1cc41cd6 DS |
6359 | /* Figure out the architecture of this particular ELF file. The |
6360 | dwarfless register-name mappings depend on it. */ | |
6361 | GElf_Ehdr ehdr_mem; | |
6362 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
6363 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } | |
d8f31d0a | 6364 | assert(em); |
1cc41cd6 DS |
6365 | int elf_machine = em->e_machine; |
6366 | sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, module_val, | |
6367 | provider_name, probe_name, | |
6368 | probe_type, arg_string, arg_count); | |
6369 | svv.replace (new_base->body); | |
6370 | need_debug_info = svv.need_debug_info; | |
c57ea854 | 6371 | |
1cc41cd6 DS |
6372 | // XXX: why not derive_probes() in the uprobes case too? |
6373 | literal_map_t params; | |
6374 | for (unsigned i = 0; i < new_location->components.size(); ++i) | |
6375 | { | |
6376 | probe_point::component *c = new_location->components[i]; | |
6377 | params[c->functor] = c->arg; | |
6378 | } | |
c57ea854 | 6379 | |
73d53dd2 | 6380 | unsigned prior_results_size = results.size(); |
1cc41cd6 DS |
6381 | dwarf_query q(new_base, new_location, dw, params, results, "", ""); |
6382 | q.has_mark = true; // enables mid-statement probing | |
30263a73 | 6383 | |
73d53dd2 JS |
6384 | // V1 probes always need dwarf info |
6385 | // V2+ probes need dwarf info in case of a variable reference | |
1cc41cd6 DS |
6386 | if (have_debuginfo_uprobe(need_debug_info)) |
6387 | dw.iterate_over_modules(&query_module, &q); | |
73d53dd2 JS |
6388 | |
6389 | // For V2+ probes, if variable references weren't used or failed (PR14369), | |
6390 | // then try with the more direct approach. Unresolved $vars might still | |
6391 | // cause their own error, but this gives them a chance to be optimized out. | |
6392 | if (have_debuginfoless_uprobe() && results.size() == prior_results_size) | |
1cc41cd6 DS |
6393 | { |
6394 | string section; | |
6395 | Dwarf_Addr reloc_addr = q.statement_num_val + bias; | |
6396 | if (dwfl_module_relocations (q.dw.mod_info->mod) > 0) | |
6397 | { | |
6398 | dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr); | |
6399 | section = ".dynamic"; | |
6400 | } | |
6401 | else | |
6402 | section = ".absolute"; | |
edce5b67 | 6403 | |
1cc41cd6 DS |
6404 | uprobe_derived_probe* p = |
6405 | new uprobe_derived_probe ("", "", 0, | |
6406 | path_remove_sysroot(sess,q.module_val), | |
6407 | section, | |
6408 | q.statement_num_val, reloc_addr, q, 0); | |
6409 | p->saveargs (arg_count); | |
6410 | results.push_back (p); | |
c57ea854 | 6411 | } |
487bf4e2 | 6412 | sess.unwindsym_modules.insert (dw.module_name); |
73d53dd2 | 6413 | record_semaphore(results, prior_results_size); |
c57ea854 | 6414 | } |
edce5b67 | 6415 | |
4ddb6dd0 | 6416 | |
c57ea854 SC |
6417 | void |
6418 | sdt_query::handle_query_module() | |
6419 | { | |
6420 | if (!init_probe_scn()) | |
6421 | return; | |
edce5b67 | 6422 | |
c57ea854 SC |
6423 | if (sess.verbose > 3) |
6424 | clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl; | |
edce5b67 | 6425 | |
40fe32e0 SC |
6426 | if (probe_loc == note_section) |
6427 | { | |
6428 | GElf_Shdr shdr_mem; | |
6429 | GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem); | |
6430 | ||
909ab234 JS |
6431 | // The 'base' lets us adjust the hardcoded addresses in notes for prelink |
6432 | // effects. The 'semaphore_load_offset' accounts for the difference in | |
6433 | // load addresses between text and data, so the semaphore can be | |
6434 | // converted to a file offset if needed. | |
40fe32e0 | 6435 | if (shdr) |
909ab234 JS |
6436 | { |
6437 | base = shdr->sh_addr; | |
6438 | GElf_Addr base_offset = shdr->sh_offset; | |
6439 | shdr = dw.get_section (".probes", &shdr_mem); | |
6440 | if (shdr) | |
6441 | semaphore_load_offset = | |
6442 | (shdr->sh_addr - shdr->sh_offset) - (base - base_offset); | |
6443 | } | |
40fe32e0 | 6444 | else |
909ab234 JS |
6445 | base = semaphore_load_offset = 0; |
6446 | ||
40fe32e0 SC |
6447 | dw.iterate_over_notes ((void*) this, &sdt_query::setup_note_probe_entry_callback); |
6448 | } | |
d61ea602 | 6449 | else if (probe_loc == probe_section) |
40fe32e0 | 6450 | iterate_over_probe_entries (); |
edce5b67 JS |
6451 | } |
6452 | ||
6453 | ||
6454 | bool | |
6455 | sdt_query::init_probe_scn() | |
6456 | { | |
448a86b7 | 6457 | Elf* elf; |
edce5b67 | 6458 | GElf_Shdr shdr_mem; |
40fe32e0 SC |
6459 | |
6460 | GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem); | |
6461 | if (shdr) | |
6462 | { | |
6463 | probe_loc = note_section; | |
6464 | return true; | |
6465 | } | |
edce5b67 | 6466 | |
448a86b7 | 6467 | shdr = dw.get_section (".probes", &shdr_mem, &elf); |
fea74777 | 6468 | if (shdr) |
edce5b67 | 6469 | { |
fea74777 SC |
6470 | pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
6471 | probe_scn_offset = 0; | |
6472 | probe_scn_addr = shdr->sh_addr; | |
6473 | assert (pdata != NULL); | |
6474 | if (sess.verbose > 4) | |
ce0f6648 LB |
6475 | clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: " |
6476 | << pdata->d_size << endl; | |
40fe32e0 | 6477 | probe_loc = probe_section; |
fea74777 | 6478 | return true; |
edce5b67 | 6479 | } |
fea74777 | 6480 | else |
edce5b67 | 6481 | return false; |
edce5b67 JS |
6482 | } |
6483 | ||
40fe32e0 SC |
6484 | void |
6485 | sdt_query::setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len) | |
6486 | { | |
6487 | sdt_query *me = (sdt_query*)object; | |
6488 | me->setup_note_probe_entry (type, data, len); | |
6489 | } | |
6490 | ||
6491 | ||
6492 | void | |
6493 | sdt_query::setup_note_probe_entry (int type, const char *data, size_t len) | |
6494 | { | |
6495 | // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME | |
6496 | // && !memcmp (data->d_buf + name_off, | |
6497 | // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME)) | |
6498 | ||
6499 | // probes are in the .note.stapsdt section | |
6500 | #define _SDT_NOTE_TYPE 3 | |
6501 | if (type != _SDT_NOTE_TYPE) | |
6502 | return; | |
6503 | ||
6504 | union | |
6505 | { | |
6506 | Elf64_Addr a64[3]; | |
6507 | Elf32_Addr a32[3]; | |
6508 | } buf; | |
6509 | Dwarf_Addr bias; | |
6510 | Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias)); | |
6511 | Elf_Data dst = | |
6512 | { | |
6513 | &buf, ELF_T_ADDR, EV_CURRENT, | |
6514 | gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0 | |
6515 | }; | |
6516 | assert (dst.d_size <= sizeof buf); | |
6517 | ||
6518 | if (len < dst.d_size + 3) | |
6519 | return; | |
6520 | ||
6521 | Elf_Data src = | |
6522 | { | |
6523 | (void *) data, ELF_T_ADDR, EV_CURRENT, | |
6524 | dst.d_size, 0, 0 | |
6525 | }; | |
6526 | ||
6527 | if (gelf_xlatetom (elf, &dst, &src, | |
6528 | elf_getident (elf, NULL)[EI_DATA]) == NULL) | |
6529 | printf ("gelf_xlatetom: %s", elf_errmsg (-1)); | |
6530 | ||
6531 | probe_type = uprobe3_type; | |
6532 | const char * provider = data + dst.d_size; | |
3f803f9e | 6533 | |
40fe32e0 | 6534 | const char *name = (const char*)memchr (provider, '\0', data + len - provider); |
3f803f9e CM |
6535 | if(name++ == NULL) |
6536 | return; | |
6537 | ||
6538 | const char *args = (const char*)memchr (name, '\0', data + len - name); | |
6539 | if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1) | |
6540 | return; | |
6541 | ||
6542 | provider_name = provider; | |
6543 | probe_name = name; | |
6544 | arg_string = args; | |
40fe32e0 SC |
6545 | |
6546 | // Did we find a matching probe? | |
6547 | if (! (dw.function_name_matches_pattern (probe_name, pp_mark) | |
6548 | && ((pp_provider == "") | |
6549 | || dw.function_name_matches_pattern (provider_name, pp_provider)))) | |
6550 | return; | |
6551 | ||
e9a90eee JS |
6552 | // PR13934: Assembly probes are not forced to use the N@OP form. |
6553 | // If we have '@' then great, else count based on space-delimiters. | |
6554 | arg_count = count(arg_string.begin(), arg_string.end(), '@'); | |
6555 | if (!arg_count && !arg_string.empty()) | |
6556 | arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' '); | |
6557 | ||
40fe32e0 SC |
6558 | GElf_Addr base_ref; |
6559 | if (gelf_getclass (elf) == ELFCLASS32) | |
6560 | { | |
6561 | pc = buf.a32[0]; | |
6562 | base_ref = buf.a32[1]; | |
6563 | semaphore = buf.a32[2]; | |
6564 | } | |
6565 | else | |
6566 | { | |
6567 | pc = buf.a64[0]; | |
6568 | base_ref = buf.a64[1]; | |
6569 | semaphore = buf.a64[2]; | |
6570 | } | |
6571 | ||
6572 | semaphore += base - base_ref; | |
6573 | pc += base - base_ref; | |
6574 | ||
7d395255 JS |
6575 | // The semaphore also needs the ELF bias added now, so |
6576 | // record_semaphore can properly relocate it later. | |
6577 | semaphore += bias; | |
6578 | ||
40fe32e0 | 6579 | if (sess.verbose > 4) |
b530b5b3 | 6580 | 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 |
6581 | |
6582 | handle_probe_entry(); | |
6583 | } | |
6584 | ||
6585 | ||
c57ea854 SC |
6586 | void |
6587 | sdt_query::iterate_over_probe_entries() | |
edce5b67 | 6588 | { |
c57ea854 | 6589 | // probes are in the .probe section |
edce5b67 JS |
6590 | while (probe_scn_offset < pdata->d_size) |
6591 | { | |
aff5d390 SC |
6592 | stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset); |
6593 | stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset); | |
15284963 | 6594 | probe_type = (stap_sdt_probe_type)(pbe_v1->type_a); |
1cc41cd6 | 6595 | if (! have_uprobe()) |
edce5b67 JS |
6596 | { |
6597 | // Unless this is a mangled .probes section, this happens | |
6598 | // because the name of the probe comes first, followed by | |
6599 | // the sentinel. | |
6600 | if (sess.verbose > 5) | |
b530b5b3 | 6601 | clog << _F("got unknown probe_type : 0x%x", probe_type) << endl; |
edce5b67 JS |
6602 | probe_scn_offset += sizeof(__uint32_t); |
6603 | continue; | |
6604 | } | |
aff5d390 SC |
6605 | if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b |
6606 | { | |
6607 | pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t)); | |
1cc41cd6 | 6608 | if (pbe_v1->type_b != uprobe1_type) |
aff5d390 SC |
6609 | continue; |
6610 | } | |
6611 | ||
1cc41cd6 | 6612 | if (probe_type == uprobe1_type) |
aff5d390 | 6613 | { |
79a0ca08 | 6614 | if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6615 | return; |
79a0ca08 | 6616 | semaphore = 0; |
aff5d390 | 6617 | probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr); |
a794dbeb | 6618 | provider_name = ""; // unknown |
1cc41cd6 DS |
6619 | pc = pbe_v1->arg; |
6620 | arg_count = 0; | |
aff5d390 SC |
6621 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v1); |
6622 | } | |
08b5a50c | 6623 | else if (probe_type == uprobe2_type) |
aff5d390 | 6624 | { |
79a0ca08 | 6625 | if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6626 | return; |
79a0ca08 | 6627 | semaphore = pbe_v2->semaphore; |
aff5d390 | 6628 | probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr); |
a794dbeb | 6629 | provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr); |
aff5d390 SC |
6630 | arg_count = pbe_v2->arg_count; |
6631 | pc = pbe_v2->pc; | |
6632 | if (pbe_v2->arg_string) | |
6633 | arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr); | |
79a0ca08 SC |
6634 | // skip over pbe_v2, probe_name text and provider text |
6635 | probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length(); | |
6636 | probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t); | |
aff5d390 | 6637 | } |
edce5b67 | 6638 | if (sess.verbose > 4) |
b530b5b3 | 6639 | clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "") |
aff5d390 | 6640 | << "@0x" << hex << pc << dec << endl; |
edce5b67 | 6641 | |
a794dbeb FCE |
6642 | if (dw.function_name_matches_pattern (probe_name, pp_mark) |
6643 | && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider))) | |
c57ea854 | 6644 | handle_probe_entry (); |
edce5b67 | 6645 | } |
edce5b67 JS |
6646 | } |
6647 | ||
6648 | ||
6846cfc8 | 6649 | void |
4ddb6dd0 | 6650 | sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start) |
6846cfc8 | 6651 | { |
a794dbeb FCE |
6652 | for (unsigned i=0; i<2; i++) { |
6653 | // prefer with-provider symbol; look without provider prefix for backward compatibility only | |
6654 | string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore"; | |
6655 | // XXX: multiple addresses? | |
6656 | if (sess.verbose > 2) | |
b530b5b3 | 6657 | clog << _F("looking for semaphore symbol %s ", semaphore.c_str()); |
a794dbeb | 6658 | |
79a0ca08 SC |
6659 | Dwarf_Addr addr; |
6660 | if (this->semaphore) | |
6661 | addr = this->semaphore; | |
6662 | else | |
6663 | addr = lookup_symbol_address(dw.module, semaphore.c_str()); | |
a794dbeb FCE |
6664 | if (addr) |
6665 | { | |
7d395255 | 6666 | if (dwfl_module_relocations (dw.module) > 0) |
a794dbeb FCE |
6667 | dwfl_module_relocate_address (dw.module, &addr); |
6668 | // XXX: relocation basis? | |
909ab234 JS |
6669 | |
6670 | // Dyninst needs the *file*-based offset for semaphores, | |
6671 | // so subtract the difference in load addresses between .text and .probes | |
6672 | if (dw.sess.runtime_usermode_p()) | |
6673 | addr -= semaphore_load_offset; | |
6674 | ||
a794dbeb FCE |
6675 | for (unsigned i = start; i < results.size(); ++i) |
6676 | results[i]->sdt_semaphore_addr = addr; | |
6677 | if (sess.verbose > 2) | |
b530b5b3 | 6678 | clog << _(", found at 0x") << hex << addr << dec << endl; |
a794dbeb FCE |
6679 | return; |
6680 | } | |
6681 | else | |
6682 | if (sess.verbose > 2) | |
b530b5b3 | 6683 | clog << _(", not found") << endl; |
a794dbeb | 6684 | } |
6846cfc8 SC |
6685 | } |
6686 | ||
6687 | ||
edce5b67 JS |
6688 | void |
6689 | sdt_query::convert_probe (probe *base) | |
6690 | { | |
6691 | block *b = new block; | |
6692 | b->tok = base->body->tok; | |
6693 | ||
edce5b67 JS |
6694 | // Generate: if (arg1 != mark("label")) next; |
6695 | functioncall *fc = new functioncall; | |
bbafcb1e | 6696 | fc->function = "ulong_arg"; |
edce5b67 | 6697 | fc->tok = b->tok; |
bbafcb1e | 6698 | literal_number* num = new literal_number(1); |
edce5b67 JS |
6699 | num->tok = b->tok; |
6700 | fc->args.push_back(num); | |
6701 | ||
6702 | functioncall *fcus = new functioncall; | |
6703 | fcus->function = "user_string"; | |
6704 | fcus->type = pe_string; | |
6705 | fcus->tok = b->tok; | |
6706 | fcus->args.push_back(fc); | |
6707 | ||
6708 | if_statement *is = new if_statement; | |
6709 | is->thenblock = new next_statement; | |
6710 | is->elseblock = NULL; | |
6711 | is->tok = b->tok; | |
63ea4244 | 6712 | is->thenblock->tok = b->tok; |
edce5b67 JS |
6713 | comparison *be = new comparison; |
6714 | be->op = "!="; | |
6715 | be->tok = b->tok; | |
6716 | be->left = fcus; | |
6717 | be->right = new literal_string(probe_name); | |
63ea4244 | 6718 | be->right->tok = b->tok; |
edce5b67 JS |
6719 | is->condition = be; |
6720 | b->statements.push_back(is); | |
6721 | ||
6722 | // Now replace the body | |
6723 | b->statements.push_back(base->body); | |
6724 | base->body = b; | |
6725 | } | |
6726 | ||
6727 | ||
c72aa911 JS |
6728 | probe* |
6729 | sdt_query::convert_location () | |
edce5b67 | 6730 | { |
c72aa911 | 6731 | probe_point* specific_loc = new probe_point(*base_loc); |
662539d9 | 6732 | vector<probe_point::component*> derived_comps; |
edce5b67 | 6733 | |
662539d9 JS |
6734 | vector<probe_point::component*>::iterator it; |
6735 | for (it = specific_loc->components.begin(); | |
6736 | it != specific_loc->components.end(); ++it) | |
6737 | if ((*it)->functor == TOK_PROCESS) | |
6738 | { | |
1cc41cd6 DS |
6739 | // copy the process name |
6740 | derived_comps.push_back(*it); | |
662539d9 JS |
6741 | } |
6742 | else if ((*it)->functor == TOK_LIBRARY) | |
6743 | { | |
1cc41cd6 DS |
6744 | // copy the library name for process probes |
6745 | derived_comps.push_back(*it); | |
662539d9 JS |
6746 | } |
6747 | else if ((*it)->functor == TOK_PROVIDER) | |
6748 | { | |
6749 | // replace the possibly wildcarded arg with the specific provider name | |
6750 | *it = new probe_point::component(TOK_PROVIDER, | |
6751 | new literal_string(provider_name)); | |
6752 | } | |
6753 | else if ((*it)->functor == TOK_MARK) | |
c72aa911 JS |
6754 | { |
6755 | // replace the possibly wildcarded arg with the specific marker name | |
662539d9 JS |
6756 | *it = new probe_point::component(TOK_MARK, |
6757 | new literal_string(probe_name)); | |
a794dbeb | 6758 | |
aff5d390 SC |
6759 | if (sess.verbose > 3) |
6760 | switch (probe_type) | |
6761 | { | |
6762 | case uprobe1_type: | |
b530b5b3 | 6763 | clog << _("probe_type == uprobe1, use statement addr: 0x") |
aff5d390 SC |
6764 | << hex << pc << dec << endl; |
6765 | break; | |
6766 | case uprobe2_type: | |
b530b5b3 | 6767 | clog << _("probe_type == uprobe2, use statement addr: 0x") |
aff5d390 SC |
6768 | << hex << pc << dec << endl; |
6769 | break; | |
40fe32e0 | 6770 | case uprobe3_type: |
b530b5b3 | 6771 | clog << _("probe_type == uprobe3, use statement addr: 0x") |
40fe32e0 SC |
6772 | << hex << pc << dec << endl; |
6773 | break; | |
aff5d390 | 6774 | default: |
b530b5b3 LB |
6775 | clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s", |
6776 | pp_mark.c_str()) << endl; | |
aff5d390 SC |
6777 | } |
6778 | ||
c72aa911 JS |
6779 | switch (probe_type) |
6780 | { | |
aff5d390 SC |
6781 | case uprobe1_type: |
6782 | case uprobe2_type: | |
40fe32e0 | 6783 | case uprobe3_type: |
c72aa911 | 6784 | // process("executable").statement(probe_arg) |
662539d9 JS |
6785 | derived_comps.push_back |
6786 | (new probe_point::component(TOK_STATEMENT, | |
6787 | new literal_number(pc, true))); | |
c72aa911 JS |
6788 | break; |
6789 | ||
a794dbeb | 6790 | default: // deprecated |
c72aa911 | 6791 | // process("executable").function("*").label("_stapprobe1_MARK_NAME") |
662539d9 JS |
6792 | derived_comps.push_back |
6793 | (new probe_point::component(TOK_FUNCTION, | |
6794 | new literal_string("*"))); | |
6795 | derived_comps.push_back | |
c72aa911 | 6796 | (new probe_point::component(TOK_LABEL, |
a794dbeb | 6797 | new literal_string("_stapprobe1_" + pp_mark))); |
c72aa911 JS |
6798 | break; |
6799 | } | |
6800 | } | |
edce5b67 | 6801 | |
662539d9 JS |
6802 | probe_point* derived_loc = new probe_point(*specific_loc); |
6803 | derived_loc->components = derived_comps; | |
8159bf55 | 6804 | return new probe (new probe (base_probe, specific_loc), derived_loc); |
edce5b67 JS |
6805 | } |
6806 | ||
6807 | ||
51d6bda3 SC |
6808 | void |
6809 | sdt_query::query_library (const char *library) | |
6810 | { | |
6811 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
6812 | } | |
6813 | ||
40a70f52 JL |
6814 | string |
6815 | suggest_plt_functions(systemtap_session& sess, | |
6816 | const set<string>& modules, | |
6817 | const string& func) | |
6818 | { | |
6819 | if (func.empty() || modules.empty() || sess.module_cache == NULL) | |
6820 | return ""; | |
6821 | ||
6822 | set<string> funcs; | |
6823 | const map<string, module_info*> &cache = sess.module_cache->cache; | |
6824 | ||
6825 | for (set<string>::iterator itmod = modules.begin(); | |
6826 | itmod != modules.end(); ++itmod) | |
6827 | { | |
6828 | map<string, module_info*>::const_iterator itcache; | |
6829 | if ((itcache = cache.find(*itmod)) != cache.end()) | |
6830 | funcs.insert(itcache->second->plt_funcs.begin(), | |
6831 | itcache->second->plt_funcs.end()); | |
6832 | } | |
6833 | ||
6834 | if (sess.verbose > 2) | |
6835 | clog << "suggesting from " << funcs.size() << " functions" << endl; | |
6836 | ||
6837 | if (funcs.empty()) | |
6838 | return ""; | |
6839 | ||
6840 | return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only | |
6841 | } | |
51d6bda3 | 6842 | |
0508c45f JL |
6843 | string |
6844 | suggest_dwarf_functions(systemtap_session& sess, | |
6845 | const set<string>& modules, | |
6846 | string func) | |
44ffe90c JL |
6847 | { |
6848 | // Trim any @ component | |
6849 | size_t pos = func.find('@'); | |
6850 | if (pos != string::npos) | |
6851 | func.erase(pos); | |
6852 | ||
0508c45f | 6853 | if (func.empty() || modules.empty() || sess.module_cache == NULL) |
44ffe90c JL |
6854 | return ""; |
6855 | ||
6856 | // We must first aggregate all the functions from the cache | |
6857 | set<string> funcs; | |
6858 | const map<string, module_info*> &cache = sess.module_cache->cache; | |
6859 | ||
0508c45f JL |
6860 | for (set<string>::iterator itmod = modules.begin(); |
6861 | itmod != modules.end(); ++itmod) | |
44ffe90c JL |
6862 | { |
6863 | map<string, module_info*>::const_iterator itcache; | |
6864 | if ((itcache = cache.find(*itmod)) != cache.end()) | |
6865 | funcs.insert(itcache->second->sym_seen.begin(), | |
6866 | itcache->second->sym_seen.end()); | |
6867 | } | |
6868 | ||
6869 | if (sess.verbose > 2) | |
6870 | clog << "suggesting from " << funcs.size() << " functions" << endl; | |
6871 | ||
6872 | if (funcs.empty()) | |
6873 | return ""; | |
6874 | ||
593f09eb | 6875 | return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only |
44ffe90c JL |
6876 | } |
6877 | ||
20c6c071 | 6878 | void |
5227f1ea | 6879 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 6880 | probe * base, |
20c6c071 | 6881 | probe_point * location, |
86bf665e | 6882 | literal_map_t const & parameters, |
20c6c071 GH |
6883 | vector<derived_probe *> & finished_results) |
6884 | { | |
b20febf3 FCE |
6885 | // NB: the kernel/user dwlfpp objects are long-lived. |
6886 | // XXX: but they should be per-session, as this builder object | |
6887 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 6888 | |
7a24d422 | 6889 | dwflpp* dw = 0; |
6d5d594e | 6890 | literal_map_t filled_parameters = parameters; |
7a24d422 | 6891 | |
7a24d422 | 6892 | string module_name; |
ae2552da FCE |
6893 | if (has_null_param (parameters, TOK_KERNEL)) |
6894 | { | |
6895 | dw = get_kern_dw(sess, "kernel"); | |
6896 | } | |
6897 | else if (get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 6898 | { |
c523a015 LB |
6899 | size_t dash_pos = 0; |
6900 | while((dash_pos=module_name.find('-'))!=string::npos) | |
6901 | module_name.replace(int(dash_pos),1,"_"); | |
6902 | filled_parameters[TOK_MODULE] = new literal_string(module_name); | |
37001baa FCE |
6903 | // NB: glob patterns get expanded later, during the offline |
6904 | // elfutils module listing. | |
ae2552da | 6905 | dw = get_kern_dw(sess, module_name); |
b8da0ad1 | 6906 | } |
6d5d594e | 6907 | else if (get_param (parameters, TOK_PROCESS, module_name) || has_null_param(parameters, TOK_PROCESS)) |
84c84ac4 | 6908 | { |
05fb3e0c | 6909 | module_name = sess.sysroot + module_name; |
6d5d594e LB |
6910 | if(has_null_param(filled_parameters, TOK_PROCESS)) |
6911 | { | |
6912 | wordexp_t words; | |
6913 | int rc = wordexp(sess.cmd.c_str(), &words, WRDE_NOCMD|WRDE_UNDEF); | |
6914 | if(rc || words.we_wordc <= 0) | |
dc09353a | 6915 | throw SEMANTIC_ERROR(_("unspecified process probe is invalid without a -c COMMAND")); |
05fb3e0c | 6916 | module_name = sess.sysroot + words.we_wordv[0]; |
6d5d594e LB |
6917 | filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map |
6918 | // in the case of TOK_MARK we need to modify locations as well | |
6919 | if(location->components[0]->functor==TOK_PROCESS && | |
6920 | location->components[0]->arg == 0) | |
6921 | location->components[0]->arg = new literal_string(module_name); | |
6922 | wordfree (& words); | |
6923 | } | |
5750ecc6 | 6924 | |
37001baa FCE |
6925 | // PR6456 process("/bin/*") glob handling |
6926 | if (contains_glob_chars (module_name)) | |
6927 | { | |
6928 | // Expand glob via rewriting the probe-point process("....") | |
6929 | // parameter, asserted to be the first one. | |
6930 | ||
6931 | assert (location->components.size() > 0); | |
6932 | assert (location->components[0]->functor == TOK_PROCESS); | |
6933 | assert (location->components[0]->arg); | |
6934 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
6935 | assert (lit); | |
6936 | ||
6937 | // Evaluate glob here, and call derive_probes recursively with each match. | |
6938 | glob_t the_blob; | |
88f8e2c0 | 6939 | set<string> dupes; |
37001baa | 6940 | int rc = glob (module_name.c_str(), 0, NULL, & the_blob); |
b530b5b3 | 6941 | if (rc) |
dc09353a | 6942 | throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() )); |
44ffe90c | 6943 | unsigned results_pre = finished_results.size(); |
37001baa FCE |
6944 | for (unsigned i = 0; i < the_blob.gl_pathc; ++i) |
6945 | { | |
e19ebcf7 | 6946 | assert_no_interrupts(); |
37001baa FCE |
6947 | |
6948 | const char* globbed = the_blob.gl_pathv[i]; | |
6949 | struct stat st; | |
6950 | ||
6951 | if (access (globbed, X_OK) == 0 | |
6952 | && stat (globbed, &st) == 0 | |
6953 | && S_ISREG (st.st_mode)) // see find_executable() | |
6954 | { | |
7977a734 FCE |
6955 | // Need to call canonicalize here, in order to path-expand |
6956 | // patterns like process("stap*"). Otherwise it may go through | |
6957 | // to the next round of expansion as ("stap"), leading to a $PATH | |
6958 | // search that's not consistent with the glob search already done. | |
5bca76a8 JS |
6959 | string canononicalized = resolve_path (globbed); |
6960 | globbed = canononicalized.c_str(); | |
7977a734 | 6961 | |
88f8e2c0 JS |
6962 | // The canonical names can result in duplication, for example |
6963 | // having followed symlinks that are common with shared | |
6964 | // libraries. Filter those out. | |
6965 | if (!dupes.insert(canononicalized).second) | |
6966 | continue; | |
6967 | ||
37001baa FCE |
6968 | // synthesize a new probe_point, with the glob-expanded string |
6969 | probe_point *pp = new probe_point (*location); | |
5750ecc6 FCE |
6970 | // PR13338: quote results to prevent recursion |
6971 | string eglobbed = escape_glob_chars (globbed); | |
6972 | ||
6973 | if (sess.verbose > 1) | |
6974 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", | |
6975 | module_name.c_str(), eglobbed.c_str()) << endl; | |
05fb3e0c | 6976 | string eglobbed_tgt = path_remove_sysroot(sess, eglobbed); |
5750ecc6 | 6977 | |
37001baa | 6978 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 6979 | new literal_string (eglobbed_tgt)); |
37001baa FCE |
6980 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
6981 | pp->components[0] = ppc; | |
6982 | ||
d885563b | 6983 | probe* new_probe = new probe (base, pp); |
7977a734 FCE |
6984 | |
6985 | // We override "optional = true" here, as if the | |
6986 | // wildcarded probe point was given a "?" suffix. | |
6987 | ||
6988 | // This is because wildcard probes will be expected | |
6989 | // by users to apply only to some subset of the | |
6990 | // matching binaries, in the sense of "any", rather | |
6991 | // than "all", sort of similarly how | |
6992 | // module("*").function("...") patterns work. | |
6993 | ||
6994 | derive_probes (sess, new_probe, finished_results, | |
6995 | true /* NB: not location->optional */ ); | |
37001baa FCE |
6996 | } |
6997 | } | |
6998 | ||
6999 | globfree (& the_blob); | |
44ffe90c JL |
7000 | |
7001 | unsigned results_post = finished_results.size(); | |
7002 | ||
7003 | // Did we fail to find a function by name? Let's suggest | |
7004 | // something! | |
7005 | string func; | |
7006 | if (results_pre == results_post | |
7007 | && get_param(filled_parameters, TOK_FUNCTION, func) | |
7008 | && !func.empty()) | |
7009 | { | |
7010 | if (sess.verbose > 2) | |
7011 | { | |
7012 | clog << "suggesting functions from modules:" << endl; | |
7013 | for (set<string>::const_iterator it = modules_seen.begin(); | |
7014 | it != modules_seen.end(); ++it) | |
7015 | { | |
7016 | clog << *it << endl; | |
7017 | } | |
7018 | } | |
0508c45f | 7019 | string sugs = suggest_dwarf_functions(sess, modules_seen, func); |
44ffe90c JL |
7020 | modules_seen.clear(); |
7021 | if (!sugs.empty()) | |
dc09353a | 7022 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", |
44ffe90c JL |
7023 | "no match (similar functions: %s)", |
7024 | sugs.find(',') == string::npos, | |
7025 | sugs.c_str())); | |
7026 | } | |
40a70f52 JL |
7027 | else if (results_pre == results_post |
7028 | && get_param(filled_parameters, TOK_PLT, func) | |
7029 | && !func.empty()) | |
7030 | { | |
7031 | string sugs = suggest_plt_functions(sess, modules_seen, func); | |
7032 | modules_seen.clear(); | |
7033 | if (!sugs.empty()) | |
7034 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", | |
7035 | "no match (similar functions: %s)", | |
7036 | sugs.find(',') == string::npos, | |
7037 | sugs.c_str())); | |
7038 | } | |
44ffe90c | 7039 | |
37001baa FCE |
7040 | return; // avoid falling through |
7041 | } | |
7042 | ||
5750ecc6 FCE |
7043 | // PR13338: unquote glob results |
7044 | module_name = unescape_glob_chars (module_name); | |
05fb3e0c | 7045 | user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it |
d1bcbe71 RH |
7046 | |
7047 | // if the executable starts with "#!", we look for the interpreter of the script | |
7048 | { | |
7049 | ifstream script_file (user_path.c_str () ); | |
7050 | ||
7051 | if (script_file.good ()) | |
7052 | { | |
7053 | string line; | |
7054 | ||
7055 | getline (script_file, line); | |
7056 | ||
7057 | if (line.compare (0, 2, "#!") == 0) | |
7058 | { | |
7059 | string path_head = line.substr(2); | |
7060 | ||
7061 | // remove white spaces at the beginning of the string | |
7062 | size_t p2 = path_head.find_first_not_of(" \t"); | |
7063 | ||
7064 | if (p2 != string::npos) | |
7065 | { | |
7066 | string path = path_head.substr(p2); | |
7067 | ||
7068 | // remove white spaces at the end of the string | |
7069 | p2 = path.find_last_not_of(" \t\n"); | |
7070 | if (string::npos != p2) | |
7071 | path.erase(p2+1); | |
7072 | ||
8e13c1a1 RH |
7073 | // handle "#!/usr/bin/env" redirect |
7074 | size_t offset = 0; | |
7075 | if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0) | |
7076 | { | |
7077 | offset = sizeof("/bin/env")-1; | |
7078 | } | |
7079 | else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0) | |
7080 | { | |
7081 | offset = sizeof("/usr/bin/env")-1; | |
7082 | } | |
7083 | ||
7084 | if (offset != 0) | |
7085 | { | |
7086 | size_t p3 = path.find_first_not_of(" \t", offset); | |
7087 | ||
7088 | if (p3 != string::npos) | |
7089 | { | |
7090 | string env_path = path.substr(p3); | |
05fb3e0c WF |
7091 | user_path = find_executable (env_path, sess.sysroot, |
7092 | sess.sysenv); | |
8e13c1a1 RH |
7093 | } |
7094 | } | |
7095 | else | |
7096 | { | |
05fb3e0c | 7097 | user_path = find_executable (path, sess.sysroot, sess.sysenv); |
8e13c1a1 | 7098 | } |
d1bcbe71 RH |
7099 | |
7100 | struct stat st; | |
7101 | ||
7102 | if (access (user_path.c_str(), X_OK) == 0 | |
7103 | && stat (user_path.c_str(), &st) == 0 | |
7104 | && S_ISREG (st.st_mode)) // see find_executable() | |
7105 | { | |
7106 | if (sess.verbose > 1) | |
b530b5b3 LB |
7107 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", |
7108 | module_name.c_str(), user_path.c_str()) << endl; | |
d1bcbe71 RH |
7109 | |
7110 | assert (location->components.size() > 0); | |
7111 | assert (location->components[0]->functor == TOK_PROCESS); | |
7112 | assert (location->components[0]->arg); | |
7113 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
7114 | assert (lit); | |
7115 | ||
7116 | // synthesize a new probe_point, with the expanded string | |
7117 | probe_point *pp = new probe_point (*location); | |
05fb3e0c | 7118 | string user_path_tgt = path_remove_sysroot(sess, user_path); |
d1bcbe71 | 7119 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 7120 | new literal_string (user_path_tgt.c_str())); |
d1bcbe71 RH |
7121 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
7122 | pp->components[0] = ppc; | |
7123 | ||
d885563b | 7124 | probe* new_probe = new probe (base, pp); |
d1bcbe71 RH |
7125 | |
7126 | derive_probes (sess, new_probe, finished_results); | |
7127 | ||
7128 | script_file.close(); | |
7129 | return; | |
7130 | } | |
7131 | } | |
7132 | } | |
7133 | } | |
7134 | script_file.close(); | |
7135 | } | |
7136 | ||
47e226ed | 7137 | if (get_param (parameters, TOK_LIBRARY, user_lib) |
378d78b5 | 7138 | && user_lib.length() && ! contains_glob_chars (user_lib)) |
47e226ed SC |
7139 | { |
7140 | module_name = find_executable (user_lib, sess.sysroot, sess.sysenv, | |
7141 | "LD_LIBRARY_PATH"); | |
7142 | if (module_name.find('/') == string::npos) | |
7143 | // We didn't find user_lib so use iterate_over_libraries | |
7144 | module_name = user_path; | |
7145 | } | |
63b4fd14 | 7146 | else |
b642c901 | 7147 | module_name = user_path; // canonicalize it |
d0a7f5a9 | 7148 | |
3667d615 | 7149 | // uretprobes aren't available everywhere |
536f1261 | 7150 | if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p()) |
2b69faaf | 7151 | { |
3667d615 JS |
7152 | if (kernel_supports_inode_uprobes(sess) && |
7153 | !kernel_supports_inode_uretprobes(sess)) | |
dc09353a | 7154 | throw SEMANTIC_ERROR |
3ffeaf3c | 7155 | (_("process return probes not available [man error::inode-uprobes]")); |
2b69faaf | 7156 | } |
3667d615 | 7157 | |
f4000852 MW |
7158 | // There is a similar check in pass 4 (buildrun), but it is |
7159 | // needed here too to make sure alternatives for optional | |
7160 | // (? or !) process probes are disposed and/or alternatives | |
7161 | // are selected. | |
3667d615 JS |
7162 | if (!sess.runtime_usermode_p()) |
7163 | check_process_probe_kernel_support(sess); | |
e34d5d13 | 7164 | |
7a24d422 FCE |
7165 | // user-space target; we use one dwflpp instance per module name |
7166 | // (= program or shared library) | |
707bf35e | 7167 | dw = get_user_dw(sess, module_name); |
c8959a29 | 7168 | } |
20c6c071 | 7169 | |
1acfc030 JS |
7170 | assert(dw); |
7171 | ||
5896cd05 | 7172 | if (sess.verbose > 3) |
b530b5b3 | 7173 | clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl; |
5896cd05 | 7174 | |
a794dbeb FCE |
7175 | string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __ |
7176 | if (get_param(parameters, TOK_MARK, dummy_mark_name)) | |
f28a8c28 | 7177 | { |
51d6bda3 | 7178 | sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib); |
edce5b67 JS |
7179 | dw->iterate_over_modules(&query_module, &sdtq); |
7180 | return; | |
7a05f484 | 7181 | } |
20c6c071 | 7182 | |
8f14e444 | 7183 | unsigned results_pre = finished_results.size(); |
6d5d594e | 7184 | dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib); |
7a24d422 FCE |
7185 | |
7186 | // XXX: kernel.statement.absolute is a special case that requires no | |
7187 | // dwfl processing. This code should be in a separate builder. | |
7a24d422 | 7188 | if (q.has_kernel && q.has_absolute) |
37ebca01 | 7189 | { |
4baf0e53 | 7190 | // assert guru mode for absolute probes |
37ebca01 FCE |
7191 | if (! q.base_probe->privileged) |
7192 | { | |
dc09353a | 7193 | throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), |
edce5b67 | 7194 | q.base_probe->tok); |
37ebca01 FCE |
7195 | } |
7196 | ||
7197 | // For kernel.statement(NUM).absolute probe points, we bypass | |
7198 | // all the debuginfo stuff: We just wire up a | |
7199 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 7200 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
7201 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
7202 | q.statement_num_val, q.statement_num_val, | |
7203 | q, 0); | |
37ebca01 | 7204 | finished_results.push_back (p); |
1a0dbc5a | 7205 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
7206 | return; |
7207 | } | |
7208 | ||
51178501 | 7209 | dw->iterate_over_modules(&query_module, &q); |
8f14e444 | 7210 | |
44ffe90c JL |
7211 | // We need to update modules_seen with the modules we've visited |
7212 | modules_seen.insert(q.visited_modules.begin(), | |
7213 | q.visited_modules.end()); | |
8f14e444 FCE |
7214 | |
7215 | // PR11553 special processing: .return probes requested, but | |
7216 | // some inlined function instances matched. | |
7217 | unsigned i_n_r = q.inlined_non_returnable.size(); | |
7218 | unsigned results_post = finished_results.size(); | |
7219 | if (i_n_r > 0) | |
7220 | { | |
7221 | if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning | |
7222 | { | |
7223 | string quicklist; | |
7224 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
7225 | it != q.inlined_non_returnable.end(); | |
7226 | it++) | |
7227 | { | |
7228 | quicklist += " " + (*it); | |
7229 | if (quicklist.size() > 80) // heuristic, don't make an overlong report line | |
7230 | { | |
7231 | quicklist += " ..."; | |
7232 | break; | |
7233 | } | |
7234 | } | |
c57ea854 | 7235 | |
52c2652f | 7236 | sess.print_warning (_NF("cannot probe .return of %u inlined function %s", |
b530b5b3 | 7237 | "cannot probe .return of %u inlined functions %s", |
52c2652f | 7238 | quicklist.size(), i_n_r, quicklist.c_str())); |
8f14e444 FCE |
7239 | // There will be also a "no matches" semantic error generated. |
7240 | } | |
7241 | if (sess.verbose > 1) | |
52c2652f NMA |
7242 | clog << _NF("skipped .return probe of %u inlined function", |
7243 | "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl; | |
8f14e444 FCE |
7244 | if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity |
7245 | { | |
7246 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
7247 | it != q.inlined_non_returnable.end(); | |
7248 | it++) | |
7249 | clog << (*it) << " "; | |
7250 | clog << endl; | |
7251 | } | |
7252 | } // i_n_r > 0 | |
44ffe90c JL |
7253 | |
7254 | // Did we fail to find a function by name? Let's suggest something! We | |
7255 | // need to check for optional because otherwise, we will be suggesting | |
7256 | // things during intermediate results without including all the | |
7257 | // possible functions. For example, process("/usr/bin/*").function | |
7258 | // will go through here for each executable found (all labelled as | |
7259 | // optionals). Similarly for library(glob) probes. TODO: find a | |
7260 | // mechanism to have suggestions for optional probes as well when no | |
7261 | // probes could be derived, e.g. probepoint1?, probepoint2 should | |
7262 | // suggest for both 1 and 2 if both fail to resolve (maybe print as a | |
7263 | // warning?). This may entails detecting the difference between script | |
7264 | // optional probes and probes that are optional in recursive calls. | |
7265 | string func; | |
7266 | if (results_pre == results_post && !location->optional | |
7267 | && get_param(filled_parameters, TOK_FUNCTION, func) | |
7268 | && !func.empty()) | |
7269 | { | |
7270 | if (sess.verbose > 2) | |
7271 | { | |
7272 | clog << "suggesting functions from modules:" << endl; | |
7273 | for (set<string>::const_iterator it = modules_seen.begin(); | |
7274 | it != modules_seen.end(); ++it) | |
7275 | clog << *it << endl; | |
7276 | } | |
0508c45f | 7277 | string sugs = suggest_dwarf_functions(sess, modules_seen, func); |
44ffe90c JL |
7278 | modules_seen.clear(); |
7279 | if (!sugs.empty()) | |
7280 | // Note that this error will not even be printed out if it is | |
7281 | // exactly the same suggestion as a previous throw (since | |
7282 | // print_error() filters out identical errors). Which makes | |
7283 | // sense since it's possible that the user misspelled the same | |
7284 | // function in different probes, in which case the first | |
7285 | // suggestion is sufficient. | |
dc09353a | 7286 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", |
44ffe90c JL |
7287 | "no match (similar functions: %s)", |
7288 | sugs.find(',') == string::npos, | |
7289 | sugs.c_str())); | |
7290 | } | |
40a70f52 JL |
7291 | else if (results_pre == results_post && !location->optional |
7292 | && get_param(filled_parameters, TOK_PLT, func) | |
7293 | && !func.empty()) | |
7294 | { | |
7295 | string sugs = suggest_plt_functions(sess, modules_seen, func); | |
7296 | modules_seen.clear(); | |
7297 | if (!sugs.empty()) | |
7298 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", | |
7299 | "no match (similar functions: %s)", | |
7300 | sugs.find(',') == string::npos, | |
7301 | sugs.c_str())); | |
7302 | } | |
44ffe90c JL |
7303 | else if (results_pre != results_post) |
7304 | // Something was derived so we won't need to suggest something | |
7305 | modules_seen.clear(); | |
5f0a03a6 JK |
7306 | } |
7307 | ||
7308 | symbol_table::~symbol_table() | |
7309 | { | |
c9efa5c9 | 7310 | delete_map(map_by_addr); |
5f0a03a6 JK |
7311 | } |
7312 | ||
7313 | void | |
2867a2a1 | 7314 | symbol_table::add_symbol(const char *name, bool weak, bool descriptor, |
44ffe90c | 7315 | Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/) |
5f0a03a6 | 7316 | { |
ab91b232 JK |
7317 | #ifdef __powerpc__ |
7318 | // Map ".sys_foo" to "sys_foo". | |
7319 | if (name[0] == '.') | |
7320 | name++; | |
7321 | #endif | |
5f0a03a6 JK |
7322 | func_info *fi = new func_info(); |
7323 | fi->addr = addr; | |
7324 | fi->name = name; | |
ab91b232 | 7325 | fi->weak = weak; |
2867a2a1 | 7326 | fi->descriptor = descriptor; |
5f0a03a6 JK |
7327 | map_by_name[fi->name] = fi; |
7328 | // TODO: Use a multimap in case there are multiple static | |
7329 | // functions with the same name? | |
1c6b77e5 | 7330 | map_by_addr.insert(make_pair(addr, fi)); |
5f0a03a6 JK |
7331 | } |
7332 | ||
7333 | enum info_status | |
7334 | symbol_table::read_symbols(FILE *f, const string& path) | |
7335 | { | |
7336 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
7337 | int ret; | |
2e67a43b TM |
7338 | char *name = 0; |
7339 | char *mod = 0; | |
5f0a03a6 JK |
7340 | char type; |
7341 | unsigned long long addr; | |
7342 | Dwarf_Addr high_addr = 0; | |
7343 | int line = 0; | |
7344 | ||
62a4021d FCE |
7345 | #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7) |
7346 | #define MS_FMT "%ms" | |
7347 | #else | |
7348 | #define MS_FMT "%as" | |
7349 | #endif | |
7350 | // %ms (newer than %as) mallocs space for the string and stores its address. | |
7351 | while ((ret = fscanf(f, "%llx %c " MS_FMT " [" MS_FMT, &addr, &type, &name, &mod)) > 0) | |
5f0a03a6 | 7352 | { |
2e67a43b TM |
7353 | auto_free free_name(name); |
7354 | auto_free free_mod(mod); | |
5f0a03a6 JK |
7355 | line++; |
7356 | if (ret < 3) | |
7357 | { | |
3d372d6b | 7358 | cerr << _F("Symbol table error: Line %d of symbol list from %s is not in correct format: address type name [module]\n", |
b530b5b3 | 7359 | line, path.c_str()); |
5f0a03a6 JK |
7360 | // Caller should delete symbol_table object. |
7361 | return info_absent; | |
7362 | } | |
2e67a43b | 7363 | else if (ret > 3) |
5f0a03a6 JK |
7364 | { |
7365 | // Modules are loaded above the kernel, so if we're getting | |
7366 | // modules, we're done. | |
2e67a43b | 7367 | break; |
5f0a03a6 | 7368 | } |
ab91b232 | 7369 | if (type == 'T' || type == 't' || type == 'W') |
2867a2a1 | 7370 | add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr); |
5f0a03a6 JK |
7371 | } |
7372 | ||
1c6b77e5 | 7373 | if (map_by_addr.size() < 1) |
5f0a03a6 | 7374 | { |
3d372d6b | 7375 | cerr << _F("Symbol table error: %s contains no function symbols.\n", |
b530b5b3 | 7376 | path.c_str()) << endl; |
5f0a03a6 JK |
7377 | return info_absent; |
7378 | } | |
7379 | return info_present; | |
7380 | } | |
7381 | ||
7382 | // NB: This currently unused. We use get_from_elf() instead because | |
7383 | // that gives us raw addresses -- which we need for modules -- whereas | |
7384 | // nm provides the address relative to the beginning of the section. | |
7385 | enum info_status | |
83ca3872 | 7386 | symbol_table::read_from_elf_file(const string &path, |
2713ea24 | 7387 | systemtap_session &sess) |
5f0a03a6 | 7388 | { |
58502ae4 JS |
7389 | vector<string> cmd; |
7390 | cmd.push_back("/usr/bin/nm"); | |
7391 | cmd.push_back("-n"); | |
7392 | cmd.push_back("--defined-only"); | |
7393 | cmd.push_back("path"); | |
7394 | ||
5f0a03a6 | 7395 | FILE *f; |
58502ae4 JS |
7396 | int child_fd; |
7397 | pid_t child = stap_spawn_piped(sess.verbose, cmd, NULL, &child_fd); | |
7398 | if (child <= 0 || !(f = fdopen(child_fd, "r"))) | |
5f0a03a6 | 7399 | { |
58502ae4 | 7400 | // nm failures are detected by stap_waitpid |
3d372d6b | 7401 | cerr << _F("Internal error reading symbol table from %s -- %s\n", |
b530b5b3 | 7402 | path.c_str(), strerror(errno)); |
5f0a03a6 JK |
7403 | return info_absent; |
7404 | } | |
7405 | enum info_status status = read_symbols(f, path); | |
58502ae4 | 7406 | if (fclose(f) || stap_waitpid(sess.verbose, child)) |
5f0a03a6 | 7407 | { |
2713ea24 CM |
7408 | if (status == info_present) |
7409 | sess.print_warning("nm cannot read symbol table from " + path); | |
5f0a03a6 JK |
7410 | return info_absent; |
7411 | } | |
7412 | return status; | |
7413 | } | |
7414 | ||
7415 | enum info_status | |
83ca3872 | 7416 | symbol_table::read_from_text_file(const string& path, |
2713ea24 | 7417 | systemtap_session &sess) |
5f0a03a6 JK |
7418 | { |
7419 | FILE *f = fopen(path.c_str(), "r"); | |
7420 | if (!f) | |
7421 | { | |
2713ea24 | 7422 | sess.print_warning("cannot read symbol table from " + path + " -- " + strerror(errno)); |
5f0a03a6 JK |
7423 | return info_absent; |
7424 | } | |
7425 | enum info_status status = read_symbols(f, path); | |
7426 | (void) fclose(f); | |
7427 | return status; | |
7428 | } | |
7429 | ||
46f7b6be | 7430 | void |
f98c6346 | 7431 | symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused))) |
46f7b6be JK |
7432 | { |
7433 | #ifdef __powerpc__ | |
7434 | /* | |
7435 | * The .opd section contains function descriptors that can look | |
7436 | * just like function entry points. For example, there's a function | |
7437 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
7438 | * Reject all symbols in .opd. | |
7439 | */ | |
7440 | opd_section = SHN_UNDEF; | |
7441 | Dwarf_Addr bias; | |
7442 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
7443 | ?: dwfl_module_getelf (mod, &bias)); | |
7444 | Elf_Scn* scn = 0; | |
7445 | size_t shstrndx; | |
7446 | ||
7447 | if (!elf) | |
7448 | return; | |
fcc30d6d | 7449 | if (elf_getshdrstrndx (elf, &shstrndx) != 0) |
46f7b6be JK |
7450 | return; |
7451 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
7452 | { | |
7453 | GElf_Shdr shdr_mem; | |
7454 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
7455 | if (!shdr) | |
7456 | continue; | |
7457 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
7458 | if (!strcmp(name, ".opd")) | |
7459 | { | |
7460 | opd_section = elf_ndxscn(scn); | |
7461 | return; | |
7462 | } | |
7463 | } | |
7464 | #endif | |
7465 | } | |
7466 | ||
7467 | bool | |
7468 | symbol_table::reject_section(GElf_Word section) | |
7469 | { | |
7470 | if (section == SHN_UNDEF) | |
7471 | return true; | |
7472 | #ifdef __powerpc__ | |
7473 | if (section == opd_section) | |
7474 | return true; | |
7475 | #endif | |
7476 | return false; | |
7477 | } | |
7478 | ||
5f0a03a6 JK |
7479 | enum info_status |
7480 | symbol_table::get_from_elf() | |
7481 | { | |
7482 | Dwarf_Addr high_addr = 0; | |
7483 | Dwfl_Module *mod = mod_info->mod; | |
7484 | int syments = dwfl_module_getsymtab(mod); | |
7485 | assert(syments); | |
46f7b6be | 7486 | prepare_section_rejection(mod); |
5f0a03a6 JK |
7487 | for (int i = 1; i < syments; ++i) |
7488 | { | |
7489 | GElf_Sym sym; | |
ab91b232 JK |
7490 | GElf_Word section; |
7491 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
2867a2a1 | 7492 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC) |
ab91b232 | 7493 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
2867a2a1 | 7494 | reject_section(section), sym.st_value, &high_addr); |
5f0a03a6 JK |
7495 | } |
7496 | return info_present; | |
7497 | } | |
7498 | ||
5f0a03a6 JK |
7499 | func_info * |
7500 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
7501 | { | |
1c6b77e5 JS |
7502 | iterator_t iter = map_by_addr.upper_bound(addr); |
7503 | if (iter == map_by_addr.begin()) | |
5f0a03a6 | 7504 | return NULL; |
2e67a43b | 7505 | else |
1c6b77e5 | 7506 | return (--iter)->second; |
5f0a03a6 JK |
7507 | } |
7508 | ||
3d372d6b SC |
7509 | func_info * |
7510 | symbol_table::get_first_func() | |
7511 | { | |
7512 | iterator_t iter = map_by_addr.begin(); | |
7513 | return (iter)->second; | |
7514 | } | |
7515 | ||
5f0a03a6 JK |
7516 | func_info * |
7517 | symbol_table::lookup_symbol(const string& name) | |
7518 | { | |
7519 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
7520 | if (i == map_by_name.end()) | |
7521 | return NULL; | |
7522 | return i->second; | |
7523 | } | |
7524 | ||
7525 | Dwarf_Addr | |
7526 | symbol_table::lookup_symbol_address(const string& name) | |
7527 | { | |
7528 | func_info *fi = lookup_symbol(name); | |
7529 | if (fi) | |
7530 | return fi->addr; | |
7531 | return 0; | |
7532 | } | |
7533 | ||
ab91b232 JK |
7534 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
7535 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
7536 | // For system calls not implemented elsewhere, this weak symbol shows up | |
7537 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
7538 | // we refuse to consider such symbols. Here we delete them from our | |
7539 | // symbol table. | |
7540 | // TODO: Consider generalizing this and/or making it part of blacklist | |
7541 | // processing. | |
7542 | void | |
7543 | symbol_table::purge_syscall_stubs() | |
7544 | { | |
7545 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
7546 | if (stub_addr == 0) | |
7547 | return; | |
1c6b77e5 | 7548 | range_t purge_range = map_by_addr.equal_range(stub_addr); |
2e67a43b TM |
7549 | for (iterator_t iter = purge_range.first; |
7550 | iter != purge_range.second; | |
1c6b77e5 | 7551 | ) |
ab91b232 | 7552 | { |
1c6b77e5 | 7553 | func_info *fi = iter->second; |
2e67a43b | 7554 | if (fi->weak && fi->name != "sys_ni_syscall") |
ab91b232 | 7555 | { |
2e67a43b | 7556 | map_by_name.erase(fi->name); |
1c6b77e5 | 7557 | map_by_addr.erase(iter++); |
2e67a43b | 7558 | delete fi; |
2e67a43b | 7559 | } |
1c6b77e5 JS |
7560 | else |
7561 | iter++; | |
ab91b232 JK |
7562 | } |
7563 | } | |
7564 | ||
5f0a03a6 | 7565 | void |
d906ab9d | 7566 | module_info::get_symtab(base_query *q) |
5f0a03a6 | 7567 | { |
1c6b77e5 JS |
7568 | if (symtab_status != info_unknown) |
7569 | return; | |
7570 | ||
5f0a03a6 JK |
7571 | sym_table = new symbol_table(this); |
7572 | if (!elf_path.empty()) | |
7573 | { | |
5f0a03a6 JK |
7574 | symtab_status = sym_table->get_from_elf(); |
7575 | } | |
7576 | else | |
7577 | { | |
7578 | assert(name == TOK_KERNEL); | |
ab3ed72d DS |
7579 | symtab_status = info_absent; |
7580 | cerr << _("Error: Cannot find vmlinux.") << endl;; | |
5f0a03a6 JK |
7581 | } |
7582 | if (symtab_status == info_absent) | |
7583 | { | |
7584 | delete sym_table; | |
7585 | sym_table = NULL; | |
7586 | return; | |
7587 | } | |
7588 | ||
ab91b232 JK |
7589 | if (name == TOK_KERNEL) |
7590 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
7591 | } |
7592 | ||
1c6b77e5 JS |
7593 | // update_symtab reconciles data between the elf symbol table and the dwarf |
7594 | // function enumeration. It updates the symbol table entries with the dwarf | |
7595 | // die that describes the function, which also signals to query_module_symtab | |
7596 | // that a statement probe isn't needed. In return, it also adds aliases to the | |
7597 | // function table for names that share the same addr/die. | |
7598 | void | |
7599 | module_info::update_symtab(cu_function_cache_t *funcs) | |
7600 | { | |
7601 | if (!sym_table) | |
7602 | return; | |
7603 | ||
7604 | cu_function_cache_t new_funcs; | |
7605 | ||
7606 | for (cu_function_cache_t::iterator func = funcs->begin(); | |
7607 | func != funcs->end(); func++) | |
7608 | { | |
6059d7c4 JL |
7609 | sym_seen.insert(func->first); |
7610 | ||
1c6b77e5 JS |
7611 | // optimization: inlines will never be in the symbol table |
7612 | if (dwarf_func_inline(&func->second) != 0) | |
7613 | continue; | |
7614 | ||
1ffb8bd1 JS |
7615 | // XXX We may want to make additional efforts to match mangled elf names |
7616 | // to dwarf too. MIPS_linkage_name can help, but that's sometimes | |
7617 | // missing, so we may also need to try matching by address. See also the | |
7618 | // notes about _Z in dwflpp::iterate_over_functions(). | |
7619 | ||
1c6b77e5 JS |
7620 | func_info *fi = sym_table->lookup_symbol(func->first); |
7621 | if (!fi) | |
7622 | continue; | |
7623 | ||
7624 | // iterate over all functions at the same address | |
7625 | symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr); | |
7626 | for (symbol_table::iterator_t it = er.first; it != er.second; ++it) | |
7627 | { | |
7628 | // update this function with the dwarf die | |
7629 | it->second->die = func->second; | |
7630 | ||
7631 | // if this function is a new alias, then | |
7632 | // save it to merge into the function cache | |
7633 | if (it->second != fi) | |
b7478964 | 7634 | new_funcs.insert(make_pair(it->second->name, it->second->die)); |
1c6b77e5 JS |
7635 | } |
7636 | } | |
7637 | ||
7638 | // add all discovered aliases back into the function cache | |
7639 | // NB: this won't replace any names that dwarf may have already found | |
7640 | funcs->insert(new_funcs.begin(), new_funcs.end()); | |
7641 | } | |
7642 | ||
5f0a03a6 JK |
7643 | module_info::~module_info() |
7644 | { | |
7645 | if (sym_table) | |
7646 | delete sym_table; | |
b55bc428 FCE |
7647 | } |
7648 | ||
935447c8 | 7649 | // ------------------------------------------------------------------------ |
888af770 | 7650 | // user-space probes |
935447c8 DS |
7651 | // ------------------------------------------------------------------------ |
7652 | ||
935447c8 | 7653 | |
888af770 | 7654 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> |
935447c8 | 7655 | { |
89ba3085 FCE |
7656 | private: |
7657 | string make_pbm_key (uprobe_derived_probe* p) { | |
cfcab6c7 | 7658 | return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid); |
89ba3085 FCE |
7659 | } |
7660 | ||
cfcab6c7 JS |
7661 | void emit_module_maxuprobes (systemtap_session& s); |
7662 | ||
2b69faaf JS |
7663 | // Using our own utrace-based uprobes |
7664 | void emit_module_utrace_decls (systemtap_session& s); | |
7665 | void emit_module_utrace_init (systemtap_session& s); | |
7666 | void emit_module_utrace_exit (systemtap_session& s); | |
7667 | ||
7668 | // Using the upstream inode-based uprobes | |
7669 | void emit_module_inode_decls (systemtap_session& s); | |
7670 | void emit_module_inode_init (systemtap_session& s); | |
7671 | void emit_module_inode_exit (systemtap_session& s); | |
7672 | ||
3a894f7e JS |
7673 | // Using the dyninst backend (via stapdyn) |
7674 | void emit_module_dyninst_decls (systemtap_session& s); | |
7675 | void emit_module_dyninst_init (systemtap_session& s); | |
7676 | void emit_module_dyninst_exit (systemtap_session& s); | |
7677 | ||
935447c8 | 7678 | public: |
888af770 | 7679 | void emit_module_decls (systemtap_session& s); |
935447c8 DS |
7680 | void emit_module_init (systemtap_session& s); |
7681 | void emit_module_exit (systemtap_session& s); | |
7682 | }; | |
7683 | ||
7684 | ||
888af770 FCE |
7685 | void |
7686 | uprobe_derived_probe::join_group (systemtap_session& s) | |
7687 | { | |
7688 | if (! s.uprobe_derived_probes) | |
7689 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
7690 | s.uprobe_derived_probes->enroll (this); | |
f31a77f5 DS |
7691 | if (s.runtime_usermode_p()) |
7692 | enable_dynprobes(s); | |
7693 | else | |
4441e344 | 7694 | enable_task_finder(s); |
a96d1db0 | 7695 | |
8a03658e | 7696 | // Ask buildrun.cxx to build extra module if needed, and |
d3e959b0 DS |
7697 | // signal staprun to load that module. If we're using the builtin |
7698 | // inode-uprobes, we still need to know that it is required. | |
8a03658e | 7699 | s.need_uprobes = true; |
a96d1db0 DN |
7700 | } |
7701 | ||
888af770 | 7702 | |
c0f84e7b SC |
7703 | void |
7704 | uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const | |
7705 | { | |
7706 | dwarf_derived_probe::getargs(arg_set); | |
7707 | arg_set.insert(arg_set.end(), args.begin(), args.end()); | |
7708 | } | |
7709 | ||
7710 | ||
7711 | void | |
7712 | uprobe_derived_probe::saveargs(int nargs) | |
7713 | { | |
7714 | for (int i = 1; i <= nargs; i++) | |
7715 | args.push_back("$arg" + lex_cast (i) + ":long"); | |
7716 | } | |
7717 | ||
7718 | ||
2865d17a | 7719 | void |
42e38653 | 7720 | uprobe_derived_probe::emit_privilege_assertion (translator_output* o) |
2865d17a DB |
7721 | { |
7722 | // These probes are allowed for unprivileged users, but only in the | |
7723 | // context of processes which they own. | |
7724 | emit_process_owner_assertion (o); | |
7725 | } | |
7726 | ||
7727 | ||
888af770 | 7728 | struct uprobe_builder: public derived_probe_builder |
a96d1db0 | 7729 | { |
888af770 | 7730 | uprobe_builder() {} |
2b69faaf | 7731 | virtual void build(systemtap_session & sess, |
a96d1db0 DN |
7732 | probe * base, |
7733 | probe_point * location, | |
86bf665e | 7734 | literal_map_t const & parameters, |
a96d1db0 DN |
7735 | vector<derived_probe *> & finished_results) |
7736 | { | |
888af770 | 7737 | int64_t process, address; |
a96d1db0 | 7738 | |
2b69faaf | 7739 | if (kernel_supports_inode_uprobes(sess)) |
dc09353a | 7740 | throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]")); |
2b69faaf | 7741 | |
888af770 | 7742 | bool b1 = get_param (parameters, TOK_PROCESS, process); |
ced347a9 | 7743 | (void) b1; |
888af770 | 7744 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 7745 | (void) b2; |
888af770 FCE |
7746 | bool rr = has_null_param (parameters, TOK_RETURN); |
7747 | assert (b1 && b2); // by pattern_root construction | |
a96d1db0 | 7748 | |
0973d815 | 7749 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
a96d1db0 DN |
7750 | } |
7751 | }; | |
7752 | ||
7753 | ||
7754 | void | |
cfcab6c7 | 7755 | uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s) |
a96d1db0 | 7756 | { |
43241c44 FCE |
7757 | // We'll probably need at least this many: |
7758 | unsigned minuprobes = probes.size(); | |
7759 | // .. but we don't want so many that .bss is inflated (PR10507): | |
7760 | unsigned uprobesize = 64; | |
7761 | unsigned maxuprobesmem = 10*1024*1024; // 10 MB | |
7762 | unsigned maxuprobes = maxuprobesmem / uprobesize; | |
7763 | ||
aaf7ffe8 FCE |
7764 | // Let's choose a value on the geometric middle. This should end up |
7765 | // between minuprobes and maxuprobes. It's OK if this number turns | |
7766 | // out to be < minuprobes or > maxuprobes. At worst, we get a | |
7767 | // run-time error of one kind (too few: missed uprobe registrations) | |
7768 | // or another (too many: vmalloc errors at module load time). | |
7769 | unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes); | |
43241c44 | 7770 | |
6d0f3f0c | 7771 | s.op->newline() << "#ifndef MAXUPROBES"; |
43241c44 | 7772 | s.op->newline() << "#define MAXUPROBES " << default_maxuprobes; |
6d0f3f0c | 7773 | s.op->newline() << "#endif"; |
cfcab6c7 JS |
7774 | } |
7775 | ||
7776 | ||
7777 | void | |
7778 | uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s) | |
7779 | { | |
7780 | if (probes.empty()) return; | |
7781 | s.op->newline() << "/* ---- utrace uprobes ---- */"; | |
7782 | // If uprobes isn't in the kernel, pull it in from the runtime. | |
7783 | ||
7784 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; | |
7785 | s.op->newline() << "#include <linux/uprobes.h>"; | |
7786 | s.op->newline() << "#else"; | |
2ba1736a | 7787 | s.op->newline() << "#include \"linux/uprobes/uprobes.h\""; |
cfcab6c7 JS |
7788 | s.op->newline() << "#endif"; |
7789 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; | |
7790 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
7791 | s.op->newline() << "#endif"; | |
7792 | ||
7793 | emit_module_maxuprobes (s); | |
a96d1db0 | 7794 | |
cc52276b | 7795 | // Forward decls |
2ba1736a | 7796 | s.op->newline() << "#include \"linux/uprobes-common.h\""; |
cc52276b | 7797 | |
5e112f92 FCE |
7798 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
7799 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
cc52276b WC |
7800 | // XXX: consider a slab cache or somesuch for stap_uprobes |
7801 | s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];"; | |
6b378b7c | 7802 | s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
a96d1db0 | 7803 | |
89ba3085 FCE |
7804 | s.op->assert_0_indent(); |
7805 | ||
89ba3085 FCE |
7806 | // Assign task-finder numbers as we build up the stap_uprobe_tf table. |
7807 | // This means we process probes[] in two passes. | |
7808 | map <string,unsigned> module_index; | |
7809 | unsigned module_index_ctr = 0; | |
7810 | ||
cc52276b WC |
7811 | // not const since embedded task_finder_target struct changes |
7812 | s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {"; | |
89ba3085 FCE |
7813 | s.op->indent(1); |
7814 | for (unsigned i=0; i<probes.size(); i++) | |
7815 | { | |
7816 | uprobe_derived_probe *p = probes[i]; | |
7817 | string pbmkey = make_pbm_key (p); | |
7818 | if (module_index.find (pbmkey) == module_index.end()) | |
7819 | { | |
7820 | module_index[pbmkey] = module_index_ctr++; | |
7821 | ||
7822 | s.op->newline() << "{"; | |
7823 | // NB: it's essential that make_pbm_key() use all of and | |
7824 | // only the same fields as we're about to emit. | |
7825 | s.op->line() << " .finder={"; | |
1af100fc | 7826 | s.op->line() << " .purpose=\"uprobes\","; |
89ba3085 | 7827 | if (p->pid != 0) |
68910c97 JK |
7828 | s.op->line() << " .pid=" << p->pid << ","; |
7829 | ||
7830 | if (p->section == "") // .statement(addr).absolute | |
7831 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
89ba3085 FCE |
7832 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program |
7833 | { | |
7834 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
7835 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
7836 | } | |
68910c97 | 7837 | else if (p->section != ".absolute") // ET_DYN |
89ba3085 | 7838 | { |
4ad95bbc SC |
7839 | if (p->has_library) |
7840 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
89ba3085 FCE |
7841 | s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, "; |
7842 | s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, "; | |
19d91f6c | 7843 | s.op->line() << " .callback=&stap_uprobe_process_munmap,"; |
89ba3085 | 7844 | } |
89ba3085 | 7845 | s.op->line() << " },"; |
68910c97 JK |
7846 | if (p->module != "") |
7847 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
89ba3085 FCE |
7848 | s.op->line() << " },"; |
7849 | } | |
c57ea854 | 7850 | else |
822a6a3d | 7851 | { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it. |
89ba3085 FCE |
7852 | } |
7853 | s.op->newline(-1) << "};"; | |
7854 | ||
7855 | s.op->assert_0_indent(); | |
7856 | ||
3689db05 SC |
7857 | unsigned pci; |
7858 | for (pci=0; pci<probes.size(); pci++) | |
7859 | { | |
7860 | // List of perf counters used by each probe | |
7861 | // This list is an index into struct stap_perf_probe, | |
7862 | uprobe_derived_probe *p = probes[pci]; | |
698de6cc | 7863 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
7864 | s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {"; |
7865 | for (pcii = p->perf_counter_refs.begin(); | |
7866 | pcii != p->perf_counter_refs.end(); pcii++) | |
7867 | { | |
7868 | map<string, pair<string,derived_probe*> >::iterator it; | |
7869 | unsigned i = 0; | |
7870 | // Find the associated perf.counter probe | |
7871 | for (it=s.perf_counters.begin() ; | |
7872 | it != s.perf_counters.end(); it++, i++) | |
7873 | if ((*it).second.second == (*pcii)) | |
7874 | break; | |
7875 | s.op->line() << lex_cast(i) << ", "; | |
7876 | } | |
7877 | s.op->newline() << "};"; | |
7878 | } | |
7879 | ||
cc52276b WC |
7880 | // NB: read-only structure |
7881 | s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {"; | |
a96d1db0 | 7882 | s.op->indent(1); |
888af770 FCE |
7883 | for (unsigned i =0; i<probes.size(); i++) |
7884 | { | |
7885 | uprobe_derived_probe* p = probes[i]; | |
7886 | s.op->newline() << "{"; | |
89ba3085 FCE |
7887 | string key = make_pbm_key (p); |
7888 | unsigned value = module_index[key]; | |
759e1d76 FCE |
7889 | if (value != 0) |
7890 | s.op->line() << " .tfi=" << value << ","; | |
6b66b9f7 | 7891 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
faea5e16 | 7892 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
4ddb6dd0 | 7893 | |
038c38c6 | 7894 | if (p->sdt_semaphore_addr != 0) |
63b4fd14 | 7895 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" |
038c38c6 | 7896 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; |
4ddb6dd0 | 7897 | |
0d049a1d | 7898 | // XXX: don't bother emit if array is empty |
3689db05 SC |
7899 | s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),"; |
7900 | // List of perf counters used by a probe from above | |
0d049a1d | 7901 | s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ","; |
3689db05 | 7902 | |
4ddb6dd0 JS |
7903 | if (p->has_return) |
7904 | s.op->line() << " .return_p=1,"; | |
888af770 FCE |
7905 | s.op->line() << " },"; |
7906 | } | |
7907 | s.op->newline(-1) << "};"; | |
a96d1db0 | 7908 | |
89ba3085 FCE |
7909 | s.op->assert_0_indent(); |
7910 | ||
48e685da | 7911 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 7912 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
89ba3085 | 7913 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
71db462b | 7914 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe", |
cda141c2 | 7915 | "stp_probe_type_uprobe"); |
0e090c74 | 7916 | s.op->newline() << "if (sup->spec_index < 0 || " |
6e895029 DS |
7917 | << "sup->spec_index >= " << probes.size() << ") {"; |
7918 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 7919 | << "): %s\", sup->spec_index, c->probe_point);"; |
6e895029 DS |
7920 | s.op->newline() << "atomic_dec (&c->busy);"; |
7921 | s.op->newline() << "goto probe_epilogue;"; | |
7922 | s.op->newline(-1) << "}"; | |
d9aed31e | 7923 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 7924 | s.op->newline() << "c->user_mode_p = 1;"; |
6415ddde MW |
7925 | |
7926 | // Make it look like the IP is set as it would in the actual user | |
7927 | // task when calling real probe handler. Reset IP regs on return, so | |
7928 | // we don't confuse uprobes. PR10458 | |
7929 | s.op->newline() << "{"; | |
7930 | s.op->indent(1); | |
d9aed31e | 7931 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
259d54c0 | 7932 | s.op->newline() << "SET_REG_IP(regs, inst->vaddr);"; |
26e63673 | 7933 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 7934 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
7935 | s.op->newline(-1) << "}"; |
7936 | ||
f887a8c9 | 7937 | common_probe_entryfn_epilogue (s, true); |
888af770 | 7938 | s.op->newline(-1) << "}"; |
a96d1db0 | 7939 | |
48e685da | 7940 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 7941 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
89ba3085 | 7942 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
71db462b | 7943 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe", |
cda141c2 | 7944 | "stp_probe_type_uretprobe"); |
6dceb5c9 | 7945 | s.op->newline() << "c->ips.ri = inst;"; |
0e090c74 | 7946 | s.op->newline() << "if (sup->spec_index < 0 || " |
0d5561a5 DS |
7947 | << "sup->spec_index >= " << probes.size() << ") {"; |
7948 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 7949 | << "): %s\", sup->spec_index, c->probe_point);"; |
0d5561a5 DS |
7950 | s.op->newline() << "atomic_dec (&c->busy);"; |
7951 | s.op->newline() << "goto probe_epilogue;"; | |
7952 | s.op->newline(-1) << "}"; | |
7953 | ||
d9aed31e | 7954 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 7955 | s.op->newline() << "c->user_mode_p = 1;"; |
6415ddde MW |
7956 | |
7957 | // Make it look like the IP is set as it would in the actual user | |
7958 | // task when calling real probe handler. Reset IP regs on return, so | |
7959 | // we don't confuse uprobes. PR10458 | |
7960 | s.op->newline() << "{"; | |
7961 | s.op->indent(1); | |
d9aed31e | 7962 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
5e562a69 | 7963 | s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);"; |
26e63673 | 7964 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 7965 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
7966 | s.op->newline(-1) << "}"; |
7967 | ||
f887a8c9 | 7968 | common_probe_entryfn_epilogue (s, true); |
a96d1db0 DN |
7969 | s.op->newline(-1) << "}"; |
7970 | ||
89ba3085 | 7971 | s.op->newline(); |
2ba1736a | 7972 | s.op->newline() << "#include \"linux/uprobes-common.c\""; |
6d0f3f0c | 7973 | s.op->newline(); |
888af770 | 7974 | } |
935447c8 DS |
7975 | |
7976 | ||
888af770 | 7977 | void |
2b69faaf | 7978 | uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s) |
935447c8 | 7979 | { |
888af770 | 7980 | if (probes.empty()) return; |
935447c8 | 7981 | |
2b69faaf | 7982 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
935447c8 | 7983 | |
01b05e2e | 7984 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7985 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7986 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
7987 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
7988 | // initialized to zero. This is so that we can use | |
7989 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
7990 | s.op->newline(-1) << "}"; |
7991 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
935447c8 | 7992 | |
89ba3085 FCE |
7993 | // Set up the task_finders |
7994 | s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {"; | |
7995 | s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];"; | |
c57ea854 | 7996 | s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better |
89ba3085 | 7997 | s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);"; |
935447c8 | 7998 | |
5e112f92 FCE |
7999 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any |
8000 | // finders already registered, since mere registration does not | |
8001 | // cause any utrace or memory allocation actions. That happens only | |
8002 | // later, once the task finder engine starts running. So, for a | |
8003 | // partial initialization requiring unwind, we need do nothing. | |
8004 | s.op->newline() << "if (rc) break;"; | |
a7a68293 | 8005 | |
888af770 FCE |
8006 | s.op->newline(-1) << "}"; |
8007 | } | |
d0ea46ce | 8008 | |
d0a7f5a9 | 8009 | |
888af770 | 8010 | void |
2b69faaf | 8011 | uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s) |
888af770 FCE |
8012 | { |
8013 | if (probes.empty()) return; | |
2b69faaf | 8014 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
e56e51c9 | 8015 | |
6d0f3f0c FCE |
8016 | // NB: there is no stap_unregister_task_finder_target call; |
8017 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
8018 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
8019 | // | |
8020 | // This function blocks until all callbacks are completed, so there | |
8021 | // is supposed to be no possibility of any registration-related code starting | |
8022 | // to run in parallel with our shutdown here. So we don't need to protect the | |
8023 | // stap_uprobes[] array with the mutex. | |
d0a7f5a9 | 8024 | |
01b05e2e | 8025 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 | 8026 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
89ba3085 | 8027 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6d0f3f0c | 8028 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd | 8029 | |
8faa1fc5 | 8030 | // PR10655: decrement that ENABLED semaphore |
c116c31b | 8031 | s.op->newline() << "if (sup->sdt_sem_address) {"; |
8faa1fc5 FCE |
8032 | s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size |
8033 | s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);"; | |
8034 | s.op->newline() << "struct task_struct *tsk;"; | |
8035 | s.op->newline() << "rcu_read_lock();"; | |
6846cfc8 | 8036 | |
86229a55 DS |
8037 | // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes |
8038 | // that the pid is always in the global namespace, not in any | |
8039 | // private namespace. | |
8faa1fc5 | 8040 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)"; |
86229a55 DS |
8041 | // We'd like to call find_task_by_pid_ns() here, but it isn't |
8042 | // exported. So, we call what it calls... | |
8043 | s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);"; | |
8faa1fc5 FCE |
8044 | s.op->newline() << "#else"; |
8045 | s.op->newline() << " tsk = find_task_by_pid (pid);"; | |
8046 | s.op->newline() << "#endif /* 2.6.24 */"; | |
8faa1fc5 FCE |
8047 | |
8048 | s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching | |
3c5b8e2b | 8049 | s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {"; |
63b4fd14 | 8050 | s.op->newline(1) << "sdt_semaphore --;"; |
903b9fcd | 8051 | s.op->newline() << "#ifdef DEBUG_UPROBES"; |
c116c31b | 8052 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);"; |
903b9fcd | 8053 | s.op->newline() << "#endif"; |
3c5b8e2b | 8054 | s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);"; |
93c84191 | 8055 | s.op->newline(-1) << "}"; |
8faa1fc5 FCE |
8056 | // XXX: need to analyze possibility of race condition |
8057 | s.op->newline(-1) << "}"; | |
8058 | s.op->newline() << "rcu_read_unlock();"; | |
8059 | s.op->newline(-1) << "}"; | |
6846cfc8 | 8060 | |
3568f1dd FCE |
8061 | s.op->newline() << "if (sups->return_p) {"; |
8062 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
89ba3085 | 8063 | 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 | 8064 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
8065 | // NB: PR6829 does not change that we still need to unregister at |
8066 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
8067 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
8068 | s.op->newline(-1) << "} else {"; | |
8069 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
8faa1fc5 | 8070 | 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 |
8071 | s.op->newline() << "#endif"; |
8072 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
8073 | s.op->newline(-1) << "}"; | |
935447c8 | 8074 | |
6d0f3f0c | 8075 | s.op->newline() << "sup->spec_index = -1;"; |
935447c8 | 8076 | |
3568f1dd FCE |
8077 | // XXX: uprobe missed counts? |
8078 | ||
6d0f3f0c | 8079 | s.op->newline(-1) << "}"; |
935447c8 | 8080 | |
5e112f92 | 8081 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; |
935447c8 DS |
8082 | } |
8083 | ||
2b69faaf JS |
8084 | |
8085 | void | |
8086 | uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s) | |
8087 | { | |
8088 | if (probes.empty()) return; | |
8089 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 8090 | emit_module_maxuprobes (s); |
2ba1736a | 8091 | s.op->newline() << "#include \"linux/uprobes-inode.c\""; |
2b69faaf JS |
8092 | |
8093 | // Write the probe handler. | |
79af55c3 JS |
8094 | s.op->newline() << "static int stapiu_probe_handler " |
8095 | << "(struct stapiu_consumer *sup, struct pt_regs *regs) {"; | |
8096 | s.op->newline(1); | |
2dbbd473 JS |
8097 | |
8098 | // Since we're sharing the entry function, we have to dynamically choose the probe_type | |
8099 | string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)"; | |
71db462b | 8100 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe", |
2dbbd473 JS |
8101 | probe_type); |
8102 | ||
3bff6634 | 8103 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 8104 | s.op->newline() << "c->user_mode_p = 1;"; |
497cfcb2 | 8105 | // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c |
2b69faaf | 8106 | s.op->newline() << "(*sup->probe->ph) (c);"; |
39e63bae | 8107 | |
f887a8c9 | 8108 | common_probe_entryfn_epilogue (s, true); |
2b69faaf JS |
8109 | s.op->newline() << "return 0;"; |
8110 | s.op->newline(-1) << "}"; | |
8111 | s.op->assert_0_indent(); | |
8112 | ||
8113 | // Index of all the modules for which we need inodes. | |
8114 | map<string, unsigned> module_index; | |
8115 | unsigned module_index_ctr = 0; | |
8116 | ||
8117 | // Discover and declare targets for each unique path. | |
cfcab6c7 | 8118 | s.op->newline() << "static struct stapiu_target " |
2b69faaf JS |
8119 | << "stap_inode_uprobe_targets[] = {"; |
8120 | s.op->indent(1); | |
8121 | for (unsigned i=0; i<probes.size(); i++) | |
8122 | { | |
8123 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
8124 | const string key = make_pbm_key(p); |
8125 | if (module_index.find (key) == module_index.end()) | |
2b69faaf | 8126 | { |
cfcab6c7 JS |
8127 | module_index[key] = module_index_ctr++; |
8128 | s.op->newline() << "{"; | |
8129 | s.op->line() << " .finder={"; | |
1af100fc | 8130 | s.op->line() << " .purpose=\"inode-uprobes\","; |
cfcab6c7 JS |
8131 | if (p->pid != 0) |
8132 | s.op->line() << " .pid=" << p->pid << ","; | |
8133 | ||
8134 | if (p->section == "") // .statement(addr).absolute XXX? | |
8135 | s.op->line() << " .callback=&stapiu_process_found,"; | |
8136 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program | |
8137 | { | |
8138 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
8139 | s.op->line() << " .callback=&stapiu_process_found,"; | |
8140 | } | |
8141 | else if (p->section != ".absolute") // ET_DYN | |
8142 | { | |
8143 | if (p->has_library) | |
8144 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
8145 | s.op->line() << " .mmap_callback=&stapiu_mmap_found, "; | |
8146 | s.op->line() << " .munmap_callback=&stapiu_munmap_found, "; | |
8147 | s.op->line() << " .callback=&stapiu_process_munmap,"; | |
8148 | } | |
8149 | s.op->line() << " },"; | |
8150 | s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ","; | |
8151 | s.op->line() << " },"; | |
2b69faaf JS |
8152 | } |
8153 | } | |
8154 | s.op->newline(-1) << "};"; | |
8155 | s.op->assert_0_indent(); | |
8156 | ||
8157 | // Declare the actual probes. | |
3689db05 SC |
8158 | unsigned pci; |
8159 | for (pci=0; pci<probes.size(); pci++) | |
8160 | { | |
8161 | // List of perf counters used by each probe | |
8162 | // This list is an index into struct stap_perf_probe, | |
8163 | uprobe_derived_probe *p = probes[pci]; | |
698de6cc | 8164 | std::set<derived_probe*>::iterator pcii; |
3689db05 SC |
8165 | s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {"; |
8166 | for (pcii = p->perf_counter_refs.begin(); | |
8167 | pcii != p->perf_counter_refs.end(); pcii++) | |
8168 | { | |
8169 | map<string, pair<string,derived_probe*> >::iterator it; | |
8170 | unsigned i = 0; | |
8171 | // Find the associated perf.counter probe | |
4fa83377 SC |
8172 | for (it=s.perf_counters.begin() ; |
8173 | it != s.perf_counters.end(); it++, i++) | |
3689db05 SC |
8174 | if ((*it).second.second == (*pcii)) |
8175 | break; | |
8176 | s.op->line() << lex_cast(i) << ", "; | |
8177 | } | |
8178 | s.op->newline() << "};"; | |
8179 | } | |
8180 | ||
cfcab6c7 | 8181 | s.op->newline() << "static struct stapiu_consumer " |
2b69faaf JS |
8182 | << "stap_inode_uprobe_consumers[] = {"; |
8183 | s.op->indent(1); | |
8184 | for (unsigned i=0; i<probes.size(); i++) | |
8185 | { | |
8186 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
8187 | unsigned index = module_index[make_pbm_key(p)]; |
8188 | s.op->newline() << "{"; | |
79af55c3 JS |
8189 | if (p->has_return) |
8190 | s.op->line() << " .return_p=1,"; | |
cfcab6c7 JS |
8191 | s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],"; |
8192 | s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,"; | |
8193 | if (p->sdt_semaphore_addr) | |
8194 | s.op->line() << " .sdt_sem_offset=(loff_t)0x" | |
8195 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; | |
0d049a1d | 8196 | // XXX: don't bother emit if array is empty |
3689db05 SC |
8197 | s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),"; |
8198 | // List of perf counters used by a probe from above | |
0d049a1d | 8199 | s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ","; |
cfcab6c7 JS |
8200 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
8201 | s.op->line() << " },"; | |
2b69faaf JS |
8202 | } |
8203 | s.op->newline(-1) << "};"; | |
8204 | s.op->assert_0_indent(); | |
8205 | } | |
8206 | ||
8207 | ||
8208 | void | |
8209 | uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s) | |
8210 | { | |
8211 | if (probes.empty()) return; | |
8212 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
b0f614a0 DS |
8213 | // Let stapiu_init() handle reporting errors by setting probe_point |
8214 | // to NULL. | |
8215 | s.op->newline() << "probe_point = NULL;"; | |
cfcab6c7 | 8216 | s.op->newline() << "rc = stapiu_init (" |
2b69faaf JS |
8217 | << "stap_inode_uprobe_targets, " |
8218 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
8219 | << "stap_inode_uprobe_consumers, " | |
8220 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
8221 | } | |
8222 | ||
8223 | ||
8224 | void | |
8225 | uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s) | |
8226 | { | |
8227 | if (probes.empty()) return; | |
8228 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 8229 | s.op->newline() << "stapiu_exit (" |
2b69faaf JS |
8230 | << "stap_inode_uprobe_targets, " |
8231 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
8232 | << "stap_inode_uprobe_consumers, " | |
8233 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
8234 | } | |
8235 | ||
8236 | ||
3a894f7e JS |
8237 | void |
8238 | uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s) | |
8239 | { | |
8240 | if (probes.empty()) return; | |
8241 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
8242 | emit_module_maxuprobes (s); | |
e00f3fb7 | 8243 | s.op->newline() << "#include \"dyninst/uprobes.h\""; |
3a894f7e | 8244 | |
f31a77f5 DS |
8245 | // Let the dynprobe_derived_probe_group handle outputting targets |
8246 | // and probes. This allows us to merge different types of probes. | |
8247 | s.op->newline() << "static struct stapdu_probe stapdu_probes[];"; | |
8248 | for (unsigned i = 0; i < probes.size(); i++) | |
3a894f7e JS |
8249 | { |
8250 | uprobe_derived_probe *p = probes[i]; | |
e00f3fb7 | 8251 | |
f31a77f5 DS |
8252 | dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr, |
8253 | (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"), | |
8254 | common_probe_init(p)); | |
3a894f7e | 8255 | } |
874d38bf JS |
8256 | // loc2c-generated code assumes pt_regs are available, so use this to make |
8257 | // sure we always have *something* for it to dereference... | |
f31a77f5 | 8258 | s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;"; |
e00f3fb7 | 8259 | |
3a894f7e JS |
8260 | // Write the probe handler. |
8261 | // NB: not static, so dyninst can find it | |
8262 | s.op->newline() << "int enter_dyninst_uprobe " | |
8263 | << "(uint64_t index, struct pt_regs *regs) {"; | |
e00f3fb7 | 8264 | s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];"; |
92b97104 JS |
8265 | |
8266 | // Since we're sharing the entry function, we have to dynamically choose the probe_type | |
8267 | string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?" | |
8268 | " stp_probe_type_uretprobe : stp_probe_type_uprobe)"; | |
71db462b | 8269 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe", |
92b97104 JS |
8270 | probe_type); |
8271 | ||
874d38bf | 8272 | s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;"; |
e04b5d74 | 8273 | s.op->newline() << "c->user_mode_p = 1;"; |
3a894f7e JS |
8274 | // XXX: once we have regs, check how dyninst sets the IP |
8275 | // XXX: the way that dyninst rewrites stuff is probably going to be | |
8276 | // ... very confusing to our backtracer (at least if we stay in process) | |
8277 | s.op->newline() << "(*sup->probe->ph) (c);"; | |
f887a8c9 | 8278 | common_probe_entryfn_epilogue (s, true); |
3a894f7e JS |
8279 | s.op->newline() << "return 0;"; |
8280 | s.op->newline(-1) << "}"; | |
3debb935 | 8281 | s.op->newline() << "#include \"dyninst/uprobes-regs.c\""; |
3a894f7e JS |
8282 | s.op->assert_0_indent(); |
8283 | } | |
8284 | ||
8285 | ||
8286 | void | |
8287 | uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s) | |
8288 | { | |
8289 | if (probes.empty()) return; | |
8290 | ||
8291 | /* stapdyn handles the dirty work via dyninst */ | |
8292 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
8293 | s.op->newline() << "/* this section left intentionally blank */"; | |
8294 | } | |
8295 | ||
8296 | ||
8297 | void | |
8298 | uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s) | |
8299 | { | |
8300 | if (probes.empty()) return; | |
8301 | ||
8302 | /* stapdyn handles the dirty work via dyninst */ | |
8303 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
8304 | s.op->newline() << "/* this section left intentionally blank */"; | |
8305 | } | |
8306 | ||
8307 | ||
2b69faaf JS |
8308 | void |
8309 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8310 | { | |
ac3af990 | 8311 | if (s.runtime_usermode_p()) |
4441e344 JS |
8312 | emit_module_dyninst_decls (s); |
8313 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
8314 | emit_module_inode_decls (s); |
8315 | else | |
8316 | emit_module_utrace_decls (s); | |
8317 | } | |
8318 | ||
8319 | ||
8320 | void | |
8321 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
8322 | { | |
ac3af990 | 8323 | if (s.runtime_usermode_p()) |
4441e344 JS |
8324 | emit_module_dyninst_init (s); |
8325 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
8326 | emit_module_inode_init (s); |
8327 | else | |
8328 | emit_module_utrace_init (s); | |
8329 | } | |
8330 | ||
8331 | ||
8332 | void | |
8333 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8334 | { | |
ac3af990 | 8335 | if (s.runtime_usermode_p()) |
4441e344 JS |
8336 | emit_module_dyninst_exit (s); |
8337 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
8338 | emit_module_inode_exit (s); |
8339 | else | |
8340 | emit_module_utrace_exit (s); | |
8341 | } | |
8342 | ||
8343 | ||
e6fe60e7 AM |
8344 | // ------------------------------------------------------------------------ |
8345 | // Kprobe derived probes | |
8346 | // ------------------------------------------------------------------------ | |
8347 | ||
4627ed58 | 8348 | static const string TOK_KPROBE("kprobe"); |
935447c8 | 8349 | |
bae55db9 | 8350 | struct kprobe_derived_probe: public derived_probe |
d0ea46ce | 8351 | { |
23dc94f6 DS |
8352 | kprobe_derived_probe (systemtap_session& sess, |
8353 | vector<derived_probe *> & results, | |
8354 | probe *base, | |
bae55db9 JS |
8355 | probe_point *location, |
8356 | const string& name, | |
8357 | int64_t stmt_addr, | |
8358 | bool has_return, | |
8359 | bool has_statement, | |
8360 | bool has_maxactive, | |
b642c901 SC |
8361 | bool has_path, |
8362 | bool has_library, | |
8363 | long maxactive_val, | |
8364 | const string& path, | |
8365 | const string& library | |
bae55db9 JS |
8366 | ); |
8367 | string symbol_name; | |
8368 | Dwarf_Addr addr; | |
8369 | bool has_return; | |
8370 | bool has_statement; | |
8371 | bool has_maxactive; | |
b642c901 SC |
8372 | bool has_path; |
8373 | bool has_library; | |
bae55db9 | 8374 | long maxactive_val; |
b642c901 SC |
8375 | string path; |
8376 | string library; | |
bae55db9 JS |
8377 | bool access_var; |
8378 | void printsig (std::ostream &o) const; | |
8379 | void join_group (systemtap_session& s); | |
8380 | }; | |
d0ea46ce | 8381 | |
bae55db9 JS |
8382 | struct kprobe_derived_probe_group: public derived_probe_group |
8383 | { | |
8384 | private: | |
8385 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
8386 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
d0ea46ce | 8387 | |
bae55db9 JS |
8388 | public: |
8389 | void enroll (kprobe_derived_probe* probe); | |
8390 | void emit_module_decls (systemtap_session& s); | |
8391 | void emit_module_init (systemtap_session& s); | |
8392 | void emit_module_exit (systemtap_session& s); | |
8393 | }; | |
d0ea46ce | 8394 | |
23dc94f6 DS |
8395 | struct kprobe_var_expanding_visitor: public var_expanding_visitor |
8396 | { | |
8397 | systemtap_session& sess; | |
8398 | block *add_block; | |
8399 | block *add_call_probe; // synthesized from .return probes with saved $vars | |
8400 | bool add_block_tid, add_call_probe_tid; | |
bd5b25e1 | 8401 | bool has_return; |
23dc94f6 | 8402 | |
bd5b25e1 | 8403 | kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return): |
23dc94f6 | 8404 | sess(sess), add_block(NULL), add_call_probe(NULL), |
bd5b25e1 JS |
8405 | add_block_tid(false), add_call_probe_tid(false), |
8406 | has_return(has_return) {} | |
23dc94f6 DS |
8407 | |
8408 | void visit_entry_op (entry_op* e); | |
8409 | }; | |
8410 | ||
8411 | ||
8412 | kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess, | |
8413 | vector<derived_probe *> & results, | |
8414 | probe *base, | |
e6fe60e7 | 8415 | probe_point *location, |
b6371390 | 8416 | const string& name, |
e6fe60e7 | 8417 | int64_t stmt_addr, |
b6371390 JS |
8418 | bool has_return, |
8419 | bool has_statement, | |
8420 | bool has_maxactive, | |
b642c901 SC |
8421 | bool has_path, |
8422 | bool has_library, | |
8423 | long maxactive_val, | |
8424 | const string& path, | |
8425 | const string& library | |
b6371390 | 8426 | ): |
4c5d1300 | 8427 | derived_probe (base, location, true /* .components soon rewritten */ ), |
e6fe60e7 | 8428 | symbol_name (name), addr (stmt_addr), |
b6371390 | 8429 | has_return (has_return), has_statement (has_statement), |
b642c901 SC |
8430 | has_maxactive (has_maxactive), has_path (has_path), |
8431 | has_library (has_library), | |
8432 | maxactive_val (maxactive_val), | |
8433 | path (path), library (library) | |
e6fe60e7 AM |
8434 | { |
8435 | this->tok = base->tok; | |
8436 | this->access_var = false; | |
d0ea46ce | 8437 | |
e6fe60e7 AM |
8438 | #ifndef USHRT_MAX |
8439 | #define USHRT_MAX 32767 | |
8440 | #endif | |
d0ea46ce | 8441 | |
46856d8d JS |
8442 | // Expansion of $target variables in the probe body produces an error during |
8443 | // translate phase, since we're not using debuginfo | |
d0ea46ce | 8444 | |
e6fe60e7 | 8445 | vector<probe_point::component*> comps; |
46856d8d | 8446 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 8447 | |
46856d8d JS |
8448 | if (has_statement) |
8449 | { | |
9ea68eb9 JS |
8450 | comps.push_back (new probe_point::component(TOK_STATEMENT, |
8451 | new literal_number(addr, true))); | |
46856d8d JS |
8452 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); |
8453 | } | |
8454 | else | |
8455 | { | |
8456 | size_t pos = name.find(':'); | |
8457 | if (pos != string::npos) | |
d0ea46ce | 8458 | { |
46856d8d JS |
8459 | string module = name.substr(0, pos); |
8460 | string function = name.substr(pos + 1); | |
8461 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
8462 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
8463 | } | |
8464 | else | |
8465 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 8466 | } |
d0ea46ce | 8467 | |
b6371390 JS |
8468 | if (has_return) |
8469 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
8470 | if (has_maxactive) | |
8471 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d0ea46ce | 8472 | |
bd5b25e1 | 8473 | kprobe_var_expanding_visitor v (sess, has_return); |
23dc94f6 DS |
8474 | v.replace (this->body); |
8475 | ||
8476 | // If during target-variable-expanding the probe, we added a new block | |
8477 | // of code, add it to the start of the probe. | |
8478 | if (v.add_block) | |
8479 | this->body = new block(v.add_block, this->body); | |
8480 | ||
8481 | // If when target-variable-expanding the probe, we need to | |
8482 | // synthesize a sibling function-entry probe. We don't go through | |
8483 | // the whole probe derivation business (PR10642) that could lead to | |
8484 | // wildcard/alias resolution, or for that dwarf-induced duplication. | |
8485 | if (v.add_call_probe) | |
8486 | { | |
8487 | assert (has_return); | |
8488 | ||
8489 | // We temporarily replace base. | |
8490 | statement* old_body = base->body; | |
8491 | base->body = v.add_call_probe; | |
8492 | ||
8493 | derived_probe *entry_handler | |
8494 | = new kprobe_derived_probe (sess, results, base, location, name, 0, | |
8495 | false, has_statement, has_maxactive, | |
8496 | has_path, has_library, maxactive_val, | |
8497 | path, library); | |
8498 | results.push_back (entry_handler); | |
8499 | ||
8500 | base->body = old_body; | |
8501 | } | |
8502 | ||
e6fe60e7 AM |
8503 | this->sole_location()->components = comps; |
8504 | } | |
d0ea46ce | 8505 | |
e6fe60e7 AM |
8506 | void kprobe_derived_probe::printsig (ostream& o) const |
8507 | { | |
8508 | sole_location()->print (o); | |
8509 | o << " /* " << " name = " << symbol_name << "*/"; | |
8510 | printsig_nested (o); | |
8511 | } | |
d0ea46ce | 8512 | |
e6fe60e7 AM |
8513 | void kprobe_derived_probe::join_group (systemtap_session& s) |
8514 | { | |
d0ea46ce | 8515 | |
e6fe60e7 AM |
8516 | if (! s.kprobe_derived_probes) |
8517 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
8518 | s.kprobe_derived_probes->enroll (this); | |
d0ea46ce | 8519 | |
e6fe60e7 | 8520 | } |
d0ea46ce | 8521 | |
e6fe60e7 AM |
8522 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) |
8523 | { | |
8524 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
8525 | // probes of same symbol should share single kprobe/kretprobe | |
8526 | } | |
d0ea46ce | 8527 | |
e6fe60e7 AM |
8528 | void |
8529 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8530 | { | |
8531 | if (probes_by_module.empty()) return; | |
d0ea46ce | 8532 | |
e6fe60e7 | 8533 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; |
d0ea46ce | 8534 | |
e6fe60e7 AM |
8535 | // Warn of misconfigured kernels |
8536 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
8537 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
8538 | s.op->newline() << "#endif"; | |
8539 | s.op->newline(); | |
d0ea46ce | 8540 | |
f07c3b68 | 8541 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 8542 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
8543 | s.op->newline() << "#endif"; |
8544 | ||
e6fe60e7 | 8545 | // Forward declare the master entry functions |
88747011 | 8546 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 8547 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 8548 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 | 8549 | s.op->line() << " struct pt_regs *regs);"; |
d0ea46ce | 8550 | |
e6fe60e7 AM |
8551 | // Emit an array of kprobe/kretprobe pointers |
8552 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
c9116e99 | 8553 | s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];"; |
e6fe60e7 | 8554 | s.op->newline() << "#endif"; |
d0ea46ce | 8555 | |
e6fe60e7 | 8556 | // Emit the actual probe list. |
d0ea46ce | 8557 | |
e6fe60e7 AM |
8558 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; |
8559 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
8560 | s.op->newline() << "#ifdef __ia64__"; | |
8561 | s.op->newline() << "struct kprobe dummy;"; | |
8562 | s.op->newline() << "#endif"; | |
8563 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
8564 | // NB: bss! | |
d0ea46ce | 8565 | |
e6fe60e7 AM |
8566 | s.op->newline() << "static struct stap_dwarfless_probe {"; |
8567 | s.op->newline(1) << "const unsigned return_p:1;"; | |
8568 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 8569 | s.op->newline() << "const unsigned optional_p:1;"; |
e6fe60e7 AM |
8570 | s.op->newline() << "unsigned registered_p:1;"; |
8571 | s.op->newline() << "const unsigned short maxactive_val;"; | |
935447c8 | 8572 | |
e6fe60e7 AM |
8573 | // Function Names are mostly small and uniform enough to justify putting |
8574 | // char[MAX]'s into the array instead of relocated char*'s. | |
935447c8 | 8575 | |
faea5e16 JS |
8576 | size_t symbol_string_name_max = 0; |
8577 | size_t symbol_string_name_tot = 0; | |
e6fe60e7 | 8578 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
6270adc1 | 8579 | { |
e6fe60e7 AM |
8580 | kprobe_derived_probe* p = it->second; |
8581 | #define DOIT(var,expr) do { \ | |
8582 | size_t var##_size = (expr) + 1; \ | |
8583 | var##_max = max (var##_max, var##_size); \ | |
8584 | var##_tot += var##_size; } while (0) | |
e6fe60e7 AM |
8585 | DOIT(symbol_string_name, p->symbol_name.size()); |
8586 | #undef DOIT | |
6270adc1 MH |
8587 | } |
8588 | ||
e6fe60e7 AM |
8589 | #define CALCIT(var) \ |
8590 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
935447c8 | 8591 | |
e6fe60e7 AM |
8592 | CALCIT(symbol_string); |
8593 | #undef CALCIT | |
6270adc1 | 8594 | |
bd659351 | 8595 | s.op->newline() << "unsigned long address;"; |
7c3e97f4 | 8596 | s.op->newline() << "const struct stap_probe * const probe;"; |
e6fe60e7 AM |
8597 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; |
8598 | s.op->indent(1); | |
6270adc1 | 8599 | |
e6fe60e7 AM |
8600 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
8601 | { | |
8602 | kprobe_derived_probe* p = it->second; | |
8603 | s.op->newline() << "{"; | |
8604 | if (p->has_return) | |
8605 | s.op->line() << " .return_p=1,"; | |
6270adc1 | 8606 | |
e6fe60e7 AM |
8607 | if (p->has_maxactive) |
8608 | { | |
8609 | s.op->line() << " .maxactive_p=1,"; | |
8610 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
8611 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
8612 | } | |
6270adc1 | 8613 | |
b350f56b JS |
8614 | if (p->locations[0]->optional) |
8615 | s.op->line() << " .optional_p=1,"; | |
8616 | ||
e6fe60e7 | 8617 | if (p->has_statement) |
c8d9d15e | 8618 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 8619 | else |
c8d9d15e | 8620 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
5d67b47c | 8621 | |
faea5e16 | 8622 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
e6fe60e7 | 8623 | s.op->line() << " },"; |
935447c8 DS |
8624 | } |
8625 | ||
e6fe60e7 | 8626 | s.op->newline(-1) << "};"; |
5d67b47c | 8627 | |
e6fe60e7 AM |
8628 | // Emit the kprobes callback function |
8629 | s.op->newline(); | |
88747011 | 8630 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
8631 | s.op->line() << " struct pt_regs *regs) {"; |
8632 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
8633 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8634 | // Check that the index is plausible | |
8635 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8636 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8637 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8638 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8639 | s.op->line() << "];"; | |
71db462b | 8640 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 8641 | "stp_probe_type_kprobe"); |
d9aed31e | 8642 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
8643 | |
8644 | // Make it look like the IP is set as it wouldn't have been replaced | |
8645 | // by a breakpoint instruction when calling real probe handler. Reset | |
8646 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
8647 | s.op->newline() << "{"; | |
8648 | s.op->indent(1); | |
d9aed31e | 8649 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 8650 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 8651 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 8652 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
8653 | s.op->newline(-1) << "}"; |
8654 | ||
f887a8c9 | 8655 | common_probe_entryfn_epilogue (s, true); |
e6fe60e7 AM |
8656 | s.op->newline() << "return 0;"; |
8657 | s.op->newline(-1) << "}"; | |
935447c8 | 8658 | |
e6fe60e7 AM |
8659 | // Same for kretprobes |
8660 | s.op->newline(); | |
88747011 | 8661 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
8662 | s.op->line() << " struct pt_regs *regs) {"; |
8663 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
935447c8 | 8664 | |
e6fe60e7 AM |
8665 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
8666 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8667 | // Check that the index is plausible | |
8668 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8669 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8670 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8671 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8672 | s.op->line() << "];"; | |
935447c8 | 8673 | |
71db462b | 8674 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 8675 | "stp_probe_type_kretprobe"); |
d9aed31e | 8676 | s.op->newline() << "c->kregs = regs;"; |
6dceb5c9 | 8677 | s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic |
6415ddde MW |
8678 | |
8679 | // Make it look like the IP is set as it wouldn't have been replaced | |
8680 | // by a breakpoint instruction when calling real probe handler. Reset | |
8681 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
8682 | s.op->newline() << "{"; | |
8683 | s.op->indent(1); | |
d9aed31e | 8684 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 8685 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
26e63673 | 8686 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 8687 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
8688 | s.op->newline(-1) << "}"; |
8689 | ||
f887a8c9 | 8690 | common_probe_entryfn_epilogue (s, true); |
e6fe60e7 AM |
8691 | s.op->newline() << "return 0;"; |
8692 | s.op->newline(-1) << "}"; | |
bd659351 | 8693 | |
03a4ec63 | 8694 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
bd659351 MW |
8695 | s.op->newline() << "static int kprobe_resolve(void *data, const char *name,"; |
8696 | s.op->newline() << " struct module *owner,"; | |
8697 | s.op->newline() << " unsigned long val) {"; | |
8698 | s.op->newline(1) << "int i;"; | |
fc1d2aa2 MW |
8699 | s.op->newline() << "int *p = (int *) data;"; |
8700 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() | |
8701 | << " && *p > 0; i++) {"; | |
bd659351 | 8702 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
8c272819 YW |
8703 | s.op->newline() << "if (! sdp->address) {"; |
8704 | s.op->indent(1); | |
8705 | s.op->newline() << "const char *colon;"; | |
8706 | s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {"; | |
8707 | s.op->indent(1); | |
8708 | s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))"; | |
8709 | s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)"; | |
8710 | s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {"; | |
8711 | s.op->newline(1) << "sdp->address = val;"; | |
8712 | s.op->newline() << "(*p)--;"; | |
8713 | s.op->newline(-1) << "}"; | |
8714 | s.op->newline(-1) << "}"; | |
8715 | s.op->newline() << "else {"; | |
fc1d2aa2 | 8716 | s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {"; |
bd659351 | 8717 | s.op->newline(1) << "sdp->address = val;"; |
fc1d2aa2 MW |
8718 | s.op->newline() << "(*p)--;"; |
8719 | s.op->newline(-1) << "}"; | |
8c272819 YW |
8720 | s.op->newline(-1) << "}"; |
8721 | s.op->newline(-1) << "}"; | |
8722 | s.op->newline(-1) << "}"; | |
fc1d2aa2 | 8723 | s.op->newline() << "return (p > 0) ? 0 : -1;"; |
bd659351 | 8724 | s.op->newline(-1) << "}"; |
03a4ec63 | 8725 | s.op->newline() << "#endif"; |
935447c8 DS |
8726 | } |
8727 | ||
e6fe60e7 | 8728 | |
6270adc1 | 8729 | void |
e6fe60e7 | 8730 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) |
6270adc1 | 8731 | { |
03a4ec63 | 8732 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
fc1d2aa2 MW |
8733 | s.op->newline() << "{"; |
8734 | s.op->newline(1) << "int p = 0;"; | |
8735 | s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {"; | |
8736 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8737 | s.op->newline() << "if (! sdp->address)"; | |
8738 | s.op->newline(1) << "p++;"; | |
8739 | s.op->newline(-2) << "}"; | |
8740 | s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);"; | |
8741 | s.op->newline(-1) << "}"; | |
03a4ec63 | 8742 | s.op->newline() << "#endif"; |
bd659351 | 8743 | |
e6fe60e7 | 8744 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 8745 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 8746 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e | 8747 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
03a4ec63 MW |
8748 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; |
8749 | ||
8750 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; | |
bd659351 MW |
8751 | s.op->newline() << "if (! addr) {"; |
8752 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
9319b767 MW |
8753 | s.op->newline() << "if (!sdp->optional_p)"; |
8754 | s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);"; | |
8755 | s.op->newline(-1) << "continue;"; | |
bd659351 | 8756 | s.op->newline(-1) << "}"; |
03a4ec63 MW |
8757 | s.op->newline() << "#endif"; |
8758 | ||
26e63673 | 8759 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
e6fe60e7 | 8760 | s.op->newline() << "if (sdp->return_p) {"; |
c8d9d15e | 8761 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
766cee5f | 8762 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 8763 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 8764 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
8765 | s.op->newline() << "if (sdp->maxactive_p) {"; |
8766 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
8767 | s.op->newline(-1) << "} else {"; | |
f07c3b68 | 8768 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
e6fe60e7 | 8769 | s.op->newline(-1) << "}"; |
88747011 | 8770 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
8771 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
8772 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 8773 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
766cee5f | 8774 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 8775 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
766cee5f | 8776 | s.op->newline() << "#endif"; |
c8d9d15e | 8777 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
e6fe60e7 AM |
8778 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
8779 | s.op->newline() << "if (rc == 0) {"; | |
8780 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
8781 | s.op->newline() << "if (rc != 0)"; | |
8782 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
8783 | s.op->newline(-2) << "}"; | |
8784 | s.op->newline() << "#else"; | |
8785 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
8786 | s.op->newline() << "#endif"; | |
8787 | s.op->newline(-1) << "} else {"; | |
8788 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 8789 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
766cee5f | 8790 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 8791 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 8792 | s.op->newline() << "#endif"; |
88747011 | 8793 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 8794 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 8795 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e | 8796 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
766cee5f | 8797 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 8798 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; |
766cee5f | 8799 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
8800 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
8801 | s.op->newline() << "if (rc == 0) {"; | |
8802 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
8803 | s.op->newline() << "if (rc != 0)"; | |
8804 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
8805 | s.op->newline(-2) << "}"; | |
8806 | s.op->newline() << "#else"; | |
8807 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
8808 | s.op->newline() << "#endif"; | |
8809 | s.op->newline(-1) << "}"; | |
8810 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
8811 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 8812 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 8813 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);"; |
b350f56b | 8814 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
e6fe60e7 AM |
8815 | // XXX: shall we increment numskipped? |
8816 | s.op->newline(-1) << "}"; | |
6270adc1 | 8817 | |
e6fe60e7 AM |
8818 | s.op->newline() << "else sdp->registered_p = 1;"; |
8819 | s.op->newline(-1) << "}"; // for loop | |
6270adc1 MH |
8820 | } |
8821 | ||
b4be7cbc | 8822 | |
e6fe60e7 AM |
8823 | void |
8824 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
935447c8 | 8825 | { |
e6fe60e7 AM |
8826 | //Unregister kprobes by batch interfaces. |
8827 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8828 | s.op->newline() << "j = 0;"; | |
8829 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8830 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8831 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8832 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8833 | s.op->newline() << "if (!sdp->return_p)"; | |
c9116e99 | 8834 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;"; |
e6fe60e7 | 8835 | s.op->newline(-2) << "}"; |
c9116e99 | 8836 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8837 | s.op->newline() << "j = 0;"; |
8838 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8839 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8840 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8841 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8842 | s.op->newline() << "if (sdp->return_p)"; | |
c9116e99 | 8843 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;"; |
e6fe60e7 | 8844 | s.op->newline(-2) << "}"; |
c9116e99 | 8845 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8846 | s.op->newline() << "#ifdef __ia64__"; |
8847 | s.op->newline() << "j = 0;"; | |
8848 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8849 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8850 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8851 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
c9116e99 | 8852 | s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;"; |
e6fe60e7 | 8853 | s.op->newline(-1) << "}"; |
c9116e99 | 8854 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8855 | s.op->newline() << "#endif"; |
8856 | s.op->newline() << "#endif"; | |
3e3bd7b6 | 8857 | |
e6fe60e7 AM |
8858 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
8859 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8860 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8861 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8862 | s.op->newline() << "if (sdp->return_p) {"; | |
8863 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8864 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
8865 | s.op->newline() << "#endif"; | |
065d5567 | 8866 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
e6fe60e7 AM |
8867 | s.op->newline() << "#ifdef STP_TIMING"; |
8868 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 8869 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
e6fe60e7 | 8870 | s.op->newline(-1) << "#endif"; |
065d5567 | 8871 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
e6fe60e7 AM |
8872 | s.op->newline() << "#ifdef STP_TIMING"; |
8873 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 8874 | 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 |
8875 | s.op->newline(-1) << "#endif"; |
8876 | s.op->newline(-1) << "} else {"; | |
8877 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8878 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
8879 | s.op->newline() << "#endif"; | |
065d5567 | 8880 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
e6fe60e7 AM |
8881 | s.op->newline() << "#ifdef STP_TIMING"; |
8882 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 8883 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
e6fe60e7 AM |
8884 | s.op->newline(-1) << "#endif"; |
8885 | s.op->newline(-1) << "}"; | |
8886 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
8887 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
8888 | s.op->newline() << "#endif"; | |
8889 | s.op->newline() << "sdp->registered_p = 0;"; | |
8890 | s.op->newline(-1) << "}"; | |
f8a968bc JS |
8891 | } |
8892 | ||
e6fe60e7 | 8893 | struct kprobe_builder: public derived_probe_builder |
3c1b3d06 | 8894 | { |
9fdf787d | 8895 | public: |
2a639817 | 8896 | kprobe_builder() {} |
9fdf787d | 8897 | |
2a639817 | 8898 | void build_no_more (systemtap_session &s) {} |
9fdf787d | 8899 | |
e6fe60e7 AM |
8900 | virtual void build(systemtap_session & sess, |
8901 | probe * base, | |
8902 | probe_point * location, | |
8903 | literal_map_t const & parameters, | |
8904 | vector<derived_probe *> & finished_results); | |
8905 | }; | |
3c1b3d06 FCE |
8906 | |
8907 | ||
79189b84 | 8908 | void |
05fb3e0c | 8909 | kprobe_builder::build(systemtap_session & sess, |
e6fe60e7 AM |
8910 | probe * base, |
8911 | probe_point * location, | |
8912 | literal_map_t const & parameters, | |
8913 | vector<derived_probe *> & finished_results) | |
79189b84 | 8914 | { |
e6fe60e7 | 8915 | string function_string_val, module_string_val; |
05fb3e0c | 8916 | string path, library, path_tgt, library_tgt; |
b6371390 JS |
8917 | int64_t statement_num_val = 0, maxactive_val = 0; |
8918 | bool has_function_str, has_module_str, has_statement_num; | |
8919 | bool has_absolute, has_return, has_maxactive; | |
b642c901 | 8920 | bool has_path, has_library; |
79189b84 | 8921 | |
b6371390 JS |
8922 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
8923 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
8924 | has_return = has_null_param (parameters, TOK_RETURN); | |
8925 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
8926 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
8927 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
b642c901 SC |
8928 | has_path = get_param (parameters, TOK_PROCESS, path); |
8929 | has_library = get_param (parameters, TOK_LIBRARY, library); | |
8930 | ||
8931 | if (has_path) | |
05fb3e0c WF |
8932 | { |
8933 | path = find_executable (path, sess.sysroot, sess.sysenv); | |
8934 | path_tgt = path_remove_sysroot(sess, path); | |
8935 | } | |
b642c901 | 8936 | if (has_library) |
05fb3e0c WF |
8937 | { |
8938 | library = find_executable (library, sess.sysroot, sess.sysenv, | |
8939 | "LD_LIBRARY_PATH"); | |
8940 | library_tgt = path_remove_sysroot(sess, library); | |
8941 | } | |
c57ea854 | 8942 | |
b6371390 | 8943 | if (has_function_str) |
6fb70fb7 | 8944 | { |
2a639817 | 8945 | if (has_module_str) |
9fdf787d DS |
8946 | { |
8947 | function_string_val = module_string_val + ":" + function_string_val; | |
8948 | derived_probe *dp | |
23dc94f6 DS |
8949 | = new kprobe_derived_probe (sess, finished_results, base, |
8950 | location, function_string_val, | |
9fdf787d DS |
8951 | 0, has_return, has_statement_num, |
8952 | has_maxactive, has_path, has_library, | |
8953 | maxactive_val, path_tgt, library_tgt); | |
8954 | finished_results.push_back (dp); | |
8955 | } | |
8956 | else | |
8957 | { | |
2a639817 JS |
8958 | vector<string> matches; |
8959 | ||
8960 | // Simple names can be found directly | |
8961 | if (function_string_val.find_first_of("*?[") == string::npos) | |
8962 | { | |
8963 | if (sess.kernel_functions.count(function_string_val)) | |
8964 | matches.push_back(function_string_val); | |
8965 | } | |
8966 | else // Search function name list for matching names | |
8967 | { | |
8968 | for (set<string>::const_iterator it = sess.kernel_functions.begin(); | |
8969 | it != sess.kernel_functions.end(); it++) | |
8970 | // fnmatch returns zero for matching. | |
8971 | if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0) | |
8972 | matches.push_back(*it); | |
8973 | } | |
86758d5f | 8974 | |
2a639817 JS |
8975 | for (vector<string>::const_iterator it = matches.begin(); |
8976 | it != matches.end(); it++) | |
9fdf787d | 8977 | { |
2a639817 JS |
8978 | derived_probe *dp |
8979 | = new kprobe_derived_probe (sess, finished_results, base, | |
8980 | location, *it, 0, has_return, | |
8981 | has_statement_num, | |
8982 | has_maxactive, has_path, | |
8983 | has_library, maxactive_val, | |
8984 | path_tgt, library_tgt); | |
8985 | finished_results.push_back (dp); | |
9fdf787d DS |
8986 | } |
8987 | } | |
6fb70fb7 | 8988 | } |
e6fe60e7 | 8989 | else |
b6371390 JS |
8990 | { |
8991 | // assert guru mode for absolute probes | |
8992 | if ( has_statement_num && has_absolute && !base->privileged ) | |
dc09353a | 8993 | throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok); |
b6371390 | 8994 | |
23dc94f6 DS |
8995 | finished_results.push_back (new kprobe_derived_probe (sess, |
8996 | finished_results, | |
8997 | base, | |
b6371390 JS |
8998 | location, "", |
8999 | statement_num_val, | |
9000 | has_return, | |
9001 | has_statement_num, | |
9002 | has_maxactive, | |
b642c901 SC |
9003 | has_path, |
9004 | has_library, | |
9005 | maxactive_val, | |
05fb3e0c WF |
9006 | path_tgt, |
9007 | library_tgt)); | |
96b030fe | 9008 | } |
79189b84 JS |
9009 | } |
9010 | ||
23dc94f6 DS |
9011 | |
9012 | void | |
9013 | kprobe_var_expanding_visitor::visit_entry_op (entry_op *e) | |
9014 | { | |
9015 | expression *repl = e; | |
9016 | ||
bd5b25e1 JS |
9017 | if (has_return) |
9018 | { | |
9019 | // expand the operand as if it weren't a return probe | |
9020 | has_return = false; | |
9021 | replace (e->operand); | |
9022 | has_return = true; | |
23dc94f6 | 9023 | |
bd5b25e1 JS |
9024 | // XXX it would be nice to use gen_kretprobe_saved_return when |
9025 | // available, but it requires knowing the types already, which is | |
9026 | // problematic for arbitrary expressons. | |
9027 | repl = gen_mapped_saved_return (sess, e->operand, "entry", | |
9028 | add_block, add_block_tid, | |
9029 | add_call_probe, add_call_probe_tid); | |
9030 | } | |
23dc94f6 DS |
9031 | provide (repl); |
9032 | } | |
9033 | ||
9034 | ||
dd225250 PS |
9035 | // ------------------------------------------------------------------------ |
9036 | // Hardware breakpoint based probes. | |
9037 | // ------------------------------------------------------------------------ | |
9038 | ||
9039 | static const string TOK_HWBKPT("data"); | |
9040 | static const string TOK_HWBKPT_WRITE("write"); | |
9041 | static const string TOK_HWBKPT_RW("rw"); | |
9042 | static const string TOK_LENGTH("length"); | |
9043 | ||
9044 | #define HWBKPT_READ 0 | |
9045 | #define HWBKPT_WRITE 1 | |
9046 | #define HWBKPT_RW 2 | |
9047 | struct hwbkpt_derived_probe: public derived_probe | |
9048 | { | |
9049 | hwbkpt_derived_probe (probe *base, | |
9050 | probe_point *location, | |
9051 | uint64_t addr, | |
9052 | string symname, | |
9053 | unsigned int len, | |
9054 | bool has_only_read_access, | |
9055 | bool has_only_write_access, | |
9056 | bool has_rw_access | |
9057 | ); | |
9058 | Dwarf_Addr hwbkpt_addr; | |
9059 | string symbol_name; | |
9060 | unsigned int hwbkpt_access,hwbkpt_len; | |
9061 | ||
9062 | void printsig (std::ostream &o) const; | |
9063 | void join_group (systemtap_session& s); | |
9064 | }; | |
9065 | ||
9066 | struct hwbkpt_derived_probe_group: public derived_probe_group | |
9067 | { | |
dd225250 | 9068 | private: |
dac77b80 | 9069 | vector<hwbkpt_derived_probe*> hwbkpt_probes; |
dd225250 PS |
9070 | |
9071 | public: | |
9072 | void enroll (hwbkpt_derived_probe* probe, systemtap_session& s); | |
9073 | void emit_module_decls (systemtap_session& s); | |
9074 | void emit_module_init (systemtap_session& s); | |
9075 | void emit_module_exit (systemtap_session& s); | |
9076 | }; | |
9077 | ||
9078 | hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base, | |
9ea68eb9 JS |
9079 | probe_point *location, |
9080 | uint64_t addr, | |
9081 | string symname, | |
9082 | unsigned int len, | |
9083 | bool has_only_read_access, | |
9084 | bool has_only_write_access, | |
822a6a3d | 9085 | bool): |
4c5d1300 | 9086 | derived_probe (base, location, true /* .components soon rewritten */ ), |
dd225250 PS |
9087 | hwbkpt_addr (addr), |
9088 | symbol_name (symname), | |
9089 | hwbkpt_len (len) | |
9090 | { | |
9091 | this->tok = base->tok; | |
9092 | ||
9093 | vector<probe_point::component*> comps; | |
9094 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
9095 | ||
9096 | if (hwbkpt_addr) | |
9ea68eb9 JS |
9097 | comps.push_back (new probe_point::component (TOK_HWBKPT, |
9098 | new literal_number(hwbkpt_addr, true))); | |
9099 | else if (symbol_name.size()) | |
9100 | comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name))); | |
dd225250 PS |
9101 | |
9102 | comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len))); | |
9103 | ||
9104 | if (has_only_read_access) | |
9ea68eb9 | 9105 | this->hwbkpt_access = HWBKPT_READ ; |
dd225250 PS |
9106 | //TODO add code for comps.push_back for read, since this flag is not for x86 |
9107 | ||
9108 | else | |
9ea68eb9 JS |
9109 | { |
9110 | if (has_only_write_access) | |
9111 | { | |
9112 | this->hwbkpt_access = HWBKPT_WRITE ; | |
9113 | comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE)); | |
9114 | } | |
9115 | else | |
9116 | { | |
9117 | this->hwbkpt_access = HWBKPT_RW ; | |
9118 | comps.push_back (new probe_point::component(TOK_HWBKPT_RW)); | |
9119 | } | |
9120 | } | |
dd225250 PS |
9121 | |
9122 | this->sole_location()->components = comps; | |
9123 | } | |
9124 | ||
9125 | void hwbkpt_derived_probe::printsig (ostream& o) const | |
9126 | { | |
9127 | sole_location()->print (o); | |
9128 | printsig_nested (o); | |
9129 | } | |
9130 | ||
9131 | void hwbkpt_derived_probe::join_group (systemtap_session& s) | |
9132 | { | |
dac77b80 FCE |
9133 | if (! s.hwbkpt_derived_probes) |
9134 | s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group (); | |
dd225250 PS |
9135 | s.hwbkpt_derived_probes->enroll (this, s); |
9136 | } | |
9137 | ||
9138 | void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s) | |
9139 | { | |
dac77b80 FCE |
9140 | hwbkpt_probes.push_back (p); |
9141 | ||
9142 | unsigned max_hwbkpt_probes_by_arch = 0; | |
9143 | if (s.architecture == "i386" || s.architecture == "x86_64") | |
9144 | max_hwbkpt_probes_by_arch = 4; | |
9145 | else if (s.architecture == "s390") | |
9146 | max_hwbkpt_probes_by_arch = 1; | |
9147 | ||
c57ea854 | 9148 | if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch) |
2713ea24 | 9149 | s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)", |
b530b5b3 | 9150 | s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch)); |
dd225250 PS |
9151 | } |
9152 | ||
9153 | void | |
9154 | hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s) | |
9155 | { | |
dac77b80 | 9156 | if (hwbkpt_probes.empty()) return; |
dd225250 PS |
9157 | |
9158 | s.op->newline() << "/* ---- hwbkpt-based probes ---- */"; | |
9159 | ||
9160 | s.op->newline() << "#include <linux/perf_event.h>"; | |
9161 | s.op->newline() << "#include <linux/hw_breakpoint.h>"; | |
9162 | s.op->newline(); | |
9163 | ||
9164 | // Forward declare the master entry functions | |
23063de1 | 9165 | s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI"; |
dd225250 PS |
9166 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; |
9167 | s.op->line() << " int nmi,"; | |
9168 | s.op->line() << " struct perf_sample_data *data,"; | |
9169 | s.op->line() << " struct pt_regs *regs);"; | |
23063de1 FCE |
9170 | s.op->newline() << "#else"; |
9171 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
9172 | s.op->line() << " struct perf_sample_data *data,"; | |
9173 | s.op->line() << " struct pt_regs *regs);"; | |
9174 | s.op->newline() << "#endif"; | |
79189b84 | 9175 | |
dd225250 PS |
9176 | // Emit the actual probe list. |
9177 | ||
9178 | s.op->newline() << "static struct perf_event_attr "; | |
dac77b80 | 9179 | s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
9180 | |
9181 | s.op->newline() << "static struct perf_event **"; | |
dac77b80 | 9182 | s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
9183 | s.op->newline() << "static struct stap_hwbkpt_probe {"; |
9184 | s.op->newline() << "int registered_p:1;"; | |
43650b10 | 9185 | // registered_p = 0 signifies a probe that is unregistered (or failed) |
dd225250 PS |
9186 | // registered_p = 1 signifies a probe that got registered successfully |
9187 | ||
faea5e16 | 9188 | // Symbol Names are mostly small and uniform enough |
dd225250 | 9189 | // to justify putting const char*. |
dac77b80 | 9190 | s.op->newline() << "const char * const symbol;"; |
dd225250 PS |
9191 | |
9192 | s.op->newline() << "const unsigned long address;"; | |
9193 | s.op->newline() << "uint8_t atype;"; | |
bb0a4e12 | 9194 | s.op->newline() << "unsigned int len;"; |
7c3e97f4 | 9195 | s.op->newline() << "const struct stap_probe * const probe;"; |
dd225250 PS |
9196 | s.op->newline() << "} stap_hwbkpt_probes[] = {"; |
9197 | s.op->indent(1); | |
9198 | ||
dac77b80 | 9199 | for (unsigned int it = 0; it < hwbkpt_probes.size(); it++) |
dd225250 | 9200 | { |
dac77b80 | 9201 | hwbkpt_derived_probe* p = hwbkpt_probes.at(it); |
dd225250 | 9202 | s.op->newline() << "{"; |
dd225250 PS |
9203 | if (p->symbol_name.size()) |
9204 | s.op->line() << " .address=(unsigned long)0x0" << "ULL,"; | |
9205 | else | |
9206 | s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,"; | |
9207 | switch(p->hwbkpt_access){ | |
9208 | case HWBKPT_READ: | |
9209 | s.op->line() << " .atype=HW_BREAKPOINT_R ,"; | |
bb0a4e12 | 9210 | break; |
dd225250 PS |
9211 | case HWBKPT_WRITE: |
9212 | s.op->line() << " .atype=HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 9213 | break; |
dd225250 PS |
9214 | case HWBKPT_RW: |
9215 | s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 9216 | break; |
dd225250 PS |
9217 | }; |
9218 | s.op->line() << " .len=" << p->hwbkpt_len << ","; | |
faea5e16 | 9219 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
dd225250 | 9220 | s.op->line() << " .symbol=\"" << p->symbol_name << "\","; |
dd225250 PS |
9221 | s.op->line() << " },"; |
9222 | } | |
dac77b80 | 9223 | s.op->newline(-1) << "};"; |
dd225250 PS |
9224 | |
9225 | // Emit the hwbkpt callback function | |
9226 | s.op->newline() ; | |
23063de1 | 9227 | s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI"; |
dd225250 PS |
9228 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; |
9229 | s.op->line() << " int nmi,"; | |
9230 | s.op->line() << " struct perf_sample_data *data,"; | |
9231 | s.op->line() << " struct pt_regs *regs) {"; | |
23063de1 FCE |
9232 | s.op->newline() << "#else"; |
9233 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
9234 | s.op->line() << " struct perf_sample_data *data,"; | |
9235 | s.op->line() << " struct pt_regs *regs) {"; | |
9236 | s.op->newline() << "#endif"; | |
dac77b80 FCE |
9237 | s.op->newline(1) << "unsigned int i;"; |
9238 | s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;"; | |
9239 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; | |
9240 | s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
9241 | // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead? | |
9242 | 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) {"; | |
9243 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];"; | |
71db462b | 9244 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 9245 | "stp_probe_type_hwbkpt"); |
d9aed31e | 9246 | s.op->newline() << "if (user_mode(regs)) {"; |
e04b5d74 | 9247 | s.op->newline(1)<< "c->user_mode_p = 1;"; |
d9aed31e MW |
9248 | s.op->newline() << "c->uregs = regs;"; |
9249 | s.op->newline(-1) << "} else {"; | |
9250 | s.op->newline(1) << "c->kregs = regs;"; | |
9251 | s.op->newline(-1) << "}"; | |
26e63673 | 9252 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
f887a8c9 | 9253 | common_probe_entryfn_epilogue (s, true); |
dac77b80 | 9254 | s.op->newline(-1) << "}"; |
dd225250 PS |
9255 | s.op->newline(-1) << "}"; |
9256 | s.op->newline() << "return 0;"; | |
dac77b80 | 9257 | s.op->newline(-1) << "}"; |
dd225250 PS |
9258 | } |
9259 | ||
9260 | void | |
9261 | hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s) | |
9262 | { | |
dac77b80 | 9263 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 PS |
9264 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
9265 | s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
9266 | s.op->newline() << "void *addr = (void *) sdp->address;"; | |
9267 | s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;"; | |
dac77b80 FCE |
9268 | s.op->newline() << "hw_breakpoint_init(hp);"; |
9269 | s.op->newline() << "if (addr)"; | |
9270 | s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;"; | |
9271 | s.op->newline(-1) << "else { "; | |
9272 | s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);"; | |
9273 | s.op->newline() << "if (!hp->bp_addr) { "; | |
26e63673 | 9274 | s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);"; |
dac77b80 FCE |
9275 | s.op->newline() << "continue;"; |
9276 | s.op->newline(-1) << "}"; | |
9277 | s.op->newline(-1) << "}"; | |
9278 | s.op->newline() << "hp->bp_type = sdp->atype;"; | |
9279 | ||
9280 | // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!). | |
c57ea854 | 9281 | if (s.architecture == "i386" || s.architecture == "x86_64" ) |
dac77b80 FCE |
9282 | { |
9283 | s.op->newline() << "switch(sdp->len) {"; | |
9284 | s.op->newline() << "case 1:"; | |
9285 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;"; | |
9286 | s.op->newline() << "break;"; | |
9287 | s.op->newline(-1) << "case 2:"; | |
9288 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;"; | |
9289 | s.op->newline() << "break;"; | |
9290 | s.op->newline(-1) << "case 3:"; | |
9291 | s.op->newline() << "case 4:"; | |
9292 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;"; | |
9293 | s.op->newline() << "break;"; | |
9294 | s.op->newline(-1) << "case 5:"; | |
9295 | s.op->newline() << "case 6:"; | |
9296 | s.op->newline() << "case 7:"; | |
9297 | s.op->newline() << "case 8:"; | |
9298 | s.op->newline() << "default:"; // XXX: could instead reject | |
9299 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;"; | |
9300 | s.op->newline() << "break;"; | |
9301 | s.op->newline(-1) << "}"; | |
9302 | } | |
9303 | else // other architectures presumed straightforward | |
9304 | s.op->newline() << "hp->bp_len = sdp->len;"; | |
9305 | ||
26e63673 | 9306 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b273669e MW |
9307 | s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT"; |
9308 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);"; | |
9309 | s.op->newline() << "#else"; | |
dac77b80 | 9310 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);"; |
b273669e | 9311 | s.op->newline() << "#endif"; |
43650b10 | 9312 | s.op->newline() << "rc = 0;"; |
dac77b80 | 9313 | s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {"; |
43650b10 WC |
9314 | s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);"; |
9315 | s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;"; | |
9316 | s.op->newline(-1) << "}"; | |
217ef1f4 WC |
9317 | s.op->newline() << "if (rc) {"; |
9318 | s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);"; | |
43650b10 | 9319 | s.op->newline() << "sdp->registered_p = 0;"; |
dac77b80 | 9320 | s.op->newline(-1) << "}"; |
dd225250 | 9321 | s.op->newline() << " else sdp->registered_p = 1;"; |
dd225250 PS |
9322 | s.op->newline(-1) << "}"; // for loop |
9323 | } | |
9324 | ||
9325 | void | |
9326 | hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s) | |
9327 | { | |
9328 | //Unregister hwbkpt probes. | |
dac77b80 | 9329 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 | 9330 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
dac77b80 FCE |
9331 | s.op->newline() << "if (sdp->registered_p == 0) continue;"; |
9332 | s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);"; | |
dd225250 PS |
9333 | s.op->newline() << "sdp->registered_p = 0;"; |
9334 | s.op->newline(-1) << "}"; | |
9335 | } | |
9336 | ||
9337 | struct hwbkpt_builder: public derived_probe_builder | |
9338 | { | |
9339 | hwbkpt_builder() {} | |
9340 | virtual void build(systemtap_session & sess, | |
9341 | probe * base, | |
9342 | probe_point * location, | |
9343 | literal_map_t const & parameters, | |
9344 | vector<derived_probe *> & finished_results); | |
9345 | }; | |
9346 | ||
9347 | void | |
9348 | hwbkpt_builder::build(systemtap_session & sess, | |
9349 | probe * base, | |
9350 | probe_point * location, | |
9351 | literal_map_t const & parameters, | |
9352 | vector<derived_probe *> & finished_results) | |
9353 | { | |
9354 | string symbol_str_val; | |
9355 | int64_t hwbkpt_address, len; | |
9356 | bool has_addr, has_symbol_str, has_write, has_rw, has_len; | |
9357 | ||
b47f3a55 | 9358 | if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y"))) |
dc09353a | 9359 | throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"), |
b47f3a55 FCE |
9360 | location->components[0]->tok); |
9361 | if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y"))) | |
dc09353a | 9362 | throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"), |
b47f3a55 FCE |
9363 | location->components[0]->tok); |
9364 | ||
dd225250 PS |
9365 | has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address); |
9366 | has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val); | |
9367 | has_len = get_param (parameters, TOK_LENGTH, len); | |
9368 | has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end()); | |
9369 | has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end()); | |
9370 | ||
9371 | if (!has_len) | |
9372 | len = 1; | |
9373 | ||
9374 | if (has_addr) | |
9375 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
9376 | location, | |
9377 | hwbkpt_address, | |
9378 | "",len,0, | |
9379 | has_write, | |
9380 | has_rw)); | |
5d8a0aea | 9381 | else if (has_symbol_str) |
dd225250 PS |
9382 | finished_results.push_back (new hwbkpt_derived_probe (base, |
9383 | location, | |
9384 | 0, | |
9385 | symbol_str_val,len,0, | |
9386 | has_write, | |
9387 | has_rw)); | |
5d8a0aea FCE |
9388 | else |
9389 | assert (0); | |
dd225250 | 9390 | } |
342d3f96 | 9391 | |
0a6f5a3f JS |
9392 | // ------------------------------------------------------------------------ |
9393 | // statically inserted kernel-tracepoint derived probes | |
9394 | // ------------------------------------------------------------------------ | |
9395 | ||
6fb70fb7 | 9396 | struct tracepoint_arg |
79189b84 | 9397 | { |
ad370dcc | 9398 | string name, c_type, typecast; |
dcaa1a65 | 9399 | bool usable, used, isptr; |
f8a968bc | 9400 | Dwarf_Die type_die; |
dcaa1a65 | 9401 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 9402 | }; |
79189b84 | 9403 | |
0a6f5a3f JS |
9404 | struct tracepoint_derived_probe: public derived_probe |
9405 | { | |
79189b84 JS |
9406 | tracepoint_derived_probe (systemtap_session& s, |
9407 | dwflpp& dw, Dwarf_Die& func_die, | |
9408 | const string& tracepoint_name, | |
9409 | probe* base_probe, probe_point* location); | |
bc9a523d | 9410 | |
79189b84 | 9411 | systemtap_session& sess; |
6fb70fb7 JS |
9412 | string tracepoint_name, header; |
9413 | vector <struct tracepoint_arg> args; | |
bc9a523d | 9414 | |
6fb70fb7 | 9415 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
d0bfd2ac | 9416 | void getargs (std::list<std::string> &arg_set) const; |
79189b84 | 9417 | void join_group (systemtap_session& s); |
3e3bd7b6 | 9418 | void print_dupe_stamp(ostream& o); |
0a6f5a3f | 9419 | }; |
79189b84 JS |
9420 | |
9421 | ||
0a6f5a3f | 9422 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> |
79189b84 | 9423 | { |
79189b84 JS |
9424 | void emit_module_decls (systemtap_session& s); |
9425 | void emit_module_init (systemtap_session& s); | |
9426 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f | 9427 | }; |
79189b84 | 9428 | |
bc9a523d | 9429 | |
f8a968bc JS |
9430 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
9431 | { | |
9432 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
9433 | vector <struct tracepoint_arg>& args): | |
9434 | dw (dw), probe_name (probe_name), args (args) {} | |
9435 | dwflpp& dw; | |
9436 | const string& probe_name; | |
9437 | vector <struct tracepoint_arg>& args; | |
bc9a523d | 9438 | |
f8a968bc JS |
9439 | void visit_target_symbol (target_symbol* e); |
9440 | void visit_target_symbol_arg (target_symbol* e); | |
9441 | void visit_target_symbol_context (target_symbol* e); | |
9442 | }; | |
79189b84 JS |
9443 | |
9444 | ||
f8a968bc JS |
9445 | void |
9446 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
75ead1f7 | 9447 | { |
cc9001af | 9448 | string argname = e->sym_name(); |
75ead1f7 | 9449 | |
f8a968bc JS |
9450 | // search for a tracepoint parameter matching this name |
9451 | tracepoint_arg *arg = NULL; | |
9452 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 9453 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
9454 | { |
9455 | arg = &args[i]; | |
9456 | arg->used = true; | |
9457 | break; | |
9458 | } | |
75ead1f7 | 9459 | |
f8a968bc JS |
9460 | if (arg == NULL) |
9461 | { | |
1d0499c2 | 9462 | set<string> vars; |
f8a968bc | 9463 | for (unsigned i = 0; i < args.size(); ++i) |
1d0499c2 JL |
9464 | vars.insert("$" + args[i].name); |
9465 | vars.insert("$$name"); | |
9466 | vars.insert("$$parms"); | |
9467 | vars.insert("$$vars"); | |
9468 | string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many | |
75ead1f7 | 9469 | |
f8a968bc JS |
9470 | // We hope that this value ends up not being referenced after all, so it |
9471 | // can be optimized out quietly. | |
1d0499c2 JL |
9472 | throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s", |
9473 | e->name.c_str(), sugs.empty() ? "" : | |
9474 | (_(" (alternatives: ") + sugs + ")").c_str()), e->tok); | |
9475 | // NB: we use 'alternatives' because we list all | |
f8a968bc JS |
9476 | // NB: we can have multiple errors, since a target variable |
9477 | // may be expanded in several different contexts: | |
9478 | // trace ("*") { $foo->bar } | |
f8a968bc | 9479 | } |
75ead1f7 | 9480 | |
f8a968bc | 9481 | // make sure we're not dereferencing base types |
dc5a09fc | 9482 | if (!arg->isptr) |
d19a9a82 | 9483 | e->assert_no_components("tracepoint", true); |
75ead1f7 | 9484 | |
f8a968bc JS |
9485 | // we can only write to dereferenced fields, and only if guru mode is on |
9486 | bool lvalue = is_active_lvalue(e); | |
9487 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
dc09353a | 9488 | throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok); |
c69a87e0 | 9489 | |
ad370dcc JS |
9490 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
9491 | // is also meaningless until you dereference past a pointer member. It's | |
9492 | // harder to detect and prevent that though... | |
75ead1f7 | 9493 | |
f8a968bc JS |
9494 | if (e->components.empty()) |
9495 | { | |
03c75a4a | 9496 | if (e->addressof) |
dc09353a | 9497 | throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok); |
a45664f4 | 9498 | |
3e3bd7b6 | 9499 | // Just grab the value from the probe locals |
a45664f4 JS |
9500 | symbol* sym = new symbol; |
9501 | sym->tok = e->tok; | |
9502 | sym->name = "__tracepoint_arg_" + arg->name; | |
9503 | provide (sym); | |
f8a968bc JS |
9504 | } |
9505 | else | |
9506 | { | |
5f36109e JS |
9507 | // make a copy of the original as a bare target symbol for the tracepoint |
9508 | // value, which will be passed into the dwarf dereferencing code | |
9509 | target_symbol* e2 = deep_copy_visitor::deep_copy(e); | |
9510 | e2->components.clear(); | |
9511 | ||
9512 | if (e->components.back().type == target_symbol::comp_pretty_print) | |
9513 | { | |
9514 | if (lvalue) | |
dc09353a | 9515 | throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 9516 | |
d19a9a82 | 9517 | dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e); |
5f36109e JS |
9518 | dpp.expand()->visit (this); |
9519 | return; | |
9520 | } | |
9521 | ||
1c0be8c7 | 9522 | bool userspace_p = false; |
f8a968bc | 9523 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") |
cc9001af | 9524 | + "_" + e->sym_name() |
aca66a36 | 9525 | + "_" + lex_cast(tick++)); |
75ead1f7 | 9526 | |
1c0be8c7 JS |
9527 | exp_type type = pe_long; |
9528 | string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, type); | |
6fda2dff | 9529 | |
1c0be8c7 JS |
9530 | functioncall* n = synthetic_embedded_deref_call(dw.sess, fname, code, type, |
9531 | userspace_p, lvalue, e, e2); | |
75ead1f7 | 9532 | |
f8a968bc JS |
9533 | if (lvalue) |
9534 | { | |
9535 | // Provide the functioncall to our parent, so that it can be | |
9536 | // used to substitute for the assignment node immediately above | |
9537 | // us. | |
9538 | assert(!target_symbol_setter_functioncalls.empty()); | |
9539 | *(target_symbol_setter_functioncalls.top()) = n; | |
9540 | } | |
75ead1f7 | 9541 | |
1c0be8c7 JS |
9542 | // Revisit the functioncall so arguments can be expanded. |
9543 | n->visit (this); | |
f8a968bc | 9544 | } |
75ead1f7 JS |
9545 | } |
9546 | ||
9547 | ||
f8a968bc JS |
9548 | void |
9549 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
0a6f5a3f | 9550 | { |
03c75a4a | 9551 | if (e->addressof) |
dc09353a | 9552 | throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok); |
03c75a4a | 9553 | |
f8a968bc | 9554 | if (is_active_lvalue (e)) |
dc09353a | 9555 | throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok); |
0a6f5a3f | 9556 | |
277c21bc | 9557 | if (e->name == "$$name") |
f8a968bc | 9558 | { |
5f36109e JS |
9559 | e->assert_no_components("tracepoint"); |
9560 | ||
bfdaad1e DS |
9561 | // Synthesize an embedded expression. |
9562 | embedded_expr *expr = new embedded_expr; | |
9563 | expr->tok = e->tok; | |
9564 | expr->code = string("/* string */ /* pure */ ") | |
6dceb5c9 | 9565 | + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\""); |
bfdaad1e | 9566 | provide (expr); |
f8a968bc | 9567 | } |
277c21bc | 9568 | else if (e->name == "$$vars" || e->name == "$$parms") |
f8a968bc | 9569 | { |
5f36109e JS |
9570 | e->assert_no_components("tracepoint", true); |
9571 | ||
f8a968bc JS |
9572 | token* pf_tok = new token(*e->tok); |
9573 | pf_tok->content = "sprintf"; | |
0a6f5a3f | 9574 | |
d5e178c1 | 9575 | print_format* pf = print_format::create(pf_tok); |
0a6f5a3f | 9576 | |
f8a968bc | 9577 | for (unsigned i = 0; i < args.size(); ++i) |
b278033a | 9578 | { |
dcaa1a65 JS |
9579 | if (!args[i].usable) |
9580 | continue; | |
f8a968bc JS |
9581 | if (i > 0) |
9582 | pf->raw_components += " "; | |
9583 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 9584 | target_symbol *tsym = new target_symbol; |
f8a968bc | 9585 | tsym->tok = e->tok; |
277c21bc | 9586 | tsym->name = "$" + args[i].name; |
5f36109e | 9587 | tsym->components = e->components; |
b278033a | 9588 | |
f8a968bc JS |
9589 | // every variable should always be accessible! |
9590 | tsym->saved_conversion_error = 0; | |
8c2f50c0 | 9591 | expression *texp = require<expression> (tsym); // NB: throws nothing ... |
14a97852 JS |
9592 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
9593 | { | |
9594 | if (dw.sess.verbose>2) | |
e26c2f83 | 9595 | for (const semantic_error *c = tsym->saved_conversion_error; |
14a97852 | 9596 | c != 0; c = c->chain) |
4c5d9906 | 9597 | clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl; |
14a97852 JS |
9598 | pf->raw_components += "=?"; |
9599 | continue; | |
9600 | } | |
b278033a | 9601 | |
5f36109e JS |
9602 | if (!e->components.empty() && |
9603 | e->components[0].type == target_symbol::comp_pretty_print) | |
9604 | pf->raw_components += "=%s"; | |
9605 | else | |
9606 | pf->raw_components += args[i].isptr ? "=%p" : "=%#x"; | |
f8a968bc JS |
9607 | pf->args.push_back(texp); |
9608 | } | |
0a6f5a3f | 9609 | |
f8a968bc JS |
9610 | pf->components = print_format::string_to_components(pf->raw_components); |
9611 | provide (pf); | |
b278033a | 9612 | } |
f8a968bc JS |
9613 | else |
9614 | assert(0); // shouldn't get here | |
0a6f5a3f JS |
9615 | } |
9616 | ||
0a6f5a3f | 9617 | void |
f8a968bc | 9618 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a6f5a3f | 9619 | { |
aff5d390 | 9620 | try |
c69a87e0 | 9621 | { |
bd1fcbad | 9622 | assert(e->name.size() > 0 && e->name[0] == '$'); |
aff5d390 | 9623 | |
277c21bc | 9624 | if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars") |
c69a87e0 | 9625 | visit_target_symbol_context (e); |
bd1fcbad | 9626 | |
c69a87e0 FCE |
9627 | else |
9628 | visit_target_symbol_arg (e); | |
9629 | } | |
9630 | catch (const semantic_error &er) | |
9631 | { | |
1af1e62d | 9632 | e->chain (er); |
c69a87e0 FCE |
9633 | provide (e); |
9634 | } | |
0a6f5a3f JS |
9635 | } |
9636 | ||
9637 | ||
79189b84 JS |
9638 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
9639 | dwflpp& dw, Dwarf_Die& func_die, | |
9640 | const string& tracepoint_name, | |
9641 | probe* base, probe_point* loc): | |
4c5d1300 | 9642 | derived_probe (base, loc, true /* .components soon rewritten */), |
79189b84 | 9643 | sess (s), tracepoint_name (tracepoint_name) |
56894e91 | 9644 | { |
79189b84 JS |
9645 | // create synthetic probe point name; preserve condition |
9646 | vector<probe_point::component*> comps; | |
9647 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
9648 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
9649 | this->sole_location()->components = comps; | |
9650 | ||
6fb70fb7 JS |
9651 | // fill out the available arguments in this tracepoint |
9652 | build_args(dw, func_die); | |
56894e91 | 9653 | |
6fb70fb7 JS |
9654 | // determine which header defined this tracepoint |
9655 | string decl_file = dwarf_decl_file(&func_die); | |
d4393459 FCE |
9656 | header = decl_file; |
9657 | ||
9658 | #if 0 /* This convention is not enforced. */ | |
6fb70fb7 JS |
9659 | size_t header_pos = decl_file.rfind("trace/"); |
9660 | if (header_pos == string::npos) | |
dc09353a | 9661 | throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '" |
6fb70fb7 JS |
9662 | + tracepoint_name + "' in '" |
9663 | + decl_file + "'"); | |
9664 | header = decl_file.substr(header_pos); | |
d4393459 | 9665 | #endif |
56894e91 | 9666 | |
6fb70fb7 JS |
9667 | // tracepoints from FOO_event_types.h should really be included from FOO.h |
9668 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
9669 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 9670 | // XXX: see also PR9993. |
d4393459 | 9671 | size_t header_pos = header.find("_event_types"); |
6fb70fb7 JS |
9672 | if (header_pos != string::npos) |
9673 | header.erase(header_pos, 12); | |
56894e91 | 9674 | |
f8a968bc JS |
9675 | // Now expand the local variables in the probe body |
9676 | tracepoint_var_expanding_visitor v (dw, name, args); | |
8b095b45 | 9677 | v.replace (this->body); |
a45664f4 JS |
9678 | for (unsigned i = 0; i < args.size(); i++) |
9679 | if (args[i].used) | |
9680 | { | |
9681 | vardecl* v = new vardecl; | |
9682 | v->name = "__tracepoint_arg_" + args[i].name; | |
9683 | v->tok = this->tok; | |
58701b78 | 9684 | v->set_arity(0, this->tok); |
a45664f4 | 9685 | v->type = pe_long; |
69aa668e | 9686 | v->synthetic = true; |
a45664f4 JS |
9687 | this->locals.push_back (v); |
9688 | } | |
56894e91 | 9689 | |
79189b84 | 9690 | if (sess.verbose > 2) |
ce0f6648 | 9691 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl; |
79189b84 | 9692 | } |
dc38c0ae | 9693 | |
56894e91 | 9694 | |
f8a968bc | 9695 | static bool |
dcaa1a65 | 9696 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
46b84a80 | 9697 | { |
d19a9a82 | 9698 | Dwarf_Die type; |
dcaa1a65 | 9699 | switch (dwarf_tag(&arg.type_die)) |
b20febf3 | 9700 | { |
f8a968bc JS |
9701 | case DW_TAG_typedef: |
9702 | case DW_TAG_const_type: | |
9703 | case DW_TAG_volatile_type: | |
9704 | // iterate on the referent type | |
3d1ad340 | 9705 | return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die) |
dcaa1a65 | 9706 | && resolve_tracepoint_arg_type(arg)); |
f8a968bc | 9707 | case DW_TAG_base_type: |
a52d2ac0 | 9708 | case DW_TAG_enumeration_type: |
f8a968bc | 9709 | // base types will simply be treated as script longs |
dcaa1a65 | 9710 | arg.isptr = false; |
f8a968bc JS |
9711 | return true; |
9712 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
9713 | // pointers can be treated as script longs, |
9714 | // and if we know their type, they can also be dereferenced | |
d19a9a82 JS |
9715 | type = arg.type_die; |
9716 | while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)) | |
9717 | { | |
9718 | // It still might be a non-type, e.g. const void, | |
9719 | // so we need to strip away all qualifiers. | |
9720 | int tag = dwarf_tag(&arg.type_die); | |
9721 | if (tag != DW_TAG_typedef && | |
9722 | tag != DW_TAG_const_type && | |
9723 | tag != DW_TAG_volatile_type) | |
9724 | { | |
9725 | arg.isptr = true; | |
9726 | break; | |
9727 | } | |
9728 | } | |
9729 | if (!arg.isptr) | |
9730 | arg.type_die = type; | |
ad370dcc JS |
9731 | arg.typecast = "(intptr_t)"; |
9732 | return true; | |
9733 | case DW_TAG_structure_type: | |
9734 | case DW_TAG_union_type: | |
9735 | // for structs/unions which are passed by value, we turn it into | |
9736 | // a pointer that can be dereferenced. | |
9737 | arg.isptr = true; | |
9738 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 9739 | return true; |
f8a968bc JS |
9740 | default: |
9741 | // should we consider other types too? | |
9742 | return false; | |
b20febf3 | 9743 | } |
56894e91 JS |
9744 | } |
9745 | ||
9746 | ||
9747 | void | |
822a6a3d | 9748 | tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die) |
56894e91 | 9749 | { |
6fb70fb7 JS |
9750 | Dwarf_Die arg; |
9751 | if (dwarf_child(&func_die, &arg) == 0) | |
9752 | do | |
9753 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
9754 | { | |
9755 | // build a tracepoint_arg for this parameter | |
9756 | tracepoint_arg tparg; | |
23d106b9 | 9757 | tparg.name = dwarf_diename(&arg); |
56894e91 | 9758 | |
6fb70fb7 | 9759 | // read the type of this parameter |
3d1ad340 | 9760 | if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die) |
f1c8f8a5 | 9761 | || !dwarf_type_name(&tparg.type_die, tparg.c_type)) |
dc09353a | 9762 | throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'", |
ce0f6648 | 9763 | tparg.name.c_str(), tracepoint_name.c_str())); |
a68f81a2 | 9764 | |
dcaa1a65 | 9765 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
9766 | args.push_back(tparg); |
9767 | if (sess.verbose > 4) | |
a52d2ac0 JS |
9768 | clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s", |
9769 | tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(), | |
9770 | tparg.usable ? "ok" : "unavailable") << endl; | |
6fb70fb7 JS |
9771 | } |
9772 | while (dwarf_siblingof(&arg, &arg) == 0); | |
56894e91 JS |
9773 | } |
9774 | ||
dc38c0ae | 9775 | void |
d0bfd2ac | 9776 | tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const |
dc38c0ae | 9777 | { |
dcaa1a65 JS |
9778 | for (unsigned i = 0; i < args.size(); ++i) |
9779 | if (args[i].usable) | |
d0bfd2ac | 9780 | arg_set.push_back("$"+args[i].name+":"+args[i].c_type); |
dc38c0ae DS |
9781 | } |
9782 | ||
79189b84 JS |
9783 | void |
9784 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
197a4d62 | 9785 | { |
79189b84 JS |
9786 | if (! s.tracepoint_derived_probes) |
9787 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
9788 | s.tracepoint_derived_probes->enroll (this); | |
9789 | } | |
e38d6504 | 9790 | |
56894e91 | 9791 | |
197a4d62 | 9792 | void |
3e3bd7b6 | 9793 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) |
56894e91 | 9794 | { |
3e3bd7b6 JS |
9795 | for (unsigned i = 0; i < args.size(); i++) |
9796 | if (args[i].used) | |
9797 | o << "__tracepoint_arg_" << args[i].name << endl; | |
197a4d62 | 9798 | } |
56894e91 | 9799 | |
3e3bd7b6 | 9800 | |
c9ccb642 | 9801 | static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header) |
47dd066d | 9802 | { |
3c1b3d06 FCE |
9803 | vector<string> they_live; |
9804 | // PR 9993 | |
9805 | // XXX: may need this to be configurable | |
d4393459 | 9806 | they_live.push_back ("#include <linux/skbuff.h>"); |
9e0cd21a FCE |
9807 | |
9808 | // PR11649: conditional extra header | |
9809 | // for kvm tracepoints in 2.6.33ish | |
9810 | if (s.kernel_config["CONFIG_KVM"] != string("")) { | |
d4393459 FCE |
9811 | they_live.push_back ("#include <linux/kvm_host.h>"); |
9812 | } | |
9813 | ||
50b72692 | 9814 | if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) { |
d4393459 | 9815 | they_live.push_back ("#define XFS_BIG_BLKNOS 1"); |
88637c31 FCE |
9816 | if (s.kernel_source_tree != "") |
9817 | they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree | |
d4393459 FCE |
9818 | they_live.push_back ("struct xfs_mount;"); |
9819 | they_live.push_back ("struct xfs_inode;"); | |
9820 | they_live.push_back ("struct xfs_buf;"); | |
9821 | they_live.push_back ("struct xfs_bmbt_irec;"); | |
c2cf1b87 | 9822 | they_live.push_back ("struct xfs_trans;"); |
9e0cd21a | 9823 | } |
d4393459 | 9824 | |
50b72692 | 9825 | if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) { |
d4393459 FCE |
9826 | they_live.push_back ("struct rpc_task;"); |
9827 | } | |
b64d65e2 FCE |
9828 | // RHEL6.3 |
9829 | if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) { | |
9830 | they_live.push_back ("struct rpc_clnt;"); | |
9831 | they_live.push_back ("struct rpc_wait_queue;"); | |
9832 | } | |
d4393459 FCE |
9833 | |
9834 | they_live.push_back ("#include <asm/cputime.h>"); | |
9835 | ||
c2cf1b87 FCE |
9836 | // linux 3.0 |
9837 | they_live.push_back ("struct cpu_workqueue_struct;"); | |
9838 | ||
50b72692 | 9839 | if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string("")) |
c2cf1b87 FCE |
9840 | if (s.kernel_source_tree != "") |
9841 | they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree | |
9842 | ||
50b72692 HP |
9843 | if (header.find("ext3") != string::npos && s.kernel_config["CONFIG_EXT3_FS"] != string("")) |
9844 | they_live.push_back ("struct ext3_reserve_window_node;"); | |
9845 | ||
3c1b3d06 FCE |
9846 | return they_live; |
9847 | } | |
47dd066d WC |
9848 | |
9849 | ||
9850 | void | |
79189b84 | 9851 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) |
47dd066d | 9852 | { |
79189b84 JS |
9853 | if (probes.empty()) |
9854 | return; | |
47dd066d | 9855 | |
96b030fe JS |
9856 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
9857 | s.op->newline(); | |
79189b84 | 9858 | |
47dd066d | 9859 | |
a4b9c3b3 FCE |
9860 | // We create a MODULE_aux_N.c file for each tracepoint header, to allow them |
9861 | // to be separately compiled. That's because kernel tracepoint headers sometimes | |
9862 | // conflict. PR13155. | |
9863 | ||
9864 | map<string,translator_output*> per_header_aux; | |
9865 | // GC NB: the translator_output* structs are owned/retained by the systemtap_session. | |
47dd066d | 9866 | |
6fb70fb7 JS |
9867 | for (unsigned i = 0; i < probes.size(); ++i) |
9868 | { | |
9869 | tracepoint_derived_probe *p = probes[i]; | |
75ae2ec9 | 9870 | string header = p->header; |
5f73a260 | 9871 | |
a4b9c3b3 FCE |
9872 | // We cache the auxiliary output files on a per-header basis. We don't |
9873 | // need one aux file per tracepoint, only one per tracepoint-header. | |
9874 | translator_output *tpop = per_header_aux[header]; | |
9875 | if (tpop == 0) | |
9876 | { | |
9877 | tpop = s.op_create_auxiliary(); | |
9878 | per_header_aux[header] = tpop; | |
9879 | ||
9880 | // PR9993: Add extra headers to work around undeclared types in individual | |
9881 | // include/trace/foo.h files | |
9882 | const vector<string>& extra_decls = tracepoint_extra_decls (s, header); | |
9883 | for (unsigned z=0; z<extra_decls.size(); z++) | |
9884 | tpop->newline() << extra_decls[z] << "\n"; | |
720c435f | 9885 | |
a4b9c3b3 FCE |
9886 | // strip include/ substring, the same way as done in get_tracequery_module() |
9887 | size_t root_pos = header.rfind("include/"); | |
9888 | header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header); | |
bfffa443 FCE |
9889 | |
9890 | tpop->newline() << "#include <linux/tracepoint.h>" << endl; | |
a4b9c3b3 | 9891 | tpop->newline() << "#include <" << header << ">"; |
720c435f | 9892 | |
a4b9c3b3 FCE |
9893 | // Starting in 2.6.35, at the same time NOARGS was added, the callback |
9894 | // always has a void* as the first parameter. PR11599 | |
9895 | tpop->newline() << "#ifdef DECLARE_TRACE_NOARGS"; | |
9896 | tpop->newline() << "#define STAP_TP_DATA , NULL"; | |
9897 | tpop->newline() << "#define STAP_TP_PROTO void *cb_data" | |
9898 | << " __attribute__ ((unused))"; | |
9899 | if (!p->args.empty()) | |
9900 | tpop->line() << ","; | |
9901 | tpop->newline() << "#else"; | |
9902 | tpop->newline() << "#define STAP_TP_DATA"; | |
9903 | tpop->newline() << "#define STAP_TP_PROTO"; | |
9904 | if (p->args.empty()) | |
9905 | tpop->line() << " void"; | |
9906 | tpop->newline() << "#endif"; | |
720c435f JS |
9907 | |
9908 | tpop->newline() << "#define intptr_t long"; | |
a4b9c3b3 FCE |
9909 | } |
9910 | ||
720c435f JS |
9911 | // collect the args that are actually in use |
9912 | vector<const tracepoint_arg*> used_args; | |
6fb70fb7 | 9913 | for (unsigned j = 0; j < p->args.size(); ++j) |
720c435f JS |
9914 | if (p->args[j].used) |
9915 | used_args.push_back(&p->args[j]); | |
9916 | ||
a4b9c3b3 FCE |
9917 | // forward-declare the generated-side tracepoint callback |
9918 | tpop->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
9919 | tpop->indent(2); |
9920 | if (used_args.empty()) | |
9921 | tpop->line() << "void"; | |
9922 | for (unsigned j = 0; j < used_args.size(); ++j) | |
6fb70fb7 JS |
9923 | { |
9924 | if (j > 0) | |
a4b9c3b3 FCE |
9925 | tpop->line() << ", "; |
9926 | tpop->line() << "int64_t"; | |
6fb70fb7 | 9927 | } |
720c435f JS |
9928 | tpop->line() << ");"; |
9929 | tpop->indent(-2); | |
5f73a260 | 9930 | |
a4b9c3b3 FCE |
9931 | // define the generated-side tracepoint callback - in the main translator-output |
9932 | s.op->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
9933 | s.op->indent(2); |
9934 | if (used_args.empty()) | |
9935 | s.op->newline() << "void"; | |
9936 | for (unsigned j = 0; j < used_args.size(); ++j) | |
a4b9c3b3 | 9937 | { |
a4b9c3b3 FCE |
9938 | if (j > 0) |
9939 | s.op->line() << ", "; | |
720c435f | 9940 | s.op->newline() << "int64_t __tracepoint_arg_" << used_args[j]->name; |
6fb70fb7 | 9941 | } |
5f73a260 JS |
9942 | s.op->newline() << ")"; |
9943 | s.op->newline(-2) << "{"; | |
7c3e97f4 | 9944 | s.op->newline(1) << "const struct stap_probe * const probe = " |
faea5e16 | 9945 | << common_probe_init (p) << ";"; |
71db462b | 9946 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe", |
cda141c2 | 9947 | "stp_probe_type_tracepoint"); |
6dceb5c9 | 9948 | s.op->newline() << "c->ips.tracepoint_name = " |
c12d974f FCE |
9949 | << lex_cast_qstring (p->tracepoint_name) |
9950 | << ";"; | |
720c435f JS |
9951 | for (unsigned j = 0; j < used_args.size(); ++j) |
9952 | { | |
9953 | s.op->newline() << "c->probe_locals." << p->name | |
0386bbcf | 9954 | << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name) |
720c435f JS |
9955 | << " = __tracepoint_arg_" << used_args[j]->name << ";"; |
9956 | } | |
26e63673 | 9957 | s.op->newline() << "(*probe->ph) (c);"; |
f887a8c9 | 9958 | common_probe_entryfn_epilogue (s, true); |
6fb70fb7 | 9959 | s.op->newline(-1) << "}"; |
47dd066d | 9960 | |
a4b9c3b3 | 9961 | // define the real tracepoint callback function |
720c435f | 9962 | tpop->newline() << "static void enter_tracepoint_probe_" << i << "("; |
a4b9c3b3 FCE |
9963 | tpop->newline(2) << "STAP_TP_PROTO"; |
9964 | for (unsigned j = 0; j < p->args.size(); ++j) | |
9965 | { | |
9966 | if (j > 0) | |
9967 | tpop->line() << ", "; | |
9968 | tpop->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
9969 | } | |
9970 | tpop->newline() << ")"; | |
9971 | tpop->newline(-2) << "{"; | |
720c435f JS |
9972 | tpop->newline(1) << "enter_real_tracepoint_probe_" << i << "("; |
9973 | tpop->indent(2); | |
9974 | for (unsigned j = 0; j < used_args.size(); ++j) | |
9975 | { | |
9976 | if (j > 0) | |
9977 | tpop->line() << ", "; | |
9978 | tpop->newline() << "(int64_t)" << used_args[j]->typecast | |
9979 | << "__tracepoint_arg_" << used_args[j]->name; | |
9980 | } | |
9981 | tpop->newline() << ");"; | |
9982 | tpop->newline(-3) << "}"; | |
a4b9c3b3 FCE |
9983 | |
9984 | ||
96b030fe | 9985 | // emit normalized registration functions |
720c435f | 9986 | tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 9987 | tpop->newline(1) << "return register_trace_" << p->tracepoint_name |
5f73a260 | 9988 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 | 9989 | tpop->newline(-1) << "}"; |
47dd066d | 9990 | |
86758d5f JS |
9991 | // NB: we're not prepared to deal with unreg failures. However, failures |
9992 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
9993 | // weren't even registered. The former should be OKed by the initial | |
9994 | // registration call, and the latter is safe to ignore. | |
720c435f | 9995 | tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 9996 | tpop->newline(1) << "(void) unregister_trace_" << p->tracepoint_name |
5f73a260 | 9997 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 FCE |
9998 | tpop->newline(-1) << "}"; |
9999 | tpop->newline(); | |
5f73a260 | 10000 | |
720c435f JS |
10001 | // declare normalized registration functions |
10002 | s.op->newline() << "int register_tracepoint_probe_" << i << "(void);"; | |
10003 | s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);"; | |
5f73a260 | 10004 | |
a4b9c3b3 | 10005 | tpop->assert_0_indent(); |
af304783 DS |
10006 | } |
10007 | ||
96b030fe JS |
10008 | // emit an array of registration functions for easy init/shutdown |
10009 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
10010 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 10011 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
10012 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
10013 | s.op->indent(1); | |
10014 | for (unsigned i = 0; i < probes.size(); ++i) | |
10015 | { | |
10016 | s.op->newline () << "{"; | |
10017 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
10018 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
10019 | s.op->line() << " },"; | |
10020 | } | |
10021 | s.op->newline(-1) << "};"; | |
10022 | s.op->newline(); | |
47dd066d WC |
10023 | } |
10024 | ||
10025 | ||
79189b84 JS |
10026 | void |
10027 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
47dd066d | 10028 | { |
79189b84 JS |
10029 | if (probes.size () == 0) |
10030 | return; | |
47dd066d | 10031 | |
79189b84 | 10032 | s.op->newline() << "/* init tracepoint probes */"; |
96b030fe JS |
10033 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
10034 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
10035 | s.op->newline() << "if (rc) {"; | |
10036 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
10037 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
10038 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
10039 | s.op->newline(-1) << "}"; | |
10040 | s.op->newline(-1) << "}"; | |
47dd066d | 10041 | |
bc9a523d FCE |
10042 | // This would be technically proper (on those autoconf-detectable |
10043 | // kernels that include this function in tracepoint.h), however we | |
10044 | // already make several calls to synchronze_sched() during our | |
10045 | // shutdown processes. | |
47dd066d | 10046 | |
bc9a523d FCE |
10047 | // s.op->newline() << "if (rc)"; |
10048 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
10049 | // s.op->indent(-1); | |
79189b84 | 10050 | } |
47dd066d WC |
10051 | |
10052 | ||
79189b84 JS |
10053 | void |
10054 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
47dd066d | 10055 | { |
79189b84 JS |
10056 | if (probes.empty()) |
10057 | return; | |
47dd066d | 10058 | |
96b030fe JS |
10059 | s.op->newline() << "/* deregister tracepoint probes */"; |
10060 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
10061 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
10062 | s.op->indent(-1); | |
47dd066d | 10063 | |
bc9a523d | 10064 | // Not necessary: see above. |
47dd066d | 10065 | |
bc9a523d | 10066 | // s.op->newline() << "tracepoint_synchronize_unregister();"; |
79189b84 | 10067 | } |
b20febf3 | 10068 | |
47dd066d | 10069 | |
75ead1f7 | 10070 | struct tracepoint_query : public base_query |
47dd066d | 10071 | { |
75ead1f7 JS |
10072 | tracepoint_query(dwflpp & dw, const string & tracepoint, |
10073 | probe * base_probe, probe_point * base_loc, | |
10074 | vector<derived_probe *> & results): | |
10075 | base_query(dw, "*"), tracepoint(tracepoint), | |
10076 | base_probe(base_probe), base_loc(base_loc), | |
10077 | results(results) {} | |
47dd066d | 10078 | |
75ead1f7 | 10079 | const string& tracepoint; |
47dd066d | 10080 | |
75ead1f7 JS |
10081 | probe * base_probe; |
10082 | probe_point * base_loc; | |
10083 | vector<derived_probe *> & results; | |
f982c59b | 10084 | set<string> probed_names; |
47dd066d | 10085 | |
75ead1f7 JS |
10086 | void handle_query_module(); |
10087 | int handle_query_cu(Dwarf_Die * cudie); | |
10088 | int handle_query_func(Dwarf_Die * func); | |
822a6a3d | 10089 | void query_library (const char *) {} |
576eaefe | 10090 | void query_plt (const char *entry, size_t addr) {} |
b20febf3 | 10091 | |
75ead1f7 JS |
10092 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); |
10093 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
10094 | }; | |
47dd066d WC |
10095 | |
10096 | ||
10097 | void | |
75ead1f7 | 10098 | tracepoint_query::handle_query_module() |
47dd066d | 10099 | { |
75ead1f7 | 10100 | // look for the tracepoints in each CU |
337b7c44 | 10101 | dw.iterate_over_cus(tracepoint_query_cu, this, false); |
47dd066d WC |
10102 | } |
10103 | ||
10104 | ||
75ead1f7 JS |
10105 | int |
10106 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
47dd066d | 10107 | { |
75ead1f7 | 10108 | dw.focus_on_cu (cudie); |
d906ab9d | 10109 | dw.mod_info->get_symtab(this); |
47dd066d | 10110 | |
75ead1f7 JS |
10111 | // look at each function to see if it's a tracepoint |
10112 | string function = "stapprobe_" + tracepoint; | |
10113 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
47dd066d WC |
10114 | } |
10115 | ||
10116 | ||
75ead1f7 JS |
10117 | int |
10118 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
47dd066d | 10119 | { |
75ead1f7 | 10120 | dw.focus_on_function (func); |
47dd066d | 10121 | |
60d98537 | 10122 | assert(startswith(dw.function_name, "stapprobe_")); |
75ead1f7 | 10123 | string tracepoint_instance = dw.function_name.substr(10); |
f982c59b JS |
10124 | |
10125 | // check for duplicates -- sometimes tracepoint headers may be indirectly | |
10126 | // included in more than one of our tracequery modules. | |
10127 | if (!probed_names.insert(tracepoint_instance).second) | |
10128 | return DWARF_CB_OK; | |
10129 | ||
79189b84 JS |
10130 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
10131 | tracepoint_instance, | |
10132 | base_probe, base_loc); | |
10133 | results.push_back (dp); | |
75ead1f7 | 10134 | return DWARF_CB_OK; |
47dd066d WC |
10135 | } |
10136 | ||
10137 | ||
75ead1f7 JS |
10138 | int |
10139 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
47dd066d | 10140 | { |
75ead1f7 | 10141 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); |
85007c04 | 10142 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 10143 | return q->handle_query_cu(cudie); |
47dd066d WC |
10144 | } |
10145 | ||
10146 | ||
75ead1f7 JS |
10147 | int |
10148 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
47dd066d | 10149 | { |
75ead1f7 | 10150 | tracepoint_query * q = static_cast<tracepoint_query *>(query); |
85007c04 | 10151 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 10152 | return q->handle_query_func(func); |
47dd066d WC |
10153 | } |
10154 | ||
10155 | ||
0a6f5a3f | 10156 | struct tracepoint_builder: public derived_probe_builder |
47dd066d | 10157 | { |
0a6f5a3f JS |
10158 | private: |
10159 | dwflpp *dw; | |
10160 | bool init_dw(systemtap_session& s); | |
c9ccb642 FCE |
10161 | void get_tracequery_modules(systemtap_session& s, |
10162 | const vector<string>& headers, | |
10163 | vector<string>& modules); | |
47dd066d | 10164 | |
0a6f5a3f | 10165 | public: |
47dd066d | 10166 | |
0a6f5a3f JS |
10167 | tracepoint_builder(): dw(0) {} |
10168 | ~tracepoint_builder() { delete dw; } | |
47dd066d | 10169 | |
0a6f5a3f JS |
10170 | void build_no_more (systemtap_session& s) |
10171 | { | |
10172 | if (dw && s.verbose > 3) | |
b530b5b3 | 10173 | clog << _("tracepoint_builder releasing dwflpp") << endl; |
0a6f5a3f JS |
10174 | delete dw; |
10175 | dw = NULL; | |
435f53a7 FCE |
10176 | |
10177 | delete_session_module_cache (s); | |
0a6f5a3f | 10178 | } |
47dd066d | 10179 | |
0a6f5a3f JS |
10180 | void build(systemtap_session& s, |
10181 | probe *base, probe_point *location, | |
10182 | literal_map_t const& parameters, | |
10183 | vector<derived_probe*>& finished_results); | |
10184 | }; | |
47dd066d | 10185 | |
47dd066d | 10186 | |
c9ccb642 | 10187 | |
2a0e62a8 | 10188 | // Create (or cache) one or more tracequery .o modules, based upon the |
c9ccb642 FCE |
10189 | // tracepoint-related header files given. Return the generated or cached |
10190 | // modules[]. | |
10191 | ||
10192 | void | |
10193 | tracepoint_builder::get_tracequery_modules(systemtap_session& s, | |
10194 | const vector<string>& headers, | |
10195 | vector<string>& modules) | |
0a6f5a3f | 10196 | { |
c95eddf7 | 10197 | if (s.verbose > 2) |
55e50c24 | 10198 | { |
ce0f6648 | 10199 | clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl; |
55e50c24 JS |
10200 | for (size_t i = 0; i < headers.size(); ++i) |
10201 | clog << " " << headers[i] << endl; | |
10202 | } | |
c95eddf7 | 10203 | |
2a0e62a8 JS |
10204 | map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name |
10205 | // Map the headers to cache .o names. Note that this has side-effects of | |
c9ccb642 FCE |
10206 | // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file, |
10207 | // so we prefer not to repeat this. | |
10208 | vector<string> uncached_headers; | |
10209 | for (size_t i=0; i<headers.size(); i++) | |
2a0e62a8 | 10210 | headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]); |
c9ccb642 FCE |
10211 | |
10212 | // They may be in the cache already. | |
10213 | if (s.use_cache && !s.poison_cache) | |
10214 | for (size_t i=0; i<headers.size(); i++) | |
10215 | { | |
10216 | // see if the cached module exists | |
2a0e62a8 | 10217 | const string& tracequery_path = headers_cache_obj[headers[i]]; |
c9ccb642 FCE |
10218 | if (!tracequery_path.empty() && file_exists(tracequery_path)) |
10219 | { | |
10220 | if (s.verbose > 2) | |
10221 | clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl; | |
47dd066d | 10222 | |
c252fca2 JS |
10223 | // an empty file is a cached failure |
10224 | if (get_file_size(tracequery_path) > 0) | |
10225 | modules.push_back (tracequery_path); | |
c9ccb642 FCE |
10226 | } |
10227 | else | |
10228 | uncached_headers.push_back(headers[i]); | |
10229 | } | |
10230 | else | |
10231 | uncached_headers = headers; | |
f982c59b | 10232 | |
c9ccb642 FCE |
10233 | // If we have nothing left to search for, quit |
10234 | if (uncached_headers.empty()) return; | |
55e50c24 | 10235 | |
c9ccb642 | 10236 | map<string,string> headers_tracequery_src; // header -> C-source code mapping |
55e50c24 | 10237 | |
c9ccb642 FCE |
10238 | // We could query several subsets of headers[] to make this go |
10239 | // faster, but let's KISS and do one at a time. | |
10240 | for (size_t i=0; i<uncached_headers.size(); i++) | |
55e50c24 | 10241 | { |
c9ccb642 FCE |
10242 | const string& header = uncached_headers[i]; |
10243 | ||
10244 | // create a tracequery source file | |
10245 | ostringstream osrc; | |
10246 | ||
10247 | // PR9993: Add extra headers to work around undeclared types in individual | |
10248 | // include/trace/foo.h files | |
10249 | vector<string> short_decls = tracepoint_extra_decls(s, header); | |
10250 | ||
10251 | // add each requested tracepoint header | |
75ae2ec9 | 10252 | size_t root_pos = header.rfind("include/"); |
832f100d | 10253 | short_decls.push_back(string("#include <") + |
75ae2ec9 | 10254 | ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) + |
d4393459 | 10255 | string(">")); |
f982c59b | 10256 | |
c9ccb642 FCE |
10257 | osrc << "#ifdef CONFIG_TRACEPOINTS" << endl; |
10258 | osrc << "#include <linux/tracepoint.h>" << endl; | |
832f100d JS |
10259 | |
10260 | // the kernel has changed this naming a few times, previously TPPROTO, | |
10261 | // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest. | |
10262 | osrc << "#ifndef PARAMS" << endl; | |
10263 | osrc << "#define PARAMS(args...) args" << endl; | |
10264 | osrc << "#endif" << endl; | |
10265 | ||
c9ccb642 FCE |
10266 | // override DECLARE_TRACE to synthesize probe functions for us |
10267 | osrc << "#undef DECLARE_TRACE" << endl; | |
10268 | osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl; | |
10269 | osrc << " void stapprobe_##name(proto) {}" << endl; | |
832f100d | 10270 | |
c9ccb642 FCE |
10271 | // 2.6.35 added the NOARGS variant, but it's the same for us |
10272 | osrc << "#undef DECLARE_TRACE_NOARGS" << endl; | |
10273 | osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl; | |
10274 | osrc << " DECLARE_TRACE(name, void, )" << endl; | |
832f100d JS |
10275 | |
10276 | // 2.6.38 added the CONDITION variant, which can also just redirect | |
10277 | osrc << "#undef DECLARE_TRACE_CONDITION" << endl; | |
10278 | osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl; | |
10279 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; | |
10280 | ||
c9ccb642 FCE |
10281 | // older tracepoints used DEFINE_TRACE, so redirect that too |
10282 | osrc << "#undef DEFINE_TRACE" << endl; | |
10283 | osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl; | |
832f100d JS |
10284 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; |
10285 | ||
c9ccb642 FCE |
10286 | // add the specified decls/#includes |
10287 | for (unsigned z=0; z<short_decls.size(); z++) | |
10288 | osrc << "#undef TRACE_INCLUDE_FILE\n" | |
10289 | << "#undef TRACE_INCLUDE_PATH\n" | |
10290 | << short_decls[z] << "\n"; | |
10291 | ||
10292 | // finish up the module source | |
10293 | osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl; | |
47dd066d | 10294 | |
c9ccb642 FCE |
10295 | // save the source file away |
10296 | headers_tracequery_src[header] = osrc.str(); | |
55e50c24 | 10297 | } |
f982c59b | 10298 | |
c9ccb642 | 10299 | // now build them all together |
2a0e62a8 | 10300 | map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src); |
47dd066d | 10301 | |
c9ccb642 | 10302 | // now plop them into the cache |
b278033a | 10303 | if (s.use_cache) |
c9ccb642 FCE |
10304 | for (size_t i=0; i<uncached_headers.size(); i++) |
10305 | { | |
10306 | const string& header = uncached_headers[i]; | |
2a0e62a8 JS |
10307 | const string& tracequery_obj = tracequery_objs[header]; |
10308 | const string& tracequery_path = headers_cache_obj[header]; | |
10309 | if (tracequery_obj !="" && file_exists(tracequery_obj)) | |
c9ccb642 | 10310 | { |
2a0e62a8 | 10311 | copy_file(tracequery_obj, tracequery_path, s.verbose > 2); |
c9ccb642 FCE |
10312 | modules.push_back (tracequery_path); |
10313 | } | |
c252fca2 JS |
10314 | else |
10315 | // cache an empty file for failures | |
10316 | copy_file("/dev/null", tracequery_path, s.verbose > 2); | |
c9ccb642 | 10317 | } |
f982c59b JS |
10318 | } |
10319 | ||
10320 | ||
d4393459 | 10321 | |
f982c59b JS |
10322 | bool |
10323 | tracepoint_builder::init_dw(systemtap_session& s) | |
10324 | { | |
10325 | if (dw != NULL) | |
10326 | return true; | |
10327 | ||
10328 | vector<string> tracequery_modules; | |
55e50c24 | 10329 | vector<string> system_headers; |
f982c59b JS |
10330 | |
10331 | glob_t trace_glob; | |
d4393459 FCE |
10332 | |
10333 | // find kernel_source_tree | |
10334 | if (s.kernel_source_tree == "") | |
f982c59b | 10335 | { |
d4393459 FCE |
10336 | unsigned found; |
10337 | DwflPtr dwfl_ptr = setup_dwfl_kernel ("kernel", &found, s); | |
10338 | Dwfl *dwfl = dwfl_ptr.get()->dwfl; | |
10339 | if (found) | |
10340 | { | |
10341 | Dwarf_Die *cudie = 0; | |
10342 | Dwarf_Addr bias; | |
10343 | while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL) | |
10344 | { | |
e19ebcf7 | 10345 | assert_no_interrupts(); |
d4393459 FCE |
10346 | Dwarf_Attribute attr; |
10347 | const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr)); | |
36d65b45 | 10348 | if (name) |
d4393459 | 10349 | { |
36d65b45 JS |
10350 | // check that the path actually exists locally before we try to use it |
10351 | if (file_exists(name)) | |
10352 | { | |
10353 | if (s.verbose > 2) | |
10354 | clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl; | |
10355 | s.kernel_source_tree = name; | |
10356 | } | |
10357 | else | |
10358 | { | |
10359 | if (s.verbose > 2) | |
10360 | clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl; | |
10361 | } | |
61f1a63b | 10362 | |
d4393459 FCE |
10363 | break; // skip others; modern Kbuild uses same comp_dir for them all |
10364 | } | |
10365 | } | |
10366 | } | |
10367 | } | |
10368 | ||
10369 | // prefixes | |
10370 | vector<string> glob_prefixes; | |
10371 | glob_prefixes.push_back (s.kernel_build_tree); | |
10372 | if (s.kernel_source_tree != "") | |
10373 | glob_prefixes.push_back (s.kernel_source_tree); | |
10374 | ||
10375 | // suffixes | |
10376 | vector<string> glob_suffixes; | |
10377 | glob_suffixes.push_back("include/trace/events/*.h"); | |
10378 | glob_suffixes.push_back("include/trace/*.h"); | |
10379 | glob_suffixes.push_back("arch/x86/kvm/*trace.h"); | |
b1966849 | 10380 | glob_suffixes.push_back("arch/x86/include/asm/trace/*.h"); |
f89ff3e2 | 10381 | glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h"); |
ca51c2cd | 10382 | glob_suffixes.push_back("fs/xfs/xfs_trace*.h"); |
d4393459 FCE |
10383 | |
10384 | // compute cartesian product | |
10385 | vector<string> globs; | |
10386 | for (unsigned i=0; i<glob_prefixes.size(); i++) | |
10387 | for (unsigned j=0; j<glob_suffixes.size(); j++) | |
10388 | globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]); | |
10389 | ||
8aa43b8d | 10390 | set<string> duped_headers; |
d4393459 FCE |
10391 | for (unsigned z = 0; z < globs.size(); z++) |
10392 | { | |
10393 | string glob_str = globs[z]; | |
10394 | if (s.verbose > 3) | |
b530b5b3 | 10395 | clog << _("Checking tracepoint glob ") << glob_str << endl; |
d4393459 | 10396 | |
067cc66f CM |
10397 | int r = glob(glob_str.c_str(), 0, NULL, &trace_glob); |
10398 | if (r == GLOB_NOSPACE || r == GLOB_ABORTED) | |
10399 | throw runtime_error("Error globbing tracepoint"); | |
10400 | ||
f982c59b JS |
10401 | for (unsigned i = 0; i < trace_glob.gl_pathc; ++i) |
10402 | { | |
10403 | string header(trace_glob.gl_pathv[i]); | |
10404 | ||
10405 | // filter out a few known "internal-only" headers | |
60d98537 JS |
10406 | if (endswith(header, "/define_trace.h") || |
10407 | endswith(header, "/ftrace.h") || | |
10408 | endswith(header, "/trace_events.h") || | |
10409 | endswith(header, "_event_types.h")) | |
f982c59b JS |
10410 | continue; |
10411 | ||
b1966849 FCE |
10412 | // With headers now plopped under arch/FOO/include/asm/*, |
10413 | // the following logic miss some tracepoints. | |
10414 | #if 0 | |
8aa43b8d JS |
10415 | // skip identical headers from the build and source trees. |
10416 | size_t root_pos = header.rfind("include/"); | |
10417 | if (root_pos != string::npos && | |
10418 | !duped_headers.insert(header.substr(root_pos + 8)).second) | |
10419 | continue; | |
b1966849 | 10420 | #endif |
8aa43b8d | 10421 | |
55e50c24 | 10422 | system_headers.push_back(header); |
f982c59b JS |
10423 | } |
10424 | globfree(&trace_glob); | |
10425 | } | |
10426 | ||
c9ccb642 FCE |
10427 | // Build tracequery modules |
10428 | get_tracequery_modules(s, system_headers, tracequery_modules); | |
55e50c24 | 10429 | |
f982c59b JS |
10430 | // TODO: consider other sources of tracepoint headers too, like from |
10431 | // a command-line parameter or some environment or .systemtaprc | |
47dd066d | 10432 | |
59c11f91 | 10433 | dw = new dwflpp(s, tracequery_modules, true); |
0a6f5a3f JS |
10434 | return true; |
10435 | } | |
47dd066d | 10436 | |
0a6f5a3f JS |
10437 | void |
10438 | tracepoint_builder::build(systemtap_session& s, | |
10439 | probe *base, probe_point *location, | |
10440 | literal_map_t const& parameters, | |
10441 | vector<derived_probe*>& finished_results) | |
10442 | { | |
10443 | if (!init_dw(s)) | |
10444 | return; | |
47dd066d | 10445 | |
75ead1f7 JS |
10446 | string tracepoint; |
10447 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
47dd066d | 10448 | |
75ead1f7 | 10449 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); |
d906ab9d | 10450 | unsigned results_pre = finished_results.size(); |
51178501 | 10451 | dw->iterate_over_modules(&query_module, &q); |
d906ab9d JL |
10452 | unsigned results_post = finished_results.size(); |
10453 | ||
10454 | // Did we fail to find a match? Let's suggest something! | |
10455 | if (results_pre == results_post) | |
10456 | { | |
10457 | size_t pos; | |
10458 | string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint); | |
10459 | while ((pos = sugs.find("stapprobe_")) != string::npos) | |
10460 | sugs.erase(pos, string("stapprobe_").size()); | |
10461 | if (!sugs.empty()) | |
10462 | throw SEMANTIC_ERROR (_NF("no match (similar function: %s)", | |
10463 | "no match (similar functions: %s)", | |
10464 | sugs.find(',') == string::npos, | |
10465 | sugs.c_str())); | |
10466 | } | |
47dd066d | 10467 | } |
47dd066d | 10468 | |
e6fe60e7 | 10469 | |
b55bc428 | 10470 | // ------------------------------------------------------------------------ |
bd2b1e68 | 10471 | // Standard tapset registry. |
b55bc428 FCE |
10472 | // ------------------------------------------------------------------------ |
10473 | ||
7a053d3b | 10474 | void |
f8220a7b | 10475 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 10476 | { |
47e0478e | 10477 | register_tapset_been(s); |
93646f4d | 10478 | register_tapset_itrace(s); |
dd0e4fa7 | 10479 | register_tapset_mark(s); |
7a212aa8 | 10480 | register_tapset_procfs(s); |
912e8c59 | 10481 | register_tapset_timers(s); |
8d9609f5 | 10482 | register_tapset_netfilter(s); |
b84779a5 | 10483 | register_tapset_utrace(s); |
b98a8d73 | 10484 | |
7a24d422 | 10485 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 10486 | dwarf_derived_probe::register_patterns(s); |
30a279be | 10487 | |
888af770 FCE |
10488 | // XXX: user-space starter set |
10489 | s.pattern_root->bind_num(TOK_PROCESS) | |
10490 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
f66bb29a | 10491 | ->bind_privilege(pr_all) |
888af770 FCE |
10492 | ->bind(new uprobe_builder ()); |
10493 | s.pattern_root->bind_num(TOK_PROCESS) | |
10494 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
f66bb29a | 10495 | ->bind_privilege(pr_all) |
888af770 FCE |
10496 | ->bind(new uprobe_builder ()); |
10497 | ||
0a6f5a3f JS |
10498 | // kernel tracepoint probes |
10499 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
10500 | ->bind(new tracepoint_builder()); | |
10501 | ||
e6fe60e7 AM |
10502 | // Kprobe based probe |
10503 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
10504 | ->bind(new kprobe_builder()); | |
3c57fe1f JS |
10505 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL) |
10506 | ->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10507 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10508 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
3c57fe1f JS |
10509 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10510 | ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10511 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
10512 | ->bind(new kprobe_builder()); | |
b6371390 JS |
10513 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
10514 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10515 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10516 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
10517 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10518 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
10519 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10520 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
10521 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
dd225250 PS |
10522 | |
10523 | //Hwbkpt based probe | |
b47f3a55 FCE |
10524 | // NB: we formerly registered the probe point types only if the kernel configuration |
10525 | // allowed it. However, we get better error messages if we allow probes to resolve. | |
10526 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10527 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10528 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10529 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10530 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10531 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10532 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10533 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10534 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10535 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10536 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10537 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10538 | // length supported with address only, not symbol names | |
83ea76b1 WC |
10539 | |
10540 | //perf event based probe | |
4763f713 | 10541 | register_tapset_perf(s); |
a29858ef | 10542 | register_tapset_java(s); |
b55bc428 | 10543 | } |
dc38c0ae DS |
10544 | |
10545 | ||
b20febf3 FCE |
10546 | vector<derived_probe_group*> |
10547 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 10548 | { |
b20febf3 | 10549 | vector<derived_probe_group*> g; |
912e8c59 JS |
10550 | |
10551 | #define DOONE(x) \ | |
10552 | if (s. x##_derived_probes) \ | |
10553 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
10554 | |
10555 | // Note that order *is* important here. We want to make sure we | |
10556 | // register (actually run) begin probes before any other probe type | |
10557 | // is run. Similarly, when unregistering probes, we want to | |
10558 | // unregister (actually run) end probes after every other probe type | |
10559 | // has be unregistered. To do the latter, | |
10560 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
10561 | DOONE(be); |
10562 | DOONE(dwarf); | |
888af770 | 10563 | DOONE(uprobe); |
b20febf3 FCE |
10564 | DOONE(timer); |
10565 | DOONE(profile); | |
10566 | DOONE(mark); | |
0a6f5a3f | 10567 | DOONE(tracepoint); |
e6fe60e7 | 10568 | DOONE(kprobe); |
dd225250 | 10569 | DOONE(hwbkpt); |
83ea76b1 | 10570 | DOONE(perf); |
b20febf3 | 10571 | DOONE(hrtimer); |
ce82316f | 10572 | DOONE(procfs); |
8d9609f5 | 10573 | DOONE(netfilter); |
935447c8 DS |
10574 | |
10575 | // Another "order is important" item. We want to make sure we | |
10576 | // "register" the dummy task_finder probe group after all probe | |
10577 | // groups that use the task_finder. | |
10578 | DOONE(utrace); | |
a96d1db0 | 10579 | DOONE(itrace); |
f31a77f5 | 10580 | DOONE(dynprobe); |
e7d4410d | 10581 | DOONE(java); |
935447c8 | 10582 | DOONE(task_finder); |
b20febf3 FCE |
10583 | #undef DOONE |
10584 | return g; | |
46b84a80 | 10585 | } |
73267b89 JS |
10586 | |
10587 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |