]>
Commit | Line | Data |
---|---|---|
56e12059 | 1 | // tapset resolution |
e3bbc038 | 2 | // Copyright (C) 2005-2012 Red Hat Inc. |
aa30ccd3 | 3 | // Copyright (C) 2005-2007 Intel Corporation. |
0b8f6579 | 4 | // Copyright (C) 2008 James.Bottomley@HansenPartnership.com |
56e12059 FCE |
5 | // |
6 | // This file is part of systemtap, and is free software. You can | |
7 | // redistribute it and/or modify it under the terms of the GNU General | |
8 | // Public License (GPL); either version 2, or (at your option) any | |
9 | // later version. | |
10 | ||
11 | #include "config.h" | |
12 | #include "staptree.h" | |
13 | #include "elaborate.h" | |
b55bc428 | 14 | #include "tapsets.h" |
93646f4d | 15 | #include "task_finder.h" |
56e12059 | 16 | #include "translate.h" |
dc38c0ae | 17 | #include "session.h" |
72dbc915 | 18 | #include "util.h" |
0a6f5a3f | 19 | #include "buildrun.h" |
86bf665e | 20 | #include "dwarf_wrappers.h" |
2e67a43b | 21 | #include "auto_free.h" |
b278033a | 22 | #include "hash.h" |
440f755a | 23 | #include "dwflpp.h" |
5f8ca04f | 24 | #include "setupdwfl.h" |
6b51ee12 | 25 | #include <gelf.h> |
22d737e8 | 26 | |
4d0d9e8a | 27 | #include "sdt_types.h" |
bd2b1e68 | 28 | |
3b579393 FCE |
29 | #include <cstdlib> |
30 | #include <algorithm> | |
bd2b1e68 | 31 | #include <deque> |
56e12059 | 32 | #include <iostream> |
d1bcbe71 | 33 | #include <fstream> |
bd2b1e68 | 34 | #include <map> |
ec4373ff | 35 | #include <set> |
56e12059 | 36 | #include <sstream> |
bd2b1e68 | 37 | #include <stdexcept> |
b55bc428 | 38 | #include <vector> |
e36387d7 | 39 | #include <cstdarg> |
29e64872 | 40 | #include <cassert> |
1969b5bc | 41 | #include <iomanip> |
f781f849 | 42 | #include <cerrno> |
bd2b1e68 GH |
43 | |
44 | extern "C" { | |
df8fadee | 45 | #include <fcntl.h> |
bd2b1e68 | 46 | #include <elfutils/libdwfl.h> |
7a053d3b | 47 | #include <elfutils/libdw.h> |
77de5e9e GH |
48 | #include <dwarf.h> |
49 | #include <elf.h> | |
50 | #include <obstack.h> | |
b20febf3 | 51 | #include <glob.h> |
30a279be | 52 | #include <fnmatch.h> |
5f0a03a6 | 53 | #include <stdio.h> |
349dc70e | 54 | #include <sys/types.h> |
37001baa | 55 | #include <sys/stat.h> |
aaf7ffe8 | 56 | #include <math.h> |
aff5d390 | 57 | #include <regex.h> |
37001baa | 58 | #include <unistd.h> |
6d5d594e | 59 | #include <wordexp.h> |
4b1ad75e RM |
60 | |
61 | #define __STDC_FORMAT_MACROS | |
62 | #include <inttypes.h> | |
bd2b1e68 | 63 | } |
77de5e9e | 64 | |
56e12059 FCE |
65 | |
66 | using namespace std; | |
2171f774 | 67 | using namespace __gnu_cxx; |
56e12059 | 68 | |
47dd066d | 69 | |
b20febf3 FCE |
70 | |
71 | // ------------------------------------------------------------------------ | |
faea5e16 JS |
72 | |
73 | string | |
74 | common_probe_init (derived_probe* p) | |
75 | { | |
26e63673 JS |
76 | assert(p->session_index != (unsigned)-1); |
77 | return "(&stap_probes[" + lex_cast(p->session_index) + "])"; | |
faea5e16 JS |
78 | } |
79 | ||
80 | ||
b20febf3 | 81 | void |
71db462b DS |
82 | common_probe_entryfn_prologue (systemtap_session& s, |
83 | string statestr, string probe, | |
84 | string probe_type, bool overload_processing) | |
b20febf3 | 85 | { |
71db462b DS |
86 | s.op->newline() << "#ifdef STP_ALIBI"; |
87 | s.op->newline() << "atomic_inc(&(" << probe << "->alibi));"; | |
88 | s.op->newline() << "#else"; | |
653e6a9a | 89 | |
71db462b DS |
90 | s.op->newline() << "struct context* __restrict__ c;"; |
91 | s.op->newline() << "#if !INTERRUPTIBLE"; | |
92 | s.op->newline() << "unsigned long flags;"; | |
93 | s.op->newline() << "#endif"; | |
b20febf3 | 94 | |
f887a8c9 DS |
95 | s.op->newline() << "#ifdef STP_TIMING"; |
96 | s.op->newline() << "Stat stat = " << probe << "->timing;"; | |
97 | s.op->newline() << "#endif"; | |
98 | if (overload_processing && !s.runtime_usermode_p()) | |
71db462b | 99 | s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; |
a58d79d0 | 100 | else |
71db462b | 101 | s.op->newline() << "#ifdef STP_TIMING"; |
b20febf3 | 102 | |
f887a8c9 DS |
103 | if (! s.runtime_usermode_p()) |
104 | s.op->newline() << "cycles_t cycles_atstart = get_cycles ();"; | |
105 | else | |
106 | { | |
107 | s.op->newline() << "struct timespec timespec_atstart;"; | |
108 | s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC_RAW, ×pec_atstart);"; | |
109 | } | |
71db462b | 110 | s.op->newline() << "#endif"; |
994aac0e | 111 | |
71db462b DS |
112 | s.op->newline() << "#if INTERRUPTIBLE"; |
113 | s.op->newline() << "preempt_disable ();"; | |
114 | s.op->newline() << "#else"; | |
115 | s.op->newline() << "local_irq_save (flags);"; | |
116 | s.op->newline() << "#endif"; | |
b20febf3 | 117 | |
ac3af990 | 118 | if (! s.runtime_usermode_p()) |
71db462b DS |
119 | { |
120 | // Check for enough free enough stack space | |
121 | s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space | |
122 | s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space | |
123 | // XXX: may need porting to platforms where task_struct is not | |
124 | // at bottom of kernel stack NB: see also | |
125 | // CONFIG_DEBUG_STACKOVERFLOW | |
065d5567 | 126 | s.op->newline() << "atomic_inc (skipped_count());"; |
71db462b | 127 | s.op->newline() << "#ifdef STP_TIMING"; |
065d5567 | 128 | s.op->newline() << "atomic_inc (skipped_count_lowstack());"; |
71db462b DS |
129 | s.op->newline() << "#endif"; |
130 | s.op->newline() << "goto probe_epilogue;"; | |
131 | s.op->newline(-1) << "}"; | |
132 | } | |
c931ec8a | 133 | |
065d5567 | 134 | s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")"; |
71db462b DS |
135 | s.op->newline(1) << "goto probe_epilogue;"; |
136 | s.op->indent(-1); | |
9a604fac | 137 | |
1d0e697d DS |
138 | s.op->newline() << "c = _stp_runtime_entryfn_get_context();"; |
139 | if (s.runtime_usermode_p()) | |
140 | { | |
141 | s.op->newline() << "if (!c) {"; | |
142 | s.op->newline(1) << "#if !INTERRUPTIBLE"; | |
065d5567 | 143 | s.op->newline() << "atomic_inc (skipped_count());"; |
1d0e697d DS |
144 | s.op->newline() << "#endif"; |
145 | s.op->newline() << "#ifdef STP_TIMING"; | |
065d5567 | 146 | s.op->newline() << "atomic_inc (skipped_count_reentrant());"; |
1d0e697d DS |
147 | s.op->newline() << "#ifdef DEBUG_REENTRANCY"; |
148 | s.op->newline() << "_stp_warn (\"Skipped %s\\n\", " << probe << "->pp);"; | |
149 | s.op->newline() << "#endif"; | |
150 | s.op->newline() << "#endif"; | |
151 | s.op->newline() << "goto probe_epilogue;"; | |
152 | s.op->newline(-1) << "}"; | |
153 | } | |
154 | ||
71db462b DS |
155 | s.op->newline() << "if (atomic_inc_return (& c->busy) != 1) {"; |
156 | s.op->newline(1) << "#if !INTERRUPTIBLE"; | |
065d5567 | 157 | s.op->newline() << "atomic_inc (skipped_count());"; |
71db462b DS |
158 | s.op->newline() << "#endif"; |
159 | s.op->newline() << "#ifdef STP_TIMING"; | |
065d5567 | 160 | s.op->newline() << "atomic_inc (skipped_count_reentrant());"; |
71db462b DS |
161 | s.op->newline() << "#ifdef DEBUG_REENTRANCY"; |
162 | s.op->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", " | |
26e63673 | 163 | << probe << "->pp, c->probe_point ?: \"?\", smp_processor_id());"; |
c12d974f FCE |
164 | // NB: There is a conceivable race condition here with reading |
165 | // c->probe_point, knowing that this other probe is sort of running. | |
166 | // However, in reality, it's interrupted. Plus even if it were able | |
167 | // to somehow start again, and stop before we read c->probe_point, | |
168 | // at least we have that ?: "?" bit in there to avoid a NULL deref. | |
71db462b DS |
169 | s.op->newline() << "#endif"; |
170 | s.op->newline() << "#endif"; | |
171 | s.op->newline() << "atomic_dec (& c->busy);"; | |
172 | s.op->newline() << "goto probe_epilogue;"; | |
173 | s.op->newline(-1) << "}"; | |
174 | s.op->newline(); | |
175 | s.op->newline() << "c->last_stmt = 0;"; | |
176 | s.op->newline() << "c->last_error = 0;"; | |
177 | s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter | |
178 | s.op->newline() << "c->uregs = 0;"; | |
179 | s.op->newline() << "c->kregs = 0;"; | |
180 | s.op->newline() << "#if defined __ia64__"; | |
181 | s.op->newline() << "c->unwaddr = 0;"; | |
182 | s.op->newline() << "#endif"; | |
183 | s.op->newline() << "c->probe_point = " << probe << "->pp;"; | |
184 | s.op->newline() << "#ifdef STP_NEED_PROBE_NAME"; | |
185 | s.op->newline() << "c->probe_name = " << probe << "->pn;"; | |
186 | s.op->newline() << "#endif"; | |
187 | s.op->newline() << "c->probe_type = " << probe_type << ";"; | |
6dceb5c9 | 188 | // reset Individual Probe State union |
71db462b | 189 | s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));"; |
e04b5d74 | 190 | s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;"; |
71db462b DS |
191 | s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp |
192 | s.op->newline() << "c->regparm = 0;"; | |
193 | s.op->newline() << "#endif"; | |
e0a17418 | 194 | |
71db462b DS |
195 | s.op->newline() << "#if INTERRUPTIBLE"; |
196 | s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;"; | |
197 | s.op->newline() << "#else"; | |
198 | s.op->newline() << "c->actionremaining = MAXACTION;"; | |
199 | s.op->newline() << "#endif"; | |
9915575b FCE |
200 | // NB: The following would actually be incorrect. |
201 | // That's because cycles_sum/cycles_base values are supposed to survive | |
202 | // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL | |
203 | // cycles), the values will be reset. | |
204 | /* | |
71db462b DS |
205 | s.op->newline() << "#ifdef STP_OVERLOAD"; |
206 | s.op->newline() << "c->cycles_sum = 0;"; | |
207 | s.op->newline() << "c->cycles_base = 0;"; | |
208 | s.op->newline() << "#endif"; | |
9915575b | 209 | */ |
44cfbe25 SM |
210 | |
211 | s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)"; | |
212 | s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;"; | |
213 | s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;"; | |
214 | s.op->newline() << "#endif"; | |
b20febf3 | 215 | } |
9a604fac | 216 | |
a44a0785 | 217 | |
b20febf3 | 218 | void |
f887a8c9 DS |
219 | common_probe_entryfn_epilogue (systemtap_session& s, |
220 | bool overload_processing) | |
b20febf3 | 221 | { |
f887a8c9 DS |
222 | if (overload_processing && !s.runtime_usermode_p()) |
223 | s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)"; | |
a58d79d0 | 224 | else |
f887a8c9 DS |
225 | s.op->newline() << "#ifdef STP_TIMING"; |
226 | s.op->newline() << "{"; | |
227 | s.op->indent(1); | |
228 | if (! s.runtime_usermode_p()) | |
229 | { | |
230 | s.op->newline() << "cycles_t cycles_atend = get_cycles ();"; | |
231 | // NB: we truncate cycles counts to 32 bits. Perhaps it should be | |
232 | // fewer, if the hardware counter rolls over really quickly. We | |
233 | // handle 32-bit wraparound here. | |
234 | s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)"; | |
235 | s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)"; | |
236 | s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;"; | |
237 | s.op->indent(-1); | |
238 | } | |
239 | else | |
240 | { | |
241 | s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;"; | |
242 | s.op->newline() << "long cycles_elapsed;"; | |
243 | s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC_RAW, ×pec_atend);"; | |
244 | s.op->newline() << "_stp_timespec_sub(×pec_atend, ×pec_atstart, ×pec_elapsed);"; | |
245 | // 'cycles_elapsed' is really elapsed nanoseconds | |
246 | s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;"; | |
247 | } | |
248 | ||
249 | s.op->newline() << "#ifdef STP_TIMING"; | |
250 | s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);"; | |
251 | s.op->newline() << "#endif"; | |
252 | ||
253 | if (overload_processing && !s.runtime_usermode_p()) | |
254 | { | |
255 | s.op->newline() << "#ifdef STP_OVERLOAD"; | |
256 | s.op->newline() << "{"; | |
a58d79d0 DS |
257 | // If the cycle count has wrapped (cycles_atend > cycles_base), |
258 | // let's go ahead and pretend the interval has been reached. | |
259 | // This should reset cycles_base and cycles_sum. | |
f887a8c9 DS |
260 | s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)"; |
261 | s.op->newline(1) << "? (cycles_atend - c->cycles_base)"; | |
262 | s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);"; | |
263 | s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;"; | |
a58d79d0 DS |
264 | |
265 | // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a | |
266 | // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe | |
267 | // has overloaded the system and we need to quit. | |
7baf48e9 FCE |
268 | // NB: this is not suppressible via --suppress-runtime-errors, |
269 | // because this is a system safety metric that we cannot trust | |
270 | // unprivileged users to override. | |
f887a8c9 DS |
271 | s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {"; |
272 | s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {"; | |
273 | s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");"; | |
065d5567 JS |
274 | s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);"; |
275 | s.op->newline() << "atomic_inc (error_count());"; | |
f887a8c9 DS |
276 | s.op->newline(-1) << "}"; |
277 | ||
278 | s.op->newline() << "c->cycles_base = cycles_atend;"; | |
279 | s.op->newline() << "c->cycles_sum = 0;"; | |
280 | s.op->newline(-1) << "}"; | |
281 | s.op->newline(-1) << "}"; | |
282 | s.op->newline() << "#endif"; | |
a58d79d0 | 283 | } |
e57b735a | 284 | |
f887a8c9 DS |
285 | s.op->newline(-1) << "}"; |
286 | s.op->newline() << "#endif"; | |
e57b735a | 287 | |
f887a8c9 DS |
288 | s.op->newline() << "c->probe_point = 0;"; // vacated |
289 | s.op->newline() << "#ifdef STP_NEED_PROBE_NAME"; | |
290 | s.op->newline() << "c->probe_name = 0;"; | |
291 | s.op->newline() << "#endif"; | |
292 | s.op->newline() << "c->probe_type = 0;"; | |
7baf48e9 FCE |
293 | |
294 | ||
f887a8c9 DS |
295 | s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {"; |
296 | s.op->indent(1); | |
297 | if (s.suppress_handler_errors) // PR 13306 | |
7baf48e9 | 298 | { |
065d5567 | 299 | s.op->newline() << "atomic_inc (error_count());"; |
7baf48e9 FCE |
300 | } |
301 | else | |
302 | { | |
f887a8c9 DS |
303 | s.op->newline() << "if (c->last_stmt != NULL)"; |
304 | s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);"; | |
305 | s.op->newline(-1) << "else"; | |
306 | s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);"; | |
307 | s.op->indent(-1); | |
065d5567 JS |
308 | s.op->newline() << "atomic_inc (error_count());"; |
309 | s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {"; | |
310 | s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);"; | |
f887a8c9 DS |
311 | s.op->newline() << "_stp_exit ();"; |
312 | s.op->newline(-1) << "}"; | |
7baf48e9 FCE |
313 | } |
314 | ||
f887a8c9 | 315 | s.op->newline(-1) << "}"; |
7baf48e9 FCE |
316 | |
317 | ||
f887a8c9 | 318 | s.op->newline() << "atomic_dec (&c->busy);"; |
e57b735a | 319 | |
f887a8c9 DS |
320 | s.op->newline(-1) << "probe_epilogue:"; // context is free |
321 | s.op->indent(1); | |
e57b735a | 322 | |
1d0e697d | 323 | s.op->newline() << "_stp_runtime_entryfn_put_context();"; |
f887a8c9 | 324 | if (! s.suppress_handler_errors) // PR 13306 |
7baf48e9 FCE |
325 | { |
326 | // Check for excessive skip counts. | |
065d5567 JS |
327 | s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {"; |
328 | s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))"; | |
f887a8c9 DS |
329 | s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");"; |
330 | s.op->newline(-1) << "}"; | |
7baf48e9 | 331 | } |
e57b735a | 332 | |
f887a8c9 DS |
333 | s.op->newline() << "#if INTERRUPTIBLE"; |
334 | s.op->newline() << "preempt_enable_no_resched ();"; | |
335 | s.op->newline() << "#else"; | |
336 | s.op->newline() << "local_irq_restore (flags);"; | |
337 | s.op->newline() << "#endif"; | |
653e6a9a | 338 | |
f887a8c9 | 339 | s.op->newline() << "#endif // STP_ALIBI"; |
440f755a | 340 | } |
e57b735a | 341 | |
e57b735a | 342 | |
440f755a | 343 | // ------------------------------------------------------------------------ |
e57b735a | 344 | |
440f755a JS |
345 | // ------------------------------------------------------------------------ |
346 | // Dwarf derived probes. "We apologize for the inconvience." | |
347 | // ------------------------------------------------------------------------ | |
e57b735a | 348 | |
4627ed58 JS |
349 | static const string TOK_KERNEL("kernel"); |
350 | static const string TOK_MODULE("module"); | |
351 | static const string TOK_FUNCTION("function"); | |
352 | static const string TOK_INLINE("inline"); | |
353 | static const string TOK_CALL("call"); | |
4bda987e | 354 | static const string TOK_EXPORTED("exported"); |
4627ed58 JS |
355 | static const string TOK_RETURN("return"); |
356 | static const string TOK_MAXACTIVE("maxactive"); | |
357 | static const string TOK_STATEMENT("statement"); | |
358 | static const string TOK_ABSOLUTE("absolute"); | |
359 | static const string TOK_PROCESS("process"); | |
a794dbeb | 360 | static const string TOK_PROVIDER("provider"); |
4627ed58 JS |
361 | static const string TOK_MARK("mark"); |
362 | static const string TOK_TRACE("trace"); | |
363 | static const string TOK_LABEL("label"); | |
63b4fd14 | 364 | static const string TOK_LIBRARY("library"); |
576eaefe | 365 | static const string TOK_PLT("plt"); |
e57b735a | 366 | |
1adf8ef1 | 367 | static int query_cu (Dwarf_Die * cudie, void * arg); |
6b517475 | 368 | static void query_addr(Dwarf_Addr addr, dwarf_query *q); |
e57b735a | 369 | |
440f755a JS |
370 | // Can we handle this query with just symbol-table info? |
371 | enum dbinfo_reqt | |
372 | { | |
373 | dbr_unknown, | |
374 | dbr_none, // kernel.statement(NUM).absolute | |
375 | dbr_need_symtab, // can get by with symbol table if there's no dwarf | |
376 | dbr_need_dwarf | |
377 | }; | |
e57b735a | 378 | |
20e4a32c | 379 | |
440f755a JS |
380 | struct base_query; // forward decls |
381 | struct dwarf_query; | |
382 | struct dwflpp; | |
383 | struct symbol_table; | |
20e4a32c | 384 | |
a781f401 | 385 | |
440f755a JS |
386 | struct |
387 | symbol_table | |
388 | { | |
389 | module_info *mod_info; // associated module | |
390 | map<string, func_info*> map_by_name; | |
1c6b77e5 JS |
391 | multimap<Dwarf_Addr, func_info*> map_by_addr; |
392 | typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t; | |
440f755a JS |
393 | typedef pair<iterator_t, iterator_t> range_t; |
394 | #ifdef __powerpc__ | |
395 | GElf_Word opd_section; | |
396 | #endif | |
2867a2a1 JS |
397 | void add_symbol(const char *name, bool weak, bool descriptor, |
398 | Dwarf_Addr addr, Dwarf_Addr *high_addr); | |
440f755a | 399 | enum info_status read_symbols(FILE *f, const string& path); |
83ca3872 | 400 | enum info_status read_from_elf_file(const string& path, |
2713ea24 | 401 | systemtap_session &sess); |
83ca3872 | 402 | enum info_status read_from_text_file(const string& path, |
2713ea24 | 403 | systemtap_session &sess); |
440f755a JS |
404 | enum info_status get_from_elf(); |
405 | void prepare_section_rejection(Dwfl_Module *mod); | |
406 | bool reject_section(GElf_Word section); | |
440f755a JS |
407 | void purge_syscall_stubs(); |
408 | func_info *lookup_symbol(const string& name); | |
409 | Dwarf_Addr lookup_symbol_address(const string& name); | |
410 | func_info *get_func_containing_address(Dwarf_Addr addr); | |
3d372d6b | 411 | func_info *get_first_func(); |
7a053d3b | 412 | |
440f755a JS |
413 | symbol_table(module_info *mi) : mod_info(mi) {} |
414 | ~symbol_table(); | |
415 | }; | |
77de5e9e | 416 | |
440f755a JS |
417 | static bool null_die(Dwarf_Die *die) |
418 | { | |
822a6a3d | 419 | static Dwarf_Die null; |
440f755a JS |
420 | return (!die || !memcmp(die, &null, sizeof(null))); |
421 | } | |
c4ce66a1 JS |
422 | |
423 | ||
7a053d3b | 424 | enum |
bd2b1e68 | 425 | function_spec_type |
7a053d3b | 426 | { |
bd2b1e68 GH |
427 | function_alone, |
428 | function_and_file, | |
7a053d3b | 429 | function_file_and_line |
bd2b1e68 GH |
430 | }; |
431 | ||
ec4373ff | 432 | |
bd2b1e68 | 433 | struct dwarf_builder; |
f10534c6 | 434 | struct dwarf_var_expanding_visitor; |
77de5e9e | 435 | |
2930abc7 | 436 | |
b20febf3 FCE |
437 | // XXX: This class is a candidate for subclassing to separate |
438 | // the relocation vs non-relocation variants. Likewise for | |
439 | // kprobe vs kretprobe variants. | |
440 | ||
441 | struct dwarf_derived_probe: public derived_probe | |
b55bc428 | 442 | { |
b20febf3 FCE |
443 | dwarf_derived_probe (const string& function, |
444 | const string& filename, | |
445 | int line, | |
446 | const string& module, | |
447 | const string& section, | |
448 | Dwarf_Addr dwfl_addr, | |
2930abc7 | 449 | Dwarf_Addr addr, |
b20febf3 FCE |
450 | dwarf_query & q, |
451 | Dwarf_Die* scope_die); | |
20e4a32c | 452 | |
b20febf3 FCE |
453 | string module; |
454 | string section; | |
455 | Dwarf_Addr addr; | |
63b4fd14 | 456 | string path; |
27dc09b1 | 457 | bool has_process; |
2930abc7 | 458 | bool has_return; |
c9bad430 | 459 | bool has_maxactive; |
63b4fd14 | 460 | bool has_library; |
c9bad430 | 461 | long maxactive_val; |
4ad95bbc | 462 | // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition |
b642c901 SC |
463 | string user_path; |
464 | string user_lib; | |
b95e2b79 | 465 | bool access_vars; |
2930abc7 | 466 | |
af234c40 JS |
467 | unsigned saved_longs, saved_strings; |
468 | dwarf_derived_probe* entry_handler; | |
469 | ||
b8da0ad1 | 470 | void printsig (std::ostream &o) const; |
6b66b9f7 | 471 | virtual void join_group (systemtap_session& s); |
9020300d | 472 | void emit_probe_local_init(translator_output * o); |
d0bfd2ac | 473 | void getargs(std::list<std::string> &arg_set) const; |
0a98fd42 | 474 | |
42e38653 | 475 | void emit_privilege_assertion (translator_output*); |
27dc09b1 DB |
476 | void print_dupe_stamp(ostream& o); |
477 | ||
bd2b1e68 | 478 | // Pattern registration helpers. |
7a053d3b | 479 | static void register_statement_variants(match_node * root, |
27dc09b1 | 480 | dwarf_builder * dw, |
42e38653 | 481 | privilege_t privilege); |
fd6602a0 | 482 | static void register_function_variants(match_node * root, |
27dc09b1 | 483 | dwarf_builder * dw, |
42e38653 | 484 | privilege_t privilege); |
440d9b00 DB |
485 | static void register_function_and_statement_variants(systemtap_session& s, |
486 | match_node * root, | |
27dc09b1 | 487 | dwarf_builder * dw, |
42e38653 | 488 | privilege_t privilege); |
b1615c74 JS |
489 | static void register_sdt_variants(systemtap_session& s, |
490 | match_node * root, | |
491 | dwarf_builder * dw); | |
492 | static void register_plt_variants(systemtap_session& s, | |
493 | match_node * root, | |
494 | dwarf_builder * dw); | |
c4ce66a1 | 495 | static void register_patterns(systemtap_session& s); |
6b66b9f7 JS |
496 | |
497 | protected: | |
498 | dwarf_derived_probe(probe *base, | |
499 | probe_point *location, | |
500 | Dwarf_Addr addr, | |
501 | bool has_return): | |
74fe61bc LB |
502 | derived_probe(base, location), addr(addr), has_process(0), |
503 | has_return(has_return), has_maxactive(0), has_library(0), | |
504 | maxactive_val(0), access_vars(false), saved_longs(0), | |
505 | saved_strings(0), entry_handler(0) | |
6b66b9f7 JS |
506 | {} |
507 | ||
508 | private: | |
d0bfd2ac | 509 | list<string> args; |
8c67c337 | 510 | void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr); |
20c6c071 GH |
511 | }; |
512 | ||
dc38c0ae | 513 | |
6b66b9f7 | 514 | struct uprobe_derived_probe: public dwarf_derived_probe |
6d0f3f0c | 515 | { |
6d0f3f0c | 516 | int pid; // 0 => unrestricted |
0973d815 | 517 | |
6d0f3f0c FCE |
518 | uprobe_derived_probe (const string& function, |
519 | const string& filename, | |
520 | int line, | |
521 | const string& module, | |
6d0f3f0c FCE |
522 | const string& section, |
523 | Dwarf_Addr dwfl_addr, | |
524 | Dwarf_Addr addr, | |
525 | dwarf_query & q, | |
6b66b9f7 JS |
526 | Dwarf_Die* scope_die): |
527 | dwarf_derived_probe(function, filename, line, module, section, | |
528 | dwfl_addr, addr, q, scope_die), pid(0) | |
529 | {} | |
6d0f3f0c | 530 | |
0973d815 FCE |
531 | // alternate constructor for process(PID).statement(ADDR).absolute |
532 | uprobe_derived_probe (probe *base, | |
533 | probe_point *location, | |
534 | int pid, | |
535 | Dwarf_Addr addr, | |
6b66b9f7 JS |
536 | bool has_return): |
537 | dwarf_derived_probe(base, location, addr, has_return), pid(pid) | |
538 | {} | |
9ace370f | 539 | |
6d0f3f0c | 540 | void join_group (systemtap_session& s); |
2865d17a | 541 | |
42e38653 | 542 | void emit_privilege_assertion (translator_output*); |
8f6d8c2b | 543 | void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); } |
c0f84e7b SC |
544 | void getargs(std::list<std::string> &arg_set) const; |
545 | void saveargs(int nargs); | |
546 | private: | |
547 | list<string> args; | |
6d0f3f0c FCE |
548 | }; |
549 | ||
dc38c0ae DS |
550 | struct dwarf_derived_probe_group: public derived_probe_group |
551 | { | |
552 | private: | |
b20febf3 FCE |
553 | multimap<string,dwarf_derived_probe*> probes_by_module; |
554 | typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator; | |
dc38c0ae DS |
555 | |
556 | public: | |
08b5a50c | 557 | dwarf_derived_probe_group() {} |
b20febf3 FCE |
558 | void enroll (dwarf_derived_probe* probe); |
559 | void emit_module_decls (systemtap_session& s); | |
560 | void emit_module_init (systemtap_session& s); | |
b4be7cbc | 561 | void emit_module_refresh (systemtap_session& s); |
b20febf3 | 562 | void emit_module_exit (systemtap_session& s); |
dc38c0ae DS |
563 | }; |
564 | ||
565 | ||
20c6c071 | 566 | // Helper struct to thread through the dwfl callbacks. |
2c384610 | 567 | struct base_query |
20c6c071 | 568 | { |
c4ce66a1 JS |
569 | base_query(dwflpp & dw, literal_map_t const & params); |
570 | base_query(dwflpp & dw, const string & module_val); | |
2c384610 | 571 | virtual ~base_query() {} |
bd2b1e68 | 572 | |
5227f1ea | 573 | systemtap_session & sess; |
2c384610 | 574 | dwflpp & dw; |
5227f1ea | 575 | |
bd2b1e68 | 576 | // Parameter extractors. |
86bf665e | 577 | static bool has_null_param(literal_map_t const & params, |
888af770 | 578 | string const & k); |
86bf665e | 579 | static bool get_string_param(literal_map_t const & params, |
bd2b1e68 | 580 | string const & k, string & v); |
86bf665e | 581 | static bool get_number_param(literal_map_t const & params, |
bd2b1e68 | 582 | string const & k, long & v); |
86bf665e | 583 | static bool get_number_param(literal_map_t const & params, |
c239d28c | 584 | string const & k, Dwarf_Addr & v); |
f301a9ff | 585 | static void query_library_callback (void *object, const char *data); |
576eaefe | 586 | static void query_plt_callback (void *object, const char *link, size_t addr); |
f301a9ff | 587 | virtual void query_library (const char *data) = 0; |
576eaefe | 588 | virtual void query_plt (const char *link, size_t addr) = 0; |
84c84ac4 | 589 | |
b55bc428 | 590 | |
2c384610 DS |
591 | // Extracted parameters. |
592 | bool has_kernel; | |
91af0778 FCE |
593 | bool has_module; |
594 | bool has_process; | |
63b4fd14 | 595 | bool has_library; |
576eaefe SC |
596 | bool has_plt; |
597 | bool has_statement; | |
2c384610 | 598 | string module_val; // has_kernel => module_val = "kernel" |
63b4fd14 | 599 | string path; // executable path if module is a .so |
576eaefe | 600 | string plt_val; // has_plt => plt wildcard |
2c384610 DS |
601 | |
602 | virtual void handle_query_module() = 0; | |
603 | }; | |
604 | ||
605 | ||
c4ce66a1 | 606 | base_query::base_query(dwflpp & dw, literal_map_t const & params): |
576eaefe | 607 | sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false) |
2c384610 | 608 | { |
91af0778 | 609 | has_kernel = has_null_param (params, TOK_KERNEL); |
2c384610 DS |
610 | if (has_kernel) |
611 | module_val = "kernel"; | |
91af0778 FCE |
612 | |
613 | has_module = get_string_param (params, TOK_MODULE, module_val); | |
614 | if (has_module) | |
615 | has_process = false; | |
4baf0e53 | 616 | else |
d0a7f5a9 | 617 | { |
63b4fd14 | 618 | string library_name; |
576eaefe | 619 | long statement_num_val; |
d0a7f5a9 | 620 | has_process = get_string_param(params, TOK_PROCESS, module_val); |
63b4fd14 | 621 | has_library = get_string_param (params, TOK_LIBRARY, library_name); |
576eaefe SC |
622 | if ((has_plt = has_null_param (params, TOK_PLT))) |
623 | plt_val = "*"; | |
624 | else has_plt = get_string_param (params, TOK_PLT, plt_val); | |
18418d34 SC |
625 | if (has_plt) |
626 | sess.consult_symtab = true; | |
576eaefe SC |
627 | has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val); |
628 | ||
84c84ac4 | 629 | if (has_process) |
05fb3e0c | 630 | module_val = find_executable (module_val, sess.sysroot, sess.sysenv); |
84c84ac4 SC |
631 | if (has_library) |
632 | { | |
633 | if (! contains_glob_chars (library_name)) | |
634 | { | |
05fb3e0c WF |
635 | path = path_remove_sysroot(sess, module_val); |
636 | module_val = find_executable (library_name, sess.sysroot, | |
637 | sess.sysenv, "LD_LIBRARY_PATH"); | |
47e226ed SC |
638 | if (module_val.find('/') == string::npos) |
639 | { | |
640 | // We didn't find library_name so use iterate_over_libraries | |
641 | module_val = path; | |
642 | path = library_name; | |
643 | } | |
84c84ac4 SC |
644 | } |
645 | else | |
646 | path = library_name; | |
647 | } | |
d0a7f5a9 | 648 | } |
91af0778 FCE |
649 | |
650 | assert (has_kernel || has_process || has_module); | |
2c384610 DS |
651 | } |
652 | ||
c4ce66a1 | 653 | base_query::base_query(dwflpp & dw, const string & module_val) |
576eaefe SC |
654 | : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false), |
655 | module_val(module_val) | |
c4ce66a1 JS |
656 | { |
657 | // NB: This uses '/' to distinguish between kernel modules and userspace, | |
658 | // which means that userspace modules won't get any PATH searching. | |
659 | if (module_val.find('/') == string::npos) | |
660 | { | |
661 | has_kernel = (module_val == TOK_KERNEL); | |
662 | has_module = !has_kernel; | |
663 | has_process = false; | |
664 | } | |
665 | else | |
666 | { | |
667 | has_kernel = has_module = false; | |
668 | has_process = true; | |
669 | } | |
670 | } | |
671 | ||
2c384610 | 672 | bool |
86bf665e | 673 | base_query::has_null_param(literal_map_t const & params, |
2c384610 DS |
674 | string const & k) |
675 | { | |
888af770 | 676 | return derived_probe_builder::has_null_param(params, k); |
2c384610 DS |
677 | } |
678 | ||
679 | ||
680 | bool | |
86bf665e | 681 | base_query::get_string_param(literal_map_t const & params, |
2c384610 DS |
682 | string const & k, string & v) |
683 | { | |
684 | return derived_probe_builder::get_param (params, k, v); | |
685 | } | |
686 | ||
687 | ||
688 | bool | |
86bf665e | 689 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
690 | string const & k, long & v) |
691 | { | |
692 | int64_t value; | |
693 | bool present = derived_probe_builder::get_param (params, k, value); | |
694 | v = (long) value; | |
695 | return present; | |
696 | } | |
697 | ||
698 | ||
699 | bool | |
86bf665e | 700 | base_query::get_number_param(literal_map_t const & params, |
2c384610 DS |
701 | string const & k, Dwarf_Addr & v) |
702 | { | |
703 | int64_t value; | |
704 | bool present = derived_probe_builder::get_param (params, k, value); | |
705 | v = (Dwarf_Addr) value; | |
706 | return present; | |
707 | } | |
708 | ||
2c384610 DS |
709 | struct dwarf_query : public base_query |
710 | { | |
e1278bd4 | 711 | dwarf_query(probe * base_probe, |
2c384610 DS |
712 | probe_point * base_loc, |
713 | dwflpp & dw, | |
86bf665e | 714 | literal_map_t const & params, |
b642c901 SC |
715 | vector<derived_probe *> & results, |
716 | const string user_path, | |
717 | const string user_lib); | |
2c384610 | 718 | |
c4ce66a1 | 719 | vector<derived_probe *> & results; |
8f14e444 | 720 | set<string> inlined_non_returnable; // function names |
c4ce66a1 JS |
721 | probe * base_probe; |
722 | probe_point * base_loc; | |
b642c901 SC |
723 | string user_path; |
724 | string user_lib; | |
c4ce66a1 | 725 | |
2c384610 | 726 | virtual void handle_query_module(); |
5f0a03a6 JK |
727 | void query_module_dwarf(); |
728 | void query_module_symtab(); | |
5d5bd369 | 729 | void query_library (const char *data); |
576eaefe | 730 | void query_plt (const char *entry, size_t addr); |
2c384610 | 731 | |
2930abc7 FCE |
732 | void add_probe_point(string const & funcname, |
733 | char const * filename, | |
734 | int line, | |
735 | Dwarf_Die *scope_die, | |
736 | Dwarf_Addr addr); | |
36f9dd1d | 737 | |
857bdfd1 JS |
738 | // Track addresses we've already seen in a given module |
739 | set<Dwarf_Addr> alias_dupes; | |
740 | ||
7fdd3e2c JS |
741 | // Track inlines we've already seen as well |
742 | // NB: this can't be compared just by entrypc, as inlines can overlap | |
743 | set<inline_instance_info> inline_dupes; | |
744 | ||
2930abc7 | 745 | // Extracted parameters. |
7a053d3b | 746 | string function_val; |
20c6c071 GH |
747 | |
748 | bool has_function_str; | |
749 | bool has_statement_str; | |
750 | bool has_function_num; | |
751 | bool has_statement_num; | |
7a053d3b RM |
752 | string statement_str_val; |
753 | string function_str_val; | |
c239d28c GH |
754 | Dwarf_Addr statement_num_val; |
755 | Dwarf_Addr function_num_val; | |
20c6c071 | 756 | |
b8da0ad1 | 757 | bool has_call; |
4bda987e | 758 | bool has_exported; |
b8da0ad1 | 759 | bool has_inline; |
20c6c071 GH |
760 | bool has_return; |
761 | ||
c9bad430 DS |
762 | bool has_maxactive; |
763 | long maxactive_val; | |
764 | ||
20c6c071 GH |
765 | bool has_label; |
766 | string label_val; | |
767 | ||
768 | bool has_relative; | |
769 | long relative_val; | |
770 | ||
37ebca01 FCE |
771 | bool has_absolute; |
772 | ||
467bea43 SC |
773 | bool has_mark; |
774 | ||
5f0a03a6 JK |
775 | enum dbinfo_reqt dbinfo_reqt; |
776 | enum dbinfo_reqt assess_dbinfo_reqt(); | |
777 | ||
7d6d0afc | 778 | void parse_function_spec(const string & spec); |
20c6c071 | 779 | function_spec_type spec_type; |
7d6d0afc | 780 | vector<string> scopes; |
20c6c071 GH |
781 | string function; |
782 | string file; | |
0c8b7d37 | 783 | line_t line_type; |
879eb9e9 | 784 | int line[2]; |
5f0a03a6 | 785 | bool query_done; // Found exact match |
20c6c071 | 786 | |
bd25380d | 787 | set<string> filtered_srcfiles; |
7e1279ea FCE |
788 | |
789 | // Map official entrypc -> func_info object | |
86bf665e TM |
790 | inline_instance_map_t filtered_inlines; |
791 | func_info_map_t filtered_functions; | |
7e1279ea FCE |
792 | bool choose_next_line; |
793 | Dwarf_Addr entrypc_for_next_line; | |
4df79aaf JS |
794 | |
795 | void query_module_functions (); | |
b55bc428 FCE |
796 | }; |
797 | ||
98afd80e | 798 | |
435f53a7 FCE |
799 | static void delete_session_module_cache (systemtap_session& s); // forward decl |
800 | ||
801 | ||
98afd80e | 802 | struct dwarf_builder: public derived_probe_builder |
b55bc428 | 803 | { |
665e1256 | 804 | map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */ |
7a24d422 | 805 | map <string,dwflpp*> user_dw; |
b642c901 SC |
806 | string user_path; |
807 | string user_lib; | |
ae2552da | 808 | dwarf_builder() {} |
aa30ccd3 | 809 | |
ae2552da | 810 | dwflpp *get_kern_dw(systemtap_session& sess, const string& module) |
707bf35e | 811 | { |
ea14cf67 FCE |
812 | if (kern_dw[module] == 0) |
813 | kern_dw[module] = new dwflpp(sess, module, true); // might throw | |
ae2552da | 814 | return kern_dw[module]; |
707bf35e JS |
815 | } |
816 | ||
817 | dwflpp *get_user_dw(systemtap_session& sess, const string& module) | |
818 | { | |
ea14cf67 FCE |
819 | if (user_dw[module] == 0) |
820 | user_dw[module] = new dwflpp(sess, module, false); // might throw | |
707bf35e JS |
821 | return user_dw[module]; |
822 | } | |
7a24d422 FCE |
823 | |
824 | /* NB: not virtual, so can be called from dtor too: */ | |
822a6a3d | 825 | void dwarf_build_no_more (bool) |
aa30ccd3 | 826 | { |
435f53a7 FCE |
827 | delete_map(kern_dw); |
828 | delete_map(user_dw); | |
7a24d422 FCE |
829 | } |
830 | ||
831 | void build_no_more (systemtap_session &s) | |
832 | { | |
833 | dwarf_build_no_more (s.verbose > 3); | |
435f53a7 | 834 | delete_session_module_cache (s); |
aa30ccd3 FCE |
835 | } |
836 | ||
e38d6504 RM |
837 | ~dwarf_builder() |
838 | { | |
7a24d422 | 839 | dwarf_build_no_more (false); |
c8959a29 | 840 | } |
aa30ccd3 | 841 | |
5227f1ea | 842 | virtual void build(systemtap_session & sess, |
7a053d3b | 843 | probe * base, |
20c6c071 | 844 | probe_point * location, |
86bf665e | 845 | literal_map_t const & parameters, |
20c6c071 | 846 | vector<derived_probe *> & finished_results); |
b55bc428 FCE |
847 | }; |
848 | ||
5111fc3e | 849 | |
e1278bd4 | 850 | dwarf_query::dwarf_query(probe * base_probe, |
20c6c071 GH |
851 | probe_point * base_loc, |
852 | dwflpp & dw, | |
86bf665e | 853 | literal_map_t const & params, |
b642c901 SC |
854 | vector<derived_probe *> & results, |
855 | const string user_path, | |
856 | const string user_lib) | |
c4ce66a1 | 857 | : base_query(dw, params), results(results), |
b642c901 | 858 | base_probe(base_probe), base_loc(base_loc), |
74fe61bc LB |
859 | user_path(user_path), user_lib(user_lib), has_relative(false), |
860 | relative_val(0), choose_next_line(false), entrypc_for_next_line(0) | |
bd2b1e68 GH |
861 | { |
862 | // Reduce the query to more reasonable semantic values (booleans, | |
863 | // extracted strings, numbers, etc). | |
bd2b1e68 GH |
864 | has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val); |
865 | has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val); | |
866 | ||
867 | has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val); | |
868 | has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val); | |
869 | ||
0f336e95 SC |
870 | has_label = get_string_param(params, TOK_LABEL, label_val); |
871 | ||
b8da0ad1 | 872 | has_call = has_null_param(params, TOK_CALL); |
4bda987e | 873 | has_exported = has_null_param(params, TOK_EXPORTED); |
b8da0ad1 | 874 | has_inline = has_null_param(params, TOK_INLINE); |
bd2b1e68 | 875 | has_return = has_null_param(params, TOK_RETURN); |
c9bad430 | 876 | has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val); |
37ebca01 | 877 | has_absolute = has_null_param(params, TOK_ABSOLUTE); |
467bea43 | 878 | has_mark = false; |
37ebca01 | 879 | |
bd2b1e68 | 880 | if (has_function_str) |
7d6d0afc | 881 | parse_function_spec(function_str_val); |
bd2b1e68 | 882 | else if (has_statement_str) |
7d6d0afc | 883 | parse_function_spec(statement_str_val); |
0daad364 | 884 | |
5f0a03a6 JK |
885 | dbinfo_reqt = assess_dbinfo_reqt(); |
886 | query_done = false; | |
0daad364 JS |
887 | } |
888 | ||
889 | ||
440f755a JS |
890 | func_info_map_t * |
891 | get_filtered_functions(dwarf_query *q) | |
892 | { | |
893 | return &q->filtered_functions; | |
894 | } | |
895 | ||
896 | ||
897 | inline_instance_map_t * | |
898 | get_filtered_inlines(dwarf_query *q) | |
899 | { | |
900 | return &q->filtered_inlines; | |
901 | } | |
902 | ||
903 | ||
2c384610 | 904 | void |
5f0a03a6 | 905 | dwarf_query::query_module_dwarf() |
2c384610 DS |
906 | { |
907 | if (has_function_num || has_statement_num) | |
908 | { | |
909 | // If we have module("foo").function(0xbeef) or | |
910 | // module("foo").statement(0xbeef), the address is relative | |
911 | // to the start of the module, so we seek the function | |
912 | // number plus the module's bias. | |
6b517475 JS |
913 | Dwarf_Addr addr = has_function_num ? |
914 | function_num_val : statement_num_val; | |
08d1d520 MW |
915 | |
916 | // These are raw addresses, we need to know what the elf_bias | |
917 | // is to feed it to libdwfl based functions. | |
918 | Dwarf_Addr elf_bias; | |
919 | Elf *elf = dwfl_module_getelf (dw.module, &elf_bias); | |
920 | assert(elf); | |
921 | addr += elf_bias; | |
6b517475 | 922 | query_addr(addr, this); |
2c384610 DS |
923 | } |
924 | else | |
925 | { | |
926 | // Otherwise if we have a function("foo") or statement("foo") | |
927 | // specifier, we have to scan over all the CUs looking for | |
928 | // the function(s) in question | |
929 | assert(has_function_str || has_statement_str); | |
4df79aaf JS |
930 | |
931 | // For simple cases, no wildcard and no source:line, we can do a very | |
932 | // quick function lookup in a module-wide cache. | |
1ffb8bd1 JS |
933 | if (spec_type == function_alone && |
934 | !dw.name_has_wildcard(function) && | |
935 | !startswith(function, "_Z")) | |
4df79aaf JS |
936 | query_module_functions(); |
937 | else | |
337b7c44 | 938 | dw.iterate_over_cus(&query_cu, this, false); |
2c384610 DS |
939 | } |
940 | } | |
941 | ||
5f0a03a6 JK |
942 | static void query_func_info (Dwarf_Addr entrypc, func_info & fi, |
943 | dwarf_query * q); | |
944 | ||
945 | void | |
946 | dwarf_query::query_module_symtab() | |
947 | { | |
948 | // Get the symbol table if it's necessary, sufficient, and not already got. | |
949 | if (dbinfo_reqt == dbr_need_dwarf) | |
950 | return; | |
951 | ||
952 | module_info *mi = dw.mod_info; | |
953 | if (dbinfo_reqt == dbr_need_symtab) | |
954 | { | |
955 | if (mi->symtab_status == info_unknown) | |
956 | mi->get_symtab(this); | |
957 | if (mi->symtab_status == info_absent) | |
958 | return; | |
959 | } | |
960 | ||
961 | func_info *fi = NULL; | |
962 | symbol_table *sym_table = mi->sym_table; | |
963 | ||
964 | if (has_function_str) | |
965 | { | |
966 | // Per dwarf_query::assess_dbinfo_reqt()... | |
967 | assert(spec_type == function_alone); | |
968 | if (dw.name_has_wildcard(function_str_val)) | |
969 | { | |
970 | // Until we augment the blacklist sufficently... | |
971 | if (function_str_val.find_first_not_of("*?") == string::npos) | |
972 | { | |
973 | // e.g., kernel.function("*") | |
b530b5b3 LB |
974 | cerr << _F("Error: Pattern '%s' matches every single " |
975 | "instruction address in the symbol table,\n" | |
976 | "some of which aren't even functions.\n", function_str_val.c_str()) << endl; | |
5f0a03a6 JK |
977 | return; |
978 | } | |
2e67a43b | 979 | symbol_table::iterator_t iter; |
1c6b77e5 JS |
980 | for (iter = sym_table->map_by_addr.begin(); |
981 | iter != sym_table->map_by_addr.end(); | |
2e67a43b | 982 | ++iter) |
5f0a03a6 | 983 | { |
1c6b77e5 | 984 | fi = iter->second; |
5f0a03a6 JK |
985 | if (!null_die(&fi->die)) |
986 | continue; // already handled in query_module_dwarf() | |
987 | if (dw.function_name_matches_pattern(fi->name, function_str_val)) | |
988 | query_func_info(fi->addr, *fi, this); | |
989 | } | |
990 | } | |
991 | else | |
992 | { | |
993 | fi = sym_table->lookup_symbol(function_str_val); | |
2867a2a1 | 994 | if (fi && !fi->descriptor && null_die(&fi->die)) |
5f0a03a6 JK |
995 | query_func_info(fi->addr, *fi, this); |
996 | } | |
997 | } | |
998 | else | |
999 | { | |
1000 | assert(has_function_num || has_statement_num); | |
1001 | // Find the "function" in which the indicated address resides. | |
1002 | Dwarf_Addr addr = | |
1003 | (has_function_num ? function_num_val : statement_num_val); | |
576eaefe | 1004 | if (has_plt) |
3d372d6b SC |
1005 | { |
1006 | // Use the raw address from the .plt | |
1007 | fi = sym_table->get_first_func(); | |
1008 | fi->addr = addr; | |
1009 | } | |
1010 | else | |
1011 | fi = sym_table->get_func_containing_address(addr); | |
1012 | ||
5f0a03a6 JK |
1013 | if (!fi) |
1014 | { | |
2713ea24 CM |
1015 | sess.print_warning(_F("address %#" PRIx64 " out of range for module %s", |
1016 | addr, dw.module_name.c_str())); | |
1017 | return; | |
5f0a03a6 JK |
1018 | } |
1019 | if (!null_die(&fi->die)) | |
1020 | { | |
1021 | // addr looks like it's in the compilation unit containing | |
1022 | // the indicated function, but query_module_dwarf() didn't | |
1023 | // match addr to any compilation unit, so addr must be | |
1024 | // above that cu's address range. | |
2713ea24 CM |
1025 | sess.print_warning(_F("address %#" PRIx64 " maps to no known compilation unit in module %s", |
1026 | addr, dw.module_name.c_str())); | |
5f0a03a6 JK |
1027 | return; |
1028 | } | |
1029 | query_func_info(fi->addr, *fi, this); | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | void | |
1034 | dwarf_query::handle_query_module() | |
1035 | { | |
1c6b77e5 JS |
1036 | bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab; |
1037 | dw.get_module_dwarf(false, report); | |
1038 | ||
1039 | // prebuild the symbol table to resolve aliases | |
1040 | dw.mod_info->get_symtab(this); | |
1041 | ||
857bdfd1 JS |
1042 | // reset the dupe-checking for each new module |
1043 | alias_dupes.clear(); | |
7fdd3e2c | 1044 | inline_dupes.clear(); |
857bdfd1 | 1045 | |
5f0a03a6 JK |
1046 | if (dw.mod_info->dwarf_status == info_present) |
1047 | query_module_dwarf(); | |
1c6b77e5 | 1048 | |
5f0a03a6 JK |
1049 | // Consult the symbol table if we haven't found all we're looking for. |
1050 | // asm functions can show up in the symbol table but not in dwarf. | |
1051 | if (sess.consult_symtab && !query_done) | |
1052 | query_module_symtab(); | |
1053 | } | |
1054 | ||
2c384610 | 1055 | |
7d6d0afc JS |
1056 | void |
1057 | dwarf_query::parse_function_spec(const string & spec) | |
bd2b1e68 | 1058 | { |
1d12a9b2 JS |
1059 | line_type = ABSOLUTE; |
1060 | line[0] = line[1] = 0; | |
1061 | ||
91699a70 | 1062 | size_t src_pos, line_pos, dash_pos, scope_pos; |
bd2b1e68 | 1063 | |
7d6d0afc | 1064 | // look for named scopes |
91699a70 JS |
1065 | scope_pos = spec.rfind("::"); |
1066 | if (scope_pos != string::npos) | |
bd2b1e68 | 1067 | { |
91699a70 JS |
1068 | tokenize_cxx(spec.substr(0, scope_pos), scopes); |
1069 | scope_pos += 2; | |
bd2b1e68 | 1070 | } |
91699a70 JS |
1071 | else |
1072 | scope_pos = 0; | |
bd2b1e68 | 1073 | |
7d6d0afc JS |
1074 | // look for a source separator |
1075 | src_pos = spec.find('@', scope_pos); | |
1076 | if (src_pos == string::npos) | |
bd2b1e68 | 1077 | { |
7d6d0afc JS |
1078 | function = spec.substr(scope_pos); |
1079 | spec_type = function_alone; | |
bd2b1e68 | 1080 | } |
7d6d0afc | 1081 | else |
879eb9e9 | 1082 | { |
7d6d0afc | 1083 | function = spec.substr(scope_pos, src_pos - scope_pos); |
7a053d3b | 1084 | |
7d6d0afc JS |
1085 | // look for a line-number separator |
1086 | line_pos = spec.find_first_of(":+", src_pos); | |
1087 | if (line_pos == string::npos) | |
1088 | { | |
1089 | file = spec.substr(src_pos + 1); | |
1090 | spec_type = function_and_file; | |
1091 | } | |
1092 | else | |
1093 | { | |
1094 | file = spec.substr(src_pos + 1, line_pos - src_pos - 1); | |
1095 | ||
1096 | // classify the line spec | |
1097 | spec_type = function_file_and_line; | |
1098 | if (spec[line_pos] == '+') | |
1099 | line_type = RELATIVE; | |
1100 | else if (spec[line_pos + 1] == '*' && | |
1101 | spec.length() == line_pos + 2) | |
1102 | line_type = WILDCARD; | |
1103 | else | |
1104 | line_type = ABSOLUTE; | |
1105 | ||
1106 | if (line_type != WILDCARD) | |
1107 | try | |
1108 | { | |
1109 | // try to parse either N or N-M | |
1110 | dash_pos = spec.find('-', line_pos + 1); | |
1111 | if (dash_pos == string::npos) | |
1112 | line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1)); | |
1113 | else | |
1114 | { | |
1115 | line_type = RANGE; | |
1116 | line[0] = lex_cast<int>(spec.substr(line_pos + 1, | |
1117 | dash_pos - line_pos - 1)); | |
1118 | line[1] = lex_cast<int>(spec.substr(dash_pos + 1)); | |
1119 | } | |
1120 | } | |
1121 | catch (runtime_error & exn) | |
1122 | { | |
1123 | goto bad; | |
1124 | } | |
1125 | } | |
bd2b1e68 GH |
1126 | } |
1127 | ||
7d6d0afc JS |
1128 | if (function.empty() || |
1129 | (spec_type != function_alone && file.empty())) | |
bd2b1e68 GH |
1130 | goto bad; |
1131 | ||
7d6d0afc | 1132 | if (sess.verbose > 2) |
bd2b1e68 | 1133 | { |
b530b5b3 LB |
1134 | //clog << "parsed '" << spec << "'"; |
1135 | clog << _F("parse '%s'", spec.c_str()); | |
41c262f3 | 1136 | |
7d6d0afc JS |
1137 | if (!scopes.empty()) |
1138 | clog << ", scope '" << scopes[0] << "'"; | |
1139 | for (unsigned i = 1; i < scopes.size(); ++i) | |
1140 | clog << "::'" << scopes[i] << "'"; | |
41c262f3 | 1141 | |
7d6d0afc JS |
1142 | clog << ", func '" << function << "'"; |
1143 | ||
1144 | if (spec_type != function_alone) | |
1145 | clog << ", file '" << file << "'"; | |
1146 | ||
1147 | if (spec_type == function_file_and_line) | |
1148 | { | |
1149 | clog << ", line "; | |
1150 | switch (line_type) | |
1151 | { | |
1152 | case ABSOLUTE: | |
1153 | clog << line[0]; | |
1154 | break; | |
1155 | ||
1156 | case RELATIVE: | |
1157 | clog << "+" << line[0]; | |
1158 | break; | |
1159 | ||
1160 | case RANGE: | |
1161 | clog << line[0] << " - " << line[1]; | |
1162 | break; | |
1163 | ||
1164 | case WILDCARD: | |
1165 | clog << "*"; | |
1166 | break; | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | clog << endl; | |
bd2b1e68 GH |
1171 | } |
1172 | ||
7d6d0afc JS |
1173 | return; |
1174 | ||
1175 | bad: | |
b530b5b3 | 1176 | throw semantic_error(_F("malformed specification '%s'", spec.c_str()), |
7d6d0afc | 1177 | base_probe->tok); |
bd2b1e68 GH |
1178 | } |
1179 | ||
05fb3e0c WF |
1180 | string path_remove_sysroot(const systemtap_session& sess, const string& path) |
1181 | { | |
1182 | size_t pos; | |
1183 | string retval = path; | |
1184 | if (!sess.sysroot.empty() && | |
1185 | (pos = retval.find(sess.sysroot)) != string::npos) | |
1186 | retval.replace(pos, sess.sysroot.length(), "/"); | |
1187 | return retval; | |
1188 | } | |
bd2b1e68 | 1189 | |
36f9dd1d | 1190 | void |
1ffb8bd1 | 1191 | dwarf_query::add_probe_point(const string& dw_funcname, |
b20febf3 | 1192 | const char* filename, |
36f9dd1d | 1193 | int line, |
b20febf3 | 1194 | Dwarf_Die* scope_die, |
36f9dd1d FCE |
1195 | Dwarf_Addr addr) |
1196 | { | |
b20febf3 | 1197 | string reloc_section; // base section for relocation purposes |
27646582 | 1198 | Dwarf_Addr reloc_addr; // relocated |
b20febf3 | 1199 | const string& module = dw.module_name; // "kernel" or other |
1ffb8bd1 | 1200 | string funcname = dw_funcname; |
36f9dd1d | 1201 | |
37ebca01 FCE |
1202 | assert (! has_absolute); // already handled in dwarf_builder::build() |
1203 | ||
576eaefe SC |
1204 | if (!has_plt) |
1205 | reloc_addr = dw.relocate_address(addr, reloc_section); | |
1206 | else | |
1207 | { | |
3d372d6b | 1208 | // Set the reloc_section but use the plt entry for reloc_addr |
576eaefe SC |
1209 | dw.relocate_address(addr, reloc_section); |
1210 | reloc_addr = addr; | |
1211 | } | |
2930abc7 | 1212 | |
1ffb8bd1 | 1213 | // If we originally used the linkage name, then let's call it that way |
1ffb8bd1 JS |
1214 | const char* linkage_name; |
1215 | if (scope_die && startswith (this->function, "_Z") | |
f450a7e3 | 1216 | && (linkage_name = dwarf_linkage_name (scope_die))) |
1ffb8bd1 JS |
1217 | funcname = linkage_name; |
1218 | ||
7f9f3386 FCE |
1219 | if (sess.verbose > 1) |
1220 | { | |
b530b5b3 | 1221 | clog << _("probe ") << funcname << "@" << filename << ":" << line; |
b20febf3 | 1222 | if (string(module) == TOK_KERNEL) |
b530b5b3 | 1223 | clog << _(" kernel"); |
91af0778 | 1224 | else if (has_module) |
b530b5b3 | 1225 | clog << _(" module=") << module; |
91af0778 | 1226 | else if (has_process) |
b530b5b3 | 1227 | clog << _(" process=") << module; |
b20febf3 | 1228 | if (reloc_section != "") clog << " reloc=" << reloc_section; |
b20febf3 | 1229 | clog << " pc=0x" << hex << addr << dec; |
7f9f3386 | 1230 | } |
4baf0e53 | 1231 | |
27646582 | 1232 | bool bad = dw.blacklisted_p (funcname, filename, line, module, |
789448a3 | 1233 | addr, has_return); |
b20febf3 FCE |
1234 | if (sess.verbose > 1) |
1235 | clog << endl; | |
7f9f3386 | 1236 | |
84048984 FCE |
1237 | if (module == TOK_KERNEL) |
1238 | { | |
1239 | // PR 4224: adapt to relocatable kernel by subtracting the _stext address here. | |
1240 | reloc_addr = addr - sess.sym_stext; | |
37ebca01 | 1241 | reloc_section = "_stext"; // a message to runtime's _stp_module_relocate |
84048984 FCE |
1242 | } |
1243 | ||
b20febf3 FCE |
1244 | if (! bad) |
1245 | { | |
1a0dbc5a | 1246 | sess.unwindsym_modules.insert (module); |
6d0f3f0c FCE |
1247 | |
1248 | if (has_process) | |
1249 | { | |
05fb3e0c | 1250 | string module_tgt = path_remove_sysroot(sess, module); |
6d0f3f0c | 1251 | results.push_back (new uprobe_derived_probe(funcname, filename, line, |
05fb3e0c | 1252 | module_tgt, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1253 | *this, scope_die)); |
1254 | } | |
1255 | else | |
1256 | { | |
1257 | assert (has_kernel || has_module); | |
1258 | results.push_back (new dwarf_derived_probe(funcname, filename, line, | |
06aca46a | 1259 | module, reloc_section, addr, reloc_addr, |
6d0f3f0c FCE |
1260 | *this, scope_die)); |
1261 | } | |
b20febf3 | 1262 | } |
2930abc7 FCE |
1263 | } |
1264 | ||
5f0a03a6 JK |
1265 | enum dbinfo_reqt |
1266 | dwarf_query::assess_dbinfo_reqt() | |
1267 | { | |
1268 | if (has_absolute) | |
1269 | { | |
1270 | // kernel.statement(NUM).absolute | |
1271 | return dbr_none; | |
1272 | } | |
1273 | if (has_inline) | |
1274 | { | |
1275 | // kernel.function("f").inline or module("m").function("f").inline | |
1276 | return dbr_need_dwarf; | |
1277 | } | |
1278 | if (has_function_str && spec_type == function_alone) | |
1279 | { | |
1280 | // kernel.function("f") or module("m").function("f") | |
1281 | return dbr_need_symtab; | |
1282 | } | |
1283 | if (has_statement_num) | |
1284 | { | |
1285 | // kernel.statement(NUM) or module("m").statement(NUM) | |
1286 | // Technically, all we need is the module offset (or _stext, for | |
1287 | // the kernel). But for that we need either the ELF file or (for | |
1288 | // _stext) the symbol table. In either case, the symbol table | |
1289 | // is available, and that allows us to map the NUM (address) | |
1290 | // to a function, which is goodness. | |
1291 | return dbr_need_symtab; | |
1292 | } | |
1293 | if (has_function_num) | |
1294 | { | |
1295 | // kernel.function(NUM) or module("m").function(NUM) | |
1296 | // Need the symbol table so we can back up from NUM to the | |
1297 | // start of the function. | |
1298 | return dbr_need_symtab; | |
1299 | } | |
1300 | // Symbol table tells us nothing about source files or line numbers. | |
1301 | return dbr_need_dwarf; | |
1302 | } | |
2930abc7 FCE |
1303 | |
1304 | ||
b8da0ad1 FCE |
1305 | // The critical determining factor when interpreting a pattern |
1306 | // string is, perhaps surprisingly: "presence of a lineno". The | |
1307 | // presence of a lineno changes the search strategy completely. | |
1308 | // | |
1309 | // Compare the two cases: | |
1310 | // | |
1311 | // 1. {statement,function}(foo@file.c:lineno) | |
1312 | // - find the files matching file.c | |
1313 | // - in each file, find the functions matching foo | |
1314 | // - query the file for line records matching lineno | |
1315 | // - iterate over the line records, | |
1316 | // - and iterate over the functions, | |
1317 | // - if(haspc(function.DIE, line.addr)) | |
1318 | // - if looking for statements: probe(lineno.addr) | |
1319 | // - if looking for functions: probe(function.{entrypc,return,etc.}) | |
1320 | // | |
1321 | // 2. {statement,function}(foo@file.c) | |
1322 | // - find the files matching file.c | |
1323 | // - in each file, find the functions matching foo | |
1324 | // - probe(function.{entrypc,return,etc.}) | |
1325 | // | |
1326 | // Thus the first decision we make is based on the presence of a | |
1327 | // lineno, and we enter entirely different sets of callbacks | |
1328 | // depending on that decision. | |
1329 | // | |
1330 | // Note that the first case is a generalization fo the second, in that | |
1331 | // we could theoretically search through line records for matching | |
1332 | // file names (a "table scan" in rdbms lingo). Luckily, file names | |
1333 | // are already cached elsewhere, so we can do an "index scan" as an | |
1334 | // optimization. | |
7e1279ea | 1335 | |
bd2b1e68 | 1336 | static void |
4cd232e4 | 1337 | query_statement (string const & func, |
20e4a32c | 1338 | char const * file, |
4cd232e4 | 1339 | int line, |
bcc12710 | 1340 | Dwarf_Die *scope_die, |
20e4a32c | 1341 | Dwarf_Addr stmt_addr, |
4cd232e4 | 1342 | dwarf_query * q) |
bd2b1e68 | 1343 | { |
39bcd429 FCE |
1344 | try |
1345 | { | |
cee35f73 | 1346 | q->add_probe_point(func, file ? file : "", |
a9b2f3a5 | 1347 | line, scope_die, stmt_addr); |
39bcd429 FCE |
1348 | } |
1349 | catch (const semantic_error& e) | |
1350 | { | |
1351 | q->sess.print_error (e); | |
1352 | } | |
bd2b1e68 GH |
1353 | } |
1354 | ||
6b517475 JS |
1355 | static void |
1356 | query_addr(Dwarf_Addr addr, dwarf_query *q) | |
1357 | { | |
1358 | dwflpp &dw = q->dw; | |
1359 | ||
08d1d520 MW |
1360 | if (q->sess.verbose > 2) |
1361 | clog << "query_addr 0x" << hex << addr << dec << endl; | |
6b517475 JS |
1362 | |
1363 | // First pick which CU contains this address | |
1364 | Dwarf_Die* cudie = dw.query_cu_containing_address(addr); | |
1365 | if (!cudie) // address could be wildly out of range | |
1366 | return; | |
1367 | dw.focus_on_cu(cudie); | |
1368 | ||
1369 | // Now compensate for the dw bias | |
1370 | addr -= dw.module_bias; | |
1371 | ||
1372 | // Per PR5787, we look up the scope die even for | |
1373 | // statement_num's, for blacklist sensitivity and $var | |
1374 | // resolution purposes. | |
1375 | ||
1376 | // Find the scopes containing this address | |
1377 | vector<Dwarf_Die> scopes = dw.getscopes(addr); | |
1378 | if (scopes.empty()) | |
1379 | return; | |
1380 | ||
1381 | // Look for the innermost containing function | |
1382 | Dwarf_Die *fnscope = NULL; | |
1383 | for (size_t i = 0; i < scopes.size(); ++i) | |
1384 | { | |
1385 | int tag = dwarf_tag(&scopes[i]); | |
1386 | if ((tag == DW_TAG_subprogram && !q->has_inline) || | |
1387 | (tag == DW_TAG_inlined_subroutine && | |
4bda987e | 1388 | !q->has_call && !q->has_return && !q->has_exported)) |
6b517475 JS |
1389 | { |
1390 | fnscope = &scopes[i]; | |
1391 | break; | |
1392 | } | |
1393 | } | |
1394 | if (!fnscope) | |
1395 | return; | |
1396 | dw.focus_on_function(fnscope); | |
1397 | ||
1398 | Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0]; | |
1399 | ||
1400 | const char *file = dwarf_decl_file(fnscope); | |
1401 | int line; | |
1402 | dwarf_decl_line(fnscope, &line); | |
1403 | ||
1404 | // Function probes should reset the addr to the function entry | |
1405 | // and possibly perform prologue searching | |
1406 | if (q->has_function_num) | |
1407 | { | |
1408 | dw.die_entrypc(fnscope, &addr); | |
1409 | if (dwarf_tag(fnscope) == DW_TAG_subprogram && | |
1410 | (q->sess.prologue_searching || q->has_process)) // PR 6871 | |
1411 | { | |
1412 | func_info func; | |
1413 | func.die = *fnscope; | |
1414 | func.name = dw.function_name; | |
1415 | func.decl_file = file; | |
1416 | func.decl_line = line; | |
1417 | func.entrypc = addr; | |
1418 | ||
1419 | func_info_map_t funcs(1, func); | |
1420 | dw.resolve_prologue_endings (funcs); | |
464379bb FCE |
1421 | if (q->has_return) // PR13200 |
1422 | { | |
1423 | if (q->sess.verbose > 2) | |
1424 | clog << "ignoring prologue for .return probes" << endl; | |
1425 | } | |
1426 | else | |
1427 | { | |
1428 | if (funcs[0].prologue_end) | |
1429 | addr = funcs[0].prologue_end; | |
1430 | } | |
6b517475 JS |
1431 | } |
1432 | } | |
1433 | else | |
1434 | { | |
1435 | dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr)); | |
1436 | if (address_line) | |
1437 | { | |
1438 | file = address_line.linesrc(); | |
1439 | line = address_line.lineno(); | |
1440 | } | |
1441 | ||
1442 | // Verify that a raw address matches the beginning of a | |
1443 | // statement. This is a somewhat lame check that the address | |
1444 | // is at the start of an assembly instruction. Mark probes are in the | |
1445 | // middle of a macro and thus not strictly at a statement beginning. | |
1446 | // Guru mode may override this check. | |
1447 | if (!q->has_mark && (!address_line || address_line.addr() != addr)) | |
1448 | { | |
1449 | stringstream msg; | |
2a97f50b | 1450 | msg << _F("address %#" PRIx64 " does not match the beginning of a statement", |
b530b5b3 | 1451 | addr); |
6b517475 | 1452 | if (address_line) |
2a97f50b | 1453 | msg << _F(" (try %#" PRIx64 ")", address_line.addr()); |
6b517475 | 1454 | else |
b530b5b3 LB |
1455 | msg << _F(" (no line info found for '%s', in module '%s')", |
1456 | dw.cu_name().c_str(), dw.module_name.c_str()); | |
6b517475 JS |
1457 | if (! q->sess.guru_mode) |
1458 | throw semantic_error(msg.str()); | |
2713ea24 | 1459 | else |
6b517475 JS |
1460 | q->sess.print_warning(msg.str()); |
1461 | } | |
1462 | } | |
1463 | ||
1464 | // Build a probe at this point | |
1465 | query_statement(dw.function_name, file, line, scope, addr, q); | |
1466 | } | |
1467 | ||
8096dd7d JS |
1468 | static void |
1469 | query_label (string const & func, | |
1470 | char const * label, | |
1471 | char const * file, | |
1472 | int line, | |
1473 | Dwarf_Die *scope_die, | |
1474 | Dwarf_Addr stmt_addr, | |
1475 | dwarf_query * q) | |
1476 | { | |
6b517475 JS |
1477 | assert (q->has_statement_str || q->has_function_str); |
1478 | ||
8096dd7d JS |
1479 | size_t i = q->results.size(); |
1480 | ||
1481 | // weed out functions whose decl_file isn't one of | |
1482 | // the source files that we actually care about | |
6b517475 | 1483 | if (q->spec_type != function_alone && |
8096dd7d JS |
1484 | q->filtered_srcfiles.count(file) == 0) |
1485 | return; | |
1486 | ||
1487 | query_statement(func, file, line, scope_die, stmt_addr, q); | |
1488 | ||
c72aa911 JS |
1489 | // after the fact, insert the label back into the derivation chain |
1490 | probe_point::component* ppc = | |
1491 | new probe_point::component(TOK_LABEL, new literal_string (label)); | |
1492 | for (; i < q->results.size(); ++i) | |
1493 | { | |
1494 | derived_probe* p = q->results[i]; | |
1495 | probe_point* pp = new probe_point(*p->locations[0]); | |
1496 | pp->components.push_back (ppc); | |
1497 | p->base = p->base->create_alias(p->locations[0], pp); | |
1498 | } | |
8096dd7d JS |
1499 | } |
1500 | ||
7e1279ea | 1501 | static void |
3e961ba6 | 1502 | query_inline_instance_info (inline_instance_info & ii, |
7e1279ea FCE |
1503 | dwarf_query * q) |
1504 | { | |
b6581717 | 1505 | try |
7e1279ea | 1506 | { |
8f14e444 FCE |
1507 | assert (! q->has_return); // checked by caller already |
1508 | if (q->sess.verbose>2) | |
b530b5b3 LB |
1509 | clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n", |
1510 | ii.entrypc, ii.name.c_str()); | |
8f14e444 FCE |
1511 | query_statement (ii.name, ii.decl_file, ii.decl_line, |
1512 | &ii.die, ii.entrypc, q); | |
7e1279ea | 1513 | } |
b6581717 | 1514 | catch (semantic_error &e) |
7e1279ea | 1515 | { |
b6581717 | 1516 | q->sess.print_error (e); |
7e1279ea FCE |
1517 | } |
1518 | } | |
1519 | ||
1520 | static void | |
1521 | query_func_info (Dwarf_Addr entrypc, | |
bcc12710 | 1522 | func_info & fi, |
7e1279ea FCE |
1523 | dwarf_query * q) |
1524 | { | |
b6581717 | 1525 | try |
7e1279ea | 1526 | { |
b6581717 GH |
1527 | if (q->has_return) |
1528 | { | |
1529 | // NB. dwarf_derived_probe::emit_registrations will emit a | |
1530 | // kretprobe based on the entrypc in this case. | |
464379bb FCE |
1531 | if (fi.prologue_end != 0 && q->has_return) // PR13200 |
1532 | { | |
1533 | if (q->sess.verbose > 2) | |
1534 | clog << "ignoring prologue for .return probes" << endl; | |
1535 | } | |
20e4a32c | 1536 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
b6581717 GH |
1537 | &fi.die, entrypc, q); |
1538 | } | |
1539 | else | |
1540 | { | |
35dc8b04 | 1541 | if (fi.prologue_end != 0) |
44f75386 | 1542 | { |
44f75386 FCE |
1543 | query_statement (fi.name, fi.decl_file, fi.decl_line, |
1544 | &fi.die, fi.prologue_end, q); | |
1545 | } | |
1546 | else | |
1547 | { | |
1548 | query_statement (fi.name, fi.decl_file, fi.decl_line, | |
1549 | &fi.die, entrypc, q); | |
1550 | } | |
b6581717 | 1551 | } |
7e1279ea | 1552 | } |
b6581717 | 1553 | catch (semantic_error &e) |
7e1279ea | 1554 | { |
b6581717 | 1555 | q->sess.print_error (e); |
7e1279ea FCE |
1556 | } |
1557 | } | |
1558 | ||
1559 | ||
bd4b874d SC |
1560 | static void |
1561 | query_srcfile_label (const dwarf_line_t& line, void * arg) | |
1562 | { | |
1563 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1564 | ||
1565 | Dwarf_Addr addr = line.addr(); | |
1566 | ||
1567 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1568 | i != q->filtered_functions.end(); ++i) | |
1569 | if (q->dw.die_has_pc (i->die, addr)) | |
f09d0d1e JS |
1570 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, |
1571 | q, query_label); | |
1572 | ||
1573 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1574 | i != q->filtered_inlines.end(); ++i) | |
1575 | if (q->dw.die_has_pc (i->die, addr)) | |
1576 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1577 | q, query_label); | |
bd4b874d SC |
1578 | } |
1579 | ||
7e1279ea | 1580 | static void |
86bf665e | 1581 | query_srcfile_line (const dwarf_line_t& line, void * arg) |
7e1279ea FCE |
1582 | { |
1583 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
1584 | ||
86bf665e | 1585 | Dwarf_Addr addr = line.addr(); |
4cd232e4 | 1586 | |
86bf665e | 1587 | int lineno = line.lineno(); |
847bf07f | 1588 | |
86bf665e | 1589 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); |
7e1279ea FCE |
1590 | i != q->filtered_functions.end(); ++i) |
1591 | { | |
3e961ba6 | 1592 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1593 | { |
b0ee93c4 | 1594 | if (q->sess.verbose>3) |
b530b5b3 | 1595 | clog << _("function DIE lands on srcfile\n"); |
4cd232e4 | 1596 | if (q->has_statement_str) |
f5958c8f JS |
1597 | { |
1598 | Dwarf_Die scope; | |
1599 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1600 | query_statement (i->name, i->decl_file, | |
1601 | lineno, // NB: not q->line ! | |
1602 | &scope, addr, q); | |
1603 | } | |
4cd232e4 | 1604 | else |
3e961ba6 | 1605 | query_func_info (i->entrypc, *i, q); |
7e1279ea | 1606 | } |
20e4a32c RM |
1607 | } |
1608 | ||
86bf665e | 1609 | for (inline_instance_map_t::iterator i |
897820ca GH |
1610 | = q->filtered_inlines.begin(); |
1611 | i != q->filtered_inlines.end(); ++i) | |
1612 | { | |
3e961ba6 | 1613 | if (q->dw.die_has_pc (i->die, addr)) |
7e1279ea | 1614 | { |
b0ee93c4 | 1615 | if (q->sess.verbose>3) |
b530b5b3 | 1616 | clog << _("inline instance DIE lands on srcfile\n"); |
897820ca | 1617 | if (q->has_statement_str) |
f5958c8f JS |
1618 | { |
1619 | Dwarf_Die scope; | |
1620 | q->dw.inner_die_containing_pc(i->die, addr, scope); | |
1621 | query_statement (i->name, i->decl_file, | |
1622 | q->line[0], &scope, addr, q); | |
1623 | } | |
897820ca | 1624 | else |
3e961ba6 | 1625 | query_inline_instance_info (*i, q); |
897820ca | 1626 | } |
20e4a32c | 1627 | } |
7e1279ea FCE |
1628 | } |
1629 | ||
1630 | ||
7fdd3e2c JS |
1631 | bool |
1632 | inline_instance_info::operator<(const inline_instance_info& other) const | |
1633 | { | |
1634 | if (entrypc != other.entrypc) | |
1635 | return entrypc < other.entrypc; | |
1636 | ||
1637 | if (decl_line != other.decl_line) | |
1638 | return decl_line < other.decl_line; | |
1639 | ||
1640 | int cmp = name.compare(other.name); | |
1641 | if (!cmp) | |
1642 | cmp = strcmp(decl_file, other.decl_file); | |
1643 | return cmp < 0; | |
1644 | } | |
1645 | ||
1646 | ||
4fa7b22b | 1647 | static int |
7e1279ea | 1648 | query_dwarf_inline_instance (Dwarf_Die * die, void * arg) |
4fa7b22b GH |
1649 | { |
1650 | dwarf_query * q = static_cast<dwarf_query *>(arg); | |
6b517475 | 1651 | assert (q->has_statement_str || q->has_function_str); |
4bda987e | 1652 | assert (!q->has_call && !q->has_return && !q->has_exported); |
bd2b1e68 | 1653 | |
39bcd429 | 1654 | try |
7a053d3b | 1655 | { |
b0ee93c4 | 1656 | if (q->sess.verbose>2) |
b530b5b3 | 1657 | clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str()); |
7e1279ea | 1658 | |
6b517475 JS |
1659 | Dwarf_Addr entrypc; |
1660 | if (q->dw.die_entrypc (die, &entrypc)) | |
1661 | { | |
1662 | inline_instance_info inl; | |
1663 | inl.die = *die; | |
1664 | inl.name = q->dw.function_name; | |
1665 | inl.entrypc = entrypc; | |
1666 | q->dw.function_file (&inl.decl_file); | |
1667 | q->dw.function_line (&inl.decl_line); | |
1668 | ||
1669 | // make sure that this inline hasn't already | |
1670 | // been matched from a different CU | |
1671 | if (q->inline_dupes.insert(inl).second) | |
1672 | q->filtered_inlines.push_back(inl); | |
1673 | } | |
7e1279ea FCE |
1674 | return DWARF_CB_OK; |
1675 | } | |
1676 | catch (const semantic_error& e) | |
1677 | { | |
1678 | q->sess.print_error (e); | |
1679 | return DWARF_CB_ABORT; | |
1680 | } | |
1681 | } | |
bb788f9f | 1682 | |
7e1279ea | 1683 | static int |
2da9cedb | 1684 | query_dwarf_func (Dwarf_Die * func, base_query * bq) |
7e1279ea | 1685 | { |
2da9cedb | 1686 | dwarf_query * q = static_cast<dwarf_query *>(bq); |
6b517475 | 1687 | assert (q->has_statement_str || q->has_function_str); |
bb788f9f | 1688 | |
bd25380d JS |
1689 | // weed out functions whose decl_file isn't one of |
1690 | // the source files that we actually care about | |
6b517475 | 1691 | if (q->spec_type != function_alone && |
bd25380d | 1692 | q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0) |
8096dd7d | 1693 | return DWARF_CB_OK; |
bd25380d | 1694 | |
7e1279ea FCE |
1695 | try |
1696 | { | |
7e1279ea FCE |
1697 | q->dw.focus_on_function (func); |
1698 | ||
7d6d0afc JS |
1699 | if (!q->dw.function_scope_matches(q->scopes)) |
1700 | return DWARF_CB_OK; | |
1701 | ||
857bdfd1 JS |
1702 | // make sure that this function address hasn't |
1703 | // already been matched under an aliased name | |
1704 | Dwarf_Addr addr; | |
1705 | if (!q->dw.func_is_inline() && | |
1706 | dwarf_entrypc(func, &addr) == 0 && | |
1707 | !q->alias_dupes.insert(addr).second) | |
1708 | return DWARF_CB_OK; | |
1709 | ||
4bda987e | 1710 | if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported)) |
7e1279ea | 1711 | { |
4bda987e | 1712 | if (q->sess.verbose>3) |
b530b5b3 | 1713 | clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str()); |
4bda987e | 1714 | q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q); |
7e1279ea | 1715 | } |
8f14e444 FCE |
1716 | else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553 |
1717 | { | |
1718 | q->inlined_non_returnable.insert (q->dw.function_name); | |
1719 | } | |
396afcee | 1720 | else if (!q->dw.func_is_inline () && (! q->has_inline)) |
20e4a32c | 1721 | { |
4bda987e SC |
1722 | if (q->has_exported && !q->dw.func_is_exported ()) |
1723 | return DWARF_CB_OK; | |
6b517475 | 1724 | if (q->sess.verbose>2) |
b530b5b3 | 1725 | clog << _F("selected function %s\n", q->dw.function_name.c_str()); |
6b517475 JS |
1726 | |
1727 | func_info func; | |
1728 | q->dw.function_die (&func.die); | |
1729 | func.name = q->dw.function_name; | |
1730 | q->dw.function_file (&func.decl_file); | |
1731 | q->dw.function_line (&func.decl_line); | |
1732 | ||
1733 | Dwarf_Addr entrypc; | |
1734 | if (q->dw.function_entrypc (&entrypc)) | |
1735 | { | |
1736 | func.entrypc = entrypc; | |
1737 | q->filtered_functions.push_back (func); | |
1738 | } | |
1739 | /* else this function is fully inlined, just ignore it */ | |
7e1279ea | 1740 | } |
39bcd429 | 1741 | return DWARF_CB_OK; |
bd2b1e68 | 1742 | } |
39bcd429 | 1743 | catch (const semantic_error& e) |
bd2b1e68 | 1744 | { |
39bcd429 FCE |
1745 | q->sess.print_error (e); |
1746 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1747 | } |
bd2b1e68 GH |
1748 | } |
1749 | ||
1750 | static int | |
1751 | query_cu (Dwarf_Die * cudie, void * arg) | |
1752 | { | |
20c6c071 | 1753 | dwarf_query * q = static_cast<dwarf_query *>(arg); |
6b517475 JS |
1754 | assert (q->has_statement_str || q->has_function_str); |
1755 | ||
85007c04 | 1756 | if (pending_interrupts) return DWARF_CB_ABORT; |
7a053d3b | 1757 | |
39bcd429 | 1758 | try |
bd2b1e68 | 1759 | { |
7e1279ea | 1760 | q->dw.focus_on_cu (cudie); |
b5d77020 | 1761 | |
b0ee93c4 | 1762 | if (false && q->sess.verbose>2) |
b530b5b3 LB |
1763 | clog << _F("focused on CU '%s', in module '%s'\n", |
1764 | q->dw.cu_name().c_str(), q->dw.module_name.c_str()); | |
d9b516ca | 1765 | |
6b517475 JS |
1766 | q->filtered_srcfiles.clear(); |
1767 | q->filtered_functions.clear(); | |
1768 | q->filtered_inlines.clear(); | |
1769 | ||
1770 | // In this path, we find "abstract functions", record | |
1771 | // information about them, and then (depending on lineno | |
1772 | // matching) possibly emit one or more of the function's | |
1773 | // associated addresses. Unfortunately the control of this | |
1774 | // cannot easily be turned inside out. | |
1775 | ||
1776 | if (q->spec_type != function_alone) | |
39bcd429 | 1777 | { |
6b517475 JS |
1778 | // If we have a pattern string with a filename, we need |
1779 | // to elaborate the srcfile mask in question first. | |
1780 | q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles); | |
1781 | ||
1782 | // If we have a file pattern and *no* srcfile matches, there's | |
1783 | // no need to look further into this CU, so skip. | |
1784 | if (q->filtered_srcfiles.empty()) | |
1785 | return DWARF_CB_OK; | |
1786 | } | |
e4c58386 | 1787 | |
6b517475 JS |
1788 | // Pick up [entrypc, name, DIE] tuples for all the functions |
1789 | // matching the query, and fill in the prologue endings of them | |
1790 | // all in a single pass. | |
5898b6e1 | 1791 | int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function); |
6b517475 JS |
1792 | if (rc != DWARF_CB_OK) |
1793 | q->query_done = true; | |
1794 | ||
1795 | if ((q->sess.prologue_searching || q->has_process) // PR 6871 | |
1796 | && !q->has_statement_str) // PR 2608 | |
1797 | if (! q->filtered_functions.empty()) | |
1798 | q->dw.resolve_prologue_endings (q->filtered_functions); | |
464379bb FCE |
1799 | // NB: we could skip the resolve_prologue_endings() call here for has_return case (PR13200), |
1800 | // but don't have to. We can resolve the prologue, just not actually use it in query_addr(). | |
6b517475 JS |
1801 | |
1802 | if (q->spec_type == function_file_and_line) | |
1803 | { | |
58b070fb | 1804 | // .statement(...:NN) often gets mixed up with .function(...:NN) |
2713ea24 | 1805 | if (q->has_function_str) |
b530b5b3 LB |
1806 | q->sess.print_warning (_("For probing a particular line, use a " |
1807 | ".statement() probe, not .function()"), | |
af2e341f | 1808 | q->base_probe->tok); |
58b070fb | 1809 | |
6b517475 JS |
1810 | // If we have a pattern string with target *line*, we |
1811 | // have to look at lines in all the matched srcfiles. | |
1812 | void (* callback) (const dwarf_line_t&, void*) = | |
1813 | q->has_label ? query_srcfile_label : query_srcfile_line; | |
1814 | for (set<string>::const_iterator i = q->filtered_srcfiles.begin(); | |
1815 | i != q->filtered_srcfiles.end(); ++i) | |
1816 | q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str, | |
1817 | q->line_type, callback, q->function, q); | |
1818 | } | |
1819 | else if (q->has_label) | |
1820 | { | |
1821 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1822 | i != q->filtered_functions.end(); ++i) | |
1823 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1824 | q, query_label); | |
1825 | ||
1826 | for (inline_instance_map_t::iterator i = q->filtered_inlines.begin(); | |
1827 | i != q->filtered_inlines.end(); ++i) | |
1828 | q->dw.iterate_over_labels (&i->die, q->label_val, i->name, | |
1829 | q, query_label); | |
39bcd429 | 1830 | } |
6b517475 JS |
1831 | else |
1832 | { | |
1833 | // Otherwise, simply probe all resolved functions. | |
1834 | for (func_info_map_t::iterator i = q->filtered_functions.begin(); | |
1835 | i != q->filtered_functions.end(); ++i) | |
1836 | query_func_info (i->entrypc, *i, q); | |
1837 | ||
1838 | // And all inline instances (if we're not excluding inlines with ".call") | |
1839 | if (! q->has_call) | |
1840 | for (inline_instance_map_t::iterator i | |
1841 | = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i) | |
1842 | query_inline_instance_info (*i, q); | |
1843 | } | |
39bcd429 | 1844 | return DWARF_CB_OK; |
bd2b1e68 | 1845 | } |
39bcd429 | 1846 | catch (const semantic_error& e) |
bd2b1e68 | 1847 | { |
39bcd429 FCE |
1848 | q->sess.print_error (e); |
1849 | return DWARF_CB_ABORT; | |
bd2b1e68 | 1850 | } |
bd2b1e68 GH |
1851 | } |
1852 | ||
0ce64fb8 | 1853 | |
4df79aaf JS |
1854 | void |
1855 | dwarf_query::query_module_functions () | |
1856 | { | |
1857 | try | |
1858 | { | |
1859 | filtered_srcfiles.clear(); | |
1860 | filtered_functions.clear(); | |
1861 | filtered_inlines.clear(); | |
1862 | ||
1863 | // Collect all module functions so we know which CUs are interesting | |
1864 | int rc = dw.iterate_single_function(query_dwarf_func, this, function); | |
1865 | if (rc != DWARF_CB_OK) | |
1866 | { | |
1867 | query_done = true; | |
1868 | return; | |
1869 | } | |
1870 | ||
1871 | set<void*> used_cus; // by cu->addr | |
1872 | vector<Dwarf_Die> cus; | |
1873 | Dwarf_Die cu_mem; | |
1874 | ||
1875 | for (func_info_map_t::iterator i = filtered_functions.begin(); | |
1876 | i != filtered_functions.end(); ++i) | |
1877 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1878 | used_cus.insert(cu_mem.addr).second) | |
1879 | cus.push_back(cu_mem); | |
1880 | ||
1881 | for (inline_instance_map_t::iterator i = filtered_inlines.begin(); | |
1882 | i != filtered_inlines.end(); ++i) | |
1883 | if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) && | |
1884 | used_cus.insert(cu_mem.addr).second) | |
1885 | cus.push_back(cu_mem); | |
1886 | ||
1887 | // Reset the dupes since we didn't actually collect them the first time | |
1888 | alias_dupes.clear(); | |
1889 | inline_dupes.clear(); | |
1890 | ||
1891 | // Run the query again on the individual CUs | |
1892 | for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i) | |
1893 | query_cu(&*i, this); | |
1894 | } | |
1895 | catch (const semantic_error& e) | |
1896 | { | |
1897 | sess.print_error (e); | |
1898 | } | |
1899 | } | |
1900 | ||
1901 | ||
5f0a03a6 JK |
1902 | static void |
1903 | validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q) | |
1904 | { | |
1905 | // Validate the machine code in this elf file against the | |
1906 | // session machine. This is important, in case the wrong kind | |
1907 | // of debuginfo is being automagically processed by elfutils. | |
1908 | // While we can tell i686 apart from x86-64, unfortunately | |
1909 | // we can't help confusing i586 vs i686 (both EM_386). | |
1910 | ||
1911 | Dwarf_Addr bias; | |
1912 | // We prefer dwfl_module_getdwarf to dwfl_module_getelf here, | |
1913 | // because dwfl_module_getelf can force costly section relocations | |
1914 | // we don't really need, while either will do for this purpose. | |
1915 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
1916 | ?: dwfl_module_getelf (mod, &bias)); | |
1917 | ||
1918 | GElf_Ehdr ehdr_mem; | |
1919 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
86bf665e | 1920 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } |
d8f31d0a | 1921 | assert(em); |
5f0a03a6 JK |
1922 | int elf_machine = em->e_machine; |
1923 | const char* debug_filename = ""; | |
1924 | const char* main_filename = ""; | |
1925 | (void) dwfl_module_info (mod, NULL, NULL, | |
1926 | NULL, NULL, NULL, | |
1927 | & main_filename, | |
1928 | & debug_filename); | |
1929 | const string& sess_machine = q->sess.architecture; | |
756c9462 FCE |
1930 | |
1931 | string expect_machine; // to match sess.machine (i.e., kernel machine) | |
1932 | string expect_machine2; | |
5f0a03a6 | 1933 | |
d27e6fd5 | 1934 | // NB: See also the 'uname -m' squashing done in main.cxx. |
5f0a03a6 JK |
1935 | switch (elf_machine) |
1936 | { | |
756c9462 FCE |
1937 | // x86 and ppc are bi-architecture; a 64-bit kernel |
1938 | // can normally run either 32-bit or 64-bit *userspace*. | |
1939 | case EM_386: | |
1940 | expect_machine = "i?86"; | |
1941 | if (! q->has_process) break; // 32-bit kernel/module | |
1942 | /* FALLSTHROUGH */ | |
1943 | case EM_X86_64: | |
1944 | expect_machine2 = "x86_64"; | |
1945 | break; | |
1946 | case EM_PPC: | |
756c9462 | 1947 | case EM_PPC64: |
5a1c472e | 1948 | expect_machine = "powerpc"; |
756c9462 | 1949 | break; |
3fe7d888 | 1950 | case EM_S390: expect_machine = "s390"; break; |
5f0a03a6 | 1951 | case EM_IA_64: expect_machine = "ia64"; break; |
d27e6fd5 | 1952 | case EM_ARM: expect_machine = "arm*"; break; |
5f0a03a6 JK |
1953 | // XXX: fill in some more of these |
1954 | default: expect_machine = "?"; break; | |
1955 | } | |
1956 | ||
1957 | if (! debug_filename) debug_filename = main_filename; | |
1958 | if (! debug_filename) debug_filename = name; | |
1959 | ||
756c9462 FCE |
1960 | if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 && |
1961 | fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0) | |
5f0a03a6 JK |
1962 | { |
1963 | stringstream msg; | |
b530b5b3 LB |
1964 | msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'", |
1965 | expect_machine.c_str(), expect_machine2.c_str(), elf_machine, | |
1966 | sess_machine.c_str(), debug_filename); | |
5f0a03a6 JK |
1967 | throw semantic_error(msg.str ()); |
1968 | } | |
1969 | ||
b57082de | 1970 | if (q->sess.verbose>1) |
2a97f50b | 1971 | clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64 |
b530b5b3 LB |
1972 | " file %s ELF machine %s|%s (code %d)\n", |
1973 | q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end, | |
1974 | q->dw.module_bias, debug_filename, expect_machine.c_str(), | |
1975 | expect_machine2.c_str(), elf_machine); | |
5f0a03a6 | 1976 | } |
1d3a40b6 | 1977 | |
91af0778 FCE |
1978 | |
1979 | ||
1980 | static Dwarf_Addr | |
1981 | lookup_symbol_address (Dwfl_Module *m, const char* wanted) | |
1982 | { | |
1983 | int syments = dwfl_module_getsymtab(m); | |
1984 | assert(syments); | |
1985 | for (int i = 1; i < syments; ++i) | |
1986 | { | |
1987 | GElf_Sym sym; | |
1988 | const char *name = dwfl_module_getsym(m, i, &sym, NULL); | |
1989 | if (name != NULL && strcmp(name, wanted) == 0) | |
1990 | return sym.st_value; | |
1991 | } | |
1992 | ||
1993 | return 0; | |
1994 | } | |
1995 | ||
1996 | ||
1997 | ||
bd2b1e68 | 1998 | static int |
b8da0ad1 | 1999 | query_module (Dwfl_Module *mod, |
91af0778 | 2000 | void **, |
b8da0ad1 | 2001 | const char *name, |
6f4c1275 | 2002 | Dwarf_Addr addr, |
b8da0ad1 | 2003 | void *arg) |
bd2b1e68 | 2004 | { |
91af0778 | 2005 | base_query *q = static_cast<base_query *>(arg); |
bd2b1e68 | 2006 | |
39bcd429 | 2007 | try |
e38d6504 | 2008 | { |
91af0778 FCE |
2009 | module_info* mi = q->sess.module_cache->cache[name]; |
2010 | if (mi == 0) | |
2011 | { | |
2012 | mi = q->sess.module_cache->cache[name] = new module_info(name); | |
2013 | ||
6f4c1275 FCE |
2014 | mi->mod = mod; |
2015 | mi->addr = addr; | |
91af0778 | 2016 | |
6f4c1275 FCE |
2017 | const char* debug_filename = ""; |
2018 | const char* main_filename = ""; | |
2019 | (void) dwfl_module_info (mod, NULL, NULL, | |
2020 | NULL, NULL, NULL, | |
2021 | & main_filename, | |
2022 | & debug_filename); | |
2023 | ||
ab3ed72d | 2024 | if (debug_filename || main_filename) |
91af0778 | 2025 | { |
6f4c1275 FCE |
2026 | mi->elf_path = debug_filename ?: main_filename; |
2027 | } | |
2028 | else if (name == TOK_KERNEL) | |
2029 | { | |
2030 | mi->dwarf_status = info_absent; | |
91af0778 | 2031 | } |
91af0778 FCE |
2032 | } |
2033 | // OK, enough of that module_info caching business. | |
2034 | ||
5f0a03a6 | 2035 | q->dw.focus_on_module(mod, mi); |
d9b516ca | 2036 | |
39bcd429 FCE |
2037 | // If we have enough information in the pattern to skip a module and |
2038 | // the module does not match that information, return early. | |
b8da0ad1 | 2039 | if (!q->dw.module_name_matches(q->module_val)) |
85007c04 | 2040 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
0cbbf9d1 FCE |
2041 | |
2042 | // Don't allow module("*kernel*") type expressions to match the | |
2043 | // elfutils module "kernel", which we refer to in the probe | |
2044 | // point syntax exclusively as "kernel.*". | |
2045 | if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel) | |
85007c04 | 2046 | return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK; |
b5d77020 | 2047 | |
5f0a03a6 JK |
2048 | if (mod) |
2049 | validate_module_elf(mod, name, q); | |
2050 | else | |
91af0778 FCE |
2051 | assert(q->has_kernel); // and no vmlinux to examine |
2052 | ||
2053 | if (q->sess.verbose>2) | |
b530b5b3 | 2054 | cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str()); |
91af0778 FCE |
2055 | |
2056 | ||
2057 | // Collect a few kernel addresses. XXX: these belong better | |
2058 | // to the sess.module_info["kernel"] struct. | |
2059 | if (q->dw.module_name == TOK_KERNEL) | |
c931ec8a | 2060 | { |
91af0778 FCE |
2061 | if (! q->sess.sym_kprobes_text_start) |
2062 | q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start"); | |
2063 | if (! q->sess.sym_kprobes_text_end) | |
2064 | q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end"); | |
2065 | if (! q->sess.sym_stext) | |
2066 | q->sess.sym_stext = lookup_symbol_address (mod, "_stext"); | |
c931ec8a FCE |
2067 | } |
2068 | ||
47e226ed SC |
2069 | // We either have a wildcard or an unresolved library |
2070 | if (q->has_library && (contains_glob_chars (q->path) | |
2071 | || q->path.find('/') == string::npos)) | |
84c84ac4 SC |
2072 | // handle .library(GLOB) |
2073 | q->dw.iterate_over_libraries (&q->query_library_callback, q); | |
576eaefe SC |
2074 | // .plt is translated to .plt.statement(N). We only want to iterate for the |
2075 | // .plt case | |
2076 | else if (q->has_plt && ! q->has_statement) | |
2077 | q->dw.iterate_over_plt (q, &q->query_plt_callback); | |
84c84ac4 SC |
2078 | else |
2079 | // search the module for matches of the probe point. | |
2080 | q->handle_query_module(); | |
bb788f9f | 2081 | |
b8da0ad1 | 2082 | // If we know that there will be no more matches, abort early. |
85007c04 | 2083 | if (q->dw.module_name_final_match(q->module_val) || pending_interrupts) |
b8da0ad1 FCE |
2084 | return DWARF_CB_ABORT; |
2085 | else | |
2086 | return DWARF_CB_OK; | |
7a053d3b | 2087 | } |
39bcd429 | 2088 | catch (const semantic_error& e) |
bd2b1e68 | 2089 | { |
39bcd429 FCE |
2090 | q->sess.print_error (e); |
2091 | return DWARF_CB_ABORT; | |
bd2b1e68 | 2092 | } |
bd2b1e68 GH |
2093 | } |
2094 | ||
35d4ab18 | 2095 | |
84c84ac4 | 2096 | void |
5d5bd369 | 2097 | base_query::query_library_callback (void *q, const char *data) |
84c84ac4 SC |
2098 | { |
2099 | base_query *me = (base_query*)q; | |
5d5bd369 | 2100 | me->query_library (data); |
84c84ac4 SC |
2101 | } |
2102 | ||
2103 | ||
2104 | void | |
51d6bda3 SC |
2105 | query_one_library (const char *library, dwflpp & dw, |
2106 | const string user_lib, probe * base_probe, probe_point *base_loc, | |
2107 | vector<derived_probe *> & results) | |
84c84ac4 | 2108 | { |
47e226ed | 2109 | if (dw.function_name_matches_pattern(library, "*" + user_lib)) |
84c84ac4 | 2110 | { |
05fb3e0c WF |
2111 | string library_path = find_executable (library, "", dw.sess.sysenv, |
2112 | "LD_LIBRARY_PATH"); | |
84c84ac4 SC |
2113 | probe_point* specific_loc = new probe_point(*base_loc); |
2114 | specific_loc->optional = true; | |
2115 | vector<probe_point::component*> derived_comps; | |
2116 | ||
2117 | vector<probe_point::component*>::iterator it; | |
2118 | for (it = specific_loc->components.begin(); | |
2119 | it != specific_loc->components.end(); ++it) | |
2120 | if ((*it)->functor == TOK_LIBRARY) | |
2121 | derived_comps.push_back(new probe_point::component(TOK_LIBRARY, | |
2122 | new literal_string(library_path))); | |
2123 | else | |
2124 | derived_comps.push_back(*it); | |
2125 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2126 | derived_loc->components = derived_comps; | |
2127 | probe *new_base = base_probe->create_alias(derived_loc, specific_loc); | |
51d6bda3 SC |
2128 | derive_probes(dw.sess, new_base, results); |
2129 | if (dw.sess.verbose > 2) | |
84c84ac4 SC |
2130 | clog << _("module=") << library_path; |
2131 | } | |
2132 | } | |
2133 | ||
2134 | ||
51d6bda3 SC |
2135 | void |
2136 | dwarf_query::query_library (const char *library) | |
2137 | { | |
2138 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
2139 | } | |
2140 | ||
576eaefe SC |
2141 | struct plt_expanding_visitor: public var_expanding_visitor |
2142 | { | |
2143 | plt_expanding_visitor(const string & entry): | |
2144 | entry (entry) | |
2145 | { | |
2146 | } | |
2147 | const string & entry; | |
2148 | ||
2149 | void visit_target_symbol (target_symbol* e); | |
2150 | }; | |
2151 | ||
2152 | ||
2153 | void | |
2154 | base_query::query_plt_callback (void *q, const char *entry, size_t address) | |
2155 | { | |
2156 | base_query *me = (base_query*)q; | |
2157 | if (me->dw.function_name_matches_pattern (entry, me->plt_val)) | |
2158 | me->query_plt (entry, address); | |
2159 | } | |
2160 | ||
2161 | ||
2162 | void | |
2163 | query_one_plt (const char *entry, long addr, dwflpp & dw, | |
2164 | probe * base_probe, probe_point *base_loc, | |
2165 | vector<derived_probe *> & results) | |
2166 | { | |
2167 | probe_point* specific_loc = new probe_point(*base_loc); | |
2168 | specific_loc->optional = true; | |
2169 | vector<probe_point::component*> derived_comps; | |
2170 | ||
2171 | if (dw.sess.verbose > 2) | |
2172 | clog << _F("plt entry=%s\n", entry); | |
2173 | ||
2174 | // query_module_symtab requires .plt to recognize that it can set the probe at | |
2175 | // a plt entry so we convert process.plt to process.plt.statement | |
2176 | vector<probe_point::component*>::iterator it; | |
2177 | for (it = specific_loc->components.begin(); | |
2178 | it != specific_loc->components.end(); ++it) | |
2179 | if ((*it)->functor == TOK_PLT) | |
3d372d6b SC |
2180 | { |
2181 | derived_comps.push_back(*it); | |
2182 | derived_comps.push_back(new probe_point::component(TOK_STATEMENT, | |
2183 | new literal_number(addr))); | |
2184 | } | |
576eaefe SC |
2185 | else |
2186 | derived_comps.push_back(*it); | |
2187 | probe_point* derived_loc = new probe_point(*specific_loc); | |
2188 | derived_loc->components = derived_comps; | |
2189 | probe *new_base = base_probe->create_alias(derived_loc, specific_loc); | |
2190 | string e = string(entry); | |
2191 | plt_expanding_visitor pltv (e); | |
2192 | pltv.replace (new_base->body); | |
2193 | derive_probes(dw.sess, new_base, results); | |
2194 | } | |
2195 | ||
2196 | ||
2197 | void | |
2198 | dwarf_query::query_plt (const char *entry, size_t address) | |
2199 | { | |
2200 | query_one_plt (entry, address, dw, base_probe, base_loc, results); | |
2201 | } | |
51d6bda3 | 2202 | |
435f53a7 FCE |
2203 | // This would more naturally fit into elaborate.cxx:semantic_pass_symbols, |
2204 | // but the needed declaration for module_cache is not available there. | |
2205 | // Nor for that matter in session.cxx. Only in this CU is that field ever | |
2206 | // set (in query_module() above), so we clean it up here too. | |
2207 | static void | |
2208 | delete_session_module_cache (systemtap_session& s) | |
2209 | { | |
2210 | if (s.module_cache) { | |
2211 | if (s.verbose > 3) | |
b530b5b3 | 2212 | clog << _("deleting module_cache") << endl; |
435f53a7 FCE |
2213 | delete s.module_cache; |
2214 | s.module_cache = 0; | |
2215 | } | |
2216 | } | |
2217 | ||
2218 | ||
de688825 | 2219 | struct dwarf_var_expanding_visitor: public var_expanding_visitor |
35d4ab18 | 2220 | { |
77de5e9e | 2221 | dwarf_query & q; |
bcc12710 | 2222 | Dwarf_Die *scope_die; |
77de5e9e | 2223 | Dwarf_Addr addr; |
8c819921 | 2224 | block *add_block; |
2260f4e3 | 2225 | block *add_call_probe; // synthesized from .return probes with saved $vars |
1630966a JS |
2226 | // NB: tids are not always collected in add_block & add_call_probe, because |
2227 | // gen_kretprobe_saved_return doesn't need them. Thus we need these extra | |
2228 | // *_tid bools for gen_mapped_saved_return to tell what's there. | |
8cc799a5 | 2229 | bool add_block_tid, add_call_probe_tid; |
af234c40 JS |
2230 | unsigned saved_longs, saved_strings; // data saved within kretprobes |
2231 | map<std::string, expression *> return_ts_map; | |
729455a7 | 2232 | vector<Dwarf_Die> scopes; |
b95e2b79 | 2233 | bool visited; |
77de5e9e | 2234 | |
de688825 | 2235 | dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a): |
af234c40 | 2236 | q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL), |
8cc799a5 | 2237 | add_block_tid(false), add_call_probe_tid(false), |
af234c40 | 2238 | saved_longs(0), saved_strings(0), visited(false) {} |
277c21bc | 2239 | expression* gen_mapped_saved_return(expression* e, const string& name); |
140be17a | 2240 | expression* gen_kretprobe_saved_return(expression* e); |
a7999c82 JS |
2241 | void visit_target_symbol_saved_return (target_symbol* e); |
2242 | void visit_target_symbol_context (target_symbol* e); | |
d7f3e0c5 | 2243 | void visit_target_symbol (target_symbol* e); |
c24447be | 2244 | void visit_cast_op (cast_op* e); |
8cc799a5 | 2245 | void visit_entry_op (entry_op* e); |
729455a7 | 2246 | private: |
bfa7e523 | 2247 | vector<Dwarf_Die>& getcuscope(target_symbol *e); |
729455a7 | 2248 | vector<Dwarf_Die>& getscopes(target_symbol *e); |
77de5e9e GH |
2249 | }; |
2250 | ||
2251 | ||
de688825 | 2252 | unsigned var_expanding_visitor::tick = 0; |
77de5e9e | 2253 | |
a50de939 | 2254 | |
74fe61bc | 2255 | var_expanding_visitor::var_expanding_visitor (): op() |
a50de939 DS |
2256 | { |
2257 | // FIXME: for the time being, by default we only support plain '$foo | |
2258 | // = bar', not '+=' or any other op= variant. This is fixable, but a | |
2259 | // bit ugly. | |
2260 | // | |
2261 | // If derived classes desire to add additional operator support, add | |
2262 | // new operators to this list in the derived class constructor. | |
2263 | valid_ops.insert ("="); | |
2264 | } | |
2265 | ||
2266 | ||
87214add JS |
2267 | bool |
2268 | var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop, | |
2269 | expression*& lvalue, expression*& rvalue) | |
77de5e9e | 2270 | { |
e57b735a GH |
2271 | // Our job would normally be to require() the left and right sides |
2272 | // into a new assignment. What we're doing is slightly trickier: | |
2273 | // we're pushing a functioncall** onto a stack, and if our left | |
2274 | // child sets the functioncall* for that value, we're going to | |
2275 | // assume our left child was a target symbol -- transformed into a | |
2276 | // set_target_foo(value) call, and it wants to take our right child | |
2277 | // as the argument "value". | |
2278 | // | |
2279 | // This is why some people claim that languages with | |
2280 | // constructor-decomposing case expressions have a leg up on | |
2281 | // visitors. | |
2282 | ||
2283 | functioncall *fcall = NULL; | |
d9b516ca | 2284 | |
a50de939 | 2285 | // Let visit_target_symbol know what operator it should handle. |
87214add JS |
2286 | const string* old_op = op; |
2287 | op = &eop; | |
a50de939 | 2288 | |
e57b735a | 2289 | target_symbol_setter_functioncalls.push (&fcall); |
87214add | 2290 | replace (lvalue); |
e57b735a | 2291 | target_symbol_setter_functioncalls.pop (); |
87214add JS |
2292 | replace (rvalue); |
2293 | ||
2294 | op = old_op; | |
e57b735a GH |
2295 | |
2296 | if (fcall != NULL) | |
77de5e9e | 2297 | { |
e57b735a GH |
2298 | // Our left child is informing us that it was a target variable |
2299 | // and it has been replaced with a set_target_foo() function | |
2300 | // call; we are going to provide that function call -- with the | |
2301 | // right child spliced in as sole argument -- in place of | |
de688825 | 2302 | // ourselves, in the var expansion we're in the middle of making. |
e57b735a | 2303 | |
87214add | 2304 | if (valid_ops.find (eop) == valid_ops.end ()) |
a50de939 DS |
2305 | { |
2306 | // Build up a list of supported operators. | |
2307 | string ops; | |
2308 | std::set<string>::iterator i; | |
b530b5b3 | 2309 | int valid_ops_size = 0; |
a50de939 | 2310 | for (i = valid_ops.begin(); i != valid_ops.end(); i++) |
b530b5b3 | 2311 | { |
a50de939 | 2312 | ops += " " + *i + ","; |
b530b5b3 LB |
2313 | valid_ops_size++; |
2314 | } | |
a50de939 DS |
2315 | ops.resize(ops.size() - 1); // chop off the last ',' |
2316 | ||
2317 | // Throw the error. | |
1e41115c LB |
2318 | throw semantic_error (_F(ngettext("Only the following assign operator is implemented on target variables: %s", |
2319 | "Only the following assign operators are implemented on target variables: %s", | |
b530b5b3 LB |
2320 | valid_ops_size), ops.c_str()), tok); |
2321 | ||
a50de939 | 2322 | } |
e57b735a | 2323 | |
87214add JS |
2324 | assert (lvalue == fcall); |
2325 | if (rvalue) | |
2326 | fcall->args.push_back (rvalue); | |
4ed05b15 | 2327 | provide (fcall); |
87214add | 2328 | return true; |
77de5e9e | 2329 | } |
e57b735a | 2330 | else |
87214add JS |
2331 | return false; |
2332 | } | |
2333 | ||
2334 | ||
2335 | void | |
2336 | var_expanding_visitor::visit_assignment (assignment* e) | |
2337 | { | |
2338 | if (!rewrite_lvalue (e->tok, e->op, e->left, e->right)) | |
2339 | provide (e); | |
2340 | } | |
2341 | ||
2342 | ||
2343 | void | |
2344 | var_expanding_visitor::visit_pre_crement (pre_crement* e) | |
2345 | { | |
2346 | expression *dummy = NULL; | |
2347 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2348 | provide (e); | |
2349 | } | |
2350 | ||
2351 | ||
2352 | void | |
2353 | var_expanding_visitor::visit_post_crement (post_crement* e) | |
2354 | { | |
2355 | expression *dummy = NULL; | |
2356 | if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy)) | |
2357 | provide (e); | |
2358 | } | |
2359 | ||
2360 | ||
2361 | void | |
2362 | var_expanding_visitor::visit_delete_statement (delete_statement* s) | |
2363 | { | |
2364 | string fakeop = "delete"; | |
2365 | expression *dummy = NULL; | |
2366 | if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy)) | |
2367 | provide (s); | |
e57b735a | 2368 | } |
d9b516ca | 2369 | |
d7f3e0c5 | 2370 | |
30263a73 FCE |
2371 | void |
2372 | var_expanding_visitor::visit_defined_op (defined_op* e) | |
2373 | { | |
2374 | bool resolved = true; | |
2375 | ||
2376 | defined_ops.push (e); | |
2377 | try { | |
2378 | // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is | |
2379 | // defined with a target_symbol* operand, a subsidiary call may attempt to | |
2380 | // rewrite it to a general expression* instead, and require<> happily | |
2381 | // casts to/from void*, causing possible memory corruption. We use | |
2382 | // expression* here, being the general case of rewritten $variable. | |
2383 | expression *foo1 = e->operand; | |
2384 | foo1 = require (foo1); | |
2385 | ||
c69a87e0 | 2386 | // NB: Formerly, we had some curious cases to consider here, depending on what |
30263a73 | 2387 | // various visit_target_symbol() implementations do for successful or |
c69a87e0 FCE |
2388 | // erroneous resolutions. Some would signal a visit_target_symbol failure |
2389 | // with an exception, with a set flag within the target_symbol, or nothing | |
2390 | // at all. | |
30263a73 | 2391 | // |
c69a87e0 FCE |
2392 | // Now, failures always have to be signalled with a |
2393 | // saved_conversion_error being chained to the target_symbol. | |
2394 | // Successes have to result in an attempted rewrite of the | |
850bfddd | 2395 | // target_symbol (via provide()). |
780f11ff | 2396 | // |
c69a87e0 FCE |
2397 | // Edna Mode: "no capes". fche: "no exceptions". |
2398 | ||
30263a73 FCE |
2399 | // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error |
2400 | // | |
2401 | // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error | |
2402 | // | |
2403 | // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic | |
2404 | // dwarf probe to take care of it. | |
2405 | // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor | |
2406 | // to pass through @defined() to the synthetic dwarf probe. | |
780f11ff | 2407 | // |
30263a73 FCE |
2408 | // utrace: success: rewrites to function; failure: semantic_error |
2409 | // | |
850bfddd | 2410 | // procfs: success: rewrites to function; failure: semantic_error |
30263a73 FCE |
2411 | |
2412 | target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1); | |
c69a87e0 | 2413 | if (foo2 && foo2->saved_conversion_error) // failing |
30263a73 | 2414 | resolved = false; |
a45664f4 | 2415 | else if (foo2) // unresolved but not marked failing |
b7aedf26 | 2416 | { |
780f11ff JS |
2417 | // There are some visitors that won't touch certain target_symbols, |
2418 | // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should | |
2419 | // leave it for now so some other visitor can have a chance. | |
b7aedf26 JS |
2420 | e->operand = foo2; |
2421 | provide (e); | |
2422 | return; | |
2423 | } | |
30263a73 FCE |
2424 | else // resolved, rewritten to some other expression type |
2425 | resolved = true; | |
780f11ff | 2426 | } catch (const semantic_error& e) { |
c69a87e0 | 2427 | assert (0); // should not happen |
30263a73 FCE |
2428 | } |
2429 | defined_ops.pop (); | |
2430 | ||
2431 | literal_number* ln = new literal_number (resolved ? 1 : 0); | |
2432 | ln->tok = e->tok; | |
2433 | provide (ln); | |
2434 | } | |
2435 | ||
2436 | ||
5f36109e JS |
2437 | struct dwarf_pretty_print |
2438 | { | |
2439 | dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc, | |
2440 | const string& local, bool userspace_p, | |
2441 | const target_symbol& e): | |
d19a9a82 JS |
2442 | dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL), |
2443 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2444 | { |
2445 | init_ts (e); | |
2446 | dw.type_die_for_local (scopes, pc, local, ts, &base_type); | |
2447 | } | |
2448 | ||
2449 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc, | |
2450 | bool userspace_p, const target_symbol& e): | |
d19a9a82 JS |
2451 | dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL), |
2452 | userspace_p(userspace_p), deref_p(true) | |
5f36109e JS |
2453 | { |
2454 | init_ts (e); | |
2455 | dw.type_die_for_return (&scopes[0], pc, ts, &base_type); | |
2456 | } | |
2457 | ||
2458 | dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer, | |
d19a9a82 | 2459 | bool deref_p, bool userspace_p, const target_symbol& e): |
5f36109e | 2460 | dw(dw), pc(0), pointer(pointer), pointer_type(*type_die), |
d19a9a82 | 2461 | userspace_p(userspace_p), deref_p(deref_p) |
5f36109e JS |
2462 | { |
2463 | init_ts (e); | |
2464 | dw.type_die_for_pointer (type_die, ts, &base_type); | |
2465 | } | |
2466 | ||
2467 | functioncall* expand (); | |
ce83ff57 | 2468 | ~dwarf_pretty_print () { delete ts; } |
5f36109e JS |
2469 | |
2470 | private: | |
2471 | dwflpp& dw; | |
2472 | target_symbol* ts; | |
7d11d8c9 | 2473 | bool print_full; |
5f36109e JS |
2474 | Dwarf_Die base_type; |
2475 | ||
2476 | string local; | |
2477 | vector<Dwarf_Die> scopes; | |
2478 | Dwarf_Addr pc; | |
2479 | ||
2480 | expression* pointer; | |
2481 | Dwarf_Die pointer_type; | |
2482 | ||
d19a9a82 | 2483 | const bool userspace_p, deref_p; |
5f36109e JS |
2484 | |
2485 | void recurse (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2486 | print_format* pf, bool top=false); |
600551ca JS |
2487 | void recurse_bitfield (Dwarf_Die* type, target_symbol* e, |
2488 | print_format* pf); | |
5f36109e | 2489 | void recurse_base (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2490 | print_format* pf); |
5f36109e | 2491 | void recurse_array (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2492 | print_format* pf, bool top); |
5f36109e | 2493 | void recurse_pointer (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2494 | print_format* pf, bool top); |
5f36109e | 2495 | void recurse_struct (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2496 | print_format* pf, bool top); |
5f36109e | 2497 | void recurse_struct_members (Dwarf_Die* type, target_symbol* e, |
7d11d8c9 | 2498 | print_format* pf, int& count); |
bbee5bb8 | 2499 | bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf); |
5f36109e JS |
2500 | |
2501 | void init_ts (const target_symbol& e); | |
2502 | expression* deref (target_symbol* e); | |
c55ea10d | 2503 | bool push_deref (print_format* pf, const string& fmt, target_symbol* e); |
5f36109e JS |
2504 | }; |
2505 | ||
2506 | ||
2507 | void | |
2508 | dwarf_pretty_print::init_ts (const target_symbol& e) | |
2509 | { | |
2510 | // Work with a new target_symbol so we can modify arguments | |
2511 | ts = new target_symbol (e); | |
2512 | ||
2513 | if (ts->addressof) | |
b530b5b3 | 2514 | throw semantic_error(_("cannot take address of pretty-printed variable"), ts->tok); |
5f36109e JS |
2515 | |
2516 | if (ts->components.empty() || | |
2517 | ts->components.back().type != target_symbol::comp_pretty_print) | |
b530b5b3 | 2518 | throw semantic_error(_("invalid target_symbol for pretty-print"), ts->tok); |
7d11d8c9 | 2519 | print_full = ts->components.back().member.length() > 1; |
5f36109e JS |
2520 | ts->components.pop_back(); |
2521 | } | |
2522 | ||
2523 | ||
2524 | functioncall* | |
2525 | dwarf_pretty_print::expand () | |
2526 | { | |
2527 | static unsigned tick = 0; | |
2528 | ||
2529 | // function pretty_print_X([pointer], [arg1, arg2, ...]) { | |
7d11d8c9 JS |
2530 | // try { |
2531 | // return sprintf("{.foo=...}", (ts)->foo, ...) | |
2532 | // } catch { | |
2533 | // return "ERROR" | |
2534 | // } | |
5f36109e JS |
2535 | // } |
2536 | ||
2537 | // Create the function decl and call. | |
2538 | ||
2539 | functiondecl *fdecl = new functiondecl; | |
2540 | fdecl->tok = ts->tok; | |
2541 | fdecl->synthetic = true; | |
2542 | fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++); | |
2543 | fdecl->type = pe_string; | |
2544 | ||
2545 | functioncall* fcall = new functioncall; | |
2546 | fcall->tok = ts->tok; | |
2547 | fcall->function = fdecl->name; | |
140be17a | 2548 | fcall->type = pe_string; |
5f36109e JS |
2549 | |
2550 | // If there's a <pointer>, replace it with a new var and make that | |
2551 | // the first function argument. | |
2552 | if (pointer) | |
2553 | { | |
2554 | vardecl *v = new vardecl; | |
2555 | v->type = pe_long; | |
2556 | v->name = "pointer"; | |
2557 | v->tok = ts->tok; | |
2558 | fdecl->formal_args.push_back (v); | |
2559 | fcall->args.push_back (pointer); | |
2560 | ||
2561 | symbol* sym = new symbol; | |
2562 | sym->tok = ts->tok; | |
2563 | sym->name = v->name; | |
5f36109e JS |
2564 | pointer = sym; |
2565 | } | |
2566 | ||
2567 | // For each expression argument, replace it with a function argument. | |
2568 | for (unsigned i = 0; i < ts->components.size(); ++i) | |
2569 | if (ts->components[i].type == target_symbol::comp_expression_array_index) | |
2570 | { | |
2571 | vardecl *v = new vardecl; | |
2572 | v->type = pe_long; | |
2573 | v->name = "index" + lex_cast(i); | |
2574 | v->tok = ts->tok; | |
2575 | fdecl->formal_args.push_back (v); | |
2576 | fcall->args.push_back (ts->components[i].expr_index); | |
2577 | ||
2578 | symbol* sym = new symbol; | |
2579 | sym->tok = ts->tok; | |
2580 | sym->name = v->name; | |
5f36109e JS |
2581 | ts->components[i].expr_index = sym; |
2582 | } | |
2583 | ||
2584 | // Create the return sprintf. | |
2585 | token* pf_tok = new token(*ts->tok); | |
2586 | pf_tok->content = "sprintf"; | |
2587 | print_format* pf = print_format::create(pf_tok); | |
2588 | return_statement* rs = new return_statement; | |
2589 | rs->tok = ts->tok; | |
2590 | rs->value = pf; | |
5f36109e JS |
2591 | |
2592 | // Recurse into the actual values. | |
7d11d8c9 | 2593 | recurse (&base_type, ts, pf, true); |
5f36109e JS |
2594 | pf->components = print_format::string_to_components(pf->raw_components); |
2595 | ||
7d11d8c9 JS |
2596 | // Create the try-catch net |
2597 | try_block* tb = new try_block; | |
2598 | tb->tok = ts->tok; | |
2599 | tb->try_block = rs; | |
2600 | tb->catch_error_var = 0; | |
2601 | return_statement* rs2 = new return_statement; | |
2602 | rs2->tok = ts->tok; | |
2603 | rs2->value = new literal_string ("ERROR"); | |
2604 | rs2->value->tok = ts->tok; | |
2605 | tb->catch_block = rs2; | |
2606 | fdecl->body = tb; | |
2607 | ||
f8809d54 | 2608 | fdecl->join (dw.sess); |
5f36109e JS |
2609 | return fcall; |
2610 | } | |
2611 | ||
2612 | ||
2613 | void | |
2614 | dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e, | |
7d11d8c9 | 2615 | print_format* pf, bool top) |
5f36109e JS |
2616 | { |
2617 | Dwarf_Die type; | |
2618 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2619 | ||
2620 | switch (dwarf_tag(&type)) | |
2621 | { | |
2622 | default: | |
2623 | // XXX need a warning? | |
2624 | // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type)) | |
2625 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2626 | pf->raw_components.append("?"); | |
2627 | break; | |
2628 | ||
2629 | case DW_TAG_enumeration_type: | |
2630 | case DW_TAG_base_type: | |
7d11d8c9 | 2631 | recurse_base (&type, e, pf); |
5f36109e JS |
2632 | break; |
2633 | ||
2634 | case DW_TAG_array_type: | |
7d11d8c9 | 2635 | recurse_array (&type, e, pf, top); |
5f36109e JS |
2636 | break; |
2637 | ||
2638 | case DW_TAG_pointer_type: | |
2639 | case DW_TAG_reference_type: | |
2640 | case DW_TAG_rvalue_reference_type: | |
7d11d8c9 | 2641 | recurse_pointer (&type, e, pf, top); |
5f36109e JS |
2642 | break; |
2643 | ||
2644 | case DW_TAG_subroutine_type: | |
c55ea10d | 2645 | push_deref (pf, "<function>:%p", e); |
5f36109e JS |
2646 | break; |
2647 | ||
2648 | case DW_TAG_union_type: | |
5f36109e JS |
2649 | case DW_TAG_structure_type: |
2650 | case DW_TAG_class_type: | |
7d11d8c9 | 2651 | recurse_struct (&type, e, pf, top); |
5f36109e JS |
2652 | break; |
2653 | } | |
2654 | } | |
2655 | ||
2656 | ||
600551ca JS |
2657 | // Bit fields are handled as a special-case combination of recurse() and |
2658 | // recurse_base(), only called from recurse_struct_members(). The main | |
2659 | // difference is that the value is always printed numerically, even if the | |
2660 | // underlying type is a char. | |
2661 | void | |
2662 | dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e, | |
2663 | print_format* pf) | |
2664 | { | |
2665 | Dwarf_Die type; | |
2666 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
2667 | ||
2668 | int tag = dwarf_tag(&type); | |
2669 | if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type) | |
2670 | { | |
2671 | // XXX need a warning? | |
2672 | // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag) | |
2673 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2674 | pf->raw_components.append("?"); | |
2675 | return; | |
2676 | } | |
2677 | ||
2678 | Dwarf_Attribute attr; | |
2679 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2680 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr), | |
2681 | &encoding); | |
2682 | switch (encoding) | |
2683 | { | |
2684 | case DW_ATE_float: | |
2685 | case DW_ATE_complex_float: | |
2686 | // XXX need a warning? | |
2687 | // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding) | |
2688 | // + ") for " + dwarf_type_name(&type), e->tok); | |
2689 | pf->raw_components.append("?"); | |
2690 | break; | |
2691 | ||
2692 | case DW_ATE_unsigned: | |
2693 | case DW_ATE_unsigned_char: | |
2694 | push_deref (pf, "%u", e); | |
2695 | break; | |
2696 | ||
2697 | case DW_ATE_signed: | |
2698 | case DW_ATE_signed_char: | |
2699 | default: | |
2700 | push_deref (pf, "%i", e); | |
2701 | break; | |
2702 | } | |
2703 | } | |
2704 | ||
2705 | ||
5f36109e JS |
2706 | void |
2707 | dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2708 | print_format* pf) |
5f36109e JS |
2709 | { |
2710 | Dwarf_Attribute attr; | |
2711 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2712 | dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr), | |
2713 | &encoding); | |
5f36109e JS |
2714 | switch (encoding) |
2715 | { | |
2716 | case DW_ATE_float: | |
2717 | case DW_ATE_complex_float: | |
2718 | // XXX need a warning? | |
2719 | // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding) | |
2720 | // + ") for " + dwarf_type_name(type), e->tok); | |
2721 | pf->raw_components.append("?"); | |
5f36109e JS |
2722 | break; |
2723 | ||
6561d8d1 | 2724 | case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char |
5f36109e JS |
2725 | case DW_ATE_signed_char: |
2726 | case DW_ATE_unsigned_char: | |
941101c1 JS |
2727 | // Use escapes to make sure that non-printable characters |
2728 | // don't interrupt our stream (especially '\0' values). | |
2729 | push_deref (pf, "'%#c'", e); | |
5f36109e JS |
2730 | break; |
2731 | ||
2732 | case DW_ATE_unsigned: | |
c55ea10d | 2733 | push_deref (pf, "%u", e); |
5f36109e JS |
2734 | break; |
2735 | ||
600551ca | 2736 | case DW_ATE_signed: |
5f36109e | 2737 | default: |
c55ea10d | 2738 | push_deref (pf, "%i", e); |
5f36109e JS |
2739 | break; |
2740 | } | |
5f36109e JS |
2741 | } |
2742 | ||
2743 | ||
2744 | void | |
2745 | dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2746 | print_format* pf, bool top) |
5f36109e | 2747 | { |
7d11d8c9 JS |
2748 | if (!top && !print_full) |
2749 | { | |
2750 | pf->raw_components.append("[...]"); | |
2751 | return; | |
2752 | } | |
2753 | ||
5f36109e JS |
2754 | Dwarf_Die childtype; |
2755 | dwarf_attr_die (type, DW_AT_type, &childtype); | |
bbee5bb8 JS |
2756 | |
2757 | if (print_chars (&childtype, e, pf)) | |
2758 | return; | |
2759 | ||
5f36109e JS |
2760 | pf->raw_components.append("["); |
2761 | ||
2762 | // We print the array up to the first 5 elements. | |
2763 | // XXX how can we determine the array size? | |
2764 | // ... for now, just print the first element | |
64cddf39 | 2765 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
5f36109e | 2766 | unsigned i, size = 1; |
64cddf39 | 2767 | for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i) |
5f36109e JS |
2768 | { |
2769 | if (i > 0) | |
2770 | pf->raw_components.append(", "); | |
2771 | target_symbol* e2 = new target_symbol(*e); | |
2772 | e2->components.push_back (target_symbol::component(e->tok, i)); | |
7d11d8c9 | 2773 | recurse (&childtype, e2, pf); |
5f36109e JS |
2774 | } |
2775 | if (i < size || 1/*XXX until real size is known */) | |
2776 | pf->raw_components.append(", ..."); | |
2777 | pf->raw_components.append("]"); | |
2778 | } | |
2779 | ||
2780 | ||
2781 | void | |
2782 | dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2783 | print_format* pf, bool top) |
5f36109e | 2784 | { |
7d11d8c9 | 2785 | // We chase to top-level pointers, but leave the rest alone |
d19a9a82 | 2786 | bool void_p = true; |
7d11d8c9 | 2787 | Dwarf_Die pointee; |
bbee5bb8 | 2788 | if (dwarf_attr_die (type, DW_AT_type, &pointee)) |
d19a9a82 JS |
2789 | { |
2790 | try | |
2791 | { | |
2792 | dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e); | |
2793 | void_p = false; | |
2794 | } | |
2795 | catch (const semantic_error&) {} | |
2796 | } | |
2797 | ||
2798 | if (!void_p) | |
5f36109e | 2799 | { |
bbee5bb8 JS |
2800 | if (print_chars (&pointee, e, pf)) |
2801 | return; | |
2802 | ||
2803 | if (top) | |
2804 | { | |
2805 | recurse (&pointee, e, pf, top); | |
2806 | return; | |
2807 | } | |
5f36109e | 2808 | } |
bbee5bb8 | 2809 | |
c55ea10d | 2810 | push_deref (pf, "%p", e); |
5f36109e JS |
2811 | } |
2812 | ||
2813 | ||
2814 | void | |
2815 | dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2816 | print_format* pf, bool top) |
5f36109e | 2817 | { |
bdec0e18 JS |
2818 | if (dwarf_hasattr(type, DW_AT_declaration)) |
2819 | { | |
a44a7cb5 | 2820 | Dwarf_Die *resolved = dw.declaration_resolve(type); |
bdec0e18 JS |
2821 | if (!resolved) |
2822 | { | |
2823 | // could be an error, but for now just stub it | |
2824 | // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok); | |
2825 | pf->raw_components.append("{...}"); | |
2826 | return; | |
2827 | } | |
2828 | type = resolved; | |
2829 | } | |
2830 | ||
5f36109e JS |
2831 | int count = 0; |
2832 | pf->raw_components.append("{"); | |
7d11d8c9 JS |
2833 | if (top || print_full) |
2834 | recurse_struct_members (type, e, pf, count); | |
2835 | else | |
2836 | pf->raw_components.append("..."); | |
5f36109e JS |
2837 | pf->raw_components.append("}"); |
2838 | } | |
2839 | ||
2840 | ||
2841 | void | |
2842 | dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e, | |
7d11d8c9 | 2843 | print_format* pf, int& count) |
5f36109e | 2844 | { |
a80f28d8 JS |
2845 | /* With inheritance, a subclass may mask member names of parent classes, so |
2846 | * our search among the inheritance tree must be breadth-first rather than | |
2847 | * depth-first (recursive). The type die is still our starting point. When | |
2848 | * we encounter a masked name, just skip it. */ | |
2849 | set<string> dupes; | |
2850 | deque<Dwarf_Die> inheritees(1, *type); | |
2851 | for (; !inheritees.empty(); inheritees.pop_front()) | |
2852 | { | |
dee830d9 | 2853 | Dwarf_Die child, childtype, import; |
a80f28d8 JS |
2854 | if (dwarf_child (&inheritees.front(), &child) == 0) |
2855 | do | |
2856 | { | |
2857 | target_symbol* e2 = e; | |
5f36109e | 2858 | |
a80f28d8 JS |
2859 | // skip static members |
2860 | if (dwarf_hasattr(&child, DW_AT_declaration)) | |
2861 | continue; | |
5f36109e | 2862 | |
a80f28d8 | 2863 | int tag = dwarf_tag (&child); |
5f36109e | 2864 | |
dee830d9 MW |
2865 | /* Pretend imported units contain members by recursing into |
2866 | struct_member printing with the same count. */ | |
2867 | if (tag == DW_TAG_imported_unit | |
2868 | && dwarf_attr_die (&child, DW_AT_import, &import)) | |
2869 | recurse_struct_members (&import, e2, pf, count); | |
2870 | ||
a80f28d8 JS |
2871 | if (tag != DW_TAG_member && tag != DW_TAG_inheritance) |
2872 | continue; | |
5f36109e | 2873 | |
a80f28d8 | 2874 | dwarf_attr_die (&child, DW_AT_type, &childtype); |
5f36109e | 2875 | |
a80f28d8 JS |
2876 | if (tag == DW_TAG_inheritance) |
2877 | { | |
2878 | inheritees.push_back(childtype); | |
2879 | continue; | |
2880 | } | |
5f36109e | 2881 | |
a80f28d8 JS |
2882 | int childtag = dwarf_tag (&childtype); |
2883 | const char *member = dwarf_diename (&child); | |
3a147004 | 2884 | |
a80f28d8 JS |
2885 | // "_vptr.foo" members are C++ virtual function tables, |
2886 | // which (generally?) aren't interesting for users. | |
2887 | if (member && startswith(member, "_vptr.")) | |
2888 | continue; | |
3a147004 | 2889 | |
a80f28d8 JS |
2890 | // skip inheritance-masked duplicates |
2891 | if (member && !dupes.insert(member).second) | |
2892 | continue; | |
64cddf39 | 2893 | |
a80f28d8 JS |
2894 | if (++count > 1) |
2895 | pf->raw_components.append(", "); | |
64cddf39 | 2896 | |
a80f28d8 JS |
2897 | // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format. |
2898 | if (pf->args.size() >= 32) | |
2899 | { | |
2900 | pf->raw_components.append("..."); | |
2901 | break; | |
2902 | } | |
2903 | ||
2904 | if (member) | |
2905 | { | |
2906 | pf->raw_components.append("."); | |
2907 | pf->raw_components.append(member); | |
5f36109e | 2908 | |
a80f28d8 JS |
2909 | e2 = new target_symbol(*e); |
2910 | e2->components.push_back (target_symbol::component(e->tok, member)); | |
2911 | } | |
2912 | else if (childtag == DW_TAG_union_type) | |
2913 | pf->raw_components.append("<union>"); | |
2914 | else if (childtag == DW_TAG_structure_type) | |
2915 | pf->raw_components.append("<class>"); | |
2916 | else if (childtag == DW_TAG_class_type) | |
2917 | pf->raw_components.append("<struct>"); | |
2918 | pf->raw_components.append("="); | |
600551ca JS |
2919 | |
2920 | if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset)) | |
2921 | recurse_bitfield (&childtype, e2, pf); | |
2922 | else | |
2923 | recurse (&childtype, e2, pf); | |
5f36109e | 2924 | } |
a80f28d8 JS |
2925 | while (dwarf_siblingof (&child, &child) == 0); |
2926 | } | |
5f36109e JS |
2927 | } |
2928 | ||
2929 | ||
bbee5bb8 JS |
2930 | bool |
2931 | dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e, | |
2932 | print_format* pf) | |
2933 | { | |
2934 | Dwarf_Die type; | |
2935 | dw.resolve_unqualified_inner_typedie (start_type, &type, e); | |
6561d8d1 JS |
2936 | |
2937 | Dwarf_Attribute attr; | |
2938 | Dwarf_Word encoding = (Dwarf_Word) -1; | |
2939 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr), | |
2940 | &encoding); | |
2941 | switch (encoding) | |
bbee5bb8 | 2942 | { |
6561d8d1 JS |
2943 | case DW_ATE_UTF: |
2944 | case DW_ATE_signed_char: | |
2945 | case DW_ATE_unsigned_char: | |
2946 | break; | |
2947 | default: | |
2948 | return false; | |
2949 | } | |
2950 | ||
2951 | string function = userspace_p ? "user_string2" : "kernel_string2"; | |
2952 | Dwarf_Word size = (Dwarf_Word) -1; | |
2953 | dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size); | |
2954 | switch (size) | |
2955 | { | |
2956 | case 1: | |
2957 | break; | |
2958 | case 2: | |
2959 | function += "_utf16"; | |
2960 | break; | |
2961 | case 4: | |
2962 | function += "_utf32"; | |
2963 | break; | |
2964 | default: | |
2965 | return false; | |
2966 | } | |
2967 | ||
2968 | if (push_deref (pf, "\"%s\"", e)) | |
2969 | { | |
2970 | // steal the last arg for a string access | |
2971 | assert (!pf->args.empty()); | |
2972 | functioncall* fcall = new functioncall; | |
2973 | fcall->tok = e->tok; | |
2974 | fcall->function = function; | |
2975 | fcall->args.push_back (pf->args.back()); | |
2976 | expression *err_msg = new literal_string ("<unknown>"); | |
2977 | err_msg->tok = e->tok; | |
2978 | fcall->args.push_back (err_msg); | |
2979 | pf->args.back() = fcall; | |
bbee5bb8 | 2980 | } |
6561d8d1 | 2981 | return true; |
bbee5bb8 JS |
2982 | } |
2983 | ||
a5ce5211 MW |
2984 | // PR10601: adapt to kernel-vs-userspace loc2c-runtime |
2985 | static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n") | |
f1e8e7e0 MW |
2986 | + "#define fetch_register k_fetch_register\n" |
2987 | + "#define store_register k_store_register\n" | |
2988 | + "#define deref kderef\n" | |
2989 | + "#define store_deref store_kderef\n"; | |
a5ce5211 MW |
2990 | |
2991 | static const string EMBEDDED_FETCH_DEREF_USER = string("\n") | |
f1e8e7e0 MW |
2992 | + "#define fetch_register u_fetch_register\n" |
2993 | + "#define store_register u_store_register\n" | |
2994 | + "#define deref uderef\n" | |
2995 | + "#define store_deref store_uderef\n"; | |
a5ce5211 MW |
2996 | |
2997 | #define EMBEDDED_FETCH_DEREF(U) \ | |
f1e8e7e0 | 2998 | (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL) |
a5ce5211 MW |
2999 | |
3000 | static const string EMBEDDED_FETCH_DEREF_DONE = string("\n") | |
f1e8e7e0 MW |
3001 | + "#undef fetch_register\n" |
3002 | + "#undef store_register\n" | |
3003 | + "#undef deref\n" | |
3004 | + "#undef store_deref\n"; | |
bbee5bb8 | 3005 | |
5f36109e JS |
3006 | expression* |
3007 | dwarf_pretty_print::deref (target_symbol* e) | |
3008 | { | |
3009 | static unsigned tick = 0; | |
3010 | ||
d19a9a82 JS |
3011 | if (!deref_p) |
3012 | { | |
3013 | assert (pointer && e->components.empty()); | |
3014 | return pointer; | |
3015 | } | |
3016 | ||
5f36109e JS |
3017 | // Synthesize a function to dereference the dwarf fields, |
3018 | // with a pointer parameter that is the base tracepoint variable | |
3019 | functiondecl *fdecl = new functiondecl; | |
3020 | fdecl->synthetic = true; | |
3021 | fdecl->tok = e->tok; | |
3022 | embeddedcode *ec = new embeddedcode; | |
3023 | ec->tok = e->tok; | |
3024 | ||
3025 | fdecl->name = "_dwarf_pretty_print_deref_" + lex_cast(tick++); | |
3026 | fdecl->body = ec; | |
3027 | ||
3028 | // Synthesize a functioncall. | |
3029 | functioncall* fcall = new functioncall; | |
3030 | fcall->tok = e->tok; | |
3031 | fcall->function = fdecl->name; | |
5f36109e | 3032 | |
a5ce5211 | 3033 | ec->code += EMBEDDED_FETCH_DEREF(userspace_p); |
5f36109e JS |
3034 | |
3035 | if (pointer) | |
3036 | { | |
3037 | ec->code += dw.literal_stmt_for_pointer (&pointer_type, e, | |
3038 | false, fdecl->type); | |
3039 | ||
3040 | vardecl *v = new vardecl; | |
3041 | v->type = pe_long; | |
3042 | v->name = "pointer"; | |
3043 | v->tok = e->tok; | |
3044 | fdecl->formal_args.push_back(v); | |
3045 | fcall->args.push_back(pointer); | |
3046 | } | |
3047 | else if (!local.empty()) | |
3048 | ec->code += dw.literal_stmt_for_local (scopes, pc, local, e, | |
3049 | false, fdecl->type); | |
3050 | else | |
3051 | ec->code += dw.literal_stmt_for_return (&scopes[0], pc, e, | |
3052 | false, fdecl->type); | |
3053 | ||
3054 | // Any non-literal indexes need to be passed in too. | |
3055 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3056 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3057 | { | |
3058 | vardecl *v = new vardecl; | |
3059 | v->type = pe_long; | |
3060 | v->name = "index" + lex_cast(i); | |
3061 | v->tok = e->tok; | |
3062 | fdecl->formal_args.push_back(v); | |
3063 | fcall->args.push_back(e->components[i].expr_index); | |
3064 | } | |
3065 | ||
3066 | ec->code += "/* pure */"; | |
3067 | ec->code += "/* unprivileged */"; | |
3068 | ||
a5ce5211 | 3069 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
5f36109e | 3070 | |
f8809d54 | 3071 | fdecl->join (dw.sess); |
5f36109e JS |
3072 | return fcall; |
3073 | } | |
3074 | ||
3075 | ||
c55ea10d JS |
3076 | bool |
3077 | dwarf_pretty_print::push_deref (print_format* pf, const string& fmt, | |
3078 | target_symbol* e) | |
3079 | { | |
3080 | expression* e2 = NULL; | |
3081 | try | |
3082 | { | |
3083 | e2 = deref (e); | |
3084 | } | |
3085 | catch (const semantic_error&) | |
3086 | { | |
3087 | pf->raw_components.append ("?"); | |
3088 | return false; | |
3089 | } | |
3090 | pf->raw_components.append (fmt); | |
3091 | pf->args.push_back (e2); | |
3092 | return true; | |
3093 | } | |
3094 | ||
3095 | ||
e57b735a | 3096 | void |
a7999c82 | 3097 | dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e) |
e57b735a | 3098 | { |
a7999c82 JS |
3099 | // Get the full name of the target symbol. |
3100 | stringstream ts_name_stream; | |
3101 | e->print(ts_name_stream); | |
3102 | string ts_name = ts_name_stream.str(); | |
3103 | ||
3104 | // Check and make sure we haven't already seen this target | |
3105 | // variable in this return probe. If we have, just return our | |
3106 | // last replacement. | |
af234c40 | 3107 | map<string, expression *>::iterator i = return_ts_map.find(ts_name); |
a7999c82 | 3108 | if (i != return_ts_map.end()) |
85ecf79a | 3109 | { |
a7999c82 JS |
3110 | provide (i->second); |
3111 | return; | |
3112 | } | |
85ecf79a | 3113 | |
70208613 JS |
3114 | // Attempt the expansion directly first, so if there's a problem with the |
3115 | // variable we won't have a bogus entry probe lying around. Like in | |
3116 | // saveargs(), we pretend for a moment that we're not in a .return. | |
3117 | bool saved_has_return = q.has_return; | |
3118 | q.has_return = false; | |
3119 | expression *repl = e; | |
3120 | replace (repl); | |
3121 | q.has_return = saved_has_return; | |
3122 | target_symbol* n = dynamic_cast<target_symbol*>(repl); | |
3123 | if (n && n->saved_conversion_error) | |
3124 | { | |
3125 | provide (repl); | |
3126 | return; | |
3127 | } | |
3128 | ||
af234c40 JS |
3129 | expression *exp; |
3130 | if (!q.has_process && | |
3131 | strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0) | |
140be17a | 3132 | exp = gen_kretprobe_saved_return(repl); |
af234c40 | 3133 | else |
cc9001af | 3134 | exp = gen_mapped_saved_return(repl, e->sym_name()); |
af234c40 JS |
3135 | |
3136 | // Provide the variable to our parent so it can be used as a | |
3137 | // substitute for the target symbol. | |
3138 | provide (exp); | |
3139 | ||
3140 | // Remember this replacement since we might be able to reuse | |
3141 | // it later if the same return probe references this target | |
3142 | // symbol again. | |
3143 | return_ts_map[ts_name] = exp; | |
3144 | } | |
3145 | ||
4a2970a3 | 3146 | static expression* |
23dc94f6 DS |
3147 | gen_mapped_saved_return(systemtap_session &sess, expression* e, |
3148 | const string& name, | |
3149 | block *& add_block, bool& add_block_tid, | |
3150 | block *& add_call_probe, bool& add_call_probe_tid) | |
af234c40 | 3151 | { |
23dc94f6 DS |
3152 | static unsigned tick = 0; |
3153 | ||
a7999c82 JS |
3154 | // We've got to do several things here to handle target |
3155 | // variables in return probes. | |
85ecf79a | 3156 | |
a7999c82 JS |
3157 | // (1) Synthesize two global arrays. One is the cache of the |
3158 | // target variable and the other contains a thread specific | |
3159 | // nesting level counter. The arrays will look like | |
3160 | // this: | |
3161 | // | |
23dc94f6 DS |
3162 | // _entry_tvar_{name}_{num} |
3163 | // _entry_tvar_{name}_{num}_ctr | |
a7999c82 | 3164 | |
23dc94f6 | 3165 | string aname = (string("_entry_tvar_") |
cc9001af | 3166 | + name |
aca66a36 | 3167 | + "_" + lex_cast(tick++)); |
a7999c82 JS |
3168 | vardecl* vd = new vardecl; |
3169 | vd->name = aname; | |
3170 | vd->tok = e->tok; | |
23dc94f6 | 3171 | sess.globals.push_back (vd); |
a7999c82 JS |
3172 | |
3173 | string ctrname = aname + "_ctr"; | |
3174 | vd = new vardecl; | |
3175 | vd->name = ctrname; | |
3176 | vd->tok = e->tok; | |
23dc94f6 | 3177 | sess.globals.push_back (vd); |
a7999c82 JS |
3178 | |
3179 | // (2) Create a new code block we're going to insert at the | |
3180 | // beginning of this probe to get the cached value into a | |
3181 | // temporary variable. We'll replace the target variable | |
3182 | // reference with the temporary variable reference. The code | |
3183 | // will look like this: | |
3184 | // | |
23dc94f6 DS |
3185 | // _entry_tvar_tid = tid() |
3186 | // _entry_tvar_{name}_{num}_tmp | |
3187 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3188 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
3189 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3190 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
3191 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) | |
3192 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3193 | |
3194 | // (2a) Synthesize the tid temporary expression, which will look | |
3195 | // like this: | |
3196 | // | |
23dc94f6 | 3197 | // _entry_tvar_tid = tid() |
a7999c82 | 3198 | symbol* tidsym = new symbol; |
23dc94f6 | 3199 | tidsym->name = string("_entry_tvar_tid"); |
a7999c82 | 3200 | tidsym->tok = e->tok; |
85ecf79a | 3201 | |
a7999c82 JS |
3202 | if (add_block == NULL) |
3203 | { | |
3204 | add_block = new block; | |
3205 | add_block->tok = e->tok; | |
8cc799a5 | 3206 | } |
8c819921 | 3207 | |
8cc799a5 JS |
3208 | if (!add_block_tid) |
3209 | { | |
a7999c82 JS |
3210 | // Synthesize a functioncall to grab the thread id. |
3211 | functioncall* fc = new functioncall; | |
3212 | fc->tok = e->tok; | |
3213 | fc->function = string("tid"); | |
8c819921 | 3214 | |
23dc94f6 | 3215 | // Assign the tid to '_entry_tvar_tid'. |
8c819921 DS |
3216 | assignment* a = new assignment; |
3217 | a->tok = e->tok; | |
3218 | a->op = "="; | |
a7999c82 JS |
3219 | a->left = tidsym; |
3220 | a->right = fc; | |
8c819921 DS |
3221 | |
3222 | expr_statement* es = new expr_statement; | |
3223 | es->tok = e->tok; | |
3224 | es->value = a; | |
8c819921 | 3225 | add_block->statements.push_back (es); |
8cc799a5 | 3226 | add_block_tid = true; |
a7999c82 | 3227 | } |
8c819921 | 3228 | |
a7999c82 JS |
3229 | // (2b) Synthesize an array reference and assign it to a |
3230 | // temporary variable (that we'll use as replacement for the | |
3231 | // target variable reference). It will look like this: | |
3232 | // | |
23dc94f6 DS |
3233 | // _entry_tvar_{name}_{num}_tmp |
3234 | // = _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3235 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3236 | |
3237 | arrayindex* ai_tvar_base = new arrayindex; | |
3238 | ai_tvar_base->tok = e->tok; | |
3239 | ||
3240 | symbol* sym = new symbol; | |
3241 | sym->name = aname; | |
3242 | sym->tok = e->tok; | |
3243 | ai_tvar_base->base = sym; | |
3244 | ||
3245 | ai_tvar_base->indexes.push_back(tidsym); | |
3246 | ||
3247 | // We need to create a copy of the array index in its current | |
3248 | // state so we can have 2 variants of it (the original and one | |
3249 | // that post-decrements the second index). | |
3250 | arrayindex* ai_tvar = new arrayindex; | |
3251 | arrayindex* ai_tvar_postdec = new arrayindex; | |
3252 | *ai_tvar = *ai_tvar_base; | |
3253 | *ai_tvar_postdec = *ai_tvar_base; | |
3254 | ||
3255 | // Synthesize the | |
23dc94f6 | 3256 | // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the |
a7999c82 JS |
3257 | // second index into the array. |
3258 | arrayindex* ai_ctr = new arrayindex; | |
3259 | ai_ctr->tok = e->tok; | |
3260 | ||
3261 | sym = new symbol; | |
3262 | sym->name = ctrname; | |
3263 | sym->tok = e->tok; | |
3264 | ai_ctr->base = sym; | |
3265 | ai_ctr->indexes.push_back(tidsym); | |
3266 | ai_tvar->indexes.push_back(ai_ctr); | |
3267 | ||
3268 | symbol* tmpsym = new symbol; | |
3269 | tmpsym->name = aname + "_tmp"; | |
3270 | tmpsym->tok = e->tok; | |
3271 | ||
3272 | assignment* a = new assignment; | |
3273 | a->tok = e->tok; | |
3274 | a->op = "="; | |
3275 | a->left = tmpsym; | |
3276 | a->right = ai_tvar; | |
3277 | ||
3278 | expr_statement* es = new expr_statement; | |
3279 | es->tok = e->tok; | |
3280 | es->value = a; | |
3281 | ||
3282 | add_block->statements.push_back (es); | |
3283 | ||
3284 | // (2c) Add a post-decrement to the second array index and | |
3285 | // delete the array value. It will look like this: | |
3286 | // | |
23dc94f6 DS |
3287 | // delete _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3288 | // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--] | |
a7999c82 JS |
3289 | |
3290 | post_crement* pc = new post_crement; | |
3291 | pc->tok = e->tok; | |
3292 | pc->op = "--"; | |
3293 | pc->operand = ai_ctr; | |
3294 | ai_tvar_postdec->indexes.push_back(pc); | |
3295 | ||
3296 | delete_statement* ds = new delete_statement; | |
3297 | ds->tok = e->tok; | |
3298 | ds->value = ai_tvar_postdec; | |
3299 | ||
3300 | add_block->statements.push_back (ds); | |
3301 | ||
3302 | // (2d) Delete the counter value if it is 0. It will look like | |
3303 | // this: | |
23dc94f6 DS |
3304 | // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]) |
3305 | // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid] | |
a7999c82 JS |
3306 | |
3307 | ds = new delete_statement; | |
3308 | ds->tok = e->tok; | |
3309 | ds->value = ai_ctr; | |
3310 | ||
3311 | unary_expression *ue = new unary_expression; | |
3312 | ue->tok = e->tok; | |
3313 | ue->op = "!"; | |
3314 | ue->operand = ai_ctr; | |
3315 | ||
3316 | if_statement *ifs = new if_statement; | |
3317 | ifs->tok = e->tok; | |
3318 | ifs->condition = ue; | |
3319 | ifs->thenblock = ds; | |
3320 | ifs->elseblock = NULL; | |
3321 | ||
3322 | add_block->statements.push_back (ifs); | |
3323 | ||
3324 | // (3) We need an entry probe that saves the value for us in the | |
3325 | // global array we created. Create the entry probe, which will | |
3326 | // look like this: | |
3327 | // | |
2260f4e3 | 3328 | // probe kernel.function("{function}").call { |
23dc94f6 DS |
3329 | // _entry_tvar_tid = tid() |
3330 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, | |
3331 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3332 | // = ${param} |
3333 | // } | |
3334 | ||
2260f4e3 | 3335 | if (add_call_probe == NULL) |
a7999c82 | 3336 | { |
2260f4e3 FCE |
3337 | add_call_probe = new block; |
3338 | add_call_probe->tok = e->tok; | |
8cc799a5 | 3339 | } |
4baf0e53 | 3340 | |
8cc799a5 JS |
3341 | if (!add_call_probe_tid) |
3342 | { | |
a7999c82 JS |
3343 | // Synthesize a functioncall to grab the thread id. |
3344 | functioncall* fc = new functioncall; | |
3345 | fc->tok = e->tok; | |
3346 | fc->function = string("tid"); | |
4baf0e53 | 3347 | |
23dc94f6 | 3348 | // Assign the tid to '_entry_tvar_tid'. |
a7999c82 | 3349 | assignment* a = new assignment; |
8fc05e57 DS |
3350 | a->tok = e->tok; |
3351 | a->op = "="; | |
a7999c82 JS |
3352 | a->left = tidsym; |
3353 | a->right = fc; | |
8fc05e57 | 3354 | |
a7999c82 | 3355 | expr_statement* es = new expr_statement; |
8fc05e57 DS |
3356 | es->tok = e->tok; |
3357 | es->value = a; | |
2260f4e3 | 3358 | add_call_probe = new block(add_call_probe, es); |
8cc799a5 | 3359 | add_call_probe_tid = true; |
85ecf79a | 3360 | } |
cf2a1f85 | 3361 | |
a7999c82 | 3362 | // Save the value, like this: |
23dc94f6 DS |
3363 | // _entry_tvar_{name}_{num}[_entry_tvar_tid, |
3364 | // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]] | |
a7999c82 JS |
3365 | // = ${param} |
3366 | arrayindex* ai_tvar_preinc = new arrayindex; | |
3367 | *ai_tvar_preinc = *ai_tvar_base; | |
3368 | ||
3369 | pre_crement* preinc = new pre_crement; | |
3370 | preinc->tok = e->tok; | |
3371 | preinc->op = "++"; | |
3372 | preinc->operand = ai_ctr; | |
3373 | ai_tvar_preinc->indexes.push_back(preinc); | |
3374 | ||
3375 | a = new assignment; | |
3376 | a->tok = e->tok; | |
3377 | a->op = "="; | |
3378 | a->left = ai_tvar_preinc; | |
3379 | a->right = e; | |
3380 | ||
3381 | es = new expr_statement; | |
3382 | es->tok = e->tok; | |
3383 | es->value = a; | |
3384 | ||
2260f4e3 | 3385 | add_call_probe = new block(add_call_probe, es); |
a7999c82 | 3386 | |
23dc94f6 | 3387 | // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to |
a7999c82 JS |
3388 | // our parent so it can be used as a substitute for the target |
3389 | // symbol. | |
3f803f9e | 3390 | delete ai_tvar_base; |
af234c40 JS |
3391 | return tmpsym; |
3392 | } | |
a7999c82 | 3393 | |
af234c40 | 3394 | |
23dc94f6 DS |
3395 | expression* |
3396 | dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e, | |
3397 | const string& name) | |
3398 | { | |
3399 | return ::gen_mapped_saved_return(q.sess, e, name, add_block, | |
3400 | add_block_tid, add_call_probe, | |
3401 | add_call_probe_tid); | |
3402 | } | |
3403 | ||
3404 | ||
af234c40 | 3405 | expression* |
140be17a | 3406 | dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e) |
af234c40 JS |
3407 | { |
3408 | // The code for this is simple. | |
3409 | // | |
3410 | // .call: | |
3411 | // _set_kretprobe_long(index, $value) | |
3412 | // | |
3413 | // .return: | |
3414 | // _get_kretprobe_long(index) | |
3415 | // | |
3416 | // (or s/long/string/ for things like $$parms) | |
3417 | ||
3418 | unsigned index; | |
3419 | string setfn, getfn; | |
3420 | ||
140be17a JS |
3421 | // We need the caller to predetermine the type of the expression! |
3422 | switch (e->type) | |
af234c40 | 3423 | { |
140be17a | 3424 | case pe_string: |
af234c40 JS |
3425 | index = saved_strings++; |
3426 | setfn = "_set_kretprobe_string"; | |
3427 | getfn = "_get_kretprobe_string"; | |
140be17a JS |
3428 | break; |
3429 | case pe_long: | |
af234c40 JS |
3430 | index = saved_longs++; |
3431 | setfn = "_set_kretprobe_long"; | |
3432 | getfn = "_get_kretprobe_long"; | |
140be17a JS |
3433 | break; |
3434 | default: | |
b530b5b3 | 3435 | throw semantic_error(_("unknown type to save in kretprobe"), e->tok); |
af234c40 JS |
3436 | } |
3437 | ||
3438 | // Create the entry code | |
3439 | // _set_kretprobe_{long|string}(index, $value) | |
3440 | ||
3441 | if (add_call_probe == NULL) | |
3442 | { | |
3443 | add_call_probe = new block; | |
3444 | add_call_probe->tok = e->tok; | |
3445 | } | |
3446 | ||
3447 | functioncall* set_fc = new functioncall; | |
3448 | set_fc->tok = e->tok; | |
3449 | set_fc->function = setfn; | |
3450 | set_fc->args.push_back(new literal_number(index)); | |
3451 | set_fc->args.back()->tok = e->tok; | |
3452 | set_fc->args.push_back(e); | |
3453 | ||
3454 | expr_statement* set_es = new expr_statement; | |
3455 | set_es->tok = e->tok; | |
3456 | set_es->value = set_fc; | |
3457 | ||
3458 | add_call_probe->statements.push_back(set_es); | |
3459 | ||
3460 | // Create the return code | |
3461 | // _get_kretprobe_{long|string}(index) | |
3462 | ||
3463 | functioncall* get_fc = new functioncall; | |
3464 | get_fc->tok = e->tok; | |
3465 | get_fc->function = getfn; | |
3466 | get_fc->args.push_back(new literal_number(index)); | |
3467 | get_fc->args.back()->tok = e->tok; | |
3468 | ||
3469 | return get_fc; | |
a7999c82 | 3470 | } |
a43ba433 | 3471 | |
2cb3fe26 | 3472 | |
a7999c82 JS |
3473 | void |
3474 | dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
3475 | { | |
9aa8ffce | 3476 | if (null_die(scope_die)) |
a7999c82 | 3477 | return; |
2cb3fe26 | 3478 | |
5f36109e JS |
3479 | target_symbol *tsym = new target_symbol(*e); |
3480 | ||
fde50242 JS |
3481 | bool pretty = (!e->components.empty() && |
3482 | e->components[0].type == target_symbol::comp_pretty_print); | |
3483 | string format = pretty ? "=%s" : "=%#x"; | |
a43ba433 | 3484 | |
a7999c82 JS |
3485 | // Convert $$parms to sprintf of a list of parms and active local vars |
3486 | // which we recursively evaluate | |
a43ba433 | 3487 | |
a7999c82 JS |
3488 | // NB: we synthesize a new token here rather than reusing |
3489 | // e->tok, because print_format::print likes to use | |
3490 | // its tok->content. | |
5f36109e | 3491 | token* pf_tok = new token(*e->tok); |
a7999c82 | 3492 | pf_tok->type = tok_identifier; |
b393f6f2 | 3493 | pf_tok->content = "sprintf"; |
2cb3fe26 | 3494 | |
d5e178c1 | 3495 | print_format* pf = print_format::create(pf_tok); |
a7999c82 | 3496 | |
277c21bc | 3497 | if (q.has_return && (e->name == "$$return")) |
a7999c82 | 3498 | { |
277c21bc | 3499 | tsym->name = "$return"; |
a7999c82 JS |
3500 | |
3501 | // Ignore any variable that isn't accessible. | |
3502 | tsym->saved_conversion_error = 0; | |
3503 | expression *texp = tsym; | |
8b095b45 | 3504 | replace (texp); // NB: throws nothing ... |
a7999c82 | 3505 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
a43ba433 | 3506 | { |
2cb3fe26 | 3507 | |
a43ba433 FCE |
3508 | } |
3509 | else | |
3510 | { | |
a7999c82 | 3511 | pf->raw_components += "return"; |
5f36109e | 3512 | pf->raw_components += format; |
a7999c82 JS |
3513 | pf->args.push_back(texp); |
3514 | } | |
3515 | } | |
3516 | else | |
3517 | { | |
3518 | // non-.return probe: support $$parms, $$vars, $$locals | |
345bbb3d | 3519 | bool first = true; |
a7999c82 | 3520 | Dwarf_Die result; |
d48bc7eb JS |
3521 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
3522 | for (unsigned i = 0; i < scopes.size(); ++i) | |
3523 | { | |
3524 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
3525 | break; // we don't want file-level variables | |
3526 | if (dwarf_child (&scopes[i], &result) == 0) | |
3527 | do | |
00cf3709 | 3528 | { |
d48bc7eb JS |
3529 | switch (dwarf_tag (&result)) |
3530 | { | |
3531 | case DW_TAG_variable: | |
3532 | if (e->name == "$$parms") | |
3533 | continue; | |
3534 | break; | |
3535 | case DW_TAG_formal_parameter: | |
3536 | if (e->name == "$$locals") | |
3537 | continue; | |
3538 | break; | |
3539 | ||
3540 | default: | |
3541 | continue; | |
3542 | } | |
41c262f3 | 3543 | |
d48bc7eb JS |
3544 | const char *diename = dwarf_diename (&result); |
3545 | if (! diename) continue; | |
f76427a2 | 3546 | |
d48bc7eb JS |
3547 | if (! first) |
3548 | pf->raw_components += " "; | |
3549 | pf->raw_components += diename; | |
fde50242 JS |
3550 | first = false; |
3551 | ||
3552 | // Write a placeholder for ugly aggregates | |
3553 | Dwarf_Die type; | |
3554 | if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type)) | |
3555 | { | |
3556 | q.dw.resolve_unqualified_inner_typedie(&type, &type, e); | |
3557 | switch (dwarf_tag(&type)) | |
3558 | { | |
3559 | case DW_TAG_union_type: | |
3560 | case DW_TAG_structure_type: | |
3561 | case DW_TAG_class_type: | |
3562 | pf->raw_components += "={...}"; | |
3563 | continue; | |
3564 | ||
3565 | case DW_TAG_array_type: | |
3566 | pf->raw_components += "=[...]"; | |
3567 | continue; | |
3568 | } | |
3569 | } | |
345bbb3d | 3570 | |
d48bc7eb JS |
3571 | tsym->name = "$"; |
3572 | tsym->name += diename; | |
41c262f3 | 3573 | |
d48bc7eb JS |
3574 | // Ignore any variable that isn't accessible. |
3575 | tsym->saved_conversion_error = 0; | |
3576 | expression *texp = tsym; | |
3577 | replace (texp); // NB: throws nothing ... | |
3578 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. | |
a43ba433 | 3579 | { |
d48bc7eb JS |
3580 | if (q.sess.verbose>2) |
3581 | { | |
e26c2f83 | 3582 | for (const semantic_error *c = tsym->saved_conversion_error; |
d48bc7eb JS |
3583 | c != 0; |
3584 | c = c->chain) { | |
b530b5b3 | 3585 | clog << _("variable location problem: ") << c->what() << endl; |
d48bc7eb JS |
3586 | } |
3587 | } | |
3588 | ||
3589 | pf->raw_components += "=?"; | |
a43ba433 | 3590 | } |
d48bc7eb JS |
3591 | else |
3592 | { | |
3593 | pf->raw_components += format; | |
3594 | pf->args.push_back(texp); | |
3595 | } | |
a7999c82 | 3596 | } |
d48bc7eb JS |
3597 | while (dwarf_siblingof (&result, &result) == 0); |
3598 | } | |
a7999c82 | 3599 | } |
2cb3fe26 | 3600 | |
a7999c82 | 3601 | pf->components = print_format::string_to_components(pf->raw_components); |
140be17a | 3602 | pf->type = pe_string; |
a7999c82 JS |
3603 | provide (pf); |
3604 | } | |
3605 | ||
2cb3fe26 | 3606 | |
a7999c82 JS |
3607 | void |
3608 | dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e) | |
3609 | { | |
cc9001af MW |
3610 | assert(e->name.size() > 0 |
3611 | && ((e->name[0] == '$' && e->target_name == "") | |
3612 | || (e->name == "@var" && e->target_name != ""))); | |
a7999c82 | 3613 | visited = true; |
30263a73 FCE |
3614 | bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e)); |
3615 | // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars | |
a7999c82 | 3616 | |
70208613 | 3617 | try |
a7999c82 | 3618 | { |
c69a87e0 FCE |
3619 | bool lvalue = is_active_lvalue(e); |
3620 | if (lvalue && !q.sess.guru_mode) | |
b3741c9d | 3621 | throw semantic_error(_("write to target variable not permitted; need stap -g"), e->tok); |
2cb3fe26 | 3622 | |
100a540e | 3623 | // XXX: process $context vars should be writable |
70208613 | 3624 | |
c69a87e0 FCE |
3625 | // See if we need to generate a new probe to save/access function |
3626 | // parameters from a return probe. PR 1382. | |
3627 | if (q.has_return | |
3628 | && !defined_being_checked | |
277c21bc JS |
3629 | && e->name != "$return" // not the special return-value variable handled below |
3630 | && e->name != "$$return") // nor the other special variable handled below | |
c69a87e0 FCE |
3631 | { |
3632 | if (lvalue) | |
b530b5b3 | 3633 | throw semantic_error(_("write to target variable not permitted in .return probes"), e->tok); |
c69a87e0 FCE |
3634 | visit_target_symbol_saved_return(e); |
3635 | return; | |
3636 | } | |
e57b735a | 3637 | |
277c21bc JS |
3638 | if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals" |
3639 | || (q.has_return && (e->name == "$$return"))) | |
c69a87e0 FCE |
3640 | { |
3641 | if (lvalue) | |
b530b5b3 | 3642 | throw semantic_error(_("cannot write to context variable"), e->tok); |
70208613 | 3643 | |
c69a87e0 | 3644 | if (e->addressof) |
b530b5b3 | 3645 | throw semantic_error(_("cannot take address of context variable"), e->tok); |
70208613 | 3646 | |
5f36109e JS |
3647 | e->assert_no_components("dwarf", true); |
3648 | ||
c69a87e0 FCE |
3649 | visit_target_symbol_context(e); |
3650 | return; | |
3651 | } | |
70208613 | 3652 | |
5f36109e JS |
3653 | if (!e->components.empty() && |
3654 | e->components.back().type == target_symbol::comp_pretty_print) | |
3655 | { | |
3656 | if (lvalue) | |
b530b5b3 | 3657 | throw semantic_error(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 3658 | |
277c21bc | 3659 | if (q.has_return && (e->name == "$return")) |
5f36109e JS |
3660 | { |
3661 | dwarf_pretty_print dpp (q.dw, scope_die, addr, | |
3662 | q.has_process, *e); | |
3663 | dpp.expand()->visit(this); | |
3664 | } | |
3665 | else | |
3666 | { | |
3667 | dwarf_pretty_print dpp (q.dw, getscopes(e), addr, | |
cc9001af | 3668 | e->sym_name(), |
5f36109e JS |
3669 | q.has_process, *e); |
3670 | dpp.expand()->visit(this); | |
3671 | } | |
3672 | return; | |
3673 | } | |
3674 | ||
c69a87e0 FCE |
3675 | // Synthesize a function. |
3676 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 3677 | fdecl->synthetic = true; |
c69a87e0 FCE |
3678 | fdecl->tok = e->tok; |
3679 | embeddedcode *ec = new embeddedcode; | |
3680 | ec->tok = e->tok; | |
70208613 | 3681 | |
c69a87e0 | 3682 | string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get") |
cc9001af | 3683 | + "_" + e->sym_name() |
c69a87e0 | 3684 | + "_" + lex_cast(tick++)); |
70208613 | 3685 | |
a5ce5211 | 3686 | ec->code += EMBEDDED_FETCH_DEREF(q.has_process); |
70208613 | 3687 | |
277c21bc | 3688 | if (q.has_return && (e->name == "$return")) |
e19fda4e | 3689 | { |
b5a0dd41 | 3690 | ec->code += q.dw.literal_stmt_for_return (scope_die, |
e19fda4e | 3691 | addr, |
b4c34c26 | 3692 | e, |
e19fda4e DS |
3693 | lvalue, |
3694 | fdecl->type); | |
3695 | } | |
3696 | else | |
3697 | { | |
b5a0dd41 | 3698 | ec->code += q.dw.literal_stmt_for_local (getscopes(e), |
e19fda4e | 3699 | addr, |
cc9001af | 3700 | e->sym_name(), |
b4c34c26 | 3701 | e, |
e19fda4e DS |
3702 | lvalue, |
3703 | fdecl->type); | |
3704 | } | |
3705 | ||
1b07c728 FCE |
3706 | if (! lvalue) |
3707 | ec->code += "/* pure */"; | |
64211010 DB |
3708 | |
3709 | ec->code += "/* unprivileged */"; | |
a5ce5211 | 3710 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
c69a87e0 FCE |
3711 | |
3712 | fdecl->name = fname; | |
3713 | fdecl->body = ec; | |
70208613 | 3714 | |
c69a87e0 FCE |
3715 | // Any non-literal indexes need to be passed in too. |
3716 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3717 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3718 | { | |
3719 | vardecl *v = new vardecl; | |
3720 | v->type = pe_long; | |
3721 | v->name = "index" + lex_cast(i); | |
3722 | v->tok = e->tok; | |
3723 | fdecl->formal_args.push_back(v); | |
3724 | } | |
70208613 | 3725 | |
c69a87e0 FCE |
3726 | if (lvalue) |
3727 | { | |
3728 | // Modify the fdecl so it carries a single pe_long formal | |
3729 | // argument called "value". | |
70208613 | 3730 | |
c69a87e0 FCE |
3731 | // FIXME: For the time being we only support setting target |
3732 | // variables which have base types; these are 'pe_long' in | |
3733 | // stap's type vocabulary. Strings and pointers might be | |
3734 | // reasonable, some day, but not today. | |
70208613 | 3735 | |
c69a87e0 FCE |
3736 | vardecl *v = new vardecl; |
3737 | v->type = pe_long; | |
3738 | v->name = "value"; | |
3739 | v->tok = e->tok; | |
3740 | fdecl->formal_args.push_back(v); | |
3741 | } | |
f8809d54 | 3742 | fdecl->join (q.sess); |
70208613 | 3743 | |
c69a87e0 FCE |
3744 | // Synthesize a functioncall. |
3745 | functioncall* n = new functioncall; | |
3746 | n->tok = e->tok; | |
3747 | n->function = fname; | |
140be17a | 3748 | n->type = fdecl->type; |
70208613 | 3749 | |
c69a87e0 FCE |
3750 | // Any non-literal indexes need to be passed in too. |
3751 | for (unsigned i = 0; i < e->components.size(); ++i) | |
3752 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
3753 | n->args.push_back(require(e->components[i].expr_index)); | |
70208613 | 3754 | |
c69a87e0 FCE |
3755 | if (lvalue) |
3756 | { | |
3757 | // Provide the functioncall to our parent, so that it can be | |
3758 | // used to substitute for the assignment node immediately above | |
3759 | // us. | |
3760 | assert(!target_symbol_setter_functioncalls.empty()); | |
3761 | *(target_symbol_setter_functioncalls.top()) = n; | |
3762 | } | |
70208613 | 3763 | |
c69a87e0 | 3764 | provide (n); |
66d284f4 FCE |
3765 | } |
3766 | catch (const semantic_error& er) | |
3767 | { | |
9fab2262 JS |
3768 | // We suppress this error message, and pass the unresolved |
3769 | // target_symbol to the next pass. We hope that this value ends | |
3770 | // up not being referenced after all, so it can be optimized out | |
3771 | // quietly. | |
1af1e62d | 3772 | e->chain (er); |
9fab2262 | 3773 | provide (e); |
66d284f4 | 3774 | } |
77de5e9e GH |
3775 | } |
3776 | ||
3777 | ||
c24447be JS |
3778 | void |
3779 | dwarf_var_expanding_visitor::visit_cast_op (cast_op *e) | |
3780 | { | |
3781 | // Fill in our current module context if needed | |
3782 | if (e->module.empty()) | |
3783 | e->module = q.dw.module_name; | |
3784 | ||
3785 | var_expanding_visitor::visit_cast_op(e); | |
3786 | } | |
3787 | ||
3788 | ||
8cc799a5 JS |
3789 | void |
3790 | dwarf_var_expanding_visitor::visit_entry_op (entry_op *e) | |
3791 | { | |
3792 | expression *repl = e; | |
3793 | if (q.has_return) | |
3794 | { | |
3795 | // expand the operand as if it weren't a return probe | |
3796 | q.has_return = false; | |
3797 | replace (e->operand); | |
3798 | q.has_return = true; | |
3799 | ||
3800 | // XXX it would be nice to use gen_kretprobe_saved_return when available, | |
3801 | // but it requires knowing the types already, which is problematic for | |
3802 | // arbitrary expressons. | |
cc9001af | 3803 | repl = gen_mapped_saved_return (e->operand, "entry"); |
8cc799a5 JS |
3804 | } |
3805 | provide (repl); | |
3806 | } | |
3807 | ||
bfa7e523 MW |
3808 | vector<Dwarf_Die>& |
3809 | dwarf_var_expanding_visitor::getcuscope(target_symbol *e) | |
3810 | { | |
54e9f062 MW |
3811 | Dwarf_Off cu_off = 0; |
3812 | const char *cu_name = NULL; | |
bfa7e523 MW |
3813 | |
3814 | string prefixed_srcfile = string("*/") + e->cu_name; | |
3815 | ||
3816 | Dwarf_Off off = 0; | |
3817 | size_t cuhl; | |
3818 | Dwarf_Off noff; | |
3819 | Dwarf_Off module_bias; | |
3820 | Dwarf *dw = dwfl_module_getdwarf(q.dw.module, &module_bias); | |
54e9f062 | 3821 | while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) |
bfa7e523 MW |
3822 | { |
3823 | Dwarf_Die die_mem; | |
3824 | Dwarf_Die *die; | |
3825 | die = dwarf_offdie (dw, off + cuhl, &die_mem); | |
54e9f062 | 3826 | |
dee830d9 MW |
3827 | /* We are not interested in partial units. */ |
3828 | if (dwarf_tag (die) == DW_TAG_compile_unit) | |
54e9f062 | 3829 | { |
dee830d9 MW |
3830 | const char *die_name = dwarf_diename (die); |
3831 | if (strcmp (die_name, e->cu_name.c_str()) == 0) // Perfect match. | |
3832 | { | |
3833 | cu_name = die_name; | |
3834 | cu_off = off + cuhl; | |
3835 | break; | |
3836 | } | |
3837 | ||
3838 | if (fnmatch(prefixed_srcfile.c_str(), die_name, 0) == 0) | |
3839 | if (cu_name == NULL || strlen (die_name) < strlen (cu_name)) | |
3840 | { | |
3841 | cu_name = die_name; | |
3842 | cu_off = off + cuhl; | |
3843 | } | |
54e9f062 | 3844 | } |
bfa7e523 MW |
3845 | off = noff; |
3846 | } | |
3847 | ||
54e9f062 | 3848 | if (cu_name == NULL) |
bfa7e523 MW |
3849 | throw semantic_error ("unable to find CU '" + e->cu_name + "'" |
3850 | + " while searching for '" + e->target_name + "'", | |
3851 | e->tok); | |
3852 | ||
3853 | vector<Dwarf_Die> *cu_scope = new vector<Dwarf_Die>; | |
54e9f062 MW |
3854 | Dwarf_Die cu_die; |
3855 | dwarf_offdie (dw, cu_off, &cu_die); | |
3856 | cu_scope->push_back(cu_die); | |
bfa7e523 MW |
3857 | return *cu_scope; |
3858 | } | |
8cc799a5 | 3859 | |
729455a7 JS |
3860 | vector<Dwarf_Die>& |
3861 | dwarf_var_expanding_visitor::getscopes(target_symbol *e) | |
3862 | { | |
bfa7e523 MW |
3863 | // "static globals" can only be found in the top-level CU. |
3864 | if (e->name == "@var" && e->cu_name != "") | |
3865 | return this->getcuscope(e); | |
3866 | ||
729455a7 JS |
3867 | if (scopes.empty()) |
3868 | { | |
f25a9197 CM |
3869 | if(scope_die != NULL) |
3870 | scopes = q.dw.getscopes(scope_die); | |
729455a7 | 3871 | if (scopes.empty()) |
b530b5b3 LB |
3872 | //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok); |
3873 | // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" )) | |
729455a7 JS |
3874 | throw semantic_error ("unable to find any scopes containing " |
3875 | + lex_cast_hex(addr) | |
3876 | + ((scope_die == NULL) ? "" | |
3877 | : (string (" in ") | |
3878 | + (dwarf_diename(scope_die) ?: "<unknown>") | |
3879 | + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") | |
3880 | + ")")) | |
3881 | + " while searching for local '" | |
cc9001af | 3882 | + e->sym_name() + "'", |
729455a7 JS |
3883 | e->tok); |
3884 | } | |
3885 | return scopes; | |
3886 | } | |
3887 | ||
3888 | ||
5f36109e JS |
3889 | struct dwarf_cast_expanding_visitor: public var_expanding_visitor |
3890 | { | |
3891 | systemtap_session& s; | |
3892 | dwarf_builder& db; | |
3893 | ||
3894 | dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db): | |
3895 | s(s), db(db) {} | |
3896 | void visit_cast_op (cast_op* e); | |
3897 | void filter_special_modules(string& module); | |
3898 | }; | |
3899 | ||
3900 | ||
c4ce66a1 JS |
3901 | struct dwarf_cast_query : public base_query |
3902 | { | |
946e1a48 | 3903 | cast_op& e; |
c4ce66a1 | 3904 | const bool lvalue; |
5f36109e JS |
3905 | const bool userspace_p; |
3906 | functioncall*& result; | |
c4ce66a1 | 3907 | |
5f36109e JS |
3908 | dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue, |
3909 | const bool userspace_p, functioncall*& result): | |
abb41d92 | 3910 | base_query(dw, module), e(e), lvalue(lvalue), |
5f36109e | 3911 | userspace_p(userspace_p), result(result) {} |
c4ce66a1 JS |
3912 | |
3913 | void handle_query_module(); | |
822a6a3d | 3914 | void query_library (const char *) {} |
576eaefe | 3915 | void query_plt (const char *entry, size_t addr) {} |
c4ce66a1 JS |
3916 | }; |
3917 | ||
3918 | ||
c4ce66a1 JS |
3919 | void |
3920 | dwarf_cast_query::handle_query_module() | |
3921 | { | |
5f36109e JS |
3922 | static unsigned tick = 0; |
3923 | ||
3924 | if (result) | |
c4ce66a1 JS |
3925 | return; |
3926 | ||
ea1e477a | 3927 | // look for the type in any CU |
a44a7cb5 JS |
3928 | Dwarf_Die* type_die = NULL; |
3929 | if (startswith(e.type_name, "class ")) | |
3930 | { | |
3931 | // normalize to match dwflpp::global_alias_caching_callback | |
3932 | string struct_name = "struct " + e.type_name.substr(6); | |
3933 | type_die = dw.declaration_resolve_other_cus(struct_name); | |
3934 | } | |
3935 | else | |
3936 | type_die = dw.declaration_resolve_other_cus(e.type_name); | |
3937 | ||
3938 | // NB: We now index the types as "struct name"/"union name"/etc. instead of | |
3939 | // just "name". But since we didn't require users to be explicit before, and | |
3940 | // actually sort of discouraged it, we must be flexible now. So if a lookup | |
3941 | // fails with a bare name, try augmenting it. | |
3942 | if (!type_die && | |
3943 | !startswith(e.type_name, "class ") && | |
3944 | !startswith(e.type_name, "struct ") && | |
3945 | !startswith(e.type_name, "union ") && | |
3946 | !startswith(e.type_name, "enum ")) | |
3947 | { | |
3948 | type_die = dw.declaration_resolve_other_cus("struct " + e.type_name); | |
3949 | if (!type_die) | |
3950 | type_die = dw.declaration_resolve_other_cus("union " + e.type_name); | |
3951 | if (!type_die) | |
3952 | type_die = dw.declaration_resolve_other_cus("enum " + e.type_name); | |
3953 | } | |
3954 | ||
ea1e477a JS |
3955 | if (!type_die) |
3956 | return; | |
c4ce66a1 | 3957 | |
5f36109e JS |
3958 | string code; |
3959 | exp_type type = pe_long; | |
3960 | ||
ea1e477a | 3961 | try |
c4ce66a1 | 3962 | { |
ea1e477a JS |
3963 | Dwarf_Die cu_mem; |
3964 | dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL)); | |
5f36109e JS |
3965 | |
3966 | if (!e.components.empty() && | |
3967 | e.components.back().type == target_symbol::comp_pretty_print) | |
3968 | { | |
3969 | if (lvalue) | |
b530b5b3 | 3970 | throw semantic_error(_("cannot write to pretty-printed variable"), e.tok); |
5f36109e | 3971 | |
d19a9a82 | 3972 | dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e); |
5f36109e JS |
3973 | result = dpp.expand(); |
3974 | return; | |
3975 | } | |
3976 | ||
3977 | code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type); | |
ea1e477a JS |
3978 | } |
3979 | catch (const semantic_error& er) | |
3980 | { | |
3981 | // NB: we can have multiple errors, since a @cast | |
1af1e62d JS |
3982 | // may be attempted using several different modules: |
3983 | // @cast(ptr, "type", "module1:module2:...") | |
3984 | e.chain (er); | |
c4ce66a1 | 3985 | } |
c4ce66a1 | 3986 | |
5f36109e JS |
3987 | if (code.empty()) |
3988 | return; | |
c4ce66a1 | 3989 | |
5f36109e | 3990 | string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get") |
cc9001af | 3991 | + "_" + e.sym_name() |
5f36109e | 3992 | + "_" + lex_cast(tick++)); |
c4ce66a1 | 3993 | |
5f36109e JS |
3994 | // Synthesize a function. |
3995 | functiondecl *fdecl = new functiondecl; | |
3996 | fdecl->synthetic = true; | |
3997 | fdecl->tok = e.tok; | |
3998 | fdecl->type = type; | |
3999 | fdecl->name = fname; | |
4000 | ||
4001 | embeddedcode *ec = new embeddedcode; | |
4002 | ec->tok = e.tok; | |
4003 | fdecl->body = ec; | |
4004 | ||
a5ce5211 | 4005 | ec->code += EMBEDDED_FETCH_DEREF(userspace_p); |
5f36109e JS |
4006 | ec->code += code; |
4007 | ||
4008 | // Give the fdecl an argument for the pointer we're trying to cast | |
4009 | vardecl *v1 = new vardecl; | |
4010 | v1->type = pe_long; | |
4011 | v1->name = "pointer"; | |
4012 | v1->tok = e.tok; | |
4013 | fdecl->formal_args.push_back(v1); | |
4014 | ||
4015 | // Any non-literal indexes need to be passed in too. | |
4016 | for (unsigned i = 0; i < e.components.size(); ++i) | |
4017 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
4018 | { | |
4019 | vardecl *v = new vardecl; | |
4020 | v->type = pe_long; | |
4021 | v->name = "index" + lex_cast(i); | |
4022 | v->tok = e.tok; | |
4023 | fdecl->formal_args.push_back(v); | |
4024 | } | |
4025 | ||
4026 | if (lvalue) | |
4027 | { | |
4028 | // Modify the fdecl so it carries a second pe_long formal | |
4029 | // argument called "value". | |
4030 | ||
4031 | // FIXME: For the time being we only support setting target | |
4032 | // variables which have base types; these are 'pe_long' in | |
4033 | // stap's type vocabulary. Strings and pointers might be | |
4034 | // reasonable, some day, but not today. | |
4035 | ||
4036 | vardecl *v2 = new vardecl; | |
4037 | v2->type = pe_long; | |
4038 | v2->name = "value"; | |
4039 | v2->tok = e.tok; | |
4040 | fdecl->formal_args.push_back(v2); | |
4041 | } | |
4042 | else | |
4043 | ec->code += "/* pure */"; | |
4044 | ||
4045 | ec->code += "/* unprivileged */"; | |
a5ce5211 | 4046 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
5f36109e | 4047 | |
f8809d54 | 4048 | fdecl->join (dw.sess); |
5f36109e JS |
4049 | |
4050 | // Synthesize a functioncall. | |
4051 | functioncall* n = new functioncall; | |
4052 | n->tok = e.tok; | |
4053 | n->function = fname; | |
5f36109e JS |
4054 | n->args.push_back(e.operand); |
4055 | ||
4056 | // Any non-literal indexes need to be passed in too. | |
4057 | for (unsigned i = 0; i < e.components.size(); ++i) | |
4058 | if (e.components[i].type == target_symbol::comp_expression_array_index) | |
4059 | n->args.push_back(e.components[i].expr_index); | |
4060 | ||
4061 | result = n; | |
4062 | } | |
c4ce66a1 JS |
4063 | |
4064 | ||
fb0274bc JS |
4065 | void dwarf_cast_expanding_visitor::filter_special_modules(string& module) |
4066 | { | |
d90053e7 | 4067 | // look for "<path/to/header>" or "kernel<path/to/header>" |
fb0274bc | 4068 | // for those cases, build a module including that header |
d90053e7 | 4069 | if (module[module.size() - 1] == '>' && |
60d98537 | 4070 | (module[0] == '<' || startswith(module, "kernel<"))) |
fb0274bc JS |
4071 | { |
4072 | string cached_module; | |
4073 | if (s.use_cache) | |
4074 | { | |
4075 | // see if the cached module exists | |
a2639cb7 | 4076 | cached_module = find_typequery_hash(s, module); |
d105f664 | 4077 | if (!cached_module.empty() && !s.poison_cache) |
fb0274bc JS |
4078 | { |
4079 | int fd = open(cached_module.c_str(), O_RDONLY); | |
4080 | if (fd != -1) | |
4081 | { | |
4082 | if (s.verbose > 2) | |
b530b5b3 LB |
4083 | //TRANSLATORS: Here we're using a cached module. |
4084 | clog << _("Pass 2: using cached ") << cached_module << endl; | |
fb0274bc JS |
4085 | module = cached_module; |
4086 | close(fd); | |
4087 | return; | |
4088 | } | |
4089 | } | |
4090 | } | |
4091 | ||
4092 | // no cached module, time to make it | |
d90053e7 | 4093 | if (make_typequery(s, module) == 0) |
fb0274bc | 4094 | { |
e16dc041 | 4095 | // try to save typequery in the cache |
fb0274bc | 4096 | if (s.use_cache) |
e16dc041 | 4097 | copy_file(module, cached_module, s.verbose > 2); |
fb0274bc JS |
4098 | } |
4099 | } | |
4100 | } | |
4101 | ||
4102 | ||
c4ce66a1 JS |
4103 | void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e) |
4104 | { | |
4105 | bool lvalue = is_active_lvalue(e); | |
4106 | if (lvalue && !s.guru_mode) | |
b3741c9d | 4107 | throw semantic_error(_("write to @cast context variable not permitted; need stap -g"), e->tok); |
c4ce66a1 JS |
4108 | |
4109 | if (e->module.empty()) | |
4110 | e->module = "kernel"; // "*" may also be reasonable to search all kernel modules | |
4111 | ||
5f36109e | 4112 | functioncall* result = NULL; |
8b31197b JS |
4113 | |
4114 | // split the module string by ':' for alternatives | |
4115 | vector<string> modules; | |
4116 | tokenize(e->module, modules, ":"); | |
b5a0dd41 | 4117 | bool userspace_p=false; // PR10601 |
5f36109e | 4118 | for (unsigned i = 0; !result && i < modules.size(); ++i) |
c4ce66a1 | 4119 | { |
8b31197b | 4120 | string& module = modules[i]; |
fb0274bc | 4121 | filter_special_modules(module); |
abb41d92 | 4122 | |
c4ce66a1 JS |
4123 | // NB: This uses '/' to distinguish between kernel modules and userspace, |
4124 | // which means that userspace modules won't get any PATH searching. | |
4125 | dwflpp* dw; | |
707bf35e JS |
4126 | try |
4127 | { | |
b5a0dd41 FCE |
4128 | userspace_p=is_user_module (module); |
4129 | if (! userspace_p) | |
707bf35e JS |
4130 | { |
4131 | // kernel or kernel module target | |
ae2552da | 4132 | dw = db.get_kern_dw(s, module); |
707bf35e JS |
4133 | } |
4134 | else | |
4135 | { | |
05fb3e0c | 4136 | module = find_executable (module, "", s.sysenv); // canonicalize it |
707bf35e JS |
4137 | dw = db.get_user_dw(s, module); |
4138 | } | |
4139 | } | |
4140 | catch (const semantic_error& er) | |
4141 | { | |
4142 | /* ignore and go to the next module */ | |
4143 | continue; | |
4144 | } | |
c4ce66a1 | 4145 | |
5f36109e | 4146 | dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result); |
51178501 | 4147 | dw->iterate_over_modules(&query_module, &q); |
c4ce66a1 | 4148 | } |
abb41d92 | 4149 | |
5f36109e | 4150 | if (!result) |
c4ce66a1 | 4151 | { |
946e1a48 JS |
4152 | // We pass the unresolved cast_op to the next pass, and hope |
4153 | // that this value ends up not being referenced after all, so | |
4154 | // it can be optimized out quietly. | |
c4ce66a1 JS |
4155 | provide (e); |
4156 | return; | |
4157 | } | |
4158 | ||
c4ce66a1 JS |
4159 | if (lvalue) |
4160 | { | |
4161 | // Provide the functioncall to our parent, so that it can be | |
4162 | // used to substitute for the assignment node immediately above | |
4163 | // us. | |
4164 | assert(!target_symbol_setter_functioncalls.empty()); | |
5f36109e | 4165 | *(target_symbol_setter_functioncalls.top()) = result; |
c4ce66a1 JS |
4166 | } |
4167 | ||
5f36109e | 4168 | result->visit (this); |
77de5e9e GH |
4169 | } |
4170 | ||
4171 | ||
b8da0ad1 FCE |
4172 | void |
4173 | dwarf_derived_probe::printsig (ostream& o) const | |
4174 | { | |
4175 | // Instead of just printing the plain locations, we add a PC value | |
4176 | // as a comment as a way of telling e.g. apart multiple inlined | |
4177 | // function instances. This is distinct from the verbose/clog | |
4178 | // output, since this part goes into the cache hash calculations. | |
4179 | sole_location()->print (o); | |
6d0f3f0c | 4180 | o << " /* pc=" << section << "+0x" << hex << addr << dec << " */"; |
b8da0ad1 FCE |
4181 | printsig_nested (o); |
4182 | } | |
4183 | ||
4184 | ||
4185 | ||
dc38c0ae | 4186 | void |
b20febf3 FCE |
4187 | dwarf_derived_probe::join_group (systemtap_session& s) |
4188 | { | |
af234c40 JS |
4189 | // skip probes which are paired entry-handlers |
4190 | if (!has_return && (saved_longs || saved_strings)) | |
4191 | return; | |
4192 | ||
b20febf3 FCE |
4193 | if (! s.dwarf_derived_probes) |
4194 | s.dwarf_derived_probes = new dwarf_derived_probe_group (); | |
4195 | s.dwarf_derived_probes->enroll (this); | |
4196 | } | |
4197 | ||
4198 | ||
2b69faaf JS |
4199 | static bool |
4200 | kernel_supports_inode_uprobes(systemtap_session& s) | |
4201 | { | |
4202 | // The arch-supports is new to the builtin inode-uprobes, so it makes a | |
4203 | // reasonable indicator of the new API. Else we'll need an autoconf... | |
64e807c2 | 4204 | // see also buildrun.cxx:kernel_built_uprobs() |
2b69faaf JS |
4205 | return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y" |
4206 | && s.kernel_config["CONFIG_UPROBES"] == "y"); | |
4207 | } | |
4208 | ||
4209 | ||
3667d615 JS |
4210 | static bool |
4211 | kernel_supports_inode_uretprobes(systemtap_session& s) | |
4212 | { | |
4213 | // We need inode-uprobes first, then look for uretprobe_register either as a | |
4214 | // real export or in the list possibly accessible by kallsyms. | |
4215 | return kernel_supports_inode_uprobes(s) && | |
4216 | (s.kernel_exports.count("uretprobe_register") > 0 || | |
4217 | s.kernel_functions.count("uretprobe_register") > 0); | |
4218 | } | |
4219 | ||
4220 | ||
5261f7ab DS |
4221 | void |
4222 | check_process_probe_kernel_support(systemtap_session& s) | |
4223 | { | |
4224 | // If we've got utrace, we're good to go. | |
4225 | if (s.kernel_config["CONFIG_UTRACE"] == "y") | |
4226 | return; | |
4227 | ||
8c021542 DS |
4228 | // We don't have utrace. For process probes that aren't |
4229 | // uprobes-based, we just need the task_finder. The task_finder | |
b266d318 DS |
4230 | // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a |
4231 | // specific autoconf test for its needs. | |
8c021542 DS |
4232 | // |
4233 | // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty | |
4234 | // approximation. | |
4235 | if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y") | |
4236 | return; | |
4237 | ||
d3e959b0 DS |
4238 | // For uprobes-based process probes, we need the task_finder plus |
4239 | // the builtin inode-uprobes. | |
8c021542 DS |
4240 | if (s.need_uprobes |
4241 | && s.kernel_config["CONFIG_TRACEPOINTS"] == "y" | |
d3e959b0 | 4242 | && kernel_supports_inode_uprobes(s)) |
8c021542 DS |
4243 | return; |
4244 | ||
4245 | throw semantic_error (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES")); | |
5261f7ab DS |
4246 | } |
4247 | ||
4248 | ||
b20febf3 FCE |
4249 | dwarf_derived_probe::dwarf_derived_probe(const string& funcname, |
4250 | const string& filename, | |
4251 | int line, | |
91af0778 | 4252 | // module & section specify a relocation |
b20febf3 FCE |
4253 | // base for <addr>, unless section=="" |
4254 | // (equivalently module=="kernel") | |
4255 | const string& module, | |
4256 | const string& section, | |
4257 | // NB: dwfl_addr is the virtualized | |
4258 | // address for this symbol. | |
4259 | Dwarf_Addr dwfl_addr, | |
4260 | // addr is the section-offset for | |
4261 | // actual relocation. | |
4262 | Dwarf_Addr addr, | |
4263 | dwarf_query& q, | |
37ebca01 | 4264 | Dwarf_Die* scope_die /* may be null */) |
4c5d1300 | 4265 | : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ), |
b20febf3 | 4266 | module (module), section (section), addr (addr), |
63b4fd14 | 4267 | path (q.path), |
27dc09b1 | 4268 | has_process (q.has_process), |
c9bad430 DS |
4269 | has_return (q.has_return), |
4270 | has_maxactive (q.has_maxactive), | |
c57ea854 | 4271 | has_library (q.has_library), |
6b66b9f7 | 4272 | maxactive_val (q.maxactive_val), |
b642c901 SC |
4273 | user_path (q.user_path), |
4274 | user_lib (q.user_lib), | |
af234c40 | 4275 | access_vars(false), |
c57ea854 | 4276 | saved_longs(0), saved_strings(0), |
af234c40 | 4277 | entry_handler(0) |
bd2b1e68 | 4278 | { |
b642c901 SC |
4279 | if (user_lib.size() != 0) |
4280 | has_library = true; | |
4281 | ||
6b66b9f7 JS |
4282 | if (q.has_process) |
4283 | { | |
4284 | // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN. | |
4285 | // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas | |
4286 | // ET_DYN ones do (addr += run-time mmap base address). We tell these apart | |
4287 | // by the incoming section value (".absolute" vs. ".dynamic"). | |
4288 | // XXX Assert invariants here too? | |
2b69faaf JS |
4289 | |
4290 | // inode-uprobes needs an offset rather than an absolute VM address. | |
4441e344 | 4291 | // ditto for userspace runtimes (dyninst) |
ac3af990 | 4292 | if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) && |
2b69faaf JS |
4293 | section == ".absolute" && addr == dwfl_addr && |
4294 | addr >= q.dw.module_start && addr < q.dw.module_end) | |
4295 | this->addr = addr - q.dw.module_start; | |
6b66b9f7 JS |
4296 | } |
4297 | else | |
4298 | { | |
4299 | // Assert kernel relocation invariants | |
4300 | if (section == "" && dwfl_addr != addr) // addr should be absolute | |
ce0f6648 | 4301 | throw semantic_error (_("missing relocation basis"), tok); |
6b66b9f7 | 4302 | if (section != "" && dwfl_addr == addr) // addr should be an offset |
b530b5b3 | 4303 | throw semantic_error (_("inconsistent relocation address"), tok); |
6b66b9f7 | 4304 | } |
2930abc7 | 4305 | |
21beacc9 FCE |
4306 | // XXX: hack for strange g++/gcc's |
4307 | #ifndef USHRT_MAX | |
4308 | #define USHRT_MAX 32767 | |
4309 | #endif | |
4310 | ||
606fd9c8 | 4311 | // Range limit maxactive() value |
6b66b9f7 | 4312 | if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX)) |
b530b5b3 LB |
4313 | throw semantic_error (_F("maxactive value out of range [0,%s]", |
4314 | lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok); | |
606fd9c8 | 4315 | |
de688825 | 4316 | // Expand target variables in the probe body |
5f0a03a6 | 4317 | if (!null_die(scope_die)) |
8fc05e57 | 4318 | { |
6b66b9f7 | 4319 | // XXX: user-space deref's for q.has_process! |
de688825 | 4320 | dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); |
8b095b45 | 4321 | v.replace (this->body); |
6b66b9f7 JS |
4322 | if (!q.has_process) |
4323 | access_vars = v.visited; | |
37ebca01 FCE |
4324 | |
4325 | // If during target-variable-expanding the probe, we added a new block | |
4326 | // of code, add it to the start of the probe. | |
4327 | if (v.add_block) | |
ba6f838d | 4328 | this->body = new block(v.add_block, this->body); |
2260f4e3 FCE |
4329 | |
4330 | // If when target-variable-expanding the probe, we need to synthesize a | |
4331 | // sibling function-entry probe. We don't go through the whole probe derivation | |
4332 | // business (PR10642) that could lead to wildcard/alias resolution, or for that | |
4333 | // dwarf-induced duplication. | |
4334 | if (v.add_call_probe) | |
37ebca01 | 4335 | { |
2260f4e3 FCE |
4336 | assert (q.has_return && !q.has_call); |
4337 | ||
4338 | // We temporarily replace q.base_probe. | |
4339 | statement* old_body = q.base_probe->body; | |
4340 | q.base_probe->body = v.add_call_probe; | |
4341 | q.has_return = false; | |
4342 | q.has_call = true; | |
af234c40 | 4343 | |
da23eceb | 4344 | if (q.has_process) |
af234c40 JS |
4345 | entry_handler = new uprobe_derived_probe (funcname, filename, line, |
4346 | module, section, dwfl_addr, | |
4347 | addr, q, scope_die); | |
da23eceb | 4348 | else |
af234c40 JS |
4349 | entry_handler = new dwarf_derived_probe (funcname, filename, line, |
4350 | module, section, dwfl_addr, | |
4351 | addr, q, scope_die); | |
4352 | ||
4353 | saved_longs = entry_handler->saved_longs = v.saved_longs; | |
4354 | saved_strings = entry_handler->saved_strings = v.saved_strings; | |
4355 | ||
4356 | q.results.push_back (entry_handler); | |
2260f4e3 FCE |
4357 | |
4358 | q.has_return = true; | |
4359 | q.has_call = false; | |
4360 | q.base_probe->body = old_body; | |
37ebca01 | 4361 | } |
f10534c6 WH |
4362 | // Save the local variables for listing mode |
4363 | if (q.sess.listing_mode_vars) | |
8c67c337 | 4364 | saveargs(q, scope_die, dwfl_addr); |
8fc05e57 | 4365 | } |
37ebca01 | 4366 | // else - null scope_die - $target variables will produce an error during translate phase |
8fc05e57 | 4367 | |
f10534c6 | 4368 | // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs |
0a98fd42 | 4369 | |
5d23847d | 4370 | // Reset the sole element of the "locations" vector as a |
b20febf3 FCE |
4371 | // "reverse-engineered" form of the incoming (q.base_loc) probe |
4372 | // point. This allows a user to see what function / file / line | |
4373 | // number any particular match of the wildcards. | |
2930abc7 | 4374 | |
a229fcd7 | 4375 | vector<probe_point::component*> comps; |
91af0778 FCE |
4376 | if (q.has_kernel) |
4377 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
4378 | else if(q.has_module) | |
4379 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
4380 | else if(q.has_process) | |
4381 | comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module))); | |
4382 | else | |
4383 | assert (0); | |
b5d77020 | 4384 | |
db520b00 FCE |
4385 | string fn_or_stmt; |
4386 | if (q.has_function_str || q.has_function_num) | |
4387 | fn_or_stmt = "function"; | |
4388 | else | |
4389 | fn_or_stmt = "statement"; | |
a229fcd7 | 4390 | |
b8da0ad1 | 4391 | if (q.has_function_str || q.has_statement_str) |
db520b00 | 4392 | { |
4cd232e4 | 4393 | string retro_name = funcname; |
b20febf3 | 4394 | if (filename != "") |
cee35f73 | 4395 | { |
fb84c077 | 4396 | retro_name += ("@" + string (filename)); |
cee35f73 | 4397 | if (line > 0) |
aca66a36 | 4398 | retro_name += (":" + lex_cast (line)); |
cee35f73 | 4399 | } |
db520b00 FCE |
4400 | comps.push_back |
4401 | (new probe_point::component | |
4402 | (fn_or_stmt, new literal_string (retro_name))); | |
4403 | } | |
b8da0ad1 | 4404 | else if (q.has_function_num || q.has_statement_num) |
db520b00 FCE |
4405 | { |
4406 | Dwarf_Addr retro_addr; | |
4407 | if (q.has_function_num) | |
4408 | retro_addr = q.function_num_val; | |
4409 | else | |
4410 | retro_addr = q.statement_num_val; | |
db520b00 FCE |
4411 | comps.push_back (new probe_point::component |
4412 | (fn_or_stmt, | |
9ea68eb9 | 4413 | new literal_number(retro_addr, true))); |
37ebca01 FCE |
4414 | |
4415 | if (q.has_absolute) | |
4416 | comps.push_back (new probe_point::component (TOK_ABSOLUTE)); | |
a229fcd7 GH |
4417 | } |
4418 | ||
b8da0ad1 FCE |
4419 | if (q.has_call) |
4420 | comps.push_back (new probe_point::component(TOK_CALL)); | |
4bda987e SC |
4421 | if (q.has_exported) |
4422 | comps.push_back (new probe_point::component(TOK_EXPORTED)); | |
b8da0ad1 FCE |
4423 | if (q.has_inline) |
4424 | comps.push_back (new probe_point::component(TOK_INLINE)); | |
db520b00 | 4425 | if (has_return) |
b8da0ad1 FCE |
4426 | comps.push_back (new probe_point::component(TOK_RETURN)); |
4427 | if (has_maxactive) | |
4428 | comps.push_back (new probe_point::component | |
4429 | (TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d9b516ca | 4430 | |
5d23847d FCE |
4431 | // Overwrite it. |
4432 | this->sole_location()->components = comps; | |
2930abc7 FCE |
4433 | } |
4434 | ||
bd2b1e68 | 4435 | |
0a98fd42 | 4436 | void |
8c67c337 JS |
4437 | dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die, |
4438 | Dwarf_Addr dwfl_addr) | |
0a98fd42 | 4439 | { |
9aa8ffce | 4440 | if (null_die(scope_die)) |
0a98fd42 | 4441 | return; |
0a98fd42 | 4442 | |
8c67c337 | 4443 | bool verbose = q.sess.verbose > 2; |
0a98fd42 | 4444 | |
8c67c337 | 4445 | if (verbose) |
b530b5b3 | 4446 | clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die)); |
0a98fd42 | 4447 | |
8c67c337 JS |
4448 | if (has_return) |
4449 | { | |
4450 | /* Only save the return value if it has a type. */ | |
4451 | string type_name; | |
4452 | Dwarf_Die type_die; | |
4453 | if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) && | |
4454 | dwarf_type_name(&type_die, type_name)) | |
4455 | args.push_back("$return:"+type_name); | |
4456 | ||
4457 | else if (verbose) | |
b530b5b3 LB |
4458 | clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n", |
4459 | lex_cast_hex(dwarf_dieoffset(scope_die)).c_str()); | |
8c67c337 | 4460 | } |
d87623a1 | 4461 | |
0a98fd42 | 4462 | Dwarf_Die arg; |
4ef35696 JS |
4463 | vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die); |
4464 | for (unsigned i = 0; i < scopes.size(); ++i) | |
4465 | { | |
4466 | if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) | |
4467 | break; // we don't want file-level variables | |
4468 | if (dwarf_child (&scopes[i], &arg) == 0) | |
4469 | do | |
0a98fd42 | 4470 | { |
4ef35696 JS |
4471 | switch (dwarf_tag (&arg)) |
4472 | { | |
4473 | case DW_TAG_variable: | |
4474 | case DW_TAG_formal_parameter: | |
4475 | break; | |
0a98fd42 | 4476 | |
4ef35696 JS |
4477 | default: |
4478 | continue; | |
4479 | } | |
0a98fd42 | 4480 | |
4ef35696 JS |
4481 | /* Ignore this local if it has no name. */ |
4482 | const char *arg_name = dwarf_diename (&arg); | |
4483 | if (!arg_name) | |
8c67c337 JS |
4484 | { |
4485 | if (verbose) | |
b530b5b3 LB |
4486 | clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n", |
4487 | lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4488 | continue; |
4489 | } | |
4ef35696 JS |
4490 | |
4491 | if (verbose) | |
b530b5b3 LB |
4492 | clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n", |
4493 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4494 | |
4495 | /* Ignore this local if it has no location (or not at this PC). */ | |
4496 | /* NB: It still may not be directly accessible, e.g. if it is an | |
4497 | * aggregate type, implicit_pointer, etc., but the user can later | |
4498 | * figure out how to access the interesting parts. */ | |
4499 | Dwarf_Attribute attr_mem; | |
4500 | if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem)) | |
4501 | { | |
4502 | Dwarf_Op *expr; | |
4503 | size_t len; | |
4504 | if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem)) | |
4505 | { | |
4506 | if (verbose) | |
b530b5b3 LB |
4507 | clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n", |
4508 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
4ef35696 JS |
4509 | continue; |
4510 | } | |
4511 | else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr, | |
4512 | &len, 1) == 1 && len > 0)) | |
4513 | { | |
4514 | if (verbose) | |
b530b5b3 LB |
4515 | clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n", |
4516 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str()); | |
4ef35696 JS |
4517 | continue; |
4518 | } | |
4519 | } | |
4520 | ||
4521 | /* Ignore this local if it has no type. */ | |
4522 | string type_name; | |
4523 | Dwarf_Die type_die; | |
4524 | if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) || | |
4525 | !dwarf_type_name(&type_die, type_name)) | |
8c67c337 JS |
4526 | { |
4527 | if (verbose) | |
b530b5b3 LB |
4528 | clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n", |
4529 | arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str()); | |
8c67c337 JS |
4530 | continue; |
4531 | } | |
8c67c337 | 4532 | |
4ef35696 JS |
4533 | /* This local looks good -- save it! */ |
4534 | args.push_back("$"+string(arg_name)+":"+type_name); | |
8c67c337 | 4535 | } |
4ef35696 JS |
4536 | while (dwarf_siblingof (&arg, &arg) == 0); |
4537 | } | |
0a98fd42 JS |
4538 | } |
4539 | ||
4540 | ||
4541 | void | |
d0bfd2ac | 4542 | dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const |
0a98fd42 | 4543 | { |
d0bfd2ac | 4544 | arg_set.insert(arg_set.end(), args.begin(), args.end()); |
0a98fd42 JS |
4545 | } |
4546 | ||
4547 | ||
27dc09b1 | 4548 | void |
42e38653 | 4549 | dwarf_derived_probe::emit_privilege_assertion (translator_output* o) |
27dc09b1 DB |
4550 | { |
4551 | if (has_process) | |
4552 | { | |
4553 | // These probes are allowed for unprivileged users, but only in the | |
4554 | // context of processes which they own. | |
4555 | emit_process_owner_assertion (o); | |
4556 | return; | |
4557 | } | |
4558 | ||
4559 | // Other probes must contain the default assertion which aborts | |
4560 | // if executed by an unprivileged user. | |
42e38653 | 4561 | derived_probe::emit_privilege_assertion (o); |
27dc09b1 DB |
4562 | } |
4563 | ||
4564 | ||
4565 | void | |
4566 | dwarf_derived_probe::print_dupe_stamp(ostream& o) | |
4567 | { | |
4568 | if (has_process) | |
4569 | { | |
4570 | // These probes are allowed for unprivileged users, but only in the | |
4571 | // context of processes which they own. | |
4572 | print_dupe_stamp_unprivileged_process_owner (o); | |
4573 | return; | |
4574 | } | |
4575 | ||
4576 | // Other probes must contain the default dupe stamp | |
4577 | derived_probe::print_dupe_stamp (o); | |
4578 | } | |
4579 | ||
64211010 | 4580 | |
7a053d3b | 4581 | void |
20c6c071 | 4582 | dwarf_derived_probe::register_statement_variants(match_node * root, |
27dc09b1 | 4583 | dwarf_builder * dw, |
42e38653 | 4584 | privilege_t privilege) |
bd2b1e68 | 4585 | { |
27dc09b1 | 4586 | root |
42e38653 | 4587 | ->bind_privilege(privilege) |
27dc09b1 | 4588 | ->bind(dw); |
54efe513 GH |
4589 | } |
4590 | ||
7a053d3b | 4591 | void |
fd6602a0 | 4592 | dwarf_derived_probe::register_function_variants(match_node * root, |
27dc09b1 | 4593 | dwarf_builder * dw, |
42e38653 | 4594 | privilege_t privilege) |
2865d17a | 4595 | { |
27dc09b1 | 4596 | root |
42e38653 | 4597 | ->bind_privilege(privilege) |
27dc09b1 | 4598 | ->bind(dw); |
27dc09b1 | 4599 | root->bind(TOK_CALL) |
42e38653 | 4600 | ->bind_privilege(privilege) |
27dc09b1 | 4601 | ->bind(dw); |
4bda987e SC |
4602 | root->bind(TOK_EXPORTED) |
4603 | ->bind_privilege(privilege) | |
4604 | ->bind(dw); | |
27dc09b1 | 4605 | root->bind(TOK_RETURN) |
42e38653 | 4606 | ->bind_privilege(privilege) |
27dc09b1 | 4607 | ->bind(dw); |
1e035395 | 4608 | |
f6be7c06 DB |
4609 | // For process probes / uprobes, .maxactive() is unused. |
4610 | if (! pr_contains (privilege, pr_stapusr)) | |
1e035395 FCE |
4611 | { |
4612 | root->bind(TOK_RETURN) | |
1e035395 FCE |
4613 | ->bind_num(TOK_MAXACTIVE)->bind(dw); |
4614 | } | |
bd2b1e68 GH |
4615 | } |
4616 | ||
7a053d3b | 4617 | void |
27dc09b1 | 4618 | dwarf_derived_probe::register_function_and_statement_variants( |
440d9b00 | 4619 | systemtap_session& s, |
27dc09b1 DB |
4620 | match_node * root, |
4621 | dwarf_builder * dw, | |
42e38653 | 4622 | privilege_t privilege |
27dc09b1 | 4623 | ) |
bd2b1e68 GH |
4624 | { |
4625 | // Here we match 4 forms: | |
4626 | // | |
4627 | // .function("foo") | |
4628 | // .function(0xdeadbeef) | |
4629 | // .statement("foo") | |
4630 | // .statement(0xdeadbeef) | |
4631 | ||
440d9b00 | 4632 | match_node *fv_root = root->bind_str(TOK_FUNCTION); |
42e38653 | 4633 | register_function_variants(fv_root, dw, privilege); |
7f02ca94 | 4634 | // ROOT.function("STRING") always gets the .inline and .label variants. |
440d9b00 | 4635 | fv_root->bind(TOK_INLINE) |
42e38653 | 4636 | ->bind_privilege(privilege) |
440d9b00 | 4637 | ->bind(dw); |
7f02ca94 JS |
4638 | fv_root->bind_str(TOK_LABEL) |
4639 | ->bind_privilege(privilege) | |
440d9b00 DB |
4640 | ->bind(dw); |
4641 | ||
4642 | fv_root = root->bind_num(TOK_FUNCTION); | |
42e38653 | 4643 | register_function_variants(fv_root, dw, privilege); |
440d9b00 DB |
4644 | // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter. |
4645 | if (strverscmp(s.compatible.c_str(), "1.7") <= 0) | |
4646 | { | |
4647 | fv_root->bind(TOK_INLINE) | |
42e38653 | 4648 | ->bind_privilege(privilege) |
440d9b00 DB |
4649 | ->bind(dw); |
4650 | } | |
4651 | ||
42e38653 DB |
4652 | register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege); |
4653 | register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege); | |
bd2b1e68 GH |
4654 | } |
4655 | ||
b1615c74 JS |
4656 | void |
4657 | dwarf_derived_probe::register_sdt_variants(systemtap_session& s, | |
4658 | match_node * root, | |
4659 | dwarf_builder * dw) | |
4660 | { | |
4661 | root->bind_str(TOK_MARK) | |
f66bb29a | 4662 | ->bind_privilege(pr_all) |
b1615c74 JS |
4663 | ->bind(dw); |
4664 | root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK) | |
f66bb29a | 4665 | ->bind_privilege(pr_all) |
b1615c74 JS |
4666 | ->bind(dw); |
4667 | } | |
4668 | ||
4669 | void | |
4670 | dwarf_derived_probe::register_plt_variants(systemtap_session& s, | |
4671 | match_node * root, | |
4672 | dwarf_builder * dw) | |
4673 | { | |
4674 | root->bind(TOK_PLT) | |
f66bb29a | 4675 | ->bind_privilege(pr_all) |
b1615c74 JS |
4676 | ->bind(dw); |
4677 | root->bind_str(TOK_PLT) | |
f66bb29a | 4678 | ->bind_privilege(pr_all) |
b1615c74 JS |
4679 | ->bind(dw); |
4680 | root->bind(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 4681 | ->bind_privilege(pr_all) |
b1615c74 JS |
4682 | ->bind(dw); |
4683 | root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT) | |
f66bb29a | 4684 | ->bind_privilege(pr_all) |
b1615c74 | 4685 | ->bind(dw); |
bd2b1e68 GH |
4686 | } |
4687 | ||
4688 | void | |
c4ce66a1 | 4689 | dwarf_derived_probe::register_patterns(systemtap_session& s) |
bd2b1e68 | 4690 | { |
c4ce66a1 | 4691 | match_node* root = s.pattern_root; |
bd2b1e68 GH |
4692 | dwarf_builder *dw = new dwarf_builder(); |
4693 | ||
c4ce66a1 JS |
4694 | update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw); |
4695 | s.code_filters.push_back(filter); | |
4696 | ||
73f52eb4 DB |
4697 | register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged); |
4698 | register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged); | |
27dc09b1 DB |
4699 | root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) |
4700 | ->bind(dw); | |
2cab6244 | 4701 | |
7f02ca94 JS |
4702 | match_node* uprobes[] = { |
4703 | root->bind(TOK_PROCESS), | |
4704 | root->bind_str(TOK_PROCESS), | |
4705 | root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
4706 | root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY), | |
4707 | }; | |
4708 | for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i) | |
4709 | { | |
f66bb29a | 4710 | register_function_and_statement_variants(s, uprobes[i], dw, pr_all); |
7f02ca94 JS |
4711 | register_sdt_variants(s, uprobes[i], dw); |
4712 | register_plt_variants(s, uprobes[i], dw); | |
4713 | } | |
bd2b1e68 GH |
4714 | } |
4715 | ||
9020300d FCE |
4716 | void |
4717 | dwarf_derived_probe::emit_probe_local_init(translator_output * o) | |
4718 | { | |
b95e2b79 MH |
4719 | if (access_vars) |
4720 | { | |
4721 | // if accessing $variables, emit bsp cache setup for speeding up | |
d4670309 | 4722 | o->newline() << "#if defined __ia64__"; |
d9aed31e | 4723 | o->newline() << "bspcache(c->unwaddr, c->kregs);"; |
d4670309 | 4724 | o->newline() << "#endif"; |
b95e2b79 | 4725 | } |
9020300d | 4726 | } |
2930abc7 | 4727 | |
b20febf3 | 4728 | // ------------------------------------------------------------------------ |
46b84a80 DS |
4729 | |
4730 | void | |
b20febf3 | 4731 | dwarf_derived_probe_group::enroll (dwarf_derived_probe* p) |
46b84a80 | 4732 | { |
b20febf3 | 4733 | probes_by_module.insert (make_pair (p->module, p)); |
b8da0ad1 FCE |
4734 | |
4735 | // XXX: probes put at the same address should all share a | |
4736 | // single kprobe/kretprobe, and have their handlers executed | |
4737 | // sequentially. | |
b55bc428 FCE |
4738 | } |
4739 | ||
7a053d3b | 4740 | void |
775d51e5 | 4741 | dwarf_derived_probe_group::emit_module_decls (systemtap_session& s) |
ec4373ff | 4742 | { |
b20febf3 | 4743 | if (probes_by_module.empty()) return; |
2930abc7 | 4744 | |
775d51e5 DS |
4745 | s.op->newline() << "/* ---- dwarf probes ---- */"; |
4746 | ||
4747 | // Warn of misconfigured kernels | |
f41595cc FCE |
4748 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; |
4749 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
4750 | s.op->newline() << "#endif"; | |
775d51e5 | 4751 | s.op->newline(); |
f41595cc | 4752 | |
f07c3b68 | 4753 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 4754 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
4755 | s.op->newline() << "#endif"; |
4756 | ||
14cf7e42 | 4757 | // Forward decls |
2ba1736a | 4758 | s.op->newline() << "#include \"linux/kprobes-common.h\""; |
14cf7e42 | 4759 | |
b20febf3 FCE |
4760 | // Forward declare the master entry functions |
4761 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4762 | s.op->line() << " struct pt_regs *regs);"; | |
4763 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4764 | s.op->line() << " struct pt_regs *regs);"; | |
4765 | ||
42cb22bd MH |
4766 | // Emit an array of kprobe/kretprobe pointers |
4767 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
4768 | s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];"; | |
4769 | s.op->newline() << "#endif"; | |
4770 | ||
b20febf3 | 4771 | // Emit the actual probe list. |
606fd9c8 FCE |
4772 | |
4773 | // NB: we used to plop a union { struct kprobe; struct kretprobe } into | |
4774 | // struct stap_dwarf_probe, but it being initialized data makes it add | |
4775 | // hundreds of bytes of padding per stap_dwarf_probe. (PR5673) | |
14cf7e42 | 4776 | s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];"; |
606fd9c8 FCE |
4777 | // NB: bss! |
4778 | ||
4c2732a1 | 4779 | s.op->newline() << "static struct stap_dwarf_probe {"; |
b0986e7a DS |
4780 | s.op->newline(1) << "const unsigned return_p:1;"; |
4781 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 4782 | s.op->newline() << "const unsigned optional_p:1;"; |
b20febf3 | 4783 | s.op->newline() << "unsigned registered_p:1;"; |
b0986e7a | 4784 | s.op->newline() << "const unsigned short maxactive_val;"; |
606fd9c8 | 4785 | |
af234c40 JS |
4786 | // data saved in the kretprobe_instance packet |
4787 | s.op->newline() << "const unsigned short saved_longs;"; | |
4788 | s.op->newline() << "const unsigned short saved_strings;"; | |
4789 | ||
faea5e16 | 4790 | // Let's find some stats for the embedded strings. Maybe they |
606fd9c8 FCE |
4791 | // are small and uniform enough to justify putting char[MAX]'s into |
4792 | // the array instead of relocated char*'s. | |
faea5e16 JS |
4793 | size_t module_name_max = 0, section_name_max = 0; |
4794 | size_t module_name_tot = 0, section_name_tot = 0; | |
606fd9c8 FCE |
4795 | size_t all_name_cnt = probes_by_module.size(); // for average |
4796 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
4797 | { | |
4798 | dwarf_derived_probe* p = it->second; | |
4799 | #define DOIT(var,expr) do { \ | |
4800 | size_t var##_size = (expr) + 1; \ | |
4801 | var##_max = max (var##_max, var##_size); \ | |
4802 | var##_tot += var##_size; } while (0) | |
4803 | DOIT(module_name, p->module.size()); | |
4804 | DOIT(section_name, p->section.size()); | |
606fd9c8 FCE |
4805 | #undef DOIT |
4806 | } | |
4807 | ||
4808 | // Decide whether it's worthwhile to use char[] or char* by comparing | |
4809 | // the amount of average waste (max - avg) to the relocation data size | |
4810 | // (3 native long words). | |
4811 | #define CALCIT(var) \ | |
4812 | if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \ | |
4813 | { \ | |
4814 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \ | |
4815 | if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \ | |
4816 | << "[" << var##_name_max << "]" << endl; \ | |
4817 | } \ | |
4818 | else \ | |
4819 | { \ | |
b0986e7a | 4820 | s.op->newline() << "const char * const " << #var << ";"; \ |
606fd9c8 FCE |
4821 | if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \ |
4822 | } | |
4823 | ||
4824 | CALCIT(module); | |
4825 | CALCIT(section); | |
e6fe60e7 | 4826 | #undef CALCIT |
606fd9c8 | 4827 | |
b0986e7a | 4828 | s.op->newline() << "const unsigned long address;"; |
26e63673 | 4829 | s.op->newline() << "struct stap_probe * const probe;"; |
c87ae2c1 | 4830 | s.op->newline() << "struct stap_probe * const entry_probe;"; |
b20febf3 FCE |
4831 | s.op->newline(-1) << "} stap_dwarf_probes[] = {"; |
4832 | s.op->indent(1); | |
4833 | ||
4834 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) | |
2930abc7 | 4835 | { |
b20febf3 FCE |
4836 | dwarf_derived_probe* p = it->second; |
4837 | s.op->newline() << "{"; | |
4838 | if (p->has_return) | |
4839 | s.op->line() << " .return_p=1,"; | |
c9bad430 | 4840 | if (p->has_maxactive) |
606fd9c8 FCE |
4841 | { |
4842 | s.op->line() << " .maxactive_p=1,"; | |
4843 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
4844 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
4845 | } | |
af234c40 JS |
4846 | if (p->saved_longs || p->saved_strings) |
4847 | { | |
4848 | if (p->saved_longs) | |
4849 | s.op->line() << " .saved_longs=" << p->saved_longs << ","; | |
4850 | if (p->saved_strings) | |
4851 | s.op->line() << " .saved_strings=" << p->saved_strings << ","; | |
4852 | if (p->entry_handler) | |
c87ae2c1 | 4853 | s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ","; |
af234c40 | 4854 | } |
b350f56b JS |
4855 | if (p->locations[0]->optional) |
4856 | s.op->line() << " .optional_p=1,"; | |
dc38c256 | 4857 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
84048984 FCE |
4858 | s.op->line() << " .module=\"" << p->module << "\","; |
4859 | s.op->line() << " .section=\"" << p->section << "\","; | |
faea5e16 | 4860 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
b20febf3 | 4861 | s.op->line() << " },"; |
2930abc7 | 4862 | } |
2930abc7 | 4863 | |
b20febf3 FCE |
4864 | s.op->newline(-1) << "};"; |
4865 | ||
4866 | // Emit the kprobes callback function | |
4867 | s.op->newline(); | |
4868 | s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,"; | |
4869 | s.op->line() << " struct pt_regs *regs) {"; | |
606fd9c8 FCE |
4870 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
4871 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; | |
4872 | // Check that the index is plausible | |
4873 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4874 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4875 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4876 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4877 | s.op->line() << "];"; | |
71db462b | 4878 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 4879 | "stp_probe_type_kprobe"); |
d9aed31e | 4880 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
4881 | |
4882 | // Make it look like the IP is set as it wouldn't have been replaced | |
4883 | // by a breakpoint instruction when calling real probe handler. Reset | |
4884 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
4885 | s.op->newline() << "{"; | |
4886 | s.op->indent(1); | |
d9aed31e | 4887 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 4888 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 4889 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 4890 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4891 | s.op->newline(-1) << "}"; |
4892 | ||
f887a8c9 | 4893 | common_probe_entryfn_epilogue (s, true); |
b20febf3 FCE |
4894 | s.op->newline() << "return 0;"; |
4895 | s.op->newline(-1) << "}"; | |
4896 | ||
4897 | // Same for kretprobes | |
4898 | s.op->newline(); | |
af234c40 JS |
4899 | s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,"; |
4900 | s.op->line() << " struct pt_regs *regs, int entry) {"; | |
b20febf3 | 4901 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; |
606fd9c8 FCE |
4902 | |
4903 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
a36378d7 | 4904 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);"; |
606fd9c8 FCE |
4905 | // Check that the index is plausible |
4906 | s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes["; | |
4907 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
4908 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
4909 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
4910 | s.op->line() << "];"; | |
4911 | ||
c87ae2c1 JS |
4912 | s.op->newline() << "struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;"; |
4913 | s.op->newline() << "if (sp) {"; | |
4914 | s.op->indent(1); | |
71db462b | 4915 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp", |
cda141c2 | 4916 | "stp_probe_type_kretprobe"); |
d9aed31e | 4917 | s.op->newline() << "c->kregs = regs;"; |
af234c40 JS |
4918 | |
4919 | // for assisting runtime's backtrace logic and accessing kretprobe data packets | |
6dceb5c9 MW |
4920 | s.op->newline() << "c->ips.krp.pi = inst;"; |
4921 | s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;"; | |
6415ddde MW |
4922 | |
4923 | // Make it look like the IP is set as it wouldn't have been replaced | |
4924 | // by a breakpoint instruction when calling real probe handler. Reset | |
4925 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
4926 | s.op->newline() << "{"; | |
d9aed31e | 4927 | s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
c87ae2c1 JS |
4928 | s.op->newline() << "if (entry)"; |
4929 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; | |
4930 | s.op->newline(-1) << "else"; | |
4931 | s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);"; | |
4932 | s.op->newline(-1) << "(sp->ph) (c);"; | |
259d54c0 | 4933 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
4934 | s.op->newline(-1) << "}"; |
4935 | ||
f887a8c9 | 4936 | common_probe_entryfn_epilogue (s, true); |
c87ae2c1 | 4937 | s.op->newline(-1) << "}"; |
b20febf3 FCE |
4938 | s.op->newline() << "return 0;"; |
4939 | s.op->newline(-1) << "}"; | |
af234c40 JS |
4940 | |
4941 | s.op->newline(); | |
4942 | s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,"; | |
4943 | s.op->line() << " struct pt_regs *regs) {"; | |
4944 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);"; | |
4945 | s.op->newline(-1) << "}"; | |
4946 | ||
4947 | s.op->newline(); | |
4948 | s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,"; | |
4949 | s.op->line() << " struct pt_regs *regs) {"; | |
4950 | s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);"; | |
4951 | s.op->newline(-1) << "}"; | |
b642c901 | 4952 | |
14cf7e42 | 4953 | s.op->newline(); |
20c6c071 | 4954 | } |
ec4373ff | 4955 | |
20c6c071 | 4956 | |
dc38c0ae | 4957 | void |
b20febf3 FCE |
4958 | dwarf_derived_probe_group::emit_module_init (systemtap_session& s) |
4959 | { | |
4960 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
4961 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 4962 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
a049e342 | 4963 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; |
b20febf3 | 4964 | s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent |
26e63673 | 4965 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b20febf3 | 4966 | s.op->newline() << "if (sdp->return_p) {"; |
606fd9c8 | 4967 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; |
c9bad430 | 4968 | s.op->newline() << "if (sdp->maxactive_p) {"; |
606fd9c8 | 4969 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; |
c9bad430 | 4970 | s.op->newline(-1) << "} else {"; |
f07c3b68 | 4971 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
c9bad430 | 4972 | s.op->newline(-1) << "}"; |
606fd9c8 | 4973 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; |
af234c40 | 4974 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; |
c87ae2c1 | 4975 | s.op->newline() << "if (sdp->entry_probe) {"; |
af234c40 JS |
4976 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; |
4977 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
4978 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
4979 | s.op->newline(-1) << "}"; | |
4980 | s.op->newline() << "#endif"; | |
e4cb375f MH |
4981 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
4982 | s.op->newline() << "#ifdef __ia64__"; | |
4983 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
4984 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
4985 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
4986 | s.op->newline() << "if (rc == 0) {"; | |
4987 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
4988 | s.op->newline() << "if (rc != 0)"; | |
4989 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
4990 | s.op->newline(-2) << "}"; | |
4991 | s.op->newline() << "#else"; | |
606fd9c8 | 4992 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; |
e4cb375f | 4993 | s.op->newline() << "#endif"; |
b20febf3 | 4994 | s.op->newline(-1) << "} else {"; |
e4cb375f | 4995 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
606fd9c8 FCE |
4996 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; |
4997 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
e4cb375f MH |
4998 | s.op->newline() << "#ifdef __ia64__"; |
4999 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5000 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5001 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5002 | s.op->newline() << "if (rc == 0) {"; | |
5003 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5004 | s.op->newline() << "if (rc != 0)"; | |
5005 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5006 | s.op->newline(-2) << "}"; | |
5007 | s.op->newline() << "#else"; | |
606fd9c8 | 5008 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; |
e4cb375f | 5009 | s.op->newline() << "#endif"; |
b20febf3 | 5010 | s.op->newline(-1) << "}"; |
9063462a FCE |
5011 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. |
5012 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 5013 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 5014 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);"; |
b350f56b | 5015 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
9063462a FCE |
5016 | // XXX: shall we increment numskipped? |
5017 | s.op->newline(-1) << "}"; | |
5018 | ||
5019 | #if 0 /* pre PR 6749; XXX consider making an option */ | |
c48cb0cc | 5020 | s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback |
b20febf3 | 5021 | s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];"; |
606fd9c8 FCE |
5022 | s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];"; |
5023 | s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);"; | |
5024 | s.op->newline() << "else unregister_kprobe (&kp2->u.kp);"; | |
e4cb375f MH |
5025 | s.op->newline() << "#ifdef __ia64__"; |
5026 | s.op->newline() << "unregister_kprobe (&kp2->dummy);"; | |
5027 | s.op->newline() << "#endif"; | |
c48cb0cc FCE |
5028 | // NB: we don't have to clear sdp2->registered_p, since the module_exit code is |
5029 | // not run for this early-abort case. | |
5030 | s.op->newline(-1) << "}"; | |
5031 | s.op->newline() << "break;"; // don't attempt to register any more probes | |
b20febf3 | 5032 | s.op->newline(-1) << "}"; |
9063462a FCE |
5033 | #endif |
5034 | ||
b20febf3 FCE |
5035 | s.op->newline() << "else sdp->registered_p = 1;"; |
5036 | s.op->newline(-1) << "}"; // for loop | |
dc38c0ae DS |
5037 | } |
5038 | ||
5039 | ||
b4be7cbc FCE |
5040 | void |
5041 | dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s) | |
5042 | { | |
5043 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5044 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5045 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5046 | s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);"; | |
5047 | s.op->newline() << "int rc;"; | |
5048 | ||
5049 | // new module arrived? | |
5050 | s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {"; | |
5051 | s.op->newline(1) << "if (sdp->return_p) {"; | |
5052 | s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;"; | |
5053 | s.op->newline() << "if (sdp->maxactive_p) {"; | |
5054 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
5055 | s.op->newline(-1) << "} else {"; | |
5056 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; | |
5057 | s.op->newline(-1) << "}"; | |
5058 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;"; | |
5059 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)"; | |
5060 | s.op->newline() << "if (sdp->entry_probe) {"; | |
5061 | s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;"; | |
5062 | s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + "; | |
5063 | s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;"; | |
5064 | s.op->newline(-1) << "}"; | |
5065 | s.op->newline() << "#endif"; | |
5066 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
5067 | s.op->newline() << "#ifdef __ia64__"; | |
5068 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; | |
5069 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5070 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5071 | s.op->newline() << "if (rc == 0) {"; | |
5072 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
5073 | s.op->newline() << "if (rc != 0)"; | |
5074 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5075 | s.op->newline(-2) << "}"; | |
5076 | s.op->newline() << "#else"; | |
5077 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
5078 | s.op->newline() << "#endif"; | |
5079 | s.op->newline(-1) << "} else {"; | |
5080 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
5081 | s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;"; | |
5082 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;"; | |
5083 | s.op->newline() << "#ifdef __ia64__"; | |
5084 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; | |
5085 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; | |
5086 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; | |
5087 | s.op->newline() << "if (rc == 0) {"; | |
5088 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
5089 | s.op->newline() << "if (rc != 0)"; | |
5090 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
5091 | s.op->newline(-2) << "}"; | |
5092 | s.op->newline() << "#else"; | |
5093 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
5094 | s.op->newline() << "#endif"; | |
5095 | s.op->newline(-1) << "}"; | |
5096 | s.op->newline() << "if (rc == 0) sdp->registered_p = 1;"; | |
5097 | ||
5098 | // old module disappeared? | |
5099 | s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {"; | |
5100 | s.op->newline(1) << "if (sdp->return_p) {"; | |
5101 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
065d5567 | 5102 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5103 | s.op->newline() << "#ifdef STP_TIMING"; |
5104 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
5105 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; | |
5106 | s.op->newline(-1) << "#endif"; | |
065d5567 | 5107 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5108 | s.op->newline() << "#ifdef STP_TIMING"; |
5109 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
5110 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);"; | |
5111 | s.op->newline(-1) << "#endif"; | |
5112 | s.op->newline(-1) << "} else {"; | |
5113 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
065d5567 | 5114 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
b4be7cbc FCE |
5115 | s.op->newline() << "#ifdef STP_TIMING"; |
5116 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
5117 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; | |
5118 | s.op->newline(-1) << "#endif"; | |
5119 | s.op->newline(-1) << "}"; | |
5120 | s.op->newline() << "#if defined(__ia64__)"; | |
5121 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5122 | s.op->newline() << "#endif"; | |
5123 | s.op->newline() << "sdp->registered_p = 0;"; | |
5124 | s.op->newline(-1) << "}"; | |
5125 | ||
5126 | s.op->newline(-1) << "}"; // for loop | |
5127 | } | |
5128 | ||
5129 | ||
5130 | ||
5131 | ||
46b84a80 | 5132 | void |
b20febf3 | 5133 | dwarf_derived_probe_group::emit_module_exit (systemtap_session& s) |
46b84a80 | 5134 | { |
42cb22bd MH |
5135 | //Unregister kprobes by batch interfaces. |
5136 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
5137 | s.op->newline() << "j = 0;"; | |
5138 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5139 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5140 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5141 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5142 | s.op->newline() << "if (!sdp->return_p)"; | |
5143 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;"; | |
5144 | s.op->newline(-2) << "}"; | |
5145 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5146 | s.op->newline() << "j = 0;"; | |
5147 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5148 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5149 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5150 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5151 | s.op->newline() << "if (sdp->return_p)"; | |
5152 | s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;"; | |
5153 | s.op->newline(-2) << "}"; | |
5154 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);"; | |
e4cb375f MH |
5155 | s.op->newline() << "#ifdef __ia64__"; |
5156 | s.op->newline() << "j = 0;"; | |
5157 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
5158 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
5159 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; | |
5160 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
5161 | s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;"; | |
5162 | s.op->newline(-1) << "}"; | |
5163 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);"; | |
5164 | s.op->newline() << "#endif"; | |
42cb22bd MH |
5165 | s.op->newline() << "#endif"; |
5166 | ||
b20febf3 FCE |
5167 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
5168 | s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];"; | |
a36378d7 | 5169 | s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];"; |
b20febf3 FCE |
5170 | s.op->newline() << "if (! sdp->registered_p) continue;"; |
5171 | s.op->newline() << "if (sdp->return_p) {"; | |
42cb22bd | 5172 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5173 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; |
42cb22bd | 5174 | s.op->newline() << "#endif"; |
065d5567 | 5175 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
73209876 FCE |
5176 | s.op->newline() << "#ifdef STP_TIMING"; |
5177 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 5178 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
73209876 | 5179 | s.op->newline(-1) << "#endif"; |
065d5567 | 5180 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
73209876 FCE |
5181 | s.op->newline() << "#ifdef STP_TIMING"; |
5182 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 5183 | 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 | 5184 | s.op->newline(-1) << "#endif"; |
557fb7a8 | 5185 | s.op->newline(-1) << "} else {"; |
42cb22bd | 5186 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; |
606fd9c8 | 5187 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; |
42cb22bd | 5188 | s.op->newline() << "#endif"; |
065d5567 | 5189 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
73209876 FCE |
5190 | s.op->newline() << "#ifdef STP_TIMING"; |
5191 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 5192 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
73209876 | 5193 | s.op->newline(-1) << "#endif"; |
b20febf3 | 5194 | s.op->newline(-1) << "}"; |
e4cb375f MH |
5195 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; |
5196 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
5197 | s.op->newline() << "#endif"; | |
b20febf3 FCE |
5198 | s.op->newline() << "sdp->registered_p = 0;"; |
5199 | s.op->newline(-1) << "}"; | |
46b84a80 DS |
5200 | } |
5201 | ||
272c9036 WF |
5202 | static void sdt_v3_tokenize(const string& str, vector<string>& tokens) |
5203 | { | |
5204 | string::size_type pos; | |
5205 | string::size_type lastPos = str.find_first_not_of(" ", 0); | |
5206 | string::size_type nextAt = str.find("@", lastPos); | |
e9a90eee JS |
5207 | |
5208 | if (nextAt == string::npos) | |
5209 | { | |
5210 | // PR13934: Assembly probes are not forced to use the N@OP form. | |
5211 | // In this case, N is inferred to be the native word size. Since we | |
5212 | // don't have a nice delimiter, just split it on spaces. SDT-asm authors | |
5213 | // then must not put any spaces in arguments, to avoid ambiguity. | |
5214 | tokenize(str, tokens, " "); | |
5215 | return; | |
5216 | } | |
5217 | ||
272c9036 WF |
5218 | while (lastPos != string::npos) |
5219 | { | |
5220 | pos = nextAt + 1; | |
5221 | nextAt = str.find("@", pos); | |
5222 | if (nextAt == string::npos) | |
5223 | pos = string::npos; | |
5224 | else | |
5225 | pos = str.rfind(" ", nextAt); | |
5226 | ||
5227 | tokens.push_back(str.substr(lastPos, pos - lastPos)); | |
5228 | lastPos = str.find_first_not_of(" ", pos); | |
5229 | } | |
5230 | } | |
8aabf152 | 5231 | |
8aabf152 | 5232 | |
aff5d390 | 5233 | struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor |
7a05f484 | 5234 | { |
ae1418f0 | 5235 | enum regwidths {QI, QIh, HI, SI, DI}; |
f83336a5 FCE |
5236 | sdt_uprobe_var_expanding_visitor(systemtap_session& s, |
5237 | int elf_machine, | |
5238 | const string & process_name, | |
a794dbeb | 5239 | const string & provider_name, |
aff5d390 | 5240 | const string & probe_name, |
71e5e13d | 5241 | stap_sdt_probe_type probe_type, |
aff5d390 | 5242 | const string & arg_string, |
8aabf152 | 5243 | int ac): |
332ba7e7 | 5244 | session (s), elf_machine (elf_machine), process_name (process_name), |
71e5e13d SC |
5245 | provider_name (provider_name), probe_name (probe_name), |
5246 | probe_type (probe_type), arg_count ((unsigned) ac) | |
a8ec7719 | 5247 | { |
f83336a5 FCE |
5248 | /* Register name mapping table depends on the elf machine of this particular |
5249 | probe target process/file, not upon the host. So we can't just | |
5250 | #ifdef _i686_ etc. */ | |
ae1418f0 FCE |
5251 | |
5252 | #define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width) | |
f83336a5 | 5253 | if (elf_machine == EM_X86_64) { |
46a94997 SC |
5254 | DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); |
5255 | DRI ("%al", 0, QI); DRI ("%ah", 0, QIh); | |
5256 | DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI); | |
5257 | DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh); | |
5258 | DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI); | |
5259 | DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh); | |
5260 | DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); | |
5261 | DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh); | |
5262 | DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI); | |
5263 | DRI ("%sil", 4, QI); | |
5264 | DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI); | |
5265 | DRI ("%dil", 5, QI); | |
5266 | DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI); | |
5267 | DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI); | |
5268 | DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI); | |
5269 | DRI ("%r8b", 8, QI); | |
5270 | DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI); | |
5271 | DRI ("%r9b", 9, QI); | |
5272 | DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI); | |
5273 | DRI ("%r10b", 10, QI); | |
5274 | DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI); | |
5275 | DRI ("%r11b", 11, QI); | |
5276 | DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI); | |
5277 | DRI ("%r12b", 12, QI); | |
5278 | DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI); | |
5279 | DRI ("%r13b", 13, QI); | |
5280 | DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI); | |
5281 | DRI ("%r14b", 14, QI); | |
5282 | DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI); | |
5283 | DRI ("%r15b", 15, QI); | |
f83336a5 | 5284 | } else if (elf_machine == EM_386) { |
46a94997 SC |
5285 | DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI); |
5286 | DRI ("%ah", 0, QIh); | |
5287 | DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI); | |
5288 | DRI ("%ch", 1, QIh); | |
5289 | DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI); | |
5290 | DRI ("%dh", 2, QIh); | |
5291 | DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI); | |
5292 | DRI ("%bh", 3, QIh); | |
5293 | DRI ("%esp", 4, SI); DRI ("%sp", 4, HI); | |
5294 | DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI); | |
5295 | DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI); | |
5296 | DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI); | |
0491c523 | 5297 | } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) { |
46a94997 SC |
5298 | DRI ("%r0", 0, DI); |
5299 | DRI ("%r1", 1, DI); | |
5300 | DRI ("%r2", 2, DI); | |
5301 | DRI ("%r3", 3, DI); | |
5302 | DRI ("%r4", 4, DI); | |
5303 | DRI ("%r5", 5, DI); | |
5304 | DRI ("%r6", 6, DI); | |
5305 | DRI ("%r7", 7, DI); | |
5306 | DRI ("%r8", 8, DI); | |
5307 | DRI ("%r9", 9, DI); | |
5308 | DRI ("%r10", 10, DI); | |
5309 | DRI ("%r11", 11, DI); | |
5310 | DRI ("%r12", 12, DI); | |
5311 | DRI ("%r13", 13, DI); | |
5312 | DRI ("%r14", 14, DI); | |
5313 | DRI ("%r15", 15, DI); | |
5314 | DRI ("%r16", 16, DI); | |
5315 | DRI ("%r17", 17, DI); | |
5316 | DRI ("%r18", 18, DI); | |
5317 | DRI ("%r19", 19, DI); | |
5318 | DRI ("%r20", 20, DI); | |
5319 | DRI ("%r21", 21, DI); | |
5320 | DRI ("%r22", 22, DI); | |
5321 | DRI ("%r23", 23, DI); | |
5322 | DRI ("%r24", 24, DI); | |
5323 | DRI ("%r25", 25, DI); | |
5324 | DRI ("%r26", 26, DI); | |
5325 | DRI ("%r27", 27, DI); | |
5326 | DRI ("%r28", 28, DI); | |
5327 | DRI ("%r29", 29, DI); | |
5328 | DRI ("%r30", 30, DI); | |
5329 | DRI ("%r31", 31, DI); | |
8aabf152 | 5330 | // PR11821: unadorned register "names" without -mregnames |
46a94997 SC |
5331 | DRI ("0", 0, DI); |
5332 | DRI ("1", 1, DI); | |
5333 | DRI ("2", 2, DI); | |
5334 | DRI ("3", 3, DI); | |
5335 | DRI ("4", 4, DI); | |
5336 | DRI ("5", 5, DI); | |
5337 | DRI ("6", 6, DI); | |
5338 | DRI ("7", 7, DI); | |
5339 | DRI ("8", 8, DI); | |
5340 | DRI ("9", 9, DI); | |
5341 | DRI ("10", 10, DI); | |
5342 | DRI ("11", 11, DI); | |
5343 | DRI ("12", 12, DI); | |
5344 | DRI ("13", 13, DI); | |
5345 | DRI ("14", 14, DI); | |
5346 | DRI ("15", 15, DI); | |
5347 | DRI ("16", 16, DI); | |
5348 | DRI ("17", 17, DI); | |
5349 | DRI ("18", 18, DI); | |
5350 | DRI ("19", 19, DI); | |
5351 | DRI ("20", 20, DI); | |
5352 | DRI ("21", 21, DI); | |
5353 | DRI ("22", 22, DI); | |
5354 | DRI ("23", 23, DI); | |
5355 | DRI ("24", 24, DI); | |
5356 | DRI ("25", 25, DI); | |
5357 | DRI ("26", 26, DI); | |
5358 | DRI ("27", 27, DI); | |
5359 | DRI ("28", 28, DI); | |
5360 | DRI ("29", 29, DI); | |
5361 | DRI ("30", 30, DI); | |
5362 | DRI ("31", 31, DI); | |
14900130 | 5363 | } else if (elf_machine == EM_S390) { |
46a94997 SC |
5364 | DRI ("%r0", 0, DI); |
5365 | DRI ("%r1", 1, DI); | |
5366 | DRI ("%r2", 2, DI); | |
5367 | DRI ("%r3", 3, DI); | |
5368 | DRI ("%r4", 4, DI); | |
5369 | DRI ("%r5", 5, DI); | |
5370 | DRI ("%r6", 6, DI); | |
5371 | DRI ("%r7", 7, DI); | |
5372 | DRI ("%r8", 8, DI); | |
5373 | DRI ("%r9", 9, DI); | |
5374 | DRI ("%r10", 10, DI); | |
5375 | DRI ("%r11", 11, DI); | |
5376 | DRI ("%r12", 12, DI); | |
5377 | DRI ("%r13", 13, DI); | |
5378 | DRI ("%r14", 14, DI); | |
5379 | DRI ("%r15", 15, DI); | |
272c9036 WF |
5380 | } else if (elf_machine == EM_ARM) { |
5381 | DRI ("r0", 0, SI); | |
5382 | DRI ("r1", 1, SI); | |
5383 | DRI ("r2", 2, SI); | |
5384 | DRI ("r3", 3, SI); | |
5385 | DRI ("r4", 4, SI); | |
5386 | DRI ("r5", 5, SI); | |
5387 | DRI ("r6", 6, SI); | |
5388 | DRI ("r7", 7, SI); | |
5389 | DRI ("r8", 8, SI); | |
5390 | DRI ("r9", 9, SI); | |
5391 | DRI ("sl", 10, SI); | |
5392 | DRI ("fp", 11, SI); | |
5393 | DRI ("ip", 12, SI); | |
5394 | DRI ("sp", 13, SI); | |
5395 | DRI ("lr", 14, SI); | |
5396 | DRI ("pc", 15, SI); | |
14900130 | 5397 | } else if (arg_count) { |
8aabf152 | 5398 | /* permit this case; just fall back to dwarf */ |
f83336a5 | 5399 | } |
ae1418f0 | 5400 | #undef DRI |
f83336a5 | 5401 | |
ebbd2b45 | 5402 | need_debug_info = false; |
88e39987 | 5403 | if (probe_type == uprobe3_type) |
272c9036 WF |
5404 | { |
5405 | sdt_v3_tokenize(arg_string, arg_tokens); | |
5406 | assert(arg_count <= 12); | |
5407 | } | |
88e39987 | 5408 | else |
272c9036 WF |
5409 | { |
5410 | tokenize(arg_string, arg_tokens, " "); | |
5411 | assert(arg_count <= 10); | |
5412 | } | |
a8ec7719 | 5413 | } |
8aabf152 | 5414 | |
f83336a5 | 5415 | systemtap_session& session; |
332ba7e7 | 5416 | int elf_machine; |
aff5d390 | 5417 | const string & process_name; |
a794dbeb | 5418 | const string & provider_name; |
aff5d390 | 5419 | const string & probe_name; |
71e5e13d | 5420 | stap_sdt_probe_type probe_type; |
8aabf152 | 5421 | unsigned arg_count; |
aff5d390 | 5422 | vector<string> arg_tokens; |
46a94997 | 5423 | map<string, pair<unsigned,int> > dwarf_regs; |
ebbd2b45 | 5424 | bool need_debug_info; |
aff5d390 SC |
5425 | |
5426 | void visit_target_symbol (target_symbol* e); | |
6ef331c8 SC |
5427 | void visit_target_symbol_arg (target_symbol* e); |
5428 | void visit_target_symbol_context (target_symbol* e); | |
40a0c64e | 5429 | void visit_cast_op (cast_op* e); |
aff5d390 SC |
5430 | }; |
5431 | ||
7a05f484 SC |
5432 | |
5433 | void | |
6ef331c8 | 5434 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) |
7a05f484 | 5435 | { |
6ef331c8 | 5436 | if (e->addressof) |
b530b5b3 | 5437 | throw semantic_error(_("cannot take address of context variable"), e->tok); |
6ef331c8 SC |
5438 | |
5439 | if (e->name == "$$name") | |
7a05f484 | 5440 | { |
6ef331c8 SC |
5441 | literal_string *myname = new literal_string (probe_name); |
5442 | myname->tok = e->tok; | |
5443 | provide(myname); | |
5444 | return; | |
5445 | } | |
7a05f484 | 5446 | |
6ef331c8 SC |
5447 | else if (e->name == "$$provider") |
5448 | { | |
5449 | literal_string *myname = new literal_string (provider_name); | |
5450 | myname->tok = e->tok; | |
5451 | provide(myname); | |
5452 | return; | |
5453 | } | |
a794dbeb | 5454 | |
6ef331c8 SC |
5455 | else if (e->name == "$$vars" || e->name == "$$parms") |
5456 | { | |
5457 | e->assert_no_components("sdt", true); | |
6ef331c8 SC |
5458 | |
5459 | // Convert $$vars to sprintf of a list of vars which we recursively evaluate | |
5460 | // NB: we synthesize a new token here rather than reusing | |
5461 | // e->tok, because print_format::print likes to use | |
5462 | // its tok->content. | |
5463 | token* pf_tok = new token(*e->tok); | |
5464 | pf_tok->content = "sprintf"; | |
63ea4244 | 5465 | |
6ef331c8 SC |
5466 | print_format* pf = print_format::create(pf_tok); |
5467 | ||
5468 | for (unsigned i = 1; i <= arg_count; ++i) | |
5469 | { | |
5470 | if (i > 1) | |
5471 | pf->raw_components += " "; | |
5472 | target_symbol *tsym = new target_symbol; | |
5473 | tsym->tok = e->tok; | |
5474 | tsym->name = "$arg" + lex_cast(i); | |
5475 | pf->raw_components += tsym->name; | |
5476 | tsym->components = e->components; | |
5477 | ||
5478 | expression *texp = require (tsym); | |
5479 | if (!e->components.empty() && | |
5480 | e->components[0].type == target_symbol::comp_pretty_print) | |
5481 | pf->raw_components += "=%s"; | |
5482 | else | |
5483 | pf->raw_components += "=%#x"; | |
5484 | pf->args.push_back(texp); | |
5485 | } | |
5486 | ||
5487 | pf->components = print_format::string_to_components(pf->raw_components); | |
5488 | provide (pf); | |
5489 | } | |
5490 | else | |
5491 | assert(0); // shouldn't get here | |
5492 | } | |
5493 | ||
5494 | ||
5495 | void | |
5496 | sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e) | |
5497 | { | |
5498 | try | |
5499 | { | |
8aabf152 | 5500 | unsigned argno = 0; // the N in $argN |
c69a87e0 | 5501 | try |
aff5d390 | 5502 | { |
5ecaa5a7 | 5503 | if (startswith(e->name, "$arg")) |
8aabf152 | 5504 | argno = lex_cast<unsigned>(e->name.substr(4)); |
aff5d390 | 5505 | } |
c69a87e0 | 5506 | catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF |
aff5d390 | 5507 | { |
8aabf152 | 5508 | argno = 0; |
aff5d390 | 5509 | } |
5ecaa5a7 | 5510 | |
8aabf152 FCE |
5511 | if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count |
5512 | argno < 1 || argno > arg_count) // a $argN with out-of-range N | |
aff5d390 | 5513 | { |
8aabf152 FCE |
5514 | // NB: Either |
5515 | // 1) uprobe1_type $argN or $FOO (we don't know the arg_count) | |
5516 | // 2) uprobe2_type $FOO (no probe args) | |
5517 | // both of which get resolved later. | |
73d53dd2 | 5518 | // Throw it now, and it might be resolved by DWARF later. |
8aabf152 | 5519 | need_debug_info = true; |
73d53dd2 | 5520 | throw semantic_error(_("target-symbol requires debuginfo"), e->tok); |
aff5d390 | 5521 | } |
277c21bc | 5522 | |
8aabf152 FCE |
5523 | assert (arg_tokens.size() >= argno); |
5524 | string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0] | |
c57ea854 | 5525 | |
8aabf152 FCE |
5526 | // Now we try to parse this thing, which is an assembler operand |
5527 | // expression. If we can't, we warn, back down to need_debug_info | |
b874bd52 | 5528 | // and hope for the best. Here is the syntax for a few architectures. |
9859b766 | 5529 | // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i. |
8095a157 FCE |
5530 | // |
5531 | // literal reg reg reg + base+index*size+offset | |
b874bd52 | 5532 | // indirect offset |
8095a157 | 5533 | // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) |
b874bd52 SC |
5534 | // power iN R (R) N(R) |
5535 | // ia64 N rR [r16] | |
5536 | // s390 N %rR 0(rR) N(r15) | |
5537 | // arm #N rR [rR] [rR, #N] | |
5538 | ||
8aabf152 FCE |
5539 | expression* argexpr = 0; // filled in in case of successful parse |
5540 | ||
5541 | string percent_regnames; | |
5542 | string regnames; | |
5543 | vector<string> matches; | |
71e5e13d | 5544 | long precision; |
8aabf152 FCE |
5545 | int rc; |
5546 | ||
40fe32e0 SC |
5547 | // Parse the leading length |
5548 | ||
5549 | if (asmarg.find('@') != string::npos) | |
5550 | { | |
5551 | precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@'))); | |
5552 | asmarg = asmarg.substr(asmarg.find('@')+1); | |
5553 | } | |
71e5e13d SC |
5554 | else |
5555 | { | |
5556 | // V1/V2 do not have precision field so default to signed long | |
5557 | // V3 asm does not have precision field so default to unsigned long | |
5558 | if (probe_type == uprobe3_type) | |
5559 | precision = sizeof(long); // this is an asm probe | |
5560 | else | |
5561 | precision = -sizeof(long); | |
5562 | } | |
40fe32e0 | 5563 | |
8aabf152 FCE |
5564 | // test for a numeric literal. |
5565 | // Only accept (signed) decimals throughout. XXX | |
5566 | ||
5567 | // PR11821. NB: on powerpc, literals are not prefixed with $, | |
5568 | // so this regex does not match. But that's OK, since without | |
5569 | // -mregnames, we can't tell them apart from register numbers | |
5570 | // anyway. With -mregnames, we could, if gcc somehow | |
5571 | // communicated to us the presence of that option, but alas it | |
5572 | // doesn't. http://gcc.gnu.org/PR44995. | |
272c9036 | 5573 | rc = regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches); |
8aabf152 FCE |
5574 | if (! rc) |
5575 | { | |
75a371ce JS |
5576 | string sn = matches[0].substr(1); |
5577 | int64_t n; | |
5578 | try | |
5579 | { | |
5580 | // We have to pay attention to the size & sign, as gcc sometimes | |
5581 | // propagates constants that don't quite match, like a negative | |
5582 | // value to fill an unsigned type. | |
5583 | switch (precision) | |
5584 | { | |
5585 | case -1: n = lex_cast< int8_t>(sn); break; | |
5586 | case 1: n = lex_cast< uint8_t>(sn); break; | |
5587 | case -2: n = lex_cast< int16_t>(sn); break; | |
5588 | case 2: n = lex_cast<uint16_t>(sn); break; | |
5589 | case -4: n = lex_cast< int32_t>(sn); break; | |
5590 | case 4: n = lex_cast<uint32_t>(sn); break; | |
5591 | default: | |
5592 | case -8: n = lex_cast< int64_t>(sn); break; | |
5593 | case 8: n = lex_cast<uint64_t>(sn); break; | |
5594 | } | |
5595 | } | |
5596 | catch (std::runtime_error&) | |
5597 | { | |
5598 | goto not_matched; | |
5599 | } | |
5600 | literal_number* ln = new literal_number(n); | |
8aabf152 FCE |
5601 | ln->tok = e->tok; |
5602 | argexpr = ln; | |
5603 | goto matched; | |
5604 | } | |
5605 | ||
14900130 SC |
5606 | if (dwarf_regs.empty()) |
5607 | goto not_matched; | |
d5b83cee | 5608 | |
8aabf152 FCE |
5609 | // Build regex pieces out of the known dwarf_regs. We keep two separate |
5610 | // lists: ones with the % prefix (and thus unambigiuous even despite PR11821), | |
5611 | // and ones with no prefix (and thus only usable in unambiguous contexts). | |
46a94997 | 5612 | for (map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++) |
8aabf152 FCE |
5613 | { |
5614 | string regname = ri->first; | |
5615 | assert (regname != ""); | |
5616 | regnames += string("|")+regname; | |
5617 | if (regname[0]=='%') | |
5618 | percent_regnames += string("|")+regname; | |
5619 | } | |
5620 | // clip off leading | | |
5621 | regnames = regnames.substr(1); | |
272c9036 WF |
5622 | if (percent_regnames != "") |
5623 | percent_regnames = percent_regnames.substr(1); | |
8aabf152 FCE |
5624 | |
5625 | // test for REGISTER | |
5626 | // NB: Because PR11821, we must use percent_regnames here. | |
272c9036 | 5627 | if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM) |
9109f487 SC |
5628 | rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches); |
5629 | else | |
332ba7e7 | 5630 | rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches); |
8aabf152 FCE |
5631 | if (! rc) |
5632 | { | |
5633 | string regname = matches[1]; | |
46a94997 SC |
5634 | map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname); |
5635 | if (ri != dwarf_regs.end()) // known register | |
8aabf152 FCE |
5636 | { |
5637 | embedded_expr *get_arg1 = new embedded_expr; | |
19c22e1f | 5638 | string width_adjust; |
46a94997 | 5639 | switch (ri->second.second) |
19c22e1f | 5640 | { |
892ec39a SC |
5641 | case QI: width_adjust = ") & 0xff)"; break; |
5642 | case QIh: width_adjust = ">>8) & 0xff)"; break; | |
46a94997 | 5643 | case HI: |
71e5e13d | 5644 | // preserve 16 bit register signness |
892ec39a SC |
5645 | width_adjust = ") & 0xffff)"; |
5646 | if (precision < 0) | |
55b377f4 | 5647 | width_adjust += " << 48 >> 48"; |
ac8a78aa SC |
5648 | break; |
5649 | case SI: | |
5650 | // preserve 32 bit register signness | |
892ec39a SC |
5651 | width_adjust = ") & 0xffffffff)"; |
5652 | if (precision < 0) | |
55b377f4 | 5653 | width_adjust += " << 32 >> 32"; |
19c22e1f | 5654 | break; |
892ec39a | 5655 | default: width_adjust = "))"; |
19c22e1f | 5656 | } |
55b377f4 SC |
5657 | string type = ""; |
5658 | if (probe_type == uprobe3_type) | |
5659 | type = (precision < 0 | |
5660 | ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)"; | |
5661 | type = type + "(("; | |
8aabf152 FCE |
5662 | get_arg1->tok = e->tok; |
5663 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
892ec39a | 5664 | + string(" ((int64_t)") + type |
8aabf152 FCE |
5665 | + (is_user_module (process_name) |
5666 | ? string("u_fetch_register(") | |
5667 | : string("k_fetch_register(")) | |
46a94997 | 5668 | + lex_cast(dwarf_regs[regname].first) + string("))") |
19c22e1f | 5669 | + width_adjust; |
8aabf152 FCE |
5670 | argexpr = get_arg1; |
5671 | goto matched; | |
5672 | } | |
5673 | // invalid register name, fall through | |
5674 | } | |
40fe32e0 | 5675 | |
272c9036 | 5676 | int reg, offset1; |
e5b7b83f | 5677 | // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N |
40fe32e0 | 5678 | // NB: Despite PR11821, we can use regnames here, since the parentheses |
e5b7b83f | 5679 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) |
272c9036 WF |
5680 | // On ARM test for [REGISTER, OFFSET] |
5681 | if (elf_machine == EM_ARM) | |
5682 | { | |
5683 | rc = regexp_match (asmarg, string("^\\[(")+regnames+string("), #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?\\]$"), matches); | |
5684 | reg = 1; | |
5685 | offset1 = 2; | |
5686 | } | |
5687 | else | |
5688 | { | |
5689 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string(")[)]$"), matches); | |
5690 | reg = 4; | |
5691 | offset1 = 1; | |
5692 | } | |
8aabf152 FCE |
5693 | if (! rc) |
5694 | { | |
e5b7b83f | 5695 | string regname; |
8aabf152 | 5696 | int64_t disp = 0; |
272c9036 WF |
5697 | if (matches[reg].length()) |
5698 | regname = matches[reg]; | |
8095a157 FCE |
5699 | if (dwarf_regs.find (regname) == dwarf_regs.end()) |
5700 | goto not_matched; | |
5701 | ||
272c9036 | 5702 | for (int i=offset1; i <= (offset1 + 2); i++) |
e5b7b83f SC |
5703 | if (matches[i].length()) |
5704 | try | |
5705 | { | |
5706 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
5707 | } | |
8aabf152 FCE |
5708 | catch (const runtime_error& f) // unparseable offset |
5709 | { | |
5710 | goto not_matched; // can't just 'break' out of | |
5711 | // this case or use a sentinel | |
5712 | // value, unfortunately | |
5713 | } | |
5714 | ||
8aabf152 | 5715 | // synthesize user_long(%{fetch_register(R)%} + D) |
8aabf152 FCE |
5716 | embedded_expr *get_arg1 = new embedded_expr; |
5717 | get_arg1->tok = e->tok; | |
5718 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
5719 | + (is_user_module (process_name) | |
5720 | ? string("u_fetch_register(") | |
5721 | : string("k_fetch_register(")) | |
46a94997 | 5722 | + lex_cast(dwarf_regs[regname].first) + string(")"); |
8aabf152 | 5723 | // XXX: may we ever need to cast that to a narrower type? |
40fe32e0 | 5724 | |
8aabf152 FCE |
5725 | literal_number* inc = new literal_number(disp); |
5726 | inc->tok = e->tok; | |
40fe32e0 | 5727 | |
8aabf152 FCE |
5728 | binary_expression *be = new binary_expression; |
5729 | be->tok = e->tok; | |
5730 | be->left = get_arg1; | |
5731 | be->op = "+"; | |
5732 | be->right = inc; | |
40fe32e0 | 5733 | |
8aabf152 | 5734 | functioncall *fc = new functioncall; |
40fe32e0 SC |
5735 | switch (precision) |
5736 | { | |
7f6ce9ab SC |
5737 | case 1: case -1: |
5738 | fc->function = "user_int8"; break; | |
5739 | case 2: | |
5740 | fc->function = "user_uint16"; break; | |
5741 | case -2: | |
5742 | fc->function = "user_int16"; break; | |
5743 | case 4: | |
5744 | fc->function = "user_uint32"; break; | |
5745 | case -4: | |
5746 | fc->function = "user_int32"; break; | |
5747 | case 8: case -8: | |
5748 | fc->function = "user_int64"; break; | |
40fe32e0 SC |
5749 | default: fc->function = "user_long"; |
5750 | } | |
8aabf152 FCE |
5751 | fc->tok = e->tok; |
5752 | fc->args.push_back(be); | |
366af4e7 | 5753 | |
8aabf152 FCE |
5754 | argexpr = fc; |
5755 | goto matched; | |
5756 | } | |
8095a157 FCE |
5757 | |
5758 | // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N | |
5759 | // NB: Despite PR11821, we can use regnames here, since the parentheses | |
5760 | // make things unambiguous. (Note: gdb/stap-probe.c also parses this) | |
0f7b51d6 | 5761 | rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string("),(")+regnames+string(")(,[1248])?[)]$"), matches); |
8095a157 FCE |
5762 | if (! rc) |
5763 | { | |
5764 | string baseregname; | |
5765 | string indexregname; | |
5766 | int64_t disp = 0; | |
5767 | short scale = 1; | |
5768 | ||
5769 | if (matches[6].length()) | |
5770 | try | |
5771 | { | |
5772 | scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma! | |
5773 | // We could verify that scale is one of 1,2,4,8, | |
5774 | // but it doesn't really matter. An erroneous | |
5775 | // address merely results in run-time errors. | |
8aabf152 | 5776 | } |
8095a157 FCE |
5777 | catch (const runtime_error &f) // unparseable scale |
5778 | { | |
5779 | goto not_matched; | |
5780 | } | |
5781 | ||
5782 | if (matches[4].length()) | |
5783 | baseregname = matches[4]; | |
5784 | if (dwarf_regs.find (baseregname) == dwarf_regs.end()) | |
5785 | goto not_matched; | |
5786 | ||
5787 | if (matches[5].length()) | |
5788 | indexregname = matches[5]; | |
5789 | if (dwarf_regs.find (indexregname) == dwarf_regs.end()) | |
5790 | goto not_matched; | |
5791 | ||
f7719b3d | 5792 | for (int i = 1; i <= 3; i++) // up to three OFFSET terms |
8095a157 FCE |
5793 | if (matches[i].length()) |
5794 | try | |
5795 | { | |
5796 | disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal | |
5797 | } | |
5798 | catch (const runtime_error& f) // unparseable offset | |
5799 | { | |
5800 | goto not_matched; // can't just 'break' out of | |
5801 | // this case or use a sentinel | |
5802 | // value, unfortunately | |
5803 | } | |
5804 | ||
5805 | // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D) | |
5806 | ||
5807 | embedded_expr *get_arg1 = new embedded_expr; | |
5808 | string regfn = is_user_module (process_name) | |
5809 | ? string("u_fetch_register") | |
5810 | : string("k_fetch_register"); // NB: in practice sdt.h probes are for userspace only | |
5811 | ||
5812 | get_arg1->tok = e->tok; | |
5813 | get_arg1->code = string("/* unprivileged */ /* pure */") | |
5814 | + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")") | |
5815 | + string("+(") | |
5816 | + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")") | |
5817 | + string("*") | |
5818 | + lex_cast(scale) | |
5819 | + string(")"); | |
5820 | ||
5821 | // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too | |
5822 | literal_number* inc = new literal_number(disp); | |
5823 | inc->tok = e->tok; | |
5824 | ||
5825 | binary_expression *be = new binary_expression; | |
5826 | be->tok = e->tok; | |
5827 | be->left = get_arg1; | |
5828 | be->op = "+"; | |
5829 | be->right = inc; | |
5830 | ||
5831 | functioncall *fc = new functioncall; | |
5832 | switch (precision) | |
5833 | { | |
5834 | case 1: case -1: | |
5835 | fc->function = "user_int8"; break; | |
5836 | case 2: | |
5837 | fc->function = "user_uint16"; break; | |
5838 | case -2: | |
5839 | fc->function = "user_int16"; break; | |
5840 | case 4: | |
5841 | fc->function = "user_uint32"; break; | |
5842 | case -4: | |
5843 | fc->function = "user_int32"; break; | |
5844 | case 8: case -8: | |
5845 | fc->function = "user_int64"; break; | |
5846 | default: fc->function = "user_long"; | |
5847 | } | |
5848 | fc->tok = e->tok; | |
5849 | fc->args.push_back(be); | |
5850 | ||
5851 | argexpr = fc; | |
5852 | goto matched; | |
8aabf152 FCE |
5853 | } |
5854 | ||
8aabf152 FCE |
5855 | |
5856 | not_matched: | |
5857 | // The asmarg operand was not recognized. Back down to dwarf. | |
5858 | if (! session.suppress_warnings) | |
84fef8ee FCE |
5859 | { |
5860 | if (probe_type == UPROBE3_TYPE) | |
5861 | session.print_warning (_F("Can't parse SDT_V3 operand '%s'", asmarg.c_str()), e->tok); | |
5862 | else // must be *PROBE2; others don't get asm operands | |
5863 | session.print_warning (_F("Downgrading SDT_V2 probe argument to dwarf, can't parse '%s'", | |
5864 | asmarg.c_str()), e->tok); | |
5865 | } | |
8aabf152 FCE |
5866 | assert (argexpr == 0); |
5867 | need_debug_info = true; | |
73d53dd2 | 5868 | throw semantic_error(_("SDT asm not understood, requires debuginfo"), e->tok); |
366af4e7 | 5869 | |
8aabf152 FCE |
5870 | matched: |
5871 | assert (argexpr != 0); | |
366af4e7 RM |
5872 | |
5873 | if (session.verbose > 2) | |
1e41115c | 5874 | //TRANSLATORS: We're mapping the operand to a new expression*. |
b530b5b3 | 5875 | clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl; |
366af4e7 | 5876 | |
aff5d390 | 5877 | if (e->components.empty()) // We have a scalar |
8aabf152 FCE |
5878 | { |
5879 | if (e->addressof) | |
b530b5b3 | 5880 | throw semantic_error(_("cannot take address of sdt variable"), e->tok); |
8aabf152 FCE |
5881 | provide (argexpr); |
5882 | return; | |
5883 | } | |
5884 | else // $var->foo | |
5885 | { | |
5886 | cast_op *cast = new cast_op; | |
5887 | cast->name = "@cast"; | |
5888 | cast->tok = e->tok; | |
5889 | cast->operand = argexpr; | |
5890 | cast->components = e->components; | |
5891 | cast->type_name = probe_name + "_arg" + lex_cast(argno); | |
5892 | cast->module = process_name; | |
5893 | cast->visit(this); | |
5894 | return; | |
5895 | } | |
366af4e7 | 5896 | |
8aabf152 | 5897 | /* NOTREACHED */ |
aff5d390 SC |
5898 | } |
5899 | catch (const semantic_error &er) | |
5900 | { | |
5901 | e->chain (er); | |
5902 | provide (e); | |
5903 | } | |
5904 | } | |
5905 | ||
5906 | ||
6ef331c8 SC |
5907 | void |
5908 | sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e) | |
5909 | { | |
5910 | try | |
5911 | { | |
49131a6d MW |
5912 | assert(e->name.size() > 0 |
5913 | && ((e->name[0] == '$' && e->target_name == "") | |
5914 | || (e->name == "@var" && e->target_name != ""))); | |
6ef331c8 SC |
5915 | |
5916 | if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars") | |
5917 | visit_target_symbol_context (e); | |
5918 | else | |
5919 | visit_target_symbol_arg (e); | |
5920 | } | |
5921 | catch (const semantic_error &er) | |
5922 | { | |
5923 | e->chain (er); | |
5924 | provide (e); | |
5925 | } | |
5926 | } | |
5927 | ||
5928 | ||
40a0c64e JS |
5929 | void |
5930 | sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e) | |
5931 | { | |
5932 | // Fill in our current module context if needed | |
5933 | if (e->module.empty()) | |
5934 | e->module = process_name; | |
5935 | ||
5936 | var_expanding_visitor::visit_cast_op(e); | |
5937 | } | |
5938 | ||
5939 | ||
576eaefe SC |
5940 | void |
5941 | plt_expanding_visitor::visit_target_symbol (target_symbol *e) | |
5942 | { | |
5943 | try | |
5944 | { | |
5945 | if (e->name == "$$name") | |
5946 | { | |
5947 | literal_string *myname = new literal_string (entry); | |
5948 | myname->tok = e->tok; | |
5949 | provide(myname); | |
5950 | return; | |
5951 | } | |
3d69c03f JS |
5952 | |
5953 | // variable not found -> throw a semantic error | |
5954 | // (only to be caught right away, but this may be more complex later...) | |
5955 | string alternatives = "$$name"; | |
5956 | throw semantic_error(_F("unable to find plt variable '%s' (alternatives: %s)", | |
5957 | e->name.c_str(), alternatives.c_str()), e->tok); | |
576eaefe SC |
5958 | } |
5959 | catch (const semantic_error &er) | |
5960 | { | |
5961 | e->chain (er); | |
5962 | provide (e); | |
5963 | } | |
5964 | } | |
5965 | ||
5966 | ||
edce5b67 JS |
5967 | struct sdt_query : public base_query |
5968 | { | |
5969 | sdt_query(probe * base_probe, probe_point * base_loc, | |
5970 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 5971 | vector<derived_probe *> & results, const string user_lib); |
edce5b67 | 5972 | |
51d6bda3 | 5973 | void query_library (const char *data); |
576eaefe | 5974 | void query_plt (const char *entry, size_t addr) {} |
edce5b67 JS |
5975 | void handle_query_module(); |
5976 | ||
5977 | private: | |
15284963 | 5978 | stap_sdt_probe_type probe_type; |
d61ea602 | 5979 | enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc; |
edce5b67 JS |
5980 | probe * base_probe; |
5981 | probe_point * base_loc; | |
6846cfc8 | 5982 | literal_map_t const & params; |
edce5b67 | 5983 | vector<derived_probe *> & results; |
a794dbeb FCE |
5984 | string pp_mark; |
5985 | string pp_provider; | |
51d6bda3 | 5986 | string user_lib; |
edce5b67 JS |
5987 | |
5988 | set<string> probes_handled; | |
5989 | ||
5990 | Elf_Data *pdata; | |
5991 | size_t probe_scn_offset; | |
5992 | size_t probe_scn_addr; | |
aff5d390 | 5993 | uint64_t arg_count; |
40fe32e0 | 5994 | GElf_Addr base; |
c57ea854 | 5995 | GElf_Addr pc; |
aff5d390 | 5996 | string arg_string; |
edce5b67 | 5997 | string probe_name; |
a794dbeb | 5998 | string provider_name; |
79a0ca08 | 5999 | Dwarf_Addr semaphore; |
edce5b67 JS |
6000 | |
6001 | bool init_probe_scn(); | |
6b51ee12 | 6002 | bool get_next_probe(); |
c57ea854 SC |
6003 | void iterate_over_probe_entries(); |
6004 | void handle_probe_entry(); | |
edce5b67 | 6005 | |
40fe32e0 SC |
6006 | static void setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len); |
6007 | void setup_note_probe_entry (int type, const char *data, size_t len); | |
6008 | ||
edce5b67 | 6009 | void convert_probe(probe *base); |
4ddb6dd0 | 6010 | void record_semaphore(vector<derived_probe *> & results, unsigned start); |
c72aa911 | 6011 | probe* convert_location(); |
40fe32e0 | 6012 | bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;} |
c57ea854 SC |
6013 | bool have_debuginfo_uprobe(bool need_debug_info) |
6014 | {return probe_type == uprobe1_type | |
40fe32e0 | 6015 | || ((probe_type == uprobe2_type || probe_type == uprobe3_type) |
c57ea854 | 6016 | && need_debug_info);} |
40fe32e0 | 6017 | bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;} |
edce5b67 JS |
6018 | }; |
6019 | ||
6020 | ||
6021 | sdt_query::sdt_query(probe * base_probe, probe_point * base_loc, | |
6022 | dwflpp & dw, literal_map_t const & params, | |
51d6bda3 | 6023 | vector<derived_probe *> & results, const string user_lib): |
d61ea602 JS |
6024 | base_query(dw, params), probe_type(unknown_probe_type), |
6025 | probe_loc(unknown_section), base_probe(base_probe), | |
74fe61bc LB |
6026 | base_loc(base_loc), params(params), results(results), user_lib(user_lib), |
6027 | probe_scn_offset(0), probe_scn_addr(0), arg_count(0), base(0), pc(0), | |
6028 | semaphore(0) | |
edce5b67 | 6029 | { |
a794dbeb FCE |
6030 | assert(get_string_param(params, TOK_MARK, pp_mark)); |
6031 | get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified | |
6032 | ||
ef428667 FCE |
6033 | // PR10245: permit usage of dtrace-y "-" separator in marker name; |
6034 | // map it to double-underscores. | |
6035 | size_t pos = 0; | |
6036 | while (1) // there may be more than one | |
6037 | { | |
a794dbeb | 6038 | size_t i = pp_mark.find("-", pos); |
ef428667 | 6039 | if (i == string::npos) break; |
a794dbeb | 6040 | pp_mark.replace (i, 1, "__"); |
ef428667 FCE |
6041 | pos = i+1; // resume searching after the inserted __ |
6042 | } | |
a794dbeb FCE |
6043 | |
6044 | // XXX: same for pp_provider? | |
edce5b67 JS |
6045 | } |
6046 | ||
6047 | ||
6048 | void | |
c57ea854 | 6049 | sdt_query::handle_probe_entry() |
edce5b67 | 6050 | { |
c57ea854 SC |
6051 | if (! have_uprobe() |
6052 | && !probes_handled.insert(probe_name).second) | |
edce5b67 JS |
6053 | return; |
6054 | ||
6055 | if (sess.verbose > 3) | |
c57ea854 | 6056 | { |
b530b5b3 LB |
6057 | //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe |
6058 | //TRANSLATORS: is matched to. | |
6059 | clog << _F("matched probe_name %s probe type ", probe_name.c_str()); | |
c57ea854 SC |
6060 | switch (probe_type) |
6061 | { | |
6062 | case uprobe1_type: | |
6063 | clog << "uprobe1 at 0x" << hex << pc << dec << endl; | |
6064 | break; | |
6065 | case uprobe2_type: | |
6066 | clog << "uprobe2 at 0x" << hex << pc << dec << endl; | |
6067 | break; | |
40fe32e0 SC |
6068 | case uprobe3_type: |
6069 | clog << "uprobe3 at 0x" << hex << pc << dec << endl; | |
6070 | break; | |
d61ea602 JS |
6071 | default: |
6072 | clog << "unknown!" << endl; | |
6073 | break; | |
c57ea854 SC |
6074 | } |
6075 | } | |
edce5b67 | 6076 | |
c57ea854 SC |
6077 | // Extend the derivation chain |
6078 | probe *new_base = convert_location(); | |
6079 | probe_point *new_location = new_base->locations[0]; | |
6080 | ||
c57ea854 SC |
6081 | bool need_debug_info = false; |
6082 | ||
7d395255 JS |
6083 | // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...)) |
6084 | // or dwfl_module_getelf(...). We only need it for the machine type, which | |
6085 | // should be the same. The bias is used for relocating debuginfoless probes, | |
6086 | // though, so that must come from the possibly-prelinked ELF file, not DWARF. | |
c57ea854 | 6087 | Dwarf_Addr bias; |
7d395255 | 6088 | Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias); |
c57ea854 | 6089 | |
1cc41cd6 DS |
6090 | /* Figure out the architecture of this particular ELF file. The |
6091 | dwarfless register-name mappings depend on it. */ | |
6092 | GElf_Ehdr ehdr_mem; | |
6093 | GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem); | |
6094 | if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); } | |
d8f31d0a | 6095 | assert(em); |
1cc41cd6 DS |
6096 | int elf_machine = em->e_machine; |
6097 | sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, module_val, | |
6098 | provider_name, probe_name, | |
6099 | probe_type, arg_string, arg_count); | |
6100 | svv.replace (new_base->body); | |
6101 | need_debug_info = svv.need_debug_info; | |
c57ea854 | 6102 | |
1cc41cd6 DS |
6103 | // XXX: why not derive_probes() in the uprobes case too? |
6104 | literal_map_t params; | |
6105 | for (unsigned i = 0; i < new_location->components.size(); ++i) | |
6106 | { | |
6107 | probe_point::component *c = new_location->components[i]; | |
6108 | params[c->functor] = c->arg; | |
6109 | } | |
c57ea854 | 6110 | |
73d53dd2 | 6111 | unsigned prior_results_size = results.size(); |
1cc41cd6 DS |
6112 | dwarf_query q(new_base, new_location, dw, params, results, "", ""); |
6113 | q.has_mark = true; // enables mid-statement probing | |
30263a73 | 6114 | |
73d53dd2 JS |
6115 | // V1 probes always need dwarf info |
6116 | // V2+ probes need dwarf info in case of a variable reference | |
1cc41cd6 DS |
6117 | if (have_debuginfo_uprobe(need_debug_info)) |
6118 | dw.iterate_over_modules(&query_module, &q); | |
73d53dd2 JS |
6119 | |
6120 | // For V2+ probes, if variable references weren't used or failed (PR14369), | |
6121 | // then try with the more direct approach. Unresolved $vars might still | |
6122 | // cause their own error, but this gives them a chance to be optimized out. | |
6123 | if (have_debuginfoless_uprobe() && results.size() == prior_results_size) | |
1cc41cd6 DS |
6124 | { |
6125 | string section; | |
6126 | Dwarf_Addr reloc_addr = q.statement_num_val + bias; | |
6127 | if (dwfl_module_relocations (q.dw.mod_info->mod) > 0) | |
6128 | { | |
6129 | dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr); | |
6130 | section = ".dynamic"; | |
6131 | } | |
6132 | else | |
6133 | section = ".absolute"; | |
edce5b67 | 6134 | |
1cc41cd6 DS |
6135 | uprobe_derived_probe* p = |
6136 | new uprobe_derived_probe ("", "", 0, | |
6137 | path_remove_sysroot(sess,q.module_val), | |
6138 | section, | |
6139 | q.statement_num_val, reloc_addr, q, 0); | |
6140 | p->saveargs (arg_count); | |
6141 | results.push_back (p); | |
c57ea854 | 6142 | } |
487bf4e2 | 6143 | sess.unwindsym_modules.insert (dw.module_name); |
73d53dd2 | 6144 | record_semaphore(results, prior_results_size); |
c57ea854 | 6145 | } |
edce5b67 | 6146 | |
4ddb6dd0 | 6147 | |
c57ea854 SC |
6148 | void |
6149 | sdt_query::handle_query_module() | |
6150 | { | |
6151 | if (!init_probe_scn()) | |
6152 | return; | |
edce5b67 | 6153 | |
c57ea854 SC |
6154 | if (sess.verbose > 3) |
6155 | clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl; | |
edce5b67 | 6156 | |
40fe32e0 SC |
6157 | if (probe_loc == note_section) |
6158 | { | |
6159 | GElf_Shdr shdr_mem; | |
6160 | GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem); | |
6161 | ||
6162 | if (shdr) | |
6163 | base = shdr->sh_addr; | |
6164 | else | |
6165 | base = 0; | |
6166 | dw.iterate_over_notes ((void*) this, &sdt_query::setup_note_probe_entry_callback); | |
6167 | } | |
d61ea602 | 6168 | else if (probe_loc == probe_section) |
40fe32e0 | 6169 | iterate_over_probe_entries (); |
edce5b67 JS |
6170 | } |
6171 | ||
6172 | ||
6173 | bool | |
6174 | sdt_query::init_probe_scn() | |
6175 | { | |
448a86b7 | 6176 | Elf* elf; |
edce5b67 | 6177 | GElf_Shdr shdr_mem; |
40fe32e0 SC |
6178 | |
6179 | GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem); | |
6180 | if (shdr) | |
6181 | { | |
6182 | probe_loc = note_section; | |
6183 | return true; | |
6184 | } | |
edce5b67 | 6185 | |
448a86b7 | 6186 | shdr = dw.get_section (".probes", &shdr_mem, &elf); |
fea74777 | 6187 | if (shdr) |
edce5b67 | 6188 | { |
fea74777 SC |
6189 | pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE); |
6190 | probe_scn_offset = 0; | |
6191 | probe_scn_addr = shdr->sh_addr; | |
6192 | assert (pdata != NULL); | |
6193 | if (sess.verbose > 4) | |
ce0f6648 LB |
6194 | clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: " |
6195 | << pdata->d_size << endl; | |
40fe32e0 | 6196 | probe_loc = probe_section; |
fea74777 | 6197 | return true; |
edce5b67 | 6198 | } |
fea74777 | 6199 | else |
edce5b67 | 6200 | return false; |
edce5b67 JS |
6201 | } |
6202 | ||
40fe32e0 SC |
6203 | void |
6204 | sdt_query::setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len) | |
6205 | { | |
6206 | sdt_query *me = (sdt_query*)object; | |
6207 | me->setup_note_probe_entry (type, data, len); | |
6208 | } | |
6209 | ||
6210 | ||
6211 | void | |
6212 | sdt_query::setup_note_probe_entry (int type, const char *data, size_t len) | |
6213 | { | |
6214 | // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME | |
6215 | // && !memcmp (data->d_buf + name_off, | |
6216 | // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME)) | |
6217 | ||
6218 | // probes are in the .note.stapsdt section | |
6219 | #define _SDT_NOTE_TYPE 3 | |
6220 | if (type != _SDT_NOTE_TYPE) | |
6221 | return; | |
6222 | ||
6223 | union | |
6224 | { | |
6225 | Elf64_Addr a64[3]; | |
6226 | Elf32_Addr a32[3]; | |
6227 | } buf; | |
6228 | Dwarf_Addr bias; | |
6229 | Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias)); | |
6230 | Elf_Data dst = | |
6231 | { | |
6232 | &buf, ELF_T_ADDR, EV_CURRENT, | |
6233 | gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0 | |
6234 | }; | |
6235 | assert (dst.d_size <= sizeof buf); | |
6236 | ||
6237 | if (len < dst.d_size + 3) | |
6238 | return; | |
6239 | ||
6240 | Elf_Data src = | |
6241 | { | |
6242 | (void *) data, ELF_T_ADDR, EV_CURRENT, | |
6243 | dst.d_size, 0, 0 | |
6244 | }; | |
6245 | ||
6246 | if (gelf_xlatetom (elf, &dst, &src, | |
6247 | elf_getident (elf, NULL)[EI_DATA]) == NULL) | |
6248 | printf ("gelf_xlatetom: %s", elf_errmsg (-1)); | |
6249 | ||
6250 | probe_type = uprobe3_type; | |
6251 | const char * provider = data + dst.d_size; | |
3f803f9e | 6252 | |
40fe32e0 | 6253 | const char *name = (const char*)memchr (provider, '\0', data + len - provider); |
3f803f9e CM |
6254 | if(name++ == NULL) |
6255 | return; | |
6256 | ||
6257 | const char *args = (const char*)memchr (name, '\0', data + len - name); | |
6258 | if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1) | |
6259 | return; | |
6260 | ||
6261 | provider_name = provider; | |
6262 | probe_name = name; | |
6263 | arg_string = args; | |
40fe32e0 SC |
6264 | |
6265 | // Did we find a matching probe? | |
6266 | if (! (dw.function_name_matches_pattern (probe_name, pp_mark) | |
6267 | && ((pp_provider == "") | |
6268 | || dw.function_name_matches_pattern (provider_name, pp_provider)))) | |
6269 | return; | |
6270 | ||
e9a90eee JS |
6271 | // PR13934: Assembly probes are not forced to use the N@OP form. |
6272 | // If we have '@' then great, else count based on space-delimiters. | |
6273 | arg_count = count(arg_string.begin(), arg_string.end(), '@'); | |
6274 | if (!arg_count && !arg_string.empty()) | |
6275 | arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' '); | |
6276 | ||
40fe32e0 SC |
6277 | GElf_Addr base_ref; |
6278 | if (gelf_getclass (elf) == ELFCLASS32) | |
6279 | { | |
6280 | pc = buf.a32[0]; | |
6281 | base_ref = buf.a32[1]; | |
6282 | semaphore = buf.a32[2]; | |
6283 | } | |
6284 | else | |
6285 | { | |
6286 | pc = buf.a64[0]; | |
6287 | base_ref = buf.a64[1]; | |
6288 | semaphore = buf.a64[2]; | |
6289 | } | |
6290 | ||
6291 | semaphore += base - base_ref; | |
6292 | pc += base - base_ref; | |
6293 | ||
7d395255 JS |
6294 | // The semaphore also needs the ELF bias added now, so |
6295 | // record_semaphore can properly relocate it later. | |
6296 | semaphore += bias; | |
6297 | ||
40fe32e0 | 6298 | if (sess.verbose > 4) |
b530b5b3 | 6299 | 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 |
6300 | |
6301 | handle_probe_entry(); | |
6302 | } | |
6303 | ||
6304 | ||
c57ea854 SC |
6305 | void |
6306 | sdt_query::iterate_over_probe_entries() | |
edce5b67 | 6307 | { |
c57ea854 | 6308 | // probes are in the .probe section |
edce5b67 JS |
6309 | while (probe_scn_offset < pdata->d_size) |
6310 | { | |
aff5d390 SC |
6311 | stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset); |
6312 | stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset); | |
15284963 | 6313 | probe_type = (stap_sdt_probe_type)(pbe_v1->type_a); |
1cc41cd6 | 6314 | if (! have_uprobe()) |
edce5b67 JS |
6315 | { |
6316 | // Unless this is a mangled .probes section, this happens | |
6317 | // because the name of the probe comes first, followed by | |
6318 | // the sentinel. | |
6319 | if (sess.verbose > 5) | |
b530b5b3 | 6320 | clog << _F("got unknown probe_type : 0x%x", probe_type) << endl; |
edce5b67 JS |
6321 | probe_scn_offset += sizeof(__uint32_t); |
6322 | continue; | |
6323 | } | |
aff5d390 SC |
6324 | if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b |
6325 | { | |
6326 | pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t)); | |
1cc41cd6 | 6327 | if (pbe_v1->type_b != uprobe1_type) |
aff5d390 SC |
6328 | continue; |
6329 | } | |
6330 | ||
1cc41cd6 | 6331 | if (probe_type == uprobe1_type) |
aff5d390 | 6332 | { |
79a0ca08 | 6333 | if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6334 | return; |
79a0ca08 | 6335 | semaphore = 0; |
aff5d390 | 6336 | probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr); |
a794dbeb | 6337 | provider_name = ""; // unknown |
1cc41cd6 DS |
6338 | pc = pbe_v1->arg; |
6339 | arg_count = 0; | |
aff5d390 SC |
6340 | probe_scn_offset += sizeof (stap_sdt_probe_entry_v1); |
6341 | } | |
08b5a50c | 6342 | else if (probe_type == uprobe2_type) |
aff5d390 | 6343 | { |
79a0ca08 | 6344 | if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation |
c57ea854 | 6345 | return; |
79a0ca08 | 6346 | semaphore = pbe_v2->semaphore; |
aff5d390 | 6347 | probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr); |
a794dbeb | 6348 | provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr); |
aff5d390 SC |
6349 | arg_count = pbe_v2->arg_count; |
6350 | pc = pbe_v2->pc; | |
6351 | if (pbe_v2->arg_string) | |
6352 | arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr); | |
79a0ca08 SC |
6353 | // skip over pbe_v2, probe_name text and provider text |
6354 | probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length(); | |
6355 | probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t); | |
aff5d390 | 6356 | } |
edce5b67 | 6357 | if (sess.verbose > 4) |
b530b5b3 | 6358 | clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "") |
aff5d390 | 6359 | << "@0x" << hex << pc << dec << endl; |
edce5b67 | 6360 | |
a794dbeb FCE |
6361 | if (dw.function_name_matches_pattern (probe_name, pp_mark) |
6362 | && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider))) | |
c57ea854 | 6363 | handle_probe_entry (); |
edce5b67 | 6364 | } |
edce5b67 JS |
6365 | } |
6366 | ||
6367 | ||
6846cfc8 | 6368 | void |
4ddb6dd0 | 6369 | sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start) |
6846cfc8 | 6370 | { |
a794dbeb FCE |
6371 | for (unsigned i=0; i<2; i++) { |
6372 | // prefer with-provider symbol; look without provider prefix for backward compatibility only | |
6373 | string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore"; | |
6374 | // XXX: multiple addresses? | |
6375 | if (sess.verbose > 2) | |
b530b5b3 | 6376 | clog << _F("looking for semaphore symbol %s ", semaphore.c_str()); |
a794dbeb | 6377 | |
79a0ca08 SC |
6378 | Dwarf_Addr addr; |
6379 | if (this->semaphore) | |
6380 | addr = this->semaphore; | |
6381 | else | |
6382 | addr = lookup_symbol_address(dw.module, semaphore.c_str()); | |
a794dbeb FCE |
6383 | if (addr) |
6384 | { | |
7d395255 | 6385 | if (dwfl_module_relocations (dw.module) > 0) |
a794dbeb FCE |
6386 | dwfl_module_relocate_address (dw.module, &addr); |
6387 | // XXX: relocation basis? | |
6388 | for (unsigned i = start; i < results.size(); ++i) | |
6389 | results[i]->sdt_semaphore_addr = addr; | |
6390 | if (sess.verbose > 2) | |
b530b5b3 | 6391 | clog << _(", found at 0x") << hex << addr << dec << endl; |
a794dbeb FCE |
6392 | return; |
6393 | } | |
6394 | else | |
6395 | if (sess.verbose > 2) | |
b530b5b3 | 6396 | clog << _(", not found") << endl; |
a794dbeb | 6397 | } |
6846cfc8 SC |
6398 | } |
6399 | ||
6400 | ||
edce5b67 JS |
6401 | void |
6402 | sdt_query::convert_probe (probe *base) | |
6403 | { | |
6404 | block *b = new block; | |
6405 | b->tok = base->body->tok; | |
6406 | ||
edce5b67 JS |
6407 | // Generate: if (arg1 != mark("label")) next; |
6408 | functioncall *fc = new functioncall; | |
bbafcb1e | 6409 | fc->function = "ulong_arg"; |
edce5b67 | 6410 | fc->tok = b->tok; |
bbafcb1e | 6411 | literal_number* num = new literal_number(1); |
edce5b67 JS |
6412 | num->tok = b->tok; |
6413 | fc->args.push_back(num); | |
6414 | ||
6415 | functioncall *fcus = new functioncall; | |
6416 | fcus->function = "user_string"; | |
6417 | fcus->type = pe_string; | |
6418 | fcus->tok = b->tok; | |
6419 | fcus->args.push_back(fc); | |
6420 | ||
6421 | if_statement *is = new if_statement; | |
6422 | is->thenblock = new next_statement; | |
6423 | is->elseblock = NULL; | |
6424 | is->tok = b->tok; | |
63ea4244 | 6425 | is->thenblock->tok = b->tok; |
edce5b67 JS |
6426 | comparison *be = new comparison; |
6427 | be->op = "!="; | |
6428 | be->tok = b->tok; | |
6429 | be->left = fcus; | |
6430 | be->right = new literal_string(probe_name); | |
63ea4244 | 6431 | be->right->tok = b->tok; |
edce5b67 JS |
6432 | is->condition = be; |
6433 | b->statements.push_back(is); | |
6434 | ||
6435 | // Now replace the body | |
6436 | b->statements.push_back(base->body); | |
6437 | base->body = b; | |
6438 | } | |
6439 | ||
6440 | ||
c72aa911 JS |
6441 | probe* |
6442 | sdt_query::convert_location () | |
edce5b67 | 6443 | { |
c72aa911 | 6444 | probe_point* specific_loc = new probe_point(*base_loc); |
662539d9 | 6445 | vector<probe_point::component*> derived_comps; |
edce5b67 | 6446 | |
662539d9 JS |
6447 | vector<probe_point::component*>::iterator it; |
6448 | for (it = specific_loc->components.begin(); | |
6449 | it != specific_loc->components.end(); ++it) | |
6450 | if ((*it)->functor == TOK_PROCESS) | |
6451 | { | |
1cc41cd6 DS |
6452 | // copy the process name |
6453 | derived_comps.push_back(*it); | |
662539d9 JS |
6454 | } |
6455 | else if ((*it)->functor == TOK_LIBRARY) | |
6456 | { | |
1cc41cd6 DS |
6457 | // copy the library name for process probes |
6458 | derived_comps.push_back(*it); | |
662539d9 JS |
6459 | } |
6460 | else if ((*it)->functor == TOK_PROVIDER) | |
6461 | { | |
6462 | // replace the possibly wildcarded arg with the specific provider name | |
6463 | *it = new probe_point::component(TOK_PROVIDER, | |
6464 | new literal_string(provider_name)); | |
6465 | } | |
6466 | else if ((*it)->functor == TOK_MARK) | |
c72aa911 JS |
6467 | { |
6468 | // replace the possibly wildcarded arg with the specific marker name | |
662539d9 JS |
6469 | *it = new probe_point::component(TOK_MARK, |
6470 | new literal_string(probe_name)); | |
a794dbeb | 6471 | |
aff5d390 SC |
6472 | if (sess.verbose > 3) |
6473 | switch (probe_type) | |
6474 | { | |
6475 | case uprobe1_type: | |
b530b5b3 | 6476 | clog << _("probe_type == uprobe1, use statement addr: 0x") |
aff5d390 SC |
6477 | << hex << pc << dec << endl; |
6478 | break; | |
6479 | case uprobe2_type: | |
b530b5b3 | 6480 | clog << _("probe_type == uprobe2, use statement addr: 0x") |
aff5d390 SC |
6481 | << hex << pc << dec << endl; |
6482 | break; | |
40fe32e0 | 6483 | case uprobe3_type: |
b530b5b3 | 6484 | clog << _("probe_type == uprobe3, use statement addr: 0x") |
40fe32e0 SC |
6485 | << hex << pc << dec << endl; |
6486 | break; | |
aff5d390 | 6487 | default: |
b530b5b3 LB |
6488 | clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s", |
6489 | pp_mark.c_str()) << endl; | |
aff5d390 SC |
6490 | } |
6491 | ||
c72aa911 JS |
6492 | switch (probe_type) |
6493 | { | |
aff5d390 SC |
6494 | case uprobe1_type: |
6495 | case uprobe2_type: | |
40fe32e0 | 6496 | case uprobe3_type: |
c72aa911 | 6497 | // process("executable").statement(probe_arg) |
662539d9 JS |
6498 | derived_comps.push_back |
6499 | (new probe_point::component(TOK_STATEMENT, | |
6500 | new literal_number(pc, true))); | |
c72aa911 JS |
6501 | break; |
6502 | ||
a794dbeb | 6503 | default: // deprecated |
c72aa911 | 6504 | // process("executable").function("*").label("_stapprobe1_MARK_NAME") |
662539d9 JS |
6505 | derived_comps.push_back |
6506 | (new probe_point::component(TOK_FUNCTION, | |
6507 | new literal_string("*"))); | |
6508 | derived_comps.push_back | |
c72aa911 | 6509 | (new probe_point::component(TOK_LABEL, |
a794dbeb | 6510 | new literal_string("_stapprobe1_" + pp_mark))); |
c72aa911 JS |
6511 | break; |
6512 | } | |
6513 | } | |
edce5b67 | 6514 | |
662539d9 JS |
6515 | probe_point* derived_loc = new probe_point(*specific_loc); |
6516 | derived_loc->components = derived_comps; | |
c72aa911 | 6517 | return base_probe->create_alias(derived_loc, specific_loc); |
edce5b67 JS |
6518 | } |
6519 | ||
6520 | ||
51d6bda3 SC |
6521 | void |
6522 | sdt_query::query_library (const char *library) | |
6523 | { | |
6524 | query_one_library (library, dw, user_lib, base_probe, base_loc, results); | |
6525 | } | |
6526 | ||
6527 | ||
20c6c071 | 6528 | void |
5227f1ea | 6529 | dwarf_builder::build(systemtap_session & sess, |
7a053d3b | 6530 | probe * base, |
20c6c071 | 6531 | probe_point * location, |
86bf665e | 6532 | literal_map_t const & parameters, |
20c6c071 GH |
6533 | vector<derived_probe *> & finished_results) |
6534 | { | |
b20febf3 FCE |
6535 | // NB: the kernel/user dwlfpp objects are long-lived. |
6536 | // XXX: but they should be per-session, as this builder object | |
6537 | // may be reused if we try to cross-instrument multiple targets. | |
84048984 | 6538 | |
7a24d422 | 6539 | dwflpp* dw = 0; |
6d5d594e | 6540 | literal_map_t filled_parameters = parameters; |
7a24d422 | 6541 | |
7a24d422 | 6542 | string module_name; |
ae2552da FCE |
6543 | if (has_null_param (parameters, TOK_KERNEL)) |
6544 | { | |
6545 | dw = get_kern_dw(sess, "kernel"); | |
6546 | } | |
6547 | else if (get_param (parameters, TOK_MODULE, module_name)) | |
b8da0ad1 | 6548 | { |
c523a015 LB |
6549 | size_t dash_pos = 0; |
6550 | while((dash_pos=module_name.find('-'))!=string::npos) | |
6551 | module_name.replace(int(dash_pos),1,"_"); | |
6552 | filled_parameters[TOK_MODULE] = new literal_string(module_name); | |
37001baa FCE |
6553 | // NB: glob patterns get expanded later, during the offline |
6554 | // elfutils module listing. | |
ae2552da | 6555 | dw = get_kern_dw(sess, module_name); |
b8da0ad1 | 6556 | } |
6d5d594e | 6557 | else if (get_param (parameters, TOK_PROCESS, module_name) || has_null_param(parameters, TOK_PROCESS)) |
84c84ac4 | 6558 | { |
05fb3e0c | 6559 | module_name = sess.sysroot + module_name; |
6d5d594e LB |
6560 | if(has_null_param(filled_parameters, TOK_PROCESS)) |
6561 | { | |
6562 | wordexp_t words; | |
6563 | int rc = wordexp(sess.cmd.c_str(), &words, WRDE_NOCMD|WRDE_UNDEF); | |
6564 | if(rc || words.we_wordc <= 0) | |
6565 | throw semantic_error(_("unspecified process probe is invalid without a -c COMMAND")); | |
05fb3e0c | 6566 | module_name = sess.sysroot + words.we_wordv[0]; |
6d5d594e LB |
6567 | filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map |
6568 | // in the case of TOK_MARK we need to modify locations as well | |
6569 | if(location->components[0]->functor==TOK_PROCESS && | |
6570 | location->components[0]->arg == 0) | |
6571 | location->components[0]->arg = new literal_string(module_name); | |
6572 | wordfree (& words); | |
6573 | } | |
5750ecc6 | 6574 | |
37001baa FCE |
6575 | // PR6456 process("/bin/*") glob handling |
6576 | if (contains_glob_chars (module_name)) | |
6577 | { | |
6578 | // Expand glob via rewriting the probe-point process("....") | |
6579 | // parameter, asserted to be the first one. | |
6580 | ||
6581 | assert (location->components.size() > 0); | |
6582 | assert (location->components[0]->functor == TOK_PROCESS); | |
6583 | assert (location->components[0]->arg); | |
6584 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
6585 | assert (lit); | |
6586 | ||
6587 | // Evaluate glob here, and call derive_probes recursively with each match. | |
6588 | glob_t the_blob; | |
6589 | int rc = glob (module_name.c_str(), 0, NULL, & the_blob); | |
b530b5b3 LB |
6590 | if (rc) |
6591 | throw semantic_error (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() )); | |
37001baa FCE |
6592 | for (unsigned i = 0; i < the_blob.gl_pathc; ++i) |
6593 | { | |
e19ebcf7 | 6594 | assert_no_interrupts(); |
37001baa FCE |
6595 | |
6596 | const char* globbed = the_blob.gl_pathv[i]; | |
6597 | struct stat st; | |
6598 | ||
6599 | if (access (globbed, X_OK) == 0 | |
6600 | && stat (globbed, &st) == 0 | |
6601 | && S_ISREG (st.st_mode)) // see find_executable() | |
6602 | { | |
7977a734 FCE |
6603 | // Need to call canonicalize here, in order to path-expand |
6604 | // patterns like process("stap*"). Otherwise it may go through | |
6605 | // to the next round of expansion as ("stap"), leading to a $PATH | |
6606 | // search that's not consistent with the glob search already done. | |
6607 | ||
6608 | char *cf = canonicalize_file_name (globbed); | |
6609 | if (cf) globbed = cf; | |
6610 | ||
37001baa FCE |
6611 | // synthesize a new probe_point, with the glob-expanded string |
6612 | probe_point *pp = new probe_point (*location); | |
5750ecc6 FCE |
6613 | // PR13338: quote results to prevent recursion |
6614 | string eglobbed = escape_glob_chars (globbed); | |
6615 | ||
6616 | if (sess.verbose > 1) | |
6617 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", | |
6618 | module_name.c_str(), eglobbed.c_str()) << endl; | |
05fb3e0c | 6619 | string eglobbed_tgt = path_remove_sysroot(sess, eglobbed); |
5750ecc6 | 6620 | |
37001baa | 6621 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 6622 | new literal_string (eglobbed_tgt)); |
37001baa FCE |
6623 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
6624 | pp->components[0] = ppc; | |
6625 | ||
7977a734 FCE |
6626 | probe* new_probe = new probe (*base, pp); |
6627 | ||
6628 | // We override "optional = true" here, as if the | |
6629 | // wildcarded probe point was given a "?" suffix. | |
6630 | ||
6631 | // This is because wildcard probes will be expected | |
6632 | // by users to apply only to some subset of the | |
6633 | // matching binaries, in the sense of "any", rather | |
6634 | // than "all", sort of similarly how | |
6635 | // module("*").function("...") patterns work. | |
6636 | ||
6637 | derive_probes (sess, new_probe, finished_results, | |
6638 | true /* NB: not location->optional */ ); | |
37001baa FCE |
6639 | } |
6640 | } | |
6641 | ||
6642 | globfree (& the_blob); | |
6643 | return; // avoid falling through | |
6644 | } | |
6645 | ||
5750ecc6 FCE |
6646 | // PR13338: unquote glob results |
6647 | module_name = unescape_glob_chars (module_name); | |
05fb3e0c | 6648 | user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it |
d1bcbe71 RH |
6649 | |
6650 | // if the executable starts with "#!", we look for the interpreter of the script | |
6651 | { | |
6652 | ifstream script_file (user_path.c_str () ); | |
6653 | ||
6654 | if (script_file.good ()) | |
6655 | { | |
6656 | string line; | |
6657 | ||
6658 | getline (script_file, line); | |
6659 | ||
6660 | if (line.compare (0, 2, "#!") == 0) | |
6661 | { | |
6662 | string path_head = line.substr(2); | |
6663 | ||
6664 | // remove white spaces at the beginning of the string | |
6665 | size_t p2 = path_head.find_first_not_of(" \t"); | |
6666 | ||
6667 | if (p2 != string::npos) | |
6668 | { | |
6669 | string path = path_head.substr(p2); | |
6670 | ||
6671 | // remove white spaces at the end of the string | |
6672 | p2 = path.find_last_not_of(" \t\n"); | |
6673 | if (string::npos != p2) | |
6674 | path.erase(p2+1); | |
6675 | ||
8e13c1a1 RH |
6676 | // handle "#!/usr/bin/env" redirect |
6677 | size_t offset = 0; | |
6678 | if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0) | |
6679 | { | |
6680 | offset = sizeof("/bin/env")-1; | |
6681 | } | |
6682 | else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0) | |
6683 | { | |
6684 | offset = sizeof("/usr/bin/env")-1; | |
6685 | } | |
6686 | ||
6687 | if (offset != 0) | |
6688 | { | |
6689 | size_t p3 = path.find_first_not_of(" \t", offset); | |
6690 | ||
6691 | if (p3 != string::npos) | |
6692 | { | |
6693 | string env_path = path.substr(p3); | |
05fb3e0c WF |
6694 | user_path = find_executable (env_path, sess.sysroot, |
6695 | sess.sysenv); | |
8e13c1a1 RH |
6696 | } |
6697 | } | |
6698 | else | |
6699 | { | |
05fb3e0c | 6700 | user_path = find_executable (path, sess.sysroot, sess.sysenv); |
8e13c1a1 | 6701 | } |
d1bcbe71 RH |
6702 | |
6703 | struct stat st; | |
6704 | ||
6705 | if (access (user_path.c_str(), X_OK) == 0 | |
6706 | && stat (user_path.c_str(), &st) == 0 | |
6707 | && S_ISREG (st.st_mode)) // see find_executable() | |
6708 | { | |
6709 | if (sess.verbose > 1) | |
b530b5b3 LB |
6710 | clog << _F("Expanded process(\"%s\") to process(\"%s\")", |
6711 | module_name.c_str(), user_path.c_str()) << endl; | |
d1bcbe71 RH |
6712 | |
6713 | assert (location->components.size() > 0); | |
6714 | assert (location->components[0]->functor == TOK_PROCESS); | |
6715 | assert (location->components[0]->arg); | |
6716 | literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg); | |
6717 | assert (lit); | |
6718 | ||
6719 | // synthesize a new probe_point, with the expanded string | |
6720 | probe_point *pp = new probe_point (*location); | |
05fb3e0c | 6721 | string user_path_tgt = path_remove_sysroot(sess, user_path); |
d1bcbe71 | 6722 | probe_point::component* ppc = new probe_point::component (TOK_PROCESS, |
05fb3e0c | 6723 | new literal_string (user_path_tgt.c_str())); |
d1bcbe71 RH |
6724 | ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above |
6725 | pp->components[0] = ppc; | |
6726 | ||
6727 | probe* new_probe = new probe (*base, pp); | |
6728 | ||
6729 | derive_probes (sess, new_probe, finished_results); | |
6730 | ||
6731 | script_file.close(); | |
6732 | return; | |
6733 | } | |
6734 | } | |
6735 | } | |
6736 | } | |
6737 | script_file.close(); | |
6738 | } | |
6739 | ||
47e226ed | 6740 | if (get_param (parameters, TOK_LIBRARY, user_lib) |
378d78b5 | 6741 | && user_lib.length() && ! contains_glob_chars (user_lib)) |
47e226ed SC |
6742 | { |
6743 | module_name = find_executable (user_lib, sess.sysroot, sess.sysenv, | |
6744 | "LD_LIBRARY_PATH"); | |
6745 | if (module_name.find('/') == string::npos) | |
6746 | // We didn't find user_lib so use iterate_over_libraries | |
6747 | module_name = user_path; | |
6748 | } | |
63b4fd14 | 6749 | else |
b642c901 | 6750 | module_name = user_path; // canonicalize it |
d0a7f5a9 | 6751 | |
3667d615 | 6752 | // uretprobes aren't available everywhere |
536f1261 | 6753 | if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p()) |
2b69faaf | 6754 | { |
3667d615 JS |
6755 | if (kernel_supports_inode_uprobes(sess) && |
6756 | !kernel_supports_inode_uretprobes(sess)) | |
2b69faaf JS |
6757 | throw semantic_error |
6758 | (_("process return probes not available with inode-based uprobes")); | |
6759 | } | |
3667d615 | 6760 | |
f4000852 MW |
6761 | // There is a similar check in pass 4 (buildrun), but it is |
6762 | // needed here too to make sure alternatives for optional | |
6763 | // (? or !) process probes are disposed and/or alternatives | |
6764 | // are selected. | |
3667d615 JS |
6765 | if (!sess.runtime_usermode_p()) |
6766 | check_process_probe_kernel_support(sess); | |
e34d5d13 | 6767 | |
7a24d422 FCE |
6768 | // user-space target; we use one dwflpp instance per module name |
6769 | // (= program or shared library) | |
707bf35e | 6770 | dw = get_user_dw(sess, module_name); |
c8959a29 | 6771 | } |
20c6c071 | 6772 | |
5896cd05 | 6773 | if (sess.verbose > 3) |
b530b5b3 | 6774 | clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl; |
5896cd05 | 6775 | |
a794dbeb FCE |
6776 | string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __ |
6777 | if (get_param(parameters, TOK_MARK, dummy_mark_name)) | |
f28a8c28 | 6778 | { |
d8f31d0a | 6779 | assert(dw); |
51d6bda3 | 6780 | sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib); |
edce5b67 JS |
6781 | dw->iterate_over_modules(&query_module, &sdtq); |
6782 | return; | |
7a05f484 | 6783 | } |
20c6c071 | 6784 | |
8f14e444 | 6785 | unsigned results_pre = finished_results.size(); |
6d5d594e | 6786 | dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib); |
7a24d422 FCE |
6787 | |
6788 | // XXX: kernel.statement.absolute is a special case that requires no | |
6789 | // dwfl processing. This code should be in a separate builder. | |
7a24d422 | 6790 | if (q.has_kernel && q.has_absolute) |
37ebca01 | 6791 | { |
4baf0e53 | 6792 | // assert guru mode for absolute probes |
37ebca01 FCE |
6793 | if (! q.base_probe->privileged) |
6794 | { | |
e3bbc038 | 6795 | throw semantic_error (_("absolute statement probe in unprivileged script; need stap -g"), |
edce5b67 | 6796 | q.base_probe->tok); |
37ebca01 FCE |
6797 | } |
6798 | ||
6799 | // For kernel.statement(NUM).absolute probe points, we bypass | |
6800 | // all the debuginfo stuff: We just wire up a | |
6801 | // dwarf_derived_probe right here and now. | |
4baf0e53 | 6802 | dwarf_derived_probe* p = |
b8da0ad1 FCE |
6803 | new dwarf_derived_probe ("", "", 0, "kernel", "", |
6804 | q.statement_num_val, q.statement_num_val, | |
6805 | q, 0); | |
37ebca01 | 6806 | finished_results.push_back (p); |
1a0dbc5a | 6807 | sess.unwindsym_modules.insert ("kernel"); |
37ebca01 FCE |
6808 | return; |
6809 | } | |
6810 | ||
51178501 | 6811 | dw->iterate_over_modules(&query_module, &q); |
8f14e444 FCE |
6812 | |
6813 | ||
6814 | // PR11553 special processing: .return probes requested, but | |
6815 | // some inlined function instances matched. | |
6816 | unsigned i_n_r = q.inlined_non_returnable.size(); | |
6817 | unsigned results_post = finished_results.size(); | |
6818 | if (i_n_r > 0) | |
6819 | { | |
6820 | if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning | |
6821 | { | |
6822 | string quicklist; | |
6823 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
6824 | it != q.inlined_non_returnable.end(); | |
6825 | it++) | |
6826 | { | |
6827 | quicklist += " " + (*it); | |
6828 | if (quicklist.size() > 80) // heuristic, don't make an overlong report line | |
6829 | { | |
6830 | quicklist += " ..."; | |
6831 | break; | |
6832 | } | |
6833 | } | |
c57ea854 | 6834 | |
b530b5b3 LB |
6835 | sess.print_warning (_F(ngettext("cannot probe .return of %u inlined function %s", |
6836 | "cannot probe .return of %u inlined functions %s", | |
6837 | quicklist.size()), i_n_r, quicklist.c_str())); | |
8f14e444 FCE |
6838 | // There will be also a "no matches" semantic error generated. |
6839 | } | |
6840 | if (sess.verbose > 1) | |
b530b5b3 LB |
6841 | clog << _F(ngettext("skipped .return probe of %u inlined function", |
6842 | "skipped .return probe of %u inlined functions", i_n_r), i_n_r) << endl; | |
8f14e444 FCE |
6843 | if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity |
6844 | { | |
6845 | for (set<string>::iterator it = q.inlined_non_returnable.begin(); | |
6846 | it != q.inlined_non_returnable.end(); | |
6847 | it++) | |
6848 | clog << (*it) << " "; | |
6849 | clog << endl; | |
6850 | } | |
6851 | } // i_n_r > 0 | |
5f0a03a6 JK |
6852 | } |
6853 | ||
6854 | symbol_table::~symbol_table() | |
6855 | { | |
c9efa5c9 | 6856 | delete_map(map_by_addr); |
5f0a03a6 JK |
6857 | } |
6858 | ||
6859 | void | |
2867a2a1 | 6860 | symbol_table::add_symbol(const char *name, bool weak, bool descriptor, |
822a6a3d | 6861 | Dwarf_Addr addr, Dwarf_Addr */*high_addr*/) |
5f0a03a6 | 6862 | { |
ab91b232 JK |
6863 | #ifdef __powerpc__ |
6864 | // Map ".sys_foo" to "sys_foo". | |
6865 | if (name[0] == '.') | |
6866 | name++; | |
6867 | #endif | |
5f0a03a6 JK |
6868 | func_info *fi = new func_info(); |
6869 | fi->addr = addr; | |
6870 | fi->name = name; | |
ab91b232 | 6871 | fi->weak = weak; |
2867a2a1 | 6872 | fi->descriptor = descriptor; |
5f0a03a6 JK |
6873 | map_by_name[fi->name] = fi; |
6874 | // TODO: Use a multimap in case there are multiple static | |
6875 | // functions with the same name? | |
1c6b77e5 | 6876 | map_by_addr.insert(make_pair(addr, fi)); |
5f0a03a6 JK |
6877 | } |
6878 | ||
6879 | enum info_status | |
6880 | symbol_table::read_symbols(FILE *f, const string& path) | |
6881 | { | |
6882 | // Based on do_kernel_symbols() in runtime/staprun/symbols.c | |
6883 | int ret; | |
2e67a43b TM |
6884 | char *name = 0; |
6885 | char *mod = 0; | |
5f0a03a6 JK |
6886 | char type; |
6887 | unsigned long long addr; | |
6888 | Dwarf_Addr high_addr = 0; | |
6889 | int line = 0; | |
6890 | ||
62a4021d FCE |
6891 | #if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7) |
6892 | #define MS_FMT "%ms" | |
6893 | #else | |
6894 | #define MS_FMT "%as" | |
6895 | #endif | |
6896 | // %ms (newer than %as) mallocs space for the string and stores its address. | |
6897 | while ((ret = fscanf(f, "%llx %c " MS_FMT " [" MS_FMT, &addr, &type, &name, &mod)) > 0) | |
5f0a03a6 | 6898 | { |
2e67a43b TM |
6899 | auto_free free_name(name); |
6900 | auto_free free_mod(mod); | |
5f0a03a6 JK |
6901 | line++; |
6902 | if (ret < 3) | |
6903 | { | |
3d372d6b | 6904 | cerr << _F("Symbol table error: Line %d of symbol list from %s is not in correct format: address type name [module]\n", |
b530b5b3 | 6905 | line, path.c_str()); |
5f0a03a6 JK |
6906 | // Caller should delete symbol_table object. |
6907 | return info_absent; | |
6908 | } | |
2e67a43b | 6909 | else if (ret > 3) |
5f0a03a6 JK |
6910 | { |
6911 | // Modules are loaded above the kernel, so if we're getting | |
6912 | // modules, we're done. | |
2e67a43b | 6913 | break; |
5f0a03a6 | 6914 | } |
ab91b232 | 6915 | if (type == 'T' || type == 't' || type == 'W') |
2867a2a1 | 6916 | add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr); |
5f0a03a6 JK |
6917 | } |
6918 | ||
1c6b77e5 | 6919 | if (map_by_addr.size() < 1) |
5f0a03a6 | 6920 | { |
3d372d6b | 6921 | cerr << _F("Symbol table error: %s contains no function symbols.\n", |
b530b5b3 | 6922 | path.c_str()) << endl; |
5f0a03a6 JK |
6923 | return info_absent; |
6924 | } | |
6925 | return info_present; | |
6926 | } | |
6927 | ||
6928 | // NB: This currently unused. We use get_from_elf() instead because | |
6929 | // that gives us raw addresses -- which we need for modules -- whereas | |
6930 | // nm provides the address relative to the beginning of the section. | |
6931 | enum info_status | |
83ca3872 | 6932 | symbol_table::read_from_elf_file(const string &path, |
2713ea24 | 6933 | systemtap_session &sess) |
5f0a03a6 | 6934 | { |
58502ae4 JS |
6935 | vector<string> cmd; |
6936 | cmd.push_back("/usr/bin/nm"); | |
6937 | cmd.push_back("-n"); | |
6938 | cmd.push_back("--defined-only"); | |
6939 | cmd.push_back("path"); | |
6940 | ||
5f0a03a6 | 6941 | FILE *f; |
58502ae4 JS |
6942 | int child_fd; |
6943 | pid_t child = stap_spawn_piped(sess.verbose, cmd, NULL, &child_fd); | |
6944 | if (child <= 0 || !(f = fdopen(child_fd, "r"))) | |
5f0a03a6 | 6945 | { |
58502ae4 | 6946 | // nm failures are detected by stap_waitpid |
3d372d6b | 6947 | cerr << _F("Internal error reading symbol table from %s -- %s\n", |
b530b5b3 | 6948 | path.c_str(), strerror(errno)); |
5f0a03a6 JK |
6949 | return info_absent; |
6950 | } | |
6951 | enum info_status status = read_symbols(f, path); | |
58502ae4 | 6952 | if (fclose(f) || stap_waitpid(sess.verbose, child)) |
5f0a03a6 | 6953 | { |
2713ea24 CM |
6954 | if (status == info_present) |
6955 | sess.print_warning("nm cannot read symbol table from " + path); | |
5f0a03a6 JK |
6956 | return info_absent; |
6957 | } | |
6958 | return status; | |
6959 | } | |
6960 | ||
6961 | enum info_status | |
83ca3872 | 6962 | symbol_table::read_from_text_file(const string& path, |
2713ea24 | 6963 | systemtap_session &sess) |
5f0a03a6 JK |
6964 | { |
6965 | FILE *f = fopen(path.c_str(), "r"); | |
6966 | if (!f) | |
6967 | { | |
2713ea24 | 6968 | sess.print_warning("cannot read symbol table from " + path + " -- " + strerror(errno)); |
5f0a03a6 JK |
6969 | return info_absent; |
6970 | } | |
6971 | enum info_status status = read_symbols(f, path); | |
6972 | (void) fclose(f); | |
6973 | return status; | |
6974 | } | |
6975 | ||
46f7b6be | 6976 | void |
f98c6346 | 6977 | symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused))) |
46f7b6be JK |
6978 | { |
6979 | #ifdef __powerpc__ | |
6980 | /* | |
6981 | * The .opd section contains function descriptors that can look | |
6982 | * just like function entry points. For example, there's a function | |
6983 | * descriptor called "do_exit" that links to the entry point ".do_exit". | |
6984 | * Reject all symbols in .opd. | |
6985 | */ | |
6986 | opd_section = SHN_UNDEF; | |
6987 | Dwarf_Addr bias; | |
6988 | Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias)) | |
6989 | ?: dwfl_module_getelf (mod, &bias)); | |
6990 | Elf_Scn* scn = 0; | |
6991 | size_t shstrndx; | |
6992 | ||
6993 | if (!elf) | |
6994 | return; | |
fcc30d6d | 6995 | if (elf_getshdrstrndx (elf, &shstrndx) != 0) |
46f7b6be JK |
6996 | return; |
6997 | while ((scn = elf_nextscn(elf, scn)) != NULL) | |
6998 | { | |
6999 | GElf_Shdr shdr_mem; | |
7000 | GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem); | |
7001 | if (!shdr) | |
7002 | continue; | |
7003 | const char *name = elf_strptr(elf, shstrndx, shdr->sh_name); | |
7004 | if (!strcmp(name, ".opd")) | |
7005 | { | |
7006 | opd_section = elf_ndxscn(scn); | |
7007 | return; | |
7008 | } | |
7009 | } | |
7010 | #endif | |
7011 | } | |
7012 | ||
7013 | bool | |
7014 | symbol_table::reject_section(GElf_Word section) | |
7015 | { | |
7016 | if (section == SHN_UNDEF) | |
7017 | return true; | |
7018 | #ifdef __powerpc__ | |
7019 | if (section == opd_section) | |
7020 | return true; | |
7021 | #endif | |
7022 | return false; | |
7023 | } | |
7024 | ||
5f0a03a6 JK |
7025 | enum info_status |
7026 | symbol_table::get_from_elf() | |
7027 | { | |
7028 | Dwarf_Addr high_addr = 0; | |
7029 | Dwfl_Module *mod = mod_info->mod; | |
7030 | int syments = dwfl_module_getsymtab(mod); | |
7031 | assert(syments); | |
46f7b6be | 7032 | prepare_section_rejection(mod); |
5f0a03a6 JK |
7033 | for (int i = 1; i < syments; ++i) |
7034 | { | |
7035 | GElf_Sym sym; | |
ab91b232 JK |
7036 | GElf_Word section; |
7037 | const char *name = dwfl_module_getsym(mod, i, &sym, §ion); | |
2867a2a1 | 7038 | if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC) |
ab91b232 | 7039 | add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK), |
2867a2a1 | 7040 | reject_section(section), sym.st_value, &high_addr); |
5f0a03a6 JK |
7041 | } |
7042 | return info_present; | |
7043 | } | |
7044 | ||
5f0a03a6 JK |
7045 | func_info * |
7046 | symbol_table::get_func_containing_address(Dwarf_Addr addr) | |
7047 | { | |
1c6b77e5 JS |
7048 | iterator_t iter = map_by_addr.upper_bound(addr); |
7049 | if (iter == map_by_addr.begin()) | |
5f0a03a6 | 7050 | return NULL; |
2e67a43b | 7051 | else |
1c6b77e5 | 7052 | return (--iter)->second; |
5f0a03a6 JK |
7053 | } |
7054 | ||
3d372d6b SC |
7055 | func_info * |
7056 | symbol_table::get_first_func() | |
7057 | { | |
7058 | iterator_t iter = map_by_addr.begin(); | |
7059 | return (iter)->second; | |
7060 | } | |
7061 | ||
5f0a03a6 JK |
7062 | func_info * |
7063 | symbol_table::lookup_symbol(const string& name) | |
7064 | { | |
7065 | map<string, func_info*>::iterator i = map_by_name.find(name); | |
7066 | if (i == map_by_name.end()) | |
7067 | return NULL; | |
7068 | return i->second; | |
7069 | } | |
7070 | ||
7071 | Dwarf_Addr | |
7072 | symbol_table::lookup_symbol_address(const string& name) | |
7073 | { | |
7074 | func_info *fi = lookup_symbol(name); | |
7075 | if (fi) | |
7076 | return fi->addr; | |
7077 | return 0; | |
7078 | } | |
7079 | ||
ab91b232 JK |
7080 | // This is the kernel symbol table. The kernel macro cond_syscall creates |
7081 | // a weak symbol for each system call and maps it to sys_ni_syscall. | |
7082 | // For system calls not implemented elsewhere, this weak symbol shows up | |
7083 | // in the kernel symbol table. Following the precedent of dwarfful stap, | |
7084 | // we refuse to consider such symbols. Here we delete them from our | |
7085 | // symbol table. | |
7086 | // TODO: Consider generalizing this and/or making it part of blacklist | |
7087 | // processing. | |
7088 | void | |
7089 | symbol_table::purge_syscall_stubs() | |
7090 | { | |
7091 | Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall"); | |
7092 | if (stub_addr == 0) | |
7093 | return; | |
1c6b77e5 | 7094 | range_t purge_range = map_by_addr.equal_range(stub_addr); |
2e67a43b TM |
7095 | for (iterator_t iter = purge_range.first; |
7096 | iter != purge_range.second; | |
1c6b77e5 | 7097 | ) |
ab91b232 | 7098 | { |
1c6b77e5 | 7099 | func_info *fi = iter->second; |
2e67a43b | 7100 | if (fi->weak && fi->name != "sys_ni_syscall") |
ab91b232 | 7101 | { |
2e67a43b | 7102 | map_by_name.erase(fi->name); |
1c6b77e5 | 7103 | map_by_addr.erase(iter++); |
2e67a43b | 7104 | delete fi; |
2e67a43b | 7105 | } |
1c6b77e5 JS |
7106 | else |
7107 | iter++; | |
ab91b232 JK |
7108 | } |
7109 | } | |
7110 | ||
5f0a03a6 JK |
7111 | void |
7112 | module_info::get_symtab(dwarf_query *q) | |
7113 | { | |
1c6b77e5 JS |
7114 | if (symtab_status != info_unknown) |
7115 | return; | |
7116 | ||
5f0a03a6 JK |
7117 | sym_table = new symbol_table(this); |
7118 | if (!elf_path.empty()) | |
7119 | { | |
5f0a03a6 JK |
7120 | symtab_status = sym_table->get_from_elf(); |
7121 | } | |
7122 | else | |
7123 | { | |
7124 | assert(name == TOK_KERNEL); | |
ab3ed72d DS |
7125 | symtab_status = info_absent; |
7126 | cerr << _("Error: Cannot find vmlinux.") << endl;; | |
5f0a03a6 JK |
7127 | } |
7128 | if (symtab_status == info_absent) | |
7129 | { | |
7130 | delete sym_table; | |
7131 | sym_table = NULL; | |
7132 | return; | |
7133 | } | |
7134 | ||
ab91b232 JK |
7135 | if (name == TOK_KERNEL) |
7136 | sym_table->purge_syscall_stubs(); | |
5f0a03a6 JK |
7137 | } |
7138 | ||
1c6b77e5 JS |
7139 | // update_symtab reconciles data between the elf symbol table and the dwarf |
7140 | // function enumeration. It updates the symbol table entries with the dwarf | |
7141 | // die that describes the function, which also signals to query_module_symtab | |
7142 | // that a statement probe isn't needed. In return, it also adds aliases to the | |
7143 | // function table for names that share the same addr/die. | |
7144 | void | |
7145 | module_info::update_symtab(cu_function_cache_t *funcs) | |
7146 | { | |
7147 | if (!sym_table) | |
7148 | return; | |
7149 | ||
7150 | cu_function_cache_t new_funcs; | |
7151 | ||
7152 | for (cu_function_cache_t::iterator func = funcs->begin(); | |
7153 | func != funcs->end(); func++) | |
7154 | { | |
7155 | // optimization: inlines will never be in the symbol table | |
7156 | if (dwarf_func_inline(&func->second) != 0) | |
7157 | continue; | |
7158 | ||
1ffb8bd1 JS |
7159 | // XXX We may want to make additional efforts to match mangled elf names |
7160 | // to dwarf too. MIPS_linkage_name can help, but that's sometimes | |
7161 | // missing, so we may also need to try matching by address. See also the | |
7162 | // notes about _Z in dwflpp::iterate_over_functions(). | |
7163 | ||
1c6b77e5 JS |
7164 | func_info *fi = sym_table->lookup_symbol(func->first); |
7165 | if (!fi) | |
7166 | continue; | |
7167 | ||
7168 | // iterate over all functions at the same address | |
7169 | symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr); | |
7170 | for (symbol_table::iterator_t it = er.first; it != er.second; ++it) | |
7171 | { | |
7172 | // update this function with the dwarf die | |
7173 | it->second->die = func->second; | |
7174 | ||
7175 | // if this function is a new alias, then | |
7176 | // save it to merge into the function cache | |
7177 | if (it->second != fi) | |
b7478964 | 7178 | new_funcs.insert(make_pair(it->second->name, it->second->die)); |
1c6b77e5 JS |
7179 | } |
7180 | } | |
7181 | ||
7182 | // add all discovered aliases back into the function cache | |
7183 | // NB: this won't replace any names that dwarf may have already found | |
7184 | funcs->insert(new_funcs.begin(), new_funcs.end()); | |
7185 | } | |
7186 | ||
5f0a03a6 JK |
7187 | module_info::~module_info() |
7188 | { | |
7189 | if (sym_table) | |
7190 | delete sym_table; | |
b55bc428 FCE |
7191 | } |
7192 | ||
935447c8 | 7193 | // ------------------------------------------------------------------------ |
888af770 | 7194 | // user-space probes |
935447c8 DS |
7195 | // ------------------------------------------------------------------------ |
7196 | ||
935447c8 | 7197 | |
888af770 | 7198 | struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe> |
935447c8 | 7199 | { |
89ba3085 FCE |
7200 | private: |
7201 | string make_pbm_key (uprobe_derived_probe* p) { | |
cfcab6c7 | 7202 | return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid); |
89ba3085 FCE |
7203 | } |
7204 | ||
cfcab6c7 JS |
7205 | void emit_module_maxuprobes (systemtap_session& s); |
7206 | ||
2b69faaf JS |
7207 | // Using our own utrace-based uprobes |
7208 | void emit_module_utrace_decls (systemtap_session& s); | |
7209 | void emit_module_utrace_init (systemtap_session& s); | |
7210 | void emit_module_utrace_exit (systemtap_session& s); | |
7211 | ||
7212 | // Using the upstream inode-based uprobes | |
7213 | void emit_module_inode_decls (systemtap_session& s); | |
7214 | void emit_module_inode_init (systemtap_session& s); | |
7215 | void emit_module_inode_exit (systemtap_session& s); | |
7216 | ||
3a894f7e JS |
7217 | // Using the dyninst backend (via stapdyn) |
7218 | void emit_module_dyninst_decls (systemtap_session& s); | |
7219 | void emit_module_dyninst_init (systemtap_session& s); | |
7220 | void emit_module_dyninst_exit (systemtap_session& s); | |
7221 | ||
935447c8 | 7222 | public: |
888af770 | 7223 | void emit_module_decls (systemtap_session& s); |
935447c8 DS |
7224 | void emit_module_init (systemtap_session& s); |
7225 | void emit_module_exit (systemtap_session& s); | |
7226 | }; | |
7227 | ||
7228 | ||
888af770 FCE |
7229 | void |
7230 | uprobe_derived_probe::join_group (systemtap_session& s) | |
7231 | { | |
7232 | if (! s.uprobe_derived_probes) | |
7233 | s.uprobe_derived_probes = new uprobe_derived_probe_group (); | |
7234 | s.uprobe_derived_probes->enroll (this); | |
ac3af990 | 7235 | if (!s.runtime_usermode_p()) |
4441e344 | 7236 | enable_task_finder(s); |
a96d1db0 | 7237 | |
8a03658e | 7238 | // Ask buildrun.cxx to build extra module if needed, and |
d3e959b0 DS |
7239 | // signal staprun to load that module. If we're using the builtin |
7240 | // inode-uprobes, we still need to know that it is required. | |
8a03658e | 7241 | s.need_uprobes = true; |
a96d1db0 DN |
7242 | } |
7243 | ||
888af770 | 7244 | |
c0f84e7b SC |
7245 | void |
7246 | uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const | |
7247 | { | |
7248 | dwarf_derived_probe::getargs(arg_set); | |
7249 | arg_set.insert(arg_set.end(), args.begin(), args.end()); | |
7250 | } | |
7251 | ||
7252 | ||
7253 | void | |
7254 | uprobe_derived_probe::saveargs(int nargs) | |
7255 | { | |
7256 | for (int i = 1; i <= nargs; i++) | |
7257 | args.push_back("$arg" + lex_cast (i) + ":long"); | |
7258 | } | |
7259 | ||
7260 | ||
2865d17a | 7261 | void |
42e38653 | 7262 | uprobe_derived_probe::emit_privilege_assertion (translator_output* o) |
2865d17a DB |
7263 | { |
7264 | // These probes are allowed for unprivileged users, but only in the | |
7265 | // context of processes which they own. | |
7266 | emit_process_owner_assertion (o); | |
7267 | } | |
7268 | ||
7269 | ||
888af770 | 7270 | struct uprobe_builder: public derived_probe_builder |
a96d1db0 | 7271 | { |
888af770 | 7272 | uprobe_builder() {} |
2b69faaf | 7273 | virtual void build(systemtap_session & sess, |
a96d1db0 DN |
7274 | probe * base, |
7275 | probe_point * location, | |
86bf665e | 7276 | literal_map_t const & parameters, |
a96d1db0 DN |
7277 | vector<derived_probe *> & finished_results) |
7278 | { | |
888af770 | 7279 | int64_t process, address; |
a96d1db0 | 7280 | |
2b69faaf JS |
7281 | if (kernel_supports_inode_uprobes(sess)) |
7282 | throw semantic_error (_("absolute process probes not available with inode-based uprobes")); | |
7283 | ||
888af770 | 7284 | bool b1 = get_param (parameters, TOK_PROCESS, process); |
ced347a9 | 7285 | (void) b1; |
888af770 | 7286 | bool b2 = get_param (parameters, TOK_STATEMENT, address); |
ced347a9 | 7287 | (void) b2; |
888af770 FCE |
7288 | bool rr = has_null_param (parameters, TOK_RETURN); |
7289 | assert (b1 && b2); // by pattern_root construction | |
a96d1db0 | 7290 | |
0973d815 | 7291 | finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr)); |
a96d1db0 DN |
7292 | } |
7293 | }; | |
7294 | ||
7295 | ||
7296 | void | |
cfcab6c7 | 7297 | uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s) |
a96d1db0 | 7298 | { |
43241c44 FCE |
7299 | // We'll probably need at least this many: |
7300 | unsigned minuprobes = probes.size(); | |
7301 | // .. but we don't want so many that .bss is inflated (PR10507): | |
7302 | unsigned uprobesize = 64; | |
7303 | unsigned maxuprobesmem = 10*1024*1024; // 10 MB | |
7304 | unsigned maxuprobes = maxuprobesmem / uprobesize; | |
7305 | ||
aaf7ffe8 FCE |
7306 | // Let's choose a value on the geometric middle. This should end up |
7307 | // between minuprobes and maxuprobes. It's OK if this number turns | |
7308 | // out to be < minuprobes or > maxuprobes. At worst, we get a | |
7309 | // run-time error of one kind (too few: missed uprobe registrations) | |
7310 | // or another (too many: vmalloc errors at module load time). | |
7311 | unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes); | |
43241c44 | 7312 | |
6d0f3f0c | 7313 | s.op->newline() << "#ifndef MAXUPROBES"; |
43241c44 | 7314 | s.op->newline() << "#define MAXUPROBES " << default_maxuprobes; |
6d0f3f0c | 7315 | s.op->newline() << "#endif"; |
cfcab6c7 JS |
7316 | } |
7317 | ||
7318 | ||
7319 | void | |
7320 | uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s) | |
7321 | { | |
7322 | if (probes.empty()) return; | |
7323 | s.op->newline() << "/* ---- utrace uprobes ---- */"; | |
7324 | // If uprobes isn't in the kernel, pull it in from the runtime. | |
7325 | ||
7326 | s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)"; | |
7327 | s.op->newline() << "#include <linux/uprobes.h>"; | |
7328 | s.op->newline() << "#else"; | |
2ba1736a | 7329 | s.op->newline() << "#include \"linux/uprobes/uprobes.h\""; |
cfcab6c7 JS |
7330 | s.op->newline() << "#endif"; |
7331 | s.op->newline() << "#ifndef UPROBES_API_VERSION"; | |
7332 | s.op->newline() << "#define UPROBES_API_VERSION 1"; | |
7333 | s.op->newline() << "#endif"; | |
7334 | ||
7335 | emit_module_maxuprobes (s); | |
a96d1db0 | 7336 | |
cc52276b | 7337 | // Forward decls |
2ba1736a | 7338 | s.op->newline() << "#include \"linux/uprobes-common.h\""; |
cc52276b | 7339 | |
5e112f92 FCE |
7340 | // In .bss, the shared pool of uprobe/uretprobe structs. These are |
7341 | // too big to embed in the initialized .data stap_uprobe_spec array. | |
cc52276b WC |
7342 | // XXX: consider a slab cache or somesuch for stap_uprobes |
7343 | s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];"; | |
6b378b7c | 7344 | s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration |
a96d1db0 | 7345 | |
89ba3085 FCE |
7346 | s.op->assert_0_indent(); |
7347 | ||
89ba3085 FCE |
7348 | // Assign task-finder numbers as we build up the stap_uprobe_tf table. |
7349 | // This means we process probes[] in two passes. | |
7350 | map <string,unsigned> module_index; | |
7351 | unsigned module_index_ctr = 0; | |
7352 | ||
cc52276b WC |
7353 | // not const since embedded task_finder_target struct changes |
7354 | s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {"; | |
89ba3085 FCE |
7355 | s.op->indent(1); |
7356 | for (unsigned i=0; i<probes.size(); i++) | |
7357 | { | |
7358 | uprobe_derived_probe *p = probes[i]; | |
7359 | string pbmkey = make_pbm_key (p); | |
7360 | if (module_index.find (pbmkey) == module_index.end()) | |
7361 | { | |
7362 | module_index[pbmkey] = module_index_ctr++; | |
7363 | ||
7364 | s.op->newline() << "{"; | |
7365 | // NB: it's essential that make_pbm_key() use all of and | |
7366 | // only the same fields as we're about to emit. | |
7367 | s.op->line() << " .finder={"; | |
7368 | if (p->pid != 0) | |
68910c97 JK |
7369 | s.op->line() << " .pid=" << p->pid << ","; |
7370 | ||
7371 | if (p->section == "") // .statement(addr).absolute | |
7372 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
89ba3085 FCE |
7373 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program |
7374 | { | |
7375 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
7376 | s.op->line() << " .callback=&stap_uprobe_process_found,"; | |
7377 | } | |
68910c97 | 7378 | else if (p->section != ".absolute") // ET_DYN |
89ba3085 | 7379 | { |
4ad95bbc SC |
7380 | if (p->has_library) |
7381 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
89ba3085 FCE |
7382 | s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, "; |
7383 | s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, "; | |
19d91f6c | 7384 | s.op->line() << " .callback=&stap_uprobe_process_munmap,"; |
89ba3085 | 7385 | } |
89ba3085 | 7386 | s.op->line() << " },"; |
68910c97 JK |
7387 | if (p->module != "") |
7388 | s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", "; | |
89ba3085 FCE |
7389 | s.op->line() << " },"; |
7390 | } | |
c57ea854 | 7391 | else |
822a6a3d | 7392 | { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it. |
89ba3085 FCE |
7393 | } |
7394 | s.op->newline(-1) << "};"; | |
7395 | ||
7396 | s.op->assert_0_indent(); | |
7397 | ||
cc52276b WC |
7398 | // NB: read-only structure |
7399 | s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {"; | |
a96d1db0 | 7400 | s.op->indent(1); |
888af770 FCE |
7401 | for (unsigned i =0; i<probes.size(); i++) |
7402 | { | |
7403 | uprobe_derived_probe* p = probes[i]; | |
7404 | s.op->newline() << "{"; | |
89ba3085 FCE |
7405 | string key = make_pbm_key (p); |
7406 | unsigned value = module_index[key]; | |
759e1d76 FCE |
7407 | if (value != 0) |
7408 | s.op->line() << " .tfi=" << value << ","; | |
6b66b9f7 | 7409 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
faea5e16 | 7410 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
4ddb6dd0 | 7411 | |
038c38c6 | 7412 | if (p->sdt_semaphore_addr != 0) |
63b4fd14 | 7413 | s.op->line() << " .sdt_sem_offset=(unsigned long)0x" |
038c38c6 | 7414 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; |
4ddb6dd0 JS |
7415 | |
7416 | if (p->has_return) | |
7417 | s.op->line() << " .return_p=1,"; | |
888af770 FCE |
7418 | s.op->line() << " },"; |
7419 | } | |
7420 | s.op->newline(-1) << "};"; | |
a96d1db0 | 7421 | |
89ba3085 FCE |
7422 | s.op->assert_0_indent(); |
7423 | ||
48e685da | 7424 | s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {"; |
888af770 | 7425 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);"; |
89ba3085 | 7426 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
71db462b | 7427 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe", |
cda141c2 | 7428 | "stp_probe_type_uprobe"); |
0e090c74 | 7429 | s.op->newline() << "if (sup->spec_index < 0 || " |
6e895029 DS |
7430 | << "sup->spec_index >= " << probes.size() << ") {"; |
7431 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 7432 | << "): %s\", sup->spec_index, c->probe_point);"; |
6e895029 DS |
7433 | s.op->newline() << "atomic_dec (&c->busy);"; |
7434 | s.op->newline() << "goto probe_epilogue;"; | |
7435 | s.op->newline(-1) << "}"; | |
d9aed31e | 7436 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 7437 | s.op->newline() << "c->user_mode_p = 1;"; |
6415ddde MW |
7438 | |
7439 | // Make it look like the IP is set as it would in the actual user | |
7440 | // task when calling real probe handler. Reset IP regs on return, so | |
7441 | // we don't confuse uprobes. PR10458 | |
7442 | s.op->newline() << "{"; | |
7443 | s.op->indent(1); | |
d9aed31e | 7444 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
259d54c0 | 7445 | s.op->newline() << "SET_REG_IP(regs, inst->vaddr);"; |
26e63673 | 7446 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 7447 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
7448 | s.op->newline(-1) << "}"; |
7449 | ||
f887a8c9 | 7450 | common_probe_entryfn_epilogue (s, true); |
888af770 | 7451 | s.op->newline(-1) << "}"; |
a96d1db0 | 7452 | |
48e685da | 7453 | s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {"; |
888af770 | 7454 | s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);"; |
89ba3085 | 7455 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
71db462b | 7456 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe", |
cda141c2 | 7457 | "stp_probe_type_uretprobe"); |
6dceb5c9 | 7458 | s.op->newline() << "c->ips.ri = inst;"; |
0e090c74 | 7459 | s.op->newline() << "if (sup->spec_index < 0 || " |
0d5561a5 DS |
7460 | << "sup->spec_index >= " << probes.size() << ") {"; |
7461 | s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size() | |
0e090c74 | 7462 | << "): %s\", sup->spec_index, c->probe_point);"; |
0d5561a5 DS |
7463 | s.op->newline() << "atomic_dec (&c->busy);"; |
7464 | s.op->newline() << "goto probe_epilogue;"; | |
7465 | s.op->newline(-1) << "}"; | |
7466 | ||
d9aed31e | 7467 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 7468 | s.op->newline() << "c->user_mode_p = 1;"; |
6415ddde MW |
7469 | |
7470 | // Make it look like the IP is set as it would in the actual user | |
7471 | // task when calling real probe handler. Reset IP regs on return, so | |
7472 | // we don't confuse uprobes. PR10458 | |
7473 | s.op->newline() << "{"; | |
7474 | s.op->indent(1); | |
d9aed31e | 7475 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);"; |
5e562a69 | 7476 | s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);"; |
26e63673 | 7477 | s.op->newline() << "(*sups->probe->ph) (c);"; |
259d54c0 | 7478 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
6415ddde MW |
7479 | s.op->newline(-1) << "}"; |
7480 | ||
f887a8c9 | 7481 | common_probe_entryfn_epilogue (s, true); |
a96d1db0 DN |
7482 | s.op->newline(-1) << "}"; |
7483 | ||
89ba3085 | 7484 | s.op->newline(); |
2ba1736a | 7485 | s.op->newline() << "#include \"linux/uprobes-common.c\""; |
6d0f3f0c | 7486 | s.op->newline(); |
888af770 | 7487 | } |
935447c8 DS |
7488 | |
7489 | ||
888af770 | 7490 | void |
2b69faaf | 7491 | uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s) |
935447c8 | 7492 | { |
888af770 | 7493 | if (probes.empty()) return; |
935447c8 | 7494 | |
2b69faaf | 7495 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
935447c8 | 7496 | |
01b05e2e | 7497 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 FCE |
7498 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
7499 | s.op->newline() << "sup->spec_index = -1;"; // free slot | |
80b4ad8b FCE |
7500 | // NB: we assume the rest of the struct (specificaly, sup->up) is |
7501 | // initialized to zero. This is so that we can use | |
7502 | // sup->up->kdata = NULL for "really free!" PR 6829. | |
5e112f92 FCE |
7503 | s.op->newline(-1) << "}"; |
7504 | s.op->newline() << "mutex_init (& stap_uprobes_lock);"; | |
935447c8 | 7505 | |
89ba3085 FCE |
7506 | // Set up the task_finders |
7507 | s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {"; | |
7508 | s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];"; | |
c57ea854 | 7509 | s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better |
89ba3085 | 7510 | s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);"; |
935447c8 | 7511 | |
5e112f92 FCE |
7512 | // NB: if (rc), there is no need (XXX: nor any way) to clean up any |
7513 | // finders already registered, since mere registration does not | |
7514 | // cause any utrace or memory allocation actions. That happens only | |
7515 | // later, once the task finder engine starts running. So, for a | |
7516 | // partial initialization requiring unwind, we need do nothing. | |
7517 | s.op->newline() << "if (rc) break;"; | |
a7a68293 | 7518 | |
888af770 FCE |
7519 | s.op->newline(-1) << "}"; |
7520 | } | |
d0ea46ce | 7521 | |
d0a7f5a9 | 7522 | |
888af770 | 7523 | void |
2b69faaf | 7524 | uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s) |
888af770 FCE |
7525 | { |
7526 | if (probes.empty()) return; | |
2b69faaf | 7527 | s.op->newline() << "/* ---- utrace uprobes ---- */"; |
e56e51c9 | 7528 | |
6d0f3f0c FCE |
7529 | // NB: there is no stap_unregister_task_finder_target call; |
7530 | // important stuff like utrace cleanups are done by | |
d41d451c FCE |
7531 | // __stp_task_finder_cleanup() via stap_stop_task_finder(). |
7532 | // | |
7533 | // This function blocks until all callbacks are completed, so there | |
7534 | // is supposed to be no possibility of any registration-related code starting | |
7535 | // to run in parallel with our shutdown here. So we don't need to protect the | |
7536 | // stap_uprobes[] array with the mutex. | |
d0a7f5a9 | 7537 | |
01b05e2e | 7538 | s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {"; |
5e112f92 | 7539 | s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];"; |
89ba3085 | 7540 | s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];"; |
6d0f3f0c | 7541 | s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot |
3568f1dd | 7542 | |
8faa1fc5 | 7543 | // PR10655: decrement that ENABLED semaphore |
c116c31b | 7544 | s.op->newline() << "if (sup->sdt_sem_address) {"; |
8faa1fc5 FCE |
7545 | s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size |
7546 | s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);"; | |
7547 | s.op->newline() << "struct task_struct *tsk;"; | |
7548 | s.op->newline() << "rcu_read_lock();"; | |
6846cfc8 | 7549 | |
86229a55 DS |
7550 | // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes |
7551 | // that the pid is always in the global namespace, not in any | |
7552 | // private namespace. | |
8faa1fc5 | 7553 | s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)"; |
86229a55 DS |
7554 | // We'd like to call find_task_by_pid_ns() here, but it isn't |
7555 | // exported. So, we call what it calls... | |
7556 | s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);"; | |
8faa1fc5 FCE |
7557 | s.op->newline() << "#else"; |
7558 | s.op->newline() << " tsk = find_task_by_pid (pid);"; | |
7559 | s.op->newline() << "#endif /* 2.6.24 */"; | |
8faa1fc5 FCE |
7560 | |
7561 | s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching | |
3c5b8e2b | 7562 | s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {"; |
63b4fd14 | 7563 | s.op->newline(1) << "sdt_semaphore --;"; |
903b9fcd | 7564 | s.op->newline() << "#ifdef DEBUG_UPROBES"; |
c116c31b | 7565 | s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);"; |
903b9fcd | 7566 | s.op->newline() << "#endif"; |
3c5b8e2b | 7567 | s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);"; |
93c84191 | 7568 | s.op->newline(-1) << "}"; |
8faa1fc5 FCE |
7569 | // XXX: need to analyze possibility of race condition |
7570 | s.op->newline(-1) << "}"; | |
7571 | s.op->newline() << "rcu_read_unlock();"; | |
7572 | s.op->newline(-1) << "}"; | |
6846cfc8 | 7573 | |
3568f1dd FCE |
7574 | s.op->newline() << "if (sups->return_p) {"; |
7575 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
89ba3085 | 7576 | 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 | 7577 | s.op->newline() << "#endif"; |
80b4ad8b FCE |
7578 | // NB: PR6829 does not change that we still need to unregister at |
7579 | // *this* time -- when the script as a whole exits. | |
3568f1dd FCE |
7580 | s.op->newline() << "unregister_uretprobe (& sup->urp);"; |
7581 | s.op->newline(-1) << "} else {"; | |
7582 | s.op->newline(1) << "#ifdef DEBUG_UPROBES"; | |
8faa1fc5 | 7583 | 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 |
7584 | s.op->newline() << "#endif"; |
7585 | s.op->newline() << "unregister_uprobe (& sup->up);"; | |
7586 | s.op->newline(-1) << "}"; | |
935447c8 | 7587 | |
6d0f3f0c | 7588 | s.op->newline() << "sup->spec_index = -1;"; |
935447c8 | 7589 | |
3568f1dd FCE |
7590 | // XXX: uprobe missed counts? |
7591 | ||
6d0f3f0c | 7592 | s.op->newline(-1) << "}"; |
935447c8 | 7593 | |
5e112f92 | 7594 | s.op->newline() << "mutex_destroy (& stap_uprobes_lock);"; |
935447c8 DS |
7595 | } |
7596 | ||
2b69faaf JS |
7597 | |
7598 | void | |
7599 | uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s) | |
7600 | { | |
7601 | if (probes.empty()) return; | |
7602 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 7603 | emit_module_maxuprobes (s); |
2ba1736a | 7604 | s.op->newline() << "#include \"linux/uprobes-inode.c\""; |
2b69faaf JS |
7605 | |
7606 | // Write the probe handler. | |
79af55c3 JS |
7607 | s.op->newline() << "static int stapiu_probe_handler " |
7608 | << "(struct stapiu_consumer *sup, struct pt_regs *regs) {"; | |
7609 | s.op->newline(1); | |
71db462b | 7610 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe", |
cda141c2 | 7611 | "stp_probe_type_uprobe"); |
3bff6634 | 7612 | s.op->newline() << "c->uregs = regs;"; |
e04b5d74 | 7613 | s.op->newline() << "c->user_mode_p = 1;"; |
39e63bae DS |
7614 | // Make it look like the IP is set as it would in the actual user |
7615 | // task when calling real probe handler. Reset IP regs on return, so | |
7616 | // we don't confuse uprobes. | |
7617 | s.op->newline() << "{"; | |
7618 | s.op->indent(1); | |
7619 | s.op->newline() << "unsigned long uprobes_ip = REG_IP(regs);"; | |
7620 | s.op->newline() << "SET_REG_IP(regs, uprobe_get_swbp_addr(regs));"; | |
2b69faaf | 7621 | s.op->newline() << "(*sup->probe->ph) (c);"; |
39e63bae DS |
7622 | s.op->newline() << "SET_REG_IP(regs, uprobes_ip);"; |
7623 | s.op->newline(-1) << "}"; | |
7624 | ||
f887a8c9 | 7625 | common_probe_entryfn_epilogue (s, true); |
2b69faaf JS |
7626 | s.op->newline() << "return 0;"; |
7627 | s.op->newline(-1) << "}"; | |
7628 | s.op->assert_0_indent(); | |
7629 | ||
7630 | // Index of all the modules for which we need inodes. | |
7631 | map<string, unsigned> module_index; | |
7632 | unsigned module_index_ctr = 0; | |
7633 | ||
7634 | // Discover and declare targets for each unique path. | |
cfcab6c7 | 7635 | s.op->newline() << "static struct stapiu_target " |
2b69faaf JS |
7636 | << "stap_inode_uprobe_targets[] = {"; |
7637 | s.op->indent(1); | |
7638 | for (unsigned i=0; i<probes.size(); i++) | |
7639 | { | |
7640 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
7641 | const string key = make_pbm_key(p); |
7642 | if (module_index.find (key) == module_index.end()) | |
2b69faaf | 7643 | { |
cfcab6c7 JS |
7644 | module_index[key] = module_index_ctr++; |
7645 | s.op->newline() << "{"; | |
7646 | s.op->line() << " .finder={"; | |
7647 | if (p->pid != 0) | |
7648 | s.op->line() << " .pid=" << p->pid << ","; | |
7649 | ||
7650 | if (p->section == "") // .statement(addr).absolute XXX? | |
7651 | s.op->line() << " .callback=&stapiu_process_found,"; | |
7652 | else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program | |
7653 | { | |
7654 | s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ","; | |
7655 | s.op->line() << " .callback=&stapiu_process_found,"; | |
7656 | } | |
7657 | else if (p->section != ".absolute") // ET_DYN | |
7658 | { | |
7659 | if (p->has_library) | |
7660 | s.op->line() << " .procname=\"" << p->path << "\", "; | |
7661 | s.op->line() << " .mmap_callback=&stapiu_mmap_found, "; | |
7662 | s.op->line() << " .munmap_callback=&stapiu_munmap_found, "; | |
7663 | s.op->line() << " .callback=&stapiu_process_munmap,"; | |
7664 | } | |
7665 | s.op->line() << " },"; | |
7666 | s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ","; | |
7667 | s.op->line() << " },"; | |
2b69faaf JS |
7668 | } |
7669 | } | |
7670 | s.op->newline(-1) << "};"; | |
7671 | s.op->assert_0_indent(); | |
7672 | ||
7673 | // Declare the actual probes. | |
cfcab6c7 | 7674 | s.op->newline() << "static struct stapiu_consumer " |
2b69faaf JS |
7675 | << "stap_inode_uprobe_consumers[] = {"; |
7676 | s.op->indent(1); | |
7677 | for (unsigned i=0; i<probes.size(); i++) | |
7678 | { | |
7679 | uprobe_derived_probe *p = probes[i]; | |
cfcab6c7 JS |
7680 | unsigned index = module_index[make_pbm_key(p)]; |
7681 | s.op->newline() << "{"; | |
79af55c3 JS |
7682 | if (p->has_return) |
7683 | s.op->line() << " .return_p=1,"; | |
cfcab6c7 JS |
7684 | s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],"; |
7685 | s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,"; | |
7686 | if (p->sdt_semaphore_addr) | |
7687 | s.op->line() << " .sdt_sem_offset=(loff_t)0x" | |
7688 | << hex << p->sdt_semaphore_addr << dec << "ULL,"; | |
7689 | s.op->line() << " .probe=" << common_probe_init (p) << ","; | |
7690 | s.op->line() << " },"; | |
2b69faaf JS |
7691 | } |
7692 | s.op->newline(-1) << "};"; | |
7693 | s.op->assert_0_indent(); | |
7694 | } | |
7695 | ||
7696 | ||
7697 | void | |
7698 | uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s) | |
7699 | { | |
7700 | if (probes.empty()) return; | |
7701 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
b0f614a0 DS |
7702 | // Let stapiu_init() handle reporting errors by setting probe_point |
7703 | // to NULL. | |
7704 | s.op->newline() << "probe_point = NULL;"; | |
cfcab6c7 | 7705 | s.op->newline() << "rc = stapiu_init (" |
2b69faaf JS |
7706 | << "stap_inode_uprobe_targets, " |
7707 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
7708 | << "stap_inode_uprobe_consumers, " | |
7709 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
7710 | } | |
7711 | ||
7712 | ||
7713 | void | |
7714 | uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s) | |
7715 | { | |
7716 | if (probes.empty()) return; | |
7717 | s.op->newline() << "/* ---- inode uprobes ---- */"; | |
cfcab6c7 | 7718 | s.op->newline() << "stapiu_exit (" |
2b69faaf JS |
7719 | << "stap_inode_uprobe_targets, " |
7720 | << "ARRAY_SIZE(stap_inode_uprobe_targets), " | |
7721 | << "stap_inode_uprobe_consumers, " | |
7722 | << "ARRAY_SIZE(stap_inode_uprobe_consumers));"; | |
7723 | } | |
7724 | ||
7725 | ||
3a894f7e JS |
7726 | void |
7727 | uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s) | |
7728 | { | |
7729 | if (probes.empty()) return; | |
7730 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
7731 | emit_module_maxuprobes (s); | |
e00f3fb7 | 7732 | s.op->newline() << "#include \"dyninst/uprobes.h\""; |
3a894f7e | 7733 | |
e00f3fb7 JS |
7734 | // Assign "task-finder" numbers as we build up the stapdu_target table. |
7735 | // This means we process probes[] in two passes. | |
7736 | map <string,unsigned> module_index; | |
7737 | unsigned module_index_ctr = 0; | |
7738 | ||
7739 | // Discover and declare file targets | |
7740 | s.op->newline() << "static const struct stapdu_target stapdu_targets[] = {"; | |
3a894f7e JS |
7741 | s.op->indent(1); |
7742 | for (unsigned i=0; i<probes.size(); i++) | |
7743 | { | |
7744 | uprobe_derived_probe *p = probes[i]; | |
e00f3fb7 JS |
7745 | string pbmkey = make_pbm_key (p); |
7746 | if (module_index.find (pbmkey) == module_index.end()) | |
7747 | { | |
7748 | module_index[pbmkey] = module_index_ctr++; | |
7749 | ||
7750 | s.op->newline() << "{"; | |
7751 | // NB: it's essential that make_pbm_key() use all of and | |
7752 | // only the same fields as we're about to emit. | |
7753 | s.op->line() << " .path=" << lex_cast_qstring(p->module) << ","; | |
7754 | // XXX pid/procname filtering? | |
7755 | s.op->line() << " },"; | |
7756 | } | |
3a894f7e JS |
7757 | } |
7758 | s.op->newline(-1) << "};"; | |
7759 | s.op->assert_0_indent(); | |
7760 | ||
874d38bf JS |
7761 | // loc2c-generated code assumes pt_regs are available, so use this to make |
7762 | // sure we always have *something* for it to dereference... | |
7763 | s.op->newline() << "static struct pt_regs stapdu_dummy_uregs = {0};"; | |
7764 | ||
e00f3fb7 JS |
7765 | // Declare the individual probes |
7766 | s.op->newline() << "static struct stapdu_probe stapdu_probes[] = {"; | |
3a894f7e JS |
7767 | s.op->indent(1); |
7768 | for (unsigned i=0; i<probes.size(); i++) | |
7769 | { | |
7770 | uprobe_derived_probe *p = probes[i]; | |
e00f3fb7 | 7771 | unsigned index = module_index[make_pbm_key(p)]; |
3a894f7e | 7772 | s.op->newline() << "{"; |
e00f3fb7 JS |
7773 | s.op->line() << " .target=" << index << ","; |
7774 | s.op->line() << " .offset=" << lex_cast_hex(p->addr) << "ULL,"; | |
7775 | if (p->sdt_semaphore_addr) | |
7776 | s.op->line() << " .semaphore=" | |
7777 | << lex_cast_hex(p->sdt_semaphore_addr) << "ULL,"; | |
536f1261 JS |
7778 | |
7779 | s.op->line() << " .flags=0"; | |
7780 | if (p->has_return) | |
7781 | s.op->line() << "|STAPDYN_PROBE_FLAG_RETURN"; | |
7782 | s.op->line() << ","; | |
7783 | ||
3a894f7e JS |
7784 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
7785 | s.op->line() << " },"; | |
7786 | } | |
7787 | s.op->newline(-1) << "};"; | |
7788 | s.op->assert_0_indent(); | |
7789 | ||
e00f3fb7 JS |
7790 | s.op->newline() << "#include \"dyninst/uprobes.c\""; |
7791 | ||
3a894f7e JS |
7792 | // Write the probe handler. |
7793 | // NB: not static, so dyninst can find it | |
7794 | s.op->newline() << "int enter_dyninst_uprobe " | |
7795 | << "(uint64_t index, struct pt_regs *regs) {"; | |
e00f3fb7 | 7796 | s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];"; |
71db462b | 7797 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe", |
cda141c2 | 7798 | "stp_probe_type_uprobe"); |
874d38bf | 7799 | s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;"; |
e04b5d74 | 7800 | s.op->newline() << "c->user_mode_p = 1;"; |
3a894f7e JS |
7801 | // XXX: once we have regs, check how dyninst sets the IP |
7802 | // XXX: the way that dyninst rewrites stuff is probably going to be | |
7803 | // ... very confusing to our backtracer (at least if we stay in process) | |
7804 | s.op->newline() << "(*sup->probe->ph) (c);"; | |
f887a8c9 | 7805 | common_probe_entryfn_epilogue (s, true); |
3a894f7e JS |
7806 | s.op->newline() << "return 0;"; |
7807 | s.op->newline(-1) << "}"; | |
7808 | s.op->assert_0_indent(); | |
7809 | } | |
7810 | ||
7811 | ||
7812 | void | |
7813 | uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s) | |
7814 | { | |
7815 | if (probes.empty()) return; | |
7816 | ||
7817 | /* stapdyn handles the dirty work via dyninst */ | |
7818 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
7819 | s.op->newline() << "/* this section left intentionally blank */"; | |
7820 | } | |
7821 | ||
7822 | ||
7823 | void | |
7824 | uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s) | |
7825 | { | |
7826 | if (probes.empty()) return; | |
7827 | ||
7828 | /* stapdyn handles the dirty work via dyninst */ | |
7829 | s.op->newline() << "/* ---- dyninst uprobes ---- */"; | |
7830 | s.op->newline() << "/* this section left intentionally blank */"; | |
7831 | } | |
7832 | ||
7833 | ||
2b69faaf JS |
7834 | void |
7835 | uprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
7836 | { | |
ac3af990 | 7837 | if (s.runtime_usermode_p()) |
4441e344 JS |
7838 | emit_module_dyninst_decls (s); |
7839 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
7840 | emit_module_inode_decls (s); |
7841 | else | |
7842 | emit_module_utrace_decls (s); | |
7843 | } | |
7844 | ||
7845 | ||
7846 | void | |
7847 | uprobe_derived_probe_group::emit_module_init (systemtap_session& s) | |
7848 | { | |
ac3af990 | 7849 | if (s.runtime_usermode_p()) |
4441e344 JS |
7850 | emit_module_dyninst_init (s); |
7851 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
7852 | emit_module_inode_init (s); |
7853 | else | |
7854 | emit_module_utrace_init (s); | |
7855 | } | |
7856 | ||
7857 | ||
7858 | void | |
7859 | uprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
7860 | { | |
ac3af990 | 7861 | if (s.runtime_usermode_p()) |
4441e344 JS |
7862 | emit_module_dyninst_exit (s); |
7863 | else if (kernel_supports_inode_uprobes (s)) | |
2b69faaf JS |
7864 | emit_module_inode_exit (s); |
7865 | else | |
7866 | emit_module_utrace_exit (s); | |
7867 | } | |
7868 | ||
7869 | ||
e6fe60e7 AM |
7870 | // ------------------------------------------------------------------------ |
7871 | // Kprobe derived probes | |
7872 | // ------------------------------------------------------------------------ | |
7873 | ||
4627ed58 | 7874 | static const string TOK_KPROBE("kprobe"); |
935447c8 | 7875 | |
bae55db9 | 7876 | struct kprobe_derived_probe: public derived_probe |
d0ea46ce | 7877 | { |
23dc94f6 DS |
7878 | kprobe_derived_probe (systemtap_session& sess, |
7879 | vector<derived_probe *> & results, | |
7880 | probe *base, | |
bae55db9 JS |
7881 | probe_point *location, |
7882 | const string& name, | |
7883 | int64_t stmt_addr, | |
7884 | bool has_return, | |
7885 | bool has_statement, | |
7886 | bool has_maxactive, | |
b642c901 SC |
7887 | bool has_path, |
7888 | bool has_library, | |
7889 | long maxactive_val, | |
7890 | const string& path, | |
7891 | const string& library | |
bae55db9 JS |
7892 | ); |
7893 | string symbol_name; | |
7894 | Dwarf_Addr addr; | |
7895 | bool has_return; | |
7896 | bool has_statement; | |
7897 | bool has_maxactive; | |
b642c901 SC |
7898 | bool has_path; |
7899 | bool has_library; | |
bae55db9 | 7900 | long maxactive_val; |
b642c901 SC |
7901 | string path; |
7902 | string library; | |
bae55db9 JS |
7903 | bool access_var; |
7904 | void printsig (std::ostream &o) const; | |
7905 | void join_group (systemtap_session& s); | |
7906 | }; | |
d0ea46ce | 7907 | |
bae55db9 JS |
7908 | struct kprobe_derived_probe_group: public derived_probe_group |
7909 | { | |
7910 | private: | |
7911 | multimap<string,kprobe_derived_probe*> probes_by_module; | |
7912 | typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator; | |
d0ea46ce | 7913 | |
bae55db9 JS |
7914 | public: |
7915 | void enroll (kprobe_derived_probe* probe); | |
7916 | void emit_module_decls (systemtap_session& s); | |
7917 | void emit_module_init (systemtap_session& s); | |
7918 | void emit_module_exit (systemtap_session& s); | |
7919 | }; | |
d0ea46ce | 7920 | |
23dc94f6 DS |
7921 | struct kprobe_var_expanding_visitor: public var_expanding_visitor |
7922 | { | |
7923 | systemtap_session& sess; | |
7924 | block *add_block; | |
7925 | block *add_call_probe; // synthesized from .return probes with saved $vars | |
7926 | bool add_block_tid, add_call_probe_tid; | |
bd5b25e1 | 7927 | bool has_return; |
23dc94f6 | 7928 | |
bd5b25e1 | 7929 | kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return): |
23dc94f6 | 7930 | sess(sess), add_block(NULL), add_call_probe(NULL), |
bd5b25e1 JS |
7931 | add_block_tid(false), add_call_probe_tid(false), |
7932 | has_return(has_return) {} | |
23dc94f6 DS |
7933 | |
7934 | void visit_entry_op (entry_op* e); | |
7935 | }; | |
7936 | ||
7937 | ||
7938 | kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess, | |
7939 | vector<derived_probe *> & results, | |
7940 | probe *base, | |
e6fe60e7 | 7941 | probe_point *location, |
b6371390 | 7942 | const string& name, |
e6fe60e7 | 7943 | int64_t stmt_addr, |
b6371390 JS |
7944 | bool has_return, |
7945 | bool has_statement, | |
7946 | bool has_maxactive, | |
b642c901 SC |
7947 | bool has_path, |
7948 | bool has_library, | |
7949 | long maxactive_val, | |
7950 | const string& path, | |
7951 | const string& library | |
b6371390 | 7952 | ): |
4c5d1300 | 7953 | derived_probe (base, location, true /* .components soon rewritten */ ), |
e6fe60e7 | 7954 | symbol_name (name), addr (stmt_addr), |
b6371390 | 7955 | has_return (has_return), has_statement (has_statement), |
b642c901 SC |
7956 | has_maxactive (has_maxactive), has_path (has_path), |
7957 | has_library (has_library), | |
7958 | maxactive_val (maxactive_val), | |
7959 | path (path), library (library) | |
e6fe60e7 AM |
7960 | { |
7961 | this->tok = base->tok; | |
7962 | this->access_var = false; | |
d0ea46ce | 7963 | |
e6fe60e7 AM |
7964 | #ifndef USHRT_MAX |
7965 | #define USHRT_MAX 32767 | |
7966 | #endif | |
d0ea46ce | 7967 | |
46856d8d JS |
7968 | // Expansion of $target variables in the probe body produces an error during |
7969 | // translate phase, since we're not using debuginfo | |
d0ea46ce | 7970 | |
e6fe60e7 | 7971 | vector<probe_point::component*> comps; |
46856d8d | 7972 | comps.push_back (new probe_point::component(TOK_KPROBE)); |
e6fe60e7 | 7973 | |
46856d8d JS |
7974 | if (has_statement) |
7975 | { | |
9ea68eb9 JS |
7976 | comps.push_back (new probe_point::component(TOK_STATEMENT, |
7977 | new literal_number(addr, true))); | |
46856d8d JS |
7978 | comps.push_back (new probe_point::component(TOK_ABSOLUTE)); |
7979 | } | |
7980 | else | |
7981 | { | |
7982 | size_t pos = name.find(':'); | |
7983 | if (pos != string::npos) | |
d0ea46ce | 7984 | { |
46856d8d JS |
7985 | string module = name.substr(0, pos); |
7986 | string function = name.substr(pos + 1); | |
7987 | comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module))); | |
7988 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function))); | |
7989 | } | |
7990 | else | |
7991 | comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name))); | |
46856d8d | 7992 | } |
d0ea46ce | 7993 | |
b6371390 JS |
7994 | if (has_return) |
7995 | comps.push_back (new probe_point::component(TOK_RETURN)); | |
7996 | if (has_maxactive) | |
7997 | comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val))); | |
d0ea46ce | 7998 | |
bd5b25e1 | 7999 | kprobe_var_expanding_visitor v (sess, has_return); |
23dc94f6 DS |
8000 | v.replace (this->body); |
8001 | ||
8002 | // If during target-variable-expanding the probe, we added a new block | |
8003 | // of code, add it to the start of the probe. | |
8004 | if (v.add_block) | |
8005 | this->body = new block(v.add_block, this->body); | |
8006 | ||
8007 | // If when target-variable-expanding the probe, we need to | |
8008 | // synthesize a sibling function-entry probe. We don't go through | |
8009 | // the whole probe derivation business (PR10642) that could lead to | |
8010 | // wildcard/alias resolution, or for that dwarf-induced duplication. | |
8011 | if (v.add_call_probe) | |
8012 | { | |
8013 | assert (has_return); | |
8014 | ||
8015 | // We temporarily replace base. | |
8016 | statement* old_body = base->body; | |
8017 | base->body = v.add_call_probe; | |
8018 | ||
8019 | derived_probe *entry_handler | |
8020 | = new kprobe_derived_probe (sess, results, base, location, name, 0, | |
8021 | false, has_statement, has_maxactive, | |
8022 | has_path, has_library, maxactive_val, | |
8023 | path, library); | |
8024 | results.push_back (entry_handler); | |
8025 | ||
8026 | base->body = old_body; | |
8027 | } | |
8028 | ||
e6fe60e7 AM |
8029 | this->sole_location()->components = comps; |
8030 | } | |
d0ea46ce | 8031 | |
e6fe60e7 AM |
8032 | void kprobe_derived_probe::printsig (ostream& o) const |
8033 | { | |
8034 | sole_location()->print (o); | |
8035 | o << " /* " << " name = " << symbol_name << "*/"; | |
8036 | printsig_nested (o); | |
8037 | } | |
d0ea46ce | 8038 | |
e6fe60e7 AM |
8039 | void kprobe_derived_probe::join_group (systemtap_session& s) |
8040 | { | |
d0ea46ce | 8041 | |
e6fe60e7 AM |
8042 | if (! s.kprobe_derived_probes) |
8043 | s.kprobe_derived_probes = new kprobe_derived_probe_group (); | |
8044 | s.kprobe_derived_probes->enroll (this); | |
d0ea46ce | 8045 | |
e6fe60e7 | 8046 | } |
d0ea46ce | 8047 | |
e6fe60e7 AM |
8048 | void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p) |
8049 | { | |
8050 | probes_by_module.insert (make_pair (p->symbol_name, p)); | |
8051 | // probes of same symbol should share single kprobe/kretprobe | |
8052 | } | |
d0ea46ce | 8053 | |
e6fe60e7 AM |
8054 | void |
8055 | kprobe_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8056 | { | |
8057 | if (probes_by_module.empty()) return; | |
d0ea46ce | 8058 | |
e6fe60e7 | 8059 | s.op->newline() << "/* ---- kprobe-based probes ---- */"; |
d0ea46ce | 8060 | |
e6fe60e7 AM |
8061 | // Warn of misconfigured kernels |
8062 | s.op->newline() << "#if ! defined(CONFIG_KPROBES)"; | |
8063 | s.op->newline() << "#error \"Need CONFIG_KPROBES!\""; | |
8064 | s.op->newline() << "#endif"; | |
8065 | s.op->newline(); | |
d0ea46ce | 8066 | |
f07c3b68 | 8067 | s.op->newline() << "#ifndef KRETACTIVE"; |
1ee6b5fc | 8068 | s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))"; |
f07c3b68 FCE |
8069 | s.op->newline() << "#endif"; |
8070 | ||
e6fe60e7 | 8071 | // Forward declare the master entry functions |
88747011 | 8072 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 | 8073 | s.op->line() << " struct pt_regs *regs);"; |
88747011 | 8074 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 | 8075 | s.op->line() << " struct pt_regs *regs);"; |
d0ea46ce | 8076 | |
e6fe60e7 AM |
8077 | // Emit an array of kprobe/kretprobe pointers |
8078 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
c9116e99 | 8079 | s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];"; |
e6fe60e7 | 8080 | s.op->newline() << "#endif"; |
d0ea46ce | 8081 | |
e6fe60e7 | 8082 | // Emit the actual probe list. |
d0ea46ce | 8083 | |
e6fe60e7 AM |
8084 | s.op->newline() << "static struct stap_dwarfless_kprobe {"; |
8085 | s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;"; | |
8086 | s.op->newline() << "#ifdef __ia64__"; | |
8087 | s.op->newline() << "struct kprobe dummy;"; | |
8088 | s.op->newline() << "#endif"; | |
8089 | s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];"; | |
8090 | // NB: bss! | |
d0ea46ce | 8091 | |
e6fe60e7 AM |
8092 | s.op->newline() << "static struct stap_dwarfless_probe {"; |
8093 | s.op->newline(1) << "const unsigned return_p:1;"; | |
8094 | s.op->newline() << "const unsigned maxactive_p:1;"; | |
b350f56b | 8095 | s.op->newline() << "const unsigned optional_p:1;"; |
e6fe60e7 AM |
8096 | s.op->newline() << "unsigned registered_p:1;"; |
8097 | s.op->newline() << "const unsigned short maxactive_val;"; | |
935447c8 | 8098 | |
e6fe60e7 AM |
8099 | // Function Names are mostly small and uniform enough to justify putting |
8100 | // char[MAX]'s into the array instead of relocated char*'s. | |
935447c8 | 8101 | |
faea5e16 JS |
8102 | size_t symbol_string_name_max = 0; |
8103 | size_t symbol_string_name_tot = 0; | |
e6fe60e7 | 8104 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
6270adc1 | 8105 | { |
e6fe60e7 AM |
8106 | kprobe_derived_probe* p = it->second; |
8107 | #define DOIT(var,expr) do { \ | |
8108 | size_t var##_size = (expr) + 1; \ | |
8109 | var##_max = max (var##_max, var##_size); \ | |
8110 | var##_tot += var##_size; } while (0) | |
e6fe60e7 AM |
8111 | DOIT(symbol_string_name, p->symbol_name.size()); |
8112 | #undef DOIT | |
6270adc1 MH |
8113 | } |
8114 | ||
e6fe60e7 AM |
8115 | #define CALCIT(var) \ |
8116 | s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;"; | |
935447c8 | 8117 | |
e6fe60e7 AM |
8118 | CALCIT(symbol_string); |
8119 | #undef CALCIT | |
6270adc1 | 8120 | |
bd659351 | 8121 | s.op->newline() << "unsigned long address;"; |
26e63673 | 8122 | s.op->newline() << "struct stap_probe * const probe;"; |
e6fe60e7 AM |
8123 | s.op->newline(-1) << "} stap_dwarfless_probes[] = {"; |
8124 | s.op->indent(1); | |
6270adc1 | 8125 | |
e6fe60e7 AM |
8126 | for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++) |
8127 | { | |
8128 | kprobe_derived_probe* p = it->second; | |
8129 | s.op->newline() << "{"; | |
8130 | if (p->has_return) | |
8131 | s.op->line() << " .return_p=1,"; | |
6270adc1 | 8132 | |
e6fe60e7 AM |
8133 | if (p->has_maxactive) |
8134 | { | |
8135 | s.op->line() << " .maxactive_p=1,"; | |
8136 | assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX); | |
8137 | s.op->line() << " .maxactive_val=" << p->maxactive_val << ","; | |
8138 | } | |
6270adc1 | 8139 | |
b350f56b JS |
8140 | if (p->locations[0]->optional) |
8141 | s.op->line() << " .optional_p=1,"; | |
8142 | ||
e6fe60e7 | 8143 | if (p->has_statement) |
c8d9d15e | 8144 | s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,"; |
e6fe60e7 | 8145 | else |
c8d9d15e | 8146 | s.op->line() << " .symbol_string=\"" << p->symbol_name << "\","; |
5d67b47c | 8147 | |
faea5e16 | 8148 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
e6fe60e7 | 8149 | s.op->line() << " },"; |
935447c8 DS |
8150 | } |
8151 | ||
e6fe60e7 | 8152 | s.op->newline(-1) << "};"; |
5d67b47c | 8153 | |
e6fe60e7 AM |
8154 | // Emit the kprobes callback function |
8155 | s.op->newline(); | |
88747011 | 8156 | s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,"; |
e6fe60e7 AM |
8157 | s.op->line() << " struct pt_regs *regs) {"; |
8158 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS | |
8159 | s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8160 | // Check that the index is plausible | |
8161 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8162 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8163 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8164 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8165 | s.op->line() << "];"; | |
71db462b | 8166 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 8167 | "stp_probe_type_kprobe"); |
d9aed31e | 8168 | s.op->newline() << "c->kregs = regs;"; |
6415ddde MW |
8169 | |
8170 | // Make it look like the IP is set as it wouldn't have been replaced | |
8171 | // by a breakpoint instruction when calling real probe handler. Reset | |
8172 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
8173 | s.op->newline() << "{"; | |
8174 | s.op->indent(1); | |
d9aed31e | 8175 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 8176 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);"; |
26e63673 | 8177 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 8178 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
8179 | s.op->newline(-1) << "}"; |
8180 | ||
f887a8c9 | 8181 | common_probe_entryfn_epilogue (s, true); |
e6fe60e7 AM |
8182 | s.op->newline() << "return 0;"; |
8183 | s.op->newline(-1) << "}"; | |
935447c8 | 8184 | |
e6fe60e7 AM |
8185 | // Same for kretprobes |
8186 | s.op->newline(); | |
88747011 | 8187 | s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,"; |
e6fe60e7 AM |
8188 | s.op->line() << " struct pt_regs *regs) {"; |
8189 | s.op->newline(1) << "struct kretprobe *krp = inst->rp;"; | |
935447c8 | 8190 | |
e6fe60e7 AM |
8191 | // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS |
8192 | s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);"; | |
8193 | // Check that the index is plausible | |
8194 | s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes["; | |
8195 | s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?"; | |
8196 | s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption | |
8197 | // XXX: it would be nice to give a more verbose error though; BUG_ON later? | |
8198 | s.op->line() << "];"; | |
935447c8 | 8199 | |
71db462b | 8200 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 8201 | "stp_probe_type_kretprobe"); |
d9aed31e | 8202 | s.op->newline() << "c->kregs = regs;"; |
6dceb5c9 | 8203 | s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic |
6415ddde MW |
8204 | |
8205 | // Make it look like the IP is set as it wouldn't have been replaced | |
8206 | // by a breakpoint instruction when calling real probe handler. Reset | |
8207 | // IP regs on return, so we don't confuse kprobes. PR10458 | |
8208 | s.op->newline() << "{"; | |
8209 | s.op->indent(1); | |
d9aed31e | 8210 | s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);"; |
259d54c0 | 8211 | s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);"; |
26e63673 | 8212 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
259d54c0 | 8213 | s.op->newline() << "SET_REG_IP(regs, kprobes_ip);"; |
6415ddde MW |
8214 | s.op->newline(-1) << "}"; |
8215 | ||
f887a8c9 | 8216 | common_probe_entryfn_epilogue (s, true); |
e6fe60e7 AM |
8217 | s.op->newline() << "return 0;"; |
8218 | s.op->newline(-1) << "}"; | |
bd659351 | 8219 | |
03a4ec63 | 8220 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
bd659351 MW |
8221 | s.op->newline() << "static int kprobe_resolve(void *data, const char *name,"; |
8222 | s.op->newline() << " struct module *owner,"; | |
8223 | s.op->newline() << " unsigned long val) {"; | |
8224 | s.op->newline(1) << "int i;"; | |
fc1d2aa2 MW |
8225 | s.op->newline() << "int *p = (int *) data;"; |
8226 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() | |
8227 | << " && *p > 0; i++) {"; | |
bd659351 MW |
8228 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
8229 | s.op->newline() << "if (! sdp->address)"; | |
fc1d2aa2 | 8230 | s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {"; |
bd659351 | 8231 | s.op->newline(1) << "sdp->address = val;"; |
fc1d2aa2 MW |
8232 | s.op->newline() << "(*p)--;"; |
8233 | s.op->newline(-1) << "}"; | |
8234 | s.op->newline(-2) << "}"; | |
8235 | s.op->newline() << "return (p > 0) ? 0 : -1;"; | |
bd659351 | 8236 | s.op->newline(-1) << "}"; |
03a4ec63 | 8237 | s.op->newline() << "#endif"; |
935447c8 DS |
8238 | } |
8239 | ||
e6fe60e7 | 8240 | |
6270adc1 | 8241 | void |
e6fe60e7 | 8242 | kprobe_derived_probe_group::emit_module_init (systemtap_session& s) |
6270adc1 | 8243 | { |
03a4ec63 | 8244 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; |
fc1d2aa2 MW |
8245 | s.op->newline() << "{"; |
8246 | s.op->newline(1) << "int p = 0;"; | |
8247 | s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {"; | |
8248 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8249 | s.op->newline() << "if (! sdp->address)"; | |
8250 | s.op->newline(1) << "p++;"; | |
8251 | s.op->newline(-2) << "}"; | |
8252 | s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);"; | |
8253 | s.op->newline(-1) << "}"; | |
03a4ec63 | 8254 | s.op->newline() << "#endif"; |
bd659351 | 8255 | |
e6fe60e7 | 8256 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
c8d9d15e | 8257 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; |
e6fe60e7 | 8258 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; |
c8d9d15e | 8259 | s.op->newline() << "void *addr = (void *) sdp->address;"; |
03a4ec63 MW |
8260 | s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;"; |
8261 | ||
8262 | s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL"; | |
bd659351 MW |
8263 | s.op->newline() << "if (! addr) {"; |
8264 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
9319b767 MW |
8265 | s.op->newline() << "if (!sdp->optional_p)"; |
8266 | s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);"; | |
8267 | s.op->newline(-1) << "continue;"; | |
bd659351 | 8268 | s.op->newline(-1) << "}"; |
03a4ec63 MW |
8269 | s.op->newline() << "#endif"; |
8270 | ||
26e63673 | 8271 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
e6fe60e7 | 8272 | s.op->newline() << "if (sdp->return_p) {"; |
c8d9d15e | 8273 | s.op->newline(1) << "kp->u.krp.kp.addr = addr;"; |
766cee5f | 8274 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 8275 | s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 8276 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
8277 | s.op->newline() << "if (sdp->maxactive_p) {"; |
8278 | s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;"; | |
8279 | s.op->newline(-1) << "} else {"; | |
f07c3b68 | 8280 | s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;"; |
e6fe60e7 | 8281 | s.op->newline(-1) << "}"; |
88747011 | 8282 | s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;"; |
e6fe60e7 AM |
8283 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 |
8284 | s.op->newline() << "#ifdef __ia64__"; | |
e6fe60e7 | 8285 | s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;"; |
766cee5f | 8286 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 8287 | s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;"; |
766cee5f | 8288 | s.op->newline() << "#endif"; |
c8d9d15e | 8289 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
e6fe60e7 AM |
8290 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
8291 | s.op->newline() << "if (rc == 0) {"; | |
8292 | s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);"; | |
8293 | s.op->newline() << "if (rc != 0)"; | |
8294 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
8295 | s.op->newline(-2) << "}"; | |
8296 | s.op->newline() << "#else"; | |
8297 | s.op->newline() << "rc = register_kretprobe (& kp->u.krp);"; | |
8298 | s.op->newline() << "#endif"; | |
8299 | s.op->newline(-1) << "} else {"; | |
8300 | // to ensure safeness of bspcache, always use aggr_kprobe on ia64 | |
c8d9d15e | 8301 | s.op->newline(1) << "kp->u.kp.addr = addr;"; |
766cee5f | 8302 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
03a4ec63 | 8303 | s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;"; |
766cee5f | 8304 | s.op->newline() << "#endif"; |
88747011 | 8305 | s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;"; |
e6fe60e7 | 8306 | s.op->newline() << "#ifdef __ia64__"; |
e6fe60e7 | 8307 | s.op->newline() << "kp->dummy.pre_handler = NULL;"; |
c8d9d15e | 8308 | s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;"; |
766cee5f | 8309 | s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME"; |
c8d9d15e | 8310 | s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;"; |
766cee5f | 8311 | s.op->newline() << "#endif"; |
e6fe60e7 AM |
8312 | s.op->newline() << "rc = register_kprobe (& kp->dummy);"; |
8313 | s.op->newline() << "if (rc == 0) {"; | |
8314 | s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);"; | |
8315 | s.op->newline() << "if (rc != 0)"; | |
8316 | s.op->newline(1) << "unregister_kprobe (& kp->dummy);"; | |
8317 | s.op->newline(-2) << "}"; | |
8318 | s.op->newline() << "#else"; | |
8319 | s.op->newline() << "rc = register_kprobe (& kp->u.kp);"; | |
8320 | s.op->newline() << "#endif"; | |
8321 | s.op->newline(-1) << "}"; | |
8322 | s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe. | |
8323 | s.op->newline(1) << "sdp->registered_p = 0;"; | |
b350f56b | 8324 | s.op->newline() << "if (!sdp->optional_p)"; |
50b6acf7 | 8325 | s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);"; |
b350f56b | 8326 | s.op->newline(-1) << "rc = 0;"; // continue with other probes |
e6fe60e7 AM |
8327 | // XXX: shall we increment numskipped? |
8328 | s.op->newline(-1) << "}"; | |
6270adc1 | 8329 | |
e6fe60e7 AM |
8330 | s.op->newline() << "else sdp->registered_p = 1;"; |
8331 | s.op->newline(-1) << "}"; // for loop | |
6270adc1 MH |
8332 | } |
8333 | ||
b4be7cbc | 8334 | |
e6fe60e7 AM |
8335 | void |
8336 | kprobe_derived_probe_group::emit_module_exit (systemtap_session& s) | |
935447c8 | 8337 | { |
e6fe60e7 AM |
8338 | //Unregister kprobes by batch interfaces. |
8339 | s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8340 | s.op->newline() << "j = 0;"; | |
8341 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8342 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8343 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8344 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8345 | s.op->newline() << "if (!sdp->return_p)"; | |
c9116e99 | 8346 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;"; |
e6fe60e7 | 8347 | s.op->newline(-2) << "}"; |
c9116e99 | 8348 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8349 | s.op->newline() << "j = 0;"; |
8350 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8351 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8352 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8353 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8354 | s.op->newline() << "if (sdp->return_p)"; | |
c9116e99 | 8355 | s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;"; |
e6fe60e7 | 8356 | s.op->newline(-2) << "}"; |
c9116e99 | 8357 | s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8358 | s.op->newline() << "#ifdef __ia64__"; |
8359 | s.op->newline() << "j = 0;"; | |
8360 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; | |
8361 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8362 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8363 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
c9116e99 | 8364 | s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;"; |
e6fe60e7 | 8365 | s.op->newline(-1) << "}"; |
c9116e99 | 8366 | s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);"; |
e6fe60e7 AM |
8367 | s.op->newline() << "#endif"; |
8368 | s.op->newline() << "#endif"; | |
3e3bd7b6 | 8369 | |
e6fe60e7 AM |
8370 | s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {"; |
8371 | s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];"; | |
8372 | s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];"; | |
8373 | s.op->newline() << "if (! sdp->registered_p) continue;"; | |
8374 | s.op->newline() << "if (sdp->return_p) {"; | |
8375 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8376 | s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);"; | |
8377 | s.op->newline() << "#endif"; | |
065d5567 | 8378 | s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());"; |
e6fe60e7 AM |
8379 | s.op->newline() << "#ifdef STP_TIMING"; |
8380 | s.op->newline() << "if (kp->u.krp.nmissed)"; | |
26e63673 | 8381 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);"; |
e6fe60e7 | 8382 | s.op->newline(-1) << "#endif"; |
065d5567 | 8383 | s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());"; |
e6fe60e7 AM |
8384 | s.op->newline() << "#ifdef STP_TIMING"; |
8385 | s.op->newline() << "if (kp->u.krp.kp.nmissed)"; | |
26e63673 | 8386 | 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 |
8387 | s.op->newline(-1) << "#endif"; |
8388 | s.op->newline(-1) << "} else {"; | |
8389 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)"; | |
8390 | s.op->newline(1) << "unregister_kprobe (&kp->u.kp);"; | |
8391 | s.op->newline() << "#endif"; | |
065d5567 | 8392 | s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());"; |
e6fe60e7 AM |
8393 | s.op->newline() << "#ifdef STP_TIMING"; |
8394 | s.op->newline() << "if (kp->u.kp.nmissed)"; | |
26e63673 | 8395 | s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);"; |
e6fe60e7 AM |
8396 | s.op->newline(-1) << "#endif"; |
8397 | s.op->newline(-1) << "}"; | |
8398 | s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)"; | |
8399 | s.op->newline() << "unregister_kprobe (&kp->dummy);"; | |
8400 | s.op->newline() << "#endif"; | |
8401 | s.op->newline() << "sdp->registered_p = 0;"; | |
8402 | s.op->newline(-1) << "}"; | |
f8a968bc JS |
8403 | } |
8404 | ||
e6fe60e7 | 8405 | struct kprobe_builder: public derived_probe_builder |
3c1b3d06 | 8406 | { |
9fdf787d | 8407 | public: |
2a639817 | 8408 | kprobe_builder() {} |
9fdf787d | 8409 | |
2a639817 | 8410 | void build_no_more (systemtap_session &s) {} |
9fdf787d | 8411 | |
e6fe60e7 AM |
8412 | virtual void build(systemtap_session & sess, |
8413 | probe * base, | |
8414 | probe_point * location, | |
8415 | literal_map_t const & parameters, | |
8416 | vector<derived_probe *> & finished_results); | |
8417 | }; | |
3c1b3d06 FCE |
8418 | |
8419 | ||
79189b84 | 8420 | void |
05fb3e0c | 8421 | kprobe_builder::build(systemtap_session & sess, |
e6fe60e7 AM |
8422 | probe * base, |
8423 | probe_point * location, | |
8424 | literal_map_t const & parameters, | |
8425 | vector<derived_probe *> & finished_results) | |
79189b84 | 8426 | { |
e6fe60e7 | 8427 | string function_string_val, module_string_val; |
05fb3e0c | 8428 | string path, library, path_tgt, library_tgt; |
b6371390 JS |
8429 | int64_t statement_num_val = 0, maxactive_val = 0; |
8430 | bool has_function_str, has_module_str, has_statement_num; | |
8431 | bool has_absolute, has_return, has_maxactive; | |
b642c901 | 8432 | bool has_path, has_library; |
79189b84 | 8433 | |
b6371390 JS |
8434 | has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val); |
8435 | has_module_str = get_param(parameters, TOK_MODULE, module_string_val); | |
8436 | has_return = has_null_param (parameters, TOK_RETURN); | |
8437 | has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val); | |
8438 | has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val); | |
8439 | has_absolute = has_null_param (parameters, TOK_ABSOLUTE); | |
b642c901 SC |
8440 | has_path = get_param (parameters, TOK_PROCESS, path); |
8441 | has_library = get_param (parameters, TOK_LIBRARY, library); | |
8442 | ||
8443 | if (has_path) | |
05fb3e0c WF |
8444 | { |
8445 | path = find_executable (path, sess.sysroot, sess.sysenv); | |
8446 | path_tgt = path_remove_sysroot(sess, path); | |
8447 | } | |
b642c901 | 8448 | if (has_library) |
05fb3e0c WF |
8449 | { |
8450 | library = find_executable (library, sess.sysroot, sess.sysenv, | |
8451 | "LD_LIBRARY_PATH"); | |
8452 | library_tgt = path_remove_sysroot(sess, library); | |
8453 | } | |
c57ea854 | 8454 | |
b6371390 | 8455 | if (has_function_str) |
6fb70fb7 | 8456 | { |
2a639817 | 8457 | if (has_module_str) |
9fdf787d DS |
8458 | { |
8459 | function_string_val = module_string_val + ":" + function_string_val; | |
8460 | derived_probe *dp | |
23dc94f6 DS |
8461 | = new kprobe_derived_probe (sess, finished_results, base, |
8462 | location, function_string_val, | |
9fdf787d DS |
8463 | 0, has_return, has_statement_num, |
8464 | has_maxactive, has_path, has_library, | |
8465 | maxactive_val, path_tgt, library_tgt); | |
8466 | finished_results.push_back (dp); | |
8467 | } | |
8468 | else | |
8469 | { | |
2a639817 JS |
8470 | vector<string> matches; |
8471 | ||
8472 | // Simple names can be found directly | |
8473 | if (function_string_val.find_first_of("*?[") == string::npos) | |
8474 | { | |
8475 | if (sess.kernel_functions.count(function_string_val)) | |
8476 | matches.push_back(function_string_val); | |
8477 | } | |
8478 | else // Search function name list for matching names | |
8479 | { | |
8480 | for (set<string>::const_iterator it = sess.kernel_functions.begin(); | |
8481 | it != sess.kernel_functions.end(); it++) | |
8482 | // fnmatch returns zero for matching. | |
8483 | if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0) | |
8484 | matches.push_back(*it); | |
8485 | } | |
86758d5f | 8486 | |
2a639817 JS |
8487 | for (vector<string>::const_iterator it = matches.begin(); |
8488 | it != matches.end(); it++) | |
9fdf787d | 8489 | { |
2a639817 JS |
8490 | derived_probe *dp |
8491 | = new kprobe_derived_probe (sess, finished_results, base, | |
8492 | location, *it, 0, has_return, | |
8493 | has_statement_num, | |
8494 | has_maxactive, has_path, | |
8495 | has_library, maxactive_val, | |
8496 | path_tgt, library_tgt); | |
8497 | finished_results.push_back (dp); | |
9fdf787d DS |
8498 | } |
8499 | } | |
6fb70fb7 | 8500 | } |
e6fe60e7 | 8501 | else |
b6371390 JS |
8502 | { |
8503 | // assert guru mode for absolute probes | |
8504 | if ( has_statement_num && has_absolute && !base->privileged ) | |
e3bbc038 | 8505 | throw semantic_error (_("absolute statement probe in unprivileged script; need stap -g"), base->tok); |
b6371390 | 8506 | |
23dc94f6 DS |
8507 | finished_results.push_back (new kprobe_derived_probe (sess, |
8508 | finished_results, | |
8509 | base, | |
b6371390 JS |
8510 | location, "", |
8511 | statement_num_val, | |
8512 | has_return, | |
8513 | has_statement_num, | |
8514 | has_maxactive, | |
b642c901 SC |
8515 | has_path, |
8516 | has_library, | |
8517 | maxactive_val, | |
05fb3e0c WF |
8518 | path_tgt, |
8519 | library_tgt)); | |
96b030fe | 8520 | } |
79189b84 JS |
8521 | } |
8522 | ||
23dc94f6 DS |
8523 | |
8524 | void | |
8525 | kprobe_var_expanding_visitor::visit_entry_op (entry_op *e) | |
8526 | { | |
8527 | expression *repl = e; | |
8528 | ||
bd5b25e1 JS |
8529 | if (has_return) |
8530 | { | |
8531 | // expand the operand as if it weren't a return probe | |
8532 | has_return = false; | |
8533 | replace (e->operand); | |
8534 | has_return = true; | |
23dc94f6 | 8535 | |
bd5b25e1 JS |
8536 | // XXX it would be nice to use gen_kretprobe_saved_return when |
8537 | // available, but it requires knowing the types already, which is | |
8538 | // problematic for arbitrary expressons. | |
8539 | repl = gen_mapped_saved_return (sess, e->operand, "entry", | |
8540 | add_block, add_block_tid, | |
8541 | add_call_probe, add_call_probe_tid); | |
8542 | } | |
23dc94f6 DS |
8543 | provide (repl); |
8544 | } | |
8545 | ||
8546 | ||
dd225250 PS |
8547 | // ------------------------------------------------------------------------ |
8548 | // Hardware breakpoint based probes. | |
8549 | // ------------------------------------------------------------------------ | |
8550 | ||
8551 | static const string TOK_HWBKPT("data"); | |
8552 | static const string TOK_HWBKPT_WRITE("write"); | |
8553 | static const string TOK_HWBKPT_RW("rw"); | |
8554 | static const string TOK_LENGTH("length"); | |
8555 | ||
8556 | #define HWBKPT_READ 0 | |
8557 | #define HWBKPT_WRITE 1 | |
8558 | #define HWBKPT_RW 2 | |
8559 | struct hwbkpt_derived_probe: public derived_probe | |
8560 | { | |
8561 | hwbkpt_derived_probe (probe *base, | |
8562 | probe_point *location, | |
8563 | uint64_t addr, | |
8564 | string symname, | |
8565 | unsigned int len, | |
8566 | bool has_only_read_access, | |
8567 | bool has_only_write_access, | |
8568 | bool has_rw_access | |
8569 | ); | |
8570 | Dwarf_Addr hwbkpt_addr; | |
8571 | string symbol_name; | |
8572 | unsigned int hwbkpt_access,hwbkpt_len; | |
8573 | ||
8574 | void printsig (std::ostream &o) const; | |
8575 | void join_group (systemtap_session& s); | |
8576 | }; | |
8577 | ||
8578 | struct hwbkpt_derived_probe_group: public derived_probe_group | |
8579 | { | |
dd225250 | 8580 | private: |
dac77b80 | 8581 | vector<hwbkpt_derived_probe*> hwbkpt_probes; |
dd225250 PS |
8582 | |
8583 | public: | |
8584 | void enroll (hwbkpt_derived_probe* probe, systemtap_session& s); | |
8585 | void emit_module_decls (systemtap_session& s); | |
8586 | void emit_module_init (systemtap_session& s); | |
8587 | void emit_module_exit (systemtap_session& s); | |
8588 | }; | |
8589 | ||
8590 | hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base, | |
9ea68eb9 JS |
8591 | probe_point *location, |
8592 | uint64_t addr, | |
8593 | string symname, | |
8594 | unsigned int len, | |
8595 | bool has_only_read_access, | |
8596 | bool has_only_write_access, | |
822a6a3d | 8597 | bool): |
4c5d1300 | 8598 | derived_probe (base, location, true /* .components soon rewritten */ ), |
dd225250 PS |
8599 | hwbkpt_addr (addr), |
8600 | symbol_name (symname), | |
8601 | hwbkpt_len (len) | |
8602 | { | |
8603 | this->tok = base->tok; | |
8604 | ||
8605 | vector<probe_point::component*> comps; | |
8606 | comps.push_back (new probe_point::component(TOK_KERNEL)); | |
8607 | ||
8608 | if (hwbkpt_addr) | |
9ea68eb9 JS |
8609 | comps.push_back (new probe_point::component (TOK_HWBKPT, |
8610 | new literal_number(hwbkpt_addr, true))); | |
8611 | else if (symbol_name.size()) | |
8612 | comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name))); | |
dd225250 PS |
8613 | |
8614 | comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len))); | |
8615 | ||
8616 | if (has_only_read_access) | |
9ea68eb9 | 8617 | this->hwbkpt_access = HWBKPT_READ ; |
dd225250 PS |
8618 | //TODO add code for comps.push_back for read, since this flag is not for x86 |
8619 | ||
8620 | else | |
9ea68eb9 JS |
8621 | { |
8622 | if (has_only_write_access) | |
8623 | { | |
8624 | this->hwbkpt_access = HWBKPT_WRITE ; | |
8625 | comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE)); | |
8626 | } | |
8627 | else | |
8628 | { | |
8629 | this->hwbkpt_access = HWBKPT_RW ; | |
8630 | comps.push_back (new probe_point::component(TOK_HWBKPT_RW)); | |
8631 | } | |
8632 | } | |
dd225250 PS |
8633 | |
8634 | this->sole_location()->components = comps; | |
8635 | } | |
8636 | ||
8637 | void hwbkpt_derived_probe::printsig (ostream& o) const | |
8638 | { | |
8639 | sole_location()->print (o); | |
8640 | printsig_nested (o); | |
8641 | } | |
8642 | ||
8643 | void hwbkpt_derived_probe::join_group (systemtap_session& s) | |
8644 | { | |
dac77b80 FCE |
8645 | if (! s.hwbkpt_derived_probes) |
8646 | s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group (); | |
dd225250 PS |
8647 | s.hwbkpt_derived_probes->enroll (this, s); |
8648 | } | |
8649 | ||
8650 | void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s) | |
8651 | { | |
dac77b80 FCE |
8652 | hwbkpt_probes.push_back (p); |
8653 | ||
8654 | unsigned max_hwbkpt_probes_by_arch = 0; | |
8655 | if (s.architecture == "i386" || s.architecture == "x86_64") | |
8656 | max_hwbkpt_probes_by_arch = 4; | |
8657 | else if (s.architecture == "s390") | |
8658 | max_hwbkpt_probes_by_arch = 1; | |
8659 | ||
c57ea854 | 8660 | if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch) |
2713ea24 | 8661 | s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)", |
b530b5b3 | 8662 | s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch)); |
dd225250 PS |
8663 | } |
8664 | ||
8665 | void | |
8666 | hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s) | |
8667 | { | |
dac77b80 | 8668 | if (hwbkpt_probes.empty()) return; |
dd225250 PS |
8669 | |
8670 | s.op->newline() << "/* ---- hwbkpt-based probes ---- */"; | |
8671 | ||
8672 | s.op->newline() << "#include <linux/perf_event.h>"; | |
8673 | s.op->newline() << "#include <linux/hw_breakpoint.h>"; | |
8674 | s.op->newline(); | |
8675 | ||
8676 | // Forward declare the master entry functions | |
8677 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
8678 | s.op->line() << " int nmi,"; | |
8679 | s.op->line() << " struct perf_sample_data *data,"; | |
8680 | s.op->line() << " struct pt_regs *regs);"; | |
79189b84 | 8681 | |
dd225250 PS |
8682 | // Emit the actual probe list. |
8683 | ||
8684 | s.op->newline() << "static struct perf_event_attr "; | |
dac77b80 | 8685 | s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
8686 | |
8687 | s.op->newline() << "static struct perf_event **"; | |
dac77b80 | 8688 | s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];"; |
dd225250 PS |
8689 | s.op->newline() << "static struct stap_hwbkpt_probe {"; |
8690 | s.op->newline() << "int registered_p:1;"; | |
43650b10 | 8691 | // registered_p = 0 signifies a probe that is unregistered (or failed) |
dd225250 PS |
8692 | // registered_p = 1 signifies a probe that got registered successfully |
8693 | ||
faea5e16 | 8694 | // Symbol Names are mostly small and uniform enough |
dd225250 | 8695 | // to justify putting const char*. |
dac77b80 | 8696 | s.op->newline() << "const char * const symbol;"; |
dd225250 PS |
8697 | |
8698 | s.op->newline() << "const unsigned long address;"; | |
8699 | s.op->newline() << "uint8_t atype;"; | |
bb0a4e12 | 8700 | s.op->newline() << "unsigned int len;"; |
26e63673 | 8701 | s.op->newline() << "struct stap_probe * const probe;"; |
dd225250 PS |
8702 | s.op->newline() << "} stap_hwbkpt_probes[] = {"; |
8703 | s.op->indent(1); | |
8704 | ||
dac77b80 | 8705 | for (unsigned int it = 0; it < hwbkpt_probes.size(); it++) |
dd225250 | 8706 | { |
dac77b80 | 8707 | hwbkpt_derived_probe* p = hwbkpt_probes.at(it); |
dd225250 | 8708 | s.op->newline() << "{"; |
dd225250 PS |
8709 | if (p->symbol_name.size()) |
8710 | s.op->line() << " .address=(unsigned long)0x0" << "ULL,"; | |
8711 | else | |
8712 | s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,"; | |
8713 | switch(p->hwbkpt_access){ | |
8714 | case HWBKPT_READ: | |
8715 | s.op->line() << " .atype=HW_BREAKPOINT_R ,"; | |
bb0a4e12 | 8716 | break; |
dd225250 PS |
8717 | case HWBKPT_WRITE: |
8718 | s.op->line() << " .atype=HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 8719 | break; |
dd225250 PS |
8720 | case HWBKPT_RW: |
8721 | s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,"; | |
bb0a4e12 | 8722 | break; |
dd225250 PS |
8723 | }; |
8724 | s.op->line() << " .len=" << p->hwbkpt_len << ","; | |
faea5e16 | 8725 | s.op->line() << " .probe=" << common_probe_init (p) << ","; |
dd225250 | 8726 | s.op->line() << " .symbol=\"" << p->symbol_name << "\","; |
dd225250 PS |
8727 | s.op->line() << " },"; |
8728 | } | |
dac77b80 | 8729 | s.op->newline(-1) << "};"; |
dd225250 PS |
8730 | |
8731 | // Emit the hwbkpt callback function | |
8732 | s.op->newline() ; | |
8733 | s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,"; | |
8734 | s.op->line() << " int nmi,"; | |
8735 | s.op->line() << " struct perf_sample_data *data,"; | |
8736 | s.op->line() << " struct pt_regs *regs) {"; | |
dac77b80 FCE |
8737 | s.op->newline(1) << "unsigned int i;"; |
8738 | s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;"; | |
8739 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; | |
8740 | s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
8741 | // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead? | |
8742 | 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) {"; | |
8743 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];"; | |
71db462b | 8744 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe", |
cda141c2 | 8745 | "stp_probe_type_hwbkpt"); |
d9aed31e | 8746 | s.op->newline() << "if (user_mode(regs)) {"; |
e04b5d74 | 8747 | s.op->newline(1)<< "c->user_mode_p = 1;"; |
d9aed31e MW |
8748 | s.op->newline() << "c->uregs = regs;"; |
8749 | s.op->newline(-1) << "} else {"; | |
8750 | s.op->newline(1) << "c->kregs = regs;"; | |
8751 | s.op->newline(-1) << "}"; | |
26e63673 | 8752 | s.op->newline() << "(*sdp->probe->ph) (c);"; |
f887a8c9 | 8753 | common_probe_entryfn_epilogue (s, true); |
dac77b80 | 8754 | s.op->newline(-1) << "}"; |
dd225250 PS |
8755 | s.op->newline(-1) << "}"; |
8756 | s.op->newline() << "return 0;"; | |
dac77b80 | 8757 | s.op->newline(-1) << "}"; |
dd225250 PS |
8758 | } |
8759 | ||
8760 | void | |
8761 | hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s) | |
8762 | { | |
dac77b80 | 8763 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 PS |
8764 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
8765 | s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];"; | |
8766 | s.op->newline() << "void *addr = (void *) sdp->address;"; | |
8767 | s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;"; | |
dac77b80 FCE |
8768 | s.op->newline() << "hw_breakpoint_init(hp);"; |
8769 | s.op->newline() << "if (addr)"; | |
8770 | s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;"; | |
8771 | s.op->newline(-1) << "else { "; | |
8772 | s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);"; | |
8773 | s.op->newline() << "if (!hp->bp_addr) { "; | |
26e63673 | 8774 | s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);"; |
dac77b80 FCE |
8775 | s.op->newline() << "continue;"; |
8776 | s.op->newline(-1) << "}"; | |
8777 | s.op->newline(-1) << "}"; | |
8778 | s.op->newline() << "hp->bp_type = sdp->atype;"; | |
8779 | ||
8780 | // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!). | |
c57ea854 | 8781 | if (s.architecture == "i386" || s.architecture == "x86_64" ) |
dac77b80 FCE |
8782 | { |
8783 | s.op->newline() << "switch(sdp->len) {"; | |
8784 | s.op->newline() << "case 1:"; | |
8785 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;"; | |
8786 | s.op->newline() << "break;"; | |
8787 | s.op->newline(-1) << "case 2:"; | |
8788 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;"; | |
8789 | s.op->newline() << "break;"; | |
8790 | s.op->newline(-1) << "case 3:"; | |
8791 | s.op->newline() << "case 4:"; | |
8792 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;"; | |
8793 | s.op->newline() << "break;"; | |
8794 | s.op->newline(-1) << "case 5:"; | |
8795 | s.op->newline() << "case 6:"; | |
8796 | s.op->newline() << "case 7:"; | |
8797 | s.op->newline() << "case 8:"; | |
8798 | s.op->newline() << "default:"; // XXX: could instead reject | |
8799 | s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;"; | |
8800 | s.op->newline() << "break;"; | |
8801 | s.op->newline(-1) << "}"; | |
8802 | } | |
8803 | else // other architectures presumed straightforward | |
8804 | s.op->newline() << "hp->bp_len = sdp->len;"; | |
8805 | ||
26e63673 | 8806 | s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages |
b273669e MW |
8807 | s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT"; |
8808 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);"; | |
8809 | s.op->newline() << "#else"; | |
dac77b80 | 8810 | s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);"; |
b273669e | 8811 | s.op->newline() << "#endif"; |
43650b10 | 8812 | s.op->newline() << "rc = 0;"; |
dac77b80 | 8813 | s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {"; |
43650b10 WC |
8814 | s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);"; |
8815 | s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;"; | |
8816 | s.op->newline(-1) << "}"; | |
217ef1f4 WC |
8817 | s.op->newline() << "if (rc) {"; |
8818 | s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);"; | |
43650b10 | 8819 | s.op->newline() << "sdp->registered_p = 0;"; |
dac77b80 | 8820 | s.op->newline(-1) << "}"; |
dd225250 | 8821 | s.op->newline() << " else sdp->registered_p = 1;"; |
dd225250 PS |
8822 | s.op->newline(-1) << "}"; // for loop |
8823 | } | |
8824 | ||
8825 | void | |
8826 | hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s) | |
8827 | { | |
8828 | //Unregister hwbkpt probes. | |
dac77b80 | 8829 | s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {"; |
dd225250 | 8830 | s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];"; |
dac77b80 FCE |
8831 | s.op->newline() << "if (sdp->registered_p == 0) continue;"; |
8832 | s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);"; | |
dd225250 PS |
8833 | s.op->newline() << "sdp->registered_p = 0;"; |
8834 | s.op->newline(-1) << "}"; | |
8835 | } | |
8836 | ||
8837 | struct hwbkpt_builder: public derived_probe_builder | |
8838 | { | |
8839 | hwbkpt_builder() {} | |
8840 | virtual void build(systemtap_session & sess, | |
8841 | probe * base, | |
8842 | probe_point * location, | |
8843 | literal_map_t const & parameters, | |
8844 | vector<derived_probe *> & finished_results); | |
8845 | }; | |
8846 | ||
8847 | void | |
8848 | hwbkpt_builder::build(systemtap_session & sess, | |
8849 | probe * base, | |
8850 | probe_point * location, | |
8851 | literal_map_t const & parameters, | |
8852 | vector<derived_probe *> & finished_results) | |
8853 | { | |
8854 | string symbol_str_val; | |
8855 | int64_t hwbkpt_address, len; | |
8856 | bool has_addr, has_symbol_str, has_write, has_rw, has_len; | |
8857 | ||
b47f3a55 | 8858 | if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y"))) |
b530b5b3 | 8859 | throw semantic_error (_("CONFIG_PERF_EVENTS not available on this kernel"), |
b47f3a55 FCE |
8860 | location->components[0]->tok); |
8861 | if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y"))) | |
b530b5b3 | 8862 | throw semantic_error (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"), |
b47f3a55 FCE |
8863 | location->components[0]->tok); |
8864 | ||
dd225250 PS |
8865 | has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address); |
8866 | has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val); | |
8867 | has_len = get_param (parameters, TOK_LENGTH, len); | |
8868 | has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end()); | |
8869 | has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end()); | |
8870 | ||
8871 | if (!has_len) | |
8872 | len = 1; | |
8873 | ||
8874 | if (has_addr) | |
8875 | finished_results.push_back (new hwbkpt_derived_probe (base, | |
8876 | location, | |
8877 | hwbkpt_address, | |
8878 | "",len,0, | |
8879 | has_write, | |
8880 | has_rw)); | |
5d8a0aea | 8881 | else if (has_symbol_str) |
dd225250 PS |
8882 | finished_results.push_back (new hwbkpt_derived_probe (base, |
8883 | location, | |
8884 | 0, | |
8885 | symbol_str_val,len,0, | |
8886 | has_write, | |
8887 | has_rw)); | |
5d8a0aea FCE |
8888 | else |
8889 | assert (0); | |
dd225250 | 8890 | } |
342d3f96 | 8891 | |
0a6f5a3f JS |
8892 | // ------------------------------------------------------------------------ |
8893 | // statically inserted kernel-tracepoint derived probes | |
8894 | // ------------------------------------------------------------------------ | |
8895 | ||
6fb70fb7 | 8896 | struct tracepoint_arg |
79189b84 | 8897 | { |
ad370dcc | 8898 | string name, c_type, typecast; |
dcaa1a65 | 8899 | bool usable, used, isptr; |
f8a968bc | 8900 | Dwarf_Die type_die; |
dcaa1a65 | 8901 | tracepoint_arg(): usable(false), used(false), isptr(false) {} |
6fb70fb7 | 8902 | }; |
79189b84 | 8903 | |
0a6f5a3f JS |
8904 | struct tracepoint_derived_probe: public derived_probe |
8905 | { | |
79189b84 JS |
8906 | tracepoint_derived_probe (systemtap_session& s, |
8907 | dwflpp& dw, Dwarf_Die& func_die, | |
8908 | const string& tracepoint_name, | |
8909 | probe* base_probe, probe_point* location); | |
bc9a523d | 8910 | |
79189b84 | 8911 | systemtap_session& sess; |
6fb70fb7 JS |
8912 | string tracepoint_name, header; |
8913 | vector <struct tracepoint_arg> args; | |
bc9a523d | 8914 | |
6fb70fb7 | 8915 | void build_args(dwflpp& dw, Dwarf_Die& func_die); |
d0bfd2ac | 8916 | void getargs (std::list<std::string> &arg_set) const; |
79189b84 | 8917 | void join_group (systemtap_session& s); |
3e3bd7b6 | 8918 | void print_dupe_stamp(ostream& o); |
0a6f5a3f | 8919 | }; |
79189b84 JS |
8920 | |
8921 | ||
0a6f5a3f | 8922 | struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe> |
79189b84 | 8923 | { |
79189b84 JS |
8924 | void emit_module_decls (systemtap_session& s); |
8925 | void emit_module_init (systemtap_session& s); | |
8926 | void emit_module_exit (systemtap_session& s); | |
0a6f5a3f | 8927 | }; |
79189b84 | 8928 | |
bc9a523d | 8929 | |
f8a968bc JS |
8930 | struct tracepoint_var_expanding_visitor: public var_expanding_visitor |
8931 | { | |
8932 | tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name, | |
8933 | vector <struct tracepoint_arg>& args): | |
8934 | dw (dw), probe_name (probe_name), args (args) {} | |
8935 | dwflpp& dw; | |
8936 | const string& probe_name; | |
8937 | vector <struct tracepoint_arg>& args; | |
bc9a523d | 8938 | |
f8a968bc JS |
8939 | void visit_target_symbol (target_symbol* e); |
8940 | void visit_target_symbol_arg (target_symbol* e); | |
8941 | void visit_target_symbol_context (target_symbol* e); | |
8942 | }; | |
79189b84 JS |
8943 | |
8944 | ||
f8a968bc JS |
8945 | void |
8946 | tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e) | |
75ead1f7 | 8947 | { |
cc9001af | 8948 | string argname = e->sym_name(); |
75ead1f7 | 8949 | |
f8a968bc JS |
8950 | // search for a tracepoint parameter matching this name |
8951 | tracepoint_arg *arg = NULL; | |
8952 | for (unsigned i = 0; i < args.size(); ++i) | |
dcaa1a65 | 8953 | if (args[i].usable && args[i].name == argname) |
f8a968bc JS |
8954 | { |
8955 | arg = &args[i]; | |
8956 | arg->used = true; | |
8957 | break; | |
8958 | } | |
75ead1f7 | 8959 | |
f8a968bc JS |
8960 | if (arg == NULL) |
8961 | { | |
8962 | stringstream alternatives; | |
8963 | for (unsigned i = 0; i < args.size(); ++i) | |
8964 | alternatives << " $" << args[i].name; | |
046e7190 | 8965 | alternatives << " $$name $$parms $$vars"; |
75ead1f7 | 8966 | |
f8a968bc JS |
8967 | // We hope that this value ends up not being referenced after all, so it |
8968 | // can be optimized out quietly. | |
b530b5b3 LB |
8969 | throw semantic_error(_F("unable to find tracepoint variable '%s' (alternatives: %s)", |
8970 | e->name.c_str(), alternatives.str().c_str()), e->tok); | |
f8a968bc JS |
8971 | // NB: we can have multiple errors, since a target variable |
8972 | // may be expanded in several different contexts: | |
8973 | // trace ("*") { $foo->bar } | |
f8a968bc | 8974 | } |
75ead1f7 | 8975 | |
f8a968bc | 8976 | // make sure we're not dereferencing base types |
dc5a09fc | 8977 | if (!arg->isptr) |
d19a9a82 | 8978 | e->assert_no_components("tracepoint", true); |
75ead1f7 | 8979 | |
f8a968bc JS |
8980 | // we can only write to dereferenced fields, and only if guru mode is on |
8981 | bool lvalue = is_active_lvalue(e); | |
8982 | if (lvalue && (!dw.sess.guru_mode || e->components.empty())) | |
b3741c9d | 8983 | throw semantic_error(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok); |
c69a87e0 | 8984 | |
ad370dcc JS |
8985 | // XXX: if a struct/union arg is passed by value, then writing to its fields |
8986 | // is also meaningless until you dereference past a pointer member. It's | |
8987 | // harder to detect and prevent that though... | |
75ead1f7 | 8988 | |
f8a968bc JS |
8989 | if (e->components.empty()) |
8990 | { | |
03c75a4a | 8991 | if (e->addressof) |
b530b5b3 | 8992 | throw semantic_error(_("cannot take address of tracepoint variable"), e->tok); |
a45664f4 | 8993 | |
3e3bd7b6 | 8994 | // Just grab the value from the probe locals |
a45664f4 JS |
8995 | symbol* sym = new symbol; |
8996 | sym->tok = e->tok; | |
8997 | sym->name = "__tracepoint_arg_" + arg->name; | |
8998 | provide (sym); | |
f8a968bc JS |
8999 | } |
9000 | else | |
9001 | { | |
5f36109e JS |
9002 | // make a copy of the original as a bare target symbol for the tracepoint |
9003 | // value, which will be passed into the dwarf dereferencing code | |
9004 | target_symbol* e2 = deep_copy_visitor::deep_copy(e); | |
9005 | e2->components.clear(); | |
9006 | ||
9007 | if (e->components.back().type == target_symbol::comp_pretty_print) | |
9008 | { | |
9009 | if (lvalue) | |
b530b5b3 | 9010 | throw semantic_error(_("cannot write to pretty-printed variable"), e->tok); |
5f36109e | 9011 | |
d19a9a82 | 9012 | dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e); |
5f36109e JS |
9013 | dpp.expand()->visit (this); |
9014 | return; | |
9015 | } | |
9016 | ||
f8a968bc JS |
9017 | // Synthesize a function to dereference the dwarf fields, |
9018 | // with a pointer parameter that is the base tracepoint variable | |
9019 | functiondecl *fdecl = new functiondecl; | |
59de45f1 | 9020 | fdecl->synthetic = true; |
f8a968bc JS |
9021 | fdecl->tok = e->tok; |
9022 | embeddedcode *ec = new embeddedcode; | |
9023 | ec->tok = e->tok; | |
75ead1f7 | 9024 | |
f8a968bc | 9025 | string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get") |
cc9001af | 9026 | + "_" + e->sym_name() |
aca66a36 | 9027 | + "_" + lex_cast(tick++)); |
75ead1f7 | 9028 | |
f8a968bc JS |
9029 | fdecl->name = fname; |
9030 | fdecl->body = ec; | |
75ead1f7 | 9031 | |
a5ce5211 | 9032 | ec->code += EMBEDDED_FETCH_DEREF(false); |
c69a87e0 | 9033 | ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e, |
f8a968bc | 9034 | lvalue, fdecl->type); |
75ead1f7 | 9035 | |
f8a968bc JS |
9036 | // Give the fdecl an argument for the raw tracepoint value |
9037 | vardecl *v1 = new vardecl; | |
9038 | v1->type = pe_long; | |
9039 | v1->name = "pointer"; | |
9040 | v1->tok = e->tok; | |
9041 | fdecl->formal_args.push_back(v1); | |
75ead1f7 | 9042 | |
6fda2dff JS |
9043 | // Any non-literal indexes need to be passed in too. |
9044 | for (unsigned i = 0; i < e->components.size(); ++i) | |
9045 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
9046 | { | |
9047 | vardecl *v = new vardecl; | |
9048 | v->type = pe_long; | |
aca66a36 | 9049 | v->name = "index" + lex_cast(i); |
6fda2dff JS |
9050 | v->tok = e->tok; |
9051 | fdecl->formal_args.push_back(v); | |
9052 | } | |
9053 | ||
f8a968bc JS |
9054 | if (lvalue) |
9055 | { | |
9056 | // Modify the fdecl so it carries a pe_long formal | |
9057 | // argument called "value". | |
75ead1f7 | 9058 | |
f8a968bc JS |
9059 | // FIXME: For the time being we only support setting target |
9060 | // variables which have base types; these are 'pe_long' in | |
9061 | // stap's type vocabulary. Strings and pointers might be | |
9062 | // reasonable, some day, but not today. | |
9063 | ||
9064 | vardecl *v2 = new vardecl; | |
9065 | v2->type = pe_long; | |
9066 | v2->name = "value"; | |
9067 | v2->tok = e->tok; | |
9068 | fdecl->formal_args.push_back(v2); | |
9069 | } | |
9070 | else | |
9071 | ec->code += "/* pure */"; | |
9072 | ||
64211010 | 9073 | ec->code += "/* unprivileged */"; |
a5ce5211 | 9074 | ec->code += EMBEDDED_FETCH_DEREF_DONE; |
aff5d390 | 9075 | |
f8809d54 | 9076 | fdecl->join (dw.sess); |
75ead1f7 | 9077 | |
f8a968bc JS |
9078 | // Synthesize a functioncall. |
9079 | functioncall* n = new functioncall; | |
9080 | n->tok = e->tok; | |
9081 | n->function = fname; | |
6fda2dff JS |
9082 | n->args.push_back(require(e2)); |
9083 | ||
9084 | // Any non-literal indexes need to be passed in too. | |
9085 | for (unsigned i = 0; i < e->components.size(); ++i) | |
9086 | if (e->components[i].type == target_symbol::comp_expression_array_index) | |
9087 | n->args.push_back(require(e->components[i].expr_index)); | |
75ead1f7 | 9088 | |
f8a968bc JS |
9089 | if (lvalue) |
9090 | { | |
9091 | // Provide the functioncall to our parent, so that it can be | |
9092 | // used to substitute for the assignment node immediately above | |
9093 | // us. | |
9094 | assert(!target_symbol_setter_functioncalls.empty()); | |
9095 | *(target_symbol_setter_functioncalls.top()) = n; | |
9096 | } | |
75ead1f7 | 9097 | |
f8a968bc JS |
9098 | provide (n); |
9099 | } | |
75ead1f7 JS |
9100 | } |
9101 | ||
9102 | ||
f8a968bc JS |
9103 | void |
9104 | tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e) | |
0a6f5a3f | 9105 | { |
03c75a4a | 9106 | if (e->addressof) |
b530b5b3 | 9107 | throw semantic_error(_("cannot take address of context variable"), e->tok); |
03c75a4a | 9108 | |
f8a968bc | 9109 | if (is_active_lvalue (e)) |
b530b5b3 | 9110 | throw semantic_error(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok); |
0a6f5a3f | 9111 | |
277c21bc | 9112 | if (e->name == "$$name") |
f8a968bc | 9113 | { |
5f36109e JS |
9114 | e->assert_no_components("tracepoint"); |
9115 | ||
bfdaad1e DS |
9116 | // Synthesize an embedded expression. |
9117 | embedded_expr *expr = new embedded_expr; | |
9118 | expr->tok = e->tok; | |
9119 | expr->code = string("/* string */ /* pure */ ") | |
6dceb5c9 | 9120 | + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\""); |
bfdaad1e | 9121 | provide (expr); |
f8a968bc | 9122 | } |
277c21bc | 9123 | else if (e->name == "$$vars" || e->name == "$$parms") |
f8a968bc | 9124 | { |
5f36109e JS |
9125 | e->assert_no_components("tracepoint", true); |
9126 | ||
f8a968bc JS |
9127 | token* pf_tok = new token(*e->tok); |
9128 | pf_tok->content = "sprintf"; | |
0a6f5a3f | 9129 | |
d5e178c1 | 9130 | print_format* pf = print_format::create(pf_tok); |
0a6f5a3f | 9131 | |
f8a968bc | 9132 | for (unsigned i = 0; i < args.size(); ++i) |
b278033a | 9133 | { |
dcaa1a65 JS |
9134 | if (!args[i].usable) |
9135 | continue; | |
f8a968bc JS |
9136 | if (i > 0) |
9137 | pf->raw_components += " "; | |
9138 | pf->raw_components += args[i].name; | |
3e3bd7b6 | 9139 | target_symbol *tsym = new target_symbol; |
f8a968bc | 9140 | tsym->tok = e->tok; |
277c21bc | 9141 | tsym->name = "$" + args[i].name; |
5f36109e | 9142 | tsym->components = e->components; |
b278033a | 9143 | |
f8a968bc JS |
9144 | // every variable should always be accessible! |
9145 | tsym->saved_conversion_error = 0; | |
9146 | expression *texp = require (tsym); // NB: throws nothing ... | |
14a97852 JS |
9147 | if (tsym->saved_conversion_error) // ... but this is how we know it happened. |
9148 | { | |
9149 | if (dw.sess.verbose>2) | |
e26c2f83 | 9150 | for (const semantic_error *c = tsym->saved_conversion_error; |
14a97852 | 9151 | c != 0; c = c->chain) |
b530b5b3 | 9152 | clog << _("variable location problem: ") << c->what() << endl; |
14a97852 JS |
9153 | pf->raw_components += "=?"; |
9154 | continue; | |
9155 | } | |
b278033a | 9156 | |
5f36109e JS |
9157 | if (!e->components.empty() && |
9158 | e->components[0].type == target_symbol::comp_pretty_print) | |
9159 | pf->raw_components += "=%s"; | |
9160 | else | |
9161 | pf->raw_components += args[i].isptr ? "=%p" : "=%#x"; | |
f8a968bc JS |
9162 | pf->args.push_back(texp); |
9163 | } | |
0a6f5a3f | 9164 | |
f8a968bc JS |
9165 | pf->components = print_format::string_to_components(pf->raw_components); |
9166 | provide (pf); | |
b278033a | 9167 | } |
f8a968bc JS |
9168 | else |
9169 | assert(0); // shouldn't get here | |
0a6f5a3f JS |
9170 | } |
9171 | ||
0a6f5a3f | 9172 | void |
f8a968bc | 9173 | tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e) |
0a6f5a3f | 9174 | { |
aff5d390 | 9175 | try |
c69a87e0 | 9176 | { |
8fa5f271 MW |
9177 | assert(e->name.size() > 0 |
9178 | && ((e->name[0] == '$' && e->target_name == "") | |
9179 | || (e->name == "@var" && e->target_name != ""))); | |
aff5d390 | 9180 | |
277c21bc | 9181 | if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars") |
c69a87e0 | 9182 | visit_target_symbol_context (e); |
8fa5f271 MW |
9183 | else if (e->name == "@var") |
9184 | throw semantic_error(_("cannot use @var DWARF variables in tracepoints"), e->tok); | |
c69a87e0 FCE |
9185 | else |
9186 | visit_target_symbol_arg (e); | |
9187 | } | |
9188 | catch (const semantic_error &er) | |
9189 | { | |
1af1e62d | 9190 | e->chain (er); |
c69a87e0 FCE |
9191 | provide (e); |
9192 | } | |
0a6f5a3f JS |
9193 | } |
9194 | ||
9195 | ||
9196 | ||
79189b84 JS |
9197 | tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s, |
9198 | dwflpp& dw, Dwarf_Die& func_die, | |
9199 | const string& tracepoint_name, | |
9200 | probe* base, probe_point* loc): | |
4c5d1300 | 9201 | derived_probe (base, loc, true /* .components soon rewritten */), |
79189b84 | 9202 | sess (s), tracepoint_name (tracepoint_name) |
56894e91 | 9203 | { |
79189b84 JS |
9204 | // create synthetic probe point name; preserve condition |
9205 | vector<probe_point::component*> comps; | |
9206 | comps.push_back (new probe_point::component (TOK_KERNEL)); | |
9207 | comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name))); | |
9208 | this->sole_location()->components = comps; | |
9209 | ||
6fb70fb7 JS |
9210 | // fill out the available arguments in this tracepoint |
9211 | build_args(dw, func_die); | |
56894e91 | 9212 | |
6fb70fb7 JS |
9213 | // determine which header defined this tracepoint |
9214 | string decl_file = dwarf_decl_file(&func_die); | |
d4393459 FCE |
9215 | header = decl_file; |
9216 | ||
9217 | #if 0 /* This convention is not enforced. */ | |
6fb70fb7 JS |
9218 | size_t header_pos = decl_file.rfind("trace/"); |
9219 | if (header_pos == string::npos) | |
9220 | throw semantic_error ("cannot parse header location for tracepoint '" | |
9221 | + tracepoint_name + "' in '" | |
9222 | + decl_file + "'"); | |
9223 | header = decl_file.substr(header_pos); | |
d4393459 | 9224 | #endif |
56894e91 | 9225 | |
6fb70fb7 JS |
9226 | // tracepoints from FOO_event_types.h should really be included from FOO.h |
9227 | // XXX can dwarf tell us the include hierarchy? it would be better to | |
9228 | // ... walk up to see which one was directly included by tracequery.c | |
3c1b3d06 | 9229 | // XXX: see also PR9993. |
d4393459 | 9230 | size_t header_pos = header.find("_event_types"); |
6fb70fb7 JS |
9231 | if (header_pos != string::npos) |
9232 | header.erase(header_pos, 12); | |
56894e91 | 9233 | |
f8a968bc JS |
9234 | // Now expand the local variables in the probe body |
9235 | tracepoint_var_expanding_visitor v (dw, name, args); | |
8b095b45 | 9236 | v.replace (this->body); |
a45664f4 JS |
9237 | for (unsigned i = 0; i < args.size(); i++) |
9238 | if (args[i].used) | |
9239 | { | |
9240 | vardecl* v = new vardecl; | |
9241 | v->name = "__tracepoint_arg_" + args[i].name; | |
9242 | v->tok = this->tok; | |
58701b78 | 9243 | v->set_arity(0, this->tok); |
a45664f4 | 9244 | v->type = pe_long; |
69aa668e | 9245 | v->synthetic = true; |
a45664f4 JS |
9246 | this->locals.push_back (v); |
9247 | } | |
56894e91 | 9248 | |
79189b84 | 9249 | if (sess.verbose > 2) |
ce0f6648 | 9250 | clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl; |
79189b84 | 9251 | } |
dc38c0ae | 9252 | |
56894e91 | 9253 | |
f8a968bc | 9254 | static bool |
dcaa1a65 | 9255 | resolve_tracepoint_arg_type(tracepoint_arg& arg) |
46b84a80 | 9256 | { |
d19a9a82 | 9257 | Dwarf_Die type; |
dcaa1a65 | 9258 | switch (dwarf_tag(&arg.type_die)) |
b20febf3 | 9259 | { |
f8a968bc JS |
9260 | case DW_TAG_typedef: |
9261 | case DW_TAG_const_type: | |
9262 | case DW_TAG_volatile_type: | |
9263 | // iterate on the referent type | |
3d1ad340 | 9264 | return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die) |
dcaa1a65 | 9265 | && resolve_tracepoint_arg_type(arg)); |
f8a968bc | 9266 | case DW_TAG_base_type: |
a52d2ac0 | 9267 | case DW_TAG_enumeration_type: |
f8a968bc | 9268 | // base types will simply be treated as script longs |
dcaa1a65 | 9269 | arg.isptr = false; |
f8a968bc JS |
9270 | return true; |
9271 | case DW_TAG_pointer_type: | |
dcaa1a65 JS |
9272 | // pointers can be treated as script longs, |
9273 | // and if we know their type, they can also be dereferenced | |
d19a9a82 JS |
9274 | type = arg.type_die; |
9275 | while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)) | |
9276 | { | |
9277 | // It still might be a non-type, e.g. const void, | |
9278 | // so we need to strip away all qualifiers. | |
9279 | int tag = dwarf_tag(&arg.type_die); | |
9280 | if (tag != DW_TAG_typedef && | |
9281 | tag != DW_TAG_const_type && | |
9282 | tag != DW_TAG_volatile_type) | |
9283 | { | |
9284 | arg.isptr = true; | |
9285 | break; | |
9286 | } | |
9287 | } | |
9288 | if (!arg.isptr) | |
9289 | arg.type_die = type; | |
ad370dcc JS |
9290 | arg.typecast = "(intptr_t)"; |
9291 | return true; | |
9292 | case DW_TAG_structure_type: | |
9293 | case DW_TAG_union_type: | |
9294 | // for structs/unions which are passed by value, we turn it into | |
9295 | // a pointer that can be dereferenced. | |
9296 | arg.isptr = true; | |
9297 | arg.typecast = "(intptr_t)&"; | |
dcaa1a65 | 9298 | return true; |
f8a968bc JS |
9299 | default: |
9300 | // should we consider other types too? | |
9301 | return false; | |
b20febf3 | 9302 | } |
56894e91 JS |
9303 | } |
9304 | ||
9305 | ||
9306 | void | |
822a6a3d | 9307 | tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die) |
56894e91 | 9308 | { |
6fb70fb7 JS |
9309 | Dwarf_Die arg; |
9310 | if (dwarf_child(&func_die, &arg) == 0) | |
9311 | do | |
9312 | if (dwarf_tag(&arg) == DW_TAG_formal_parameter) | |
9313 | { | |
9314 | // build a tracepoint_arg for this parameter | |
9315 | tracepoint_arg tparg; | |
23d106b9 | 9316 | tparg.name = dwarf_diename(&arg); |
56894e91 | 9317 | |
6fb70fb7 | 9318 | // read the type of this parameter |
3d1ad340 | 9319 | if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die) |
f1c8f8a5 | 9320 | || !dwarf_type_name(&tparg.type_die, tparg.c_type)) |
ce0f6648 LB |
9321 | throw semantic_error (_F("cannot get type of parameter '%s' of tracepoint '%s'", |
9322 | tparg.name.c_str(), tracepoint_name.c_str())); | |
a68f81a2 | 9323 | |
dcaa1a65 | 9324 | tparg.usable = resolve_tracepoint_arg_type(tparg); |
6fb70fb7 JS |
9325 | args.push_back(tparg); |
9326 | if (sess.verbose > 4) | |
a52d2ac0 JS |
9327 | clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s", |
9328 | tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(), | |
9329 | tparg.usable ? "ok" : "unavailable") << endl; | |
6fb70fb7 JS |
9330 | } |
9331 | while (dwarf_siblingof(&arg, &arg) == 0); | |
56894e91 JS |
9332 | } |
9333 | ||
dc38c0ae | 9334 | void |
d0bfd2ac | 9335 | tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const |
dc38c0ae | 9336 | { |
dcaa1a65 JS |
9337 | for (unsigned i = 0; i < args.size(); ++i) |
9338 | if (args[i].usable) | |
d0bfd2ac | 9339 | arg_set.push_back("$"+args[i].name+":"+args[i].c_type); |
dc38c0ae DS |
9340 | } |
9341 | ||
79189b84 JS |
9342 | void |
9343 | tracepoint_derived_probe::join_group (systemtap_session& s) | |
197a4d62 | 9344 | { |
79189b84 JS |
9345 | if (! s.tracepoint_derived_probes) |
9346 | s.tracepoint_derived_probes = new tracepoint_derived_probe_group (); | |
9347 | s.tracepoint_derived_probes->enroll (this); | |
9348 | } | |
e38d6504 | 9349 | |
56894e91 | 9350 | |
197a4d62 | 9351 | void |
3e3bd7b6 | 9352 | tracepoint_derived_probe::print_dupe_stamp(ostream& o) |
56894e91 | 9353 | { |
3e3bd7b6 JS |
9354 | for (unsigned i = 0; i < args.size(); i++) |
9355 | if (args[i].used) | |
9356 | o << "__tracepoint_arg_" << args[i].name << endl; | |
197a4d62 | 9357 | } |
56894e91 | 9358 | |
3e3bd7b6 | 9359 | |
c9ccb642 | 9360 | static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header) |
47dd066d | 9361 | { |
3c1b3d06 FCE |
9362 | vector<string> they_live; |
9363 | // PR 9993 | |
9364 | // XXX: may need this to be configurable | |
d4393459 | 9365 | they_live.push_back ("#include <linux/skbuff.h>"); |
9e0cd21a FCE |
9366 | |
9367 | // PR11649: conditional extra header | |
9368 | // for kvm tracepoints in 2.6.33ish | |
9369 | if (s.kernel_config["CONFIG_KVM"] != string("")) { | |
d4393459 FCE |
9370 | they_live.push_back ("#include <linux/kvm_host.h>"); |
9371 | } | |
9372 | ||
50b72692 | 9373 | if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) { |
d4393459 | 9374 | they_live.push_back ("#define XFS_BIG_BLKNOS 1"); |
88637c31 FCE |
9375 | if (s.kernel_source_tree != "") |
9376 | they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree | |
d4393459 FCE |
9377 | they_live.push_back ("struct xfs_mount;"); |
9378 | they_live.push_back ("struct xfs_inode;"); | |
9379 | they_live.push_back ("struct xfs_buf;"); | |
9380 | they_live.push_back ("struct xfs_bmbt_irec;"); | |
c2cf1b87 | 9381 | they_live.push_back ("struct xfs_trans;"); |
9e0cd21a | 9382 | } |
d4393459 | 9383 | |
50b72692 | 9384 | if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) { |
d4393459 FCE |
9385 | they_live.push_back ("struct rpc_task;"); |
9386 | } | |
b64d65e2 FCE |
9387 | // RHEL6.3 |
9388 | if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) { | |
9389 | they_live.push_back ("struct rpc_clnt;"); | |
9390 | they_live.push_back ("struct rpc_wait_queue;"); | |
9391 | } | |
d4393459 FCE |
9392 | |
9393 | they_live.push_back ("#include <asm/cputime.h>"); | |
9394 | ||
c2cf1b87 FCE |
9395 | // linux 3.0 |
9396 | they_live.push_back ("struct cpu_workqueue_struct;"); | |
9397 | ||
50b72692 | 9398 | if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string("")) |
c2cf1b87 FCE |
9399 | if (s.kernel_source_tree != "") |
9400 | they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree | |
9401 | ||
50b72692 HP |
9402 | if (header.find("ext3") != string::npos && s.kernel_config["CONFIG_EXT3_FS"] != string("")) |
9403 | they_live.push_back ("struct ext3_reserve_window_node;"); | |
9404 | ||
3c1b3d06 FCE |
9405 | return they_live; |
9406 | } | |
47dd066d WC |
9407 | |
9408 | ||
9409 | void | |
79189b84 | 9410 | tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s) |
47dd066d | 9411 | { |
79189b84 JS |
9412 | if (probes.empty()) |
9413 | return; | |
47dd066d | 9414 | |
96b030fe JS |
9415 | s.op->newline() << "/* ---- tracepoint probes ---- */"; |
9416 | s.op->newline(); | |
79189b84 | 9417 | |
47dd066d | 9418 | |
a4b9c3b3 FCE |
9419 | // We create a MODULE_aux_N.c file for each tracepoint header, to allow them |
9420 | // to be separately compiled. That's because kernel tracepoint headers sometimes | |
9421 | // conflict. PR13155. | |
9422 | ||
9423 | map<string,translator_output*> per_header_aux; | |
9424 | // GC NB: the translator_output* structs are owned/retained by the systemtap_session. | |
47dd066d | 9425 | |
6fb70fb7 JS |
9426 | for (unsigned i = 0; i < probes.size(); ++i) |
9427 | { | |
9428 | tracepoint_derived_probe *p = probes[i]; | |
75ae2ec9 | 9429 | string header = p->header; |
5f73a260 | 9430 | |
a4b9c3b3 FCE |
9431 | // We cache the auxiliary output files on a per-header basis. We don't |
9432 | // need one aux file per tracepoint, only one per tracepoint-header. | |
9433 | translator_output *tpop = per_header_aux[header]; | |
9434 | if (tpop == 0) | |
9435 | { | |
9436 | tpop = s.op_create_auxiliary(); | |
9437 | per_header_aux[header] = tpop; | |
9438 | ||
9439 | // PR9993: Add extra headers to work around undeclared types in individual | |
9440 | // include/trace/foo.h files | |
9441 | const vector<string>& extra_decls = tracepoint_extra_decls (s, header); | |
9442 | for (unsigned z=0; z<extra_decls.size(); z++) | |
9443 | tpop->newline() << extra_decls[z] << "\n"; | |
720c435f | 9444 | |
a4b9c3b3 FCE |
9445 | // strip include/ substring, the same way as done in get_tracequery_module() |
9446 | size_t root_pos = header.rfind("include/"); | |
9447 | header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header); | |
bfffa443 FCE |
9448 | |
9449 | tpop->newline() << "#include <linux/tracepoint.h>" << endl; | |
a4b9c3b3 | 9450 | tpop->newline() << "#include <" << header << ">"; |
720c435f | 9451 | |
a4b9c3b3 FCE |
9452 | // Starting in 2.6.35, at the same time NOARGS was added, the callback |
9453 | // always has a void* as the first parameter. PR11599 | |
9454 | tpop->newline() << "#ifdef DECLARE_TRACE_NOARGS"; | |
9455 | tpop->newline() << "#define STAP_TP_DATA , NULL"; | |
9456 | tpop->newline() << "#define STAP_TP_PROTO void *cb_data" | |
9457 | << " __attribute__ ((unused))"; | |
9458 | if (!p->args.empty()) | |
9459 | tpop->line() << ","; | |
9460 | tpop->newline() << "#else"; | |
9461 | tpop->newline() << "#define STAP_TP_DATA"; | |
9462 | tpop->newline() << "#define STAP_TP_PROTO"; | |
9463 | if (p->args.empty()) | |
9464 | tpop->line() << " void"; | |
9465 | tpop->newline() << "#endif"; | |
720c435f JS |
9466 | |
9467 | tpop->newline() << "#define intptr_t long"; | |
a4b9c3b3 FCE |
9468 | } |
9469 | ||
720c435f JS |
9470 | // collect the args that are actually in use |
9471 | vector<const tracepoint_arg*> used_args; | |
6fb70fb7 | 9472 | for (unsigned j = 0; j < p->args.size(); ++j) |
720c435f JS |
9473 | if (p->args[j].used) |
9474 | used_args.push_back(&p->args[j]); | |
9475 | ||
a4b9c3b3 FCE |
9476 | // forward-declare the generated-side tracepoint callback |
9477 | tpop->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
9478 | tpop->indent(2); |
9479 | if (used_args.empty()) | |
9480 | tpop->line() << "void"; | |
9481 | for (unsigned j = 0; j < used_args.size(); ++j) | |
6fb70fb7 JS |
9482 | { |
9483 | if (j > 0) | |
a4b9c3b3 FCE |
9484 | tpop->line() << ", "; |
9485 | tpop->line() << "int64_t"; | |
6fb70fb7 | 9486 | } |
720c435f JS |
9487 | tpop->line() << ");"; |
9488 | tpop->indent(-2); | |
5f73a260 | 9489 | |
a4b9c3b3 FCE |
9490 | // define the generated-side tracepoint callback - in the main translator-output |
9491 | s.op->newline() << "void enter_real_tracepoint_probe_" << i << "("; | |
720c435f JS |
9492 | s.op->indent(2); |
9493 | if (used_args.empty()) | |
9494 | s.op->newline() << "void"; | |
9495 | for (unsigned j = 0; j < used_args.size(); ++j) | |
a4b9c3b3 | 9496 | { |
a4b9c3b3 FCE |
9497 | if (j > 0) |
9498 | s.op->line() << ", "; | |
720c435f | 9499 | s.op->newline() << "int64_t __tracepoint_arg_" << used_args[j]->name; |
6fb70fb7 | 9500 | } |
5f73a260 JS |
9501 | s.op->newline() << ")"; |
9502 | s.op->newline(-2) << "{"; | |
26e63673 | 9503 | s.op->newline(1) << "struct stap_probe * const probe = " |
faea5e16 | 9504 | << common_probe_init (p) << ";"; |
71db462b | 9505 | common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe", |
cda141c2 | 9506 | "stp_probe_type_tracepoint"); |
6dceb5c9 | 9507 | s.op->newline() << "c->ips.tracepoint_name = " |
c12d974f FCE |
9508 | << lex_cast_qstring (p->tracepoint_name) |
9509 | << ";"; | |
720c435f JS |
9510 | for (unsigned j = 0; j < used_args.size(); ++j) |
9511 | { | |
9512 | s.op->newline() << "c->probe_locals." << p->name | |
0386bbcf | 9513 | << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name) |
720c435f JS |
9514 | << " = __tracepoint_arg_" << used_args[j]->name << ";"; |
9515 | } | |
26e63673 | 9516 | s.op->newline() << "(*probe->ph) (c);"; |
f887a8c9 | 9517 | common_probe_entryfn_epilogue (s, true); |
6fb70fb7 | 9518 | s.op->newline(-1) << "}"; |
47dd066d | 9519 | |
a4b9c3b3 | 9520 | // define the real tracepoint callback function |
720c435f | 9521 | tpop->newline() << "static void enter_tracepoint_probe_" << i << "("; |
a4b9c3b3 FCE |
9522 | tpop->newline(2) << "STAP_TP_PROTO"; |
9523 | for (unsigned j = 0; j < p->args.size(); ++j) | |
9524 | { | |
9525 | if (j > 0) | |
9526 | tpop->line() << ", "; | |
9527 | tpop->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name; | |
9528 | } | |
9529 | tpop->newline() << ")"; | |
9530 | tpop->newline(-2) << "{"; | |
720c435f JS |
9531 | tpop->newline(1) << "enter_real_tracepoint_probe_" << i << "("; |
9532 | tpop->indent(2); | |
9533 | for (unsigned j = 0; j < used_args.size(); ++j) | |
9534 | { | |
9535 | if (j > 0) | |
9536 | tpop->line() << ", "; | |
9537 | tpop->newline() << "(int64_t)" << used_args[j]->typecast | |
9538 | << "__tracepoint_arg_" << used_args[j]->name; | |
9539 | } | |
9540 | tpop->newline() << ");"; | |
9541 | tpop->newline(-3) << "}"; | |
a4b9c3b3 FCE |
9542 | |
9543 | ||
96b030fe | 9544 | // emit normalized registration functions |
720c435f | 9545 | tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 9546 | tpop->newline(1) << "return register_trace_" << p->tracepoint_name |
5f73a260 | 9547 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 | 9548 | tpop->newline(-1) << "}"; |
47dd066d | 9549 | |
86758d5f JS |
9550 | // NB: we're not prepared to deal with unreg failures. However, failures |
9551 | // can only occur if the tracepoint doesn't exist (yet?), or if we | |
9552 | // weren't even registered. The former should be OKed by the initial | |
9553 | // registration call, and the latter is safe to ignore. | |
720c435f | 9554 | tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {"; |
a4b9c3b3 | 9555 | tpop->newline(1) << "(void) unregister_trace_" << p->tracepoint_name |
5f73a260 | 9556 | << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);"; |
a4b9c3b3 FCE |
9557 | tpop->newline(-1) << "}"; |
9558 | tpop->newline(); | |
5f73a260 | 9559 | |
720c435f JS |
9560 | // declare normalized registration functions |
9561 | s.op->newline() << "int register_tracepoint_probe_" << i << "(void);"; | |
9562 | s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);"; | |
5f73a260 | 9563 | |
a4b9c3b3 | 9564 | tpop->assert_0_indent(); |
af304783 DS |
9565 | } |
9566 | ||
96b030fe JS |
9567 | // emit an array of registration functions for easy init/shutdown |
9568 | s.op->newline() << "static struct stap_tracepoint_probe {"; | |
9569 | s.op->newline(1) << "int (*reg)(void);"; | |
86758d5f | 9570 | s.op->newline(0) << "void (*unreg)(void);"; |
96b030fe JS |
9571 | s.op->newline(-1) << "} stap_tracepoint_probes[] = {"; |
9572 | s.op->indent(1); | |
9573 | for (unsigned i = 0; i < probes.size(); ++i) | |
9574 | { | |
9575 | s.op->newline () << "{"; | |
9576 | s.op->line() << " .reg=®ister_tracepoint_probe_" << i << ","; | |
9577 | s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i; | |
9578 | s.op->line() << " },"; | |
9579 | } | |
9580 | s.op->newline(-1) << "};"; | |
9581 | s.op->newline(); | |
47dd066d WC |
9582 | } |
9583 | ||
9584 | ||
79189b84 JS |
9585 | void |
9586 | tracepoint_derived_probe_group::emit_module_init (systemtap_session &s) | |
47dd066d | 9587 | { |
79189b84 JS |
9588 | if (probes.size () == 0) |
9589 | return; | |
47dd066d | 9590 | |
79189b84 | 9591 | s.op->newline() << "/* init tracepoint probes */"; |
96b030fe JS |
9592 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {"; |
9593 | s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();"; | |
9594 | s.op->newline() << "if (rc) {"; | |
9595 | s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback | |
9596 | s.op->newline(1) << "stap_tracepoint_probes[j].unreg();"; | |
9597 | s.op->newline(-1) << "break;"; // don't attempt to register any more probes | |
9598 | s.op->newline(-1) << "}"; | |
9599 | s.op->newline(-1) << "}"; | |
47dd066d | 9600 | |
bc9a523d FCE |
9601 | // This would be technically proper (on those autoconf-detectable |
9602 | // kernels that include this function in tracepoint.h), however we | |
9603 | // already make several calls to synchronze_sched() during our | |
9604 | // shutdown processes. | |
47dd066d | 9605 | |
bc9a523d FCE |
9606 | // s.op->newline() << "if (rc)"; |
9607 | // s.op->newline(1) << "tracepoint_synchronize_unregister();"; | |
9608 | // s.op->indent(-1); | |
79189b84 | 9609 | } |
47dd066d WC |
9610 | |
9611 | ||
79189b84 JS |
9612 | void |
9613 | tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s) | |
47dd066d | 9614 | { |
79189b84 JS |
9615 | if (probes.empty()) |
9616 | return; | |
47dd066d | 9617 | |
96b030fe JS |
9618 | s.op->newline() << "/* deregister tracepoint probes */"; |
9619 | s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)"; | |
9620 | s.op->newline(1) << "stap_tracepoint_probes[i].unreg();"; | |
9621 | s.op->indent(-1); | |
47dd066d | 9622 | |
bc9a523d | 9623 | // Not necessary: see above. |
47dd066d | 9624 | |
bc9a523d | 9625 | // s.op->newline() << "tracepoint_synchronize_unregister();"; |
79189b84 | 9626 | } |
b20febf3 | 9627 | |
47dd066d | 9628 | |
75ead1f7 | 9629 | struct tracepoint_query : public base_query |
47dd066d | 9630 | { |
75ead1f7 JS |
9631 | tracepoint_query(dwflpp & dw, const string & tracepoint, |
9632 | probe * base_probe, probe_point * base_loc, | |
9633 | vector<derived_probe *> & results): | |
9634 | base_query(dw, "*"), tracepoint(tracepoint), | |
9635 | base_probe(base_probe), base_loc(base_loc), | |
9636 | results(results) {} | |
47dd066d | 9637 | |
75ead1f7 | 9638 | const string& tracepoint; |
47dd066d | 9639 | |
75ead1f7 JS |
9640 | probe * base_probe; |
9641 | probe_point * base_loc; | |
9642 | vector<derived_probe *> & results; | |
f982c59b | 9643 | set<string> probed_names; |
47dd066d | 9644 | |
75ead1f7 JS |
9645 | void handle_query_module(); |
9646 | int handle_query_cu(Dwarf_Die * cudie); | |
9647 | int handle_query_func(Dwarf_Die * func); | |
822a6a3d | 9648 | void query_library (const char *) {} |
576eaefe | 9649 | void query_plt (const char *entry, size_t addr) {} |
b20febf3 | 9650 | |
75ead1f7 JS |
9651 | static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg); |
9652 | static int tracepoint_query_func (Dwarf_Die * func, base_query * query); | |
9653 | }; | |
47dd066d WC |
9654 | |
9655 | ||
9656 | void | |
75ead1f7 | 9657 | tracepoint_query::handle_query_module() |
47dd066d | 9658 | { |
75ead1f7 | 9659 | // look for the tracepoints in each CU |
337b7c44 | 9660 | dw.iterate_over_cus(tracepoint_query_cu, this, false); |
47dd066d WC |
9661 | } |
9662 | ||
9663 | ||
75ead1f7 JS |
9664 | int |
9665 | tracepoint_query::handle_query_cu(Dwarf_Die * cudie) | |
47dd066d | 9666 | { |
75ead1f7 | 9667 | dw.focus_on_cu (cudie); |
47dd066d | 9668 | |
75ead1f7 JS |
9669 | // look at each function to see if it's a tracepoint |
9670 | string function = "stapprobe_" + tracepoint; | |
9671 | return dw.iterate_over_functions (tracepoint_query_func, this, function); | |
47dd066d WC |
9672 | } |
9673 | ||
9674 | ||
75ead1f7 JS |
9675 | int |
9676 | tracepoint_query::handle_query_func(Dwarf_Die * func) | |
47dd066d | 9677 | { |
75ead1f7 | 9678 | dw.focus_on_function (func); |
47dd066d | 9679 | |
60d98537 | 9680 | assert(startswith(dw.function_name, "stapprobe_")); |
75ead1f7 | 9681 | string tracepoint_instance = dw.function_name.substr(10); |
f982c59b JS |
9682 | |
9683 | // check for duplicates -- sometimes tracepoint headers may be indirectly | |
9684 | // included in more than one of our tracequery modules. | |
9685 | if (!probed_names.insert(tracepoint_instance).second) | |
9686 | return DWARF_CB_OK; | |
9687 | ||
79189b84 JS |
9688 | derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func, |
9689 | tracepoint_instance, | |
9690 | base_probe, base_loc); | |
9691 | results.push_back (dp); | |
75ead1f7 | 9692 | return DWARF_CB_OK; |
47dd066d WC |
9693 | } |
9694 | ||
9695 | ||
75ead1f7 JS |
9696 | int |
9697 | tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg) | |
47dd066d | 9698 | { |
75ead1f7 | 9699 | tracepoint_query * q = static_cast<tracepoint_query *>(arg); |
85007c04 | 9700 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 9701 | return q->handle_query_cu(cudie); |
47dd066d WC |
9702 | } |
9703 | ||
9704 | ||
75ead1f7 JS |
9705 | int |
9706 | tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query) | |
47dd066d | 9707 | { |
75ead1f7 | 9708 | tracepoint_query * q = static_cast<tracepoint_query *>(query); |
85007c04 | 9709 | if (pending_interrupts) return DWARF_CB_ABORT; |
75ead1f7 | 9710 | return q->handle_query_func(func); |
47dd066d WC |
9711 | } |
9712 | ||
9713 | ||
0a6f5a3f | 9714 | struct tracepoint_builder: public derived_probe_builder |
47dd066d | 9715 | { |
0a6f5a3f JS |
9716 | private: |
9717 | dwflpp *dw; | |
9718 | bool init_dw(systemtap_session& s); | |
c9ccb642 FCE |
9719 | void get_tracequery_modules(systemtap_session& s, |
9720 | const vector<string>& headers, | |
9721 | vector<string>& modules); | |
47dd066d | 9722 | |
0a6f5a3f | 9723 | public: |
47dd066d | 9724 | |
0a6f5a3f JS |
9725 | tracepoint_builder(): dw(0) {} |
9726 | ~tracepoint_builder() { delete dw; } | |
47dd066d | 9727 | |
0a6f5a3f JS |
9728 | void build_no_more (systemtap_session& s) |
9729 | { | |
9730 | if (dw && s.verbose > 3) | |
b530b5b3 | 9731 | clog << _("tracepoint_builder releasing dwflpp") << endl; |
0a6f5a3f JS |
9732 | delete dw; |
9733 | dw = NULL; | |
435f53a7 FCE |
9734 | |
9735 | delete_session_module_cache (s); | |
0a6f5a3f | 9736 | } |
47dd066d | 9737 | |
0a6f5a3f JS |
9738 | void build(systemtap_session& s, |
9739 | probe *base, probe_point *location, | |
9740 | literal_map_t const& parameters, | |
9741 | vector<derived_probe*>& finished_results); | |
9742 | }; | |
47dd066d | 9743 | |
47dd066d | 9744 | |
c9ccb642 | 9745 | |
2a0e62a8 | 9746 | // Create (or cache) one or more tracequery .o modules, based upon the |
c9ccb642 FCE |
9747 | // tracepoint-related header files given. Return the generated or cached |
9748 | // modules[]. | |
9749 | ||
9750 | void | |
9751 | tracepoint_builder::get_tracequery_modules(systemtap_session& s, | |
9752 | const vector<string>& headers, | |
9753 | vector<string>& modules) | |
0a6f5a3f | 9754 | { |
c95eddf7 | 9755 | if (s.verbose > 2) |
55e50c24 | 9756 | { |
ce0f6648 | 9757 | clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl; |
55e50c24 JS |
9758 | for (size_t i = 0; i < headers.size(); ++i) |
9759 | clog << " " << headers[i] << endl; | |
9760 | } | |
c95eddf7 | 9761 | |
2a0e62a8 JS |
9762 | map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name |
9763 | // Map the headers to cache .o names. Note that this has side-effects of | |
c9ccb642 FCE |
9764 | // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file, |
9765 | // so we prefer not to repeat this. | |
9766 | vector<string> uncached_headers; | |
9767 | for (size_t i=0; i<headers.size(); i++) | |
2a0e62a8 | 9768 | headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]); |
c9ccb642 FCE |
9769 | |
9770 | // They may be in the cache already. | |
9771 | if (s.use_cache && !s.poison_cache) | |
9772 | for (size_t i=0; i<headers.size(); i++) | |
9773 | { | |
9774 | // see if the cached module exists | |
2a0e62a8 | 9775 | const string& tracequery_path = headers_cache_obj[headers[i]]; |
c9ccb642 FCE |
9776 | if (!tracequery_path.empty() && file_exists(tracequery_path)) |
9777 | { | |
9778 | if (s.verbose > 2) | |
9779 | clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl; | |
47dd066d | 9780 | |
c252fca2 JS |
9781 | // an empty file is a cached failure |
9782 | if (get_file_size(tracequery_path) > 0) | |
9783 | modules.push_back (tracequery_path); | |
c9ccb642 FCE |
9784 | } |
9785 | else | |
9786 | uncached_headers.push_back(headers[i]); | |
9787 | } | |
9788 | else | |
9789 | uncached_headers = headers; | |
f982c59b | 9790 | |
c9ccb642 FCE |
9791 | // If we have nothing left to search for, quit |
9792 | if (uncached_headers.empty()) return; | |
55e50c24 | 9793 | |
c9ccb642 | 9794 | map<string,string> headers_tracequery_src; // header -> C-source code mapping |
55e50c24 | 9795 | |
c9ccb642 FCE |
9796 | // We could query several subsets of headers[] to make this go |
9797 | // faster, but let's KISS and do one at a time. | |
9798 | for (size_t i=0; i<uncached_headers.size(); i++) | |
55e50c24 | 9799 | { |
c9ccb642 FCE |
9800 | const string& header = uncached_headers[i]; |
9801 | ||
9802 | // create a tracequery source file | |
9803 | ostringstream osrc; | |
9804 | ||
9805 | // PR9993: Add extra headers to work around undeclared types in individual | |
9806 | // include/trace/foo.h files | |
9807 | vector<string> short_decls = tracepoint_extra_decls(s, header); | |
9808 | ||
9809 | // add each requested tracepoint header | |
75ae2ec9 | 9810 | size_t root_pos = header.rfind("include/"); |
832f100d | 9811 | short_decls.push_back(string("#include <") + |
75ae2ec9 | 9812 | ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) + |
d4393459 | 9813 | string(">")); |
f982c59b | 9814 | |
c9ccb642 FCE |
9815 | osrc << "#ifdef CONFIG_TRACEPOINTS" << endl; |
9816 | osrc << "#include <linux/tracepoint.h>" << endl; | |
832f100d JS |
9817 | |
9818 | // the kernel has changed this naming a few times, previously TPPROTO, | |
9819 | // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest. | |
9820 | osrc << "#ifndef PARAMS" << endl; | |
9821 | osrc << "#define PARAMS(args...) args" << endl; | |
9822 | osrc << "#endif" << endl; | |
9823 | ||
c9ccb642 FCE |
9824 | // override DECLARE_TRACE to synthesize probe functions for us |
9825 | osrc << "#undef DECLARE_TRACE" << endl; | |
9826 | osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl; | |
9827 | osrc << " void stapprobe_##name(proto) {}" << endl; | |
832f100d | 9828 | |
c9ccb642 FCE |
9829 | // 2.6.35 added the NOARGS variant, but it's the same for us |
9830 | osrc << "#undef DECLARE_TRACE_NOARGS" << endl; | |
9831 | osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl; | |
9832 | osrc << " DECLARE_TRACE(name, void, )" << endl; | |
832f100d JS |
9833 | |
9834 | // 2.6.38 added the CONDITION variant, which can also just redirect | |
9835 | osrc << "#undef DECLARE_TRACE_CONDITION" << endl; | |
9836 | osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl; | |
9837 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; | |
9838 | ||
c9ccb642 FCE |
9839 | // older tracepoints used DEFINE_TRACE, so redirect that too |
9840 | osrc << "#undef DEFINE_TRACE" << endl; | |
9841 | osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl; | |
832f100d JS |
9842 | osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl; |
9843 | ||
c9ccb642 FCE |
9844 | // add the specified decls/#includes |
9845 | for (unsigned z=0; z<short_decls.size(); z++) | |
9846 | osrc << "#undef TRACE_INCLUDE_FILE\n" | |
9847 | << "#undef TRACE_INCLUDE_PATH\n" | |
9848 | << short_decls[z] << "\n"; | |
9849 | ||
9850 | // finish up the module source | |
9851 | osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl; | |
47dd066d | 9852 | |
c9ccb642 FCE |
9853 | // save the source file away |
9854 | headers_tracequery_src[header] = osrc.str(); | |
55e50c24 | 9855 | } |
f982c59b | 9856 | |
c9ccb642 | 9857 | // now build them all together |
2a0e62a8 | 9858 | map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src); |
47dd066d | 9859 | |
c9ccb642 | 9860 | // now plop them into the cache |
b278033a | 9861 | if (s.use_cache) |
c9ccb642 FCE |
9862 | for (size_t i=0; i<uncached_headers.size(); i++) |
9863 | { | |
9864 | const string& header = uncached_headers[i]; | |
2a0e62a8 JS |
9865 | const string& tracequery_obj = tracequery_objs[header]; |
9866 | const string& tracequery_path = headers_cache_obj[header]; | |
9867 | if (tracequery_obj !="" && file_exists(tracequery_obj)) | |
c9ccb642 | 9868 | { |
2a0e62a8 | 9869 | copy_file(tracequery_obj, tracequery_path, s.verbose > 2); |
c9ccb642 FCE |
9870 | modules.push_back (tracequery_path); |
9871 | } | |
c252fca2 JS |
9872 | else |
9873 | // cache an empty file for failures | |
9874 | copy_file("/dev/null", tracequery_path, s.verbose > 2); | |
c9ccb642 | 9875 | } |
f982c59b JS |
9876 | } |
9877 | ||
9878 | ||
d4393459 | 9879 | |
f982c59b JS |
9880 | bool |
9881 | tracepoint_builder::init_dw(systemtap_session& s) | |
9882 | { | |
9883 | if (dw != NULL) | |
9884 | return true; | |
9885 | ||
9886 | vector<string> tracequery_modules; | |
55e50c24 | 9887 | vector<string> system_headers; |
f982c59b JS |
9888 | |
9889 | glob_t trace_glob; | |
d4393459 FCE |
9890 | |
9891 | // find kernel_source_tree | |
9892 | if (s.kernel_source_tree == "") | |
f982c59b | 9893 | { |
d4393459 FCE |
9894 | unsigned found; |
9895 | DwflPtr dwfl_ptr = setup_dwfl_kernel ("kernel", &found, s); | |
9896 | Dwfl *dwfl = dwfl_ptr.get()->dwfl; | |
9897 | if (found) | |
9898 | { | |
9899 | Dwarf_Die *cudie = 0; | |
9900 | Dwarf_Addr bias; | |
9901 | while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL) | |
9902 | { | |
e19ebcf7 | 9903 | assert_no_interrupts(); |
d4393459 FCE |
9904 | Dwarf_Attribute attr; |
9905 | const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr)); | |
9906 | if (name) | |
9907 | { | |
61f1a63b | 9908 | if (s.verbose > 2) |
b530b5b3 | 9909 | clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl; |
61f1a63b | 9910 | |
d4393459 FCE |
9911 | s.kernel_source_tree = name; |
9912 | break; // skip others; modern Kbuild uses same comp_dir for them all | |
9913 | } | |
9914 | } | |
9915 | } | |
9916 | } | |
9917 | ||
9918 | // prefixes | |
9919 | vector<string> glob_prefixes; | |
9920 | glob_prefixes.push_back (s.kernel_build_tree); | |
9921 | if (s.kernel_source_tree != "") | |
9922 | glob_prefixes.push_back (s.kernel_source_tree); | |
9923 | ||
9924 | // suffixes | |
9925 | vector<string> glob_suffixes; | |
9926 | glob_suffixes.push_back("include/trace/events/*.h"); | |
9927 | glob_suffixes.push_back("include/trace/*.h"); | |
9928 | glob_suffixes.push_back("arch/x86/kvm/*trace.h"); | |
f89ff3e2 | 9929 | glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h"); |
ca51c2cd | 9930 | glob_suffixes.push_back("fs/xfs/xfs_trace*.h"); |
d4393459 FCE |
9931 | |
9932 | // compute cartesian product | |
9933 | vector<string> globs; | |
9934 | for (unsigned i=0; i<glob_prefixes.size(); i++) | |
9935 | for (unsigned j=0; j<glob_suffixes.size(); j++) | |
9936 | globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]); | |
9937 | ||
8aa43b8d | 9938 | set<string> duped_headers; |
d4393459 FCE |
9939 | for (unsigned z = 0; z < globs.size(); z++) |
9940 | { | |
9941 | string glob_str = globs[z]; | |
9942 | if (s.verbose > 3) | |
b530b5b3 | 9943 | clog << _("Checking tracepoint glob ") << glob_str << endl; |
d4393459 | 9944 | |
067cc66f CM |
9945 | int r = glob(glob_str.c_str(), 0, NULL, &trace_glob); |
9946 | if (r == GLOB_NOSPACE || r == GLOB_ABORTED) | |
9947 | throw runtime_error("Error globbing tracepoint"); | |
9948 | ||
f982c59b JS |
9949 | for (unsigned i = 0; i < trace_glob.gl_pathc; ++i) |
9950 | { | |
9951 | string header(trace_glob.gl_pathv[i]); | |
9952 | ||
9953 | // filter out a few known "internal-only" headers | |
60d98537 JS |
9954 | if (endswith(header, "/define_trace.h") || |
9955 | endswith(header, "/ftrace.h") || | |
9956 | endswith(header, "/trace_events.h") || | |
9957 | endswith(header, "_event_types.h")) | |
f982c59b JS |
9958 | continue; |
9959 | ||
8aa43b8d JS |
9960 | // skip identical headers from the build and source trees. |
9961 | size_t root_pos = header.rfind("include/"); | |
9962 | if (root_pos != string::npos && | |
9963 | !duped_headers.insert(header.substr(root_pos + 8)).second) | |
9964 | continue; | |
9965 | ||
55e50c24 | 9966 | system_headers.push_back(header); |
f982c59b JS |
9967 | } |
9968 | globfree(&trace_glob); | |
9969 | } | |
9970 | ||
c9ccb642 FCE |
9971 | // Build tracequery modules |
9972 | get_tracequery_modules(s, system_headers, tracequery_modules); | |
55e50c24 | 9973 | |
f982c59b JS |
9974 | // TODO: consider other sources of tracepoint headers too, like from |
9975 | // a command-line parameter or some environment or .systemtaprc | |
47dd066d | 9976 | |
59c11f91 | 9977 | dw = new dwflpp(s, tracequery_modules, true); |
0a6f5a3f JS |
9978 | return true; |
9979 | } | |
47dd066d | 9980 | |
0a6f5a3f JS |
9981 | void |
9982 | tracepoint_builder::build(systemtap_session& s, | |
9983 | probe *base, probe_point *location, | |
9984 | literal_map_t const& parameters, | |
9985 | vector<derived_probe*>& finished_results) | |
9986 | { | |
9987 | if (!init_dw(s)) | |
9988 | return; | |
47dd066d | 9989 | |
75ead1f7 JS |
9990 | string tracepoint; |
9991 | assert(get_param (parameters, TOK_TRACE, tracepoint)); | |
47dd066d | 9992 | |
75ead1f7 | 9993 | tracepoint_query q(*dw, tracepoint, base, location, finished_results); |
51178501 | 9994 | dw->iterate_over_modules(&query_module, &q); |
47dd066d | 9995 | } |
47dd066d | 9996 | |
e6fe60e7 | 9997 | |
b55bc428 | 9998 | // ------------------------------------------------------------------------ |
bd2b1e68 | 9999 | // Standard tapset registry. |
b55bc428 FCE |
10000 | // ------------------------------------------------------------------------ |
10001 | ||
7a053d3b | 10002 | void |
f8220a7b | 10003 | register_standard_tapsets(systemtap_session & s) |
b55bc428 | 10004 | { |
47e0478e | 10005 | register_tapset_been(s); |
93646f4d | 10006 | register_tapset_itrace(s); |
dd0e4fa7 | 10007 | register_tapset_mark(s); |
7a212aa8 | 10008 | register_tapset_procfs(s); |
912e8c59 | 10009 | register_tapset_timers(s); |
8d9609f5 | 10010 | register_tapset_netfilter(s); |
b84779a5 | 10011 | register_tapset_utrace(s); |
b98a8d73 | 10012 | |
7a24d422 | 10013 | // dwarf-based kprobe/uprobe parts |
c4ce66a1 | 10014 | dwarf_derived_probe::register_patterns(s); |
30a279be | 10015 | |
888af770 FCE |
10016 | // XXX: user-space starter set |
10017 | s.pattern_root->bind_num(TOK_PROCESS) | |
10018 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE) | |
f66bb29a | 10019 | ->bind_privilege(pr_all) |
888af770 FCE |
10020 | ->bind(new uprobe_builder ()); |
10021 | s.pattern_root->bind_num(TOK_PROCESS) | |
10022 | ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN) | |
f66bb29a | 10023 | ->bind_privilege(pr_all) |
888af770 FCE |
10024 | ->bind(new uprobe_builder ()); |
10025 | ||
0a6f5a3f JS |
10026 | // kernel tracepoint probes |
10027 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE) | |
10028 | ->bind(new tracepoint_builder()); | |
10029 | ||
e6fe60e7 AM |
10030 | // Kprobe based probe |
10031 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION) | |
10032 | ->bind(new kprobe_builder()); | |
10033 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) | |
10034 | ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder()); | |
10035 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
10036 | ->bind(new kprobe_builder()); | |
b6371390 JS |
10037 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) |
10038 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10039 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10040 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder()); | |
b6371390 JS |
10041 | s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE) |
10042 | ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN) | |
10043 | ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder()); | |
e6fe60e7 AM |
10044 | s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT) |
10045 | ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder()); | |
dd225250 PS |
10046 | |
10047 | //Hwbkpt based probe | |
b47f3a55 FCE |
10048 | // NB: we formerly registered the probe point types only if the kernel configuration |
10049 | // allowed it. However, we get better error messages if we allow probes to resolve. | |
10050 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10051 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10052 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10053 | ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10054 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10055 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10056 | s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT) | |
10057 | ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10058 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10059 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder()); | |
10060 | s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT) | |
10061 | ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder()); | |
10062 | // length supported with address only, not symbol names | |
83ea76b1 WC |
10063 | |
10064 | //perf event based probe | |
4763f713 | 10065 | register_tapset_perf(s); |
b55bc428 | 10066 | } |
dc38c0ae DS |
10067 | |
10068 | ||
b20febf3 FCE |
10069 | vector<derived_probe_group*> |
10070 | all_session_groups(systemtap_session& s) | |
dc38c0ae | 10071 | { |
b20febf3 | 10072 | vector<derived_probe_group*> g; |
912e8c59 JS |
10073 | |
10074 | #define DOONE(x) \ | |
10075 | if (s. x##_derived_probes) \ | |
10076 | g.push_back ((derived_probe_group*)(s. x##_derived_probes)) | |
ab655cf8 DS |
10077 | |
10078 | // Note that order *is* important here. We want to make sure we | |
10079 | // register (actually run) begin probes before any other probe type | |
10080 | // is run. Similarly, when unregistering probes, we want to | |
10081 | // unregister (actually run) end probes after every other probe type | |
10082 | // has be unregistered. To do the latter, | |
10083 | // c_unparser::emit_module_exit() will run this list backwards. | |
b20febf3 FCE |
10084 | DOONE(be); |
10085 | DOONE(dwarf); | |
888af770 | 10086 | DOONE(uprobe); |
b20febf3 FCE |
10087 | DOONE(timer); |
10088 | DOONE(profile); | |
10089 | DOONE(mark); | |
0a6f5a3f | 10090 | DOONE(tracepoint); |
e6fe60e7 | 10091 | DOONE(kprobe); |
dd225250 | 10092 | DOONE(hwbkpt); |
83ea76b1 | 10093 | DOONE(perf); |
b20febf3 | 10094 | DOONE(hrtimer); |
ce82316f | 10095 | DOONE(procfs); |
8d9609f5 | 10096 | DOONE(netfilter); |
935447c8 DS |
10097 | |
10098 | // Another "order is important" item. We want to make sure we | |
10099 | // "register" the dummy task_finder probe group after all probe | |
10100 | // groups that use the task_finder. | |
10101 | DOONE(utrace); | |
a96d1db0 | 10102 | DOONE(itrace); |
935447c8 | 10103 | DOONE(task_finder); |
b20febf3 FCE |
10104 | #undef DOONE |
10105 | return g; | |
46b84a80 | 10106 | } |
73267b89 JS |
10107 | |
10108 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |